xref: /netbsd-src/sys/dev/pcmcia/pcmcia_cis.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: pcmcia_cis.c,v 1.40 2005/12/11 12:23:23 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Marc Horowitz.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pcmcia_cis.c,v 1.40 2005/12/11 12:23:23 christos Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 
40 #include <dev/pcmcia/pcmciareg.h>
41 #include <dev/pcmcia/pcmciachip.h>
42 #include <dev/pcmcia/pcmciavar.h>
43 
44 #ifdef PCMCIACISDEBUG
45 int	pcmciacis_debug = 0;
46 #define	DPRINTF(arg) if (pcmciacis_debug) printf arg
47 #else
48 #define	DPRINTF(arg)
49 #endif
50 
51 #define	PCMCIA_CIS_SIZE		1024
52 
53 struct cis_state {
54 	int	count;
55 	int	gotmfc;
56 	struct pcmcia_config_entry temp_cfe;
57 	struct pcmcia_config_entry *default_cfe;
58 	struct pcmcia_card *card;
59 	struct pcmcia_function *pf;
60 };
61 
62 int	pcmcia_parse_cis_tuple(struct pcmcia_tuple *, void *);
63 static int decode_funce(struct pcmcia_tuple *, struct pcmcia_function *);
64 static void create_pf(struct cis_state *);
65 
66 
67 static void
68 create_pf(struct cis_state *state)
69 {
70 	state->pf = malloc(sizeof(*state->pf), M_DEVBUF, M_NOWAIT|M_ZERO);
71 	state->pf->number = state->count++;
72 	state->pf->last_config_index = -1;
73 	SIMPLEQ_INIT(&state->pf->cfe_head);
74 	SIMPLEQ_INSERT_TAIL(&state->card->pf_head, state->pf, pf_list);
75 }
76 
77 void
78 pcmcia_free_pf(struct pcmcia_function_head *pfhead)
79 {
80 	struct pcmcia_function *pf, *npf;
81 	struct pcmcia_config_entry *cfe, *ncfe;
82 
83 	for (pf = SIMPLEQ_FIRST(pfhead); pf != NULL; pf = npf) {
84 		npf = SIMPLEQ_NEXT(pf, pf_list);
85 		for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
86 		    cfe = ncfe) {
87 			ncfe = SIMPLEQ_NEXT(cfe, cfe_list);
88 			free(cfe, M_DEVBUF);
89 		}
90 		free(pf, M_DEVBUF);
91 	}
92 
93 	SIMPLEQ_INIT(pfhead);
94 }
95 
96 void
97 pcmcia_read_cis(sc)
98 	struct pcmcia_softc *sc;
99 {
100 	struct cis_state state;
101 
102 	memset(&state, 0, sizeof state);
103 
104 	state.card = &sc->card;
105 
106 	state.card->error = 0;
107 	state.card->cis1_major = -1;
108 	state.card->cis1_minor = -1;
109 	state.card->cis1_info[0] = NULL;
110 	state.card->cis1_info[1] = NULL;
111 	state.card->cis1_info[2] = NULL;
112 	state.card->cis1_info[3] = NULL;
113 	state.card->manufacturer = PCMCIA_VENDOR_INVALID;
114 	state.card->product = PCMCIA_PRODUCT_INVALID;
115 	SIMPLEQ_INIT(&state.card->pf_head);
116 
117 	state.pf = NULL;
118 
119 	if (pcmcia_scan_cis((struct device *)sc, pcmcia_parse_cis_tuple,
120 	    &state) == -1)
121 		state.card->error++;
122 }
123 
124 int
125 pcmcia_scan_cis(dev, fct, arg)
126 	struct device *dev;
127 	int (*fct)(struct pcmcia_tuple *, void *);
128 	void *arg;
129 {
130 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
131 	pcmcia_chipset_tag_t pct;
132 	pcmcia_chipset_handle_t pch;
133 	int window;
134 	struct pcmcia_mem_handle pcmh;
135 	struct pcmcia_tuple tuple;
136 	int longlink_present;
137 	int longlink_common;
138 	u_long longlink_addr;
139 	int mfc_count;
140 	int mfc_index;
141 	struct {
142 		int	common;
143 		u_long	addr;
144 	} mfc[256 / 5];
145 	int ret;
146 
147 	ret = 0;
148 
149 	pct = sc->pct;
150 	pch = sc->pch;
151 
152 	/* allocate some memory */
153 
154 	if (pcmcia_chip_mem_alloc(pct, pch, PCMCIA_CIS_SIZE, &pcmh)) {
155 #ifdef DIAGNOSTIC
156 		printf("%s: can't alloc memory to read attributes\n",
157 		    sc->dev.dv_xname);
158 #endif
159 		return -1;
160 	}
161 	/* initialize state for the primary tuple chain */
162 	if (pcmcia_chip_mem_map(pct, pch, PCMCIA_MEM_ATTR, 0,
163 	    PCMCIA_CIS_SIZE, &pcmh, &tuple.ptr, &window)) {
164 		pcmcia_chip_mem_free(pct, pch, &pcmh);
165 #ifdef DIAGNOSTIC
166 		printf("%s: can't map memory to read attributes\n",
167 		    sc->dev.dv_xname);
168 #endif
169 		return -1;
170 	}
171 	tuple.memt = pcmh.memt;
172 	tuple.memh = pcmh.memh;
173 
174 	DPRINTF(("cis mem map %x\n", (unsigned int) tuple.memh));
175 
176 	tuple.mult = 2;
177 
178 	longlink_present = 1;
179 	longlink_common = 1;
180 	longlink_addr = 0;
181 
182 	mfc_count = 0;
183 	mfc_index = 0;
184 
185 	DPRINTF(("%s: CIS tuple chain:\n", sc->dev.dv_xname));
186 
187 	while (1) {
188 		DELAY(1000);
189 
190 		while (1) {
191 			/*
192 			 * Perform boundary check for insane cards.
193 			 * If CIS is too long, simulate CIS end.
194 			 * (This check may not be sufficient for
195 			 * malicious cards.)
196 			 */
197 			if (tuple.mult * tuple.ptr >= PCMCIA_CIS_SIZE - 1
198 			    - 32 /* ad hoc value */ ) {
199 				DPRINTF(("CISTPL_END (too long CIS)\n"));
200 				tuple.code = PCMCIA_CISTPL_END;
201 				goto cis_end;
202 			}
203 
204 			/* get the tuple code */
205 
206 			tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr);
207 
208 			/* two special-case tuples */
209 
210 			if (tuple.code == PCMCIA_CISTPL_NULL) {
211 				DPRINTF((" 00\nCISTPL_NONE\n"));
212 				tuple.ptr++;
213 				continue;
214 			} else if (tuple.code == PCMCIA_CISTPL_END) {
215 				DPRINTF((" ff\nCISTPL_END\n"));
216 			cis_end:
217 				/* Call the function for the END tuple, since
218 				   the CIS semantics depend on it */
219 				if ((*fct) (&tuple, arg)) {
220 					pcmcia_chip_mem_unmap(pct, pch,
221 							      window);
222 					ret = 1;
223 					goto done;
224 				}
225 				tuple.ptr++;
226 				break;
227 			}
228 
229 			/* now all the normal tuples */
230 
231 			tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1);
232 #ifdef PCMCIACISDEBUG
233 			/* print the tuple */
234 			{
235 				int i;
236 
237 				DPRINTF((" %02x %02x", tuple.code,
238 				    tuple.length));
239 
240 				for (i = 0; i < tuple.length; i++) {
241 					DPRINTF((" %02x",
242 					    pcmcia_tuple_read_1(&tuple, i)));
243 					if ((i % 16) == 13)
244 						DPRINTF(("\n"));
245 				}
246 				if ((i % 16) != 14)
247 					DPRINTF(("\n"));
248 			}
249 #endif
250 			switch (tuple.code) {
251 			case PCMCIA_CISTPL_LONGLINK_A:
252 			case PCMCIA_CISTPL_LONGLINK_C:
253 				if (tuple.length < 4) {
254 					DPRINTF(("CISTPL_LONGLINK_%s too "
255 					    "short %d\n",
256 					    longlink_common ? "C" : "A",
257 					    tuple.length));
258 					break;
259 				}
260 				longlink_present = 1;
261 				longlink_common = (tuple.code ==
262 				    PCMCIA_CISTPL_LONGLINK_C) ? 1 : 0;
263 				longlink_addr = pcmcia_tuple_read_4(&tuple, 0);
264 				DPRINTF(("CISTPL_LONGLINK_%s %lx\n",
265 				    longlink_common ? "C" : "A",
266 				    longlink_addr));
267 				break;
268 			case PCMCIA_CISTPL_NO_LINK:
269 				longlink_present = 0;
270 				DPRINTF(("CISTPL_NO_LINK\n"));
271 				break;
272 			case PCMCIA_CISTPL_CHECKSUM:
273 				if (tuple.length < 5) {
274 					DPRINTF(("CISTPL_CHECKSUM too "
275 					    "short %d\n", tuple.length));
276 					break;
277 				} {
278 					int16_t offset;
279 					u_long addr, length;
280 					u_int cksum, sum;
281 					int i;
282 
283 					*((u_int16_t *) & offset) =
284 					    pcmcia_tuple_read_2(&tuple, 0);
285 					length = pcmcia_tuple_read_2(&tuple, 2);
286 					cksum = pcmcia_tuple_read_1(&tuple, 4);
287 
288 					addr = tuple.ptr + offset;
289 
290 					DPRINTF(("CISTPL_CHECKSUM addr=%lx "
291 					    "len=%lx cksum=%x",
292 					    addr, length, cksum));
293 
294 					/*
295 					 * XXX do more work to deal with
296 					 * distant regions
297 					 */
298 					if ((addr >= PCMCIA_CIS_SIZE) ||
299 					    ((addr + length) < 0) ||
300 					    ((addr + length) >=
301 					      PCMCIA_CIS_SIZE)) {
302 						DPRINTF((" skipped, "
303 						    "too distant\n"));
304 						break;
305 					}
306 					sum = 0;
307 					for (i = 0; i < length; i++)
308 						sum +=
309 						    bus_space_read_1(tuple.memt,
310 						    tuple.memh,
311 						    addr + tuple.mult * i);
312 					if (cksum != (sum & 0xff)) {
313 						DPRINTF((" failed sum=%x\n",
314 						    sum));
315 						printf("%s: CIS checksum "
316 						    "failed\n",
317 						    sc->dev.dv_xname);
318 #if 0
319 						/*
320 						 * XXX Some working cards have
321 						 * XXX bad checksums!!
322 						 */
323 						ret = -1;
324 #endif
325 					} else {
326 						DPRINTF((" ok\n"));
327 					}
328 				}
329 				break;
330 			case PCMCIA_CISTPL_LONGLINK_MFC:
331 				if (tuple.length < 1) {
332 					DPRINTF(("CISTPL_LONGLINK_MFC too "
333 					    "short %d\n", tuple.length));
334 					break;
335 				}
336 				if (((tuple.length - 1) % 5) != 0) {
337 					DPRINTF(("CISTPL_LONGLINK_MFC bogus "
338 					    "length %d\n", tuple.length));
339 					break;
340 				}
341 				/*
342 				 * this is kind of ad hoc, as I don't have
343 				 * any real documentation
344 				 */
345 				{
346 					int i, tmp_count;
347 
348 					/*
349 					 * put count into tmp var so that
350 					 * if we have to bail (because it's
351 					 * a bogus count) it won't be
352 					 * remembered for later use.
353 					 */
354 					tmp_count =
355 					    pcmcia_tuple_read_1(&tuple, 0);
356 					DPRINTF(("CISTPL_LONGLINK_MFC %d",
357 					    tmp_count));
358 
359 					/*
360 					 * make _sure_ it's the right size;
361 					 * if too short, it may be a weird
362 					 * (unknown/undefined) format
363 					 */
364 					if (tuple.length != (tmp_count*5 + 1)) {
365 						DPRINTF((" bogus length %d\n",
366 						    tuple.length));
367 						break;
368 					}
369 
370 #ifdef PCMCIACISDEBUG	/* maybe enable all the time? */
371 					/*
372 					 * sanity check for a programming
373 					 * error which is difficult to find
374 					 * when debugging.
375 					 */
376 					if (tmp_count >
377 					    howmany(sizeof mfc, sizeof mfc[0]))
378 						panic("CISTPL_LONGLINK_MFC mfc "
379 						    "count would blow stack");
380 #endif
381 
382 					mfc_count = tmp_count;
383 					for (i = 0; i < mfc_count; i++) {
384 						mfc[i].common =
385 						    (pcmcia_tuple_read_1(&tuple,
386 						    1 + 5 * i) ==
387 						    PCMCIA_MFC_MEM_COMMON) ?
388 						    1 : 0;
389 						mfc[i].addr =
390 						    pcmcia_tuple_read_4(&tuple,
391 						    1 + 5 * i + 1);
392 						DPRINTF((" %s:%lx",
393 						    mfc[i].common ? "common" :
394 						    "attr", mfc[i].addr));
395 					}
396 					DPRINTF(("\n"));
397 				}
398 				/*
399 				 * for LONGLINK_MFC, fall through to the
400 				 * function.  This tuple has structural and
401 				 * semantic content.
402 				 */
403 			default:
404 				{
405 					if ((*fct) (&tuple, arg)) {
406 						pcmcia_chip_mem_unmap(pct,
407 						    pch, window);
408 						ret = 1;
409 						goto done;
410 					}
411 				}
412 				break;
413 			}	/* switch */
414 			/* skip to the next tuple */
415 			tuple.ptr += 2 + tuple.length;
416 		}
417 
418 		/*
419 		 * the chain is done.  Clean up and move onto the next one,
420 		 * if any.  The loop is here in the case that there is an MFC
421 		 * card with no longlink (which defaults to existing, == 0).
422 		 * In general, this means that if one pointer fails, it will
423 		 * try the next one, instead of just bailing.
424 		 */
425 
426 		while (1) {
427 			pcmcia_chip_mem_unmap(pct, pch, window);
428 
429 			if (longlink_present) {
430 				/*
431 				 * if the longlink is to attribute memory,
432 				 * then it is unindexed.  That is, if the
433 				 * link value is 0x100, then the actual
434 				 * memory address is 0x200.  This means that
435 				 * we need to multiply by 2 before calling
436 				 * mem_map, and then divide the resulting ptr
437 				 * by 2 after.
438 				 */
439 
440 				if (!longlink_common)
441 					longlink_addr *= 2;
442 
443 				pcmcia_chip_mem_map(pct, pch, longlink_common ?
444 				    (PCMCIA_WIDTH_MEM8 | PCMCIA_MEM_COMMON) :
445 				    PCMCIA_MEM_ATTR,
446 				    longlink_addr, PCMCIA_CIS_SIZE,
447 				    &pcmh, &tuple.ptr, &window);
448 
449 				if (!longlink_common)
450 					tuple.ptr /= 2;
451 
452 				DPRINTF(("cis mem map %x\n",
453 				    (unsigned int) tuple.memh));
454 
455 				tuple.mult = longlink_common ? 1 : 2;
456 				longlink_present = 0;
457 				longlink_common = 1;
458 				longlink_addr = 0;
459 			} else if (mfc_count && (mfc_index < mfc_count)) {
460 				if (!mfc[mfc_index].common)
461 					mfc[mfc_index].addr *= 2;
462 
463 				pcmcia_chip_mem_map(pct, pch,
464 				    mfc[mfc_index].common ?
465 				    (PCMCIA_WIDTH_MEM8 | PCMCIA_MEM_COMMON) :
466 				    PCMCIA_MEM_ATTR,
467 				    mfc[mfc_index].addr, PCMCIA_CIS_SIZE,
468 				    &pcmh, &tuple.ptr, &window);
469 
470 				if (!mfc[mfc_index].common)
471 					tuple.ptr /= 2;
472 
473 				DPRINTF(("cis mem map %x\n",
474 				    (unsigned int) tuple.memh));
475 
476 				/* set parse state, and point at the next one */
477 
478 				tuple.mult = mfc[mfc_index].common ? 1 : 2;
479 
480 				mfc_index++;
481 			} else {
482 				goto done;
483 			}
484 
485 			/* make sure that the link is valid */
486 			tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr);
487 			if (tuple.code != PCMCIA_CISTPL_LINKTARGET) {
488 				DPRINTF(("CISTPL_LINKTARGET expected, "
489 				    "code %02x observed\n", tuple.code));
490 				continue;
491 			}
492 			tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1);
493 			if (tuple.length < 3) {
494 				DPRINTF(("CISTPL_LINKTARGET too short %d\n",
495 				    tuple.length));
496 				continue;
497 			}
498 			if ((pcmcia_tuple_read_1(&tuple, 0) != 'C') ||
499 			    (pcmcia_tuple_read_1(&tuple, 1) != 'I') ||
500 			    (pcmcia_tuple_read_1(&tuple, 2) != 'S')) {
501 				DPRINTF(("CISTPL_LINKTARGET magic "
502 				    "%02x%02x%02x incorrect\n",
503 				    pcmcia_tuple_read_1(&tuple, 0),
504 				    pcmcia_tuple_read_1(&tuple, 1),
505 				    pcmcia_tuple_read_1(&tuple, 2)));
506 				continue;
507 			}
508 			tuple.ptr += 2 + tuple.length;
509 
510 			break;
511 		}
512 	}
513 
514 	pcmcia_chip_mem_unmap(pct, pch, window);
515 
516 done:
517 	/* Last, free the allocated memory block */
518 	pcmcia_chip_mem_free(pct, pch, &pcmh);
519 
520 	return (ret);
521 }
522 
523 /* XXX this is incredibly verbose.  Not sure what trt is */
524 
525 void
526 pcmcia_print_cis(sc)
527 	struct pcmcia_softc *sc;
528 {
529 	struct pcmcia_card *card = &sc->card;
530 	struct pcmcia_function *pf;
531 	struct pcmcia_config_entry *cfe;
532 	int i;
533 
534 	printf("%s: CIS version ", sc->dev.dv_xname);
535 	if (card->cis1_major == 4) {
536 		if (card->cis1_minor == 0)
537 			printf("PCMCIA 1.0\n");
538 		else if (card->cis1_minor == 1)
539 			printf("PCMCIA 2.0 or 2.1\n");
540 	} else if (card->cis1_major >= 5)
541 		printf("PC Card Standard %d.%d\n", card->cis1_major, card->cis1_minor);
542 	else
543 		printf("unknown (major=%d, minor=%d)\n",
544 		    card->cis1_major, card->cis1_minor);
545 
546 	printf("%s: CIS info: ", sc->dev.dv_xname);
547 	for (i = 0; i < 4; i++) {
548 		if (card->cis1_info[i] == NULL)
549 			break;
550 		if (i)
551 			printf(", ");
552 		printf("%s", card->cis1_info[i]);
553 	}
554 	printf("\n");
555 
556 	printf("%s: Manufacturer code 0x%x, product 0x%x\n",
557 	       sc->dev.dv_xname, card->manufacturer, card->product);
558 
559 	SIMPLEQ_FOREACH(pf, &card->pf_head, pf_list) {
560 		printf("%s: function %d: ", sc->dev.dv_xname, pf->number);
561 
562 		switch (pf->function) {
563 		case PCMCIA_FUNCTION_UNSPEC:
564 			printf("unspecified");
565 			break;
566 		case PCMCIA_FUNCTION_MULTIFUNCTION:
567 			printf("multi-function");
568 			break;
569 		case PCMCIA_FUNCTION_MEMORY:
570 			printf("memory");
571 			break;
572 		case PCMCIA_FUNCTION_SERIAL:
573 			printf("serial port");
574 			break;
575 		case PCMCIA_FUNCTION_PARALLEL:
576 			printf("parallel port");
577 			break;
578 		case PCMCIA_FUNCTION_DISK:
579 			printf("fixed disk");
580 			switch (pf->pf_funce_disk_interface) {
581 			case PCMCIA_TPLFE_DDI_PCCARD_ATA:
582 				printf("(ata)");
583 				break;
584 			default:
585 				break;
586 			}
587 			break;
588 		case PCMCIA_FUNCTION_VIDEO:
589 			printf("video adapter");
590 			break;
591 		case PCMCIA_FUNCTION_NETWORK:
592 			printf("network adapter");
593 			break;
594 		case PCMCIA_FUNCTION_AIMS:
595 			printf("auto incrementing mass storage");
596 			break;
597 		case PCMCIA_FUNCTION_SCSI:
598 			printf("SCSI bridge");
599 			break;
600 		case PCMCIA_FUNCTION_SECURITY:
601 			printf("Security services");
602 			break;
603 		case PCMCIA_FUNCTION_INSTRUMENT:
604 			printf("Instrument");
605 			break;
606 		default:
607 			printf("unknown (%d)", pf->function);
608 			break;
609 		}
610 
611 		printf(", ccr addr %lx mask %lx\n", pf->ccr_base, pf->ccr_mask);
612 
613 		SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
614 			printf("%s: function %d, config table entry %d: ",
615 			    sc->dev.dv_xname, pf->number, cfe->number);
616 
617 			switch (cfe->iftype) {
618 			case PCMCIA_IFTYPE_MEMORY:
619 				printf("memory card");
620 				break;
621 			case PCMCIA_IFTYPE_IO:
622 				printf("I/O card");
623 				break;
624 			default:
625 				printf("card type unknown");
626 				break;
627 			}
628 
629 			printf("; irq mask %x", cfe->irqmask);
630 
631 			if (cfe->num_iospace) {
632 				printf("; iomask %lx, iospace", cfe->iomask);
633 
634 				for (i = 0; i < cfe->num_iospace; i++) {
635 					printf(" %lx", cfe->iospace[i].start);
636 					if (cfe->iospace[i].length)
637 						printf("-%lx",
638 						    cfe->iospace[i].start +
639 						    cfe->iospace[i].length - 1);
640 				}
641 			}
642 			if (cfe->num_memspace) {
643 				printf("; memspace");
644 
645 				for (i = 0; i < cfe->num_memspace; i++) {
646 					printf(" %lx",
647 					    cfe->memspace[i].cardaddr);
648 					if (cfe->memspace[i].length)
649 						printf("-%lx",
650 						    cfe->memspace[i].cardaddr +
651 						    cfe->memspace[i].length - 1);
652 					if (cfe->memspace[i].hostaddr)
653 						printf("@%lx",
654 						    cfe->memspace[i].hostaddr);
655 				}
656 			}
657 			if (cfe->maxtwins)
658 				printf("; maxtwins %d", cfe->maxtwins);
659 
660 			printf(";");
661 
662 			if (cfe->flags & PCMCIA_CFE_MWAIT_REQUIRED)
663 				printf(" mwait_required");
664 			if (cfe->flags & PCMCIA_CFE_RDYBSY_ACTIVE)
665 				printf(" rdybsy_active");
666 			if (cfe->flags & PCMCIA_CFE_WP_ACTIVE)
667 				printf(" wp_active");
668 			if (cfe->flags & PCMCIA_CFE_BVD_ACTIVE)
669 				printf(" bvd_active");
670 			if (cfe->flags & PCMCIA_CFE_IO8)
671 				printf(" io8");
672 			if (cfe->flags & PCMCIA_CFE_IO16)
673 				printf(" io16");
674 			if (cfe->flags & PCMCIA_CFE_IRQSHARE)
675 				printf(" irqshare");
676 			if (cfe->flags & PCMCIA_CFE_IRQPULSE)
677 				printf(" irqpulse");
678 			if (cfe->flags & PCMCIA_CFE_IRQLEVEL)
679 				printf(" irqlevel");
680 			if (cfe->flags & PCMCIA_CFE_POWERDOWN)
681 				printf(" powerdown");
682 			if (cfe->flags & PCMCIA_CFE_READONLY)
683 				printf(" readonly");
684 			if (cfe->flags & PCMCIA_CFE_AUDIO)
685 				printf(" audio");
686 
687 			printf("\n");
688 		}
689 	}
690 
691 	if (card->error)
692 		printf("%s: %d errors found while parsing CIS\n",
693 		    sc->dev.dv_xname, card->error);
694 }
695 
696 int
697 pcmcia_parse_cis_tuple(tuple, arg)
698 	struct pcmcia_tuple *tuple;
699 	void *arg;
700 {
701 	/* most of these are educated guesses */
702 	static const struct pcmcia_config_entry init_cfe = {
703 		-1, PCMCIA_CFE_RDYBSY_ACTIVE | PCMCIA_CFE_WP_ACTIVE |
704 		PCMCIA_CFE_BVD_ACTIVE, PCMCIA_IFTYPE_MEMORY,
705 	};
706 
707 	struct cis_state *state = arg;
708 
709 	switch (tuple->code) {
710 	case PCMCIA_CISTPL_END:
711 		/* if we've seen a LONGLINK_MFC, and this is the first
712 		 * END after it, reset the function list.
713 		 *
714 		 * XXX This might also be the right place to start a
715 		 * new function, but that assumes that a function
716 		 * definition never crosses any longlink, and I'm not
717 		 * sure about that.  This is probably safe for MFC
718 		 * cards, but what we have now isn't broken, so I'd
719 		 * rather not change it.
720 		 */
721 		if (state->gotmfc == 1) {
722 			state->gotmfc = 2;
723 			state->count = 0;
724 			state->pf = NULL;
725 
726 			pcmcia_free_pf(&state->card->pf_head);
727 		}
728 		break;
729 	case PCMCIA_CISTPL_LONGLINK_MFC:
730 		/*
731 		 * this tuple's structure was dealt with in scan_cis.  here,
732 		 * record the fact that the MFC tuple was seen, so that
733 		 * functions declared before the MFC link can be cleaned
734 		 * up.
735 		 */
736 		if (state->gotmfc == 0) {
737 			state->gotmfc = 1;
738 		} else {
739 			DPRINTF(("got LONGLINK_MFC again!"));
740 		}
741 		break;
742 #ifdef PCMCIACISDEBUG
743 	case PCMCIA_CISTPL_DEVICE:
744 	case PCMCIA_CISTPL_DEVICE_A:
745 		{
746 			u_int reg, dtype, dspeed;
747 
748 			reg = pcmcia_tuple_read_1(tuple, 0);
749 			dtype = reg & PCMCIA_DTYPE_MASK;
750 			dspeed = reg & PCMCIA_DSPEED_MASK;
751 
752 			DPRINTF(("CISTPL_DEVICE%s type=",
753 			(tuple->code == PCMCIA_CISTPL_DEVICE) ? "" : "_A"));
754 			switch (dtype) {
755 			case PCMCIA_DTYPE_NULL:
756 				DPRINTF(("null"));
757 				break;
758 			case PCMCIA_DTYPE_ROM:
759 				DPRINTF(("rom"));
760 				break;
761 			case PCMCIA_DTYPE_OTPROM:
762 				DPRINTF(("otprom"));
763 				break;
764 			case PCMCIA_DTYPE_EPROM:
765 				DPRINTF(("eprom"));
766 				break;
767 			case PCMCIA_DTYPE_EEPROM:
768 				DPRINTF(("eeprom"));
769 				break;
770 			case PCMCIA_DTYPE_FLASH:
771 				DPRINTF(("flash"));
772 				break;
773 			case PCMCIA_DTYPE_SRAM:
774 				DPRINTF(("sram"));
775 				break;
776 			case PCMCIA_DTYPE_DRAM:
777 				DPRINTF(("dram"));
778 				break;
779 			case PCMCIA_DTYPE_FUNCSPEC:
780 				DPRINTF(("funcspec"));
781 				break;
782 			case PCMCIA_DTYPE_EXTEND:
783 				DPRINTF(("extend"));
784 				break;
785 			default:
786 				DPRINTF(("reserved"));
787 				break;
788 			}
789 			DPRINTF((" speed="));
790 			switch (dspeed) {
791 			case PCMCIA_DSPEED_NULL:
792 				DPRINTF(("null"));
793 				break;
794 			case PCMCIA_DSPEED_250NS:
795 				DPRINTF(("250ns"));
796 				break;
797 			case PCMCIA_DSPEED_200NS:
798 				DPRINTF(("200ns"));
799 				break;
800 			case PCMCIA_DSPEED_150NS:
801 				DPRINTF(("150ns"));
802 				break;
803 			case PCMCIA_DSPEED_100NS:
804 				DPRINTF(("100ns"));
805 				break;
806 			case PCMCIA_DSPEED_EXT:
807 				DPRINTF(("ext"));
808 				break;
809 			default:
810 				DPRINTF(("reserved"));
811 				break;
812 			}
813 		}
814 		DPRINTF(("\n"));
815 		break;
816 #endif
817 	case PCMCIA_CISTPL_VERS_1:
818 		if (tuple->length < 6) {
819 			DPRINTF(("CISTPL_VERS_1 too short %d\n",
820 			    tuple->length));
821 			break;
822 		} {
823 			int start, i, ch, count;
824 
825 			state->card->cis1_major = pcmcia_tuple_read_1(tuple, 0);
826 			state->card->cis1_minor = pcmcia_tuple_read_1(tuple, 1);
827 
828 			for (count = 0, start = 0, i = 0;
829 			    (count < 4) && ((i + 4) < 256); i++) {
830 				ch = pcmcia_tuple_read_1(tuple, 2 + i);
831 				if (ch == 0xff) {
832 					if (i > start) {
833 						state->card->cis1_info_buf[i] = 0;
834 						state->card->cis1_info[count] =
835 						    state->card->cis1_info_buf + start;
836 					}
837 					break;
838 				}
839 				state->card->cis1_info_buf[i] = ch;
840 				if (ch == 0) {
841 					state->card->cis1_info[count] =
842 					    state->card->cis1_info_buf + start;
843 					start = i + 1;
844 					count++;
845 				}
846 			}
847 			DPRINTF(("CISTPL_VERS_1\n"));
848 		}
849 		break;
850 	case PCMCIA_CISTPL_MANFID:
851 		if (tuple->length < 4) {
852 			DPRINTF(("CISTPL_MANFID too short %d\n",
853 			    tuple->length));
854 			break;
855 		}
856 		state->card->manufacturer = pcmcia_tuple_read_2(tuple, 0);
857 		state->card->product = pcmcia_tuple_read_2(tuple, 2);
858 		DPRINTF(("CISTPL_MANFID\n"));
859 		break;
860 	case PCMCIA_CISTPL_FUNCID:
861 		if (tuple->length < 1) {
862 			DPRINTF(("CISTPL_FUNCID too short %d\n",
863 			    tuple->length));
864 			break;
865 		}
866 		if (state->pf) {
867 			if (state->pf->function == PCMCIA_FUNCTION_UNSPEC) {
868 				/*
869 				 * This looks like a opportunistic function
870 				 * created by a CONFIG tuple.  Just keep it.
871 				 */
872 			} else {
873 				/*
874 				 * A function is being defined, end it.
875 				 */
876 				state->pf = NULL;
877 			}
878 		}
879 		if (state->pf == NULL)
880 			create_pf(state);
881 		state->pf->function = pcmcia_tuple_read_1(tuple, 0);
882 
883 		DPRINTF(("CISTPL_FUNCID\n"));
884 		break;
885 	case PCMCIA_CISTPL_FUNCE:
886 		if (state->pf == NULL || state->pf->function <= 0) {
887 			DPRINTF(("CISTPL_FUNCE is not followed by "
888 			    "valid CISTPL_FUNCID\n"));
889 			break;
890 		}
891 		if (tuple->length >= 2) {
892 			decode_funce(tuple, state->pf);
893 		}
894 		break;
895 	case PCMCIA_CISTPL_CONFIG:
896 		if (tuple->length < 3) {
897 			DPRINTF(("CISTPL_CONFIG too short %d\n",
898 			    tuple->length));
899 			break;
900 		} {
901 			u_int reg, rasz, rmsz, rfsz;
902 			int i;
903 
904 			reg = pcmcia_tuple_read_1(tuple, 0);
905 			rasz = 1 + ((reg & PCMCIA_TPCC_RASZ_MASK) >>
906 			    PCMCIA_TPCC_RASZ_SHIFT);
907 			rmsz = 1 + ((reg & PCMCIA_TPCC_RMSZ_MASK) >>
908 			    PCMCIA_TPCC_RMSZ_SHIFT);
909 			rfsz = ((reg & PCMCIA_TPCC_RFSZ_MASK) >>
910 			    PCMCIA_TPCC_RFSZ_SHIFT);
911 
912 			if (tuple->length < (rasz + rmsz + rfsz)) {
913 				DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too "
914 				    "short %d\n", rasz, rmsz, rfsz,
915 				    tuple->length));
916 				break;
917 			}
918 			if (state->pf == NULL) {
919 				create_pf(state);
920 				state->pf->function = PCMCIA_FUNCTION_UNSPEC;
921 			}
922 			state->pf->last_config_index =
923 			    pcmcia_tuple_read_1(tuple, 1);
924 
925 			state->pf->ccr_base = 0;
926 			for (i = 0; i < rasz; i++)
927 				state->pf->ccr_base |=
928 				    ((pcmcia_tuple_read_1(tuple, 2 + i)) <<
929 				    (i * 8));
930 
931 			state->pf->ccr_mask = 0;
932 			for (i = 0; i < rmsz; i++)
933 				state->pf->ccr_mask |=
934 				    ((pcmcia_tuple_read_1(tuple,
935 				    2 + rasz + i)) << (i * 8));
936 
937 			/* skip the reserved area and subtuples */
938 
939 			/* reset the default cfe for each cfe list */
940 			state->temp_cfe = init_cfe;
941 			state->default_cfe = &state->temp_cfe;
942 		}
943 		DPRINTF(("CISTPL_CONFIG\n"));
944 		break;
945 	case PCMCIA_CISTPL_CFTABLE_ENTRY:
946 		{
947 			int idx, i, j;
948 			u_int reg, reg2;
949 			u_int intface, def, num;
950 			u_int power, timing, iospace, irq, memspace, misc;
951 			struct pcmcia_config_entry *cfe;
952 
953 			idx = 0;
954 
955 			reg = pcmcia_tuple_read_1(tuple, idx);
956 			idx++;
957 			intface = reg & PCMCIA_TPCE_INDX_INTFACE;
958 			def = reg & PCMCIA_TPCE_INDX_DEFAULT;
959 			num = reg & PCMCIA_TPCE_INDX_NUM_MASK;
960 
961 			/*
962 			 * this is a little messy.  Some cards have only a
963 			 * cfentry with the default bit set.  So, as we go
964 			 * through the list, we add new indexes to the queue,
965 			 * and keep a pointer to the last one with the
966 			 * default bit set.  if we see a record with the same
967 			 * index, as the default, we stash the default and
968 			 * replace the queue entry. otherwise, we just add
969 			 * new entries to the queue, pointing the default ptr
970 			 * at them if the default bit is set.  if we get to
971 			 * the end with the default pointer pointing at a
972 			 * record which hasn't had a matching index, that's
973 			 * ok; it just becomes a cfentry like any other.
974 			 */
975 
976 			/*
977 			 * if the index in the cis differs from the default
978 			 * cis, create new entry in the queue and start it
979 			 * with the current default
980 			 */
981 			if (state->default_cfe == NULL) {
982 				DPRINTF(("CISTPL_CFTABLE_ENTRY with no "
983 				    "default\n"));
984 				break;
985 			}
986 			if (num != state->default_cfe->number) {
987 				cfe = (struct pcmcia_config_entry *)
988 				    malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
989 
990 				*cfe = *state->default_cfe;
991 
992 				SIMPLEQ_INSERT_TAIL(&state->pf->cfe_head,
993 				    cfe, cfe_list);
994 
995 				cfe->number = num;
996 
997 				/*
998 				 * if the default bit is set in the cis, then
999 				 * point the new default at whatever is being
1000 				 * filled in
1001 				 */
1002 				if (def)
1003 					state->default_cfe = cfe;
1004 			} else {
1005 				/*
1006 				 * the cis index matches the default index,
1007 				 * fill in the default cfentry.  It is
1008 				 * assumed that the cfdefault index is in the
1009 				 * queue.  For it to be otherwise, the cis
1010 				 * index would have to be -1 (initial
1011 				 * condition) which is not possible, or there
1012 				 * would have to be a preceding cis entry
1013 				 * which had the same cis index and had the
1014 				 * default bit unset. Neither condition
1015 				 * should happen.  If it does, this cfentry
1016 				 * is lost (written into temp space), which
1017 				 * is an acceptable failure mode.
1018 				 */
1019 
1020 				cfe = state->default_cfe;
1021 
1022 				/*
1023 				 * if the cis entry does not have the default
1024 				 * bit set, copy the default out of the way
1025 				 * first.
1026 				 */
1027 				if (!def) {
1028 					state->temp_cfe = *state->default_cfe;
1029 					state->default_cfe = &state->temp_cfe;
1030 				}
1031 			}
1032 
1033 			if (intface) {
1034 				reg = pcmcia_tuple_read_1(tuple, idx);
1035 				idx++;
1036 				cfe->flags &= ~(PCMCIA_CFE_MWAIT_REQUIRED
1037 				    | PCMCIA_CFE_RDYBSY_ACTIVE
1038 				    | PCMCIA_CFE_WP_ACTIVE
1039 				    | PCMCIA_CFE_BVD_ACTIVE);
1040 				if (reg & PCMCIA_TPCE_IF_MWAIT)
1041 					cfe->flags |= PCMCIA_CFE_MWAIT_REQUIRED;
1042 				if (reg & PCMCIA_TPCE_IF_RDYBSY)
1043 					cfe->flags |= PCMCIA_CFE_RDYBSY_ACTIVE;
1044 				if (reg & PCMCIA_TPCE_IF_WP)
1045 					cfe->flags |= PCMCIA_CFE_WP_ACTIVE;
1046 				if (reg & PCMCIA_TPCE_IF_BVD)
1047 					cfe->flags |= PCMCIA_CFE_BVD_ACTIVE;
1048 				cfe->iftype = reg & PCMCIA_TPCE_IF_IFTYPE;
1049 			}
1050 			reg = pcmcia_tuple_read_1(tuple, idx);
1051 			idx++;
1052 
1053 			power = reg & PCMCIA_TPCE_FS_POWER_MASK;
1054 			timing = reg & PCMCIA_TPCE_FS_TIMING;
1055 			iospace = reg & PCMCIA_TPCE_FS_IOSPACE;
1056 			irq = reg & PCMCIA_TPCE_FS_IRQ;
1057 			memspace = reg & PCMCIA_TPCE_FS_MEMSPACE_MASK;
1058 			misc = reg & PCMCIA_TPCE_FS_MISC;
1059 
1060 			if (power) {
1061 				/* skip over power, don't save */
1062 				/* for each parameter selection byte */
1063 				for (i = 0; i < power; i++) {
1064 					reg = pcmcia_tuple_read_1(tuple, idx);
1065 					idx++;
1066 					/* for each bit */
1067 					for (j = 0; j < 7; j++) {
1068 						/* if the bit is set */
1069 						if ((reg >> j) & 0x01) {
1070 							/* skip over bytes */
1071 							do {
1072 								reg2 = pcmcia_tuple_read_1(tuple, idx);
1073 								idx++;
1074 								/*
1075 								 * until
1076 								 * non-
1077 								 * extension
1078 								 * byte
1079 								 */
1080 							} while (reg2 & 0x80);
1081 						}
1082 					}
1083 				}
1084 			}
1085 			if (timing) {
1086 				/* skip over timing, don't save */
1087 				reg = pcmcia_tuple_read_1(tuple, idx);
1088 				idx++;
1089 
1090 				if ((reg & PCMCIA_TPCE_TD_RESERVED_MASK) !=
1091 				    PCMCIA_TPCE_TD_RESERVED_MASK)
1092 					idx++;
1093 				if ((reg & PCMCIA_TPCE_TD_RDYBSY_MASK) !=
1094 				    PCMCIA_TPCE_TD_RDYBSY_MASK)
1095 					idx++;
1096 				if ((reg & PCMCIA_TPCE_TD_WAIT_MASK) !=
1097 				    PCMCIA_TPCE_TD_WAIT_MASK)
1098 					idx++;
1099 			}
1100 			if (iospace) {
1101 				if (tuple->length <= idx) {
1102 					DPRINTF(("ran out of space before TCPE_IO\n"));
1103 					goto abort_cfe;
1104 				}
1105 
1106 				reg = pcmcia_tuple_read_1(tuple, idx);
1107 				idx++;
1108 
1109 				cfe->flags &=
1110 				    ~(PCMCIA_CFE_IO8 | PCMCIA_CFE_IO16);
1111 				if (reg & PCMCIA_TPCE_IO_BUSWIDTH_8BIT)
1112 					cfe->flags |= PCMCIA_CFE_IO8;
1113 				if (reg & PCMCIA_TPCE_IO_BUSWIDTH_16BIT)
1114 					cfe->flags |= PCMCIA_CFE_IO16;
1115 				cfe->iomask =
1116 				    reg & PCMCIA_TPCE_IO_IOADDRLINES_MASK;
1117 
1118 				if (reg & PCMCIA_TPCE_IO_HASRANGE) {
1119 					reg = pcmcia_tuple_read_1(tuple, idx);
1120 					idx++;
1121 
1122 					cfe->num_iospace = 1 + (reg &
1123 					    PCMCIA_TPCE_IO_RANGE_COUNT);
1124 
1125 					if (cfe->num_iospace >
1126 					    (sizeof(cfe->iospace) /
1127 					     sizeof(cfe->iospace[0]))) {
1128 						DPRINTF(("too many io "
1129 						    "spaces %d",
1130 						    cfe->num_iospace));
1131 						state->card->error++;
1132 						break;
1133 					}
1134 					for (i = 0; i < cfe->num_iospace; i++) {
1135 						switch (reg & PCMCIA_TPCE_IO_RANGE_ADDRSIZE_MASK) {
1136 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_NONE:
1137 							cfe->iospace[i].start =
1138 							    0;
1139 							break;
1140 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_ONE:
1141 							cfe->iospace[i].start =
1142 								pcmcia_tuple_read_1(tuple, idx);
1143 							idx++;
1144 							break;
1145 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_TWO:
1146 							cfe->iospace[i].start =
1147 								pcmcia_tuple_read_2(tuple, idx);
1148 							idx += 2;
1149 							break;
1150 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_FOUR:
1151 							cfe->iospace[i].start =
1152 								pcmcia_tuple_read_4(tuple, idx);
1153 							idx += 4;
1154 							break;
1155 						}
1156 						switch (reg &
1157 							PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_MASK) {
1158 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_NONE:
1159 							cfe->iospace[i].length =
1160 							    0;
1161 							break;
1162 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_ONE:
1163 							cfe->iospace[i].length =
1164 								pcmcia_tuple_read_1(tuple, idx);
1165 							idx++;
1166 							break;
1167 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_TWO:
1168 							cfe->iospace[i].length =
1169 								pcmcia_tuple_read_2(tuple, idx);
1170 							idx += 2;
1171 							break;
1172 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_FOUR:
1173 							cfe->iospace[i].length =
1174 								pcmcia_tuple_read_4(tuple, idx);
1175 							idx += 4;
1176 							break;
1177 						}
1178 						cfe->iospace[i].length++;
1179 					}
1180 				} else {
1181 					cfe->num_iospace = 1;
1182 					cfe->iospace[0].start = 0;
1183 					cfe->iospace[0].length =
1184 					    (1 << cfe->iomask);
1185 				}
1186 			}
1187 			if (irq) {
1188 				if (tuple->length <= idx) {
1189 					DPRINTF(("ran out of space before TCPE_IR\n"));
1190 					goto abort_cfe;
1191 				}
1192 
1193 				reg = pcmcia_tuple_read_1(tuple, idx);
1194 				idx++;
1195 
1196 				cfe->flags &= ~(PCMCIA_CFE_IRQSHARE
1197 				    | PCMCIA_CFE_IRQPULSE
1198 				    | PCMCIA_CFE_IRQLEVEL);
1199 				if (reg & PCMCIA_TPCE_IR_SHARE)
1200 					cfe->flags |= PCMCIA_CFE_IRQSHARE;
1201 				if (reg & PCMCIA_TPCE_IR_PULSE)
1202 					cfe->flags |= PCMCIA_CFE_IRQPULSE;
1203 				if (reg & PCMCIA_TPCE_IR_LEVEL)
1204 					cfe->flags |= PCMCIA_CFE_IRQLEVEL;
1205 
1206 				if (reg & PCMCIA_TPCE_IR_HASMASK) {
1207 					/*
1208 					 * it's legal to ignore the
1209 					 * special-interrupt bits, so I will
1210 					 */
1211 
1212 					cfe->irqmask =
1213 					    pcmcia_tuple_read_2(tuple, idx);
1214 					idx += 2;
1215 				} else {
1216 					cfe->irqmask =
1217 					    (1 << (reg & PCMCIA_TPCE_IR_IRQ));
1218 				}
1219 			}
1220 			if (memspace) {
1221 				if (tuple->length <= idx) {
1222 					DPRINTF(("ran out of space before TCPE_MS\n"));
1223 					goto abort_cfe;
1224 				}
1225 
1226 				if (memspace == PCMCIA_TPCE_FS_MEMSPACE_NONE) {
1227 					cfe->num_memspace = 0;
1228 				} else if (memspace == PCMCIA_TPCE_FS_MEMSPACE_LENGTH) {
1229 					cfe->num_memspace = 1;
1230 					cfe->memspace[0].length = 256 *
1231 					    pcmcia_tuple_read_2(tuple, idx);
1232 					idx += 2;
1233 					cfe->memspace[0].cardaddr = 0;
1234 					cfe->memspace[0].hostaddr = 0;
1235 				} else if (memspace ==
1236 				    PCMCIA_TPCE_FS_MEMSPACE_LENGTHADDR) {
1237 					cfe->num_memspace = 1;
1238 					cfe->memspace[0].length = 256 *
1239 					    pcmcia_tuple_read_2(tuple, idx);
1240 					idx += 2;
1241 					cfe->memspace[0].cardaddr = 256 *
1242 					    pcmcia_tuple_read_2(tuple, idx);
1243 					idx += 2;
1244 					cfe->memspace[0].hostaddr = cfe->memspace[0].cardaddr;
1245 				} else {
1246 					int lengthsize;
1247 					int cardaddrsize;
1248 					int hostaddrsize;
1249 
1250 					reg = pcmcia_tuple_read_1(tuple, idx);
1251 					idx++;
1252 
1253 					cfe->num_memspace = (reg &
1254 					    PCMCIA_TPCE_MS_COUNT) + 1;
1255 
1256 					if (cfe->num_memspace >
1257 					    (sizeof(cfe->memspace) /
1258 					     sizeof(cfe->memspace[0]))) {
1259 						DPRINTF(("too many mem "
1260 						    "spaces %d",
1261 						    cfe->num_memspace));
1262 						state->card->error++;
1263 						break;
1264 					}
1265 					lengthsize =
1266 						((reg & PCMCIA_TPCE_MS_LENGTH_SIZE_MASK) >>
1267 						 PCMCIA_TPCE_MS_LENGTH_SIZE_SHIFT);
1268 					cardaddrsize =
1269 						((reg & PCMCIA_TPCE_MS_CARDADDR_SIZE_MASK) >>
1270 						 PCMCIA_TPCE_MS_CARDADDR_SIZE_SHIFT);
1271 					hostaddrsize =
1272 						(reg & PCMCIA_TPCE_MS_HOSTADDR) ? cardaddrsize : 0;
1273 
1274 					if (lengthsize == 0) {
1275 						DPRINTF(("cfe memspace "
1276 						    "lengthsize == 0"));
1277 						state->card->error++;
1278 					}
1279 					for (i = 0; i < cfe->num_memspace; i++) {
1280 						if (lengthsize) {
1281 							cfe->memspace[i].length =
1282 								256 * pcmcia_tuple_read_n(tuple, lengthsize,
1283 								       idx);
1284 							idx += lengthsize;
1285 						} else {
1286 							cfe->memspace[i].length = 0;
1287 						}
1288 						if (cfe->memspace[i].length == 0) {
1289 							DPRINTF(("cfe->memspace[%d].length == 0",
1290 								 i));
1291 							state->card->error++;
1292 						}
1293 						if (cardaddrsize) {
1294 							cfe->memspace[i].cardaddr =
1295 								256 * pcmcia_tuple_read_n(tuple, cardaddrsize,
1296 								       idx);
1297 							idx += cardaddrsize;
1298 						} else {
1299 							cfe->memspace[i].cardaddr = 0;
1300 						}
1301 						if (hostaddrsize) {
1302 							cfe->memspace[i].hostaddr =
1303 								256 * pcmcia_tuple_read_n(tuple, hostaddrsize,
1304 								       idx);
1305 							idx += hostaddrsize;
1306 						} else {
1307 							cfe->memspace[i].hostaddr = 0;
1308 						}
1309 					}
1310 				}
1311 			}
1312 			if (misc) {
1313 				if (tuple->length <= idx) {
1314 					DPRINTF(("ran out of space before TCPE_MI\n"));
1315 					goto abort_cfe;
1316 				}
1317 
1318 				reg = pcmcia_tuple_read_1(tuple, idx);
1319 				idx++;
1320 
1321 				cfe->flags &= ~(PCMCIA_CFE_POWERDOWN
1322 				    | PCMCIA_CFE_READONLY
1323 				    | PCMCIA_CFE_AUDIO);
1324 				if (reg & PCMCIA_TPCE_MI_PWRDOWN)
1325 					cfe->flags |= PCMCIA_CFE_POWERDOWN;
1326 				if (reg & PCMCIA_TPCE_MI_READONLY)
1327 					cfe->flags |= PCMCIA_CFE_READONLY;
1328 				if (reg & PCMCIA_TPCE_MI_AUDIO)
1329 					cfe->flags |= PCMCIA_CFE_AUDIO;
1330 				cfe->maxtwins = reg & PCMCIA_TPCE_MI_MAXTWINS;
1331 
1332 				while (reg & PCMCIA_TPCE_MI_EXT) {
1333 					reg = pcmcia_tuple_read_1(tuple, idx);
1334 					idx++;
1335 				}
1336 			}
1337 			/* skip all the subtuples */
1338 		}
1339 
1340 	abort_cfe:
1341 		DPRINTF(("CISTPL_CFTABLE_ENTRY\n"));
1342 		break;
1343 	default:
1344 		DPRINTF(("unhandled CISTPL %x\n", tuple->code));
1345 		break;
1346 	}
1347 
1348 	return (0);
1349 }
1350 
1351 
1352 
1353 static int
1354 decode_funce(tuple, pf)
1355 	struct pcmcia_tuple *tuple;
1356 	struct pcmcia_function *pf;
1357 {
1358 	int type = pcmcia_tuple_read_1(tuple, 0);
1359 
1360 	switch (pf->function) {
1361 	case PCMCIA_FUNCTION_DISK:
1362 		if (type == PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE) {
1363 			pf->pf_funce_disk_interface
1364 			    = pcmcia_tuple_read_1(tuple, 1);
1365 		}
1366 		break;
1367 	case PCMCIA_FUNCTION_NETWORK:
1368 		if (type == PCMCIA_TPLFE_TYPE_LAN_NID) {
1369 			int i;
1370 			int len = pcmcia_tuple_read_1(tuple, 1);
1371 			if (tuple->length < 2 + len || len > 8) {
1372 				/* tuple length not enough or nid too long */
1373 				break;
1374 			}
1375 			for (i = 0; i < len; ++i) {
1376 				pf->pf_funce_lan_nid[i]
1377 				    = pcmcia_tuple_read_1(tuple, 2 + i);
1378 			}
1379 			pf->pf_funce_lan_nidlen = len;
1380 		}
1381 		break;
1382 	default:
1383 		break;
1384 	}
1385 
1386 	return 0;
1387 }
1388