2 * drivers/pci/pci-driver.c
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/mempolicy.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
17 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
21 struct list_head node;
22 struct pci_device_id id;
28 * store_new_id - add a new PCI device ID to this driver and re-probe devices
29 * @driver: target device driver
30 * @buf: buffer for scanning device ID data
33 * Adds a new dynamic pci device ID to this driver,
34 * and causes the driver to probe for all devices again.
37 store_new_id(struct device_driver *driver, const char *buf, size_t count)
39 struct pci_dynid *dynid;
40 struct pci_driver *pdrv = to_pci_driver(driver);
41 __u32 vendor, device, subvendor=PCI_ANY_ID,
42 subdevice=PCI_ANY_ID, class=0, class_mask=0;
43 unsigned long driver_data=0;
47 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
48 &vendor, &device, &subvendor, &subdevice,
49 &class, &class_mask, &driver_data);
53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
57 dynid->id.vendor = vendor;
58 dynid->id.device = device;
59 dynid->id.subvendor = subvendor;
60 dynid->id.subdevice = subdevice;
61 dynid->id.class = class;
62 dynid->id.class_mask = class_mask;
63 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
66 spin_lock(&pdrv->dynids.lock);
67 list_add_tail(&dynid->node, &pdrv->dynids.list);
68 spin_unlock(&pdrv->dynids.lock);
70 if (get_driver(&pdrv->driver)) {
71 retval = driver_attach(&pdrv->driver);
72 put_driver(&pdrv->driver);
79 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
82 pci_free_dynids(struct pci_driver *drv)
84 struct pci_dynid *dynid, *n;
86 spin_lock(&drv->dynids.lock);
87 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
88 list_del(&dynid->node);
91 spin_unlock(&drv->dynids.lock);
95 pci_create_newid_file(struct pci_driver *drv)
98 if (drv->probe != NULL)
99 error = driver_create_file(&drv->driver, &driver_attr_new_id);
103 static void pci_remove_newid_file(struct pci_driver *drv)
105 driver_remove_file(&drv->driver, &driver_attr_new_id);
107 #else /* !CONFIG_HOTPLUG */
108 static inline void pci_free_dynids(struct pci_driver *drv) {}
109 static inline int pci_create_newid_file(struct pci_driver *drv)
113 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
117 * pci_match_id - See if a pci device matches a given pci_id table
118 * @ids: array of PCI device id structures to search in
119 * @dev: the PCI device structure to match against.
121 * Used by a driver to check whether a PCI device present in the
122 * system is in its list of supported devices. Returns the matching
123 * pci_device_id structure or %NULL if there is no match.
125 * Deprecated, don't use this as it will not catch any dynamic ids
126 * that a driver might want to check for.
128 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
132 while (ids->vendor || ids->subvendor || ids->class_mask) {
133 if (pci_match_one_device(ids, dev))
142 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
143 * @drv: the PCI driver to match against
144 * @dev: the PCI device structure to match against
146 * Used by a driver to check whether a PCI device present in the
147 * system is in its list of supported devices. Returns the matching
148 * pci_device_id structure or %NULL if there is no match.
150 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
153 struct pci_dynid *dynid;
155 /* Look at the dynamic ids first, before the static ones */
156 spin_lock(&drv->dynids.lock);
157 list_for_each_entry(dynid, &drv->dynids.list, node) {
158 if (pci_match_one_device(&dynid->id, dev)) {
159 spin_unlock(&drv->dynids.lock);
163 spin_unlock(&drv->dynids.lock);
165 return pci_match_id(drv->id_table, dev);
168 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
169 const struct pci_device_id *id)
173 /* Execute driver initialization on node where the
174 device's bus is attached to. This way the driver likely
175 allocates its local memory on the right node without
176 any need to change it. */
177 struct mempolicy *oldpol;
178 cpumask_t oldmask = current->cpus_allowed;
179 int node = pcibus_to_node(dev->bus);
180 if (node >= 0 && node_online(node))
181 set_cpus_allowed(current, node_to_cpumask(node));
182 /* And set default memory allocation policy */
183 oldpol = current->mempolicy;
184 current->mempolicy = &default_policy;
185 mpol_get(current->mempolicy);
187 error = drv->probe(dev, id);
189 set_cpus_allowed(current, oldmask);
190 mpol_free(current->mempolicy);
191 current->mempolicy = oldpol;
197 * __pci_device_probe()
198 * @drv: driver to call to check if it wants the PCI device
199 * @pci_dev: PCI device being probed
201 * returns 0 on success, else error.
202 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
205 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
207 const struct pci_device_id *id;
210 if (!pci_dev->driver && drv->probe) {
213 id = pci_match_device(drv, pci_dev);
215 error = pci_call_probe(drv, pci_dev, id);
217 pci_dev->driver = drv;
224 static int pci_device_probe(struct device * dev)
227 struct pci_driver *drv;
228 struct pci_dev *pci_dev;
230 drv = to_pci_driver(dev->driver);
231 pci_dev = to_pci_dev(dev);
232 pci_dev_get(pci_dev);
233 error = __pci_device_probe(drv, pci_dev);
235 pci_dev_put(pci_dev);
240 static int pci_device_remove(struct device * dev)
242 struct pci_dev * pci_dev = to_pci_dev(dev);
243 struct pci_driver * drv = pci_dev->driver;
247 drv->remove(pci_dev);
248 pci_dev->driver = NULL;
252 * If the device is still on, set the power state as "unknown",
253 * since it might change by the next time we load the driver.
255 if (pci_dev->current_state == PCI_D0)
256 pci_dev->current_state = PCI_UNKNOWN;
259 * We would love to complain here if pci_dev->is_enabled is set, that
260 * the driver should have called pci_disable_device(), but the
261 * unfortunate fact is there are too many odd BIOS and bridge setups
262 * that don't like drivers doing that all of the time.
263 * Oh well, we can dream of sane hardware when we sleep, no matter how
264 * horrible the crap we have to deal with is when we are awake...
267 pci_dev_put(pci_dev);
271 static int pci_device_suspend(struct device * dev, pm_message_t state)
273 struct pci_dev * pci_dev = to_pci_dev(dev);
274 struct pci_driver * drv = pci_dev->driver;
277 if (drv && drv->suspend) {
278 i = drv->suspend(pci_dev, state);
279 suspend_report_result(drv->suspend, i);
281 pci_save_state(pci_dev);
283 * mark its power state as "unknown", since we don't know if
284 * e.g. the BIOS will change its device state when we suspend.
286 if (pci_dev->current_state == PCI_D0)
287 pci_dev->current_state = PCI_UNKNOWN;
292 static int pci_device_suspend_late(struct device * dev, pm_message_t state)
294 struct pci_dev * pci_dev = to_pci_dev(dev);
295 struct pci_driver * drv = pci_dev->driver;
298 if (drv && drv->suspend_late) {
299 i = drv->suspend_late(pci_dev, state);
300 suspend_report_result(drv->suspend_late, i);
306 * Default resume method for devices that have no driver provided resume,
307 * or not even a driver at all.
309 static int pci_default_resume(struct pci_dev *pci_dev)
313 /* restore the PCI config space */
314 pci_restore_state(pci_dev);
315 /* if the device was enabled before suspend, reenable */
316 retval = pci_reenable_device(pci_dev);
317 /* if the device was busmaster before the suspend, make it busmaster again */
318 if (pci_dev->is_busmaster)
319 pci_set_master(pci_dev);
324 static int pci_device_resume(struct device * dev)
327 struct pci_dev * pci_dev = to_pci_dev(dev);
328 struct pci_driver * drv = pci_dev->driver;
330 if (drv && drv->resume)
331 error = drv->resume(pci_dev);
333 error = pci_default_resume(pci_dev);
337 static int pci_device_resume_early(struct device * dev)
340 struct pci_dev * pci_dev = to_pci_dev(dev);
341 struct pci_driver * drv = pci_dev->driver;
343 pci_fixup_device(pci_fixup_resume, pci_dev);
345 if (drv && drv->resume_early)
346 error = drv->resume_early(pci_dev);
350 static void pci_device_shutdown(struct device *dev)
352 struct pci_dev *pci_dev = to_pci_dev(dev);
353 struct pci_driver *drv = pci_dev->driver;
355 if (drv && drv->shutdown)
356 drv->shutdown(pci_dev);
359 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
360 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
363 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
365 struct device_driver *driver = kobj_to_pci_driver(kobj);
366 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
369 if (!get_driver(driver))
372 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
379 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
380 const char *buf, size_t count)
382 struct device_driver *driver = kobj_to_pci_driver(kobj);
383 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
386 if (!get_driver(driver))
389 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
395 static struct sysfs_ops pci_driver_sysfs_ops = {
396 .show = pci_driver_attr_show,
397 .store = pci_driver_attr_store,
399 static struct kobj_type pci_driver_kobj_type = {
400 .sysfs_ops = &pci_driver_sysfs_ops,
404 * __pci_register_driver - register a new pci driver
405 * @drv: the driver structure to register
406 * @owner: owner module of drv
407 * @mod_name: module name string
409 * Adds the driver structure to the list of registered drivers.
410 * Returns a negative value on error, otherwise 0.
411 * If no error occurred, the driver remains registered even if
412 * no device was claimed during registration.
414 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
415 const char *mod_name)
419 /* initialize common driver fields */
420 drv->driver.name = drv->name;
421 drv->driver.bus = &pci_bus_type;
422 drv->driver.owner = owner;
423 drv->driver.mod_name = mod_name;
424 drv->driver.kobj.ktype = &pci_driver_kobj_type;
426 spin_lock_init(&drv->dynids.lock);
427 INIT_LIST_HEAD(&drv->dynids.list);
429 /* register with core */
430 error = driver_register(&drv->driver);
434 error = pci_create_newid_file(drv);
436 driver_unregister(&drv->driver);
442 * pci_unregister_driver - unregister a pci driver
443 * @drv: the driver structure to unregister
445 * Deletes the driver structure from the list of registered PCI drivers,
446 * gives it a chance to clean up by calling its remove() function for
447 * each device it was responsible for, and marks those devices as
452 pci_unregister_driver(struct pci_driver *drv)
454 pci_remove_newid_file(drv);
455 driver_unregister(&drv->driver);
456 pci_free_dynids(drv);
459 static struct pci_driver pci_compat_driver = {
464 * pci_dev_driver - get the pci_driver of a device
465 * @dev: the device to query
467 * Returns the appropriate pci_driver structure or %NULL if there is no
468 * registered driver for the device.
471 pci_dev_driver(const struct pci_dev *dev)
477 for(i=0; i<=PCI_ROM_RESOURCE; i++)
478 if (dev->resource[i].flags & IORESOURCE_BUSY)
479 return &pci_compat_driver;
485 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
486 * @dev: the PCI device structure to match against
487 * @drv: the device driver to search for matching PCI device id structures
489 * Used by a driver to check whether a PCI device present in the
490 * system is in its list of supported devices. Returns the matching
491 * pci_device_id structure or %NULL if there is no match.
493 static int pci_bus_match(struct device *dev, struct device_driver *drv)
495 struct pci_dev *pci_dev = to_pci_dev(dev);
496 struct pci_driver *pci_drv = to_pci_driver(drv);
497 const struct pci_device_id *found_id;
499 found_id = pci_match_device(pci_drv, pci_dev);
507 * pci_dev_get - increments the reference count of the pci device structure
508 * @dev: the device being referenced
510 * Each live reference to a device should be refcounted.
512 * Drivers for PCI devices should normally record such references in
513 * their probe() methods, when they bind to a device, and release
514 * them by calling pci_dev_put(), in their disconnect() methods.
516 * A pointer to the device with the incremented reference counter is returned.
518 struct pci_dev *pci_dev_get(struct pci_dev *dev)
521 get_device(&dev->dev);
526 * pci_dev_put - release a use of the pci device structure
527 * @dev: device that's been disconnected
529 * Must be called when a user of a device is finished with it. When the last
530 * user of the device calls this function, the memory of the device is freed.
532 void pci_dev_put(struct pci_dev *dev)
535 put_device(&dev->dev);
538 #ifndef CONFIG_HOTPLUG
539 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
545 struct bus_type pci_bus_type = {
547 .match = pci_bus_match,
548 .uevent = pci_uevent,
549 .probe = pci_device_probe,
550 .remove = pci_device_remove,
551 .suspend = pci_device_suspend,
552 .suspend_late = pci_device_suspend_late,
553 .resume_early = pci_device_resume_early,
554 .resume = pci_device_resume,
555 .shutdown = pci_device_shutdown,
556 .dev_attrs = pci_dev_attrs,
559 static int __init pci_driver_init(void)
561 return bus_register(&pci_bus_type);
564 postcore_initcall(pci_driver_init);
566 EXPORT_SYMBOL(pci_match_id);
567 EXPORT_SYMBOL(__pci_register_driver);
568 EXPORT_SYMBOL(pci_unregister_driver);
569 EXPORT_SYMBOL(pci_dev_driver);
570 EXPORT_SYMBOL(pci_bus_type);
571 EXPORT_SYMBOL(pci_dev_get);
572 EXPORT_SYMBOL(pci_dev_put);