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