card.c 9.0 KB

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