sdio_cis.c 8.4 KB

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