The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/pccard/pccard.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $     */
    2 /* $FreeBSD: releng/5.1/sys/dev/pccard/pccard.c 113313 2003-04-10 04:11:15Z imp $ */
    3 
    4 /*
    5  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *      This product includes software developed by Marc Horowitz.
   18  * 4. The name of the author may not be used to endorse or promote products
   19  *    derived from this software without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/malloc.h>
   36 #include <sys/module.h>
   37 #include <sys/kernel.h>
   38 #include <sys/queue.h>
   39 #include <sys/sysctl.h>
   40 #include <sys/types.h>
   41 
   42 #include <sys/bus.h>
   43 #include <machine/bus.h>
   44 #include <sys/rman.h>
   45 #include <machine/resource.h>
   46 
   47 #include <net/ethernet.h>
   48 
   49 #include <dev/pccard/pccardreg.h>
   50 #include <dev/pccard/pccardvar.h>
   51 
   52 #include "power_if.h"
   53 #include "card_if.h"
   54 
   55 #define PCCARDDEBUG
   56 
   57 /* sysctl vars */
   58 SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
   59 
   60 int     pccard_debug = 0;
   61 TUNABLE_INT("hw.pccard.debug", &pccard_debug);
   62 SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
   63     &pccard_debug, 0,
   64   "pccard debug");
   65 
   66 int     pccard_cis_debug = 0;
   67 TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
   68 SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
   69     &pccard_cis_debug, 0, "pccard CIS debug");
   70 
   71 #ifdef PCCARDDEBUG
   72 #define DPRINTF(arg) if (pccard_debug) printf arg
   73 #define DEVPRINTF(arg) if (pccard_debug) device_printf arg
   74 #define PRVERBOSE(arg) printf arg
   75 #define DEVPRVERBOSE(arg) device_printf arg
   76 #else
   77 #define DPRINTF(arg)
   78 #define DEVPRINTF(arg)
   79 #define PRVERBOSE(arg) if (bootverbose) printf arg
   80 #define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
   81 #endif
   82 
   83 static int      pccard_ccr_read(struct pccard_function *pf, int ccr);
   84 static void     pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
   85 static int      pccard_attach_card(device_t dev);
   86 static int      pccard_detach_card(device_t dev);
   87 static int      pccard_card_gettype(device_t dev, int *type);
   88 static void     pccard_function_init(struct pccard_function *pf);
   89 static void     pccard_function_free(struct pccard_function *pf);
   90 static int      pccard_function_enable(struct pccard_function *pf);
   91 static void     pccard_function_disable(struct pccard_function *pf);
   92 static int      pccard_compat_do_probe(device_t bus, device_t dev);
   93 static int      pccard_compat_do_attach(device_t bus, device_t dev);
   94 static int      pccard_add_children(device_t dev, int busno);
   95 static int      pccard_probe(device_t dev);
   96 static int      pccard_attach(device_t dev);
   97 static int      pccard_detach(device_t dev);
   98 static void     pccard_print_resources(struct resource_list *rl,
   99                     const char *name, int type, int count, const char *format);
  100 static int      pccard_print_child(device_t dev, device_t child);
  101 static int      pccard_set_resource(device_t dev, device_t child, int type,
  102                     int rid, u_long start, u_long count);
  103 static int      pccard_get_resource(device_t dev, device_t child, int type,
  104                     int rid, u_long *startp, u_long *countp);
  105 static void     pccard_delete_resource(device_t dev, device_t child, int type,
  106                     int rid);
  107 static int      pccard_set_res_flags(device_t dev, device_t child, int type,
  108                     int rid, u_int32_t flags);
  109 static int      pccard_set_memory_offset(device_t dev, device_t child, int rid,
  110                     u_int32_t offset, u_int32_t *deltap);
  111 static void     pccard_probe_nomatch(device_t cbdev, device_t child);
  112 static int      pccard_read_ivar(device_t bus, device_t child, int which,
  113                     u_char *result);
  114 static void     pccard_driver_added(device_t dev, driver_t *driver);
  115 static struct resource *pccard_alloc_resource(device_t dev,
  116                     device_t child, int type, int *rid, u_long start,
  117                     u_long end, u_long count, u_int flags);
  118 static int      pccard_release_resource(device_t dev, device_t child, int type,
  119                     int rid, struct resource *r);
  120 static void     pccard_child_detached(device_t parent, device_t dev);
  121 static void     pccard_intr(void *arg);
  122 static int      pccard_setup_intr(device_t dev, device_t child,
  123                     struct resource *irq, int flags, driver_intr_t *intr,
  124                     void *arg, void **cookiep);
  125 static int      pccard_teardown_intr(device_t dev, device_t child,
  126                     struct resource *r, void *cookie);
  127 
  128 static const struct pccard_product *
  129 pccard_do_product_lookup(device_t bus, device_t dev,
  130                          const struct pccard_product *tab, size_t ent_size,
  131                          pccard_product_match_fn matchfn);
  132 
  133 
  134 static int
  135 pccard_ccr_read(struct pccard_function *pf, int ccr)
  136 {
  137         return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
  138             pf->pf_ccr_offset + ccr));
  139 }
  140 
  141 static void
  142 pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
  143 {
  144         if ((pf->ccr_mask) & (1 << (ccr / 2))) {
  145                 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
  146                     pf->pf_ccr_offset + ccr, val);
  147         }
  148 }
  149 
  150 static int
  151 pccard_set_default_descr(device_t dev)
  152 {
  153         char *vendorstr, *prodstr, *str;
  154 
  155         if (pccard_get_vendor_str(dev, &vendorstr))
  156                 return (0);
  157         if (pccard_get_product_str(dev, &prodstr))
  158                 return (0);
  159         str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
  160             M_WAITOK);
  161         sprintf(str, "%s %s", vendorstr, prodstr);
  162         device_set_desc_copy(dev, str);
  163         free(str, M_DEVBUF);
  164         return (0);
  165 }
  166 
  167 static int
  168 pccard_attach_card(device_t dev)
  169 {
  170         struct pccard_softc *sc = PCCARD_SOFTC(dev);
  171         struct pccard_function *pf;
  172         struct pccard_ivar *ivar;
  173         device_t child;
  174         int i;
  175 
  176         /*
  177          * this is here so that when socket_enable calls gettype, trt happens
  178          */
  179         STAILQ_INIT(&sc->card.pf_head);
  180 
  181         DEVPRINTF((dev, "chip_socket_enable\n"));
  182         POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
  183 
  184         DEVPRINTF((dev, "read_cis\n"));
  185         pccard_read_cis(sc);
  186 
  187         DEVPRINTF((dev, "check_cis_quirks\n"));
  188         pccard_check_cis_quirks(dev);
  189 
  190         /*
  191          * bail now if the card has no functions, or if there was an error in
  192          * the cis.
  193          */
  194 
  195         if (sc->card.error) {
  196                 device_printf(dev, "CARD ERROR!\n");
  197                 return (1);
  198         }
  199         if (STAILQ_EMPTY(&sc->card.pf_head)) {
  200                 device_printf(dev, "Card has no functions!\n");
  201                 return (1);
  202         }
  203 
  204         if (bootverbose || pccard_debug)
  205                 pccard_print_cis(dev);
  206 
  207         DEVPRINTF((dev, "functions scanning\n"));
  208         i = -1;
  209         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
  210                 i++;
  211                 if (STAILQ_EMPTY(&pf->cfe_head)) {
  212                         device_printf(dev,
  213                             "Function %d has no config entries.!\n", i);
  214                         continue;
  215                 }
  216                 pf->sc = sc;
  217                 pf->cfe = NULL;
  218                 pf->dev = NULL;
  219         }
  220         DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
  221             pccard_mfc(sc)));
  222 
  223         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
  224                 if (STAILQ_EMPTY(&pf->cfe_head))
  225                         continue;
  226                 /*
  227                  * In NetBSD, the drivers are responsible for activating
  228                  * each function of a card.  I think that in FreeBSD we
  229                  * want to activate them enough for the usual bus_*_resource
  230                  * routines will do the right thing.  This many mean a
  231                  * departure from the current NetBSD model.
  232                  *
  233                  * This seems to work well in practice for most cards.
  234                  * However, there are two cases that are problematic.
  235                  * If a driver wishes to pick and chose which config
  236                  * entry to use, then this method falls down.  These
  237                  * are usually older cards.  In addition, there are
  238                  * some cards that have multiple hardware units on the
  239                  * cards, but presents only one CIS chain.  These cards
  240                  * are combination cards, but only one of these units
  241                  * can be on at a time.
  242                  */
  243                 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
  244                     M_WAITOK | M_ZERO);
  245                 child = device_add_child(dev, NULL, -1);
  246                 device_set_ivars(child, ivar);
  247                 ivar->fcn = pf;
  248                 pf->dev = child;
  249                 /*
  250                  * XXX We might want to move the next two lines into
  251                  * XXX the pccard interface layer.  For the moment, this
  252                  * XXX is OK, but some drivers want to pick the config
  253                  * XXX entry to use as well as some address tweaks (mostly
  254                  * XXX due to bugs in decode logic that makes some
  255                  * XXX addresses illegal or broken).
  256                  */
  257                 pccard_function_init(pf);
  258                 if (sc->sc_enabled_count == 0)
  259                         POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
  260                 if (pccard_function_enable(pf) == 0 &&
  261                     pccard_set_default_descr(child) == 0 &&
  262                     device_probe_and_attach(child) == 0) {
  263                         DEVPRINTF((sc->dev, "function %d CCR at %d "
  264                             "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
  265                             pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
  266                             pccard_ccr_read(pf, 0x00),
  267                         pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
  268                         pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
  269                         pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
  270                         pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
  271                 } else {
  272                         if (pf->cfe != NULL)
  273                                 pccard_function_disable(pf);
  274                 }
  275         }
  276         return (0);
  277 }
  278 
  279 static int
  280 pccard_detach_card(device_t dev)
  281 {
  282         struct pccard_softc *sc = PCCARD_SOFTC(dev);
  283         struct pccard_function *pf;
  284         struct pccard_config_entry *cfe;
  285 
  286         /*
  287          * We are running on either the PCCARD socket's event thread
  288          * or in user context detaching a device by user request.
  289          */
  290         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
  291                 int state = device_get_state(pf->dev);
  292 
  293                 if (state == DS_ATTACHED || state == DS_BUSY)
  294                         device_detach(pf->dev);
  295                 if (pf->cfe != NULL)
  296                         pccard_function_disable(pf);
  297                 pccard_function_free(pf);
  298                 device_delete_child(dev, pf->dev);
  299         }
  300         if (sc->sc_enabled_count == 0)
  301                 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
  302 
  303         while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
  304                 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
  305                         STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
  306                         free(cfe, M_DEVBUF);
  307                 }
  308                 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
  309                 free(pf, M_DEVBUF);
  310         }
  311         return (0);
  312 }
  313 
  314 static const struct pccard_product *
  315 pccard_do_product_lookup(device_t bus, device_t dev,
  316     const struct pccard_product *tab, size_t ent_size,
  317     pccard_product_match_fn matchfn)
  318 {
  319         const struct pccard_product *ent;
  320         int matches;
  321         u_int32_t fcn;
  322         u_int32_t vendor;
  323         u_int32_t prod;
  324         char *vendorstr;
  325         char *prodstr;
  326 
  327 #ifdef DIAGNOSTIC
  328         if (sizeof *ent > ent_size)
  329                 panic("pccard_product_lookup: bogus ent_size %jd",
  330                     (intmax_t) ent_size);
  331 #endif
  332         if (pccard_get_vendor(dev, &vendor))
  333                 return (NULL);
  334         if (pccard_get_product(dev, &prod))
  335                 return (NULL);
  336         if (pccard_get_function_number(dev, &fcn))
  337                 return (NULL);
  338         if (pccard_get_vendor_str(dev, &vendorstr))
  339                 return (NULL);
  340         if (pccard_get_product_str(dev, &prodstr))
  341                 return (NULL);
  342         for (ent = tab; ent->pp_vendor != 0; ent =
  343             (const struct pccard_product *) ((const char *) ent + ent_size)) {
  344                 matches = 1;
  345                 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
  346                     ent->pp_product == PCCARD_PRODUCT_ANY &&
  347                     ent->pp_cis[0] == NULL &&
  348                     ent->pp_cis[1] == NULL) {
  349                         if (ent->pp_name)
  350                                 device_printf(dev,
  351                                     "Total wildcard entry ignored for %s\n",
  352                                     ent->pp_name);
  353                         continue;
  354                 }
  355                 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
  356                     vendor != ent->pp_vendor)
  357                         matches = 0;
  358                 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
  359                     prod != ent->pp_product)
  360                         matches = 0;
  361                 if (matches && fcn != ent->pp_expfunc)
  362                         matches = 0;
  363                 if (matches && ent->pp_cis[0] &&
  364                     strcmp(ent->pp_cis[0], vendorstr) != 0)
  365                         matches = 0;
  366                 if (matches && ent->pp_cis[1] &&
  367                     strcmp(ent->pp_cis[1], prodstr) != 0)
  368                         matches = 0;
  369                 /* XXX need to match cis[2] and cis[3] also XXX */
  370                 if (matchfn != NULL)
  371                         matches = (*matchfn)(dev, ent, matches);
  372                 if (matches)
  373                         return (ent);
  374         }
  375         return (NULL);
  376 }
  377 
  378 static int
  379 pccard_card_gettype(device_t dev, int *type)
  380 {
  381         struct pccard_softc *sc = PCCARD_SOFTC(dev);
  382         struct pccard_function *pf;
  383 
  384         /*
  385          * set the iftype to memory if this card has no functions (not yet
  386          * probed), or only one function, and that is not initialized yet or
  387          * that is memory.
  388          */
  389         pf = STAILQ_FIRST(&sc->card.pf_head);
  390         if (pf == NULL ||
  391             (STAILQ_NEXT(pf, pf_list) == NULL &&
  392             (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
  393                 *type = PCCARD_IFTYPE_MEMORY;
  394         else
  395                 *type = PCCARD_IFTYPE_IO;
  396         return (0);
  397 }
  398 
  399 /*
  400  * Initialize a PCCARD function.  May be called as long as the function is
  401  * disabled.
  402  *
  403  * Note: pccard_function_init should not keep resources allocated.  It should
  404  * only set them up ala isa pnp, set the values in the rl lists, and return.
  405  * Any resource held after pccard_function_init is called is a bug.  However,
  406  * the bus routines to get the resources also assume that pccard_function_init
  407  * does this, so they need to be fixed too.
  408  */
  409 static void
  410 pccard_function_init(struct pccard_function *pf)
  411 {
  412         struct pccard_config_entry *cfe;
  413         int i;
  414         struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
  415         struct resource_list *rl = &devi->resources;
  416         struct resource_list_entry *rle;
  417         struct resource *r = 0;
  418         device_t bus;
  419         int start;
  420         int end;
  421         int spaces;
  422 
  423         if (pf->pf_flags & PFF_ENABLED) {
  424                 printf("pccard_function_init: function is enabled");
  425                 return;
  426         }
  427         bus = device_get_parent(pf->dev);
  428         /* Remember which configuration entry we are using. */
  429         STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
  430                 for (i = 0; i < cfe->num_iospace; i++)
  431                         cfe->iores[i] = NULL;
  432                 cfe->irqres = NULL;
  433                 spaces = 0;
  434                 for (i = 0; i < cfe->num_iospace; i++) {
  435                         start = cfe->iospace[i].start;
  436                         if (start)
  437                                 end = start + cfe->iospace[i].length - 1;
  438                         else
  439                                 end = ~0;
  440                         cfe->iorid[i] = i;
  441                         DEVPRINTF((bus, "I/O rid %d start %x end %x\n",
  442                             i, start, end));
  443                         r = cfe->iores[i] = bus_alloc_resource(bus,
  444                             SYS_RES_IOPORT, &cfe->iorid[i], start, end,
  445                             cfe->iospace[i].length,
  446                             rman_make_alignment_flags(cfe->iospace[i].length));
  447                         if (cfe->iores[i] == NULL)
  448                                 goto not_this_one;
  449                         resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
  450                             rman_get_start(r), rman_get_end(r),
  451                             cfe->iospace[i].length);
  452                         rle = resource_list_find(rl, SYS_RES_IOPORT,
  453                             cfe->iorid[i]);
  454                         rle->res = r;
  455                         spaces++;
  456                 }
  457                 if (cfe->num_memspace > 0) {
  458                         /*
  459                          * Not implement yet, Fix me.
  460                          */
  461                         DEVPRINTF((bus, "Memory space not yet implemented.\n"));
  462                 }
  463                 if (spaces == 0) {
  464                         DEVPRINTF((bus, "Neither memory nor I/O mampped\n"));
  465                         goto not_this_one;
  466                 }
  467                 if (cfe->irqmask) {
  468                         cfe->irqrid = 0;
  469                         r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
  470                             &cfe->irqrid, 0, ~0, 1, 0);
  471                         if (cfe->irqres == NULL)
  472                                 goto not_this_one;
  473                         resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
  474                             rman_get_start(r), rman_get_end(r), 1);
  475                         rle = resource_list_find(rl, SYS_RES_IRQ,
  476                             cfe->irqrid);
  477                         rle->res = r;
  478                 }
  479                 /* If we get to here, we've allocated all we need */
  480                 pf->cfe = cfe;
  481                 break;
  482             not_this_one:;
  483                 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
  484                     cfe->number));
  485                 /*
  486                  * Release resources that we partially allocated
  487                  * from this config entry.
  488                  */
  489                 for (i = 0; i < cfe->num_iospace; i++) {
  490                         if (cfe->iores[i] != NULL) {
  491                                 bus_release_resource(bus, SYS_RES_IOPORT,
  492                                     cfe->iorid[i], cfe->iores[i]);
  493                                 rle = resource_list_find(rl, SYS_RES_IOPORT,
  494                                     cfe->iorid[i]);
  495                                 rle->res = NULL;
  496                                 resource_list_delete(rl, SYS_RES_IOPORT,
  497                                     cfe->iorid[i]);
  498                         }
  499                         cfe->iores[i] = NULL;
  500                 }
  501                 if (cfe->irqmask && cfe->irqres != NULL) {
  502                         bus_release_resource(bus, SYS_RES_IRQ,
  503                             cfe->irqrid, cfe->irqres);
  504                         rle = resource_list_find(rl, SYS_RES_IRQ,
  505                             cfe->irqrid);
  506                         rle->res = NULL;
  507                         resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
  508                         cfe->irqres = NULL;
  509                 }
  510         }
  511 }
  512 
  513 /*
  514  * Free resources allocated by pccard_function_init(), May be called as long
  515  * as the function is disabled.
  516  *
  517  * NOTE: This function should be unnecessary.  pccard_function_init should
  518  * never keep resources initialized.
  519  */
  520 static void
  521 pccard_function_free(struct pccard_function *pf)
  522 {
  523         struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
  524         struct resource_list_entry *rle;
  525 
  526         if (pf->pf_flags & PFF_ENABLED) {
  527                 printf("pccard_function_init: function is enabled");
  528                 return;
  529         }
  530 
  531         SLIST_FOREACH(rle, &devi->resources, link) {
  532                 if (rle->res) {
  533                         if (rman_get_device(rle->res) != pf->sc->dev)
  534                                 device_printf(pf->sc->dev,
  535                                     "function_free: Resource still owned by "
  536                                     "child, oops. "
  537                                     "(type=%d, rid=%d, addr=%lx)\n",
  538                                     rle->type, rle->rid,
  539                                     rman_get_start(rle->res));
  540                         BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
  541                             pf->sc->dev, rle->type, rle->rid, rle->res);
  542                         rle->res = NULL;
  543                 }
  544         }
  545         resource_list_free(&devi->resources);
  546 }
  547 
  548 /* Enable a PCCARD function */
  549 static int
  550 pccard_function_enable(struct pccard_function *pf)
  551 {
  552         struct pccard_function *tmp;
  553         int reg;
  554         device_t dev = pf->sc->dev;
  555 
  556         if (pf->cfe == NULL) {
  557                 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
  558                 return (ENOMEM);
  559         }
  560 
  561         /*
  562          * Increase the reference count on the socket, enabling power, if
  563          * necessary.
  564          */
  565         pf->sc->sc_enabled_count++;
  566 
  567         if (pf->pf_flags & PFF_ENABLED) {
  568                 /*
  569                  * Don't do anything if we're already enabled.
  570                  */
  571                 return (0);
  572         }
  573 
  574         /*
  575          * it's possible for different functions' CCRs to be in the same
  576          * underlying page.  Check for that.
  577          */
  578         STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
  579                 if ((tmp->pf_flags & PFF_ENABLED) &&
  580                     (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
  581                     ((pf->ccr_base + PCCARD_CCR_SIZE) <=
  582                     (tmp->ccr_base - tmp->pf_ccr_offset +
  583                     tmp->pf_ccr_realsize))) {
  584                         pf->pf_ccrt = tmp->pf_ccrt;
  585                         pf->pf_ccrh = tmp->pf_ccrh;
  586                         pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
  587 
  588                         /*
  589                          * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
  590                          * tmp->ccr_base) + pf->ccr_base;
  591                          */
  592                         /* pf->pf_ccr_offset =
  593                             (tmp->pf_ccr_offset + pf->ccr_base) -
  594                             tmp->ccr_base; */
  595                         pf->pf_ccr_window = tmp->pf_ccr_window;
  596                         break;
  597                 }
  598         }
  599         if (tmp == NULL) {
  600                 pf->ccr_rid = 0;
  601                 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
  602                     &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
  603                 if (!pf->ccr_res)
  604                         goto bad;
  605                 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
  606                     rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
  607                     pf->ccr_base));
  608                 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
  609                     pf->ccr_rid, PCCARD_A_MEM_ATTR);
  610                 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
  611                     pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
  612                 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
  613                 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
  614                 pf->pf_ccr_realsize = 1;
  615         }
  616 
  617         reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
  618         reg |= PCCARD_CCR_OPTION_LEVIREQ;
  619         if (pccard_mfc(pf->sc)) {
  620                 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
  621                         PCCARD_CCR_OPTION_ADDR_DECODE);
  622                 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
  623         }
  624         pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
  625 
  626         reg = 0;
  627         if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
  628                 reg |= PCCARD_CCR_STATUS_IOIS8;
  629         if (pf->cfe->flags & PCCARD_CFE_AUDIO)
  630                 reg |= PCCARD_CCR_STATUS_AUDIO;
  631         pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
  632 
  633         pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
  634 
  635         if (pccard_mfc(pf->sc)) {
  636                 long tmp, iosize;
  637 
  638                 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
  639                 /* round up to nearest (2^n)-1 */
  640                 for (iosize = 1; iosize < tmp; iosize <<= 1)
  641                         ;
  642                 iosize--;
  643 
  644                 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
  645                                  pf->pf_mfc_iobase & 0xff);
  646                 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
  647                                  (pf->pf_mfc_iobase >> 8) & 0xff);
  648                 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
  649                 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
  650 
  651                 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
  652         }
  653 
  654 #ifdef PCCARDDEBUG
  655         if (pccard_debug) {
  656                 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
  657                         device_printf(tmp->sc->dev,
  658                             "function %d CCR at %d offset %x: "
  659                             "%x %x %x %x, %x %x %x %x, %x\n",
  660                             tmp->number, tmp->pf_ccr_window,
  661                             tmp->pf_ccr_offset,
  662                             pccard_ccr_read(tmp, 0x00),
  663                             pccard_ccr_read(tmp, 0x02),
  664                             pccard_ccr_read(tmp, 0x04),
  665                             pccard_ccr_read(tmp, 0x06),
  666                             pccard_ccr_read(tmp, 0x0A),
  667                             pccard_ccr_read(tmp, 0x0C),
  668                             pccard_ccr_read(tmp, 0x0E),
  669                             pccard_ccr_read(tmp, 0x10),
  670                             pccard_ccr_read(tmp, 0x12));
  671                 }
  672         }
  673 #endif
  674         pf->pf_flags |= PFF_ENABLED;
  675         return (0);
  676 
  677  bad:
  678         /*
  679          * Decrement the reference count, and power down the socket, if
  680          * necessary.
  681          */
  682         pf->sc->sc_enabled_count--;
  683         DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
  684 
  685         return (1);
  686 }
  687 
  688 /* Disable PCCARD function. */
  689 static void
  690 pccard_function_disable(struct pccard_function *pf)
  691 {
  692         struct pccard_function *tmp;
  693         device_t dev = pf->sc->dev;
  694 
  695         if (pf->cfe == NULL)
  696                 panic("pccard_function_disable: function not initialized");
  697 
  698         if ((pf->pf_flags & PFF_ENABLED) == 0) {
  699                 /*
  700                  * Don't do anything if we're already disabled.
  701                  */
  702                 return;
  703         }
  704 
  705         if (pf->intr_handler != NULL) {
  706                 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
  707                 struct resource_list_entry *rle =
  708                     resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
  709                 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
  710                     pf->intr_handler_cookie);
  711         }
  712 
  713         /*
  714          * it's possible for different functions' CCRs to be in the same
  715          * underlying page.  Check for that.  Note we mark us as disabled
  716          * first to avoid matching ourself.
  717          */
  718 
  719         pf->pf_flags &= ~PFF_ENABLED;
  720         STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
  721                 if ((tmp->pf_flags & PFF_ENABLED) &&
  722                     (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
  723                     ((pf->ccr_base + PCCARD_CCR_SIZE) <=
  724                     (tmp->ccr_base - tmp->pf_ccr_offset +
  725                     tmp->pf_ccr_realsize)))
  726                         break;
  727         }
  728 
  729         /* Not used by anyone else; unmap the CCR. */
  730         if (tmp == NULL) {
  731                 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
  732                     pf->ccr_res);
  733                 pf->ccr_res = NULL;
  734         }
  735 
  736         /*
  737          * Decrement the reference count, and power down the socket, if
  738          * necessary.
  739          */
  740         pf->sc->sc_enabled_count--;
  741 }
  742 
  743 /*
  744  * simulate the old "probe" routine.  In the new world order, the driver
  745  * needs to grab devices while in the old they were assigned to the device by
  746  * the pccardd process.  These symbols are exported to the upper layers.
  747  */
  748 static int
  749 pccard_compat_do_probe(device_t bus, device_t dev)
  750 {
  751         return (CARD_COMPAT_MATCH(dev));
  752 }
  753 
  754 static int
  755 pccard_compat_do_attach(device_t bus, device_t dev)
  756 {
  757         int err;
  758 
  759         err = CARD_COMPAT_PROBE(dev);
  760         if (err == 0)
  761                 err = CARD_COMPAT_ATTACH(dev);
  762         return (err);
  763 }
  764 
  765 #define PCCARD_NPORT    2
  766 #define PCCARD_NMEM     5
  767 #define PCCARD_NIRQ     1
  768 #define PCCARD_NDRQ     0
  769 
  770 static int
  771 pccard_add_children(device_t dev, int busno)
  772 {
  773         /* Call parent to scan for any current children */
  774         return (0);
  775 }
  776 
  777 static int
  778 pccard_probe(device_t dev)
  779 {
  780         device_set_desc(dev, "16-bit PCCard bus");
  781         return (pccard_add_children(dev, device_get_unit(dev)));
  782 }
  783 
  784 static int
  785 pccard_attach(device_t dev)
  786 {
  787         struct pccard_softc *sc = PCCARD_SOFTC(dev);
  788 
  789         sc->dev = dev;
  790         sc->sc_enabled_count = 0;
  791         return (bus_generic_attach(dev));
  792 }
  793 
  794 static int
  795 pccard_detach(device_t dev)
  796 {
  797         pccard_detach_card(dev);
  798         return 0;
  799 }
  800 
  801 static int
  802 pccard_suspend(device_t self)
  803 {
  804         pccard_detach_card(self);
  805         return (0);
  806 }
  807 
  808 static
  809 int
  810 pccard_resume(device_t self)
  811 {
  812         return (0);
  813 }
  814 
  815 static void
  816 pccard_print_resources(struct resource_list *rl, const char *name, int type,
  817     int count, const char *format)
  818 {
  819         struct resource_list_entry *rle;
  820         int printed;
  821         int i;
  822 
  823         printed = 0;
  824         for (i = 0; i < count; i++) {
  825                 rle = resource_list_find(rl, type, i);
  826                 if (rle != NULL) {
  827                         if (printed == 0)
  828                                 printf(" %s ", name);
  829                         else if (printed > 0)
  830                                 printf(",");
  831                         printed++;
  832                         printf(format, rle->start);
  833                         if (rle->count > 1) {
  834                                 printf("-");
  835                                 printf(format, rle->start + rle->count - 1);
  836                         }
  837                 } else if (i > 3) {
  838                         /* check the first few regardless */
  839                         break;
  840                 }
  841         }
  842 }
  843 
  844 static int
  845 pccard_print_child(device_t dev, device_t child)
  846 {
  847         struct pccard_ivar *devi = PCCARD_IVAR(child);
  848         struct resource_list *rl = &devi->resources;
  849         int retval = 0;
  850 
  851         retval += bus_print_child_header(dev, child);
  852         retval += printf(" at");
  853 
  854         if (devi != NULL) {
  855                 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
  856                     PCCARD_NPORT, "%#lx");
  857                 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
  858                     PCCARD_NMEM, "%#lx");
  859                 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
  860                     "%ld");
  861                 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
  862                     "%ld");
  863                 retval += printf(" function %d config %d", devi->fcn->number,
  864                     devi->fcn->cfe->number);
  865         }
  866 
  867         retval += bus_print_child_footer(dev, child);
  868 
  869         return (retval);
  870 }
  871 
  872 static int
  873 pccard_set_resource(device_t dev, device_t child, int type, int rid,
  874                  u_long start, u_long count)
  875 {
  876         struct pccard_ivar *devi = PCCARD_IVAR(child);
  877         struct resource_list *rl = &devi->resources;
  878 
  879         if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
  880             && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
  881                 return (EINVAL);
  882         if (rid < 0)
  883                 return (EINVAL);
  884         if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
  885                 return (EINVAL);
  886         if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
  887                 return (EINVAL);
  888         if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
  889                 return (EINVAL);
  890         if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
  891                 return (EINVAL);
  892 
  893         resource_list_add(rl, type, rid, start, start + count - 1, count);
  894         if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
  895             type, &rid, start, start + count - 1, count, 0))
  896                 return 0;
  897         else
  898                 return ENOMEM;
  899 }
  900 
  901 static int
  902 pccard_get_resource(device_t dev, device_t child, int type, int rid,
  903     u_long *startp, u_long *countp)
  904 {
  905         struct pccard_ivar *devi = PCCARD_IVAR(child);
  906         struct resource_list *rl = &devi->resources;
  907         struct resource_list_entry *rle;
  908 
  909         rle = resource_list_find(rl, type, rid);
  910         if (rle == NULL)
  911                 return (ENOENT);
  912 
  913         if (startp != NULL)
  914                 *startp = rle->start;
  915         if (countp != NULL)
  916                 *countp = rle->count;
  917 
  918         return (0);
  919 }
  920 
  921 static void
  922 pccard_delete_resource(device_t dev, device_t child, int type, int rid)
  923 {
  924         struct pccard_ivar *devi = PCCARD_IVAR(child);
  925         struct resource_list *rl = &devi->resources;
  926         resource_list_delete(rl, type, rid);
  927 }
  928 
  929 static int
  930 pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
  931     u_int32_t flags)
  932 {
  933         return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
  934             rid, flags));
  935 }
  936 
  937 static int
  938 pccard_set_memory_offset(device_t dev, device_t child, int rid,
  939     u_int32_t offset, u_int32_t *deltap)
  940 
  941 {
  942         return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
  943             offset, deltap));
  944 }
  945 
  946 static void
  947 pccard_probe_nomatch(device_t bus, device_t child)
  948 {
  949         struct pccard_ivar *devi = PCCARD_IVAR(child);
  950         struct pccard_function *func = devi->fcn;
  951         struct pccard_softc *sc = PCCARD_SOFTC(bus);
  952 
  953         device_printf(bus, "<unknown card>");
  954         printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
  955           sc->card.manufacturer, sc->card.product, func->number);
  956         device_printf(bus, "   CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
  957           sc->card.cis1_info[1], sc->card.cis1_info[2]);
  958         return;
  959 }
  960 
  961 static int
  962 pccard_child_location_str(device_t bus, device_t child, char *buf,
  963     size_t buflen)
  964 {
  965         struct pccard_ivar *devi = PCCARD_IVAR(child);
  966         struct pccard_function *func = devi->fcn;
  967 
  968         snprintf(buf, buflen, "function=%d", func->number);
  969         return (0);
  970 }
  971 
  972 static int
  973 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
  974     size_t buflen)
  975 {
  976         struct pccard_ivar *devi = PCCARD_IVAR(child);
  977         struct pccard_function *func = devi->fcn;
  978         struct pccard_softc *sc = PCCARD_SOFTC(bus);
  979 
  980         snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
  981             "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
  982             sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
  983             sc->card.cis1_info[1], func->function);
  984         return (0);
  985 }
  986 
  987 static int
  988 pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
  989 {
  990         struct pccard_ivar *devi = PCCARD_IVAR(child);
  991         struct pccard_function *func = devi->fcn;
  992         struct pccard_softc *sc = PCCARD_SOFTC(bus);
  993 
  994         switch (which) {
  995         default:
  996         case PCCARD_IVAR_ETHADDR:
  997                 bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
  998                 break;
  999         case PCCARD_IVAR_VENDOR:
 1000                 *(u_int32_t *) result = sc->card.manufacturer;
 1001                 break;
 1002         case PCCARD_IVAR_PRODUCT:
 1003                 *(u_int32_t *) result = sc->card.product;
 1004                 break;
 1005         case PCCARD_IVAR_PRODEXT:
 1006                 *(u_int16_t *) result = sc->card.prodext;
 1007                 break;
 1008         case PCCARD_IVAR_FUNCTION:
 1009                 *(u_int32_t *) result = func->function;
 1010                 break;
 1011         case PCCARD_IVAR_FUNCTION_NUMBER:
 1012                 if (!func) {
 1013                         device_printf(bus, "No function number, bug!\n");
 1014                         return (ENOENT);
 1015                 }
 1016                 *(u_int32_t *) result = func->number;
 1017                 break;
 1018         case PCCARD_IVAR_VENDOR_STR:
 1019                 *(char **) result = sc->card.cis1_info[0];
 1020                 break;
 1021         case PCCARD_IVAR_PRODUCT_STR:
 1022                 *(char **) result = sc->card.cis1_info[1];
 1023                 break;
 1024         case PCCARD_IVAR_CIS3_STR:
 1025                 *(char **) result = sc->card.cis1_info[2];
 1026                 break;
 1027         case PCCARD_IVAR_CIS4_STR:
 1028                 *(char **) result = sc->card.cis1_info[3];
 1029                 break;
 1030         }
 1031         return (0);
 1032 }
 1033 
 1034 static void
 1035 pccard_driver_added(device_t dev, driver_t *driver)
 1036 {
 1037         struct pccard_softc *sc = PCCARD_SOFTC(dev);
 1038         struct pccard_function *pf;
 1039         device_t child;
 1040 
 1041         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
 1042                 if (STAILQ_EMPTY(&pf->cfe_head))
 1043                         continue;
 1044                 child = pf->dev;
 1045                 if (device_get_state(child) != DS_NOTPRESENT)
 1046                         continue;
 1047                 if (pccard_function_enable(pf) == 0 &&
 1048                     device_probe_and_attach(child) == 0) {
 1049                         DEVPRINTF((sc->dev, "function %d CCR at %d "
 1050                             "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
 1051                             pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
 1052                             pccard_ccr_read(pf, 0x00),
 1053                         pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
 1054                         pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
 1055                         pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
 1056                         pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
 1057                 } else {
 1058                         if (pf->cfe != NULL)
 1059                                 pccard_function_disable(pf);
 1060                 }
 1061         }
 1062         return;
 1063 }
 1064 
 1065 static struct resource *
 1066 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
 1067     u_long start, u_long end, u_long count, u_int flags)
 1068 {
 1069         struct pccard_ivar *dinfo;
 1070         struct resource_list_entry *rle = 0;
 1071         int passthrough = (device_get_parent(child) != dev);
 1072         struct resource *r = NULL;
 1073 
 1074         if (passthrough) {
 1075                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
 1076                     type, rid, start, end, count, flags));
 1077         }
 1078 
 1079         dinfo = device_get_ivars(child);
 1080         rle = resource_list_find(&dinfo->resources, type, *rid);
 1081 
 1082         if (rle == NULL)
 1083                 return (NULL);          /* no resource of that type/rid */
 1084 
 1085         if (rle->res == NULL) {
 1086                 switch(type) {
 1087                 case SYS_RES_IOPORT:
 1088                         r = bus_alloc_resource(dev, type, rid, start, end,
 1089                             count, rman_make_alignment_flags(count));
 1090                         if (r == NULL)
 1091                                 goto bad;
 1092                         resource_list_add(&dinfo->resources, type, *rid,
 1093                             rman_get_start(r), rman_get_end(r), count);
 1094                         rle = resource_list_find(&dinfo->resources, type, *rid);
 1095                         if (!rle)
 1096                                 goto bad;
 1097                         rle->res = r;
 1098                         break;
 1099                 case SYS_RES_MEMORY:
 1100                         break;
 1101                 case SYS_RES_IRQ:
 1102                         break;
 1103                 }
 1104                 return (rle->res);
 1105         }
 1106         if (rman_get_device(rle->res) != dev)
 1107                 return (NULL);
 1108         bus_release_resource(dev, type, *rid, rle->res);
 1109         rle->res = NULL;
 1110         switch(type) {
 1111         case SYS_RES_IOPORT:
 1112         case SYS_RES_MEMORY:
 1113                 if (!(flags & RF_ALIGNMENT_MASK))
 1114                         flags |= rman_make_alignment_flags(rle->count);
 1115                 break;
 1116         case SYS_RES_IRQ:
 1117                 flags |= RF_SHAREABLE;
 1118                 break;
 1119         }
 1120         rle->res = resource_list_alloc(&dinfo->resources, dev, child,
 1121             type, rid, rle->start, rle->end, rle->count, flags);
 1122         return (rle->res);
 1123 bad:;
 1124         device_printf(dev, "WARNING: Resource not reserved by pccard\n");
 1125         return (NULL);
 1126 }
 1127 
 1128 static int
 1129 pccard_release_resource(device_t dev, device_t child, int type, int rid,
 1130     struct resource *r)
 1131 {
 1132         struct pccard_ivar *dinfo;
 1133         int passthrough = (device_get_parent(child) != dev);
 1134         struct resource_list_entry *rle = 0;
 1135         int ret;
 1136         int flags;
 1137 
 1138         if (passthrough)
 1139                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
 1140                     type, rid, r);
 1141 
 1142         dinfo = device_get_ivars(child);
 1143 
 1144         rle = resource_list_find(&dinfo->resources, type, rid);
 1145 
 1146         if (!rle) {
 1147                 device_printf(dev, "Allocated resource not found, "
 1148                     "%d %x %lx %lx\n",
 1149                     type, rid, rman_get_start(r), rman_get_size(r));
 1150                 return ENOENT;
 1151         }
 1152         if (!rle->res) {
 1153                 device_printf(dev, "Allocated resource not recorded\n");
 1154                 return ENOENT;
 1155         }
 1156 
 1157         ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
 1158             type, rid, r);
 1159         switch(type) {
 1160         case SYS_RES_IOPORT:
 1161         case SYS_RES_MEMORY:
 1162                 flags = rman_make_alignment_flags(rle->count);
 1163                 break;
 1164         case SYS_RES_IRQ:
 1165                 flags = RF_SHAREABLE;
 1166                 break;
 1167         default:
 1168                 flags = 0;
 1169         }
 1170         rle->res = bus_alloc_resource(dev, type, &rid,
 1171             rle->start, rle->end, rle->count, flags);
 1172         if (rle->res == NULL)
 1173                 device_printf(dev, "release_resource: "
 1174                     "unable to reaquire resource\n");
 1175         return ret;
 1176 }
 1177 
 1178 static void
 1179 pccard_child_detached(device_t parent, device_t dev)
 1180 {
 1181         struct pccard_ivar *ivar = PCCARD_IVAR(dev);
 1182         struct pccard_function *pf = ivar->fcn;
 1183 
 1184         pccard_function_disable(pf);
 1185 }
 1186 
 1187 static void
 1188 pccard_intr(void *arg)
 1189 {
 1190         struct pccard_function *pf = (struct pccard_function*) arg;
 1191         int reg;
 1192         int doisr = 1;
 1193 
 1194         /*
 1195          * MFC cards know if they interrupted, so we have to ack the
 1196          * interrupt and call the ISR.  Non-MFC cards don't have these
 1197          * bits, so they always get called.  Many non-MFC cards have
 1198          * this bit set always upon read, but some do not.
 1199          *
 1200          * We always ack the interrupt, even if there's no ISR
 1201          * for the card.  This is done on the theory that acking
 1202          * the interrupt will pacify the card enough to keep an
 1203          * interrupt storm from happening.  Of course this won't
 1204          * help in the non-MFC case.
 1205          */
 1206         if (pccard_mfc(pf->sc)) {
 1207                 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
 1208                 if (reg & PCCARD_CCR_STATUS_INTR)
 1209                         pccard_ccr_write(pf, PCCARD_CCR_STATUS,
 1210                             reg & ~PCCARD_CCR_STATUS_INTR);
 1211                 else
 1212                         doisr = 0;
 1213         }
 1214         if (pf->intr_handler != NULL && doisr)
 1215                 pf->intr_handler(pf->intr_handler_arg);
 1216 }
 1217 
 1218 static int
 1219 pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
 1220     int flags, driver_intr_t *intr, void *arg, void **cookiep)
 1221 {
 1222         struct pccard_softc *sc = PCCARD_SOFTC(dev);
 1223         struct pccard_ivar *ivar = PCCARD_IVAR(child);
 1224         struct pccard_function *func = ivar->fcn;
 1225         int err;
 1226 
 1227         if (func->intr_handler != NULL)
 1228                 panic("Only one interrupt handler per function allowed");
 1229         err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
 1230             func, cookiep);
 1231         if (err != 0)
 1232                 return (err);
 1233         func->intr_handler = intr;
 1234         func->intr_handler_arg = arg;
 1235         func->intr_handler_cookie = *cookiep;
 1236         if (pccard_mfc(sc)) {
 1237                 pccard_ccr_write(func, PCCARD_CCR_OPTION,
 1238                     pccard_ccr_read(func, PCCARD_CCR_OPTION) |
 1239                     PCCARD_CCR_OPTION_IREQ_ENABLE);
 1240         }
 1241         return (0);
 1242 }
 1243 
 1244 static int
 1245 pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
 1246     void *cookie)
 1247 {
 1248         struct pccard_softc *sc = PCCARD_SOFTC(dev);
 1249         struct pccard_ivar *ivar = PCCARD_IVAR(child);
 1250         struct pccard_function *func = ivar->fcn;
 1251         int ret;
 1252 
 1253         if (pccard_mfc(sc)) {
 1254                 pccard_ccr_write(func, PCCARD_CCR_OPTION,
 1255                     pccard_ccr_read(func, PCCARD_CCR_OPTION) &
 1256                     ~PCCARD_CCR_OPTION_IREQ_ENABLE);
 1257         }
 1258         ret = bus_generic_teardown_intr(dev, child, r, cookie);
 1259         if (ret == 0) {
 1260                 func->intr_handler = NULL;
 1261                 func->intr_handler_arg = NULL;
 1262                 func->intr_handler_cookie = NULL;
 1263         }
 1264 
 1265         return (ret);
 1266 }
 1267 
 1268 static device_method_t pccard_methods[] = {
 1269         /* Device interface */
 1270         DEVMETHOD(device_probe,         pccard_probe),
 1271         DEVMETHOD(device_attach,        pccard_attach),
 1272         DEVMETHOD(device_detach,        pccard_detach),
 1273         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
 1274         DEVMETHOD(device_suspend,       pccard_suspend),
 1275         DEVMETHOD(device_resume,        pccard_resume),
 1276 
 1277         /* Bus interface */
 1278         DEVMETHOD(bus_print_child,      pccard_print_child),
 1279         DEVMETHOD(bus_driver_added,     pccard_driver_added),
 1280         DEVMETHOD(bus_child_detached,   pccard_child_detached),
 1281         DEVMETHOD(bus_alloc_resource,   pccard_alloc_resource),
 1282         DEVMETHOD(bus_release_resource, pccard_release_resource),
 1283         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
 1284         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
 1285         DEVMETHOD(bus_setup_intr,       pccard_setup_intr),
 1286         DEVMETHOD(bus_teardown_intr,    pccard_teardown_intr),
 1287         DEVMETHOD(bus_set_resource,     pccard_set_resource),
 1288         DEVMETHOD(bus_get_resource,     pccard_get_resource),
 1289         DEVMETHOD(bus_delete_resource,  pccard_delete_resource),
 1290         DEVMETHOD(bus_probe_nomatch,    pccard_probe_nomatch),
 1291         DEVMETHOD(bus_read_ivar,        pccard_read_ivar),
 1292         DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
 1293         DEVMETHOD(bus_child_location_str, pccard_child_location_str),
 1294 
 1295         /* Card Interface */
 1296         DEVMETHOD(card_set_res_flags,   pccard_set_res_flags),
 1297         DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
 1298         DEVMETHOD(card_get_type,        pccard_card_gettype),
 1299         DEVMETHOD(card_attach_card,     pccard_attach_card),
 1300         DEVMETHOD(card_detach_card,     pccard_detach_card),
 1301         DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
 1302         DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
 1303         DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
 1304 
 1305         { 0, 0 }
 1306 };
 1307 
 1308 static driver_t pccard_driver = {
 1309         "pccard",
 1310         pccard_methods,
 1311         sizeof(struct pccard_softc)
 1312 };
 1313 
 1314 devclass_t      pccard_devclass;
 1315 
 1316 /* Maybe we need to have a slot device? */
 1317 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
 1318 DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
 1319 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
 1320 DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
 1321 MODULE_VERSION(pccard, 1);
 1322 /*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/

Cache object: c3edcd79557907573a22beb646103afe


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.