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