2 * Copyright (c) 1997, 1998, 1999, 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.78 2003/12/17 14:23:07 sanpei Exp $
36 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
37 * Datasheet is available from http://www.admtek.com.tw.
39 * Written by Bill Paul <wpaul@ee.columbia.edu>
40 * Electrical Engineering Department
41 * Columbia University, New York City
45 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
46 * support: the control endpoint for reading/writing registers, burst
47 * read endpoint for packet reception, burst write for packet transmission
48 * and one for "interrupts." The chip uses the same RX filter scheme
49 * as the other ADMtek ethernet parts: one perfect filter entry for the
50 * the station address and a 64-bit multicast hash table. The chip supports
51 * both MII and HomePNA attachments.
53 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
54 * you're never really going to get 100Mbps speeds from this device. I
55 * think the idea is to allow the device to connect to 10 or 100Mbps
56 * networks, not necessarily to provide 100Mbps performance. Also, since
57 * the controller uses an external PHY chip, it's possible that board
58 * designers might simply choose a 10Mbps PHY.
60 * Registers are accessed using usbd_do_request(). Packet transfers are
61 * done using usbd_transfer() and friends.
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/sockio.h>
68 #include <sys/malloc.h>
69 #include <sys/kernel.h>
70 #include <sys/socket.h>
74 #include <net/ifq_var.h>
75 #include <net/if_arp.h>
76 #include <net/ethernet.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
81 #include <bus/usb/usb.h>
82 #include <bus/usb/usbdi.h>
83 #include <bus/usb/usbdi_util.h>
84 #include <bus/usb/usbdivar.h>
85 #include <bus/usb/usb_ethersubr.h>
87 #include "../mii_layer/mii.h"
88 #include "../mii_layer/miivar.h"
90 #include "if_auereg.h"
92 MODULE_DEPEND(aue, usb, 1, 1, 1);
93 MODULE_DEPEND(aue, miibus, 1, 1, 1);
95 /* "controller miibus0" required. See GENERIC if you get errors here. */
96 #include "miibus_if.h"
99 struct usb_devno aue_dev;
101 #define LSYS 0x0001 /* use Linksys reset */
102 #define PNA 0x0002 /* has Home PNA */
103 #define PII 0x0004 /* Pegasus II chip */
106 static const struct aue_type aue_devs[] = {
107 {{ USB_DEVICE(0x03f0, 0x811c) }, PII }, /* HP HN210E */
108 {{ USB_DEVICE(0x0411, 0x0001) }, 0 }, /* Melco LUA-TX */
109 {{ USB_DEVICE(0x0411, 0x0005) }, 0 }, /* Melco LUA-TX */
110 {{ USB_DEVICE(0x0411, 0x0009) }, PII }, /* Melco LUA2-TX */
111 {{ USB_DEVICE(0x045e, 0x007a) }, PII }, /* Microsoft MN110 */
112 {{ USB_DEVICE(0x04bb, 0x0904) }, 0 }, /* I-O DATA USB ETTX */
113 {{ USB_DEVICE(0x04bb, 0x0913) }, PII }, /* I-O DATA USB ETTX */
114 {{ USB_DEVICE(0x0506, 0x4601) }, PII }, /* 3com HomeConnect 3C460B */
115 {{ USB_DEVICE(0x050d, 0x0121) }, PII }, /* Belkin USB to LAN Converter */
116 {{ USB_DEVICE(0x056e, 0x200c) }, 0 }, /* Elecom LD-USB/TX */
117 {{ USB_DEVICE(0x056e, 0x4002) }, LSYS }, /* Elecom LD-USB/TX */
118 {{ USB_DEVICE(0x056e, 0x4005) }, PII }, /* Elecom LD-USBL/TX */
119 {{ USB_DEVICE(0x056e, 0x400b) }, 0 }, /* Elecom LD-USB/TX */
120 {{ USB_DEVICE(0x056e, 0xabc1) }, LSYS }, /* Elecom LD-USB/TX */
121 {{ USB_DEVICE(0x05cc, 0x3000) }, 0 }, /* Elsa Microlink USB2Ethernet */
122 {{ USB_DEVICE(0x066b, 0x200c) }, LSYS|PII }, /* Linksys USB10TX */
123 {{ USB_DEVICE(0x066b, 0x2202) }, LSYS }, /* Linksys USB10T */
124 {{ USB_DEVICE(0x066b, 0x2203) }, LSYS }, /* Linksys USB100TX */
125 {{ USB_DEVICE(0x066b, 0x2204) }, LSYS|PNA }, /* Linksys USB100H1 */
126 {{ USB_DEVICE(0x066b, 0x2206) }, LSYS }, /* Linksys USB10TA */
127 {{ USB_DEVICE(0x066b, 0x400b) }, LSYS|PII }, /* Linksys USB10TX */
128 {{ USB_DEVICE(0x067c, 0x1001) }, PII }, /* Siemens SpeedStream USB */
129 {{ USB_DEVICE(0x0707, 0x0200) }, 0 }, /* SMC 2202USB */
130 {{ USB_DEVICE(0x0707, 0x0201) }, PII }, /* SMC 2206USB */
131 {{ USB_DEVICE(0x07a6, 0x0986) }, PNA }, /* ADMtek AN986 */
132 {{ USB_DEVICE(0x07a6, 0x8511) }, PII }, /* ADMtek AN8511 */
133 {{ USB_DEVICE(0x07a6, 0x8513) }, PII }, /* ADMtek AN8513 */
134 {{ USB_DEVICE(0x07aa, 0x0004) }, 0 }, /* Corega FEther USB-TX */
135 {{ USB_DEVICE(0x07aa, 0x000d) }, PII }, /* Corega FEther USB-TXS */
136 {{ USB_DEVICE(0x07b8, 0x110c) }, PNA|PII }, /* AboCom XX1 */
137 {{ USB_DEVICE(0x07b8, 0x200c) }, PII }, /* AboCom XX2 */
138 {{ USB_DEVICE(0x07b8, 0x4002) }, LSYS }, /* AboCom UFE1000 */
139 {{ USB_DEVICE(0x07b8, 0x4003) }, 0 }, /* AboCom DSB650TX_PNA */
140 {{ USB_DEVICE(0x07b8, 0x4004) }, PNA }, /* AboCom XX4 */
141 {{ USB_DEVICE(0x07b8, 0x4007) }, PNA }, /* AboCom XX5 */
142 {{ USB_DEVICE(0x07b8, 0x400b) }, PII }, /* AboCom XX6 */
143 {{ USB_DEVICE(0x07b8, 0x400c) }, PII }, /* AboCom XX7 */
144 {{ USB_DEVICE(0x07b8, 0x4102) }, PII }, /* AboCom XX8 */
145 {{ USB_DEVICE(0x07b8, 0x4104) }, PNA }, /* AboCom XX9 */
146 {{ USB_DEVICE(0x07b8, 0xabc1) }, 0 }, /* AboCom XX10 */
147 {{ USB_DEVICE(0x083a, 0x1046) }, 0 }, /* Accton USB320-EC */
148 {{ USB_DEVICE(0x083a, 0x5046) }, PII }, /* Accton SpeedStream 1001 */
149 {{ USB_DEVICE(0x08d1, 0x0003) }, PII }, /* SmartBridges smartNIC 2 PnP */
150 {{ USB_DEVICE(0x08dd, 0x0986) }, 0 }, /* Billionton USB100N */
151 {{ USB_DEVICE(0x08dd, 0x0987) }, PNA }, /* Billionton USB100LP */
152 {{ USB_DEVICE(0x08dd, 0x0988) }, 0 }, /* Billionton USB100EL */
153 {{ USB_DEVICE(0x08dd, 0x8511) }, PII }, /* Billionton USBE100 */
154 {{ USB_DEVICE(0x0951, 0x000a) }, 0 }, /* Kingston KNU101TX */
155 {{ USB_DEVICE(0x0e66, 0x400c) }, PII }, /* Hawking UF100 */
156 {{ USB_DEVICE(0x15e8, 0x9100) }, 0 }, /* SOHOware NUB100 */
157 {{ USB_DEVICE(0x2001, 0x200c) }, LSYS|PII },/* D-Link DSB650TX4 */
158 {{ USB_DEVICE(0x2001, 0x4001) }, LSYS }, /* D-Link DSB650TX1 */
159 {{ USB_DEVICE(0x2001, 0x4002) }, LSYS }, /* D-Link DSB650TX */
160 {{ USB_DEVICE(0x2001, 0x4003) }, PNA }, /* D-Link DSB650TX_PNA */
161 {{ USB_DEVICE(0x2001, 0x400b) }, LSYS|PII }, /* D-Link DSB650TX3 */
162 {{ USB_DEVICE(0x2001, 0x4102) }, LSYS|PII }, /* D-Link DSB650TX2 */
163 {{ USB_DEVICE(0x2001, 0xabc1) }, LSYS }, /* D-Link DSB650 */
165 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
167 static int aue_match(device_t);
168 static int aue_attach(device_t);
169 static int aue_detach(device_t);
171 static void aue_reset_pegasus_II(struct aue_softc *sc);
172 static int aue_tx_list_init(struct aue_softc *);
173 static int aue_rx_list_init(struct aue_softc *);
174 static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
175 static int aue_encap(struct aue_softc *, struct mbuf *, int);
177 static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
179 static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
180 static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
181 static void aue_tick(void *);
182 static void aue_rxstart(struct ifnet *);
183 static void aue_start(struct ifnet *, struct ifaltq_subque *);
184 static int aue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
185 static void aue_init(void *);
186 static void aue_stop(struct aue_softc *);
187 static void aue_watchdog(struct ifnet *);
188 static void aue_shutdown(device_t);
189 static int aue_ifmedia_upd(struct ifnet *);
190 static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
192 static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
193 static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
194 static int aue_miibus_readreg(device_t, int, int);
195 static int aue_miibus_writereg(device_t, int, int, int);
196 static void aue_miibus_statchg(device_t);
198 static void aue_setmulti(struct aue_softc *);
199 static void aue_reset(struct aue_softc *);
201 static int aue_csr_read_1(struct aue_softc *, int);
202 static int aue_csr_write_1(struct aue_softc *, int, int);
203 static int aue_csr_read_2(struct aue_softc *, int);
204 static int aue_csr_write_2(struct aue_softc *, int, int);
206 static device_method_t aue_methods[] = {
207 /* Device interface */
208 DEVMETHOD(device_probe, aue_match),
209 DEVMETHOD(device_attach, aue_attach),
210 DEVMETHOD(device_detach, aue_detach),
211 DEVMETHOD(device_shutdown, aue_shutdown),
214 DEVMETHOD(bus_print_child, bus_generic_print_child),
215 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
218 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
219 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
220 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
225 static driver_t aue_driver = {
228 sizeof(struct aue_softc)
231 static devclass_t aue_devclass;
233 DECLARE_DUMMY_MODULE(if_aue);
234 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, NULL);
235 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, NULL, NULL);
237 #define AUE_SETBIT(sc, reg, x) \
238 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
240 #define AUE_CLRBIT(sc, reg, x) \
241 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
244 aue_csr_read_1(struct aue_softc *sc, int reg)
246 usb_device_request_t req;
255 req.bmRequestType = UT_READ_VENDOR_DEVICE;
256 req.bRequest = AUE_UR_READREG;
257 USETW(req.wValue, 0);
258 USETW(req.wIndex, reg);
259 USETW(req.wLength, 1);
261 err = usbd_do_request(sc->aue_udev, &req, &val);
273 aue_csr_read_2(struct aue_softc *sc, int reg)
275 usb_device_request_t req;
284 req.bmRequestType = UT_READ_VENDOR_DEVICE;
285 req.bRequest = AUE_UR_READREG;
286 USETW(req.wValue, 0);
287 USETW(req.wIndex, reg);
288 USETW(req.wLength, 2);
290 err = usbd_do_request(sc->aue_udev, &req, &val);
302 aue_csr_write_1(struct aue_softc *sc, int reg, int val)
304 usb_device_request_t req;
312 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
313 req.bRequest = AUE_UR_WRITEREG;
314 USETW(req.wValue, val);
315 USETW(req.wIndex, reg);
316 USETW(req.wLength, 1);
318 err = usbd_do_request(sc->aue_udev, &req, &val);
330 aue_csr_write_2(struct aue_softc *sc, int reg, int val)
332 usb_device_request_t req;
340 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
341 req.bRequest = AUE_UR_WRITEREG;
342 USETW(req.wValue, val);
343 USETW(req.wIndex, reg);
344 USETW(req.wLength, 2);
346 err = usbd_do_request(sc->aue_udev, &req, &val);
358 * Read a word of data stored in the EEPROM at address 'addr.'
361 aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
366 aue_csr_write_1(sc, AUE_EE_REG, addr);
367 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
369 for (i = 0; i < AUE_TIMEOUT; i++) {
370 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
374 if (i == AUE_TIMEOUT)
375 if_printf(&sc->arpcom.ac_if, "EEPROM read timed out\n");
377 word = aue_csr_read_2(sc, AUE_EE_DATA);
384 * Read a sequence of words from the EEPROM.
387 aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
390 u_int16_t word = 0, *ptr;
392 for (i = 0; i < cnt; i++) {
393 aue_eeprom_getword(sc, off + i, &word);
394 ptr = (u_int16_t *)(dest + (i * 2));
405 aue_miibus_readreg(device_t dev, int phy, int reg)
407 struct aue_softc *sc = device_get_softc(dev);
412 * The Am79C901 HomePNA PHY actually contains
413 * two transceivers: a 1Mbps HomePNA PHY and a
414 * 10Mbps full/half duplex ethernet PHY with
415 * NWAY autoneg. However in the ADMtek adapter,
416 * only the 1Mbps PHY is actually connected to
417 * anything, so we ignore the 10Mbps one. It
418 * happens to be configured for MII address 3,
419 * so we filter that out.
421 if (sc->aue_vendor == 0x07a6 && sc->aue_product == 0x0986) {
430 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
431 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
433 for (i = 0; i < AUE_TIMEOUT; i++) {
434 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
438 if (i == AUE_TIMEOUT)
439 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
441 val = aue_csr_read_2(sc, AUE_PHY_DATA);
447 aue_miibus_writereg(device_t dev, int phy, int reg, int data)
449 struct aue_softc *sc = device_get_softc(dev);
455 aue_csr_write_2(sc, AUE_PHY_DATA, data);
456 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
457 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
459 for (i = 0; i < AUE_TIMEOUT; i++) {
460 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
464 if (i == AUE_TIMEOUT)
465 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
471 aue_miibus_statchg(device_t dev)
473 struct aue_softc *sc = device_get_softc(dev);
474 struct mii_data *mii = GET_MII(sc);
476 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
477 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
478 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
480 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
483 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
484 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
486 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
488 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
491 * Set the LED modes on the LinkSys adapter.
492 * This turns on the 'dual link LED' bin in the auxmode
493 * register of the Broadcom PHY.
495 if (sc->aue_flags & LSYS) {
497 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
498 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
507 aue_setmulti(struct aue_softc *sc)
510 struct ifmultiaddr *ifma;
513 ifp = &sc->arpcom.ac_if;
515 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
516 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
520 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
522 /* first, zot all the existing hash bits */
523 for (i = 0; i < 8; i++)
524 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
526 /* now program new ones */
527 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
529 if (ifma->ifma_addr->sa_family != AF_LINK)
531 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
532 ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
533 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
540 aue_reset_pegasus_II(struct aue_softc *sc)
542 /* Magic constants taken from Linux driver. */
543 aue_csr_write_1(sc, AUE_REG_1D, 0);
544 aue_csr_write_1(sc, AUE_REG_7B, 2);
546 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
547 aue_csr_write_1(sc, AUE_REG_81, 6);
550 aue_csr_write_1(sc, AUE_REG_81, 2);
554 aue_reset(struct aue_softc *sc)
558 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
560 for (i = 0; i < AUE_TIMEOUT; i++) {
561 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
565 if (i == AUE_TIMEOUT)
566 if_printf(&sc->arpcom.ac_if, "reset failed\n");
569 * The PHY(s) attached to the Pegasus chip may be held
570 * in reset until we flip on the GPIO outputs. Make sure
571 * to set the GPIO pins high so that the PHY(s) will
574 * Note: We force all of the GPIO pins low first, *then*
575 * enable the ones we want.
577 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
578 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
580 if (sc->aue_flags & LSYS) {
581 /* Grrr. LinkSys has to be different from everyone else. */
582 aue_csr_write_1(sc, AUE_GPIO0,
583 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
584 aue_csr_write_1(sc, AUE_GPIO0,
585 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
588 if (sc->aue_flags & PII)
589 aue_reset_pegasus_II(sc);
591 /* Wait a little while for the chip to get its brains in order. */
598 * Probe for a Pegasus chip.
601 aue_match(device_t self)
603 struct usb_attach_arg *uaa = device_get_ivars(self);
605 if (uaa->iface != NULL)
606 return (UMATCH_NONE);
608 return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
609 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
613 * Attach the interface. Allocate softc structures, do ifmedia
614 * setup and ethernet/BPF attach.
617 aue_attach(device_t self)
619 struct aue_softc *sc = device_get_softc(self);
620 struct usb_attach_arg *uaa = device_get_ivars(self);
621 u_char eaddr[ETHER_ADDR_LEN];
623 usbd_interface_handle iface;
625 usb_interface_descriptor_t *id;
626 usb_endpoint_descriptor_t *ed;
629 sc->aue_udev = uaa->device;
630 callout_init(&sc->aue_stat_timer);
632 if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
633 device_printf(self, "setting config no %d failed\n",
638 err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
640 device_printf(self, "getting interface handle failed\n");
644 sc->aue_iface = iface;
645 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
647 sc->aue_product = uaa->product;
648 sc->aue_vendor = uaa->vendor;
650 id = usbd_get_interface_descriptor(sc->aue_iface);
652 /* Find endpoints. */
653 for (i = 0; i < id->bNumEndpoints; i++) {
654 ed = usbd_interface2endpoint_descriptor(iface, i);
656 device_printf(self, "couldn't get ep %d\n", i);
659 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
660 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
661 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
662 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
663 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
664 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
665 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
666 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
667 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
673 ifp = &sc->arpcom.ac_if;
674 if_initname(ifp, device_get_name(self), device_get_unit(self));
676 /* Reset the adapter. */
680 * Get station address from the EEPROM.
682 aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
685 ifp->if_mtu = ETHERMTU;
686 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
687 ifp->if_ioctl = aue_ioctl;
688 ifp->if_start = aue_start;
689 ifp->if_watchdog = aue_watchdog;
690 ifp->if_init = aue_init;
691 ifp->if_baudrate = 10000000;
692 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
693 ifq_set_ready(&ifp->if_snd);
697 * NOTE: Doing this causes child devices to be attached to us,
698 * which we would normally disconnect at in the detach routine
699 * using device_delete_child(). However the USB code is set up
700 * such that when this driver is removed, all children devices
701 * are removed as well. In effect, the USB code ends up detaching
702 * all of our children for us, so we don't have to do is ourselves
703 * in aue_detach(). It's important to point this out since if
704 * we *do* try to detach the child devices ourselves, we will
705 * end up getting the children deleted twice, which will crash
708 if (mii_phy_probe(self, &sc->aue_miibus,
709 aue_ifmedia_upd, aue_ifmedia_sts)) {
710 device_printf(self, "MII without any PHY!\n");
716 * Call MI attach routine.
718 ether_ifattach(ifp, eaddr, NULL);
719 usb_register_netisr();
727 aue_detach(device_t dev)
729 struct aue_softc *sc;
732 sc = device_get_softc(dev);
734 ifp = &sc->arpcom.ac_if;
737 callout_stop(&sc->aue_stat_timer);
740 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
741 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
742 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
743 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
745 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
746 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
755 * Initialize an RX descriptor and attach an MBUF cluster.
758 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
760 struct mbuf *m_new = NULL;
763 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
765 if_printf(&sc->arpcom.ac_if,
766 "no memory for rx list -- packet dropped!\n");
769 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
772 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
773 m_new->m_data = m_new->m_ext.ext_buf;
776 m_adj(m_new, ETHER_ALIGN);
783 aue_rx_list_init(struct aue_softc *sc)
785 struct aue_cdata *cd;
790 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
791 c = &cd->aue_rx_chain[i];
794 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
796 if (c->aue_xfer == NULL) {
797 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
798 if (c->aue_xfer == NULL)
807 aue_tx_list_init(struct aue_softc *sc)
809 struct aue_cdata *cd;
814 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
815 c = &cd->aue_tx_chain[i];
819 if (c->aue_xfer == NULL) {
820 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
821 if (c->aue_xfer == NULL)
824 c->aue_buf = kmalloc(AUE_BUFSZ, M_USBDEV, M_WAITOK);
832 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
834 struct aue_softc *sc = priv;
836 struct aue_intrpkt *p;
839 ifp = &sc->arpcom.ac_if;
841 if (!(ifp->if_flags & IFF_RUNNING)) {
846 if (status != USBD_NORMAL_COMPLETION) {
847 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
851 if_printf(ifp, "usb error on intr: %s\n", usbd_errstr(status));
852 if (status == USBD_STALLED)
853 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
858 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
863 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
864 ifp->if_collisions++;
872 aue_rxstart(struct ifnet *ifp)
874 struct aue_softc *sc;
879 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
881 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
882 IFNET_STAT_INC(ifp, ierrors, 1);
887 /* Setup new transfer. */
888 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
889 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
890 USBD_NO_TIMEOUT, aue_rxeof);
891 usbd_transfer(c->aue_xfer);
898 * A frame has been uploaded: pass the resulting mbuf chain up to
899 * the higher level protocols.
902 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
904 struct aue_chain *c = priv;
905 struct aue_softc *sc = c->aue_sc;
914 ifp = &sc->arpcom.ac_if;
916 if (!(ifp->if_flags & IFF_RUNNING))
919 if (status != USBD_NORMAL_COMPLETION) {
920 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
922 if (usbd_ratecheck(&sc->aue_rx_notice)) {
923 if_printf(ifp, "usb error on rx: %s\n",
924 usbd_errstr(status));
926 if (status == USBD_STALLED)
927 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
931 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
933 if (total_len <= 4 + ETHER_CRC_LEN) {
934 IFNET_STAT_INC(ifp, ierrors, 1);
939 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
941 /* Turn off all the non-error bits in the rx status word. */
942 r.aue_rxstat &= AUE_RXSTAT_MASK;
945 IFNET_STAT_INC(ifp, ierrors, 1);
949 /* No errors; receive the packet. */
950 total_len -= (4 + ETHER_CRC_LEN);
952 IFNET_STAT_INC(ifp, ipackets, 1);
953 m->m_pkthdr.rcvif = ifp;
954 m->m_pkthdr.len = m->m_len = total_len;
956 /* Put the packet on the special USB input queue. */
962 /* Setup new transfer. */
963 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
964 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
965 USBD_NO_TIMEOUT, aue_rxeof);
970 * A frame was downloaded to the chip. It's safe for us to clean up
974 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
976 struct aue_chain *c = priv;
977 struct aue_softc *sc = c->aue_sc;
981 ifp = &sc->arpcom.ac_if;
983 if (status != USBD_NORMAL_COMPLETION) {
984 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
986 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
987 if (status == USBD_STALLED)
988 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
992 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err);
994 IFNET_STAT_INC(ifp, oerrors, 1);
996 IFNET_STAT_INC(ifp, opackets, 1);
998 /* XXX should hold serializer */
1000 ifq_clr_oactive(&ifp->if_snd);
1002 if (!ifq_is_empty(&ifp->if_snd))
1003 if_devstart_sched(ifp);
1009 struct aue_softc *sc = xsc;
1011 struct mii_data *mii;
1016 ifp = &sc->arpcom.ac_if;
1018 lwkt_serialize_enter(ifp->if_serializer);
1022 lwkt_serialize_exit(ifp->if_serializer);
1027 if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1028 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1030 if (!ifq_is_empty(&ifp->if_snd))
1031 if_devstart_sched(ifp);
1034 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1036 lwkt_serialize_exit(ifp->if_serializer);
1040 aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1043 struct aue_chain *c;
1046 c = &sc->aue_cdata.aue_tx_chain[idx];
1049 * Copy the mbuf data into a contiguous buffer, leaving two
1050 * bytes at the beginning to hold the frame length.
1052 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1055 total_len = m->m_pkthdr.len + 2;
1058 * The ADMtek documentation says that the packet length is
1059 * supposed to be specified in the first two bytes of the
1060 * transfer, however it actually seems to ignore this info
1061 * and base the frame size on the bulk transfer length.
1063 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1064 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1066 m_freem(c->aue_mbuf);
1070 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1071 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER,
1075 err = usbd_transfer(c->aue_xfer);
1076 if (err != USBD_IN_PROGRESS) {
1081 sc->aue_cdata.aue_tx_cnt++;
1087 aue_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1089 struct aue_softc *sc = ifp->if_softc;
1090 struct mbuf *m_head = NULL;
1092 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1095 if (!sc->aue_link) {
1096 ifq_purge(&ifp->if_snd);
1101 if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1102 ifq_is_oactive(&ifp->if_snd)) {
1107 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1108 if (m_head == NULL) {
1113 if (aue_encap(sc, m_head, 0)) {
1114 /* aue_encap() will free m_head, if we reach here */
1115 ifq_set_oactive(&ifp->if_snd);
1121 * If there's a BPF listener, bounce a copy of this frame
1124 BPF_MTAP(ifp, m_head);
1126 ifq_set_oactive(&ifp->if_snd);
1129 * Set a timeout in case the chip goes out to lunch.
1140 struct aue_softc *sc = xsc;
1141 struct ifnet *ifp = &sc->arpcom.ac_if;
1142 struct mii_data *mii = GET_MII(sc);
1143 struct aue_chain *c;
1149 if (ifp->if_flags & IFF_RUNNING) {
1155 * Cancel pending I/O and free all RX/TX buffers.
1159 /* Set MAC address */
1160 for (i = 0; i < ETHER_ADDR_LEN; i++)
1161 aue_csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1163 /* If we want promiscuous mode, set the allframes bit. */
1164 if (ifp->if_flags & IFF_PROMISC)
1165 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1167 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1170 if (aue_tx_list_init(sc) == ENOBUFS) {
1171 if_printf(&sc->arpcom.ac_if, "tx list init failed\n");
1177 if (aue_rx_list_init(sc) == ENOBUFS) {
1178 if_printf(&sc->arpcom.ac_if, "rx list init failed\n");
1183 #ifdef AUE_INTR_PIPE
1184 sc->aue_cdata.aue_ibuf = kmalloc(AUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1187 /* Load the multicast filter. */
1190 /* Enable RX and TX */
1191 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1192 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1193 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1197 /* Open RX and TX pipes. */
1198 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1199 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1201 if_printf(&sc->arpcom.ac_if, "open rx pipe failed: %s\n",
1206 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1207 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1209 if_printf(&sc->arpcom.ac_if, "open tx pipe failed: %s\n",
1215 #ifdef AUE_INTR_PIPE
1216 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1217 USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1218 sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1221 if_printf(&sc->arpcom.ac_if, "open intr pipe failed: %s\n",
1228 /* Start up the receive pipe. */
1229 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1230 c = &sc->aue_cdata.aue_rx_chain[i];
1231 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1232 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ,
1233 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1234 usbd_transfer(c->aue_xfer);
1237 ifp->if_flags |= IFF_RUNNING;
1238 ifq_clr_oactive(&ifp->if_snd);
1240 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1248 * Set media options.
1251 aue_ifmedia_upd(struct ifnet *ifp)
1253 struct aue_softc *sc = ifp->if_softc;
1254 struct mii_data *mii = GET_MII(sc);
1257 if (mii->mii_instance) {
1258 struct mii_softc *miisc;
1259 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1260 mii_phy_reset(miisc);
1268 * Report current media status.
1271 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1273 struct aue_softc *sc = ifp->if_softc;
1274 struct mii_data *mii = GET_MII(sc);
1277 ifmr->ifm_active = mii->mii_media_active;
1278 ifmr->ifm_status = mii->mii_media_status;
1284 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1286 struct aue_softc *sc = ifp->if_softc;
1287 struct ifreq *ifr = (struct ifreq *)data;
1288 struct mii_data *mii;
1295 if (ifp->if_flags & IFF_UP) {
1296 if (ifp->if_flags & IFF_RUNNING &&
1297 ifp->if_flags & IFF_PROMISC &&
1298 !(sc->aue_if_flags & IFF_PROMISC)) {
1299 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1300 } else if (ifp->if_flags & IFF_RUNNING &&
1301 !(ifp->if_flags & IFF_PROMISC) &&
1302 sc->aue_if_flags & IFF_PROMISC) {
1303 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1304 } else if (!(ifp->if_flags & IFF_RUNNING))
1307 if (ifp->if_flags & IFF_RUNNING)
1310 sc->aue_if_flags = ifp->if_flags;
1321 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1324 error = ether_ioctl(ifp, command, data);
1334 aue_watchdog(struct ifnet *ifp)
1336 struct aue_softc *sc = ifp->if_softc;
1337 struct aue_chain *c;
1340 ASSERT_SERIALIZED(ifp->if_serializer);
1342 IFNET_STAT_INC(ifp, oerrors, 1);
1343 if_printf(ifp, "watchdog timeout\n");
1345 c = &sc->aue_cdata.aue_tx_chain[0];
1346 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1347 aue_txeof(c->aue_xfer, c, stat);
1351 * Stop the adapter and free any mbufs allocated to the
1355 aue_stop(struct aue_softc *sc)
1362 ifp = &sc->arpcom.ac_if;
1365 aue_csr_write_1(sc, AUE_CTL0, 0);
1366 aue_csr_write_1(sc, AUE_CTL1, 0);
1368 callout_stop(&sc->aue_stat_timer);
1370 /* Stop transfers. */
1371 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1372 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1374 if_printf(ifp, "abort rx pipe failed: %s\n",
1377 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1379 if_printf(ifp, "close rx pipe failed: %s\n",
1382 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1385 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1386 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1388 if_printf(ifp, "abort tx pipe failed: %s\n",
1391 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1393 if_printf(ifp, "close tx pipe failed: %s\n",
1396 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1399 #ifdef AUE_INTR_PIPE
1400 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1401 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1403 if_printf(ifp, "abort intr pipe failed: %s\n",
1406 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1408 if_printf(ifp, "close intr pipe failed: %s\n",
1411 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1415 /* Free RX resources. */
1416 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1417 if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) {
1418 kfree(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV);
1419 sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL;
1421 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1422 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1423 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1425 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1426 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1427 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1431 /* Free TX resources. */
1432 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1433 if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) {
1434 kfree(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV);
1435 sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL;
1437 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1438 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1439 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1441 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1442 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1443 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1447 #ifdef AUE_INTR_PIPE
1448 if (sc->aue_cdata.aue_ibuf != NULL) {
1449 kfree(sc->aue_cdata.aue_ibuf, M_USBDEV);
1450 sc->aue_cdata.aue_ibuf = NULL;
1456 ifp->if_flags &= ~IFF_RUNNING;
1457 ifq_clr_oactive(&ifp->if_snd);
1464 * Stop all chip I/O so that the kernel's probe routines don't
1465 * get confused by errant DMAs when rebooting.
1468 aue_shutdown(device_t dev)
1470 struct aue_softc *sc;
1473 sc = device_get_softc(dev);
1476 ifp = &sc->arpcom.ac_if;
1478 lwkt_serialize_enter(ifp->if_serializer);
1481 lwkt_serialize_exit(ifp->if_serializer);