card.c 10 KB

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