Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.18
      1 /*	$NetBSD: cardbus.c,v 1.18 2000/01/26 09:04:59 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 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/cardbus/cardbusvar.h>
     48 #include <dev/cardbus/cardbusdevs.h>
     49 #include <dev/cardbus/pccardcis.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> /* XXX */
     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 
     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) == CARDBUS_VENDOR_INVALID) {
    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     memset(&ca, 0, sizeof(ca));
    510 
    511     ca.ca_unit = sc->sc_dev.dv_unit;
    512     ca.ca_ct = ct;
    513 
    514     ca.ca_iot = sc->sc_iot;
    515     ca.ca_memt = sc->sc_memt;
    516     ca.ca_dmat = sc->sc_dmat;
    517 
    518     ca.ca_tag = tag;
    519     ca.ca_device = sc->sc_device;
    520     ca.ca_function = function;
    521     ca.ca_id = id;
    522     ca.ca_class = class;
    523 
    524     ca.ca_intrline = sc->sc_intrline;
    525 
    526     bzero(tuple, 2048);
    527 
    528     if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    529       printf("cardbus_attach_card: failed to read CIS\n");
    530     } else {
    531 #ifdef CARDBUS_DEBUG
    532       decode_tuples(tuple, 2048, print_tuple, NULL);
    533 #endif
    534       decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    535     }
    536 
    537     if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
    538       /* do not match */
    539       disable_function(sc, function);
    540       free(ct, M_DEVBUF);
    541       *previous_next = NULL;
    542     } else {
    543       /* found */
    544       previous_next = &(ct->ct_next);
    545       ct->ct_device = csc;
    546       ++no_work_funcs;
    547     }
    548   }
    549   /*
    550    * XXX power down pseudo function 8 (this will power down the card
    551    * if no functions were attached).
    552    */
    553   disable_function(sc, 8);
    554 
    555   return no_work_funcs;
    556 }
    557 
    558 
    559 static int
    560 #ifdef __BROKEN_INDIRECT_CONFIG
    561 cardbussubmatch(parent, match, aux)
    562 #else
    563 cardbussubmatch(parent, cf, aux)
    564 #endif
    565      struct device *parent;
    566 #ifdef __BROKEN_INDIRECT_CONFIG
    567      void *match;
    568 #else
    569      struct cfdata *cf;
    570 #endif
    571      void *aux;
    572 {
    573 #ifdef __BROKEN_INDIRECT_CONFIG
    574   struct cfdata *cf = match;
    575 #endif
    576   struct cardbus_attach_args *ca = aux;
    577 
    578   if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    579       cf->cardbuscf_dev != ca->ca_unit) {
    580     return 0;
    581   }
    582   if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    583       cf->cardbuscf_function != ca->ca_function) {
    584     return 0;
    585   }
    586 
    587   return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    588 }
    589 
    590 
    591 
    592 static int
    593 cardbusprint(aux, pnp)
    594      void *aux;
    595      const char *pnp;
    596 {
    597     struct cardbus_attach_args *ca = aux;
    598     char devinfo[256];
    599     int i;
    600     if (pnp) {
    601 	pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    602 	for (i = 0; i < 4; i++) {
    603 	    if (ca->ca_cis.cis1_info[i] == NULL)
    604 		break;
    605 	    if (i)
    606 		printf(", ");
    607 	    printf("%s", ca->ca_cis.cis1_info[i]);
    608 	}
    609 	if (i)
    610 	    printf(" ");
    611 	printf("(manufacturer 0x%x, product 0x%x)", ca->ca_cis.manufacturer,
    612 	       ca->ca_cis.product);
    613 	printf(" %s at %s", devinfo, pnp);
    614     }
    615     printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    616 
    617     return UNCONF;
    618 }
    619 
    620 
    621 
    622 
    623 
    624 
    625 /*
    626  * void cardbus_detach_card(struct cardbus_softc *sc)
    627  *
    628  *    This function detaches the card on the slot: detach device data
    629  *    structure and turns off the power.
    630  *
    631  *    This function must not be called under interrupt context.
    632  */
    633 void
    634 cardbus_detach_card(sc)
    635      struct cardbus_softc *sc;
    636 {
    637     struct cardbus_devfunc *ct, *ct_next, **prev_next;
    638 
    639     prev_next = &(sc->sc_funcs->ct_next);
    640 
    641     for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
    642 	struct device *fndev = ct->ct_device;
    643 	ct_next = ct->ct_next;
    644 
    645 	printf("%s: detaching %s\n", sc->sc_dev.dv_xname, fndev->dv_xname);
    646 	/* call device detach function */
    647 
    648 
    649 	if (0 != config_detach(fndev, 0)) {
    650 	    printf("%s: cannot detaching dev %s, function %d\n",
    651 		   sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
    652 	    prev_next = &(ct->ct_next);
    653 	} else {
    654 	    sc->sc_poweron_func &= ~(1 << ct->ct_func);
    655 	    *prev_next = ct->ct_next;
    656 	    free(ct, M_DEVBUF);
    657 	}
    658     }
    659 }
    660 
    661 
    662 
    663 
    664 /*
    665  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    666  *   Interrupt handler of pccard.
    667  *  args:
    668  *   cardbus_chipset_tag_t *cc
    669  *   int irq:
    670  */
    671 void *
    672 cardbus_intr_establish(cc, cf, irq, level, func, arg)
    673      cardbus_chipset_tag_t cc;
    674      cardbus_function_tag_t cf;
    675      cardbus_intr_handle_t irq;
    676      int level;
    677      int (*func) __P((void *));
    678      void *arg;
    679 {
    680   DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    681 
    682   return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
    683 }
    684 
    685 
    686 
    687 /*
    688  * void cardbus_intr_disestablish(cc, cf, handler)
    689  *   Interrupt handler of pccard.
    690  *  args:
    691  *   cardbus_chipset_tag_t *cc
    692  */
    693 void
    694 cardbus_intr_disestablish(cc, cf, handler)
    695      cardbus_chipset_tag_t cc;
    696      cardbus_function_tag_t cf;
    697      void *handler;
    698 {
    699   DPRINTF(("- pccard_intr_disestablish\n"));
    700 
    701  (*cf->cardbus_intr_disestablish)(cc, handler);
    702   return;
    703 }
    704 
    705 
    706 
    707 /* XXX this should be merged with cardbus_function_{enable,disable},
    708    but we don't have a ct when these functions are called */
    709 
    710 static void
    711 enable_function(sc, cdstatus, function)
    712      struct cardbus_softc *sc;
    713      int cdstatus;
    714      int function;
    715 {
    716 
    717     if(sc->sc_poweron_func == 0) {
    718 	if (cdstatus & CARDBUS_3V_CARD) {
    719 	    sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
    720 	}
    721 	(sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    722     }
    723     sc->sc_poweron_func |= (1 << function);
    724 }
    725 
    726 static void
    727 disable_function(sc, function)
    728      struct cardbus_softc *sc;
    729      int function;
    730 {
    731 
    732     sc->sc_poweron_func &= ~(1 << function);
    733     if(sc->sc_poweron_func == 0) {
    734 	/* power-off because no functions are enabled */
    735 	sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
    736     }
    737 }
    738 
    739 /*
    740  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    741  *
    742  *   This function enables a function on a card.  When no power is
    743  *  applied on the card, power will be applied on it.
    744  */
    745 int
    746 cardbus_function_enable(sc, func)
    747      struct cardbus_softc *sc;
    748      int func;
    749 {
    750   cardbus_chipset_tag_t cc = sc->sc_cc;
    751   cardbus_function_tag_t cf = sc->sc_cf;
    752   cardbusreg_t command;
    753   cardbustag_t tag;
    754 
    755   DPRINTF(("entering cardbus_function_enable...  "));
    756 
    757   /* entering critical area */
    758 
    759   enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
    760 
    761   /* exiting critical area */
    762 
    763   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    764 
    765   command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    766   command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    767 
    768   cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    769 
    770   cardbus_free_tag(cc, cf, tag);
    771 
    772   DPRINTF(("%x\n", sc->sc_poweron_func));
    773 
    774   return 0;
    775 }
    776 
    777 
    778 /*
    779  * int cardbus_function_disable(struct cardbus_softc *, int func)
    780  *
    781  *   This function disable a function on a card.  When no functions are
    782  *  enabled, it turns off the power.
    783  */
    784 int
    785 cardbus_function_disable(sc, func)
    786      struct cardbus_softc *sc;
    787      int func;
    788 {
    789 
    790   DPRINTF(("entering cardbus_function_disable...  "));
    791 
    792   disable_function(sc, func);
    793 
    794   return 0;
    795 }
    796 
    797 
    798 /*
    799  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    800  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    801  *	cardbusreg_t *value)
    802  *
    803  *	Find the specified PCI capability.
    804  */
    805 int
    806 cardbus_get_capability(cc, cf, tag, capid, offset, value)
    807 	cardbus_chipset_tag_t cc;
    808 	cardbus_function_tag_t cf;
    809 	cardbustag_t tag;
    810 	int capid;
    811 	int *offset;
    812 	cardbusreg_t *value;
    813 {
    814 	cardbusreg_t reg;
    815 	unsigned int ofs;
    816 
    817 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    818 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    819 		return (0);
    820 
    821 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    822 	    PCI_CAPLISTPTR_REG));
    823 	while (ofs != 0) {
    824 #ifdef DIAGNOSTIC
    825 		if ((ofs & 3) || (ofs < 0x40))
    826 			panic("cardbus_get_capability");
    827 #endif
    828 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    829 		if (PCI_CAPLIST_CAP(reg) == capid) {
    830 			if (offset)
    831 				*offset = ofs;
    832 			if (value)
    833 				*value = reg;
    834 			return (1);
    835 		}
    836 		ofs = PCI_CAPLIST_NEXT(reg);
    837 	}
    838 
    839 	return (0);
    840 }
    841 
    842 
    843 /*
    844  * below this line, there are some functions for decoding tuples.
    845  * They should go out from this file.
    846  */
    847 
    848 static u_int8_t *
    849 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
    850 
    851 static int
    852 decode_tuples(tuple, buflen, func, data)
    853      u_int8_t *tuple;
    854      int buflen;
    855      tuple_decode_func func;
    856      void *data;
    857 {
    858   u_int8_t *tp = tuple;
    859 
    860   if (CISTPL_LINKTARGET != *tuple) {
    861     DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    862     return 0;
    863   }
    864 
    865   while (NULL != (tp = decode_tuple(tp, func, data))) {
    866     if (tuple + buflen < tp) {
    867       break;
    868     }
    869   }
    870 
    871   return 1;
    872 }
    873 
    874 
    875 static u_int8_t *
    876 decode_tuple(tuple, func, data)
    877      u_int8_t *tuple;
    878      tuple_decode_func func;
    879      void *data;
    880 {
    881     u_int8_t type;
    882     u_int8_t len;
    883 
    884     type = tuple[0];
    885     len = tuple[1] + 2;
    886 
    887     (*func)(tuple, len, data);
    888 
    889     if (CISTPL_END == type) {
    890 	return NULL;
    891     }
    892 
    893     return tuple + len;
    894 }
    895 
    896 
    897 #ifdef CARDBUS_DEBUG
    898 static char *tuple_name __P((int type));
    899 
    900 static char *
    901 tuple_name(type)
    902      int type;
    903 {
    904   static char *tuple_name_s [] = {
    905     "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    906     "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    907     "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    908     "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    909     "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET",	/* 10-13 */
    910     "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    911     "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    912     "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    913     "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    914     "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    915     "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    916     "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    917     "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    918     "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    919     "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    920     "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    921     "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    922     "DATE", "BATTERY", "ORG"
    923   };
    924 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    925 
    926   if (type > 0 && type < NAME_LEN(tuple_name_s)) {
    927     return tuple_name_s[type];
    928   } else if (0xff == type) {
    929     return "END";
    930   } else {
    931     return "Reserved";
    932   }
    933 }
    934 
    935 static void
    936 print_tuple(tuple, len, data)
    937      u_int8_t *tuple;
    938      int len;
    939      void *data;
    940 {
    941     int i;
    942 
    943     printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
    944 
    945     for (i = 0; i < len; ++i) {
    946 	if (i % 16 == 0) {
    947 	    printf("  0x%2x:", i);
    948 	}
    949 	printf(" %x",tuple[i]);
    950 	if (i % 16 == 15) {
    951 	    printf("\n");
    952 	}
    953     }
    954     if (i % 16 != 0) {
    955 	printf("\n");
    956     }
    957 }
    958 
    959 #endif
    960 
    961