pci-driver.c 35 KB

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