[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / pci / pci-driver.c
blobe5d47be3c6d78eb66b252b25aeae8210a9141c9e
1 /*
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.
9 */
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>
19 #include <linux/cpu.h>
20 #include "pci.h"
22 struct pci_dynid {
23 struct list_head node;
24 struct pci_device_id id;
27 /**
28 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
29 * @drv: target pci driver
30 * @vendor: PCI vendor ID
31 * @device: PCI device ID
32 * @subvendor: PCI subvendor ID
33 * @subdevice: PCI subdevice ID
34 * @class: PCI class
35 * @class_mask: PCI class mask
36 * @driver_data: private driver data
38 * Adds a new dynamic pci device ID to this driver and causes the
39 * driver to probe for all devices again. @drv must have been
40 * registered prior to calling this function.
42 * CONTEXT:
43 * Does GFP_KERNEL allocation.
45 * RETURNS:
46 * 0 on success, -errno on failure.
48 int pci_add_dynid(struct pci_driver *drv,
49 unsigned int vendor, unsigned int device,
50 unsigned int subvendor, unsigned int subdevice,
51 unsigned int class, unsigned int class_mask,
52 unsigned long driver_data)
54 struct pci_dynid *dynid;
55 int retval;
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
61 dynid->id.vendor = vendor;
62 dynid->id.device = device;
63 dynid->id.subvendor = subvendor;
64 dynid->id.subdevice = subdevice;
65 dynid->id.class = class;
66 dynid->id.class_mask = class_mask;
67 dynid->id.driver_data = driver_data;
69 spin_lock(&drv->dynids.lock);
70 list_add_tail(&dynid->node, &drv->dynids.list);
71 spin_unlock(&drv->dynids.lock);
73 get_driver(&drv->driver);
74 retval = driver_attach(&drv->driver);
75 put_driver(&drv->driver);
77 return retval;
80 static void pci_free_dynids(struct pci_driver *drv)
82 struct pci_dynid *dynid, *n;
84 spin_lock(&drv->dynids.lock);
85 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
86 list_del(&dynid->node);
87 kfree(dynid);
89 spin_unlock(&drv->dynids.lock);
93 * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
95 #ifdef CONFIG_HOTPLUG
96 /**
97 * store_new_id - sysfs frontend to pci_add_dynid()
98 * @driver: target device driver
99 * @buf: buffer for scanning device ID data
100 * @count: input size
102 * Allow PCI IDs to be added to an existing driver via sysfs.
104 static ssize_t
105 store_new_id(struct device_driver *driver, const char *buf, size_t count)
107 struct pci_driver *pdrv = to_pci_driver(driver);
108 const struct pci_device_id *ids = pdrv->id_table;
109 __u32 vendor, device, subvendor=PCI_ANY_ID,
110 subdevice=PCI_ANY_ID, class=0, class_mask=0;
111 unsigned long driver_data=0;
112 int fields=0;
113 int retval;
115 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
116 &vendor, &device, &subvendor, &subdevice,
117 &class, &class_mask, &driver_data);
118 if (fields < 2)
119 return -EINVAL;
121 /* Only accept driver_data values that match an existing id_table
122 entry */
123 if (ids) {
124 retval = -EINVAL;
125 while (ids->vendor || ids->subvendor || ids->class_mask) {
126 if (driver_data == ids->driver_data) {
127 retval = 0;
128 break;
130 ids++;
132 if (retval) /* No match */
133 return retval;
136 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
137 class, class_mask, driver_data);
138 if (retval)
139 return retval;
140 return count;
142 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
145 * store_remove_id - remove a PCI device ID from this driver
146 * @driver: target device driver
147 * @buf: buffer for scanning device ID data
148 * @count: input size
150 * Removes a dynamic pci device ID to this driver.
152 static ssize_t
153 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
155 struct pci_dynid *dynid, *n;
156 struct pci_driver *pdrv = to_pci_driver(driver);
157 __u32 vendor, device, subvendor = PCI_ANY_ID,
158 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
159 int fields = 0;
160 int retval = -ENODEV;
162 fields = sscanf(buf, "%x %x %x %x %x %x",
163 &vendor, &device, &subvendor, &subdevice,
164 &class, &class_mask);
165 if (fields < 2)
166 return -EINVAL;
168 spin_lock(&pdrv->dynids.lock);
169 list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
170 struct pci_device_id *id = &dynid->id;
171 if ((id->vendor == vendor) &&
172 (id->device == device) &&
173 (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
174 (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
175 !((id->class ^ class) & class_mask)) {
176 list_del(&dynid->node);
177 kfree(dynid);
178 retval = 0;
179 break;
182 spin_unlock(&pdrv->dynids.lock);
184 if (retval)
185 return retval;
186 return count;
188 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
190 static int
191 pci_create_newid_file(struct pci_driver *drv)
193 int error = 0;
194 if (drv->probe != NULL)
195 error = driver_create_file(&drv->driver, &driver_attr_new_id);
196 return error;
199 static void pci_remove_newid_file(struct pci_driver *drv)
201 driver_remove_file(&drv->driver, &driver_attr_new_id);
204 static int
205 pci_create_removeid_file(struct pci_driver *drv)
207 int error = 0;
208 if (drv->probe != NULL)
209 error = driver_create_file(&drv->driver,&driver_attr_remove_id);
210 return error;
213 static void pci_remove_removeid_file(struct pci_driver *drv)
215 driver_remove_file(&drv->driver, &driver_attr_remove_id);
217 #else /* !CONFIG_HOTPLUG */
218 static inline int pci_create_newid_file(struct pci_driver *drv)
220 return 0;
222 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
223 static inline int pci_create_removeid_file(struct pci_driver *drv)
225 return 0;
227 static inline void pci_remove_removeid_file(struct pci_driver *drv) {}
228 #endif
231 * pci_match_id - See if a pci device matches a given pci_id table
232 * @ids: array of PCI device id structures to search in
233 * @dev: the PCI device structure to match against.
235 * Used by a driver to check whether a PCI device present in the
236 * system is in its list of supported devices. Returns the matching
237 * pci_device_id structure or %NULL if there is no match.
239 * Deprecated, don't use this as it will not catch any dynamic ids
240 * that a driver might want to check for.
242 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
243 struct pci_dev *dev)
245 if (ids) {
246 while (ids->vendor || ids->subvendor || ids->class_mask) {
247 if (pci_match_one_device(ids, dev))
248 return ids;
249 ids++;
252 return NULL;
256 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
257 * @drv: the PCI driver to match against
258 * @dev: the PCI device structure to match against
260 * Used by a driver to check whether a PCI device present in the
261 * system is in its list of supported devices. Returns the matching
262 * pci_device_id structure or %NULL if there is no match.
264 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
265 struct pci_dev *dev)
267 struct pci_dynid *dynid;
269 /* Look at the dynamic ids first, before the static ones */
270 spin_lock(&drv->dynids.lock);
271 list_for_each_entry(dynid, &drv->dynids.list, node) {
272 if (pci_match_one_device(&dynid->id, dev)) {
273 spin_unlock(&drv->dynids.lock);
274 return &dynid->id;
277 spin_unlock(&drv->dynids.lock);
279 return pci_match_id(drv->id_table, dev);
282 struct drv_dev_and_id {
283 struct pci_driver *drv;
284 struct pci_dev *dev;
285 const struct pci_device_id *id;
288 static long local_pci_probe(void *_ddi)
290 struct drv_dev_and_id *ddi = _ddi;
292 return ddi->drv->probe(ddi->dev, ddi->id);
295 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
296 const struct pci_device_id *id)
298 int error, node;
299 struct drv_dev_and_id ddi = { drv, dev, id };
301 /* Execute driver initialization on node where the device's
302 bus is attached to. This way the driver likely allocates
303 its local memory on the right node without any need to
304 change it. */
305 node = dev_to_node(&dev->dev);
306 if (node >= 0) {
307 int cpu;
309 get_online_cpus();
310 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
311 if (cpu < nr_cpu_ids)
312 error = work_on_cpu(cpu, local_pci_probe, &ddi);
313 else
314 error = local_pci_probe(&ddi);
315 put_online_cpus();
316 } else
317 error = local_pci_probe(&ddi);
318 return error;
322 * __pci_device_probe()
323 * @drv: driver to call to check if it wants the PCI device
324 * @pci_dev: PCI device being probed
326 * returns 0 on success, else error.
327 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
329 static int
330 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
332 const struct pci_device_id *id;
333 int error = 0;
335 if (!pci_dev->driver && drv->probe) {
336 error = -ENODEV;
338 id = pci_match_device(drv, pci_dev);
339 if (id)
340 error = pci_call_probe(drv, pci_dev, id);
341 if (error >= 0) {
342 pci_dev->driver = drv;
343 error = 0;
346 return error;
349 static int pci_device_probe(struct device * dev)
351 int error = 0;
352 struct pci_driver *drv;
353 struct pci_dev *pci_dev;
355 drv = to_pci_driver(dev->driver);
356 pci_dev = to_pci_dev(dev);
357 pci_dev_get(pci_dev);
358 error = __pci_device_probe(drv, pci_dev);
359 if (error)
360 pci_dev_put(pci_dev);
362 return error;
365 static int pci_device_remove(struct device * dev)
367 struct pci_dev * pci_dev = to_pci_dev(dev);
368 struct pci_driver * drv = pci_dev->driver;
370 if (drv) {
371 if (drv->remove)
372 drv->remove(pci_dev);
373 pci_dev->driver = NULL;
377 * If the device is still on, set the power state as "unknown",
378 * since it might change by the next time we load the driver.
380 if (pci_dev->current_state == PCI_D0)
381 pci_dev->current_state = PCI_UNKNOWN;
384 * We would love to complain here if pci_dev->is_enabled is set, that
385 * the driver should have called pci_disable_device(), but the
386 * unfortunate fact is there are too many odd BIOS and bridge setups
387 * that don't like drivers doing that all of the time.
388 * Oh well, we can dream of sane hardware when we sleep, no matter how
389 * horrible the crap we have to deal with is when we are awake...
392 pci_dev_put(pci_dev);
393 return 0;
396 static void pci_device_shutdown(struct device *dev)
398 struct pci_dev *pci_dev = to_pci_dev(dev);
399 struct pci_driver *drv = pci_dev->driver;
401 if (drv && drv->shutdown)
402 drv->shutdown(pci_dev);
403 pci_msi_shutdown(pci_dev);
404 pci_msix_shutdown(pci_dev);
407 #ifdef CONFIG_PM_SLEEP
410 * Default "suspend" method for devices that have no driver provided suspend,
411 * or not even a driver at all (second part).
413 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
416 * mark its power state as "unknown", since we don't know if
417 * e.g. the BIOS will change its device state when we suspend.
419 if (pci_dev->current_state == PCI_D0)
420 pci_dev->current_state = PCI_UNKNOWN;
424 * Default "resume" method for devices that have no driver provided resume,
425 * or not even a driver at all (second part).
427 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
429 int retval;
431 /* if the device was enabled before suspend, reenable */
432 retval = pci_reenable_device(pci_dev);
434 * if the device was busmaster before the suspend, make it busmaster
435 * again
437 if (pci_dev->is_busmaster)
438 pci_set_master(pci_dev);
440 return retval;
443 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
445 struct pci_dev * pci_dev = to_pci_dev(dev);
446 struct pci_driver * drv = pci_dev->driver;
448 if (drv && drv->suspend) {
449 pci_power_t prev = pci_dev->current_state;
450 int error;
452 error = drv->suspend(pci_dev, state);
453 suspend_report_result(drv->suspend, error);
454 if (error)
455 return error;
457 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
458 && pci_dev->current_state != PCI_UNKNOWN) {
459 WARN_ONCE(pci_dev->current_state != prev,
460 "PCI PM: Device state not saved by %pF\n",
461 drv->suspend);
465 pci_fixup_device(pci_fixup_suspend, pci_dev);
467 return 0;
470 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
472 struct pci_dev * pci_dev = to_pci_dev(dev);
473 struct pci_driver * drv = pci_dev->driver;
475 if (drv && drv->suspend_late) {
476 pci_power_t prev = pci_dev->current_state;
477 int error;
479 error = drv->suspend_late(pci_dev, state);
480 suspend_report_result(drv->suspend_late, error);
481 if (error)
482 return error;
484 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
485 && pci_dev->current_state != PCI_UNKNOWN) {
486 WARN_ONCE(pci_dev->current_state != prev,
487 "PCI PM: Device state not saved by %pF\n",
488 drv->suspend_late);
489 return 0;
493 if (!pci_dev->state_saved)
494 pci_save_state(pci_dev);
496 pci_pm_set_unknown_state(pci_dev);
498 return 0;
501 static int pci_legacy_resume_early(struct device *dev)
503 struct pci_dev * pci_dev = to_pci_dev(dev);
504 struct pci_driver * drv = pci_dev->driver;
506 return drv && drv->resume_early ?
507 drv->resume_early(pci_dev) : 0;
510 static int pci_legacy_resume(struct device *dev)
512 struct pci_dev * pci_dev = to_pci_dev(dev);
513 struct pci_driver * drv = pci_dev->driver;
515 pci_fixup_device(pci_fixup_resume, pci_dev);
517 return drv && drv->resume ?
518 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
521 /* Auxiliary functions used by the new power management framework */
524 * pci_restore_standard_config - restore standard config registers of PCI device
525 * @pci_dev: PCI device to handle
527 static int pci_restore_standard_config(struct pci_dev *pci_dev)
529 pci_update_current_state(pci_dev, PCI_UNKNOWN);
531 if (pci_dev->current_state != PCI_D0) {
532 int error = pci_set_power_state(pci_dev, PCI_D0);
533 if (error)
534 return error;
537 return pci_restore_state(pci_dev);
540 static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
542 pci_restore_standard_config(pci_dev);
543 pci_fixup_device(pci_fixup_resume_early, pci_dev);
546 static void pci_pm_default_resume(struct pci_dev *pci_dev)
548 pci_fixup_device(pci_fixup_resume, pci_dev);
550 if (!pci_is_bridge(pci_dev))
551 pci_enable_wake(pci_dev, PCI_D0, false);
554 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
556 /* Disable non-bridge devices without PM support */
557 if (!pci_is_bridge(pci_dev))
558 pci_disable_enabled_device(pci_dev);
561 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
563 struct pci_driver *drv = pci_dev->driver;
564 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
565 || drv->resume_early);
568 * Legacy PM support is used by default, so warn if the new framework is
569 * supported as well. Drivers are supposed to support either the
570 * former, or the latter, but not both at the same time.
572 WARN_ON(ret && drv->driver.pm);
574 return ret;
577 /* New power management framework */
579 static int pci_pm_prepare(struct device *dev)
581 struct device_driver *drv = dev->driver;
582 int error = 0;
584 if (drv && drv->pm && drv->pm->prepare)
585 error = drv->pm->prepare(dev);
587 return error;
590 static void pci_pm_complete(struct device *dev)
592 struct device_driver *drv = dev->driver;
594 if (drv && drv->pm && drv->pm->complete)
595 drv->pm->complete(dev);
598 #ifdef CONFIG_SUSPEND
600 static int pci_pm_suspend(struct device *dev)
602 struct pci_dev *pci_dev = to_pci_dev(dev);
603 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
605 if (pci_has_legacy_pm_support(pci_dev))
606 return pci_legacy_suspend(dev, PMSG_SUSPEND);
608 if (!pm) {
609 pci_pm_default_suspend(pci_dev);
610 goto Fixup;
613 if (pm->suspend) {
614 pci_power_t prev = pci_dev->current_state;
615 int error;
617 error = pm->suspend(dev);
618 suspend_report_result(pm->suspend, error);
619 if (error)
620 return error;
622 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
623 && pci_dev->current_state != PCI_UNKNOWN) {
624 WARN_ONCE(pci_dev->current_state != prev,
625 "PCI PM: State of device not saved by %pF\n",
626 pm->suspend);
630 Fixup:
631 pci_fixup_device(pci_fixup_suspend, pci_dev);
633 return 0;
636 static int pci_pm_suspend_noirq(struct device *dev)
638 struct pci_dev *pci_dev = to_pci_dev(dev);
639 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
641 if (pci_has_legacy_pm_support(pci_dev))
642 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
644 if (!pm) {
645 pci_save_state(pci_dev);
646 return 0;
649 if (pm->suspend_noirq) {
650 pci_power_t prev = pci_dev->current_state;
651 int error;
653 error = pm->suspend_noirq(dev);
654 suspend_report_result(pm->suspend_noirq, error);
655 if (error)
656 return error;
658 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
659 && pci_dev->current_state != PCI_UNKNOWN) {
660 WARN_ONCE(pci_dev->current_state != prev,
661 "PCI PM: State of device not saved by %pF\n",
662 pm->suspend_noirq);
663 return 0;
667 if (!pci_dev->state_saved) {
668 pci_save_state(pci_dev);
669 if (!pci_is_bridge(pci_dev))
670 pci_prepare_to_sleep(pci_dev);
673 pci_pm_set_unknown_state(pci_dev);
675 return 0;
678 static int pci_pm_resume_noirq(struct device *dev)
680 struct pci_dev *pci_dev = to_pci_dev(dev);
681 struct device_driver *drv = dev->driver;
682 int error = 0;
684 pci_pm_default_resume_noirq(pci_dev);
686 if (pci_has_legacy_pm_support(pci_dev))
687 return pci_legacy_resume_early(dev);
689 if (drv && drv->pm && drv->pm->resume_noirq)
690 error = drv->pm->resume_noirq(dev);
692 return error;
695 static int pci_pm_resume(struct device *dev)
697 struct pci_dev *pci_dev = to_pci_dev(dev);
698 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
699 int error = 0;
702 * This is necessary for the suspend error path in which resume is
703 * called without restoring the standard config registers of the device.
705 if (pci_dev->state_saved)
706 pci_restore_standard_config(pci_dev);
708 if (pci_has_legacy_pm_support(pci_dev))
709 return pci_legacy_resume(dev);
711 pci_pm_default_resume(pci_dev);
713 if (pm) {
714 if (pm->resume)
715 error = pm->resume(dev);
716 } else {
717 pci_pm_reenable_device(pci_dev);
720 return error;
723 #else /* !CONFIG_SUSPEND */
725 #define pci_pm_suspend NULL
726 #define pci_pm_suspend_noirq NULL
727 #define pci_pm_resume NULL
728 #define pci_pm_resume_noirq NULL
730 #endif /* !CONFIG_SUSPEND */
732 #ifdef CONFIG_HIBERNATION
734 static int pci_pm_freeze(struct device *dev)
736 struct pci_dev *pci_dev = to_pci_dev(dev);
737 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
739 if (pci_has_legacy_pm_support(pci_dev))
740 return pci_legacy_suspend(dev, PMSG_FREEZE);
742 if (!pm) {
743 pci_pm_default_suspend(pci_dev);
744 return 0;
747 if (pm->freeze) {
748 int error;
750 error = pm->freeze(dev);
751 suspend_report_result(pm->freeze, error);
752 if (error)
753 return error;
756 return 0;
759 static int pci_pm_freeze_noirq(struct device *dev)
761 struct pci_dev *pci_dev = to_pci_dev(dev);
762 struct device_driver *drv = dev->driver;
764 if (pci_has_legacy_pm_support(pci_dev))
765 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
767 if (drv && drv->pm && drv->pm->freeze_noirq) {
768 int error;
770 error = drv->pm->freeze_noirq(dev);
771 suspend_report_result(drv->pm->freeze_noirq, error);
772 if (error)
773 return error;
776 if (!pci_dev->state_saved)
777 pci_save_state(pci_dev);
779 pci_pm_set_unknown_state(pci_dev);
781 return 0;
784 static int pci_pm_thaw_noirq(struct device *dev)
786 struct pci_dev *pci_dev = to_pci_dev(dev);
787 struct device_driver *drv = dev->driver;
788 int error = 0;
790 if (pci_has_legacy_pm_support(pci_dev))
791 return pci_legacy_resume_early(dev);
793 pci_update_current_state(pci_dev, PCI_D0);
795 if (drv && drv->pm && drv->pm->thaw_noirq)
796 error = drv->pm->thaw_noirq(dev);
798 return error;
801 static int pci_pm_thaw(struct device *dev)
803 struct pci_dev *pci_dev = to_pci_dev(dev);
804 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
805 int error = 0;
807 if (pci_has_legacy_pm_support(pci_dev))
808 return pci_legacy_resume(dev);
810 if (pm) {
811 if (pm->thaw)
812 error = pm->thaw(dev);
813 } else {
814 pci_pm_reenable_device(pci_dev);
817 pci_dev->state_saved = false;
819 return error;
822 static int pci_pm_poweroff(struct device *dev)
824 struct pci_dev *pci_dev = to_pci_dev(dev);
825 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
827 if (pci_has_legacy_pm_support(pci_dev))
828 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
830 if (!pm) {
831 pci_pm_default_suspend(pci_dev);
832 goto Fixup;
835 if (pm->poweroff) {
836 int error;
838 error = pm->poweroff(dev);
839 suspend_report_result(pm->poweroff, error);
840 if (error)
841 return error;
844 Fixup:
845 pci_fixup_device(pci_fixup_suspend, pci_dev);
847 return 0;
850 static int pci_pm_poweroff_noirq(struct device *dev)
852 struct pci_dev *pci_dev = to_pci_dev(dev);
853 struct device_driver *drv = dev->driver;
855 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
856 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
858 if (!drv || !drv->pm)
859 return 0;
861 if (drv->pm->poweroff_noirq) {
862 int error;
864 error = drv->pm->poweroff_noirq(dev);
865 suspend_report_result(drv->pm->poweroff_noirq, error);
866 if (error)
867 return error;
870 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
871 pci_prepare_to_sleep(pci_dev);
873 return 0;
876 static int pci_pm_restore_noirq(struct device *dev)
878 struct pci_dev *pci_dev = to_pci_dev(dev);
879 struct device_driver *drv = dev->driver;
880 int error = 0;
882 pci_pm_default_resume_noirq(pci_dev);
884 if (pci_has_legacy_pm_support(pci_dev))
885 return pci_legacy_resume_early(dev);
887 if (drv && drv->pm && drv->pm->restore_noirq)
888 error = drv->pm->restore_noirq(dev);
890 return error;
893 static int pci_pm_restore(struct device *dev)
895 struct pci_dev *pci_dev = to_pci_dev(dev);
896 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
897 int error = 0;
900 * This is necessary for the hibernation error path in which restore is
901 * called without restoring the standard config registers of the device.
903 if (pci_dev->state_saved)
904 pci_restore_standard_config(pci_dev);
906 if (pci_has_legacy_pm_support(pci_dev))
907 return pci_legacy_resume(dev);
909 pci_pm_default_resume(pci_dev);
911 if (pm) {
912 if (pm->restore)
913 error = pm->restore(dev);
914 } else {
915 pci_pm_reenable_device(pci_dev);
918 return error;
921 #else /* !CONFIG_HIBERNATION */
923 #define pci_pm_freeze NULL
924 #define pci_pm_freeze_noirq NULL
925 #define pci_pm_thaw NULL
926 #define pci_pm_thaw_noirq NULL
927 #define pci_pm_poweroff NULL
928 #define pci_pm_poweroff_noirq NULL
929 #define pci_pm_restore NULL
930 #define pci_pm_restore_noirq NULL
932 #endif /* !CONFIG_HIBERNATION */
934 const struct dev_pm_ops pci_dev_pm_ops = {
935 .prepare = pci_pm_prepare,
936 .complete = pci_pm_complete,
937 .suspend = pci_pm_suspend,
938 .resume = pci_pm_resume,
939 .freeze = pci_pm_freeze,
940 .thaw = pci_pm_thaw,
941 .poweroff = pci_pm_poweroff,
942 .restore = pci_pm_restore,
943 .suspend_noirq = pci_pm_suspend_noirq,
944 .resume_noirq = pci_pm_resume_noirq,
945 .freeze_noirq = pci_pm_freeze_noirq,
946 .thaw_noirq = pci_pm_thaw_noirq,
947 .poweroff_noirq = pci_pm_poweroff_noirq,
948 .restore_noirq = pci_pm_restore_noirq,
951 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
953 #else /* !CONFIG_PM_SLEEP */
955 #define PCI_PM_OPS_PTR NULL
957 #endif /* !CONFIG_PM_SLEEP */
960 * __pci_register_driver - register a new pci driver
961 * @drv: the driver structure to register
962 * @owner: owner module of drv
963 * @mod_name: module name string
965 * Adds the driver structure to the list of registered drivers.
966 * Returns a negative value on error, otherwise 0.
967 * If no error occurred, the driver remains registered even if
968 * no device was claimed during registration.
970 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
971 const char *mod_name)
973 int error;
975 /* initialize common driver fields */
976 drv->driver.name = drv->name;
977 drv->driver.bus = &pci_bus_type;
978 drv->driver.owner = owner;
979 drv->driver.mod_name = mod_name;
981 spin_lock_init(&drv->dynids.lock);
982 INIT_LIST_HEAD(&drv->dynids.list);
984 /* register with core */
985 error = driver_register(&drv->driver);
986 if (error)
987 goto out;
989 error = pci_create_newid_file(drv);
990 if (error)
991 goto out_newid;
993 error = pci_create_removeid_file(drv);
994 if (error)
995 goto out_removeid;
996 out:
997 return error;
999 out_removeid:
1000 pci_remove_newid_file(drv);
1001 out_newid:
1002 driver_unregister(&drv->driver);
1003 goto out;
1007 * pci_unregister_driver - unregister a pci driver
1008 * @drv: the driver structure to unregister
1010 * Deletes the driver structure from the list of registered PCI drivers,
1011 * gives it a chance to clean up by calling its remove() function for
1012 * each device it was responsible for, and marks those devices as
1013 * driverless.
1016 void
1017 pci_unregister_driver(struct pci_driver *drv)
1019 pci_remove_removeid_file(drv);
1020 pci_remove_newid_file(drv);
1021 driver_unregister(&drv->driver);
1022 pci_free_dynids(drv);
1025 static struct pci_driver pci_compat_driver = {
1026 .name = "compat"
1030 * pci_dev_driver - get the pci_driver of a device
1031 * @dev: the device to query
1033 * Returns the appropriate pci_driver structure or %NULL if there is no
1034 * registered driver for the device.
1036 struct pci_driver *
1037 pci_dev_driver(const struct pci_dev *dev)
1039 if (dev->driver)
1040 return dev->driver;
1041 else {
1042 int i;
1043 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1044 if (dev->resource[i].flags & IORESOURCE_BUSY)
1045 return &pci_compat_driver;
1047 return NULL;
1051 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1052 * @dev: the PCI device structure to match against
1053 * @drv: the device driver to search for matching PCI device id structures
1055 * Used by a driver to check whether a PCI device present in the
1056 * system is in its list of supported devices. Returns the matching
1057 * pci_device_id structure or %NULL if there is no match.
1059 static int pci_bus_match(struct device *dev, struct device_driver *drv)
1061 struct pci_dev *pci_dev = to_pci_dev(dev);
1062 struct pci_driver *pci_drv = to_pci_driver(drv);
1063 const struct pci_device_id *found_id;
1065 found_id = pci_match_device(pci_drv, pci_dev);
1066 if (found_id)
1067 return 1;
1069 return 0;
1073 * pci_dev_get - increments the reference count of the pci device structure
1074 * @dev: the device being referenced
1076 * Each live reference to a device should be refcounted.
1078 * Drivers for PCI devices should normally record such references in
1079 * their probe() methods, when they bind to a device, and release
1080 * them by calling pci_dev_put(), in their disconnect() methods.
1082 * A pointer to the device with the incremented reference counter is returned.
1084 struct pci_dev *pci_dev_get(struct pci_dev *dev)
1086 if (dev)
1087 get_device(&dev->dev);
1088 return dev;
1092 * pci_dev_put - release a use of the pci device structure
1093 * @dev: device that's been disconnected
1095 * Must be called when a user of a device is finished with it. When the last
1096 * user of the device calls this function, the memory of the device is freed.
1098 void pci_dev_put(struct pci_dev *dev)
1100 if (dev)
1101 put_device(&dev->dev);
1104 #ifndef CONFIG_HOTPLUG
1105 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1107 return -ENODEV;
1109 #endif
1111 struct bus_type pci_bus_type = {
1112 .name = "pci",
1113 .match = pci_bus_match,
1114 .uevent = pci_uevent,
1115 .probe = pci_device_probe,
1116 .remove = pci_device_remove,
1117 .shutdown = pci_device_shutdown,
1118 .dev_attrs = pci_dev_attrs,
1119 .bus_attrs = pci_bus_attrs,
1120 .pm = PCI_PM_OPS_PTR,
1123 static int __init pci_driver_init(void)
1125 return bus_register(&pci_bus_type);
1128 postcore_initcall(pci_driver_init);
1130 EXPORT_SYMBOL_GPL(pci_add_dynid);
1131 EXPORT_SYMBOL(pci_match_id);
1132 EXPORT_SYMBOL(__pci_register_driver);
1133 EXPORT_SYMBOL(pci_unregister_driver);
1134 EXPORT_SYMBOL(pci_dev_driver);
1135 EXPORT_SYMBOL(pci_bus_type);
1136 EXPORT_SYMBOL(pci_dev_get);
1137 EXPORT_SYMBOL(pci_dev_put);