2 * drivers/pci/pci-driver.c
4 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
7 * Released under the GPL v2 only.
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/mempolicy.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
22 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
26 struct list_head node;
27 struct pci_device_id id;
33 * store_new_id - add a new PCI device ID to this driver and re-probe devices
34 * @driver: target device driver
35 * @buf: buffer for scanning device ID data
38 * Adds a new dynamic pci device ID to this driver,
39 * and causes the driver to probe for all devices again.
42 store_new_id(struct device_driver *driver, const char *buf, size_t count)
44 struct pci_dynid *dynid;
45 struct pci_driver *pdrv = to_pci_driver(driver);
46 __u32 vendor, device, subvendor=PCI_ANY_ID,
47 subdevice=PCI_ANY_ID, class=0, class_mask=0;
48 unsigned long driver_data=0;
52 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
53 &vendor, &device, &subvendor, &subdevice,
54 &class, &class_mask, &driver_data);
58 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
62 dynid->id.vendor = vendor;
63 dynid->id.device = device;
64 dynid->id.subvendor = subvendor;
65 dynid->id.subdevice = subdevice;
66 dynid->id.class = class;
67 dynid->id.class_mask = class_mask;
68 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
71 spin_lock(&pdrv->dynids.lock);
72 list_add_tail(&dynid->node, &pdrv->dynids.list);
73 spin_unlock(&pdrv->dynids.lock);
75 if (get_driver(&pdrv->driver)) {
76 retval = driver_attach(&pdrv->driver);
77 put_driver(&pdrv->driver);
84 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
87 pci_free_dynids(struct pci_driver *drv)
89 struct pci_dynid *dynid, *n;
91 spin_lock(&drv->dynids.lock);
92 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
93 list_del(&dynid->node);
96 spin_unlock(&drv->dynids.lock);
100 pci_create_newid_file(struct pci_driver *drv)
103 if (drv->probe != NULL)
104 error = driver_create_file(&drv->driver, &driver_attr_new_id);
108 static void pci_remove_newid_file(struct pci_driver *drv)
110 driver_remove_file(&drv->driver, &driver_attr_new_id);
112 #else /* !CONFIG_HOTPLUG */
113 static inline void pci_free_dynids(struct pci_driver *drv) {}
114 static inline int pci_create_newid_file(struct pci_driver *drv)
118 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
122 * pci_match_id - See if a pci device matches a given pci_id table
123 * @ids: array of PCI device id structures to search in
124 * @dev: the PCI device structure to match against.
126 * Used by a driver to check whether a PCI device present in the
127 * system is in its list of supported devices. Returns the matching
128 * pci_device_id structure or %NULL if there is no match.
130 * Deprecated, don't use this as it will not catch any dynamic ids
131 * that a driver might want to check for.
133 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
137 while (ids->vendor || ids->subvendor || ids->class_mask) {
138 if (pci_match_one_device(ids, dev))
147 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
148 * @drv: the PCI driver to match against
149 * @dev: the PCI device structure to match against
151 * Used by a driver to check whether a PCI device present in the
152 * system is in its list of supported devices. Returns the matching
153 * pci_device_id structure or %NULL if there is no match.
155 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
158 struct pci_dynid *dynid;
160 /* Look at the dynamic ids first, before the static ones */
161 spin_lock(&drv->dynids.lock);
162 list_for_each_entry(dynid, &drv->dynids.list, node) {
163 if (pci_match_one_device(&dynid->id, dev)) {
164 spin_unlock(&drv->dynids.lock);
168 spin_unlock(&drv->dynids.lock);
170 return pci_match_id(drv->id_table, dev);
173 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
174 const struct pci_device_id *id)
178 /* Execute driver initialization on node where the
179 device's bus is attached to. This way the driver likely
180 allocates its local memory on the right node without
181 any need to change it. */
182 struct mempolicy *oldpol;
183 cpumask_t oldmask = current->cpus_allowed;
184 int node = dev_to_node(&dev->dev);
187 node_to_cpumask_ptr(nodecpumask, node);
188 set_cpus_allowed_ptr(current, nodecpumask);
190 /* And set default memory allocation policy */
191 oldpol = current->mempolicy;
192 current->mempolicy = NULL; /* fall back to system default policy */
194 error = drv->probe(dev, id);
196 set_cpus_allowed_ptr(current, &oldmask);
197 current->mempolicy = oldpol;
203 * __pci_device_probe()
204 * @drv: driver to call to check if it wants the PCI device
205 * @pci_dev: PCI device being probed
207 * returns 0 on success, else error.
208 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
211 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
213 const struct pci_device_id *id;
216 if (!pci_dev->driver && drv->probe) {
219 id = pci_match_device(drv, pci_dev);
221 error = pci_call_probe(drv, pci_dev, id);
223 pci_dev->driver = drv;
230 static int pci_device_probe(struct device * dev)
233 struct pci_driver *drv;
234 struct pci_dev *pci_dev;
236 drv = to_pci_driver(dev->driver);
237 pci_dev = to_pci_dev(dev);
238 pci_dev_get(pci_dev);
239 error = __pci_device_probe(drv, pci_dev);
241 pci_dev_put(pci_dev);
246 static int pci_device_remove(struct device * dev)
248 struct pci_dev * pci_dev = to_pci_dev(dev);
249 struct pci_driver * drv = pci_dev->driver;
253 drv->remove(pci_dev);
254 pci_dev->driver = NULL;
258 * If the device is still on, set the power state as "unknown",
259 * since it might change by the next time we load the driver.
261 if (pci_dev->current_state == PCI_D0)
262 pci_dev->current_state = PCI_UNKNOWN;
265 * We would love to complain here if pci_dev->is_enabled is set, that
266 * the driver should have called pci_disable_device(), but the
267 * unfortunate fact is there are too many odd BIOS and bridge setups
268 * that don't like drivers doing that all of the time.
269 * Oh well, we can dream of sane hardware when we sleep, no matter how
270 * horrible the crap we have to deal with is when we are awake...
273 pci_dev_put(pci_dev);
277 static void pci_device_shutdown(struct device *dev)
279 struct pci_dev *pci_dev = to_pci_dev(dev);
280 struct pci_driver *drv = pci_dev->driver;
282 if (drv && drv->shutdown)
283 drv->shutdown(pci_dev);
284 pci_msi_shutdown(pci_dev);
285 pci_msix_shutdown(pci_dev);
288 #ifdef CONFIG_PM_SLEEP
291 * Default "suspend" method for devices that have no driver provided suspend,
292 * or not even a driver at all.
294 static void pci_default_pm_suspend(struct pci_dev *pci_dev)
296 pci_save_state(pci_dev);
298 * mark its power state as "unknown", since we don't know if
299 * e.g. the BIOS will change its device state when we suspend.
301 if (pci_dev->current_state == PCI_D0)
302 pci_dev->current_state = PCI_UNKNOWN;
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_pm_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);
318 * if the device was busmaster before the suspend, make it busmaster
321 if (pci_dev->is_busmaster)
322 pci_set_master(pci_dev);
327 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
329 struct pci_dev * pci_dev = to_pci_dev(dev);
330 struct pci_driver * drv = pci_dev->driver;
333 if (drv && drv->suspend) {
334 i = drv->suspend(pci_dev, state);
335 suspend_report_result(drv->suspend, i);
337 pci_default_pm_suspend(pci_dev);
342 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
344 struct pci_dev * pci_dev = to_pci_dev(dev);
345 struct pci_driver * drv = pci_dev->driver;
348 if (drv && drv->suspend_late) {
349 i = drv->suspend_late(pci_dev, state);
350 suspend_report_result(drv->suspend_late, i);
355 static int pci_legacy_resume(struct device *dev)
358 struct pci_dev * pci_dev = to_pci_dev(dev);
359 struct pci_driver * drv = pci_dev->driver;
361 if (drv && drv->resume)
362 error = drv->resume(pci_dev);
364 error = pci_default_pm_resume(pci_dev);
368 static int pci_legacy_resume_early(struct device *dev)
371 struct pci_dev * pci_dev = to_pci_dev(dev);
372 struct pci_driver * drv = pci_dev->driver;
374 if (drv && drv->resume_early)
375 error = drv->resume_early(pci_dev);
379 static int pci_pm_prepare(struct device *dev)
381 struct device_driver *drv = dev->driver;
384 if (drv && drv->pm && drv->pm->prepare)
385 error = drv->pm->prepare(dev);
390 static void pci_pm_complete(struct device *dev)
392 struct device_driver *drv = dev->driver;
394 if (drv && drv->pm && drv->pm->complete)
395 drv->pm->complete(dev);
398 #ifdef CONFIG_SUSPEND
400 static int pci_pm_suspend(struct device *dev)
402 struct pci_dev *pci_dev = to_pci_dev(dev);
403 struct device_driver *drv = dev->driver;
406 if (drv && drv->pm) {
407 if (drv->pm->suspend) {
408 error = drv->pm->suspend(dev);
409 suspend_report_result(drv->pm->suspend, error);
411 pci_default_pm_suspend(pci_dev);
414 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
416 pci_fixup_device(pci_fixup_suspend, pci_dev);
421 static int pci_pm_suspend_noirq(struct device *dev)
423 struct pci_dev *pci_dev = to_pci_dev(dev);
424 struct pci_driver *drv = pci_dev->driver;
427 if (drv && drv->pm) {
428 if (drv->pm->suspend_noirq) {
429 error = drv->pm->suspend_noirq(dev);
430 suspend_report_result(drv->pm->suspend_noirq, error);
433 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
439 static int pci_pm_resume(struct device *dev)
441 struct pci_dev *pci_dev = to_pci_dev(dev);
442 struct device_driver *drv = dev->driver;
445 pci_fixup_device(pci_fixup_resume, pci_dev);
447 if (drv && drv->pm) {
448 error = drv->pm->resume ? drv->pm->resume(dev) :
449 pci_default_pm_resume(pci_dev);
451 error = pci_legacy_resume(dev);
457 static int pci_pm_resume_noirq(struct device *dev)
459 struct pci_dev *pci_dev = to_pci_dev(dev);
460 struct pci_driver *drv = pci_dev->driver;
463 pci_fixup_device(pci_fixup_resume_early, pci_dev);
465 if (drv && drv->pm) {
466 if (drv->pm->resume_noirq)
467 error = drv->pm->resume_noirq(dev);
469 error = pci_legacy_resume_early(dev);
475 #else /* !CONFIG_SUSPEND */
477 #define pci_pm_suspend NULL
478 #define pci_pm_suspend_noirq NULL
479 #define pci_pm_resume NULL
480 #define pci_pm_resume_noirq NULL
482 #endif /* !CONFIG_SUSPEND */
484 #ifdef CONFIG_HIBERNATION
486 static int pci_pm_freeze(struct device *dev)
488 struct pci_dev *pci_dev = to_pci_dev(dev);
489 struct device_driver *drv = dev->driver;
492 if (drv && drv->pm) {
493 if (drv->pm->freeze) {
494 error = drv->pm->freeze(dev);
495 suspend_report_result(drv->pm->freeze, error);
497 pci_default_pm_suspend(pci_dev);
500 error = pci_legacy_suspend(dev, PMSG_FREEZE);
501 pci_fixup_device(pci_fixup_suspend, pci_dev);
507 static int pci_pm_freeze_noirq(struct device *dev)
509 struct pci_dev *pci_dev = to_pci_dev(dev);
510 struct pci_driver *drv = pci_dev->driver;
513 if (drv && drv->pm) {
514 if (drv->pm->freeze_noirq) {
515 error = drv->pm->freeze_noirq(dev);
516 suspend_report_result(drv->pm->freeze_noirq, error);
519 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
525 static int pci_pm_thaw(struct device *dev)
527 struct device_driver *drv = dev->driver;
530 if (drv && drv->pm) {
532 error = drv->pm->thaw(dev);
534 pci_fixup_device(pci_fixup_resume, to_pci_dev(dev));
535 error = pci_legacy_resume(dev);
541 static int pci_pm_thaw_noirq(struct device *dev)
543 struct pci_dev *pci_dev = to_pci_dev(dev);
544 struct pci_driver *drv = pci_dev->driver;
547 if (drv && drv->pm) {
548 if (drv->pm->thaw_noirq)
549 error = drv->pm->thaw_noirq(dev);
551 pci_fixup_device(pci_fixup_resume_early, pci_dev);
552 error = pci_legacy_resume_early(dev);
558 static int pci_pm_poweroff(struct device *dev)
560 struct device_driver *drv = dev->driver;
563 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
565 if (drv && drv->pm) {
566 if (drv->pm->poweroff) {
567 error = drv->pm->poweroff(dev);
568 suspend_report_result(drv->pm->poweroff, error);
571 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
577 static int pci_pm_poweroff_noirq(struct device *dev)
579 struct pci_dev *pci_dev = to_pci_dev(dev);
580 struct pci_driver *drv = pci_dev->driver;
583 if (drv && drv->pm) {
584 if (drv->pm->poweroff_noirq) {
585 error = drv->pm->poweroff_noirq(dev);
586 suspend_report_result(drv->pm->poweroff_noirq, error);
589 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
595 static int pci_pm_restore(struct device *dev)
597 struct pci_dev *pci_dev = to_pci_dev(dev);
598 struct device_driver *drv = dev->driver;
601 if (drv && drv->pm) {
602 error = drv->pm->restore ? drv->pm->restore(dev) :
603 pci_default_pm_resume(pci_dev);
605 error = pci_legacy_resume(dev);
607 pci_fixup_device(pci_fixup_resume, pci_dev);
612 static int pci_pm_restore_noirq(struct device *dev)
614 struct pci_dev *pci_dev = to_pci_dev(dev);
615 struct pci_driver *drv = pci_dev->driver;
618 pci_fixup_device(pci_fixup_resume, pci_dev);
620 if (drv && drv->pm) {
621 if (drv->pm->restore_noirq)
622 error = drv->pm->restore_noirq(dev);
624 error = pci_legacy_resume_early(dev);
626 pci_fixup_device(pci_fixup_resume_early, pci_dev);
631 #else /* !CONFIG_HIBERNATION */
633 #define pci_pm_freeze NULL
634 #define pci_pm_freeze_noirq NULL
635 #define pci_pm_thaw NULL
636 #define pci_pm_thaw_noirq NULL
637 #define pci_pm_poweroff NULL
638 #define pci_pm_poweroff_noirq NULL
639 #define pci_pm_restore NULL
640 #define pci_pm_restore_noirq NULL
642 #endif /* !CONFIG_HIBERNATION */
644 struct pm_ext_ops pci_pm_ops = {
646 .prepare = pci_pm_prepare,
647 .complete = pci_pm_complete,
648 .suspend = pci_pm_suspend,
649 .resume = pci_pm_resume,
650 .freeze = pci_pm_freeze,
652 .poweroff = pci_pm_poweroff,
653 .restore = pci_pm_restore,
655 .suspend_noirq = pci_pm_suspend_noirq,
656 .resume_noirq = pci_pm_resume_noirq,
657 .freeze_noirq = pci_pm_freeze_noirq,
658 .thaw_noirq = pci_pm_thaw_noirq,
659 .poweroff_noirq = pci_pm_poweroff_noirq,
660 .restore_noirq = pci_pm_restore_noirq,
663 #define PCI_PM_OPS_PTR &pci_pm_ops
665 #else /* !CONFIG_PM_SLEEP */
667 #define PCI_PM_OPS_PTR NULL
669 #endif /* !CONFIG_PM_SLEEP */
672 * __pci_register_driver - register a new pci driver
673 * @drv: the driver structure to register
674 * @owner: owner module of drv
675 * @mod_name: module name string
677 * Adds the driver structure to the list of registered drivers.
678 * Returns a negative value on error, otherwise 0.
679 * If no error occurred, the driver remains registered even if
680 * no device was claimed during registration.
682 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
683 const char *mod_name)
687 /* initialize common driver fields */
688 drv->driver.name = drv->name;
689 drv->driver.bus = &pci_bus_type;
690 drv->driver.owner = owner;
691 drv->driver.mod_name = mod_name;
694 drv->driver.pm = &drv->pm->base;
696 spin_lock_init(&drv->dynids.lock);
697 INIT_LIST_HEAD(&drv->dynids.list);
699 /* register with core */
700 error = driver_register(&drv->driver);
704 error = pci_create_newid_file(drv);
706 driver_unregister(&drv->driver);
712 * pci_unregister_driver - unregister a pci driver
713 * @drv: the driver structure to unregister
715 * Deletes the driver structure from the list of registered PCI drivers,
716 * gives it a chance to clean up by calling its remove() function for
717 * each device it was responsible for, and marks those devices as
722 pci_unregister_driver(struct pci_driver *drv)
724 pci_remove_newid_file(drv);
725 driver_unregister(&drv->driver);
726 pci_free_dynids(drv);
729 static struct pci_driver pci_compat_driver = {
734 * pci_dev_driver - get the pci_driver of a device
735 * @dev: the device to query
737 * Returns the appropriate pci_driver structure or %NULL if there is no
738 * registered driver for the device.
741 pci_dev_driver(const struct pci_dev *dev)
747 for(i=0; i<=PCI_ROM_RESOURCE; i++)
748 if (dev->resource[i].flags & IORESOURCE_BUSY)
749 return &pci_compat_driver;
755 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
756 * @dev: the PCI device structure to match against
757 * @drv: the device driver to search for matching PCI device id structures
759 * Used by a driver to check whether a PCI device present in the
760 * system is in its list of supported devices. Returns the matching
761 * pci_device_id structure or %NULL if there is no match.
763 static int pci_bus_match(struct device *dev, struct device_driver *drv)
765 struct pci_dev *pci_dev = to_pci_dev(dev);
766 struct pci_driver *pci_drv = to_pci_driver(drv);
767 const struct pci_device_id *found_id;
769 found_id = pci_match_device(pci_drv, pci_dev);
777 * pci_dev_get - increments the reference count of the pci device structure
778 * @dev: the device being referenced
780 * Each live reference to a device should be refcounted.
782 * Drivers for PCI devices should normally record such references in
783 * their probe() methods, when they bind to a device, and release
784 * them by calling pci_dev_put(), in their disconnect() methods.
786 * A pointer to the device with the incremented reference counter is returned.
788 struct pci_dev *pci_dev_get(struct pci_dev *dev)
791 get_device(&dev->dev);
796 * pci_dev_put - release a use of the pci device structure
797 * @dev: device that's been disconnected
799 * Must be called when a user of a device is finished with it. When the last
800 * user of the device calls this function, the memory of the device is freed.
802 void pci_dev_put(struct pci_dev *dev)
805 put_device(&dev->dev);
808 #ifndef CONFIG_HOTPLUG
809 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
815 struct bus_type pci_bus_type = {
817 .match = pci_bus_match,
818 .uevent = pci_uevent,
819 .probe = pci_device_probe,
820 .remove = pci_device_remove,
821 .shutdown = pci_device_shutdown,
822 .dev_attrs = pci_dev_attrs,
823 .pm = PCI_PM_OPS_PTR,
826 static int __init pci_driver_init(void)
828 return bus_register(&pci_bus_type);
831 postcore_initcall(pci_driver_init);
833 EXPORT_SYMBOL(pci_match_id);
834 EXPORT_SYMBOL(__pci_register_driver);
835 EXPORT_SYMBOL(pci_unregister_driver);
836 EXPORT_SYMBOL(pci_dev_driver);
837 EXPORT_SYMBOL(pci_bus_type);
838 EXPORT_SYMBOL(pci_dev_get);
839 EXPORT_SYMBOL(pci_dev_put);