pci-driver.c 24 KB

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