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