1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/mmc/core/sdio_cis.c
5 * Author: Nicolas Pitre
6 * Created: June 11, 2007
7 * Copyright: MontaVista Software Inc.
9 * Copyright 2007 Pierre Ossman
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
23 #define SDIO_READ_CIS_TIMEOUT_MS (10 * 1000) /* 10s */
25 static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
26 const unsigned char *buf, unsigned size)
28 unsigned i, nr_strings;
29 char **buffer, *string;
34 /* Find all null-terminated (including zero length) strings in
35 the TPLLV1_INFO field. Trailing garbage is ignored. */
40 for (i = 0; i < size; i++) {
51 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
55 string = (char*)(buffer + nr_strings);
57 for (i = 0; i < nr_strings; i++) {
60 string += strlen(string) + 1;
61 buf += strlen(buf) + 1;
65 func->num_info = nr_strings;
66 func->info = (const char**)buffer;
68 card->num_info = nr_strings;
69 card->info = (const char**)buffer;
75 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
76 const unsigned char *buf, unsigned size)
78 unsigned int vendor, device;
81 vendor = buf[0] | (buf[1] << 8);
84 device = buf[2] | (buf[3] << 8);
87 func->vendor = vendor;
88 func->device = device;
90 card->cis.vendor = vendor;
91 card->cis.device = device;
97 static const unsigned char speed_val[16] =
98 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
99 static const unsigned int speed_unit[8] =
100 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
103 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
104 const unsigned char *, unsigned);
108 unsigned char min_size;
112 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
113 const char *tpl_descr,
114 const struct cis_tpl *tpl, int tpl_count,
116 const unsigned char *buf, unsigned size)
120 /* look for a matching code in the table */
121 for (i = 0; i < tpl_count; i++, tpl++) {
122 if (tpl->code == code)
126 if (size >= tpl->min_size) {
128 ret = tpl->parse(card, func, buf, size);
130 ret = -EILSEQ; /* known tuple, not parsed */
135 if (ret && ret != -EILSEQ && ret != -ENOENT) {
136 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
137 mmc_hostname(card->host), tpl_descr, code, size);
147 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
148 const unsigned char *buf, unsigned size)
150 /* Only valid for the common CIS (function 0) */
154 /* TPLFE_FN0_BLK_SIZE */
155 card->cis.blksize = buf[1] | (buf[2] << 8);
157 /* TPLFE_MAX_TRAN_SPEED */
158 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
159 speed_unit[buf[3] & 7];
164 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
165 const unsigned char *buf, unsigned size)
170 /* Only valid for the individual function's CIS (1-7) */
175 * This tuple has a different length depending on the SDIO spec
178 vsn = func->card->cccr.sdio_vsn;
179 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
181 if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
182 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
183 mmc_hostname(card->host));
184 vsn = SDIO_SDIO_REV_1_00;
185 } else if (size < min_size) {
189 /* TPLFE_MAX_BLK_SIZE */
190 func->max_blksize = buf[12] | (buf[13] << 8);
192 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
193 if (vsn > SDIO_SDIO_REV_1_00)
194 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
196 func->enable_timeout = jiffies_to_msecs(HZ);
202 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
204 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
205 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
206 * TPLFID_FUNCTION is always hardcoded to 0x0C.
208 static const struct cis_tpl cis_tpl_funce_list[] = {
209 { 0x00, 4, cistpl_funce_common },
210 { 0x01, 0, cistpl_funce_func },
211 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
214 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
215 const unsigned char *buf, unsigned size)
220 return cis_tpl_parse(card, func, "CISTPL_FUNCE",
222 ARRAY_SIZE(cis_tpl_funce_list),
226 /* Known TPL_CODEs table for CIS tuples */
227 static const struct cis_tpl cis_tpl_list[] = {
228 { 0x15, 3, cistpl_vers_1 },
229 { 0x20, 4, cistpl_manfid },
230 { 0x21, 2, /* cistpl_funcid */ },
231 { 0x22, 0, cistpl_funce },
232 { 0x91, 2, /* cistpl_sdio_std */ },
235 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
238 struct sdio_func_tuple *this, **prev;
242 * Note that this works for the common CIS (function number 0) as
243 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
244 * have the same offset.
246 for (i = 0; i < 3; i++) {
254 ret = mmc_io_rw_direct(card, 0, 0,
255 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
262 prev = &func->tuples;
264 prev = &card->tuples;
270 unsigned char tpl_code, tpl_link;
271 unsigned long timeout = jiffies +
272 msecs_to_jiffies(SDIO_READ_CIS_TIMEOUT_MS);
274 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
278 /* 0xff means we're done */
279 if (tpl_code == 0xff)
282 /* null entries have no link field or data */
283 if (tpl_code == 0x00)
286 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
290 /* a size of 0xff also means we're done */
291 if (tpl_link == 0xff)
294 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
298 for (i = 0; i < tpl_link; i++) {
299 ret = mmc_io_rw_direct(card, 0, 0,
300 ptr + i, 0, &this->data[i]);
309 /* Try to parse the CIS tuple */
310 ret = cis_tpl_parse(card, func, "CIS",
311 cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
312 tpl_code, this->data, tpl_link);
313 if (ret == -EILSEQ || ret == -ENOENT) {
315 * The tuple is unknown or known but not parsed.
316 * Queue the tuple for the function driver.
319 this->code = tpl_code;
320 this->size = tpl_link;
324 if (ret == -ENOENT) {
325 if (time_after(jiffies, timeout))
327 /* warn about unknown tuples */
328 pr_warn_ratelimited("%s: queuing unknown"
329 " CIS tuple 0x%02x (%u bytes)\n",
330 mmc_hostname(card->host),
334 /* keep on analyzing tuples */
338 * We don't need the tuple anymore if it was
339 * successfully parsed by the SDIO core or if it is
340 * not going to be queued for a driver.
349 * Link in all unknown tuples found in the common CIS so that
350 * drivers don't have to go digging in two places.
353 *prev = card->tuples;
358 int sdio_read_common_cis(struct mmc_card *card)
360 return sdio_read_cis(card, NULL);
363 void sdio_free_common_cis(struct mmc_card *card)
365 struct sdio_func_tuple *tuple, *victim;
367 tuple = card->tuples;
378 int sdio_read_func_cis(struct sdio_func *func)
382 ret = sdio_read_cis(func->card, func);
387 * Since we've linked to tuples in the card structure,
388 * we must make sure we have a reference to it.
390 get_device(&func->card->dev);
393 * Vendor/device id is optional for function CIS, so
394 * copy it from the card structure as needed.
396 if (func->vendor == 0) {
397 func->vendor = func->card->cis.vendor;
398 func->device = func->card->cis.device;
404 void sdio_free_func_cis(struct sdio_func *func)
406 struct sdio_func_tuple *tuple, *victim;
408 tuple = func->tuples;
410 while (tuple && tuple != func->card->tuples) {
419 * We have now removed the link to the tuples in the
420 * card structure, so remove the reference.
422 put_device(&func->card->dev);