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>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/sdio.h>
21 #include <linux/mmc/sdio_func.h>
26 static int cistpl_vers_1(struct mmc_card
*card
, struct sdio_func
*func
,
27 const unsigned char *buf
, unsigned size
)
29 unsigned i
, nr_strings
;
30 char **buffer
, *string
;
36 for (i
= 0; i
< size
; i
++) {
43 if (buf
[i
-1] != '\0') {
44 printk(KERN_WARNING
"SDIO: ignoring broken CISTPL_VERS_1\n");
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 };
101 static int cistpl_funce_common(struct mmc_card
*card
,
102 const unsigned char *buf
, unsigned size
)
104 if (size
< 0x04 || buf
[0] != 0)
107 /* TPLFE_FN0_BLK_SIZE */
108 card
->cis
.blksize
= buf
[1] | (buf
[2] << 8);
110 /* TPLFE_MAX_TRAN_SPEED */
111 card
->cis
.max_dtr
= speed_val
[(buf
[3] >> 3) & 15] *
112 speed_unit
[buf
[3] & 7];
117 static int cistpl_funce_func(struct sdio_func
*func
,
118 const unsigned char *buf
, unsigned size
)
123 vsn
= func
->card
->cccr
.sdio_vsn
;
124 min_size
= (vsn
== SDIO_SDIO_REV_1_00
) ? 28 : 42;
126 if (size
< min_size
|| buf
[0] != 1)
129 /* TPLFE_MAX_BLK_SIZE */
130 func
->max_blksize
= buf
[12] | (buf
[13] << 8);
132 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
133 if (vsn
> SDIO_SDIO_REV_1_00
)
134 func
->enable_timeout
= (buf
[28] | (buf
[29] << 8)) * 10;
136 func
->enable_timeout
= jiffies_to_msecs(HZ
);
141 static int cistpl_funce(struct mmc_card
*card
, struct sdio_func
*func
,
142 const unsigned char *buf
, unsigned size
)
147 * There should be two versions of the CISTPL_FUNCE tuple,
148 * one for the common CIS (function 0) and a version used by
149 * the individual function's CIS (1-7). Yet, the later has a
150 * different length depending on the SDIO spec version.
153 ret
= cistpl_funce_func(func
, buf
, size
);
155 ret
= cistpl_funce_common(card
, buf
, size
);
158 printk(KERN_ERR
"%s: bad CISTPL_FUNCE size %u "
159 "type %u\n", mmc_hostname(card
->host
), size
, buf
[0]);
166 typedef int (tpl_parse_t
)(struct mmc_card
*, struct sdio_func
*,
167 const unsigned char *, unsigned);
171 unsigned char min_size
;
175 static const struct cis_tpl cis_tpl_list
[] = {
176 { 0x15, 3, cistpl_vers_1
},
177 { 0x20, 4, cistpl_manfid
},
178 { 0x21, 2, /* cistpl_funcid */ },
179 { 0x22, 0, cistpl_funce
},
182 static int sdio_read_cis(struct mmc_card
*card
, struct sdio_func
*func
)
185 struct sdio_func_tuple
*this, **prev
;
189 * Note that this works for the common CIS (function number 0) as
190 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
191 * have the same offset.
193 for (i
= 0; i
< 3; i
++) {
201 ret
= mmc_io_rw_direct(card
, 0, 0,
202 SDIO_FBR_BASE(fn
) + SDIO_FBR_CIS
+ i
, 0, &x
);
209 prev
= &func
->tuples
;
211 prev
= &card
->tuples
;
216 unsigned char tpl_code
, tpl_link
;
218 ret
= mmc_io_rw_direct(card
, 0, 0, ptr
++, 0, &tpl_code
);
222 /* 0xff means we're done */
223 if (tpl_code
== 0xff)
226 /* null entries have no link field or data */
227 if (tpl_code
== 0x00)
230 ret
= mmc_io_rw_direct(card
, 0, 0, ptr
++, 0, &tpl_link
);
234 /* a size of 0xff also means we're done */
235 if (tpl_link
== 0xff)
238 this = kmalloc(sizeof(*this) + tpl_link
, GFP_KERNEL
);
242 for (i
= 0; i
< tpl_link
; i
++) {
243 ret
= mmc_io_rw_direct(card
, 0, 0,
244 ptr
+ i
, 0, &this->data
[i
]);
253 for (i
= 0; i
< ARRAY_SIZE(cis_tpl_list
); i
++)
254 if (cis_tpl_list
[i
].code
== tpl_code
)
256 if (i
>= ARRAY_SIZE(cis_tpl_list
)) {
257 /* this tuple is unknown to the core */
259 this->code
= tpl_code
;
260 this->size
= tpl_link
;
264 "%s: queuing CIS tuple 0x%02x length %u\n",
265 mmc_hostname(card
->host
), tpl_code
, tpl_link
);
267 const struct cis_tpl
*tpl
= cis_tpl_list
+ i
;
268 if (tpl_link
< tpl
->min_size
) {
270 "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
271 mmc_hostname(card
->host
),
272 tpl_code
, tpl_link
, tpl
->min_size
);
274 } else if (tpl
->parse
) {
275 ret
= tpl
->parse(card
, func
,
276 this->data
, tpl_link
);
285 * Link in all unknown tuples found in the common CIS so that
286 * drivers don't have to go digging in two places.
289 *prev
= card
->tuples
;
294 int sdio_read_common_cis(struct mmc_card
*card
)
296 return sdio_read_cis(card
, NULL
);
299 void sdio_free_common_cis(struct mmc_card
*card
)
301 struct sdio_func_tuple
*tuple
, *victim
;
303 tuple
= card
->tuples
;
314 int sdio_read_func_cis(struct sdio_func
*func
)
318 ret
= sdio_read_cis(func
->card
, func
);
323 * Since we've linked to tuples in the card structure,
324 * we must make sure we have a reference to it.
326 get_device(&func
->card
->dev
);
329 * Vendor/device id is optional for function CIS, so
330 * copy it from the card structure as needed.
332 if (func
->vendor
== 0) {
333 func
->vendor
= func
->card
->cis
.vendor
;
334 func
->device
= func
->card
->cis
.device
;
340 void sdio_free_func_cis(struct sdio_func
*func
)
342 struct sdio_func_tuple
*tuple
, *victim
;
344 tuple
= func
->tuples
;
346 while (tuple
&& tuple
!= func
->card
->tuples
) {
355 * We have now removed the link to the tuples in the
356 * card structure, so remove the reference.
358 put_device(&func
->card
->dev
);