blob: 6ba93f599281f3eb53659fc32dc93cff15e3a6d6 [file] [log] [blame]
Nicolas Pitreb7261262007-06-16 02:04:16 -04001/*
2 * linux/drivers/mmc/core/sdio_cis.c
3 *
4 * Author: Nicolas Pitre
5 * Created: June 11, 2007
6 * Copyright: MontaVista Software Inc.
7 *
Nicolas Pitreb1538bc2007-06-16 02:06:47 -04008 * Copyright 2007 Pierre Ossman
9 *
Nicolas Pitreb7261262007-06-16 02:04:16 -040010 * 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.
14 */
15
16#include <linux/kernel.h>
17
18#include <linux/mmc/host.h>
Pierre Ossman1a632f82007-07-30 15:15:30 +020019#include <linux/mmc/card.h>
Nicolas Pitreb7261262007-06-16 02:04:16 -040020#include <linux/mmc/sdio.h>
21#include <linux/mmc/sdio_func.h>
22
23#include "sdio_cis.h"
24#include "sdio_ops.h"
25
Pierre Ossman759bdc72007-09-19 18:42:16 +020026static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
27 const unsigned char *buf, unsigned size)
28{
29 unsigned i, nr_strings;
30 char **buffer, *string;
31
32 buf += 2;
33 size -= 2;
34
35 nr_strings = 0;
36 for (i = 0; i < size; i++) {
37 if (buf[i] == 0xff)
38 break;
39 if (buf[i] == 0)
40 nr_strings++;
41 }
42
43 if (buf[i-1] != '\0') {
44 printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
45 return 0;
46 }
47
48 size = i;
49
50 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
51 if (!buffer)
52 return -ENOMEM;
53
54 string = (char*)(buffer + nr_strings);
55
56 for (i = 0; i < nr_strings; i++) {
57 buffer[i] = string;
58 strcpy(string, buf);
59 string += strlen(string) + 1;
60 buf += strlen(buf) + 1;
61 }
62
63 if (func) {
64 func->num_info = nr_strings;
65 func->info = (const char**)buffer;
66 } else {
67 card->num_info = nr_strings;
68 card->info = (const char**)buffer;
69 }
70
71 return 0;
72}
73
Pierre Ossman1a632f82007-07-30 15:15:30 +020074static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
75 const unsigned char *buf, unsigned size)
Nicolas Pitreb7261262007-06-16 02:04:16 -040076{
Pierre Ossman1a632f82007-07-30 15:15:30 +020077 unsigned int vendor, device;
78
Nicolas Pitreb7261262007-06-16 02:04:16 -040079 /* TPLMID_MANF */
Pierre Ossman1a632f82007-07-30 15:15:30 +020080 vendor = buf[0] | (buf[1] << 8);
Nicolas Pitreb7261262007-06-16 02:04:16 -040081
82 /* TPLMID_CARD */
Pierre Ossman1a632f82007-07-30 15:15:30 +020083 device = buf[2] | (buf[3] << 8);
84
85 if (func) {
86 func->vendor = vendor;
87 func->device = device;
88 } else {
89 card->cis.vendor = vendor;
90 card->cis.device = device;
91 }
Nicolas Pitreb7261262007-06-16 02:04:16 -040092
93 return 0;
94}
95
Pierre Ossman1a632f82007-07-30 15:15:30 +020096static const unsigned char speed_val[16] =
97 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
98static const unsigned int speed_unit[8] =
99 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
100
101static int cistpl_funce_common(struct mmc_card *card,
102 const unsigned char *buf, unsigned size)
103{
104 if (size < 0x04 || buf[0] != 0)
105 return -EINVAL;
106
107 /* TPLFE_FN0_BLK_SIZE */
108 card->cis.blksize = buf[1] | (buf[2] << 8);
109
110 /* TPLFE_MAX_TRAN_SPEED */
111 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
112 speed_unit[buf[3] & 7];
113
114 return 0;
115}
116
117static int cistpl_funce_func(struct sdio_func *func,
118 const unsigned char *buf, unsigned size)
119{
120 unsigned vsn;
121 unsigned min_size;
122
123 vsn = func->card->cccr.sdio_vsn;
124 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
125
126 if (size < min_size || buf[0] != 1)
127 return -EINVAL;
128
129 /* TPLFE_MAX_BLK_SIZE */
David Vrabel9a08f822007-08-08 14:23:48 +0100130 func->max_blksize = buf[12] | (buf[13] << 8);
Pierre Ossman1a632f82007-07-30 15:15:30 +0200131
Benzi Zbit62a75732008-07-10 02:41:43 +0300132 /* 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;
135 else
136 func->enable_timeout = jiffies_to_msecs(HZ);
137
Pierre Ossman1a632f82007-07-30 15:15:30 +0200138 return 0;
139}
140
141static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
142 const unsigned char *buf, unsigned size)
143{
144 int ret;
145
146 /*
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.
151 */
152 if (func)
153 ret = cistpl_funce_func(func, buf, size);
154 else
155 ret = cistpl_funce_common(card, buf, size);
156
157 if (ret) {
158 printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
159 "type %u\n", mmc_hostname(card->host), size, buf[0]);
160 return ret;
161 }
162
163 return 0;
164}
165
166typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
167 const unsigned char *, unsigned);
168
Nicolas Pitreb7261262007-06-16 02:04:16 -0400169struct cis_tpl {
170 unsigned char code;
171 unsigned char min_size;
Pierre Ossman1a632f82007-07-30 15:15:30 +0200172 tpl_parse_t *parse;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400173};
174
175static const struct cis_tpl cis_tpl_list[] = {
Pierre Ossman759bdc72007-09-19 18:42:16 +0200176 { 0x15, 3, cistpl_vers_1 },
Nicolas Pitreb7261262007-06-16 02:04:16 -0400177 { 0x20, 4, cistpl_manfid },
178 { 0x21, 2, /* cistpl_funcid */ },
Pierre Ossman1a632f82007-07-30 15:15:30 +0200179 { 0x22, 0, cistpl_funce },
Nicolas Pitreb7261262007-06-16 02:04:16 -0400180};
181
Pierre Ossman1a632f82007-07-30 15:15:30 +0200182static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
Nicolas Pitreb7261262007-06-16 02:04:16 -0400183{
184 int ret;
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400185 struct sdio_func_tuple *this, **prev;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400186 unsigned i, ptr = 0;
187
Pierre Ossman1a632f82007-07-30 15:15:30 +0200188 /*
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.
192 */
Nicolas Pitreb7261262007-06-16 02:04:16 -0400193 for (i = 0; i < 3; i++) {
Pierre Ossman1a632f82007-07-30 15:15:30 +0200194 unsigned char x, fn;
195
196 if (func)
197 fn = func->num;
198 else
199 fn = 0;
200
201 ret = mmc_io_rw_direct(card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +0100202 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400203 if (ret)
204 return ret;
205 ptr |= x << (i * 8);
206 }
207
Pierre Ossman1a632f82007-07-30 15:15:30 +0200208 if (func)
209 prev = &func->tuples;
210 else
211 prev = &card->tuples;
212
213 BUG_ON(*prev);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400214
215 do {
216 unsigned char tpl_code, tpl_link;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400217
Pierre Ossman1a632f82007-07-30 15:15:30 +0200218 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400219 if (ret)
220 break;
221
222 /* 0xff means we're done */
223 if (tpl_code == 0xff)
224 break;
225
Pierre Ossman1a632f82007-07-30 15:15:30 +0200226 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400227 if (ret)
228 break;
229
Pierre Ossman0d6132b2009-03-05 19:37:28 +0100230 /* a size of 0xff also means we're done */
231 if (tpl_link == 0xff)
232 break;
233
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400234 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
235 if (!this)
236 return -ENOMEM;
237
238 for (i = 0; i < tpl_link; i++) {
Pierre Ossman1a632f82007-07-30 15:15:30 +0200239 ret = mmc_io_rw_direct(card, 0, 0,
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400240 ptr + i, 0, &this->data[i]);
241 if (ret)
242 break;
243 }
244 if (ret) {
245 kfree(this);
246 break;
247 }
248
Nicolas Pitreb7261262007-06-16 02:04:16 -0400249 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
250 if (cis_tpl_list[i].code == tpl_code)
251 break;
252 if (i >= ARRAY_SIZE(cis_tpl_list)) {
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400253 /* this tuple is unknown to the core */
254 this->next = NULL;
255 this->code = tpl_code;
256 this->size = tpl_link;
257 *prev = this;
258 prev = &this->next;
259 printk(KERN_DEBUG
260 "%s: queuing CIS tuple 0x%02x length %u\n",
Pierre Ossman1a632f82007-07-30 15:15:30 +0200261 mmc_hostname(card->host), tpl_code, tpl_link);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400262 } else {
263 const struct cis_tpl *tpl = cis_tpl_list + i;
264 if (tpl_link < tpl->min_size) {
265 printk(KERN_ERR
266 "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
Pierre Ossman1a632f82007-07-30 15:15:30 +0200267 mmc_hostname(card->host),
268 tpl_code, tpl_link, tpl->min_size);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400269 ret = -EINVAL;
Pierre Ossman1a632f82007-07-30 15:15:30 +0200270 } else if (tpl->parse) {
271 ret = tpl->parse(card, func,
272 this->data, tpl_link);
273 }
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400274 kfree(this);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400275 }
276
Nicolas Pitreb7261262007-06-16 02:04:16 -0400277 ptr += tpl_link;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400278 } while (!ret);
279
Pierre Ossman1a632f82007-07-30 15:15:30 +0200280 /*
281 * Link in all unknown tuples found in the common CIS so that
282 * drivers don't have to go digging in two places.
283 */
284 if (func)
285 *prev = card->tuples;
286
Nicolas Pitreb7261262007-06-16 02:04:16 -0400287 return ret;
288}
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400289
Pierre Ossman1a632f82007-07-30 15:15:30 +0200290int sdio_read_common_cis(struct mmc_card *card)
291{
292 return sdio_read_cis(card, NULL);
293}
294
295void sdio_free_common_cis(struct mmc_card *card)
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400296{
297 struct sdio_func_tuple *tuple, *victim;
298
Pierre Ossman1a632f82007-07-30 15:15:30 +0200299 tuple = card->tuples;
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400300
301 while (tuple) {
302 victim = tuple;
303 tuple = tuple->next;
304 kfree(victim);
305 }
306
Pierre Ossman1a632f82007-07-30 15:15:30 +0200307 card->tuples = NULL;
308}
309
310int sdio_read_func_cis(struct sdio_func *func)
311{
312 int ret;
313
314 ret = sdio_read_cis(func->card, func);
315 if (ret)
316 return ret;
317
318 /*
319 * Since we've linked to tuples in the card structure,
320 * we must make sure we have a reference to it.
321 */
322 get_device(&func->card->dev);
323
324 /*
325 * Vendor/device id is optional for function CIS, so
326 * copy it from the card structure as needed.
327 */
328 if (func->vendor == 0) {
329 func->vendor = func->card->cis.vendor;
330 func->device = func->card->cis.device;
331 }
332
333 return 0;
334}
335
336void sdio_free_func_cis(struct sdio_func *func)
337{
338 struct sdio_func_tuple *tuple, *victim;
339
340 tuple = func->tuples;
341
342 while (tuple && tuple != func->card->tuples) {
343 victim = tuple;
344 tuple = tuple->next;
345 kfree(victim);
346 }
347
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400348 func->tuples = NULL;
Pierre Ossman1a632f82007-07-30 15:15:30 +0200349
350 /*
351 * We have now removed the link to the tuples in the
352 * card structure, so remove the reference.
353 */
354 put_device(&func->card->dev);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400355}
356