sdio_cis.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * linux/drivers/mmc/core/sdio_cis.c
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: June 11, 2007
  6. * Copyright: MontaVista Software Inc.
  7. *
  8. * Copyright 2007 Pierre Ossman
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/mmc/host.h>
  18. #include <linux/mmc/card.h>
  19. #include <linux/mmc/sdio.h>
  20. #include <linux/mmc/sdio_func.h>
  21. #include "sdio_cis.h"
  22. #include "sdio_ops.h"
  23. #define SDIO_READ_CIS_TIMEOUT_MS (10 * 1000) /* 10s */
  24. static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
  25. const unsigned char *buf, unsigned size)
  26. {
  27. unsigned i, nr_strings;
  28. char **buffer, *string;
  29. if (size < 2)
  30. return 0;
  31. /* Find all null-terminated (including zero length) strings in
  32. the TPLLV1_INFO field. Trailing garbage is ignored. */
  33. buf += 2;
  34. size -= 2;
  35. nr_strings = 0;
  36. for (i = 0; i < size; i++) {
  37. if (buf[i] == 0xff)
  38. break;
  39. if (buf[i] == 0)
  40. nr_strings++;
  41. }
  42. if (nr_strings == 0)
  43. return 0;
  44. size = i;
  45. buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
  46. if (!buffer)
  47. return -ENOMEM;
  48. string = (char*)(buffer + nr_strings);
  49. for (i = 0; i < nr_strings; i++) {
  50. buffer[i] = string;
  51. strcpy(string, buf);
  52. string += strlen(string) + 1;
  53. buf += strlen(buf) + 1;
  54. }
  55. if (func) {
  56. func->num_info = nr_strings;
  57. func->info = (const char**)buffer;
  58. } else {
  59. card->num_info = nr_strings;
  60. card->info = (const char**)buffer;
  61. }
  62. return 0;
  63. }
  64. static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
  65. const unsigned char *buf, unsigned size)
  66. {
  67. unsigned int vendor, device;
  68. /* TPLMID_MANF */
  69. vendor = buf[0] | (buf[1] << 8);
  70. /* TPLMID_CARD */
  71. device = buf[2] | (buf[3] << 8);
  72. if (func) {
  73. func->vendor = vendor;
  74. func->device = device;
  75. } else {
  76. card->cis.vendor = vendor;
  77. card->cis.device = device;
  78. }
  79. return 0;
  80. }
  81. static const unsigned char speed_val[16] =
  82. { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
  83. static const unsigned int speed_unit[8] =
  84. { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
  85. typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
  86. const unsigned char *, unsigned);
  87. struct cis_tpl {
  88. unsigned char code;
  89. unsigned char min_size;
  90. tpl_parse_t *parse;
  91. };
  92. static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
  93. const char *tpl_descr,
  94. const struct cis_tpl *tpl, int tpl_count,
  95. unsigned char code,
  96. const unsigned char *buf, unsigned size)
  97. {
  98. int i, ret;
  99. /* look for a matching code in the table */
  100. for (i = 0; i < tpl_count; i++, tpl++) {
  101. if (tpl->code == code)
  102. break;
  103. }
  104. if (i < tpl_count) {
  105. if (size >= tpl->min_size) {
  106. if (tpl->parse)
  107. ret = tpl->parse(card, func, buf, size);
  108. else
  109. ret = -EILSEQ; /* known tuple, not parsed */
  110. } else {
  111. /* invalid tuple */
  112. ret = -EINVAL;
  113. }
  114. if (ret && ret != -EILSEQ && ret != -ENOENT) {
  115. pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
  116. mmc_hostname(card->host), tpl_descr, code, size);
  117. }
  118. } else {
  119. /* unknown tuple */
  120. ret = -ENOENT;
  121. }
  122. return ret;
  123. }
  124. static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
  125. const unsigned char *buf, unsigned size)
  126. {
  127. /* Only valid for the common CIS (function 0) */
  128. if (func)
  129. return -EINVAL;
  130. /* TPLFE_FN0_BLK_SIZE */
  131. card->cis.blksize = buf[1] | (buf[2] << 8);
  132. /* TPLFE_MAX_TRAN_SPEED */
  133. card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
  134. speed_unit[buf[3] & 7];
  135. return 0;
  136. }
  137. static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
  138. const unsigned char *buf, unsigned size)
  139. {
  140. unsigned vsn;
  141. unsigned min_size;
  142. /* Only valid for the individual function's CIS (1-7) */
  143. if (!func)
  144. return -EINVAL;
  145. /*
  146. * This tuple has a different length depending on the SDIO spec
  147. * version.
  148. */
  149. vsn = func->card->cccr.sdio_vsn;
  150. min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
  151. if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
  152. pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
  153. mmc_hostname(card->host));
  154. vsn = SDIO_SDIO_REV_1_00;
  155. } else if (size < min_size) {
  156. return -EINVAL;
  157. }
  158. /* TPLFE_MAX_BLK_SIZE */
  159. func->max_blksize = buf[12] | (buf[13] << 8);
  160. /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
  161. if (vsn > SDIO_SDIO_REV_1_00)
  162. func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
  163. else
  164. func->enable_timeout = jiffies_to_msecs(HZ);
  165. return 0;
  166. }
  167. /*
  168. * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
  169. *
  170. * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
  171. * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
  172. * TPLFID_FUNCTION is always hardcoded to 0x0C.
  173. */
  174. static const struct cis_tpl cis_tpl_funce_list[] = {
  175. { 0x00, 4, cistpl_funce_common },
  176. { 0x01, 0, cistpl_funce_func },
  177. { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
  178. };
  179. static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
  180. const unsigned char *buf, unsigned size)
  181. {
  182. if (size < 1)
  183. return -EINVAL;
  184. return cis_tpl_parse(card, func, "CISTPL_FUNCE",
  185. cis_tpl_funce_list,
  186. ARRAY_SIZE(cis_tpl_funce_list),
  187. buf[0], buf, size);
  188. }
  189. /* Known TPL_CODEs table for CIS tuples */
  190. static const struct cis_tpl cis_tpl_list[] = {
  191. { 0x15, 3, cistpl_vers_1 },
  192. { 0x20, 4, cistpl_manfid },
  193. { 0x21, 2, /* cistpl_funcid */ },
  194. { 0x22, 0, cistpl_funce },
  195. { 0x91, 2, /* cistpl_sdio_std */ },
  196. };
  197. static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
  198. {
  199. int ret;
  200. struct sdio_func_tuple *this, **prev;
  201. unsigned i, ptr = 0;
  202. /*
  203. * Note that this works for the common CIS (function number 0) as
  204. * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
  205. * have the same offset.
  206. */
  207. for (i = 0; i < 3; i++) {
  208. unsigned char x, fn;
  209. if (func)
  210. fn = func->num;
  211. else
  212. fn = 0;
  213. ret = mmc_io_rw_direct(card, 0, 0,
  214. SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
  215. if (ret)
  216. return ret;
  217. ptr |= x << (i * 8);
  218. }
  219. if (func)
  220. prev = &func->tuples;
  221. else
  222. prev = &card->tuples;
  223. if (*prev)
  224. return -EINVAL;
  225. do {
  226. unsigned char tpl_code, tpl_link;
  227. unsigned long timeout = jiffies +
  228. msecs_to_jiffies(SDIO_READ_CIS_TIMEOUT_MS);
  229. ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
  230. if (ret)
  231. break;
  232. /* 0xff means we're done */
  233. if (tpl_code == 0xff)
  234. break;
  235. /* null entries have no link field or data */
  236. if (tpl_code == 0x00)
  237. continue;
  238. ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
  239. if (ret)
  240. break;
  241. /* a size of 0xff also means we're done */
  242. if (tpl_link == 0xff)
  243. break;
  244. this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
  245. if (!this)
  246. return -ENOMEM;
  247. for (i = 0; i < tpl_link; i++) {
  248. ret = mmc_io_rw_direct(card, 0, 0,
  249. ptr + i, 0, &this->data[i]);
  250. if (ret)
  251. break;
  252. }
  253. if (ret) {
  254. kfree(this);
  255. break;
  256. }
  257. /* Try to parse the CIS tuple */
  258. ret = cis_tpl_parse(card, func, "CIS",
  259. cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
  260. tpl_code, this->data, tpl_link);
  261. if (ret == -EILSEQ || ret == -ENOENT) {
  262. /*
  263. * The tuple is unknown or known but not parsed.
  264. * Queue the tuple for the function driver.
  265. */
  266. this->next = NULL;
  267. this->code = tpl_code;
  268. this->size = tpl_link;
  269. *prev = this;
  270. prev = &this->next;
  271. if (ret == -ENOENT) {
  272. if (time_after(jiffies, timeout))
  273. break;
  274. /* warn about unknown tuples */
  275. pr_warn_ratelimited("%s: queuing unknown"
  276. " CIS tuple 0x%02x (%u bytes)\n",
  277. mmc_hostname(card->host),
  278. tpl_code, tpl_link);
  279. }
  280. /* keep on analyzing tuples */
  281. ret = 0;
  282. } else {
  283. /*
  284. * We don't need the tuple anymore if it was
  285. * successfully parsed by the SDIO core or if it is
  286. * not going to be queued for a driver.
  287. */
  288. kfree(this);
  289. }
  290. ptr += tpl_link;
  291. } while (!ret);
  292. /*
  293. * Link in all unknown tuples found in the common CIS so that
  294. * drivers don't have to go digging in two places.
  295. */
  296. if (func)
  297. *prev = card->tuples;
  298. return ret;
  299. }
  300. int sdio_read_common_cis(struct mmc_card *card)
  301. {
  302. return sdio_read_cis(card, NULL);
  303. }
  304. void sdio_free_common_cis(struct mmc_card *card)
  305. {
  306. struct sdio_func_tuple *tuple, *victim;
  307. tuple = card->tuples;
  308. while (tuple) {
  309. victim = tuple;
  310. tuple = tuple->next;
  311. kfree(victim);
  312. }
  313. card->tuples = NULL;
  314. }
  315. int sdio_read_func_cis(struct sdio_func *func)
  316. {
  317. int ret;
  318. ret = sdio_read_cis(func->card, func);
  319. if (ret)
  320. return ret;
  321. /*
  322. * Since we've linked to tuples in the card structure,
  323. * we must make sure we have a reference to it.
  324. */
  325. get_device(&func->card->dev);
  326. /*
  327. * Vendor/device id is optional for function CIS, so
  328. * copy it from the card structure as needed.
  329. */
  330. if (func->vendor == 0) {
  331. func->vendor = func->card->cis.vendor;
  332. func->device = func->card->cis.device;
  333. }
  334. return 0;
  335. }
  336. void sdio_free_func_cis(struct sdio_func *func)
  337. {
  338. struct sdio_func_tuple *tuple, *victim;
  339. tuple = func->tuples;
  340. while (tuple && tuple != func->card->tuples) {
  341. victim = tuple;
  342. tuple = tuple->next;
  343. kfree(victim);
  344. }
  345. func->tuples = NULL;
  346. /*
  347. * We have now removed the link to the tuples in the
  348. * card structure, so remove the reference.
  349. */
  350. put_device(&func->card->dev);
  351. }