2 * linux/drivers/mmc/core/sdio_cis.c
4 * Author: Nicolas Pitre
5 * Created: June 11, 2007
6 * Copyright: MontaVista Software Inc.
8 * Copyright 2007 Pierre Ossman
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.
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/sdio.h>
22 #include <linux/mmc/sdio_func.h>
27 #define SDIO_READ_CIS_TIMEOUT_MS (10 * 1000) /* 10s */
29 static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
30 const unsigned char *buf, unsigned size)
32 unsigned i, nr_strings;
33 char **buffer, *string;
38 /* Find all null-terminated (including zero length) strings in
39 the TPLLV1_INFO field. Trailing garbage is ignored. */
44 for (i = 0; i < size; i++) {
55 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
59 string = (char*)(buffer + nr_strings);
61 for (i = 0; i < nr_strings; i++) {
64 string += strlen(string) + 1;
65 buf += strlen(buf) + 1;
69 func->num_info = nr_strings;
70 func->info = (const char**)buffer;
72 card->num_info = nr_strings;
73 card->info = (const char**)buffer;
79 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
80 const unsigned char *buf, unsigned size)
82 unsigned int vendor, device;
85 vendor = buf[0] | (buf[1] << 8);
88 device = buf[2] | (buf[3] << 8);
91 func->vendor = vendor;
92 func->device = device;
94 card->cis.vendor = vendor;
95 card->cis.device = device;
101 static const unsigned char speed_val[16] =
102 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
103 static const unsigned int speed_unit[8] =
104 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
107 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
108 const unsigned char *, unsigned);
112 unsigned char min_size;
116 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
117 const char *tpl_descr,
118 const struct cis_tpl *tpl, int tpl_count,
120 const unsigned char *buf, unsigned size)
124 /* look for a matching code in the table */
125 for (i = 0; i < tpl_count; i++, tpl++) {
126 if (tpl->code == code)
130 if (size >= tpl->min_size) {
132 ret = tpl->parse(card, func, buf, size);
134 ret = -EILSEQ; /* known tuple, not parsed */
139 if (ret && ret != -EILSEQ && ret != -ENOENT) {
140 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
141 mmc_hostname(card->host), tpl_descr, code, size);
151 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
152 const unsigned char *buf, unsigned size)
154 /* Only valid for the common CIS (function 0) */
158 /* TPLFE_FN0_BLK_SIZE */
159 card->cis.blksize = buf[1] | (buf[2] << 8);
161 /* TPLFE_MAX_TRAN_SPEED */
162 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
163 speed_unit[buf[3] & 7];
168 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
169 const unsigned char *buf, unsigned size)
174 /* Only valid for the individual function's CIS (1-7) */
179 * This tuple has a different length depending on the SDIO spec
182 vsn = func->card->cccr.sdio_vsn;
183 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
185 if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
186 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
187 mmc_hostname(card->host));
188 vsn = SDIO_SDIO_REV_1_00;
189 } else if (size < min_size) {
193 /* TPLFE_MAX_BLK_SIZE */
194 func->max_blksize = buf[12] | (buf[13] << 8);
196 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
197 if (vsn > SDIO_SDIO_REV_1_00)
198 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
200 func->enable_timeout = jiffies_to_msecs(HZ);
206 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
208 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
209 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
210 * TPLFID_FUNCTION is always hardcoded to 0x0C.
212 static const struct cis_tpl cis_tpl_funce_list[] = {
213 { 0x00, 4, cistpl_funce_common },
214 { 0x01, 0, cistpl_funce_func },
215 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
218 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
219 const unsigned char *buf, unsigned size)
224 return cis_tpl_parse(card, func, "CISTPL_FUNCE",
226 ARRAY_SIZE(cis_tpl_funce_list),
230 /* Known TPL_CODEs table for CIS tuples */
231 static const struct cis_tpl cis_tpl_list[] = {
232 { 0x15, 3, cistpl_vers_1 },
233 { 0x20, 4, cistpl_manfid },
234 { 0x21, 2, /* cistpl_funcid */ },
235 { 0x22, 0, cistpl_funce },
236 { 0x91, 2, /* cistpl_sdio_std */ },
239 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
242 struct sdio_func_tuple *this, **prev;
246 * Note that this works for the common CIS (function number 0) as
247 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
248 * have the same offset.
250 for (i = 0; i < 3; i++) {
258 ret = mmc_io_rw_direct(card, 0, 0,
259 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
266 prev = &func->tuples;
268 prev = &card->tuples;
274 unsigned char tpl_code, tpl_link;
275 unsigned long timeout = jiffies +
276 msecs_to_jiffies(SDIO_READ_CIS_TIMEOUT_MS);
278 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
282 /* 0xff means we're done */
283 if (tpl_code == 0xff)
286 /* null entries have no link field or data */
287 if (tpl_code == 0x00)
290 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
294 /* a size of 0xff also means we're done */
295 if (tpl_link == 0xff)
298 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
302 for (i = 0; i < tpl_link; i++) {
303 ret = mmc_io_rw_direct(card, 0, 0,
304 ptr + i, 0, &this->data[i]);
313 /* Try to parse the CIS tuple */
314 ret = cis_tpl_parse(card, func, "CIS",
315 cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
316 tpl_code, this->data, tpl_link);
317 if (ret == -EILSEQ || ret == -ENOENT) {
319 * The tuple is unknown or known but not parsed.
320 * Queue the tuple for the function driver.
323 this->code = tpl_code;
324 this->size = tpl_link;
328 if (ret == -ENOENT) {
329 if (time_after(jiffies, timeout))
331 /* warn about unknown tuples */
332 pr_warn_ratelimited("%s: queuing unknown"
333 " CIS tuple 0x%02x (%u bytes)\n",
334 mmc_hostname(card->host),
338 /* keep on analyzing tuples */
342 * We don't need the tuple anymore if it was
343 * successfully parsed by the SDIO core or if it is
344 * not going to be queued for a driver.
353 * Link in all unknown tuples found in the common CIS so that
354 * drivers don't have to go digging in two places.
357 *prev = card->tuples;
362 int sdio_read_common_cis(struct mmc_card *card)
364 return sdio_read_cis(card, NULL);
367 void sdio_free_common_cis(struct mmc_card *card)
369 struct sdio_func_tuple *tuple, *victim;
371 tuple = card->tuples;
382 int sdio_read_func_cis(struct sdio_func *func)
386 ret = sdio_read_cis(func->card, func);
391 * Since we've linked to tuples in the card structure,
392 * we must make sure we have a reference to it.
394 get_device(&func->card->dev);
397 * Vendor/device id is optional for function CIS, so
398 * copy it from the card structure as needed.
400 if (func->vendor == 0) {
401 func->vendor = func->card->cis.vendor;
402 func->device = func->card->cis.device;
408 void sdio_free_func_cis(struct sdio_func *func)
410 struct sdio_func_tuple *tuple, *victim;
412 tuple = func->tuples;
414 while (tuple && tuple != func->card->tuples) {
423 * We have now removed the link to the tuples in the
424 * card structure, so remove the reference.
426 put_device(&func->card->dev);