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