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