]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/sdio_cis.c
sdio: store vendor strings
[linux-2.6-omap-h63xx.git] / drivers / mmc / core / sdio_cis.c
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  *
8  * Copyright 2007 Pierre Ossman
9  *
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>
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
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
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
74 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
75                          const unsigned char *buf, unsigned size)
76 {
77         unsigned int vendor, device;
78
79         /* TPLMID_MANF */
80         vendor = buf[0] | (buf[1] << 8);
81
82         /* TPLMID_CARD */
83         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         }
92
93         return 0;
94 }
95
96 static const unsigned char speed_val[16] =
97         { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
98 static const unsigned int speed_unit[8] =
99         { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
100
101 static 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
117 static 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 */
130         func->max_blksize = buf[12] | (buf[13] << 8);
131
132         return 0;
133 }
134
135 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
136                         const unsigned char *buf, unsigned size)
137 {
138         int ret;
139
140         /*
141          * There should be two versions of the CISTPL_FUNCE tuple,
142          * one for the common CIS (function 0) and a version used by
143          * the individual function's CIS (1-7). Yet, the later has a
144          * different length depending on the SDIO spec version.
145          */
146         if (func)
147                 ret = cistpl_funce_func(func, buf, size);
148         else
149                 ret = cistpl_funce_common(card, buf, size);
150
151         if (ret) {
152                 printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
153                        "type %u\n", mmc_hostname(card->host), size, buf[0]);
154                 return ret;
155         }
156
157         return 0;
158 }
159
160 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
161                            const unsigned char *, unsigned);
162
163 struct cis_tpl {
164         unsigned char code;
165         unsigned char min_size;
166         tpl_parse_t *parse;
167 };
168
169 static const struct cis_tpl cis_tpl_list[] = {
170         {       0x15,   3,      cistpl_vers_1           },
171         {       0x20,   4,      cistpl_manfid           },
172         {       0x21,   2,      /* cistpl_funcid */     },
173         {       0x22,   0,      cistpl_funce            },
174 };
175
176 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
177 {
178         int ret;
179         struct sdio_func_tuple *this, **prev;
180         unsigned i, ptr = 0;
181
182         /*
183          * Note that this works for the common CIS (function number 0) as
184          * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
185          * have the same offset.
186          */
187         for (i = 0; i < 3; i++) {
188                 unsigned char x, fn;
189
190                 if (func)
191                         fn = func->num;
192                 else
193                         fn = 0;
194
195                 ret = mmc_io_rw_direct(card, 0, 0,
196                         SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
197                 if (ret)
198                         return ret;
199                 ptr |= x << (i * 8);
200         }
201
202         if (func)
203                 prev = &func->tuples;
204         else
205                 prev = &card->tuples;
206
207         BUG_ON(*prev);
208
209         do {
210                 unsigned char tpl_code, tpl_link;
211
212                 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
213                 if (ret)
214                         break;
215
216                 /* 0xff means we're done */
217                 if (tpl_code == 0xff)
218                         break;
219
220                 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
221                 if (ret)
222                         break;
223
224                 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
225                 if (!this)
226                         return -ENOMEM;
227
228                 for (i = 0; i < tpl_link; i++) {
229                         ret = mmc_io_rw_direct(card, 0, 0,
230                                                ptr + i, 0, &this->data[i]);
231                         if (ret)
232                                 break;
233                 }
234                 if (ret) {
235                         kfree(this);
236                         break;
237                 }
238
239                 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
240                         if (cis_tpl_list[i].code == tpl_code)
241                                 break;
242                 if (i >= ARRAY_SIZE(cis_tpl_list)) {
243                         /* this tuple is unknown to the core */
244                         this->next = NULL;
245                         this->code = tpl_code;
246                         this->size = tpl_link;
247                         *prev = this;
248                         prev = &this->next;
249                         printk(KERN_DEBUG
250                                "%s: queuing CIS tuple 0x%02x length %u\n",
251                                mmc_hostname(card->host), tpl_code, tpl_link);
252                 } else {
253                         const struct cis_tpl *tpl = cis_tpl_list + i;
254                         if (tpl_link < tpl->min_size) {
255                                 printk(KERN_ERR
256                                        "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
257                                        mmc_hostname(card->host),
258                                        tpl_code, tpl_link, tpl->min_size);
259                                 ret = -EINVAL;
260                         } else if (tpl->parse) {
261                                 ret = tpl->parse(card, func,
262                                                  this->data, tpl_link);
263                         }
264                         kfree(this);
265                 }
266
267                 ptr += tpl_link;
268         } while (!ret);
269
270         /*
271          * Link in all unknown tuples found in the common CIS so that
272          * drivers don't have to go digging in two places.
273          */
274         if (func)
275                 *prev = card->tuples;
276
277         return ret;
278 }
279
280 int sdio_read_common_cis(struct mmc_card *card)
281 {
282         return sdio_read_cis(card, NULL);
283 }
284
285 void sdio_free_common_cis(struct mmc_card *card)
286 {
287         struct sdio_func_tuple *tuple, *victim;
288
289         tuple = card->tuples;
290
291         while (tuple) {
292                 victim = tuple;
293                 tuple = tuple->next;
294                 kfree(victim);
295         }
296
297         card->tuples = NULL;
298 }
299
300 int sdio_read_func_cis(struct sdio_func *func)
301 {
302         int ret;
303
304         ret = sdio_read_cis(func->card, func);
305         if (ret)
306                 return ret;
307
308         /*
309          * Since we've linked to tuples in the card structure,
310          * we must make sure we have a reference to it.
311          */
312         get_device(&func->card->dev);
313
314         /*
315          * Vendor/device id is optional for function CIS, so
316          * copy it from the card structure as needed.
317          */
318         if (func->vendor == 0) {
319                 func->vendor = func->card->cis.vendor;
320                 func->device = func->card->cis.device;
321         }
322
323         return 0;
324 }
325
326 void sdio_free_func_cis(struct sdio_func *func)
327 {
328         struct sdio_func_tuple *tuple, *victim;
329
330         tuple = func->tuples;
331
332         while (tuple && tuple != func->card->tuples) {
333                 victim = tuple;
334                 tuple = tuple->next;
335                 kfree(victim);
336         }
337
338         func->tuples = NULL;
339
340         /*
341          * We have now removed the link to the tuples in the
342          * card structure, so remove the reference.
343          */
344         put_device(&func->card->dev);
345 }
346