sdio_cis.c 9.0 KB

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