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/usb/if_kue.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) 1997, 1998, 1999, 2000
    3  *      Bill Paul <wpaul@ee.columbia.edu>.  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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by Bill Paul.
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   30  * THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/7.3/sys/dev/usb/if_kue.c 171006 2007-06-23 06:47:43Z imp $");
   35 
   36 /*
   37  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
   38  *
   39  * Written by Bill Paul <wpaul@ee.columbia.edu>
   40  * Electrical Engineering Department
   41  * Columbia University, New York City
   42  */
   43 
   44 /*
   45  * The KLSI USB to ethernet adapter chip contains an USB serial interface,
   46  * ethernet MAC and embedded microcontroller (called the QT Engine).
   47  * The chip must have firmware loaded into it before it will operate.
   48  * Packets are passed between the chip and host via bulk transfers.
   49  * There is an interrupt endpoint mentioned in the software spec, however
   50  * it's currently unused. This device is 10Mbps half-duplex only, hence
   51  * there is no media selection logic. The MAC supports a 128 entry
   52  * multicast filter, though the exact size of the filter can depend
   53  * on the firmware. Curiously, while the software spec describes various
   54  * ethernet statistics counters, my sample adapter and firmware combination
   55  * claims not to support any statistics counters at all.
   56  *
   57  * Note that once we load the firmware in the device, we have to be
   58  * careful not to load it again: if you restart your computer but
   59  * leave the adapter attached to the USB controller, it may remain
   60  * powered on and retain its firmware. In this case, we don't need
   61  * to load the firmware a second time.
   62  *
   63  * Special thanks to Rob Furr for providing an ADS Technologies
   64  * adapter for development and testing. No monkeys were harmed during
   65  * the development of this driver.
   66  */
   67 
   68 #include <sys/param.h>
   69 #include <sys/systm.h>
   70 #include <sys/sockio.h>
   71 #include <sys/mbuf.h>
   72 #include <sys/malloc.h>
   73 #include <sys/kernel.h>
   74 #include <sys/module.h>
   75 #include <sys/socket.h>
   76 
   77 #include <net/if.h>
   78 #include <net/if_arp.h>
   79 #include <net/ethernet.h>
   80 #include <net/if_dl.h>
   81 #include <net/if_media.h>
   82 #include <net/if_types.h>
   83 
   84 #include <net/bpf.h>
   85 
   86 #include <sys/bus.h>
   87 #include <machine/bus.h>
   88 
   89 #include <dev/usb/usb.h>
   90 #include <dev/usb/usbdi.h>
   91 #include <dev/usb/usbdi_util.h>
   92 #include <dev/usb/usbdivar.h>
   93 #include "usbdevs.h"
   94 #include <dev/usb/usb_ethersubr.h>
   95 
   96 #include <dev/usb/if_kuereg.h>
   97 #include <dev/usb/kue_fw.h>
   98 
   99 MODULE_DEPEND(kue, usb, 1, 1, 1);
  100 MODULE_DEPEND(kue, ether, 1, 1, 1);
  101 
  102 /*
  103  * Various supported device vendors/products.
  104  */
  105 static struct kue_type kue_devs[] = {
  106         { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
  107         { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
  108         { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
  109         { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
  110         { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
  111         { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
  112         { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
  113         { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
  114         { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
  115         { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
  116         { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
  117         { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
  118         { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
  119         { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
  120         { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
  121         { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
  122         { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
  123         { USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101 },
  124         { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
  125         { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
  126         { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
  127         { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
  128         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
  129         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
  130         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
  131         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
  132         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
  133         { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
  134         { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
  135         { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
  136         { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
  137         { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
  138         { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE },
  139         { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
  140         { 0, 0 }
  141 };
  142 
  143 static device_probe_t kue_match;
  144 static device_attach_t kue_attach;
  145 static device_detach_t kue_detach;
  146 static device_shutdown_t kue_shutdown;
  147 static int kue_encap(struct kue_softc *, struct mbuf *, int);
  148 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
  149 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
  150 static void kue_start(struct ifnet *);
  151 static void kue_rxstart(struct ifnet *);
  152 static int kue_ioctl(struct ifnet *, u_long, caddr_t);
  153 static void kue_init(void *);
  154 static void kue_stop(struct kue_softc *);
  155 static void kue_watchdog(struct ifnet *);
  156 
  157 static void kue_setmulti(struct kue_softc *);
  158 static void kue_reset(struct kue_softc *);
  159 
  160 static usbd_status kue_do_request(usbd_device_handle,
  161                                   usb_device_request_t *, void *);
  162 static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
  163                            u_int16_t, char *, int);
  164 static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
  165 static int kue_load_fw(struct kue_softc *);
  166 
  167 static device_method_t kue_methods[] = {
  168         /* Device interface */
  169         DEVMETHOD(device_probe,         kue_match),
  170         DEVMETHOD(device_attach,        kue_attach),
  171         DEVMETHOD(device_detach,        kue_detach),
  172         DEVMETHOD(device_shutdown,      kue_shutdown),
  173 
  174         { 0, 0 }
  175 };
  176 
  177 static driver_t kue_driver = {
  178         "kue",
  179         kue_methods,
  180         sizeof(struct kue_softc)
  181 };
  182 
  183 static devclass_t kue_devclass;
  184 
  185 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
  186 
  187 /*
  188  * We have a custom do_request function which is almost like the
  189  * regular do_request function, except it has a much longer timeout.
  190  * Why? Because we need to make requests over the control endpoint
  191  * to download the firmware to the device, which can take longer
  192  * than the default timeout.
  193  */
  194 static usbd_status
  195 kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
  196 {
  197         usbd_xfer_handle        xfer;
  198         usbd_status             err;
  199 
  200         xfer = usbd_alloc_xfer(dev);
  201         usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
  202             data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
  203         err = usbd_sync_transfer(xfer);
  204         usbd_free_xfer(xfer);
  205         return(err);
  206 }
  207 
  208 static usbd_status
  209 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
  210 {
  211         usbd_device_handle      dev;
  212         usb_device_request_t    req;
  213         usbd_status             err;
  214 
  215         if (sc->kue_dying)
  216                 return(USBD_NORMAL_COMPLETION);
  217 
  218         dev = sc->kue_udev;
  219 
  220         KUE_LOCK(sc);
  221 
  222         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
  223 
  224         req.bRequest = breq;
  225         USETW(req.wValue, word);
  226         USETW(req.wIndex, 0);
  227         USETW(req.wLength, 0);
  228 
  229         err = kue_do_request(dev, &req, NULL);
  230 
  231         KUE_UNLOCK(sc);
  232 
  233         return(err);
  234 }
  235 
  236 static usbd_status
  237 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
  238         char *data, int len)
  239 {
  240         usbd_device_handle      dev;
  241         usb_device_request_t    req;
  242         usbd_status             err;
  243 
  244         dev = sc->kue_udev;
  245 
  246         if (sc->kue_dying)
  247                 return(USBD_NORMAL_COMPLETION);
  248 
  249         KUE_LOCK(sc);
  250 
  251         if (rw == KUE_CTL_WRITE)
  252                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
  253         else
  254                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
  255 
  256         req.bRequest = breq;
  257         USETW(req.wValue, val);
  258         USETW(req.wIndex, 0);
  259         USETW(req.wLength, len);
  260 
  261         err = kue_do_request(dev, &req, data);
  262 
  263         KUE_UNLOCK(sc);
  264 
  265         return(err);
  266 }
  267 
  268 static int
  269 kue_load_fw(struct kue_softc *sc)
  270 {
  271         usbd_status             err;
  272         usb_device_descriptor_t *dd;
  273         int                     hwrev;
  274 
  275         dd = &sc->kue_udev->ddesc;
  276         hwrev = UGETW(dd->bcdDevice);
  277 
  278         /*
  279          * First, check if we even need to load the firmware.
  280          * If the device was still attached when the system was
  281          * rebooted, it may already have firmware loaded in it.
  282          * If this is the case, we don't need to do it again.
  283          * And in fact, if we try to load it again, we'll hang,
  284          * so we have to avoid this condition if we don't want
  285          * to look stupid.
  286          *
  287          * We can test this quickly by checking the bcdRevision
  288          * code. The NIC will return a different revision code if
  289          * it's probed while the firmware is still loaded and
  290          * running.
  291          */
  292         if (hwrev == 0x0202)
  293                 return(0);
  294 
  295         /* Load code segment */
  296         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
  297             0, kue_code_seg, sizeof(kue_code_seg));
  298         if (err) {
  299                 device_printf(sc->kue_dev, "failed to load code segment: %s\n",
  300                     usbd_errstr(err));
  301                 return(ENXIO);
  302         }
  303 
  304         /* Load fixup segment */
  305         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
  306             0, kue_fix_seg, sizeof(kue_fix_seg));
  307         if (err) {
  308                 device_printf(sc->kue_dev, "failed to load fixup segment: %s\n",
  309                     usbd_errstr(err));
  310                 return(ENXIO);
  311         }
  312 
  313         /* Send trigger command. */
  314         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
  315             0, kue_trig_seg, sizeof(kue_trig_seg));
  316         if (err) {
  317                 device_printf(sc->kue_dev, "failed to load trigger segment: %s\n",
  318                     usbd_errstr(err));
  319                 return(ENXIO);
  320         }
  321 
  322         return(0);
  323 }
  324 
  325 static void
  326 kue_setmulti(struct kue_softc *sc)
  327 {
  328         struct ifnet            *ifp;
  329         struct ifmultiaddr      *ifma;
  330         int                     i = 0;
  331 
  332         ifp = sc->kue_ifp;
  333 
  334         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
  335                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
  336                 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
  337                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
  338                 return;
  339         }
  340 
  341         sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
  342 
  343         IF_ADDR_LOCK(ifp);
  344         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
  345         {
  346                 if (ifma->ifma_addr->sa_family != AF_LINK)
  347                         continue;
  348                 /*
  349                  * If there are too many addresses for the
  350                  * internal filter, switch over to allmulti mode.
  351                  */
  352                 if (i == KUE_MCFILTCNT(sc))
  353                         break;
  354                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
  355                     KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
  356                 i++;
  357         }
  358         IF_ADDR_UNLOCK(ifp);
  359 
  360         if (i == KUE_MCFILTCNT(sc))
  361                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
  362         else {
  363                 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
  364                 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
  365                     i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
  366         }
  367 
  368         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
  369 
  370         return;
  371 }
  372 
  373 /*
  374  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
  375  * done after the firmware is loaded into the adapter in order to
  376  * bring it into proper operation.
  377  */
  378 static void
  379 kue_reset(struct kue_softc *sc)
  380 {
  381         if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
  382             usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
  383             &sc->kue_iface)) {
  384                 device_printf(sc->kue_dev, "getting interface handle failed\n");
  385         }
  386 
  387         /* Wait a little while for the chip to get its brains in order. */
  388         DELAY(1000);
  389         return;
  390 }
  391 
  392 /*
  393  * Probe for a KLSI chip.
  394  */
  395 static int
  396 kue_match(device_t self)
  397 {
  398         struct usb_attach_arg *uaa = device_get_ivars(self);
  399         struct kue_type                 *t;
  400 
  401         if (!uaa->iface)
  402                 return(UMATCH_NONE);
  403 
  404         t = kue_devs;
  405         while (t->kue_vid) {
  406                 if (uaa->vendor == t->kue_vid && uaa->product == t->kue_did)
  407                         return (UMATCH_VENDOR_PRODUCT);
  408                 t++;
  409         }
  410         return (UMATCH_NONE);
  411 }
  412 
  413 /*
  414  * Attach the interface. Allocate softc structures, do
  415  * setup and ethernet/BPF attach.
  416  */
  417 static int
  418 kue_attach(device_t self)
  419 {
  420         struct kue_softc *sc = device_get_softc(self);
  421         struct usb_attach_arg *uaa = device_get_ivars(self);
  422         struct ifnet            *ifp;
  423         usbd_status             err;
  424         usb_interface_descriptor_t      *id;
  425         usb_endpoint_descriptor_t       *ed;
  426         int                     i;
  427 
  428         sc->kue_dev = self;
  429         sc->kue_iface = uaa->iface;
  430         sc->kue_udev = uaa->device;
  431 
  432         id = usbd_get_interface_descriptor(uaa->iface);
  433 
  434         /* Find endpoints. */
  435         for (i = 0; i < id->bNumEndpoints; i++) {
  436                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
  437                 if (!ed) {
  438                         device_printf(sc->kue_dev, "couldn't get ep %d\n", i);
  439                         return ENXIO;
  440                 }
  441                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  442                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  443                         sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
  444                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
  445                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  446                         sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
  447                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  448                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
  449                         sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
  450                 }
  451         }
  452 
  453         mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
  454             MTX_DEF | MTX_RECURSE);
  455         KUE_LOCK(sc);
  456 
  457         /* Load the firmware into the NIC. */
  458         if (kue_load_fw(sc)) {
  459                 KUE_UNLOCK(sc);
  460                 mtx_destroy(&sc->kue_mtx);
  461                 return ENXIO;
  462         }
  463 
  464         /* Reset the adapter. */
  465         kue_reset(sc);
  466 
  467         /* Read ethernet descriptor */
  468         err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
  469             0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
  470 
  471         sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
  472             M_USBDEV, M_NOWAIT);
  473 
  474         ifp = sc->kue_ifp = if_alloc(IFT_ETHER);
  475         if (ifp == NULL) {
  476                 device_printf(sc->kue_dev, "can not if_alloc()\n");
  477                 KUE_UNLOCK(sc);
  478                 mtx_destroy(&sc->kue_mtx);
  479                 return ENXIO;
  480         }
  481         ifp->if_softc = sc;
  482         if_initname(ifp, "kue", device_get_unit(sc->kue_dev));
  483         ifp->if_mtu = ETHERMTU;
  484         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
  485             IFF_NEEDSGIANT;
  486         ifp->if_ioctl = kue_ioctl;
  487         ifp->if_start = kue_start;
  488         ifp->if_watchdog = kue_watchdog;
  489         ifp->if_init = kue_init;
  490         ifp->if_baudrate = 10000000;
  491         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  492 
  493         sc->kue_qdat.ifp = ifp;
  494         sc->kue_qdat.if_rxstart = kue_rxstart;
  495 
  496         /*
  497          * Call MI attach routine.
  498          */
  499         ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
  500         usb_register_netisr();
  501         sc->kue_dying = 0;
  502 
  503         KUE_UNLOCK(sc);
  504 
  505         return 0;
  506 }
  507 
  508 static int
  509 kue_detach(device_t dev)
  510 {
  511         struct kue_softc        *sc;
  512         struct ifnet            *ifp;
  513 
  514         sc = device_get_softc(dev);
  515         KUE_LOCK(sc);
  516         ifp = sc->kue_ifp;
  517 
  518         sc->kue_dying = 1;
  519 
  520         if (ifp != NULL) {
  521                 ether_ifdetach(ifp);
  522                 if_free(ifp);
  523         }
  524 
  525         if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
  526                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
  527         if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
  528                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
  529         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
  530                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
  531 
  532         if (sc->kue_mcfilters != NULL)
  533                 free(sc->kue_mcfilters, M_USBDEV);
  534 
  535         KUE_UNLOCK(sc);
  536         mtx_destroy(&sc->kue_mtx);
  537 
  538         return(0);
  539 }
  540 
  541 static void
  542 kue_rxstart(struct ifnet *ifp)
  543 {
  544         struct kue_softc        *sc;
  545         struct ue_chain *c;
  546 
  547         sc = ifp->if_softc;
  548         KUE_LOCK(sc);
  549         c = &sc->kue_cdata.ue_rx_chain[sc->kue_cdata.ue_rx_prod];
  550 
  551         c->ue_mbuf = usb_ether_newbuf();
  552         if (c->ue_mbuf == NULL) {
  553                 device_printf(sc->kue_dev, "no memory for rx list "
  554                     "-- packet dropped!\n");
  555                 ifp->if_ierrors++;
  556                 KUE_UNLOCK(sc);
  557                 return;
  558         }
  559 
  560         /* Setup new transfer. */
  561         usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
  562             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
  563             USBD_NO_TIMEOUT, kue_rxeof);
  564         usbd_transfer(c->ue_xfer);
  565 
  566         KUE_UNLOCK(sc);
  567 
  568         return;
  569 }
  570 
  571 /*
  572  * A frame has been uploaded: pass the resulting mbuf chain up to
  573  * the higher level protocols.
  574  */
  575 static void kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
  576                       usbd_status status)
  577 {
  578         struct kue_softc        *sc;
  579         struct ue_chain *c;
  580         struct mbuf             *m;
  581         struct ifnet            *ifp;
  582         int                     total_len = 0;
  583         u_int16_t               len;
  584 
  585         c = priv;
  586         sc = c->ue_sc;
  587         KUE_LOCK(sc);
  588         ifp = sc->kue_ifp;
  589 
  590         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  591                 KUE_UNLOCK(sc);
  592                 return;
  593         }
  594 
  595         if (status != USBD_NORMAL_COMPLETION) {
  596                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
  597                         KUE_UNLOCK(sc);
  598                         return;
  599                 }
  600                 if (usbd_ratecheck(&sc->kue_rx_notice))
  601                         device_printf(sc->kue_dev, "usb error on rx: %s\n",
  602                             usbd_errstr(status));
  603                 if (status == USBD_STALLED)
  604                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
  605                 goto done;
  606         }
  607 
  608         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
  609         m = c->ue_mbuf;
  610         if (total_len <= 1)
  611                 goto done;
  612 
  613         len = *mtod(m, u_int16_t *);
  614         m_adj(m, sizeof(u_int16_t));
  615 
  616         /* No errors; receive the packet. */
  617         total_len = len;
  618 
  619         if (len < sizeof(struct ether_header)) {
  620                 ifp->if_ierrors++;
  621                 goto done;
  622         }
  623 
  624         ifp->if_ipackets++;
  625         m->m_pkthdr.rcvif = (void *)&sc->kue_qdat;
  626         m->m_pkthdr.len = m->m_len = total_len;
  627 
  628         /* Put the packet on the special USB input queue. */
  629         usb_ether_input(m);
  630         KUE_UNLOCK(sc);
  631 
  632         return;
  633 done:
  634 
  635         /* Setup new transfer. */
  636         usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
  637             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
  638             USBD_NO_TIMEOUT, kue_rxeof);
  639         usbd_transfer(c->ue_xfer);
  640         KUE_UNLOCK(sc);
  641 
  642         return;
  643 }
  644 
  645 /*
  646  * A frame was downloaded to the chip. It's safe for us to clean up
  647  * the list buffers.
  648  */
  649 
  650 static void
  651 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  652 {
  653         struct kue_softc        *sc;
  654         struct ue_chain *c;
  655         struct ifnet            *ifp;
  656         usbd_status             err;
  657 
  658         c = priv;
  659         sc = c->ue_sc;
  660         KUE_LOCK(sc);
  661 
  662         ifp = sc->kue_ifp;
  663         ifp->if_timer = 0;
  664         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  665 
  666         if (status != USBD_NORMAL_COMPLETION) {
  667                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
  668                         KUE_UNLOCK(sc);
  669                         return;
  670                 }
  671                 device_printf(sc->kue_dev, "usb error on tx: %s\n",
  672                     usbd_errstr(status));
  673                 if (status == USBD_STALLED)
  674                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
  675                 KUE_UNLOCK(sc);
  676                 return;
  677         }
  678 
  679         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
  680 
  681         if (c->ue_mbuf != NULL) {
  682                 c->ue_mbuf->m_pkthdr.rcvif = ifp;
  683                 usb_tx_done(c->ue_mbuf);
  684                 c->ue_mbuf = NULL;
  685         }
  686 
  687         if (err)
  688                 ifp->if_oerrors++;
  689         else
  690                 ifp->if_opackets++;
  691 
  692         KUE_UNLOCK(sc);
  693 
  694         return;
  695 }
  696 
  697 static int
  698 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
  699 {
  700         int                     total_len;
  701         struct ue_chain *c;
  702         usbd_status             err;
  703 
  704         c = &sc->kue_cdata.ue_tx_chain[idx];
  705 
  706         /*
  707          * Copy the mbuf data into a contiguous buffer, leaving two
  708          * bytes at the beginning to hold the frame length.
  709          */
  710         m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf + 2);
  711         c->ue_mbuf = m;
  712 
  713         total_len = m->m_pkthdr.len + 2;
  714         total_len += 64 - (total_len % 64);
  715 
  716         /* Frame length is specified in the first 2 bytes of the buffer. */
  717         c->ue_buf[0] = (u_int8_t)m->m_pkthdr.len;
  718         c->ue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
  719 
  720         usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_TX],
  721             c, c->ue_buf, total_len, 0, 10000, kue_txeof);
  722 
  723         /* Transmit */
  724         err = usbd_transfer(c->ue_xfer);
  725         if (err != USBD_IN_PROGRESS) {
  726                 kue_stop(sc);
  727                 return(EIO);
  728         }
  729 
  730         sc->kue_cdata.ue_tx_cnt++;
  731 
  732         return(0);
  733 }
  734 
  735 static void
  736 kue_start(struct ifnet *ifp)
  737 {
  738         struct kue_softc        *sc;
  739         struct mbuf             *m_head = NULL;
  740 
  741         sc = ifp->if_softc;
  742         KUE_LOCK(sc);
  743 
  744         if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
  745                 KUE_UNLOCK(sc);
  746                 return;
  747         }
  748 
  749         IF_DEQUEUE(&ifp->if_snd, m_head);
  750         if (m_head == NULL) {
  751                 KUE_UNLOCK(sc);
  752                 return;
  753         }
  754 
  755         if (kue_encap(sc, m_head, 0)) {
  756                 IF_PREPEND(&ifp->if_snd, m_head);
  757                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  758                 KUE_UNLOCK(sc);
  759                 return;
  760         }
  761 
  762         /*
  763          * If there's a BPF listener, bounce a copy of this frame
  764          * to him.
  765          */
  766         BPF_MTAP(ifp, m_head);
  767 
  768         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  769 
  770         /*
  771          * Set a timeout in case the chip goes out to lunch.
  772          */
  773         ifp->if_timer = 5;
  774         KUE_UNLOCK(sc);
  775 
  776         return;
  777 }
  778 
  779 static void
  780 kue_init(void *xsc)
  781 {
  782         struct kue_softc        *sc = xsc;
  783         struct ifnet            *ifp = sc->kue_ifp;
  784         struct ue_chain *c;
  785         usbd_status             err;
  786         int                     i;
  787 
  788         KUE_LOCK(sc);
  789 
  790         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
  791                 KUE_UNLOCK(sc);
  792                 return;
  793         }
  794 
  795         /* Set MAC address */
  796         kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
  797             0, IF_LLADDR(sc->kue_ifp), ETHER_ADDR_LEN);
  798 
  799         sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
  800 
  801          /* If we want promiscuous mode, set the allframes bit. */
  802         if (ifp->if_flags & IFF_PROMISC)
  803                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
  804 
  805         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
  806 
  807         /* I'm not sure how to tune these. */
  808 #ifdef notdef
  809         /*
  810          * Leave this one alone for now; setting it
  811          * wrong causes lockups on some machines/controllers.
  812          */
  813         kue_setword(sc, KUE_CMD_SET_SOFS, 1);
  814 #endif
  815         kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
  816 
  817         /* Init TX ring. */
  818         if (usb_ether_tx_list_init(sc, &sc->kue_cdata,
  819             sc->kue_udev) == ENOBUFS) {
  820                 device_printf(sc->kue_dev, "tx list init failed\n");
  821                 KUE_UNLOCK(sc);
  822                 return;
  823         }
  824 
  825         /* Init RX ring. */
  826         if (usb_ether_rx_list_init(sc, &sc->kue_cdata,
  827             sc->kue_udev) == ENOBUFS) {
  828                 device_printf(sc->kue_dev, "rx list init failed\n");
  829                 KUE_UNLOCK(sc);
  830                 return;
  831         }
  832 
  833         /* Load the multicast filter. */
  834         kue_setmulti(sc);
  835 
  836         /* Open RX and TX pipes. */
  837         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
  838             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
  839         if (err) {
  840                 device_printf(sc->kue_dev, "open rx pipe failed: %s\n",
  841                     usbd_errstr(err));
  842                 KUE_UNLOCK(sc);
  843                 return;
  844         }
  845 
  846         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
  847             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
  848         if (err) {
  849                 device_printf(sc->kue_dev, "open tx pipe failed: %s\n",
  850                     usbd_errstr(err));
  851                 KUE_UNLOCK(sc);
  852                 return;
  853         }
  854 
  855         /* Start up the receive pipe. */
  856         for (i = 0; i < UE_RX_LIST_CNT; i++) {
  857                 c = &sc->kue_cdata.ue_rx_chain[i];
  858                 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
  859                     c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
  860                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
  861                 usbd_transfer(c->ue_xfer);
  862         }
  863 
  864         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  865         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  866 
  867         KUE_UNLOCK(sc);
  868 
  869         return;
  870 }
  871 
  872 static int
  873 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
  874 {
  875         struct kue_softc        *sc = ifp->if_softc;
  876         int                     error = 0;
  877 
  878         KUE_LOCK(sc);
  879 
  880         switch(command) {
  881         case SIOCSIFFLAGS:
  882                 if (ifp->if_flags & IFF_UP) {
  883                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
  884                             ifp->if_flags & IFF_PROMISC &&
  885                             !(sc->kue_if_flags & IFF_PROMISC)) {
  886                                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
  887                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
  888                                     sc->kue_rxfilt);
  889                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
  890                             !(ifp->if_flags & IFF_PROMISC) &&
  891                             sc->kue_if_flags & IFF_PROMISC) {
  892                                 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
  893                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
  894                                     sc->kue_rxfilt);
  895                         } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
  896                                 kue_init(sc);
  897                 } else {
  898                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
  899                                 kue_stop(sc);
  900                 }
  901                 sc->kue_if_flags = ifp->if_flags;
  902                 error = 0;
  903                 break;
  904         case SIOCADDMULTI:
  905         case SIOCDELMULTI:
  906                 kue_setmulti(sc);
  907                 error = 0;
  908                 break;
  909         default:
  910                 error = ether_ioctl(ifp, command, data);
  911                 break;
  912         }
  913 
  914         KUE_UNLOCK(sc);
  915 
  916         return(error);
  917 }
  918 
  919 static void
  920 kue_watchdog(struct ifnet *ifp)
  921 {
  922         struct kue_softc        *sc;
  923         struct ue_chain *c;
  924         usbd_status             stat;
  925 
  926         sc = ifp->if_softc;
  927         KUE_LOCK(sc);
  928         ifp->if_oerrors++;
  929         device_printf(sc->kue_dev, "watchdog timeout\n");
  930 
  931         c = &sc->kue_cdata.ue_tx_chain[0];
  932         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
  933         kue_txeof(c->ue_xfer, c, stat);
  934 
  935         if (ifp->if_snd.ifq_head != NULL)
  936                 kue_start(ifp);
  937         KUE_UNLOCK(sc);
  938 
  939         return;
  940 }
  941 
  942 /*
  943  * Stop the adapter and free any mbufs allocated to the
  944  * RX and TX lists.
  945  */
  946 static void
  947 kue_stop(struct kue_softc *sc)
  948 {
  949         usbd_status             err;
  950         struct ifnet            *ifp;
  951 
  952         KUE_LOCK(sc);
  953         ifp = sc->kue_ifp;
  954         ifp->if_timer = 0;
  955 
  956         /* Stop transfers. */
  957         if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
  958                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
  959                 if (err) {
  960                         device_printf(sc->kue_dev, "abort rx pipe failed: %s\n",
  961                             usbd_errstr(err));
  962                 }
  963                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
  964                 if (err) {
  965                         device_printf(sc->kue_dev, "close rx pipe failed: %s\n",
  966                             usbd_errstr(err));
  967                 }
  968                 sc->kue_ep[KUE_ENDPT_RX] = NULL;
  969         }
  970 
  971         if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
  972                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
  973                 if (err) {
  974                         device_printf(sc->kue_dev, "abort tx pipe failed: %s\n",
  975                             usbd_errstr(err));
  976                 }
  977                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
  978                 if (err) {
  979                         device_printf(sc->kue_dev, "close tx pipe failed: %s\n",
  980                             usbd_errstr(err));
  981                 }
  982                 sc->kue_ep[KUE_ENDPT_TX] = NULL;
  983         }
  984 
  985         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
  986                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
  987                 if (err) {
  988                         device_printf(sc->kue_dev, "abort intr pipe failed: %s\n",
  989                             usbd_errstr(err));
  990                 }
  991                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
  992                 if (err) {
  993                         device_printf(sc->kue_dev, "close intr pipe failed: %s\n",
  994                             usbd_errstr(err));
  995                 }
  996                 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
  997         }
  998 
  999         /* Free RX resources. */
 1000         usb_ether_rx_list_free(&sc->kue_cdata);
 1001         /* Free TX resources. */
 1002         usb_ether_tx_list_free(&sc->kue_cdata);
 1003 
 1004         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
 1005         KUE_UNLOCK(sc);
 1006 
 1007         return;
 1008 }
 1009 
 1010 /*
 1011  * Stop all chip I/O so that the kernel's probe routines don't
 1012  * get confused by errant DMAs when rebooting.
 1013  */
 1014 static int
 1015 kue_shutdown(device_t dev)
 1016 {
 1017         struct kue_softc        *sc;
 1018 
 1019         sc = device_get_softc(dev);
 1020 
 1021         kue_stop(sc);
 1022 
 1023         return (0);
 1024 }

Cache object: 3a57a0eec79464e9fdd3257b0ded9b50


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