pci-driver.c 35 KB

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