Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / sdmmc / sdmmc_cis.c
blob6d449fb31c4f730a192d23b9c7615c978ae1f475
1 /* $NetBSD$ */
2 /* $OpenBSD: sdmmc_cis.c,v 1.1 2006/06/01 21:53:41 uwe Exp $ */
4 /*
5 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Routines to decode the Card Information Structure of SD I/O cards */
22 #include <sys/cdefs.h>
23 __KERNEL_RCSID(0, "$NetBSD$");
25 #include <sys/param.h>
26 #include <sys/systm.h>
28 #include <dev/sdmmc/sdmmc_ioreg.h>
29 #include <dev/sdmmc/sdmmcdevs.h>
30 #include <dev/sdmmc/sdmmcvar.h>
32 #ifdef SDMMC_DEBUG
33 #define DPRINTF(s) printf s
34 #else
35 #define DPRINTF(s) /**/
36 #endif
38 static uint32_t sdmmc_cisptr(struct sdmmc_function *);
39 static uint32_t
40 sdmmc_cisptr(struct sdmmc_function *sf)
42 uint32_t cisptr = 0;
44 /* XXX where is the per-function CIS pointer register? */
45 if (sf->number != 0)
46 return SD_IO_CIS_START;
48 /* XXX is the CIS pointer stored in little-endian format? */
49 cisptr |= sdmmc_io_read_1(sf, SD_IO_CCCR_CISPTR + 0) << 0;
50 cisptr |= sdmmc_io_read_1(sf, SD_IO_CCCR_CISPTR + 1) << 8;
51 cisptr |= sdmmc_io_read_1(sf, SD_IO_CCCR_CISPTR + 2) << 16;
52 return cisptr;
55 int
56 sdmmc_read_cis(struct sdmmc_function *sf, struct sdmmc_cis *cis)
58 device_t dev = sf->sc->sc_dev;
59 uint32_t reg;
60 uint8_t tplcode;
61 uint8_t tpllen;
62 int start, ch, count;
63 int i;
65 memset(cis, 0, sizeof *cis);
67 /* XXX read per-function CIS */
68 if (sf->number != 0)
69 return 1;
71 reg = sdmmc_cisptr(sf);
72 if (reg < SD_IO_CIS_START ||
73 reg >= (SD_IO_CIS_START + SD_IO_CIS_SIZE - 16)) {
74 aprint_error_dev(dev, "bad CIS ptr %#x\n", reg);
75 return 1;
78 for (;;) {
79 tplcode = sdmmc_io_read_1(sf, reg++);
80 tpllen = sdmmc_io_read_1(sf, reg++);
82 if (tplcode == 0xff || tpllen == 0) {
83 if (tplcode != 0xff)
84 aprint_error_dev(dev, "CIS parse error at %d, "
85 "tuple code %#x, length %d\n",
86 reg, tplcode, tpllen);
87 break;
90 switch (tplcode) {
91 case SD_IO_CISTPL_FUNCID:
92 if (tpllen < 2) {
93 aprint_error_dev(dev,
94 "bad CISTPL_FUNCID length\n");
95 reg += tpllen;
96 break;
98 cis->function = sdmmc_io_read_1(sf, reg);
99 reg += tpllen;
100 break;
102 case SD_IO_CISTPL_MANFID:
103 if (tpllen < 4) {
104 aprint_error_dev(dev,
105 "bad CISTPL_MANFID length\n");
106 reg += tpllen;
107 break;
109 cis->manufacturer = sdmmc_io_read_1(sf, reg++);
110 cis->manufacturer |= sdmmc_io_read_1(sf, reg++) << 8;
111 cis->product = sdmmc_io_read_1(sf, reg++);
112 cis->product |= sdmmc_io_read_1(sf, reg++) << 8;
113 break;
115 case SD_IO_CISTPL_VERS_1:
116 if (tpllen < 2) {
117 aprint_error_dev(dev,
118 "CISTPL_VERS_1 too short\n");
119 reg += tpllen;
120 break;
123 cis->cis1_major = sdmmc_io_read_1(sf, reg++);
124 cis->cis1_minor = sdmmc_io_read_1(sf, reg++);
126 for (count = 0, start = 0, i = 0;
127 (count < 4) && ((i + 4) < 256); i++) {
128 ch = sdmmc_io_read_1(sf, reg + i);
129 if (ch == 0xff)
130 break;
131 cis->cis1_info_buf[i] = ch;
132 if (ch == 0) {
133 cis->cis1_info[count] =
134 cis->cis1_info_buf + start;
135 start = i + 1;
136 count++;
140 reg += tpllen - 2;
141 break;
143 default:
144 aprint_error_dev(dev,
145 "unknown tuple code %#x, length %d\n",
146 tplcode, tpllen);
147 reg += tpllen;
148 break;
152 return 0;
155 void
156 sdmmc_print_cis(struct sdmmc_function *sf)
158 device_t dev = sf->sc->sc_dev;
159 struct sdmmc_cis *cis = &sf->cis;
160 int i;
162 printf("%s: CIS version %u.%u\n", device_xname(dev), cis->cis1_major,
163 cis->cis1_minor);
165 printf("%s: CIS info: ", device_xname(dev));
166 for (i = 0; i < 4; i++) {
167 if (cis->cis1_info[i] == NULL)
168 break;
169 if (i != 0)
170 aprint_verbose(", ");
171 printf("%s", cis->cis1_info[i]);
173 printf("\n");
175 printf("%s: Manufacturer code 0x%x, product 0x%x\n", device_xname(dev),
176 cis->manufacturer, cis->product);
178 printf("%s: function %d: ", device_xname(dev), sf->number);
179 switch (sf->cis.function) {
180 case SDMMC_FUNCTION_WLAN:
181 printf("wireless network adapter");
182 break;
184 default:
185 printf("unknown function (%d)", sf->cis.function);
186 break;
188 printf("\n");
191 void
192 sdmmc_check_cis_quirks(struct sdmmc_function *sf)
194 char *p;
195 int i;
197 if (sf->cis.manufacturer == SDMMC_VENDOR_SPECTEC &&
198 sf->cis.product == SDMMC_PRODUCT_SPECTEC_SDW820) {
199 /* This card lacks the VERS_1 tuple. */
200 static const char cis1_info[] =
201 "Spectec\0SDIO WLAN Card\0SDW-820\0\0";
203 sf->cis.cis1_major = 0x01;
204 sf->cis.cis1_minor = 0x00;
206 p = sf->cis.cis1_info_buf;
207 strlcpy(p, cis1_info, sizeof(sf->cis.cis1_info_buf));
208 for (i = 0; i < 4; i++) {
209 sf->cis.cis1_info[i] = p;
210 p += strlen(p) + 1;