pci-driver.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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 "pci.h"
  20. /*
  21. * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
  22. */
  23. struct pci_dynid {
  24. struct list_head node;
  25. struct pci_device_id id;
  26. };
  27. #ifdef CONFIG_HOTPLUG
  28. /**
  29. * store_new_id - add a new PCI device ID to this driver and re-probe devices
  30. * @driver: target device driver
  31. * @buf: buffer for scanning device ID data
  32. * @count: input size
  33. *
  34. * Adds a new dynamic pci device ID to this driver,
  35. * and causes the driver to probe for all devices again.
  36. */
  37. static ssize_t
  38. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  39. {
  40. struct pci_dynid *dynid;
  41. struct pci_driver *pdrv = to_pci_driver(driver);
  42. const struct pci_device_id *ids = pdrv->id_table;
  43. __u32 vendor, device, subvendor=PCI_ANY_ID,
  44. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  45. unsigned long driver_data=0;
  46. int fields=0;
  47. int retval=0;
  48. fields = sscanf(buf, "%x %x %x %x %x %x %lx",
  49. &vendor, &device, &subvendor, &subdevice,
  50. &class, &class_mask, &driver_data);
  51. if (fields < 2)
  52. return -EINVAL;
  53. /* Only accept driver_data values that match an existing id_table
  54. entry */
  55. if (ids) {
  56. retval = -EINVAL;
  57. while (ids->vendor || ids->subvendor || ids->class_mask) {
  58. if (driver_data == ids->driver_data) {
  59. retval = 0;
  60. break;
  61. }
  62. ids++;
  63. }
  64. if (retval) /* No match */
  65. return retval;
  66. }
  67. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  68. if (!dynid)
  69. return -ENOMEM;
  70. dynid->id.vendor = vendor;
  71. dynid->id.device = device;
  72. dynid->id.subvendor = subvendor;
  73. dynid->id.subdevice = subdevice;
  74. dynid->id.class = class;
  75. dynid->id.class_mask = class_mask;
  76. dynid->id.driver_data = driver_data;
  77. spin_lock(&pdrv->dynids.lock);
  78. list_add_tail(&dynid->node, &pdrv->dynids.list);
  79. spin_unlock(&pdrv->dynids.lock);
  80. if (get_driver(&pdrv->driver)) {
  81. retval = driver_attach(&pdrv->driver);
  82. put_driver(&pdrv->driver);
  83. }
  84. if (retval)
  85. return retval;
  86. return count;
  87. }
  88. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  89. /**
  90. * store_remove_id - remove a PCI device ID from this driver
  91. * @driver: target device driver
  92. * @buf: buffer for scanning device ID data
  93. * @count: input size
  94. *
  95. * Removes a dynamic pci device ID to this driver.
  96. */
  97. static ssize_t
  98. store_remove_id(struct device_driver *driver, const char *buf, size_t count)
  99. {
  100. struct pci_dynid *dynid, *n;
  101. struct pci_driver *pdrv = to_pci_driver(driver);
  102. __u32 vendor, device, subvendor = PCI_ANY_ID,
  103. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  104. int fields = 0;
  105. int retval = -ENODEV;
  106. fields = sscanf(buf, "%x %x %x %x %x %x",
  107. &vendor, &device, &subvendor, &subdevice,
  108. &class, &class_mask);
  109. if (fields < 2)
  110. return -EINVAL;
  111. spin_lock(&pdrv->dynids.lock);
  112. list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
  113. struct pci_device_id *id = &dynid->id;
  114. if ((id->vendor == vendor) &&
  115. (id->device == device) &&
  116. (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
  117. (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
  118. !((id->class ^ class) & class_mask)) {
  119. list_del(&dynid->node);
  120. kfree(dynid);
  121. retval = 0;
  122. break;
  123. }
  124. }
  125. spin_unlock(&pdrv->dynids.lock);
  126. if (retval)
  127. return retval;
  128. return count;
  129. }
  130. static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
  131. static void
  132. pci_free_dynids(struct pci_driver *drv)
  133. {
  134. struct pci_dynid *dynid, *n;
  135. spin_lock(&drv->dynids.lock);
  136. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  137. list_del(&dynid->node);
  138. kfree(dynid);
  139. }
  140. spin_unlock(&drv->dynids.lock);
  141. }
  142. static int
  143. pci_create_newid_file(struct pci_driver *drv)
  144. {
  145. int error = 0;
  146. if (drv->probe != NULL)
  147. error = driver_create_file(&drv->driver, &driver_attr_new_id);
  148. return error;
  149. }
  150. static void pci_remove_newid_file(struct pci_driver *drv)
  151. {
  152. driver_remove_file(&drv->driver, &driver_attr_new_id);
  153. }
  154. static int
  155. pci_create_removeid_file(struct pci_driver *drv)
  156. {
  157. int error = 0;
  158. if (drv->probe != NULL)
  159. error = driver_create_file(&drv->driver,&driver_attr_remove_id);
  160. return error;
  161. }
  162. static void pci_remove_removeid_file(struct pci_driver *drv)
  163. {
  164. driver_remove_file(&drv->driver, &driver_attr_remove_id);
  165. }
  166. #else /* !CONFIG_HOTPLUG */
  167. static inline void pci_free_dynids(struct pci_driver *drv) {}
  168. static inline int pci_create_newid_file(struct pci_driver *drv)
  169. {
  170. return 0;
  171. }
  172. static inline void pci_remove_newid_file(struct pci_driver *drv) {}
  173. static inline int pci_create_removeid_file(struct pci_driver *drv)
  174. {
  175. return 0;
  176. }
  177. static inline void pci_remove_removeid_file(struct pci_driver *drv) {}
  178. #endif
  179. /**
  180. * pci_match_id - See if a pci device matches a given pci_id table
  181. * @ids: array of PCI device id structures to search in
  182. * @dev: the PCI device structure to match against.
  183. *
  184. * Used by a driver to check whether a PCI device present in the
  185. * system is in its list of supported devices. Returns the matching
  186. * pci_device_id structure or %NULL if there is no match.
  187. *
  188. * Deprecated, don't use this as it will not catch any dynamic ids
  189. * that a driver might want to check for.
  190. */
  191. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  192. struct pci_dev *dev)
  193. {
  194. if (ids) {
  195. while (ids->vendor || ids->subvendor || ids->class_mask) {
  196. if (pci_match_one_device(ids, dev))
  197. return ids;
  198. ids++;
  199. }
  200. }
  201. return NULL;
  202. }
  203. /**
  204. * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  205. * @drv: the PCI driver to match against
  206. * @dev: the PCI device structure to match against
  207. *
  208. * Used by a driver to check whether a PCI device present in the
  209. * system is in its list of supported devices. Returns the matching
  210. * pci_device_id structure or %NULL if there is no match.
  211. */
  212. static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  213. struct pci_dev *dev)
  214. {
  215. struct pci_dynid *dynid;
  216. /* Look at the dynamic ids first, before the static ones */
  217. spin_lock(&drv->dynids.lock);
  218. list_for_each_entry(dynid, &drv->dynids.list, node) {
  219. if (pci_match_one_device(&dynid->id, dev)) {
  220. spin_unlock(&drv->dynids.lock);
  221. return &dynid->id;
  222. }
  223. }
  224. spin_unlock(&drv->dynids.lock);
  225. return pci_match_id(drv->id_table, dev);
  226. }
  227. struct drv_dev_and_id {
  228. struct pci_driver *drv;
  229. struct pci_dev *dev;
  230. const struct pci_device_id *id;
  231. };
  232. static long local_pci_probe(void *_ddi)
  233. {
  234. struct drv_dev_and_id *ddi = _ddi;
  235. return ddi->drv->probe(ddi->dev, ddi->id);
  236. }
  237. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  238. const struct pci_device_id *id)
  239. {
  240. int error, node;
  241. struct drv_dev_and_id ddi = { drv, dev, id };
  242. /* Execute driver initialization on node where the device's
  243. bus is attached to. This way the driver likely allocates
  244. its local memory on the right node without any need to
  245. change it. */
  246. node = dev_to_node(&dev->dev);
  247. if (node >= 0) {
  248. int cpu;
  249. node_to_cpumask_ptr(nodecpumask, node);
  250. get_online_cpus();
  251. cpu = cpumask_any_and(nodecpumask, cpu_online_mask);
  252. if (cpu < nr_cpu_ids)
  253. error = work_on_cpu(cpu, local_pci_probe, &ddi);
  254. else
  255. error = local_pci_probe(&ddi);
  256. put_online_cpus();
  257. } else
  258. error = local_pci_probe(&ddi);
  259. return error;
  260. }
  261. /**
  262. * __pci_device_probe()
  263. * @drv: driver to call to check if it wants the PCI device
  264. * @pci_dev: PCI device being probed
  265. *
  266. * returns 0 on success, else error.
  267. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  268. */
  269. static int
  270. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  271. {
  272. const struct pci_device_id *id;
  273. int error = 0;
  274. if (!pci_dev->driver && drv->probe) {
  275. error = -ENODEV;
  276. id = pci_match_device(drv, pci_dev);
  277. if (id)
  278. error = pci_call_probe(drv, pci_dev, id);
  279. if (error >= 0) {
  280. pci_dev->driver = drv;
  281. error = 0;
  282. }
  283. }
  284. return error;
  285. }
  286. static int pci_device_probe(struct device * dev)
  287. {
  288. int error = 0;
  289. struct pci_driver *drv;
  290. struct pci_dev *pci_dev;
  291. drv = to_pci_driver(dev->driver);
  292. pci_dev = to_pci_dev(dev);
  293. pci_dev_get(pci_dev);
  294. error = __pci_device_probe(drv, pci_dev);
  295. if (error)
  296. pci_dev_put(pci_dev);
  297. return error;
  298. }
  299. static int pci_device_remove(struct device * dev)
  300. {
  301. struct pci_dev * pci_dev = to_pci_dev(dev);
  302. struct pci_driver * drv = pci_dev->driver;
  303. if (drv) {
  304. if (drv->remove)
  305. drv->remove(pci_dev);
  306. pci_dev->driver = NULL;
  307. }
  308. /*
  309. * If the device is still on, set the power state as "unknown",
  310. * since it might change by the next time we load the driver.
  311. */
  312. if (pci_dev->current_state == PCI_D0)
  313. pci_dev->current_state = PCI_UNKNOWN;
  314. /*
  315. * We would love to complain here if pci_dev->is_enabled is set, that
  316. * the driver should have called pci_disable_device(), but the
  317. * unfortunate fact is there are too many odd BIOS and bridge setups
  318. * that don't like drivers doing that all of the time.
  319. * Oh well, we can dream of sane hardware when we sleep, no matter how
  320. * horrible the crap we have to deal with is when we are awake...
  321. */
  322. pci_dev_put(pci_dev);
  323. return 0;
  324. }
  325. static void pci_device_shutdown(struct device *dev)
  326. {
  327. struct pci_dev *pci_dev = to_pci_dev(dev);
  328. struct pci_driver *drv = pci_dev->driver;
  329. if (drv && drv->shutdown)
  330. drv->shutdown(pci_dev);
  331. pci_msi_shutdown(pci_dev);
  332. pci_msix_shutdown(pci_dev);
  333. }
  334. #ifdef CONFIG_PM_SLEEP
  335. /*
  336. * Default "suspend" method for devices that have no driver provided suspend,
  337. * or not even a driver at all (second part).
  338. */
  339. static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
  340. {
  341. /*
  342. * mark its power state as "unknown", since we don't know if
  343. * e.g. the BIOS will change its device state when we suspend.
  344. */
  345. if (pci_dev->current_state == PCI_D0)
  346. pci_dev->current_state = PCI_UNKNOWN;
  347. }
  348. /*
  349. * Default "resume" method for devices that have no driver provided resume,
  350. * or not even a driver at all (second part).
  351. */
  352. static int pci_pm_reenable_device(struct pci_dev *pci_dev)
  353. {
  354. int retval;
  355. /* if the device was enabled before suspend, reenable */
  356. retval = pci_reenable_device(pci_dev);
  357. /*
  358. * if the device was busmaster before the suspend, make it busmaster
  359. * again
  360. */
  361. if (pci_dev->is_busmaster)
  362. pci_set_master(pci_dev);
  363. return retval;
  364. }
  365. static int pci_legacy_suspend(struct device *dev, pm_message_t state)
  366. {
  367. struct pci_dev * pci_dev = to_pci_dev(dev);
  368. struct pci_driver * drv = pci_dev->driver;
  369. pci_dev->state_saved = false;
  370. if (drv && drv->suspend) {
  371. pci_power_t prev = pci_dev->current_state;
  372. int error;
  373. error = drv->suspend(pci_dev, state);
  374. suspend_report_result(drv->suspend, error);
  375. if (error)
  376. return error;
  377. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  378. && pci_dev->current_state != PCI_UNKNOWN) {
  379. WARN_ONCE(pci_dev->current_state != prev,
  380. "PCI PM: Device state not saved by %pF\n",
  381. drv->suspend);
  382. }
  383. }
  384. pci_fixup_device(pci_fixup_suspend, pci_dev);
  385. return 0;
  386. }
  387. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  388. {
  389. struct pci_dev * pci_dev = to_pci_dev(dev);
  390. struct pci_driver * drv = pci_dev->driver;
  391. if (drv && drv->suspend_late) {
  392. pci_power_t prev = pci_dev->current_state;
  393. int error;
  394. error = drv->suspend_late(pci_dev, state);
  395. suspend_report_result(drv->suspend_late, error);
  396. if (error)
  397. return error;
  398. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  399. && pci_dev->current_state != PCI_UNKNOWN) {
  400. WARN_ONCE(pci_dev->current_state != prev,
  401. "PCI PM: Device state not saved by %pF\n",
  402. drv->suspend_late);
  403. return 0;
  404. }
  405. }
  406. if (!pci_dev->state_saved)
  407. pci_save_state(pci_dev);
  408. pci_pm_set_unknown_state(pci_dev);
  409. return 0;
  410. }
  411. static int pci_legacy_resume_early(struct device *dev)
  412. {
  413. struct pci_dev * pci_dev = to_pci_dev(dev);
  414. struct pci_driver * drv = pci_dev->driver;
  415. return drv && drv->resume_early ?
  416. drv->resume_early(pci_dev) : 0;
  417. }
  418. static int pci_legacy_resume(struct device *dev)
  419. {
  420. struct pci_dev * pci_dev = to_pci_dev(dev);
  421. struct pci_driver * drv = pci_dev->driver;
  422. pci_fixup_device(pci_fixup_resume, pci_dev);
  423. return drv && drv->resume ?
  424. drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
  425. }
  426. /* Auxiliary functions used by the new power management framework */
  427. /**
  428. * pci_restore_standard_config - restore standard config registers of PCI device
  429. * @pci_dev: PCI device to handle
  430. */
  431. static int pci_restore_standard_config(struct pci_dev *pci_dev)
  432. {
  433. pci_update_current_state(pci_dev, PCI_UNKNOWN);
  434. if (pci_dev->current_state != PCI_D0) {
  435. int error = pci_set_power_state(pci_dev, PCI_D0);
  436. if (error)
  437. return error;
  438. }
  439. return pci_dev->state_saved ? pci_restore_state(pci_dev) : 0;
  440. }
  441. static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
  442. {
  443. pci_restore_standard_config(pci_dev);
  444. pci_dev->state_saved = false;
  445. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  446. }
  447. static void pci_pm_default_resume(struct pci_dev *pci_dev)
  448. {
  449. pci_fixup_device(pci_fixup_resume, pci_dev);
  450. if (!pci_is_bridge(pci_dev))
  451. pci_enable_wake(pci_dev, PCI_D0, false);
  452. }
  453. static void pci_pm_default_suspend(struct pci_dev *pci_dev)
  454. {
  455. /* Disable non-bridge devices without PM support */
  456. if (!pci_is_bridge(pci_dev))
  457. pci_disable_enabled_device(pci_dev);
  458. }
  459. static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
  460. {
  461. struct pci_driver *drv = pci_dev->driver;
  462. bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
  463. || drv->resume_early);
  464. /*
  465. * Legacy PM support is used by default, so warn if the new framework is
  466. * supported as well. Drivers are supposed to support either the
  467. * former, or the latter, but not both at the same time.
  468. */
  469. WARN_ON(ret && drv->driver.pm);
  470. return ret;
  471. }
  472. /* New power management framework */
  473. static int pci_pm_prepare(struct device *dev)
  474. {
  475. struct device_driver *drv = dev->driver;
  476. int error = 0;
  477. if (drv && drv->pm && drv->pm->prepare)
  478. error = drv->pm->prepare(dev);
  479. return error;
  480. }
  481. static void pci_pm_complete(struct device *dev)
  482. {
  483. struct device_driver *drv = dev->driver;
  484. if (drv && drv->pm && drv->pm->complete)
  485. drv->pm->complete(dev);
  486. }
  487. #ifdef CONFIG_SUSPEND
  488. static int pci_pm_suspend(struct device *dev)
  489. {
  490. struct pci_dev *pci_dev = to_pci_dev(dev);
  491. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  492. if (pci_has_legacy_pm_support(pci_dev))
  493. return pci_legacy_suspend(dev, PMSG_SUSPEND);
  494. pci_dev->state_saved = false;
  495. if (!pm) {
  496. pci_pm_default_suspend(pci_dev);
  497. goto Fixup;
  498. }
  499. if (pm->suspend) {
  500. pci_power_t prev = pci_dev->current_state;
  501. int error;
  502. error = pm->suspend(dev);
  503. suspend_report_result(pm->suspend, 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: State of device not saved by %pF\n",
  510. pm->suspend);
  511. }
  512. }
  513. Fixup:
  514. pci_fixup_device(pci_fixup_suspend, pci_dev);
  515. return 0;
  516. }
  517. static int pci_pm_suspend_noirq(struct device *dev)
  518. {
  519. struct pci_dev *pci_dev = to_pci_dev(dev);
  520. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  521. if (pci_has_legacy_pm_support(pci_dev))
  522. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  523. if (!pm) {
  524. pci_save_state(pci_dev);
  525. return 0;
  526. }
  527. if (pm->suspend_noirq) {
  528. pci_power_t prev = pci_dev->current_state;
  529. int error;
  530. error = pm->suspend_noirq(dev);
  531. suspend_report_result(pm->suspend_noirq, error);
  532. if (error)
  533. return error;
  534. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  535. && pci_dev->current_state != PCI_UNKNOWN) {
  536. WARN_ONCE(pci_dev->current_state != prev,
  537. "PCI PM: State of device not saved by %pF\n",
  538. pm->suspend_noirq);
  539. return 0;
  540. }
  541. }
  542. if (!pci_dev->state_saved) {
  543. pci_save_state(pci_dev);
  544. if (!pci_is_bridge(pci_dev))
  545. pci_prepare_to_sleep(pci_dev);
  546. }
  547. pci_pm_set_unknown_state(pci_dev);
  548. return 0;
  549. }
  550. static int pci_pm_resume_noirq(struct device *dev)
  551. {
  552. struct pci_dev *pci_dev = to_pci_dev(dev);
  553. struct device_driver *drv = dev->driver;
  554. int error = 0;
  555. pci_pm_default_resume_noirq(pci_dev);
  556. if (pci_has_legacy_pm_support(pci_dev))
  557. return pci_legacy_resume_early(dev);
  558. if (drv && drv->pm && drv->pm->resume_noirq)
  559. error = drv->pm->resume_noirq(dev);
  560. return error;
  561. }
  562. static int pci_pm_resume(struct device *dev)
  563. {
  564. struct pci_dev *pci_dev = to_pci_dev(dev);
  565. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  566. int error = 0;
  567. /*
  568. * This is necessary for the suspend error path in which resume is
  569. * called without restoring the standard config registers of the device.
  570. */
  571. if (pci_dev->state_saved)
  572. pci_restore_standard_config(pci_dev);
  573. if (pci_has_legacy_pm_support(pci_dev))
  574. return pci_legacy_resume(dev);
  575. pci_pm_default_resume(pci_dev);
  576. if (pm) {
  577. if (pm->resume)
  578. error = pm->resume(dev);
  579. } else {
  580. pci_pm_reenable_device(pci_dev);
  581. }
  582. return 0;
  583. }
  584. #else /* !CONFIG_SUSPEND */
  585. #define pci_pm_suspend NULL
  586. #define pci_pm_suspend_noirq NULL
  587. #define pci_pm_resume NULL
  588. #define pci_pm_resume_noirq NULL
  589. #endif /* !CONFIG_SUSPEND */
  590. #ifdef CONFIG_HIBERNATION
  591. static int pci_pm_freeze(struct device *dev)
  592. {
  593. struct pci_dev *pci_dev = to_pci_dev(dev);
  594. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  595. if (pci_has_legacy_pm_support(pci_dev))
  596. return pci_legacy_suspend(dev, PMSG_FREEZE);
  597. pci_dev->state_saved = false;
  598. if (!pm) {
  599. pci_pm_default_suspend(pci_dev);
  600. return 0;
  601. }
  602. if (pm->freeze) {
  603. int error;
  604. error = pm->freeze(dev);
  605. suspend_report_result(pm->freeze, error);
  606. if (error)
  607. return error;
  608. }
  609. return 0;
  610. }
  611. static int pci_pm_freeze_noirq(struct device *dev)
  612. {
  613. struct pci_dev *pci_dev = to_pci_dev(dev);
  614. struct device_driver *drv = dev->driver;
  615. if (pci_has_legacy_pm_support(pci_dev))
  616. return pci_legacy_suspend_late(dev, PMSG_FREEZE);
  617. if (drv && drv->pm && drv->pm->freeze_noirq) {
  618. int error;
  619. error = drv->pm->freeze_noirq(dev);
  620. suspend_report_result(drv->pm->freeze_noirq, error);
  621. if (error)
  622. return error;
  623. }
  624. if (!pci_dev->state_saved)
  625. pci_save_state(pci_dev);
  626. pci_pm_set_unknown_state(pci_dev);
  627. return 0;
  628. }
  629. static int pci_pm_thaw_noirq(struct device *dev)
  630. {
  631. struct pci_dev *pci_dev = to_pci_dev(dev);
  632. struct device_driver *drv = dev->driver;
  633. int error = 0;
  634. if (pci_has_legacy_pm_support(pci_dev))
  635. return pci_legacy_resume_early(dev);
  636. pci_update_current_state(pci_dev, PCI_D0);
  637. if (drv && drv->pm && drv->pm->thaw_noirq)
  638. error = drv->pm->thaw_noirq(dev);
  639. return error;
  640. }
  641. static int pci_pm_thaw(struct device *dev)
  642. {
  643. struct pci_dev *pci_dev = to_pci_dev(dev);
  644. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  645. int error = 0;
  646. if (pci_has_legacy_pm_support(pci_dev))
  647. return pci_legacy_resume(dev);
  648. if (pm) {
  649. if (pm->thaw)
  650. error = pm->thaw(dev);
  651. } else {
  652. pci_pm_reenable_device(pci_dev);
  653. }
  654. return error;
  655. }
  656. static int pci_pm_poweroff(struct device *dev)
  657. {
  658. struct pci_dev *pci_dev = to_pci_dev(dev);
  659. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  660. if (pci_has_legacy_pm_support(pci_dev))
  661. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  662. pci_dev->state_saved = false;
  663. if (!pm) {
  664. pci_pm_default_suspend(pci_dev);
  665. goto Fixup;
  666. }
  667. if (pm->poweroff) {
  668. int error;
  669. error = pm->poweroff(dev);
  670. suspend_report_result(pm->poweroff, error);
  671. if (error)
  672. return error;
  673. }
  674. Fixup:
  675. pci_fixup_device(pci_fixup_suspend, pci_dev);
  676. return 0;
  677. }
  678. static int pci_pm_poweroff_noirq(struct device *dev)
  679. {
  680. struct pci_dev *pci_dev = to_pci_dev(dev);
  681. struct device_driver *drv = dev->driver;
  682. if (pci_has_legacy_pm_support(to_pci_dev(dev)))
  683. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  684. if (!drv || !drv->pm)
  685. return 0;
  686. if (drv->pm->poweroff_noirq) {
  687. int error;
  688. error = drv->pm->poweroff_noirq(dev);
  689. suspend_report_result(drv->pm->poweroff_noirq, error);
  690. if (error)
  691. return error;
  692. }
  693. if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
  694. pci_prepare_to_sleep(pci_dev);
  695. return 0;
  696. }
  697. static int pci_pm_restore_noirq(struct device *dev)
  698. {
  699. struct pci_dev *pci_dev = to_pci_dev(dev);
  700. struct device_driver *drv = dev->driver;
  701. int error = 0;
  702. pci_pm_default_resume_noirq(pci_dev);
  703. if (pci_has_legacy_pm_support(pci_dev))
  704. return pci_legacy_resume_early(dev);
  705. if (drv && drv->pm && drv->pm->restore_noirq)
  706. error = drv->pm->restore_noirq(dev);
  707. return error;
  708. }
  709. static int pci_pm_restore(struct device *dev)
  710. {
  711. struct pci_dev *pci_dev = to_pci_dev(dev);
  712. struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  713. int error = 0;
  714. /*
  715. * This is necessary for the hibernation error path in which restore is
  716. * called without restoring the standard config registers of the device.
  717. */
  718. if (pci_dev->state_saved)
  719. pci_restore_standard_config(pci_dev);
  720. if (pci_has_legacy_pm_support(pci_dev))
  721. return pci_legacy_resume(dev);
  722. pci_pm_default_resume(pci_dev);
  723. if (pm) {
  724. if (pm->restore)
  725. error = pm->restore(dev);
  726. } else {
  727. pci_pm_reenable_device(pci_dev);
  728. }
  729. return error;
  730. }
  731. #else /* !CONFIG_HIBERNATION */
  732. #define pci_pm_freeze NULL
  733. #define pci_pm_freeze_noirq NULL
  734. #define pci_pm_thaw NULL
  735. #define pci_pm_thaw_noirq NULL
  736. #define pci_pm_poweroff NULL
  737. #define pci_pm_poweroff_noirq NULL
  738. #define pci_pm_restore NULL
  739. #define pci_pm_restore_noirq NULL
  740. #endif /* !CONFIG_HIBERNATION */
  741. struct dev_pm_ops pci_dev_pm_ops = {
  742. .prepare = pci_pm_prepare,
  743. .complete = pci_pm_complete,
  744. .suspend = pci_pm_suspend,
  745. .resume = pci_pm_resume,
  746. .freeze = pci_pm_freeze,
  747. .thaw = pci_pm_thaw,
  748. .poweroff = pci_pm_poweroff,
  749. .restore = pci_pm_restore,
  750. .suspend_noirq = pci_pm_suspend_noirq,
  751. .resume_noirq = pci_pm_resume_noirq,
  752. .freeze_noirq = pci_pm_freeze_noirq,
  753. .thaw_noirq = pci_pm_thaw_noirq,
  754. .poweroff_noirq = pci_pm_poweroff_noirq,
  755. .restore_noirq = pci_pm_restore_noirq,
  756. };
  757. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  758. #else /* !CONFIG_PM_SLEEP */
  759. #define PCI_PM_OPS_PTR NULL
  760. #endif /* !CONFIG_PM_SLEEP */
  761. /**
  762. * __pci_register_driver - register a new pci driver
  763. * @drv: the driver structure to register
  764. * @owner: owner module of drv
  765. * @mod_name: module name string
  766. *
  767. * Adds the driver structure to the list of registered drivers.
  768. * Returns a negative value on error, otherwise 0.
  769. * If no error occurred, the driver remains registered even if
  770. * no device was claimed during registration.
  771. */
  772. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  773. const char *mod_name)
  774. {
  775. int error;
  776. /* initialize common driver fields */
  777. drv->driver.name = drv->name;
  778. drv->driver.bus = &pci_bus_type;
  779. drv->driver.owner = owner;
  780. drv->driver.mod_name = mod_name;
  781. spin_lock_init(&drv->dynids.lock);
  782. INIT_LIST_HEAD(&drv->dynids.list);
  783. /* register with core */
  784. error = driver_register(&drv->driver);
  785. if (error)
  786. goto out;
  787. error = pci_create_newid_file(drv);
  788. if (error)
  789. goto out_newid;
  790. error = pci_create_removeid_file(drv);
  791. if (error)
  792. goto out_removeid;
  793. out:
  794. return error;
  795. out_removeid:
  796. pci_remove_newid_file(drv);
  797. out_newid:
  798. driver_unregister(&drv->driver);
  799. goto out;
  800. }
  801. /**
  802. * pci_unregister_driver - unregister a pci driver
  803. * @drv: the driver structure to unregister
  804. *
  805. * Deletes the driver structure from the list of registered PCI drivers,
  806. * gives it a chance to clean up by calling its remove() function for
  807. * each device it was responsible for, and marks those devices as
  808. * driverless.
  809. */
  810. void
  811. pci_unregister_driver(struct pci_driver *drv)
  812. {
  813. pci_remove_removeid_file(drv);
  814. pci_remove_newid_file(drv);
  815. driver_unregister(&drv->driver);
  816. pci_free_dynids(drv);
  817. }
  818. static struct pci_driver pci_compat_driver = {
  819. .name = "compat"
  820. };
  821. /**
  822. * pci_dev_driver - get the pci_driver of a device
  823. * @dev: the device to query
  824. *
  825. * Returns the appropriate pci_driver structure or %NULL if there is no
  826. * registered driver for the device.
  827. */
  828. struct pci_driver *
  829. pci_dev_driver(const struct pci_dev *dev)
  830. {
  831. if (dev->driver)
  832. return dev->driver;
  833. else {
  834. int i;
  835. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  836. if (dev->resource[i].flags & IORESOURCE_BUSY)
  837. return &pci_compat_driver;
  838. }
  839. return NULL;
  840. }
  841. /**
  842. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  843. * @dev: the PCI device structure to match against
  844. * @drv: the device driver to search for matching PCI device id structures
  845. *
  846. * Used by a driver to check whether a PCI device present in the
  847. * system is in its list of supported devices. Returns the matching
  848. * pci_device_id structure or %NULL if there is no match.
  849. */
  850. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  851. {
  852. struct pci_dev *pci_dev = to_pci_dev(dev);
  853. struct pci_driver *pci_drv = to_pci_driver(drv);
  854. const struct pci_device_id *found_id;
  855. found_id = pci_match_device(pci_drv, pci_dev);
  856. if (found_id)
  857. return 1;
  858. return 0;
  859. }
  860. /**
  861. * pci_dev_get - increments the reference count of the pci device structure
  862. * @dev: the device being referenced
  863. *
  864. * Each live reference to a device should be refcounted.
  865. *
  866. * Drivers for PCI devices should normally record such references in
  867. * their probe() methods, when they bind to a device, and release
  868. * them by calling pci_dev_put(), in their disconnect() methods.
  869. *
  870. * A pointer to the device with the incremented reference counter is returned.
  871. */
  872. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  873. {
  874. if (dev)
  875. get_device(&dev->dev);
  876. return dev;
  877. }
  878. /**
  879. * pci_dev_put - release a use of the pci device structure
  880. * @dev: device that's been disconnected
  881. *
  882. * Must be called when a user of a device is finished with it. When the last
  883. * user of the device calls this function, the memory of the device is freed.
  884. */
  885. void pci_dev_put(struct pci_dev *dev)
  886. {
  887. if (dev)
  888. put_device(&dev->dev);
  889. }
  890. #ifndef CONFIG_HOTPLUG
  891. int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  892. {
  893. return -ENODEV;
  894. }
  895. #endif
  896. struct bus_type pci_bus_type = {
  897. .name = "pci",
  898. .match = pci_bus_match,
  899. .uevent = pci_uevent,
  900. .probe = pci_device_probe,
  901. .remove = pci_device_remove,
  902. .shutdown = pci_device_shutdown,
  903. .dev_attrs = pci_dev_attrs,
  904. .bus_attrs = pci_bus_attrs,
  905. .pm = PCI_PM_OPS_PTR,
  906. };
  907. static int __init pci_driver_init(void)
  908. {
  909. return bus_register(&pci_bus_type);
  910. }
  911. postcore_initcall(pci_driver_init);
  912. EXPORT_SYMBOL(pci_match_id);
  913. EXPORT_SYMBOL(__pci_register_driver);
  914. EXPORT_SYMBOL(pci_unregister_driver);
  915. EXPORT_SYMBOL(pci_dev_driver);
  916. EXPORT_SYMBOL(pci_bus_type);
  917. EXPORT_SYMBOL(pci_dev_get);
  918. EXPORT_SYMBOL(pci_dev_put);