pcmcia_cis.c 10 KB

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