pci-driver.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. i = drv->suspend(pci_dev, state);
  313. suspend_report_result(drv->suspend, i);
  314. } else {
  315. pci_save_state(pci_dev);
  316. /*
  317. * This is for compatibility with existing code with legacy PM
  318. * support.
  319. */
  320. pci_pm_set_unknown_state(pci_dev);
  321. }
  322. pci_fixup_device(pci_fixup_suspend, pci_dev);
  323. return i;
  324. }
  325. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  326. {
  327. struct pci_dev * pci_dev = to_pci_dev(dev);
  328. struct pci_driver * drv = pci_dev->driver;
  329. int i = 0;
  330. if (drv && drv->suspend_late) {
  331. i = drv->suspend_late(pci_dev, state);
  332. suspend_report_result(drv->suspend_late, i);
  333. }
  334. return i;
  335. }
  336. static int pci_legacy_resume_early(struct device *dev)
  337. {
  338. int error = 0;
  339. struct pci_dev * pci_dev = to_pci_dev(dev);
  340. struct pci_driver * drv = pci_dev->driver;
  341. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  342. if (drv && drv->resume_early)
  343. error = drv->resume_early(pci_dev);
  344. return error;
  345. }
  346. static int pci_legacy_resume(struct device *dev)
  347. {
  348. int error;
  349. struct pci_dev * pci_dev = to_pci_dev(dev);
  350. struct pci_driver * drv = pci_dev->driver;
  351. pci_fixup_device(pci_fixup_resume, pci_dev);
  352. if (drv && drv->resume) {
  353. error = drv->resume(pci_dev);
  354. } else {
  355. /* restore the PCI config space */
  356. pci_restore_state(pci_dev);
  357. error = pci_pm_reenable_device(pci_dev);
  358. }
  359. return error;
  360. }
  361. /* Auxiliary functions used by the new power management framework */
  362. static int pci_restore_standard_config(struct pci_dev *pci_dev)
  363. {
  364. struct pci_dev *parent = pci_dev->bus->self;
  365. int error = 0;
  366. /* Check if the device's bus is operational */
  367. if (!parent || parent->current_state == PCI_D0) {
  368. pci_restore_state(pci_dev);
  369. pci_update_current_state(pci_dev, PCI_D0);
  370. } else {
  371. dev_warn(&pci_dev->dev, "unable to restore config, "
  372. "bridge %s in low power state D%d\n", pci_name(parent),
  373. parent->current_state);
  374. pci_dev->current_state = PCI_UNKNOWN;
  375. error = -EAGAIN;
  376. }
  377. return error;
  378. }
  379. static bool pci_is_bridge(struct pci_dev *pci_dev)
  380. {
  381. return !!(pci_dev->subordinate);
  382. }
  383. static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
  384. {
  385. if (pci_restore_standard_config(pci_dev))
  386. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  387. }
  388. static int pci_pm_default_resume(struct pci_dev *pci_dev)
  389. {
  390. /*
  391. * pci_restore_standard_config() should have been called once already,
  392. * but it would have failed if the device's parent bridge had not been
  393. * in power state D0 at that time. Check it and try again if necessary.
  394. */
  395. if (pci_dev->current_state == PCI_UNKNOWN) {
  396. int error = pci_restore_standard_config(pci_dev);
  397. if (error)
  398. return error;
  399. }
  400. pci_fixup_device(pci_fixup_resume, pci_dev);
  401. if (!pci_is_bridge(pci_dev))
  402. pci_enable_wake(pci_dev, PCI_D0, false);
  403. return pci_pm_reenable_device(pci_dev);
  404. }
  405. static void pci_pm_default_suspend_generic(struct pci_dev *pci_dev)
  406. {
  407. /* If device is enabled at this point, disable it */
  408. pci_disable_enabled_device(pci_dev);
  409. /*
  410. * Save state with interrupts enabled, because in principle the bus the
  411. * device is on may be put into a low power state after this code runs.
  412. */
  413. pci_save_state(pci_dev);
  414. }
  415. static void pci_pm_default_suspend(struct pci_dev *pci_dev)
  416. {
  417. pci_pm_default_suspend_generic(pci_dev);
  418. if (!pci_is_bridge(pci_dev))
  419. pci_prepare_to_sleep(pci_dev);
  420. pci_fixup_device(pci_fixup_suspend, pci_dev);
  421. }
  422. static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
  423. {
  424. struct pci_driver *drv = pci_dev->driver;
  425. bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
  426. || drv->resume_early);
  427. /*
  428. * Legacy PM support is used by default, so warn if the new framework is
  429. * supported as well. Drivers are supposed to support either the
  430. * former, or the latter, but not both at the same time.
  431. */
  432. WARN_ON(ret && drv->driver.pm);
  433. return ret;
  434. }
  435. /* New power management framework */
  436. static int pci_pm_prepare(struct device *dev)
  437. {
  438. struct device_driver *drv = dev->driver;
  439. int error = 0;
  440. if (drv && drv->pm && drv->pm->prepare)
  441. error = drv->pm->prepare(dev);
  442. return error;
  443. }
  444. static void pci_pm_complete(struct device *dev)
  445. {
  446. struct device_driver *drv = dev->driver;
  447. if (drv && drv->pm && drv->pm->complete)
  448. drv->pm->complete(dev);
  449. }
  450. #ifdef CONFIG_SUSPEND
  451. static int pci_pm_suspend(struct device *dev)
  452. {
  453. struct pci_dev *pci_dev = to_pci_dev(dev);
  454. struct device_driver *drv = dev->driver;
  455. int error = 0;
  456. if (pci_has_legacy_pm_support(pci_dev))
  457. return pci_legacy_suspend(dev, PMSG_SUSPEND);
  458. if (drv && drv->pm && drv->pm->suspend) {
  459. error = drv->pm->suspend(dev);
  460. suspend_report_result(drv->pm->suspend, error);
  461. }
  462. if (!error)
  463. pci_pm_default_suspend(pci_dev);
  464. return error;
  465. }
  466. static int pci_pm_suspend_noirq(struct device *dev)
  467. {
  468. struct pci_dev *pci_dev = to_pci_dev(dev);
  469. struct device_driver *drv = dev->driver;
  470. int error = 0;
  471. if (pci_has_legacy_pm_support(pci_dev))
  472. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  473. if (drv && drv->pm && drv->pm->suspend_noirq) {
  474. error = drv->pm->suspend_noirq(dev);
  475. suspend_report_result(drv->pm->suspend_noirq, error);
  476. }
  477. if (!error)
  478. pci_pm_set_unknown_state(pci_dev);
  479. return error;
  480. }
  481. static int pci_pm_resume_noirq(struct device *dev)
  482. {
  483. struct pci_dev *pci_dev = to_pci_dev(dev);
  484. struct device_driver *drv = dev->driver;
  485. int error = 0;
  486. if (pci_has_legacy_pm_support(pci_dev))
  487. return pci_legacy_resume_early(dev);
  488. pci_pm_default_resume_noirq(pci_dev);
  489. if (drv && drv->pm && drv->pm->resume_noirq)
  490. error = drv->pm->resume_noirq(dev);
  491. return error;
  492. }
  493. static int pci_pm_resume(struct device *dev)
  494. {
  495. struct pci_dev *pci_dev = to_pci_dev(dev);
  496. struct device_driver *drv = dev->driver;
  497. int error = 0;
  498. if (pci_has_legacy_pm_support(pci_dev))
  499. return pci_legacy_resume(dev);
  500. error = pci_pm_default_resume(pci_dev);
  501. if (!error && drv && drv->pm && drv->pm->resume)
  502. error = drv->pm->resume(dev);
  503. return error;
  504. }
  505. #else /* !CONFIG_SUSPEND */
  506. #define pci_pm_suspend NULL
  507. #define pci_pm_suspend_noirq NULL
  508. #define pci_pm_resume NULL
  509. #define pci_pm_resume_noirq NULL
  510. #endif /* !CONFIG_SUSPEND */
  511. #ifdef CONFIG_HIBERNATION
  512. static int pci_pm_freeze(struct device *dev)
  513. {
  514. struct pci_dev *pci_dev = to_pci_dev(dev);
  515. struct device_driver *drv = dev->driver;
  516. int error = 0;
  517. if (pci_has_legacy_pm_support(pci_dev))
  518. return pci_legacy_suspend(dev, PMSG_FREEZE);
  519. if (drv && drv->pm && drv->pm->freeze) {
  520. error = drv->pm->freeze(dev);
  521. suspend_report_result(drv->pm->freeze, error);
  522. }
  523. if (!error)
  524. pci_pm_default_suspend_generic(pci_dev);
  525. return error;
  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 device_driver *drv = dev->driver;
  558. int error = 0;
  559. if (pci_has_legacy_pm_support(pci_dev))
  560. return pci_legacy_resume(dev);
  561. pci_pm_reenable_device(pci_dev);
  562. if (drv && drv->pm && drv->pm->thaw)
  563. error = drv->pm->thaw(dev);
  564. return error;
  565. }
  566. static int pci_pm_poweroff(struct device *dev)
  567. {
  568. struct pci_dev *pci_dev = to_pci_dev(dev);
  569. struct device_driver *drv = dev->driver;
  570. int error = 0;
  571. if (pci_has_legacy_pm_support(pci_dev))
  572. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  573. if (drv && drv->pm && drv->pm->poweroff) {
  574. error = drv->pm->poweroff(dev);
  575. suspend_report_result(drv->pm->poweroff, error);
  576. }
  577. if (!error)
  578. pci_pm_default_suspend(pci_dev);
  579. return error;
  580. }
  581. static int pci_pm_poweroff_noirq(struct device *dev)
  582. {
  583. struct device_driver *drv = dev->driver;
  584. int error = 0;
  585. if (pci_has_legacy_pm_support(to_pci_dev(dev)))
  586. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  587. if (drv && drv->pm && drv->pm->poweroff_noirq) {
  588. error = drv->pm->poweroff_noirq(dev);
  589. suspend_report_result(drv->pm->poweroff_noirq, error);
  590. }
  591. return error;
  592. }
  593. static int pci_pm_restore_noirq(struct device *dev)
  594. {
  595. struct pci_dev *pci_dev = to_pci_dev(dev);
  596. struct device_driver *drv = dev->driver;
  597. int error = 0;
  598. if (pci_has_legacy_pm_support(pci_dev))
  599. return pci_legacy_resume_early(dev);
  600. pci_pm_default_resume_noirq(pci_dev);
  601. if (drv && drv->pm && drv->pm->restore_noirq)
  602. error = drv->pm->restore_noirq(dev);
  603. return error;
  604. }
  605. static int pci_pm_restore(struct device *dev)
  606. {
  607. struct pci_dev *pci_dev = to_pci_dev(dev);
  608. struct device_driver *drv = dev->driver;
  609. int error = 0;
  610. if (pci_has_legacy_pm_support(pci_dev))
  611. return pci_legacy_resume(dev);
  612. error = pci_pm_default_resume(pci_dev);
  613. if (!error && drv && drv->pm && drv->pm->restore)
  614. error = drv->pm->restore(dev);
  615. return error;
  616. }
  617. #else /* !CONFIG_HIBERNATION */
  618. #define pci_pm_freeze NULL
  619. #define pci_pm_freeze_noirq NULL
  620. #define pci_pm_thaw NULL
  621. #define pci_pm_thaw_noirq NULL
  622. #define pci_pm_poweroff NULL
  623. #define pci_pm_poweroff_noirq NULL
  624. #define pci_pm_restore NULL
  625. #define pci_pm_restore_noirq NULL
  626. #endif /* !CONFIG_HIBERNATION */
  627. struct dev_pm_ops pci_dev_pm_ops = {
  628. .prepare = pci_pm_prepare,
  629. .complete = pci_pm_complete,
  630. .suspend = pci_pm_suspend,
  631. .resume = pci_pm_resume,
  632. .freeze = pci_pm_freeze,
  633. .thaw = pci_pm_thaw,
  634. .poweroff = pci_pm_poweroff,
  635. .restore = pci_pm_restore,
  636. .suspend_noirq = pci_pm_suspend_noirq,
  637. .resume_noirq = pci_pm_resume_noirq,
  638. .freeze_noirq = pci_pm_freeze_noirq,
  639. .thaw_noirq = pci_pm_thaw_noirq,
  640. .poweroff_noirq = pci_pm_poweroff_noirq,
  641. .restore_noirq = pci_pm_restore_noirq,
  642. };
  643. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  644. #else /* !CONFIG_PM_SLEEP */
  645. #define PCI_PM_OPS_PTR NULL
  646. #endif /* !CONFIG_PM_SLEEP */
  647. /**
  648. * __pci_register_driver - register a new pci driver
  649. * @drv: the driver structure to register
  650. * @owner: owner module of drv
  651. * @mod_name: module name string
  652. *
  653. * Adds the driver structure to the list of registered drivers.
  654. * Returns a negative value on error, otherwise 0.
  655. * If no error occurred, the driver remains registered even if
  656. * no device was claimed during registration.
  657. */
  658. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  659. const char *mod_name)
  660. {
  661. int error;
  662. /* initialize common driver fields */
  663. drv->driver.name = drv->name;
  664. drv->driver.bus = &pci_bus_type;
  665. drv->driver.owner = owner;
  666. drv->driver.mod_name = mod_name;
  667. spin_lock_init(&drv->dynids.lock);
  668. INIT_LIST_HEAD(&drv->dynids.list);
  669. /* register with core */
  670. error = driver_register(&drv->driver);
  671. if (error)
  672. return error;
  673. error = pci_create_newid_file(drv);
  674. if (error)
  675. driver_unregister(&drv->driver);
  676. return error;
  677. }
  678. /**
  679. * pci_unregister_driver - unregister a pci driver
  680. * @drv: the driver structure to unregister
  681. *
  682. * Deletes the driver structure from the list of registered PCI drivers,
  683. * gives it a chance to clean up by calling its remove() function for
  684. * each device it was responsible for, and marks those devices as
  685. * driverless.
  686. */
  687. void
  688. pci_unregister_driver(struct pci_driver *drv)
  689. {
  690. pci_remove_newid_file(drv);
  691. driver_unregister(&drv->driver);
  692. pci_free_dynids(drv);
  693. }
  694. static struct pci_driver pci_compat_driver = {
  695. .name = "compat"
  696. };
  697. /**
  698. * pci_dev_driver - get the pci_driver of a device
  699. * @dev: the device to query
  700. *
  701. * Returns the appropriate pci_driver structure or %NULL if there is no
  702. * registered driver for the device.
  703. */
  704. struct pci_driver *
  705. pci_dev_driver(const struct pci_dev *dev)
  706. {
  707. if (dev->driver)
  708. return dev->driver;
  709. else {
  710. int i;
  711. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  712. if (dev->resource[i].flags & IORESOURCE_BUSY)
  713. return &pci_compat_driver;
  714. }
  715. return NULL;
  716. }
  717. /**
  718. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  719. * @dev: the PCI device structure to match against
  720. * @drv: the device driver to search for matching PCI device id structures
  721. *
  722. * Used by a driver to check whether a PCI device present in the
  723. * system is in its list of supported devices. Returns the matching
  724. * pci_device_id structure or %NULL if there is no match.
  725. */
  726. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  727. {
  728. struct pci_dev *pci_dev = to_pci_dev(dev);
  729. struct pci_driver *pci_drv = to_pci_driver(drv);
  730. const struct pci_device_id *found_id;
  731. found_id = pci_match_device(pci_drv, pci_dev);
  732. if (found_id)
  733. return 1;
  734. return 0;
  735. }
  736. /**
  737. * pci_dev_get - increments the reference count of the pci device structure
  738. * @dev: the device being referenced
  739. *
  740. * Each live reference to a device should be refcounted.
  741. *
  742. * Drivers for PCI devices should normally record such references in
  743. * their probe() methods, when they bind to a device, and release
  744. * them by calling pci_dev_put(), in their disconnect() methods.
  745. *
  746. * A pointer to the device with the incremented reference counter is returned.
  747. */
  748. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  749. {
  750. if (dev)
  751. get_device(&dev->dev);
  752. return dev;
  753. }
  754. /**
  755. * pci_dev_put - release a use of the pci device structure
  756. * @dev: device that's been disconnected
  757. *
  758. * Must be called when a user of a device is finished with it. When the last
  759. * user of the device calls this function, the memory of the device is freed.
  760. */
  761. void pci_dev_put(struct pci_dev *dev)
  762. {
  763. if (dev)
  764. put_device(&dev->dev);
  765. }
  766. #ifndef CONFIG_HOTPLUG
  767. int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  768. {
  769. return -ENODEV;
  770. }
  771. #endif
  772. struct bus_type pci_bus_type = {
  773. .name = "pci",
  774. .match = pci_bus_match,
  775. .uevent = pci_uevent,
  776. .probe = pci_device_probe,
  777. .remove = pci_device_remove,
  778. .shutdown = pci_device_shutdown,
  779. .dev_attrs = pci_dev_attrs,
  780. .pm = PCI_PM_OPS_PTR,
  781. };
  782. static int __init pci_driver_init(void)
  783. {
  784. return bus_register(&pci_bus_type);
  785. }
  786. postcore_initcall(pci_driver_init);
  787. EXPORT_SYMBOL(pci_match_id);
  788. EXPORT_SYMBOL(__pci_register_driver);
  789. EXPORT_SYMBOL(pci_unregister_driver);
  790. EXPORT_SYMBOL(pci_dev_driver);
  791. EXPORT_SYMBOL(pci_bus_type);
  792. EXPORT_SYMBOL(pci_dev_get);
  793. EXPORT_SYMBOL(pci_dev_put);