pccard_cis.c revision 167086
1104640Simp/* $NetBSD: pcmcia_cis.c,v 1.17 2000/02/10 09:01:52 chopps Exp $ */
252506Simp/* $FreeBSD: head/sys/dev/pccard/pccard_cis.c 167086 2007-02-27 17:23:29Z jhb $ */
352506Simp
4139749Simp/*-
552506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
652506Simp *
752506Simp * Redistribution and use in source and binary forms, with or without
852506Simp * modification, are permitted provided that the following conditions
952506Simp * are met:
1052506Simp * 1. Redistributions of source code must retain the above copyright
1152506Simp *    notice, this list of conditions and the following disclaimer.
1252506Simp * 2. Redistributions in binary form must reproduce the above copyright
1352506Simp *    notice, this list of conditions and the following disclaimer in the
1452506Simp *    documentation and/or other materials provided with the distribution.
1552506Simp * 3. All advertising materials mentioning features or use of this software
1652506Simp *    must display the following acknowledgement:
1752506Simp *	This product includes software developed by Marc Horowitz.
1852506Simp * 4. The name of the author may not be used to endorse or promote products
1952506Simp *    derived from this software without specific prior written permission.
2052506Simp *
2152506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2252506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2352506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2452506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2552506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2652506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2752506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2852506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2952506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3052506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3152506Simp */
3252506Simp
3352506Simp#include <sys/param.h>
3452506Simp#include <sys/systm.h>
3552506Simp#include <sys/malloc.h>
3652506Simp#include <sys/module.h>
3752506Simp#include <sys/kernel.h>
3852506Simp#include <sys/queue.h>
3952506Simp#include <sys/types.h>
4052506Simp
4152506Simp#include <sys/bus.h>
4252506Simp#include <machine/bus.h>
4352506Simp#include <sys/rman.h>
4452506Simp#include <machine/resource.h>
4552506Simp
4652506Simp#include <dev/pccard/pccardreg.h>
4752506Simp#include <dev/pccard/pccardvar.h>
48150361Simp#include <dev/pccard/pccardvarp.h>
49120868Simp#include <dev/pccard/pccard_cis.h>
5052506Simp
5159193Simp#include "card_if.h"
5259193Simp
5391786Simpextern int	pccard_cis_debug;
5491786Simp
5559193Simp#define PCCARDCISDEBUG
5652506Simp#ifdef PCCARDCISDEBUG
57119161Simp#define	DPRINTF(arg) do { if (pccard_cis_debug) printf arg; } while (0)
58119161Simp#define	DEVPRINTF(arg) do { if (pccard_cis_debug) device_printf arg; } while (0)
5952506Simp#else
6052506Simp#define	DPRINTF(arg)
6155720Simp#define	DEVPRINTF(arg)
6252506Simp#endif
6352506Simp
64128169Simp#define	PCCARD_CIS_SIZE		4096
6552506Simp
6652506Simpstruct cis_state {
6752506Simp	int	count;
6852506Simp	int	gotmfc;
6952506Simp	struct pccard_config_entry temp_cfe;
7052506Simp	struct pccard_config_entry *default_cfe;
7152506Simp	struct pccard_card *card;
7252506Simp	struct pccard_function *pf;
7352506Simp};
7452506Simp
75147729Simpstatic int pccard_parse_cis_tuple(const struct pccard_tuple *, void *);
76147729Simpstatic int decode_funce(const struct pccard_tuple *, struct pccard_function *);
7752506Simp
7852506Simpvoid
7955720Simppccard_read_cis(struct pccard_softc *sc)
8052506Simp{
8152506Simp	struct cis_state state;
8252506Simp
83104640Simp	bzero(&state, sizeof state);
8452506Simp	state.card = &sc->card;
8552506Simp	state.card->error = 0;
8652506Simp	state.card->cis1_major = -1;
8752506Simp	state.card->cis1_minor = -1;
8852506Simp	state.card->cis1_info[0] = NULL;
8952506Simp	state.card->cis1_info[1] = NULL;
9052506Simp	state.card->cis1_info[2] = NULL;
9152506Simp	state.card->cis1_info[3] = NULL;
9286272Simp	state.card->manufacturer = PCMCIA_VENDOR_INVALID;
9386272Simp	state.card->product = PCMCIA_PRODUCT_INVALID;
9452506Simp	STAILQ_INIT(&state.card->pf_head);
9552506Simp	state.pf = NULL;
9652506Simp
97161108Simp	/*
98161108Simp	 * XXX The following shouldn't be needed, but some slow cards
99161108Simp	 * XXX seem to need it still.  Need to investigate if there's
100161108Simp	 * XXX a way to tell if the card is 'ready' or not rather than
101161108Simp	 * XXX sleeping like this.  We're called just after the power
102161108Simp	 * XXX up of the socket.  The standard timing diagrams don't
103161108Simp	 * XXX seem to indicate that a delay is required.  The old
104161108Simp	 * XXX delay was 1s.  This delay is .1s.
105161108Simp	 */
106167086Sjhb	pause("pccard", hz / 10);
107150098Simp	if (pccard_scan_cis(device_get_parent(sc->dev), sc->dev,
108150098Simp	    pccard_parse_cis_tuple, &state) == -1)
10952506Simp		state.card->error++;
11052506Simp}
11152506Simp
11252506Simpint
113150098Simppccard_scan_cis(device_t bus, device_t dev, pccard_scan_t fct, void *arg)
11452506Simp{
11555720Simp	struct resource *res;
11655720Simp	int rid;
11752506Simp	struct pccard_tuple tuple;
11852506Simp	int longlink_present;
11952506Simp	int longlink_common;
120112358Simp	u_long longlink_addr;		/* Type suspect */
12152506Simp	int mfc_count;
12252506Simp	int mfc_index;
123119161Simp#ifdef PCCARDCISDEBUG
124119161Simp	int cis_none_cnt = 10;	/* Only report 10 CIS_NONEs */
125119161Simp#endif
12652506Simp	struct {
12752506Simp		int	common;
12852506Simp		u_long	addr;
12952506Simp	} mfc[256 / 5];
13052506Simp	int ret;
13152506Simp
13252506Simp	ret = 0;
13352506Simp
13452506Simp	/* allocate some memory */
13552506Simp
136128169Simp	/*
137128169Simp	 * Some reports from the field suggest that a 64k memory boundary
138128169Simp	 * helps card CIS being able to be read.  Try it here and see what
139128169Simp	 * the results actually are.  I'm not sure I understand why this
140128169Simp	 * would make cards work better, but it is easy enough to test.
141128169Simp	 */
14255720Simp	rid = 0;
143128169Simp	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0,
144128169Simp	    PCCARD_CIS_SIZE, RF_ACTIVE | rman_make_alignment_flags(64*1024));
14555720Simp	if (res == NULL) {
14655720Simp		device_printf(dev, "can't alloc memory to read attributes\n");
14752506Simp		return -1;
14852506Simp	}
149150098Simp	CARD_SET_RES_FLAGS(bus, dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
15055720Simp	tuple.memt = rman_get_bustag(res);
15155720Simp	tuple.memh = rman_get_bushandle(res);
15259389Simp	tuple.ptr = 0;
15352506Simp
154119060Simp	DPRINTF(("cis mem map 0x%x (resource: 0x%lx)\n",
155119060Simp	    (unsigned int) tuple.memh, rman_get_start(res)));
15655720Simp
15752506Simp	tuple.mult = 2;
15852506Simp
15952506Simp	longlink_present = 1;
16052506Simp	longlink_common = 1;
16152506Simp	longlink_addr = 0;
16252506Simp
16352506Simp	mfc_count = 0;
16452506Simp	mfc_index = 0;
16552506Simp
16655720Simp	DEVPRINTF((dev, "CIS tuple chain:\n"));
16752506Simp
16852506Simp	while (1) {
16952506Simp		while (1) {
170119060Simp			/*
171119060Simp			 * Perform boundary check for insane cards.
172119060Simp			 * If CIS is too long, simulate CIS end.
173119060Simp			 * (This check may not be sufficient for
174119060Simp			 * malicious cards.)
175119060Simp			 */
176119060Simp			if (tuple.mult * tuple.ptr >= PCCARD_CIS_SIZE - 1
177119060Simp			    - 32 /* ad hoc value */ ) {
178119060Simp				printf("CIS is too long -- truncating\n");
179120868Simp				tuple.code = CISTPL_END;
180119060Simp			} else {
181119060Simp				/* get the tuple code */
182119060Simp				tuple.code = pccard_cis_read_1(&tuple, tuple.ptr);
183119060Simp			}
18452506Simp
18552506Simp			/* two special-case tuples */
18652506Simp
187120868Simp			if (tuple.code == CISTPL_NULL) {
188119161Simp#ifdef PCCARDCISDEBUG
189119161Simp				if (cis_none_cnt > 0)
190119161Simp					DPRINTF(("CISTPL_NONE\n 00\n"));
191119161Simp				else if (cis_none_cnt == 0)
192119161Simp					DPRINTF(("TOO MANY CIS_NONE\n"));
193119161Simp				cis_none_cnt--;
194119161Simp#endif
195150361Simp				if ((*fct)(&tuple, arg)) {
196150361Simp					ret = 1;
197150361Simp					goto done;
198150361Simp				}
19952506Simp				tuple.ptr++;
20052506Simp				continue;
201120868Simp			} else if (tuple.code == CISTPL_END) {
20252506Simp				DPRINTF(("CISTPL_END\n ff\n"));
20352506Simp				/* Call the function for the END tuple, since
20452506Simp				   the CIS semantics depend on it */
205147711Simp				if ((*fct)(&tuple, arg)) {
20652506Simp					ret = 1;
20752506Simp					goto done;
20852506Simp				}
20952506Simp				tuple.ptr++;
21052506Simp				break;
21152506Simp			}
21252506Simp			/* now all the normal tuples */
21352506Simp
21452506Simp			tuple.length = pccard_cis_read_1(&tuple, tuple.ptr + 1);
21552506Simp			switch (tuple.code) {
216120868Simp			case CISTPL_LONGLINK_A:
217120868Simp			case CISTPL_LONGLINK_C:
218150361Simp				if ((*fct)(&tuple, arg)) {
219150361Simp					ret = 1;
220150361Simp					goto done;
221150361Simp				}
22252506Simp				if (tuple.length < 4) {
22352506Simp					DPRINTF(("CISTPL_LONGLINK_%s too "
22452506Simp					    "short %d\n",
22552506Simp					    longlink_common ? "C" : "A",
22652506Simp					    tuple.length));
22752506Simp					break;
22852506Simp				}
22952506Simp				longlink_present = 1;
23052506Simp				longlink_common = (tuple.code ==
231120868Simp				    CISTPL_LONGLINK_C) ? 1 : 0;
23252506Simp				longlink_addr = pccard_tuple_read_4(&tuple, 0);
23352506Simp				DPRINTF(("CISTPL_LONGLINK_%s %lx\n",
23452506Simp				    longlink_common ? "C" : "A",
23552506Simp				    longlink_addr));
23652506Simp				break;
237120868Simp			case CISTPL_NO_LINK:
238150361Simp				if ((*fct)(&tuple, arg)) {
239150361Simp					ret = 1;
240150361Simp					goto done;
241150361Simp				}
24252506Simp				longlink_present = 0;
24352506Simp				DPRINTF(("CISTPL_NO_LINK\n"));
24452506Simp				break;
245120868Simp			case CISTPL_CHECKSUM:
246150361Simp				if ((*fct)(&tuple, arg)) {
247150361Simp					ret = 1;
248150361Simp					goto done;
249150361Simp				}
25052506Simp				if (tuple.length < 5) {
25152506Simp					DPRINTF(("CISTPL_CHECKSUM too "
25252506Simp					    "short %d\n", tuple.length));
25352506Simp					break;
25452506Simp				} {
25552506Simp					int16_t offset;
25652506Simp					u_long addr, length;
25752506Simp					u_int cksum, sum;
25852506Simp					int i;
25952506Simp
26090896Simp					offset = (uint16_t)
26152506Simp					    pccard_tuple_read_2(&tuple, 0);
26252506Simp					length = pccard_tuple_read_2(&tuple, 2);
26352506Simp					cksum = pccard_tuple_read_1(&tuple, 4);
26452506Simp
26552506Simp					addr = tuple.ptr + offset;
26652506Simp
26752506Simp					DPRINTF(("CISTPL_CHECKSUM addr=%lx "
26852506Simp					    "len=%lx cksum=%x",
26952506Simp					    addr, length, cksum));
27052506Simp
27152506Simp					/*
27252506Simp					 * XXX do more work to deal with
27352506Simp					 * distant regions
27452506Simp					 */
27552506Simp					if ((addr >= PCCARD_CIS_SIZE) ||
27652506Simp					    ((addr + length) >=
277104640Simp					    PCCARD_CIS_SIZE)) {
27852506Simp						DPRINTF((" skipped, "
27952506Simp						    "too distant\n"));
28052506Simp						break;
28152506Simp					}
28252506Simp					sum = 0;
28352506Simp					for (i = 0; i < length; i++)
28452506Simp						sum +=
28552506Simp						    bus_space_read_1(tuple.memt,
28652506Simp						    tuple.memh,
28752506Simp						    addr + tuple.mult * i);
28852506Simp					if (cksum != (sum & 0xff)) {
28952506Simp						DPRINTF((" failed sum=%x\n",
29052506Simp						    sum));
29155720Simp						device_printf(dev,
29255720Simp						    "CIS checksum failed\n");
29352506Simp#if 0
29452506Simp						/*
29552506Simp						 * XXX Some working cards have
29652506Simp						 * XXX bad checksums!!
29752506Simp						 */
29852506Simp						ret = -1;
29952506Simp#endif
30052506Simp					} else {
30152506Simp						DPRINTF((" ok\n"));
30252506Simp					}
30352506Simp				}
30452506Simp				break;
305120868Simp			case CISTPL_LONGLINK_MFC:
30652506Simp				if (tuple.length < 1) {
30752506Simp					DPRINTF(("CISTPL_LONGLINK_MFC too "
30852506Simp					    "short %d\n", tuple.length));
30952506Simp					break;
31052506Simp				}
311104640Simp				if (((tuple.length - 1) % 5) != 0) {
312104640Simp					DPRINTF(("CISTPL_LONGLINK_MFC bogus "
313104640Simp					    "length %d\n", tuple.length));
314104640Simp					break;
315104640Simp				}
31652506Simp				/*
31752506Simp				 * this is kind of ad hoc, as I don't have
31852506Simp				 * any real documentation
31952506Simp				 */
32052506Simp				{
321104640Simp					int i, tmp_count;
32252506Simp
323104640Simp					/*
324104640Simp					 * put count into tmp var so that
325104640Simp					 * if we have to bail (because it's
326104640Simp					 * a bogus count) it won't be
327104640Simp					 * remembered for later use.
328104640Simp					 */
329104640Simp					tmp_count =
33052506Simp					    pccard_tuple_read_1(&tuple, 0);
331104640Simp
33252506Simp					DPRINTF(("CISTPL_LONGLINK_MFC %d",
333104640Simp					    tmp_count));
334104640Simp
335104640Simp					/*
336104640Simp					 * make _sure_ it's the right size;
337104640Simp					 * if too short, it may be a weird
338104640Simp					 * (unknown/undefined) format
339104640Simp					 */
340104640Simp					if (tuple.length != (tmp_count*5 + 1)) {
341104640Simp						DPRINTF((" bogus length %d\n",
342104640Simp						    tuple.length));
343104640Simp						break;
344104640Simp					}
345104640Simp					/*
346104640Simp					 * sanity check for a programming
347104640Simp					 * error which is difficult to find
348104640Simp					 * when debugging.
349104640Simp					 */
350104640Simp					if (tmp_count >
351104640Simp					    howmany(sizeof mfc, sizeof mfc[0]))
352104640Simp						panic("CISTPL_LONGLINK_MFC mfc "
353104640Simp						    "count would blow stack");
354104640Simp                                        mfc_count = tmp_count;
35552506Simp					for (i = 0; i < mfc_count; i++) {
35652506Simp						mfc[i].common =
35752506Simp						    (pccard_tuple_read_1(&tuple,
35852506Simp						    1 + 5 * i) ==
35952506Simp						    PCCARD_MFC_MEM_COMMON) ?
36052506Simp						    1 : 0;
36152506Simp						mfc[i].addr =
36252506Simp						    pccard_tuple_read_4(&tuple,
36352506Simp						    1 + 5 * i + 1);
36452506Simp						DPRINTF((" %s:%lx",
36552506Simp						    mfc[i].common ? "common" :
36652506Simp						    "attr", mfc[i].addr));
36752506Simp					}
36852506Simp					DPRINTF(("\n"));
36952506Simp				}
37052506Simp				/*
37152506Simp				 * for LONGLINK_MFC, fall through to the
37252506Simp				 * function.  This tuple has structural and
37352506Simp				 * semantic content.
37452506Simp				 */
37552506Simp			default:
37652506Simp				{
377147711Simp					if ((*fct)(&tuple, arg)) {
37852506Simp						ret = 1;
37952506Simp						goto done;
38052506Simp					}
38152506Simp				}
38252506Simp				break;
38352506Simp			}	/* switch */
38452506Simp#ifdef PCCARDCISDEBUG
38552506Simp			/* print the tuple */
38652506Simp			{
38752506Simp				int i;
38852506Simp
38952506Simp				DPRINTF((" %02x %02x", tuple.code,
39052506Simp				    tuple.length));
39152506Simp
39252506Simp				for (i = 0; i < tuple.length; i++) {
39352506Simp					DPRINTF((" %02x",
39452506Simp					    pccard_tuple_read_1(&tuple, i)));
39552506Simp					if ((i % 16) == 13)
39652506Simp						DPRINTF(("\n"));
39752506Simp				}
39887352Simp
39952506Simp				if ((i % 16) != 14)
40052506Simp					DPRINTF(("\n"));
40152506Simp			}
40252506Simp#endif
40352506Simp			/* skip to the next tuple */
40452506Simp			tuple.ptr += 2 + tuple.length;
40552506Simp		}
40652506Simp
40752506Simp		/*
40852506Simp		 * the chain is done.  Clean up and move onto the next one,
40952506Simp		 * if any.  The loop is here in the case that there is an MFC
41052506Simp		 * card with no longlink (which defaults to existing, == 0).
41152506Simp		 * In general, this means that if one pointer fails, it will
41252506Simp		 * try the next one, instead of just bailing.
41352506Simp		 */
41452506Simp		while (1) {
41552506Simp			if (longlink_present) {
416150098Simp				CARD_SET_RES_FLAGS(bus, dev, SYS_RES_MEMORY,
417150098Simp				    rid, longlink_common ?
41893370Simp				    PCCARD_A_MEM_COM : PCCARD_A_MEM_ATTR);
41952506Simp				DPRINTF(("cis mem map %x\n",
42052506Simp				    (unsigned int) tuple.memh));
42152506Simp				tuple.mult = longlink_common ? 1 : 2;
42293370Simp				tuple.ptr = longlink_addr;
42352506Simp				longlink_present = 0;
42452506Simp				longlink_common = 1;
42552506Simp				longlink_addr = 0;
42652506Simp			} else if (mfc_count && (mfc_index < mfc_count)) {
427150098Simp				CARD_SET_RES_FLAGS(bus, dev, SYS_RES_MEMORY,
428150098Simp				    rid, mfc[mfc_index].common ?
429150098Simp				    PCCARD_A_MEM_COM : PCCARD_A_MEM_ATTR);
43052506Simp				DPRINTF(("cis mem map %x\n",
43152506Simp				    (unsigned int) tuple.memh));
43252506Simp				/* set parse state, and point at the next one */
43352506Simp				tuple.mult = mfc[mfc_index].common ? 1 : 2;
43493370Simp				tuple.ptr = mfc[mfc_index].addr;
43552506Simp				mfc_index++;
43652506Simp			} else {
43752506Simp				goto done;
43852506Simp			}
43952506Simp
44052506Simp			/* make sure that the link is valid */
44152506Simp			tuple.code = pccard_cis_read_1(&tuple, tuple.ptr);
442120868Simp			if (tuple.code != CISTPL_LINKTARGET) {
44352506Simp				DPRINTF(("CISTPL_LINKTARGET expected, "
44452506Simp				    "code %02x observed\n", tuple.code));
44552506Simp				continue;
44652506Simp			}
44752506Simp			tuple.length = pccard_cis_read_1(&tuple, tuple.ptr + 1);
44852506Simp			if (tuple.length < 3) {
44952506Simp				DPRINTF(("CISTPL_LINKTARGET too short %d\n",
45052506Simp				    tuple.length));
45152506Simp				continue;
45252506Simp			}
45352506Simp			if ((pccard_tuple_read_1(&tuple, 0) != 'C') ||
45452506Simp			    (pccard_tuple_read_1(&tuple, 1) != 'I') ||
45552506Simp			    (pccard_tuple_read_1(&tuple, 2) != 'S')) {
45652506Simp				DPRINTF(("CISTPL_LINKTARGET magic "
45752506Simp				    "%02x%02x%02x incorrect\n",
45852506Simp				    pccard_tuple_read_1(&tuple, 0),
45952506Simp				    pccard_tuple_read_1(&tuple, 1),
46052506Simp				    pccard_tuple_read_1(&tuple, 2)));
46152506Simp				continue;
46252506Simp			}
46352506Simp			tuple.ptr += 2 + tuple.length;
46452506Simp			break;
46552506Simp		}
46652506Simp	}
46752506Simp
46852506Simpdone:
46955720Simp	bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
47052506Simp
47152506Simp	return (ret);
47252506Simp}
47352506Simp
47452506Simp/* XXX this is incredibly verbose.  Not sure what trt is */
47552506Simp
47652506Simpvoid
47752506Simppccard_print_cis(device_t dev)
47852506Simp{
47964850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
48052506Simp	struct pccard_card *card = &sc->card;
48152506Simp	struct pccard_function *pf;
48252506Simp	struct pccard_config_entry *cfe;
48352506Simp	int i;
48452506Simp
48552506Simp	device_printf(dev, "CIS version ");
48652506Simp	if (card->cis1_major == 4) {
48752506Simp		if (card->cis1_minor == 0)
48852506Simp			printf("PCCARD 1.0\n");
48952506Simp		else if (card->cis1_minor == 1)
49052506Simp			printf("PCCARD 2.0 or 2.1\n");
49152506Simp	} else if (card->cis1_major >= 5)
49252506Simp		printf("PC Card Standard %d.%d\n", card->cis1_major, card->cis1_minor);
49352506Simp	else
49452506Simp		printf("unknown (major=%d, minor=%d)\n",
49552506Simp		    card->cis1_major, card->cis1_minor);
49652506Simp
49752506Simp	device_printf(dev, "CIS info: ");
49852506Simp	for (i = 0; i < 4; i++) {
49952506Simp		if (card->cis1_info[i] == NULL)
50052506Simp			break;
50152506Simp		if (i)
50252506Simp			printf(", ");
50352506Simp		printf("%s", card->cis1_info[i]);
50452506Simp	}
50552506Simp	printf("\n");
50652506Simp
50752506Simp	device_printf(dev, "Manufacturer code 0x%x, product 0x%x\n",
50852506Simp	    card->manufacturer, card->product);
50952506Simp
51052506Simp	STAILQ_FOREACH(pf, &card->pf_head, pf_list) {
51152506Simp		device_printf(dev, "function %d: ", pf->number);
51252506Simp
51352506Simp		switch (pf->function) {
51452506Simp		case PCCARD_FUNCTION_UNSPEC:
51552506Simp			printf("unspecified");
51652506Simp			break;
51752506Simp		case PCCARD_FUNCTION_MULTIFUNCTION:
51852506Simp			printf("multi-function");
51952506Simp			break;
52052506Simp		case PCCARD_FUNCTION_MEMORY:
52152506Simp			printf("memory");
52252506Simp			break;
52352506Simp		case PCCARD_FUNCTION_SERIAL:
52452506Simp			printf("serial port");
52552506Simp			break;
52652506Simp		case PCCARD_FUNCTION_PARALLEL:
52752506Simp			printf("parallel port");
52852506Simp			break;
52952506Simp		case PCCARD_FUNCTION_DISK:
53052506Simp			printf("fixed disk");
53152506Simp			break;
53252506Simp		case PCCARD_FUNCTION_VIDEO:
53352506Simp			printf("video adapter");
53452506Simp			break;
53552506Simp		case PCCARD_FUNCTION_NETWORK:
53652506Simp			printf("network adapter");
53752506Simp			break;
53852506Simp		case PCCARD_FUNCTION_AIMS:
53952506Simp			printf("auto incrementing mass storage");
54052506Simp			break;
54152506Simp		case PCCARD_FUNCTION_SCSI:
54252506Simp			printf("SCSI bridge");
54352506Simp			break;
54452506Simp		case PCCARD_FUNCTION_SECURITY:
54552506Simp			printf("Security services");
54652506Simp			break;
54752506Simp		case PCCARD_FUNCTION_INSTRUMENT:
54852506Simp			printf("Instrument");
54952506Simp			break;
55052506Simp		default:
55152506Simp			printf("unknown (%d)", pf->function);
55252506Simp			break;
55352506Simp		}
55452506Simp
555106914Smux		printf(", ccr addr %x mask %x\n", pf->ccr_base, pf->ccr_mask);
55652506Simp
55752506Simp		STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
55852506Simp			device_printf(dev, "function %d, config table entry "
55952506Simp			    "%d: ", pf->number, cfe->number);
56052506Simp
56152506Simp			switch (cfe->iftype) {
56252506Simp			case PCCARD_IFTYPE_MEMORY:
56352506Simp				printf("memory card");
56452506Simp				break;
56552506Simp			case PCCARD_IFTYPE_IO:
56652506Simp				printf("I/O card");
56752506Simp				break;
56852506Simp			default:
56952506Simp				printf("card type unknown");
57052506Simp				break;
57152506Simp			}
57252506Simp
57352506Simp			printf("; irq mask %x", cfe->irqmask);
57452506Simp
57552506Simp			if (cfe->num_iospace) {
57652506Simp				printf("; iomask %lx, iospace", cfe->iomask);
57752506Simp
57853813Simp				for (i = 0; i < cfe->num_iospace; i++) {
57953813Simp					printf(" %lx", cfe->iospace[i].start);
58053813Simp					if (cfe->iospace[i].length)
58153813Simp						printf("-%lx",
58253813Simp						    cfe->iospace[i].start +
58353813Simp						    cfe->iospace[i].length - 1);
58453813Simp				}
58552506Simp			}
58652506Simp			if (cfe->num_memspace) {
58752506Simp				printf("; memspace");
58852506Simp
58953813Simp				for (i = 0; i < cfe->num_memspace; i++) {
59053813Simp					printf(" %lx",
59153813Simp					    cfe->memspace[i].cardaddr);
59253813Simp					if (cfe->memspace[i].length)
59353813Simp						printf("-%lx",
59453813Simp						    cfe->memspace[i].cardaddr +
59553813Simp						    cfe->memspace[i].length - 1);
59653813Simp					if (cfe->memspace[i].hostaddr)
59753813Simp						printf("@%lx",
59853813Simp						    cfe->memspace[i].hostaddr);
59953813Simp				}
60052506Simp			}
60152506Simp			if (cfe->maxtwins)
60252506Simp				printf("; maxtwins %d", cfe->maxtwins);
60352506Simp
60452506Simp			printf(";");
60552506Simp
60652506Simp			if (cfe->flags & PCCARD_CFE_MWAIT_REQUIRED)
60752506Simp				printf(" mwait_required");
60852506Simp			if (cfe->flags & PCCARD_CFE_RDYBSY_ACTIVE)
60952506Simp				printf(" rdybsy_active");
61052506Simp			if (cfe->flags & PCCARD_CFE_WP_ACTIVE)
61152506Simp				printf(" wp_active");
61252506Simp			if (cfe->flags & PCCARD_CFE_BVD_ACTIVE)
61352506Simp				printf(" bvd_active");
61452506Simp			if (cfe->flags & PCCARD_CFE_IO8)
61552506Simp				printf(" io8");
61652506Simp			if (cfe->flags & PCCARD_CFE_IO16)
61752506Simp				printf(" io16");
61852506Simp			if (cfe->flags & PCCARD_CFE_IRQSHARE)
61952506Simp				printf(" irqshare");
62052506Simp			if (cfe->flags & PCCARD_CFE_IRQPULSE)
62152506Simp				printf(" irqpulse");
62252506Simp			if (cfe->flags & PCCARD_CFE_IRQLEVEL)
62352506Simp				printf(" irqlevel");
62452506Simp			if (cfe->flags & PCCARD_CFE_POWERDOWN)
62552506Simp				printf(" powerdown");
62652506Simp			if (cfe->flags & PCCARD_CFE_READONLY)
62752506Simp				printf(" readonly");
62852506Simp			if (cfe->flags & PCCARD_CFE_AUDIO)
62952506Simp				printf(" audio");
63052506Simp
63152506Simp			printf("\n");
63252506Simp		}
63352506Simp	}
63452506Simp
63552506Simp	if (card->error)
63652506Simp		device_printf(dev, "%d errors found while parsing CIS\n",
63752506Simp		    card->error);
63852506Simp}
63952506Simp
640147711Simpstatic int
641147729Simppccard_parse_cis_tuple(const struct pccard_tuple *tuple, void *arg)
64252506Simp{
64352506Simp	/* most of these are educated guesses */
64452506Simp	static struct pccard_config_entry init_cfe = {
64552506Simp		-1, PCCARD_CFE_RDYBSY_ACTIVE | PCCARD_CFE_WP_ACTIVE |
64652506Simp		PCCARD_CFE_BVD_ACTIVE, PCCARD_IFTYPE_MEMORY,
64752506Simp	};
64852506Simp
64952506Simp	struct cis_state *state = arg;
65052506Simp
65152506Simp	switch (tuple->code) {
652120868Simp	case CISTPL_END:
65352506Simp		/* if we've seen a LONGLINK_MFC, and this is the first
65452506Simp		 * END after it, reset the function list.
65552506Simp		 *
65652506Simp		 * XXX This might also be the right place to start a
65752506Simp		 * new function, but that assumes that a function
65852506Simp		 * definition never crosses any longlink, and I'm not
65952506Simp		 * sure about that.  This is probably safe for MFC
66052506Simp		 * cards, but what we have now isn't broken, so I'd
66152506Simp		 * rather not change it.
66252506Simp		 */
66352506Simp		if (state->gotmfc == 1) {
66452506Simp			struct pccard_function *pf, *pfnext;
66552506Simp
66652506Simp			for (pf = STAILQ_FIRST(&state->card->pf_head);
66752506Simp			     pf != NULL; pf = pfnext) {
66852506Simp				pfnext = STAILQ_NEXT(pf, pf_list);
66952506Simp				free(pf, M_DEVBUF);
67052506Simp			}
67152506Simp
67252506Simp			STAILQ_INIT(&state->card->pf_head);
67352506Simp
67452506Simp			state->count = 0;
67552506Simp			state->gotmfc = 2;
67652506Simp			state->pf = NULL;
67752506Simp		}
67852506Simp		break;
679120868Simp	case CISTPL_LONGLINK_MFC:
68052506Simp		/*
68152506Simp		 * this tuple's structure was dealt with in scan_cis.  here,
68252506Simp		 * record the fact that the MFC tuple was seen, so that
68352506Simp		 * functions declared before the MFC link can be cleaned
68452506Simp		 * up.
68552506Simp		 */
68652506Simp		state->gotmfc = 1;
68752506Simp		break;
68852506Simp#ifdef PCCARDCISDEBUG
689120868Simp	case CISTPL_DEVICE:
690120868Simp	case CISTPL_DEVICE_A:
69152506Simp		{
69252506Simp			u_int reg, dtype, dspeed;
69352506Simp
69452506Simp			reg = pccard_tuple_read_1(tuple, 0);
69552506Simp			dtype = reg & PCCARD_DTYPE_MASK;
69652506Simp			dspeed = reg & PCCARD_DSPEED_MASK;
69752506Simp
69852506Simp			DPRINTF(("CISTPL_DEVICE%s type=",
699120868Simp			(tuple->code == CISTPL_DEVICE) ? "" : "_A"));
70052506Simp			switch (dtype) {
70152506Simp			case PCCARD_DTYPE_NULL:
70252506Simp				DPRINTF(("null"));
70352506Simp				break;
70452506Simp			case PCCARD_DTYPE_ROM:
70552506Simp				DPRINTF(("rom"));
70652506Simp				break;
70752506Simp			case PCCARD_DTYPE_OTPROM:
70852506Simp				DPRINTF(("otprom"));
70952506Simp				break;
71052506Simp			case PCCARD_DTYPE_EPROM:
71152506Simp				DPRINTF(("eprom"));
71252506Simp				break;
71352506Simp			case PCCARD_DTYPE_EEPROM:
71452506Simp				DPRINTF(("eeprom"));
71552506Simp				break;
71652506Simp			case PCCARD_DTYPE_FLASH:
71752506Simp				DPRINTF(("flash"));
71852506Simp				break;
71952506Simp			case PCCARD_DTYPE_SRAM:
72052506Simp				DPRINTF(("sram"));
72152506Simp				break;
72252506Simp			case PCCARD_DTYPE_DRAM:
72352506Simp				DPRINTF(("dram"));
72452506Simp				break;
72552506Simp			case PCCARD_DTYPE_FUNCSPEC:
72652506Simp				DPRINTF(("funcspec"));
72752506Simp				break;
72852506Simp			case PCCARD_DTYPE_EXTEND:
72952506Simp				DPRINTF(("extend"));
73052506Simp				break;
73152506Simp			default:
73252506Simp				DPRINTF(("reserved"));
73352506Simp				break;
73452506Simp			}
73552506Simp			DPRINTF((" speed="));
73652506Simp			switch (dspeed) {
73752506Simp			case PCCARD_DSPEED_NULL:
73852506Simp				DPRINTF(("null"));
73952506Simp				break;
74052506Simp			case PCCARD_DSPEED_250NS:
74152506Simp				DPRINTF(("250ns"));
74252506Simp				break;
74352506Simp			case PCCARD_DSPEED_200NS:
74452506Simp				DPRINTF(("200ns"));
74552506Simp				break;
74652506Simp			case PCCARD_DSPEED_150NS:
74752506Simp				DPRINTF(("150ns"));
74852506Simp				break;
74952506Simp			case PCCARD_DSPEED_100NS:
75052506Simp				DPRINTF(("100ns"));
75152506Simp				break;
75252506Simp			case PCCARD_DSPEED_EXT:
75352506Simp				DPRINTF(("ext"));
75452506Simp				break;
75552506Simp			default:
75652506Simp				DPRINTF(("reserved"));
75752506Simp				break;
75852506Simp			}
75952506Simp		}
76052506Simp		DPRINTF(("\n"));
76152506Simp		break;
76252506Simp#endif
763120868Simp	case CISTPL_VERS_1:
76452506Simp		if (tuple->length < 6) {
76552506Simp			DPRINTF(("CISTPL_VERS_1 too short %d\n",
76652506Simp			    tuple->length));
76752506Simp			break;
76852506Simp		} {
76952506Simp			int start, i, ch, count;
77052506Simp
77152506Simp			state->card->cis1_major = pccard_tuple_read_1(tuple, 0);
77252506Simp			state->card->cis1_minor = pccard_tuple_read_1(tuple, 1);
77352506Simp
77452506Simp			for (count = 0, start = 0, i = 0;
77552506Simp			    (count < 4) && ((i + 4) < 256); i++) {
77652506Simp				ch = pccard_tuple_read_1(tuple, 2 + i);
77752506Simp				if (ch == 0xff)
77852506Simp					break;
77952506Simp				state->card->cis1_info_buf[i] = ch;
78052506Simp				if (ch == 0) {
78152506Simp					state->card->cis1_info[count] =
78252506Simp					    state->card->cis1_info_buf + start;
78352506Simp					start = i + 1;
78452506Simp					count++;
78552506Simp				}
78652506Simp			}
78752506Simp			DPRINTF(("CISTPL_VERS_1\n"));
78852506Simp		}
78952506Simp		break;
790120868Simp	case CISTPL_MANFID:
79152506Simp		if (tuple->length < 4) {
79252506Simp			DPRINTF(("CISTPL_MANFID too short %d\n",
79352506Simp			    tuple->length));
79452506Simp			break;
79552506Simp		}
79652506Simp		state->card->manufacturer = pccard_tuple_read_2(tuple, 0);
79752506Simp		state->card->product = pccard_tuple_read_2(tuple, 2);
79890964Sshiba		/*
799104604Simp		 * This is for xe driver. But not limited to that driver.
80090964Sshiba		 * In PC Card Standard,
80190964Sshiba		 * Manufacturer ID: 2byte.
802104604Simp		 * Product ID: typically 2bytes, but there's no limit on its
803104604Simp		 * size.  prodext is a two byte field, so maybe we should
804104604Simp		 * also handle the '6' case.  So far no cards have surfaced
805104604Simp		 * with a length of '6'.
80690964Sshiba		 */
807147962Simp		if (tuple->length == 5 )
80890964Sshiba			state->card->prodext = pccard_tuple_read_1(tuple, 4);
80952506Simp		DPRINTF(("CISTPL_MANFID\n"));
81052506Simp		break;
811120868Simp	case CISTPL_FUNCID:
81252506Simp		if (tuple->length < 1) {
81352506Simp			DPRINTF(("CISTPL_FUNCID too short %d\n",
81452506Simp			    tuple->length));
81552506Simp			break;
81652506Simp		}
81752506Simp		if ((state->pf == NULL) || (state->gotmfc == 2)) {
81852506Simp			state->pf = malloc(sizeof(*state->pf), M_DEVBUF,
81967897Sdwmalone			    M_NOWAIT | M_ZERO);
82052506Simp			state->pf->number = state->count++;
82152506Simp			state->pf->last_config_index = -1;
82252506Simp			STAILQ_INIT(&state->pf->cfe_head);
82352506Simp
82452506Simp			STAILQ_INSERT_TAIL(&state->card->pf_head, state->pf,
82552506Simp			    pf_list);
82652506Simp		}
82752506Simp		state->pf->function = pccard_tuple_read_1(tuple, 0);
82852506Simp
82952506Simp		DPRINTF(("CISTPL_FUNCID\n"));
83052506Simp		break;
831120868Simp        case CISTPL_FUNCE:
83282781Sshiba                if (state->pf == NULL || state->pf->function <= 0) {
83382781Sshiba                        DPRINTF(("CISTPL_FUNCE is not followed by "
83482781Sshiba                                "valid CISTPL_FUNCID\n"));
83582781Sshiba                        break;
83682781Sshiba                }
837147729Simp                if (tuple->length >= 2)
83882781Sshiba                        decode_funce(tuple, state->pf);
83982781Sshiba                DPRINTF(("CISTPL_FUNCE\n"));
84082781Sshiba                break;
841120868Simp	case CISTPL_CONFIG:
84252506Simp		if (tuple->length < 3) {
84352506Simp			DPRINTF(("CISTPL_CONFIG too short %d\n",
84452506Simp			    tuple->length));
84552506Simp			break;
84652506Simp		} {
84752506Simp			u_int reg, rasz, rmsz, rfsz;
84852506Simp			int i;
84952506Simp
85052506Simp			reg = pccard_tuple_read_1(tuple, 0);
85152506Simp			rasz = 1 + ((reg & PCCARD_TPCC_RASZ_MASK) >>
85252506Simp			    PCCARD_TPCC_RASZ_SHIFT);
85352506Simp			rmsz = 1 + ((reg & PCCARD_TPCC_RMSZ_MASK) >>
85452506Simp			    PCCARD_TPCC_RMSZ_SHIFT);
85552506Simp			rfsz = ((reg & PCCARD_TPCC_RFSZ_MASK) >>
85652506Simp			    PCCARD_TPCC_RFSZ_SHIFT);
85752506Simp
85852506Simp			if (tuple->length < (rasz + rmsz + rfsz)) {
85952506Simp				DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too "
86052506Simp				    "short %d\n", rasz, rmsz, rfsz,
86152506Simp				    tuple->length));
86252506Simp				break;
86352506Simp			}
86452506Simp			if (state->pf == NULL) {
86552506Simp				state->pf = malloc(sizeof(*state->pf),
86667897Sdwmalone				    M_DEVBUF, M_NOWAIT | M_ZERO);
86752506Simp				state->pf->number = state->count++;
86852506Simp				state->pf->last_config_index = -1;
86952506Simp				STAILQ_INIT(&state->pf->cfe_head);
87052506Simp
87152506Simp				STAILQ_INSERT_TAIL(&state->card->pf_head,
87252506Simp				    state->pf, pf_list);
87352506Simp
87452506Simp				state->pf->function = PCCARD_FUNCTION_UNSPEC;
87552506Simp			}
87652506Simp			state->pf->last_config_index =
87752506Simp			    pccard_tuple_read_1(tuple, 1);
87852506Simp
87952506Simp			state->pf->ccr_base = 0;
88052506Simp			for (i = 0; i < rasz; i++)
88152506Simp				state->pf->ccr_base |=
88252506Simp				    ((pccard_tuple_read_1(tuple, 2 + i)) <<
88352506Simp				    (i * 8));
88452506Simp
88552506Simp			state->pf->ccr_mask = 0;
88652506Simp			for (i = 0; i < rmsz; i++)
88752506Simp				state->pf->ccr_mask |=
88852506Simp				    ((pccard_tuple_read_1(tuple,
88952506Simp				    2 + rasz + i)) << (i * 8));
89052506Simp
89152506Simp			/* skip the reserved area and subtuples */
89252506Simp
89352506Simp			/* reset the default cfe for each cfe list */
89452506Simp			state->temp_cfe = init_cfe;
89552506Simp			state->default_cfe = &state->temp_cfe;
89652506Simp		}
89752506Simp		DPRINTF(("CISTPL_CONFIG\n"));
89852506Simp		break;
899120868Simp	case CISTPL_CFTABLE_ENTRY:
90052506Simp		{
90152506Simp			int idx, i, j;
90252506Simp			u_int reg, reg2;
90352506Simp			u_int intface, def, num;
90452506Simp			u_int power, timing, iospace, irq, memspace, misc;
90552506Simp			struct pccard_config_entry *cfe;
90652506Simp
90752506Simp			idx = 0;
90852506Simp
90952506Simp			reg = pccard_tuple_read_1(tuple, idx);
91052506Simp			idx++;
91152506Simp			intface = reg & PCCARD_TPCE_INDX_INTFACE;
91252506Simp			def = reg & PCCARD_TPCE_INDX_DEFAULT;
91352506Simp			num = reg & PCCARD_TPCE_INDX_NUM_MASK;
91452506Simp
91552506Simp			/*
91652506Simp			 * this is a little messy.  Some cards have only a
91752506Simp			 * cfentry with the default bit set.  So, as we go
91852506Simp			 * through the list, we add new indexes to the queue,
91952506Simp			 * and keep a pointer to the last one with the
92052506Simp			 * default bit set.  if we see a record with the same
92152506Simp			 * index, as the default, we stash the default and
92252506Simp			 * replace the queue entry. otherwise, we just add
92352506Simp			 * new entries to the queue, pointing the default ptr
92452506Simp			 * at them if the default bit is set.  if we get to
92552506Simp			 * the end with the default pointer pointing at a
92652506Simp			 * record which hasn't had a matching index, that's
92752506Simp			 * ok; it just becomes a cfentry like any other.
92852506Simp			 */
92952506Simp
93052506Simp			/*
93152506Simp			 * if the index in the cis differs from the default
93252506Simp			 * cis, create new entry in the queue and start it
93352506Simp			 * with the current default
93452506Simp			 */
93552506Simp			if (num != state->default_cfe->number) {
93652506Simp				cfe = (struct pccard_config_entry *)
93752506Simp				    malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
938144159Ssam				if (cfe == NULL) {
939144159Ssam					DPRINTF(("no memory for config entry\n"));
940144159Ssam					goto abort_cfe;
941144159Ssam				}
94252506Simp				*cfe = *state->default_cfe;
94352506Simp
94452506Simp				STAILQ_INSERT_TAIL(&state->pf->cfe_head,
94552506Simp				    cfe, cfe_list);
94652506Simp
94752506Simp				cfe->number = num;
94852506Simp
94952506Simp				/*
95052506Simp				 * if the default bit is set in the cis, then
95152506Simp				 * point the new default at whatever is being
95252506Simp				 * filled in
95352506Simp				 */
95452506Simp				if (def)
95552506Simp					state->default_cfe = cfe;
95652506Simp			} else {
95752506Simp				/*
95852506Simp				 * the cis index matches the default index,
95952506Simp				 * fill in the default cfentry.  It is
96052506Simp				 * assumed that the cfdefault index is in the
96152506Simp				 * queue.  For it to be otherwise, the cis
96252506Simp				 * index would have to be -1 (initial
96352506Simp				 * condition) which is not possible, or there
96452506Simp				 * would have to be a preceding cis entry
96552506Simp				 * which had the same cis index and had the
96652506Simp				 * default bit unset. Neither condition
96752506Simp				 * should happen.  If it does, this cfentry
96852506Simp				 * is lost (written into temp space), which
96952506Simp				 * is an acceptable failure mode.
97052506Simp				 */
97152506Simp
97252506Simp				cfe = state->default_cfe;
97352506Simp
97452506Simp				/*
97552506Simp				 * if the cis entry does not have the default
97652506Simp				 * bit set, copy the default out of the way
97752506Simp				 * first.
97852506Simp				 */
97952506Simp				if (!def) {
98052506Simp					state->temp_cfe = *state->default_cfe;
98152506Simp					state->default_cfe = &state->temp_cfe;
98252506Simp				}
98352506Simp			}
98452506Simp
98552506Simp			if (intface) {
98652506Simp				reg = pccard_tuple_read_1(tuple, idx);
98752506Simp				idx++;
988104640Simp				cfe->flags &= ~(PCCARD_CFE_MWAIT_REQUIRED
989104640Simp				    | PCCARD_CFE_RDYBSY_ACTIVE
990104640Simp				    | PCCARD_CFE_WP_ACTIVE
991104640Simp				    | PCCARD_CFE_BVD_ACTIVE);
99252506Simp				if (reg & PCCARD_TPCE_IF_MWAIT)
99352506Simp					cfe->flags |= PCCARD_CFE_MWAIT_REQUIRED;
99452506Simp				if (reg & PCCARD_TPCE_IF_RDYBSY)
99552506Simp					cfe->flags |= PCCARD_CFE_RDYBSY_ACTIVE;
99652506Simp				if (reg & PCCARD_TPCE_IF_WP)
99752506Simp					cfe->flags |= PCCARD_CFE_WP_ACTIVE;
99852506Simp				if (reg & PCCARD_TPCE_IF_BVD)
99952506Simp					cfe->flags |= PCCARD_CFE_BVD_ACTIVE;
100052506Simp				cfe->iftype = reg & PCCARD_TPCE_IF_IFTYPE;
100152506Simp			}
100252506Simp			reg = pccard_tuple_read_1(tuple, idx);
100352506Simp			idx++;
100452506Simp
100552506Simp			power = reg & PCCARD_TPCE_FS_POWER_MASK;
100652506Simp			timing = reg & PCCARD_TPCE_FS_TIMING;
100752506Simp			iospace = reg & PCCARD_TPCE_FS_IOSPACE;
100852506Simp			irq = reg & PCCARD_TPCE_FS_IRQ;
100952506Simp			memspace = reg & PCCARD_TPCE_FS_MEMSPACE_MASK;
101052506Simp			misc = reg & PCCARD_TPCE_FS_MISC;
101152506Simp
101252506Simp			if (power) {
101352506Simp				/* skip over power, don't save */
101452506Simp				/* for each parameter selection byte */
101552506Simp				for (i = 0; i < power; i++) {
101652506Simp					reg = pccard_tuple_read_1(tuple, idx);
101752506Simp					idx++;
101852506Simp					/* for each bit */
101952506Simp					for (j = 0; j < 7; j++) {
102052506Simp						/* if the bit is set */
102152506Simp						if ((reg >> j) & 0x01) {
102252506Simp							/* skip over bytes */
102352506Simp							do {
102452506Simp								reg2 = pccard_tuple_read_1(tuple, idx);
102552506Simp								idx++;
102652506Simp								/*
102752506Simp								 * until
102852506Simp								 * non-extensi
102952506Simp								 * on byte
103052506Simp								 */
103152506Simp							} while (reg2 & 0x80);
103252506Simp						}
103352506Simp					}
103452506Simp				}
103552506Simp			}
103652506Simp			if (timing) {
103752506Simp				/* skip over timing, don't save */
103852506Simp				reg = pccard_tuple_read_1(tuple, idx);
103952506Simp				idx++;
104052506Simp
104152506Simp				if ((reg & PCCARD_TPCE_TD_RESERVED_MASK) !=
104252506Simp				    PCCARD_TPCE_TD_RESERVED_MASK)
104352506Simp					idx++;
104452506Simp				if ((reg & PCCARD_TPCE_TD_RDYBSY_MASK) !=
104552506Simp				    PCCARD_TPCE_TD_RDYBSY_MASK)
104652506Simp					idx++;
104752506Simp				if ((reg & PCCARD_TPCE_TD_WAIT_MASK) !=
104852506Simp				    PCCARD_TPCE_TD_WAIT_MASK)
104952506Simp					idx++;
105052506Simp			}
105152506Simp			if (iospace) {
105252506Simp				if (tuple->length <= idx) {
105352506Simp					DPRINTF(("ran out of space before TCPE_IO\n"));
105452506Simp					goto abort_cfe;
105552506Simp				}
105652506Simp
105752506Simp				reg = pccard_tuple_read_1(tuple, idx);
105852506Simp				idx++;
1059104640Simp				cfe->flags &=
1060104640Simp				    ~(PCCARD_CFE_IO8 | PCCARD_CFE_IO16);
106152506Simp				if (reg & PCCARD_TPCE_IO_BUSWIDTH_8BIT)
106252506Simp					cfe->flags |= PCCARD_CFE_IO8;
106352506Simp				if (reg & PCCARD_TPCE_IO_BUSWIDTH_16BIT)
106452506Simp					cfe->flags |= PCCARD_CFE_IO16;
106552506Simp				cfe->iomask =
106652506Simp				    reg & PCCARD_TPCE_IO_IOADDRLINES_MASK;
106752506Simp
106852506Simp				if (reg & PCCARD_TPCE_IO_HASRANGE) {
106952506Simp					reg = pccard_tuple_read_1(tuple, idx);
107052506Simp					idx++;
107152506Simp
107252506Simp					cfe->num_iospace = 1 + (reg &
107352506Simp					    PCCARD_TPCE_IO_RANGE_COUNT);
107452506Simp
107552506Simp					if (cfe->num_iospace >
107652506Simp					    (sizeof(cfe->iospace) /
107752506Simp					     sizeof(cfe->iospace[0]))) {
107852506Simp						DPRINTF(("too many io "
107952506Simp						    "spaces %d",
108052506Simp						    cfe->num_iospace));
108152506Simp						state->card->error++;
108252506Simp						break;
108352506Simp					}
108452506Simp					for (i = 0; i < cfe->num_iospace; i++) {
108552506Simp						switch (reg & PCCARD_TPCE_IO_RANGE_ADDRSIZE_MASK) {
108652506Simp						case PCCARD_TPCE_IO_RANGE_ADDRSIZE_ONE:
108752506Simp							cfe->iospace[i].start =
108852506Simp								pccard_tuple_read_1(tuple, idx);
108952506Simp							idx++;
109052506Simp							break;
109152506Simp						case PCCARD_TPCE_IO_RANGE_ADDRSIZE_TWO:
109252506Simp							cfe->iospace[i].start =
109352506Simp								pccard_tuple_read_2(tuple, idx);
109452506Simp							idx += 2;
109552506Simp							break;
109652506Simp						case PCCARD_TPCE_IO_RANGE_ADDRSIZE_FOUR:
109752506Simp							cfe->iospace[i].start =
109852506Simp								pccard_tuple_read_4(tuple, idx);
109952506Simp							idx += 4;
110052506Simp							break;
110152506Simp						}
110252506Simp						switch (reg &
110352506Simp							PCCARD_TPCE_IO_RANGE_LENGTHSIZE_MASK) {
110452506Simp						case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_ONE:
110552506Simp							cfe->iospace[i].length =
110652506Simp								pccard_tuple_read_1(tuple, idx);
110752506Simp							idx++;
110852506Simp							break;
110952506Simp						case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_TWO:
111052506Simp							cfe->iospace[i].length =
111152506Simp								pccard_tuple_read_2(tuple, idx);
111252506Simp							idx += 2;
111352506Simp							break;
111452506Simp						case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_FOUR:
111552506Simp							cfe->iospace[i].length =
111652506Simp								pccard_tuple_read_4(tuple, idx);
111752506Simp							idx += 4;
111852506Simp							break;
111952506Simp						}
112052506Simp						cfe->iospace[i].length++;
112152506Simp					}
112252506Simp				} else {
112352506Simp					cfe->num_iospace = 1;
112452506Simp					cfe->iospace[0].start = 0;
112552506Simp					cfe->iospace[0].length =
112652506Simp					    (1 << cfe->iomask);
112752506Simp				}
112852506Simp			}
112952506Simp			if (irq) {
113052506Simp				if (tuple->length <= idx) {
113152506Simp					DPRINTF(("ran out of space before TCPE_IR\n"));
113252506Simp					goto abort_cfe;
113352506Simp				}
113452506Simp
113552506Simp				reg = pccard_tuple_read_1(tuple, idx);
113652506Simp				idx++;
1137104640Simp				cfe->flags &= ~(PCCARD_CFE_IRQSHARE
1138104640Simp				    | PCCARD_CFE_IRQPULSE
1139104640Simp				    | PCCARD_CFE_IRQLEVEL);
114052506Simp				if (reg & PCCARD_TPCE_IR_SHARE)
114152506Simp					cfe->flags |= PCCARD_CFE_IRQSHARE;
114252506Simp				if (reg & PCCARD_TPCE_IR_PULSE)
114352506Simp					cfe->flags |= PCCARD_CFE_IRQPULSE;
114452506Simp				if (reg & PCCARD_TPCE_IR_LEVEL)
114552506Simp					cfe->flags |= PCCARD_CFE_IRQLEVEL;
114652506Simp
114752506Simp				if (reg & PCCARD_TPCE_IR_HASMASK) {
114852506Simp					/*
114952506Simp					 * it's legal to ignore the
115052506Simp					 * special-interrupt bits, so I will
115152506Simp					 */
115252506Simp
115352506Simp					cfe->irqmask =
115452506Simp					    pccard_tuple_read_2(tuple, idx);
115552506Simp					idx += 2;
115652506Simp				} else {
115752506Simp					cfe->irqmask =
115852506Simp					    (1 << (reg & PCCARD_TPCE_IR_IRQ));
115952506Simp				}
116052506Simp			}
116152506Simp			if (memspace) {
116252506Simp				if (tuple->length <= idx) {
116352506Simp					DPRINTF(("ran out of space before TCPE_MS\n"));
116452506Simp					goto abort_cfe;
116552506Simp				}
116652506Simp
1167142027Simp				if (memspace == PCCARD_TPCE_FS_MEMSPACE_LENGTH) {
116852506Simp					cfe->num_memspace = 1;
116952506Simp					cfe->memspace[0].length = 256 *
117052506Simp					    pccard_tuple_read_2(tuple, idx);
117152506Simp					idx += 2;
117252506Simp					cfe->memspace[0].cardaddr = 0;
117352506Simp					cfe->memspace[0].hostaddr = 0;
117452506Simp				} else if (memspace ==
117552506Simp				    PCCARD_TPCE_FS_MEMSPACE_LENGTHADDR) {
117652506Simp					cfe->num_memspace = 1;
117752506Simp					cfe->memspace[0].length = 256 *
117852506Simp					    pccard_tuple_read_2(tuple, idx);
117952506Simp					idx += 2;
118052506Simp					cfe->memspace[0].cardaddr = 256 *
118152506Simp					    pccard_tuple_read_2(tuple, idx);
118252506Simp					idx += 2;
118352506Simp					cfe->memspace[0].hostaddr = cfe->memspace[0].cardaddr;
118452506Simp				} else {
118552506Simp					int lengthsize;
118652506Simp					int cardaddrsize;
118752506Simp					int hostaddrsize;
118852506Simp
118952506Simp					reg = pccard_tuple_read_1(tuple, idx);
119052506Simp					idx++;
119152506Simp
119253873Simp					cfe->num_memspace = (reg &
119353873Simp					    PCCARD_TPCE_MS_COUNT) + 1;
119452506Simp
119552506Simp					if (cfe->num_memspace >
119652506Simp					    (sizeof(cfe->memspace) /
119752506Simp					     sizeof(cfe->memspace[0]))) {
119852506Simp						DPRINTF(("too many mem "
119952506Simp						    "spaces %d",
120052506Simp						    cfe->num_memspace));
120152506Simp						state->card->error++;
120252506Simp						break;
120352506Simp					}
120452506Simp					lengthsize =
120552506Simp						((reg & PCCARD_TPCE_MS_LENGTH_SIZE_MASK) >>
120652506Simp						 PCCARD_TPCE_MS_LENGTH_SIZE_SHIFT);
120752506Simp					cardaddrsize =
120852506Simp						((reg & PCCARD_TPCE_MS_CARDADDR_SIZE_MASK) >>
120952506Simp						 PCCARD_TPCE_MS_CARDADDR_SIZE_SHIFT);
121052506Simp					hostaddrsize =
121152506Simp						(reg & PCCARD_TPCE_MS_HOSTADDR) ? cardaddrsize : 0;
121252506Simp
121352506Simp					if (lengthsize == 0) {
121452506Simp						DPRINTF(("cfe memspace "
121552506Simp						    "lengthsize == 0"));
121652506Simp						state->card->error++;
121752506Simp					}
121852506Simp					for (i = 0; i < cfe->num_memspace; i++) {
121952506Simp						if (lengthsize) {
122052506Simp							cfe->memspace[i].length =
122152506Simp								256 * pccard_tuple_read_n(tuple, lengthsize,
122252506Simp								       idx);
122352506Simp							idx += lengthsize;
122452506Simp						} else {
122552506Simp							cfe->memspace[i].length = 0;
122652506Simp						}
122752506Simp						if (cfe->memspace[i].length == 0) {
122852506Simp							DPRINTF(("cfe->memspace[%d].length == 0",
122952506Simp								 i));
123052506Simp							state->card->error++;
123152506Simp						}
123252506Simp						if (cardaddrsize) {
123352506Simp							cfe->memspace[i].cardaddr =
123452506Simp								256 * pccard_tuple_read_n(tuple, cardaddrsize,
123552506Simp								       idx);
123652506Simp							idx += cardaddrsize;
123752506Simp						} else {
123852506Simp							cfe->memspace[i].cardaddr = 0;
123952506Simp						}
124052506Simp						if (hostaddrsize) {
124152506Simp							cfe->memspace[i].hostaddr =
124252506Simp								256 * pccard_tuple_read_n(tuple, hostaddrsize,
124352506Simp								       idx);
124452506Simp							idx += hostaddrsize;
124552506Simp						} else {
124652506Simp							cfe->memspace[i].hostaddr = 0;
124752506Simp						}
124852506Simp					}
124952506Simp				}
1250142027Simp			} else
1251142027Simp				cfe->num_memspace = 0;
125252506Simp			if (misc) {
125352506Simp				if (tuple->length <= idx) {
125452506Simp					DPRINTF(("ran out of space before TCPE_MI\n"));
125552506Simp					goto abort_cfe;
125652506Simp				}
125752506Simp
125852506Simp				reg = pccard_tuple_read_1(tuple, idx);
125952506Simp				idx++;
1260104640Simp				cfe->flags &= ~(PCCARD_CFE_POWERDOWN
1261104640Simp				    | PCCARD_CFE_READONLY
1262104640Simp				    | PCCARD_CFE_AUDIO);
126352506Simp				if (reg & PCCARD_TPCE_MI_PWRDOWN)
1264104640Simp					cfe->flags |= PCCARD_CFE_POWERDOWN;
126552506Simp				if (reg & PCCARD_TPCE_MI_READONLY)
1266104640Simp					cfe->flags |= PCCARD_CFE_READONLY;
126752506Simp				if (reg & PCCARD_TPCE_MI_AUDIO)
1268104640Simp					cfe->flags |= PCCARD_CFE_AUDIO;
126952506Simp				cfe->maxtwins = reg & PCCARD_TPCE_MI_MAXTWINS;
127052506Simp
127152506Simp				while (reg & PCCARD_TPCE_MI_EXT) {
127252506Simp					reg = pccard_tuple_read_1(tuple, idx);
127352506Simp					idx++;
127452506Simp				}
127552506Simp			}
127652506Simp			/* skip all the subtuples */
127752506Simp		}
127852506Simp
127952506Simp	abort_cfe:
128052506Simp		DPRINTF(("CISTPL_CFTABLE_ENTRY\n"));
128152506Simp		break;
128252506Simp	default:
128352506Simp		DPRINTF(("unhandled CISTPL %x\n", tuple->code));
128452506Simp		break;
128552506Simp	}
128652506Simp
128752506Simp	return (0);
128852506Simp}
128982781Sshiba
129082781Sshibastatic int
1291147729Simpdecode_funce(const struct pccard_tuple *tuple, struct pccard_function *pf)
129282781Sshiba{
1293140542Simp	int i;
1294140542Simp	int len;
129582781Sshiba	int type = pccard_tuple_read_1(tuple, 0);
129682781Sshiba
129782781Sshiba	switch (pf->function) {
129882781Sshiba	case PCCARD_FUNCTION_DISK:
129982781Sshiba		if (type == PCCARD_TPLFE_TYPE_DISK_DEVICE_INTERFACE) {
130082781Sshiba			pf->pf_funce_disk_interface
130182781Sshiba				= pccard_tuple_read_1(tuple, 1);
130282781Sshiba		}
130382781Sshiba		break;
130482781Sshiba	case PCCARD_FUNCTION_NETWORK:
130582781Sshiba		if (type == PCCARD_TPLFE_TYPE_LAN_NID) {
1306140542Simp			len = pccard_tuple_read_1(tuple, 1);
130782781Sshiba			if (tuple->length < 2 + len || len > 8) {
130882781Sshiba				/* tuple length not enough or nid too long */
130982781Sshiba				break;
131082781Sshiba                        }
131182781Sshiba			for (i = 0; i < len; i++) {
131282781Sshiba				pf->pf_funce_lan_nid[i]
131382781Sshiba					= pccard_tuple_read_1(tuple, i + 2);
131482781Sshiba			}
131582781Sshiba			pf->pf_funce_lan_nidlen = len;
131682781Sshiba		}
131782781Sshiba		break;
131882781Sshiba	default:
131982781Sshiba		break;
132082781Sshiba	}
132182781Sshiba	return 0;
132282781Sshiba}
1323