pcmcia_cis.c 10 KB

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