2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 static const char rcsid[] =
29 "$FreeBSD: src/usr.sbin/pccard/pccardd/readcis.c,v 1.17.2.4 2001/05/23 21:53:50 dmlb Exp $";
33 * Code cleanup, bug-fix and extension
34 * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
42 #include <sys/ioctl.h>
44 #include <pccard/cardinfo.h>
45 #include <pccard/cis.h>
50 static int rex5588 = 0;
53 static int read_attr(int, char *, int);
54 static int ck_linktarget(int, off_t, int);
55 static void cis_info(struct cis *, unsigned char *, int);
56 static void device_desc(unsigned char *, int, struct dev_mem *);
57 static void config_map(struct cis *, unsigned char *, int);
58 static void cis_config(struct cis *, unsigned char *, int);
59 static void cis_func_id(struct cis *, unsigned char *, int);
60 static void cis_network_ext(struct cis *, unsigned char *, int);
61 static struct tuple_list *read_one_tuplelist(int, int, off_t);
62 static struct tuple_list *read_tuples(int);
63 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
64 static struct tuple_info *get_tuple_info(unsigned char);
66 static struct tuple_info tuple_info[] = {
67 {"Null tuple", 0x00, 0},
68 {"Common memory descriptor", 0x01, 255},
69 {"Long link to next chain for CardBus", 0x02, 255},
70 {"Indirect access", 0x03, 255},
71 {"Configuration map for CardBus", 0x04, 255},
72 {"Configuration entry for CardBus", 0x05, 255},
73 {"Long link to next chain for MFC", 0x06, 255},
74 {"Base address register for CardBus", 0x07, 6},
75 {"Checksum", 0x10, 5},
76 {"Long link to attribute memory", 0x11, 4},
77 {"Long link to common memory", 0x12, 4},
78 {"Link target", 0x13, 3},
80 {"Version 1 info", 0x15, 255},
81 {"Alternate language string", 0x16, 255},
82 {"Attribute memory descriptor", 0x17, 255},
83 {"JEDEC descr for common memory", 0x18, 255},
84 {"JEDEC descr for attribute memory", 0x19, 255},
85 {"Configuration map", 0x1A, 255},
86 {"Configuration entry", 0x1B, 255},
87 {"Other conditions for common memory", 0x1C, 255},
88 {"Other conditions for attribute memory", 0x1D, 255},
89 {"Geometry info for common memory", 0x1E, 255},
90 {"Geometry info for attribute memory", 0x1F, 255},
91 {"Manufacturer ID", 0x20, 4},
92 {"Functional ID", 0x21, 2},
93 {"Functional EXT", 0x22, 255},
94 {"Software interleave", 0x23, 2},
95 {"Version 2 Info", 0x40, 255},
96 {"Data format", 0x41, 255},
97 {"Geometry", 0x42, 4},
98 {"Byte order", 0x43, 2},
99 {"Card init date", 0x44, 4},
100 {"Battery replacement", 0x45, 4},
101 {"Organization", 0x46, 255},
102 {"Terminator", 0xFF, 0},
107 * After reading the tuples, decode the relevant ones.
112 struct tuple_list *tl;
116 cp = xmalloc(sizeof(*cp));
117 cp->tlist = read_tuples(fd);
121 for (tl = cp->tlist; tl; tl = tl->next)
122 for (tp = tl->tuples; tp; tp = tp->next) {
124 printf("tuple code = 0x%02x, data is\n", tp->code);
125 dump(tp->data, tp->length);
128 case CIS_MEM_COMMON: /* 0x01 */
129 device_desc(tp->data, tp->length, &cp->common_mem);
131 case CIS_INFO_V1: /* 0x15 */
132 cis_info(cp, tp->data, tp->length);
134 case CIS_MEM_ATTR: /* 0x17 */
135 device_desc(tp->data, tp->length, &cp->attr_mem);
137 case CIS_CONF_MAP: /* 0x1A */
138 config_map(cp, tp->data, tp->length);
140 case CIS_CONFIG: /* 0x1B */
141 cis_config(cp, tp->data, tp->length);
143 case CIS_FUNC_ID: /* 0x21 */
144 cis_func_id(cp, tp->data, tp->length);
146 case CIS_FUNC_EXT: /* 0x22 */
147 if (cp->func_id1 == 6) /* LAN adaptor */
148 cis_network_ext(cp, tp->data, tp->length);
156 * free_cis - delete cis entry.
159 freecis(struct cis *cp)
161 struct cis_ioblk *io;
162 struct cis_memblk *mem;
163 struct cis_config *conf;
165 struct tuple_list *tl;
167 while ((tl = cp->tlist) != 0) {
168 cp->tlist = tl->next;
169 while ((tp = tl->tuples) != 0) {
170 tl->tuples = tp->next;
176 while ((conf = cp->conf) != 0) {
177 cp->conf = conf->next;
178 while ((io = conf->io) != 0) {
182 while ((mem = conf->mem) != 0) {
183 conf->mem = mem->next;
192 * Fills in CIS version data.
195 cis_info(struct cis *cp, unsigned char *p, int len)
204 if (len > 1 && *p != 0xff) {
205 cp->manuf = strdup(p);
206 while (*p++ && --len > 0);
212 if (len > 1 && *p != 0xff) {
213 cp->vers = strdup(p);
214 while (*p++ && --len > 0);
216 cp->vers = strdup("?");
220 cp->add_info1 = NULL;
222 if (len > 1 && *p != 0xff) {
223 cp->add_info1 = strdup(p);
224 while (*p++ && --len > 0);
228 cp->add_info2 = NULL;
230 if (len > 1 && *p != 0xff)
231 cp->add_info2 = strdup(p);
235 * Fills in CIS function ID.
238 cis_func_id(struct cis *cp, unsigned char *p, int len)
245 cis_network_ext(struct cis *cp, unsigned char *p, int len)
250 case 4: /* Node ID */
251 if (len <= 2 || len < p[1] + 2)
256 cp->lan_nid = xmalloc(p[1]);
258 for (i = 0; i <= p[1]; i++)
259 cp->lan_nid[i] = p[i + 1];
265 * "FUJITSU LAN Card (FMV-J182)" has broken CIS
268 fmvj182_check(unsigned char *p)
270 char manuf[BUFSIZ], vers[BUFSIZ];
272 p++; /* major version */
273 p++; /* minor version */
274 strncpy(manuf, p, sizeof(manuf) - 1);
276 strncpy(vers, p, sizeof(vers) - 1);
277 if (!strcmp(manuf, "FUJITSU") && !strcmp(vers, "LAN Card(FMV-J182)"))
285 * "RATOC LAN Card (REX-5588)" has broken CIS
288 rex5588_check(unsigned char *p)
290 char manuf[BUFSIZ], vers[BUFSIZ];
292 p++; /* major version */
293 p++; /* minor version */
294 strncpy(manuf, p, sizeof(manuf) - 1);
296 strncpy(vers, p, sizeof(manuf) - 1);
297 if (!strcmp(manuf, "PCMCIA LAN MBH10304 ES"))
306 * Broken CIS for "HITACHI MICROCOMPUTER SYSTEM LTD." "MSSHVPC02"
309 hss_check(unsigned char *p)
311 char manuf[BUFSIZ], vers[BUFSIZ];
313 p++; /* major version */
314 p++; /* minor version */
315 strncpy(manuf, p, sizeof(manuf) - 1);
317 strncpy(vers, p, sizeof(vers) - 1);
318 if (!strcmp(manuf, "HITACHI MICROCOMPUTER SYSTEMS LTD.")
319 && !strcmp(vers, "MSSHVPC02"))
327 * device_desc - decode device descriptor.
330 device_desc(unsigned char *p, int len, struct dev_mem *dp)
332 while (len > 0 && *p != 0xFF) {
334 dp->type = (*p & 0xF0) >> 4;
335 dp->wps = !!(*p & 0x8);
339 dp->addr = (*p >> 3) & 0xF;
348 * configuration map of card control register.
351 config_map(struct cis *cp, unsigned char *p, int len)
354 int rlen = (*p & 3) + 1;
357 cp->last_config = *p1++ & 0x3F;
358 cp->reg_addr = parse_num(rlen | 0x10, p1, &p1, 0);
363 * Parse variable length value.
366 parse_num(int sz, u_char *p, u_char **q, int ofs)
380 num = tpl16(p) + ofs;
384 num = tpl24(p) + ofs;
389 num = tpl32(p) + ofs;
399 * CIS config entry - Decode and build configuration entry.
402 cis_config(struct cis *cp, unsigned char *p, int len)
406 struct cis_config *conf, *last;
409 conf = xmalloc(sizeof(*conf));
410 if ((last = cp->conf) != 0) {
416 conf->id = *p & 0x3F; /* Config index */
418 if (rex5588 && conf->id >= 0x08 && conf->id <= 0x1d)
421 if (*p & 0x40) /* Default flag */
422 cp->def_config = conf;
424 p++; /* Interface byte skip */
425 feat = *p++; /* Features byte */
426 for (i = 0; i < CIS_FEAT_POWER(feat); i++) {
427 unsigned char parms = *p++;
430 for (j = 0; j < 8; j++)
431 if (parms & (1 << j))
434 if (feat & CIS_FEAT_TIMING) {
437 if (CIS_WAIT_SCALE(i) != 3)
439 if (CIS_READY_SCALE(i) != 7)
441 if (CIS_RESERVED_SCALE(i) != 7)
444 if (feat & CIS_FEAT_I_O) {
446 if (CIS_IO_RANGE & *p)
447 conf->io_blks = CIS_IO_BLKS(p[1]) + 1;
448 conf->io_addr = CIS_IO_ADDR(*p);
449 conf->io_bus = (*p >> 5) & 3; /* CIS_IO_8BIT | CIS_IO_16BIT */
450 if (*p++ & CIS_IO_RANGE) {
451 struct cis_ioblk *io;
452 struct cis_ioblk *last_io = NULL;
455 j = CIS_IO_BLKSZ(*p++);
456 for (x = 0; x < conf->io_blks; x++) {
457 io = xmalloc(sizeof(*io));
463 io->addr = parse_num(i, p, &p, 0);
464 io->size = parse_num(j, p, &p, 1);
468 if (feat & CIS_FEAT_IRQ) {
470 conf->irqlevel = *p & 0xF;
471 conf->irq_flags = *p & 0xF0;
472 if (*p++ & CIS_IRQ_MASK) {
473 conf->irq_mask = tpl16(p);
477 switch (CIS_FEAT_MEMORY(feat)) {
478 case CIS_FEAT_MEM_NONE:
480 case CIS_FEAT_MEM_LEN:
482 conf->mem = xmalloc(sizeof(*conf->mem));
483 conf->mem->length = tpl16(p) << 8;
485 case CIS_FEAT_MEM_ADDR:
487 conf->mem = xmalloc(sizeof(*conf->mem));
488 conf->mem->length = tpl16(p) << 8;
489 conf->mem->address = tpl16(p + 2) << 8;
491 case CIS_FEAT_MEM_WIN: {
492 struct cis_memblk *mem;
493 struct cis_memblk *last_mem = NULL;
497 conf->memwins = CIS_MEM_WINS(x);
498 for (i = 0; i < conf->memwins; i++) {
499 mem = xmalloc(sizeof(*mem));
501 last_mem->next = mem;
505 mem->length = parse_num(CIS_MEM_LENSZ(x) | 0x10, p, &p, 0) << 8;
506 mem->address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10, p, &p, 0) << 8;
507 if (x & CIS_MEM_HOST) {
508 mem->host_address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10,
515 if (feat & CIS_FEAT_MISC) {
516 conf->misc_valid = 1;
522 * Read the tuples from the card.
523 * The processing of tuples is as follows:
524 * - Read tuples at attribute memory, offset 0.
525 * - If a CIS_END is the first tuple, look for
526 * a tuple list at common memory offset 0; this list
527 * must start with a LINKTARGET.
528 * - If a long link tuple was encountered, execute the long
530 * - If a no-link tuple was seen, terminate processing.
531 * - If no no-link tuple exists, and no long link tuple
532 * exists while processing the primary tuple list,
533 * then look for a LINKTARGET tuple in common memory.
534 * - If a long link tuple is found in any list, then process
535 * it. Only one link is allowed per list.
537 static struct tuple_list *tlist;
539 static struct tuple_list *
542 struct tuple_list *tl = 0, *last_tl;
548 last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
550 /* Now start processing the links (if any). */
553 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
556 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
558 if (tp && tp->length == 4) {
559 offs = tpl32(tp->data);
561 printf("Checking long link at %qd (%s memory)\n",
562 offs, flag ? "Attribute" : "Common");
564 /* If a link was found, read the tuple list from it. */
565 if (ck_linktarget(fd, offs, flag)) {
566 tl = read_one_tuplelist(fd, flag, offs);
575 * If the primary list had no NOLINK tuple, and no LINKTARGET,
576 * then try to read a tuple list at common memory (offset 0).
578 if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 && tlist->next == 0 &&
579 ck_linktarget(fd, (off_t) 0, 0)) {
581 printf("Reading long link at %qd (%s memory)\n",
582 offs, flag ? "Attribute" : "Common");
584 tlist->next = read_one_tuplelist(fd, 0, (off_t) 0);
590 * Read one tuple list from the card.
592 static struct tuple_list *
593 read_one_tuplelist(int fd, int flags, off_t offs)
595 struct tuple *tp, *last_tp = 0;
596 struct tuple_list *tl;
597 struct tuple_info *tinfo;
599 unsigned char code, length;
605 /* Check to see if this memory has already been scanned. */
606 for (tl = tlist; tl; tl = tl->next)
607 if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
609 tl = xmalloc(sizeof(*tl));
611 tl->flags = flags & MDF_ATTR;
612 ioctl(fd, PIOCRWFLAG, &flags);
613 lseek(fd, offs, SEEK_SET);
615 if (read_attr(fd, &code, 1) != 1) {
616 warn("CIS code read");
620 if (code == CIS_NULL)
622 tp = xmalloc(sizeof(*tp));
627 if (read_attr(fd, &length, 1) != 1) {
628 warn("CIS len read");
632 if (fmvj182 && (code == 0x1b) && (length == 25))
637 printf("Tuple code = 0x%x, len = %d\n", code, length);
639 if (length == 0xFF) {
640 length = tp->length = 0;
645 tp->data = xmalloc(length);
646 if (read_attr(fd, tp->data, length) != length) {
653 * Check the tuple, and ignore it if it isn't in the table
654 * or the length is illegal.
656 tinfo = get_tuple_info(code);
657 if (code == CIS_INFO_V1) {
658 /* Hack for broken CIS of FMV-J182 Ethernet card */
659 fmvj182 = fmvj182_check(tp->data);
661 /* Hack for RATOC LAN card */
662 rex5588 = rex5588_check(tp->data);
663 #endif /* RATOCLAN */
665 /* Hack for Hitachi Speech Synthesis card */
666 hss = hss_check(tp->data);
669 if (tinfo == NULL || (tinfo->length != 255 && tinfo->length > length)) {
670 printf("code %s ignored\n", tuple_name(code));
673 if (tl->tuples == NULL)
678 } while (code != CIS_END && total < 1024);
683 * return true if the offset points to a LINKTARGET tuple.
686 ck_linktarget(int fd, off_t offs, int flag)
690 ioctl(fd, PIOCRWFLAG, &flag);
691 lseek(fd, offs, SEEK_SET);
692 if (read_attr(fd, blk, 5) != 5)
694 if (blk[0] == 0x13 &&
704 * find_tuple_in_list - find a tuple within a
707 static struct tuple *
708 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
712 for (tp = tl->tuples; tp; tp = tp->next)
713 if (tp->code == code)
719 read_attr(int fd, char *bp, int len)
721 char blk[1024], *p = blk;
724 if (len > sizeof(blk) / 2)
725 len = sizeof(blk) / 2;
726 l = i = read(fd, blk, len * 2);
728 printf("Read return %d bytes (expected %d)\n", i, len * 2);
740 * return table entry for code.
742 static struct tuple_info *
743 get_tuple_info(unsigned char code)
745 struct tuple_info *tp;
747 for (tp = tuple_info; tp->name; tp++)
748 if (tp->code == code)
750 printf("Code %d not found\n", code);
755 tuple_name(unsigned char code)
757 struct tuple_info *tp;
759 tp = get_tuple_info(code);