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