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.30 2006/09/05 00:55:39 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>
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>
83 #include <machine/bus.h>
85 #include <bus/usb/usb.h>
86 #include <bus/usb/usbdi.h>
87 #include <bus/usb/usbdi_util.h>
88 #include <bus/usb/usbdivar.h>
89 #include <bus/usb/usbdevs.h>
90 #include <bus/usb/usb_ethersubr.h>
92 #include "../mii_layer/mii.h"
93 #include "../mii_layer/miivar.h"
95 #include "if_auereg.h"
97 MODULE_DEPEND(aue, usb, 1, 1, 1);
98 MODULE_DEPEND(aue, miibus, 1, 1, 1);
100 /* "controller miibus0" required. See GENERIC if you get errors here. */
101 #include "miibus_if.h"
104 struct usb_devno aue_dev;
106 #define LSYS 0x0001 /* use Linksys reset */
107 #define PNA 0x0002 /* has Home PNA */
108 #define PII 0x0004 /* Pegasus II chip */
111 Static const struct aue_type aue_devs[] = {
112 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII },
113 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA|PII },
114 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII },
115 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS },
116 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA },
117 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA },
118 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII },
119 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII },
120 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII },
121 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA },
122 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 },
123 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
124 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 },
125 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII },
126 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA },
127 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII },
128 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII },
129 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN}, PII },
130 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 },
131 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
132 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
133 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII },
134 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
135 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
136 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS|PII },
137 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS },
138 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS },
139 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA },
140 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS|PII },
141 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS|PII },
142 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, LSYS },
143 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 },
144 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS },
145 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 },
146 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS },
147 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII },
148 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 },
149 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII },
150 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII },
151 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 },
152 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII },
153 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 },
154 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS|PII },
155 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS },
156 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS },
157 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS|PNA },
158 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS },
159 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS|PII },
160 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII },
161 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 },
162 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 },
163 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII },
164 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
165 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
166 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 },
167 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII },
168 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 },
170 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
172 Static int aue_match(device_ptr_t);
173 Static int aue_attach(device_ptr_t);
174 Static int aue_detach(device_ptr_t);
176 Static void aue_reset_pegasus_II(struct aue_softc *sc);
177 Static int aue_tx_list_init(struct aue_softc *);
178 Static int aue_rx_list_init(struct aue_softc *);
179 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
180 Static int aue_encap(struct aue_softc *, struct mbuf *, int);
182 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
184 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
185 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
186 Static void aue_tick(void *);
187 Static void aue_rxstart(struct ifnet *);
188 Static void aue_start(struct ifnet *);
189 Static int aue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
190 Static void aue_init(void *);
191 Static void aue_stop(struct aue_softc *);
192 Static void aue_watchdog(struct ifnet *);
193 Static void aue_shutdown(device_ptr_t);
194 Static int aue_ifmedia_upd(struct ifnet *);
195 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
197 Static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
198 Static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
199 Static int aue_miibus_readreg(device_ptr_t, int, int);
200 Static int aue_miibus_writereg(device_ptr_t, int, int, int);
201 Static void aue_miibus_statchg(device_ptr_t);
203 Static void aue_setmulti(struct aue_softc *);
204 Static void aue_reset(struct aue_softc *);
206 Static int aue_csr_read_1(struct aue_softc *, int);
207 Static int aue_csr_write_1(struct aue_softc *, int, int);
208 Static int aue_csr_read_2(struct aue_softc *, int);
209 Static int aue_csr_write_2(struct aue_softc *, int, int);
211 Static device_method_t aue_methods[] = {
212 /* Device interface */
213 DEVMETHOD(device_probe, aue_match),
214 DEVMETHOD(device_attach, aue_attach),
215 DEVMETHOD(device_detach, aue_detach),
216 DEVMETHOD(device_shutdown, aue_shutdown),
219 DEVMETHOD(bus_print_child, bus_generic_print_child),
220 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
223 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
224 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
225 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
230 Static driver_t aue_driver = {
233 sizeof(struct aue_softc)
236 Static devclass_t aue_devclass;
238 DECLARE_DUMMY_MODULE(if_aue);
239 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
240 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
242 #define AUE_SETBIT(sc, reg, x) \
243 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
245 #define AUE_CLRBIT(sc, reg, x) \
246 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
249 aue_csr_read_1(struct aue_softc *sc, int reg)
251 usb_device_request_t req;
260 req.bmRequestType = UT_READ_VENDOR_DEVICE;
261 req.bRequest = AUE_UR_READREG;
262 USETW(req.wValue, 0);
263 USETW(req.wIndex, reg);
264 USETW(req.wLength, 1);
266 err = usbd_do_request(sc->aue_udev, &req, &val);
278 aue_csr_read_2(struct aue_softc *sc, int reg)
280 usb_device_request_t req;
289 req.bmRequestType = UT_READ_VENDOR_DEVICE;
290 req.bRequest = AUE_UR_READREG;
291 USETW(req.wValue, 0);
292 USETW(req.wIndex, reg);
293 USETW(req.wLength, 2);
295 err = usbd_do_request(sc->aue_udev, &req, &val);
307 aue_csr_write_1(struct aue_softc *sc, int reg, int val)
309 usb_device_request_t req;
317 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
318 req.bRequest = AUE_UR_WRITEREG;
319 USETW(req.wValue, val);
320 USETW(req.wIndex, reg);
321 USETW(req.wLength, 1);
323 err = usbd_do_request(sc->aue_udev, &req, &val);
335 aue_csr_write_2(struct aue_softc *sc, int reg, int val)
337 usb_device_request_t req;
345 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
346 req.bRequest = AUE_UR_WRITEREG;
347 USETW(req.wValue, val);
348 USETW(req.wIndex, reg);
349 USETW(req.wLength, 2);
351 err = usbd_do_request(sc->aue_udev, &req, &val);
363 * Read a word of data stored in the EEPROM at address 'addr.'
366 aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
371 aue_csr_write_1(sc, AUE_EE_REG, addr);
372 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
374 for (i = 0; i < AUE_TIMEOUT; i++) {
375 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
379 if (i == AUE_TIMEOUT)
380 if_printf(&sc->arpcom.ac_if, "EEPROM read timed out\n");
382 word = aue_csr_read_2(sc, AUE_EE_DATA);
389 * Read a sequence of words from the EEPROM.
392 aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
395 u_int16_t word = 0, *ptr;
397 for (i = 0; i < cnt; i++) {
398 aue_eeprom_getword(sc, off + i, &word);
399 ptr = (u_int16_t *)(dest + (i * 2));
410 aue_miibus_readreg(device_ptr_t dev, int phy, int reg)
412 struct aue_softc *sc = USBGETSOFTC(dev);
417 * The Am79C901 HomePNA PHY actually contains
418 * two transceivers: a 1Mbps HomePNA PHY and a
419 * 10Mbps full/half duplex ethernet PHY with
420 * NWAY autoneg. However in the ADMtek adapter,
421 * only the 1Mbps PHY is actually connected to
422 * anything, so we ignore the 10Mbps one. It
423 * happens to be configured for MII address 3,
424 * so we filter that out.
426 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
427 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
436 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
437 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
439 for (i = 0; i < AUE_TIMEOUT; i++) {
440 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
444 if (i == AUE_TIMEOUT)
445 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
447 val = aue_csr_read_2(sc, AUE_PHY_DATA);
453 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
455 struct aue_softc *sc = USBGETSOFTC(dev);
461 aue_csr_write_2(sc, AUE_PHY_DATA, data);
462 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
463 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
465 for (i = 0; i < AUE_TIMEOUT; i++) {
466 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
470 if (i == AUE_TIMEOUT)
471 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
477 aue_miibus_statchg(device_ptr_t dev)
479 struct aue_softc *sc = USBGETSOFTC(dev);
480 struct mii_data *mii = GET_MII(sc);
482 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
483 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
484 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
486 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
489 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
490 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
492 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
494 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
497 * Set the LED modes on the LinkSys adapter.
498 * This turns on the 'dual link LED' bin in the auxmode
499 * register of the Broadcom PHY.
501 if (sc->aue_flags & LSYS) {
503 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
504 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
513 aue_setmulti(struct aue_softc *sc)
516 struct ifmultiaddr *ifma;
519 ifp = &sc->arpcom.ac_if;
521 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
522 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
526 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
528 /* first, zot all the existing hash bits */
529 for (i = 0; i < 8; i++)
530 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
532 /* now program new ones */
533 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
535 if (ifma->ifma_addr->sa_family != AF_LINK)
537 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
538 ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
539 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
546 aue_reset_pegasus_II(struct aue_softc *sc)
548 /* Magic constants taken from Linux driver. */
549 aue_csr_write_1(sc, AUE_REG_1D, 0);
550 aue_csr_write_1(sc, AUE_REG_7B, 2);
552 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
553 aue_csr_write_1(sc, AUE_REG_81, 6);
556 aue_csr_write_1(sc, AUE_REG_81, 2);
560 aue_reset(struct aue_softc *sc)
564 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
566 for (i = 0; i < AUE_TIMEOUT; i++) {
567 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
571 if (i == AUE_TIMEOUT)
572 if_printf(&sc->arpcom.ac_if, "reset failed\n");
575 * The PHY(s) attached to the Pegasus chip may be held
576 * in reset until we flip on the GPIO outputs. Make sure
577 * to set the GPIO pins high so that the PHY(s) will
580 * Note: We force all of the GPIO pins low first, *then*
581 * enable the ones we want.
583 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
584 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
586 if (sc->aue_flags & LSYS) {
587 /* Grrr. LinkSys has to be different from everyone else. */
588 aue_csr_write_1(sc, AUE_GPIO0,
589 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
590 aue_csr_write_1(sc, AUE_GPIO0,
591 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
594 if (sc->aue_flags & PII)
595 aue_reset_pegasus_II(sc);
597 /* Wait a little while for the chip to get its brains in order. */
604 * Probe for a Pegasus chip.
608 USB_MATCH_START(aue, uaa);
610 if (uaa->iface != NULL)
611 return (UMATCH_NONE);
613 return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
614 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
618 * Attach the interface. Allocate softc structures, do ifmedia
619 * setup and ethernet/BPF attach.
623 USB_ATTACH_START(aue, sc, uaa);
625 u_char eaddr[ETHER_ADDR_LEN];
627 usbd_interface_handle iface;
629 usb_interface_descriptor_t *id;
630 usb_endpoint_descriptor_t *ed;
633 usbd_devinfo(uaa->device, 0, devinfo);
635 sc->aue_udev = uaa->device;
636 callout_init(&sc->aue_stat_timer);
638 if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
639 device_printf(self, "setting config no %d failed\n",
641 USB_ATTACH_ERROR_RETURN;
644 err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
646 device_printf(self, "getting interface handle failed\n");
647 USB_ATTACH_ERROR_RETURN;
650 sc->aue_iface = iface;
651 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
653 sc->aue_product = uaa->product;
654 sc->aue_vendor = uaa->vendor;
656 id = usbd_get_interface_descriptor(sc->aue_iface);
658 usbd_devinfo(uaa->device, 0, devinfo);
659 device_set_desc_copy(self, devinfo);
660 device_printf(self, "%s\n", devinfo);
662 /* Find endpoints. */
663 for (i = 0; i < id->bNumEndpoints; i++) {
664 ed = usbd_interface2endpoint_descriptor(iface, i);
666 device_printf(self, "couldn't get ep %d\n", i);
667 USB_ATTACH_ERROR_RETURN;
669 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
670 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
671 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
672 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
673 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
674 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
675 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
676 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
677 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
683 ifp = &sc->arpcom.ac_if;
684 if_initname(ifp, device_get_name(self), device_get_unit(self));
686 /* Reset the adapter. */
690 * Get station address from the EEPROM.
692 aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
695 ifp->if_mtu = ETHERMTU;
696 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
697 ifp->if_ioctl = aue_ioctl;
698 ifp->if_start = aue_start;
699 ifp->if_watchdog = aue_watchdog;
700 ifp->if_init = aue_init;
701 ifp->if_baudrate = 10000000;
702 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
703 ifq_set_ready(&ifp->if_snd);
707 * NOTE: Doing this causes child devices to be attached to us,
708 * which we would normally disconnect at in the detach routine
709 * using device_delete_child(). However the USB code is set up
710 * such that when this driver is removed, all children devices
711 * are removed as well. In effect, the USB code ends up detaching
712 * all of our children for us, so we don't have to do is ourselves
713 * in aue_detach(). It's important to point this out since if
714 * we *do* try to detach the child devices ourselves, we will
715 * end up getting the children deleted twice, which will crash
718 if (mii_phy_probe(self, &sc->aue_miibus,
719 aue_ifmedia_upd, aue_ifmedia_sts)) {
720 device_printf(self, "MII without any PHY!\n");
722 USB_ATTACH_ERROR_RETURN;
726 * Call MI attach routine.
728 ether_ifattach(ifp, eaddr, NULL);
729 usb_register_netisr();
733 USB_ATTACH_SUCCESS_RETURN;
737 aue_detach(device_ptr_t dev)
739 struct aue_softc *sc;
742 sc = device_get_softc(dev);
744 ifp = &sc->arpcom.ac_if;
747 callout_stop(&sc->aue_stat_timer);
750 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
751 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
752 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
753 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
755 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
756 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
765 * Initialize an RX descriptor and attach an MBUF cluster.
768 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
770 struct mbuf *m_new = NULL;
773 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
775 if_printf(&sc->arpcom.ac_if,
776 "no memory for rx list -- packet dropped!\n");
779 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
782 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
783 m_new->m_data = m_new->m_ext.ext_buf;
786 m_adj(m_new, ETHER_ALIGN);
793 aue_rx_list_init(struct aue_softc *sc)
795 struct aue_cdata *cd;
800 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
801 c = &cd->aue_rx_chain[i];
804 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
806 if (c->aue_xfer == NULL) {
807 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
808 if (c->aue_xfer == NULL)
817 aue_tx_list_init(struct aue_softc *sc)
819 struct aue_cdata *cd;
824 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
825 c = &cd->aue_tx_chain[i];
829 if (c->aue_xfer == NULL) {
830 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
831 if (c->aue_xfer == NULL)
834 c->aue_buf = kmalloc(AUE_BUFSZ, M_USBDEV, M_WAITOK);
835 if (c->aue_buf == NULL)
844 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
846 struct aue_softc *sc = priv;
848 struct aue_intrpkt *p;
851 ifp = &sc->arpcom.ac_if;
853 if (!(ifp->if_flags & IFF_RUNNING)) {
858 if (status != USBD_NORMAL_COMPLETION) {
859 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
863 if_printf(ifp, "usb error on intr: %s\n", usbd_errstr(status));
864 if (status == USBD_STALLED)
865 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
870 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
875 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
876 ifp->if_collisions++;
884 aue_rxstart(struct ifnet *ifp)
886 struct aue_softc *sc;
891 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
893 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
899 /* Setup new transfer. */
900 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
901 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
902 USBD_NO_TIMEOUT, aue_rxeof);
903 usbd_transfer(c->aue_xfer);
910 * A frame has been uploaded: pass the resulting mbuf chain up to
911 * the higher level protocols.
914 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
916 struct aue_chain *c = priv;
917 struct aue_softc *sc = c->aue_sc;
926 ifp = &sc->arpcom.ac_if;
928 if (!(ifp->if_flags & IFF_RUNNING)) {
933 if (status != USBD_NORMAL_COMPLETION) {
934 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
938 if (usbd_ratecheck(&sc->aue_rx_notice)) {
939 if_printf(ifp, "usb error on rx: %s\n",
940 usbd_errstr(status));
942 if (status == USBD_STALLED)
943 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
947 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
949 if (total_len <= 4 + ETHER_CRC_LEN) {
955 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
957 /* Turn off all the non-error bits in the rx status word. */
958 r.aue_rxstat &= AUE_RXSTAT_MASK;
965 /* No errors; receive the packet. */
966 total_len -= (4 + ETHER_CRC_LEN);
969 m->m_pkthdr.rcvif = ifp;
970 m->m_pkthdr.len = m->m_len = total_len;
972 /* Put the packet on the special USB input queue. */
975 if (!ifq_is_empty(&ifp->if_snd))
976 (*ifp->if_start)(ifp);
981 /* Setup new transfer. */
982 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
983 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
984 USBD_NO_TIMEOUT, aue_rxeof);
992 * A frame was downloaded to the chip. It's safe for us to clean up
997 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
999 struct aue_chain *c = priv;
1000 struct aue_softc *sc = c->aue_sc;
1005 ifp = &sc->arpcom.ac_if;
1007 if (status != USBD_NORMAL_COMPLETION) {
1008 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1012 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
1013 if (status == USBD_STALLED)
1014 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1020 ifp->if_flags &= ~IFF_OACTIVE;
1021 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err);
1023 if (c->aue_mbuf != NULL) {
1024 m_free(c->aue_mbuf);
1033 if (!ifq_is_empty(&ifp->if_snd))
1034 (*ifp->if_start)(ifp);
1044 struct aue_softc *sc = xsc;
1046 struct mii_data *mii;
1053 ifp = &sc->arpcom.ac_if;
1061 if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1062 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1064 if (!ifq_is_empty(&ifp->if_snd))
1068 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1076 aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1079 struct aue_chain *c;
1082 c = &sc->aue_cdata.aue_tx_chain[idx];
1085 * Copy the mbuf data into a contiguous buffer, leaving two
1086 * bytes at the beginning to hold the frame length.
1088 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1091 total_len = m->m_pkthdr.len + 2;
1094 * The ADMtek documentation says that the packet length is
1095 * supposed to be specified in the first two bytes of the
1096 * transfer, however it actually seems to ignore this info
1097 * and base the frame size on the bulk transfer length.
1099 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1100 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1102 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1103 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER,
1107 err = usbd_transfer(c->aue_xfer);
1108 if (err != USBD_IN_PROGRESS) {
1113 sc->aue_cdata.aue_tx_cnt++;
1119 aue_start(struct ifnet *ifp)
1121 struct aue_softc *sc = ifp->if_softc;
1122 struct mbuf *m_head = NULL;
1126 if (!sc->aue_link) {
1131 if (ifp->if_flags & IFF_OACTIVE) {
1136 m_head = ifq_poll(&ifp->if_snd);
1137 if (m_head == NULL) {
1142 if (aue_encap(sc, m_head, 0)) {
1143 ifp->if_flags |= IFF_OACTIVE;
1147 ifq_dequeue(&ifp->if_snd, m_head);
1150 * If there's a BPF listener, bounce a copy of this frame
1153 BPF_MTAP(ifp, m_head);
1155 ifp->if_flags |= IFF_OACTIVE;
1158 * Set a timeout in case the chip goes out to lunch.
1169 struct aue_softc *sc = xsc;
1170 struct ifnet *ifp = &sc->arpcom.ac_if;
1171 struct mii_data *mii = GET_MII(sc);
1172 struct aue_chain *c;
1178 if (ifp->if_flags & IFF_RUNNING) {
1184 * Cancel pending I/O and free all RX/TX buffers.
1188 /* Set MAC address */
1189 for (i = 0; i < ETHER_ADDR_LEN; i++)
1190 aue_csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1192 /* If we want promiscuous mode, set the allframes bit. */
1193 if (ifp->if_flags & IFF_PROMISC)
1194 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1196 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1199 if (aue_tx_list_init(sc) == ENOBUFS) {
1200 if_printf(&sc->arpcom.ac_if, "tx list init failed\n");
1206 if (aue_rx_list_init(sc) == ENOBUFS) {
1207 if_printf(&sc->arpcom.ac_if, "rx list init failed\n");
1212 #ifdef AUE_INTR_PIPE
1213 sc->aue_cdata.aue_ibuf = kmalloc(AUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1216 /* Load the multicast filter. */
1219 /* Enable RX and TX */
1220 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1221 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1222 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1226 /* Open RX and TX pipes. */
1227 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1228 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1230 if_printf(&sc->arpcom.ac_if, "open rx pipe failed: %s\n",
1235 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1236 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1238 if_printf(&sc->arpcom.ac_if, "open tx pipe failed: %s\n",
1244 #ifdef AUE_INTR_PIPE
1245 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1246 USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1247 sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1250 if_printf(&sc->arpcom.ac_if, "open intr pipe failed: %s\n",
1257 /* Start up the receive pipe. */
1258 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1259 c = &sc->aue_cdata.aue_rx_chain[i];
1260 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1261 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ,
1262 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1263 usbd_transfer(c->aue_xfer);
1266 ifp->if_flags |= IFF_RUNNING;
1267 ifp->if_flags &= ~IFF_OACTIVE;
1269 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1277 * Set media options.
1280 aue_ifmedia_upd(struct ifnet *ifp)
1282 struct aue_softc *sc = ifp->if_softc;
1283 struct mii_data *mii = GET_MII(sc);
1286 if (mii->mii_instance) {
1287 struct mii_softc *miisc;
1288 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1289 mii_phy_reset(miisc);
1297 * Report current media status.
1300 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1302 struct aue_softc *sc = ifp->if_softc;
1303 struct mii_data *mii = GET_MII(sc);
1306 ifmr->ifm_active = mii->mii_media_active;
1307 ifmr->ifm_status = mii->mii_media_status;
1313 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1315 struct aue_softc *sc = ifp->if_softc;
1316 struct ifreq *ifr = (struct ifreq *)data;
1317 struct mii_data *mii;
1324 if (ifp->if_flags & IFF_UP) {
1325 if (ifp->if_flags & IFF_RUNNING &&
1326 ifp->if_flags & IFF_PROMISC &&
1327 !(sc->aue_if_flags & IFF_PROMISC)) {
1328 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1329 } else if (ifp->if_flags & IFF_RUNNING &&
1330 !(ifp->if_flags & IFF_PROMISC) &&
1331 sc->aue_if_flags & IFF_PROMISC) {
1332 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1333 } else if (!(ifp->if_flags & IFF_RUNNING))
1336 if (ifp->if_flags & IFF_RUNNING)
1339 sc->aue_if_flags = ifp->if_flags;
1350 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1353 error = ether_ioctl(ifp, command, data);
1363 aue_watchdog(struct ifnet *ifp)
1365 struct aue_softc *sc = ifp->if_softc;
1366 struct aue_chain *c;
1372 if_printf(ifp, "watchdog timeout\n");
1374 c = &sc->aue_cdata.aue_tx_chain[0];
1375 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1376 aue_txeof(c->aue_xfer, c, stat);
1378 if (!ifq_is_empty(&ifp->if_snd))
1385 * Stop the adapter and free any mbufs allocated to the
1389 aue_stop(struct aue_softc *sc)
1396 ifp = &sc->arpcom.ac_if;
1399 aue_csr_write_1(sc, AUE_CTL0, 0);
1400 aue_csr_write_1(sc, AUE_CTL1, 0);
1402 callout_stop(&sc->aue_stat_timer);
1404 /* Stop transfers. */
1405 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1406 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1408 if_printf(ifp, "abort rx pipe failed: %s\n",
1411 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1413 if_printf(ifp, "close rx pipe failed: %s\n",
1416 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1419 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1420 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1422 if_printf(ifp, "abort tx pipe failed: %s\n",
1425 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1427 if_printf(ifp, "close tx pipe failed: %s\n",
1430 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1433 #ifdef AUE_INTR_PIPE
1434 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1435 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1437 if_printf(ifp, "abort intr pipe failed: %s\n",
1440 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1442 if_printf(ifp, "close intr pipe failed: %s\n",
1445 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1449 /* Free RX resources. */
1450 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1451 if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) {
1452 kfree(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV);
1453 sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL;
1455 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1456 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1457 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1459 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1460 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1461 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1465 /* Free TX resources. */
1466 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1467 if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) {
1468 kfree(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV);
1469 sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL;
1471 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1472 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1473 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1475 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1476 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1477 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1481 #ifdef AUE_INTR_PIPE
1482 if (sc->aue_cdata.aue_ibuf != NULL) {
1483 kfree(sc->aue_cdata.aue_ibuf, M_USBDEV);
1484 sc->aue_cdata.aue_ibuf = NULL;
1490 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1497 * Stop all chip I/O so that the kernel's probe routines don't
1498 * get confused by errant DMAs when rebooting.
1501 aue_shutdown(device_ptr_t dev)
1503 struct aue_softc *sc;
1505 sc = device_get_softc(dev);