pci-driver.c 36 KB

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