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
101static const unsigned char funce_type_whitelist[] = {
102 4
103};
104
105static int cistpl_funce_whitelisted(unsigned char type)
106{
107 int i;
108
109 for (i = 0; i < ARRAY_SIZE(funce_type_whitelist); i++) {
110 if (funce_type_whitelist[i] == type)
111 return 1;
112 }
113 return 0;
114}
115
116static int cistpl_funce_common(struct mmc_card *card,
117 const unsigned char *buf, unsigned size)
118{
119 if (size < 0x04 || buf[0] != 0)
120 return -EINVAL;
121
122
123 card->cis.blksize = buf[1] | (buf[2] << 8);
124
125
126 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
127 speed_unit[buf[3] & 7];
128
129 return 0;
130}
131
132static int cistpl_funce_func(struct sdio_func *func,
133 const unsigned char *buf, unsigned size)
134{
135 unsigned vsn;
136 unsigned min_size;
137
138
139 if (cistpl_funce_whitelisted(buf[0]))
140 return -EILSEQ;
141
142 vsn = func->card->cccr.sdio_vsn;
143 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
144
145 if (size < min_size || buf[0] != 1)
146 return -EINVAL;
147
148
149 func->max_blksize = buf[12] | (buf[13] << 8);
150
151
152 if (vsn > SDIO_SDIO_REV_1_00)
153 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
154 else
155 func->enable_timeout = jiffies_to_msecs(HZ);
156
157 return 0;
158}
159
160static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
161 const unsigned char *buf, unsigned size)
162{
163 int ret;
164
165
166
167
168
169
170
171 if (func)
172 ret = cistpl_funce_func(func, buf, size);
173 else
174 ret = cistpl_funce_common(card, buf, size);
175
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]);
179 }
180
181 return ret;
182}
183
184typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
185 const unsigned char *, unsigned);
186
187struct cis_tpl {
188 unsigned char code;
189 unsigned char min_size;
190 tpl_parse_t *parse;
191};
192
193static const struct cis_tpl cis_tpl_list[] = {
194 { 0x15, 3, cistpl_vers_1 },
195 { 0x20, 4, cistpl_manfid },
196 { 0x21, 2, },
197 { 0x22, 0, cistpl_funce },
198};
199
200static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
201{
202 int ret;
203 struct sdio_func_tuple *this, **prev;
204 unsigned i, ptr = 0;
205
206
207
208
209
210
211 for (i = 0; i < 3; i++) {
212 unsigned char x, fn;
213
214 if (func)
215 fn = func->num;
216 else
217 fn = 0;
218
219 ret = mmc_io_rw_direct(card, 0, 0,
220 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
221 if (ret)
222 return ret;
223 ptr |= x << (i * 8);
224 }
225
226 if (func)
227 prev = &func->tuples;
228 else
229 prev = &card->tuples;
230
231 BUG_ON(*prev);
232
233 do {
234 unsigned char tpl_code, tpl_link;
235
236 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
237 if (ret)
238 break;
239
240
241 if (tpl_code == 0xff)
242 break;
243
244
245 if (tpl_code == 0x00)
246 continue;
247
248 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
249 if (ret)
250 break;
251
252
253 if (tpl_link == 0xff)
254 break;
255
256 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
257 if (!this)
258 return -ENOMEM;
259
260 for (i = 0; i < tpl_link; i++) {
261 ret = mmc_io_rw_direct(card, 0, 0,
262 ptr + i, 0, &this->data[i]);
263 if (ret)
264 break;
265 }
266 if (ret) {
267 kfree(this);
268 break;
269 }
270
271 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
272 if (cis_tpl_list[i].code == tpl_code)
273 break;
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) {
277 printk(KERN_ERR
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);
282 ret = -EINVAL;
283 } else if (tpl->parse) {
284 ret = tpl->parse(card, func,
285 this->data, tpl_link);
286 }
287
288
289
290
291
292 if (!ret || ret != -EILSEQ)
293 kfree(this);
294 } else {
295
296 ret = -EILSEQ;
297 }
298
299 if (ret == -EILSEQ) {
300
301 this->next = NULL;
302 this->code = tpl_code;
303 this->size = tpl_link;
304 *prev = this;
305 prev = &this->next;
306 printk(KERN_DEBUG
307 "%s: queuing CIS tuple 0x%02x length %u\n",
308 mmc_hostname(card->host), tpl_code, tpl_link);
309
310 ret = 0;
311 }
312
313 ptr += tpl_link;
314 } while (!ret);
315
316
317
318
319
320 if (func)
321 *prev = card->tuples;
322
323 return ret;
324}
325
326int sdio_read_common_cis(struct mmc_card *card)
327{
328 return sdio_read_cis(card, NULL);
329}
330
331void sdio_free_common_cis(struct mmc_card *card)
332{
333 struct sdio_func_tuple *tuple, *victim;
334
335 tuple = card->tuples;
336
337 while (tuple) {
338 victim = tuple;
339 tuple = tuple->next;
340 kfree(victim);
341 }
342
343 card->tuples = NULL;
344}
345
346int sdio_read_func_cis(struct sdio_func *func)
347{
348 int ret;
349
350 ret = sdio_read_cis(func->card, func);
351 if (ret)
352 return ret;
353
354
355
356
357
358 get_device(&func->card->dev);
359
360
361
362
363
364 if (func->vendor == 0) {
365 func->vendor = func->card->cis.vendor;
366 func->device = func->card->cis.device;
367 }
368
369 return 0;
370}
371
372void sdio_free_func_cis(struct sdio_func *func)
373{
374 struct sdio_func_tuple *tuple, *victim;
375
376 tuple = func->tuples;
377
378 while (tuple && tuple != func->card->tuples) {
379 victim = tuple;
380 tuple = tuple->next;
381 kfree(victim);
382 }
383
384 func->tuples = NULL;
385
386
387
388
389
390 put_device(&func->card->dev);
391}
392
393