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 = driver_data;
70 spin_lock(&pdrv->dynids.lock);
71 list_add_tail(&dynid->node, &pdrv->dynids.list);
72 spin_unlock(&pdrv->dynids.lock);
74 if (get_driver(&pdrv->driver)) {
75 retval = driver_attach(&pdrv->driver);
76 put_driver(&pdrv->driver);
83 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
86 pci_free_dynids(struct pci_driver *drv)
88 struct pci_dynid *dynid, *n;
90 spin_lock(&drv->dynids.lock);
91 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
92 list_del(&dynid->node);
95 spin_unlock(&drv->dynids.lock);
99 pci_create_newid_file(struct pci_driver *drv)
102 if (drv->probe != NULL)
103 error = driver_create_file(&drv->driver, &driver_attr_new_id);
107 static void pci_remove_newid_file(struct pci_driver *drv)
109 driver_remove_file(&drv->driver, &driver_attr_new_id);
111 #else /* !CONFIG_HOTPLUG */
112 static inline void pci_free_dynids(struct pci_driver *drv) {}
113 static inline int pci_create_newid_file(struct pci_driver *drv)
117 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
121 * pci_match_id - See if a pci device matches a given pci_id table
122 * @ids: array of PCI device id structures to search in
123 * @dev: the PCI device structure to match against.
125 * Used by a driver to check whether a PCI device present in the
126 * system is in its list of supported devices. Returns the matching
127 * pci_device_id structure or %NULL if there is no match.
129 * Deprecated, don't use this as it will not catch any dynamic ids
130 * that a driver might want to check for.
132 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
136 while (ids->vendor || ids->subvendor || ids->class_mask) {
137 if (pci_match_one_device(ids, dev))
146 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
147 * @drv: the PCI driver to match against
148 * @dev: the PCI device structure to match against
150 * Used by a driver to check whether a PCI device present in the
151 * system is in its list of supported devices. Returns the matching
152 * pci_device_id structure or %NULL if there is no match.
154 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
157 struct pci_dynid *dynid;
159 /* Look at the dynamic ids first, before the static ones */
160 spin_lock(&drv->dynids.lock);
161 list_for_each_entry(dynid, &drv->dynids.list, node) {
162 if (pci_match_one_device(&dynid->id, dev)) {
163 spin_unlock(&drv->dynids.lock);
167 spin_unlock(&drv->dynids.lock);
169 return pci_match_id(drv->id_table, dev);
172 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
173 const struct pci_device_id *id)
177 /* Execute driver initialization on node where the
178 device's bus is attached to. This way the driver likely
179 allocates its local memory on the right node without
180 any need to change it. */
181 struct mempolicy *oldpol;
182 cpumask_t oldmask = current->cpus_allowed;
183 int node = dev_to_node(&dev->dev);
186 node_to_cpumask_ptr(nodecpumask, node);
187 set_cpus_allowed_ptr(current, nodecpumask);
189 /* And set default memory allocation policy */
190 oldpol = current->mempolicy;
191 current->mempolicy = NULL; /* fall back to system default policy */
193 error = drv->probe(dev, id);
195 set_cpus_allowed_ptr(current, &oldmask);
196 current->mempolicy = oldpol;
202 * __pci_device_probe()
203 * @drv: driver to call to check if it wants the PCI device
204 * @pci_dev: PCI device being probed
206 * returns 0 on success, else error.
207 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
210 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
212 const struct pci_device_id *id;
215 if (!pci_dev->driver && drv->probe) {
218 id = pci_match_device(drv, pci_dev);
220 error = pci_call_probe(drv, pci_dev, id);
222 pci_dev->driver = drv;
229 static int pci_device_probe(struct device * dev)
232 struct pci_driver *drv;
233 struct pci_dev *pci_dev;
235 drv = to_pci_driver(dev->driver);
236 pci_dev = to_pci_dev(dev);
237 pci_dev_get(pci_dev);
238 error = __pci_device_probe(drv, pci_dev);
240 pci_dev_put(pci_dev);
245 static int pci_device_remove(struct device * dev)
247 struct pci_dev * pci_dev = to_pci_dev(dev);
248 struct pci_driver * drv = pci_dev->driver;
252 drv->remove(pci_dev);
253 pci_dev->driver = NULL;
257 * If the device is still on, set the power state as "unknown",
258 * since it might change by the next time we load the driver.
260 if (pci_dev->current_state == PCI_D0)
261 pci_dev->current_state = PCI_UNKNOWN;
264 * We would love to complain here if pci_dev->is_enabled is set, that
265 * the driver should have called pci_disable_device(), but the
266 * unfortunate fact is there are too many odd BIOS and bridge setups
267 * that don't like drivers doing that all of the time.
268 * Oh well, we can dream of sane hardware when we sleep, no matter how
269 * horrible the crap we have to deal with is when we are awake...
272 pci_dev_put(pci_dev);
276 static void pci_device_shutdown(struct device *dev)
278 struct pci_dev *pci_dev = to_pci_dev(dev);
279 struct pci_driver *drv = pci_dev->driver;
281 if (drv && drv->shutdown)
282 drv->shutdown(pci_dev);
283 pci_msi_shutdown(pci_dev);
284 pci_msix_shutdown(pci_dev);
287 #ifdef CONFIG_PM_SLEEP
290 * Default "suspend" method for devices that have no driver provided suspend,
291 * or not even a driver at all.
293 static void pci_default_pm_suspend(struct pci_dev *pci_dev)
295 pci_save_state(pci_dev);
297 * mark its power state as "unknown", since we don't know if
298 * e.g. the BIOS will change its device state when we suspend.
300 if (pci_dev->current_state == PCI_D0)
301 pci_dev->current_state = PCI_UNKNOWN;
305 * Default "resume" method for devices that have no driver provided resume,
306 * or not even a driver at all.
308 static int pci_default_pm_resume(struct pci_dev *pci_dev)
312 /* restore the PCI config space */
313 pci_restore_state(pci_dev);
314 /* if the device was enabled before suspend, reenable */
315 retval = pci_reenable_device(pci_dev);
317 * if the device was busmaster before the suspend, make it busmaster
320 if (pci_dev->is_busmaster)
321 pci_set_master(pci_dev);
326 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
328 struct pci_dev * pci_dev = to_pci_dev(dev);
329 struct pci_driver * drv = pci_dev->driver;
332 if (drv && drv->suspend) {
333 i = drv->suspend(pci_dev, state);
334 suspend_report_result(drv->suspend, i);
336 pci_default_pm_suspend(pci_dev);
341 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
343 struct pci_dev * pci_dev = to_pci_dev(dev);
344 struct pci_driver * drv = pci_dev->driver;
347 if (drv && drv->suspend_late) {
348 i = drv->suspend_late(pci_dev, state);
349 suspend_report_result(drv->suspend_late, i);
354 static int pci_legacy_resume(struct device *dev)
357 struct pci_dev * pci_dev = to_pci_dev(dev);
358 struct pci_driver * drv = pci_dev->driver;
360 if (drv && drv->resume)
361 error = drv->resume(pci_dev);
363 error = pci_default_pm_resume(pci_dev);
367 static int pci_legacy_resume_early(struct device *dev)
370 struct pci_dev * pci_dev = to_pci_dev(dev);
371 struct pci_driver * drv = pci_dev->driver;
373 if (drv && drv->resume_early)
374 error = drv->resume_early(pci_dev);
378 static int pci_pm_prepare(struct device *dev)
380 struct device_driver *drv = dev->driver;
383 if (drv && drv->pm && drv->pm->prepare)
384 error = drv->pm->prepare(dev);
389 static void pci_pm_complete(struct device *dev)
391 struct device_driver *drv = dev->driver;
393 if (drv && drv->pm && drv->pm->complete)
394 drv->pm->complete(dev);
397 #ifdef CONFIG_SUSPEND
399 static int pci_pm_suspend(struct device *dev)
401 struct pci_dev *pci_dev = to_pci_dev(dev);
402 struct device_driver *drv = dev->driver;
405 if (drv && drv->pm) {
406 if (drv->pm->suspend) {
407 error = drv->pm->suspend(dev);
408 suspend_report_result(drv->pm->suspend, error);
410 pci_default_pm_suspend(pci_dev);
413 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
415 pci_fixup_device(pci_fixup_suspend, pci_dev);
420 static int pci_pm_suspend_noirq(struct device *dev)
422 struct pci_dev *pci_dev = to_pci_dev(dev);
423 struct pci_driver *drv = pci_dev->driver;
426 if (drv && drv->pm) {
427 if (drv->pm->suspend_noirq) {
428 error = drv->pm->suspend_noirq(dev);
429 suspend_report_result(drv->pm->suspend_noirq, error);
432 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
438 static int pci_pm_resume(struct device *dev)
440 struct pci_dev *pci_dev = to_pci_dev(dev);
441 struct device_driver *drv = dev->driver;
444 pci_fixup_device(pci_fixup_resume, pci_dev);
446 if (drv && drv->pm) {
447 error = drv->pm->resume ? drv->pm->resume(dev) :
448 pci_default_pm_resume(pci_dev);
450 error = pci_legacy_resume(dev);
456 static int pci_pm_resume_noirq(struct device *dev)
458 struct pci_dev *pci_dev = to_pci_dev(dev);
459 struct pci_driver *drv = pci_dev->driver;
462 pci_fixup_device(pci_fixup_resume_early, pci_dev);
464 if (drv && drv->pm) {
465 if (drv->pm->resume_noirq)
466 error = drv->pm->resume_noirq(dev);
468 error = pci_legacy_resume_early(dev);
474 #else /* !CONFIG_SUSPEND */
476 #define pci_pm_suspend NULL
477 #define pci_pm_suspend_noirq NULL
478 #define pci_pm_resume NULL
479 #define pci_pm_resume_noirq NULL
481 #endif /* !CONFIG_SUSPEND */
483 #ifdef CONFIG_HIBERNATION
485 static int pci_pm_freeze(struct device *dev)
487 struct pci_dev *pci_dev = to_pci_dev(dev);
488 struct device_driver *drv = dev->driver;
491 if (drv && drv->pm) {
492 if (drv->pm->freeze) {
493 error = drv->pm->freeze(dev);
494 suspend_report_result(drv->pm->freeze, error);
496 pci_default_pm_suspend(pci_dev);
499 error = pci_legacy_suspend(dev, PMSG_FREEZE);
500 pci_fixup_device(pci_fixup_suspend, pci_dev);
506 static int pci_pm_freeze_noirq(struct device *dev)
508 struct pci_dev *pci_dev = to_pci_dev(dev);
509 struct pci_driver *drv = pci_dev->driver;
512 if (drv && drv->pm) {
513 if (drv->pm->freeze_noirq) {
514 error = drv->pm->freeze_noirq(dev);
515 suspend_report_result(drv->pm->freeze_noirq, error);
518 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
524 static int pci_pm_thaw(struct device *dev)
526 struct device_driver *drv = dev->driver;
529 if (drv && drv->pm) {
531 error = drv->pm->thaw(dev);
533 pci_fixup_device(pci_fixup_resume, to_pci_dev(dev));
534 error = pci_legacy_resume(dev);
540 static int pci_pm_thaw_noirq(struct device *dev)
542 struct pci_dev *pci_dev = to_pci_dev(dev);
543 struct pci_driver *drv = pci_dev->driver;
546 if (drv && drv->pm) {
547 if (drv->pm->thaw_noirq)
548 error = drv->pm->thaw_noirq(dev);
550 pci_fixup_device(pci_fixup_resume_early, pci_dev);
551 error = pci_legacy_resume_early(dev);
557 static int pci_pm_poweroff(struct device *dev)
559 struct device_driver *drv = dev->driver;
562 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
564 if (drv && drv->pm) {
565 if (drv->pm->poweroff) {
566 error = drv->pm->poweroff(dev);
567 suspend_report_result(drv->pm->poweroff, error);
570 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
576 static int pci_pm_poweroff_noirq(struct device *dev)
578 struct pci_dev *pci_dev = to_pci_dev(dev);
579 struct pci_driver *drv = pci_dev->driver;
582 if (drv && drv->pm) {
583 if (drv->pm->poweroff_noirq) {
584 error = drv->pm->poweroff_noirq(dev);
585 suspend_report_result(drv->pm->poweroff_noirq, error);
588 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
594 static int pci_pm_restore(struct device *dev)
596 struct pci_dev *pci_dev = to_pci_dev(dev);
597 struct device_driver *drv = dev->driver;
600 if (drv && drv->pm) {
601 error = drv->pm->restore ? drv->pm->restore(dev) :
602 pci_default_pm_resume(pci_dev);
604 error = pci_legacy_resume(dev);
606 pci_fixup_device(pci_fixup_resume, pci_dev);
611 static int pci_pm_restore_noirq(struct device *dev)
613 struct pci_dev *pci_dev = to_pci_dev(dev);
614 struct pci_driver *drv = pci_dev->driver;
617 pci_fixup_device(pci_fixup_resume, pci_dev);
619 if (drv && drv->pm) {
620 if (drv->pm->restore_noirq)
621 error = drv->pm->restore_noirq(dev);
623 error = pci_legacy_resume_early(dev);
625 pci_fixup_device(pci_fixup_resume_early, pci_dev);
630 #else /* !CONFIG_HIBERNATION */
632 #define pci_pm_freeze NULL
633 #define pci_pm_freeze_noirq NULL
634 #define pci_pm_thaw NULL
635 #define pci_pm_thaw_noirq NULL
636 #define pci_pm_poweroff NULL
637 #define pci_pm_poweroff_noirq NULL
638 #define pci_pm_restore NULL
639 #define pci_pm_restore_noirq NULL
641 #endif /* !CONFIG_HIBERNATION */
643 struct pm_ext_ops pci_pm_ops = {
645 .prepare = pci_pm_prepare,
646 .complete = pci_pm_complete,
647 .suspend = pci_pm_suspend,
648 .resume = pci_pm_resume,
649 .freeze = pci_pm_freeze,
651 .poweroff = pci_pm_poweroff,
652 .restore = pci_pm_restore,
654 .suspend_noirq = pci_pm_suspend_noirq,
655 .resume_noirq = pci_pm_resume_noirq,
656 .freeze_noirq = pci_pm_freeze_noirq,
657 .thaw_noirq = pci_pm_thaw_noirq,
658 .poweroff_noirq = pci_pm_poweroff_noirq,
659 .restore_noirq = pci_pm_restore_noirq,
662 #define PCI_PM_OPS_PTR &pci_pm_ops
664 #else /* !CONFIG_PM_SLEEP */
666 #define PCI_PM_OPS_PTR NULL
668 #endif /* !CONFIG_PM_SLEEP */
671 * __pci_register_driver - register a new pci driver
672 * @drv: the driver structure to register
673 * @owner: owner module of drv
674 * @mod_name: module name string
676 * Adds the driver structure to the list of registered drivers.
677 * Returns a negative value on error, otherwise 0.
678 * If no error occurred, the driver remains registered even if
679 * no device was claimed during registration.
681 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
682 const char *mod_name)
686 /* initialize common driver fields */
687 drv->driver.name = drv->name;
688 drv->driver.bus = &pci_bus_type;
689 drv->driver.owner = owner;
690 drv->driver.mod_name = mod_name;
693 drv->driver.pm = &drv->pm->base;
695 spin_lock_init(&drv->dynids.lock);
696 INIT_LIST_HEAD(&drv->dynids.list);
698 /* register with core */
699 error = driver_register(&drv->driver);
703 error = pci_create_newid_file(drv);
705 driver_unregister(&drv->driver);
711 * pci_unregister_driver - unregister a pci driver
712 * @drv: the driver structure to unregister
714 * Deletes the driver structure from the list of registered PCI drivers,
715 * gives it a chance to clean up by calling its remove() function for
716 * each device it was responsible for, and marks those devices as
721 pci_unregister_driver(struct pci_driver *drv)
723 pci_remove_newid_file(drv);
724 driver_unregister(&drv->driver);
725 pci_free_dynids(drv);
728 static struct pci_driver pci_compat_driver = {
733 * pci_dev_driver - get the pci_driver of a device
734 * @dev: the device to query
736 * Returns the appropriate pci_driver structure or %NULL if there is no
737 * registered driver for the device.
740 pci_dev_driver(const struct pci_dev *dev)
746 for(i=0; i<=PCI_ROM_RESOURCE; i++)
747 if (dev->resource[i].flags & IORESOURCE_BUSY)
748 return &pci_compat_driver;
754 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
755 * @dev: the PCI device structure to match against
756 * @drv: the device driver to search for matching PCI device id structures
758 * Used by a driver to check whether a PCI device present in the
759 * system is in its list of supported devices. Returns the matching
760 * pci_device_id structure or %NULL if there is no match.
762 static int pci_bus_match(struct device *dev, struct device_driver *drv)
764 struct pci_dev *pci_dev = to_pci_dev(dev);
765 struct pci_driver *pci_drv = to_pci_driver(drv);
766 const struct pci_device_id *found_id;
768 found_id = pci_match_device(pci_drv, pci_dev);
776 * pci_dev_get - increments the reference count of the pci device structure
777 * @dev: the device being referenced
779 * Each live reference to a device should be refcounted.
781 * Drivers for PCI devices should normally record such references in
782 * their probe() methods, when they bind to a device, and release
783 * them by calling pci_dev_put(), in their disconnect() methods.
785 * A pointer to the device with the incremented reference counter is returned.
787 struct pci_dev *pci_dev_get(struct pci_dev *dev)
790 get_device(&dev->dev);
795 * pci_dev_put - release a use of the pci device structure
796 * @dev: device that's been disconnected
798 * Must be called when a user of a device is finished with it. When the last
799 * user of the device calls this function, the memory of the device is freed.
801 void pci_dev_put(struct pci_dev *dev)
804 put_device(&dev->dev);
807 #ifndef CONFIG_HOTPLUG
808 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
814 struct bus_type pci_bus_type = {
816 .match = pci_bus_match,
817 .uevent = pci_uevent,
818 .probe = pci_device_probe,
819 .remove = pci_device_remove,
820 .shutdown = pci_device_shutdown,
821 .dev_attrs = pci_dev_attrs,
822 .pm = PCI_PM_OPS_PTR,
825 static int __init pci_driver_init(void)
827 return bus_register(&pci_bus_type);
830 postcore_initcall(pci_driver_init);
832 EXPORT_SYMBOL(pci_match_id);
833 EXPORT_SYMBOL(__pci_register_driver);
834 EXPORT_SYMBOL(pci_unregister_driver);
835 EXPORT_SYMBOL(pci_dev_driver);
836 EXPORT_SYMBOL(pci_bus_type);
837 EXPORT_SYMBOL(pci_dev_get);
838 EXPORT_SYMBOL(pci_dev_put);