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 u8 major_rev, minor_rev;
29 unsigned i, nr_strings;
30 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->major_rev = major_rev;
70 func->minor_rev = minor_rev;
71 func->num_info = nr_strings;
72 func->info = (const char**)buffer;
74 card->major_rev = major_rev;
75 card->minor_rev = minor_rev;
76 card->num_info = nr_strings;
77 card->info = (const char**)buffer;
83 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
84 const unsigned char *buf, unsigned size)
86 unsigned int vendor, device;
89 vendor = buf[0] | (buf[1] << 8);
92 device = buf[2] | (buf[3] << 8);
95 func->vendor = vendor;
96 func->device = device;
98 card->cis.vendor = vendor;
99 card->cis.device = device;
105 static const unsigned char speed_val[16] =
106 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
107 static const unsigned int speed_unit[8] =
108 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
111 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
112 const unsigned char *, unsigned);
116 unsigned char min_size;
120 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
121 const char *tpl_descr,
122 const struct cis_tpl *tpl, int tpl_count,
124 const unsigned char *buf, unsigned size)
128 /* look for a matching code in the table */
129 for (i = 0; i < tpl_count; i++, tpl++) {
130 if (tpl->code == code)
134 if (size >= tpl->min_size) {
136 ret = tpl->parse(card, func, buf, size);
138 ret = -EILSEQ; /* known tuple, not parsed */
143 if (ret && ret != -EILSEQ && ret != -ENOENT) {
144 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
145 mmc_hostname(card->host), tpl_descr, code, size);
155 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
156 const unsigned char *buf, unsigned size)
158 /* Only valid for the common CIS (function 0) */
162 /* TPLFE_FN0_BLK_SIZE */
163 card->cis.blksize = buf[1] | (buf[2] << 8);
165 /* TPLFE_MAX_TRAN_SPEED */
166 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
167 speed_unit[buf[3] & 7];
172 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
173 const unsigned char *buf, unsigned size)
178 /* Only valid for the individual function's CIS (1-7) */
183 * This tuple has a different length depending on the SDIO spec
186 vsn = func->card->cccr.sdio_vsn;
187 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
189 if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
190 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
191 mmc_hostname(card->host));
192 vsn = SDIO_SDIO_REV_1_00;
193 } else if (size < min_size) {
197 /* TPLFE_MAX_BLK_SIZE */
198 func->max_blksize = buf[12] | (buf[13] << 8);
200 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
201 if (vsn > SDIO_SDIO_REV_1_00)
202 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
204 func->enable_timeout = jiffies_to_msecs(HZ);
210 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
212 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
213 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
214 * TPLFID_FUNCTION is always hardcoded to 0x0C.
216 static const struct cis_tpl cis_tpl_funce_list[] = {
217 { 0x00, 4, cistpl_funce_common },
218 { 0x01, 0, cistpl_funce_func },
219 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
222 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
223 const unsigned char *buf, unsigned size)
228 return cis_tpl_parse(card, func, "CISTPL_FUNCE",
230 ARRAY_SIZE(cis_tpl_funce_list),
234 /* Known TPL_CODEs table for CIS tuples */
235 static const struct cis_tpl cis_tpl_list[] = {
236 { 0x15, 3, cistpl_vers_1 },
237 { 0x20, 4, cistpl_manfid },
238 { 0x21, 2, /* cistpl_funcid */ },
239 { 0x22, 0, cistpl_funce },
240 { 0x91, 2, /* cistpl_sdio_std */ },
243 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
246 struct sdio_func_tuple *this, **prev;
250 * Note that this works for the common CIS (function number 0) as
251 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
252 * have the same offset.
254 for (i = 0; i < 3; i++) {
262 ret = mmc_io_rw_direct(card, 0, 0,
263 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
270 prev = &func->tuples;
272 prev = &card->tuples;
278 unsigned char tpl_code, tpl_link;
279 unsigned long timeout = jiffies +
280 msecs_to_jiffies(SDIO_READ_CIS_TIMEOUT_MS);
282 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
286 /* 0xff means we're done */
287 if (tpl_code == 0xff)
290 /* null entries have no link field or data */
291 if (tpl_code == 0x00)
294 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
298 /* a size of 0xff also means we're done */
299 if (tpl_link == 0xff)
302 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
306 for (i = 0; i < tpl_link; i++) {
307 ret = mmc_io_rw_direct(card, 0, 0,
308 ptr + i, 0, &this->data[i]);
317 /* Try to parse the CIS tuple */
318 ret = cis_tpl_parse(card, func, "CIS",
319 cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
320 tpl_code, this->data, tpl_link);
321 if (ret == -EILSEQ || ret == -ENOENT) {
323 * The tuple is unknown or known but not parsed.
324 * Queue the tuple for the function driver.
327 this->code = tpl_code;
328 this->size = tpl_link;
332 if (ret == -ENOENT) {
334 if (time_after(jiffies, timeout))
337 #define FMT(type) "%s: queuing " type " CIS tuple 0x%02x [%*ph] (%u bytes)\n"
339 * Tuples in this range are reserved for
340 * vendors, so don't warn about them
342 if (tpl_code >= 0x80 && tpl_code <= 0x8f)
343 pr_debug_ratelimited(FMT("vendor"),
344 mmc_hostname(card->host),
345 tpl_code, tpl_link, this->data,
348 pr_warn_ratelimited(FMT("unknown"),
349 mmc_hostname(card->host),
350 tpl_code, tpl_link, this->data,
354 /* keep on analyzing tuples */
358 * We don't need the tuple anymore if it was
359 * successfully parsed by the SDIO core or if it is
360 * not going to be queued for a driver.
369 * Link in all unknown tuples found in the common CIS so that
370 * drivers don't have to go digging in two places.
373 *prev = card->tuples;
378 int sdio_read_common_cis(struct mmc_card *card)
380 return sdio_read_cis(card, NULL);
383 void sdio_free_common_cis(struct mmc_card *card)
385 struct sdio_func_tuple *tuple, *victim;
387 tuple = card->tuples;
398 int sdio_read_func_cis(struct sdio_func *func)
402 ret = sdio_read_cis(func->card, func);
407 * Vendor/device id is optional for function CIS, so
408 * copy it from the card structure as needed.
410 if (func->vendor == 0) {
411 func->vendor = func->card->cis.vendor;
412 func->device = func->card->cis.device;
418 void sdio_free_func_cis(struct sdio_func *func)
420 struct sdio_func_tuple *tuple, *victim;
422 tuple = func->tuples;
424 while (tuple && tuple != func->card->tuples) {