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
62 while (ids->vendor || ids->subvendor || ids->class_mask) {
63 if (driver_data == ids->driver_data) {
69 if (retval) /* No match */
72 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
76 dynid->id.vendor = vendor;
77 dynid->id.device = device;
78 dynid->id.subvendor = subvendor;
79 dynid->id.subdevice = subdevice;
80 dynid->id.class = class;
81 dynid->id.class_mask = class_mask;
82 dynid->id.driver_data = driver_data;
84 spin_lock(&pdrv->dynids.lock);
85 list_add_tail(&dynid->node, &pdrv->dynids.list);
86 spin_unlock(&pdrv->dynids.lock);
88 if (get_driver(&pdrv->driver)) {
89 retval = driver_attach(&pdrv->driver);
90 put_driver(&pdrv->driver);
97 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
100 pci_free_dynids(struct pci_driver *drv)
102 struct pci_dynid *dynid, *n;
104 spin_lock(&drv->dynids.lock);
105 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
106 list_del(&dynid->node);
109 spin_unlock(&drv->dynids.lock);
113 pci_create_newid_file(struct pci_driver *drv)
116 if (drv->probe != NULL)
117 error = driver_create_file(&drv->driver, &driver_attr_new_id);
121 static void pci_remove_newid_file(struct pci_driver *drv)
123 driver_remove_file(&drv->driver, &driver_attr_new_id);
125 #else /* !CONFIG_HOTPLUG */
126 static inline void pci_free_dynids(struct pci_driver *drv) {}
127 static inline int pci_create_newid_file(struct pci_driver *drv)
131 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
135 * pci_match_id - See if a pci device matches a given pci_id table
136 * @ids: array of PCI device id structures to search in
137 * @dev: the PCI device structure to match against.
139 * Used by a driver to check whether a PCI device present in the
140 * system is in its list of supported devices. Returns the matching
141 * pci_device_id structure or %NULL if there is no match.
143 * Deprecated, don't use this as it will not catch any dynamic ids
144 * that a driver might want to check for.
146 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
150 while (ids->vendor || ids->subvendor || ids->class_mask) {
151 if (pci_match_one_device(ids, dev))
160 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
161 * @drv: the PCI driver to match against
162 * @dev: the PCI device structure to match against
164 * Used by a driver to check whether a PCI device present in the
165 * system is in its list of supported devices. Returns the matching
166 * pci_device_id structure or %NULL if there is no match.
168 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
171 struct pci_dynid *dynid;
173 /* Look at the dynamic ids first, before the static ones */
174 spin_lock(&drv->dynids.lock);
175 list_for_each_entry(dynid, &drv->dynids.list, node) {
176 if (pci_match_one_device(&dynid->id, dev)) {
177 spin_unlock(&drv->dynids.lock);
181 spin_unlock(&drv->dynids.lock);
183 return pci_match_id(drv->id_table, dev);
186 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
187 const struct pci_device_id *id)
191 /* Execute driver initialization on node where the
192 device's bus is attached to. This way the driver likely
193 allocates its local memory on the right node without
194 any need to change it. */
195 struct mempolicy *oldpol;
196 cpumask_t oldmask = current->cpus_allowed;
197 int node = dev_to_node(&dev->dev);
200 node_to_cpumask_ptr(nodecpumask, node);
201 set_cpus_allowed_ptr(current, nodecpumask);
203 /* And set default memory allocation policy */
204 oldpol = current->mempolicy;
205 current->mempolicy = NULL; /* fall back to system default policy */
207 error = drv->probe(dev, id);
209 set_cpus_allowed_ptr(current, &oldmask);
210 current->mempolicy = oldpol;
216 * __pci_device_probe()
217 * @drv: driver to call to check if it wants the PCI device
218 * @pci_dev: PCI device being probed
220 * returns 0 on success, else error.
221 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
224 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
226 const struct pci_device_id *id;
229 if (!pci_dev->driver && drv->probe) {
232 id = pci_match_device(drv, pci_dev);
234 error = pci_call_probe(drv, pci_dev, id);
236 pci_dev->driver = drv;
243 static int pci_device_probe(struct device * dev)
246 struct pci_driver *drv;
247 struct pci_dev *pci_dev;
249 drv = to_pci_driver(dev->driver);
250 pci_dev = to_pci_dev(dev);
251 pci_dev_get(pci_dev);
252 error = __pci_device_probe(drv, pci_dev);
254 pci_dev_put(pci_dev);
259 static int pci_device_remove(struct device * dev)
261 struct pci_dev * pci_dev = to_pci_dev(dev);
262 struct pci_driver * drv = pci_dev->driver;
266 drv->remove(pci_dev);
267 pci_dev->driver = NULL;
271 * If the device is still on, set the power state as "unknown",
272 * since it might change by the next time we load the driver.
274 if (pci_dev->current_state == PCI_D0)
275 pci_dev->current_state = PCI_UNKNOWN;
278 * We would love to complain here if pci_dev->is_enabled is set, that
279 * the driver should have called pci_disable_device(), but the
280 * unfortunate fact is there are too many odd BIOS and bridge setups
281 * that don't like drivers doing that all of the time.
282 * Oh well, we can dream of sane hardware when we sleep, no matter how
283 * horrible the crap we have to deal with is when we are awake...
286 pci_dev_put(pci_dev);
290 static void pci_device_shutdown(struct device *dev)
292 struct pci_dev *pci_dev = to_pci_dev(dev);
293 struct pci_driver *drv = pci_dev->driver;
295 if (drv && drv->shutdown)
296 drv->shutdown(pci_dev);
297 pci_msi_shutdown(pci_dev);
298 pci_msix_shutdown(pci_dev);
301 #ifdef CONFIG_PM_SLEEP
303 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
305 struct pci_driver *drv = pci_dev->driver;
307 return drv && (drv->suspend || drv->suspend_late || drv->resume
308 || drv->resume_early);
312 * Default "suspend" method for devices that have no driver provided suspend,
313 * or not even a driver at all.
315 static void pci_default_pm_suspend(struct pci_dev *pci_dev)
317 pci_save_state(pci_dev);
319 * mark its power state as "unknown", since we don't know if
320 * e.g. the BIOS will change its device state when we suspend.
322 if (pci_dev->current_state == PCI_D0)
323 pci_dev->current_state = PCI_UNKNOWN;
327 * Default "resume" method for devices that have no driver provided resume,
328 * or not even a driver at all (first part).
330 static void pci_default_pm_resume_early(struct pci_dev *pci_dev)
332 /* restore the PCI config space */
333 pci_restore_state(pci_dev);
337 * Default "resume" method for devices that have no driver provided resume,
338 * or not even a driver at all (second part).
340 static int pci_default_pm_resume_late(struct pci_dev *pci_dev)
344 /* if the device was enabled before suspend, reenable */
345 retval = pci_reenable_device(pci_dev);
347 * if the device was busmaster before the suspend, make it busmaster
350 if (pci_dev->is_busmaster)
351 pci_set_master(pci_dev);
356 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
358 struct pci_dev * pci_dev = to_pci_dev(dev);
359 struct pci_driver * drv = pci_dev->driver;
362 if (drv && drv->suspend) {
363 i = drv->suspend(pci_dev, state);
364 suspend_report_result(drv->suspend, i);
366 pci_default_pm_suspend(pci_dev);
371 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
373 struct pci_dev * pci_dev = to_pci_dev(dev);
374 struct pci_driver * drv = pci_dev->driver;
377 if (drv && drv->suspend_late) {
378 i = drv->suspend_late(pci_dev, state);
379 suspend_report_result(drv->suspend_late, i);
384 static int pci_legacy_resume(struct device *dev)
387 struct pci_dev * pci_dev = to_pci_dev(dev);
388 struct pci_driver * drv = pci_dev->driver;
390 if (drv && drv->resume) {
391 error = drv->resume(pci_dev);
393 pci_default_pm_resume_early(pci_dev);
394 error = pci_default_pm_resume_late(pci_dev);
399 static int pci_legacy_resume_early(struct device *dev)
402 struct pci_dev * pci_dev = to_pci_dev(dev);
403 struct pci_driver * drv = pci_dev->driver;
405 if (drv && drv->resume_early)
406 error = drv->resume_early(pci_dev);
410 static int pci_pm_prepare(struct device *dev)
412 struct device_driver *drv = dev->driver;
415 if (drv && drv->pm && drv->pm->prepare)
416 error = drv->pm->prepare(dev);
421 static void pci_pm_complete(struct device *dev)
423 struct device_driver *drv = dev->driver;
425 if (drv && drv->pm && drv->pm->complete)
426 drv->pm->complete(dev);
429 #ifdef CONFIG_SUSPEND
431 static int pci_pm_suspend(struct device *dev)
433 struct pci_dev *pci_dev = to_pci_dev(dev);
434 struct device_driver *drv = dev->driver;
437 if (drv && drv->pm) {
438 if (drv->pm->suspend) {
439 error = drv->pm->suspend(dev);
440 suspend_report_result(drv->pm->suspend, error);
442 } else if (pci_has_legacy_pm_support(pci_dev)) {
443 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
445 pci_fixup_device(pci_fixup_suspend, pci_dev);
450 static int pci_pm_suspend_noirq(struct device *dev)
452 struct pci_dev *pci_dev = to_pci_dev(dev);
453 struct device_driver *drv = dev->driver;
456 if (drv && drv->pm) {
457 if (drv->pm->suspend_noirq) {
458 error = drv->pm->suspend_noirq(dev);
459 suspend_report_result(drv->pm->suspend_noirq, error);
461 } else if (pci_has_legacy_pm_support(pci_dev)) {
462 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
464 pci_default_pm_suspend(pci_dev);
470 static int pci_pm_resume(struct device *dev)
472 struct pci_dev *pci_dev = to_pci_dev(dev);
473 struct device_driver *drv = dev->driver;
476 pci_fixup_device(pci_fixup_resume, pci_dev);
478 if (drv && drv->pm) {
480 error = drv->pm->resume(dev);
481 } else if (pci_has_legacy_pm_support(pci_dev)) {
482 error = pci_legacy_resume(dev);
484 error = pci_default_pm_resume_late(pci_dev);
490 static int pci_pm_resume_noirq(struct device *dev)
492 struct pci_dev *pci_dev = to_pci_dev(dev);
493 struct device_driver *drv = dev->driver;
496 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
498 if (drv && drv->pm) {
499 if (drv->pm->resume_noirq)
500 error = drv->pm->resume_noirq(dev);
501 } else if (pci_has_legacy_pm_support(pci_dev)) {
502 error = pci_legacy_resume_early(dev);
504 pci_default_pm_resume_early(pci_dev);
510 #else /* !CONFIG_SUSPEND */
512 #define pci_pm_suspend NULL
513 #define pci_pm_suspend_noirq NULL
514 #define pci_pm_resume NULL
515 #define pci_pm_resume_noirq NULL
517 #endif /* !CONFIG_SUSPEND */
519 #ifdef CONFIG_HIBERNATION
521 static int pci_pm_freeze(struct device *dev)
523 struct pci_dev *pci_dev = to_pci_dev(dev);
524 struct device_driver *drv = dev->driver;
527 if (drv && drv->pm) {
528 if (drv->pm->freeze) {
529 error = drv->pm->freeze(dev);
530 suspend_report_result(drv->pm->freeze, error);
532 } else if (pci_has_legacy_pm_support(pci_dev)) {
533 error = pci_legacy_suspend(dev, PMSG_FREEZE);
534 pci_fixup_device(pci_fixup_suspend, pci_dev);
540 static int pci_pm_freeze_noirq(struct device *dev)
542 struct pci_dev *pci_dev = to_pci_dev(dev);
543 struct device_driver *drv = dev->driver;
546 if (drv && drv->pm) {
547 if (drv->pm->freeze_noirq) {
548 error = drv->pm->freeze_noirq(dev);
549 suspend_report_result(drv->pm->freeze_noirq, error);
551 } else if (pci_has_legacy_pm_support(pci_dev)) {
552 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
554 pci_default_pm_suspend(pci_dev);
560 static int pci_pm_thaw(struct device *dev)
562 struct pci_dev *pci_dev = to_pci_dev(dev);
563 struct device_driver *drv = dev->driver;
566 if (drv && drv->pm) {
568 error = drv->pm->thaw(dev);
569 } else if (pci_has_legacy_pm_support(pci_dev)) {
570 pci_fixup_device(pci_fixup_resume, pci_dev);
571 error = pci_legacy_resume(dev);
577 static int pci_pm_thaw_noirq(struct device *dev)
579 struct pci_dev *pci_dev = to_pci_dev(dev);
580 struct device_driver *drv = dev->driver;
583 if (drv && drv->pm) {
584 if (drv->pm->thaw_noirq)
585 error = drv->pm->thaw_noirq(dev);
586 } else if (pci_has_legacy_pm_support(pci_dev)) {
587 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
588 error = pci_legacy_resume_early(dev);
594 static int pci_pm_poweroff(struct device *dev)
596 struct pci_dev *pci_dev = to_pci_dev(dev);
597 struct device_driver *drv = dev->driver;
600 pci_fixup_device(pci_fixup_suspend, pci_dev);
602 if (drv && drv->pm) {
603 if (drv->pm->poweroff) {
604 error = drv->pm->poweroff(dev);
605 suspend_report_result(drv->pm->poweroff, error);
607 } else if (pci_has_legacy_pm_support(pci_dev)) {
608 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
614 static int pci_pm_poweroff_noirq(struct device *dev)
616 struct device_driver *drv = dev->driver;
619 if (drv && drv->pm) {
620 if (drv->pm->poweroff_noirq) {
621 error = drv->pm->poweroff_noirq(dev);
622 suspend_report_result(drv->pm->poweroff_noirq, error);
624 } else if (pci_has_legacy_pm_support(to_pci_dev(dev))) {
625 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
631 static int pci_pm_restore(struct device *dev)
633 struct pci_dev *pci_dev = to_pci_dev(dev);
634 struct device_driver *drv = dev->driver;
637 if (drv && drv->pm) {
638 if (drv->pm->restore)
639 error = drv->pm->restore(dev);
640 } else if (pci_has_legacy_pm_support(pci_dev)) {
641 error = pci_legacy_resume(dev);
643 error = pci_default_pm_resume_late(pci_dev);
645 pci_fixup_device(pci_fixup_resume, pci_dev);
650 static int pci_pm_restore_noirq(struct device *dev)
652 struct pci_dev *pci_dev = to_pci_dev(dev);
653 struct device_driver *drv = dev->driver;
656 pci_fixup_device(pci_fixup_resume, pci_dev);
658 if (drv && drv->pm) {
659 if (drv->pm->restore_noirq)
660 error = drv->pm->restore_noirq(dev);
661 } else if (pci_has_legacy_pm_support(pci_dev)) {
662 error = pci_legacy_resume_early(dev);
664 pci_default_pm_resume_early(pci_dev);
666 pci_fixup_device(pci_fixup_resume_early, pci_dev);
671 #else /* !CONFIG_HIBERNATION */
673 #define pci_pm_freeze NULL
674 #define pci_pm_freeze_noirq NULL
675 #define pci_pm_thaw NULL
676 #define pci_pm_thaw_noirq NULL
677 #define pci_pm_poweroff NULL
678 #define pci_pm_poweroff_noirq NULL
679 #define pci_pm_restore NULL
680 #define pci_pm_restore_noirq NULL
682 #endif /* !CONFIG_HIBERNATION */
684 struct dev_pm_ops pci_dev_pm_ops = {
685 .prepare = pci_pm_prepare,
686 .complete = pci_pm_complete,
687 .suspend = pci_pm_suspend,
688 .resume = pci_pm_resume,
689 .freeze = pci_pm_freeze,
691 .poweroff = pci_pm_poweroff,
692 .restore = pci_pm_restore,
693 .suspend_noirq = pci_pm_suspend_noirq,
694 .resume_noirq = pci_pm_resume_noirq,
695 .freeze_noirq = pci_pm_freeze_noirq,
696 .thaw_noirq = pci_pm_thaw_noirq,
697 .poweroff_noirq = pci_pm_poweroff_noirq,
698 .restore_noirq = pci_pm_restore_noirq,
701 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
703 #else /* !CONFIG_PM_SLEEP */
705 #define PCI_PM_OPS_PTR NULL
707 #endif /* !CONFIG_PM_SLEEP */
710 * __pci_register_driver - register a new pci driver
711 * @drv: the driver structure to register
712 * @owner: owner module of drv
713 * @mod_name: module name string
715 * Adds the driver structure to the list of registered drivers.
716 * Returns a negative value on error, otherwise 0.
717 * If no error occurred, the driver remains registered even if
718 * no device was claimed during registration.
720 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
721 const char *mod_name)
725 /* initialize common driver fields */
726 drv->driver.name = drv->name;
727 drv->driver.bus = &pci_bus_type;
728 drv->driver.owner = owner;
729 drv->driver.mod_name = mod_name;
731 spin_lock_init(&drv->dynids.lock);
732 INIT_LIST_HEAD(&drv->dynids.list);
734 /* register with core */
735 error = driver_register(&drv->driver);
739 error = pci_create_newid_file(drv);
741 driver_unregister(&drv->driver);
747 * pci_unregister_driver - unregister a pci driver
748 * @drv: the driver structure to unregister
750 * Deletes the driver structure from the list of registered PCI drivers,
751 * gives it a chance to clean up by calling its remove() function for
752 * each device it was responsible for, and marks those devices as
757 pci_unregister_driver(struct pci_driver *drv)
759 pci_remove_newid_file(drv);
760 driver_unregister(&drv->driver);
761 pci_free_dynids(drv);
764 static struct pci_driver pci_compat_driver = {
769 * pci_dev_driver - get the pci_driver of a device
770 * @dev: the device to query
772 * Returns the appropriate pci_driver structure or %NULL if there is no
773 * registered driver for the device.
776 pci_dev_driver(const struct pci_dev *dev)
782 for(i=0; i<=PCI_ROM_RESOURCE; i++)
783 if (dev->resource[i].flags & IORESOURCE_BUSY)
784 return &pci_compat_driver;
790 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
791 * @dev: the PCI device structure to match against
792 * @drv: the device driver to search for matching PCI device id structures
794 * Used by a driver to check whether a PCI device present in the
795 * system is in its list of supported devices. Returns the matching
796 * pci_device_id structure or %NULL if there is no match.
798 static int pci_bus_match(struct device *dev, struct device_driver *drv)
800 struct pci_dev *pci_dev = to_pci_dev(dev);
801 struct pci_driver *pci_drv = to_pci_driver(drv);
802 const struct pci_device_id *found_id;
804 found_id = pci_match_device(pci_drv, pci_dev);
812 * pci_dev_get - increments the reference count of the pci device structure
813 * @dev: the device being referenced
815 * Each live reference to a device should be refcounted.
817 * Drivers for PCI devices should normally record such references in
818 * their probe() methods, when they bind to a device, and release
819 * them by calling pci_dev_put(), in their disconnect() methods.
821 * A pointer to the device with the incremented reference counter is returned.
823 struct pci_dev *pci_dev_get(struct pci_dev *dev)
826 get_device(&dev->dev);
831 * pci_dev_put - release a use of the pci device structure
832 * @dev: device that's been disconnected
834 * Must be called when a user of a device is finished with it. When the last
835 * user of the device calls this function, the memory of the device is freed.
837 void pci_dev_put(struct pci_dev *dev)
840 put_device(&dev->dev);
843 #ifndef CONFIG_HOTPLUG
844 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
850 struct bus_type pci_bus_type = {
852 .match = pci_bus_match,
853 .uevent = pci_uevent,
854 .probe = pci_device_probe,
855 .remove = pci_device_remove,
856 .shutdown = pci_device_shutdown,
857 .dev_attrs = pci_dev_attrs,
858 .pm = PCI_PM_OPS_PTR,
861 static int __init pci_driver_init(void)
863 return bus_register(&pci_bus_type);
866 postcore_initcall(pci_driver_init);
868 EXPORT_SYMBOL(pci_match_id);
869 EXPORT_SYMBOL(__pci_register_driver);
870 EXPORT_SYMBOL(pci_unregister_driver);
871 EXPORT_SYMBOL(pci_dev_driver);
872 EXPORT_SYMBOL(pci_bus_type);
873 EXPORT_SYMBOL(pci_dev_get);
874 EXPORT_SYMBOL(pci_dev_put);