sdio_cis.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. buf += 2;
  28. size -= 2;
  29. nr_strings = 0;
  30. for (i = 0; i < size; i++) {
  31. if (buf[i] == 0xff)
  32. break;
  33. if (buf[i] == 0)
  34. nr_strings++;
  35. }
  36. if (buf[i-1] != '\0') {
  37. printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
  38. return 0;
  39. }
  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. static int cistpl_funce_common(struct mmc_card *card,
  82. const unsigned char *buf, unsigned size)
  83. {
  84. if (size < 0x04 || buf[0] != 0)
  85. return -EINVAL;
  86. /* TPLFE_FN0_BLK_SIZE */
  87. card->cis.blksize = buf[1] | (buf[2] << 8);
  88. /* TPLFE_MAX_TRAN_SPEED */
  89. card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
  90. speed_unit[buf[3] & 7];
  91. return 0;
  92. }
  93. static int cistpl_funce_func(struct sdio_func *func,
  94. const unsigned char *buf, unsigned size)
  95. {
  96. unsigned vsn;
  97. unsigned min_size;
  98. vsn = func->card->cccr.sdio_vsn;
  99. min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
  100. if (size < min_size || buf[0] != 1)
  101. return -EINVAL;
  102. /* TPLFE_MAX_BLK_SIZE */
  103. func->max_blksize = buf[12] | (buf[13] << 8);
  104. /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
  105. if (vsn > SDIO_SDIO_REV_1_00)
  106. func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
  107. else
  108. func->enable_timeout = jiffies_to_msecs(HZ);
  109. return 0;
  110. }
  111. static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
  112. const unsigned char *buf, unsigned size)
  113. {
  114. int ret;
  115. /*
  116. * There should be two versions of the CISTPL_FUNCE tuple,
  117. * one for the common CIS (function 0) and a version used by
  118. * the individual function's CIS (1-7). Yet, the later has a
  119. * different length depending on the SDIO spec version.
  120. */
  121. if (func)
  122. ret = cistpl_funce_func(func, buf, size);
  123. else
  124. ret = cistpl_funce_common(card, buf, size);
  125. if (ret) {
  126. printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
  127. "type %u\n", mmc_hostname(card->host), size, buf[0]);
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
  133. const unsigned char *, unsigned);
  134. struct cis_tpl {
  135. unsigned char code;
  136. unsigned char min_size;
  137. tpl_parse_t *parse;
  138. };
  139. static const struct cis_tpl cis_tpl_list[] = {
  140. { 0x15, 3, cistpl_vers_1 },
  141. { 0x20, 4, cistpl_manfid },
  142. { 0x21, 2, /* cistpl_funcid */ },
  143. { 0x22, 0, cistpl_funce },
  144. };
  145. static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
  146. {
  147. int ret;
  148. struct sdio_func_tuple *this, **prev;
  149. unsigned i, ptr = 0;
  150. /*
  151. * Note that this works for the common CIS (function number 0) as
  152. * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
  153. * have the same offset.
  154. */
  155. for (i = 0; i < 3; i++) {
  156. unsigned char x, fn;
  157. if (func)
  158. fn = func->num;
  159. else
  160. fn = 0;
  161. ret = mmc_io_rw_direct(card, 0, 0,
  162. SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
  163. if (ret)
  164. return ret;
  165. ptr |= x << (i * 8);
  166. }
  167. if (func)
  168. prev = &func->tuples;
  169. else
  170. prev = &card->tuples;
  171. BUG_ON(*prev);
  172. do {
  173. unsigned char tpl_code, tpl_link;
  174. ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
  175. if (ret)
  176. break;
  177. /* 0xff means we're done */
  178. if (tpl_code == 0xff)
  179. break;
  180. ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
  181. if (ret)
  182. break;
  183. this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
  184. if (!this)
  185. return -ENOMEM;
  186. for (i = 0; i < tpl_link; i++) {
  187. ret = mmc_io_rw_direct(card, 0, 0,
  188. ptr + i, 0, &this->data[i]);
  189. if (ret)
  190. break;
  191. }
  192. if (ret) {
  193. kfree(this);
  194. break;
  195. }
  196. for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
  197. if (cis_tpl_list[i].code == tpl_code)
  198. break;
  199. if (i >= ARRAY_SIZE(cis_tpl_list)) {
  200. /* this tuple is unknown to the core */
  201. this->next = NULL;
  202. this->code = tpl_code;
  203. this->size = tpl_link;
  204. *prev = this;
  205. prev = &this->next;
  206. printk(KERN_DEBUG
  207. "%s: queuing CIS tuple 0x%02x length %u\n",
  208. mmc_hostname(card->host), tpl_code, tpl_link);
  209. } else {
  210. const struct cis_tpl *tpl = cis_tpl_list + i;
  211. if (tpl_link < tpl->min_size) {
  212. printk(KERN_ERR
  213. "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
  214. mmc_hostname(card->host),
  215. tpl_code, tpl_link, tpl->min_size);
  216. ret = -EINVAL;
  217. } else if (tpl->parse) {
  218. ret = tpl->parse(card, func,
  219. this->data, tpl_link);
  220. }
  221. kfree(this);
  222. }
  223. ptr += tpl_link;
  224. } while (!ret);
  225. /*
  226. * Link in all unknown tuples found in the common CIS so that
  227. * drivers don't have to go digging in two places.
  228. */
  229. if (func)
  230. *prev = card->tuples;
  231. return ret;
  232. }
  233. int sdio_read_common_cis(struct mmc_card *card)
  234. {
  235. return sdio_read_cis(card, NULL);
  236. }
  237. void sdio_free_common_cis(struct mmc_card *card)
  238. {
  239. struct sdio_func_tuple *tuple, *victim;
  240. tuple = card->tuples;
  241. while (tuple) {
  242. victim = tuple;
  243. tuple = tuple->next;
  244. kfree(victim);
  245. }
  246. card->tuples = NULL;
  247. }
  248. int sdio_read_func_cis(struct sdio_func *func)
  249. {
  250. int ret;
  251. ret = sdio_read_cis(func->card, func);
  252. if (ret)
  253. return ret;
  254. /*
  255. * Since we've linked to tuples in the card structure,
  256. * we must make sure we have a reference to it.
  257. */
  258. get_device(&func->card->dev);
  259. /*
  260. * Vendor/device id is optional for function CIS, so
  261. * copy it from the card structure as needed.
  262. */
  263. if (func->vendor == 0) {
  264. func->vendor = func->card->cis.vendor;
  265. func->device = func->card->cis.device;
  266. }
  267. return 0;
  268. }
  269. void sdio_free_func_cis(struct sdio_func *func)
  270. {
  271. struct sdio_func_tuple *tuple, *victim;
  272. tuple = func->tuples;
  273. while (tuple && tuple != func->card->tuples) {
  274. victim = tuple;
  275. tuple = tuple->next;
  276. kfree(victim);
  277. }
  278. func->tuples = NULL;
  279. /*
  280. * We have now removed the link to the tuples in the
  281. * card structure, so remove the reference.
  282. */
  283. put_device(&func->card->dev);
  284. }