1*29dd1b42Sjsg /* $OpenBSD: pcmcia_cis.c,v 1.22 2021/03/07 06:20:09 jsg Exp $ */
20720f62cSfgsch /* $NetBSD: pcmcia_cis.c,v 1.9 1998/08/22 23:41:48 msaitoh Exp $ */
30720f62cSfgsch
40720f62cSfgsch /*
50720f62cSfgsch * Copyright (c) 1997 Marc Horowitz. All rights reserved.
60720f62cSfgsch *
70720f62cSfgsch * Redistribution and use in source and binary forms, with or without
80720f62cSfgsch * modification, are permitted provided that the following conditions
90720f62cSfgsch * are met:
100720f62cSfgsch * 1. Redistributions of source code must retain the above copyright
110720f62cSfgsch * notice, this list of conditions and the following disclaimer.
120720f62cSfgsch * 2. Redistributions in binary form must reproduce the above copyright
130720f62cSfgsch * notice, this list of conditions and the following disclaimer in the
140720f62cSfgsch * documentation and/or other materials provided with the distribution.
150720f62cSfgsch * 3. All advertising materials mentioning features or use of this software
160720f62cSfgsch * must display the following acknowledgement:
170720f62cSfgsch * This product includes software developed by Marc Horowitz.
180720f62cSfgsch * 4. The name of the author may not be used to endorse or promote products
190720f62cSfgsch * derived from this software without specific prior written permission.
200720f62cSfgsch *
210720f62cSfgsch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
220720f62cSfgsch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
230720f62cSfgsch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
240720f62cSfgsch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
250720f62cSfgsch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
260720f62cSfgsch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
270720f62cSfgsch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
280720f62cSfgsch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290720f62cSfgsch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
300720f62cSfgsch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310720f62cSfgsch */
320720f62cSfgsch
330720f62cSfgsch #include <sys/param.h>
340720f62cSfgsch #include <sys/systm.h>
350720f62cSfgsch #include <sys/device.h>
360720f62cSfgsch #include <sys/malloc.h>
370720f62cSfgsch
380720f62cSfgsch #include <dev/pcmcia/pcmciareg.h>
390720f62cSfgsch #include <dev/pcmcia/pcmciachip.h>
400720f62cSfgsch #include <dev/pcmcia/pcmciavar.h>
410720f62cSfgsch
420720f62cSfgsch #ifdef PCMCIACISDEBUG
43709e0656Sfgsch #define DPRINTF(arg) printf arg
440720f62cSfgsch #else
450720f62cSfgsch #define DPRINTF(arg)
460720f62cSfgsch #endif
470720f62cSfgsch
480720f62cSfgsch #define PCMCIA_CIS_SIZE 1024
490720f62cSfgsch
500720f62cSfgsch struct cis_state {
510720f62cSfgsch int count;
520720f62cSfgsch int gotmfc;
530720f62cSfgsch struct pcmcia_config_entry temp_cfe;
540720f62cSfgsch struct pcmcia_config_entry *default_cfe;
550720f62cSfgsch struct pcmcia_card *card;
560720f62cSfgsch struct pcmcia_function *pf;
570720f62cSfgsch };
580720f62cSfgsch
59c4071fd1Smillert int pcmcia_parse_cis_tuple(struct pcmcia_tuple *, void *);
600720f62cSfgsch
61ba796609Smiod uint8_t
pcmcia_cis_read_1(struct pcmcia_tuple * tuple,bus_size_t idx)62ba796609Smiod pcmcia_cis_read_1(struct pcmcia_tuple *tuple, bus_size_t idx)
63ba796609Smiod {
64ba796609Smiod if (tuple->flags & PTF_INDIRECT) {
65ba796609Smiod bus_space_write_1(tuple->memt, tuple->memh,
66ba796609Smiod tuple->indirect_ptr + PCMCIA_INDR_CONTROL, PCMCIA_ICR_ATTR);
67ba796609Smiod idx <<= tuple->addrshift;
68ba796609Smiod bus_space_write_1(tuple->memt, tuple->memh,
69ba796609Smiod tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 0, idx >> 0);
70ba796609Smiod bus_space_write_1(tuple->memt, tuple->memh,
71ba796609Smiod tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 1, idx >> 8);
72ba796609Smiod bus_space_write_1(tuple->memt, tuple->memh,
73ba796609Smiod tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 2, idx >> 16);
74ba796609Smiod bus_space_write_1(tuple->memt, tuple->memh,
75ba796609Smiod tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 3, idx >> 24);
76ba796609Smiod return bus_space_read_1(tuple->memt, tuple->memh,
77ba796609Smiod tuple->indirect_ptr + PCMCIA_INDR_DATA);
78ba796609Smiod } else
79ba796609Smiod return bus_space_read_1(tuple->memt, tuple->memh,
80ba796609Smiod idx << tuple->addrshift);
81ba796609Smiod }
82ba796609Smiod
830720f62cSfgsch void
pcmcia_read_cis(struct pcmcia_softc * sc)84*29dd1b42Sjsg pcmcia_read_cis(struct pcmcia_softc *sc)
850720f62cSfgsch {
860720f62cSfgsch struct cis_state state;
870720f62cSfgsch
8805460420Sbrad memset(&state, 0, sizeof state);
890720f62cSfgsch
900720f62cSfgsch state.card = &sc->card;
910720f62cSfgsch
920720f62cSfgsch state.card->error = 0;
930720f62cSfgsch state.card->cis1_major = -1;
940720f62cSfgsch state.card->cis1_minor = -1;
950720f62cSfgsch state.card->cis1_info[0] = NULL;
960720f62cSfgsch state.card->cis1_info[1] = NULL;
970720f62cSfgsch state.card->cis1_info[2] = NULL;
980720f62cSfgsch state.card->cis1_info[3] = NULL;
990720f62cSfgsch state.card->manufacturer = PCMCIA_VENDOR_INVALID;
1000720f62cSfgsch state.card->product = PCMCIA_PRODUCT_INVALID;
1010720f62cSfgsch SIMPLEQ_INIT(&state.card->pf_head);
1020720f62cSfgsch
1030720f62cSfgsch state.pf = NULL;
1040720f62cSfgsch
1050720f62cSfgsch if (pcmcia_scan_cis((struct device *)sc, pcmcia_parse_cis_tuple,
1060720f62cSfgsch &state) == -1)
1070720f62cSfgsch state.card->error++;
1080720f62cSfgsch }
1090720f62cSfgsch
1100720f62cSfgsch int
pcmcia_scan_cis(struct device * dev,int (* fct)(struct pcmcia_tuple *,void *),void * arg)111*29dd1b42Sjsg pcmcia_scan_cis(struct device *dev, int (*fct)(struct pcmcia_tuple *, void *),
112*29dd1b42Sjsg void *arg)
1130720f62cSfgsch {
1140720f62cSfgsch struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
1150720f62cSfgsch pcmcia_chipset_tag_t pct;
1160720f62cSfgsch pcmcia_chipset_handle_t pch;
1170720f62cSfgsch int window;
1180720f62cSfgsch struct pcmcia_mem_handle pcmh;
1190720f62cSfgsch struct pcmcia_tuple tuple;
120ba796609Smiod int indirect_present;
1210720f62cSfgsch int longlink_present;
1220720f62cSfgsch int longlink_common;
1230720f62cSfgsch u_long longlink_addr;
1240720f62cSfgsch int mfc_count;
1250720f62cSfgsch int mfc_index;
1260720f62cSfgsch struct {
1270720f62cSfgsch int common;
1280720f62cSfgsch u_long addr;
1290720f62cSfgsch } mfc[256 / 5];
1300720f62cSfgsch int ret;
1310720f62cSfgsch
1320720f62cSfgsch ret = 0;
1330720f62cSfgsch
1340720f62cSfgsch pct = sc->pct;
1350720f62cSfgsch pch = sc->pch;
1360720f62cSfgsch
1370720f62cSfgsch /* allocate some memory */
1380720f62cSfgsch
1390720f62cSfgsch if (pcmcia_chip_mem_alloc(pct, pch, PCMCIA_CIS_SIZE, &pcmh)) {
1400720f62cSfgsch #ifdef DIAGNOSTIC
1410720f62cSfgsch printf("%s: can't alloc memory to read attributes\n",
1420720f62cSfgsch sc->dev.dv_xname);
1430720f62cSfgsch #endif
1440720f62cSfgsch return -1;
1450720f62cSfgsch }
1460720f62cSfgsch
1470720f62cSfgsch /* initialize state for the primary tuple chain */
1480720f62cSfgsch if (pcmcia_chip_mem_map(pct, pch, PCMCIA_MEM_ATTR, 0,
1490720f62cSfgsch PCMCIA_CIS_SIZE, &pcmh, &tuple.ptr, &window)) {
1500720f62cSfgsch pcmcia_chip_mem_free(pct, pch, &pcmh);
1510720f62cSfgsch #ifdef DIAGNOSTIC
1520720f62cSfgsch printf("%s: can't map memory to read attributes\n",
1530720f62cSfgsch sc->dev.dv_xname);
1540720f62cSfgsch #endif
1550720f62cSfgsch return -1;
1560720f62cSfgsch }
157c773fb73Sfgsch tuple.memt = pcmh.memt;
158c773fb73Sfgsch tuple.memh = pcmh.memh;
159c773fb73Sfgsch
1600720f62cSfgsch DPRINTF(("cis mem map %x\n", (unsigned int) tuple.memh));
1610720f62cSfgsch
162ba796609Smiod tuple.addrshift = 1;
163ba796609Smiod tuple.flags = 0;
1640720f62cSfgsch
165ba796609Smiod indirect_present = 0;
1660720f62cSfgsch longlink_present = 1;
1670720f62cSfgsch longlink_common = 1;
1680720f62cSfgsch longlink_addr = 0;
1690720f62cSfgsch
1700720f62cSfgsch mfc_count = 0;
1710720f62cSfgsch mfc_index = 0;
1720720f62cSfgsch
1730720f62cSfgsch DPRINTF(("%s: CIS tuple chain:\n", sc->dev.dv_xname));
1740720f62cSfgsch
1750720f62cSfgsch while (1) {
1760720f62cSfgsch while (1) {
17705460420Sbrad /*
17805460420Sbrad * Perform boundary check for insane cards.
17905460420Sbrad * If CIS is too long, simulate CIS end.
18005460420Sbrad * (This check may not be sufficient for
18105460420Sbrad * malicious cards.)
18205460420Sbrad */
183ba796609Smiod if ((tuple.ptr << tuple.addrshift) >=
184ba796609Smiod PCMCIA_CIS_SIZE - 1 - 32 /* ad hoc value */) {
18505460420Sbrad DPRINTF(("CISTPL_END (too long CIS)\n"));
186e41a3487Shenning tuple.code = PCMCIA_CISTPL_END;
18705460420Sbrad goto cis_end;
18805460420Sbrad }
18905460420Sbrad
19005460420Sbrad /* get the tuple code */
19105460420Sbrad
19205460420Sbrad tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr);
1930720f62cSfgsch
1940720f62cSfgsch /* two special-case tuples */
1950720f62cSfgsch
1960720f62cSfgsch if (tuple.code == PCMCIA_CISTPL_NULL) {
1970720f62cSfgsch DPRINTF(("CISTPL_NONE\n 00\n"));
1980720f62cSfgsch tuple.ptr++;
1990720f62cSfgsch continue;
2000720f62cSfgsch } else if (tuple.code == PCMCIA_CISTPL_END) {
2010720f62cSfgsch DPRINTF(("CISTPL_END\n ff\n"));
20205460420Sbrad cis_end:
2030720f62cSfgsch /* Call the function for the END tuple, since
2040720f62cSfgsch the CIS semantics depend on it */
2050720f62cSfgsch if ((*fct) (&tuple, arg)) {
2060720f62cSfgsch pcmcia_chip_mem_unmap(pct, pch,
2070720f62cSfgsch window);
2080720f62cSfgsch ret = 1;
2090720f62cSfgsch goto done;
2100720f62cSfgsch }
2110720f62cSfgsch tuple.ptr++;
2120720f62cSfgsch break;
2130720f62cSfgsch }
2140720f62cSfgsch /* now all the normal tuples */
2150720f62cSfgsch
2160720f62cSfgsch tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1);
2170720f62cSfgsch switch (tuple.code) {
218ba796609Smiod case PCMCIA_CISTPL_INDIRECT:
219ba796609Smiod indirect_present = 1;
220ba796609Smiod DPRINTF(("CISTPL_INDIRECT\n"));
221ba796609Smiod break;
2220720f62cSfgsch case PCMCIA_CISTPL_LONGLINK_A:
2230720f62cSfgsch case PCMCIA_CISTPL_LONGLINK_C:
2240720f62cSfgsch if (tuple.length < 4) {
2250720f62cSfgsch DPRINTF(("CISTPL_LONGLINK_%s too "
2260720f62cSfgsch "short %d\n",
2270720f62cSfgsch longlink_common ? "C" : "A",
2280720f62cSfgsch tuple.length));
2290720f62cSfgsch break;
2300720f62cSfgsch }
2310720f62cSfgsch longlink_present = 1;
2320720f62cSfgsch longlink_common = (tuple.code ==
2330720f62cSfgsch PCMCIA_CISTPL_LONGLINK_C) ? 1 : 0;
2340720f62cSfgsch longlink_addr = pcmcia_tuple_read_4(&tuple, 0);
2350720f62cSfgsch DPRINTF(("CISTPL_LONGLINK_%s %lx\n",
2360720f62cSfgsch longlink_common ? "C" : "A",
2370720f62cSfgsch longlink_addr));
2380720f62cSfgsch break;
2390720f62cSfgsch case PCMCIA_CISTPL_NO_LINK:
2400720f62cSfgsch longlink_present = 0;
2410720f62cSfgsch DPRINTF(("CISTPL_NO_LINK\n"));
2420720f62cSfgsch break;
2430720f62cSfgsch case PCMCIA_CISTPL_CHECKSUM:
2440720f62cSfgsch if (tuple.length < 5) {
2450720f62cSfgsch DPRINTF(("CISTPL_CHECKSUM too "
2460720f62cSfgsch "short %d\n", tuple.length));
2470720f62cSfgsch break;
2480720f62cSfgsch } {
2490720f62cSfgsch int16_t offset;
2500720f62cSfgsch u_long addr, length;
2510720f62cSfgsch u_int cksum, sum;
2520720f62cSfgsch int i;
2530720f62cSfgsch
2540720f62cSfgsch *((u_int16_t *) & offset) =
2550720f62cSfgsch pcmcia_tuple_read_2(&tuple, 0);
2560720f62cSfgsch length = pcmcia_tuple_read_2(&tuple, 2);
2570720f62cSfgsch cksum = pcmcia_tuple_read_1(&tuple, 4);
2580720f62cSfgsch
2590720f62cSfgsch addr = tuple.ptr + offset;
2600720f62cSfgsch
2610720f62cSfgsch DPRINTF(("CISTPL_CHECKSUM addr=%lx "
2620720f62cSfgsch "len=%lx cksum=%x",
2630720f62cSfgsch addr, length, cksum));
2640720f62cSfgsch
2650720f62cSfgsch /*
2660720f62cSfgsch * XXX do more work to deal with
2670720f62cSfgsch * distant regions
2680720f62cSfgsch */
2690720f62cSfgsch if ((addr >= PCMCIA_CIS_SIZE) ||
2700720f62cSfgsch ((addr + length) >=
2710720f62cSfgsch PCMCIA_CIS_SIZE)) {
2720720f62cSfgsch DPRINTF((" skipped, "
2730720f62cSfgsch "too distant\n"));
2740720f62cSfgsch break;
2750720f62cSfgsch }
2760720f62cSfgsch sum = 0;
2770720f62cSfgsch for (i = 0; i < length; i++)
278bdc49634Smiod sum += pcmcia_cis_read_1(&tuple,
279bdc49634Smiod addr + i);
2800720f62cSfgsch if (cksum != (sum & 0xff)) {
2810720f62cSfgsch DPRINTF((" failed sum=%x\n",
2820720f62cSfgsch sum));
2830720f62cSfgsch printf("%s: CIS checksum "
2840720f62cSfgsch "failed\n",
2850720f62cSfgsch sc->dev.dv_xname);
2860720f62cSfgsch #if 0
2870720f62cSfgsch /*
2880720f62cSfgsch * XXX Some working cards have
2890720f62cSfgsch * XXX bad checksums!!
2900720f62cSfgsch */
2910720f62cSfgsch ret = -1;
2920720f62cSfgsch #endif
2930720f62cSfgsch } else {
2940720f62cSfgsch DPRINTF((" ok\n"));
2950720f62cSfgsch }
2960720f62cSfgsch }
2970720f62cSfgsch break;
2980720f62cSfgsch case PCMCIA_CISTPL_LONGLINK_MFC:
2998e9dce62Sfgsch if (tuple.length < 6) {
3000720f62cSfgsch DPRINTF(("CISTPL_LONGLINK_MFC too "
3010720f62cSfgsch "short %d\n", tuple.length));
3020720f62cSfgsch break;
3030720f62cSfgsch }
3048e9dce62Sfgsch if (((tuple.length - 1) % 5) != 0) {
3058e9dce62Sfgsch DPRINTF(("CISTPL_LONGLINK_MFC bogus "
3068e9dce62Sfgsch "length %d\n", tuple.length));
3078e9dce62Sfgsch break;
3088e9dce62Sfgsch }
3090720f62cSfgsch {
31005460420Sbrad int i, tmp_count;
3110720f62cSfgsch
31205460420Sbrad /*
31305460420Sbrad * put count into tmp var so that
31405460420Sbrad * if we have to bail (because it's
31505460420Sbrad * a bogus count) it won't be
31605460420Sbrad * remembered for later use.
31705460420Sbrad */
31805460420Sbrad tmp_count =
3190720f62cSfgsch pcmcia_tuple_read_1(&tuple, 0);
3200720f62cSfgsch DPRINTF(("CISTPL_LONGLINK_MFC %d",
32105460420Sbrad tmp_count));
32205460420Sbrad
32305460420Sbrad /*
32405460420Sbrad * make _sure_ it's the right size;
32505460420Sbrad * if too short, it may be a weird
32605460420Sbrad * (unknown/undefined) format
32705460420Sbrad */
32805460420Sbrad if (tuple.length != (tmp_count*5 + 1)) {
32905460420Sbrad DPRINTF((" bogus length %d\n",
33005460420Sbrad tuple.length));
33105460420Sbrad break;
33205460420Sbrad }
33305460420Sbrad
33405460420Sbrad #ifdef PCMCIACISDEBUG /* maybe enable all the time? */
33505460420Sbrad /*
33605460420Sbrad * sanity check for a programming
33705460420Sbrad * error which is difficult to find
33805460420Sbrad * when debugging.
33905460420Sbrad */
34005460420Sbrad if (tmp_count >
34105460420Sbrad howmany(sizeof mfc, sizeof mfc[0]))
34205460420Sbrad panic("CISTPL_LONGLINK_MFC mfc "
34305460420Sbrad "count would blow stack");
34405460420Sbrad #endif
34505460420Sbrad
34605460420Sbrad mfc_count = tmp_count;
3470720f62cSfgsch for (i = 0; i < mfc_count; i++) {
3480720f62cSfgsch mfc[i].common =
3490720f62cSfgsch (pcmcia_tuple_read_1(&tuple,
3500720f62cSfgsch 1 + 5 * i) ==
3510720f62cSfgsch PCMCIA_MFC_MEM_COMMON) ?
3520720f62cSfgsch 1 : 0;
3530720f62cSfgsch mfc[i].addr =
3540720f62cSfgsch pcmcia_tuple_read_4(&tuple,
3550720f62cSfgsch 1 + 5 * i + 1);
3560720f62cSfgsch DPRINTF((" %s:%lx",
3570720f62cSfgsch mfc[i].common ? "common" :
3580720f62cSfgsch "attr", mfc[i].addr));
3590720f62cSfgsch }
3600720f62cSfgsch DPRINTF(("\n"));
3610720f62cSfgsch }
3620720f62cSfgsch /*
3630720f62cSfgsch * for LONGLINK_MFC, fall through to the
3640720f62cSfgsch * function. This tuple has structural and
3650720f62cSfgsch * semantic content.
3660720f62cSfgsch */
3670720f62cSfgsch default:
3680720f62cSfgsch {
3690720f62cSfgsch if ((*fct) (&tuple, arg)) {
3700720f62cSfgsch pcmcia_chip_mem_unmap(pct,
3710720f62cSfgsch pch, window);
3720720f62cSfgsch ret = 1;
3730720f62cSfgsch goto done;
3740720f62cSfgsch }
3750720f62cSfgsch }
3760720f62cSfgsch break;
3770720f62cSfgsch } /* switch */
3780720f62cSfgsch #ifdef PCMCIACISDEBUG
3790720f62cSfgsch /* print the tuple */
3800720f62cSfgsch {
3810720f62cSfgsch int i;
3820720f62cSfgsch
3830720f62cSfgsch DPRINTF((" %02x %02x", tuple.code,
3840720f62cSfgsch tuple.length));
3850720f62cSfgsch
3860720f62cSfgsch for (i = 0; i < tuple.length; i++) {
3870720f62cSfgsch DPRINTF((" %02x",
3880720f62cSfgsch pcmcia_tuple_read_1(&tuple, i)));
3890720f62cSfgsch if ((i % 16) == 13)
3900720f62cSfgsch DPRINTF(("\n"));
3910720f62cSfgsch }
3920720f62cSfgsch if ((i % 16) != 14)
3930720f62cSfgsch DPRINTF(("\n"));
3940720f62cSfgsch }
3950720f62cSfgsch #endif
3960720f62cSfgsch /* skip to the next tuple */
3970720f62cSfgsch tuple.ptr += 2 + tuple.length;
3980720f62cSfgsch }
399709e0656Sfgsch
4000720f62cSfgsch /*
4010720f62cSfgsch * the chain is done. Clean up and move onto the next one,
4020720f62cSfgsch * if any. The loop is here in the case that there is an MFC
4030720f62cSfgsch * card with no longlink (which defaults to existing, == 0).
4040720f62cSfgsch * In general, this means that if one pointer fails, it will
4050720f62cSfgsch * try the next one, instead of just bailing.
4060720f62cSfgsch */
4070720f62cSfgsch
4080720f62cSfgsch while (1) {
4090720f62cSfgsch pcmcia_chip_mem_unmap(pct, pch, window);
4100720f62cSfgsch
411ba796609Smiod if (indirect_present) {
412ba796609Smiod /*
413ba796609Smiod * Indirect CIS data needs to be obtained
414ba796609Smiod * from specific registers accessible at
415ba796609Smiod * a fixed location in the common window,
416ba796609Smiod * but otherwise is similar to longlink
417ba796609Smiod * in attribute memory.
418ba796609Smiod */
419ba796609Smiod
420ba796609Smiod pcmcia_chip_mem_map(pct, pch, PCMCIA_MEM_COMMON,
421ba796609Smiod 0, PCMCIA_INDR_SIZE,
422ba796609Smiod &pcmh, &tuple.indirect_ptr, &window);
423ba796609Smiod
424ba796609Smiod DPRINTF(("cis mem map %x ind %x\n",
425ba796609Smiod (unsigned int) tuple.memh,
426ba796609Smiod (unsigned int) tuple.indirect_ptr));
427ba796609Smiod
428ba796609Smiod tuple.addrshift = 1;
429ba796609Smiod tuple.flags |= PTF_INDIRECT;
430ba796609Smiod tuple.ptr = 0;
431ba796609Smiod longlink_present = 0;
432ba796609Smiod indirect_present = 0;
433ba796609Smiod } else if (longlink_present) {
4340720f62cSfgsch /*
4350720f62cSfgsch * if the longlink is to attribute memory,
4360720f62cSfgsch * then it is unindexed. That is, if the
4370720f62cSfgsch * link value is 0x100, then the actual
4380720f62cSfgsch * memory address is 0x200. This means that
4390720f62cSfgsch * we need to multiply by 2 before calling
4400720f62cSfgsch * mem_map, and then divide the resulting ptr
4410720f62cSfgsch * by 2 after.
4420720f62cSfgsch */
4430720f62cSfgsch
4440720f62cSfgsch if (!longlink_common)
4450720f62cSfgsch longlink_addr *= 2;
4460720f62cSfgsch
4470720f62cSfgsch pcmcia_chip_mem_map(pct, pch, longlink_common ?
4480720f62cSfgsch PCMCIA_MEM_COMMON : PCMCIA_MEM_ATTR,
4490720f62cSfgsch longlink_addr, PCMCIA_CIS_SIZE,
4500720f62cSfgsch &pcmh, &tuple.ptr, &window);
4510720f62cSfgsch
4520720f62cSfgsch if (!longlink_common)
4530720f62cSfgsch tuple.ptr /= 2;
4540720f62cSfgsch
4550720f62cSfgsch DPRINTF(("cis mem map %x\n",
4560720f62cSfgsch (unsigned int) tuple.memh));
4570720f62cSfgsch
458ba796609Smiod tuple.addrshift = longlink_common ? 0 : 1;
4590720f62cSfgsch longlink_present = 0;
4600720f62cSfgsch longlink_common = 1;
4610720f62cSfgsch longlink_addr = 0;
4620720f62cSfgsch } else if (mfc_count && (mfc_index < mfc_count)) {
4630720f62cSfgsch if (!mfc[mfc_index].common)
4640720f62cSfgsch mfc[mfc_index].addr *= 2;
4650720f62cSfgsch
4660720f62cSfgsch pcmcia_chip_mem_map(pct, pch,
4670720f62cSfgsch mfc[mfc_index].common ?
4680720f62cSfgsch PCMCIA_MEM_COMMON : PCMCIA_MEM_ATTR,
4690720f62cSfgsch mfc[mfc_index].addr, PCMCIA_CIS_SIZE,
4700720f62cSfgsch &pcmh, &tuple.ptr, &window);
4710720f62cSfgsch
4720720f62cSfgsch if (!mfc[mfc_index].common)
4730720f62cSfgsch tuple.ptr /= 2;
4740720f62cSfgsch
4750720f62cSfgsch DPRINTF(("cis mem map %x\n",
4760720f62cSfgsch (unsigned int) tuple.memh));
4770720f62cSfgsch
4780720f62cSfgsch /* set parse state, and point at the next one */
4790720f62cSfgsch
480ba796609Smiod tuple.addrshift = mfc[mfc_index].common ? 0 : 1;
4810720f62cSfgsch
4820720f62cSfgsch mfc_index++;
4830720f62cSfgsch } else {
4840720f62cSfgsch goto done;
4850720f62cSfgsch }
4860720f62cSfgsch
4870720f62cSfgsch /* make sure that the link is valid */
4880720f62cSfgsch tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr);
4890720f62cSfgsch if (tuple.code != PCMCIA_CISTPL_LINKTARGET) {
4900720f62cSfgsch DPRINTF(("CISTPL_LINKTARGET expected, "
4910720f62cSfgsch "code %02x observed\n", tuple.code));
4920720f62cSfgsch continue;
4930720f62cSfgsch }
4940720f62cSfgsch tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1);
4950720f62cSfgsch if (tuple.length < 3) {
4960720f62cSfgsch DPRINTF(("CISTPL_LINKTARGET too short %d\n",
4970720f62cSfgsch tuple.length));
4980720f62cSfgsch continue;
4990720f62cSfgsch }
5000720f62cSfgsch if ((pcmcia_tuple_read_1(&tuple, 0) != 'C') ||
5010720f62cSfgsch (pcmcia_tuple_read_1(&tuple, 1) != 'I') ||
5020720f62cSfgsch (pcmcia_tuple_read_1(&tuple, 2) != 'S')) {
5030720f62cSfgsch DPRINTF(("CISTPL_LINKTARGET magic "
5040720f62cSfgsch "%02x%02x%02x incorrect\n",
5050720f62cSfgsch pcmcia_tuple_read_1(&tuple, 0),
5060720f62cSfgsch pcmcia_tuple_read_1(&tuple, 1),
5070720f62cSfgsch pcmcia_tuple_read_1(&tuple, 2)));
5080720f62cSfgsch continue;
5090720f62cSfgsch }
5100720f62cSfgsch tuple.ptr += 2 + tuple.length;
5110720f62cSfgsch
5120720f62cSfgsch break;
5130720f62cSfgsch }
5140720f62cSfgsch }
5150720f62cSfgsch
5160720f62cSfgsch pcmcia_chip_mem_unmap(pct, pch, window);
5170720f62cSfgsch
5180720f62cSfgsch done:
5190720f62cSfgsch /* Last, free the allocated memory block */
5200720f62cSfgsch pcmcia_chip_mem_free(pct, pch, &pcmh);
5210720f62cSfgsch
5220720f62cSfgsch return (ret);
5230720f62cSfgsch }
5240720f62cSfgsch
5250720f62cSfgsch /* XXX this is incredibly verbose. Not sure what trt is */
5260720f62cSfgsch
5270720f62cSfgsch void
pcmcia_print_cis(struct pcmcia_softc * sc)528*29dd1b42Sjsg pcmcia_print_cis(struct pcmcia_softc *sc)
5290720f62cSfgsch {
5300720f62cSfgsch struct pcmcia_card *card = &sc->card;
5310720f62cSfgsch struct pcmcia_function *pf;
5320720f62cSfgsch struct pcmcia_config_entry *cfe;
5330720f62cSfgsch int i;
5340720f62cSfgsch
5350720f62cSfgsch printf("%s: CIS version ", sc->dev.dv_xname);
5360720f62cSfgsch if (card->cis1_major == 4) {
5370720f62cSfgsch if (card->cis1_minor == 0)
5380720f62cSfgsch printf("PCMCIA 1.0\n");
5390720f62cSfgsch else if (card->cis1_minor == 1)
5400720f62cSfgsch printf("PCMCIA 2.0 or 2.1\n");
5410720f62cSfgsch } else if (card->cis1_major >= 5)
5428e9dce62Sfgsch printf("PC Card Standard %d.%d\n", card->cis1_major,
5438e9dce62Sfgsch card->cis1_minor);
5440720f62cSfgsch else
5450720f62cSfgsch printf("unknown (major=%d, minor=%d)\n",
5460720f62cSfgsch card->cis1_major, card->cis1_minor);
5470720f62cSfgsch
5480720f62cSfgsch printf("%s: CIS info: ", sc->dev.dv_xname);
5490720f62cSfgsch for (i = 0; i < 4; i++) {
5500720f62cSfgsch if (card->cis1_info[i] == NULL)
5510720f62cSfgsch break;
5520720f62cSfgsch if (i)
5530720f62cSfgsch printf(", ");
5540720f62cSfgsch printf("%s", card->cis1_info[i]);
5550720f62cSfgsch }
5560720f62cSfgsch printf("\n");
5570720f62cSfgsch
5580720f62cSfgsch printf("%s: Manufacturer code 0x%x, product 0x%x\n",
5590720f62cSfgsch sc->dev.dv_xname, card->manufacturer, card->product);
5600720f62cSfgsch
56110412f8dSjason SIMPLEQ_FOREACH(pf, &card->pf_head, pf_list) {
5620720f62cSfgsch printf("%s: function %d: ", sc->dev.dv_xname, pf->number);
5630720f62cSfgsch
5640720f62cSfgsch switch (pf->function) {
5650720f62cSfgsch case PCMCIA_FUNCTION_UNSPEC:
5660720f62cSfgsch printf("unspecified");
5670720f62cSfgsch break;
5680720f62cSfgsch case PCMCIA_FUNCTION_MULTIFUNCTION:
5690720f62cSfgsch printf("multi-function");
5700720f62cSfgsch break;
5710720f62cSfgsch case PCMCIA_FUNCTION_MEMORY:
5720720f62cSfgsch printf("memory");
5730720f62cSfgsch break;
5740720f62cSfgsch case PCMCIA_FUNCTION_SERIAL:
5750720f62cSfgsch printf("serial port");
5760720f62cSfgsch break;
5770720f62cSfgsch case PCMCIA_FUNCTION_PARALLEL:
5780720f62cSfgsch printf("parallel port");
5790720f62cSfgsch break;
5800720f62cSfgsch case PCMCIA_FUNCTION_DISK:
5810720f62cSfgsch printf("fixed disk");
5820720f62cSfgsch break;
5830720f62cSfgsch case PCMCIA_FUNCTION_VIDEO:
5840720f62cSfgsch printf("video adapter");
5850720f62cSfgsch break;
5860720f62cSfgsch case PCMCIA_FUNCTION_NETWORK:
5870720f62cSfgsch printf("network adapter");
5880720f62cSfgsch break;
5890720f62cSfgsch case PCMCIA_FUNCTION_AIMS:
5900720f62cSfgsch printf("auto incrementing mass storage");
5910720f62cSfgsch break;
5920720f62cSfgsch case PCMCIA_FUNCTION_SCSI:
5930720f62cSfgsch printf("SCSI bridge");
5940720f62cSfgsch break;
5950720f62cSfgsch case PCMCIA_FUNCTION_SECURITY:
5960720f62cSfgsch printf("Security services");
5970720f62cSfgsch break;
5980720f62cSfgsch case PCMCIA_FUNCTION_INSTRUMENT:
5990720f62cSfgsch printf("Instrument");
6000720f62cSfgsch break;
6018e9dce62Sfgsch case PCMCIA_FUNCTION_IOBUS:
6028e9dce62Sfgsch printf("Serial I/O Bus Adapter");
6038e9dce62Sfgsch break;
6040720f62cSfgsch default:
6050720f62cSfgsch printf("unknown (%d)", pf->function);
6060720f62cSfgsch break;
6070720f62cSfgsch }
6080720f62cSfgsch
6090720f62cSfgsch printf(", ccr addr %lx mask %lx\n", pf->ccr_base, pf->ccr_mask);
6100720f62cSfgsch
61110412f8dSjason SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
6120720f62cSfgsch printf("%s: function %d, config table entry %d: ",
6130720f62cSfgsch sc->dev.dv_xname, pf->number, cfe->number);
6140720f62cSfgsch
6150720f62cSfgsch switch (cfe->iftype) {
6160720f62cSfgsch case PCMCIA_IFTYPE_MEMORY:
6170720f62cSfgsch printf("memory card");
6180720f62cSfgsch break;
6190720f62cSfgsch case PCMCIA_IFTYPE_IO:
6200720f62cSfgsch printf("I/O card");
6210720f62cSfgsch break;
6220720f62cSfgsch default:
6230720f62cSfgsch printf("card type unknown");
6240720f62cSfgsch break;
6250720f62cSfgsch }
6260720f62cSfgsch
6270720f62cSfgsch printf("; irq mask %x", cfe->irqmask);
6280720f62cSfgsch
6290720f62cSfgsch if (cfe->num_iospace) {
6300720f62cSfgsch printf("; iomask %lx, iospace", cfe->iomask);
6310720f62cSfgsch
6320720f62cSfgsch for (i = 0; i < cfe->num_iospace; i++)
6330720f62cSfgsch printf(" %lx%s%lx",
6340720f62cSfgsch cfe->iospace[i].start,
6350720f62cSfgsch cfe->iospace[i].length ? "-" : "",
6360720f62cSfgsch cfe->iospace[i].start +
6370720f62cSfgsch cfe->iospace[i].length - 1);
6380720f62cSfgsch }
6390720f62cSfgsch if (cfe->num_memspace) {
6400720f62cSfgsch printf("; memspace");
6410720f62cSfgsch
6420720f62cSfgsch for (i = 0; i < cfe->num_memspace; i++)
6430720f62cSfgsch printf(" %lx%s%lx%s%lx",
6440720f62cSfgsch cfe->memspace[i].cardaddr,
6450720f62cSfgsch cfe->memspace[i].length ? "-" : "",
6460720f62cSfgsch cfe->memspace[i].cardaddr +
6470720f62cSfgsch cfe->memspace[i].length - 1,
6480720f62cSfgsch cfe->memspace[i].hostaddr ?
6490720f62cSfgsch "@" : "",
6500720f62cSfgsch cfe->memspace[i].hostaddr);
6510720f62cSfgsch }
6520720f62cSfgsch if (cfe->maxtwins)
6530720f62cSfgsch printf("; maxtwins %d", cfe->maxtwins);
6540720f62cSfgsch
6550720f62cSfgsch printf(";");
6560720f62cSfgsch
6570720f62cSfgsch if (cfe->flags & PCMCIA_CFE_MWAIT_REQUIRED)
6580720f62cSfgsch printf(" mwait_required");
6590720f62cSfgsch if (cfe->flags & PCMCIA_CFE_RDYBSY_ACTIVE)
6600720f62cSfgsch printf(" rdybsy_active");
6610720f62cSfgsch if (cfe->flags & PCMCIA_CFE_WP_ACTIVE)
6620720f62cSfgsch printf(" wp_active");
6630720f62cSfgsch if (cfe->flags & PCMCIA_CFE_BVD_ACTIVE)
6640720f62cSfgsch printf(" bvd_active");
6650720f62cSfgsch if (cfe->flags & PCMCIA_CFE_IO8)
6660720f62cSfgsch printf(" io8");
6670720f62cSfgsch if (cfe->flags & PCMCIA_CFE_IO16)
6680720f62cSfgsch printf(" io16");
6690720f62cSfgsch if (cfe->flags & PCMCIA_CFE_IRQSHARE)
6700720f62cSfgsch printf(" irqshare");
6710720f62cSfgsch if (cfe->flags & PCMCIA_CFE_IRQPULSE)
6720720f62cSfgsch printf(" irqpulse");
6730720f62cSfgsch if (cfe->flags & PCMCIA_CFE_IRQLEVEL)
6740720f62cSfgsch printf(" irqlevel");
6750720f62cSfgsch if (cfe->flags & PCMCIA_CFE_POWERDOWN)
6760720f62cSfgsch printf(" powerdown");
6770720f62cSfgsch if (cfe->flags & PCMCIA_CFE_READONLY)
6780720f62cSfgsch printf(" readonly");
6790720f62cSfgsch if (cfe->flags & PCMCIA_CFE_AUDIO)
6800720f62cSfgsch printf(" audio");
6810720f62cSfgsch
6820720f62cSfgsch printf("\n");
6830720f62cSfgsch }
6840720f62cSfgsch }
6850720f62cSfgsch
6860720f62cSfgsch if (card->error)
6870720f62cSfgsch printf("%s: %d errors found while parsing CIS\n",
6880720f62cSfgsch sc->dev.dv_xname, card->error);
6890720f62cSfgsch }
6900720f62cSfgsch
6910720f62cSfgsch int
pcmcia_parse_cis_tuple(struct pcmcia_tuple * tuple,void * arg)692*29dd1b42Sjsg pcmcia_parse_cis_tuple(struct pcmcia_tuple *tuple, void *arg)
6930720f62cSfgsch {
6940720f62cSfgsch /* most of these are educated guesses */
6950720f62cSfgsch static struct pcmcia_config_entry init_cfe = {
6960720f62cSfgsch -1, PCMCIA_CFE_RDYBSY_ACTIVE | PCMCIA_CFE_WP_ACTIVE |
6970720f62cSfgsch PCMCIA_CFE_BVD_ACTIVE, PCMCIA_IFTYPE_MEMORY,
6980720f62cSfgsch };
6990720f62cSfgsch
7000720f62cSfgsch struct cis_state *state = arg;
7010720f62cSfgsch
7020720f62cSfgsch switch (tuple->code) {
7030720f62cSfgsch case PCMCIA_CISTPL_END:
704bd4ab2b6Sniklas /*
705bd4ab2b6Sniklas * If we've seen a LONGLINK_MFC, and this is the first
7060720f62cSfgsch * END after it, reset the function list.
7070720f62cSfgsch *
7080720f62cSfgsch * XXX This might also be the right place to start a
7090720f62cSfgsch * new function, but that assumes that a function
7100720f62cSfgsch * definition never crosses any longlink, and I'm not
7110720f62cSfgsch * sure about that. This is probably safe for MFC
7120720f62cSfgsch * cards, but what we have now isn't broken, so I'd
7130720f62cSfgsch * rather not change it.
7140720f62cSfgsch */
7150720f62cSfgsch if (state->gotmfc == 1) {
7160720f62cSfgsch struct pcmcia_function *pf, *pfnext;
7170720f62cSfgsch
71810412f8dSjason for (pf = SIMPLEQ_FIRST(&state->card->pf_head);
71910412f8dSjason pf != NULL; pf = pfnext) {
72010412f8dSjason pfnext = SIMPLEQ_NEXT(pf, pf_list);
721df2ac69fStedu free(pf, M_DEVBUF, 0);
7220720f62cSfgsch }
7230720f62cSfgsch
7240720f62cSfgsch SIMPLEQ_INIT(&state->card->pf_head);
7250720f62cSfgsch
7260720f62cSfgsch state->count = 0;
7270720f62cSfgsch state->gotmfc = 2;
7280720f62cSfgsch state->pf = NULL;
7290720f62cSfgsch }
7300720f62cSfgsch break;
731bd4ab2b6Sniklas
7320720f62cSfgsch case PCMCIA_CISTPL_LONGLINK_MFC:
7330720f62cSfgsch /*
734bd4ab2b6Sniklas * This tuple's structure was dealt with in scan_cis. here,
7350720f62cSfgsch * record the fact that the MFC tuple was seen, so that
7360720f62cSfgsch * functions declared before the MFC link can be cleaned
7370720f62cSfgsch * up.
7380720f62cSfgsch */
7390720f62cSfgsch state->gotmfc = 1;
7400720f62cSfgsch break;
741bd4ab2b6Sniklas
7420720f62cSfgsch #ifdef PCMCIACISDEBUG
7430720f62cSfgsch case PCMCIA_CISTPL_DEVICE:
7440720f62cSfgsch case PCMCIA_CISTPL_DEVICE_A:
7450720f62cSfgsch {
7460720f62cSfgsch u_int reg, dtype, dspeed;
7470720f62cSfgsch
7480720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, 0);
7490720f62cSfgsch dtype = reg & PCMCIA_DTYPE_MASK;
7500720f62cSfgsch dspeed = reg & PCMCIA_DSPEED_MASK;
7510720f62cSfgsch
7520720f62cSfgsch DPRINTF(("CISTPL_DEVICE%s type=",
7530720f62cSfgsch (tuple->code == PCMCIA_CISTPL_DEVICE) ? "" : "_A"));
7540720f62cSfgsch switch (dtype) {
7550720f62cSfgsch case PCMCIA_DTYPE_NULL:
7560720f62cSfgsch DPRINTF(("null"));
7570720f62cSfgsch break;
7580720f62cSfgsch case PCMCIA_DTYPE_ROM:
7590720f62cSfgsch DPRINTF(("rom"));
7600720f62cSfgsch break;
7610720f62cSfgsch case PCMCIA_DTYPE_OTPROM:
7620720f62cSfgsch DPRINTF(("otprom"));
7630720f62cSfgsch break;
7640720f62cSfgsch case PCMCIA_DTYPE_EPROM:
7650720f62cSfgsch DPRINTF(("eprom"));
7660720f62cSfgsch break;
7670720f62cSfgsch case PCMCIA_DTYPE_EEPROM:
7680720f62cSfgsch DPRINTF(("eeprom"));
7690720f62cSfgsch break;
7700720f62cSfgsch case PCMCIA_DTYPE_FLASH:
7710720f62cSfgsch DPRINTF(("flash"));
7720720f62cSfgsch break;
7730720f62cSfgsch case PCMCIA_DTYPE_SRAM:
7740720f62cSfgsch DPRINTF(("sram"));
7750720f62cSfgsch break;
7760720f62cSfgsch case PCMCIA_DTYPE_DRAM:
7770720f62cSfgsch DPRINTF(("dram"));
7780720f62cSfgsch break;
7790720f62cSfgsch case PCMCIA_DTYPE_FUNCSPEC:
7800720f62cSfgsch DPRINTF(("funcspec"));
7810720f62cSfgsch break;
7820720f62cSfgsch case PCMCIA_DTYPE_EXTEND:
7830720f62cSfgsch DPRINTF(("extend"));
7840720f62cSfgsch break;
7850720f62cSfgsch default:
7860720f62cSfgsch DPRINTF(("reserved"));
7870720f62cSfgsch break;
7880720f62cSfgsch }
7890720f62cSfgsch DPRINTF((" speed="));
7900720f62cSfgsch switch (dspeed) {
7910720f62cSfgsch case PCMCIA_DSPEED_NULL:
7920720f62cSfgsch DPRINTF(("null"));
7930720f62cSfgsch break;
7940720f62cSfgsch case PCMCIA_DSPEED_250NS:
7950720f62cSfgsch DPRINTF(("250ns"));
7960720f62cSfgsch break;
7970720f62cSfgsch case PCMCIA_DSPEED_200NS:
7980720f62cSfgsch DPRINTF(("200ns"));
7990720f62cSfgsch break;
8000720f62cSfgsch case PCMCIA_DSPEED_150NS:
8010720f62cSfgsch DPRINTF(("150ns"));
8020720f62cSfgsch break;
8030720f62cSfgsch case PCMCIA_DSPEED_100NS:
8040720f62cSfgsch DPRINTF(("100ns"));
8050720f62cSfgsch break;
8060720f62cSfgsch case PCMCIA_DSPEED_EXT:
8070720f62cSfgsch DPRINTF(("ext"));
8080720f62cSfgsch break;
8090720f62cSfgsch default:
8100720f62cSfgsch DPRINTF(("reserved"));
8110720f62cSfgsch break;
8120720f62cSfgsch }
8130720f62cSfgsch }
8140720f62cSfgsch DPRINTF(("\n"));
8150720f62cSfgsch break;
8160720f62cSfgsch #endif
817bd4ab2b6Sniklas
8180720f62cSfgsch case PCMCIA_CISTPL_VERS_1:
8190720f62cSfgsch if (tuple->length < 6) {
8200720f62cSfgsch DPRINTF(("CISTPL_VERS_1 too short %d\n",
8210720f62cSfgsch tuple->length));
8220720f62cSfgsch break;
8230720f62cSfgsch } {
8240720f62cSfgsch int start, i, ch, count;
8250720f62cSfgsch
8260720f62cSfgsch state->card->cis1_major = pcmcia_tuple_read_1(tuple, 0);
8270720f62cSfgsch state->card->cis1_minor = pcmcia_tuple_read_1(tuple, 1);
8280720f62cSfgsch
8290720f62cSfgsch for (count = 0, start = 0, i = 0;
8300720f62cSfgsch (count < 4) && ((i + 4) < 256); i++) {
8310720f62cSfgsch ch = pcmcia_tuple_read_1(tuple, 2 + i);
8320720f62cSfgsch if (ch == 0xff)
8330720f62cSfgsch break;
8340720f62cSfgsch state->card->cis1_info_buf[i] = ch;
8350720f62cSfgsch if (ch == 0) {
8360720f62cSfgsch state->card->cis1_info[count] =
8370720f62cSfgsch state->card->cis1_info_buf + start;
8380720f62cSfgsch start = i + 1;
8390720f62cSfgsch count++;
8400720f62cSfgsch }
8410720f62cSfgsch }
8420720f62cSfgsch DPRINTF(("CISTPL_VERS_1\n"));
8430720f62cSfgsch }
8440720f62cSfgsch break;
845bd4ab2b6Sniklas
8460720f62cSfgsch case PCMCIA_CISTPL_MANFID:
8470720f62cSfgsch if (tuple->length < 4) {
8480720f62cSfgsch DPRINTF(("CISTPL_MANFID too short %d\n",
8490720f62cSfgsch tuple->length));
8500720f62cSfgsch break;
8510720f62cSfgsch }
8520720f62cSfgsch state->card->manufacturer = pcmcia_tuple_read_2(tuple, 0);
8530720f62cSfgsch state->card->product = pcmcia_tuple_read_2(tuple, 2);
8540720f62cSfgsch DPRINTF(("CISTPL_MANFID\n"));
8550720f62cSfgsch break;
856bd4ab2b6Sniklas
8570720f62cSfgsch case PCMCIA_CISTPL_FUNCID:
8588e9dce62Sfgsch if (tuple->length < 2) {
8590720f62cSfgsch DPRINTF(("CISTPL_FUNCID too short %d\n",
8600720f62cSfgsch tuple->length));
8610720f62cSfgsch break;
8620720f62cSfgsch }
863bd4ab2b6Sniklas
864bd4ab2b6Sniklas /*
865bd4ab2b6Sniklas * As far as I understand this, manufacturers do multifunction
866bd4ab2b6Sniklas * cards in various ways. Sadly enough I do not have the
867bd4ab2b6Sniklas * PC-Card standard (donate!) so I can only guess what can
868bd4ab2b6Sniklas * be done.
869bd4ab2b6Sniklas * The original code implies FUNCID nodes are above CONFIG
870bd4ab2b6Sniklas * nodes in the CIS tree, however Xircom does it the other
871bd4ab2b6Sniklas * way round, which of course makes things a bit hard.
872bd4ab2b6Sniklas * --niklas@openbsd.org
873bd4ab2b6Sniklas */
874bd4ab2b6Sniklas if (state->pf) {
875bd4ab2b6Sniklas if (state->pf->function == PCMCIA_FUNCTION_UNSPEC) {
876bd4ab2b6Sniklas /*
877bd4ab2b6Sniklas * This looks like a opportunistic function
878bd4ab2b6Sniklas * created by a CONFIG tuple. Just keep it.
879bd4ab2b6Sniklas */
880bd4ab2b6Sniklas } else {
881bd4ab2b6Sniklas /*
882bd4ab2b6Sniklas * A function is being defined, end it.
883bd4ab2b6Sniklas */
884bd4ab2b6Sniklas state->pf = NULL;
885bd4ab2b6Sniklas }
886bd4ab2b6Sniklas }
887bd4ab2b6Sniklas if (state->pf == NULL) {
8880720f62cSfgsch state->pf = malloc(sizeof(*state->pf), M_DEVBUF,
889109be09cSfgsch M_NOWAIT | M_ZERO);
8908584d5e3Snordin if (state->pf == NULL)
8918584d5e3Snordin panic("pcmcia_parse_cis_tuple");
8920720f62cSfgsch state->pf->number = state->count++;
8930720f62cSfgsch state->pf->last_config_index = -1;
8940720f62cSfgsch SIMPLEQ_INIT(&state->pf->cfe_head);
8950720f62cSfgsch
8960720f62cSfgsch SIMPLEQ_INSERT_TAIL(&state->card->pf_head, state->pf,
8970720f62cSfgsch pf_list);
8980720f62cSfgsch }
8990720f62cSfgsch state->pf->function = pcmcia_tuple_read_1(tuple, 0);
9000720f62cSfgsch
9010720f62cSfgsch DPRINTF(("CISTPL_FUNCID\n"));
9020720f62cSfgsch break;
903bd4ab2b6Sniklas
9040720f62cSfgsch case PCMCIA_CISTPL_CONFIG:
9057bbf5fc9Sfgsch if (tuple->length < 3) {
9060720f62cSfgsch DPRINTF(("CISTPL_CONFIG too short %d\n",
9070720f62cSfgsch tuple->length));
9080720f62cSfgsch break;
9090720f62cSfgsch } {
9100720f62cSfgsch u_int reg, rasz, rmsz, rfsz;
9110720f62cSfgsch int i;
9120720f62cSfgsch
9130720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, 0);
9140720f62cSfgsch rasz = 1 + ((reg & PCMCIA_TPCC_RASZ_MASK) >>
9150720f62cSfgsch PCMCIA_TPCC_RASZ_SHIFT);
9160720f62cSfgsch rmsz = 1 + ((reg & PCMCIA_TPCC_RMSZ_MASK) >>
9170720f62cSfgsch PCMCIA_TPCC_RMSZ_SHIFT);
9180720f62cSfgsch rfsz = ((reg & PCMCIA_TPCC_RFSZ_MASK) >>
9190720f62cSfgsch PCMCIA_TPCC_RFSZ_SHIFT);
9200720f62cSfgsch
9211ac9a535Smiod if (tuple->length < 2 + rasz + rmsz + rfsz) {
9220720f62cSfgsch DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too "
9230720f62cSfgsch "short %d\n", rasz, rmsz, rfsz,
9240720f62cSfgsch tuple->length));
9250720f62cSfgsch break;
9260720f62cSfgsch }
9270720f62cSfgsch if (state->pf == NULL) {
9280720f62cSfgsch state->pf = malloc(sizeof(*state->pf),
929109be09cSfgsch M_DEVBUF, M_NOWAIT | M_ZERO);
9308584d5e3Snordin if (state->pf == NULL)
9318584d5e3Snordin panic("pcmcia_parse_cis_tuple");
9320720f62cSfgsch state->pf->number = state->count++;
9330720f62cSfgsch state->pf->last_config_index = -1;
9340720f62cSfgsch SIMPLEQ_INIT(&state->pf->cfe_head);
9350720f62cSfgsch
9360720f62cSfgsch SIMPLEQ_INSERT_TAIL(&state->card->pf_head,
9370720f62cSfgsch state->pf, pf_list);
9380720f62cSfgsch
9390720f62cSfgsch state->pf->function = PCMCIA_FUNCTION_UNSPEC;
9400720f62cSfgsch }
9410720f62cSfgsch state->pf->last_config_index =
9420720f62cSfgsch pcmcia_tuple_read_1(tuple, 1);
9430720f62cSfgsch
9440720f62cSfgsch state->pf->ccr_base = 0;
9450720f62cSfgsch for (i = 0; i < rasz; i++)
9460720f62cSfgsch state->pf->ccr_base |=
9470720f62cSfgsch ((pcmcia_tuple_read_1(tuple, 2 + i)) <<
9480720f62cSfgsch (i * 8));
9490720f62cSfgsch
9500720f62cSfgsch state->pf->ccr_mask = 0;
9510720f62cSfgsch for (i = 0; i < rmsz; i++)
9520720f62cSfgsch state->pf->ccr_mask |=
9530720f62cSfgsch ((pcmcia_tuple_read_1(tuple,
9540720f62cSfgsch 2 + rasz + i)) << (i * 8));
9550720f62cSfgsch
9560720f62cSfgsch /* skip the reserved area and subtuples */
9570720f62cSfgsch
9580720f62cSfgsch /* reset the default cfe for each cfe list */
9590720f62cSfgsch state->temp_cfe = init_cfe;
9600720f62cSfgsch state->default_cfe = &state->temp_cfe;
9610720f62cSfgsch }
9620720f62cSfgsch DPRINTF(("CISTPL_CONFIG\n"));
9630720f62cSfgsch break;
964bd4ab2b6Sniklas
9650720f62cSfgsch case PCMCIA_CISTPL_CFTABLE_ENTRY:
9668e9dce62Sfgsch if (tuple->length < 2) {
9678e9dce62Sfgsch DPRINTF(("CISTPL_CFTABLE_ENTRY too short %d\n",
9688e9dce62Sfgsch tuple->length));
9698e9dce62Sfgsch break;
9708e9dce62Sfgsch } {
9710720f62cSfgsch int idx, i, j;
9720720f62cSfgsch u_int reg, reg2;
9730720f62cSfgsch u_int intface, def, num;
9740720f62cSfgsch u_int power, timing, iospace, irq, memspace, misc;
9750720f62cSfgsch struct pcmcia_config_entry *cfe;
9760720f62cSfgsch
9770720f62cSfgsch idx = 0;
9780720f62cSfgsch
9790720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
9800720f62cSfgsch idx++;
9810720f62cSfgsch intface = reg & PCMCIA_TPCE_INDX_INTFACE;
9820720f62cSfgsch def = reg & PCMCIA_TPCE_INDX_DEFAULT;
9830720f62cSfgsch num = reg & PCMCIA_TPCE_INDX_NUM_MASK;
9840720f62cSfgsch
9850720f62cSfgsch /*
9860720f62cSfgsch * this is a little messy. Some cards have only a
9870720f62cSfgsch * cfentry with the default bit set. So, as we go
9880720f62cSfgsch * through the list, we add new indexes to the queue,
9890720f62cSfgsch * and keep a pointer to the last one with the
9900720f62cSfgsch * default bit set. if we see a record with the same
9910720f62cSfgsch * index, as the default, we stash the default and
9920720f62cSfgsch * replace the queue entry. otherwise, we just add
9930720f62cSfgsch * new entries to the queue, pointing the default ptr
9940720f62cSfgsch * at them if the default bit is set. if we get to
9950720f62cSfgsch * the end with the default pointer pointing at a
9960720f62cSfgsch * record which hasn't had a matching index, that's
9970720f62cSfgsch * ok; it just becomes a cfentry like any other.
9980720f62cSfgsch */
9990720f62cSfgsch
10000720f62cSfgsch /*
10010720f62cSfgsch * if the index in the cis differs from the default
10020720f62cSfgsch * cis, create new entry in the queue and start it
10030720f62cSfgsch * with the current default
10040720f62cSfgsch */
100505460420Sbrad if (state->default_cfe == NULL) {
100605460420Sbrad DPRINTF(("CISTPL_CFTABLE_ENTRY with no "
100705460420Sbrad "default\n"));
100805460420Sbrad break;
100905460420Sbrad }
10100720f62cSfgsch if (num != state->default_cfe->number) {
10110720f62cSfgsch cfe = (struct pcmcia_config_entry *)
10120720f62cSfgsch malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
10138584d5e3Snordin if (cfe == NULL)
10148584d5e3Snordin panic("pcmcia_parse_cis_tuple");
10150720f62cSfgsch
10160720f62cSfgsch *cfe = *state->default_cfe;
10170720f62cSfgsch
10180720f62cSfgsch SIMPLEQ_INSERT_TAIL(&state->pf->cfe_head,
10190720f62cSfgsch cfe, cfe_list);
10200720f62cSfgsch
10210720f62cSfgsch cfe->number = num;
10220720f62cSfgsch
10230720f62cSfgsch /*
10240720f62cSfgsch * if the default bit is set in the cis, then
10250720f62cSfgsch * point the new default at whatever is being
10260720f62cSfgsch * filled in
10270720f62cSfgsch */
10280720f62cSfgsch if (def)
10290720f62cSfgsch state->default_cfe = cfe;
10300720f62cSfgsch } else {
10310720f62cSfgsch /*
10320720f62cSfgsch * the cis index matches the default index,
10330720f62cSfgsch * fill in the default cfentry. It is
10340720f62cSfgsch * assumed that the cfdefault index is in the
10350720f62cSfgsch * queue. For it to be otherwise, the cis
10360720f62cSfgsch * index would have to be -1 (initial
10370720f62cSfgsch * condition) which is not possible, or there
10380720f62cSfgsch * would have to be a preceding cis entry
10390720f62cSfgsch * which had the same cis index and had the
10400720f62cSfgsch * default bit unset. Neither condition
10410720f62cSfgsch * should happen. If it does, this cfentry
10420720f62cSfgsch * is lost (written into temp space), which
10430720f62cSfgsch * is an acceptable failure mode.
10440720f62cSfgsch */
10450720f62cSfgsch
10460720f62cSfgsch cfe = state->default_cfe;
10470720f62cSfgsch
10480720f62cSfgsch /*
10490720f62cSfgsch * if the cis entry does not have the default
10500720f62cSfgsch * bit set, copy the default out of the way
10510720f62cSfgsch * first.
10520720f62cSfgsch */
10530720f62cSfgsch if (!def) {
10540720f62cSfgsch state->temp_cfe = *state->default_cfe;
10550720f62cSfgsch state->default_cfe = &state->temp_cfe;
10560720f62cSfgsch }
10570720f62cSfgsch }
10580720f62cSfgsch
10590720f62cSfgsch if (intface) {
10600720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
10610720f62cSfgsch idx++;
10628e9dce62Sfgsch cfe->flags &= ~(PCMCIA_CFE_MWAIT_REQUIRED
10638e9dce62Sfgsch | PCMCIA_CFE_RDYBSY_ACTIVE
10648e9dce62Sfgsch | PCMCIA_CFE_WP_ACTIVE
10658e9dce62Sfgsch | PCMCIA_CFE_BVD_ACTIVE);
10660720f62cSfgsch if (reg & PCMCIA_TPCE_IF_MWAIT)
10670720f62cSfgsch cfe->flags |= PCMCIA_CFE_MWAIT_REQUIRED;
10680720f62cSfgsch if (reg & PCMCIA_TPCE_IF_RDYBSY)
10690720f62cSfgsch cfe->flags |= PCMCIA_CFE_RDYBSY_ACTIVE;
10700720f62cSfgsch if (reg & PCMCIA_TPCE_IF_WP)
10710720f62cSfgsch cfe->flags |= PCMCIA_CFE_WP_ACTIVE;
10720720f62cSfgsch if (reg & PCMCIA_TPCE_IF_BVD)
10730720f62cSfgsch cfe->flags |= PCMCIA_CFE_BVD_ACTIVE;
10740720f62cSfgsch cfe->iftype = reg & PCMCIA_TPCE_IF_IFTYPE;
10750720f62cSfgsch }
10760720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
10770720f62cSfgsch idx++;
10780720f62cSfgsch
10790720f62cSfgsch power = reg & PCMCIA_TPCE_FS_POWER_MASK;
10800720f62cSfgsch timing = reg & PCMCIA_TPCE_FS_TIMING;
10810720f62cSfgsch iospace = reg & PCMCIA_TPCE_FS_IOSPACE;
10820720f62cSfgsch irq = reg & PCMCIA_TPCE_FS_IRQ;
10830720f62cSfgsch memspace = reg & PCMCIA_TPCE_FS_MEMSPACE_MASK;
10840720f62cSfgsch misc = reg & PCMCIA_TPCE_FS_MISC;
10850720f62cSfgsch
10860720f62cSfgsch if (power) {
10870720f62cSfgsch /* skip over power, don't save */
10880720f62cSfgsch /* for each parameter selection byte */
10890720f62cSfgsch for (i = 0; i < power; i++) {
10900720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
10910720f62cSfgsch idx++;
10920720f62cSfgsch /* for each bit */
10930720f62cSfgsch for (j = 0; j < 7; j++) {
10940720f62cSfgsch /* if the bit is set */
10950720f62cSfgsch if ((reg >> j) & 0x01) {
10960720f62cSfgsch /* skip over bytes */
10970720f62cSfgsch do {
10980720f62cSfgsch reg2 = pcmcia_tuple_read_1(tuple, idx);
10990720f62cSfgsch idx++;
11000720f62cSfgsch /*
11010720f62cSfgsch * until
11022ba33ebbSjmc * non-
11032ba33ebbSjmc * extension
11042ba33ebbSjmc * byte
11050720f62cSfgsch */
11060720f62cSfgsch } while (reg2 & 0x80);
11070720f62cSfgsch }
11080720f62cSfgsch }
11090720f62cSfgsch }
11100720f62cSfgsch }
11110720f62cSfgsch if (timing) {
11120720f62cSfgsch /* skip over timing, don't save */
11130720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
11140720f62cSfgsch idx++;
11150720f62cSfgsch
11160720f62cSfgsch if ((reg & PCMCIA_TPCE_TD_RESERVED_MASK) !=
11170720f62cSfgsch PCMCIA_TPCE_TD_RESERVED_MASK)
11180720f62cSfgsch idx++;
11190720f62cSfgsch if ((reg & PCMCIA_TPCE_TD_RDYBSY_MASK) !=
11200720f62cSfgsch PCMCIA_TPCE_TD_RDYBSY_MASK)
11210720f62cSfgsch idx++;
11220720f62cSfgsch if ((reg & PCMCIA_TPCE_TD_WAIT_MASK) !=
11230720f62cSfgsch PCMCIA_TPCE_TD_WAIT_MASK)
11240720f62cSfgsch idx++;
11250720f62cSfgsch }
11260720f62cSfgsch if (iospace) {
1127709e0656Sfgsch if (tuple->length <= idx) {
1128709e0656Sfgsch DPRINTF(("ran out of space before TPCE_IO\n"));
1129709e0656Sfgsch
1130709e0656Sfgsch goto abort_cfe;
1131709e0656Sfgsch }
1132709e0656Sfgsch
11330720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
11340720f62cSfgsch idx++;
11350720f62cSfgsch
1136ae2d8a60Sfgsch cfe->flags &=
1137ae2d8a60Sfgsch ~(PCMCIA_CFE_IO8 | PCMCIA_CFE_IO16);
11380720f62cSfgsch if (reg & PCMCIA_TPCE_IO_BUSWIDTH_8BIT)
11390720f62cSfgsch cfe->flags |= PCMCIA_CFE_IO8;
11400720f62cSfgsch if (reg & PCMCIA_TPCE_IO_BUSWIDTH_16BIT)
11410720f62cSfgsch cfe->flags |= PCMCIA_CFE_IO16;
11420720f62cSfgsch cfe->iomask =
11430720f62cSfgsch reg & PCMCIA_TPCE_IO_IOADDRLINES_MASK;
11440720f62cSfgsch
11450720f62cSfgsch if (reg & PCMCIA_TPCE_IO_HASRANGE) {
11460720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
11470720f62cSfgsch idx++;
11480720f62cSfgsch
11490720f62cSfgsch cfe->num_iospace = 1 + (reg &
11500720f62cSfgsch PCMCIA_TPCE_IO_RANGE_COUNT);
11510720f62cSfgsch
11520720f62cSfgsch if (cfe->num_iospace >
11530720f62cSfgsch (sizeof(cfe->iospace) /
11540720f62cSfgsch sizeof(cfe->iospace[0]))) {
11550720f62cSfgsch DPRINTF(("too many io "
11560720f62cSfgsch "spaces %d",
11570720f62cSfgsch cfe->num_iospace));
11580720f62cSfgsch state->card->error++;
11590720f62cSfgsch break;
11600720f62cSfgsch }
11610720f62cSfgsch for (i = 0; i < cfe->num_iospace; i++) {
11620720f62cSfgsch switch (reg & PCMCIA_TPCE_IO_RANGE_ADDRSIZE_MASK) {
11630720f62cSfgsch case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_ONE:
11640720f62cSfgsch cfe->iospace[i].start =
11650720f62cSfgsch pcmcia_tuple_read_1(tuple, idx);
11660720f62cSfgsch idx++;
11670720f62cSfgsch break;
11680720f62cSfgsch case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_TWO:
11690720f62cSfgsch cfe->iospace[i].start =
11700720f62cSfgsch pcmcia_tuple_read_2(tuple, idx);
11710720f62cSfgsch idx += 2;
11720720f62cSfgsch break;
11730720f62cSfgsch case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_FOUR:
11740720f62cSfgsch cfe->iospace[i].start =
11750720f62cSfgsch pcmcia_tuple_read_4(tuple, idx);
11760720f62cSfgsch idx += 4;
11770720f62cSfgsch break;
11780720f62cSfgsch }
11790720f62cSfgsch switch (reg &
11800720f62cSfgsch PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_MASK) {
11810720f62cSfgsch case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_ONE:
11820720f62cSfgsch cfe->iospace[i].length =
11830720f62cSfgsch pcmcia_tuple_read_1(tuple, idx);
11840720f62cSfgsch idx++;
11850720f62cSfgsch break;
11860720f62cSfgsch case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_TWO:
11870720f62cSfgsch cfe->iospace[i].length =
11880720f62cSfgsch pcmcia_tuple_read_2(tuple, idx);
11890720f62cSfgsch idx += 2;
11900720f62cSfgsch break;
11910720f62cSfgsch case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_FOUR:
11920720f62cSfgsch cfe->iospace[i].length =
11930720f62cSfgsch pcmcia_tuple_read_4(tuple, idx);
11940720f62cSfgsch idx += 4;
11950720f62cSfgsch break;
11960720f62cSfgsch }
11970720f62cSfgsch cfe->iospace[i].length++;
11980720f62cSfgsch }
11990720f62cSfgsch } else {
12000720f62cSfgsch cfe->num_iospace = 1;
12010720f62cSfgsch cfe->iospace[0].start = 0;
12020720f62cSfgsch cfe->iospace[0].length =
12030720f62cSfgsch (1 << cfe->iomask);
12040720f62cSfgsch }
12050720f62cSfgsch }
1206bd4ab2b6Sniklas
12070720f62cSfgsch if (irq) {
1208709e0656Sfgsch if (tuple->length <= idx) {
1209709e0656Sfgsch DPRINTF(("ran out of space before TPCE_IR\n"));
1210709e0656Sfgsch
1211709e0656Sfgsch goto abort_cfe;
1212709e0656Sfgsch }
1213709e0656Sfgsch
12140720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
12150720f62cSfgsch idx++;
12160720f62cSfgsch
1217ae2d8a60Sfgsch cfe->flags &= ~(PCMCIA_CFE_IRQSHARE
1218ae2d8a60Sfgsch | PCMCIA_CFE_IRQPULSE
1219ae2d8a60Sfgsch | PCMCIA_CFE_IRQLEVEL);
12200720f62cSfgsch if (reg & PCMCIA_TPCE_IR_SHARE)
12210720f62cSfgsch cfe->flags |= PCMCIA_CFE_IRQSHARE;
12220720f62cSfgsch if (reg & PCMCIA_TPCE_IR_PULSE)
12230720f62cSfgsch cfe->flags |= PCMCIA_CFE_IRQPULSE;
12240720f62cSfgsch if (reg & PCMCIA_TPCE_IR_LEVEL)
12250720f62cSfgsch cfe->flags |= PCMCIA_CFE_IRQLEVEL;
12260720f62cSfgsch
12270720f62cSfgsch if (reg & PCMCIA_TPCE_IR_HASMASK) {
12280720f62cSfgsch /*
12290720f62cSfgsch * it's legal to ignore the
12300720f62cSfgsch * special-interrupt bits, so I will
12310720f62cSfgsch */
12320720f62cSfgsch
12330720f62cSfgsch cfe->irqmask =
12340720f62cSfgsch pcmcia_tuple_read_2(tuple, idx);
12350720f62cSfgsch idx += 2;
12360720f62cSfgsch } else {
12370720f62cSfgsch cfe->irqmask =
12380720f62cSfgsch (1 << (reg & PCMCIA_TPCE_IR_IRQ));
12390720f62cSfgsch }
12400720f62cSfgsch }
12410720f62cSfgsch if (memspace) {
1242709e0656Sfgsch if (tuple->length <= idx) {
1243709e0656Sfgsch DPRINTF(("ran out of space before TPCE_MS\n"));
1244709e0656Sfgsch goto abort_cfe;
1245709e0656Sfgsch }
1246709e0656Sfgsch
1247709e0656Sfgsch if (memspace == PCMCIA_TPCE_FS_MEMSPACE_NONE) {
1248709e0656Sfgsch cfe->num_memspace = 0;
1249709e0656Sfgsch } else if (memspace == PCMCIA_TPCE_FS_MEMSPACE_LENGTH) {
12500720f62cSfgsch cfe->num_memspace = 1;
12510720f62cSfgsch cfe->memspace[0].length = 256 *
12520720f62cSfgsch pcmcia_tuple_read_2(tuple, idx);
12530720f62cSfgsch idx += 2;
12540720f62cSfgsch cfe->memspace[0].cardaddr = 0;
12550720f62cSfgsch cfe->memspace[0].hostaddr = 0;
12560720f62cSfgsch } else if (memspace ==
12570720f62cSfgsch PCMCIA_TPCE_FS_MEMSPACE_LENGTHADDR) {
12580720f62cSfgsch cfe->num_memspace = 1;
12590720f62cSfgsch cfe->memspace[0].length = 256 *
12600720f62cSfgsch pcmcia_tuple_read_2(tuple, idx);
12610720f62cSfgsch idx += 2;
12620720f62cSfgsch cfe->memspace[0].cardaddr = 256 *
12630720f62cSfgsch pcmcia_tuple_read_2(tuple, idx);
12640720f62cSfgsch idx += 2;
1265709e0656Sfgsch cfe->memspace[0].hostaddr = cfe->memspace[0].cardaddr;
12660720f62cSfgsch } else {
12670720f62cSfgsch int lengthsize;
12680720f62cSfgsch int cardaddrsize;
12690720f62cSfgsch int hostaddrsize;
12700720f62cSfgsch
12710720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
12720720f62cSfgsch idx++;
12730720f62cSfgsch
12748e9dce62Sfgsch cfe->num_memspace = (reg &
12758e9dce62Sfgsch PCMCIA_TPCE_MS_COUNT) + 1;
12760720f62cSfgsch
12770720f62cSfgsch if (cfe->num_memspace >
12780720f62cSfgsch (sizeof(cfe->memspace) /
12790720f62cSfgsch sizeof(cfe->memspace[0]))) {
12800720f62cSfgsch DPRINTF(("too many mem "
12810720f62cSfgsch "spaces %d",
12820720f62cSfgsch cfe->num_memspace));
12830720f62cSfgsch state->card->error++;
12840720f62cSfgsch break;
12850720f62cSfgsch }
12860720f62cSfgsch lengthsize =
12870720f62cSfgsch ((reg & PCMCIA_TPCE_MS_LENGTH_SIZE_MASK) >>
12880720f62cSfgsch PCMCIA_TPCE_MS_LENGTH_SIZE_SHIFT);
12890720f62cSfgsch cardaddrsize =
12900720f62cSfgsch ((reg & PCMCIA_TPCE_MS_CARDADDR_SIZE_MASK) >>
12910720f62cSfgsch PCMCIA_TPCE_MS_CARDADDR_SIZE_SHIFT);
12920720f62cSfgsch hostaddrsize =
12930720f62cSfgsch (reg & PCMCIA_TPCE_MS_HOSTADDR) ? cardaddrsize : 0;
12940720f62cSfgsch
12950720f62cSfgsch if (lengthsize == 0) {
12960720f62cSfgsch DPRINTF(("cfe memspace "
12970720f62cSfgsch "lengthsize == 0"));
12980720f62cSfgsch state->card->error++;
12990720f62cSfgsch }
13000720f62cSfgsch for (i = 0; i < cfe->num_memspace; i++) {
13010720f62cSfgsch if (lengthsize) {
13020720f62cSfgsch cfe->memspace[i].length =
13030720f62cSfgsch 256 * pcmcia_tuple_read_n(tuple, lengthsize,
13040720f62cSfgsch idx);
13050720f62cSfgsch idx += lengthsize;
13060720f62cSfgsch } else {
13070720f62cSfgsch cfe->memspace[i].length = 0;
13080720f62cSfgsch }
13090720f62cSfgsch if (cfe->memspace[i].length == 0) {
13100720f62cSfgsch DPRINTF(("cfe->memspace[%d].length == 0",
13110720f62cSfgsch i));
13120720f62cSfgsch state->card->error++;
13130720f62cSfgsch }
13140720f62cSfgsch if (cardaddrsize) {
13150720f62cSfgsch cfe->memspace[i].cardaddr =
13160720f62cSfgsch 256 * pcmcia_tuple_read_n(tuple, cardaddrsize,
13170720f62cSfgsch idx);
13180720f62cSfgsch idx += cardaddrsize;
13190720f62cSfgsch } else {
13200720f62cSfgsch cfe->memspace[i].cardaddr = 0;
13210720f62cSfgsch }
13220720f62cSfgsch if (hostaddrsize) {
13230720f62cSfgsch cfe->memspace[i].hostaddr =
13240720f62cSfgsch 256 * pcmcia_tuple_read_n(tuple, hostaddrsize,
13250720f62cSfgsch idx);
13260720f62cSfgsch idx += hostaddrsize;
13270720f62cSfgsch } else {
13280720f62cSfgsch cfe->memspace[i].hostaddr = 0;
13290720f62cSfgsch }
13300720f62cSfgsch }
13310720f62cSfgsch }
13320720f62cSfgsch }
13330720f62cSfgsch if (misc) {
1334709e0656Sfgsch if (tuple->length <= idx) {
1335709e0656Sfgsch DPRINTF(("ran out of space before TPCE_MI\n"));
1336709e0656Sfgsch
1337709e0656Sfgsch goto abort_cfe;
1338709e0656Sfgsch }
1339709e0656Sfgsch
13400720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
13410720f62cSfgsch idx++;
13420720f62cSfgsch
1343ae2d8a60Sfgsch cfe->flags &= ~(PCMCIA_CFE_POWERDOWN
1344ae2d8a60Sfgsch | PCMCIA_CFE_READONLY
1345ae2d8a60Sfgsch | PCMCIA_CFE_AUDIO);
13460720f62cSfgsch if (reg & PCMCIA_TPCE_MI_PWRDOWN)
1347ae2d8a60Sfgsch cfe->flags |= PCMCIA_CFE_POWERDOWN;
13480720f62cSfgsch if (reg & PCMCIA_TPCE_MI_READONLY)
1349ae2d8a60Sfgsch cfe->flags |= PCMCIA_CFE_READONLY;
13500720f62cSfgsch if (reg & PCMCIA_TPCE_MI_AUDIO)
1351ae2d8a60Sfgsch cfe->flags |= PCMCIA_CFE_AUDIO;
13520720f62cSfgsch cfe->maxtwins = reg & PCMCIA_TPCE_MI_MAXTWINS;
13530720f62cSfgsch
13540720f62cSfgsch while (reg & PCMCIA_TPCE_MI_EXT) {
13550720f62cSfgsch reg = pcmcia_tuple_read_1(tuple, idx);
13560720f62cSfgsch idx++;
13570720f62cSfgsch }
13580720f62cSfgsch }
13590720f62cSfgsch /* skip all the subtuples */
13600720f62cSfgsch }
1361709e0656Sfgsch
1362709e0656Sfgsch abort_cfe:
13630720f62cSfgsch DPRINTF(("CISTPL_CFTABLE_ENTRY\n"));
13640720f62cSfgsch break;
1365bd4ab2b6Sniklas
13660720f62cSfgsch default:
13670720f62cSfgsch DPRINTF(("unhandled CISTPL %x\n", tuple->code));
13680720f62cSfgsch break;
13690720f62cSfgsch }
13700720f62cSfgsch
13710720f62cSfgsch return (0);
13720720f62cSfgsch }
1373