sdio: link unknown CIS tuples to the sdio_func structure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / core / sdio_cis.c
blobb6c7342572c13ef179139e7dfc62ab629442eebd
1 /*
2 * linux/drivers/mmc/core/sdio_cis.c
4 * Author: Nicolas Pitre
5 * Created: June 11, 2007
6 * Copyright: MontaVista Software Inc.
8 * Copyright 2007 Pierre Ossman
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.
16 #include <linux/kernel.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/sdio.h>
20 #include <linux/mmc/sdio_func.h>
22 #include "sdio_cis.h"
23 #include "sdio_ops.h"
25 static int cistpl_manfid(struct sdio_func *func,
26 const unsigned char *buf,
27 unsigned size)
29 /* TPLMID_MANF */
30 func->vendor = buf[0] | (buf[1] << 8);
32 /* TPLMID_CARD */
33 func->device = buf[2] | (buf[3] << 8);
35 return 0;
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);
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 */ },
51 int sdio_read_cis(struct sdio_func *func)
53 int ret;
54 struct sdio_func_tuple *this, **prev;
55 unsigned i, ptr = 0;
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);
66 /* find the list tail */
67 for (prev = &func->tuples; *prev; prev = &(*prev)->next);
69 do {
70 unsigned char tpl_code, tpl_link;
72 ret = mmc_io_rw_direct(func->card, 0, 0, ptr++, 0, &tpl_code);
73 if (ret)
74 break;
76 /* 0xff means we're done */
77 if (tpl_code == 0xff)
78 break;
80 ret = mmc_io_rw_direct(func->card, 0, 0, ptr++, 0, &tpl_link);
81 if (ret)
82 break;
84 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
85 if (!this)
86 return -ENOMEM;
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;
94 if (ret) {
95 kfree(this);
96 break;
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);
124 ptr += tpl_link;
125 } while (!ret);
127 return ret;
130 void sdio_free_cis(struct sdio_func *func)
132 struct sdio_func_tuple *tuple, *victim;
134 tuple = func->tuples;
136 while (tuple) {
137 victim = tuple;
138 tuple = tuple->next;
139 kfree(victim);
142 func->tuples = NULL;