LCOV - code coverage report
Current view: top level - dev/cardbus - cardbus.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 324 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13