pci-driver.c 35 KB

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