pci-driver.c 35 KB

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