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