pci-driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
  284. #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
  285. static ssize_t
  286. pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
  287. {
  288. struct device_driver *driver = kobj_to_pci_driver(kobj);
  289. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  290. ssize_t ret = 0;
  291. if (get_driver(driver)) {
  292. if (dattr->show)
  293. ret = dattr->show(driver, buf);
  294. put_driver(driver);
  295. }
  296. return ret;
  297. }
  298. static ssize_t
  299. pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
  300. const char *buf, size_t count)
  301. {
  302. struct device_driver *driver = kobj_to_pci_driver(kobj);
  303. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  304. ssize_t ret = 0;
  305. if (get_driver(driver)) {
  306. if (dattr->store)
  307. ret = dattr->store(driver, buf, count);
  308. put_driver(driver);
  309. }
  310. return ret;
  311. }
  312. static struct sysfs_ops pci_driver_sysfs_ops = {
  313. .show = pci_driver_attr_show,
  314. .store = pci_driver_attr_store,
  315. };
  316. static struct kobj_type pci_driver_kobj_type = {
  317. .sysfs_ops = &pci_driver_sysfs_ops,
  318. };
  319. static int
  320. pci_populate_driver_dir(struct pci_driver *drv)
  321. {
  322. return pci_create_newid_file(drv);
  323. }
  324. /**
  325. * pci_register_driver - register a new pci driver
  326. * @drv: the driver structure to register
  327. *
  328. * Adds the driver structure to the list of registered drivers.
  329. * Returns a negative value on error, otherwise 0.
  330. * If no error occured, the driver remains registered even if
  331. * no device was claimed during registration.
  332. */
  333. int pci_register_driver(struct pci_driver *drv)
  334. {
  335. int error;
  336. /* initialize common driver fields */
  337. drv->driver.name = drv->name;
  338. drv->driver.bus = &pci_bus_type;
  339. drv->driver.probe = pci_device_probe;
  340. drv->driver.remove = pci_device_remove;
  341. drv->driver.owner = drv->owner;
  342. drv->driver.kobj.ktype = &pci_driver_kobj_type;
  343. pci_init_dynids(&drv->dynids);
  344. /* register with core */
  345. error = driver_register(&drv->driver);
  346. if (!error)
  347. pci_populate_driver_dir(drv);
  348. return error;
  349. }
  350. /**
  351. * pci_unregister_driver - unregister a pci driver
  352. * @drv: the driver structure to unregister
  353. *
  354. * Deletes the driver structure from the list of registered PCI drivers,
  355. * gives it a chance to clean up by calling its remove() function for
  356. * each device it was responsible for, and marks those devices as
  357. * driverless.
  358. */
  359. void
  360. pci_unregister_driver(struct pci_driver *drv)
  361. {
  362. driver_unregister(&drv->driver);
  363. pci_free_dynids(drv);
  364. }
  365. static struct pci_driver pci_compat_driver = {
  366. .name = "compat"
  367. };
  368. /**
  369. * pci_dev_driver - get the pci_driver of a device
  370. * @dev: the device to query
  371. *
  372. * Returns the appropriate pci_driver structure or %NULL if there is no
  373. * registered driver for the device.
  374. */
  375. struct pci_driver *
  376. pci_dev_driver(const struct pci_dev *dev)
  377. {
  378. if (dev->driver)
  379. return dev->driver;
  380. else {
  381. int i;
  382. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  383. if (dev->resource[i].flags & IORESOURCE_BUSY)
  384. return &pci_compat_driver;
  385. }
  386. return NULL;
  387. }
  388. /**
  389. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  390. * @ids: array of PCI device id structures to search in
  391. * @dev: the PCI device structure to match against
  392. *
  393. * Used by a driver to check whether a PCI device present in the
  394. * system is in its list of supported devices.Returns the matching
  395. * pci_device_id structure or %NULL if there is no match.
  396. */
  397. static int pci_bus_match(struct device * dev, struct device_driver * drv)
  398. {
  399. const struct pci_dev * pci_dev = to_pci_dev(dev);
  400. struct pci_driver * pci_drv = to_pci_driver(drv);
  401. const struct pci_device_id * ids = pci_drv->id_table;
  402. const struct pci_device_id *found_id;
  403. if (!ids)
  404. return 0;
  405. found_id = pci_match_device(ids, pci_dev);
  406. if (found_id)
  407. return 1;
  408. return pci_bus_match_dynids(pci_dev, pci_drv);
  409. }
  410. /**
  411. * pci_dev_get - increments the reference count of the pci device structure
  412. * @dev: the device being referenced
  413. *
  414. * Each live reference to a device should be refcounted.
  415. *
  416. * Drivers for PCI devices should normally record such references in
  417. * their probe() methods, when they bind to a device, and release
  418. * them by calling pci_dev_put(), in their disconnect() methods.
  419. *
  420. * A pointer to the device with the incremented reference counter is returned.
  421. */
  422. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  423. {
  424. if (dev)
  425. get_device(&dev->dev);
  426. return dev;
  427. }
  428. /**
  429. * pci_dev_put - release a use of the pci device structure
  430. * @dev: device that's been disconnected
  431. *
  432. * Must be called when a user of a device is finished with it. When the last
  433. * user of the device calls this function, the memory of the device is freed.
  434. */
  435. void pci_dev_put(struct pci_dev *dev)
  436. {
  437. if (dev)
  438. put_device(&dev->dev);
  439. }
  440. #ifndef CONFIG_HOTPLUG
  441. int pci_hotplug (struct device *dev, char **envp, int num_envp,
  442. char *buffer, int buffer_size)
  443. {
  444. return -ENODEV;
  445. }
  446. #endif
  447. struct bus_type pci_bus_type = {
  448. .name = "pci",
  449. .match = pci_bus_match,
  450. .hotplug = pci_hotplug,
  451. .suspend = pci_device_suspend,
  452. .resume = pci_device_resume,
  453. .dev_attrs = pci_dev_attrs,
  454. };
  455. static int __init pci_driver_init(void)
  456. {
  457. return bus_register(&pci_bus_type);
  458. }
  459. postcore_initcall(pci_driver_init);
  460. EXPORT_SYMBOL(pci_match_device);
  461. EXPORT_SYMBOL(pci_register_driver);
  462. EXPORT_SYMBOL(pci_unregister_driver);
  463. EXPORT_SYMBOL(pci_dev_driver);
  464. EXPORT_SYMBOL(pci_bus_type);
  465. EXPORT_SYMBOL(pci_dev_get);
  466. EXPORT_SYMBOL(pci_dev_put);