pci-driver.c 20 KB

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