pci-driver.c 36 KB

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