pci-driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * drivers/pci/pci-driver.c
  3. *
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/device.h>
  9. #include <linux/pci-dynids.h>
  10. #include "pci.h"
  11. /*
  12. * Registration of PCI drivers and handling of hot-pluggable devices.
  13. */
  14. /*
  15. * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
  16. */
  17. #ifdef CONFIG_HOTPLUG
  18. /**
  19. * pci_device_probe_dynamic()
  20. *
  21. * Walk the dynamic ID list looking for a match.
  22. * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
  23. */
  24. static int
  25. pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
  26. {
  27. int error = -ENODEV;
  28. struct list_head *pos;
  29. struct dynid *dynid;
  30. spin_lock(&drv->dynids.lock);
  31. list_for_each(pos, &drv->dynids.list) {
  32. dynid = list_entry(pos, struct dynid, node);
  33. if (pci_match_one_device(&dynid->id, pci_dev)) {
  34. spin_unlock(&drv->dynids.lock);
  35. error = drv->probe(pci_dev, &dynid->id);
  36. if (error >= 0) {
  37. pci_dev->driver = drv;
  38. return 0;
  39. }
  40. return error;
  41. }
  42. }
  43. spin_unlock(&drv->dynids.lock);
  44. return error;
  45. }
  46. /**
  47. * store_new_id
  48. *
  49. * Adds a new dynamic pci device ID to this driver,
  50. * and causes the driver to probe for all devices again.
  51. */
  52. static inline ssize_t
  53. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  54. {
  55. struct dynid *dynid;
  56. struct bus_type * bus;
  57. struct pci_driver *pdrv = to_pci_driver(driver);
  58. __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
  59. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  60. unsigned long driver_data=0;
  61. int fields=0;
  62. fields = sscanf(buf, "%x %x %x %x %x %x %lux",
  63. &vendor, &device, &subvendor, &subdevice,
  64. &class, &class_mask, &driver_data);
  65. if (fields < 0)
  66. return -EINVAL;
  67. dynid = kmalloc(sizeof(*dynid), GFP_KERNEL);
  68. if (!dynid)
  69. return -ENOMEM;
  70. memset(dynid, 0, sizeof(*dynid));
  71. INIT_LIST_HEAD(&dynid->node);
  72. dynid->id.vendor = vendor;
  73. dynid->id.device = device;
  74. dynid->id.subvendor = subvendor;
  75. dynid->id.subdevice = subdevice;
  76. dynid->id.class = class;
  77. dynid->id.class_mask = class_mask;
  78. dynid->id.driver_data = pdrv->dynids.use_driver_data ?
  79. driver_data : 0UL;
  80. spin_lock(&pdrv->dynids.lock);
  81. list_add_tail(&pdrv->dynids.list, &dynid->node);
  82. spin_unlock(&pdrv->dynids.lock);
  83. bus = get_bus(pdrv->driver.bus);
  84. if (bus) {
  85. if (get_driver(&pdrv->driver)) {
  86. down_write(&bus->subsys.rwsem);
  87. driver_attach(&pdrv->driver);
  88. up_write(&bus->subsys.rwsem);
  89. put_driver(&pdrv->driver);
  90. }
  91. put_bus(bus);
  92. }
  93. return count;
  94. }
  95. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  96. static inline void
  97. pci_init_dynids(struct pci_dynids *dynids)
  98. {
  99. spin_lock_init(&dynids->lock);
  100. INIT_LIST_HEAD(&dynids->list);
  101. }
  102. static void
  103. pci_free_dynids(struct pci_driver *drv)
  104. {
  105. struct list_head *pos, *n;
  106. struct dynid *dynid;
  107. spin_lock(&drv->dynids.lock);
  108. list_for_each_safe(pos, n, &drv->dynids.list) {
  109. dynid = list_entry(pos, struct dynid, node);
  110. list_del(&dynid->node);
  111. kfree(dynid);
  112. }
  113. spin_unlock(&drv->dynids.lock);
  114. }
  115. static int
  116. pci_create_newid_file(struct pci_driver *drv)
  117. {
  118. int error = 0;
  119. if (drv->probe != NULL)
  120. error = sysfs_create_file(&drv->driver.kobj,
  121. &driver_attr_new_id.attr);
  122. return error;
  123. }
  124. static int
  125. pci_bus_match_dynids(const struct pci_dev *pci_dev, struct pci_driver *pci_drv)
  126. {
  127. struct list_head *pos;
  128. struct dynid *dynid;
  129. spin_lock(&pci_drv->dynids.lock);
  130. list_for_each(pos, &pci_drv->dynids.list) {
  131. dynid = list_entry(pos, struct dynid, node);
  132. if (pci_match_one_device(&dynid->id, pci_dev)) {
  133. spin_unlock(&pci_drv->dynids.lock);
  134. return 1;
  135. }
  136. }
  137. spin_unlock(&pci_drv->dynids.lock);
  138. return 0;
  139. }
  140. #else /* !CONFIG_HOTPLUG */
  141. static inline int pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
  142. {
  143. return -ENODEV;
  144. }
  145. static inline void pci_init_dynids(struct pci_dynids *dynids) {}
  146. static inline void pci_free_dynids(struct pci_driver *drv) {}
  147. static inline int pci_create_newid_file(struct pci_driver *drv)
  148. {
  149. return 0;
  150. }
  151. static inline int pci_bus_match_dynids(const struct pci_dev *pci_dev, struct pci_driver *pci_drv)
  152. {
  153. return 0;
  154. }
  155. #endif
  156. /**
  157. * pci_match_device - Tell if a PCI device structure has a matching
  158. * PCI device id structure
  159. * @ids: array of PCI device id structures to search in
  160. * @dev: the PCI device structure to match against
  161. *
  162. * Used by a driver to check whether a PCI device present in the
  163. * system is in its list of supported devices.Returns the matching
  164. * pci_device_id structure or %NULL if there is no match.
  165. */
  166. const struct pci_device_id *
  167. pci_match_device(const struct pci_device_id *ids, const struct pci_dev *dev)
  168. {
  169. while (ids->vendor || ids->subvendor || ids->class_mask) {
  170. if (pci_match_one_device(ids, dev))
  171. return ids;
  172. ids++;
  173. }
  174. return NULL;
  175. }
  176. /**
  177. * pci_device_probe_static()
  178. *
  179. * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
  180. */
  181. static int
  182. pci_device_probe_static(struct pci_driver *drv, struct pci_dev *pci_dev)
  183. {
  184. int error = -ENODEV;
  185. const struct pci_device_id *id;
  186. if (!drv->id_table)
  187. return error;
  188. id = pci_match_device(drv->id_table, pci_dev);
  189. if (id)
  190. error = drv->probe(pci_dev, id);
  191. if (error >= 0) {
  192. pci_dev->driver = drv;
  193. error = 0;
  194. }
  195. return error;
  196. }
  197. /**
  198. * __pci_device_probe()
  199. *
  200. * returns 0 on success, else error.
  201. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  202. */
  203. static int
  204. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  205. {
  206. int error = 0;
  207. if (!pci_dev->driver && drv->probe) {
  208. error = pci_device_probe_static(drv, pci_dev);
  209. if (error == -ENODEV)
  210. error = pci_device_probe_dynamic(drv, pci_dev);
  211. }
  212. return error;
  213. }
  214. static int pci_device_probe(struct device * dev)
  215. {
  216. int error = 0;
  217. struct pci_driver *drv;
  218. struct pci_dev *pci_dev;
  219. drv = to_pci_driver(dev->driver);
  220. pci_dev = to_pci_dev(dev);
  221. pci_dev_get(pci_dev);
  222. error = __pci_device_probe(drv, pci_dev);
  223. if (error)
  224. pci_dev_put(pci_dev);
  225. return error;
  226. }
  227. static int pci_device_remove(struct device * dev)
  228. {
  229. struct pci_dev * pci_dev = to_pci_dev(dev);
  230. struct pci_driver * drv = pci_dev->driver;
  231. if (drv) {
  232. if (drv->remove)
  233. drv->remove(pci_dev);
  234. pci_dev->driver = NULL;
  235. }
  236. /*
  237. * We would love to complain here if pci_dev->is_enabled is set, that
  238. * the driver should have called pci_disable_device(), but the
  239. * unfortunate fact is there are too many odd BIOS and bridge setups
  240. * that don't like drivers doing that all of the time.
  241. * Oh well, we can dream of sane hardware when we sleep, no matter how
  242. * horrible the crap we have to deal with is when we are awake...
  243. */
  244. pci_dev_put(pci_dev);
  245. return 0;
  246. }
  247. static int pci_device_suspend(struct device * dev, pm_message_t state)
  248. {
  249. struct pci_dev * pci_dev = to_pci_dev(dev);
  250. struct pci_driver * drv = pci_dev->driver;
  251. int i = 0;
  252. if (drv && drv->suspend)
  253. i = drv->suspend(pci_dev, state);
  254. else
  255. pci_save_state(pci_dev);
  256. return i;
  257. }
  258. /*
  259. * Default resume method for devices that have no driver provided resume,
  260. * or not even a driver at all.
  261. */
  262. static void pci_default_resume(struct pci_dev *pci_dev)
  263. {
  264. /* restore the PCI config space */
  265. pci_restore_state(pci_dev);
  266. /* if the device was enabled before suspend, reenable */
  267. if (pci_dev->is_enabled)
  268. pci_enable_device(pci_dev);
  269. /* if the device was busmaster before the suspend, make it busmaster again */
  270. if (pci_dev->is_busmaster)
  271. pci_set_master(pci_dev);
  272. }
  273. static int pci_device_resume(struct device * dev)
  274. {
  275. struct pci_dev * pci_dev = to_pci_dev(dev);
  276. struct pci_driver * drv = pci_dev->driver;
  277. if (drv && drv->resume)
  278. drv->resume(pci_dev);
  279. else
  280. pci_default_resume(pci_dev);
  281. return 0;
  282. }
  283. static void pci_device_shutdown(struct device *dev)
  284. {
  285. struct pci_dev *pci_dev = to_pci_dev(dev);
  286. struct pci_driver *drv = pci_dev->driver;
  287. if (drv && drv->shutdown)
  288. drv->shutdown(pci_dev);
  289. }
  290. #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
  291. #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
  292. static ssize_t
  293. pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
  294. {
  295. struct device_driver *driver = kobj_to_pci_driver(kobj);
  296. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  297. ssize_t ret;
  298. if (!get_driver(driver))
  299. return -ENODEV;
  300. ret = dattr->show ? dattr->show(driver, buf) : -EIO;
  301. put_driver(driver);
  302. return ret;
  303. }
  304. static ssize_t
  305. pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
  306. const char *buf, size_t count)
  307. {
  308. struct device_driver *driver = kobj_to_pci_driver(kobj);
  309. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  310. ssize_t ret;
  311. if (!get_driver(driver))
  312. return -ENODEV;
  313. ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
  314. put_driver(driver);
  315. return ret;
  316. }
  317. static struct sysfs_ops pci_driver_sysfs_ops = {
  318. .show = pci_driver_attr_show,
  319. .store = pci_driver_attr_store,
  320. };
  321. static struct kobj_type pci_driver_kobj_type = {
  322. .sysfs_ops = &pci_driver_sysfs_ops,
  323. };
  324. static int
  325. pci_populate_driver_dir(struct pci_driver *drv)
  326. {
  327. return pci_create_newid_file(drv);
  328. }
  329. /**
  330. * pci_register_driver - register a new pci driver
  331. * @drv: the driver structure to register
  332. *
  333. * Adds the driver structure to the list of registered drivers.
  334. * Returns a negative value on error, otherwise 0.
  335. * If no error occurred, the driver remains registered even if
  336. * no device was claimed during registration.
  337. */
  338. int pci_register_driver(struct pci_driver *drv)
  339. {
  340. int error;
  341. /* initialize common driver fields */
  342. drv->driver.name = drv->name;
  343. drv->driver.bus = &pci_bus_type;
  344. drv->driver.probe = pci_device_probe;
  345. drv->driver.remove = pci_device_remove;
  346. /* FIXME, once all of the existing PCI drivers have been fixed to set
  347. * the pci shutdown function, this test can go away. */
  348. if (!drv->driver.shutdown)
  349. drv->driver.shutdown = pci_device_shutdown;
  350. drv->driver.owner = drv->owner;
  351. drv->driver.kobj.ktype = &pci_driver_kobj_type;
  352. pci_init_dynids(&drv->dynids);
  353. /* register with core */
  354. error = driver_register(&drv->driver);
  355. if (!error)
  356. pci_populate_driver_dir(drv);
  357. return error;
  358. }
  359. /**
  360. * pci_unregister_driver - unregister a pci driver
  361. * @drv: the driver structure to unregister
  362. *
  363. * Deletes the driver structure from the list of registered PCI drivers,
  364. * gives it a chance to clean up by calling its remove() function for
  365. * each device it was responsible for, and marks those devices as
  366. * driverless.
  367. */
  368. void
  369. pci_unregister_driver(struct pci_driver *drv)
  370. {
  371. driver_unregister(&drv->driver);
  372. pci_free_dynids(drv);
  373. }
  374. static struct pci_driver pci_compat_driver = {
  375. .name = "compat"
  376. };
  377. /**
  378. * pci_dev_driver - get the pci_driver of a device
  379. * @dev: the device to query
  380. *
  381. * Returns the appropriate pci_driver structure or %NULL if there is no
  382. * registered driver for the device.
  383. */
  384. struct pci_driver *
  385. pci_dev_driver(const struct pci_dev *dev)
  386. {
  387. if (dev->driver)
  388. return dev->driver;
  389. else {
  390. int i;
  391. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  392. if (dev->resource[i].flags & IORESOURCE_BUSY)
  393. return &pci_compat_driver;
  394. }
  395. return NULL;
  396. }
  397. /**
  398. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  399. * @ids: array of PCI device id structures to search in
  400. * @dev: the PCI device structure to match against
  401. *
  402. * Used by a driver to check whether a PCI device present in the
  403. * system is in its list of supported devices.Returns the matching
  404. * pci_device_id structure or %NULL if there is no match.
  405. */
  406. static int pci_bus_match(struct device * dev, struct device_driver * drv)
  407. {
  408. const struct pci_dev * pci_dev = to_pci_dev(dev);
  409. struct pci_driver * pci_drv = to_pci_driver(drv);
  410. const struct pci_device_id * ids = pci_drv->id_table;
  411. const struct pci_device_id *found_id;
  412. if (!ids)
  413. return 0;
  414. found_id = pci_match_device(ids, pci_dev);
  415. if (found_id)
  416. return 1;
  417. return pci_bus_match_dynids(pci_dev, pci_drv);
  418. }
  419. /**
  420. * pci_dev_get - increments the reference count of the pci device structure
  421. * @dev: the device being referenced
  422. *
  423. * Each live reference to a device should be refcounted.
  424. *
  425. * Drivers for PCI devices should normally record such references in
  426. * their probe() methods, when they bind to a device, and release
  427. * them by calling pci_dev_put(), in their disconnect() methods.
  428. *
  429. * A pointer to the device with the incremented reference counter is returned.
  430. */
  431. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  432. {
  433. if (dev)
  434. get_device(&dev->dev);
  435. return dev;
  436. }
  437. /**
  438. * pci_dev_put - release a use of the pci device structure
  439. * @dev: device that's been disconnected
  440. *
  441. * Must be called when a user of a device is finished with it. When the last
  442. * user of the device calls this function, the memory of the device is freed.
  443. */
  444. void pci_dev_put(struct pci_dev *dev)
  445. {
  446. if (dev)
  447. put_device(&dev->dev);
  448. }
  449. #ifndef CONFIG_HOTPLUG
  450. int pci_hotplug (struct device *dev, char **envp, int num_envp,
  451. char *buffer, int buffer_size)
  452. {
  453. return -ENODEV;
  454. }
  455. #endif
  456. struct bus_type pci_bus_type = {
  457. .name = "pci",
  458. .match = pci_bus_match,
  459. .hotplug = pci_hotplug,
  460. .suspend = pci_device_suspend,
  461. .resume = pci_device_resume,
  462. .dev_attrs = pci_dev_attrs,
  463. };
  464. static int __init pci_driver_init(void)
  465. {
  466. return bus_register(&pci_bus_type);
  467. }
  468. postcore_initcall(pci_driver_init);
  469. EXPORT_SYMBOL(pci_match_device);
  470. EXPORT_SYMBOL(pci_register_driver);
  471. EXPORT_SYMBOL(pci_unregister_driver);
  472. EXPORT_SYMBOL(pci_dev_driver);
  473. EXPORT_SYMBOL(pci_bus_type);
  474. EXPORT_SYMBOL(pci_dev_get);
  475. EXPORT_SYMBOL(pci_dev_put);