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