pcmcia_cis.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCMCIA high-level CIS access functions
  4. *
  5. * The initial developer of the original code is David A. Hinds
  6. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  7. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  8. *
  9. * Copyright (C) 1999 David A. Hinds
  10. * Copyright (C) 2004-2010 Dominik Brodowski
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <pcmcia/cisreg.h>
  17. #include <pcmcia/cistpl.h>
  18. #include <pcmcia/ss.h>
  19. #include <pcmcia/ds.h>
  20. #include "cs_internal.h"
  21. /**
  22. * pccard_read_tuple() - internal CIS tuple access
  23. * @s: the struct pcmcia_socket where the card is inserted
  24. * @function: the device function we loop for
  25. * @code: which CIS code shall we look for?
  26. * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
  27. *
  28. * pccard_read_tuple() reads out one tuple and attempts to parse it
  29. */
  30. int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function,
  31. cisdata_t code, void *parse)
  32. {
  33. tuple_t tuple;
  34. cisdata_t *buf;
  35. int ret;
  36. buf = kmalloc(256, GFP_KERNEL);
  37. if (buf == NULL) {
  38. dev_warn(&s->dev, "no memory to read tuple\n");
  39. return -ENOMEM;
  40. }
  41. tuple.DesiredTuple = code;
  42. tuple.Attributes = 0;
  43. if (function == BIND_FN_ALL)
  44. tuple.Attributes = TUPLE_RETURN_COMMON;
  45. ret = pccard_get_first_tuple(s, function, &tuple);
  46. if (ret != 0)
  47. goto done;
  48. tuple.TupleData = buf;
  49. tuple.TupleOffset = 0;
  50. tuple.TupleDataMax = 255;
  51. ret = pccard_get_tuple_data(s, &tuple);
  52. if (ret != 0)
  53. goto done;
  54. ret = pcmcia_parse_tuple(&tuple, parse);
  55. done:
  56. kfree(buf);
  57. return ret;
  58. }
  59. /**
  60. * pccard_loop_tuple() - loop over tuples in the CIS
  61. * @s: the struct pcmcia_socket where the card is inserted
  62. * @function: the device function we loop for
  63. * @code: which CIS code shall we look for?
  64. * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
  65. * @priv_data: private data to be passed to the loop_tuple function.
  66. * @loop_tuple: function to call for each CIS entry of type @function. IT
  67. * gets passed the raw tuple, the paresed tuple (if @parse is
  68. * set) and @priv_data.
  69. *
  70. * pccard_loop_tuple() loops over all CIS entries of type @function, and
  71. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  72. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  73. */
  74. static int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
  75. cisdata_t code, cisparse_t *parse, void *priv_data,
  76. int (*loop_tuple) (tuple_t *tuple,
  77. cisparse_t *parse,
  78. void *priv_data))
  79. {
  80. tuple_t tuple;
  81. cisdata_t *buf;
  82. int ret;
  83. buf = kzalloc(256, GFP_KERNEL);
  84. if (buf == NULL) {
  85. dev_warn(&s->dev, "no memory to read tuple\n");
  86. return -ENOMEM;
  87. }
  88. tuple.TupleData = buf;
  89. tuple.TupleDataMax = 255;
  90. tuple.TupleOffset = 0;
  91. tuple.DesiredTuple = code;
  92. tuple.Attributes = 0;
  93. ret = pccard_get_first_tuple(s, function, &tuple);
  94. while (!ret) {
  95. if (pccard_get_tuple_data(s, &tuple))
  96. goto next_entry;
  97. if (parse)
  98. if (pcmcia_parse_tuple(&tuple, parse))
  99. goto next_entry;
  100. ret = loop_tuple(&tuple, parse, priv_data);
  101. if (!ret)
  102. break;
  103. next_entry:
  104. ret = pccard_get_next_tuple(s, function, &tuple);
  105. }
  106. kfree(buf);
  107. return ret;
  108. }
  109. /**
  110. * pcmcia_io_cfg_data_width() - convert cfgtable to data path width parameter
  111. */
  112. static int pcmcia_io_cfg_data_width(unsigned int flags)
  113. {
  114. if (!(flags & CISTPL_IO_8BIT))
  115. return IO_DATA_PATH_WIDTH_16;
  116. if (!(flags & CISTPL_IO_16BIT))
  117. return IO_DATA_PATH_WIDTH_8;
  118. return IO_DATA_PATH_WIDTH_AUTO;
  119. }
  120. struct pcmcia_cfg_mem {
  121. struct pcmcia_device *p_dev;
  122. int (*conf_check) (struct pcmcia_device *p_dev, void *priv_data);
  123. void *priv_data;
  124. cisparse_t parse;
  125. cistpl_cftable_entry_t dflt;
  126. };
  127. /**
  128. * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
  129. *
  130. * pcmcia_do_loop_config() is the internal callback for the call from
  131. * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
  132. * by a struct pcmcia_cfg_mem.
  133. */
  134. static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
  135. {
  136. struct pcmcia_cfg_mem *cfg_mem = priv;
  137. struct pcmcia_device *p_dev = cfg_mem->p_dev;
  138. cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
  139. cistpl_cftable_entry_t *dflt = &cfg_mem->dflt;
  140. unsigned int flags = p_dev->config_flags;
  141. unsigned int vcc = p_dev->socket->socket.Vcc;
  142. dev_dbg(&p_dev->dev, "testing configuration %x, autoconf %x\n",
  143. cfg->index, flags);
  144. /* default values */
  145. cfg_mem->p_dev->config_index = cfg->index;
  146. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  147. cfg_mem->dflt = *cfg;
  148. /* check for matching Vcc? */
  149. if (flags & CONF_AUTO_CHECK_VCC) {
  150. if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  151. if (vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000)
  152. return -ENODEV;
  153. } else if (dflt->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  154. if (vcc != dflt->vcc.param[CISTPL_POWER_VNOM] / 10000)
  155. return -ENODEV;
  156. }
  157. }
  158. /* set Vpp? */
  159. if (flags & CONF_AUTO_SET_VPP) {
  160. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
  161. p_dev->vpp = cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  162. else if (dflt->vpp1.present & (1 << CISTPL_POWER_VNOM))
  163. p_dev->vpp =
  164. dflt->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  165. }
  166. /* enable audio? */
  167. if ((flags & CONF_AUTO_AUDIO) && (cfg->flags & CISTPL_CFTABLE_AUDIO))
  168. p_dev->config_flags |= CONF_ENABLE_SPKR;
  169. /* IO window settings? */
  170. if (flags & CONF_AUTO_SET_IO) {
  171. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
  172. int i = 0;
  173. p_dev->resource[0]->start = p_dev->resource[0]->end = 0;
  174. p_dev->resource[1]->start = p_dev->resource[1]->end = 0;
  175. if (io->nwin == 0)
  176. return -ENODEV;
  177. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  178. p_dev->resource[0]->flags |=
  179. pcmcia_io_cfg_data_width(io->flags);
  180. if (io->nwin > 1) {
  181. /* For multifunction cards, by convention, we
  182. * configure the network function with window 0,
  183. * and serial with window 1 */
  184. i = (io->win[1].len > io->win[0].len);
  185. p_dev->resource[1]->flags = p_dev->resource[0]->flags;
  186. p_dev->resource[1]->start = io->win[1-i].base;
  187. p_dev->resource[1]->end = io->win[1-i].len;
  188. }
  189. p_dev->resource[0]->start = io->win[i].base;
  190. p_dev->resource[0]->end = io->win[i].len;
  191. p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
  192. }
  193. /* MEM window settings? */
  194. if (flags & CONF_AUTO_SET_IOMEM) {
  195. /* so far, we only set one memory window */
  196. cistpl_mem_t *mem = (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
  197. p_dev->resource[2]->start = p_dev->resource[2]->end = 0;
  198. if (mem->nwin == 0)
  199. return -ENODEV;
  200. p_dev->resource[2]->start = mem->win[0].host_addr;
  201. p_dev->resource[2]->end = mem->win[0].len;
  202. if (p_dev->resource[2]->end < 0x1000)
  203. p_dev->resource[2]->end = 0x1000;
  204. p_dev->card_addr = mem->win[0].card_addr;
  205. }
  206. dev_dbg(&p_dev->dev,
  207. "checking configuration %x: %pr %pr %pr (%d lines)\n",
  208. p_dev->config_index, p_dev->resource[0], p_dev->resource[1],
  209. p_dev->resource[2], p_dev->io_lines);
  210. return cfg_mem->conf_check(p_dev, cfg_mem->priv_data);
  211. }
  212. /**
  213. * pcmcia_loop_config() - loop over configuration options
  214. * @p_dev: the struct pcmcia_device which we need to loop for.
  215. * @conf_check: function to call for each configuration option.
  216. * It gets passed the struct pcmcia_device and private data
  217. * being passed to pcmcia_loop_config()
  218. * @priv_data: private data to be passed to the conf_check function.
  219. *
  220. * pcmcia_loop_config() loops over all configuration options, and calls
  221. * the driver-specific conf_check() for each one, checking whether
  222. * it is a valid one. Returns 0 on success or errorcode otherwise.
  223. */
  224. int pcmcia_loop_config(struct pcmcia_device *p_dev,
  225. int (*conf_check) (struct pcmcia_device *p_dev,
  226. void *priv_data),
  227. void *priv_data)
  228. {
  229. struct pcmcia_cfg_mem *cfg_mem;
  230. int ret;
  231. cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
  232. if (cfg_mem == NULL)
  233. return -ENOMEM;
  234. cfg_mem->p_dev = p_dev;
  235. cfg_mem->conf_check = conf_check;
  236. cfg_mem->priv_data = priv_data;
  237. ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
  238. CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
  239. cfg_mem, pcmcia_do_loop_config);
  240. kfree(cfg_mem);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL(pcmcia_loop_config);
  244. struct pcmcia_loop_mem {
  245. struct pcmcia_device *p_dev;
  246. void *priv_data;
  247. int (*loop_tuple) (struct pcmcia_device *p_dev,
  248. tuple_t *tuple,
  249. void *priv_data);
  250. };
  251. /**
  252. * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
  253. *
  254. * pcmcia_do_loop_tuple() is the internal callback for the call from
  255. * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
  256. * by a struct pcmcia_cfg_mem.
  257. */
  258. static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
  259. {
  260. struct pcmcia_loop_mem *loop = priv;
  261. return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
  262. };
  263. /**
  264. * pcmcia_loop_tuple() - loop over tuples in the CIS
  265. * @p_dev: the struct pcmcia_device which we need to loop for.
  266. * @code: which CIS code shall we look for?
  267. * @priv_data: private data to be passed to the loop_tuple function.
  268. * @loop_tuple: function to call for each CIS entry of type @function. IT
  269. * gets passed the raw tuple and @priv_data.
  270. *
  271. * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
  272. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  273. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  274. */
  275. int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  276. int (*loop_tuple) (struct pcmcia_device *p_dev,
  277. tuple_t *tuple,
  278. void *priv_data),
  279. void *priv_data)
  280. {
  281. struct pcmcia_loop_mem loop = {
  282. .p_dev = p_dev,
  283. .loop_tuple = loop_tuple,
  284. .priv_data = priv_data};
  285. return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
  286. &loop, pcmcia_do_loop_tuple);
  287. }
  288. EXPORT_SYMBOL(pcmcia_loop_tuple);
  289. struct pcmcia_loop_get {
  290. size_t len;
  291. cisdata_t **buf;
  292. };
  293. /**
  294. * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
  295. *
  296. * pcmcia_do_get_tuple() is the internal callback for the call from
  297. * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
  298. * the first tuple, return 0 unconditionally. Create a memory buffer large
  299. * enough to hold the content of the tuple, and fill it with the tuple data.
  300. * The caller is responsible to free the buffer.
  301. */
  302. static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
  303. void *priv)
  304. {
  305. struct pcmcia_loop_get *get = priv;
  306. *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
  307. if (*get->buf) {
  308. get->len = tuple->TupleDataLen;
  309. memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
  310. } else
  311. dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
  312. return 0;
  313. }
  314. /**
  315. * pcmcia_get_tuple() - get first tuple from CIS
  316. * @p_dev: the struct pcmcia_device which we need to loop for.
  317. * @code: which CIS code shall we look for?
  318. * @buf: pointer to store the buffer to.
  319. *
  320. * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
  321. * It returns the buffer length (or zero). The caller is responsible to free
  322. * the buffer passed in @buf.
  323. */
  324. size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  325. unsigned char **buf)
  326. {
  327. struct pcmcia_loop_get get = {
  328. .len = 0,
  329. .buf = buf,
  330. };
  331. *get.buf = NULL;
  332. pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
  333. return get.len;
  334. }
  335. EXPORT_SYMBOL(pcmcia_get_tuple);
  336. /**
  337. * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
  338. *
  339. * pcmcia_do_get_mac() is the internal callback for the call from
  340. * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
  341. * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
  342. * to struct net_device->dev_addr[i].
  343. */
  344. static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
  345. void *priv)
  346. {
  347. struct net_device *dev = priv;
  348. int i;
  349. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  350. return -EINVAL;
  351. if (tuple->TupleDataLen < ETH_ALEN + 2) {
  352. dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
  353. "LAN_NODE_ID\n");
  354. return -EINVAL;
  355. }
  356. if (tuple->TupleData[1] != ETH_ALEN) {
  357. dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
  358. return -EINVAL;
  359. }
  360. for (i = 0; i < 6; i++)
  361. dev->dev_addr[i] = tuple->TupleData[i+2];
  362. return 0;
  363. }
  364. /**
  365. * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
  366. * @p_dev: the struct pcmcia_device for which we want the address.
  367. * @dev: a properly prepared struct net_device to store the info to.
  368. *
  369. * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
  370. * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
  371. * must be set up properly by the driver (see examples!).
  372. */
  373. int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
  374. {
  375. return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
  376. }
  377. EXPORT_SYMBOL(pcmcia_get_mac_from_cis);