pci-driver.c 26 KB

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