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 const struct pci_device_id *ids = pdrv->id_table;
47 __u32 vendor, device, subvendor=PCI_ANY_ID,
48 subdevice=PCI_ANY_ID, class=0, class_mask=0;
49 unsigned long driver_data=0;
53 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
54 &vendor, &device, &subvendor, &subdevice,
55 &class, &class_mask, &driver_data);
59 /* Only accept driver_data values that match an existing id_table
63 while (ids->vendor || ids->subvendor || ids->class_mask) {
64 if (driver_data == ids->driver_data) {
70 if (retval) /* No match */
74 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
78 dynid->id.vendor = vendor;
79 dynid->id.device = device;
80 dynid->id.subvendor = subvendor;
81 dynid->id.subdevice = subdevice;
82 dynid->id.class = class;
83 dynid->id.class_mask = class_mask;
84 dynid->id.driver_data = driver_data;
86 spin_lock(&pdrv->dynids.lock);
87 list_add_tail(&dynid->node, &pdrv->dynids.list);
88 spin_unlock(&pdrv->dynids.lock);
90 if (get_driver(&pdrv->driver)) {
91 retval = driver_attach(&pdrv->driver);
92 put_driver(&pdrv->driver);
99 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
102 pci_free_dynids(struct pci_driver *drv)
104 struct pci_dynid *dynid, *n;
106 spin_lock(&drv->dynids.lock);
107 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
108 list_del(&dynid->node);
111 spin_unlock(&drv->dynids.lock);
115 pci_create_newid_file(struct pci_driver *drv)
118 if (drv->probe != NULL)
119 error = driver_create_file(&drv->driver, &driver_attr_new_id);
123 static void pci_remove_newid_file(struct pci_driver *drv)
125 driver_remove_file(&drv->driver, &driver_attr_new_id);
127 #else /* !CONFIG_HOTPLUG */
128 static inline void pci_free_dynids(struct pci_driver *drv) {}
129 static inline int pci_create_newid_file(struct pci_driver *drv)
133 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
137 * pci_match_id - See if a pci device matches a given pci_id table
138 * @ids: array of PCI device id structures to search in
139 * @dev: the PCI device structure to match against.
141 * Used by a driver to check whether a PCI device present in the
142 * system is in its list of supported devices. Returns the matching
143 * pci_device_id structure or %NULL if there is no match.
145 * Deprecated, don't use this as it will not catch any dynamic ids
146 * that a driver might want to check for.
148 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
152 while (ids->vendor || ids->subvendor || ids->class_mask) {
153 if (pci_match_one_device(ids, dev))
162 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
163 * @drv: the PCI driver to match against
164 * @dev: the PCI device structure to match against
166 * Used by a driver to check whether a PCI device present in the
167 * system is in its list of supported devices. Returns the matching
168 * pci_device_id structure or %NULL if there is no match.
170 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
173 struct pci_dynid *dynid;
175 /* Look at the dynamic ids first, before the static ones */
176 spin_lock(&drv->dynids.lock);
177 list_for_each_entry(dynid, &drv->dynids.list, node) {
178 if (pci_match_one_device(&dynid->id, dev)) {
179 spin_unlock(&drv->dynids.lock);
183 spin_unlock(&drv->dynids.lock);
185 return pci_match_id(drv->id_table, dev);
188 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
189 const struct pci_device_id *id)
193 /* Execute driver initialization on node where the
194 device's bus is attached to. This way the driver likely
195 allocates its local memory on the right node without
196 any need to change it. */
197 struct mempolicy *oldpol;
198 cpumask_t oldmask = current->cpus_allowed;
199 int node = dev_to_node(&dev->dev);
202 node_to_cpumask_ptr(nodecpumask, node);
203 set_cpus_allowed_ptr(current, nodecpumask);
205 /* And set default memory allocation policy */
206 oldpol = current->mempolicy;
207 current->mempolicy = NULL; /* fall back to system default policy */
209 error = drv->probe(dev, id);
211 set_cpus_allowed_ptr(current, &oldmask);
212 current->mempolicy = oldpol;
218 * __pci_device_probe()
219 * @drv: driver to call to check if it wants the PCI device
220 * @pci_dev: PCI device being probed
222 * returns 0 on success, else error.
223 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
226 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
228 const struct pci_device_id *id;
231 if (!pci_dev->driver && drv->probe) {
234 id = pci_match_device(drv, pci_dev);
236 error = pci_call_probe(drv, pci_dev, id);
238 pci_dev->driver = drv;
245 static int pci_device_probe(struct device * dev)
248 struct pci_driver *drv;
249 struct pci_dev *pci_dev;
251 drv = to_pci_driver(dev->driver);
252 pci_dev = to_pci_dev(dev);
253 pci_dev_get(pci_dev);
254 error = __pci_device_probe(drv, pci_dev);
256 pci_dev_put(pci_dev);
261 static int pci_device_remove(struct device * dev)
263 struct pci_dev * pci_dev = to_pci_dev(dev);
264 struct pci_driver * drv = pci_dev->driver;
268 drv->remove(pci_dev);
269 pci_dev->driver = NULL;
273 * If the device is still on, set the power state as "unknown",
274 * since it might change by the next time we load the driver.
276 if (pci_dev->current_state == PCI_D0)
277 pci_dev->current_state = PCI_UNKNOWN;
280 * We would love to complain here if pci_dev->is_enabled is set, that
281 * the driver should have called pci_disable_device(), but the
282 * unfortunate fact is there are too many odd BIOS and bridge setups
283 * that don't like drivers doing that all of the time.
284 * Oh well, we can dream of sane hardware when we sleep, no matter how
285 * horrible the crap we have to deal with is when we are awake...
288 pci_dev_put(pci_dev);
292 static void pci_device_shutdown(struct device *dev)
294 struct pci_dev *pci_dev = to_pci_dev(dev);
295 struct pci_driver *drv = pci_dev->driver;
297 if (drv && drv->shutdown)
298 drv->shutdown(pci_dev);
299 pci_msi_shutdown(pci_dev);
300 pci_msix_shutdown(pci_dev);
303 #ifdef CONFIG_PM_SLEEP
305 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
307 struct pci_driver *drv = pci_dev->driver;
309 return drv && (drv->suspend || drv->suspend_late || drv->resume
310 || drv->resume_early);
314 * Default "suspend" method for devices that have no driver provided suspend,
315 * or not even a driver at all.
317 static void pci_default_pm_suspend(struct pci_dev *pci_dev)
319 pci_save_state(pci_dev);
321 * mark its power state as "unknown", since we don't know if
322 * e.g. the BIOS will change its device state when we suspend.
324 if (pci_dev->current_state == PCI_D0)
325 pci_dev->current_state = PCI_UNKNOWN;
329 * Default "resume" method for devices that have no driver provided resume,
330 * or not even a driver at all (first part).
332 static void pci_default_pm_resume_early(struct pci_dev *pci_dev)
334 /* restore the PCI config space */
335 pci_restore_state(pci_dev);
339 * Default "resume" method for devices that have no driver provided resume,
340 * or not even a driver at all (second part).
342 static int pci_default_pm_resume_late(struct pci_dev *pci_dev)
346 /* if the device was enabled before suspend, reenable */
347 retval = pci_reenable_device(pci_dev);
349 * if the device was busmaster before the suspend, make it busmaster
352 if (pci_dev->is_busmaster)
353 pci_set_master(pci_dev);
358 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
360 struct pci_dev * pci_dev = to_pci_dev(dev);
361 struct pci_driver * drv = pci_dev->driver;
364 if (drv && drv->suspend) {
365 i = drv->suspend(pci_dev, state);
366 suspend_report_result(drv->suspend, i);
368 pci_default_pm_suspend(pci_dev);
373 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
375 struct pci_dev * pci_dev = to_pci_dev(dev);
376 struct pci_driver * drv = pci_dev->driver;
379 if (drv && drv->suspend_late) {
380 i = drv->suspend_late(pci_dev, state);
381 suspend_report_result(drv->suspend_late, i);
386 static int pci_legacy_resume(struct device *dev)
389 struct pci_dev * pci_dev = to_pci_dev(dev);
390 struct pci_driver * drv = pci_dev->driver;
392 if (drv && drv->resume) {
393 error = drv->resume(pci_dev);
395 pci_default_pm_resume_early(pci_dev);
396 error = pci_default_pm_resume_late(pci_dev);
401 static int pci_legacy_resume_early(struct device *dev)
404 struct pci_dev * pci_dev = to_pci_dev(dev);
405 struct pci_driver * drv = pci_dev->driver;
407 if (drv && drv->resume_early)
408 error = drv->resume_early(pci_dev);
412 static int pci_pm_prepare(struct device *dev)
414 struct device_driver *drv = dev->driver;
417 if (drv && drv->pm && drv->pm->prepare)
418 error = drv->pm->prepare(dev);
423 static void pci_pm_complete(struct device *dev)
425 struct device_driver *drv = dev->driver;
427 if (drv && drv->pm && drv->pm->complete)
428 drv->pm->complete(dev);
431 #ifdef CONFIG_SUSPEND
433 static int pci_pm_suspend(struct device *dev)
435 struct pci_dev *pci_dev = to_pci_dev(dev);
436 struct device_driver *drv = dev->driver;
439 if (drv && drv->pm) {
440 if (drv->pm->suspend) {
441 error = drv->pm->suspend(dev);
442 suspend_report_result(drv->pm->suspend, error);
444 } else if (pci_has_legacy_pm_support(pci_dev)) {
445 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
447 pci_fixup_device(pci_fixup_suspend, pci_dev);
452 static int pci_pm_suspend_noirq(struct device *dev)
454 struct pci_dev *pci_dev = to_pci_dev(dev);
455 struct device_driver *drv = dev->driver;
458 if (drv && drv->pm) {
459 if (drv->pm->suspend_noirq) {
460 error = drv->pm->suspend_noirq(dev);
461 suspend_report_result(drv->pm->suspend_noirq, error);
463 } else if (pci_has_legacy_pm_support(pci_dev)) {
464 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
466 pci_default_pm_suspend(pci_dev);
472 static int pci_pm_resume(struct device *dev)
474 struct pci_dev *pci_dev = to_pci_dev(dev);
475 struct device_driver *drv = dev->driver;
478 pci_fixup_device(pci_fixup_resume, pci_dev);
480 if (drv && drv->pm) {
482 error = drv->pm->resume(dev);
483 } else if (pci_has_legacy_pm_support(pci_dev)) {
484 error = pci_legacy_resume(dev);
486 error = pci_default_pm_resume_late(pci_dev);
492 static int pci_pm_resume_noirq(struct device *dev)
494 struct pci_dev *pci_dev = to_pci_dev(dev);
495 struct device_driver *drv = dev->driver;
498 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
500 if (drv && drv->pm) {
501 if (drv->pm->resume_noirq)
502 error = drv->pm->resume_noirq(dev);
503 } else if (pci_has_legacy_pm_support(pci_dev)) {
504 error = pci_legacy_resume_early(dev);
506 pci_default_pm_resume_early(pci_dev);
512 #else /* !CONFIG_SUSPEND */
514 #define pci_pm_suspend NULL
515 #define pci_pm_suspend_noirq NULL
516 #define pci_pm_resume NULL
517 #define pci_pm_resume_noirq NULL
519 #endif /* !CONFIG_SUSPEND */
521 #ifdef CONFIG_HIBERNATION
523 static int pci_pm_freeze(struct device *dev)
525 struct pci_dev *pci_dev = to_pci_dev(dev);
526 struct device_driver *drv = dev->driver;
529 if (drv && drv->pm) {
530 if (drv->pm->freeze) {
531 error = drv->pm->freeze(dev);
532 suspend_report_result(drv->pm->freeze, error);
534 } else if (pci_has_legacy_pm_support(pci_dev)) {
535 error = pci_legacy_suspend(dev, PMSG_FREEZE);
536 pci_fixup_device(pci_fixup_suspend, pci_dev);
542 static int pci_pm_freeze_noirq(struct device *dev)
544 struct pci_dev *pci_dev = to_pci_dev(dev);
545 struct device_driver *drv = dev->driver;
548 if (drv && drv->pm) {
549 if (drv->pm->freeze_noirq) {
550 error = drv->pm->freeze_noirq(dev);
551 suspend_report_result(drv->pm->freeze_noirq, error);
553 } else if (pci_has_legacy_pm_support(pci_dev)) {
554 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
556 pci_default_pm_suspend(pci_dev);
562 static int pci_pm_thaw(struct device *dev)
564 struct pci_dev *pci_dev = to_pci_dev(dev);
565 struct device_driver *drv = dev->driver;
568 if (drv && drv->pm) {
570 error = drv->pm->thaw(dev);
571 } else if (pci_has_legacy_pm_support(pci_dev)) {
572 pci_fixup_device(pci_fixup_resume, pci_dev);
573 error = pci_legacy_resume(dev);
579 static int pci_pm_thaw_noirq(struct device *dev)
581 struct pci_dev *pci_dev = to_pci_dev(dev);
582 struct device_driver *drv = dev->driver;
585 if (drv && drv->pm) {
586 if (drv->pm->thaw_noirq)
587 error = drv->pm->thaw_noirq(dev);
588 } else if (pci_has_legacy_pm_support(pci_dev)) {
589 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
590 error = pci_legacy_resume_early(dev);
596 static int pci_pm_poweroff(struct device *dev)
598 struct pci_dev *pci_dev = to_pci_dev(dev);
599 struct device_driver *drv = dev->driver;
602 pci_fixup_device(pci_fixup_suspend, pci_dev);
604 if (drv && drv->pm) {
605 if (drv->pm->poweroff) {
606 error = drv->pm->poweroff(dev);
607 suspend_report_result(drv->pm->poweroff, error);
609 } else if (pci_has_legacy_pm_support(pci_dev)) {
610 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
616 static int pci_pm_poweroff_noirq(struct device *dev)
618 struct device_driver *drv = dev->driver;
621 if (drv && drv->pm) {
622 if (drv->pm->poweroff_noirq) {
623 error = drv->pm->poweroff_noirq(dev);
624 suspend_report_result(drv->pm->poweroff_noirq, error);
626 } else if (pci_has_legacy_pm_support(to_pci_dev(dev))) {
627 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
633 static int pci_pm_restore(struct device *dev)
635 struct pci_dev *pci_dev = to_pci_dev(dev);
636 struct device_driver *drv = dev->driver;
639 if (drv && drv->pm) {
640 if (drv->pm->restore)
641 error = drv->pm->restore(dev);
642 } else if (pci_has_legacy_pm_support(pci_dev)) {
643 error = pci_legacy_resume(dev);
645 error = pci_default_pm_resume_late(pci_dev);
647 pci_fixup_device(pci_fixup_resume, pci_dev);
652 static int pci_pm_restore_noirq(struct device *dev)
654 struct pci_dev *pci_dev = to_pci_dev(dev);
655 struct device_driver *drv = dev->driver;
658 pci_fixup_device(pci_fixup_resume, pci_dev);
660 if (drv && drv->pm) {
661 if (drv->pm->restore_noirq)
662 error = drv->pm->restore_noirq(dev);
663 } else if (pci_has_legacy_pm_support(pci_dev)) {
664 error = pci_legacy_resume_early(dev);
666 pci_default_pm_resume_early(pci_dev);
668 pci_fixup_device(pci_fixup_resume_early, pci_dev);
673 #else /* !CONFIG_HIBERNATION */
675 #define pci_pm_freeze NULL
676 #define pci_pm_freeze_noirq NULL
677 #define pci_pm_thaw NULL
678 #define pci_pm_thaw_noirq NULL
679 #define pci_pm_poweroff NULL
680 #define pci_pm_poweroff_noirq NULL
681 #define pci_pm_restore NULL
682 #define pci_pm_restore_noirq NULL
684 #endif /* !CONFIG_HIBERNATION */
686 struct dev_pm_ops pci_dev_pm_ops = {
687 .prepare = pci_pm_prepare,
688 .complete = pci_pm_complete,
689 .suspend = pci_pm_suspend,
690 .resume = pci_pm_resume,
691 .freeze = pci_pm_freeze,
693 .poweroff = pci_pm_poweroff,
694 .restore = pci_pm_restore,
695 .suspend_noirq = pci_pm_suspend_noirq,
696 .resume_noirq = pci_pm_resume_noirq,
697 .freeze_noirq = pci_pm_freeze_noirq,
698 .thaw_noirq = pci_pm_thaw_noirq,
699 .poweroff_noirq = pci_pm_poweroff_noirq,
700 .restore_noirq = pci_pm_restore_noirq,
703 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
705 #else /* !CONFIG_PM_SLEEP */
707 #define PCI_PM_OPS_PTR NULL
709 #endif /* !CONFIG_PM_SLEEP */
712 * __pci_register_driver - register a new pci driver
713 * @drv: the driver structure to register
714 * @owner: owner module of drv
715 * @mod_name: module name string
717 * Adds the driver structure to the list of registered drivers.
718 * Returns a negative value on error, otherwise 0.
719 * If no error occurred, the driver remains registered even if
720 * no device was claimed during registration.
722 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
723 const char *mod_name)
727 /* initialize common driver fields */
728 drv->driver.name = drv->name;
729 drv->driver.bus = &pci_bus_type;
730 drv->driver.owner = owner;
731 drv->driver.mod_name = mod_name;
733 spin_lock_init(&drv->dynids.lock);
734 INIT_LIST_HEAD(&drv->dynids.list);
736 /* register with core */
737 error = driver_register(&drv->driver);
741 error = pci_create_newid_file(drv);
743 driver_unregister(&drv->driver);
749 * pci_unregister_driver - unregister a pci driver
750 * @drv: the driver structure to unregister
752 * Deletes the driver structure from the list of registered PCI drivers,
753 * gives it a chance to clean up by calling its remove() function for
754 * each device it was responsible for, and marks those devices as
759 pci_unregister_driver(struct pci_driver *drv)
761 pci_remove_newid_file(drv);
762 driver_unregister(&drv->driver);
763 pci_free_dynids(drv);
766 static struct pci_driver pci_compat_driver = {
771 * pci_dev_driver - get the pci_driver of a device
772 * @dev: the device to query
774 * Returns the appropriate pci_driver structure or %NULL if there is no
775 * registered driver for the device.
778 pci_dev_driver(const struct pci_dev *dev)
784 for(i=0; i<=PCI_ROM_RESOURCE; i++)
785 if (dev->resource[i].flags & IORESOURCE_BUSY)
786 return &pci_compat_driver;
792 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
793 * @dev: the PCI device structure to match against
794 * @drv: the device driver to search for matching PCI device id structures
796 * Used by a driver to check whether a PCI device present in the
797 * system is in its list of supported devices. Returns the matching
798 * pci_device_id structure or %NULL if there is no match.
800 static int pci_bus_match(struct device *dev, struct device_driver *drv)
802 struct pci_dev *pci_dev = to_pci_dev(dev);
803 struct pci_driver *pci_drv = to_pci_driver(drv);
804 const struct pci_device_id *found_id;
806 found_id = pci_match_device(pci_drv, pci_dev);
814 * pci_dev_get - increments the reference count of the pci device structure
815 * @dev: the device being referenced
817 * Each live reference to a device should be refcounted.
819 * Drivers for PCI devices should normally record such references in
820 * their probe() methods, when they bind to a device, and release
821 * them by calling pci_dev_put(), in their disconnect() methods.
823 * A pointer to the device with the incremented reference counter is returned.
825 struct pci_dev *pci_dev_get(struct pci_dev *dev)
828 get_device(&dev->dev);
833 * pci_dev_put - release a use of the pci device structure
834 * @dev: device that's been disconnected
836 * Must be called when a user of a device is finished with it. When the last
837 * user of the device calls this function, the memory of the device is freed.
839 void pci_dev_put(struct pci_dev *dev)
842 put_device(&dev->dev);
845 #ifndef CONFIG_HOTPLUG
846 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
852 struct bus_type pci_bus_type = {
854 .match = pci_bus_match,
855 .uevent = pci_uevent,
856 .probe = pci_device_probe,
857 .remove = pci_device_remove,
858 .shutdown = pci_device_shutdown,
859 .dev_attrs = pci_dev_attrs,
860 .pm = PCI_PM_OPS_PTR,
863 static int __init pci_driver_init(void)
865 return bus_register(&pci_bus_type);
868 postcore_initcall(pci_driver_init);
870 EXPORT_SYMBOL(pci_match_id);
871 EXPORT_SYMBOL(__pci_register_driver);
872 EXPORT_SYMBOL(pci_unregister_driver);
873 EXPORT_SYMBOL(pci_dev_driver);
874 EXPORT_SYMBOL(pci_bus_type);
875 EXPORT_SYMBOL(pci_dev_get);
876 EXPORT_SYMBOL(pci_dev_put);