]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/pci/pci-driver.c
PCI/PM: Keep runtime PM enabled for unbound PCI devices
[linux.git] / drivers / pci / pci-driver.c
1 /*
2  * drivers/pci/pci-driver.c
3  *
4  * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5  * (C) Copyright 2007 Novell Inc.
6  *
7  * Released under the GPL v2 only.
8  *
9  */
10
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 <linux/pm_runtime.h>
21 #include <linux/suspend.h>
22 #include "pci.h"
23
24 struct pci_dynid {
25         struct list_head node;
26         struct pci_device_id id;
27 };
28
29 /**
30  * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
31  * @drv: target pci driver
32  * @vendor: PCI vendor ID
33  * @device: PCI device ID
34  * @subvendor: PCI subvendor ID
35  * @subdevice: PCI subdevice ID
36  * @class: PCI class
37  * @class_mask: PCI class mask
38  * @driver_data: private driver data
39  *
40  * Adds a new dynamic pci device ID to this driver and causes the
41  * driver to probe for all devices again.  @drv must have been
42  * registered prior to calling this function.
43  *
44  * CONTEXT:
45  * Does GFP_KERNEL allocation.
46  *
47  * RETURNS:
48  * 0 on success, -errno on failure.
49  */
50 int pci_add_dynid(struct pci_driver *drv,
51                   unsigned int vendor, unsigned int device,
52                   unsigned int subvendor, unsigned int subdevice,
53                   unsigned int class, unsigned int class_mask,
54                   unsigned long driver_data)
55 {
56         struct pci_dynid *dynid;
57         int retval;
58
59         dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
60         if (!dynid)
61                 return -ENOMEM;
62
63         dynid->id.vendor = vendor;
64         dynid->id.device = device;
65         dynid->id.subvendor = subvendor;
66         dynid->id.subdevice = subdevice;
67         dynid->id.class = class;
68         dynid->id.class_mask = class_mask;
69         dynid->id.driver_data = driver_data;
70
71         spin_lock(&drv->dynids.lock);
72         list_add_tail(&dynid->node, &drv->dynids.list);
73         spin_unlock(&drv->dynids.lock);
74
75         retval = driver_attach(&drv->driver);
76
77         return retval;
78 }
79
80 static void pci_free_dynids(struct pci_driver *drv)
81 {
82         struct pci_dynid *dynid, *n;
83
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);
88         }
89         spin_unlock(&drv->dynids.lock);
90 }
91
92 /*
93  * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
94  */
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
101  *
102  * Allow PCI IDs to be added to an existing driver via sysfs.
103  */
104 static ssize_t
105 store_new_id(struct device_driver *driver, const char *buf, size_t count)
106 {
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;
114
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;
120
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;
129                         }
130                         ids++;
131                 }
132                 if (retval)     /* No match */
133                         return retval;
134         }
135
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;
141 }
142
143 /**
144  * store_remove_id - remove a PCI device ID from this driver
145  * @driver: target device driver
146  * @buf: buffer for scanning device ID data
147  * @count: input size
148  *
149  * Removes a dynamic pci device ID to this driver.
150  */
151 static ssize_t
152 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
153 {
154         struct pci_dynid *dynid, *n;
155         struct pci_driver *pdrv = to_pci_driver(driver);
156         __u32 vendor, device, subvendor = PCI_ANY_ID,
157                 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
158         int fields = 0;
159         int retval = -ENODEV;
160
161         fields = sscanf(buf, "%x %x %x %x %x %x",
162                         &vendor, &device, &subvendor, &subdevice,
163                         &class, &class_mask);
164         if (fields < 2)
165                 return -EINVAL;
166
167         spin_lock(&pdrv->dynids.lock);
168         list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
169                 struct pci_device_id *id = &dynid->id;
170                 if ((id->vendor == vendor) &&
171                     (id->device == device) &&
172                     (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
173                     (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
174                     !((id->class ^ class) & class_mask)) {
175                         list_del(&dynid->node);
176                         kfree(dynid);
177                         retval = 0;
178                         break;
179                 }
180         }
181         spin_unlock(&pdrv->dynids.lock);
182
183         if (retval)
184                 return retval;
185         return count;
186 }
187
188 static struct driver_attribute pci_drv_attrs[] = {
189         __ATTR(new_id, S_IWUSR, NULL, store_new_id),
190         __ATTR(remove_id, S_IWUSR, NULL, store_remove_id),
191         __ATTR_NULL,
192 };
193
194 #else
195 #define pci_drv_attrs   NULL
196 #endif /* CONFIG_HOTPLUG */
197
198 /**
199  * pci_match_id - See if a pci device matches a given pci_id table
200  * @ids: array of PCI device id structures to search in
201  * @dev: the PCI device structure to match against.
202  *
203  * Used by a driver to check whether a PCI device present in the
204  * system is in its list of supported devices.  Returns the matching
205  * pci_device_id structure or %NULL if there is no match.
206  *
207  * Deprecated, don't use this as it will not catch any dynamic ids
208  * that a driver might want to check for.
209  */
210 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
211                                          struct pci_dev *dev)
212 {
213         if (ids) {
214                 while (ids->vendor || ids->subvendor || ids->class_mask) {
215                         if (pci_match_one_device(ids, dev))
216                                 return ids;
217                         ids++;
218                 }
219         }
220         return NULL;
221 }
222
223 /**
224  * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
225  * @drv: the PCI driver to match against
226  * @dev: the PCI device structure to match against
227  *
228  * Used by a driver to check whether a PCI device present in the
229  * system is in its list of supported devices.  Returns the matching
230  * pci_device_id structure or %NULL if there is no match.
231  */
232 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
233                                                     struct pci_dev *dev)
234 {
235         struct pci_dynid *dynid;
236
237         /* Look at the dynamic ids first, before the static ones */
238         spin_lock(&drv->dynids.lock);
239         list_for_each_entry(dynid, &drv->dynids.list, node) {
240                 if (pci_match_one_device(&dynid->id, dev)) {
241                         spin_unlock(&drv->dynids.lock);
242                         return &dynid->id;
243                 }
244         }
245         spin_unlock(&drv->dynids.lock);
246
247         return pci_match_id(drv->id_table, dev);
248 }
249
250 struct drv_dev_and_id {
251         struct pci_driver *drv;
252         struct pci_dev *dev;
253         const struct pci_device_id *id;
254 };
255
256 static long local_pci_probe(void *_ddi)
257 {
258         struct drv_dev_and_id *ddi = _ddi;
259         struct pci_dev *pci_dev = ddi->dev;
260         struct pci_driver *pci_drv = ddi->drv;
261         struct device *dev = &pci_dev->dev;
262         int rc;
263
264         /*
265          * Unbound PCI devices are always put in D0, regardless of
266          * runtime PM status.  During probe, the device is set to
267          * active and the usage count is incremented.  If the driver
268          * supports runtime PM, it should call pm_runtime_put_noidle()
269          * in its probe routine and pm_runtime_get_noresume() in its
270          * remove routine.
271          */
272         pm_runtime_get_sync(dev);
273         pci_dev->driver = pci_drv;
274         rc = pci_drv->probe(pci_dev, ddi->id);
275         if (rc) {
276                 pci_dev->driver = NULL;
277                 pm_runtime_put_sync(dev);
278         }
279         return rc;
280 }
281
282 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
283                           const struct pci_device_id *id)
284 {
285         int error, node;
286         struct drv_dev_and_id ddi = { drv, dev, id };
287
288         /* Execute driver initialization on node where the device's
289            bus is attached to.  This way the driver likely allocates
290            its local memory on the right node without any need to
291            change it. */
292         node = dev_to_node(&dev->dev);
293         if (node >= 0) {
294                 int cpu;
295
296                 get_online_cpus();
297                 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
298                 if (cpu < nr_cpu_ids)
299                         error = work_on_cpu(cpu, local_pci_probe, &ddi);
300                 else
301                         error = local_pci_probe(&ddi);
302                 put_online_cpus();
303         } else
304                 error = local_pci_probe(&ddi);
305         return error;
306 }
307
308 /**
309  * __pci_device_probe - check if a driver wants to claim a specific PCI device
310  * @drv: driver to call to check if it wants the PCI device
311  * @pci_dev: PCI device being probed
312  * 
313  * returns 0 on success, else error.
314  * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
315  */
316 static int
317 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
318 {
319         const struct pci_device_id *id;
320         int error = 0;
321
322         if (!pci_dev->driver && drv->probe) {
323                 error = -ENODEV;
324
325                 id = pci_match_device(drv, pci_dev);
326                 if (id)
327                         error = pci_call_probe(drv, pci_dev, id);
328                 if (error >= 0)
329                         error = 0;
330         }
331         return error;
332 }
333
334 static int pci_device_probe(struct device * dev)
335 {
336         int error = 0;
337         struct pci_driver *drv;
338         struct pci_dev *pci_dev;
339
340         drv = to_pci_driver(dev->driver);
341         pci_dev = to_pci_dev(dev);
342         pci_dev_get(pci_dev);
343         error = __pci_device_probe(drv, pci_dev);
344         if (error)
345                 pci_dev_put(pci_dev);
346
347         return error;
348 }
349
350 static int pci_device_remove(struct device * dev)
351 {
352         struct pci_dev * pci_dev = to_pci_dev(dev);
353         struct pci_driver * drv = pci_dev->driver;
354
355         if (drv) {
356                 if (drv->remove) {
357                         pm_runtime_get_sync(dev);
358                         drv->remove(pci_dev);
359                         pm_runtime_put_noidle(dev);
360                 }
361                 pci_dev->driver = NULL;
362         }
363
364         /* Undo the runtime PM settings in local_pci_probe() */
365         pm_runtime_put_sync(dev);
366
367         /*
368          * If the device is still on, set the power state as "unknown",
369          * since it might change by the next time we load the driver.
370          */
371         if (pci_dev->current_state == PCI_D0)
372                 pci_dev->current_state = PCI_UNKNOWN;
373
374         /*
375          * We would love to complain here if pci_dev->is_enabled is set, that
376          * the driver should have called pci_disable_device(), but the
377          * unfortunate fact is there are too many odd BIOS and bridge setups
378          * that don't like drivers doing that all of the time.  
379          * Oh well, we can dream of sane hardware when we sleep, no matter how
380          * horrible the crap we have to deal with is when we are awake...
381          */
382
383         pci_dev_put(pci_dev);
384         return 0;
385 }
386
387 static void pci_device_shutdown(struct device *dev)
388 {
389         struct pci_dev *pci_dev = to_pci_dev(dev);
390         struct pci_driver *drv = pci_dev->driver;
391
392         pm_runtime_resume(dev);
393
394         if (drv && drv->shutdown)
395                 drv->shutdown(pci_dev);
396         pci_msi_shutdown(pci_dev);
397         pci_msix_shutdown(pci_dev);
398
399         /*
400          * Turn off Bus Master bit on the device to tell it to not
401          * continue to do DMA
402          */
403         pci_disable_device(pci_dev);
404 }
405
406 #ifdef CONFIG_PM
407
408 /* Auxiliary functions used for system resume and run-time resume. */
409
410 /**
411  * pci_restore_standard_config - restore standard config registers of PCI device
412  * @pci_dev: PCI device to handle
413  */
414 static int pci_restore_standard_config(struct pci_dev *pci_dev)
415 {
416         pci_update_current_state(pci_dev, PCI_UNKNOWN);
417
418         if (pci_dev->current_state != PCI_D0) {
419                 int error = pci_set_power_state(pci_dev, PCI_D0);
420                 if (error)
421                         return error;
422         }
423
424         pci_restore_state(pci_dev);
425         return 0;
426 }
427
428 #endif
429
430 #ifdef CONFIG_PM_SLEEP
431
432 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
433 {
434         pci_power_up(pci_dev);
435         pci_restore_state(pci_dev);
436         pci_fixup_device(pci_fixup_resume_early, pci_dev);
437 }
438
439 /*
440  * Default "suspend" method for devices that have no driver provided suspend,
441  * or not even a driver at all (second part).
442  */
443 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
444 {
445         /*
446          * mark its power state as "unknown", since we don't know if
447          * e.g. the BIOS will change its device state when we suspend.
448          */
449         if (pci_dev->current_state == PCI_D0)
450                 pci_dev->current_state = PCI_UNKNOWN;
451 }
452
453 /*
454  * Default "resume" method for devices that have no driver provided resume,
455  * or not even a driver at all (second part).
456  */
457 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
458 {
459         int retval;
460
461         /* if the device was enabled before suspend, reenable */
462         retval = pci_reenable_device(pci_dev);
463         /*
464          * if the device was busmaster before the suspend, make it busmaster
465          * again
466          */
467         if (pci_dev->is_busmaster)
468                 pci_set_master(pci_dev);
469
470         return retval;
471 }
472
473 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
474 {
475         struct pci_dev * pci_dev = to_pci_dev(dev);
476         struct pci_driver * drv = pci_dev->driver;
477
478         if (drv && drv->suspend) {
479                 pci_power_t prev = pci_dev->current_state;
480                 int error;
481
482                 error = drv->suspend(pci_dev, state);
483                 suspend_report_result(drv->suspend, error);
484                 if (error)
485                         return error;
486
487                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
488                     && pci_dev->current_state != PCI_UNKNOWN) {
489                         WARN_ONCE(pci_dev->current_state != prev,
490                                 "PCI PM: Device state not saved by %pF\n",
491                                 drv->suspend);
492                 }
493         }
494
495         pci_fixup_device(pci_fixup_suspend, pci_dev);
496
497         return 0;
498 }
499
500 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
501 {
502         struct pci_dev * pci_dev = to_pci_dev(dev);
503         struct pci_driver * drv = pci_dev->driver;
504
505         if (drv && drv->suspend_late) {
506                 pci_power_t prev = pci_dev->current_state;
507                 int error;
508
509                 error = drv->suspend_late(pci_dev, state);
510                 suspend_report_result(drv->suspend_late, error);
511                 if (error)
512                         return error;
513
514                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
515                     && pci_dev->current_state != PCI_UNKNOWN) {
516                         WARN_ONCE(pci_dev->current_state != prev,
517                                 "PCI PM: Device state not saved by %pF\n",
518                                 drv->suspend_late);
519                         return 0;
520                 }
521         }
522
523         if (!pci_dev->state_saved)
524                 pci_save_state(pci_dev);
525
526         pci_pm_set_unknown_state(pci_dev);
527
528         return 0;
529 }
530
531 static int pci_legacy_resume_early(struct device *dev)
532 {
533         struct pci_dev * pci_dev = to_pci_dev(dev);
534         struct pci_driver * drv = pci_dev->driver;
535
536         return drv && drv->resume_early ?
537                         drv->resume_early(pci_dev) : 0;
538 }
539
540 static int pci_legacy_resume(struct device *dev)
541 {
542         struct pci_dev * pci_dev = to_pci_dev(dev);
543         struct pci_driver * drv = pci_dev->driver;
544
545         pci_fixup_device(pci_fixup_resume, pci_dev);
546
547         return drv && drv->resume ?
548                         drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
549 }
550
551 /* Auxiliary functions used by the new power management framework */
552
553 static void pci_pm_default_resume(struct pci_dev *pci_dev)
554 {
555         pci_fixup_device(pci_fixup_resume, pci_dev);
556
557         if (!pci_is_bridge(pci_dev))
558                 pci_enable_wake(pci_dev, PCI_D0, false);
559 }
560
561 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
562 {
563         /* Disable non-bridge devices without PM support */
564         if (!pci_is_bridge(pci_dev))
565                 pci_disable_enabled_device(pci_dev);
566 }
567
568 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
569 {
570         struct pci_driver *drv = pci_dev->driver;
571         bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
572                 || drv->resume_early);
573
574         /*
575          * Legacy PM support is used by default, so warn if the new framework is
576          * supported as well.  Drivers are supposed to support either the
577          * former, or the latter, but not both at the same time.
578          */
579         WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
580                 drv->name, pci_dev->vendor, pci_dev->device);
581
582         return ret;
583 }
584
585 /* New power management framework */
586
587 static int pci_pm_prepare(struct device *dev)
588 {
589         struct device_driver *drv = dev->driver;
590         int error = 0;
591
592         /*
593          * PCI devices suspended at run time need to be resumed at this
594          * point, because in general it is necessary to reconfigure them for
595          * system suspend.  Namely, if the device is supposed to wake up the
596          * system from the sleep state, we may need to reconfigure it for this
597          * purpose.  In turn, if the device is not supposed to wake up the
598          * system from the sleep state, we'll have to prevent it from signaling
599          * wake-up.
600          */
601         pm_runtime_resume(dev);
602
603         if (drv && drv->pm && drv->pm->prepare)
604                 error = drv->pm->prepare(dev);
605
606         return error;
607 }
608
609 static void pci_pm_complete(struct device *dev)
610 {
611         struct device_driver *drv = dev->driver;
612
613         if (drv && drv->pm && drv->pm->complete)
614                 drv->pm->complete(dev);
615 }
616
617 #else /* !CONFIG_PM_SLEEP */
618
619 #define pci_pm_prepare  NULL
620 #define pci_pm_complete NULL
621
622 #endif /* !CONFIG_PM_SLEEP */
623
624 #ifdef CONFIG_SUSPEND
625
626 static int pci_pm_suspend(struct device *dev)
627 {
628         struct pci_dev *pci_dev = to_pci_dev(dev);
629         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
630
631         if (pci_has_legacy_pm_support(pci_dev))
632                 return pci_legacy_suspend(dev, PMSG_SUSPEND);
633
634         if (!pm) {
635                 pci_pm_default_suspend(pci_dev);
636                 goto Fixup;
637         }
638
639         if (pm->suspend) {
640                 pci_power_t prev = pci_dev->current_state;
641                 int error;
642
643                 error = pm->suspend(dev);
644                 suspend_report_result(pm->suspend, error);
645                 if (error)
646                         return error;
647
648                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
649                     && pci_dev->current_state != PCI_UNKNOWN) {
650                         WARN_ONCE(pci_dev->current_state != prev,
651                                 "PCI PM: State of device not saved by %pF\n",
652                                 pm->suspend);
653                 }
654         }
655
656  Fixup:
657         pci_fixup_device(pci_fixup_suspend, pci_dev);
658
659         return 0;
660 }
661
662 static int pci_pm_suspend_noirq(struct device *dev)
663 {
664         struct pci_dev *pci_dev = to_pci_dev(dev);
665         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
666
667         if (pci_has_legacy_pm_support(pci_dev))
668                 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
669
670         if (!pm) {
671                 pci_save_state(pci_dev);
672                 return 0;
673         }
674
675         if (pm->suspend_noirq) {
676                 pci_power_t prev = pci_dev->current_state;
677                 int error;
678
679                 error = pm->suspend_noirq(dev);
680                 suspend_report_result(pm->suspend_noirq, error);
681                 if (error)
682                         return error;
683
684                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
685                     && pci_dev->current_state != PCI_UNKNOWN) {
686                         WARN_ONCE(pci_dev->current_state != prev,
687                                 "PCI PM: State of device not saved by %pF\n",
688                                 pm->suspend_noirq);
689                         return 0;
690                 }
691         }
692
693         if (!pci_dev->state_saved) {
694                 pci_save_state(pci_dev);
695                 if (!pci_is_bridge(pci_dev))
696                         pci_prepare_to_sleep(pci_dev);
697         }
698
699         pci_pm_set_unknown_state(pci_dev);
700
701         /*
702          * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
703          * PCI COMMAND register isn't 0, the BIOS assumes that the controller
704          * hasn't been quiesced and tries to turn it off.  If the controller
705          * is already in D3, this can hang or cause memory corruption.
706          *
707          * Since the value of the COMMAND register doesn't matter once the
708          * device has been suspended, we can safely set it to 0 here.
709          */
710         if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
711                 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
712
713         return 0;
714 }
715
716 static int pci_pm_resume_noirq(struct device *dev)
717 {
718         struct pci_dev *pci_dev = to_pci_dev(dev);
719         struct device_driver *drv = dev->driver;
720         int error = 0;
721
722         pci_pm_default_resume_early(pci_dev);
723
724         if (pci_has_legacy_pm_support(pci_dev))
725                 return pci_legacy_resume_early(dev);
726
727         if (drv && drv->pm && drv->pm->resume_noirq)
728                 error = drv->pm->resume_noirq(dev);
729
730         return error;
731 }
732
733 static int pci_pm_resume(struct device *dev)
734 {
735         struct pci_dev *pci_dev = to_pci_dev(dev);
736         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
737         int error = 0;
738
739         /*
740          * This is necessary for the suspend error path in which resume is
741          * called without restoring the standard config registers of the device.
742          */
743         if (pci_dev->state_saved)
744                 pci_restore_standard_config(pci_dev);
745
746         if (pci_has_legacy_pm_support(pci_dev))
747                 return pci_legacy_resume(dev);
748
749         pci_pm_default_resume(pci_dev);
750
751         if (pm) {
752                 if (pm->resume)
753                         error = pm->resume(dev);
754         } else {
755                 pci_pm_reenable_device(pci_dev);
756         }
757
758         return error;
759 }
760
761 #else /* !CONFIG_SUSPEND */
762
763 #define pci_pm_suspend          NULL
764 #define pci_pm_suspend_noirq    NULL
765 #define pci_pm_resume           NULL
766 #define pci_pm_resume_noirq     NULL
767
768 #endif /* !CONFIG_SUSPEND */
769
770 #ifdef CONFIG_HIBERNATE_CALLBACKS
771
772 static int pci_pm_freeze(struct device *dev)
773 {
774         struct pci_dev *pci_dev = to_pci_dev(dev);
775         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
776
777         if (pci_has_legacy_pm_support(pci_dev))
778                 return pci_legacy_suspend(dev, PMSG_FREEZE);
779
780         if (!pm) {
781                 pci_pm_default_suspend(pci_dev);
782                 return 0;
783         }
784
785         if (pm->freeze) {
786                 int error;
787
788                 error = pm->freeze(dev);
789                 suspend_report_result(pm->freeze, error);
790                 if (error)
791                         return error;
792         }
793
794         return 0;
795 }
796
797 static int pci_pm_freeze_noirq(struct device *dev)
798 {
799         struct pci_dev *pci_dev = to_pci_dev(dev);
800         struct device_driver *drv = dev->driver;
801
802         if (pci_has_legacy_pm_support(pci_dev))
803                 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
804
805         if (drv && drv->pm && drv->pm->freeze_noirq) {
806                 int error;
807
808                 error = drv->pm->freeze_noirq(dev);
809                 suspend_report_result(drv->pm->freeze_noirq, error);
810                 if (error)
811                         return error;
812         }
813
814         if (!pci_dev->state_saved)
815                 pci_save_state(pci_dev);
816
817         pci_pm_set_unknown_state(pci_dev);
818
819         return 0;
820 }
821
822 static int pci_pm_thaw_noirq(struct device *dev)
823 {
824         struct pci_dev *pci_dev = to_pci_dev(dev);
825         struct device_driver *drv = dev->driver;
826         int error = 0;
827
828         if (pci_has_legacy_pm_support(pci_dev))
829                 return pci_legacy_resume_early(dev);
830
831         pci_update_current_state(pci_dev, PCI_D0);
832
833         if (drv && drv->pm && drv->pm->thaw_noirq)
834                 error = drv->pm->thaw_noirq(dev);
835
836         return error;
837 }
838
839 static int pci_pm_thaw(struct device *dev)
840 {
841         struct pci_dev *pci_dev = to_pci_dev(dev);
842         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
843         int error = 0;
844
845         if (pci_has_legacy_pm_support(pci_dev))
846                 return pci_legacy_resume(dev);
847
848         if (pm) {
849                 if (pm->thaw)
850                         error = pm->thaw(dev);
851         } else {
852                 pci_pm_reenable_device(pci_dev);
853         }
854
855         pci_dev->state_saved = false;
856
857         return error;
858 }
859
860 static int pci_pm_poweroff(struct device *dev)
861 {
862         struct pci_dev *pci_dev = to_pci_dev(dev);
863         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
864
865         if (pci_has_legacy_pm_support(pci_dev))
866                 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
867
868         if (!pm) {
869                 pci_pm_default_suspend(pci_dev);
870                 goto Fixup;
871         }
872
873         if (pm->poweroff) {
874                 int error;
875
876                 error = pm->poweroff(dev);
877                 suspend_report_result(pm->poweroff, error);
878                 if (error)
879                         return error;
880         }
881
882  Fixup:
883         pci_fixup_device(pci_fixup_suspend, pci_dev);
884
885         return 0;
886 }
887
888 static int pci_pm_poweroff_noirq(struct device *dev)
889 {
890         struct pci_dev *pci_dev = to_pci_dev(dev);
891         struct device_driver *drv = dev->driver;
892
893         if (pci_has_legacy_pm_support(to_pci_dev(dev)))
894                 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
895
896         if (!drv || !drv->pm)
897                 return 0;
898
899         if (drv->pm->poweroff_noirq) {
900                 int error;
901
902                 error = drv->pm->poweroff_noirq(dev);
903                 suspend_report_result(drv->pm->poweroff_noirq, error);
904                 if (error)
905                         return error;
906         }
907
908         if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
909                 pci_prepare_to_sleep(pci_dev);
910
911         /*
912          * The reason for doing this here is the same as for the analogous code
913          * in pci_pm_suspend_noirq().
914          */
915         if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
916                 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
917
918         return 0;
919 }
920
921 static int pci_pm_restore_noirq(struct device *dev)
922 {
923         struct pci_dev *pci_dev = to_pci_dev(dev);
924         struct device_driver *drv = dev->driver;
925         int error = 0;
926
927         pci_pm_default_resume_early(pci_dev);
928
929         if (pci_has_legacy_pm_support(pci_dev))
930                 return pci_legacy_resume_early(dev);
931
932         if (drv && drv->pm && drv->pm->restore_noirq)
933                 error = drv->pm->restore_noirq(dev);
934
935         return error;
936 }
937
938 static int pci_pm_restore(struct device *dev)
939 {
940         struct pci_dev *pci_dev = to_pci_dev(dev);
941         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
942         int error = 0;
943
944         /*
945          * This is necessary for the hibernation error path in which restore is
946          * called without restoring the standard config registers of the device.
947          */
948         if (pci_dev->state_saved)
949                 pci_restore_standard_config(pci_dev);
950
951         if (pci_has_legacy_pm_support(pci_dev))
952                 return pci_legacy_resume(dev);
953
954         pci_pm_default_resume(pci_dev);
955
956         if (pm) {
957                 if (pm->restore)
958                         error = pm->restore(dev);
959         } else {
960                 pci_pm_reenable_device(pci_dev);
961         }
962
963         return error;
964 }
965
966 #else /* !CONFIG_HIBERNATE_CALLBACKS */
967
968 #define pci_pm_freeze           NULL
969 #define pci_pm_freeze_noirq     NULL
970 #define pci_pm_thaw             NULL
971 #define pci_pm_thaw_noirq       NULL
972 #define pci_pm_poweroff         NULL
973 #define pci_pm_poweroff_noirq   NULL
974 #define pci_pm_restore          NULL
975 #define pci_pm_restore_noirq    NULL
976
977 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
978
979 #ifdef CONFIG_PM_RUNTIME
980
981 static int pci_pm_runtime_suspend(struct device *dev)
982 {
983         struct pci_dev *pci_dev = to_pci_dev(dev);
984         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
985         pci_power_t prev = pci_dev->current_state;
986         int error;
987
988         /*
989          * If pci_dev->driver is not set (unbound), the device should
990          * always remain in D0 regardless of the runtime PM status
991          */
992         if (!pci_dev->driver)
993                 return 0;
994
995         if (!pm || !pm->runtime_suspend)
996                 return -ENOSYS;
997
998         pci_dev->no_d3cold = false;
999         error = pm->runtime_suspend(dev);
1000         suspend_report_result(pm->runtime_suspend, error);
1001         if (error)
1002                 return error;
1003         if (!pci_dev->d3cold_allowed)
1004                 pci_dev->no_d3cold = true;
1005
1006         pci_fixup_device(pci_fixup_suspend, pci_dev);
1007
1008         if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
1009             && pci_dev->current_state != PCI_UNKNOWN) {
1010                 WARN_ONCE(pci_dev->current_state != prev,
1011                         "PCI PM: State of device not saved by %pF\n",
1012                         pm->runtime_suspend);
1013                 return 0;
1014         }
1015
1016         if (!pci_dev->state_saved)
1017                 pci_save_state(pci_dev);
1018
1019         pci_finish_runtime_suspend(pci_dev);
1020
1021         return 0;
1022 }
1023
1024 static int pci_pm_runtime_resume(struct device *dev)
1025 {
1026         int rc;
1027         struct pci_dev *pci_dev = to_pci_dev(dev);
1028         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1029
1030         /*
1031          * If pci_dev->driver is not set (unbound), the device should
1032          * always remain in D0 regardless of the runtime PM status
1033          */
1034         if (!pci_dev->driver)
1035                 return 0;
1036
1037         if (!pm || !pm->runtime_resume)
1038                 return -ENOSYS;
1039
1040         pci_restore_standard_config(pci_dev);
1041         pci_fixup_device(pci_fixup_resume_early, pci_dev);
1042         __pci_enable_wake(pci_dev, PCI_D0, true, false);
1043         pci_fixup_device(pci_fixup_resume, pci_dev);
1044
1045         rc = pm->runtime_resume(dev);
1046
1047         pci_dev->runtime_d3cold = false;
1048
1049         return rc;
1050 }
1051
1052 static int pci_pm_runtime_idle(struct device *dev)
1053 {
1054         struct pci_dev *pci_dev = to_pci_dev(dev);
1055         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1056
1057         /*
1058          * If pci_dev->driver is not set (unbound), the device should
1059          * always remain in D0 regardless of the runtime PM status
1060          */
1061         if (!pci_dev->driver)
1062                 goto out;
1063
1064         if (!pm)
1065                 return -ENOSYS;
1066
1067         if (pm->runtime_idle) {
1068                 int ret = pm->runtime_idle(dev);
1069                 if (ret)
1070                         return ret;
1071         }
1072
1073 out:
1074         pm_runtime_suspend(dev);
1075         return 0;
1076 }
1077
1078 #else /* !CONFIG_PM_RUNTIME */
1079
1080 #define pci_pm_runtime_suspend  NULL
1081 #define pci_pm_runtime_resume   NULL
1082 #define pci_pm_runtime_idle     NULL
1083
1084 #endif /* !CONFIG_PM_RUNTIME */
1085
1086 #ifdef CONFIG_PM
1087
1088 const struct dev_pm_ops pci_dev_pm_ops = {
1089         .prepare = pci_pm_prepare,
1090         .complete = pci_pm_complete,
1091         .suspend = pci_pm_suspend,
1092         .resume = pci_pm_resume,
1093         .freeze = pci_pm_freeze,
1094         .thaw = pci_pm_thaw,
1095         .poweroff = pci_pm_poweroff,
1096         .restore = pci_pm_restore,
1097         .suspend_noirq = pci_pm_suspend_noirq,
1098         .resume_noirq = pci_pm_resume_noirq,
1099         .freeze_noirq = pci_pm_freeze_noirq,
1100         .thaw_noirq = pci_pm_thaw_noirq,
1101         .poweroff_noirq = pci_pm_poweroff_noirq,
1102         .restore_noirq = pci_pm_restore_noirq,
1103         .runtime_suspend = pci_pm_runtime_suspend,
1104         .runtime_resume = pci_pm_runtime_resume,
1105         .runtime_idle = pci_pm_runtime_idle,
1106 };
1107
1108 #define PCI_PM_OPS_PTR  (&pci_dev_pm_ops)
1109
1110 #else /* !COMFIG_PM_OPS */
1111
1112 #define PCI_PM_OPS_PTR  NULL
1113
1114 #endif /* !COMFIG_PM_OPS */
1115
1116 /**
1117  * __pci_register_driver - register a new pci driver
1118  * @drv: the driver structure to register
1119  * @owner: owner module of drv
1120  * @mod_name: module name string
1121  * 
1122  * Adds the driver structure to the list of registered drivers.
1123  * Returns a negative value on error, otherwise 0. 
1124  * If no error occurred, the driver remains registered even if 
1125  * no device was claimed during registration.
1126  */
1127 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1128                           const char *mod_name)
1129 {
1130         /* initialize common driver fields */
1131         drv->driver.name = drv->name;
1132         drv->driver.bus = &pci_bus_type;
1133         drv->driver.owner = owner;
1134         drv->driver.mod_name = mod_name;
1135
1136         spin_lock_init(&drv->dynids.lock);
1137         INIT_LIST_HEAD(&drv->dynids.list);
1138
1139         /* register with core */
1140         return driver_register(&drv->driver);
1141 }
1142
1143 /**
1144  * pci_unregister_driver - unregister a pci driver
1145  * @drv: the driver structure to unregister
1146  * 
1147  * Deletes the driver structure from the list of registered PCI drivers,
1148  * gives it a chance to clean up by calling its remove() function for
1149  * each device it was responsible for, and marks those devices as
1150  * driverless.
1151  */
1152
1153 void
1154 pci_unregister_driver(struct pci_driver *drv)
1155 {
1156         driver_unregister(&drv->driver);
1157         pci_free_dynids(drv);
1158 }
1159
1160 static struct pci_driver pci_compat_driver = {
1161         .name = "compat"
1162 };
1163
1164 /**
1165  * pci_dev_driver - get the pci_driver of a device
1166  * @dev: the device to query
1167  *
1168  * Returns the appropriate pci_driver structure or %NULL if there is no 
1169  * registered driver for the device.
1170  */
1171 struct pci_driver *
1172 pci_dev_driver(const struct pci_dev *dev)
1173 {
1174         if (dev->driver)
1175                 return dev->driver;
1176         else {
1177                 int i;
1178                 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1179                         if (dev->resource[i].flags & IORESOURCE_BUSY)
1180                                 return &pci_compat_driver;
1181         }
1182         return NULL;
1183 }
1184
1185 /**
1186  * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1187  * @dev: the PCI device structure to match against
1188  * @drv: the device driver to search for matching PCI device id structures
1189  * 
1190  * Used by a driver to check whether a PCI device present in the
1191  * system is in its list of supported devices. Returns the matching
1192  * pci_device_id structure or %NULL if there is no match.
1193  */
1194 static int pci_bus_match(struct device *dev, struct device_driver *drv)
1195 {
1196         struct pci_dev *pci_dev = to_pci_dev(dev);
1197         struct pci_driver *pci_drv = to_pci_driver(drv);
1198         const struct pci_device_id *found_id;
1199
1200         found_id = pci_match_device(pci_drv, pci_dev);
1201         if (found_id)
1202                 return 1;
1203
1204         return 0;
1205 }
1206
1207 /**
1208  * pci_dev_get - increments the reference count of the pci device structure
1209  * @dev: the device being referenced
1210  *
1211  * Each live reference to a device should be refcounted.
1212  *
1213  * Drivers for PCI devices should normally record such references in
1214  * their probe() methods, when they bind to a device, and release
1215  * them by calling pci_dev_put(), in their disconnect() methods.
1216  *
1217  * A pointer to the device with the incremented reference counter is returned.
1218  */
1219 struct pci_dev *pci_dev_get(struct pci_dev *dev)
1220 {
1221         if (dev)
1222                 get_device(&dev->dev);
1223         return dev;
1224 }
1225
1226 /**
1227  * pci_dev_put - release a use of the pci device structure
1228  * @dev: device that's been disconnected
1229  *
1230  * Must be called when a user of a device is finished with it.  When the last
1231  * user of the device calls this function, the memory of the device is freed.
1232  */
1233 void pci_dev_put(struct pci_dev *dev)
1234 {
1235         if (dev)
1236                 put_device(&dev->dev);
1237 }
1238
1239 #ifndef CONFIG_HOTPLUG
1240 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1241 {
1242         return -ENODEV;
1243 }
1244 #endif
1245
1246 struct bus_type pci_bus_type = {
1247         .name           = "pci",
1248         .match          = pci_bus_match,
1249         .uevent         = pci_uevent,
1250         .probe          = pci_device_probe,
1251         .remove         = pci_device_remove,
1252         .shutdown       = pci_device_shutdown,
1253         .dev_attrs      = pci_dev_attrs,
1254         .bus_attrs      = pci_bus_attrs,
1255         .drv_attrs      = pci_drv_attrs,
1256         .pm             = PCI_PM_OPS_PTR,
1257 };
1258
1259 static int __init pci_driver_init(void)
1260 {
1261         return bus_register(&pci_bus_type);
1262 }
1263
1264 postcore_initcall(pci_driver_init);
1265
1266 EXPORT_SYMBOL_GPL(pci_add_dynid);
1267 EXPORT_SYMBOL(pci_match_id);
1268 EXPORT_SYMBOL(__pci_register_driver);
1269 EXPORT_SYMBOL(pci_unregister_driver);
1270 EXPORT_SYMBOL(pci_dev_driver);
1271 EXPORT_SYMBOL(pci_bus_type);
1272 EXPORT_SYMBOL(pci_dev_get);
1273 EXPORT_SYMBOL(pci_dev_put);