pci-driver.c 20 KB

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