blob: 9538389783c10e17d4494e875499c302b93ed01b [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
David Vrabela1125b12009-10-07 16:32:33 -070032 /* Find all null-terminated (including zero length) strings in
33 the TPLLV1_INFO field. Trailing garbage is ignored. */
Pierre Ossman759bdc72007-09-19 18:42:16 +020034 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 }
David Vrabela1125b12009-10-07 16:32:33 -070044 if (nr_strings == 0)
Pierre Ossman759bdc72007-09-19 18:42:16 +020045 return 0;
Pierre Ossman759bdc72007-09-19 18:42:16 +020046
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
Pierre Ossman1a632f82007-07-30 15:15:30 +020073static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
74 const unsigned char *buf, unsigned size)
Nicolas Pitreb7261262007-06-16 02:04:16 -040075{
Pierre Ossman1a632f82007-07-30 15:15:30 +020076 unsigned int vendor, device;
77
Nicolas Pitreb7261262007-06-16 02:04:16 -040078 /* TPLMID_MANF */
Pierre Ossman1a632f82007-07-30 15:15:30 +020079 vendor = buf[0] | (buf[1] << 8);
Nicolas Pitreb7261262007-06-16 02:04:16 -040080
81 /* TPLMID_CARD */
Pierre Ossman1a632f82007-07-30 15:15:30 +020082 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 }
Nicolas Pitreb7261262007-06-16 02:04:16 -040091
92 return 0;
93}
94
Pierre Ossman1a632f82007-07-30 15:15:30 +020095static const unsigned char speed_val[16] =
96 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
97static const unsigned int speed_unit[8] =
98 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
99
Albert Herranz4ec64962009-12-14 18:01:19 -0800100
101typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
102 const unsigned char *, unsigned);
103
104struct cis_tpl {
105 unsigned char code;
106 unsigned char min_size;
107 tpl_parse_t *parse;
Albert Herranzed9935f42009-10-01 15:44:05 -0700108};
109
Albert Herranz4ec64962009-12-14 18:01:19 -0800110static 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)
Albert Herranzed9935f42009-10-01 15:44:05 -0700115{
Albert Herranz4ec64962009-12-14 18:01:19 -0800116 int i, ret;
Albert Herranzed9935f42009-10-01 15:44:05 -0700117
Albert Herranz4ec64962009-12-14 18:01:19 -0800118 /* look for a matching code in the table */
119 for (i = 0; i < tpl_count; i++, tpl++) {
120 if (tpl->code == code)
121 break;
Albert Herranzed9935f42009-10-01 15:44:05 -0700122 }
Albert Herranz4ec64962009-12-14 18:01:19 -0800123 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;
Albert Herranzed9935f42009-10-01 15:44:05 -0700143}
144
Albert Herranz4ec64962009-12-14 18:01:19 -0800145static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
Pierre Ossman1a632f82007-07-30 15:15:30 +0200146 const unsigned char *buf, unsigned size)
147{
Albert Herranz4ec64962009-12-14 18:01:19 -0800148 /* Only valid for the common CIS (function 0) */
149 if (func)
Pierre Ossman1a632f82007-07-30 15:15:30 +0200150 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
Albert Herranz4ec64962009-12-14 18:01:19 -0800162static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
Pierre Ossman1a632f82007-07-30 15:15:30 +0200163 const unsigned char *buf, unsigned size)
164{
165 unsigned vsn;
166 unsigned min_size;
167
Albert Herranz4ec64962009-12-14 18:01:19 -0800168 /* Only valid for the individual function's CIS (1-7) */
169 if (!func)
170 return -EINVAL;
Albert Herranzed9935f42009-10-01 15:44:05 -0700171
Albert Herranz4ec64962009-12-14 18:01:19 -0800172 /*
173 * This tuple has a different length depending on the SDIO spec
174 * version.
175 */
Pierre Ossman1a632f82007-07-30 15:15:30 +0200176 vsn = func->card->cccr.sdio_vsn;
177 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
178
Albert Herranz4ec64962009-12-14 18:01:19 -0800179 if (size < min_size)
Pierre Ossman1a632f82007-07-30 15:15:30 +0200180 return -EINVAL;
181
182 /* TPLFE_MAX_BLK_SIZE */
David Vrabel9a08f822007-08-08 14:23:48 +0100183 func->max_blksize = buf[12] | (buf[13] << 8);
Pierre Ossman1a632f82007-07-30 15:15:30 +0200184
Benzi Zbit62a75732008-07-10 02:41:43 +0300185 /* 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
Pierre Ossman1a632f82007-07-30 15:15:30 +0200191 return 0;
192}
193
Albert Herranz4ec64962009-12-14 18:01:19 -0800194/*
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 */
201static 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
Pierre Ossman1a632f82007-07-30 15:15:30 +0200207static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
208 const unsigned char *buf, unsigned size)
209{
Albert Herranz4ec64962009-12-14 18:01:19 -0800210 if (size < 1)
211 return -EINVAL;
Pierre Ossman1a632f82007-07-30 15:15:30 +0200212
Albert Herranz4ec64962009-12-14 18:01:19 -0800213 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);
Pierre Ossman1a632f82007-07-30 15:15:30 +0200217}
218
Albert Herranz4ec64962009-12-14 18:01:19 -0800219/* Known TPL_CODEs table for CIS tuples */
Nicolas Pitreb7261262007-06-16 02:04:16 -0400220static const struct cis_tpl cis_tpl_list[] = {
Pierre Ossman759bdc72007-09-19 18:42:16 +0200221 { 0x15, 3, cistpl_vers_1 },
Nicolas Pitreb7261262007-06-16 02:04:16 -0400222 { 0x20, 4, cistpl_manfid },
223 { 0x21, 2, /* cistpl_funcid */ },
Pierre Ossman1a632f82007-07-30 15:15:30 +0200224 { 0x22, 0, cistpl_funce },
Nicolas Pitreb7261262007-06-16 02:04:16 -0400225};
226
Pierre Ossman1a632f82007-07-30 15:15:30 +0200227static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
Nicolas Pitreb7261262007-06-16 02:04:16 -0400228{
229 int ret;
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400230 struct sdio_func_tuple *this, **prev;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400231 unsigned i, ptr = 0;
232
Pierre Ossman1a632f82007-07-30 15:15:30 +0200233 /*
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 */
Nicolas Pitreb7261262007-06-16 02:04:16 -0400238 for (i = 0; i < 3; i++) {
Pierre Ossman1a632f82007-07-30 15:15:30 +0200239 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,
David Vrabel7616ee92007-08-08 14:23:05 +0100247 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400248 if (ret)
249 return ret;
250 ptr |= x << (i * 8);
251 }
252
Pierre Ossman1a632f82007-07-30 15:15:30 +0200253 if (func)
254 prev = &func->tuples;
255 else
256 prev = &card->tuples;
257
258 BUG_ON(*prev);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400259
260 do {
261 unsigned char tpl_code, tpl_link;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400262
Pierre Ossman1a632f82007-07-30 15:15:30 +0200263 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400264 if (ret)
265 break;
266
267 /* 0xff means we're done */
268 if (tpl_code == 0xff)
269 break;
270
Pierre Ossmanc8d718f2009-03-05 19:38:38 +0100271 /* null entries have no link field or data */
272 if (tpl_code == 0x00)
273 continue;
274
Pierre Ossman1a632f82007-07-30 15:15:30 +0200275 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400276 if (ret)
277 break;
278
Pierre Ossman0d6132b2009-03-05 19:37:28 +0100279 /* a size of 0xff also means we're done */
280 if (tpl_link == 0xff)
281 break;
282
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400283 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
284 if (!this)
285 return -ENOMEM;
286
287 for (i = 0; i < tpl_link; i++) {
Pierre Ossman1a632f82007-07-30 15:15:30 +0200288 ret = mmc_io_rw_direct(card, 0, 0,
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400289 ptr + i, 0, &this->data[i]);
290 if (ret)
291 break;
292 }
293 if (ret) {
294 kfree(this);
295 break;
296 }
297
Albert Herranz4ec64962009-12-14 18:01:19 -0800298 /* 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) {
Albert Herranzed9935f42009-10-01 15:44:05 -0700303 /*
Albert Herranz4ec64962009-12-14 18:01:19 -0800304 * The tuple is unknown or known but not parsed.
305 * Queue the tuple for the function driver.
Albert Herranzed9935f42009-10-01 15:44:05 -0700306 */
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400307 this->next = NULL;
308 this->code = tpl_code;
309 this->size = tpl_link;
310 *prev = this;
311 prev = &this->next;
Albert Herranz4ec64962009-12-14 18:01:19 -0800312
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
Albert Herranzed9935f42009-10-01 15:44:05 -0700321 /* keep on analyzing tuples */
322 ret = 0;
Albert Herranz4ec64962009-12-14 18:01:19 -0800323 } 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);
Nicolas Pitreb7261262007-06-16 02:04:16 -0400330 }
331
Nicolas Pitreb7261262007-06-16 02:04:16 -0400332 ptr += tpl_link;
Nicolas Pitreb7261262007-06-16 02:04:16 -0400333 } while (!ret);
334
Pierre Ossman1a632f82007-07-30 15:15:30 +0200335 /*
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
Nicolas Pitreb7261262007-06-16 02:04:16 -0400342 return ret;
343}
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400344
Pierre Ossman1a632f82007-07-30 15:15:30 +0200345int sdio_read_common_cis(struct mmc_card *card)
346{
347 return sdio_read_cis(card, NULL);
348}
349
350void sdio_free_common_cis(struct mmc_card *card)
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400351{
352 struct sdio_func_tuple *tuple, *victim;
353
Pierre Ossman1a632f82007-07-30 15:15:30 +0200354 tuple = card->tuples;
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400355
356 while (tuple) {
357 victim = tuple;
358 tuple = tuple->next;
359 kfree(victim);
360 }
361
Pierre Ossman1a632f82007-07-30 15:15:30 +0200362 card->tuples = NULL;
363}
364
365int 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
391void 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
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400403 func->tuples = NULL;
Pierre Ossman1a632f82007-07-30 15:15:30 +0200404
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);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400410}
411