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 static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
24 const unsigned char *buf, unsigned size)
26 unsigned i, nr_strings;
27 char **buffer, *string;
29 /* Find all null-terminated (including zero length) strings in
30 the TPLLV1_INFO field. Trailing garbage is ignored. */
35 for (i = 0; i < size; i++) {
46 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
50 string = (char*)(buffer + nr_strings);
52 for (i = 0; i < nr_strings; i++) {
55 string += strlen(string) + 1;
56 buf += strlen(buf) + 1;
60 func->num_info = nr_strings;
61 func->info = (const char**)buffer;
63 card->num_info = nr_strings;
64 card->info = (const char**)buffer;
70 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
71 const unsigned char *buf, unsigned size)
73 unsigned int vendor, device;
76 vendor = buf[0] | (buf[1] << 8);
79 device = buf[2] | (buf[3] << 8);
82 func->vendor = vendor;
83 func->device = device;
85 card->cis.vendor = vendor;
86 card->cis.device = device;
92 static const unsigned char speed_val[16] =
93 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
94 static const unsigned int speed_unit[8] =
95 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
98 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
99 const unsigned char *, unsigned);
103 unsigned char min_size;
107 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
108 const char *tpl_descr,
109 const struct cis_tpl *tpl, int tpl_count,
111 const unsigned char *buf, unsigned size)
115 /* look for a matching code in the table */
116 for (i = 0; i < tpl_count; i++, tpl++) {
117 if (tpl->code == code)
121 if (size >= tpl->min_size) {
123 ret = tpl->parse(card, func, buf, size);
125 ret = -EILSEQ; /* known tuple, not parsed */
130 if (ret && ret != -EILSEQ && ret != -ENOENT) {
131 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
132 mmc_hostname(card->host), tpl_descr, code, size);
142 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
143 const unsigned char *buf, unsigned size)
145 /* Only valid for the common CIS (function 0) */
149 /* TPLFE_FN0_BLK_SIZE */
150 card->cis.blksize = buf[1] | (buf[2] << 8);
152 /* TPLFE_MAX_TRAN_SPEED */
153 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
154 speed_unit[buf[3] & 7];
159 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
160 const unsigned char *buf, unsigned size)
165 /* Only valid for the individual function's CIS (1-7) */
170 * This tuple has a different length depending on the SDIO spec
173 vsn = func->card->cccr.sdio_vsn;
174 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
176 if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
177 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
178 mmc_hostname(card->host));
179 vsn = SDIO_SDIO_REV_1_00;
180 } else if (size < min_size) {
184 /* TPLFE_MAX_BLK_SIZE */
185 func->max_blksize = buf[12] | (buf[13] << 8);
187 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
188 if (vsn > SDIO_SDIO_REV_1_00)
189 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
191 func->enable_timeout = jiffies_to_msecs(HZ);
197 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
199 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
200 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
201 * TPLFID_FUNCTION is always hardcoded to 0x0C.
203 static const struct cis_tpl cis_tpl_funce_list[] = {
204 { 0x00, 4, cistpl_funce_common },
205 { 0x01, 0, cistpl_funce_func },
206 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
209 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
210 const unsigned char *buf, unsigned size)
215 return cis_tpl_parse(card, func, "CISTPL_FUNCE",
217 ARRAY_SIZE(cis_tpl_funce_list),
221 /* Known TPL_CODEs table for CIS tuples */
222 static const struct cis_tpl cis_tpl_list[] = {
223 { 0x15, 3, cistpl_vers_1 },
224 { 0x20, 4, cistpl_manfid },
225 { 0x21, 2, /* cistpl_funcid */ },
226 { 0x22, 0, cistpl_funce },
227 { 0x91, 2, /* cistpl_sdio_std */ },
230 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
233 struct sdio_func_tuple *this, **prev;
237 * Note that this works for the common CIS (function number 0) as
238 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
239 * have the same offset.
241 for (i = 0; i < 3; i++) {
249 ret = mmc_io_rw_direct(card, 0, 0,
250 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
257 prev = &func->tuples;
259 prev = &card->tuples;
265 unsigned char tpl_code, tpl_link;
267 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
271 /* 0xff means we're done */
272 if (tpl_code == 0xff)
275 /* null entries have no link field or data */
276 if (tpl_code == 0x00)
279 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
283 /* a size of 0xff also means we're done */
284 if (tpl_link == 0xff)
287 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
291 for (i = 0; i < tpl_link; i++) {
292 ret = mmc_io_rw_direct(card, 0, 0,
293 ptr + i, 0, &this->data[i]);
302 /* Try to parse the CIS tuple */
303 ret = cis_tpl_parse(card, func, "CIS",
304 cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
305 tpl_code, this->data, tpl_link);
306 if (ret == -EILSEQ || ret == -ENOENT) {
308 * The tuple is unknown or known but not parsed.
309 * Queue the tuple for the function driver.
312 this->code = tpl_code;
313 this->size = tpl_link;
317 if (ret == -ENOENT) {
318 /* warn about unknown tuples */
319 pr_warn_ratelimited("%s: queuing unknown"
320 " CIS tuple 0x%02x (%u bytes)\n",
321 mmc_hostname(card->host),
325 /* keep on analyzing tuples */
329 * We don't need the tuple anymore if it was
330 * successfully parsed by the SDIO core or if it is
331 * not going to be queued for a driver.
340 * Link in all unknown tuples found in the common CIS so that
341 * drivers don't have to go digging in two places.
344 *prev = card->tuples;
349 int sdio_read_common_cis(struct mmc_card *card)
351 return sdio_read_cis(card, NULL);
354 void sdio_free_common_cis(struct mmc_card *card)
356 struct sdio_func_tuple *tuple, *victim;
358 tuple = card->tuples;
369 int sdio_read_func_cis(struct sdio_func *func)
373 ret = sdio_read_cis(func->card, func);
378 * Since we've linked to tuples in the card structure,
379 * we must make sure we have a reference to it.
381 get_device(&func->card->dev);
384 * Vendor/device id is optional for function CIS, so
385 * copy it from the card structure as needed.
387 if (func->vendor == 0) {
388 func->vendor = func->card->cis.vendor;
389 func->device = func->card->cis.device;
395 void sdio_free_func_cis(struct sdio_func *func)
397 struct sdio_func_tuple *tuple, *victim;
399 tuple = func->tuples;
401 while (tuple && tuple != func->card->tuples) {
410 * We have now removed the link to the tuples in the
411 * card structure, so remove the reference.
413 put_device(&func->card->dev);