pci-driver.c 23 KB

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