sdio: link unknown CIS tuples to the sdio_func structure
[linux-2.6] / 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/sdio.h>
20 #include <linux/mmc/sdio_func.h>
21
22 #include "sdio_cis.h"
23 #include "sdio_ops.h"
24
25 static int cistpl_manfid(struct sdio_func *func,
26                          const unsigned char *buf,
27                          unsigned size)
28 {
29         /* TPLMID_MANF */
30         func->vendor = buf[0] | (buf[1] << 8);
31
32         /* TPLMID_CARD */
33         func->device = buf[2] | (buf[3] << 8);
34
35         return 0;
36 }
37
38 struct cis_tpl {
39         unsigned char code;
40         unsigned char min_size;
41         int (*parse)(struct sdio_func *, const unsigned char *buf, unsigned size);
42 };
43
44 static const struct cis_tpl cis_tpl_list[] = {
45         {       0x15,   3,      /* cistpl_vers_1 */     },
46         {       0x20,   4,      cistpl_manfid           },
47         {       0x21,   2,      /* cistpl_funcid */     },
48         {       0x22,   0,      /* cistpl_funce */      },
49 };
50
51 int sdio_read_cis(struct sdio_func *func)
52 {
53         int ret;
54         struct sdio_func_tuple *this, **prev;
55         unsigned i, ptr = 0;
56
57         for (i = 0; i < 3; i++) {
58                 unsigned char x;
59                 ret = mmc_io_rw_direct(func->card, 0, 0,
60                                 func->num * 0x100 + SDIO_FBR_CIS + i, 0, &x);
61                 if (ret)
62                         return ret;
63                 ptr |= x << (i * 8);
64         }
65
66         /* find the list tail */
67         for (prev = &func->tuples; *prev; prev = &(*prev)->next);
68
69         do {
70                 unsigned char tpl_code, tpl_link;
71
72                 ret = mmc_io_rw_direct(func->card, 0, 0, ptr++, 0, &tpl_code);
73                 if (ret)
74                         break;
75
76                 /* 0xff means we're done */
77                 if (tpl_code == 0xff)
78                         break;
79
80                 ret = mmc_io_rw_direct(func->card, 0, 0, ptr++, 0, &tpl_link);
81                 if (ret)
82                         break;
83
84                 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
85                 if (!this)
86                         return -ENOMEM;
87
88                 for (i = 0; i < tpl_link; i++) {
89                         ret = mmc_io_rw_direct(func->card, 0, 0,
90                                                ptr + i, 0, &this->data[i]);
91                         if (ret)
92                                 break;
93                 }
94                 if (ret) {
95                         kfree(this);
96                         break;
97                 }
98
99                 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
100                         if (cis_tpl_list[i].code == tpl_code)
101                                 break;
102                 if (i >= ARRAY_SIZE(cis_tpl_list)) {
103                         /* this tuple is unknown to the core */
104                         this->next = NULL;
105                         this->code = tpl_code;
106                         this->size = tpl_link;
107                         *prev = this;
108                         prev = &this->next;
109                         printk(KERN_DEBUG
110                                "%s: queuing CIS tuple 0x%02x length %u\n",
111                                sdio_func_id(func), tpl_code, tpl_link);
112                 } else {
113                         const struct cis_tpl *tpl = cis_tpl_list + i;
114                         if (tpl_link < tpl->min_size) {
115                                 printk(KERN_ERR
116                                        "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
117                                        sdio_func_id(func), tpl_code, tpl_link, tpl->min_size);
118                                 ret = -EINVAL;
119                         } else if (tpl->parse)
120                                 ret = tpl->parse(func, this->data, tpl_link);
121                         kfree(this);
122                 }
123
124                 ptr += tpl_link;
125         } while (!ret);
126
127         return ret;
128 }
129
130 void sdio_free_cis(struct sdio_func *func)
131 {
132         struct sdio_func_tuple *tuple, *victim;
133
134         tuple = func->tuples;
135
136         while (tuple) {
137                 victim = tuple;
138                 tuple = tuple->next;
139                 kfree(victim);
140         }
141
142         func->tuples = NULL;
143 }
144