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[] =
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;
52 int isdumpcisfile = 0;
54 static int read_attr(int, char *, int);
55 static int ck_linktarget(int, off_t, int);
56 static void cis_info(struct cis *, unsigned char *, int);
57 static void device_desc(unsigned char *, int, struct dev_mem *);
58 static void config_map(struct cis *, unsigned char *, int);
59 static void cis_config(struct cis *, unsigned char *, int);
60 static void cis_manuf_id(struct cis *, unsigned char *, int);
61 static void cis_func_id(struct cis *, unsigned char *, int);
62 static void cis_network_ext(struct cis *, unsigned char *, int);
63 static struct tuple_list *read_one_tuplelist(int, int, off_t);
64 static struct tuple_list *read_tuples(int);
65 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
66 static struct tuple_info *get_tuple_info(unsigned char);
68 static struct tuple_info tuple_info[] = {
69 {"Null tuple", 0x00, 0},
70 {"Common memory descriptor", 0x01, 255},
71 {"Long link to next chain for CardBus", 0x02, 255},
72 {"Indirect access", 0x03, 255},
73 {"Configuration map for CardBus", 0x04, 255},
74 {"Configuration entry for CardBus", 0x05, 255},
75 {"Long link to next chain for MFC", 0x06, 255},
76 {"Base address register for CardBus", 0x07, 6},
77 {"Checksum", 0x10, 5},
78 {"Long link to attribute memory", 0x11, 4},
79 {"Long link to common memory", 0x12, 4},
80 {"Link target", 0x13, 3},
82 {"Version 1 info", 0x15, 255},
83 {"Alternate language string", 0x16, 255},
84 {"Attribute memory descriptor", 0x17, 255},
85 {"JEDEC descr for common memory", 0x18, 255},
86 {"JEDEC descr for attribute memory", 0x19, 255},
87 {"Configuration map", 0x1A, 255},
88 {"Configuration entry", 0x1B, 255},
89 {"Other conditions for common memory", 0x1C, 255},
90 {"Other conditions for attribute memory", 0x1D, 255},
91 {"Geometry info for common memory", 0x1E, 255},
92 {"Geometry info for attribute memory", 0x1F, 255},
93 {"Manufacturer ID", 0x20, 4},
94 {"Functional ID", 0x21, 2},
95 {"Functional EXT", 0x22, 255},
96 {"Software interleave", 0x23, 2},
97 {"Version 2 Info", 0x40, 255},
98 {"Data format", 0x41, 255},
99 {"Geometry", 0x42, 4},
100 {"Byte order", 0x43, 2},
101 {"Card init date", 0x44, 4},
102 {"Battery replacement", 0x45, 4},
103 {"Organization", 0x46, 255},
104 {"Terminator", 0xFF, 0},
109 * After reading the tuples, decode the relevant ones.
114 struct tuple_list *tl;
118 cp = xmalloc(sizeof(*cp));
119 cp->tlist = read_tuples(fd);
123 for (tl = cp->tlist; tl; tl = tl->next)
124 for (tp = tl->tuples; tp; tp = tp->next) {
126 printf("tuple code = 0x%02x, data is\n", tp->code);
127 dump(tp->data, tp->length);
130 case CIS_MEM_COMMON: /* 0x01 */
131 device_desc(tp->data, tp->length, &cp->common_mem);
133 case CIS_INFO_V1: /* 0x15 */
134 cis_info(cp, tp->data, tp->length);
136 case CIS_MEM_ATTR: /* 0x17 */
137 device_desc(tp->data, tp->length, &cp->attr_mem);
139 case CIS_CONF_MAP: /* 0x1A */
140 config_map(cp, tp->data, tp->length);
142 case CIS_CONFIG: /* 0x1B */
143 cis_config(cp, tp->data, tp->length);
145 case CIS_MANUF_ID: /* 0x20 */
146 cis_manuf_id(cp, tp->data, tp->length);
148 case CIS_FUNC_ID: /* 0x21 */
149 cis_func_id(cp, tp->data, tp->length);
151 case CIS_FUNC_EXT: /* 0x22 */
152 if (cp->func_id1 == 6) /* LAN adaptor */
153 cis_network_ext(cp, tp->data, tp->length);
161 * free_cis - delete cis entry.
164 freecis(struct cis *cp)
166 struct cis_ioblk *io;
167 struct cis_memblk *mem;
168 struct cis_config *conf;
170 struct tuple_list *tl;
172 while ((tl = cp->tlist) != 0) {
173 cp->tlist = tl->next;
174 while ((tp = tl->tuples) != 0) {
175 tl->tuples = tp->next;
181 while ((conf = cp->conf) != 0) {
182 cp->conf = conf->next;
183 while ((io = conf->io) != 0) {
187 while ((mem = conf->mem) != 0) {
188 conf->mem = mem->next;
197 * Fills in CIS version data.
200 cis_info(struct cis *cp, unsigned char *p, int len)
209 if (len > 1 && *p != 0xff) {
210 cp->manuf = strdup(p);
211 len -= strlen(p) + 1;
218 if (len > 1 && *p != 0xff) {
219 cp->vers = strdup(p);
220 len -= strlen(p) + 1;
223 cp->vers = strdup("[none]");
227 cp->add_info1 = NULL;
229 if (len > 1 && *p != 0xff) {
230 cp->add_info1 = strdup(p);
231 len -= strlen(p) + 1;
234 cp->add_info1 = strdup("[none]");
238 cp->add_info2 = NULL;
240 if (len > 1 && *p != 0xff)
241 cp->add_info2 = strdup(p);
243 cp->add_info2 = strdup("[none]");
247 cis_manuf_id(struct cis *cp, unsigned char *p, int len)
250 cp->manufacturer = tpl16(p);
251 cp->product = tpl16(p+2);
253 cp->prodext = *(p+4); /* For xe driver */
261 * Fills in CIS function ID.
264 cis_func_id(struct cis *cp, unsigned char *p, int len)
271 cis_network_ext(struct cis *cp, unsigned char *p, int len)
276 case 4: /* Node ID */
277 if (len <= 2 || len < p[1] + 2)
282 cp->lan_nid = xmalloc(p[1]);
284 for (i = 0; i <= p[1]; i++)
285 cp->lan_nid[i] = p[i + 1];
291 * "FUJITSU LAN Card (FMV-J182)" has broken CIS
294 fmvj182_check(unsigned char *p)
296 char manuf[BUFSIZ], vers[BUFSIZ];
298 p++; /* major version */
299 p++; /* minor version */
300 strncpy(manuf, p, sizeof(manuf) - 1);
302 strncpy(vers, p, sizeof(vers) - 1);
303 if (!strcmp(manuf, "FUJITSU") && !strcmp(vers, "LAN Card(FMV-J182)"))
311 * "RATOC LAN Card (REX-5588)" has broken CIS
314 rex5588_check(unsigned char *p)
316 char manuf[BUFSIZ], vers[BUFSIZ];
318 p++; /* major version */
319 p++; /* minor version */
320 strncpy(manuf, p, sizeof(manuf) - 1);
322 strncpy(vers, p, sizeof(manuf) - 1);
323 if (!strcmp(manuf, "PCMCIA LAN MBH10304 ES"))
332 * Broken CIS for "HITACHI MICROCOMPUTER SYSTEM LTD." "MSSHVPC02"
335 hss_check(unsigned char *p)
337 char manuf[BUFSIZ], vers[BUFSIZ];
339 p++; /* major version */
340 p++; /* minor version */
341 strncpy(manuf, p, sizeof(manuf) - 1);
343 strncpy(vers, p, sizeof(vers) - 1);
344 if (!strcmp(manuf, "HITACHI MICROCOMPUTER SYSTEMS LTD.")
345 && !strcmp(vers, "MSSHVPC02"))
353 * device_desc - decode device descriptor.
356 device_desc(unsigned char *p, int len, struct dev_mem *dp)
358 while (len > 0 && *p != 0xFF) {
360 dp->type = (*p & 0xF0) >> 4;
361 dp->wps = !!(*p & 0x8);
365 dp->addr = (*p >> 3) & 0xF;
374 * configuration map of card control register.
377 config_map(struct cis *cp, unsigned char *p, int len)
380 int rlen = (*p & 3) + 1;
383 cp->last_config = *p1++ & 0x3F;
384 cp->reg_addr = parse_num(rlen | 0x10, p1, &p1, 0);
389 * Parse variable length value.
392 parse_num(int sz, u_char *p, u_char **q, int ofs)
406 num = tpl16(p) + ofs;
410 num = tpl24(p) + ofs;
415 num = tpl32(p) + ofs;
425 * CIS config entry - Decode and build configuration entry.
428 cis_config(struct cis *cp, unsigned char *p, int len)
432 struct cis_config *conf, *last;
435 conf = xmalloc(sizeof(*conf));
436 if ((last = cp->conf) != 0) {
442 conf->id = *p & 0x3F; /* Config index */
444 if (rex5588 && conf->id >= 0x08 && conf->id <= 0x1d)
447 if (*p & 0x40) /* Default flag */
448 cp->def_config = conf;
450 p++; /* Interface byte skip */
451 feat = *p++; /* Features byte */
452 for (i = 0; i < CIS_FEAT_POWER(feat); i++) {
453 unsigned char parms = *p++;
456 for (j = 0; j < 8; j++)
457 if (parms & (1 << j))
460 if (feat & CIS_FEAT_TIMING) {
463 if (CIS_WAIT_SCALE(i) != 3)
465 if (CIS_READY_SCALE(i) != 7)
467 if (CIS_RESERVED_SCALE(i) != 7)
470 if (feat & CIS_FEAT_I_O) {
472 if (CIS_IO_RANGE & *p)
473 conf->io_blks = CIS_IO_BLKS(p[1]) + 1;
474 conf->io_addr = CIS_IO_ADDR(*p);
475 conf->io_bus = (*p >> 5) & 3; /* CIS_IO_8BIT | CIS_IO_16BIT */
476 if (*p++ & CIS_IO_RANGE) {
477 struct cis_ioblk *io;
478 struct cis_ioblk *last_io = NULL;
481 j = CIS_IO_BLKSZ(*p++);
482 for (x = 0; x < conf->io_blks; x++) {
483 io = xmalloc(sizeof(*io));
489 io->addr = parse_num(i, p, &p, 0);
490 io->size = parse_num(j, p, &p, 1);
494 if (feat & CIS_FEAT_IRQ) {
496 conf->irqlevel = *p & 0xF;
497 conf->irq_flags = *p & 0xF0;
498 if (*p++ & CIS_IRQ_MASK) {
499 conf->irq_mask = tpl16(p);
503 switch (CIS_FEAT_MEMORY(feat)) {
504 case CIS_FEAT_MEM_NONE:
506 case CIS_FEAT_MEM_LEN:
508 conf->mem = xmalloc(sizeof(*conf->mem));
509 conf->mem->length = tpl16(p) << 8;
511 case CIS_FEAT_MEM_ADDR:
513 conf->mem = xmalloc(sizeof(*conf->mem));
514 conf->mem->length = tpl16(p) << 8;
515 conf->mem->address = tpl16(p + 2) << 8;
517 case CIS_FEAT_MEM_WIN: {
518 struct cis_memblk *mem;
519 struct cis_memblk *last_mem = NULL;
523 conf->memwins = CIS_MEM_WINS(x);
524 for (i = 0; i < conf->memwins; i++) {
525 mem = xmalloc(sizeof(*mem));
527 last_mem->next = mem;
531 mem->length = parse_num(CIS_MEM_LENSZ(x) | 0x10, p, &p, 0) << 8;
532 mem->address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10, p, &p, 0) << 8;
533 if (x & CIS_MEM_HOST) {
534 mem->host_address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10,
541 if (feat & CIS_FEAT_MISC) {
542 conf->misc_valid = 1;
548 * Read the tuples from the card.
549 * The processing of tuples is as follows:
550 * - Read tuples at attribute memory, offset 0.
551 * - If a CIS_END is the first tuple, look for
552 * a tuple list at common memory offset 0; this list
553 * must start with a LINKTARGET.
554 * - If a long link tuple was encountered, execute the long
556 * - If a no-link tuple was seen, terminate processing.
557 * - If no no-link tuple exists, and no long link tuple
558 * exists while processing the primary tuple list,
559 * then look for a LINKTARGET tuple in common memory.
560 * - If a long link tuple is found in any list, then process
561 * it. Only one link is allowed per list.
563 static struct tuple_list *tlist;
565 static struct tuple_list *
568 struct tuple_list *tl = 0, *last_tl;
574 last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
576 /* Now start processing the links (if any). */
579 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
582 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
584 if (tp && tp->length == 4) {
585 offs = tpl32(tp->data);
587 printf("Checking long link at %qd (%s memory)\n",
588 offs, flag ? "Attribute" : "Common");
590 /* If a link was found, read the tuple list from it. */
591 if (ck_linktarget(fd, offs, flag)) {
592 tl = read_one_tuplelist(fd, flag, offs);
601 * If the primary list had no NOLINK tuple, and no LINKTARGET,
602 * then try to read a tuple list at common memory (offset 0).
604 if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 && tlist->next == 0 &&
605 ck_linktarget(fd, (off_t) 0, 0)) {
607 printf("Reading long link at %qd (%s memory)\n",
608 offs, flag ? "Attribute" : "Common");
610 tlist->next = read_one_tuplelist(fd, 0, (off_t) 0);
616 * Read one tuple list from the card.
618 static struct tuple_list *
619 read_one_tuplelist(int fd, int flags, off_t offs)
621 struct tuple *tp, *last_tp = 0;
622 struct tuple_list *tl;
623 struct tuple_info *tinfo;
625 unsigned char code, length;
631 /* Check to see if this memory has already been scanned. */
632 for (tl = tlist; tl; tl = tl->next)
633 if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
635 tl = xmalloc(sizeof(*tl));
637 tl->flags = flags & MDF_ATTR;
638 ioctl(fd, PIOCRWFLAG, &flags);
639 lseek(fd, offs, SEEK_SET);
641 if (read_attr(fd, &code, 1) != 1) {
642 warn("CIS code read");
646 if (code == CIS_NULL)
648 tp = xmalloc(sizeof(*tp));
653 if (read_attr(fd, &length, 1) != 1) {
654 warn("CIS len read");
658 if (fmvj182 && (code == 0x1b) && (length == 25))
663 printf("Tuple code = 0x%x, len = %d\n", code, length);
665 if (length == 0xFF) {
666 length = tp->length = 0;
671 tp->data = xmalloc(length);
672 if (read_attr(fd, tp->data, length) != length) {
679 * Check the tuple, and ignore it if it isn't in the table
680 * or the length is illegal.
682 tinfo = get_tuple_info(code);
683 if (code == CIS_INFO_V1) {
684 /* Hack for broken CIS of FMV-J182 Ethernet card */
685 fmvj182 = fmvj182_check(tp->data);
687 /* Hack for RATOC LAN card */
688 rex5588 = rex5588_check(tp->data);
689 #endif /* RATOCLAN */
691 /* Hack for Hitachi Speech Synthesis card */
692 hss = hss_check(tp->data);
695 if (tinfo != NULL && (tinfo->length != 255 && tinfo->length > length)) {
696 printf("code %s ignored\n", tuple_name(code));
699 if (tl->tuples == NULL)
704 } while (code != CIS_END && total < 1024);
709 * return true if the offset points to a LINKTARGET tuple.
712 ck_linktarget(int fd, off_t offs, int flag)
716 ioctl(fd, PIOCRWFLAG, &flag);
717 lseek(fd, offs, SEEK_SET);
718 if (read_attr(fd, blk, 5) != 5)
720 if (blk[0] == 0x13 &&
730 * find_tuple_in_list - find a tuple within a
733 static struct tuple *
734 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
738 for (tp = tl->tuples; tp; tp = tp->next)
739 if (tp->code == code)
745 read_attr(int fd, char *bp, int len)
747 char blk[1024], *p = blk;
751 return (read(fd, bp, len));
752 if (len > sizeof(blk) / 2)
753 len = sizeof(blk) / 2;
754 l = i = read(fd, blk, len * 2);
756 printf("Read return %d bytes (expected %d)\n", i, len * 2);
768 * return table entry for code.
770 static struct tuple_info *
771 get_tuple_info(unsigned char code)
773 struct tuple_info *tp;
775 for (tp = tuple_info; tp->name; tp++)
776 if (tp->code == code)
782 tuple_name(unsigned char code)
784 struct tuple_info *tp;
786 tp = get_tuple_info(code);