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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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/module.h>
72 #include <sys/socket.h>
75 #include <net/if_arp.h>
76 #include <net/ethernet.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
79 #include <net/if_types.h>
84 #include <machine/bus.h>
85 #if __FreeBSD_version < 500000
86 #include <machine/clock.h>
89 #include <dev/usb/usb.h>
90 #include <dev/usb/usbdi.h>
91 #include <dev/usb/usbdi_util.h>
92 #include <dev/usb/usbdivar.h>
94 #include <dev/usb/usb_ethersubr.h>
96 #include <dev/mii/mii.h>
97 #include <dev/mii/miivar.h>
99 #include <dev/usb/if_auereg.h>
101 MODULE_DEPEND(aue, usb, 1, 1, 1);
102 MODULE_DEPEND(aue, ether, 1, 1, 1);
103 MODULE_DEPEND(aue, miibus, 1, 1, 1);
105 /* "device miibus" required. See GENERIC if you get errors here. */
106 #include "miibus_if.h"
109 * Various supported device vendors/products.
112 struct usb_devno aue_dev;
114 #define LSYS 0x0001 /* use Linksys reset */
115 #define PNA 0x0002 /* has Home PNA */
116 #define PII 0x0004 /* Pegasus II chip */
119 Static const struct aue_type aue_devs[] = {
120 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII },
121 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA|PII },
122 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII },
123 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS },
124 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA },
125 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA },
126 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII },
127 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII },
128 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII },
129 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA },
130 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 },
131 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
132 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 },
133 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII },
134 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA },
135 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII },
136 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII },
137 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN}, PII },
138 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 },
139 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
140 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
141 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII },
142 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
143 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
144 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS|PII },
145 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS },
146 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS },
147 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA },
148 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS|PII },
149 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS|PII },
150 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, LSYS },
151 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 },
152 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS },
153 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 },
154 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS },
155 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII },
156 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 },
157 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII },
158 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII },
159 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 },
160 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII },
161 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 },
162 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS|PII },
163 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS },
164 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS },
165 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS|PNA },
166 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS },
167 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS|PII },
168 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII },
169 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 },
170 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 },
171 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII },
172 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
173 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
174 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 },
175 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII },
176 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 },
178 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
180 Static int aue_match(device_ptr_t);
181 Static int aue_attach(device_ptr_t);
182 Static int aue_detach(device_ptr_t);
184 Static void aue_reset_pegasus_II(struct aue_softc *sc);
185 Static int aue_encap(struct aue_softc *, struct mbuf *, int);
187 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
189 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
190 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
191 Static void aue_tick(void *);
192 Static void aue_rxstart(struct ifnet *);
193 Static void aue_start(struct ifnet *);
194 Static int aue_ioctl(struct ifnet *, u_long, caddr_t);
195 Static void aue_init(void *);
196 Static void aue_stop(struct aue_softc *);
197 Static void aue_watchdog(struct ifnet *);
198 Static void aue_shutdown(device_ptr_t);
199 Static int aue_ifmedia_upd(struct ifnet *);
200 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
202 Static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
203 Static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
204 Static int aue_miibus_readreg(device_ptr_t, int, int);
205 Static int aue_miibus_writereg(device_ptr_t, int, int, int);
206 Static void aue_miibus_statchg(device_ptr_t);
208 Static void aue_setmulti(struct aue_softc *);
209 Static void aue_reset(struct aue_softc *);
211 Static int aue_csr_read_1(struct aue_softc *, int);
212 Static int aue_csr_write_1(struct aue_softc *, int, int);
213 Static int aue_csr_read_2(struct aue_softc *, int);
214 Static int aue_csr_write_2(struct aue_softc *, int, int);
216 Static device_method_t aue_methods[] = {
217 /* Device interface */
218 DEVMETHOD(device_probe, aue_match),
219 DEVMETHOD(device_attach, aue_attach),
220 DEVMETHOD(device_detach, aue_detach),
221 DEVMETHOD(device_shutdown, aue_shutdown),
224 DEVMETHOD(bus_print_child, bus_generic_print_child),
225 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
228 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
229 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
230 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
235 Static driver_t aue_driver = {
238 sizeof(struct aue_softc)
241 Static devclass_t aue_devclass;
243 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
244 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
246 #define AUE_SETBIT(sc, reg, x) \
247 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
249 #define AUE_CLRBIT(sc, reg, x) \
250 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
253 aue_csr_read_1(struct aue_softc *sc, int reg)
255 usb_device_request_t req;
264 req.bmRequestType = UT_READ_VENDOR_DEVICE;
265 req.bRequest = AUE_UR_READREG;
266 USETW(req.wValue, 0);
267 USETW(req.wIndex, reg);
268 USETW(req.wLength, 1);
270 err = usbd_do_request(sc->aue_udev, &req, &val);
282 aue_csr_read_2(struct aue_softc *sc, int reg)
284 usb_device_request_t req;
293 req.bmRequestType = UT_READ_VENDOR_DEVICE;
294 req.bRequest = AUE_UR_READREG;
295 USETW(req.wValue, 0);
296 USETW(req.wIndex, reg);
297 USETW(req.wLength, 2);
299 err = usbd_do_request(sc->aue_udev, &req, &val);
311 aue_csr_write_1(struct aue_softc *sc, int reg, int val)
313 usb_device_request_t req;
321 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
322 req.bRequest = AUE_UR_WRITEREG;
323 USETW(req.wValue, val);
324 USETW(req.wIndex, reg);
325 USETW(req.wLength, 1);
327 err = usbd_do_request(sc->aue_udev, &req, &val);
339 aue_csr_write_2(struct aue_softc *sc, int reg, int val)
341 usb_device_request_t req;
349 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
350 req.bRequest = AUE_UR_WRITEREG;
351 USETW(req.wValue, val);
352 USETW(req.wIndex, reg);
353 USETW(req.wLength, 2);
355 err = usbd_do_request(sc->aue_udev, &req, &val);
367 * Read a word of data stored in the EEPROM at address 'addr.'
370 aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
375 aue_csr_write_1(sc, AUE_EE_REG, addr);
376 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
378 for (i = 0; i < AUE_TIMEOUT; i++) {
379 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
383 if (i == AUE_TIMEOUT) {
384 printf("aue%d: EEPROM read timed out\n",
388 word = aue_csr_read_2(sc, AUE_EE_DATA);
395 * Read a sequence of words from the EEPROM.
398 aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
401 u_int16_t word = 0, *ptr;
403 for (i = 0; i < cnt; i++) {
404 aue_eeprom_getword(sc, off + i, &word);
405 ptr = (u_int16_t *)(dest + (i * 2));
416 aue_miibus_readreg(device_ptr_t dev, int phy, int reg)
418 struct aue_softc *sc = USBGETSOFTC(dev);
423 * The Am79C901 HomePNA PHY actually contains
424 * two transceivers: a 1Mbps HomePNA PHY and a
425 * 10Mbps full/half duplex ethernet PHY with
426 * NWAY autoneg. However in the ADMtek adapter,
427 * only the 1Mbps PHY is actually connected to
428 * anything, so we ignore the 10Mbps one. It
429 * happens to be configured for MII address 3,
430 * so we filter that out.
432 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
433 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
442 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
443 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
445 for (i = 0; i < AUE_TIMEOUT; i++) {
446 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
450 if (i == AUE_TIMEOUT) {
451 printf("aue%d: MII read timed out\n", sc->aue_unit);
454 val = aue_csr_read_2(sc, AUE_PHY_DATA);
460 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
462 struct aue_softc *sc = USBGETSOFTC(dev);
468 aue_csr_write_2(sc, AUE_PHY_DATA, data);
469 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
470 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
472 for (i = 0; i < AUE_TIMEOUT; i++) {
473 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
477 if (i == AUE_TIMEOUT) {
478 printf("aue%d: MII read timed out\n",
486 aue_miibus_statchg(device_ptr_t dev)
488 struct aue_softc *sc = USBGETSOFTC(dev);
489 struct mii_data *mii = GET_MII(sc);
491 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
492 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
493 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
495 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
498 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
499 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
501 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
503 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
506 * Set the LED modes on the LinkSys adapter.
507 * This turns on the 'dual link LED' bin in the auxmode
508 * register of the Broadcom PHY.
510 if (sc->aue_flags & LSYS) {
512 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
513 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
522 aue_setmulti(struct aue_softc *sc)
525 struct ifmultiaddr *ifma;
530 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
531 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
535 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
537 /* first, zot all the existing hash bits */
538 for (i = 0; i < 8; i++)
539 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
541 /* now program new ones */
543 #if __FreeBSD_version >= 500000
544 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
546 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
549 if (ifma->ifma_addr->sa_family != AF_LINK)
551 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
552 ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
553 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
561 aue_reset_pegasus_II(struct aue_softc *sc)
563 /* Magic constants taken from Linux driver. */
564 aue_csr_write_1(sc, AUE_REG_1D, 0);
565 aue_csr_write_1(sc, AUE_REG_7B, 2);
567 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
568 aue_csr_write_1(sc, AUE_REG_81, 6);
571 aue_csr_write_1(sc, AUE_REG_81, 2);
575 aue_reset(struct aue_softc *sc)
579 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
581 for (i = 0; i < AUE_TIMEOUT; i++) {
582 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
586 if (i == AUE_TIMEOUT)
587 printf("aue%d: reset failed\n", sc->aue_unit);
590 * The PHY(s) attached to the Pegasus chip may be held
591 * in reset until we flip on the GPIO outputs. Make sure
592 * to set the GPIO pins high so that the PHY(s) will
595 * Note: We force all of the GPIO pins low first, *then*
596 * enable the ones we want.
598 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
599 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
601 if (sc->aue_flags & LSYS) {
602 /* Grrr. LinkSys has to be different from everyone else. */
603 aue_csr_write_1(sc, AUE_GPIO0,
604 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
605 aue_csr_write_1(sc, AUE_GPIO0,
606 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
609 if (sc->aue_flags & PII)
610 aue_reset_pegasus_II(sc);
612 /* Wait a little while for the chip to get its brains in order. */
619 * Probe for a Pegasus chip.
623 USB_MATCH_START(aue, uaa);
625 if (uaa->iface != NULL)
626 return (UMATCH_NONE);
628 return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
629 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
633 * Attach the interface. Allocate softc structures, do ifmedia
634 * setup and ethernet/BPF attach.
638 USB_ATTACH_START(aue, sc, uaa);
640 u_char eaddr[ETHER_ADDR_LEN];
642 usbd_interface_handle iface;
644 usb_interface_descriptor_t *id;
645 usb_endpoint_descriptor_t *ed;
648 bzero(sc, sizeof(struct aue_softc));
650 usbd_devinfo(uaa->device, 0, devinfo);
653 sc->aue_udev = uaa->device;
654 sc->aue_unit = device_get_unit(self);
656 if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
657 printf("aue%d: getting interface handle failed\n",
659 USB_ATTACH_ERROR_RETURN;
662 err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
664 printf("aue%d: getting interface handle failed\n",
666 USB_ATTACH_ERROR_RETURN;
669 sc->aue_iface = iface;
670 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
672 sc->aue_product = uaa->product;
673 sc->aue_vendor = uaa->vendor;
675 id = usbd_get_interface_descriptor(sc->aue_iface);
677 usbd_devinfo(uaa->device, 0, devinfo);
678 device_set_desc_copy(self, devinfo);
679 printf("%s: %s\n", USBDEVNAME(self), devinfo);
681 /* Find endpoints. */
682 for (i = 0; i < id->bNumEndpoints; i++) {
683 ed = usbd_interface2endpoint_descriptor(iface, i);
685 printf("aue%d: couldn't get ep %d\n",
687 USB_ATTACH_ERROR_RETURN;
689 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
690 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
691 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
692 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
693 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
694 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
695 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
696 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
697 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
701 #if __FreeBSD_version >= 500000
702 mtx_init(&sc->aue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
703 MTX_DEF | MTX_RECURSE);
707 /* Reset the adapter. */
711 * Get station address from the EEPROM.
713 aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
715 ifp = sc->aue_ifp = if_alloc(IFT_ETHER);
717 printf("aue%d: can not if_alloc()\n", sc->aue_unit);
719 #if __FreeBSD_version >= 500000
720 mtx_destroy(&sc->aue_mtx);
722 USB_ATTACH_ERROR_RETURN;
725 if_initname(ifp, "aue", sc->aue_unit);
726 ifp->if_mtu = ETHERMTU;
727 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
729 ifp->if_ioctl = aue_ioctl;
730 ifp->if_start = aue_start;
731 ifp->if_watchdog = aue_watchdog;
732 ifp->if_init = aue_init;
733 ifp->if_baudrate = 10000000;
734 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
738 * NOTE: Doing this causes child devices to be attached to us,
739 * which we would normally disconnect at in the detach routine
740 * using device_delete_child(). However the USB code is set up
741 * such that when this driver is removed, all children devices
742 * are removed as well. In effect, the USB code ends up detaching
743 * all of our children for us, so we don't have to do is ourselves
744 * in aue_detach(). It's important to point this out since if
745 * we *do* try to detach the child devices ourselves, we will
746 * end up getting the children deleted twice, which will crash
749 if (mii_phy_probe(self, &sc->aue_miibus,
750 aue_ifmedia_upd, aue_ifmedia_sts)) {
751 printf("aue%d: MII without any PHY!\n", sc->aue_unit);
754 #if __FreeBSD_version >= 500000
755 mtx_destroy(&sc->aue_mtx);
757 USB_ATTACH_ERROR_RETURN;
760 sc->aue_qdat.ifp = ifp;
761 sc->aue_qdat.if_rxstart = aue_rxstart;
764 * Call MI attach routine.
766 #if __FreeBSD_version >= 500000
767 ether_ifattach(ifp, eaddr);
769 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
771 callout_handle_init(&sc->aue_stat_ch);
772 usb_register_netisr();
776 USB_ATTACH_SUCCESS_RETURN;
780 aue_detach(device_ptr_t dev)
782 struct aue_softc *sc;
785 sc = device_get_softc(dev);
790 untimeout(aue_tick, sc, sc->aue_stat_ch);
791 #if __FreeBSD_version >= 500000
795 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
798 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
799 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
800 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
801 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
803 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
804 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
808 #if __FreeBSD_version >= 500000
809 mtx_destroy(&sc->aue_mtx);
817 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
819 struct aue_softc *sc = priv;
821 struct aue_intrpkt *p;
826 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
831 if (status != USBD_NORMAL_COMPLETION) {
832 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
836 printf("aue%d: usb error on intr: %s\n", sc->aue_unit,
837 usbd_errstr(status));
838 if (status == USBD_STALLED)
839 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
844 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
849 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
850 ifp->if_collisions++;
858 aue_rxstart(struct ifnet *ifp)
860 struct aue_softc *sc;
865 c = &sc->aue_cdata.ue_rx_chain[sc->aue_cdata.ue_rx_prod];
867 c->ue_mbuf = usb_ether_newbuf();
868 if (c->ue_mbuf == NULL) {
869 printf("%s: no memory for rx list "
870 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
876 /* Setup new transfer. */
877 usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_RX],
878 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
879 USBD_NO_TIMEOUT, aue_rxeof);
880 usbd_transfer(c->ue_xfer);
887 * A frame has been uploaded: pass the resulting mbuf chain up to
888 * the higher level protocols.
891 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
893 struct ue_chain *c = priv;
894 struct aue_softc *sc = c->ue_sc;
905 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
910 if (status != USBD_NORMAL_COMPLETION) {
911 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
915 if (usbd_ratecheck(&sc->aue_rx_notice))
916 printf("aue%d: usb error on rx: %s\n", sc->aue_unit,
917 usbd_errstr(status));
918 if (status == USBD_STALLED)
919 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
923 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
925 if (total_len <= 4 + ETHER_CRC_LEN) {
931 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
933 /* Turn off all the non-error bits in the rx status word. */
934 r.aue_rxstat &= AUE_RXSTAT_MASK;
941 /* No errors; receive the packet. */
942 total_len -= (4 + ETHER_CRC_LEN);
945 m->m_pkthdr.rcvif = (void *)&sc->aue_qdat;
946 m->m_pkthdr.len = m->m_len = total_len;
948 /* Put the packet on the special USB input queue. */
954 /* Setup new transfer. */
955 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
956 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
957 USBD_NO_TIMEOUT, aue_rxeof);
965 * A frame was downloaded to the chip. It's safe for us to clean up
970 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
972 struct ue_chain *c = priv;
973 struct aue_softc *sc = c->ue_sc;
980 if (status != USBD_NORMAL_COMPLETION) {
981 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
985 printf("aue%d: usb error on tx: %s\n", sc->aue_unit,
986 usbd_errstr(status));
987 if (status == USBD_STALLED)
988 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
994 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
995 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
997 if (c->ue_mbuf != NULL) {
998 c->ue_mbuf->m_pkthdr.rcvif = ifp;
999 usb_tx_done(c->ue_mbuf);
1016 struct aue_softc *sc = xsc;
1018 struct mii_data *mii;
1033 if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1034 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1036 if (ifp->if_snd.ifq_head != NULL)
1040 sc->aue_stat_ch = timeout(aue_tick, sc, hz);
1048 aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1054 c = &sc->aue_cdata.ue_tx_chain[idx];
1057 * Copy the mbuf data into a contiguous buffer, leaving two
1058 * bytes at the beginning to hold the frame length.
1060 m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf + 2);
1063 total_len = m->m_pkthdr.len + 2;
1066 * The ADMtek documentation says that the packet length is
1067 * supposed to be specified in the first two bytes of the
1068 * transfer, however it actually seems to ignore this info
1069 * and base the frame size on the bulk transfer length.
1071 c->ue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1072 c->ue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1074 usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1075 c, c->ue_buf, total_len, USBD_FORCE_SHORT_XFER,
1079 err = usbd_transfer(c->ue_xfer);
1080 if (err != USBD_IN_PROGRESS) {
1085 sc->aue_cdata.ue_tx_cnt++;
1091 aue_start(struct ifnet *ifp)
1093 struct aue_softc *sc = ifp->if_softc;
1094 struct mbuf *m_head = NULL;
1098 if (!sc->aue_link) {
1103 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1108 IF_DEQUEUE(&ifp->if_snd, m_head);
1109 if (m_head == NULL) {
1114 if (aue_encap(sc, m_head, 0)) {
1115 IF_PREPEND(&ifp->if_snd, m_head);
1116 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1122 * If there's a BPF listener, bounce a copy of this frame
1125 BPF_MTAP(ifp, m_head);
1127 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1130 * Set a timeout in case the chip goes out to lunch.
1141 struct aue_softc *sc = xsc;
1142 struct ifnet *ifp = sc->aue_ifp;
1143 struct mii_data *mii = GET_MII(sc);
1150 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1156 * Cancel pending I/O and free all RX/TX buffers.
1160 /* Set MAC address */
1161 for (i = 0; i < ETHER_ADDR_LEN; i++)
1162 aue_csr_write_1(sc, AUE_PAR0 + i, IF_LLADDR(sc->aue_ifp)[i]);
1164 /* If we want promiscuous mode, set the allframes bit. */
1165 if (ifp->if_flags & IFF_PROMISC)
1166 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1168 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1171 if (usb_ether_tx_list_init(sc, &sc->aue_cdata,
1172 sc->aue_udev) == ENOBUFS) {
1173 printf("aue%d: tx list init failed\n", sc->aue_unit);
1179 if (usb_ether_rx_list_init(sc, &sc->aue_cdata,
1180 sc->aue_udev) == ENOBUFS) {
1181 printf("aue%d: rx list init failed\n", sc->aue_unit);
1186 #ifdef AUE_INTR_PIPE
1187 sc->aue_cdata.ue_ibuf = malloc(AUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT);
1190 /* Load the multicast filter. */
1193 /* Enable RX and TX */
1194 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1195 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1196 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1200 /* Open RX and TX pipes. */
1201 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1202 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1204 printf("aue%d: open rx pipe failed: %s\n",
1205 sc->aue_unit, usbd_errstr(err));
1209 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1210 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1212 printf("aue%d: open tx pipe failed: %s\n",
1213 sc->aue_unit, usbd_errstr(err));
1218 #ifdef AUE_INTR_PIPE
1219 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1220 USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1221 sc->aue_cdata.ue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1224 printf("aue%d: open intr pipe failed: %s\n",
1225 sc->aue_unit, usbd_errstr(err));
1231 /* Start up the receive pipe. */
1232 for (i = 0; i < UE_RX_LIST_CNT; i++) {
1233 c = &sc->aue_cdata.ue_rx_chain[i];
1234 usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1235 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
1236 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1237 usbd_transfer(c->ue_xfer);
1240 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1241 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1243 sc->aue_stat_ch = timeout(aue_tick, sc, hz);
1251 * Set media options.
1254 aue_ifmedia_upd(struct ifnet *ifp)
1256 struct aue_softc *sc = ifp->if_softc;
1257 struct mii_data *mii = GET_MII(sc);
1260 if (mii->mii_instance) {
1261 struct mii_softc *miisc;
1262 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1263 mii_phy_reset(miisc);
1271 * Report current media status.
1274 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1276 struct aue_softc *sc = ifp->if_softc;
1277 struct mii_data *mii = GET_MII(sc);
1280 ifmr->ifm_active = mii->mii_media_active;
1281 ifmr->ifm_status = mii->mii_media_status;
1287 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1289 struct aue_softc *sc = ifp->if_softc;
1290 struct ifreq *ifr = (struct ifreq *)data;
1291 struct mii_data *mii;
1298 if (ifp->if_flags & IFF_UP) {
1299 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1300 ifp->if_flags & IFF_PROMISC &&
1301 !(sc->aue_if_flags & IFF_PROMISC)) {
1302 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1303 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1304 !(ifp->if_flags & IFF_PROMISC) &&
1305 sc->aue_if_flags & IFF_PROMISC) {
1306 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1307 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1310 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1313 sc->aue_if_flags = ifp->if_flags;
1324 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1327 error = ether_ioctl(ifp, command, data);
1337 aue_watchdog(struct ifnet *ifp)
1339 struct aue_softc *sc = ifp->if_softc;
1346 printf("aue%d: watchdog timeout\n", sc->aue_unit);
1348 c = &sc->aue_cdata.ue_tx_chain[0];
1349 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1350 aue_txeof(c->ue_xfer, c, stat);
1352 if (ifp->if_snd.ifq_head != NULL)
1359 * Stop the adapter and free any mbufs allocated to the
1363 aue_stop(struct aue_softc *sc)
1372 aue_csr_write_1(sc, AUE_CTL0, 0);
1373 aue_csr_write_1(sc, AUE_CTL1, 0);
1375 untimeout(aue_tick, sc, sc->aue_stat_ch);
1377 /* Stop transfers. */
1378 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1379 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1381 printf("aue%d: abort rx pipe failed: %s\n",
1382 sc->aue_unit, usbd_errstr(err));
1384 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1386 printf("aue%d: close rx pipe failed: %s\n",
1387 sc->aue_unit, usbd_errstr(err));
1389 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1392 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1393 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1395 printf("aue%d: abort tx pipe failed: %s\n",
1396 sc->aue_unit, usbd_errstr(err));
1398 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1400 printf("aue%d: close tx pipe failed: %s\n",
1401 sc->aue_unit, usbd_errstr(err));
1403 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1406 #ifdef AUE_INTR_PIPE
1407 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1408 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1410 printf("aue%d: abort intr pipe failed: %s\n",
1411 sc->aue_unit, usbd_errstr(err));
1413 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1415 printf("aue%d: close intr pipe failed: %s\n",
1416 sc->aue_unit, usbd_errstr(err));
1418 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1422 /* Free RX resources. */
1423 usb_ether_rx_list_free(&sc->aue_cdata);
1424 /* Free TX resources. */
1425 usb_ether_tx_list_free(&sc->aue_cdata);
1427 #ifdef AUE_INTR_PIPE
1428 free(sc->aue_cdata.ue_ibuf, M_USBDEV);
1429 sc->aue_cdata.ue_ibuf = NULL;
1434 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1441 * Stop all chip I/O so that the kernel's probe routines don't
1442 * get confused by errant DMAs when rebooting.
1445 aue_shutdown(device_ptr_t dev)
1447 struct aue_softc *sc;
1449 sc = device_get_softc(dev);