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