card.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * card.c - contains functions for managing groups of PnP devices
  3. *
  4. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5. *
  6. */
  7. #include <linux/config.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/pnp.h>
  11. #include "base.h"
  12. LIST_HEAD(pnp_cards);
  13. LIST_HEAD(pnp_card_drivers);
  14. static const struct pnp_card_device_id * match_card(struct pnp_card_driver * drv, struct pnp_card * card)
  15. {
  16. const struct pnp_card_device_id * drv_id = drv->id_table;
  17. while (*drv_id->id){
  18. if (compare_pnp_id(card->id,drv_id->id)) {
  19. int i = 0;
  20. for (;;) {
  21. int found;
  22. struct pnp_dev *dev;
  23. if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
  24. return drv_id;
  25. found = 0;
  26. card_for_each_dev(card, dev) {
  27. if (compare_pnp_id(dev->id, drv_id->devs[i].id)) {
  28. found = 1;
  29. break;
  30. }
  31. }
  32. if (! found)
  33. break;
  34. i++;
  35. }
  36. }
  37. drv_id++;
  38. }
  39. return NULL;
  40. }
  41. static void card_remove(struct pnp_dev * dev)
  42. {
  43. dev->card_link = NULL;
  44. }
  45. static void card_remove_first(struct pnp_dev * dev)
  46. {
  47. struct pnp_card_driver * drv = to_pnp_card_driver(dev->driver);
  48. if (!dev->card || !drv)
  49. return;
  50. if (drv->remove)
  51. drv->remove(dev->card_link);
  52. drv->link.remove = &card_remove;
  53. kfree(dev->card_link);
  54. card_remove(dev);
  55. }
  56. static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
  57. {
  58. const struct pnp_card_device_id *id = match_card(drv,card);
  59. if (id) {
  60. struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
  61. if (!clink)
  62. return 0;
  63. clink->card = card;
  64. clink->driver = drv;
  65. if (drv->probe) {
  66. if (drv->probe(clink, id)>=0)
  67. return 1;
  68. else {
  69. struct pnp_dev * dev;
  70. card_for_each_dev(card, dev) {
  71. if (dev->card_link == clink)
  72. pnp_release_card_device(dev);
  73. }
  74. kfree(clink);
  75. }
  76. } else
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * pnp_add_card_id - adds an EISA id to the specified card
  83. * @id: pointer to a pnp_id structure
  84. * @card: pointer to the desired card
  85. *
  86. */
  87. int pnp_add_card_id(struct pnp_id *id, struct pnp_card * card)
  88. {
  89. struct pnp_id * ptr;
  90. if (!id)
  91. return -EINVAL;
  92. if (!card)
  93. return -EINVAL;
  94. id->next = NULL;
  95. ptr = card->id;
  96. while (ptr && ptr->next)
  97. ptr = ptr->next;
  98. if (ptr)
  99. ptr->next = id;
  100. else
  101. card->id = id;
  102. return 0;
  103. }
  104. static void pnp_free_card_ids(struct pnp_card * card)
  105. {
  106. struct pnp_id * id;
  107. struct pnp_id *next;
  108. if (!card)
  109. return;
  110. id = card->id;
  111. while (id) {
  112. next = id->next;
  113. kfree(id);
  114. id = next;
  115. }
  116. }
  117. static void pnp_release_card(struct device *dmdev)
  118. {
  119. struct pnp_card * card = to_pnp_card(dmdev);
  120. pnp_free_card_ids(card);
  121. kfree(card);
  122. }
  123. static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
  124. {
  125. char *str = buf;
  126. struct pnp_card *card = to_pnp_card(dmdev);
  127. str += sprintf(str,"%s\n", card->name);
  128. return (str - buf);
  129. }
  130. static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);
  131. static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
  132. {
  133. char *str = buf;
  134. struct pnp_card *card = to_pnp_card(dmdev);
  135. struct pnp_id * pos = card->id;
  136. while (pos) {
  137. str += sprintf(str,"%s\n", pos->id);
  138. pos = pos->next;
  139. }
  140. return (str - buf);
  141. }
  142. static DEVICE_ATTR(card_id,S_IRUGO,pnp_show_card_ids,NULL);
  143. static int pnp_interface_attach_card(struct pnp_card *card)
  144. {
  145. device_create_file(&card->dev,&dev_attr_name);
  146. device_create_file(&card->dev,&dev_attr_card_id);
  147. return 0;
  148. }
  149. /**
  150. * pnp_add_card - adds a PnP card to the PnP Layer
  151. * @card: pointer to the card to add
  152. */
  153. int pnp_add_card(struct pnp_card * card)
  154. {
  155. int error;
  156. struct list_head * pos, * temp;
  157. if (!card || !card->protocol)
  158. return -EINVAL;
  159. sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
  160. card->dev.parent = &card->protocol->dev;
  161. card->dev.bus = NULL;
  162. card->dev.release = &pnp_release_card;
  163. error = device_register(&card->dev);
  164. if (error == 0) {
  165. pnp_interface_attach_card(card);
  166. spin_lock(&pnp_lock);
  167. list_add_tail(&card->global_list, &pnp_cards);
  168. list_add_tail(&card->protocol_list, &card->protocol->cards);
  169. spin_unlock(&pnp_lock);
  170. /* we wait until now to add devices in order to ensure the drivers
  171. * will be able to use all of the related devices on the card
  172. * without waiting any unresonable length of time */
  173. list_for_each(pos,&card->devices){
  174. struct pnp_dev *dev = card_to_pnp_dev(pos);
  175. __pnp_add_device(dev);
  176. }
  177. /* match with card drivers */
  178. list_for_each_safe(pos,temp,&pnp_card_drivers){
  179. struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
  180. card_probe(card,drv);
  181. }
  182. } else
  183. pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
  184. return error;
  185. }
  186. /**
  187. * pnp_remove_card - removes a PnP card from the PnP Layer
  188. * @card: pointer to the card to remove
  189. */
  190. void pnp_remove_card(struct pnp_card * card)
  191. {
  192. struct list_head *pos, *temp;
  193. if (!card)
  194. return;
  195. device_unregister(&card->dev);
  196. spin_lock(&pnp_lock);
  197. list_del(&card->global_list);
  198. list_del(&card->protocol_list);
  199. spin_unlock(&pnp_lock);
  200. list_for_each_safe(pos,temp,&card->devices){
  201. struct pnp_dev *dev = card_to_pnp_dev(pos);
  202. pnp_remove_card_device(dev);
  203. }
  204. }
  205. /**
  206. * pnp_add_card_device - adds a device to the specified card
  207. * @card: pointer to the card to add to
  208. * @dev: pointer to the device to add
  209. */
  210. int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
  211. {
  212. if (!card || !dev || !dev->protocol)
  213. return -EINVAL;
  214. dev->dev.parent = &card->dev;
  215. dev->card_link = NULL;
  216. snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
  217. card->number,dev->number);
  218. spin_lock(&pnp_lock);
  219. dev->card = card;
  220. list_add_tail(&dev->card_list, &card->devices);
  221. spin_unlock(&pnp_lock);
  222. return 0;
  223. }
  224. /**
  225. * pnp_remove_card_device- removes a device from the specified card
  226. * @dev: pointer to the device to remove
  227. */
  228. void pnp_remove_card_device(struct pnp_dev * dev)
  229. {
  230. spin_lock(&pnp_lock);
  231. dev->card = NULL;
  232. list_del(&dev->card_list);
  233. spin_unlock(&pnp_lock);
  234. __pnp_remove_device(dev);
  235. }
  236. /**
  237. * pnp_request_card_device - Searches for a PnP device under the specified card
  238. * @clink: pointer to the card link, cannot be NULL
  239. * @id: pointer to a PnP ID structure that explains the rules for finding the device
  240. * @from: Starting place to search from. If NULL it will start from the begining.
  241. */
  242. struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
  243. {
  244. struct list_head * pos;
  245. struct pnp_dev * dev;
  246. struct pnp_card_driver * drv;
  247. struct pnp_card * card;
  248. if (!clink || !id)
  249. goto done;
  250. card = clink->card;
  251. drv = clink->driver;
  252. if (!from) {
  253. pos = card->devices.next;
  254. } else {
  255. if (from->card != card)
  256. goto done;
  257. pos = from->card_list.next;
  258. }
  259. while (pos != &card->devices) {
  260. dev = card_to_pnp_dev(pos);
  261. if ((!dev->card_link) && compare_pnp_id(dev->id,id))
  262. goto found;
  263. pos = pos->next;
  264. }
  265. done:
  266. return NULL;
  267. found:
  268. down_write(&dev->dev.bus->subsys.rwsem);
  269. dev->card_link = clink;
  270. dev->dev.driver = &drv->link.driver;
  271. if (drv->link.driver.probe) {
  272. if (drv->link.driver.probe(&dev->dev)) {
  273. dev->dev.driver = NULL;
  274. dev->card_link = NULL;
  275. up_write(&dev->dev.bus->subsys.rwsem);
  276. return NULL;
  277. }
  278. }
  279. device_bind_driver(&dev->dev);
  280. up_write(&dev->dev.bus->subsys.rwsem);
  281. return dev;
  282. }
  283. /**
  284. * pnp_release_card_device - call this when the driver no longer needs the device
  285. * @dev: pointer to the PnP device stucture
  286. */
  287. void pnp_release_card_device(struct pnp_dev * dev)
  288. {
  289. struct pnp_card_driver * drv = dev->card_link->driver;
  290. if (!drv)
  291. return;
  292. down_write(&dev->dev.bus->subsys.rwsem);
  293. drv->link.remove = &card_remove;
  294. device_release_driver(&dev->dev);
  295. drv->link.remove = &card_remove_first;
  296. up_write(&dev->dev.bus->subsys.rwsem);
  297. }
  298. /**
  299. * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
  300. * @drv: pointer to the driver to register
  301. */
  302. int pnp_register_card_driver(struct pnp_card_driver * drv)
  303. {
  304. int count = 0;
  305. struct list_head *pos, *temp;
  306. drv->link.name = drv->name;
  307. drv->link.id_table = NULL; /* this will disable auto matching */
  308. drv->link.flags = drv->flags;
  309. drv->link.probe = NULL;
  310. drv->link.remove = &card_remove_first;
  311. spin_lock(&pnp_lock);
  312. list_add_tail(&drv->global_list, &pnp_card_drivers);
  313. spin_unlock(&pnp_lock);
  314. pnp_register_driver(&drv->link);
  315. list_for_each_safe(pos,temp,&pnp_cards){
  316. struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
  317. count += card_probe(card,drv);
  318. }
  319. return count;
  320. }
  321. /**
  322. * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
  323. * @drv: pointer to the driver to unregister
  324. */
  325. void pnp_unregister_card_driver(struct pnp_card_driver * drv)
  326. {
  327. spin_lock(&pnp_lock);
  328. list_del(&drv->global_list);
  329. spin_unlock(&pnp_lock);
  330. pnp_unregister_driver(&drv->link);
  331. }
  332. EXPORT_SYMBOL(pnp_add_card);
  333. EXPORT_SYMBOL(pnp_remove_card);
  334. EXPORT_SYMBOL(pnp_add_card_device);
  335. EXPORT_SYMBOL(pnp_remove_card_device);
  336. EXPORT_SYMBOL(pnp_add_card_id);
  337. EXPORT_SYMBOL(pnp_request_card_device);
  338. EXPORT_SYMBOL(pnp_release_card_device);
  339. EXPORT_SYMBOL(pnp_register_card_driver);
  340. EXPORT_SYMBOL(pnp_unregister_card_driver);