pci-driver.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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 "pci.h"
  19. /*
  20. * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
  21. */
  22. struct pci_dynid {
  23. struct list_head node;
  24. struct pci_device_id id;
  25. };
  26. #ifdef CONFIG_HOTPLUG
  27. /**
  28. * store_new_id - add a new PCI device ID to this driver and re-probe devices
  29. * @driver: target device driver
  30. * @buf: buffer for scanning device ID data
  31. * @count: input size
  32. *
  33. * Adds a new dynamic pci device ID to this driver,
  34. * and causes the driver to probe for all devices again.
  35. */
  36. static ssize_t
  37. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  38. {
  39. struct pci_dynid *dynid;
  40. struct pci_driver *pdrv = to_pci_driver(driver);
  41. const struct pci_device_id *ids = pdrv->id_table;
  42. __u32 vendor, device, subvendor=PCI_ANY_ID,
  43. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  44. unsigned long driver_data=0;
  45. int fields=0;
  46. int retval;
  47. fields = sscanf(buf, "%x %x %x %x %x %x %lx",
  48. &vendor, &device, &subvendor, &subdevice,
  49. &class, &class_mask, &driver_data);
  50. if (fields < 2)
  51. return -EINVAL;
  52. /* Only accept driver_data values that match an existing id_table
  53. entry */
  54. retval = -EINVAL;
  55. while (ids->vendor || ids->subvendor || ids->class_mask) {
  56. if (driver_data == ids->driver_data) {
  57. retval = 0;
  58. break;
  59. }
  60. ids++;
  61. }
  62. if (retval) /* No match */
  63. return retval;
  64. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  65. if (!dynid)
  66. return -ENOMEM;
  67. dynid->id.vendor = vendor;
  68. dynid->id.device = device;
  69. dynid->id.subvendor = subvendor;
  70. dynid->id.subdevice = subdevice;
  71. dynid->id.class = class;
  72. dynid->id.class_mask = class_mask;
  73. dynid->id.driver_data = driver_data;
  74. spin_lock(&pdrv->dynids.lock);
  75. list_add_tail(&dynid->node, &pdrv->dynids.list);
  76. spin_unlock(&pdrv->dynids.lock);
  77. if (get_driver(&pdrv->driver)) {
  78. retval = driver_attach(&pdrv->driver);
  79. put_driver(&pdrv->driver);
  80. }
  81. if (retval)
  82. return retval;
  83. return count;
  84. }
  85. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  86. static void
  87. pci_free_dynids(struct pci_driver *drv)
  88. {
  89. struct pci_dynid *dynid, *n;
  90. spin_lock(&drv->dynids.lock);
  91. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  92. list_del(&dynid->node);
  93. kfree(dynid);
  94. }
  95. spin_unlock(&drv->dynids.lock);
  96. }
  97. static int
  98. pci_create_newid_file(struct pci_driver *drv)
  99. {
  100. int error = 0;
  101. if (drv->probe != NULL)
  102. error = driver_create_file(&drv->driver, &driver_attr_new_id);
  103. return error;
  104. }
  105. static void pci_remove_newid_file(struct pci_driver *drv)
  106. {
  107. driver_remove_file(&drv->driver, &driver_attr_new_id);
  108. }
  109. #else /* !CONFIG_HOTPLUG */
  110. static inline void pci_free_dynids(struct pci_driver *drv) {}
  111. static inline int pci_create_newid_file(struct pci_driver *drv)
  112. {
  113. return 0;
  114. }
  115. static inline void pci_remove_newid_file(struct pci_driver *drv) {}
  116. #endif
  117. /**
  118. * pci_match_id - See if a pci device matches a given pci_id table
  119. * @ids: array of PCI device id structures to search in
  120. * @dev: the PCI device structure to match against.
  121. *
  122. * Used by a driver to check whether a PCI device present in the
  123. * system is in its list of supported devices. Returns the matching
  124. * pci_device_id structure or %NULL if there is no match.
  125. *
  126. * Deprecated, don't use this as it will not catch any dynamic ids
  127. * that a driver might want to check for.
  128. */
  129. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  130. struct pci_dev *dev)
  131. {
  132. if (ids) {
  133. while (ids->vendor || ids->subvendor || ids->class_mask) {
  134. if (pci_match_one_device(ids, dev))
  135. return ids;
  136. ids++;
  137. }
  138. }
  139. return NULL;
  140. }
  141. /**
  142. * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  143. * @drv: the PCI driver to match against
  144. * @dev: the PCI device structure to match against
  145. *
  146. * Used by a driver to check whether a PCI device present in the
  147. * system is in its list of supported devices. Returns the matching
  148. * pci_device_id structure or %NULL if there is no match.
  149. */
  150. static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  151. struct pci_dev *dev)
  152. {
  153. struct pci_dynid *dynid;
  154. /* Look at the dynamic ids first, before the static ones */
  155. spin_lock(&drv->dynids.lock);
  156. list_for_each_entry(dynid, &drv->dynids.list, node) {
  157. if (pci_match_one_device(&dynid->id, dev)) {
  158. spin_unlock(&drv->dynids.lock);
  159. return &dynid->id;
  160. }
  161. }
  162. spin_unlock(&drv->dynids.lock);
  163. return pci_match_id(drv->id_table, dev);
  164. }
  165. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  166. const struct pci_device_id *id)
  167. {
  168. int error;
  169. #ifdef CONFIG_NUMA
  170. /* Execute driver initialization on node where the
  171. device's bus is attached to. This way the driver likely
  172. allocates its local memory on the right node without
  173. any need to change it. */
  174. struct mempolicy *oldpol;
  175. cpumask_t oldmask = current->cpus_allowed;
  176. int node = dev_to_node(&dev->dev);
  177. if (node >= 0) {
  178. node_to_cpumask_ptr(nodecpumask, node);
  179. set_cpus_allowed_ptr(current, nodecpumask);
  180. }
  181. /* And set default memory allocation policy */
  182. oldpol = current->mempolicy;
  183. current->mempolicy = NULL; /* fall back to system default policy */
  184. #endif
  185. error = drv->probe(dev, id);
  186. #ifdef CONFIG_NUMA
  187. set_cpus_allowed_ptr(current, &oldmask);
  188. current->mempolicy = oldpol;
  189. #endif
  190. return error;
  191. }
  192. /**
  193. * __pci_device_probe()
  194. * @drv: driver to call to check if it wants the PCI device
  195. * @pci_dev: PCI device being probed
  196. *
  197. * returns 0 on success, else error.
  198. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  199. */
  200. static int
  201. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  202. {
  203. const struct pci_device_id *id;
  204. int error = 0;
  205. if (!pci_dev->driver && drv->probe) {
  206. error = -ENODEV;
  207. id = pci_match_device(drv, pci_dev);
  208. if (id)
  209. error = pci_call_probe(drv, pci_dev, id);
  210. if (error >= 0) {
  211. pci_dev->driver = drv;
  212. error = 0;
  213. }
  214. }
  215. return error;
  216. }
  217. static int pci_device_probe(struct device * dev)
  218. {
  219. int error = 0;
  220. struct pci_driver *drv;
  221. struct pci_dev *pci_dev;
  222. drv = to_pci_driver(dev->driver);
  223. pci_dev = to_pci_dev(dev);
  224. pci_dev_get(pci_dev);
  225. error = __pci_device_probe(drv, pci_dev);
  226. if (error)
  227. pci_dev_put(pci_dev);
  228. return error;
  229. }
  230. static int pci_device_remove(struct device * dev)
  231. {
  232. struct pci_dev * pci_dev = to_pci_dev(dev);
  233. struct pci_driver * drv = pci_dev->driver;
  234. if (drv) {
  235. if (drv->remove)
  236. drv->remove(pci_dev);
  237. pci_dev->driver = NULL;
  238. }
  239. /*
  240. * If the device is still on, set the power state as "unknown",
  241. * since it might change by the next time we load the driver.
  242. */
  243. if (pci_dev->current_state == PCI_D0)
  244. pci_dev->current_state = PCI_UNKNOWN;
  245. /*
  246. * We would love to complain here if pci_dev->is_enabled is set, that
  247. * the driver should have called pci_disable_device(), but the
  248. * unfortunate fact is there are too many odd BIOS and bridge setups
  249. * that don't like drivers doing that all of the time.
  250. * Oh well, we can dream of sane hardware when we sleep, no matter how
  251. * horrible the crap we have to deal with is when we are awake...
  252. */
  253. pci_dev_put(pci_dev);
  254. return 0;
  255. }
  256. static void pci_device_shutdown(struct device *dev)
  257. {
  258. struct pci_dev *pci_dev = to_pci_dev(dev);
  259. struct pci_driver *drv = pci_dev->driver;
  260. if (drv && drv->shutdown)
  261. drv->shutdown(pci_dev);
  262. pci_msi_shutdown(pci_dev);
  263. pci_msix_shutdown(pci_dev);
  264. }
  265. #ifdef CONFIG_PM_SLEEP
  266. /*
  267. * Default "suspend" method for devices that have no driver provided suspend,
  268. * or not even a driver at all.
  269. */
  270. static void pci_default_pm_suspend(struct pci_dev *pci_dev)
  271. {
  272. pci_save_state(pci_dev);
  273. /*
  274. * mark its power state as "unknown", since we don't know if
  275. * e.g. the BIOS will change its device state when we suspend.
  276. */
  277. if (pci_dev->current_state == PCI_D0)
  278. pci_dev->current_state = PCI_UNKNOWN;
  279. }
  280. /*
  281. * Default "resume" method for devices that have no driver provided resume,
  282. * or not even a driver at all.
  283. */
  284. static int pci_default_pm_resume(struct pci_dev *pci_dev)
  285. {
  286. int retval = 0;
  287. /* restore the PCI config space */
  288. pci_restore_state(pci_dev);
  289. /* if the device was enabled before suspend, reenable */
  290. retval = pci_reenable_device(pci_dev);
  291. /*
  292. * if the device was busmaster before the suspend, make it busmaster
  293. * again
  294. */
  295. if (pci_dev->is_busmaster)
  296. pci_set_master(pci_dev);
  297. return retval;
  298. }
  299. static int pci_legacy_suspend(struct device *dev, pm_message_t state)
  300. {
  301. struct pci_dev * pci_dev = to_pci_dev(dev);
  302. struct pci_driver * drv = pci_dev->driver;
  303. int i = 0;
  304. if (drv && drv->suspend) {
  305. i = drv->suspend(pci_dev, state);
  306. suspend_report_result(drv->suspend, i);
  307. } else {
  308. pci_default_pm_suspend(pci_dev);
  309. }
  310. return i;
  311. }
  312. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  313. {
  314. struct pci_dev * pci_dev = to_pci_dev(dev);
  315. struct pci_driver * drv = pci_dev->driver;
  316. int i = 0;
  317. if (drv && drv->suspend_late) {
  318. i = drv->suspend_late(pci_dev, state);
  319. suspend_report_result(drv->suspend_late, i);
  320. }
  321. return i;
  322. }
  323. static int pci_legacy_resume(struct device *dev)
  324. {
  325. int error;
  326. struct pci_dev * pci_dev = to_pci_dev(dev);
  327. struct pci_driver * drv = pci_dev->driver;
  328. if (drv && drv->resume)
  329. error = drv->resume(pci_dev);
  330. else
  331. error = pci_default_pm_resume(pci_dev);
  332. return error;
  333. }
  334. static int pci_legacy_resume_early(struct device *dev)
  335. {
  336. int error = 0;
  337. struct pci_dev * pci_dev = to_pci_dev(dev);
  338. struct pci_driver * drv = pci_dev->driver;
  339. if (drv && drv->resume_early)
  340. error = drv->resume_early(pci_dev);
  341. return error;
  342. }
  343. static int pci_pm_prepare(struct device *dev)
  344. {
  345. struct device_driver *drv = dev->driver;
  346. int error = 0;
  347. if (drv && drv->pm && drv->pm->prepare)
  348. error = drv->pm->prepare(dev);
  349. return error;
  350. }
  351. static void pci_pm_complete(struct device *dev)
  352. {
  353. struct device_driver *drv = dev->driver;
  354. if (drv && drv->pm && drv->pm->complete)
  355. drv->pm->complete(dev);
  356. }
  357. #ifdef CONFIG_SUSPEND
  358. static int pci_pm_suspend(struct device *dev)
  359. {
  360. struct pci_dev *pci_dev = to_pci_dev(dev);
  361. struct device_driver *drv = dev->driver;
  362. int error = 0;
  363. if (drv && drv->pm) {
  364. if (drv->pm->suspend) {
  365. error = drv->pm->suspend(dev);
  366. suspend_report_result(drv->pm->suspend, error);
  367. } else {
  368. pci_default_pm_suspend(pci_dev);
  369. }
  370. } else {
  371. error = pci_legacy_suspend(dev, PMSG_SUSPEND);
  372. }
  373. pci_fixup_device(pci_fixup_suspend, pci_dev);
  374. return error;
  375. }
  376. static int pci_pm_suspend_noirq(struct device *dev)
  377. {
  378. struct pci_dev *pci_dev = to_pci_dev(dev);
  379. struct pci_driver *drv = pci_dev->driver;
  380. int error = 0;
  381. if (drv && drv->pm) {
  382. if (drv->pm->suspend_noirq) {
  383. error = drv->pm->suspend_noirq(dev);
  384. suspend_report_result(drv->pm->suspend_noirq, error);
  385. }
  386. } else {
  387. error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  388. }
  389. return error;
  390. }
  391. static int pci_pm_resume(struct device *dev)
  392. {
  393. struct pci_dev *pci_dev = to_pci_dev(dev);
  394. struct device_driver *drv = dev->driver;
  395. int error;
  396. pci_fixup_device(pci_fixup_resume, pci_dev);
  397. if (drv && drv->pm) {
  398. error = drv->pm->resume ? drv->pm->resume(dev) :
  399. pci_default_pm_resume(pci_dev);
  400. } else {
  401. error = pci_legacy_resume(dev);
  402. }
  403. return error;
  404. }
  405. static int pci_pm_resume_noirq(struct device *dev)
  406. {
  407. struct pci_dev *pci_dev = to_pci_dev(dev);
  408. struct pci_driver *drv = pci_dev->driver;
  409. int error = 0;
  410. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  411. if (drv && drv->pm) {
  412. if (drv->pm->resume_noirq)
  413. error = drv->pm->resume_noirq(dev);
  414. } else {
  415. error = pci_legacy_resume_early(dev);
  416. }
  417. return error;
  418. }
  419. #else /* !CONFIG_SUSPEND */
  420. #define pci_pm_suspend NULL
  421. #define pci_pm_suspend_noirq NULL
  422. #define pci_pm_resume NULL
  423. #define pci_pm_resume_noirq NULL
  424. #endif /* !CONFIG_SUSPEND */
  425. #ifdef CONFIG_HIBERNATION
  426. static int pci_pm_freeze(struct device *dev)
  427. {
  428. struct pci_dev *pci_dev = to_pci_dev(dev);
  429. struct device_driver *drv = dev->driver;
  430. int error = 0;
  431. if (drv && drv->pm) {
  432. if (drv->pm->freeze) {
  433. error = drv->pm->freeze(dev);
  434. suspend_report_result(drv->pm->freeze, error);
  435. } else {
  436. pci_default_pm_suspend(pci_dev);
  437. }
  438. } else {
  439. error = pci_legacy_suspend(dev, PMSG_FREEZE);
  440. pci_fixup_device(pci_fixup_suspend, pci_dev);
  441. }
  442. return error;
  443. }
  444. static int pci_pm_freeze_noirq(struct device *dev)
  445. {
  446. struct pci_dev *pci_dev = to_pci_dev(dev);
  447. struct pci_driver *drv = pci_dev->driver;
  448. int error = 0;
  449. if (drv && drv->pm) {
  450. if (drv->pm->freeze_noirq) {
  451. error = drv->pm->freeze_noirq(dev);
  452. suspend_report_result(drv->pm->freeze_noirq, error);
  453. }
  454. } else {
  455. error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
  456. }
  457. return error;
  458. }
  459. static int pci_pm_thaw(struct device *dev)
  460. {
  461. struct device_driver *drv = dev->driver;
  462. int error = 0;
  463. if (drv && drv->pm) {
  464. if (drv->pm->thaw)
  465. error = drv->pm->thaw(dev);
  466. } else {
  467. pci_fixup_device(pci_fixup_resume, to_pci_dev(dev));
  468. error = pci_legacy_resume(dev);
  469. }
  470. return error;
  471. }
  472. static int pci_pm_thaw_noirq(struct device *dev)
  473. {
  474. struct pci_dev *pci_dev = to_pci_dev(dev);
  475. struct pci_driver *drv = pci_dev->driver;
  476. int error = 0;
  477. if (drv && drv->pm) {
  478. if (drv->pm->thaw_noirq)
  479. error = drv->pm->thaw_noirq(dev);
  480. } else {
  481. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  482. error = pci_legacy_resume_early(dev);
  483. }
  484. return error;
  485. }
  486. static int pci_pm_poweroff(struct device *dev)
  487. {
  488. struct device_driver *drv = dev->driver;
  489. int error = 0;
  490. pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
  491. if (drv && drv->pm) {
  492. if (drv->pm->poweroff) {
  493. error = drv->pm->poweroff(dev);
  494. suspend_report_result(drv->pm->poweroff, error);
  495. }
  496. } else {
  497. error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
  498. }
  499. return error;
  500. }
  501. static int pci_pm_poweroff_noirq(struct device *dev)
  502. {
  503. struct pci_dev *pci_dev = to_pci_dev(dev);
  504. struct pci_driver *drv = pci_dev->driver;
  505. int error = 0;
  506. if (drv && drv->pm) {
  507. if (drv->pm->poweroff_noirq) {
  508. error = drv->pm->poweroff_noirq(dev);
  509. suspend_report_result(drv->pm->poweroff_noirq, error);
  510. }
  511. } else {
  512. error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  513. }
  514. return error;
  515. }
  516. static int pci_pm_restore(struct device *dev)
  517. {
  518. struct pci_dev *pci_dev = to_pci_dev(dev);
  519. struct device_driver *drv = dev->driver;
  520. int error;
  521. if (drv && drv->pm) {
  522. error = drv->pm->restore ? drv->pm->restore(dev) :
  523. pci_default_pm_resume(pci_dev);
  524. } else {
  525. error = pci_legacy_resume(dev);
  526. }
  527. pci_fixup_device(pci_fixup_resume, pci_dev);
  528. return error;
  529. }
  530. static int pci_pm_restore_noirq(struct device *dev)
  531. {
  532. struct pci_dev *pci_dev = to_pci_dev(dev);
  533. struct pci_driver *drv = pci_dev->driver;
  534. int error = 0;
  535. pci_fixup_device(pci_fixup_resume, pci_dev);
  536. if (drv && drv->pm) {
  537. if (drv->pm->restore_noirq)
  538. error = drv->pm->restore_noirq(dev);
  539. } else {
  540. error = pci_legacy_resume_early(dev);
  541. }
  542. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  543. return error;
  544. }
  545. #else /* !CONFIG_HIBERNATION */
  546. #define pci_pm_freeze NULL
  547. #define pci_pm_freeze_noirq NULL
  548. #define pci_pm_thaw NULL
  549. #define pci_pm_thaw_noirq NULL
  550. #define pci_pm_poweroff NULL
  551. #define pci_pm_poweroff_noirq NULL
  552. #define pci_pm_restore NULL
  553. #define pci_pm_restore_noirq NULL
  554. #endif /* !CONFIG_HIBERNATION */
  555. struct pm_ext_ops pci_pm_ops = {
  556. .base = {
  557. .prepare = pci_pm_prepare,
  558. .complete = pci_pm_complete,
  559. .suspend = pci_pm_suspend,
  560. .resume = pci_pm_resume,
  561. .freeze = pci_pm_freeze,
  562. .thaw = pci_pm_thaw,
  563. .poweroff = pci_pm_poweroff,
  564. .restore = pci_pm_restore,
  565. },
  566. .suspend_noirq = pci_pm_suspend_noirq,
  567. .resume_noirq = pci_pm_resume_noirq,
  568. .freeze_noirq = pci_pm_freeze_noirq,
  569. .thaw_noirq = pci_pm_thaw_noirq,
  570. .poweroff_noirq = pci_pm_poweroff_noirq,
  571. .restore_noirq = pci_pm_restore_noirq,
  572. };
  573. #define PCI_PM_OPS_PTR &pci_pm_ops
  574. #else /* !CONFIG_PM_SLEEP */
  575. #define PCI_PM_OPS_PTR NULL
  576. #endif /* !CONFIG_PM_SLEEP */
  577. /**
  578. * __pci_register_driver - register a new pci driver
  579. * @drv: the driver structure to register
  580. * @owner: owner module of drv
  581. * @mod_name: module name string
  582. *
  583. * Adds the driver structure to the list of registered drivers.
  584. * Returns a negative value on error, otherwise 0.
  585. * If no error occurred, the driver remains registered even if
  586. * no device was claimed during registration.
  587. */
  588. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  589. const char *mod_name)
  590. {
  591. int error;
  592. /* initialize common driver fields */
  593. drv->driver.name = drv->name;
  594. drv->driver.bus = &pci_bus_type;
  595. drv->driver.owner = owner;
  596. drv->driver.mod_name = mod_name;
  597. if (drv->pm)
  598. drv->driver.pm = &drv->pm->base;
  599. spin_lock_init(&drv->dynids.lock);
  600. INIT_LIST_HEAD(&drv->dynids.list);
  601. /* register with core */
  602. error = driver_register(&drv->driver);
  603. if (error)
  604. return error;
  605. error = pci_create_newid_file(drv);
  606. if (error)
  607. driver_unregister(&drv->driver);
  608. return error;
  609. }
  610. /**
  611. * pci_unregister_driver - unregister a pci driver
  612. * @drv: the driver structure to unregister
  613. *
  614. * Deletes the driver structure from the list of registered PCI drivers,
  615. * gives it a chance to clean up by calling its remove() function for
  616. * each device it was responsible for, and marks those devices as
  617. * driverless.
  618. */
  619. void
  620. pci_unregister_driver(struct pci_driver *drv)
  621. {
  622. pci_remove_newid_file(drv);
  623. driver_unregister(&drv->driver);
  624. pci_free_dynids(drv);
  625. }
  626. static struct pci_driver pci_compat_driver = {
  627. .name = "compat"
  628. };
  629. /**
  630. * pci_dev_driver - get the pci_driver of a device
  631. * @dev: the device to query
  632. *
  633. * Returns the appropriate pci_driver structure or %NULL if there is no
  634. * registered driver for the device.
  635. */
  636. struct pci_driver *
  637. pci_dev_driver(const struct pci_dev *dev)
  638. {
  639. if (dev->driver)
  640. return dev->driver;
  641. else {
  642. int i;
  643. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  644. if (dev->resource[i].flags & IORESOURCE_BUSY)
  645. return &pci_compat_driver;
  646. }
  647. return NULL;
  648. }
  649. /**
  650. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  651. * @dev: the PCI device structure to match against
  652. * @drv: the device driver to search for matching PCI device id structures
  653. *
  654. * Used by a driver to check whether a PCI device present in the
  655. * system is in its list of supported devices. Returns the matching
  656. * pci_device_id structure or %NULL if there is no match.
  657. */
  658. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  659. {
  660. struct pci_dev *pci_dev = to_pci_dev(dev);
  661. struct pci_driver *pci_drv = to_pci_driver(drv);
  662. const struct pci_device_id *found_id;
  663. found_id = pci_match_device(pci_drv, pci_dev);
  664. if (found_id)
  665. return 1;
  666. return 0;
  667. }
  668. /**
  669. * pci_dev_get - increments the reference count of the pci device structure
  670. * @dev: the device being referenced
  671. *
  672. * Each live reference to a device should be refcounted.
  673. *
  674. * Drivers for PCI devices should normally record such references in
  675. * their probe() methods, when they bind to a device, and release
  676. * them by calling pci_dev_put(), in their disconnect() methods.
  677. *
  678. * A pointer to the device with the incremented reference counter is returned.
  679. */
  680. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  681. {
  682. if (dev)
  683. get_device(&dev->dev);
  684. return dev;
  685. }
  686. /**
  687. * pci_dev_put - release a use of the pci device structure
  688. * @dev: device that's been disconnected
  689. *
  690. * Must be called when a user of a device is finished with it. When the last
  691. * user of the device calls this function, the memory of the device is freed.
  692. */
  693. void pci_dev_put(struct pci_dev *dev)
  694. {
  695. if (dev)
  696. put_device(&dev->dev);
  697. }
  698. #ifndef CONFIG_HOTPLUG
  699. int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  700. {
  701. return -ENODEV;
  702. }
  703. #endif
  704. struct bus_type pci_bus_type = {
  705. .name = "pci",
  706. .match = pci_bus_match,
  707. .uevent = pci_uevent,
  708. .probe = pci_device_probe,
  709. .remove = pci_device_remove,
  710. .shutdown = pci_device_shutdown,
  711. .dev_attrs = pci_dev_attrs,
  712. .pm = PCI_PM_OPS_PTR,
  713. };
  714. static int __init pci_driver_init(void)
  715. {
  716. return bus_register(&pci_bus_type);
  717. }
  718. postcore_initcall(pci_driver_init);
  719. EXPORT_SYMBOL(pci_match_id);
  720. EXPORT_SYMBOL(__pci_register_driver);
  721. EXPORT_SYMBOL(pci_unregister_driver);
  722. EXPORT_SYMBOL(pci_dev_driver);
  723. EXPORT_SYMBOL(pci_bus_type);
  724. EXPORT_SYMBOL(pci_dev_get);
  725. EXPORT_SYMBOL(pci_dev_put);