ixj_pcmcia.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "ixj-ver.h"
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/sched.h>
  5. #include <linux/kernel.h> /* printk() */
  6. #include <linux/fs.h> /* everything... */
  7. #include <linux/errno.h> /* error codes */
  8. #include <linux/slab.h>
  9. #include <pcmcia/version.h>
  10. #include <pcmcia/cs_types.h>
  11. #include <pcmcia/cs.h>
  12. #include <pcmcia/cistpl.h>
  13. #include <pcmcia/ds.h>
  14. #include "ixj.h"
  15. /*
  16. * PCMCIA service support for Quicknet cards
  17. */
  18. #ifdef PCMCIA_DEBUG
  19. static int pc_debug = PCMCIA_DEBUG;
  20. module_param(pc_debug, int, 0644);
  21. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  22. #else
  23. #define DEBUG(n, args...)
  24. #endif
  25. typedef struct ixj_info_t {
  26. int ndev;
  27. dev_node_t node;
  28. struct ixj *port;
  29. } ixj_info_t;
  30. static dev_link_t *ixj_attach(void);
  31. static void ixj_detach(dev_link_t *);
  32. static void ixj_config(dev_link_t * link);
  33. static void ixj_cs_release(dev_link_t * link);
  34. static int ixj_event(event_t event, int priority, event_callback_args_t * args);
  35. static dev_info_t dev_info = "ixj_cs";
  36. static dev_link_t *dev_list = NULL;
  37. static dev_link_t *ixj_attach(void)
  38. {
  39. client_reg_t client_reg;
  40. dev_link_t *link;
  41. int ret;
  42. DEBUG(0, "ixj_attach()\n");
  43. /* Create new ixj device */
  44. link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
  45. if (!link)
  46. return NULL;
  47. memset(link, 0, sizeof(struct dev_link_t));
  48. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  49. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  50. link->io.IOAddrLines = 3;
  51. link->conf.Vcc = 50;
  52. link->conf.IntType = INT_MEMORY_AND_IO;
  53. link->priv = kmalloc(sizeof(struct ixj_info_t), GFP_KERNEL);
  54. if (!link->priv) {
  55. kfree(link);
  56. return NULL;
  57. }
  58. memset(link->priv, 0, sizeof(struct ixj_info_t));
  59. /* Register with Card Services */
  60. link->next = dev_list;
  61. dev_list = link;
  62. client_reg.dev_info = &dev_info;
  63. client_reg.EventMask =
  64. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  65. CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  66. CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  67. client_reg.event_handler = &ixj_event;
  68. client_reg.Version = 0x0210;
  69. client_reg.event_callback_args.client_data = link;
  70. ret = pcmcia_register_client(&link->handle, &client_reg);
  71. if (ret != CS_SUCCESS) {
  72. cs_error(link->handle, RegisterClient, ret);
  73. ixj_detach(link);
  74. return NULL;
  75. }
  76. return link;
  77. }
  78. static void ixj_detach(dev_link_t * link)
  79. {
  80. dev_link_t **linkp;
  81. int ret;
  82. DEBUG(0, "ixj_detach(0x%p)\n", link);
  83. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  84. if (*linkp == link)
  85. break;
  86. if (*linkp == NULL)
  87. return;
  88. link->state &= ~DEV_RELEASE_PENDING;
  89. if (link->state & DEV_CONFIG)
  90. ixj_cs_release(link);
  91. if (link->handle) {
  92. ret = pcmcia_deregister_client(link->handle);
  93. if (ret != CS_SUCCESS)
  94. cs_error(link->handle, DeregisterClient, ret);
  95. }
  96. /* Unlink device structure, free bits */
  97. *linkp = link->next;
  98. kfree(link->priv);
  99. kfree(link);
  100. }
  101. #define CS_CHECK(fn, ret) \
  102. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  103. static void ixj_get_serial(dev_link_t * link, IXJ * j)
  104. {
  105. client_handle_t handle;
  106. tuple_t tuple;
  107. u_short buf[128];
  108. char *str;
  109. int last_ret, last_fn, i, place;
  110. handle = link->handle;
  111. DEBUG(0, "ixj_get_serial(0x%p)\n", link);
  112. tuple.TupleData = (cisdata_t *) buf;
  113. tuple.TupleOffset = 0;
  114. tuple.TupleDataMax = 80;
  115. tuple.Attributes = 0;
  116. tuple.DesiredTuple = CISTPL_VERS_1;
  117. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  118. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  119. str = (char *) buf;
  120. printk("PCMCIA Version %d.%d\n", str[0], str[1]);
  121. str += 2;
  122. printk("%s", str);
  123. str = str + strlen(str) + 1;
  124. printk(" %s", str);
  125. str = str + strlen(str) + 1;
  126. place = 1;
  127. for (i = strlen(str) - 1; i >= 0; i--) {
  128. switch (str[i]) {
  129. case '0':
  130. case '1':
  131. case '2':
  132. case '3':
  133. case '4':
  134. case '5':
  135. case '6':
  136. case '7':
  137. case '8':
  138. case '9':
  139. j->serial += (str[i] - 48) * place;
  140. break;
  141. case 'A':
  142. case 'B':
  143. case 'C':
  144. case 'D':
  145. case 'E':
  146. case 'F':
  147. j->serial += (str[i] - 55) * place;
  148. break;
  149. case 'a':
  150. case 'b':
  151. case 'c':
  152. case 'd':
  153. case 'e':
  154. case 'f':
  155. j->serial += (str[i] - 87) * place;
  156. break;
  157. }
  158. place = place * 0x10;
  159. }
  160. str = str + strlen(str) + 1;
  161. printk(" version %s\n", str);
  162. cs_failed:
  163. return;
  164. }
  165. static void ixj_config(dev_link_t * link)
  166. {
  167. IXJ *j;
  168. client_handle_t handle;
  169. ixj_info_t *info;
  170. tuple_t tuple;
  171. u_short buf[128];
  172. cisparse_t parse;
  173. config_info_t conf;
  174. cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
  175. cistpl_cftable_entry_t dflt =
  176. {
  177. 0
  178. };
  179. int last_ret, last_fn;
  180. handle = link->handle;
  181. info = link->priv;
  182. DEBUG(0, "ixj_config(0x%p)\n", link);
  183. tuple.TupleData = (cisdata_t *) buf;
  184. tuple.TupleOffset = 0;
  185. tuple.TupleDataMax = 255;
  186. tuple.Attributes = 0;
  187. tuple.DesiredTuple = CISTPL_CONFIG;
  188. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  189. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  190. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  191. link->conf.ConfigBase = parse.config.base;
  192. link->conf.Present = parse.config.rmask[0];
  193. link->state |= DEV_CONFIG;
  194. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
  195. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  196. tuple.Attributes = 0;
  197. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  198. while (1) {
  199. if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
  200. pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
  201. goto next_entry;
  202. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  203. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  204. link->conf.ConfigIndex = cfg->index;
  205. link->io.BasePort1 = io->win[0].base;
  206. link->io.NumPorts1 = io->win[0].len;
  207. if (io->nwin == 2) {
  208. link->io.BasePort2 = io->win[1].base;
  209. link->io.NumPorts2 = io->win[1].len;
  210. }
  211. if (pcmcia_request_io(link->handle, &link->io) != 0)
  212. goto next_entry;
  213. /* If we've got this far, we're done */
  214. break;
  215. }
  216. next_entry:
  217. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  218. dflt = *cfg;
  219. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
  220. }
  221. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
  222. /*
  223. * Register the card with the core.
  224. */
  225. j=ixj_pcmcia_probe(link->io.BasePort1,link->io.BasePort1 + 0x10);
  226. info->ndev = 1;
  227. info->node.major = PHONE_MAJOR;
  228. link->dev = &info->node;
  229. ixj_get_serial(link, j);
  230. link->state &= ~DEV_CONFIG_PENDING;
  231. return;
  232. cs_failed:
  233. cs_error(link->handle, last_fn, last_ret);
  234. ixj_cs_release(link);
  235. }
  236. static void ixj_cs_release(dev_link_t *link)
  237. {
  238. ixj_info_t *info = link->priv;
  239. DEBUG(0, "ixj_cs_release(0x%p)\n", link);
  240. info->ndev = 0;
  241. link->dev = NULL;
  242. pcmcia_release_configuration(link->handle);
  243. pcmcia_release_io(link->handle, &link->io);
  244. link->state &= ~DEV_CONFIG;
  245. }
  246. static int ixj_event(event_t event, int priority, event_callback_args_t * args)
  247. {
  248. dev_link_t *link = args->client_data;
  249. DEBUG(1, "ixj_event(0x%06x)\n", event);
  250. switch (event) {
  251. case CS_EVENT_CARD_REMOVAL:
  252. link->state &= ~DEV_PRESENT;
  253. if (link->state & DEV_CONFIG) {
  254. link->state |= DEV_RELEASE_PENDING;
  255. ixj_cs_release(link);
  256. }
  257. break;
  258. case CS_EVENT_CARD_INSERTION:
  259. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  260. ixj_config(link);
  261. break;
  262. case CS_EVENT_PM_SUSPEND:
  263. link->state |= DEV_SUSPEND;
  264. /* Fall through... */
  265. case CS_EVENT_RESET_PHYSICAL:
  266. if (link->state & DEV_CONFIG)
  267. pcmcia_release_configuration(link->handle);
  268. break;
  269. case CS_EVENT_PM_RESUME:
  270. link->state &= ~DEV_SUSPEND;
  271. /* Fall through... */
  272. case CS_EVENT_CARD_RESET:
  273. if (DEV_OK(link))
  274. pcmcia_request_configuration(link->handle, &link->conf);
  275. break;
  276. }
  277. return 0;
  278. }
  279. static struct pcmcia_device_id ixj_ids[] = {
  280. PCMCIA_DEVICE_MANF_CARD(0x0257, 0x0600),
  281. PCMCIA_DEVICE_NULL
  282. };
  283. MODULE_DEVICE_TABLE(pcmcia, ixj_ids);
  284. static struct pcmcia_driver ixj_driver = {
  285. .owner = THIS_MODULE,
  286. .drv = {
  287. .name = "ixj_cs",
  288. },
  289. .attach = ixj_attach,
  290. .detach = ixj_detach,
  291. .id_table = ixj_ids,
  292. };
  293. static int __init ixj_pcmcia_init(void)
  294. {
  295. return pcmcia_register_driver(&ixj_driver);
  296. }
  297. static void ixj_pcmcia_exit(void)
  298. {
  299. pcmcia_unregister_driver(&ixj_driver);
  300. BUG_ON(dev_list != NULL);
  301. }
  302. module_init(ixj_pcmcia_init);
  303. module_exit(ixj_pcmcia_exit);
  304. MODULE_LICENSE("GPL");