Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.41
      1 /*	$NetBSD: cardbus.c,v 1.41 2002/06/01 23:50:56 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999 and 2000
      5  *     HAYAKAWA Koichi.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by HAYAKAWA Koichi.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.41 2002/06/01 23:50:56 lukem Exp $");
     37 
     38 #include "opt_cardbus.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/syslog.h>
     46 #include <sys/proc.h>
     47 #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     48 
     49 #include <machine/bus.h>
     50 
     51 #include <dev/cardbus/cardbusvar.h>
     52 #include <dev/cardbus/cardbusdevs.h>
     53 
     54 #include <dev/cardbus/cardbus_exrom.h>
     55 
     56 #include <dev/pci/pcivar.h>	/* XXX */
     57 #include <dev/pci/pcireg.h>	/* XXX */
     58 
     59 #include <dev/pcmcia/pcmciareg.h>
     60 
     61 #if defined CARDBUS_DEBUG
     62 #define STATIC
     63 #define DPRINTF(a) printf a
     64 #else
     65 #define STATIC static
     66 #define DPRINTF(a)
     67 #endif
     68 
     69 
     70 STATIC void cardbusattach(struct device *, struct device *, void *);
     71 int cardbus_attach_card(struct cardbus_softc *);
     72 
     73 STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     74 static int cardbussubmatch(struct device *, struct cfdata *, void *);
     75 static int cardbusprint(void *, const char *);
     76 
     77 typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
     78 
     79 static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*);
     80 #ifdef CARDBUS_DEBUG
     81 static void print_tuple(u_int8_t*, int, void*);
     82 #endif
     83 
     84 static int cardbus_read_tuples(struct cardbus_attach_args *,
     85     cardbusreg_t, u_int8_t *, size_t);
     86 
     87 static void enable_function(struct cardbus_softc *, int, int);
     88 static void disable_function(struct cardbus_softc *, int);
     89 
     90 struct cfattach cardbus_ca = {
     91 	sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
     92 };
     93 
     94 #ifndef __NetBSD_Version__
     95 struct cfdriver cardbus_cd = {
     96 	NULL, "cardbus", DV_DULL
     97 };
     98 #endif
     99 
    100 
    101 STATIC int
    102 cardbusmatch(struct device *parent, struct cfdata *cf, void *aux)
    103 {
    104 	struct cbslot_attach_args *cba = aux;
    105 
    106 	if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
    107 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    108 		    cba->cba_busname, cf->cf_driver->cd_name));
    109 		return (0);
    110 	}
    111 
    112 	return (1);
    113 }
    114 
    115 STATIC void
    116 cardbusattach(struct device *parent, struct device *self, void *aux)
    117 {
    118 	struct cardbus_softc *sc = (void *)self;
    119 	struct cbslot_attach_args *cba = aux;
    120 
    121 	sc->sc_bus = cba->cba_bus;
    122 	sc->sc_device = 0;
    123 	sc->sc_intrline = cba->cba_intrline;
    124 	sc->sc_cacheline = cba->cba_cacheline;
    125 	sc->sc_lattimer = cba->cba_lattimer;
    126 
    127 	printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
    128 	if (bootverbose)
    129 		printf(" cacheline 0x%x, lattimer 0x%x", sc->sc_cacheline,
    130 		       sc->sc_lattimer);
    131 	printf("\n");
    132 
    133 	sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    134 	sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    135 	sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    136 	sc->sc_cc = cba->cba_cc;
    137 	sc->sc_cf = cba->cba_cf;
    138 
    139 #if rbus
    140 	sc->sc_rbus_iot = cba->cba_rbus_iot;
    141 	sc->sc_rbus_memt = cba->cba_rbus_memt;
    142 #endif
    143 
    144 	sc->sc_funcs = NULL;
    145 }
    146 
    147 static int
    148 cardbus_read_tuples(struct cardbus_attach_args *ca, cardbusreg_t cis_ptr,
    149     u_int8_t *tuples, size_t len)
    150 {
    151 	struct cardbus_softc *sc = ca->ca_ct->ct_sc;
    152 	cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
    153 	cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
    154 	cardbustag_t tag = ca->ca_tag;
    155 	cardbusreg_t command;
    156 	bus_space_tag_t bar_tag;
    157 	bus_space_handle_t bar_memh;
    158 	bus_size_t bar_size;
    159 	bus_addr_t bar_addr;
    160 	cardbusreg_t reg;
    161 	int found = 0;
    162 	int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
    163 	int i, j;
    164 
    165 	memset(tuples, 0, len);
    166 
    167 	cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
    168 
    169 	switch (cardbus_space) {
    170 	case CARDBUS_CIS_ASI_TUPLE:
    171 		DPRINTF(("%s: reading CIS data from configuration space\n",
    172 		    sc->sc_dev.dv_xname));
    173 		for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
    174 			u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i);
    175 			tuples[j] = 0xff & e;
    176 			e >>= 8;
    177 			tuples[j + 1] = 0xff & e;
    178 			e >>= 8;
    179 			tuples[j + 2] = 0xff & e;
    180 			e >>= 8;
    181 			tuples[j + 3] = 0xff & e;
    182 			j += 4;
    183 		}
    184 		found++;
    185 		break;
    186 
    187 	case CARDBUS_CIS_ASI_BAR0:
    188 	case CARDBUS_CIS_ASI_BAR1:
    189 	case CARDBUS_CIS_ASI_BAR2:
    190 	case CARDBUS_CIS_ASI_BAR3:
    191 	case CARDBUS_CIS_ASI_BAR4:
    192 	case CARDBUS_CIS_ASI_BAR5:
    193 	case CARDBUS_CIS_ASI_ROM:
    194 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    195 			reg = CARDBUS_ROM_REG;
    196 			DPRINTF(("%s: reading CIS data from ROM\n",
    197 			    sc->sc_dev.dv_xname));
    198 		} else {
    199 			reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
    200 			DPRINTF(("%s: reading CIS data from BAR%d\n",
    201 			    sc->sc_dev.dv_xname, cardbus_space - 1));
    202 		}
    203 
    204 		/*
    205 		 * XXX zero register so mapreg_map doesn't get confused by old
    206 		 * contents.
    207 		 */
    208 		cardbus_conf_write(cc, cf, tag, reg, 0);
    209 		if (Cardbus_mapreg_map(ca->ca_ct, reg,
    210 		    CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    211 		    0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
    212 			printf("%s: failed to map memory\n",
    213 			    sc->sc_dev.dv_xname);
    214 			return (1);
    215 		}
    216 
    217 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    218 			cardbusreg_t exrom;
    219 			int save;
    220 			struct cardbus_rom_image_head rom_image;
    221 			struct cardbus_rom_image *p;
    222 
    223 			save = splhigh();
    224 			/* enable rom address decoder */
    225 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    226 			cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    227 
    228 			command = cardbus_conf_read(cc, cf, tag,
    229 			    CARDBUS_COMMAND_STATUS_REG);
    230 			cardbus_conf_write(cc, cf, tag,
    231 			    CARDBUS_COMMAND_STATUS_REG,
    232 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    233 
    234 			if (cardbus_read_exrom(ca->ca_memt, bar_memh,
    235 			    &rom_image))
    236 				goto out;
    237 
    238 			SIMPLEQ_FOREACH(p, &rom_image, next) {
    239 				if (p->rom_image ==
    240 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    241 					bus_space_read_region_1(p->romt,
    242 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    243 					    tuples, 256);
    244 					found++;
    245 				}
    246 				break;
    247 			}
    248 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    249 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
    250 				free(p, M_DEVBUF);
    251 			}
    252 		out:
    253 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    254 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    255 			splx(save);
    256 		} else {
    257 			command = cardbus_conf_read(cc, cf, tag,
    258 			    CARDBUS_COMMAND_STATUS_REG);
    259 			cardbus_conf_write(cc, cf, tag,
    260 			    CARDBUS_COMMAND_STATUS_REG,
    261 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    262 			/* XXX byte order? */
    263 			bus_space_read_region_1(ca->ca_memt, bar_memh,
    264 			    cis_ptr, tuples, 256);
    265 			found++;
    266 		}
    267 		command = cardbus_conf_read(cc, cf, tag,
    268 		    CARDBUS_COMMAND_STATUS_REG);
    269 		cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    270 		    command & ~CARDBUS_COMMAND_MEM_ENABLE);
    271 		cardbus_conf_write(cc, cf, tag, reg, 0);
    272 
    273 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
    274 		    bar_size);
    275 		break;
    276 
    277 #ifdef DIAGNOSTIC
    278 	default:
    279 		panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
    280 		    cardbus_space);
    281 #endif
    282 	}
    283 	return (!found);
    284 }
    285 
    286 static void
    287 parse_tuple(u_int8_t *tuple, int len, void *data)
    288 {
    289 	struct cardbus_cis_info *cis = data;
    290 	char *p;
    291 	int i, bar_index;
    292 
    293 	switch (tuple[0]) {
    294 	case PCMCIA_CISTPL_MANFID:
    295 		if (tuple[1] != 5) {
    296 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
    297 			    __func__, tuple[1]));
    298 			break;
    299 		}
    300 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
    301 		cis->product = tuple[4] | (tuple[5] << 8);
    302 		break;
    303 
    304 	case PCMCIA_CISTPL_VERS_1:
    305 		memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
    306 		i = 0;
    307 		p = cis->cis1_info_buf + 2;
    308 		while (i <
    309 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
    310 			cis->cis1_info[i++] = p;
    311 			while (*p != '\0' && *p != '\xff')
    312 				p++;
    313 			if (*p == '\xff')
    314 				break;
    315 			p++;
    316 		}
    317 		break;
    318 
    319 	case PCMCIA_CISTPL_BAR:
    320 		if (tuple[1] != 6) {
    321 			DPRINTF(("%s: BAR with short length (%d)\n",
    322 			    __func__, tuple[1]));
    323 			break;
    324 		}
    325 		bar_index = tuple[2] & 7;
    326 		if (bar_index == 0) {
    327 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    328 			break;
    329 		}
    330 		bar_index--;
    331 		cis->bar[bar_index].flags = tuple[2];
    332 		cis->bar[bar_index].size =
    333 		    (tuple[4] << 0) |
    334 		    (tuple[5] << 8) |
    335 		    (tuple[6] << 16) |
    336 		    (tuple[7] << 24);
    337 		break;
    338 
    339 	case PCMCIA_CISTPL_FUNCID:
    340 		cis->funcid = tuple[2];
    341 		break;
    342 
    343 	case PCMCIA_CISTPL_FUNCE:
    344 		switch (cis->funcid) {
    345 		case PCMCIA_FUNCTION_SERIAL:
    346 			if (tuple[1] >= 2 &&
    347 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    348 			    tuple[2] == 0) {
    349 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    350 				cis->funce.serial.uart_present = 1;
    351 			}
    352 			break;
    353 
    354 		case PCMCIA_FUNCTION_NETWORK:
    355 			if (tuple[1] >= 8 &&
    356 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    357 				if (tuple[3] >
    358 				    sizeof(cis->funce.network.netid)) {
    359 					DPRINTF(("%s: unknown network id type "
    360 					    "(len = %d)\n",
    361 					    __func__, tuple[3]));
    362 				} else {
    363 					cis->funce.network.netid_present = 1;
    364 					memcpy(cis->funce.network.netid,
    365 					    tuple + 4, tuple[3]);
    366 				}
    367 			}
    368 			break;
    369 		}
    370 		break;
    371 	}
    372 }
    373 
    374 /*
    375  * int cardbus_attach_card(struct cardbus_softc *sc)
    376  *
    377  *    This function attaches the card on the slot: turns on power,
    378  *    reads and analyses tuple, sets configuration index.
    379  *
    380  *    This function returns the number of recognised device functions.
    381  *    If no functions are recognised, return 0.
    382  */
    383 int
    384 cardbus_attach_card(struct cardbus_softc *sc)
    385 {
    386 	cardbus_chipset_tag_t cc;
    387 	cardbus_function_tag_t cf;
    388 	cardbustag_t tag;
    389 	cardbusreg_t id, class, cis_ptr;
    390 	cardbusreg_t bhlc;
    391 	u_int8_t tuple[2048];
    392 	int cdstatus;
    393 	int function, nfunction;
    394 	struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
    395 	struct device *csc;
    396 	int no_work_funcs = 0;
    397 	cardbus_devfunc_t ct;
    398 
    399 	cc = sc->sc_cc;
    400 	cf = sc->sc_cf;
    401 
    402 	DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    403 
    404 	/* inspect initial voltage */
    405 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    406 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
    407 		    sc->sc_dev.dv_unit));
    408 		return (0);
    409 	}
    410 
    411 	/*
    412 	 * XXX use fake function 8 to keep power on during whole
    413 	 * configuration.
    414 	 */
    415 	enable_function(sc, cdstatus, 8);
    416 	function = 0;
    417 
    418 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    419 
    420 	/*
    421 	 * Wait until power comes up.  Maxmum 500 ms.
    422 	 */
    423 	{
    424 		int i;
    425 
    426 		for (i = 0; i < 5; ++i) {
    427 			id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    428 			if (id != 0xffffffff && id != 0) {
    429 				break;
    430 			}
    431 			if (cold) {	/* before kernel thread invoked */
    432 				delay(100 * 1000);
    433 			} else {	/* thread context */
    434 				if (tsleep((void *)sc, PCATCH, "cardbus",
    435 				    hz / 10) != EWOULDBLOCK) {
    436 					break;
    437 				}
    438 			}
    439 		}
    440 		if (i == 5) {
    441 			return (0);
    442 		}
    443 	}
    444 
    445 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    446 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
    447 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    448 
    449 	for (function = 0; function < nfunction; function++) {
    450 		struct cardbus_attach_args ca;
    451 
    452 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device,
    453 		    function);
    454 
    455 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    456 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    457 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    458 
    459 		/* Invalid vendor ID value? */
    460 		if (CARDBUS_VENDOR(id) == CARDBUS_VENDOR_INVALID) {
    461 			continue;
    462 		}
    463 
    464 		DPRINTF(("cardbus_attach_card: "
    465 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    466 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    467 
    468 		enable_function(sc, cdstatus, function);
    469 
    470 		/* clean up every BAR */
    471 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    472 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    473 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    474 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    475 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    476 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    477 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    478 
    479 		/* set initial latency and cacheline size */
    480 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    481 		DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
    482 		    function, bhlc));
    483 		bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
    484 		    (CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
    485 		bhlc |= ((sc->sc_cacheline & CARDBUS_CACHELINE_MASK) << CARDBUS_CACHELINE_SHIFT);
    486 		bhlc |= ((sc->sc_lattimer & CARDBUS_LATTIMER_MASK) << CARDBUS_LATTIMER_SHIFT);
    487 
    488 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    489 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    490 		DPRINTF(("0x%08x\n", bhlc));
    491 
    492 		if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    493 			bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    494 			bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    495 			cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    496 		}
    497 
    498 		/*
    499 		 * We need to allocate the ct here, since we might
    500 		 * need it when reading the CIS
    501 		 */
    502 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    503 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    504 			panic("no room for cardbus_tag");
    505 		}
    506 
    507 		ct->ct_cc = sc->sc_cc;
    508 		ct->ct_cf = sc->sc_cf;
    509 		ct->ct_bus = sc->sc_bus;
    510 		ct->ct_dev = sc->sc_device;
    511 		ct->ct_func = function;
    512 		ct->ct_sc = sc;
    513 		ct->ct_next = NULL;
    514 		*previous_next = ct;
    515 
    516 		memset(&ca, 0, sizeof(ca));
    517 
    518 		ca.ca_unit = sc->sc_dev.dv_unit;
    519 		ca.ca_ct = ct;
    520 
    521 		ca.ca_iot = sc->sc_iot;
    522 		ca.ca_memt = sc->sc_memt;
    523 		ca.ca_dmat = sc->sc_dmat;
    524 
    525 #if rbus
    526 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    527 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    528 #endif
    529 
    530 		ca.ca_tag = tag;
    531 		ca.ca_bus = sc->sc_bus;
    532 		ca.ca_device = sc->sc_device;
    533 		ca.ca_function = function;
    534 		ca.ca_id = id;
    535 		ca.ca_class = class;
    536 
    537 		ca.ca_intrline = sc->sc_intrline;
    538 
    539 		if (cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    540 			printf("cardbus_attach_card: failed to read CIS\n");
    541 		} else {
    542 #ifdef CARDBUS_DEBUG
    543 			decode_tuples(tuple, 2048, print_tuple, NULL);
    544 #endif
    545 			decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    546 		}
    547 
    548 		if ((csc = config_found_sm((void *)sc, &ca, cardbusprint,
    549 		    cardbussubmatch)) == NULL) {
    550 			/* do not match */
    551 			disable_function(sc, function);
    552 			free(ct, M_DEVBUF);
    553 			*previous_next = NULL;
    554 		} else {
    555 			/* found */
    556 			previous_next = &(ct->ct_next);
    557 			ct->ct_device = csc;
    558 			++no_work_funcs;
    559 		}
    560 	}
    561 	/*
    562 	 * XXX power down pseudo function 8 (this will power down the card
    563 	 * if no functions were attached).
    564 	 */
    565 	disable_function(sc, 8);
    566 
    567 	return (no_work_funcs);
    568 }
    569 
    570 static int
    571 cardbussubmatch(struct device *parent, struct cfdata *cf, void *aux)
    572 {
    573 	struct cardbus_attach_args *ca = aux;
    574 
    575 	if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    576 	    cf->cardbuscf_dev != ca->ca_unit) {
    577 		return (0);
    578 	}
    579 	if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    580 	    cf->cardbuscf_function != ca->ca_function) {
    581 		return (0);
    582 	}
    583 
    584 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    585 }
    586 
    587 static int
    588 cardbusprint(void *aux, const char *pnp)
    589 {
    590 	struct cardbus_attach_args *ca = aux;
    591 	char devinfo[256];
    592 	int i;
    593 
    594 	if (pnp) {
    595 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    596 		for (i = 0; i < 4; i++) {
    597 			if (ca->ca_cis.cis1_info[i] == NULL)
    598 				break;
    599 			if (i)
    600 				printf(", ");
    601 			printf("%s", ca->ca_cis.cis1_info[i]);
    602 		}
    603 		if (bootverbose) {
    604 			if (i)
    605 				printf(" ");
    606 			printf("(manufacturer 0x%x, product 0x%x)",
    607 			       ca->ca_cis.manufacturer, ca->ca_cis.product);
    608 		}
    609 		printf(" %s at %s", devinfo, pnp);
    610 	}
    611 	printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    612 
    613 	return (UNCONF);
    614 }
    615 
    616 /*
    617  * void cardbus_detach_card(struct cardbus_softc *sc)
    618  *
    619  *    This function detaches the card on the slot: detach device data
    620  *    structure and turns off the power.
    621  *
    622  *    This function must not be called under interrupt context.
    623  */
    624 void
    625 cardbus_detach_card(struct cardbus_softc *sc)
    626 {
    627 	struct cardbus_devfunc *ct, *ct_next, **prev_next;
    628 
    629 	prev_next = &(sc->sc_funcs->ct_next);
    630 
    631 	for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
    632 		struct device *fndev = ct->ct_device;
    633 		ct_next = ct->ct_next;
    634 
    635 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    636 		    fndev->dv_xname));
    637 		/* call device detach function */
    638 
    639 		if (0 != config_detach(fndev, 0)) {
    640 			printf("%s: cannot detach dev %s, function %d\n",
    641 			    sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
    642 			prev_next = &(ct->ct_next);
    643 		} else {
    644 			sc->sc_poweron_func &= ~(1 << ct->ct_func);
    645 			*prev_next = ct->ct_next;
    646 			free(ct, M_DEVBUF);
    647 		}
    648 	}
    649 
    650 	sc->sc_poweron_func = 0;
    651 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    652 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    653 }
    654 
    655 /*
    656  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    657  *   Interrupt handler of pccard.
    658  *  args:
    659  *   cardbus_chipset_tag_t *cc
    660  *   int irq:
    661  */
    662 void *
    663 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    664     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    665 {
    666 
    667 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    668 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    669 }
    670 
    671 /*
    672  * void cardbus_intr_disestablish(cc, cf, handler)
    673  *   Interrupt handler of pccard.
    674  *  args:
    675  *   cardbus_chipset_tag_t *cc
    676  */
    677 void
    678 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    679     void *handler)
    680 {
    681 
    682 	DPRINTF(("- pccard_intr_disestablish\n"));
    683 	(*cf->cardbus_intr_disestablish)(cc, handler);
    684 }
    685 
    686 /*
    687  * XXX this should be merged with cardbus_function_{enable,disable},
    688  * but we don't have a ct when these functions are called.
    689  */
    690 static void
    691 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    692 {
    693 
    694 	if (sc->sc_poweron_func == 0) {
    695 		/* switch to 3V and/or wait for power to stabilize */
    696 		if (cdstatus & CARDBUS_3V_CARD) {
    697 			/*
    698 			 * sc_poweron_func must be substituted before
    699 			 * entering sleep, in order to avoid turn on
    700 			 * power twice.
    701 			 */
    702 			sc->sc_poweron_func |= (1 << function);
    703 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    704 		} else {
    705 			/* No cards other than 3.3V cards. */
    706 			return;
    707 		}
    708 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    709 	}
    710 	sc->sc_poweron_func |= (1 << function);
    711 }
    712 
    713 static void
    714 disable_function(struct cardbus_softc *sc, int function)
    715 {
    716 
    717 	sc->sc_poweron_func &= ~(1 << function);
    718 	if (sc->sc_poweron_func == 0) {
    719 		/* power-off because no functions are enabled */
    720 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    721 	}
    722 }
    723 
    724 /*
    725  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    726  *
    727  *   This function enables a function on a card.  When no power is
    728  *  applied on the card, power will be applied on it.
    729  */
    730 int
    731 cardbus_function_enable(struct cardbus_softc *sc, int func)
    732 {
    733 	cardbus_chipset_tag_t cc = sc->sc_cc;
    734 	cardbus_function_tag_t cf = sc->sc_cf;
    735 	cardbusreg_t command;
    736 	cardbustag_t tag;
    737 
    738 	DPRINTF(("entering cardbus_function_enable...  "));
    739 
    740 	/* entering critical area */
    741 
    742 	/* XXX: sc_vold should be used */
    743 	enable_function(sc, CARDBUS_3V_CARD, func);
    744 
    745 	/* exiting critical area */
    746 
    747 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    748 
    749 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    750 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    751 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    752 
    753 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    754 
    755 	cardbus_free_tag(cc, cf, tag);
    756 
    757 	DPRINTF(("%x\n", sc->sc_poweron_func));
    758 
    759 	return (0);
    760 }
    761 
    762 /*
    763  * int cardbus_function_disable(struct cardbus_softc *, int func)
    764  *
    765  *   This function disable a function on a card.  When no functions are
    766  *  enabled, it turns off the power.
    767  */
    768 int
    769 cardbus_function_disable(struct cardbus_softc *sc, int func)
    770 {
    771 
    772 	DPRINTF(("entering cardbus_function_disable...  "));
    773 
    774 	disable_function(sc, func);
    775 
    776 	return (0);
    777 }
    778 
    779 /*
    780  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    781  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    782  *	cardbusreg_t *value)
    783  *
    784  *	Find the specified PCI capability.
    785  */
    786 int
    787 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    788     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    789 {
    790 	cardbusreg_t reg;
    791 	unsigned int ofs;
    792 
    793 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    794 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    795 		return (0);
    796 
    797 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    798 	    PCI_CAPLISTPTR_REG));
    799 	while (ofs != 0) {
    800 #ifdef DIAGNOSTIC
    801 		if ((ofs & 3) || (ofs < 0x40))
    802 			panic("cardbus_get_capability");
    803 #endif
    804 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    805 		if (PCI_CAPLIST_CAP(reg) == capid) {
    806 			if (offset)
    807 				*offset = ofs;
    808 			if (value)
    809 				*value = reg;
    810 			return (1);
    811 		}
    812 		ofs = PCI_CAPLIST_NEXT(reg);
    813 	}
    814 
    815 	return (0);
    816 }
    817 
    818 /*
    819  * below this line, there are some functions for decoding tuples.
    820  * They should go out from this file.
    821  */
    822 
    823 static u_int8_t *
    824 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data);
    825 
    826 static int
    827 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    828 {
    829 	u_int8_t *tp = tuple;
    830 
    831 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    832 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    833 		return (0);
    834 	}
    835 
    836 	while (NULL != (tp = decode_tuple(tp, func, data))) {
    837 		if (tuple + buflen < tp) {
    838 			break;
    839 		}
    840 	}
    841 
    842 	return (1);
    843 }
    844 
    845 static u_int8_t *
    846 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data)
    847 {
    848 	u_int8_t type;
    849 	u_int8_t len;
    850 
    851 	type = tuple[0];
    852 	len = tuple[1] + 2;
    853 
    854 	(*func)(tuple, len, data);
    855 
    856 	if (type == PCMCIA_CISTPL_END) {
    857 		return (NULL);
    858 	}
    859 
    860 	return (tuple + len);
    861 }
    862 
    863 #ifdef CARDBUS_DEBUG
    864 static const char *tuple_name(int);
    865 static const char *tuple_names[] = {
    866 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    867 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    868 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    869 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    870 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
    871 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    872 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    873 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    874 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    875 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    876 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    877 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    878 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    879 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    880 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    881 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    882 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    883 	"DATE", "BATTERY", "ORG"
    884 };
    885 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    886 
    887 static const char *
    888 tuple_name(int type)
    889 {
    890 
    891 	if (0 <= type && type < NAME_LEN(tuple_names)) {
    892 		return (tuple_names[type]);
    893 	} else if (type == 0xff) {
    894 		return ("END");
    895 	} else {
    896 		return ("Reserved");
    897 	}
    898 }
    899 
    900 static void
    901 print_tuple(u_int8_t *tuple, int len, void *data)
    902 {
    903 	int i;
    904 
    905 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
    906 
    907 	for (i = 0; i < len; ++i) {
    908 		if (i % 16 == 0) {
    909 			printf("  0x%2x:", i);
    910 		}
    911 		printf(" %x", tuple[i]);
    912 		if (i % 16 == 15) {
    913 			printf("\n");
    914 		}
    915 	}
    916 	if (i % 16 != 0) {
    917 		printf("\n");
    918 	}
    919 }
    920 #endif
    921