]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/sdio_cis.c
sdio: initial CIS parsing code
[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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  */
13
14 #include <linux/kernel.h>
15
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
19
20 #include "sdio_cis.h"
21 #include "sdio_ops.h"
22
23 static int cistpl_manfid(struct sdio_func *func,
24                          const unsigned char *buf,
25                          unsigned size)
26 {
27         /* TPLMID_MANF */
28         func->vendor = buf[0] | (buf[1] << 8);
29
30         /* TPLMID_CARD */
31         func->device = buf[2] | (buf[3] << 8);
32
33         return 0;
34 }
35
36 struct cis_tpl {
37         unsigned char code;
38         unsigned char min_size;
39         int (*parse)(struct sdio_func *, const unsigned char *buf, unsigned size);
40 };
41
42 static const struct cis_tpl cis_tpl_list[] = {
43         {       0x15,   3,      /* cistpl_vers_1 */     },
44         {       0x20,   4,      cistpl_manfid           },
45         {       0x21,   2,      /* cistpl_funcid */     },
46         {       0x22,   0,      /* cistpl_funce */      },
47 };
48
49 int sdio_read_cis(struct sdio_func *func)
50 {
51         int ret;
52         unsigned char *buf;
53         unsigned i, ptr = 0;
54
55         for (i = 0; i < 3; i++) {
56                 unsigned char x;
57                 ret = mmc_io_rw_direct(func->card, 0, 0,
58                                 func->num * 0x100 + SDIO_FBR_CIS + i, 0, &x);
59                 if (ret)
60                         return ret;
61                 ptr |= x << (i * 8);
62         }
63
64         buf = kmalloc(256, GFP_KERNEL);
65         if (!buf)
66                 return -ENOMEM;
67
68         do {
69                 unsigned char tpl_code, tpl_link;
70                 const struct cis_tpl *tpl;
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                 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
85                         if (cis_tpl_list[i].code == tpl_code)
86                                 break;
87                 if (i >= ARRAY_SIZE(cis_tpl_list)) {
88                         printk(KERN_WARNING
89                                "%s: unknown CIS tuple 0x%02x of length %u\n",
90                                sdio_func_id(func), tpl_code, tpl_link);
91                         ptr += tpl_link;
92                         continue;
93                 }
94                 tpl = cis_tpl_list + i;
95
96                 if (tpl_link < tpl->min_size) {
97                         printk(KERN_ERR
98                                "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u\n",
99                                sdio_func_id(func), tpl_code, tpl_link, tpl->min_size);
100                         ret = -EINVAL;
101                         break;
102                 }
103
104                 for (i = 0; i < tpl_link; i++) {
105                         ret = mmc_io_rw_direct(func->card, 0, 0, ptr + i, 0, &buf[i]);
106                         if (ret)
107                                 break;
108                 }
109                 if (ret)
110                         break;
111                 ptr += tpl_link;
112
113                 if (tpl->parse)
114                         ret = tpl->parse(func, buf, tpl_link);
115         } while (!ret);
116
117         kfree(buf);
118         return ret;
119 }