pcmcia_cis.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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-2010 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_warn(&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_warn(&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. /**
  114. * pcmcia_io_cfg_data_width() - convert cfgtable to data path width parameter
  115. */
  116. static int pcmcia_io_cfg_data_width(unsigned int flags)
  117. {
  118. if (!(flags & CISTPL_IO_8BIT))
  119. return IO_DATA_PATH_WIDTH_16;
  120. if (!(flags & CISTPL_IO_16BIT))
  121. return IO_DATA_PATH_WIDTH_8;
  122. return IO_DATA_PATH_WIDTH_AUTO;
  123. }
  124. struct pcmcia_cfg_mem {
  125. struct pcmcia_device *p_dev;
  126. int (*conf_check) (struct pcmcia_device *p_dev, void *priv_data);
  127. void *priv_data;
  128. cisparse_t parse;
  129. cistpl_cftable_entry_t dflt;
  130. };
  131. /**
  132. * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
  133. *
  134. * pcmcia_do_loop_config() is the internal callback for the call from
  135. * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
  136. * by a struct pcmcia_cfg_mem.
  137. */
  138. static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
  139. {
  140. struct pcmcia_cfg_mem *cfg_mem = priv;
  141. struct pcmcia_device *p_dev = cfg_mem->p_dev;
  142. cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
  143. cistpl_cftable_entry_t *dflt = &cfg_mem->dflt;
  144. unsigned int flags = p_dev->config_flags;
  145. unsigned int vcc = p_dev->socket->socket.Vcc;
  146. dev_dbg(&p_dev->dev, "testing configuration %x, autoconf %x\n",
  147. cfg->index, flags);
  148. /* default values */
  149. cfg_mem->p_dev->config_index = cfg->index;
  150. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  151. cfg_mem->dflt = *cfg;
  152. /* check for matching Vcc? */
  153. if (flags & CONF_AUTO_CHECK_VCC) {
  154. if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  155. if (vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000)
  156. return -ENODEV;
  157. } else if (dflt->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  158. if (vcc != dflt->vcc.param[CISTPL_POWER_VNOM] / 10000)
  159. return -ENODEV;
  160. }
  161. }
  162. /* set Vpp? */
  163. if (flags & CONF_AUTO_SET_VPP) {
  164. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
  165. p_dev->vpp = cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  166. else if (dflt->vpp1.present & (1 << CISTPL_POWER_VNOM))
  167. p_dev->vpp =
  168. dflt->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  169. }
  170. /* enable audio? */
  171. if ((flags & CONF_AUTO_AUDIO) && (cfg->flags & CISTPL_CFTABLE_AUDIO))
  172. p_dev->config_flags |= CONF_ENABLE_SPKR;
  173. /* IO window settings? */
  174. if (flags & CONF_AUTO_SET_IO) {
  175. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
  176. int i = 0;
  177. p_dev->resource[0]->start = p_dev->resource[0]->end = 0;
  178. p_dev->resource[1]->start = p_dev->resource[1]->end = 0;
  179. if (io->nwin == 0)
  180. return -ENODEV;
  181. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  182. p_dev->resource[0]->flags |=
  183. pcmcia_io_cfg_data_width(io->flags);
  184. if (io->nwin > 1) {
  185. /* For multifunction cards, by convention, we
  186. * configure the network function with window 0,
  187. * and serial with window 1 */
  188. i = (io->win[1].len > io->win[0].len);
  189. p_dev->resource[1]->flags = p_dev->resource[0]->flags;
  190. p_dev->resource[1]->start = io->win[1-i].base;
  191. p_dev->resource[1]->end = io->win[1-i].len;
  192. }
  193. p_dev->resource[0]->start = io->win[i].base;
  194. p_dev->resource[0]->end = io->win[i].len;
  195. p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
  196. }
  197. /* MEM window settings? */
  198. if (flags & CONF_AUTO_SET_IOMEM) {
  199. /* so far, we only set one memory window */
  200. cistpl_mem_t *mem = (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
  201. p_dev->resource[2]->start = p_dev->resource[2]->end = 0;
  202. if (mem->nwin == 0)
  203. return -ENODEV;
  204. p_dev->resource[2]->start = mem->win[0].host_addr;
  205. p_dev->resource[2]->end = mem->win[0].len;
  206. if (p_dev->resource[2]->end < 0x1000)
  207. p_dev->resource[2]->end = 0x1000;
  208. p_dev->card_addr = mem->win[0].card_addr;
  209. }
  210. dev_dbg(&p_dev->dev,
  211. "checking configuration %x: %pr %pr %pr (%d lines)\n",
  212. p_dev->config_index, p_dev->resource[0], p_dev->resource[1],
  213. p_dev->resource[2], p_dev->io_lines);
  214. return cfg_mem->conf_check(p_dev, cfg_mem->priv_data);
  215. }
  216. /**
  217. * pcmcia_loop_config() - loop over configuration options
  218. * @p_dev: the struct pcmcia_device which we need to loop for.
  219. * @conf_check: function to call for each configuration option.
  220. * It gets passed the struct pcmcia_device and private data
  221. * being passed to pcmcia_loop_config()
  222. * @priv_data: private data to be passed to the conf_check function.
  223. *
  224. * pcmcia_loop_config() loops over all configuration options, and calls
  225. * the driver-specific conf_check() for each one, checking whether
  226. * it is a valid one. Returns 0 on success or errorcode otherwise.
  227. */
  228. int pcmcia_loop_config(struct pcmcia_device *p_dev,
  229. int (*conf_check) (struct pcmcia_device *p_dev,
  230. void *priv_data),
  231. void *priv_data)
  232. {
  233. struct pcmcia_cfg_mem *cfg_mem;
  234. int ret;
  235. cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
  236. if (cfg_mem == NULL)
  237. return -ENOMEM;
  238. cfg_mem->p_dev = p_dev;
  239. cfg_mem->conf_check = conf_check;
  240. cfg_mem->priv_data = priv_data;
  241. ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
  242. CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
  243. cfg_mem, pcmcia_do_loop_config);
  244. kfree(cfg_mem);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(pcmcia_loop_config);
  248. struct pcmcia_loop_mem {
  249. struct pcmcia_device *p_dev;
  250. void *priv_data;
  251. int (*loop_tuple) (struct pcmcia_device *p_dev,
  252. tuple_t *tuple,
  253. void *priv_data);
  254. };
  255. /**
  256. * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
  257. *
  258. * pcmcia_do_loop_tuple() is the internal callback for the call from
  259. * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
  260. * by a struct pcmcia_cfg_mem.
  261. */
  262. static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
  263. {
  264. struct pcmcia_loop_mem *loop = priv;
  265. return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
  266. };
  267. /**
  268. * pcmcia_loop_tuple() - loop over tuples in the CIS
  269. * @p_dev: the struct pcmcia_device which we need to loop for.
  270. * @code: which CIS code shall we look for?
  271. * @priv_data: private data to be passed to the loop_tuple function.
  272. * @loop_tuple: function to call for each CIS entry of type @function. IT
  273. * gets passed the raw tuple and @priv_data.
  274. *
  275. * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
  276. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  277. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  278. */
  279. int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  280. int (*loop_tuple) (struct pcmcia_device *p_dev,
  281. tuple_t *tuple,
  282. void *priv_data),
  283. void *priv_data)
  284. {
  285. struct pcmcia_loop_mem loop = {
  286. .p_dev = p_dev,
  287. .loop_tuple = loop_tuple,
  288. .priv_data = priv_data};
  289. return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
  290. &loop, pcmcia_do_loop_tuple);
  291. }
  292. EXPORT_SYMBOL(pcmcia_loop_tuple);
  293. struct pcmcia_loop_get {
  294. size_t len;
  295. cisdata_t **buf;
  296. };
  297. /**
  298. * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
  299. *
  300. * pcmcia_do_get_tuple() is the internal callback for the call from
  301. * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
  302. * the first tuple, return 0 unconditionally. Create a memory buffer large
  303. * enough to hold the content of the tuple, and fill it with the tuple data.
  304. * The caller is responsible to free the buffer.
  305. */
  306. static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
  307. void *priv)
  308. {
  309. struct pcmcia_loop_get *get = priv;
  310. *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
  311. if (*get->buf) {
  312. get->len = tuple->TupleDataLen;
  313. memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
  314. } else
  315. dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
  316. return 0;
  317. }
  318. /**
  319. * pcmcia_get_tuple() - get first tuple from CIS
  320. * @p_dev: the struct pcmcia_device which we need to loop for.
  321. * @code: which CIS code shall we look for?
  322. * @buf: pointer to store the buffer to.
  323. *
  324. * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
  325. * It returns the buffer length (or zero). The caller is responsible to free
  326. * the buffer passed in @buf.
  327. */
  328. size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  329. unsigned char **buf)
  330. {
  331. struct pcmcia_loop_get get = {
  332. .len = 0,
  333. .buf = buf,
  334. };
  335. *get.buf = NULL;
  336. pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
  337. return get.len;
  338. }
  339. EXPORT_SYMBOL(pcmcia_get_tuple);
  340. /**
  341. * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
  342. *
  343. * pcmcia_do_get_mac() is the internal callback for the call from
  344. * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
  345. * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
  346. * to struct net_device->dev_addr[i].
  347. */
  348. static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
  349. void *priv)
  350. {
  351. struct net_device *dev = priv;
  352. int i;
  353. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  354. return -EINVAL;
  355. if (tuple->TupleDataLen < ETH_ALEN + 2) {
  356. dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
  357. "LAN_NODE_ID\n");
  358. return -EINVAL;
  359. }
  360. if (tuple->TupleData[1] != ETH_ALEN) {
  361. dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
  362. return -EINVAL;
  363. }
  364. for (i = 0; i < 6; i++)
  365. dev->dev_addr[i] = tuple->TupleData[i+2];
  366. return 0;
  367. }
  368. /**
  369. * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
  370. * @p_dev: the struct pcmcia_device for which we want the address.
  371. * @dev: a properly prepared struct net_device to store the info to.
  372. *
  373. * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
  374. * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
  375. * must be set up properly by the driver (see examples!).
  376. */
  377. int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
  378. {
  379. return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
  380. }
  381. EXPORT_SYMBOL(pcmcia_get_mac_from_cis);