pci-driver.c 35 KB

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