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 $
33 * $DragonFly: src/sys/dev/netif/aue/if_aue.c,v 1.31 2006/10/25 20:55:56 dillon Exp $
37 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
38 * Datasheet is available from http://www.admtek.com.tw.
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47 * support: the control endpoint for reading/writing registers, burst
48 * read endpoint for packet reception, burst write for packet transmission
49 * and one for "interrupts." The chip uses the same RX filter scheme
50 * as the other ADMtek ethernet parts: one perfect filter entry for the
51 * the station address and a 64-bit multicast hash table. The chip supports
52 * both MII and HomePNA attachments.
54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55 * you're never really going to get 100Mbps speeds from this device. I
56 * think the idea is to allow the device to connect to 10 or 100Mbps
57 * networks, not necessarily to provide 100Mbps performance. Also, since
58 * the controller uses an external PHY chip, it's possible that board
59 * designers might simply choose a 10Mbps PHY.
61 * Registers are accessed using usbd_do_request(). Packet transfers are
62 * done using usbd_transfer() and friends.
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/sockio.h>
69 #include <sys/malloc.h>
70 #include <sys/kernel.h>
71 #include <sys/socket.h>
75 #include <net/ifq_var.h>
76 #include <net/if_arp.h>
77 #include <net/ethernet.h>
78 #include <net/if_dl.h>
79 #include <net/if_media.h>
82 #include <bus/usb/usb.h>
83 #include <bus/usb/usbdi.h>
84 #include <bus/usb/usbdi_util.h>
85 #include <bus/usb/usbdivar.h>
86 #include <bus/usb/usbdevs.h>
87 #include <bus/usb/usb_ethersubr.h>
89 #include "../mii_layer/mii.h"
90 #include "../mii_layer/miivar.h"
92 #include "if_auereg.h"
94 MODULE_DEPEND(aue, usb, 1, 1, 1);
95 MODULE_DEPEND(aue, miibus, 1, 1, 1);
97 /* "controller miibus0" required. See GENERIC if you get errors here. */
98 #include "miibus_if.h"
101 struct usb_devno aue_dev;
103 #define LSYS 0x0001 /* use Linksys reset */
104 #define PNA 0x0002 /* has Home PNA */
105 #define PII 0x0004 /* Pegasus II chip */
108 Static const struct aue_type aue_devs[] = {
109 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII },
110 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA|PII },
111 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII },
112 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS },
113 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA },
114 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA },
115 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII },
116 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII },
117 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII },
118 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA },
119 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 },
120 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
121 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 },
122 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII },
123 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA },
124 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII },
125 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII },
126 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN}, PII },
127 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 },
128 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
129 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
130 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII },
131 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
132 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
133 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS|PII },
134 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS },
135 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS },
136 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA },
137 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS|PII },
138 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS|PII },
139 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, LSYS },
140 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 },
141 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS },
142 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 },
143 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS },
144 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII },
145 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 },
146 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII },
147 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII },
148 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 },
149 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII },
150 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 },
151 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS|PII },
152 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS },
153 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS },
154 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS|PNA },
155 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS },
156 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS|PII },
157 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII },
158 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 },
159 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 },
160 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII },
161 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
162 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
163 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 },
164 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII },
165 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 },
167 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
169 Static int aue_match(device_ptr_t);
170 Static int aue_attach(device_ptr_t);
171 Static int aue_detach(device_ptr_t);
173 Static void aue_reset_pegasus_II(struct aue_softc *sc);
174 Static int aue_tx_list_init(struct aue_softc *);
175 Static int aue_rx_list_init(struct aue_softc *);
176 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
177 Static int aue_encap(struct aue_softc *, struct mbuf *, int);
179 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
181 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
182 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
183 Static void aue_tick(void *);
184 Static void aue_rxstart(struct ifnet *);
185 Static void aue_start(struct ifnet *);
186 Static int aue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
187 Static void aue_init(void *);
188 Static void aue_stop(struct aue_softc *);
189 Static void aue_watchdog(struct ifnet *);
190 Static void aue_shutdown(device_ptr_t);
191 Static int aue_ifmedia_upd(struct ifnet *);
192 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
194 Static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
195 Static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
196 Static int aue_miibus_readreg(device_ptr_t, int, int);
197 Static int aue_miibus_writereg(device_ptr_t, int, int, int);
198 Static void aue_miibus_statchg(device_ptr_t);
200 Static void aue_setmulti(struct aue_softc *);
201 Static void aue_reset(struct aue_softc *);
203 Static int aue_csr_read_1(struct aue_softc *, int);
204 Static int aue_csr_write_1(struct aue_softc *, int, int);
205 Static int aue_csr_read_2(struct aue_softc *, int);
206 Static int aue_csr_write_2(struct aue_softc *, int, int);
208 Static device_method_t aue_methods[] = {
209 /* Device interface */
210 DEVMETHOD(device_probe, aue_match),
211 DEVMETHOD(device_attach, aue_attach),
212 DEVMETHOD(device_detach, aue_detach),
213 DEVMETHOD(device_shutdown, aue_shutdown),
216 DEVMETHOD(bus_print_child, bus_generic_print_child),
217 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
220 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
221 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
222 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
227 Static driver_t aue_driver = {
230 sizeof(struct aue_softc)
233 Static devclass_t aue_devclass;
235 DECLARE_DUMMY_MODULE(if_aue);
236 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
237 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
239 #define AUE_SETBIT(sc, reg, x) \
240 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
242 #define AUE_CLRBIT(sc, reg, x) \
243 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
246 aue_csr_read_1(struct aue_softc *sc, int reg)
248 usb_device_request_t req;
257 req.bmRequestType = UT_READ_VENDOR_DEVICE;
258 req.bRequest = AUE_UR_READREG;
259 USETW(req.wValue, 0);
260 USETW(req.wIndex, reg);
261 USETW(req.wLength, 1);
263 err = usbd_do_request(sc->aue_udev, &req, &val);
275 aue_csr_read_2(struct aue_softc *sc, int reg)
277 usb_device_request_t req;
286 req.bmRequestType = UT_READ_VENDOR_DEVICE;
287 req.bRequest = AUE_UR_READREG;
288 USETW(req.wValue, 0);
289 USETW(req.wIndex, reg);
290 USETW(req.wLength, 2);
292 err = usbd_do_request(sc->aue_udev, &req, &val);
304 aue_csr_write_1(struct aue_softc *sc, int reg, int val)
306 usb_device_request_t req;
314 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
315 req.bRequest = AUE_UR_WRITEREG;
316 USETW(req.wValue, val);
317 USETW(req.wIndex, reg);
318 USETW(req.wLength, 1);
320 err = usbd_do_request(sc->aue_udev, &req, &val);
332 aue_csr_write_2(struct aue_softc *sc, int reg, int val)
334 usb_device_request_t req;
342 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
343 req.bRequest = AUE_UR_WRITEREG;
344 USETW(req.wValue, val);
345 USETW(req.wIndex, reg);
346 USETW(req.wLength, 2);
348 err = usbd_do_request(sc->aue_udev, &req, &val);
360 * Read a word of data stored in the EEPROM at address 'addr.'
363 aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
368 aue_csr_write_1(sc, AUE_EE_REG, addr);
369 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
371 for (i = 0; i < AUE_TIMEOUT; i++) {
372 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
376 if (i == AUE_TIMEOUT)
377 if_printf(&sc->arpcom.ac_if, "EEPROM read timed out\n");
379 word = aue_csr_read_2(sc, AUE_EE_DATA);
386 * Read a sequence of words from the EEPROM.
389 aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
392 u_int16_t word = 0, *ptr;
394 for (i = 0; i < cnt; i++) {
395 aue_eeprom_getword(sc, off + i, &word);
396 ptr = (u_int16_t *)(dest + (i * 2));
407 aue_miibus_readreg(device_ptr_t dev, int phy, int reg)
409 struct aue_softc *sc = USBGETSOFTC(dev);
414 * The Am79C901 HomePNA PHY actually contains
415 * two transceivers: a 1Mbps HomePNA PHY and a
416 * 10Mbps full/half duplex ethernet PHY with
417 * NWAY autoneg. However in the ADMtek adapter,
418 * only the 1Mbps PHY is actually connected to
419 * anything, so we ignore the 10Mbps one. It
420 * happens to be configured for MII address 3,
421 * so we filter that out.
423 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
424 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
433 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
434 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
436 for (i = 0; i < AUE_TIMEOUT; i++) {
437 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
441 if (i == AUE_TIMEOUT)
442 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
444 val = aue_csr_read_2(sc, AUE_PHY_DATA);
450 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
452 struct aue_softc *sc = USBGETSOFTC(dev);
458 aue_csr_write_2(sc, AUE_PHY_DATA, data);
459 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
460 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
462 for (i = 0; i < AUE_TIMEOUT; i++) {
463 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
467 if (i == AUE_TIMEOUT)
468 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
474 aue_miibus_statchg(device_ptr_t dev)
476 struct aue_softc *sc = USBGETSOFTC(dev);
477 struct mii_data *mii = GET_MII(sc);
479 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
480 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
481 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
483 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
486 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
487 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
489 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
491 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
494 * Set the LED modes on the LinkSys adapter.
495 * This turns on the 'dual link LED' bin in the auxmode
496 * register of the Broadcom PHY.
498 if (sc->aue_flags & LSYS) {
500 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
501 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
510 aue_setmulti(struct aue_softc *sc)
513 struct ifmultiaddr *ifma;
516 ifp = &sc->arpcom.ac_if;
518 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
519 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
523 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
525 /* first, zot all the existing hash bits */
526 for (i = 0; i < 8; i++)
527 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
529 /* now program new ones */
530 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
532 if (ifma->ifma_addr->sa_family != AF_LINK)
534 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
535 ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
536 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
543 aue_reset_pegasus_II(struct aue_softc *sc)
545 /* Magic constants taken from Linux driver. */
546 aue_csr_write_1(sc, AUE_REG_1D, 0);
547 aue_csr_write_1(sc, AUE_REG_7B, 2);
549 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
550 aue_csr_write_1(sc, AUE_REG_81, 6);
553 aue_csr_write_1(sc, AUE_REG_81, 2);
557 aue_reset(struct aue_softc *sc)
561 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
563 for (i = 0; i < AUE_TIMEOUT; i++) {
564 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
568 if (i == AUE_TIMEOUT)
569 if_printf(&sc->arpcom.ac_if, "reset failed\n");
572 * The PHY(s) attached to the Pegasus chip may be held
573 * in reset until we flip on the GPIO outputs. Make sure
574 * to set the GPIO pins high so that the PHY(s) will
577 * Note: We force all of the GPIO pins low first, *then*
578 * enable the ones we want.
580 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
581 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
583 if (sc->aue_flags & LSYS) {
584 /* Grrr. LinkSys has to be different from everyone else. */
585 aue_csr_write_1(sc, AUE_GPIO0,
586 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
587 aue_csr_write_1(sc, AUE_GPIO0,
588 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
591 if (sc->aue_flags & PII)
592 aue_reset_pegasus_II(sc);
594 /* Wait a little while for the chip to get its brains in order. */
601 * Probe for a Pegasus chip.
605 USB_MATCH_START(aue, uaa);
607 if (uaa->iface != NULL)
608 return (UMATCH_NONE);
610 return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
611 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
615 * Attach the interface. Allocate softc structures, do ifmedia
616 * setup and ethernet/BPF attach.
620 USB_ATTACH_START(aue, sc, uaa);
622 u_char eaddr[ETHER_ADDR_LEN];
624 usbd_interface_handle iface;
626 usb_interface_descriptor_t *id;
627 usb_endpoint_descriptor_t *ed;
630 usbd_devinfo(uaa->device, 0, devinfo);
632 sc->aue_udev = uaa->device;
633 callout_init(&sc->aue_stat_timer);
635 if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
636 device_printf(self, "setting config no %d failed\n",
638 USB_ATTACH_ERROR_RETURN;
641 err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
643 device_printf(self, "getting interface handle failed\n");
644 USB_ATTACH_ERROR_RETURN;
647 sc->aue_iface = iface;
648 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
650 sc->aue_product = uaa->product;
651 sc->aue_vendor = uaa->vendor;
653 id = usbd_get_interface_descriptor(sc->aue_iface);
655 usbd_devinfo(uaa->device, 0, devinfo);
656 device_set_desc_copy(self, devinfo);
657 device_printf(self, "%s\n", devinfo);
659 /* Find endpoints. */
660 for (i = 0; i < id->bNumEndpoints; i++) {
661 ed = usbd_interface2endpoint_descriptor(iface, i);
663 device_printf(self, "couldn't get ep %d\n", i);
664 USB_ATTACH_ERROR_RETURN;
666 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
667 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
668 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
669 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
670 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
671 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
672 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
673 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
674 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
680 ifp = &sc->arpcom.ac_if;
681 if_initname(ifp, device_get_name(self), device_get_unit(self));
683 /* Reset the adapter. */
687 * Get station address from the EEPROM.
689 aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
692 ifp->if_mtu = ETHERMTU;
693 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
694 ifp->if_ioctl = aue_ioctl;
695 ifp->if_start = aue_start;
696 ifp->if_watchdog = aue_watchdog;
697 ifp->if_init = aue_init;
698 ifp->if_baudrate = 10000000;
699 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
700 ifq_set_ready(&ifp->if_snd);
704 * NOTE: Doing this causes child devices to be attached to us,
705 * which we would normally disconnect at in the detach routine
706 * using device_delete_child(). However the USB code is set up
707 * such that when this driver is removed, all children devices
708 * are removed as well. In effect, the USB code ends up detaching
709 * all of our children for us, so we don't have to do is ourselves
710 * in aue_detach(). It's important to point this out since if
711 * we *do* try to detach the child devices ourselves, we will
712 * end up getting the children deleted twice, which will crash
715 if (mii_phy_probe(self, &sc->aue_miibus,
716 aue_ifmedia_upd, aue_ifmedia_sts)) {
717 device_printf(self, "MII without any PHY!\n");
719 USB_ATTACH_ERROR_RETURN;
723 * Call MI attach routine.
725 ether_ifattach(ifp, eaddr, NULL);
726 usb_register_netisr();
730 USB_ATTACH_SUCCESS_RETURN;
734 aue_detach(device_ptr_t dev)
736 struct aue_softc *sc;
739 sc = device_get_softc(dev);
741 ifp = &sc->arpcom.ac_if;
744 callout_stop(&sc->aue_stat_timer);
747 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
748 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
749 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
750 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
752 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
753 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
762 * Initialize an RX descriptor and attach an MBUF cluster.
765 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
767 struct mbuf *m_new = NULL;
770 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
772 if_printf(&sc->arpcom.ac_if,
773 "no memory for rx list -- packet dropped!\n");
776 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
779 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
780 m_new->m_data = m_new->m_ext.ext_buf;
783 m_adj(m_new, ETHER_ALIGN);
790 aue_rx_list_init(struct aue_softc *sc)
792 struct aue_cdata *cd;
797 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
798 c = &cd->aue_rx_chain[i];
801 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
803 if (c->aue_xfer == NULL) {
804 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
805 if (c->aue_xfer == NULL)
814 aue_tx_list_init(struct aue_softc *sc)
816 struct aue_cdata *cd;
821 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
822 c = &cd->aue_tx_chain[i];
826 if (c->aue_xfer == NULL) {
827 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
828 if (c->aue_xfer == NULL)
831 c->aue_buf = kmalloc(AUE_BUFSZ, M_USBDEV, M_WAITOK);
832 if (c->aue_buf == NULL)
841 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
843 struct aue_softc *sc = priv;
845 struct aue_intrpkt *p;
848 ifp = &sc->arpcom.ac_if;
850 if (!(ifp->if_flags & IFF_RUNNING)) {
855 if (status != USBD_NORMAL_COMPLETION) {
856 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
860 if_printf(ifp, "usb error on intr: %s\n", usbd_errstr(status));
861 if (status == USBD_STALLED)
862 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
867 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
872 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
873 ifp->if_collisions++;
881 aue_rxstart(struct ifnet *ifp)
883 struct aue_softc *sc;
888 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
890 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
896 /* Setup new transfer. */
897 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
898 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
899 USBD_NO_TIMEOUT, aue_rxeof);
900 usbd_transfer(c->aue_xfer);
907 * A frame has been uploaded: pass the resulting mbuf chain up to
908 * the higher level protocols.
911 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
913 struct aue_chain *c = priv;
914 struct aue_softc *sc = c->aue_sc;
923 ifp = &sc->arpcom.ac_if;
925 if (!(ifp->if_flags & IFF_RUNNING)) {
930 if (status != USBD_NORMAL_COMPLETION) {
931 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
935 if (usbd_ratecheck(&sc->aue_rx_notice)) {
936 if_printf(ifp, "usb error on rx: %s\n",
937 usbd_errstr(status));
939 if (status == USBD_STALLED)
940 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
944 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
946 if (total_len <= 4 + ETHER_CRC_LEN) {
952 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
954 /* Turn off all the non-error bits in the rx status word. */
955 r.aue_rxstat &= AUE_RXSTAT_MASK;
962 /* No errors; receive the packet. */
963 total_len -= (4 + ETHER_CRC_LEN);
966 m->m_pkthdr.rcvif = ifp;
967 m->m_pkthdr.len = m->m_len = total_len;
969 /* Put the packet on the special USB input queue. */
972 if (!ifq_is_empty(&ifp->if_snd))
973 (*ifp->if_start)(ifp);
978 /* Setup new transfer. */
979 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
980 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
981 USBD_NO_TIMEOUT, aue_rxeof);
989 * A frame was downloaded to the chip. It's safe for us to clean up
994 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
996 struct aue_chain *c = priv;
997 struct aue_softc *sc = c->aue_sc;
1002 ifp = &sc->arpcom.ac_if;
1004 if (status != USBD_NORMAL_COMPLETION) {
1005 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1009 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
1010 if (status == USBD_STALLED)
1011 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1017 ifp->if_flags &= ~IFF_OACTIVE;
1018 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err);
1020 if (c->aue_mbuf != NULL) {
1021 m_free(c->aue_mbuf);
1030 if (!ifq_is_empty(&ifp->if_snd))
1031 (*ifp->if_start)(ifp);
1041 struct aue_softc *sc = xsc;
1043 struct mii_data *mii;
1050 ifp = &sc->arpcom.ac_if;
1058 if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1059 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1061 if (!ifq_is_empty(&ifp->if_snd))
1065 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1073 aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1076 struct aue_chain *c;
1079 c = &sc->aue_cdata.aue_tx_chain[idx];
1082 * Copy the mbuf data into a contiguous buffer, leaving two
1083 * bytes at the beginning to hold the frame length.
1085 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1088 total_len = m->m_pkthdr.len + 2;
1091 * The ADMtek documentation says that the packet length is
1092 * supposed to be specified in the first two bytes of the
1093 * transfer, however it actually seems to ignore this info
1094 * and base the frame size on the bulk transfer length.
1096 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1097 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1099 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1100 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER,
1104 err = usbd_transfer(c->aue_xfer);
1105 if (err != USBD_IN_PROGRESS) {
1110 sc->aue_cdata.aue_tx_cnt++;
1116 aue_start(struct ifnet *ifp)
1118 struct aue_softc *sc = ifp->if_softc;
1119 struct mbuf *m_head = NULL;
1123 if (!sc->aue_link) {
1128 if (ifp->if_flags & IFF_OACTIVE) {
1133 m_head = ifq_poll(&ifp->if_snd);
1134 if (m_head == NULL) {
1139 if (aue_encap(sc, m_head, 0)) {
1140 ifp->if_flags |= IFF_OACTIVE;
1144 ifq_dequeue(&ifp->if_snd, m_head);
1147 * If there's a BPF listener, bounce a copy of this frame
1150 BPF_MTAP(ifp, m_head);
1152 ifp->if_flags |= IFF_OACTIVE;
1155 * Set a timeout in case the chip goes out to lunch.
1166 struct aue_softc *sc = xsc;
1167 struct ifnet *ifp = &sc->arpcom.ac_if;
1168 struct mii_data *mii = GET_MII(sc);
1169 struct aue_chain *c;
1175 if (ifp->if_flags & IFF_RUNNING) {
1181 * Cancel pending I/O and free all RX/TX buffers.
1185 /* Set MAC address */
1186 for (i = 0; i < ETHER_ADDR_LEN; i++)
1187 aue_csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1189 /* If we want promiscuous mode, set the allframes bit. */
1190 if (ifp->if_flags & IFF_PROMISC)
1191 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1193 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1196 if (aue_tx_list_init(sc) == ENOBUFS) {
1197 if_printf(&sc->arpcom.ac_if, "tx list init failed\n");
1203 if (aue_rx_list_init(sc) == ENOBUFS) {
1204 if_printf(&sc->arpcom.ac_if, "rx list init failed\n");
1209 #ifdef AUE_INTR_PIPE
1210 sc->aue_cdata.aue_ibuf = kmalloc(AUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1213 /* Load the multicast filter. */
1216 /* Enable RX and TX */
1217 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1218 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1219 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1223 /* Open RX and TX pipes. */
1224 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1225 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1227 if_printf(&sc->arpcom.ac_if, "open rx pipe failed: %s\n",
1232 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1233 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1235 if_printf(&sc->arpcom.ac_if, "open tx pipe failed: %s\n",
1241 #ifdef AUE_INTR_PIPE
1242 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1243 USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1244 sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1247 if_printf(&sc->arpcom.ac_if, "open intr pipe failed: %s\n",
1254 /* Start up the receive pipe. */
1255 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1256 c = &sc->aue_cdata.aue_rx_chain[i];
1257 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1258 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ,
1259 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1260 usbd_transfer(c->aue_xfer);
1263 ifp->if_flags |= IFF_RUNNING;
1264 ifp->if_flags &= ~IFF_OACTIVE;
1266 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1274 * Set media options.
1277 aue_ifmedia_upd(struct ifnet *ifp)
1279 struct aue_softc *sc = ifp->if_softc;
1280 struct mii_data *mii = GET_MII(sc);
1283 if (mii->mii_instance) {
1284 struct mii_softc *miisc;
1285 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1286 mii_phy_reset(miisc);
1294 * Report current media status.
1297 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1299 struct aue_softc *sc = ifp->if_softc;
1300 struct mii_data *mii = GET_MII(sc);
1303 ifmr->ifm_active = mii->mii_media_active;
1304 ifmr->ifm_status = mii->mii_media_status;
1310 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1312 struct aue_softc *sc = ifp->if_softc;
1313 struct ifreq *ifr = (struct ifreq *)data;
1314 struct mii_data *mii;
1321 if (ifp->if_flags & IFF_UP) {
1322 if (ifp->if_flags & IFF_RUNNING &&
1323 ifp->if_flags & IFF_PROMISC &&
1324 !(sc->aue_if_flags & IFF_PROMISC)) {
1325 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1326 } else if (ifp->if_flags & IFF_RUNNING &&
1327 !(ifp->if_flags & IFF_PROMISC) &&
1328 sc->aue_if_flags & IFF_PROMISC) {
1329 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1330 } else if (!(ifp->if_flags & IFF_RUNNING))
1333 if (ifp->if_flags & IFF_RUNNING)
1336 sc->aue_if_flags = ifp->if_flags;
1347 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1350 error = ether_ioctl(ifp, command, data);
1360 aue_watchdog(struct ifnet *ifp)
1362 struct aue_softc *sc = ifp->if_softc;
1363 struct aue_chain *c;
1369 if_printf(ifp, "watchdog timeout\n");
1371 c = &sc->aue_cdata.aue_tx_chain[0];
1372 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1373 aue_txeof(c->aue_xfer, c, stat);
1375 if (!ifq_is_empty(&ifp->if_snd))
1382 * Stop the adapter and free any mbufs allocated to the
1386 aue_stop(struct aue_softc *sc)
1393 ifp = &sc->arpcom.ac_if;
1396 aue_csr_write_1(sc, AUE_CTL0, 0);
1397 aue_csr_write_1(sc, AUE_CTL1, 0);
1399 callout_stop(&sc->aue_stat_timer);
1401 /* Stop transfers. */
1402 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1403 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1405 if_printf(ifp, "abort rx pipe failed: %s\n",
1408 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1410 if_printf(ifp, "close rx pipe failed: %s\n",
1413 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1416 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1417 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1419 if_printf(ifp, "abort tx pipe failed: %s\n",
1422 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1424 if_printf(ifp, "close tx pipe failed: %s\n",
1427 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1430 #ifdef AUE_INTR_PIPE
1431 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1432 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1434 if_printf(ifp, "abort intr pipe failed: %s\n",
1437 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1439 if_printf(ifp, "close intr pipe failed: %s\n",
1442 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1446 /* Free RX resources. */
1447 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1448 if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) {
1449 kfree(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV);
1450 sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL;
1452 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1453 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1454 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1456 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1457 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1458 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1462 /* Free TX resources. */
1463 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1464 if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) {
1465 kfree(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV);
1466 sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL;
1468 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1469 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1470 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1472 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1473 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1474 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1478 #ifdef AUE_INTR_PIPE
1479 if (sc->aue_cdata.aue_ibuf != NULL) {
1480 kfree(sc->aue_cdata.aue_ibuf, M_USBDEV);
1481 sc->aue_cdata.aue_ibuf = NULL;
1487 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1494 * Stop all chip I/O so that the kernel's probe routines don't
1495 * get confused by errant DMAs when rebooting.
1498 aue_shutdown(device_ptr_t dev)
1500 struct aue_softc *sc;
1502 sc = device_get_softc(dev);