pci-driver.c 12 KB

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