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;
32 /* Find all null-terminated (including zero length) strings in
33 the TPLLV1_INFO field. Trailing garbage is ignored. */
38 for (i = 0; i < size; i++) {
49 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
53 string = (char*)(buffer + nr_strings);
55 for (i = 0; i < nr_strings; i++) {
58 string += strlen(string) + 1;
59 buf += strlen(buf) + 1;
63 func->num_info = nr_strings;
64 func->info = (const char**)buffer;
66 card->num_info = nr_strings;
67 card->info = (const char**)buffer;
73 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
74 const unsigned char *buf, unsigned size)
76 unsigned int vendor, device;
79 vendor = buf[0] | (buf[1] << 8);
82 device = buf[2] | (buf[3] << 8);
85 func->vendor = vendor;
86 func->device = device;
88 card->cis.vendor = vendor;
89 card->cis.device = device;
95 static const unsigned char speed_val[16] =
96 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
97 static const unsigned int speed_unit[8] =
98 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
100 /* FUNCE tuples with these types get passed to SDIO drivers */
101 static const unsigned char funce_type_whitelist[] = {
102 4 /* CISTPL_FUNCE_LAN_NODE_ID used in Broadcom cards */
105 static int cistpl_funce_whitelisted(unsigned char type)
109 for (i = 0; i < ARRAY_SIZE(funce_type_whitelist); i++) {
110 if (funce_type_whitelist[i] == type)
116 static int cistpl_funce_common(struct mmc_card *card,
117 const unsigned char *buf, unsigned size)
119 if (size < 0x04 || buf[0] != 0)
122 /* TPLFE_FN0_BLK_SIZE */
123 card->cis.blksize = buf[1] | (buf[2] << 8);
125 /* TPLFE_MAX_TRAN_SPEED */
126 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
127 speed_unit[buf[3] & 7];
132 static int cistpl_funce_func(struct sdio_func *func,
133 const unsigned char *buf, unsigned size)
138 /* let SDIO drivers take care of whitelisted FUNCE tuples */
139 if (cistpl_funce_whitelisted(buf[0]))
142 vsn = func->card->cccr.sdio_vsn;
143 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
145 if (size < min_size || buf[0] != 1)
148 /* TPLFE_MAX_BLK_SIZE */
149 func->max_blksize = buf[12] | (buf[13] << 8);
151 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
152 if (vsn > SDIO_SDIO_REV_1_00)
153 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
155 func->enable_timeout = jiffies_to_msecs(HZ);
160 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
161 const unsigned char *buf, unsigned size)
166 * There should be two versions of the CISTPL_FUNCE tuple,
167 * one for the common CIS (function 0) and a version used by
168 * the individual function's CIS (1-7). Yet, the later has a
169 * different length depending on the SDIO spec version.
172 ret = cistpl_funce_func(func, buf, size);
174 ret = cistpl_funce_common(card, buf, size);
176 if (ret && ret != -EILSEQ) {
177 printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
178 "type %u\n", mmc_hostname(card->host), size, buf[0]);
184 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
185 const unsigned char *, unsigned);
189 unsigned char min_size;
193 static const struct cis_tpl cis_tpl_list[] = {
194 { 0x15, 3, cistpl_vers_1 },
195 { 0x20, 4, cistpl_manfid },
196 { 0x21, 2, /* cistpl_funcid */ },
197 { 0x22, 0, cistpl_funce },
200 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
203 struct sdio_func_tuple *this, **prev;
207 * Note that this works for the common CIS (function number 0) as
208 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
209 * have the same offset.
211 for (i = 0; i < 3; i++) {
219 ret = mmc_io_rw_direct(card, 0, 0,
220 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
227 prev = &func->tuples;
229 prev = &card->tuples;
234 unsigned char tpl_code, tpl_link;
236 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
240 /* 0xff means we're done */
241 if (tpl_code == 0xff)
244 /* null entries have no link field or data */
245 if (tpl_code == 0x00)
248 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
252 /* a size of 0xff also means we're done */
253 if (tpl_link == 0xff)
256 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
260 for (i = 0; i < tpl_link; i++) {
261 ret = mmc_io_rw_direct(card, 0, 0,
262 ptr + i, 0, &this->data[i]);
271 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
272 if (cis_tpl_list[i].code == tpl_code)
274 if (i < ARRAY_SIZE(cis_tpl_list)) {
275 const struct cis_tpl *tpl = cis_tpl_list + i;
276 if (tpl_link < tpl->min_size) {
278 "%s: bad CIS tuple 0x%02x"
279 " (length = %u, expected >= %u)\n",
280 mmc_hostname(card->host),
281 tpl_code, tpl_link, tpl->min_size);
283 } else if (tpl->parse) {
284 ret = tpl->parse(card, func,
285 this->data, tpl_link);
288 * We don't need the tuple anymore if it was
289 * successfully parsed by the SDIO core or if it is
290 * not going to be parsed by SDIO drivers.
292 if (!ret || ret != -EILSEQ)
299 if (ret == -EILSEQ) {
300 /* this tuple is unknown to the core or whitelisted */
302 this->code = tpl_code;
303 this->size = tpl_link;
307 "%s: queuing CIS tuple 0x%02x length %u\n",
308 mmc_hostname(card->host), tpl_code, tpl_link);
309 /* keep on analyzing tuples */
317 * Link in all unknown tuples found in the common CIS so that
318 * drivers don't have to go digging in two places.
321 *prev = card->tuples;
326 int sdio_read_common_cis(struct mmc_card *card)
328 return sdio_read_cis(card, NULL);
331 void sdio_free_common_cis(struct mmc_card *card)
333 struct sdio_func_tuple *tuple, *victim;
335 tuple = card->tuples;
346 int sdio_read_func_cis(struct sdio_func *func)
350 ret = sdio_read_cis(func->card, func);
355 * Since we've linked to tuples in the card structure,
356 * we must make sure we have a reference to it.
358 get_device(&func->card->dev);
361 * Vendor/device id is optional for function CIS, so
362 * copy it from the card structure as needed.
364 if (func->vendor == 0) {
365 func->vendor = func->card->cis.vendor;
366 func->device = func->card->cis.device;
372 void sdio_free_func_cis(struct sdio_func *func)
374 struct sdio_func_tuple *tuple, *victim;
376 tuple = func->tuples;
378 while (tuple && tuple != func->card->tuples) {
387 * We have now removed the link to the tuples in the
388 * card structure, so remove the reference.
390 put_device(&func->card->dev);