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 static int cistpl_vers_1(struct mmc_card
*card
, struct sdio_func
*func
,
28 const unsigned char *buf
, unsigned size
)
30 unsigned i
, nr_strings
;
31 char **buffer
, *string
;
33 /* Find all null-terminated (including zero length) strings in
34 the TPLLV1_INFO field. Trailing garbage is ignored. */
39 for (i
= 0; i
< size
; i
++) {
50 buffer
= kzalloc(sizeof(char*) * nr_strings
+ size
, GFP_KERNEL
);
54 string
= (char*)(buffer
+ nr_strings
);
56 for (i
= 0; i
< nr_strings
; i
++) {
59 string
+= strlen(string
) + 1;
60 buf
+= strlen(buf
) + 1;
64 func
->num_info
= nr_strings
;
65 func
->info
= (const char**)buffer
;
67 card
->num_info
= nr_strings
;
68 card
->info
= (const char**)buffer
;
74 static int cistpl_manfid(struct mmc_card
*card
, struct sdio_func
*func
,
75 const unsigned char *buf
, unsigned size
)
77 unsigned int vendor
, device
;
80 vendor
= buf
[0] | (buf
[1] << 8);
83 device
= buf
[2] | (buf
[3] << 8);
86 func
->vendor
= vendor
;
87 func
->device
= device
;
89 card
->cis
.vendor
= vendor
;
90 card
->cis
.device
= device
;
96 static const unsigned char speed_val
[16] =
97 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
98 static const unsigned int speed_unit
[8] =
99 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
102 typedef int (tpl_parse_t
)(struct mmc_card
*, struct sdio_func
*,
103 const unsigned char *, unsigned);
107 unsigned char min_size
;
111 static int cis_tpl_parse(struct mmc_card
*card
, struct sdio_func
*func
,
112 const char *tpl_descr
,
113 const struct cis_tpl
*tpl
, int tpl_count
,
115 const unsigned char *buf
, unsigned size
)
119 /* look for a matching code in the table */
120 for (i
= 0; i
< tpl_count
; i
++, tpl
++) {
121 if (tpl
->code
== code
)
125 if (size
>= tpl
->min_size
) {
127 ret
= tpl
->parse(card
, func
, buf
, size
);
129 ret
= -EILSEQ
; /* known tuple, not parsed */
134 if (ret
&& ret
!= -EILSEQ
&& ret
!= -ENOENT
) {
135 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
136 mmc_hostname(card
->host
), tpl_descr
, code
, size
);
146 static int cistpl_funce_common(struct mmc_card
*card
, struct sdio_func
*func
,
147 const unsigned char *buf
, unsigned size
)
149 /* Only valid for the common CIS (function 0) */
153 /* TPLFE_FN0_BLK_SIZE */
154 card
->cis
.blksize
= buf
[1] | (buf
[2] << 8);
156 /* TPLFE_MAX_TRAN_SPEED */
157 card
->cis
.max_dtr
= speed_val
[(buf
[3] >> 3) & 15] *
158 speed_unit
[buf
[3] & 7];
163 static int cistpl_funce_func(struct mmc_card
*card
, struct sdio_func
*func
,
164 const unsigned char *buf
, unsigned size
)
169 /* Only valid for the individual function's CIS (1-7) */
174 * This tuple has a different length depending on the SDIO spec
177 vsn
= func
->card
->cccr
.sdio_vsn
;
178 min_size
= (vsn
== SDIO_SDIO_REV_1_00
) ? 28 : 42;
183 /* TPLFE_MAX_BLK_SIZE */
184 func
->max_blksize
= buf
[12] | (buf
[13] << 8);
186 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
187 if (vsn
> SDIO_SDIO_REV_1_00
)
188 func
->enable_timeout
= (buf
[28] | (buf
[29] << 8)) * 10;
190 func
->enable_timeout
= jiffies_to_msecs(HZ
);
196 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
198 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
199 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
200 * TPLFID_FUNCTION is always hardcoded to 0x0C.
202 static const struct cis_tpl cis_tpl_funce_list
[] = {
203 { 0x00, 4, cistpl_funce_common
},
204 { 0x01, 0, cistpl_funce_func
},
205 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
208 static int cistpl_funce(struct mmc_card
*card
, struct sdio_func
*func
,
209 const unsigned char *buf
, unsigned size
)
214 return cis_tpl_parse(card
, func
, "CISTPL_FUNCE",
216 ARRAY_SIZE(cis_tpl_funce_list
),
220 /* Known TPL_CODEs table for CIS tuples */
221 static const struct cis_tpl cis_tpl_list
[] = {
222 { 0x15, 3, cistpl_vers_1
},
223 { 0x20, 4, cistpl_manfid
},
224 { 0x21, 2, /* cistpl_funcid */ },
225 { 0x22, 0, cistpl_funce
},
228 static int sdio_read_cis(struct mmc_card
*card
, struct sdio_func
*func
)
231 struct sdio_func_tuple
*this, **prev
;
235 * Note that this works for the common CIS (function number 0) as
236 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
237 * have the same offset.
239 for (i
= 0; i
< 3; i
++) {
247 ret
= mmc_io_rw_direct(card
, 0, 0,
248 SDIO_FBR_BASE(fn
) + SDIO_FBR_CIS
+ i
, 0, &x
);
255 prev
= &func
->tuples
;
257 prev
= &card
->tuples
;
262 unsigned char tpl_code
, tpl_link
;
264 ret
= mmc_io_rw_direct(card
, 0, 0, ptr
++, 0, &tpl_code
);
268 /* 0xff means we're done */
269 if (tpl_code
== 0xff)
272 /* null entries have no link field or data */
273 if (tpl_code
== 0x00)
276 ret
= mmc_io_rw_direct(card
, 0, 0, ptr
++, 0, &tpl_link
);
280 /* a size of 0xff also means we're done */
281 if (tpl_link
== 0xff)
284 this = kmalloc(sizeof(*this) + tpl_link
, GFP_KERNEL
);
288 for (i
= 0; i
< tpl_link
; i
++) {
289 ret
= mmc_io_rw_direct(card
, 0, 0,
290 ptr
+ i
, 0, &this->data
[i
]);
299 /* Try to parse the CIS tuple */
300 ret
= cis_tpl_parse(card
, func
, "CIS",
301 cis_tpl_list
, ARRAY_SIZE(cis_tpl_list
),
302 tpl_code
, this->data
, tpl_link
);
303 if (ret
== -EILSEQ
|| ret
== -ENOENT
) {
305 * The tuple is unknown or known but not parsed.
306 * Queue the tuple for the function driver.
309 this->code
= tpl_code
;
310 this->size
= tpl_link
;
314 if (ret
== -ENOENT
) {
315 /* warn about unknown tuples */
316 pr_warning("%s: queuing unknown"
317 " CIS tuple 0x%02x (%u bytes)\n",
318 mmc_hostname(card
->host
),
322 /* keep on analyzing tuples */
326 * We don't need the tuple anymore if it was
327 * successfully parsed by the SDIO core or if it is
328 * not going to be queued for a driver.
337 * Link in all unknown tuples found in the common CIS so that
338 * drivers don't have to go digging in two places.
341 *prev
= card
->tuples
;
346 int sdio_read_common_cis(struct mmc_card
*card
)
348 return sdio_read_cis(card
, NULL
);
351 void sdio_free_common_cis(struct mmc_card
*card
)
353 struct sdio_func_tuple
*tuple
, *victim
;
355 tuple
= card
->tuples
;
366 int sdio_read_func_cis(struct sdio_func
*func
)
370 ret
= sdio_read_cis(func
->card
, func
);
375 * Since we've linked to tuples in the card structure,
376 * we must make sure we have a reference to it.
378 get_device(&func
->card
->dev
);
381 * Vendor/device id is optional for function CIS, so
382 * copy it from the card structure as needed.
384 if (func
->vendor
== 0) {
385 func
->vendor
= func
->card
->cis
.vendor
;
386 func
->device
= func
->card
->cis
.device
;
392 void sdio_free_func_cis(struct sdio_func
*func
)
394 struct sdio_func_tuple
*tuple
, *victim
;
396 tuple
= func
->tuples
;
398 while (tuple
&& tuple
!= func
->card
->tuples
) {
407 * We have now removed the link to the tuples in the
408 * card structure, so remove the reference.
410 put_device(&func
->card
->dev
);