Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.10
      1 /*	$NetBSD: cardbus.c,v 1.10 1999/11/08 20:19:10 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",
    364 		     __func__, bar_index));
    365 	    break;
    366 	}
    367 	bar_index--;
    368 	cis->bar[bar_index].flags = tuple[2];
    369 	cis->bar[bar_index].size = (tuple[4] << 0) |
    370 				   (tuple[5] << 8) |
    371 				   (tuple[6] << 16) |
    372 				   (tuple[7] << 24);
    373 	break;
    374     case PCMCIA_CISTPL_FUNCID:
    375 	cis->funcid = tuple[2];
    376 	break;
    377 
    378     case PCMCIA_CISTPL_FUNCE:
    379 	if(cis->funcid == PCMCIA_FUNCTION_NETWORK && tuple[1] >= 8) {
    380 	    if(tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    381 		if(tuple[3] > sizeof(cis->funce.network.netid)) {
    382 		    DPRINTF(("%s: unknown network id type (len = %d)\n",
    383 			     __func__, tuple[3]));
    384 		} else {
    385 		    memcpy(cis->funce.network.netid,
    386 			   tuple + 4, tuple[3]);
    387 		}
    388 	    }
    389 	}
    390 	break;
    391     }
    392 }
    393 
    394 /*
    395  * int cardbus_attach_card(struct cardbus_softc *sc)
    396  *
    397  *    This function attaches the card on the slot: turns on power,
    398  *    reads and analyses tuple, sets consifuration index.
    399  *
    400  *    This function returns the number of recognised device functions.
    401  *    If no functions are recognised, return 0.
    402  */
    403 int
    404 cardbus_attach_card(sc)
    405      struct cardbus_softc *sc;
    406 {
    407   cardbus_chipset_tag_t cc;
    408   cardbus_function_tag_t cf;
    409   int cdstatus;
    410   cardbustag_t tag;
    411   cardbusreg_t id, class, cis_ptr;
    412   cardbusreg_t bhlc;
    413   u_int8_t tuple[2048];
    414   int function, nfunction;
    415   struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
    416   struct device *csc;
    417   int no_work_funcs = 0;
    418   cardbus_devfunc_t ct;
    419 
    420   cc = sc->sc_cc;
    421   cf = sc->sc_cf;
    422 
    423   DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    424 
    425   /* inspect initial voltage */
    426   if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
    427     DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
    428     return 0;
    429   }
    430 
    431   enable_function(sc, cdstatus, 8); /* XXX use fake function 8 to
    432 				       keep power on during whole
    433 				       configuration */
    434   function = 0;
    435 
    436   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    437 
    438   /*
    439    * Wait until power comes up.  Maxmum 500 ms.
    440    */
    441   {
    442     int i;
    443     for (i = 0; i < 5; ++i) {
    444       id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    445       if (id != 0xffffffff && id != 0) {
    446 	break;
    447       }
    448       delay(100*1000);		/* or tsleep */
    449     }
    450     if (i == 5) {
    451       return 0;
    452     }
    453   }
    454 
    455   bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    456   if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    457     bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    458     bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    459     cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    460   }
    461 
    462   nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    463 
    464   for(function = 0; function < nfunction; function++) {
    465       struct cardbus_attach_args ca;
    466 
    467       tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    468 
    469       id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    470       class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    471       cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    472 
    473       /* Invalid vendor ID value? */
    474       if (CARDBUS_VENDOR(id) == 0xffff)
    475 	  continue;
    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       /* we need to allocate the ct here, since we might
    483 	 need it when reading the CIS */
    484       if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
    485 						  M_DEVBUF, M_NOWAIT))) {
    486 	  panic("no room for cardbus_tag");
    487       }
    488 
    489       ct->ct_cc = sc->sc_cc;
    490       ct->ct_cf = sc->sc_cf;
    491       ct->ct_bus = sc->sc_bus;
    492       ct->ct_dev = sc->sc_device;
    493       ct->ct_func = function;
    494       ct->ct_sc = sc;
    495       ct->ct_next = NULL;
    496       *previous_next = ct;
    497 
    498       ca.ca_unit = sc->sc_dev.dv_unit;
    499       ca.ca_ct = ct;
    500 
    501       ca.ca_iot = sc->sc_iot;
    502       ca.ca_memt = sc->sc_memt;
    503       ca.ca_dmat = sc->sc_dmat;
    504 
    505       ca.ca_tag = tag;
    506       ca.ca_device = sc->sc_device;
    507       ca.ca_function = function;
    508       ca.ca_id = id;
    509       ca.ca_class = class;
    510 
    511       ca.ca_intrline = sc->sc_intrline;
    512 
    513       bzero(tuple, 2048);
    514 
    515       if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    516 	  printf("cardbus_attach_card: failed to read CIS\n");
    517 	  free(ct, M_DEVBUF);
    518 	  disable_function(sc, function);
    519 	  continue;
    520       }
    521 
    522 #ifdef CARDBUS_DEBUG
    523       decode_tuples(tuple, 2048, print_tuple, NULL);
    524 #endif
    525       decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    526 
    527 
    528       if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
    529 	  /* do not match */
    530 	  disable_function(sc, function);
    531 	  free(ct, M_DEVBUF);
    532 	  *previous_next = NULL;
    533       } else {
    534 	  /* found */
    535 	  previous_next = &(ct->ct_next);
    536 	  ct->ct_device = csc;
    537 	  ++no_work_funcs;
    538       }
    539   }
    540   /* XXX power down pseudo function 8 (this will power down the card
    541      if no functions were attached) */
    542   disable_function(sc, 8);
    543 
    544   if(no_work_funcs == 0)
    545       free(cc, M_DEVBUF);
    546   return no_work_funcs;
    547 }
    548 
    549 
    550 static int
    551 #ifdef __BROKEN_INDIRECT_CONFIG
    552 cardbussubmatch(parent, match, aux)
    553 #else
    554 cardbussubmatch(parent, cf, aux)
    555 #endif
    556      struct device *parent;
    557 #ifdef __BROKEN_INDIRECT_CONFIG
    558      void *match;
    559 #else
    560      struct cfdata *cf;
    561 #endif
    562      void *aux;
    563 {
    564 #ifdef __BROKEN_INDIRECT_CONFIG
    565   struct cfdata *cf = match;
    566 #endif
    567   struct cardbus_attach_args *ca = aux;
    568 
    569   if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    570       cf->cardbuscf_dev != ca->ca_unit) {
    571     return 0;
    572   }
    573   if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    574       cf->cardbuscf_function != ca->ca_function) {
    575     return 0;
    576   }
    577 
    578   return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    579 }
    580 
    581 
    582 
    583 static int
    584 cardbusprint(aux, pnp)
    585      void *aux;
    586      const char *pnp;
    587 {
    588     struct cardbus_attach_args *ca = aux;
    589     char devinfo[256];
    590     int i;
    591     if (pnp) {
    592 	pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    593 	for (i = 0; i < 4; i++) {
    594 	    if (ca->ca_cis.cis1_info[i] == NULL)
    595 		break;
    596 	    if (i)
    597 		printf(", ");
    598 	    printf("%s", ca->ca_cis.cis1_info[i]);
    599 	}
    600 	if (i)
    601 	    printf(" ");
    602 	printf("(manufacturer 0x%x, product 0x%x)", ca->ca_cis.manufacturer,
    603 	       ca->ca_cis.product);
    604 	printf(" %s at %s", devinfo, pnp);
    605     }
    606     printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    607 
    608     return UNCONF;
    609 }
    610 
    611 
    612 
    613 
    614 
    615 
    616 /*
    617  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    618  *   Interrupt handler of pccard.
    619  *  args:
    620  *   cardbus_chipset_tag_t *cc
    621  *   int irq:
    622  */
    623 void *
    624 cardbus_intr_establish(cc, cf, irq, level, func, arg)
    625      cardbus_chipset_tag_t cc;
    626      cardbus_function_tag_t cf;
    627      cardbus_intr_handle_t irq;
    628      int level;
    629      int (*func) __P((void *));
    630      void *arg;
    631 {
    632   DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    633 
    634   return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
    635 }
    636 
    637 
    638 
    639 /*
    640  * void cardbus_intr_disestablish(cc, cf, handler)
    641  *   Interrupt handler of pccard.
    642  *  args:
    643  *   cardbus_chipset_tag_t *cc
    644  */
    645 void
    646 cardbus_intr_disestablish(cc, cf, handler)
    647      cardbus_chipset_tag_t cc;
    648      cardbus_function_tag_t cf;
    649      void *handler;
    650 {
    651   DPRINTF(("- pccard_intr_disestablish\n"));
    652 
    653  (*cf->cardbus_intr_disestablish)(cc, handler);
    654   return;
    655 }
    656 
    657 
    658 
    659 /* XXX this should be merged with cardbus_function_{enable,disable},
    660    but we don't have a ct when these functions are called */
    661 
    662 static void
    663 enable_function(sc, cdstatus, function)
    664      struct cardbus_softc *sc;
    665      int cdstatus;
    666      int function;
    667 {
    668     if(sc->sc_poweron_func == 0) {
    669 	if (cdstatus & CARDBUS_3V_CARD) {
    670 	    sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
    671 	}
    672 	(sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    673     }
    674     sc->sc_poweron_func |= (1 << function);
    675 }
    676 
    677 static void
    678 disable_function(sc, function)
    679      struct cardbus_softc *sc;
    680      int function;
    681 {
    682     sc->sc_poweron_func &= ~(1 << function);
    683     if(sc->sc_poweron_func == 0) {
    684 	/* power-off because no functions are enabled */
    685 	sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
    686     }
    687 }
    688 
    689 /*
    690  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    691  *
    692  *   This function enables a function on a card.  When no power is
    693  *  applied on the card, power will be applied on it.
    694  */
    695 int
    696 cardbus_function_enable(sc, func)
    697      struct cardbus_softc *sc;
    698      int func;
    699 {
    700   cardbus_chipset_tag_t cc = sc->sc_cc;
    701   cardbus_function_tag_t cf = sc->sc_cf;
    702   cardbusreg_t command;
    703   cardbustag_t tag;
    704 
    705   DPRINTF(("entering cardbus_function_enable...  "));
    706 
    707   /* entering critical area */
    708 
    709   enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
    710 
    711   /* exiting critical area */
    712 
    713   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    714 
    715   command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    716   command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    717 
    718   cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    719 
    720   cardbus_free_tag(cc, cf, tag);
    721 
    722   DPRINTF(("%x\n", sc->sc_poweron_func));
    723 
    724   return 0;
    725 }
    726 
    727 
    728 /*
    729  * int cardbus_function_disable(struct cardbus_softc *, int func)
    730  *
    731  *   This function disable a function on a card.  When no functions are
    732  *  enabled, it turns off the power.
    733  */
    734 int
    735 cardbus_function_disable(sc, func)
    736      struct cardbus_softc *sc;
    737      int func;
    738 {
    739 
    740   DPRINTF(("entering cardbus_function_disable...  "));
    741 
    742   disable_function(sc, func);
    743 
    744   return 0;
    745 }
    746 
    747 
    748 
    749 
    750 
    751 
    752 
    753 /*
    754  * below this line, there are some functions for decoding tuples.
    755  * They should go out from this file.
    756  */
    757 
    758 static u_int8_t *
    759 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
    760 
    761 static int
    762 decode_tuples(tuple, buflen, func, data)
    763      u_int8_t *tuple;
    764      int buflen;
    765      tuple_decode_func func;
    766      void *data;
    767 {
    768   u_int8_t *tp = tuple;
    769 
    770   if (CISTPL_LINKTARGET != *tuple) {
    771     DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    772     return 0;
    773   }
    774 
    775   while (NULL != (tp = decode_tuple(tp, func, data))) {
    776     if (tuple + buflen < tp) {
    777       break;
    778     }
    779   }
    780 
    781   return 1;
    782 }
    783 
    784 
    785 static u_int8_t *
    786 decode_tuple(tuple, func, data)
    787      u_int8_t *tuple;
    788      tuple_decode_func func;
    789      void *data;
    790 {
    791     u_int8_t type;
    792     u_int8_t len;
    793 
    794     type = tuple[0];
    795     len = tuple[1] + 2;
    796 
    797     (*func)(tuple, len, data);
    798 
    799     if (CISTPL_END == type) {
    800 	return NULL;
    801     }
    802 
    803     return tuple + len;
    804 }
    805 
    806 
    807 #ifdef CARDBUS_DEBUG
    808 static char *tuple_name __P((int type));
    809 
    810 static char *
    811 tuple_name(type)
    812      int type;
    813 {
    814   static char *tuple_name_s [] = {
    815     "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    816     "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    817     "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    818     "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    819     "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET",	/* 10-13 */
    820     "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    821     "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    822     "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    823     "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    824     "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    825     "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    826     "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    827     "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    828     "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    829     "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    830     "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    831     "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    832     "DATE", "BATTERY", "ORG"
    833   };
    834 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    835 
    836   if (type > 0 && type < NAME_LEN(tuple_name_s)) {
    837     return tuple_name_s[type];
    838   } else if (0xff == type) {
    839     return "END";
    840   } else {
    841     return "Reserved";
    842   }
    843 }
    844 
    845 static void
    846 print_tuple(tuple, len, data)
    847      u_int8_t *tuple;
    848      int len;
    849      void *data;
    850 {
    851     int i;
    852 
    853     printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
    854 
    855     for (i = 0; i < len; ++i) {
    856 	if (i % 16 == 0) {
    857 	    printf("  0x%2x:", i);
    858 	}
    859 	printf(" %x",tuple[i]);
    860 	if (i % 16 == 15) {
    861 	    printf("\n");
    862 	}
    863     }
    864     if (i % 16 != 0) {
    865 	printf("\n");
    866     }
    867 }
    868 
    869 #endif
    870 
    871