1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17
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>
22
23#include "sdio_cis.h"
24#include "sdio_ops.h"
25
26static 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
33
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 }
44 if (nr_strings == 0)
45 return 0;
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
73static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
74 const unsigned char *buf, unsigned size)
75{
76 unsigned int vendor, device;
77
78
79 vendor = buf[0] | (buf[1] << 8);
80
81
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
95static 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
100
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;
108};
109
110static 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)
115{
116 int i, ret;
117
118
119 for (i = 0; i < tpl_count; i++, tpl++) {
120 if (tpl->code == code)
121 break;
122 }
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;
129 } else {
130
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
139 ret = -ENOENT;
140 }
141
142 return ret;
143}
144
145static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
146 const unsigned char *buf, unsigned size)
147{
148
149 if (func)
150 return -EINVAL;
151
152
153 card->cis.blksize = buf[1] | (buf[2] << 8);
154
155
156 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
157 speed_unit[buf[3] & 7];
158
159 return 0;
160}
161
162static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
163 const unsigned char *buf, unsigned size)
164{
165 unsigned vsn;
166 unsigned min_size;
167
168
169 if (!func)
170 return -EINVAL;
171
172
173
174
175
176 vsn = func->card->cccr.sdio_vsn;
177 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
178
179 if (size < min_size)
180 return -EINVAL;
181
182
183 func->max_blksize = buf[12] | (buf[13] << 8);
184
185
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
191 return 0;
192}
193
194
195
196
197
198
199
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, },
205};
206
207static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
208 const unsigned char *buf, unsigned size)
209{
210 if (size < 1)
211 return -EINVAL;
212
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);
217}
218
219
220static const struct cis_tpl cis_tpl_list[] = {
221 { 0x15, 3, cistpl_vers_1 },
222 { 0x20, 4, cistpl_manfid },
223 { 0x21, 2, },
224 { 0x22, 0, cistpl_funce },
225};
226
227static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
228{
229 int ret;
230 struct sdio_func_tuple *this, **prev;
231 unsigned i, ptr = 0;
232
233
234
235
236
237
238 for (i = 0; i < 3; i++) {
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,
247 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
248 if (ret)
249 return ret;
250 ptr |= x << (i * 8);
251 }
252
253 if (func)
254 prev = &func->tuples;
255 else
256 prev = &card->tuples;
257
258 BUG_ON(*prev);
259
260 do {
261 unsigned char tpl_code, tpl_link;
262
263 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
264 if (ret)
265 break;
266
267
268 if (tpl_code == 0xff)
269 break;
270
271
272 if (tpl_code == 0x00)
273 continue;
274
275 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
276 if (ret)
277 break;
278
279
280 if (tpl_link == 0xff)
281 break;
282
283 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
284 if (!this)
285 return -ENOMEM;
286
287 for (i = 0; i < tpl_link; i++) {
288 ret = mmc_io_rw_direct(card, 0, 0,
289 ptr + i, 0, &this->data[i]);
290 if (ret)
291 break;
292 }
293 if (ret) {
294 kfree(this);
295 break;
296 }
297
298
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) {
303
304
305
306
307 this->next = NULL;
308 this->code = tpl_code;
309 this->size = tpl_link;
310 *prev = this;
311 prev = &this->next;
312
313 if (ret == -ENOENT) {
314
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
321
322 ret = 0;
323 } else {
324
325
326
327
328
329 kfree(this);
330 }
331
332 ptr += tpl_link;
333 } while (!ret);
334
335
336
337
338
339 if (func)
340 *prev = card->tuples;
341
342 return ret;
343}
344
345int 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)
351{
352 struct sdio_func_tuple *tuple, *victim;
353
354 tuple = card->tuples;
355
356 while (tuple) {
357 victim = tuple;
358 tuple = tuple->next;
359 kfree(victim);
360 }
361
362 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
375
376
377 get_device(&func->card->dev);
378
379
380
381
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
403 func->tuples = NULL;
404
405
406
407
408
409 put_device(&func->card->dev);
410}
411
412