card.c 9.7 KB

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