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