pci-driver.c 34 KB

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