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 GK |
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; | |
2debb4d2 | 51 | int retval=0; |
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 */ | |
2debb4d2 CW |
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++; | |
b41d6cf3 | 69 | } |
2debb4d2 CW |
70 | if (retval) /* No match */ |
71 | return retval; | |
b41d6cf3 | 72 | } |
b41d6cf3 | 73 | |
f5afe806 | 74 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); |
1da177e4 LT |
75 | if (!dynid) |
76 | return -ENOMEM; | |
77 | ||
1da177e4 LT |
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; | |
edbc25ca | 84 | dynid->id.driver_data = driver_data; |
1da177e4 LT |
85 | |
86 | spin_lock(&pdrv->dynids.lock); | |
a56bc69a | 87 | list_add_tail(&dynid->node, &pdrv->dynids.list); |
1da177e4 LT |
88 | spin_unlock(&pdrv->dynids.lock); |
89 | ||
75865858 | 90 | if (get_driver(&pdrv->driver)) { |
b19441af | 91 | retval = driver_attach(&pdrv->driver); |
75865858 | 92 | put_driver(&pdrv->driver); |
1da177e4 LT |
93 | } |
94 | ||
b19441af GKH |
95 | if (retval) |
96 | return retval; | |
1da177e4 LT |
97 | return count; |
98 | } | |
1da177e4 | 99 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); |
1da177e4 LT |
100 | |
101 | static void | |
102 | pci_free_dynids(struct pci_driver *drv) | |
103 | { | |
75865858 | 104 | struct pci_dynid *dynid, *n; |
1da177e4 LT |
105 | |
106 | spin_lock(&drv->dynids.lock); | |
75865858 | 107 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
1da177e4 LT |
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) | |
03d43b19 | 119 | error = driver_create_file(&drv->driver, &driver_attr_new_id); |
1da177e4 LT |
120 | return error; |
121 | } | |
122 | ||
03d43b19 GKH |
123 | static void pci_remove_newid_file(struct pci_driver *drv) |
124 | { | |
125 | driver_remove_file(&drv->driver, &driver_attr_new_id); | |
126 | } | |
1da177e4 | 127 | #else /* !CONFIG_HOTPLUG */ |
1da177e4 LT |
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 | } | |
03d43b19 | 133 | static inline void pci_remove_newid_file(struct pci_driver *drv) {} |
1da177e4 LT |
134 | #endif |
135 | ||
136 | /** | |
75865858 | 137 | * pci_match_id - See if a pci device matches a given pci_id table |
1da177e4 | 138 | * @ids: array of PCI device id structures to search in |
75865858 GKH |
139 | * @dev: the PCI device structure to match against. |
140 | * | |
1da177e4 | 141 | * Used by a driver to check whether a PCI device present in the |
75865858 | 142 | * system is in its list of supported devices. Returns the matching |
1da177e4 | 143 | * pci_device_id structure or %NULL if there is no match. |
75865858 | 144 | * |
8b60756a | 145 | * Deprecated, don't use this as it will not catch any dynamic ids |
75865858 | 146 | * that a driver might want to check for. |
1da177e4 | 147 | */ |
75865858 GKH |
148 | const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, |
149 | struct pci_dev *dev) | |
1da177e4 | 150 | { |
75865858 GKH |
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 | } | |
1da177e4 LT |
157 | } |
158 | return NULL; | |
159 | } | |
160 | ||
161 | /** | |
ae9608af | 162 | * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure |
75865858 | 163 | * @drv: the PCI driver to match against |
39ba487f | 164 | * @dev: the PCI device structure to match against |
75865858 GKH |
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. | |
1da177e4 | 169 | */ |
d73460d7 AB |
170 | static const struct pci_device_id *pci_match_device(struct pci_driver *drv, |
171 | struct pci_dev *dev) | |
75865858 | 172 | { |
75865858 | 173 | struct pci_dynid *dynid; |
1da177e4 | 174 | |
7461b60a | 175 | /* Look at the dynamic ids first, before the static ones */ |
75865858 GKH |
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 | } | |
1da177e4 | 182 | } |
75865858 | 183 | spin_unlock(&drv->dynids.lock); |
7461b60a RK |
184 | |
185 | return pci_match_id(drv->id_table, dev); | |
1da177e4 LT |
186 | } |
187 | ||
d42c6997 AK |
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; | |
4efeb4dd | 199 | int node = dev_to_node(&dev->dev); |
f70316da MT |
200 | |
201 | if (node >= 0) { | |
202 | node_to_cpumask_ptr(nodecpumask, node); | |
203 | set_cpus_allowed_ptr(current, nodecpumask); | |
204 | } | |
d42c6997 AK |
205 | /* And set default memory allocation policy */ |
206 | oldpol = current->mempolicy; | |
74e27e44 | 207 | current->mempolicy = NULL; /* fall back to system default policy */ |
d42c6997 AK |
208 | #endif |
209 | error = drv->probe(dev, id); | |
210 | #ifdef CONFIG_NUMA | |
f70316da | 211 | set_cpus_allowed_ptr(current, &oldmask); |
d42c6997 AK |
212 | current->mempolicy = oldpol; |
213 | #endif | |
214 | return error; | |
215 | } | |
216 | ||
1da177e4 LT |
217 | /** |
218 | * __pci_device_probe() | |
8f7020d3 RD |
219 | * @drv: driver to call to check if it wants the PCI device |
220 | * @pci_dev: PCI device being probed | |
1da177e4 | 221 | * |
8f7020d3 | 222 | * returns 0 on success, else error. |
1da177e4 LT |
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) | |
75865858 GKH |
227 | { |
228 | const struct pci_device_id *id; | |
1da177e4 LT |
229 | int error = 0; |
230 | ||
231 | if (!pci_dev->driver && drv->probe) { | |
75865858 GKH |
232 | error = -ENODEV; |
233 | ||
234 | id = pci_match_device(drv, pci_dev); | |
235 | if (id) | |
d42c6997 | 236 | error = pci_call_probe(drv, pci_dev, id); |
75865858 GKH |
237 | if (error >= 0) { |
238 | pci_dev->driver = drv; | |
239 | error = 0; | |
240 | } | |
1da177e4 LT |
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 | ||
2449e06a SL |
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 | ||
1da177e4 LT |
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 | ||
bbb44d9f RW |
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 | ||
355a72d7 RW |
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 | ||
bbb44d9f RW |
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, | |
355a72d7 | 330 | * or not even a driver at all (first part). |
bbb44d9f | 331 | */ |
355a72d7 | 332 | static void pci_default_pm_resume_early(struct pci_dev *pci_dev) |
bbb44d9f | 333 | { |
bbb44d9f RW |
334 | /* restore the PCI config space */ |
335 | pci_restore_state(pci_dev); | |
355a72d7 RW |
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 | ||
bbb44d9f RW |
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) | |
1da177e4 LT |
359 | { |
360 | struct pci_dev * pci_dev = to_pci_dev(dev); | |
361 | struct pci_driver * drv = pci_dev->driver; | |
362 | int i = 0; | |
363 | ||
02669492 | 364 | if (drv && drv->suspend) { |
1da177e4 | 365 | i = drv->suspend(pci_dev, state); |
02669492 AM |
366 | suspend_report_result(drv->suspend, i); |
367 | } else { | |
bbb44d9f | 368 | pci_default_pm_suspend(pci_dev); |
02669492 | 369 | } |
1da177e4 LT |
370 | return i; |
371 | } | |
372 | ||
bbb44d9f | 373 | static int pci_legacy_suspend_late(struct device *dev, pm_message_t state) |
cbd69dbb LT |
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 | } | |
1da177e4 | 385 | |
bbb44d9f | 386 | static int pci_legacy_resume(struct device *dev) |
1da177e4 | 387 | { |
8d92bc22 | 388 | int error; |
1da177e4 LT |
389 | struct pci_dev * pci_dev = to_pci_dev(dev); |
390 | struct pci_driver * drv = pci_dev->driver; | |
391 | ||
355a72d7 | 392 | if (drv && drv->resume) { |
8d92bc22 | 393 | error = drv->resume(pci_dev); |
355a72d7 RW |
394 | } else { |
395 | pci_default_pm_resume_early(pci_dev); | |
396 | error = pci_default_pm_resume_late(pci_dev); | |
397 | } | |
8d92bc22 | 398 | return error; |
1da177e4 LT |
399 | } |
400 | ||
bbb44d9f | 401 | static int pci_legacy_resume_early(struct device *dev) |
cbd69dbb LT |
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 | ||
bbb44d9f RW |
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); | |
bbb44d9f | 443 | } |
355a72d7 | 444 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f RW |
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) | |
c8958177 | 453 | { |
355a72d7 | 454 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 455 | struct device_driver *drv = dev->driver; |
bbb44d9f | 456 | int error = 0; |
c8958177 | 457 | |
bbb44d9f RW |
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 | } | |
355a72d7 | 463 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f | 464 | error = pci_legacy_suspend_late(dev, PMSG_SUSPEND); |
355a72d7 RW |
465 | } else { |
466 | pci_default_pm_suspend(pci_dev); | |
bbb44d9f RW |
467 | } |
468 | ||
469 | return error; | |
c8958177 | 470 | } |
1da177e4 | 471 | |
bbb44d9f RW |
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; | |
355a72d7 | 476 | int error = 0; |
bbb44d9f RW |
477 | |
478 | pci_fixup_device(pci_fixup_resume, pci_dev); | |
479 | ||
480 | if (drv && drv->pm) { | |
355a72d7 RW |
481 | if (drv->pm->resume) |
482 | error = drv->pm->resume(dev); | |
483 | } else if (pci_has_legacy_pm_support(pci_dev)) { | |
bbb44d9f | 484 | error = pci_legacy_resume(dev); |
355a72d7 RW |
485 | } else { |
486 | error = pci_default_pm_resume_late(pci_dev); | |
bbb44d9f RW |
487 | } |
488 | ||
489 | return error; | |
490 | } | |
491 | ||
492 | static int pci_pm_resume_noirq(struct device *dev) | |
493 | { | |
355a72d7 | 494 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 495 | struct device_driver *drv = dev->driver; |
bbb44d9f RW |
496 | int error = 0; |
497 | ||
adf09493 | 498 | pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev)); |
bbb44d9f RW |
499 | |
500 | if (drv && drv->pm) { | |
501 | if (drv->pm->resume_noirq) | |
502 | error = drv->pm->resume_noirq(dev); | |
355a72d7 | 503 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f | 504 | error = pci_legacy_resume_early(dev); |
355a72d7 RW |
505 | } else { |
506 | pci_default_pm_resume_early(pci_dev); | |
bbb44d9f RW |
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); | |
bbb44d9f | 533 | } |
355a72d7 | 534 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f RW |
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 | { | |
355a72d7 | 544 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 545 | struct device_driver *drv = dev->driver; |
bbb44d9f RW |
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 | } | |
355a72d7 | 553 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f | 554 | error = pci_legacy_suspend_late(dev, PMSG_FREEZE); |
355a72d7 RW |
555 | } else { |
556 | pci_default_pm_suspend(pci_dev); | |
bbb44d9f RW |
557 | } |
558 | ||
559 | return error; | |
560 | } | |
561 | ||
562 | static int pci_pm_thaw(struct device *dev) | |
563 | { | |
355a72d7 | 564 | struct pci_dev *pci_dev = to_pci_dev(dev); |
bbb44d9f RW |
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); | |
355a72d7 RW |
571 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
572 | pci_fixup_device(pci_fixup_resume, pci_dev); | |
bbb44d9f RW |
573 | error = pci_legacy_resume(dev); |
574 | } | |
575 | ||
576 | return error; | |
577 | } | |
578 | ||
579 | static int pci_pm_thaw_noirq(struct device *dev) | |
580 | { | |
355a72d7 | 581 | struct pci_dev *pci_dev = to_pci_dev(dev); |
adf09493 | 582 | struct device_driver *drv = dev->driver; |
bbb44d9f RW |
583 | int error = 0; |
584 | ||
585 | if (drv && drv->pm) { | |
586 | if (drv->pm->thaw_noirq) | |
587 | error = drv->pm->thaw_noirq(dev); | |
355a72d7 | 588 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
adf09493 | 589 | pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev)); |
bbb44d9f RW |
590 | error = pci_legacy_resume_early(dev); |
591 | } | |
592 | ||
593 | return error; | |
594 | } | |
595 | ||
596 | static int pci_pm_poweroff(struct device *dev) | |
597 | { | |
355a72d7 | 598 | struct pci_dev *pci_dev = to_pci_dev(dev); |
bbb44d9f RW |
599 | struct device_driver *drv = dev->driver; |
600 | int error = 0; | |
601 | ||
355a72d7 | 602 | pci_fixup_device(pci_fixup_suspend, pci_dev); |
bbb44d9f RW |
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 | } | |
355a72d7 | 609 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f RW |
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 | { | |
adf09493 | 618 | struct device_driver *drv = dev->driver; |
bbb44d9f RW |
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 | } | |
355a72d7 | 626 | } else if (pci_has_legacy_pm_support(to_pci_dev(dev))) { |
bbb44d9f RW |
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; | |
355a72d7 | 637 | int error = 0; |
bbb44d9f RW |
638 | |
639 | if (drv && drv->pm) { | |
355a72d7 RW |
640 | if (drv->pm->restore) |
641 | error = drv->pm->restore(dev); | |
642 | } else if (pci_has_legacy_pm_support(pci_dev)) { | |
bbb44d9f | 643 | error = pci_legacy_resume(dev); |
355a72d7 RW |
644 | } else { |
645 | error = pci_default_pm_resume_late(pci_dev); | |
bbb44d9f RW |
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); | |
adf09493 | 655 | struct device_driver *drv = dev->driver; |
bbb44d9f RW |
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); | |
355a72d7 | 663 | } else if (pci_has_legacy_pm_support(pci_dev)) { |
bbb44d9f | 664 | error = pci_legacy_resume_early(dev); |
355a72d7 RW |
665 | } else { |
666 | pci_default_pm_resume_early(pci_dev); | |
bbb44d9f RW |
667 | } |
668 | pci_fixup_device(pci_fixup_resume_early, pci_dev); | |
669 | ||
670 | return error; | |
c8958177 | 671 | } |
1da177e4 | 672 | |
bbb44d9f RW |
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 | ||
adf09493 RW |
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, | |
bbb44d9f RW |
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 | ||
adf09493 | 703 | #define PCI_PM_OPS_PTR (&pci_dev_pm_ops) |
bbb44d9f RW |
704 | |
705 | #else /* !CONFIG_PM_SLEEP */ | |
706 | ||
707 | #define PCI_PM_OPS_PTR NULL | |
708 | ||
709 | #endif /* !CONFIG_PM_SLEEP */ | |
710 | ||
1da177e4 | 711 | /** |
863b18f4 | 712 | * __pci_register_driver - register a new pci driver |
1da177e4 | 713 | * @drv: the driver structure to register |
863b18f4 | 714 | * @owner: owner module of drv |
f95d882d | 715 | * @mod_name: module name string |
1da177e4 LT |
716 | * |
717 | * Adds the driver structure to the list of registered drivers. | |
718 | * Returns a negative value on error, otherwise 0. | |
eaae4b3a | 719 | * If no error occurred, the driver remains registered even if |
1da177e4 LT |
720 | * no device was claimed during registration. |
721 | */ | |
725522b5 GKH |
722 | int __pci_register_driver(struct pci_driver *drv, struct module *owner, |
723 | const char *mod_name) | |
1da177e4 LT |
724 | { |
725 | int error; | |
726 | ||
727 | /* initialize common driver fields */ | |
728 | drv->driver.name = drv->name; | |
729 | drv->driver.bus = &pci_bus_type; | |
863b18f4 | 730 | drv->driver.owner = owner; |
725522b5 | 731 | drv->driver.mod_name = mod_name; |
50b00755 | 732 | |
75865858 GKH |
733 | spin_lock_init(&drv->dynids.lock); |
734 | INIT_LIST_HEAD(&drv->dynids.list); | |
1da177e4 LT |
735 | |
736 | /* register with core */ | |
737 | error = driver_register(&drv->driver); | |
50bf14b3 AM |
738 | if (error) |
739 | return error; | |
1da177e4 | 740 | |
50bf14b3 AM |
741 | error = pci_create_newid_file(drv); |
742 | if (error) | |
743 | driver_unregister(&drv->driver); | |
1da177e4 LT |
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 | { | |
03d43b19 | 761 | pci_remove_newid_file(drv); |
1da177e4 LT |
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 | |
1da177e4 | 793 | * @dev: the PCI device structure to match against |
8f7020d3 | 794 | * @drv: the device driver to search for matching PCI device id structures |
1da177e4 LT |
795 | * |
796 | * Used by a driver to check whether a PCI device present in the | |
8f7020d3 | 797 | * system is in its list of supported devices. Returns the matching |
1da177e4 LT |
798 | * pci_device_id structure or %NULL if there is no match. |
799 | */ | |
75865858 | 800 | static int pci_bus_match(struct device *dev, struct device_driver *drv) |
1da177e4 | 801 | { |
75865858 GKH |
802 | struct pci_dev *pci_dev = to_pci_dev(dev); |
803 | struct pci_driver *pci_drv = to_pci_driver(drv); | |
1da177e4 LT |
804 | const struct pci_device_id *found_id; |
805 | ||
75865858 | 806 | found_id = pci_match_device(pci_drv, pci_dev); |
1da177e4 LT |
807 | if (found_id) |
808 | return 1; | |
809 | ||
75865858 | 810 | return 0; |
1da177e4 LT |
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 | |
7eff2e7a | 846 | int pci_uevent(struct device *dev, struct kobj_uevent_env *env) |
1da177e4 LT |
847 | { |
848 | return -ENODEV; | |
849 | } | |
850 | #endif | |
851 | ||
852 | struct bus_type pci_bus_type = { | |
853 | .name = "pci", | |
854 | .match = pci_bus_match, | |
312c004d | 855 | .uevent = pci_uevent, |
b15d686a RK |
856 | .probe = pci_device_probe, |
857 | .remove = pci_device_remove, | |
cbd69dbb | 858 | .shutdown = pci_device_shutdown, |
1da177e4 | 859 | .dev_attrs = pci_dev_attrs, |
bbb44d9f | 860 | .pm = PCI_PM_OPS_PTR, |
1da177e4 LT |
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 | ||
75865858 | 870 | EXPORT_SYMBOL(pci_match_id); |
863b18f4 | 871 | EXPORT_SYMBOL(__pci_register_driver); |
1da177e4 LT |
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); |