pcmcia_cis.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * PCMCIA high-level CIS access functions
  3. *
  4. * The initial developer of the original code is David A. Hinds
  5. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  6. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  7. *
  8. * Copyright (C) 1999 David A. Hinds
  9. * Copyright (C) 2004-2009 Dominik Brodowski
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/netdevice.h>
  20. #include <pcmcia/cisreg.h>
  21. #include <pcmcia/cistpl.h>
  22. #include <pcmcia/ss.h>
  23. #include <pcmcia/cs.h>
  24. #include <pcmcia/ds.h>
  25. #include "cs_internal.h"
  26. /**
  27. * pccard_read_tuple() - internal CIS tuple access
  28. * @s: the struct pcmcia_socket where the card is inserted
  29. * @function: the device function we loop for
  30. * @code: which CIS code shall we look for?
  31. * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
  32. *
  33. * pccard_read_tuple() reads out one tuple and attempts to parse it
  34. */
  35. int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function,
  36. cisdata_t code, void *parse)
  37. {
  38. tuple_t tuple;
  39. cisdata_t *buf;
  40. int ret;
  41. buf = kmalloc(256, GFP_KERNEL);
  42. if (buf == NULL) {
  43. dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
  44. return -ENOMEM;
  45. }
  46. tuple.DesiredTuple = code;
  47. tuple.Attributes = 0;
  48. if (function == BIND_FN_ALL)
  49. tuple.Attributes = TUPLE_RETURN_COMMON;
  50. ret = pccard_get_first_tuple(s, function, &tuple);
  51. if (ret != 0)
  52. goto done;
  53. tuple.TupleData = buf;
  54. tuple.TupleOffset = 0;
  55. tuple.TupleDataMax = 255;
  56. ret = pccard_get_tuple_data(s, &tuple);
  57. if (ret != 0)
  58. goto done;
  59. ret = pcmcia_parse_tuple(&tuple, parse);
  60. done:
  61. kfree(buf);
  62. return ret;
  63. }
  64. /**
  65. * pccard_loop_tuple() - loop over tuples in the CIS
  66. * @s: the struct pcmcia_socket where the card is inserted
  67. * @function: the device function we loop for
  68. * @code: which CIS code shall we look for?
  69. * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
  70. * @priv_data: private data to be passed to the loop_tuple function.
  71. * @loop_tuple: function to call for each CIS entry of type @function. IT
  72. * gets passed the raw tuple, the paresed tuple (if @parse is
  73. * set) and @priv_data.
  74. *
  75. * pccard_loop_tuple() loops over all CIS entries of type @function, and
  76. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  77. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  78. */
  79. int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
  80. cisdata_t code, cisparse_t *parse, void *priv_data,
  81. int (*loop_tuple) (tuple_t *tuple,
  82. cisparse_t *parse,
  83. void *priv_data))
  84. {
  85. tuple_t tuple;
  86. cisdata_t *buf;
  87. int ret;
  88. buf = kzalloc(256, GFP_KERNEL);
  89. if (buf == NULL) {
  90. dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
  91. return -ENOMEM;
  92. }
  93. tuple.TupleData = buf;
  94. tuple.TupleDataMax = 255;
  95. tuple.TupleOffset = 0;
  96. tuple.DesiredTuple = code;
  97. tuple.Attributes = 0;
  98. ret = pccard_get_first_tuple(s, function, &tuple);
  99. while (!ret) {
  100. if (pccard_get_tuple_data(s, &tuple))
  101. goto next_entry;
  102. if (parse)
  103. if (pcmcia_parse_tuple(&tuple, parse))
  104. goto next_entry;
  105. ret = loop_tuple(&tuple, parse, priv_data);
  106. if (!ret)
  107. break;
  108. next_entry:
  109. ret = pccard_get_next_tuple(s, function, &tuple);
  110. }
  111. kfree(buf);
  112. return ret;
  113. }
  114. struct pcmcia_cfg_mem {
  115. struct pcmcia_device *p_dev;
  116. void *priv_data;
  117. int (*conf_check) (struct pcmcia_device *p_dev,
  118. cistpl_cftable_entry_t *cfg,
  119. cistpl_cftable_entry_t *dflt,
  120. unsigned int vcc,
  121. void *priv_data);
  122. cisparse_t parse;
  123. cistpl_cftable_entry_t dflt;
  124. };
  125. /**
  126. * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
  127. *
  128. * pcmcia_do_loop_config() is the internal callback for the call from
  129. * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
  130. * by a struct pcmcia_cfg_mem.
  131. */
  132. static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
  133. {
  134. cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
  135. struct pcmcia_cfg_mem *cfg_mem = priv;
  136. /* default values */
  137. cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
  138. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  139. cfg_mem->dflt = *cfg;
  140. return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
  141. cfg_mem->p_dev->socket->socket.Vcc,
  142. cfg_mem->priv_data);
  143. }
  144. /**
  145. * pcmcia_loop_config() - loop over configuration options
  146. * @p_dev: the struct pcmcia_device which we need to loop for.
  147. * @conf_check: function to call for each configuration option.
  148. * It gets passed the struct pcmcia_device, the CIS data
  149. * describing the configuration option, and private data
  150. * being passed to pcmcia_loop_config()
  151. * @priv_data: private data to be passed to the conf_check function.
  152. *
  153. * pcmcia_loop_config() loops over all configuration options, and calls
  154. * the driver-specific conf_check() for each one, checking whether
  155. * it is a valid one. Returns 0 on success or errorcode otherwise.
  156. */
  157. int pcmcia_loop_config(struct pcmcia_device *p_dev,
  158. int (*conf_check) (struct pcmcia_device *p_dev,
  159. cistpl_cftable_entry_t *cfg,
  160. cistpl_cftable_entry_t *dflt,
  161. unsigned int vcc,
  162. void *priv_data),
  163. void *priv_data)
  164. {
  165. struct pcmcia_cfg_mem *cfg_mem;
  166. int ret;
  167. cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
  168. if (cfg_mem == NULL)
  169. return -ENOMEM;
  170. cfg_mem->p_dev = p_dev;
  171. cfg_mem->conf_check = conf_check;
  172. cfg_mem->priv_data = priv_data;
  173. ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
  174. CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
  175. cfg_mem, pcmcia_do_loop_config);
  176. kfree(cfg_mem);
  177. return ret;
  178. }
  179. EXPORT_SYMBOL(pcmcia_loop_config);
  180. struct pcmcia_loop_mem {
  181. struct pcmcia_device *p_dev;
  182. void *priv_data;
  183. int (*loop_tuple) (struct pcmcia_device *p_dev,
  184. tuple_t *tuple,
  185. void *priv_data);
  186. };
  187. /**
  188. * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
  189. *
  190. * pcmcia_do_loop_tuple() is the internal callback for the call from
  191. * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
  192. * by a struct pcmcia_cfg_mem.
  193. */
  194. static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
  195. {
  196. struct pcmcia_loop_mem *loop = priv;
  197. return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
  198. };
  199. /**
  200. * pcmcia_loop_tuple() - loop over tuples in the CIS
  201. * @p_dev: the struct pcmcia_device which we need to loop for.
  202. * @code: which CIS code shall we look for?
  203. * @priv_data: private data to be passed to the loop_tuple function.
  204. * @loop_tuple: function to call for each CIS entry of type @function. IT
  205. * gets passed the raw tuple and @priv_data.
  206. *
  207. * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
  208. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  209. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  210. */
  211. int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  212. int (*loop_tuple) (struct pcmcia_device *p_dev,
  213. tuple_t *tuple,
  214. void *priv_data),
  215. void *priv_data)
  216. {
  217. struct pcmcia_loop_mem loop = {
  218. .p_dev = p_dev,
  219. .loop_tuple = loop_tuple,
  220. .priv_data = priv_data};
  221. return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
  222. &loop, pcmcia_do_loop_tuple);
  223. }
  224. EXPORT_SYMBOL(pcmcia_loop_tuple);
  225. struct pcmcia_loop_get {
  226. size_t len;
  227. cisdata_t **buf;
  228. };
  229. /**
  230. * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
  231. *
  232. * pcmcia_do_get_tuple() is the internal callback for the call from
  233. * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
  234. * the first tuple, return 0 unconditionally. Create a memory buffer large
  235. * enough to hold the content of the tuple, and fill it with the tuple data.
  236. * The caller is responsible to free the buffer.
  237. */
  238. static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
  239. void *priv)
  240. {
  241. struct pcmcia_loop_get *get = priv;
  242. *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
  243. if (*get->buf) {
  244. get->len = tuple->TupleDataLen;
  245. memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
  246. } else
  247. dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
  248. return 0;
  249. }
  250. /**
  251. * pcmcia_get_tuple() - get first tuple from CIS
  252. * @p_dev: the struct pcmcia_device which we need to loop for.
  253. * @code: which CIS code shall we look for?
  254. * @buf: pointer to store the buffer to.
  255. *
  256. * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
  257. * It returns the buffer length (or zero). The caller is responsible to free
  258. * the buffer passed in @buf.
  259. */
  260. size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  261. unsigned char **buf)
  262. {
  263. struct pcmcia_loop_get get = {
  264. .len = 0,
  265. .buf = buf,
  266. };
  267. *get.buf = NULL;
  268. pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
  269. return get.len;
  270. }
  271. EXPORT_SYMBOL(pcmcia_get_tuple);
  272. /**
  273. * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
  274. *
  275. * pcmcia_do_get_mac() is the internal callback for the call from
  276. * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
  277. * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
  278. * to struct net_device->dev_addr[i].
  279. */
  280. static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
  281. void *priv)
  282. {
  283. struct net_device *dev = priv;
  284. int i;
  285. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  286. return -EINVAL;
  287. if (tuple->TupleDataLen < ETH_ALEN + 2) {
  288. dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
  289. "LAN_NODE_ID\n");
  290. return -EINVAL;
  291. }
  292. if (tuple->TupleData[1] != ETH_ALEN) {
  293. dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
  294. return -EINVAL;
  295. }
  296. for (i = 0; i < 6; i++)
  297. dev->dev_addr[i] = tuple->TupleData[i+2];
  298. return 0;
  299. }
  300. /**
  301. * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
  302. * @p_dev: the struct pcmcia_device for which we want the address.
  303. * @dev: a properly prepared struct net_device to store the info to.
  304. *
  305. * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
  306. * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
  307. * must be set up properly by the driver (see examples!).
  308. */
  309. int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
  310. {
  311. return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
  312. }
  313. EXPORT_SYMBOL(pcmcia_get_mac_from_cis);