pci-driver.c 34 KB

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