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/cardbus/cardbus.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 /*-
    2  * Copyright (c) 2003 M. Warner Losh.  All Rights Reserved.
    3  * Copyright (c) 2000,2001 Jonathan Chen.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 #include <sys/malloc.h>
   33 #include <sys/module.h>
   34 #include <sys/kernel.h>
   35 #include <sys/sysctl.h>
   36 
   37 #include <sys/bus.h>
   38 #include <machine/bus.h>
   39 #include <sys/rman.h>
   40 #include <machine/resource.h>
   41 
   42 #include <sys/pciio.h>
   43 #include <dev/pci/pcivar.h>
   44 #include <dev/pci/pcireg.h>
   45 #include <dev/pci/pci_private.h>
   46 
   47 #include <dev/cardbus/cardbusreg.h>
   48 #include <dev/cardbus/cardbusvar.h>
   49 #include <dev/cardbus/cardbus_cis.h>
   50 #include <dev/pccard/pccardvar.h>
   51 
   52 #include "power_if.h"
   53 #include "pcib_if.h"
   54 
   55 /* sysctl vars */
   56 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
   57 
   58 int    cardbus_debug = 0;
   59 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
   60 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
   61     &cardbus_debug, 0,
   62   "CardBus debug");
   63 
   64 int    cardbus_cis_debug = 0;
   65 TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
   66 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
   67     &cardbus_cis_debug, 0,
   68   "CardBus CIS debug");
   69 
   70 #define DPRINTF(a) if (cardbus_debug) printf a
   71 #define DEVPRINTF(x) if (cardbus_debug) device_printf x
   72 
   73 
   74 static int      cardbus_attach(device_t cbdev);
   75 static int      cardbus_attach_card(device_t cbdev);
   76 static int      cardbus_detach(device_t cbdev);
   77 static int      cardbus_detach_card(device_t cbdev);
   78 static void     cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
   79                     pcicfgregs *cfg);
   80 static void     cardbus_driver_added(device_t cbdev, driver_t *driver);
   81 static int      cardbus_probe(device_t cbdev);
   82 static int      cardbus_read_ivar(device_t cbdev, device_t child, int which,
   83                     uintptr_t *result);
   84 static void     cardbus_release_all_resources(device_t cbdev,
   85                     struct cardbus_devinfo *dinfo);
   86 static int      cardbus_write_ivar(device_t cbdev, device_t child, int which,
   87                     uintptr_t value);
   88 
   89 /************************************************************************/
   90 /* Probe/Attach                                                         */
   91 /************************************************************************/
   92 
   93 static int
   94 cardbus_probe(device_t cbdev)
   95 {
   96         device_set_desc(cbdev, "CardBus bus");
   97         return 0;
   98 }
   99 
  100 static int
  101 cardbus_attach(device_t cbdev)
  102 {
  103         return 0;
  104 }
  105 
  106 static int
  107 cardbus_detach(device_t cbdev)
  108 {
  109         cardbus_detach_card(cbdev);
  110         return 0;
  111 }
  112 
  113 static int
  114 cardbus_suspend(device_t self)
  115 {
  116         cardbus_detach_card(self);
  117         return (0);
  118 }
  119 
  120 static int
  121 cardbus_resume(device_t self)
  122 {
  123         return (0);
  124 }
  125 
  126 /************************************************************************/
  127 /* Attach/Detach card                                                   */
  128 /************************************************************************/
  129 
  130 static void
  131 cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
  132 {
  133         PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
  134             pci_get_irq(device_get_parent(brdev)), 1);
  135         cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
  136 
  137         PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
  138         cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
  139 
  140         PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
  141         cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
  142 
  143         PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
  144         cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
  145 
  146         PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
  147         cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
  148 }
  149 
  150 static int
  151 cardbus_attach_card(device_t cbdev)
  152 {
  153         device_t brdev = device_get_parent(cbdev);
  154         int cardattached = 0;
  155         int bus, slot, func;
  156 
  157         cardbus_detach_card(cbdev); /* detach existing cards */
  158         POWER_ENABLE_SOCKET(brdev, cbdev);
  159         bus = pcib_get_bus(cbdev);
  160         /* For each function, set it up and try to attach a driver to it */
  161         for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {
  162                 int cardbusfunchigh = 0;
  163                 for (func = 0; func <= cardbusfunchigh; func++) {
  164                         struct cardbus_devinfo *dinfo;
  165 
  166                         dinfo = (struct cardbus_devinfo *)
  167                             pci_read_device(brdev, bus, slot, func,
  168                                 sizeof(struct cardbus_devinfo));
  169                         if (dinfo == NULL)
  170                                 continue;
  171                         if (dinfo->pci.cfg.mfdev)
  172                                 cardbusfunchigh = CARDBUS_FUNCMAX;
  173 
  174                         cardbus_device_setup_regs(brdev, bus, slot, func,
  175                             &dinfo->pci.cfg);
  176                         dinfo->pci.cfg.dev = device_add_child(cbdev, NULL, -1);
  177                         if (!dinfo->pci.cfg.dev) {
  178                                 DEVPRINTF((cbdev, "Cannot add child!\n"));
  179                                 pci_freecfg((struct pci_devinfo *)dinfo);
  180                                 continue;
  181                         }
  182                         resource_list_init(&dinfo->pci.resources);
  183                         device_set_ivars(dinfo->pci.cfg.dev, dinfo);
  184                         cardbus_do_cis(cbdev, dinfo->pci.cfg.dev);
  185                         pci_print_verbose(&dinfo->pci);
  186                         if (device_probe_and_attach(dinfo->pci.cfg.dev) != 0)
  187                                 cardbus_release_all_resources(cbdev, dinfo);
  188                         else
  189                                 cardattached++;
  190                 }
  191         }
  192 
  193         if (cardattached > 0)
  194                 return (0);
  195         POWER_DISABLE_SOCKET(brdev, cbdev);
  196         return (ENOENT);
  197 }
  198 
  199 static int
  200 cardbus_detach_card(device_t cbdev)
  201 {
  202         int numdevs;
  203         device_t *devlist;
  204         int tmp;
  205         int err = 0;
  206 
  207         device_get_children(cbdev, &devlist, &numdevs);
  208 
  209         if (numdevs == 0) {
  210                 free(devlist, M_TEMP);
  211                 return (ENOENT);
  212         }
  213 
  214         for (tmp = 0; tmp < numdevs; tmp++) {
  215                 struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
  216                 int status = device_get_state(devlist[tmp]);
  217 
  218                 if (dinfo->pci.cfg.dev != devlist[tmp])
  219                         device_printf(cbdev, "devinfo dev mismatch\n");
  220                 if (status == DS_ATTACHED || status == DS_BUSY)
  221                         device_detach(devlist[tmp]);
  222                 cardbus_release_all_resources(cbdev, dinfo);
  223                 device_delete_child(cbdev, devlist[tmp]);
  224                 pci_freecfg((struct pci_devinfo *)dinfo);
  225         }
  226         POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
  227         free(devlist, M_TEMP);
  228         return (err);
  229 }
  230 
  231 static void
  232 cardbus_driver_added(device_t cbdev, driver_t *driver)
  233 {
  234         int numdevs;
  235         device_t *devlist;
  236         device_t dev;
  237         int i;
  238         struct cardbus_devinfo *dinfo;
  239 
  240         DEVICE_IDENTIFY(driver, cbdev);
  241         device_get_children(cbdev, &devlist, &numdevs);
  242         /*
  243          * If there are no drivers attached, but there are children,
  244          * then power the card up.
  245          */
  246         for (i = 0; i < numdevs; i++) {
  247                 dev = devlist[i];
  248                 if (device_get_state(dev) != DS_NOTPRESENT)
  249                     break;
  250         }
  251         if (i > 0 && i == numdevs)
  252                 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
  253         for (i = 0; i < numdevs; i++) {
  254                 dev = devlist[i];
  255                 if (device_get_state(dev) != DS_NOTPRESENT)
  256                         continue;
  257                 dinfo = device_get_ivars(dev);
  258                 pci_print_verbose(&dinfo->pci);
  259                 resource_list_init(&dinfo->pci.resources);
  260                 cardbus_do_cis(cbdev, dev);
  261                 if (device_probe_and_attach(dev) != 0)
  262                         cardbus_release_all_resources(cbdev, dinfo);
  263         }
  264         free(devlist, M_TEMP);
  265 }
  266 
  267 static void
  268 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
  269 {
  270         struct resource_list_entry *rle;
  271 
  272         /* Free all allocated resources */
  273         SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
  274                 if (rle->res) {
  275                         if (rman_get_device(rle->res) != cbdev)
  276                                 device_printf(cbdev, "release_all_resource: "
  277                                     "Resource still owned by child, oops. "
  278                                     "(type=%d, rid=%d, addr=%lx)\n",
  279                                     rle->type, rle->rid,
  280                                     rman_get_start(rle->res));
  281                         BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
  282                             cbdev, rle->type, rle->rid, rle->res);
  283                         rle->res = NULL;
  284                         /*
  285                          * zero out config so the card won't acknowledge
  286                          * access to the space anymore
  287                          */
  288                         pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
  289                 }
  290         }
  291         resource_list_free(&dinfo->pci.resources);
  292 }
  293 
  294 /************************************************************************/
  295 /* Other Bus Methods                                                    */
  296 /************************************************************************/
  297 
  298 static int
  299 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
  300 {
  301         struct cardbus_devinfo *dinfo;
  302         pcicfgregs *cfg;
  303 
  304         dinfo = device_get_ivars(child);
  305         cfg = &dinfo->pci.cfg;
  306 
  307         switch (which) {
  308         case PCI_IVAR_ETHADDR:
  309                 /*
  310                  * The generic accessor doesn't deal with failure, so
  311                  * we set the return value, then return an error.
  312                  */
  313                 if ((dinfo->fepresent & (1 << TPL_FUNCE_LAN_NID)) == 0) {
  314                         *((uint8_t **) result) = NULL;
  315                         return (EINVAL);
  316                 }
  317                 *((uint8_t **) result) = dinfo->funce.lan.nid;
  318                 break;
  319         default:
  320                 return (pci_read_ivar(cbdev, child, which, result));
  321         }
  322         return 0;
  323 }
  324 
  325 static int
  326 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
  327 {
  328         return(pci_write_ivar(cbdev, child, which, value));
  329 }
  330 
  331 static device_method_t cardbus_methods[] = {
  332         /* Device interface */
  333         DEVMETHOD(device_probe,         cardbus_probe),
  334         DEVMETHOD(device_attach,        cardbus_attach),
  335         DEVMETHOD(device_detach,        cardbus_detach),
  336         DEVMETHOD(device_suspend,       cardbus_suspend),
  337         DEVMETHOD(device_resume,        cardbus_resume),
  338 
  339         /* Bus interface */
  340         DEVMETHOD(bus_read_ivar,        cardbus_read_ivar),
  341         DEVMETHOD(bus_write_ivar,       cardbus_write_ivar),
  342         DEVMETHOD(bus_driver_added,     cardbus_driver_added),
  343 
  344         /* Card Interface */
  345         DEVMETHOD(card_attach_card,     cardbus_attach_card),
  346         DEVMETHOD(card_detach_card,     cardbus_detach_card),
  347 
  348         {0,0}
  349 };
  350 
  351 DECLARE_CLASS(pci_driver);
  352 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 0, pci_driver);
  353 
  354 static devclass_t cardbus_devclass;
  355 
  356 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
  357 MODULE_VERSION(cardbus, 1);

Cache object: e9ddb074e287bc6d8ba013f737aa299d


[ 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.