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 u8 major_rev
, minor_rev
;
27 unsigned i
, nr_strings
;
28 char **buffer
, *string
;
36 /* Find all null-terminated (including zero length) strings in
37 the TPLLV1_INFO field. Trailing garbage is ignored. */
42 for (i
= 0; i
< size
; i
++) {
53 buffer
= kzalloc(sizeof(char*) * nr_strings
+ size
, GFP_KERNEL
);
57 string
= (char*)(buffer
+ nr_strings
);
59 for (i
= 0; i
< nr_strings
; i
++) {
62 string
+= strlen(string
) + 1;
63 buf
+= strlen(buf
) + 1;
67 func
->major_rev
= major_rev
;
68 func
->minor_rev
= minor_rev
;
69 func
->num_info
= nr_strings
;
70 func
->info
= (const char**)buffer
;
72 card
->major_rev
= major_rev
;
73 card
->minor_rev
= minor_rev
;
74 card
->num_info
= nr_strings
;
75 card
->info
= (const char**)buffer
;
81 static int cistpl_manfid(struct mmc_card
*card
, struct sdio_func
*func
,
82 const unsigned char *buf
, unsigned size
)
84 unsigned int vendor
, device
;
87 vendor
= buf
[0] | (buf
[1] << 8);
90 device
= buf
[2] | (buf
[3] << 8);
93 func
->vendor
= vendor
;
94 func
->device
= device
;
96 card
->cis
.vendor
= vendor
;
97 card
->cis
.device
= device
;
103 static const unsigned char speed_val
[16] =
104 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
105 static const unsigned int speed_unit
[8] =
106 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
109 typedef int (tpl_parse_t
)(struct mmc_card
*, struct sdio_func
*,
110 const unsigned char *, unsigned);
114 unsigned char min_size
;
118 static int cis_tpl_parse(struct mmc_card
*card
, struct sdio_func
*func
,
119 const char *tpl_descr
,
120 const struct cis_tpl
*tpl
, int tpl_count
,
122 const unsigned char *buf
, unsigned size
)
126 /* look for a matching code in the table */
127 for (i
= 0; i
< tpl_count
; i
++, tpl
++) {
128 if (tpl
->code
== code
)
132 if (size
>= tpl
->min_size
) {
134 ret
= tpl
->parse(card
, func
, buf
, size
);
136 ret
= -EILSEQ
; /* known tuple, not parsed */
141 if (ret
&& ret
!= -EILSEQ
&& ret
!= -ENOENT
) {
142 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
143 mmc_hostname(card
->host
), tpl_descr
, code
, size
);
153 static int cistpl_funce_common(struct mmc_card
*card
, struct sdio_func
*func
,
154 const unsigned char *buf
, unsigned size
)
156 /* Only valid for the common CIS (function 0) */
160 /* TPLFE_FN0_BLK_SIZE */
161 card
->cis
.blksize
= buf
[1] | (buf
[2] << 8);
163 /* TPLFE_MAX_TRAN_SPEED */
164 card
->cis
.max_dtr
= speed_val
[(buf
[3] >> 3) & 15] *
165 speed_unit
[buf
[3] & 7];
170 static int cistpl_funce_func(struct mmc_card
*card
, struct sdio_func
*func
,
171 const unsigned char *buf
, unsigned size
)
176 /* Only valid for the individual function's CIS (1-7) */
181 * This tuple has a different length depending on the SDIO spec
184 vsn
= func
->card
->cccr
.sdio_vsn
;
185 min_size
= (vsn
== SDIO_SDIO_REV_1_00
) ? 28 : 42;
187 if (size
== 28 && vsn
== SDIO_SDIO_REV_1_10
) {
188 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
189 mmc_hostname(card
->host
));
190 vsn
= SDIO_SDIO_REV_1_00
;
191 } else if (size
< min_size
) {
195 /* TPLFE_MAX_BLK_SIZE */
196 func
->max_blksize
= buf
[12] | (buf
[13] << 8);
198 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
199 if (vsn
> SDIO_SDIO_REV_1_00
)
200 func
->enable_timeout
= (buf
[28] | (buf
[29] << 8)) * 10;
202 func
->enable_timeout
= jiffies_to_msecs(HZ
);
208 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
210 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
211 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
212 * TPLFID_FUNCTION is always hardcoded to 0x0C.
214 static const struct cis_tpl cis_tpl_funce_list
[] = {
215 { 0x00, 4, cistpl_funce_common
},
216 { 0x01, 0, cistpl_funce_func
},
217 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
220 static int cistpl_funce(struct mmc_card
*card
, struct sdio_func
*func
,
221 const unsigned char *buf
, unsigned size
)
226 return cis_tpl_parse(card
, func
, "CISTPL_FUNCE",
228 ARRAY_SIZE(cis_tpl_funce_list
),
232 /* Known TPL_CODEs table for CIS tuples */
233 static const struct cis_tpl cis_tpl_list
[] = {
234 { 0x15, 3, cistpl_vers_1
},
235 { 0x20, 4, cistpl_manfid
},
236 { 0x21, 2, /* cistpl_funcid */ },
237 { 0x22, 0, cistpl_funce
},
238 { 0x91, 2, /* cistpl_sdio_std */ },
241 static int sdio_read_cis(struct mmc_card
*card
, struct sdio_func
*func
)
244 struct sdio_func_tuple
*this, **prev
;
248 * Note that this works for the common CIS (function number 0) as
249 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
250 * have the same offset.
252 for (i
= 0; i
< 3; i
++) {
260 ret
= mmc_io_rw_direct(card
, 0, 0,
261 SDIO_FBR_BASE(fn
) + SDIO_FBR_CIS
+ i
, 0, &x
);
268 prev
= &func
->tuples
;
270 prev
= &card
->tuples
;
276 unsigned char tpl_code
, tpl_link
;
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 /* warn about unknown tuples */
330 pr_warn_ratelimited("%s: queuing unknown"
331 " CIS tuple 0x%02x (%u bytes)\n",
332 mmc_hostname(card
->host
),
336 /* keep on analyzing tuples */
340 * We don't need the tuple anymore if it was
341 * successfully parsed by the SDIO core or if it is
342 * not going to be queued for a driver.
351 * Link in all unknown tuples found in the common CIS so that
352 * drivers don't have to go digging in two places.
355 *prev
= card
->tuples
;
360 int sdio_read_common_cis(struct mmc_card
*card
)
362 return sdio_read_cis(card
, NULL
);
365 void sdio_free_common_cis(struct mmc_card
*card
)
367 struct sdio_func_tuple
*tuple
, *victim
;
369 tuple
= card
->tuples
;
380 int sdio_read_func_cis(struct sdio_func
*func
)
384 ret
= sdio_read_cis(func
->card
, func
);
389 * Since we've linked to tuples in the card structure,
390 * we must make sure we have a reference to it.
392 get_device(&func
->card
->dev
);
395 * Vendor/device id is optional for function CIS, so
396 * copy it from the card structure as needed.
398 if (func
->vendor
== 0) {
399 func
->vendor
= func
->card
->cis
.vendor
;
400 func
->device
= func
->card
->cis
.device
;
406 void sdio_free_func_cis(struct sdio_func
*func
)
408 struct sdio_func_tuple
*tuple
, *victim
;
410 tuple
= func
->tuples
;
412 while (tuple
&& tuple
!= func
->card
->tuples
) {
421 * We have now removed the link to the tuples in the
422 * card structure, so remove the reference.
424 put_device(&func
->card
->dev
);