PCI PM: Split PCI Express port suspend-resume
[deliverable/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 "pci.h"
20
21 /*
22 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
23 */
24
25 struct pci_dynid {
26 struct list_head node;
27 struct pci_device_id id;
28 };
29
30 #ifdef CONFIG_HOTPLUG
31
32 /**
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
36 * @count: input size
37 *
38 * Adds a new dynamic pci device ID to this driver,
39 * and causes the driver to probe for all devices again.
40 */
41 static ssize_t
42 store_new_id(struct device_driver *driver, const char *buf, size_t count)
43 {
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;
50 int fields=0;
51 int retval=0;
52
53 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
54 &vendor, &device, &subvendor, &subdevice,
55 &class, &class_mask, &driver_data);
56 if (fields < 2)
57 return -EINVAL;
58
59 /* Only accept driver_data values that match an existing id_table
60 entry */
61 if (ids) {
62 retval = -EINVAL;
63 while (ids->vendor || ids->subvendor || ids->class_mask) {
64 if (driver_data == ids->driver_data) {
65 retval = 0;
66 break;
67 }
68 ids++;
69 }
70 if (retval) /* No match */
71 return retval;
72 }
73
74 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
75 if (!dynid)
76 return -ENOMEM;
77
78 dynid->id.vendor = vendor;
79 dynid->id.device = device;
80 dynid->id.subvendor = subvendor;
81 dynid->id.subdevice = subdevice;
82 dynid->id.class = class;
83 dynid->id.class_mask = class_mask;
84 dynid->id.driver_data = driver_data;
85
86 spin_lock(&pdrv->dynids.lock);
87 list_add_tail(&dynid->node, &pdrv->dynids.list);
88 spin_unlock(&pdrv->dynids.lock);
89
90 if (get_driver(&pdrv->driver)) {
91 retval = driver_attach(&pdrv->driver);
92 put_driver(&pdrv->driver);
93 }
94
95 if (retval)
96 return retval;
97 return count;
98 }
99 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
100
101 static void
102 pci_free_dynids(struct pci_driver *drv)
103 {
104 struct pci_dynid *dynid, *n;
105
106 spin_lock(&drv->dynids.lock);
107 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
108 list_del(&dynid->node);
109 kfree(dynid);
110 }
111 spin_unlock(&drv->dynids.lock);
112 }
113
114 static int
115 pci_create_newid_file(struct pci_driver *drv)
116 {
117 int error = 0;
118 if (drv->probe != NULL)
119 error = driver_create_file(&drv->driver, &driver_attr_new_id);
120 return error;
121 }
122
123 static void pci_remove_newid_file(struct pci_driver *drv)
124 {
125 driver_remove_file(&drv->driver, &driver_attr_new_id);
126 }
127 #else /* !CONFIG_HOTPLUG */
128 static inline void pci_free_dynids(struct pci_driver *drv) {}
129 static inline int pci_create_newid_file(struct pci_driver *drv)
130 {
131 return 0;
132 }
133 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
134 #endif
135
136 /**
137 * pci_match_id - See if a pci device matches a given pci_id table
138 * @ids: array of PCI device id structures to search in
139 * @dev: the PCI device structure to match against.
140 *
141 * Used by a driver to check whether a PCI device present in the
142 * system is in its list of supported devices. Returns the matching
143 * pci_device_id structure or %NULL if there is no match.
144 *
145 * Deprecated, don't use this as it will not catch any dynamic ids
146 * that a driver might want to check for.
147 */
148 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
149 struct pci_dev *dev)
150 {
151 if (ids) {
152 while (ids->vendor || ids->subvendor || ids->class_mask) {
153 if (pci_match_one_device(ids, dev))
154 return ids;
155 ids++;
156 }
157 }
158 return NULL;
159 }
160
161 /**
162 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
163 * @drv: the PCI driver to match against
164 * @dev: the PCI device structure to match against
165 *
166 * Used by a driver to check whether a PCI device present in the
167 * system is in its list of supported devices. Returns the matching
168 * pci_device_id structure or %NULL if there is no match.
169 */
170 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
171 struct pci_dev *dev)
172 {
173 struct pci_dynid *dynid;
174
175 /* Look at the dynamic ids first, before the static ones */
176 spin_lock(&drv->dynids.lock);
177 list_for_each_entry(dynid, &drv->dynids.list, node) {
178 if (pci_match_one_device(&dynid->id, dev)) {
179 spin_unlock(&drv->dynids.lock);
180 return &dynid->id;
181 }
182 }
183 spin_unlock(&drv->dynids.lock);
184
185 return pci_match_id(drv->id_table, dev);
186 }
187
188 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
189 const struct pci_device_id *id)
190 {
191 int error;
192 #ifdef CONFIG_NUMA
193 /* Execute driver initialization on node where the
194 device's bus is attached to. This way the driver likely
195 allocates its local memory on the right node without
196 any need to change it. */
197 struct mempolicy *oldpol;
198 cpumask_t oldmask = current->cpus_allowed;
199 int node = dev_to_node(&dev->dev);
200
201 if (node >= 0) {
202 node_to_cpumask_ptr(nodecpumask, node);
203 set_cpus_allowed_ptr(current, nodecpumask);
204 }
205 /* And set default memory allocation policy */
206 oldpol = current->mempolicy;
207 current->mempolicy = NULL; /* fall back to system default policy */
208 #endif
209 error = drv->probe(dev, id);
210 #ifdef CONFIG_NUMA
211 set_cpus_allowed_ptr(current, &oldmask);
212 current->mempolicy = oldpol;
213 #endif
214 return error;
215 }
216
217 /**
218 * __pci_device_probe()
219 * @drv: driver to call to check if it wants the PCI device
220 * @pci_dev: PCI device being probed
221 *
222 * returns 0 on success, else error.
223 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
224 */
225 static int
226 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
227 {
228 const struct pci_device_id *id;
229 int error = 0;
230
231 if (!pci_dev->driver && drv->probe) {
232 error = -ENODEV;
233
234 id = pci_match_device(drv, pci_dev);
235 if (id)
236 error = pci_call_probe(drv, pci_dev, id);
237 if (error >= 0) {
238 pci_dev->driver = drv;
239 error = 0;
240 }
241 }
242 return error;
243 }
244
245 static int pci_device_probe(struct device * dev)
246 {
247 int error = 0;
248 struct pci_driver *drv;
249 struct pci_dev *pci_dev;
250
251 drv = to_pci_driver(dev->driver);
252 pci_dev = to_pci_dev(dev);
253 pci_dev_get(pci_dev);
254 error = __pci_device_probe(drv, pci_dev);
255 if (error)
256 pci_dev_put(pci_dev);
257
258 return error;
259 }
260
261 static int pci_device_remove(struct device * dev)
262 {
263 struct pci_dev * pci_dev = to_pci_dev(dev);
264 struct pci_driver * drv = pci_dev->driver;
265
266 if (drv) {
267 if (drv->remove)
268 drv->remove(pci_dev);
269 pci_dev->driver = NULL;
270 }
271
272 /*
273 * If the device is still on, set the power state as "unknown",
274 * since it might change by the next time we load the driver.
275 */
276 if (pci_dev->current_state == PCI_D0)
277 pci_dev->current_state = PCI_UNKNOWN;
278
279 /*
280 * We would love to complain here if pci_dev->is_enabled is set, that
281 * the driver should have called pci_disable_device(), but the
282 * unfortunate fact is there are too many odd BIOS and bridge setups
283 * that don't like drivers doing that all of the time.
284 * Oh well, we can dream of sane hardware when we sleep, no matter how
285 * horrible the crap we have to deal with is when we are awake...
286 */
287
288 pci_dev_put(pci_dev);
289 return 0;
290 }
291
292 static void pci_device_shutdown(struct device *dev)
293 {
294 struct pci_dev *pci_dev = to_pci_dev(dev);
295 struct pci_driver *drv = pci_dev->driver;
296
297 if (drv && drv->shutdown)
298 drv->shutdown(pci_dev);
299 pci_msi_shutdown(pci_dev);
300 pci_msix_shutdown(pci_dev);
301 }
302
303 #ifdef CONFIG_PM_SLEEP
304
305 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
306 {
307 struct pci_driver *drv = pci_dev->driver;
308
309 return drv && (drv->suspend || drv->suspend_late || drv->resume
310 || drv->resume_early);
311 }
312
313 /*
314 * Default "suspend" method for devices that have no driver provided suspend,
315 * or not even a driver at all.
316 */
317 static void pci_default_pm_suspend(struct pci_dev *pci_dev)
318 {
319 pci_save_state(pci_dev);
320 /*
321 * mark its power state as "unknown", since we don't know if
322 * e.g. the BIOS will change its device state when we suspend.
323 */
324 if (pci_dev->current_state == PCI_D0)
325 pci_dev->current_state = PCI_UNKNOWN;
326 }
327
328 /*
329 * Default "resume" method for devices that have no driver provided resume,
330 * or not even a driver at all (first part).
331 */
332 static void pci_default_pm_resume_early(struct pci_dev *pci_dev)
333 {
334 /* restore the PCI config space */
335 pci_restore_state(pci_dev);
336 }
337
338 /*
339 * Default "resume" method for devices that have no driver provided resume,
340 * or not even a driver at all (second part).
341 */
342 static int pci_default_pm_resume_late(struct pci_dev *pci_dev)
343 {
344 int retval;
345
346 /* if the device was enabled before suspend, reenable */
347 retval = pci_reenable_device(pci_dev);
348 /*
349 * if the device was busmaster before the suspend, make it busmaster
350 * again
351 */
352 if (pci_dev->is_busmaster)
353 pci_set_master(pci_dev);
354
355 return retval;
356 }
357
358 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
359 {
360 struct pci_dev * pci_dev = to_pci_dev(dev);
361 struct pci_driver * drv = pci_dev->driver;
362 int i = 0;
363
364 if (drv && drv->suspend) {
365 i = drv->suspend(pci_dev, state);
366 suspend_report_result(drv->suspend, i);
367 } else {
368 pci_default_pm_suspend(pci_dev);
369 }
370 return i;
371 }
372
373 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
374 {
375 struct pci_dev * pci_dev = to_pci_dev(dev);
376 struct pci_driver * drv = pci_dev->driver;
377 int i = 0;
378
379 if (drv && drv->suspend_late) {
380 i = drv->suspend_late(pci_dev, state);
381 suspend_report_result(drv->suspend_late, i);
382 }
383 return i;
384 }
385
386 static int pci_legacy_resume(struct device *dev)
387 {
388 int error;
389 struct pci_dev * pci_dev = to_pci_dev(dev);
390 struct pci_driver * drv = pci_dev->driver;
391
392 if (drv && drv->resume) {
393 error = drv->resume(pci_dev);
394 } else {
395 pci_default_pm_resume_early(pci_dev);
396 error = pci_default_pm_resume_late(pci_dev);
397 }
398 return error;
399 }
400
401 static int pci_legacy_resume_early(struct device *dev)
402 {
403 int error = 0;
404 struct pci_dev * pci_dev = to_pci_dev(dev);
405 struct pci_driver * drv = pci_dev->driver;
406
407 if (drv && drv->resume_early)
408 error = drv->resume_early(pci_dev);
409 return error;
410 }
411
412 static int pci_pm_prepare(struct device *dev)
413 {
414 struct device_driver *drv = dev->driver;
415 int error = 0;
416
417 if (drv && drv->pm && drv->pm->prepare)
418 error = drv->pm->prepare(dev);
419
420 return error;
421 }
422
423 static void pci_pm_complete(struct device *dev)
424 {
425 struct device_driver *drv = dev->driver;
426
427 if (drv && drv->pm && drv->pm->complete)
428 drv->pm->complete(dev);
429 }
430
431 #ifdef CONFIG_SUSPEND
432
433 static int pci_pm_suspend(struct device *dev)
434 {
435 struct pci_dev *pci_dev = to_pci_dev(dev);
436 struct device_driver *drv = dev->driver;
437 int error = 0;
438
439 if (drv && drv->pm) {
440 if (drv->pm->suspend) {
441 error = drv->pm->suspend(dev);
442 suspend_report_result(drv->pm->suspend, error);
443 }
444 } else if (pci_has_legacy_pm_support(pci_dev)) {
445 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
446 }
447 pci_fixup_device(pci_fixup_suspend, pci_dev);
448
449 return error;
450 }
451
452 static int pci_pm_suspend_noirq(struct device *dev)
453 {
454 struct pci_dev *pci_dev = to_pci_dev(dev);
455 struct device_driver *drv = dev->driver;
456 int error = 0;
457
458 if (drv && drv->pm) {
459 if (drv->pm->suspend_noirq) {
460 error = drv->pm->suspend_noirq(dev);
461 suspend_report_result(drv->pm->suspend_noirq, error);
462 }
463 } else if (pci_has_legacy_pm_support(pci_dev)) {
464 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
465 } else {
466 pci_default_pm_suspend(pci_dev);
467 }
468
469 return error;
470 }
471
472 static int pci_pm_resume(struct device *dev)
473 {
474 struct pci_dev *pci_dev = to_pci_dev(dev);
475 struct device_driver *drv = dev->driver;
476 int error = 0;
477
478 pci_fixup_device(pci_fixup_resume, pci_dev);
479
480 if (drv && drv->pm) {
481 if (drv->pm->resume)
482 error = drv->pm->resume(dev);
483 } else if (pci_has_legacy_pm_support(pci_dev)) {
484 error = pci_legacy_resume(dev);
485 } else {
486 error = pci_default_pm_resume_late(pci_dev);
487 }
488
489 return error;
490 }
491
492 static int pci_pm_resume_noirq(struct device *dev)
493 {
494 struct pci_dev *pci_dev = to_pci_dev(dev);
495 struct device_driver *drv = dev->driver;
496 int error = 0;
497
498 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
499
500 if (drv && drv->pm) {
501 if (drv->pm->resume_noirq)
502 error = drv->pm->resume_noirq(dev);
503 } else if (pci_has_legacy_pm_support(pci_dev)) {
504 error = pci_legacy_resume_early(dev);
505 } else {
506 pci_default_pm_resume_early(pci_dev);
507 }
508
509 return error;
510 }
511
512 #else /* !CONFIG_SUSPEND */
513
514 #define pci_pm_suspend NULL
515 #define pci_pm_suspend_noirq NULL
516 #define pci_pm_resume NULL
517 #define pci_pm_resume_noirq NULL
518
519 #endif /* !CONFIG_SUSPEND */
520
521 #ifdef CONFIG_HIBERNATION
522
523 static int pci_pm_freeze(struct device *dev)
524 {
525 struct pci_dev *pci_dev = to_pci_dev(dev);
526 struct device_driver *drv = dev->driver;
527 int error = 0;
528
529 if (drv && drv->pm) {
530 if (drv->pm->freeze) {
531 error = drv->pm->freeze(dev);
532 suspend_report_result(drv->pm->freeze, error);
533 }
534 } else if (pci_has_legacy_pm_support(pci_dev)) {
535 error = pci_legacy_suspend(dev, PMSG_FREEZE);
536 pci_fixup_device(pci_fixup_suspend, pci_dev);
537 }
538
539 return error;
540 }
541
542 static int pci_pm_freeze_noirq(struct device *dev)
543 {
544 struct pci_dev *pci_dev = to_pci_dev(dev);
545 struct device_driver *drv = dev->driver;
546 int error = 0;
547
548 if (drv && drv->pm) {
549 if (drv->pm->freeze_noirq) {
550 error = drv->pm->freeze_noirq(dev);
551 suspend_report_result(drv->pm->freeze_noirq, error);
552 }
553 } else if (pci_has_legacy_pm_support(pci_dev)) {
554 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
555 } else {
556 pci_default_pm_suspend(pci_dev);
557 }
558
559 return error;
560 }
561
562 static int pci_pm_thaw(struct device *dev)
563 {
564 struct pci_dev *pci_dev = to_pci_dev(dev);
565 struct device_driver *drv = dev->driver;
566 int error = 0;
567
568 if (drv && drv->pm) {
569 if (drv->pm->thaw)
570 error = drv->pm->thaw(dev);
571 } else if (pci_has_legacy_pm_support(pci_dev)) {
572 pci_fixup_device(pci_fixup_resume, pci_dev);
573 error = pci_legacy_resume(dev);
574 }
575
576 return error;
577 }
578
579 static int pci_pm_thaw_noirq(struct device *dev)
580 {
581 struct pci_dev *pci_dev = to_pci_dev(dev);
582 struct device_driver *drv = dev->driver;
583 int error = 0;
584
585 if (drv && drv->pm) {
586 if (drv->pm->thaw_noirq)
587 error = drv->pm->thaw_noirq(dev);
588 } else if (pci_has_legacy_pm_support(pci_dev)) {
589 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
590 error = pci_legacy_resume_early(dev);
591 }
592
593 return error;
594 }
595
596 static int pci_pm_poweroff(struct device *dev)
597 {
598 struct pci_dev *pci_dev = to_pci_dev(dev);
599 struct device_driver *drv = dev->driver;
600 int error = 0;
601
602 pci_fixup_device(pci_fixup_suspend, pci_dev);
603
604 if (drv && drv->pm) {
605 if (drv->pm->poweroff) {
606 error = drv->pm->poweroff(dev);
607 suspend_report_result(drv->pm->poweroff, error);
608 }
609 } else if (pci_has_legacy_pm_support(pci_dev)) {
610 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
611 }
612
613 return error;
614 }
615
616 static int pci_pm_poweroff_noirq(struct device *dev)
617 {
618 struct device_driver *drv = dev->driver;
619 int error = 0;
620
621 if (drv && drv->pm) {
622 if (drv->pm->poweroff_noirq) {
623 error = drv->pm->poweroff_noirq(dev);
624 suspend_report_result(drv->pm->poweroff_noirq, error);
625 }
626 } else if (pci_has_legacy_pm_support(to_pci_dev(dev))) {
627 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
628 }
629
630 return error;
631 }
632
633 static int pci_pm_restore(struct device *dev)
634 {
635 struct pci_dev *pci_dev = to_pci_dev(dev);
636 struct device_driver *drv = dev->driver;
637 int error = 0;
638
639 if (drv && drv->pm) {
640 if (drv->pm->restore)
641 error = drv->pm->restore(dev);
642 } else if (pci_has_legacy_pm_support(pci_dev)) {
643 error = pci_legacy_resume(dev);
644 } else {
645 error = pci_default_pm_resume_late(pci_dev);
646 }
647 pci_fixup_device(pci_fixup_resume, pci_dev);
648
649 return error;
650 }
651
652 static int pci_pm_restore_noirq(struct device *dev)
653 {
654 struct pci_dev *pci_dev = to_pci_dev(dev);
655 struct device_driver *drv = dev->driver;
656 int error = 0;
657
658 pci_fixup_device(pci_fixup_resume, pci_dev);
659
660 if (drv && drv->pm) {
661 if (drv->pm->restore_noirq)
662 error = drv->pm->restore_noirq(dev);
663 } else if (pci_has_legacy_pm_support(pci_dev)) {
664 error = pci_legacy_resume_early(dev);
665 } else {
666 pci_default_pm_resume_early(pci_dev);
667 }
668 pci_fixup_device(pci_fixup_resume_early, pci_dev);
669
670 return error;
671 }
672
673 #else /* !CONFIG_HIBERNATION */
674
675 #define pci_pm_freeze NULL
676 #define pci_pm_freeze_noirq NULL
677 #define pci_pm_thaw NULL
678 #define pci_pm_thaw_noirq NULL
679 #define pci_pm_poweroff NULL
680 #define pci_pm_poweroff_noirq NULL
681 #define pci_pm_restore NULL
682 #define pci_pm_restore_noirq NULL
683
684 #endif /* !CONFIG_HIBERNATION */
685
686 struct dev_pm_ops pci_dev_pm_ops = {
687 .prepare = pci_pm_prepare,
688 .complete = pci_pm_complete,
689 .suspend = pci_pm_suspend,
690 .resume = pci_pm_resume,
691 .freeze = pci_pm_freeze,
692 .thaw = pci_pm_thaw,
693 .poweroff = pci_pm_poweroff,
694 .restore = pci_pm_restore,
695 .suspend_noirq = pci_pm_suspend_noirq,
696 .resume_noirq = pci_pm_resume_noirq,
697 .freeze_noirq = pci_pm_freeze_noirq,
698 .thaw_noirq = pci_pm_thaw_noirq,
699 .poweroff_noirq = pci_pm_poweroff_noirq,
700 .restore_noirq = pci_pm_restore_noirq,
701 };
702
703 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
704
705 #else /* !CONFIG_PM_SLEEP */
706
707 #define PCI_PM_OPS_PTR NULL
708
709 #endif /* !CONFIG_PM_SLEEP */
710
711 /**
712 * __pci_register_driver - register a new pci driver
713 * @drv: the driver structure to register
714 * @owner: owner module of drv
715 * @mod_name: module name string
716 *
717 * Adds the driver structure to the list of registered drivers.
718 * Returns a negative value on error, otherwise 0.
719 * If no error occurred, the driver remains registered even if
720 * no device was claimed during registration.
721 */
722 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
723 const char *mod_name)
724 {
725 int error;
726
727 /* initialize common driver fields */
728 drv->driver.name = drv->name;
729 drv->driver.bus = &pci_bus_type;
730 drv->driver.owner = owner;
731 drv->driver.mod_name = mod_name;
732
733 spin_lock_init(&drv->dynids.lock);
734 INIT_LIST_HEAD(&drv->dynids.list);
735
736 /* register with core */
737 error = driver_register(&drv->driver);
738 if (error)
739 return error;
740
741 error = pci_create_newid_file(drv);
742 if (error)
743 driver_unregister(&drv->driver);
744
745 return error;
746 }
747
748 /**
749 * pci_unregister_driver - unregister a pci driver
750 * @drv: the driver structure to unregister
751 *
752 * Deletes the driver structure from the list of registered PCI drivers,
753 * gives it a chance to clean up by calling its remove() function for
754 * each device it was responsible for, and marks those devices as
755 * driverless.
756 */
757
758 void
759 pci_unregister_driver(struct pci_driver *drv)
760 {
761 pci_remove_newid_file(drv);
762 driver_unregister(&drv->driver);
763 pci_free_dynids(drv);
764 }
765
766 static struct pci_driver pci_compat_driver = {
767 .name = "compat"
768 };
769
770 /**
771 * pci_dev_driver - get the pci_driver of a device
772 * @dev: the device to query
773 *
774 * Returns the appropriate pci_driver structure or %NULL if there is no
775 * registered driver for the device.
776 */
777 struct pci_driver *
778 pci_dev_driver(const struct pci_dev *dev)
779 {
780 if (dev->driver)
781 return dev->driver;
782 else {
783 int i;
784 for(i=0; i<=PCI_ROM_RESOURCE; i++)
785 if (dev->resource[i].flags & IORESOURCE_BUSY)
786 return &pci_compat_driver;
787 }
788 return NULL;
789 }
790
791 /**
792 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
793 * @dev: the PCI device structure to match against
794 * @drv: the device driver to search for matching PCI device id structures
795 *
796 * Used by a driver to check whether a PCI device present in the
797 * system is in its list of supported devices. Returns the matching
798 * pci_device_id structure or %NULL if there is no match.
799 */
800 static int pci_bus_match(struct device *dev, struct device_driver *drv)
801 {
802 struct pci_dev *pci_dev = to_pci_dev(dev);
803 struct pci_driver *pci_drv = to_pci_driver(drv);
804 const struct pci_device_id *found_id;
805
806 found_id = pci_match_device(pci_drv, pci_dev);
807 if (found_id)
808 return 1;
809
810 return 0;
811 }
812
813 /**
814 * pci_dev_get - increments the reference count of the pci device structure
815 * @dev: the device being referenced
816 *
817 * Each live reference to a device should be refcounted.
818 *
819 * Drivers for PCI devices should normally record such references in
820 * their probe() methods, when they bind to a device, and release
821 * them by calling pci_dev_put(), in their disconnect() methods.
822 *
823 * A pointer to the device with the incremented reference counter is returned.
824 */
825 struct pci_dev *pci_dev_get(struct pci_dev *dev)
826 {
827 if (dev)
828 get_device(&dev->dev);
829 return dev;
830 }
831
832 /**
833 * pci_dev_put - release a use of the pci device structure
834 * @dev: device that's been disconnected
835 *
836 * Must be called when a user of a device is finished with it. When the last
837 * user of the device calls this function, the memory of the device is freed.
838 */
839 void pci_dev_put(struct pci_dev *dev)
840 {
841 if (dev)
842 put_device(&dev->dev);
843 }
844
845 #ifndef CONFIG_HOTPLUG
846 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
847 {
848 return -ENODEV;
849 }
850 #endif
851
852 struct bus_type pci_bus_type = {
853 .name = "pci",
854 .match = pci_bus_match,
855 .uevent = pci_uevent,
856 .probe = pci_device_probe,
857 .remove = pci_device_remove,
858 .shutdown = pci_device_shutdown,
859 .dev_attrs = pci_dev_attrs,
860 .pm = PCI_PM_OPS_PTR,
861 };
862
863 static int __init pci_driver_init(void)
864 {
865 return bus_register(&pci_bus_type);
866 }
867
868 postcore_initcall(pci_driver_init);
869
870 EXPORT_SYMBOL(pci_match_id);
871 EXPORT_SYMBOL(__pci_register_driver);
872 EXPORT_SYMBOL(pci_unregister_driver);
873 EXPORT_SYMBOL(pci_dev_driver);
874 EXPORT_SYMBOL(pci_bus_type);
875 EXPORT_SYMBOL(pci_dev_get);
876 EXPORT_SYMBOL(pci_dev_put);
This page took 0.049149 seconds and 5 git commands to generate.