Commit | Line | Data |
---|---|---|
b7261261 NP |
1 | /* |
2 | * linux/drivers/mmc/core/sdio_cis.c | |
3 | * | |
4 | * Author: Nicolas Pitre | |
5 | * Created: June 11, 2007 | |
6 | * Copyright: MontaVista Software Inc. | |
7 | * | |
b1538bcf NP |
8 | * Copyright 2007 Pierre Ossman |
9 | * | |
b7261261 NP |
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. | |
14 | */ | |
15 | ||
16 | #include <linux/kernel.h> | |
17 | ||
18 | #include <linux/mmc/host.h> | |
1a632f8c | 19 | #include <linux/mmc/card.h> |
b7261261 NP |
20 | #include <linux/mmc/sdio.h> |
21 | #include <linux/mmc/sdio_func.h> | |
22 | ||
23 | #include "sdio_cis.h" | |
24 | #include "sdio_ops.h" | |
25 | ||
759bdc7a PO |
26 | static 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 | ||
a1125b1e DV |
32 | /* Find all null-terminated (including zero length) strings in |
33 | the TPLLV1_INFO field. Trailing garbage is ignored. */ | |
759bdc7a PO |
34 | buf += 2; |
35 | size -= 2; | |
36 | ||
37 | nr_strings = 0; | |
38 | for (i = 0; i < size; i++) { | |
39 | if (buf[i] == 0xff) | |
40 | break; | |
41 | if (buf[i] == 0) | |
42 | nr_strings++; | |
43 | } | |
a1125b1e | 44 | if (nr_strings == 0) |
759bdc7a | 45 | return 0; |
759bdc7a PO |
46 | |
47 | size = i; | |
48 | ||
49 | buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL); | |
50 | if (!buffer) | |
51 | return -ENOMEM; | |
52 | ||
53 | string = (char*)(buffer + nr_strings); | |
54 | ||
55 | for (i = 0; i < nr_strings; i++) { | |
56 | buffer[i] = string; | |
57 | strcpy(string, buf); | |
58 | string += strlen(string) + 1; | |
59 | buf += strlen(buf) + 1; | |
60 | } | |
61 | ||
62 | if (func) { | |
63 | func->num_info = nr_strings; | |
64 | func->info = (const char**)buffer; | |
65 | } else { | |
66 | card->num_info = nr_strings; | |
67 | card->info = (const char**)buffer; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
1a632f8c PO |
73 | static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func, |
74 | const unsigned char *buf, unsigned size) | |
b7261261 | 75 | { |
1a632f8c PO |
76 | unsigned int vendor, device; |
77 | ||
b7261261 | 78 | /* TPLMID_MANF */ |
1a632f8c | 79 | vendor = buf[0] | (buf[1] << 8); |
b7261261 NP |
80 | |
81 | /* TPLMID_CARD */ | |
1a632f8c PO |
82 | device = buf[2] | (buf[3] << 8); |
83 | ||
84 | if (func) { | |
85 | func->vendor = vendor; | |
86 | func->device = device; | |
87 | } else { | |
88 | card->cis.vendor = vendor; | |
89 | card->cis.device = device; | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
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 }; | |
99 | ||
4ec64960 AH |
100 | |
101 | typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *, | |
102 | const unsigned char *, unsigned); | |
103 | ||
104 | struct cis_tpl { | |
105 | unsigned char code; | |
106 | unsigned char min_size; | |
107 | tpl_parse_t *parse; | |
ed9935f4 AH |
108 | }; |
109 | ||
4ec64960 AH |
110 | static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func, |
111 | const char *tpl_descr, | |
112 | const struct cis_tpl *tpl, int tpl_count, | |
113 | unsigned char code, | |
114 | const unsigned char *buf, unsigned size) | |
ed9935f4 | 115 | { |
4ec64960 | 116 | int i, ret; |
ed9935f4 | 117 | |
4ec64960 AH |
118 | /* look for a matching code in the table */ |
119 | for (i = 0; i < tpl_count; i++, tpl++) { | |
120 | if (tpl->code == code) | |
121 | break; | |
ed9935f4 | 122 | } |
4ec64960 AH |
123 | if (i < tpl_count) { |
124 | if (size >= tpl->min_size) { | |
125 | if (tpl->parse) | |
126 | ret = tpl->parse(card, func, buf, size); | |
127 | else | |
128 | ret = -EILSEQ; /* known tuple, not parsed */ | |
129 | } else { | |
130 | /* invalid tuple */ | |
131 | ret = -EINVAL; | |
132 | } | |
133 | if (ret && ret != -EILSEQ && ret != -ENOENT) { | |
134 | printk(KERN_ERR "%s: bad %s tuple 0x%02x (%u bytes)\n", | |
135 | mmc_hostname(card->host), tpl_descr, code, size); | |
136 | } | |
137 | } else { | |
138 | /* unknown tuple */ | |
139 | ret = -ENOENT; | |
140 | } | |
141 | ||
142 | return ret; | |
ed9935f4 AH |
143 | } |
144 | ||
4ec64960 | 145 | static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func, |
1a632f8c PO |
146 | const unsigned char *buf, unsigned size) |
147 | { | |
4ec64960 AH |
148 | /* Only valid for the common CIS (function 0) */ |
149 | if (func) | |
1a632f8c PO |
150 | return -EINVAL; |
151 | ||
152 | /* TPLFE_FN0_BLK_SIZE */ | |
153 | card->cis.blksize = buf[1] | (buf[2] << 8); | |
154 | ||
155 | /* TPLFE_MAX_TRAN_SPEED */ | |
156 | card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] * | |
157 | speed_unit[buf[3] & 7]; | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
4ec64960 | 162 | static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func, |
1a632f8c PO |
163 | const unsigned char *buf, unsigned size) |
164 | { | |
165 | unsigned vsn; | |
166 | unsigned min_size; | |
167 | ||
4ec64960 AH |
168 | /* Only valid for the individual function's CIS (1-7) */ |
169 | if (!func) | |
170 | return -EINVAL; | |
ed9935f4 | 171 | |
4ec64960 AH |
172 | /* |
173 | * This tuple has a different length depending on the SDIO spec | |
174 | * version. | |
175 | */ | |
1a632f8c PO |
176 | vsn = func->card->cccr.sdio_vsn; |
177 | min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42; | |
178 | ||
4ec64960 | 179 | if (size < min_size) |
1a632f8c PO |
180 | return -EINVAL; |
181 | ||
182 | /* TPLFE_MAX_BLK_SIZE */ | |
9a08f82b | 183 | func->max_blksize = buf[12] | (buf[13] << 8); |
b7261261 | 184 | |
62a7573e BZ |
185 | /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */ |
186 | if (vsn > SDIO_SDIO_REV_1_00) | |
187 | func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10; | |
188 | else | |
189 | func->enable_timeout = jiffies_to_msecs(HZ); | |
190 | ||
b7261261 NP |
191 | return 0; |
192 | } | |
193 | ||
4ec64960 AH |
194 | /* |
195 | * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples. | |
196 | * | |
197 | * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending | |
198 | * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO | |
199 | * TPLFID_FUNCTION is always hardcoded to 0x0C. | |
200 | */ | |
201 | static const struct cis_tpl cis_tpl_funce_list[] = { | |
202 | { 0x00, 4, cistpl_funce_common }, | |
203 | { 0x01, 0, cistpl_funce_func }, | |
204 | { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ }, | |
205 | }; | |
206 | ||
1a632f8c PO |
207 | static int cistpl_funce(struct mmc_card *card, struct sdio_func *func, |
208 | const unsigned char *buf, unsigned size) | |
209 | { | |
4ec64960 AH |
210 | if (size < 1) |
211 | return -EINVAL; | |
1a632f8c | 212 | |
4ec64960 AH |
213 | return cis_tpl_parse(card, func, "CISTPL_FUNCE", |
214 | cis_tpl_funce_list, | |
215 | ARRAY_SIZE(cis_tpl_funce_list), | |
216 | buf[0], buf, size); | |
1a632f8c PO |
217 | } |
218 | ||
4ec64960 | 219 | /* Known TPL_CODEs table for CIS tuples */ |
b7261261 | 220 | static const struct cis_tpl cis_tpl_list[] = { |
759bdc7a | 221 | { 0x15, 3, cistpl_vers_1 }, |
b7261261 NP |
222 | { 0x20, 4, cistpl_manfid }, |
223 | { 0x21, 2, /* cistpl_funcid */ }, | |
1a632f8c | 224 | { 0x22, 0, cistpl_funce }, |
b7261261 NP |
225 | }; |
226 | ||
1a632f8c | 227 | static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func) |
b7261261 NP |
228 | { |
229 | int ret; | |
b1538bcf | 230 | struct sdio_func_tuple *this, **prev; |
b7261261 NP |
231 | unsigned i, ptr = 0; |
232 | ||
1a632f8c PO |
233 | /* |
234 | * Note that this works for the common CIS (function number 0) as | |
235 | * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS | |
236 | * have the same offset. | |
237 | */ | |
b7261261 | 238 | for (i = 0; i < 3; i++) { |
1a632f8c PO |
239 | unsigned char x, fn; |
240 | ||
241 | if (func) | |
242 | fn = func->num; | |
243 | else | |
244 | fn = 0; | |
245 | ||
246 | ret = mmc_io_rw_direct(card, 0, 0, | |
7616ee95 | 247 | SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x); |
b7261261 NP |
248 | if (ret) |
249 | return ret; | |
250 | ptr |= x << (i * 8); | |
251 | } | |
252 | ||
1a632f8c PO |
253 | if (func) |
254 | prev = &func->tuples; | |
255 | else | |
256 | prev = &card->tuples; | |
257 | ||
258 | BUG_ON(*prev); | |
b7261261 NP |
259 | |
260 | do { | |
261 | unsigned char tpl_code, tpl_link; | |
b7261261 | 262 | |
1a632f8c | 263 | ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code); |
b7261261 NP |
264 | if (ret) |
265 | break; | |
266 | ||
267 | /* 0xff means we're done */ | |
268 | if (tpl_code == 0xff) | |
269 | break; | |
270 | ||
c8d718f1 PO |
271 | /* null entries have no link field or data */ |
272 | if (tpl_code == 0x00) | |
273 | continue; | |
274 | ||
1a632f8c | 275 | ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link); |
b7261261 NP |
276 | if (ret) |
277 | break; | |
278 | ||
0d6132ba PO |
279 | /* a size of 0xff also means we're done */ |
280 | if (tpl_link == 0xff) | |
281 | break; | |
282 | ||
b1538bcf NP |
283 | this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL); |
284 | if (!this) | |
285 | return -ENOMEM; | |
286 | ||
287 | for (i = 0; i < tpl_link; i++) { | |
1a632f8c | 288 | ret = mmc_io_rw_direct(card, 0, 0, |
b1538bcf NP |
289 | ptr + i, 0, &this->data[i]); |
290 | if (ret) | |
b7261261 | 291 | break; |
b7261261 | 292 | } |
b1538bcf NP |
293 | if (ret) { |
294 | kfree(this); | |
b7261261 NP |
295 | break; |
296 | } | |
297 | ||
4ec64960 AH |
298 | /* Try to parse the CIS tuple */ |
299 | ret = cis_tpl_parse(card, func, "CIS", | |
300 | cis_tpl_list, ARRAY_SIZE(cis_tpl_list), | |
301 | tpl_code, this->data, tpl_link); | |
302 | if (ret == -EILSEQ || ret == -ENOENT) { | |
ed9935f4 | 303 | /* |
4ec64960 AH |
304 | * The tuple is unknown or known but not parsed. |
305 | * Queue the tuple for the function driver. | |
ed9935f4 | 306 | */ |
ed9935f4 AH |
307 | this->next = NULL; |
308 | this->code = tpl_code; | |
309 | this->size = tpl_link; | |
310 | *prev = this; | |
311 | prev = &this->next; | |
4ec64960 AH |
312 | |
313 | if (ret == -ENOENT) { | |
314 | /* warn about unknown tuples */ | |
315 | printk(KERN_WARNING "%s: queuing unknown" | |
316 | " CIS tuple 0x%02x (%u bytes)\n", | |
317 | mmc_hostname(card->host), | |
318 | tpl_code, tpl_link); | |
319 | } | |
320 | ||
ed9935f4 AH |
321 | /* keep on analyzing tuples */ |
322 | ret = 0; | |
4ec64960 AH |
323 | } else { |
324 | /* | |
325 | * We don't need the tuple anymore if it was | |
326 | * successfully parsed by the SDIO core or if it is | |
327 | * not going to be queued for a driver. | |
328 | */ | |
329 | kfree(this); | |
b7261261 | 330 | } |
b7261261 | 331 | |
b1538bcf | 332 | ptr += tpl_link; |
b7261261 NP |
333 | } while (!ret); |
334 | ||
1a632f8c PO |
335 | /* |
336 | * Link in all unknown tuples found in the common CIS so that | |
337 | * drivers don't have to go digging in two places. | |
338 | */ | |
339 | if (func) | |
340 | *prev = card->tuples; | |
341 | ||
b7261261 NP |
342 | return ret; |
343 | } | |
b1538bcf | 344 | |
1a632f8c PO |
345 | int sdio_read_common_cis(struct mmc_card *card) |
346 | { | |
347 | return sdio_read_cis(card, NULL); | |
348 | } | |
349 | ||
350 | void sdio_free_common_cis(struct mmc_card *card) | |
b1538bcf NP |
351 | { |
352 | struct sdio_func_tuple *tuple, *victim; | |
353 | ||
1a632f8c | 354 | tuple = card->tuples; |
b1538bcf NP |
355 | |
356 | while (tuple) { | |
357 | victim = tuple; | |
358 | tuple = tuple->next; | |
359 | kfree(victim); | |
360 | } | |
361 | ||
1a632f8c PO |
362 | card->tuples = NULL; |
363 | } | |
364 | ||
365 | int sdio_read_func_cis(struct sdio_func *func) | |
366 | { | |
367 | int ret; | |
368 | ||
369 | ret = sdio_read_cis(func->card, func); | |
370 | if (ret) | |
371 | return ret; | |
372 | ||
373 | /* | |
374 | * Since we've linked to tuples in the card structure, | |
375 | * we must make sure we have a reference to it. | |
376 | */ | |
377 | get_device(&func->card->dev); | |
378 | ||
379 | /* | |
380 | * Vendor/device id is optional for function CIS, so | |
381 | * copy it from the card structure as needed. | |
382 | */ | |
383 | if (func->vendor == 0) { | |
384 | func->vendor = func->card->cis.vendor; | |
385 | func->device = func->card->cis.device; | |
386 | } | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | void sdio_free_func_cis(struct sdio_func *func) | |
392 | { | |
393 | struct sdio_func_tuple *tuple, *victim; | |
394 | ||
395 | tuple = func->tuples; | |
396 | ||
397 | while (tuple && tuple != func->card->tuples) { | |
398 | victim = tuple; | |
399 | tuple = tuple->next; | |
400 | kfree(victim); | |
401 | } | |
402 | ||
b1538bcf | 403 | func->tuples = NULL; |
1a632f8c PO |
404 | |
405 | /* | |
406 | * We have now removed the link to the tuples in the | |
407 | * card structure, so remove the reference. | |
408 | */ | |
409 | put_device(&func->card->dev); | |
b1538bcf NP |
410 | } |
411 |