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.
26 * $FreeBSD: src/usr.sbin/pccard/pccardd/readcis.c,v 1.17.2.4 2001/05/23 21:53:50 dmlb Exp $
27 * $DragonFly: src/usr.sbin/pccard/pccardd/Attic/readcis.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
31 * Code cleanup, bug-fix and extension
32 * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
40 #include <sys/ioctl.h>
42 #include <pccard/cardinfo.h>
43 #include <pccard/cis.h>
48 static int rex5588 = 0;
51 static int read_attr(int, char *, int);
52 static int ck_linktarget(int, off_t, int);
53 static void cis_info(struct cis *, unsigned char *, int);
54 static void device_desc(unsigned char *, int, struct dev_mem *);
55 static void config_map(struct cis *, unsigned char *, int);
56 static void cis_config(struct cis *, unsigned char *, int);
57 static void cis_func_id(struct cis *, unsigned char *, int);
58 static void cis_network_ext(struct cis *, unsigned char *, int);
59 static struct tuple_list *read_one_tuplelist(int, int, off_t);
60 static struct tuple_list *read_tuples(int);
61 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
62 static struct tuple_info *get_tuple_info(unsigned char);
64 static struct tuple_info tuple_info[] = {
65 {"Null tuple", 0x00, 0},
66 {"Common memory descriptor", 0x01, 255},
67 {"Long link to next chain for CardBus", 0x02, 255},
68 {"Indirect access", 0x03, 255},
69 {"Configuration map for CardBus", 0x04, 255},
70 {"Configuration entry for CardBus", 0x05, 255},
71 {"Long link to next chain for MFC", 0x06, 255},
72 {"Base address register for CardBus", 0x07, 6},
73 {"Checksum", 0x10, 5},
74 {"Long link to attribute memory", 0x11, 4},
75 {"Long link to common memory", 0x12, 4},
76 {"Link target", 0x13, 3},
78 {"Version 1 info", 0x15, 255},
79 {"Alternate language string", 0x16, 255},
80 {"Attribute memory descriptor", 0x17, 255},
81 {"JEDEC descr for common memory", 0x18, 255},
82 {"JEDEC descr for attribute memory", 0x19, 255},
83 {"Configuration map", 0x1A, 255},
84 {"Configuration entry", 0x1B, 255},
85 {"Other conditions for common memory", 0x1C, 255},
86 {"Other conditions for attribute memory", 0x1D, 255},
87 {"Geometry info for common memory", 0x1E, 255},
88 {"Geometry info for attribute memory", 0x1F, 255},
89 {"Manufacturer ID", 0x20, 4},
90 {"Functional ID", 0x21, 2},
91 {"Functional EXT", 0x22, 255},
92 {"Software interleave", 0x23, 2},
93 {"Version 2 Info", 0x40, 255},
94 {"Data format", 0x41, 255},
95 {"Geometry", 0x42, 4},
96 {"Byte order", 0x43, 2},
97 {"Card init date", 0x44, 4},
98 {"Battery replacement", 0x45, 4},
99 {"Organization", 0x46, 255},
100 {"Terminator", 0xFF, 0},
105 * After reading the tuples, decode the relevant ones.
110 struct tuple_list *tl;
114 cp = xmalloc(sizeof(*cp));
115 cp->tlist = read_tuples(fd);
119 for (tl = cp->tlist; tl; tl = tl->next)
120 for (tp = tl->tuples; tp; tp = tp->next) {
122 printf("tuple code = 0x%02x, data is\n", tp->code);
123 dump(tp->data, tp->length);
126 case CIS_MEM_COMMON: /* 0x01 */
127 device_desc(tp->data, tp->length, &cp->common_mem);
129 case CIS_INFO_V1: /* 0x15 */
130 cis_info(cp, tp->data, tp->length);
132 case CIS_MEM_ATTR: /* 0x17 */
133 device_desc(tp->data, tp->length, &cp->attr_mem);
135 case CIS_CONF_MAP: /* 0x1A */
136 config_map(cp, tp->data, tp->length);
138 case CIS_CONFIG: /* 0x1B */
139 cis_config(cp, tp->data, tp->length);
141 case CIS_FUNC_ID: /* 0x21 */
142 cis_func_id(cp, tp->data, tp->length);
144 case CIS_FUNC_EXT: /* 0x22 */
145 if (cp->func_id1 == 6) /* LAN adaptor */
146 cis_network_ext(cp, tp->data, tp->length);
154 * free_cis - delete cis entry.
157 freecis(struct cis *cp)
159 struct cis_ioblk *io;
160 struct cis_memblk *mem;
161 struct cis_config *conf;
163 struct tuple_list *tl;
165 while ((tl = cp->tlist) != 0) {
166 cp->tlist = tl->next;
167 while ((tp = tl->tuples) != 0) {
168 tl->tuples = tp->next;
174 while ((conf = cp->conf) != 0) {
175 cp->conf = conf->next;
176 while ((io = conf->io) != 0) {
180 while ((mem = conf->mem) != 0) {
181 conf->mem = mem->next;
190 * Fills in CIS version data.
193 cis_info(struct cis *cp, unsigned char *p, int len)
202 if (len > 1 && *p != 0xff) {
203 cp->manuf = strdup(p);
204 while (*p++ && --len > 0);
210 if (len > 1 && *p != 0xff) {
211 cp->vers = strdup(p);
212 while (*p++ && --len > 0);
214 cp->vers = strdup("?");
218 cp->add_info1 = NULL;
220 if (len > 1 && *p != 0xff) {
221 cp->add_info1 = strdup(p);
222 while (*p++ && --len > 0);
226 cp->add_info2 = NULL;
228 if (len > 1 && *p != 0xff)
229 cp->add_info2 = strdup(p);
233 * Fills in CIS function ID.
236 cis_func_id(struct cis *cp, unsigned char *p, int len)
243 cis_network_ext(struct cis *cp, unsigned char *p, int len)
248 case 4: /* Node ID */
249 if (len <= 2 || len < p[1] + 2)
254 cp->lan_nid = xmalloc(p[1]);
256 for (i = 0; i <= p[1]; i++)
257 cp->lan_nid[i] = p[i + 1];
263 * "FUJITSU LAN Card (FMV-J182)" has broken CIS
266 fmvj182_check(unsigned char *p)
268 char manuf[BUFSIZ], vers[BUFSIZ];
270 p++; /* major version */
271 p++; /* minor version */
272 strncpy(manuf, p, sizeof(manuf) - 1);
274 strncpy(vers, p, sizeof(vers) - 1);
275 if (!strcmp(manuf, "FUJITSU") && !strcmp(vers, "LAN Card(FMV-J182)"))
283 * "RATOC LAN Card (REX-5588)" has broken CIS
286 rex5588_check(unsigned char *p)
288 char manuf[BUFSIZ], vers[BUFSIZ];
290 p++; /* major version */
291 p++; /* minor version */
292 strncpy(manuf, p, sizeof(manuf) - 1);
294 strncpy(vers, p, sizeof(manuf) - 1);
295 if (!strcmp(manuf, "PCMCIA LAN MBH10304 ES"))
304 * Broken CIS for "HITACHI MICROCOMPUTER SYSTEM LTD." "MSSHVPC02"
307 hss_check(unsigned char *p)
309 char manuf[BUFSIZ], vers[BUFSIZ];
311 p++; /* major version */
312 p++; /* minor version */
313 strncpy(manuf, p, sizeof(manuf) - 1);
315 strncpy(vers, p, sizeof(vers) - 1);
316 if (!strcmp(manuf, "HITACHI MICROCOMPUTER SYSTEMS LTD.")
317 && !strcmp(vers, "MSSHVPC02"))
325 * device_desc - decode device descriptor.
328 device_desc(unsigned char *p, int len, struct dev_mem *dp)
330 while (len > 0 && *p != 0xFF) {
332 dp->type = (*p & 0xF0) >> 4;
333 dp->wps = !!(*p & 0x8);
337 dp->addr = (*p >> 3) & 0xF;
346 * configuration map of card control register.
349 config_map(struct cis *cp, unsigned char *p, int len)
352 int rlen = (*p & 3) + 1;
355 cp->last_config = *p1++ & 0x3F;
356 cp->reg_addr = parse_num(rlen | 0x10, p1, &p1, 0);
361 * Parse variable length value.
364 parse_num(int sz, u_char *p, u_char **q, int ofs)
378 num = tpl16(p) + ofs;
382 num = tpl24(p) + ofs;
387 num = tpl32(p) + ofs;
397 * CIS config entry - Decode and build configuration entry.
400 cis_config(struct cis *cp, unsigned char *p, int len)
404 struct cis_config *conf, *last;
407 conf = xmalloc(sizeof(*conf));
408 if ((last = cp->conf) != 0) {
414 conf->id = *p & 0x3F; /* Config index */
416 if (rex5588 && conf->id >= 0x08 && conf->id <= 0x1d)
419 if (*p & 0x40) /* Default flag */
420 cp->def_config = conf;
422 p++; /* Interface byte skip */
423 feat = *p++; /* Features byte */
424 for (i = 0; i < CIS_FEAT_POWER(feat); i++) {
425 unsigned char parms = *p++;
428 for (j = 0; j < 8; j++)
429 if (parms & (1 << j))
432 if (feat & CIS_FEAT_TIMING) {
435 if (CIS_WAIT_SCALE(i) != 3)
437 if (CIS_READY_SCALE(i) != 7)
439 if (CIS_RESERVED_SCALE(i) != 7)
442 if (feat & CIS_FEAT_I_O) {
444 if (CIS_IO_RANGE & *p)
445 conf->io_blks = CIS_IO_BLKS(p[1]) + 1;
446 conf->io_addr = CIS_IO_ADDR(*p);
447 conf->io_bus = (*p >> 5) & 3; /* CIS_IO_8BIT | CIS_IO_16BIT */
448 if (*p++ & CIS_IO_RANGE) {
449 struct cis_ioblk *io;
450 struct cis_ioblk *last_io = NULL;
453 j = CIS_IO_BLKSZ(*p++);
454 for (x = 0; x < conf->io_blks; x++) {
455 io = xmalloc(sizeof(*io));
461 io->addr = parse_num(i, p, &p, 0);
462 io->size = parse_num(j, p, &p, 1);
466 if (feat & CIS_FEAT_IRQ) {
468 conf->irqlevel = *p & 0xF;
469 conf->irq_flags = *p & 0xF0;
470 if (*p++ & CIS_IRQ_MASK) {
471 conf->irq_mask = tpl16(p);
475 switch (CIS_FEAT_MEMORY(feat)) {
476 case CIS_FEAT_MEM_NONE:
478 case CIS_FEAT_MEM_LEN:
480 conf->mem = xmalloc(sizeof(*conf->mem));
481 conf->mem->length = tpl16(p) << 8;
483 case CIS_FEAT_MEM_ADDR:
485 conf->mem = xmalloc(sizeof(*conf->mem));
486 conf->mem->length = tpl16(p) << 8;
487 conf->mem->address = tpl16(p + 2) << 8;
489 case CIS_FEAT_MEM_WIN: {
490 struct cis_memblk *mem;
491 struct cis_memblk *last_mem = NULL;
495 conf->memwins = CIS_MEM_WINS(x);
496 for (i = 0; i < conf->memwins; i++) {
497 mem = xmalloc(sizeof(*mem));
499 last_mem->next = mem;
503 mem->length = parse_num(CIS_MEM_LENSZ(x) | 0x10, p, &p, 0) << 8;
504 mem->address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10, p, &p, 0) << 8;
505 if (x & CIS_MEM_HOST) {
506 mem->host_address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10,
513 if (feat & CIS_FEAT_MISC) {
514 conf->misc_valid = 1;
520 * Read the tuples from the card.
521 * The processing of tuples is as follows:
522 * - Read tuples at attribute memory, offset 0.
523 * - If a CIS_END is the first tuple, look for
524 * a tuple list at common memory offset 0; this list
525 * must start with a LINKTARGET.
526 * - If a long link tuple was encountered, execute the long
528 * - If a no-link tuple was seen, terminate processing.
529 * - If no no-link tuple exists, and no long link tuple
530 * exists while processing the primary tuple list,
531 * then look for a LINKTARGET tuple in common memory.
532 * - If a long link tuple is found in any list, then process
533 * it. Only one link is allowed per list.
535 static struct tuple_list *tlist;
537 static struct tuple_list *
540 struct tuple_list *tl = 0, *last_tl;
546 last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
548 /* Now start processing the links (if any). */
551 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
554 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
556 if (tp && tp->length == 4) {
557 offs = tpl32(tp->data);
559 printf("Checking long link at %qd (%s memory)\n",
560 offs, flag ? "Attribute" : "Common");
562 /* If a link was found, read the tuple list from it. */
563 if (ck_linktarget(fd, offs, flag)) {
564 tl = read_one_tuplelist(fd, flag, offs);
573 * If the primary list had no NOLINK tuple, and no LINKTARGET,
574 * then try to read a tuple list at common memory (offset 0).
576 if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 && tlist->next == 0 &&
577 ck_linktarget(fd, (off_t) 0, 0)) {
579 printf("Reading long link at %qd (%s memory)\n",
580 offs, flag ? "Attribute" : "Common");
582 tlist->next = read_one_tuplelist(fd, 0, (off_t) 0);
588 * Read one tuple list from the card.
590 static struct tuple_list *
591 read_one_tuplelist(int fd, int flags, off_t offs)
593 struct tuple *tp, *last_tp = 0;
594 struct tuple_list *tl;
595 struct tuple_info *tinfo;
597 unsigned char code, length;
603 /* Check to see if this memory has already been scanned. */
604 for (tl = tlist; tl; tl = tl->next)
605 if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
607 tl = xmalloc(sizeof(*tl));
609 tl->flags = flags & MDF_ATTR;
610 ioctl(fd, PIOCRWFLAG, &flags);
611 lseek(fd, offs, SEEK_SET);
613 if (read_attr(fd, &code, 1) != 1) {
614 warn("CIS code read");
618 if (code == CIS_NULL)
620 tp = xmalloc(sizeof(*tp));
625 if (read_attr(fd, &length, 1) != 1) {
626 warn("CIS len read");
630 if (fmvj182 && (code == 0x1b) && (length == 25))
635 printf("Tuple code = 0x%x, len = %d\n", code, length);
637 if (length == 0xFF) {
638 length = tp->length = 0;
643 tp->data = xmalloc(length);
644 if (read_attr(fd, tp->data, length) != length) {
651 * Check the tuple, and ignore it if it isn't in the table
652 * or the length is illegal.
654 tinfo = get_tuple_info(code);
655 if (code == CIS_INFO_V1) {
656 /* Hack for broken CIS of FMV-J182 Ethernet card */
657 fmvj182 = fmvj182_check(tp->data);
659 /* Hack for RATOC LAN card */
660 rex5588 = rex5588_check(tp->data);
661 #endif /* RATOCLAN */
663 /* Hack for Hitachi Speech Synthesis card */
664 hss = hss_check(tp->data);
667 if (tinfo == NULL || (tinfo->length != 255 && tinfo->length > length)) {
668 printf("code %s ignored\n", tuple_name(code));
671 if (tl->tuples == NULL)
676 } while (code != CIS_END && total < 1024);
681 * return true if the offset points to a LINKTARGET tuple.
684 ck_linktarget(int fd, off_t offs, int flag)
688 ioctl(fd, PIOCRWFLAG, &flag);
689 lseek(fd, offs, SEEK_SET);
690 if (read_attr(fd, blk, 5) != 5)
692 if (blk[0] == 0x13 &&
702 * find_tuple_in_list - find a tuple within a
705 static struct tuple *
706 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
710 for (tp = tl->tuples; tp; tp = tp->next)
711 if (tp->code == code)
717 read_attr(int fd, char *bp, int len)
719 char blk[1024], *p = blk;
722 if (len > sizeof(blk) / 2)
723 len = sizeof(blk) / 2;
724 l = i = read(fd, blk, len * 2);
726 printf("Read return %d bytes (expected %d)\n", i, len * 2);
738 * return table entry for code.
740 static struct tuple_info *
741 get_tuple_info(unsigned char code)
743 struct tuple_info *tp;
745 for (tp = tuple_info; tp->name; tp++)
746 if (tp->code == code)
748 printf("Code %d not found\n", code);
753 tuple_name(unsigned char code)
755 struct tuple_info *tp;
757 tp = get_tuple_info(code);