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