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

          Line data    Source code
       1             : /*      $OpenBSD: if_aue.c,v 1.108 2018/07/03 14:33:43 kevlo Exp $ */
       2             : /*      $NetBSD: if_aue.c,v 1.82 2003/03/05 17:37:36 shiba Exp $        */
       3             : /*
       4             :  * Copyright (c) 1997, 1998, 1999, 2000
       5             :  *      Bill Paul <wpaul@ee.columbia.edu>.  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 Bill Paul.
      18             :  * 4. Neither the name of the author nor the names of any co-contributors
      19             :  *    may be used to endorse or promote products derived from this software
      20             :  *    without specific prior written permission.
      21             :  *
      22             :  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
      23             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      24             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      25             :  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
      26             :  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      27             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      28             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      29             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      30             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      31             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
      32             :  * THE POSSIBILITY OF SUCH DAMAGE.
      33             :  *
      34             :  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
      35             :  */
      36             : 
      37             : /*
      38             :  * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
      39             :  * Datasheet is available from http://www.admtek.com.tw.
      40             :  *
      41             :  * Written by Bill Paul <wpaul@ee.columbia.edu>
      42             :  * Electrical Engineering Department
      43             :  * Columbia University, New York City
      44             :  */
      45             : 
      46             : /*
      47             :  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
      48             :  * support: the control endpoint for reading/writing registers, burst
      49             :  * read endpoint for packet reception, burst write for packet transmission
      50             :  * and one for "interrupts." The chip uses the same RX filter scheme
      51             :  * as the other ADMtek ethernet parts: one perfect filter entry for the
      52             :  * the station address and a 64-bit multicast hash table. The chip supports
      53             :  * both MII and HomePNA attachments.
      54             :  *
      55             :  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
      56             :  * you're never really going to get 100Mbps speeds from this device. I
      57             :  * think the idea is to allow the device to connect to 10 or 100Mbps
      58             :  * networks, not necessarily to provide 100Mbps performance. Also, since
      59             :  * the controller uses an external PHY chip, it's possible that board
      60             :  * designers might simply choose a 10Mbps PHY.
      61             :  *
      62             :  * Registers are accessed using usbd_do_request(). Packet transfers are
      63             :  * done using usbd_transfer() and friends.
      64             :  */
      65             : 
      66             : /*
      67             :  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
      68             :  */
      69             : 
      70             : /*
      71             :  * TODO:
      72             :  * better error messages from rxstat
      73             :  * split out if_auevar.h
      74             :  * add thread to avoid register reads from interrupt context
      75             :  * more error checks
      76             :  * investigate short rx problem
      77             :  * proper cleanup on errors
      78             :  */
      79             : 
      80             : #include "bpfilter.h"
      81             : 
      82             : #include <sys/param.h>
      83             : #include <sys/systm.h>
      84             : #include <sys/sockio.h>
      85             : #include <sys/rwlock.h>
      86             : #include <sys/mbuf.h>
      87             : #include <sys/kernel.h>
      88             : #include <sys/socket.h>
      89             : 
      90             : #include <sys/device.h>
      91             : 
      92             : #include <net/if.h>
      93             : #include <net/if_media.h>
      94             : 
      95             : #if NBPFILTER > 0
      96             : #include <net/bpf.h>
      97             : #endif
      98             : 
      99             : #include <netinet/in.h>
     100             : #include <netinet/if_ether.h>
     101             : 
     102             : #include <dev/mii/miivar.h>
     103             : 
     104             : #include <dev/usb/usb.h>
     105             : #include <dev/usb/usbdi.h>
     106             : #include <dev/usb/usbdi_util.h>
     107             : #include <dev/usb/usbdevs.h>
     108             : 
     109             : #include <dev/usb/if_auereg.h>
     110             : 
     111             : #ifdef AUE_DEBUG
     112             : #define DPRINTF(x)      do { if (auedebug) printf x; } while (0)
     113             : #define DPRINTFN(n,x)   do { if (auedebug >= (n)) printf x; } while (0)
     114             : int     auedebug = 0;
     115             : #else
     116             : #define DPRINTF(x)
     117             : #define DPRINTFN(n,x)
     118             : #endif
     119             : 
     120             : /*
     121             :  * Various supported device vendors/products.
     122             :  */
     123             : struct aue_type {
     124             :         struct usb_devno        aue_dev;
     125             :         u_int16_t               aue_flags;
     126             : #define LSYS    0x0001          /* use Linksys reset */
     127             : #define PNA     0x0002          /* has Home PNA */
     128             : #define PII     0x0004          /* Pegasus II chip */
     129             : };
     130             : 
     131             : const struct aue_type aue_devs[] = {
     132             :  {{ USB_VENDOR_3COM,            USB_PRODUCT_3COM_3C460B},         PII },
     133             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX1},          PNA|PII },
     134             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX2},          PII },
     135             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_UFE1000},      LSYS },
     136             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX4},          PNA },
     137             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX5},          PNA },
     138             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX6},          PII },
     139             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX7},          PII },
     140             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX8},          PII },
     141             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX9},          PNA },
     142             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX10},         0 },
     143             :  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
     144             :  {{ USB_VENDOR_ACCTON,          USB_PRODUCT_ACCTON_USB320_EC},    0 },
     145             :  {{ USB_VENDOR_ACCTON,          USB_PRODUCT_ACCTON_SS1001},       PII },
     146             :  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUS},      PNA },
     147             :  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII},    PII },
     148             :  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII_2},  PII },
     149             :  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII_3},  PII },
     150             :  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII_4},  PII },
     151             :  {{ USB_VENDOR_AEI,             USB_PRODUCT_AEI_FASTETHERNET},    PII },
     152             :  {{ USB_VENDOR_ALLIEDTELESYN,   USB_PRODUCT_ALLIEDTELESYN_ATUSB100}, PII },
     153             :  {{ USB_VENDOR_ATEN,            USB_PRODUCT_ATEN_UC110T},         PII },
     154             :  {{ USB_VENDOR_BELKIN,          USB_PRODUCT_BELKIN_F5D5050},      PII },
     155             :  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USB100},   0 },
     156             :  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
     157             :  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
     158             :  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBE100},  PII },
     159             :  {{ USB_VENDOR_COREGA,          USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
     160             :  {{ USB_VENDOR_COREGA,          USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
     161             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX4},     LSYS|PII },
     162             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX1},     LSYS },
     163             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX},      LSYS },
     164             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX_PNA},  PNA },
     165             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX3},     LSYS|PII },
     166             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX2},     LSYS|PII },
     167             :  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650},        0 },
     168             :  {{ USB_VENDOR_ELCON,           USB_PRODUCT_ELCON_PLAN},          PNA|PII },
     169             :  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSB20},      PII },
     170             :  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX0},     0 },
     171             :  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX1},     LSYS },
     172             :  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX2},     0 },
     173             :  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX3},     LSYS },
     174             :  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBLTX},     PII },
     175             :  {{ USB_VENDOR_ELSA,            USB_PRODUCT_ELSA_USB2ETHERNET},   0 },
     176             :  {{ USB_VENDOR_GIGABYTE,        USB_PRODUCT_GIGABYTE_GNBR402W},   0 },
     177             :  {{ USB_VENDOR_HAWKING,         USB_PRODUCT_HAWKING_UF100},       PII },
     178             :  {{ USB_VENDOR_HP,              USB_PRODUCT_HP_HN210E},           PII },
     179             :  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_USBETTX},      0 },
     180             :  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_USBETTXS},     PII },
     181             :  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_ETXUS2},       PII },
     182             :  {{ USB_VENDOR_KINGSTON,        USB_PRODUCT_KINGSTON_KNU101TX},   0 },
     183             :  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TX1},    LSYS|PII },
     184             :  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10T},      LSYS },
     185             :  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB100TX},    LSYS },
     186             :  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB100H1},    LSYS|PNA },
     187             :  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TA},     LSYS },
     188             :  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TX2},    LSYS|PII },
     189             :  {{ USB_VENDOR_MICROSOFT,       USB_PRODUCT_MICROSOFT_MN110},     PII },
     190             :  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUATX1},        0 },
     191             :  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUATX5},        0 },
     192             :  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUA2TX5},       PII },
     193             :  {{ USB_VENDOR_MOBILITY,        USB_PRODUCT_MOBILITY_EASIDOCK},   0 },
     194             :  {{ USB_VENDOR_NETGEAR,         USB_PRODUCT_NETGEAR_FA101},       PII },
     195             :  {{ USB_VENDOR_OCT,             USB_PRODUCT_OCT_USBTOETHER},      PII },
     196             :  {{ USB_VENDOR_SIEMENS,         USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
     197             :  {{ USB_VENDOR_SMARTBRIDGES,    USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
     198             :  {{ USB_VENDOR_SMC,             USB_PRODUCT_SMC_2202USB},         0 },
     199             :  {{ USB_VENDOR_SMC,             USB_PRODUCT_SMC_2206USB},         PII },
     200             :  {{ USB_VENDOR_SOHOWARE,        USB_PRODUCT_SOHOWARE_NUB100},     0 },
     201             :  {{ USB_VENDOR_SOHOWARE,        USB_PRODUCT_SOHOWARE_NUB110},     PII },
     202             :  {{ USB_VENDOR_LOGITEC,         USB_PRODUCT_LOGITEC_LANTX},       PII },
     203             : };
     204             : #define aue_lookup(v, p) ((struct aue_type *)usb_lookup(aue_devs, v, p))
     205             : 
     206             : int aue_match(struct device *, void *, void *);
     207             : void aue_attach(struct device *, struct device *, void *);
     208             : int aue_detach(struct device *, int);
     209             : 
     210             : struct cfdriver aue_cd = {
     211             :         NULL, "aue", DV_IFNET
     212             : };
     213             : 
     214             : const struct cfattach aue_ca = {
     215             :         sizeof(struct aue_softc), aue_match, aue_attach, aue_detach
     216             : };
     217             : 
     218             : void aue_reset_pegasus_II(struct aue_softc *sc);
     219             : int aue_tx_list_init(struct aue_softc *);
     220             : int aue_rx_list_init(struct aue_softc *);
     221             : int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
     222             : int aue_send(struct aue_softc *, struct mbuf *, int);
     223             : void aue_intr(struct usbd_xfer *, void *, usbd_status);
     224             : void aue_rxeof(struct usbd_xfer *, void *, usbd_status);
     225             : void aue_txeof(struct usbd_xfer *, void *, usbd_status);
     226             : void aue_tick(void *);
     227             : void aue_tick_task(void *);
     228             : void aue_start(struct ifnet *);
     229             : int aue_ioctl(struct ifnet *, u_long, caddr_t);
     230             : void aue_init(void *);
     231             : void aue_stop(struct aue_softc *);
     232             : void aue_watchdog(struct ifnet *);
     233             : int aue_openpipes(struct aue_softc *);
     234             : int aue_ifmedia_upd(struct ifnet *);
     235             : void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
     236             : 
     237             : int aue_eeprom_getword(struct aue_softc *, int);
     238             : void aue_read_mac(struct aue_softc *, u_char *);
     239             : int aue_miibus_readreg(struct device *, int, int);
     240             : void aue_miibus_writereg(struct device *, int, int, int);
     241             : void aue_miibus_statchg(struct device *);
     242             : 
     243             : void aue_lock_mii(struct aue_softc *);
     244             : void aue_unlock_mii(struct aue_softc *);
     245             : 
     246             : void aue_iff(struct aue_softc *);
     247             : u_int32_t aue_crc(caddr_t);
     248             : void aue_reset(struct aue_softc *);
     249             : 
     250             : int aue_csr_read_1(struct aue_softc *, int);
     251             : int aue_csr_write_1(struct aue_softc *, int, int);
     252             : int aue_csr_read_2(struct aue_softc *, int);
     253             : int aue_csr_write_2(struct aue_softc *, int, int);
     254             : 
     255             : #define AUE_SETBIT(sc, reg, x)                          \
     256             :         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
     257             : 
     258             : #define AUE_CLRBIT(sc, reg, x)                          \
     259             :         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
     260             : 
     261             : int
     262           0 : aue_csr_read_1(struct aue_softc *sc, int reg)
     263             : {
     264           0 :         usb_device_request_t    req;
     265             :         usbd_status             err;
     266           0 :         uByte                   val = 0;
     267             : 
     268           0 :         if (usbd_is_dying(sc->aue_udev))
     269           0 :                 return (0);
     270             : 
     271           0 :         req.bmRequestType = UT_READ_VENDOR_DEVICE;
     272           0 :         req.bRequest = AUE_UR_READREG;
     273           0 :         USETW(req.wValue, 0);
     274           0 :         USETW(req.wIndex, reg);
     275           0 :         USETW(req.wLength, 1);
     276             : 
     277           0 :         err = usbd_do_request(sc->aue_udev, &req, &val);
     278             : 
     279           0 :         if (err) {
     280             :                 DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
     281             :                          sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
     282           0 :                 return (0);
     283             :         }
     284             : 
     285           0 :         return (val);
     286           0 : }
     287             : 
     288             : int
     289           0 : aue_csr_read_2(struct aue_softc *sc, int reg)
     290             : {
     291           0 :         usb_device_request_t    req;
     292             :         usbd_status             err;
     293           0 :         uWord                   val;
     294             : 
     295           0 :         if (usbd_is_dying(sc->aue_udev))
     296           0 :                 return (0);
     297             : 
     298           0 :         req.bmRequestType = UT_READ_VENDOR_DEVICE;
     299           0 :         req.bRequest = AUE_UR_READREG;
     300           0 :         USETW(req.wValue, 0);
     301           0 :         USETW(req.wIndex, reg);
     302           0 :         USETW(req.wLength, 2);
     303             : 
     304           0 :         err = usbd_do_request(sc->aue_udev, &req, &val);
     305             : 
     306           0 :         if (err) {
     307             :                 DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
     308             :                          sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
     309           0 :                 return (0);
     310             :         }
     311             : 
     312           0 :         return (UGETW(val));
     313           0 : }
     314             : 
     315             : int
     316           0 : aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
     317             : {
     318           0 :         usb_device_request_t    req;
     319             :         usbd_status             err;
     320           0 :         uByte                   val;
     321             : 
     322           0 :         if (usbd_is_dying(sc->aue_udev))
     323           0 :                 return (0);
     324             : 
     325           0 :         val = aval;
     326           0 :         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
     327           0 :         req.bRequest = AUE_UR_WRITEREG;
     328           0 :         USETW(req.wValue, val);
     329           0 :         USETW(req.wIndex, reg);
     330           0 :         USETW(req.wLength, 1);
     331             : 
     332           0 :         err = usbd_do_request(sc->aue_udev, &req, &val);
     333             : 
     334           0 :         if (err) {
     335             :                 DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
     336             :                          sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
     337           0 :                 return (-1);
     338             :         }
     339             : 
     340           0 :         return (0);
     341           0 : }
     342             : 
     343             : int
     344           0 : aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
     345             : {
     346           0 :         usb_device_request_t    req;
     347             :         usbd_status             err;
     348           0 :         uWord                   val;
     349             : 
     350           0 :         if (usbd_is_dying(sc->aue_udev))
     351           0 :                 return (0);
     352             : 
     353           0 :         USETW(val, aval);
     354           0 :         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
     355           0 :         req.bRequest = AUE_UR_WRITEREG;
     356           0 :         USETW(req.wValue, aval);
     357           0 :         USETW(req.wIndex, reg);
     358           0 :         USETW(req.wLength, 2);
     359             : 
     360           0 :         err = usbd_do_request(sc->aue_udev, &req, &val);
     361             : 
     362           0 :         if (err) {
     363             :                 DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
     364             :                          sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
     365           0 :                 return (-1);
     366             :         }
     367             : 
     368           0 :         return (0);
     369           0 : }
     370             : 
     371             : /*
     372             :  * Read a word of data stored in the EEPROM at address 'addr.'
     373             :  */
     374             : int
     375           0 : aue_eeprom_getword(struct aue_softc *sc, int addr)
     376             : {
     377             :         int             i;
     378             : 
     379           0 :         aue_csr_write_1(sc, AUE_EE_REG, addr);
     380           0 :         aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
     381             : 
     382           0 :         for (i = 0; i < AUE_TIMEOUT; i++) {
     383           0 :                 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
     384             :                         break;
     385             :         }
     386             : 
     387           0 :         if (i == AUE_TIMEOUT) {
     388           0 :                 printf("%s: EEPROM read timed out\n",
     389           0 :                     sc->aue_dev.dv_xname);
     390           0 :         }
     391             : 
     392           0 :         return (aue_csr_read_2(sc, AUE_EE_DATA));
     393             : }
     394             : 
     395             : /*
     396             :  * Read the MAC from the EEPROM.  It's at offset 0.
     397             :  */
     398             : void
     399           0 : aue_read_mac(struct aue_softc *sc, u_char *dest)
     400             : {
     401             :         int                     i;
     402             :         int                     off = 0;
     403             :         int                     word;
     404             : 
     405             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     406             : 
     407           0 :         for (i = 0; i < 3; i++) {
     408           0 :                 word = aue_eeprom_getword(sc, off + i);
     409           0 :                 dest[2 * i] = (u_char)word;
     410           0 :                 dest[2 * i + 1] = (u_char)(word >> 8);
     411             :         }
     412           0 : }
     413             : 
     414             : /* Get exclusive access to the MII registers */
     415             : void
     416           0 : aue_lock_mii(struct aue_softc *sc)
     417             : {
     418           0 :         sc->aue_refcnt++;
     419           0 :         rw_enter_write(&sc->aue_mii_lock);
     420           0 : }
     421             : 
     422             : void
     423           0 : aue_unlock_mii(struct aue_softc *sc)
     424             : {
     425           0 :         rw_exit_write(&sc->aue_mii_lock);
     426           0 :         if (--sc->aue_refcnt < 0)
     427           0 :                 usb_detach_wakeup(&sc->aue_dev);
     428           0 : }
     429             : 
     430             : int
     431           0 : aue_miibus_readreg(struct device *dev, int phy, int reg)
     432             : {
     433           0 :         struct aue_softc        *sc = (void *)dev;
     434             :         int                     i;
     435             :         u_int16_t               val;
     436             : 
     437           0 :         if (usbd_is_dying(sc->aue_udev)) {
     438             : #ifdef DIAGNOSTIC
     439           0 :                 printf("%s: dying\n", sc->aue_dev.dv_xname);
     440             : #endif
     441           0 :                 return 0;
     442             :         }
     443             : 
     444             : #if 0
     445             :         /*
     446             :          * The Am79C901 HomePNA PHY actually contains
     447             :          * two transceivers: a 1Mbps HomePNA PHY and a
     448             :          * 10Mbps full/half duplex ethernet PHY with
     449             :          * NWAY autoneg. However in the ADMtek adapter,
     450             :          * only the 1Mbps PHY is actually connected to
     451             :          * anything, so we ignore the 10Mbps one. It
     452             :          * happens to be configured for MII address 3,
     453             :          * so we filter that out.
     454             :          */
     455             :         if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
     456             :             sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
     457             :                 if (phy == 3)
     458             :                         return (0);
     459             :         }
     460             : #endif
     461             : 
     462           0 :         aue_lock_mii(sc);
     463           0 :         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
     464           0 :         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
     465             : 
     466           0 :         for (i = 0; i < AUE_TIMEOUT; i++) {
     467           0 :                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
     468             :                         break;
     469             :         }
     470             : 
     471           0 :         if (i == AUE_TIMEOUT) {
     472           0 :                 printf("%s: MII read timed out\n", sc->aue_dev.dv_xname);
     473           0 :         }
     474             : 
     475           0 :         val = aue_csr_read_2(sc, AUE_PHY_DATA);
     476             : 
     477             :         DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
     478             :                      sc->aue_dev.dv_xname, __func__, phy, reg, val));
     479             : 
     480           0 :         aue_unlock_mii(sc);
     481           0 :         return (val);
     482           0 : }
     483             : 
     484             : void
     485           0 : aue_miibus_writereg(struct device *dev, int phy, int reg, int data)
     486             : {
     487           0 :         struct aue_softc        *sc = (void *)dev;
     488             :         int                     i;
     489             : 
     490             : #if 0
     491             :         if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
     492             :             sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
     493             :                 if (phy == 3)
     494             :                         return;
     495             :         }
     496             : #endif
     497             : 
     498             :         DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
     499             :                      sc->aue_dev.dv_xname, __func__, phy, reg, data));
     500             : 
     501           0 :         aue_lock_mii(sc);
     502           0 :         aue_csr_write_2(sc, AUE_PHY_DATA, data);
     503           0 :         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
     504           0 :         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
     505             : 
     506           0 :         for (i = 0; i < AUE_TIMEOUT; i++) {
     507           0 :                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
     508             :                         break;
     509             :         }
     510             : 
     511           0 :         if (i == AUE_TIMEOUT) {
     512           0 :                 printf("%s: MII write timed out\n",
     513           0 :                     sc->aue_dev.dv_xname);
     514           0 :         }
     515           0 :         aue_unlock_mii(sc);
     516           0 : }
     517             : 
     518             : void
     519           0 : aue_miibus_statchg(struct device *dev)
     520             : {
     521           0 :         struct aue_softc        *sc = (void *)dev;
     522           0 :         struct mii_data         *mii = GET_MII(sc);
     523             : 
     524             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     525             : 
     526           0 :         aue_lock_mii(sc);
     527           0 :         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
     528             : 
     529           0 :         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
     530           0 :                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
     531           0 :         } else {
     532           0 :                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
     533             :         }
     534             : 
     535           0 :         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
     536           0 :                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
     537             :         else
     538           0 :                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
     539             : 
     540           0 :         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
     541           0 :         aue_unlock_mii(sc);
     542             : 
     543             :         /*
     544             :          * Set the LED modes on the LinkSys adapter.
     545             :          * This turns on the 'dual link LED' bin in the auxmode
     546             :          * register of the Broadcom PHY.
     547             :          */
     548           0 :         if (!usbd_is_dying(sc->aue_udev) && (sc->aue_flags & LSYS)) {
     549             :                 u_int16_t auxmode;
     550           0 :                 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
     551           0 :                 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
     552           0 :         }
     553             :         DPRINTFN(5,("%s: %s: exit\n", sc->aue_dev.dv_xname, __func__));
     554           0 : }
     555             : 
     556             : #define AUE_POLY        0xEDB88320
     557             : #define AUE_BITS        6
     558             : 
     559             : u_int32_t
     560           0 : aue_crc(caddr_t addr)
     561             : {
     562             :         u_int32_t               idx, bit, data, crc;
     563             : 
     564             :         /* Compute CRC for the address value. */
     565             :         crc = 0xFFFFFFFF; /* initial value */
     566             : 
     567           0 :         for (idx = 0; idx < 6; idx++) {
     568           0 :                 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
     569           0 :                         crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
     570             :         }
     571             : 
     572           0 :         return (crc & ((1 << AUE_BITS) - 1));
     573             : }
     574             : 
     575             : void
     576           0 : aue_iff(struct aue_softc *sc)
     577             : {
     578           0 :         struct ifnet            *ifp = GET_IFP(sc);
     579             :         struct arpcom           *ac = &sc->arpcom;
     580             :         struct ether_multi      *enm;
     581             :         struct ether_multistep  step;
     582             :         u_int32_t               h = 0, i;
     583             : 
     584             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     585             : 
     586           0 :         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
     587           0 :         AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
     588           0 :         ifp->if_flags &= ~IFF_ALLMULTI;
     589             : 
     590           0 :         if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
     591           0 :                 ifp->if_flags |= IFF_ALLMULTI;
     592           0 :                 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
     593           0 :                 if (ifp->if_flags & IFF_PROMISC)
     594           0 :                         AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
     595             :         } else {
     596             :                 /* first, zot all the existing hash bits */
     597           0 :                 for (i = 0; i < 8; i++)
     598           0 :                         aue_csr_write_1(sc, AUE_MAR0 + i, 0);
     599             : 
     600             :                 /* now program new ones */
     601           0 :                 ETHER_FIRST_MULTI(step, ac, enm);
     602           0 :                 while (enm != NULL) {
     603           0 :                         h = aue_crc(enm->enm_addrlo);
     604             : 
     605           0 :                         AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
     606             : 
     607           0 :                         ETHER_NEXT_MULTI(step, enm);
     608             :                 }
     609             :         }
     610           0 : }
     611             : 
     612             : void
     613           0 : aue_reset_pegasus_II(struct aue_softc *sc)
     614             : {
     615             :         /* Magic constants taken from Linux driver. */
     616           0 :         aue_csr_write_1(sc, AUE_REG_1D, 0);
     617           0 :         aue_csr_write_1(sc, AUE_REG_7B, 2);
     618             : #if 0
     619             :         if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
     620             :                 aue_csr_write_1(sc, AUE_REG_81, 6);
     621             :         else
     622             : #endif
     623           0 :                 aue_csr_write_1(sc, AUE_REG_81, 2);
     624           0 : }
     625             : 
     626             : void
     627           0 : aue_reset(struct aue_softc *sc)
     628             : {
     629             :         int             i;
     630             : 
     631             :         DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     632             : 
     633           0 :         AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
     634             : 
     635           0 :         for (i = 0; i < AUE_TIMEOUT; i++) {
     636           0 :                 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
     637             :                         break;
     638             :         }
     639             : 
     640           0 :         if (i == AUE_TIMEOUT)
     641           0 :                 printf("%s: reset failed\n", sc->aue_dev.dv_xname);
     642             : 
     643             : #if 0
     644             :         /* XXX what is mii_mode supposed to be */
     645             :         if (sc->aue_mii_mode && (sc->aue_flags & PNA))
     646             :                 aue_csr_write_1(sc, AUE_GPIO1, 0x34);
     647             :         else
     648             :                 aue_csr_write_1(sc, AUE_GPIO1, 0x26);
     649             : #endif
     650             : 
     651             :         /*
     652             :          * The PHY(s) attached to the Pegasus chip may be held
     653             :          * in reset until we flip on the GPIO outputs. Make sure
     654             :          * to set the GPIO pins high so that the PHY(s) will
     655             :          * be enabled.
     656             :          *
     657             :          * Note: We force all of the GPIO pins low first, *then*
     658             :          * enable the ones we want.
     659             :          */
     660           0 :         if (sc->aue_flags & LSYS) {
     661             :                 /* Grrr. LinkSys has to be different from everyone else. */
     662           0 :                 aue_csr_write_1(sc, AUE_GPIO0,
     663             :                     AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
     664           0 :         } else {
     665           0 :                 aue_csr_write_1(sc, AUE_GPIO0,
     666             :                     AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
     667             :         }
     668           0 :         aue_csr_write_1(sc, AUE_GPIO0,
     669             :             AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
     670             : 
     671           0 :         if (sc->aue_flags & PII)
     672           0 :                 aue_reset_pegasus_II(sc);
     673             : 
     674             :         /* Wait a little while for the chip to get its brains in order. */
     675           0 :         delay(10000);           /* XXX */
     676           0 : }
     677             : 
     678             : /*
     679             :  * Probe for a Pegasus chip.
     680             :  */
     681             : int
     682           0 : aue_match(struct device *parent, void *match, void *aux)
     683             : {
     684           0 :         struct usb_attach_arg   *uaa = aux;
     685             : 
     686           0 :         if (uaa->iface == NULL || uaa->configno != 1)
     687           0 :                 return (UMATCH_NONE);
     688             : 
     689           0 :         return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
     690             :                 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE);
     691           0 : }
     692             : 
     693             : /*
     694             :  * Attach the interface. Allocate softc structures, do ifmedia
     695             :  * setup and ethernet/BPF attach.
     696             :  */
     697             : void
     698           0 : aue_attach(struct device *parent, struct device *self, void *aux)
     699             : {
     700           0 :         struct aue_softc        *sc = (struct aue_softc *)self;
     701           0 :         struct usb_attach_arg   *uaa = aux;
     702             :         int                     s;
     703           0 :         u_char                  eaddr[ETHER_ADDR_LEN];
     704             :         struct ifnet            *ifp;
     705             :         struct mii_data         *mii;
     706           0 :         struct usbd_device      *dev = uaa->device;
     707           0 :         struct usbd_interface   *iface = uaa->iface;
     708             :         usb_interface_descriptor_t      *id;
     709             :         usb_endpoint_descriptor_t       *ed;
     710             :         int                     i;
     711             : 
     712             :         DPRINTFN(5,(" : aue_attach: sc=%p", sc));
     713             : 
     714           0 :         sc->aue_udev = dev;
     715             : 
     716           0 :         usb_init_task(&sc->aue_tick_task, aue_tick_task, sc,
     717             :             USB_TASK_TYPE_GENERIC);
     718           0 :         usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc,
     719             :             USB_TASK_TYPE_GENERIC);
     720           0 :         rw_init(&sc->aue_mii_lock, "auemii");
     721             : 
     722           0 :         sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
     723             : 
     724           0 :         sc->aue_iface = iface;
     725           0 :         sc->aue_product = uaa->product;
     726           0 :         sc->aue_vendor = uaa->vendor;
     727             : 
     728           0 :         id = usbd_get_interface_descriptor(iface);
     729             : 
     730             :         /* Find endpoints. */
     731           0 :         for (i = 0; i < id->bNumEndpoints; i++) {
     732           0 :                 ed = usbd_interface2endpoint_descriptor(iface, i);
     733           0 :                 if (ed == NULL) {
     734           0 :                         printf("%s: couldn't get endpoint descriptor %d\n",
     735           0 :                             sc->aue_dev.dv_xname, i);
     736           0 :                         return;
     737             :                 }
     738           0 :                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
     739           0 :                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
     740           0 :                         sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
     741           0 :                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
     742           0 :                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
     743           0 :                         sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
     744           0 :                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
     745           0 :                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
     746           0 :                         sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
     747           0 :                 }
     748             :         }
     749             : 
     750           0 :         if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
     751           0 :             sc->aue_ed[AUE_ENDPT_INTR] == 0) {
     752           0 :                 printf("%s: missing endpoint\n", sc->aue_dev.dv_xname);
     753           0 :                 return;
     754             :         }
     755             : 
     756             : 
     757           0 :         s = splnet();
     758             : 
     759             :         /* Reset the adapter. */
     760           0 :         aue_reset(sc);
     761             : 
     762             :         /*
     763             :          * Get station address from the EEPROM.
     764             :          */
     765           0 :         aue_read_mac(sc, eaddr);
     766             : 
     767             :         /*
     768             :          * A Pegasus chip was detected. Inform the world.
     769             :          */
     770           0 :         ifp = GET_IFP(sc);
     771           0 :         printf("%s: address %s\n", sc->aue_dev.dv_xname,
     772           0 :             ether_sprintf(eaddr));
     773             : 
     774           0 :         bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
     775             : 
     776             :         /* Initialize interface info.*/
     777           0 :         ifp->if_softc = sc;
     778           0 :         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
     779           0 :         ifp->if_ioctl = aue_ioctl;
     780           0 :         ifp->if_start = aue_start;
     781           0 :         ifp->if_watchdog = aue_watchdog;
     782           0 :         strlcpy(ifp->if_xname, sc->aue_dev.dv_xname, IFNAMSIZ);
     783             : 
     784           0 :         ifp->if_capabilities = IFCAP_VLAN_MTU;
     785             : 
     786             :         /* Initialize MII/media info. */
     787           0 :         mii = &sc->aue_mii;
     788           0 :         mii->mii_ifp = ifp;
     789           0 :         mii->mii_readreg = aue_miibus_readreg;
     790           0 :         mii->mii_writereg = aue_miibus_writereg;
     791           0 :         mii->mii_statchg = aue_miibus_statchg;
     792           0 :         mii->mii_flags = MIIF_AUTOTSLEEP;
     793           0 :         ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
     794           0 :         mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
     795           0 :         if (LIST_FIRST(&mii->mii_phys) == NULL) {
     796           0 :                 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
     797           0 :                 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
     798           0 :         } else
     799           0 :                 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
     800             : 
     801             :         /* Attach the interface. */
     802           0 :         if_attach(ifp);
     803           0 :         ether_ifattach(ifp);
     804             : 
     805           0 :         timeout_set(&sc->aue_stat_ch, aue_tick, sc);
     806             : 
     807           0 :         splx(s);
     808           0 : }
     809             : 
     810             : int
     811           0 : aue_detach(struct device *self, int flags)
     812             : {
     813           0 :         struct aue_softc        *sc = (struct aue_softc *)self;
     814           0 :         struct ifnet            *ifp = GET_IFP(sc);
     815             :         int                     s;
     816             : 
     817             :         DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     818             : 
     819           0 :         if (timeout_initialized(&sc->aue_stat_ch))
     820           0 :                 timeout_del(&sc->aue_stat_ch);
     821             : 
     822             :         /*
     823             :          * Remove any pending tasks.  They cannot be executing because they run
     824             :          * in the same thread as detach.
     825             :          */
     826           0 :         usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
     827           0 :         usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
     828             : 
     829           0 :         s = splusb();
     830             : 
     831           0 :         if (ifp->if_flags & IFF_RUNNING)
     832           0 :                 aue_stop(sc);
     833             : 
     834           0 :         mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
     835           0 :         ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
     836           0 :         if (ifp->if_softc != NULL) {
     837           0 :                 ether_ifdetach(ifp);
     838           0 :                 if_detach(ifp);
     839           0 :         }
     840             : 
     841             : #ifdef DIAGNOSTIC
     842           0 :         if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
     843           0 :             sc->aue_ep[AUE_ENDPT_RX] != NULL ||
     844           0 :             sc->aue_ep[AUE_ENDPT_INTR] != NULL)
     845           0 :                 printf("%s: detach has active endpoints\n",
     846           0 :                        sc->aue_dev.dv_xname);
     847             : #endif
     848             : 
     849           0 :         if (--sc->aue_refcnt >= 0) {
     850             :                 /* Wait for processes to go away. */
     851           0 :                 usb_detach_wait(&sc->aue_dev);
     852           0 :         }
     853           0 :         splx(s);
     854             : 
     855           0 :         return (0);
     856             : }
     857             : 
     858             : /*
     859             :  * Initialize an RX descriptor and attach an MBUF cluster.
     860             :  */
     861             : int
     862           0 : aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
     863             : {
     864             :         struct mbuf             *m_new = NULL;
     865             : 
     866             :         DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
     867             : 
     868           0 :         if (m == NULL) {
     869           0 :                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
     870           0 :                 if (m_new == NULL) {
     871           0 :                         printf("%s: no memory for rx list "
     872           0 :                             "-- packet dropped!\n", sc->aue_dev.dv_xname);
     873           0 :                         return (ENOBUFS);
     874             :                 }
     875             : 
     876           0 :                 MCLGET(m_new, M_DONTWAIT);
     877           0 :                 if (!(m_new->m_flags & M_EXT)) {
     878           0 :                         printf("%s: no memory for rx list "
     879           0 :                             "-- packet dropped!\n", sc->aue_dev.dv_xname);
     880           0 :                         m_freem(m_new);
     881           0 :                         return (ENOBUFS);
     882             :                 }
     883           0 :                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
     884           0 :         } else {
     885             :                 m_new = m;
     886           0 :                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
     887           0 :                 m_new->m_data = m_new->m_ext.ext_buf;
     888             :         }
     889             : 
     890           0 :         m_adj(m_new, ETHER_ALIGN);
     891           0 :         c->aue_mbuf = m_new;
     892             : 
     893           0 :         return (0);
     894           0 : }
     895             : 
     896             : int
     897           0 : aue_rx_list_init(struct aue_softc *sc)
     898             : {
     899             :         struct aue_cdata        *cd;
     900             :         struct aue_chain        *c;
     901             :         int                     i;
     902             : 
     903             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     904             : 
     905           0 :         cd = &sc->aue_cdata;
     906           0 :         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
     907           0 :                 c = &cd->aue_rx_chain[i];
     908           0 :                 c->aue_sc = sc;
     909           0 :                 c->aue_idx = i;
     910           0 :                 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
     911           0 :                         return (ENOBUFS);
     912           0 :                 if (c->aue_xfer == NULL) {
     913           0 :                         c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
     914           0 :                         if (c->aue_xfer == NULL)
     915           0 :                                 return (ENOBUFS);
     916           0 :                         c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
     917           0 :                         if (c->aue_buf == NULL)
     918           0 :                                 return (ENOBUFS); /* XXX free xfer */
     919             :                 }
     920             :         }
     921             : 
     922           0 :         return (0);
     923           0 : }
     924             : 
     925             : int
     926           0 : aue_tx_list_init(struct aue_softc *sc)
     927             : {
     928             :         struct aue_cdata        *cd;
     929             :         struct aue_chain        *c;
     930             :         int                     i;
     931             : 
     932             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
     933             : 
     934           0 :         cd = &sc->aue_cdata;
     935           0 :         for (i = 0; i < AUE_TX_LIST_CNT; i++) {
     936           0 :                 c = &cd->aue_tx_chain[i];
     937           0 :                 c->aue_sc = sc;
     938           0 :                 c->aue_idx = i;
     939           0 :                 c->aue_mbuf = NULL;
     940           0 :                 if (c->aue_xfer == NULL) {
     941           0 :                         c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
     942           0 :                         if (c->aue_xfer == NULL)
     943           0 :                                 return (ENOBUFS);
     944           0 :                         c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
     945           0 :                         if (c->aue_buf == NULL)
     946           0 :                                 return (ENOBUFS);
     947             :                 }
     948             :         }
     949             : 
     950           0 :         return (0);
     951           0 : }
     952             : 
     953             : void
     954           0 : aue_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
     955             : {
     956           0 :         struct aue_softc        *sc = priv;
     957           0 :         struct ifnet            *ifp = GET_IFP(sc);
     958           0 :         struct aue_intrpkt      *p = &sc->aue_cdata.aue_ibuf;
     959             : 
     960             :         DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
     961             : 
     962           0 :         if (usbd_is_dying(sc->aue_udev))
     963           0 :                 return;
     964             : 
     965           0 :         if (!(ifp->if_flags & IFF_RUNNING))
     966           0 :                 return;
     967             : 
     968           0 :         if (status != USBD_NORMAL_COMPLETION) {
     969           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
     970           0 :                         return;
     971             :                 }
     972           0 :                 sc->aue_intr_errs++;
     973           0 :                 if (usbd_ratecheck(&sc->aue_rx_notice)) {
     974           0 :                         printf("%s: %u usb errors on intr: %s\n",
     975           0 :                             sc->aue_dev.dv_xname, sc->aue_intr_errs,
     976           0 :                             usbd_errstr(status));
     977           0 :                         sc->aue_intr_errs = 0;
     978           0 :                 }
     979           0 :                 if (status == USBD_STALLED)
     980           0 :                         usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
     981           0 :                 return;
     982             :         }
     983             : 
     984           0 :         if (p->aue_txstat0)
     985           0 :                 ifp->if_oerrors++;
     986             : 
     987           0 :         if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
     988           0 :                 ifp->if_collisions++;
     989           0 : }
     990             : 
     991             : /*
     992             :  * A frame has been uploaded: pass the resulting mbuf chain up to
     993             :  * the higher level protocols.
     994             :  */
     995             : void
     996           0 : aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
     997             : {
     998           0 :         struct aue_chain        *c = priv;
     999           0 :         struct aue_softc        *sc = c->aue_sc;
    1000           0 :         struct ifnet            *ifp = GET_IFP(sc);
    1001             :         struct mbuf             *m;
    1002           0 :         struct mbuf_list        ml = MBUF_LIST_INITIALIZER();
    1003           0 :         u_int32_t               total_len;
    1004             :         struct aue_rxpkt        r;
    1005             :         int                     s;
    1006             : 
    1007             :         DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
    1008             : 
    1009           0 :         if (usbd_is_dying(sc->aue_udev))
    1010           0 :                 return;
    1011             : 
    1012           0 :         if (!(ifp->if_flags & IFF_RUNNING))
    1013           0 :                 return;
    1014             : 
    1015           0 :         if (status != USBD_NORMAL_COMPLETION) {
    1016           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    1017           0 :                         return;
    1018           0 :                 sc->aue_rx_errs++;
    1019           0 :                 if (usbd_ratecheck(&sc->aue_rx_notice)) {
    1020           0 :                         printf("%s: %u usb errors on rx: %s\n",
    1021           0 :                             sc->aue_dev.dv_xname, sc->aue_rx_errs,
    1022           0 :                             usbd_errstr(status));
    1023           0 :                         sc->aue_rx_errs = 0;
    1024           0 :                 }
    1025           0 :                 if (status == USBD_STALLED)
    1026           0 :                         usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
    1027             :                 goto done;
    1028             :         }
    1029             : 
    1030           0 :         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    1031             : 
    1032           0 :         memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
    1033             : 
    1034           0 :         if (total_len <= 4 + ETHER_CRC_LEN) {
    1035           0 :                 ifp->if_ierrors++;
    1036           0 :                 goto done;
    1037             :         }
    1038             : 
    1039           0 :         memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
    1040             : 
    1041             :         /* Turn off all the non-error bits in the rx status word. */
    1042           0 :         r.aue_rxstat &= AUE_RXSTAT_MASK;
    1043           0 :         if (r.aue_rxstat) {
    1044           0 :                 ifp->if_ierrors++;
    1045           0 :                 goto done;
    1046             :         }
    1047             : 
    1048             :         /* No errors; receive the packet. */
    1049           0 :         m = c->aue_mbuf;
    1050           0 :         total_len -= ETHER_CRC_LEN + 4;
    1051           0 :         m->m_pkthdr.len = m->m_len = total_len;
    1052           0 :         ml_enqueue(&ml, m);
    1053             : 
    1054           0 :         if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
    1055           0 :                 ifp->if_ierrors++;
    1056           0 :                 goto done;
    1057             :         }
    1058             : 
    1059           0 :         s = splnet();
    1060           0 :         if_input(ifp, &ml);
    1061           0 :         splx(s);
    1062             : 
    1063             :  done:
    1064             : 
    1065             :         /* Setup new transfer. */
    1066           0 :         usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
    1067           0 :             c, c->aue_buf, AUE_BUFSZ,
    1068             :             USBD_SHORT_XFER_OK | USBD_NO_COPY,
    1069             :             USBD_NO_TIMEOUT, aue_rxeof);
    1070           0 :         usbd_transfer(xfer);
    1071             : 
    1072             :         DPRINTFN(10,("%s: %s: start rx\n", sc->aue_dev.dv_xname,
    1073             :                     __func__));
    1074           0 : }
    1075             : 
    1076             : /*
    1077             :  * A frame was downloaded to the chip. It's safe for us to clean up
    1078             :  * the list buffers.
    1079             :  */
    1080             : 
    1081             : void
    1082           0 : aue_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    1083             : {
    1084           0 :         struct aue_chain        *c = priv;
    1085           0 :         struct aue_softc        *sc = c->aue_sc;
    1086           0 :         struct ifnet            *ifp = GET_IFP(sc);
    1087             :         int                     s;
    1088             : 
    1089           0 :         if (usbd_is_dying(sc->aue_udev))
    1090           0 :                 return;
    1091             : 
    1092           0 :         s = splnet();
    1093             : 
    1094             :         DPRINTFN(10,("%s: %s: enter status=%d\n", sc->aue_dev.dv_xname,
    1095             :                     __func__, status));
    1096             : 
    1097           0 :         ifp->if_timer = 0;
    1098           0 :         ifq_clr_oactive(&ifp->if_snd);
    1099             : 
    1100           0 :         if (status != USBD_NORMAL_COMPLETION) {
    1101           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    1102           0 :                         splx(s);
    1103           0 :                         return;
    1104             :                 }
    1105           0 :                 ifp->if_oerrors++;
    1106           0 :                 printf("%s: usb error on tx: %s\n", sc->aue_dev.dv_xname,
    1107           0 :                     usbd_errstr(status));
    1108           0 :                 if (status == USBD_STALLED)
    1109           0 :                         usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
    1110           0 :                 splx(s);
    1111           0 :                 return;
    1112             :         }
    1113             : 
    1114           0 :         m_freem(c->aue_mbuf);
    1115           0 :         c->aue_mbuf = NULL;
    1116             : 
    1117           0 :         if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    1118           0 :                 aue_start(ifp);
    1119             : 
    1120           0 :         splx(s);
    1121           0 : }
    1122             : 
    1123             : void
    1124           0 : aue_tick(void *xsc)
    1125             : {
    1126           0 :         struct aue_softc        *sc = xsc;
    1127             : 
    1128             :         DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
    1129             : 
    1130           0 :         if (sc == NULL)
    1131           0 :                 return;
    1132             : 
    1133           0 :         if (usbd_is_dying(sc->aue_udev))
    1134           0 :                 return;
    1135             : 
    1136             :         /* Perform periodic stuff in process context. */
    1137           0 :         usb_add_task(sc->aue_udev, &sc->aue_tick_task);
    1138           0 : }
    1139             : 
    1140             : void
    1141           0 : aue_tick_task(void *xsc)
    1142             : {
    1143           0 :         struct aue_softc        *sc = xsc;
    1144             :         struct ifnet            *ifp;
    1145             :         struct mii_data         *mii;
    1146             :         int                     s;
    1147             : 
    1148             :         DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
    1149             : 
    1150           0 :         if (usbd_is_dying(sc->aue_udev))
    1151           0 :                 return;
    1152             : 
    1153           0 :         ifp = GET_IFP(sc);
    1154           0 :         mii = GET_MII(sc);
    1155           0 :         if (mii == NULL)
    1156           0 :                 return;
    1157             : 
    1158           0 :         s = splnet();
    1159             : 
    1160           0 :         mii_tick(mii);
    1161           0 :         if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
    1162           0 :             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
    1163             :                 DPRINTFN(2,("%s: %s: got link\n",
    1164             :                             sc->aue_dev.dv_xname,__func__));
    1165           0 :                 sc->aue_link++;
    1166           0 :                 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    1167           0 :                         aue_start(ifp);
    1168             :         }
    1169             : 
    1170           0 :         timeout_add_sec(&sc->aue_stat_ch, 1);
    1171             : 
    1172           0 :         splx(s);
    1173           0 : }
    1174             : 
    1175             : int
    1176           0 : aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
    1177             : {
    1178             :         int                     total_len;
    1179             :         struct aue_chain        *c;
    1180             :         usbd_status             err;
    1181             : 
    1182             :         DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
    1183             : 
    1184           0 :         c = &sc->aue_cdata.aue_tx_chain[idx];
    1185             : 
    1186             :         /*
    1187             :          * Copy the mbuf data into a contiguous buffer, leaving two
    1188             :          * bytes at the beginning to hold the frame length.
    1189             :          */
    1190           0 :         m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
    1191           0 :         c->aue_mbuf = m;
    1192             : 
    1193             :         /*
    1194             :          * The ADMtek documentation says that the packet length is
    1195             :          * supposed to be specified in the first two bytes of the
    1196             :          * transfer, however it actually seems to ignore this info
    1197             :          * and base the frame size on the bulk transfer length.
    1198             :          */
    1199           0 :         c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
    1200           0 :         c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
    1201           0 :         total_len = m->m_pkthdr.len + 2;
    1202             : 
    1203           0 :         usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
    1204           0 :             c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
    1205             :             AUE_TX_TIMEOUT, aue_txeof);
    1206             : 
    1207             :         /* Transmit */
    1208           0 :         err = usbd_transfer(c->aue_xfer);
    1209           0 :         if (err != USBD_IN_PROGRESS) {
    1210           0 :                 printf("%s: aue_send error=%s\n", sc->aue_dev.dv_xname,
    1211           0 :                        usbd_errstr(err));
    1212             :                 /* Stop the interface from process context. */
    1213           0 :                 usb_add_task(sc->aue_udev, &sc->aue_stop_task);
    1214           0 :                 return (EIO);
    1215             :         }
    1216             :         DPRINTFN(5,("%s: %s: send %d bytes\n", sc->aue_dev.dv_xname,
    1217             :                     __func__, total_len));
    1218             : 
    1219           0 :         sc->aue_cdata.aue_tx_cnt++;
    1220             : 
    1221           0 :         return (0);
    1222           0 : }
    1223             : 
    1224             : void
    1225           0 : aue_start(struct ifnet *ifp)
    1226             : {
    1227           0 :         struct aue_softc        *sc = ifp->if_softc;
    1228             :         struct mbuf             *m_head = NULL;
    1229             : 
    1230             :         DPRINTFN(5,("%s: %s: enter, link=%d\n", sc->aue_dev.dv_xname,
    1231             :                     __func__, sc->aue_link));
    1232             : 
    1233           0 :         if (usbd_is_dying(sc->aue_udev))
    1234           0 :                 return;
    1235             : 
    1236           0 :         if (!sc->aue_link)
    1237           0 :                 return;
    1238             : 
    1239           0 :         if (ifq_is_oactive(&ifp->if_snd))
    1240           0 :                 return;
    1241             : 
    1242           0 :         m_head = ifq_deq_begin(&ifp->if_snd);
    1243           0 :         if (m_head == NULL)
    1244           0 :                 return;
    1245             : 
    1246           0 :         if (aue_send(sc, m_head, 0)) {
    1247           0 :                 ifq_deq_rollback(&ifp->if_snd, m_head);
    1248           0 :                 ifq_set_oactive(&ifp->if_snd);
    1249           0 :                 return;
    1250             :         }
    1251             : 
    1252           0 :         ifq_deq_commit(&ifp->if_snd, m_head);
    1253             : 
    1254             : #if NBPFILTER > 0
    1255             :         /*
    1256             :          * If there's a BPF listener, bounce a copy of this frame
    1257             :          * to him.
    1258             :          */
    1259           0 :         if (ifp->if_bpf)
    1260           0 :                 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
    1261             : #endif
    1262             : 
    1263           0 :         ifq_set_oactive(&ifp->if_snd);
    1264             : 
    1265             :         /*
    1266             :          * Set a timeout in case the chip goes out to lunch.
    1267             :          */
    1268           0 :         ifp->if_timer = 5;
    1269           0 : }
    1270             : 
    1271             : void
    1272           0 : aue_init(void *xsc)
    1273             : {
    1274           0 :         struct aue_softc        *sc = xsc;
    1275           0 :         struct ifnet            *ifp = GET_IFP(sc);
    1276           0 :         struct mii_data         *mii = GET_MII(sc);
    1277             :         int                     i, s;
    1278             :         u_char                  *eaddr;
    1279             : 
    1280             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
    1281             : 
    1282           0 :         if (usbd_is_dying(sc->aue_udev))
    1283           0 :                 return;
    1284             : 
    1285           0 :         s = splnet();
    1286             : 
    1287             :         /*
    1288             :          * Cancel pending I/O and free all RX/TX buffers.
    1289             :          */
    1290           0 :         aue_reset(sc);
    1291             : 
    1292           0 :         eaddr = sc->arpcom.ac_enaddr;
    1293           0 :         for (i = 0; i < ETHER_ADDR_LEN; i++)
    1294           0 :                 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
    1295             : 
    1296             :         /* Init TX ring. */
    1297           0 :         if (aue_tx_list_init(sc) == ENOBUFS) {
    1298           0 :                 printf("%s: tx list init failed\n", sc->aue_dev.dv_xname);
    1299           0 :                 splx(s);
    1300           0 :                 return;
    1301             :         }
    1302             : 
    1303             :         /* Init RX ring. */
    1304           0 :         if (aue_rx_list_init(sc) == ENOBUFS) {
    1305           0 :                 printf("%s: rx list init failed\n", sc->aue_dev.dv_xname);
    1306           0 :                 splx(s);
    1307           0 :                 return;
    1308             :         }
    1309             : 
    1310             :         /* Program promiscuous mode and multicast filters. */
    1311           0 :         aue_iff(sc);
    1312             : 
    1313             :         /* Enable RX and TX */
    1314           0 :         AUE_SETBIT(sc, AUE_CTL0,
    1315             :             AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    1316           0 :         AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
    1317             : 
    1318           0 :         mii_mediachg(mii);
    1319             : 
    1320           0 :         if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
    1321           0 :                 if (aue_openpipes(sc)) {
    1322           0 :                         splx(s);
    1323           0 :                         return;
    1324             :                 }
    1325             :         }
    1326             : 
    1327           0 :         ifp->if_flags |= IFF_RUNNING;
    1328           0 :         ifq_clr_oactive(&ifp->if_snd);
    1329             : 
    1330           0 :         splx(s);
    1331             : 
    1332           0 :         timeout_add_sec(&sc->aue_stat_ch, 1);
    1333           0 : }
    1334             : 
    1335             : int
    1336           0 : aue_openpipes(struct aue_softc *sc)
    1337             : {
    1338             :         struct aue_chain        *c;
    1339             :         usbd_status             err;
    1340             :         int i;
    1341             : 
    1342             :         /* Open RX and TX pipes. */
    1343           0 :         err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
    1344           0 :             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
    1345           0 :         if (err) {
    1346           0 :                 printf("%s: open rx pipe failed: %s\n",
    1347           0 :                     sc->aue_dev.dv_xname, usbd_errstr(err));
    1348           0 :                 return (EIO);
    1349             :         }
    1350           0 :         err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
    1351           0 :             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
    1352           0 :         if (err) {
    1353           0 :                 printf("%s: open tx pipe failed: %s\n",
    1354           0 :                     sc->aue_dev.dv_xname, usbd_errstr(err));
    1355           0 :                 return (EIO);
    1356             :         }
    1357           0 :         err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
    1358           0 :             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
    1359           0 :             &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
    1360             :             AUE_INTR_INTERVAL);
    1361           0 :         if (err) {
    1362           0 :                 printf("%s: open intr pipe failed: %s\n",
    1363           0 :                     sc->aue_dev.dv_xname, usbd_errstr(err));
    1364           0 :                 return (EIO);
    1365             :         }
    1366             : 
    1367             :         /* Start up the receive pipe. */
    1368           0 :         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
    1369           0 :                 c = &sc->aue_cdata.aue_rx_chain[i];
    1370           0 :                 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
    1371           0 :                     c, c->aue_buf, AUE_BUFSZ,
    1372             :                     USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
    1373             :                     aue_rxeof);
    1374           0 :                 (void)usbd_transfer(c->aue_xfer); /* XXX */
    1375             :                 DPRINTFN(5,("%s: %s: start read\n", sc->aue_dev.dv_xname,
    1376             :                             __func__));
    1377             : 
    1378             :         }
    1379           0 :         return (0);
    1380           0 : }
    1381             : 
    1382             : /*
    1383             :  * Set media options.
    1384             :  */
    1385             : int
    1386           0 : aue_ifmedia_upd(struct ifnet *ifp)
    1387             : {
    1388           0 :         struct aue_softc        *sc = ifp->if_softc;
    1389           0 :         struct mii_data         *mii = GET_MII(sc);
    1390             : 
    1391             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
    1392             : 
    1393           0 :         if (usbd_is_dying(sc->aue_udev))
    1394           0 :                 return (0);
    1395             : 
    1396           0 :         sc->aue_link = 0;
    1397           0 :         if (mii->mii_instance) {
    1398             :                 struct mii_softc        *miisc;
    1399           0 :                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    1400           0 :                         mii_phy_reset(miisc);
    1401           0 :         }
    1402           0 :         mii_mediachg(mii);
    1403             : 
    1404           0 :         return (0);
    1405           0 : }
    1406             : 
    1407             : /*
    1408             :  * Report current media status.
    1409             :  */
    1410             : void
    1411           0 : aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    1412             : {
    1413           0 :         struct aue_softc        *sc = ifp->if_softc;
    1414           0 :         struct mii_data         *mii = GET_MII(sc);
    1415             : 
    1416             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
    1417             : 
    1418           0 :         mii_pollstat(mii);
    1419           0 :         ifmr->ifm_active = mii->mii_media_active;
    1420           0 :         ifmr->ifm_status = mii->mii_media_status;
    1421           0 : }
    1422             : 
    1423             : int
    1424           0 : aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
    1425             : {
    1426           0 :         struct aue_softc        *sc = ifp->if_softc;
    1427           0 :         struct ifreq            *ifr = (struct ifreq *)data;
    1428             :         int                     s, error = 0;
    1429             : 
    1430           0 :         if (usbd_is_dying(sc->aue_udev))
    1431           0 :                 return (EIO);
    1432             : 
    1433           0 :         s = splnet();
    1434             : 
    1435           0 :         switch(command) {
    1436             :         case SIOCSIFADDR:
    1437           0 :                 ifp->if_flags |= IFF_UP;
    1438           0 :                 if (!(ifp->if_flags & IFF_RUNNING))
    1439           0 :                         aue_init(sc);
    1440             :                 break;
    1441             : 
    1442             :         case SIOCSIFFLAGS:
    1443           0 :                 if (ifp->if_flags & IFF_UP) {
    1444           0 :                         if (ifp->if_flags & IFF_RUNNING)
    1445           0 :                                 error = ENETRESET;
    1446             :                         else
    1447           0 :                                 aue_init(sc);
    1448             :                 } else {
    1449           0 :                         if (ifp->if_flags & IFF_RUNNING)
    1450           0 :                                 aue_stop(sc);
    1451             :                 }
    1452             :                 break;
    1453             : 
    1454             :         case SIOCGIFMEDIA:
    1455             :         case SIOCSIFMEDIA:
    1456           0 :                 error = ifmedia_ioctl(ifp, ifr, &sc->aue_mii.mii_media, command);
    1457           0 :                 break;
    1458             : 
    1459             :         default:
    1460           0 :                 error = ether_ioctl(ifp, &sc->arpcom, command, data);
    1461           0 :         }
    1462             : 
    1463           0 :         if (error == ENETRESET) {
    1464           0 :                 if (ifp->if_flags & IFF_RUNNING)
    1465           0 :                         aue_iff(sc);
    1466             :                 error = 0;
    1467           0 :         }
    1468             : 
    1469           0 :         splx(s);
    1470           0 :         return (error);
    1471           0 : }
    1472             : 
    1473             : void
    1474           0 : aue_watchdog(struct ifnet *ifp)
    1475             : {
    1476           0 :         struct aue_softc        *sc = ifp->if_softc;
    1477             :         struct aue_chain        *c;
    1478           0 :         usbd_status             stat;
    1479             :         int                     s;
    1480             : 
    1481             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
    1482             : 
    1483           0 :         ifp->if_oerrors++;
    1484           0 :         printf("%s: watchdog timeout\n", sc->aue_dev.dv_xname);
    1485             : 
    1486           0 :         s = splusb();
    1487           0 :         c = &sc->aue_cdata.aue_tx_chain[0];
    1488           0 :         usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
    1489           0 :         aue_txeof(c->aue_xfer, c, stat);
    1490             : 
    1491           0 :         if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    1492           0 :                 aue_start(ifp);
    1493           0 :         splx(s);
    1494           0 : }
    1495             : 
    1496             : /*
    1497             :  * Stop the adapter and free any mbufs allocated to the
    1498             :  * RX and TX lists.
    1499             :  */
    1500             : void
    1501           0 : aue_stop(struct aue_softc *sc)
    1502             : {
    1503             :         usbd_status             err;
    1504             :         struct ifnet            *ifp;
    1505             :         int                     i;
    1506             : 
    1507             :         DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
    1508             : 
    1509           0 :         ifp = GET_IFP(sc);
    1510           0 :         ifp->if_timer = 0;
    1511           0 :         ifp->if_flags &= ~IFF_RUNNING;
    1512           0 :         ifq_clr_oactive(&ifp->if_snd);
    1513             : 
    1514           0 :         aue_csr_write_1(sc, AUE_CTL0, 0);
    1515           0 :         aue_csr_write_1(sc, AUE_CTL1, 0);
    1516           0 :         aue_reset(sc);
    1517           0 :         timeout_del(&sc->aue_stat_ch);
    1518             : 
    1519             :         /* Stop transfers. */
    1520           0 :         if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
    1521           0 :                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
    1522           0 :                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
    1523           0 :                 if (err) {
    1524           0 :                         printf("%s: close rx pipe failed: %s\n",
    1525           0 :                             sc->aue_dev.dv_xname, usbd_errstr(err));
    1526           0 :                 }
    1527           0 :                 sc->aue_ep[AUE_ENDPT_RX] = NULL;
    1528           0 :         }
    1529             : 
    1530           0 :         if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
    1531           0 :                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
    1532           0 :                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
    1533           0 :                 if (err) {
    1534           0 :                         printf("%s: close tx pipe failed: %s\n",
    1535           0 :                             sc->aue_dev.dv_xname, usbd_errstr(err));
    1536           0 :                 }
    1537           0 :                 sc->aue_ep[AUE_ENDPT_TX] = NULL;
    1538           0 :         }
    1539             : 
    1540           0 :         if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
    1541           0 :                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
    1542           0 :                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
    1543           0 :                 if (err) {
    1544           0 :                         printf("%s: close intr pipe failed: %s\n",
    1545           0 :                             sc->aue_dev.dv_xname, usbd_errstr(err));
    1546           0 :                 }
    1547           0 :                 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
    1548           0 :         }
    1549             : 
    1550             :         /* Free RX resources. */
    1551           0 :         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
    1552           0 :                 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
    1553           0 :                         m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
    1554           0 :                         sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
    1555           0 :                 }
    1556           0 :                 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
    1557           0 :                         usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
    1558           0 :                         sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
    1559           0 :                 }
    1560             :         }
    1561             : 
    1562             :         /* Free TX resources. */
    1563           0 :         for (i = 0; i < AUE_TX_LIST_CNT; i++) {
    1564           0 :                 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
    1565           0 :                         m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
    1566           0 :                         sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
    1567           0 :                 }
    1568           0 :                 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
    1569           0 :                         usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
    1570           0 :                         sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
    1571           0 :                 }
    1572             :         }
    1573             : 
    1574           0 :         sc->aue_link = 0;
    1575           0 : }

Generated by: LCOV version 1.13