Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.13
      1 /*	$NetBSD: cardbus.c,v 1.13 1999/11/15 06:01:11 haya 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 
    477     DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    478 	     CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    479 
    480     enable_function(sc, cdstatus, function);
    481 
    482     /* clean up every BAR */
    483     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    484     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    485     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    486     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    487     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    488     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    489     cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    490 
    491     /*
    492      * We need to allocate the ct here, since we might
    493      * need it when reading the CIS
    494      */
    495     if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
    496 						M_DEVBUF, M_NOWAIT))) {
    497       panic("no room for cardbus_tag");
    498     }
    499 
    500     ct->ct_cc = sc->sc_cc;
    501     ct->ct_cf = sc->sc_cf;
    502     ct->ct_bus = sc->sc_bus;
    503     ct->ct_dev = sc->sc_device;
    504     ct->ct_func = function;
    505     ct->ct_sc = sc;
    506     ct->ct_next = NULL;
    507     *previous_next = ct;
    508 
    509     ca.ca_unit = sc->sc_dev.dv_unit;
    510     ca.ca_ct = ct;
    511 
    512     ca.ca_iot = sc->sc_iot;
    513     ca.ca_memt = sc->sc_memt;
    514     ca.ca_dmat = sc->sc_dmat;
    515 
    516     ca.ca_tag = tag;
    517     ca.ca_device = sc->sc_device;
    518     ca.ca_function = function;
    519     ca.ca_id = id;
    520     ca.ca_class = class;
    521 
    522     ca.ca_intrline = sc->sc_intrline;
    523 
    524     bzero(tuple, 2048);
    525 
    526     if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    527       printf("cardbus_attach_card: failed to read CIS\n");
    528     } else {
    529 #ifdef CARDBUS_DEBUG
    530       decode_tuples(tuple, 2048, print_tuple, NULL);
    531 #endif
    532       decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    533     }
    534 
    535     if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
    536       /* do not match */
    537       disable_function(sc, function);
    538       free(ct, M_DEVBUF);
    539       *previous_next = NULL;
    540     } else {
    541       /* found */
    542       previous_next = &(ct->ct_next);
    543       ct->ct_device = csc;
    544       ++no_work_funcs;
    545     }
    546   }
    547   /*
    548    * XXX power down pseudo function 8 (this will power down the card
    549    * if no functions were attached).
    550    */
    551   disable_function(sc, 8);
    552 
    553   return no_work_funcs;
    554 }
    555 
    556 
    557 static int
    558 #ifdef __BROKEN_INDIRECT_CONFIG
    559 cardbussubmatch(parent, match, aux)
    560 #else
    561 cardbussubmatch(parent, cf, aux)
    562 #endif
    563      struct device *parent;
    564 #ifdef __BROKEN_INDIRECT_CONFIG
    565      void *match;
    566 #else
    567      struct cfdata *cf;
    568 #endif
    569      void *aux;
    570 {
    571 #ifdef __BROKEN_INDIRECT_CONFIG
    572   struct cfdata *cf = match;
    573 #endif
    574   struct cardbus_attach_args *ca = aux;
    575 
    576   if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    577       cf->cardbuscf_dev != ca->ca_unit) {
    578     return 0;
    579   }
    580   if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    581       cf->cardbuscf_function != ca->ca_function) {
    582     return 0;
    583   }
    584 
    585   return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    586 }
    587 
    588 
    589 
    590 static int
    591 cardbusprint(aux, pnp)
    592      void *aux;
    593      const char *pnp;
    594 {
    595     struct cardbus_attach_args *ca = aux;
    596     char devinfo[256];
    597     int i;
    598     if (pnp) {
    599 	pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    600 	for (i = 0; i < 4; i++) {
    601 	    if (ca->ca_cis.cis1_info[i] == NULL)
    602 		break;
    603 	    if (i)
    604 		printf(", ");
    605 	    printf("%s", ca->ca_cis.cis1_info[i]);
    606 	}
    607 	if (i)
    608 	    printf(" ");
    609 	printf("(manufacturer 0x%x, product 0x%x)", ca->ca_cis.manufacturer,
    610 	       ca->ca_cis.product);
    611 	printf(" %s at %s", devinfo, pnp);
    612     }
    613     printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    614 
    615     return UNCONF;
    616 }
    617 
    618 
    619 
    620 
    621 
    622 
    623 /*
    624  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    625  *   Interrupt handler of pccard.
    626  *  args:
    627  *   cardbus_chipset_tag_t *cc
    628  *   int irq:
    629  */
    630 void *
    631 cardbus_intr_establish(cc, cf, irq, level, func, arg)
    632      cardbus_chipset_tag_t cc;
    633      cardbus_function_tag_t cf;
    634      cardbus_intr_handle_t irq;
    635      int level;
    636      int (*func) __P((void *));
    637      void *arg;
    638 {
    639   DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    640 
    641   return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
    642 }
    643 
    644 
    645 
    646 /*
    647  * void cardbus_intr_disestablish(cc, cf, handler)
    648  *   Interrupt handler of pccard.
    649  *  args:
    650  *   cardbus_chipset_tag_t *cc
    651  */
    652 void
    653 cardbus_intr_disestablish(cc, cf, handler)
    654      cardbus_chipset_tag_t cc;
    655      cardbus_function_tag_t cf;
    656      void *handler;
    657 {
    658   DPRINTF(("- pccard_intr_disestablish\n"));
    659 
    660  (*cf->cardbus_intr_disestablish)(cc, handler);
    661   return;
    662 }
    663 
    664 
    665 
    666 /* XXX this should be merged with cardbus_function_{enable,disable},
    667    but we don't have a ct when these functions are called */
    668 
    669 static void
    670 enable_function(sc, cdstatus, function)
    671      struct cardbus_softc *sc;
    672      int cdstatus;
    673      int function;
    674 {
    675     if(sc->sc_poweron_func == 0) {
    676 	if (cdstatus & CARDBUS_3V_CARD) {
    677 	    sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
    678 	}
    679 	(sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    680     }
    681     sc->sc_poweron_func |= (1 << function);
    682 }
    683 
    684 static void
    685 disable_function(sc, function)
    686      struct cardbus_softc *sc;
    687      int function;
    688 {
    689     sc->sc_poweron_func &= ~(1 << function);
    690     if(sc->sc_poweron_func == 0) {
    691 	/* power-off because no functions are enabled */
    692 	sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
    693     }
    694 }
    695 
    696 /*
    697  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    698  *
    699  *   This function enables a function on a card.  When no power is
    700  *  applied on the card, power will be applied on it.
    701  */
    702 int
    703 cardbus_function_enable(sc, func)
    704      struct cardbus_softc *sc;
    705      int func;
    706 {
    707   cardbus_chipset_tag_t cc = sc->sc_cc;
    708   cardbus_function_tag_t cf = sc->sc_cf;
    709   cardbusreg_t command;
    710   cardbustag_t tag;
    711 
    712   DPRINTF(("entering cardbus_function_enable...  "));
    713 
    714   /* entering critical area */
    715 
    716   enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
    717 
    718   /* exiting critical area */
    719 
    720   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    721 
    722   command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    723   command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    724 
    725   cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    726 
    727   cardbus_free_tag(cc, cf, tag);
    728 
    729   DPRINTF(("%x\n", sc->sc_poweron_func));
    730 
    731   return 0;
    732 }
    733 
    734 
    735 /*
    736  * int cardbus_function_disable(struct cardbus_softc *, int func)
    737  *
    738  *   This function disable a function on a card.  When no functions are
    739  *  enabled, it turns off the power.
    740  */
    741 int
    742 cardbus_function_disable(sc, func)
    743      struct cardbus_softc *sc;
    744      int func;
    745 {
    746 
    747   DPRINTF(("entering cardbus_function_disable...  "));
    748 
    749   disable_function(sc, func);
    750 
    751   return 0;
    752 }
    753 
    754 
    755 
    756 
    757 
    758 
    759 
    760 /*
    761  * below this line, there are some functions for decoding tuples.
    762  * They should go out from this file.
    763  */
    764 
    765 static u_int8_t *
    766 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
    767 
    768 static int
    769 decode_tuples(tuple, buflen, func, data)
    770      u_int8_t *tuple;
    771      int buflen;
    772      tuple_decode_func func;
    773      void *data;
    774 {
    775   u_int8_t *tp = tuple;
    776 
    777   if (CISTPL_LINKTARGET != *tuple) {
    778     DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    779     return 0;
    780   }
    781 
    782   while (NULL != (tp = decode_tuple(tp, func, data))) {
    783     if (tuple + buflen < tp) {
    784       break;
    785     }
    786   }
    787 
    788   return 1;
    789 }
    790 
    791 
    792 static u_int8_t *
    793 decode_tuple(tuple, func, data)
    794      u_int8_t *tuple;
    795      tuple_decode_func func;
    796      void *data;
    797 {
    798     u_int8_t type;
    799     u_int8_t len;
    800 
    801     type = tuple[0];
    802     len = tuple[1] + 2;
    803 
    804     (*func)(tuple, len, data);
    805 
    806     if (CISTPL_END == type) {
    807 	return NULL;
    808     }
    809 
    810     return tuple + len;
    811 }
    812 
    813 
    814 #ifdef CARDBUS_DEBUG
    815 static char *tuple_name __P((int type));
    816 
    817 static char *
    818 tuple_name(type)
    819      int type;
    820 {
    821   static char *tuple_name_s [] = {
    822     "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    823     "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    824     "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    825     "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    826     "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET",	/* 10-13 */
    827     "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    828     "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    829     "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    830     "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    831     "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    832     "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    833     "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    834     "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    835     "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    836     "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    837     "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    838     "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    839     "DATE", "BATTERY", "ORG"
    840   };
    841 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    842 
    843   if (type > 0 && type < NAME_LEN(tuple_name_s)) {
    844     return tuple_name_s[type];
    845   } else if (0xff == type) {
    846     return "END";
    847   } else {
    848     return "Reserved";
    849   }
    850 }
    851 
    852 static void
    853 print_tuple(tuple, len, data)
    854      u_int8_t *tuple;
    855      int len;
    856      void *data;
    857 {
    858     int i;
    859 
    860     printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
    861 
    862     for (i = 0; i < len; ++i) {
    863 	if (i % 16 == 0) {
    864 	    printf("  0x%2x:", i);
    865 	}
    866 	printf(" %x",tuple[i]);
    867 	if (i % 16 == 15) {
    868 	    printf("\n");
    869 	}
    870     }
    871     if (i % 16 != 0) {
    872 	printf("\n");
    873     }
    874 }
    875 
    876 #endif
    877 
    878