2 * Copyright (c) 1997, 1998, 1999, 2000-2003
3 * Bill Paul <wpaul@windriver.com>. 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_axe.c,v 1.10 2003/12/08 07:54:14 obrien Exp $
33 * $DragonFly: src/sys/dev/netif/axe/if_axe.c,v 1.23 2007/07/01 21:24:02 hasso Exp $
36 * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
37 * LinkSys USB200M and various other adapters.
39 * Manuals available from:
40 * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
41 * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
42 * controller) to find the definitions for the RX control register.
43 * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
45 * Written by Bill Paul <wpaul@windriver.com>
51 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
52 * It uses an external PHY (reference designs use a RealTek chip),
53 * and has a 64-bit multicast hash filter. There is some information
54 * missing from the manual which one needs to know in order to make
57 * - You must set bit 7 in the RX control register, otherwise the
58 * chip won't receive any packets.
59 * - You must initialize all 3 IPG registers, or you won't be able
60 * to send any packets.
62 * Note that this device appears to only support loading the station
63 * address via autload from the EEPROM (i.e. there's no way to manaully
66 * (Adam Weinberger wanted me to name this driver if_gir.c.)
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sockio.h>
73 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/socket.h>
77 #include <sys/thread2.h>
80 #include <net/ifq_var.h>
81 #include <net/if_arp.h>
82 #include <net/ethernet.h>
83 #include <net/if_dl.h>
84 #include <net/if_media.h>
88 #include <bus/usb/usb.h>
89 #include <bus/usb/usbdi.h>
90 #include <bus/usb/usbdi_util.h>
91 #include <bus/usb/usbdivar.h>
92 #include <bus/usb/usbdevs.h>
93 #include <bus/usb/usb_ethersubr.h>
95 #include "../mii_layer//mii.h"
96 #include "../mii_layer/miivar.h"
98 /* "controller miibus0" required. See GENERIC if you get errors here. */
99 #include "miibus_if.h"
101 #include "if_axereg.h"
104 * Various supported device vendors/products.
106 static struct axe_type axe_devs[] = {
107 { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172 },
108 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100 },
109 { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1 },
110 { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M },
111 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX },
112 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120 },
113 { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL },
114 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029 },
118 static int axe_match(device_t);
119 static int axe_attach(device_t);
120 static int axe_detach(device_t);
122 static int axe_tx_list_init(struct axe_softc *);
123 static int axe_rx_list_init(struct axe_softc *);
124 static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
125 static int axe_encap(struct axe_softc *, struct mbuf *, int);
126 static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
127 static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
128 static void axe_tick(void *);
129 static void axe_rxstart(struct ifnet *);
130 static void axe_start(struct ifnet *);
131 static int axe_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
132 static void axe_init(void *);
133 static void axe_stop(struct axe_softc *);
134 static void axe_watchdog(struct ifnet *);
135 static void axe_shutdown(device_t);
136 static int axe_miibus_readreg(device_t, int, int);
137 static int axe_miibus_writereg(device_t, int, int, int);
138 static void axe_miibus_statchg(device_t);
139 static int axe_cmd(struct axe_softc *, int, int, int, void *);
140 static int axe_ifmedia_upd(struct ifnet *);
141 static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
143 static void axe_setmulti(struct axe_softc *);
145 static device_method_t axe_methods[] = {
146 /* Device interface */
147 DEVMETHOD(device_probe, axe_match),
148 DEVMETHOD(device_attach, axe_attach),
149 DEVMETHOD(device_detach, axe_detach),
150 DEVMETHOD(device_shutdown, axe_shutdown),
153 DEVMETHOD(bus_print_child, bus_generic_print_child),
154 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
157 DEVMETHOD(miibus_readreg, axe_miibus_readreg),
158 DEVMETHOD(miibus_writereg, axe_miibus_writereg),
159 DEVMETHOD(miibus_statchg, axe_miibus_statchg),
164 static driver_t axe_driver = {
167 sizeof(struct axe_softc)
170 static devclass_t axe_devclass;
172 DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, usbd_driver_load, 0);
173 DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
174 MODULE_DEPEND(axe, usb, 1, 1, 1);
175 MODULE_DEPEND(axe, miibus, 1, 1, 1);
178 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
180 usb_device_request_t req;
186 if (AXE_CMD_DIR(cmd))
187 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
189 req.bmRequestType = UT_READ_VENDOR_DEVICE;
190 req.bRequest = AXE_CMD_CMD(cmd);
191 USETW(req.wValue, val);
192 USETW(req.wIndex, index);
193 USETW(req.wLength, AXE_CMD_LEN(cmd));
195 err = usbd_do_request(sc->axe_udev, &req, buf);
204 axe_miibus_readreg(device_t dev, int phy, int reg)
206 struct axe_softc *sc = device_get_softc(dev);
216 * The chip tells us the MII address of any supported
217 * PHYs attached to the chip, so only read from those.
220 if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0]) {
224 if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1]) {
228 if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy) {
232 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
233 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
234 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
237 if_printf(&sc->arpcom.ac_if, "read PHY failed\n");
242 sc->axe_phyaddrs[0] = phy;
248 axe_miibus_writereg(device_t dev, int phy, int reg, int val)
250 struct axe_softc *sc = device_get_softc(dev);
257 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
258 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
259 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
262 if_printf(&sc->arpcom.ac_if, "write PHY failed\n");
270 axe_miibus_statchg(device_t dev)
273 struct axe_softc *sc = device_get_softc(dev);
274 struct mii_data *mii = GET_MII(sc);
276 /* doesn't seem to be necessary */
285 axe_ifmedia_upd(struct ifnet *ifp)
287 struct axe_softc *sc = ifp->if_softc;
288 struct mii_data *mii = GET_MII(sc);
291 if (mii->mii_instance) {
292 struct mii_softc *miisc;
293 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
294 mii_phy_reset(miisc);
302 * Report current media status.
305 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
307 struct axe_softc *sc = ifp->if_softc;
308 struct mii_data *mii = GET_MII(sc);
311 ifmr->ifm_active = mii->mii_media_active;
312 ifmr->ifm_status = mii->mii_media_status;
318 axe_setmulti(struct axe_softc *sc)
320 struct ifnet *ifp = &sc->arpcom.ac_if;
321 struct ifmultiaddr *ifma;
324 u_int8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
326 axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
328 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
329 rxmode |= AXE_RXCMD_ALLMULTI;
330 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
333 rxmode &= ~AXE_RXCMD_ALLMULTI;
335 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
336 if (ifma->ifma_addr->sa_family != AF_LINK)
339 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
341 /* the filter bit position */
342 h = (h >> 26) & 0x0000003F;
343 hashtbl[h / 8] |= 1 << (h % 8);
346 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
347 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
351 axe_reset(struct axe_softc *sc)
356 if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1) ||
357 usbd_device2interface_handle(sc->axe_udev, AXE_IFACE_IDX,
359 if_printf(&sc->arpcom.ac_if,
360 "getting interface handle failed\n");
363 /* Wait a little while for the chip to get its brains in order. */
369 * Probe for a AX88172 chip.
372 axe_match(device_t self)
374 struct usb_attach_arg *uaa = device_get_ivars(self);
382 if (uaa->vendor == t->axe_vid &&
383 uaa->product == t->axe_did) {
384 return(UMATCH_VENDOR_PRODUCT);
393 * Attach the interface. Allocate softc structures, do ifmedia
394 * setup and ethernet/BPF attach.
397 axe_attach(device_t self)
399 struct axe_softc *sc = device_get_softc(self);
400 struct usb_attach_arg *uaa = device_get_ivars(self);
402 u_char eaddr[ETHER_ADDR_LEN];
404 usb_interface_descriptor_t *id;
405 usb_endpoint_descriptor_t *ed;
408 sc->axe_udev = uaa->device;
409 callout_init(&sc->axe_stat_timer);
411 if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1)) {
412 device_printf(self, "setting config no %d failed\n",
417 if (usbd_device2interface_handle(uaa->device,
418 AXE_IFACE_IDX, &sc->axe_iface)) {
419 device_printf(self, "getting interface handle failed\n");
423 id = usbd_get_interface_descriptor(sc->axe_iface);
425 usbd_devinfo(uaa->device, 0, devinfo);
426 device_set_desc_copy(self, devinfo);
427 device_printf(self, "%s\n", devinfo);
429 /* Find endpoints. */
430 for (i = 0; i < id->bNumEndpoints; i++) {
431 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
433 device_printf(self, "couldn't get ep %d\n", i);
436 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
437 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
438 sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
439 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
440 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
441 sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
442 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
443 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
444 sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
449 * Get station address.
451 axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
454 * Load IPG values and PHY indexes.
456 axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
457 axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
460 * Work around broken adapters that appear to lie about
461 * their PHY addresses.
463 sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
465 ifp = &sc->arpcom.ac_if;
467 if_initname(ifp, device_get_name(self), device_get_unit(self));
468 ifp->if_mtu = ETHERMTU;
469 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
470 ifp->if_ioctl = axe_ioctl;
471 ifp->if_start = axe_start;
472 ifp->if_watchdog = axe_watchdog;
473 ifp->if_init = axe_init;
474 ifp->if_baudrate = 10000000;
475 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
476 ifq_set_ready(&ifp->if_snd);
478 if (mii_phy_probe(self, &sc->axe_miibus,
479 axe_ifmedia_upd, axe_ifmedia_sts)) {
480 device_printf(self, "MII without any PHY!\n");
485 * Call MI attach routine.
488 ether_ifattach(ifp, eaddr, NULL);
492 usb_register_netisr();
498 axe_detach(device_t dev)
500 struct axe_softc *sc = device_get_softc(dev);
501 struct ifnet *ifp = &sc->arpcom.ac_if;
504 callout_stop(&sc->axe_stat_timer);
507 if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
508 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
509 if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
510 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
511 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
512 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
518 * Initialize an RX descriptor and attach an MBUF cluster.
521 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
523 struct mbuf *m_new = NULL;
526 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
528 if_printf(&sc->arpcom.ac_if, "no memory for rx list "
529 "-- packet dropped!\n");
532 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
535 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
536 m_new->m_data = m_new->m_ext.ext_buf;
539 m_adj(m_new, ETHER_ALIGN);
546 axe_rx_list_init(struct axe_softc *sc)
548 struct axe_cdata *cd;
553 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
554 c = &cd->axe_rx_chain[i];
557 if (axe_newbuf(sc, c, NULL) == ENOBUFS)
559 if (c->axe_xfer == NULL) {
560 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
561 if (c->axe_xfer == NULL)
570 axe_tx_list_init(struct axe_softc *sc)
572 struct axe_cdata *cd;
577 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
578 c = &cd->axe_tx_chain[i];
582 if (c->axe_xfer == NULL) {
583 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
584 if (c->axe_xfer == NULL)
587 c->axe_buf = kmalloc(AXE_BUFSZ, M_USBDEV, M_WAITOK);
588 if (c->axe_buf == NULL)
596 axe_rxstart(struct ifnet *ifp)
598 struct axe_softc *sc = ifp->if_softc;
601 c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
603 if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
608 /* Setup new transfer. */
609 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
610 c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
611 USBD_NO_TIMEOUT, axe_rxeof);
612 usbd_transfer(c->axe_xfer);
616 * A frame has been uploaded: pass the resulting mbuf chain up to
617 * the higher level protocols.
620 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
622 struct axe_chain *c = priv;
623 struct axe_softc *sc = c->axe_sc;
624 struct ifnet *ifp = &sc->arpcom.ac_if;
628 lwkt_serialize_enter(ifp->if_serializer);
630 if (!(ifp->if_flags & IFF_RUNNING)) {
631 lwkt_serialize_exit(ifp->if_serializer);
635 if (status != USBD_NORMAL_COMPLETION) {
636 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
637 lwkt_serialize_exit(ifp->if_serializer);
640 if (usbd_ratecheck(&sc->axe_rx_notice)) {
641 if_printf(ifp, "usb error on rx: %s\n",
642 usbd_errstr(status));
644 if (status == USBD_STALLED)
645 usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
649 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
653 if (total_len < sizeof(struct ether_header)) {
659 m->m_pkthdr.rcvif = ifp;
660 m->m_pkthdr.len = m->m_len = total_len;
662 /* Put the packet on the special USB input queue. */
665 lwkt_serialize_exit(ifp->if_serializer);
668 /* Setup new transfer. */
669 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
670 c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
671 USBD_NO_TIMEOUT, axe_rxeof);
672 usbd_transfer(c->axe_xfer);
673 lwkt_serialize_exit(ifp->if_serializer);
677 * A frame was downloaded to the chip. It's safe for us to clean up
682 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
684 struct axe_chain *c = priv;
685 struct axe_softc *sc = c->axe_sc;
686 struct ifnet *ifp = &sc->arpcom.ac_if;
689 lwkt_serialize_enter(ifp->if_serializer);
690 if (status != USBD_NORMAL_COMPLETION) {
691 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
692 lwkt_serialize_exit(ifp->if_serializer);
695 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
696 if (status == USBD_STALLED)
697 usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
698 lwkt_serialize_exit(ifp->if_serializer);
703 ifp->if_flags &= ~IFF_OACTIVE;
704 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &err);
706 if (c->axe_mbuf != NULL) {
707 m_freem(c->axe_mbuf);
716 if (!ifq_is_empty(&ifp->if_snd))
717 (*ifp->if_start)(ifp);
718 lwkt_serialize_exit(ifp->if_serializer);
724 struct axe_softc *sc = xsc;
725 struct ifnet *ifp = &sc->arpcom.ac_if;
726 struct mii_data *mii;
732 lwkt_serialize_enter(ifp->if_serializer);
734 if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
735 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
737 if (!ifq_is_empty(&ifp->if_snd))
740 callout_reset(&sc->axe_stat_timer, hz, axe_tick, sc);
741 lwkt_serialize_exit(ifp->if_serializer);
745 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
750 c = &sc->axe_cdata.axe_tx_chain[idx];
753 * Copy the mbuf data into a contiguous buffer, leaving two
754 * bytes at the beginning to hold the frame length.
756 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
759 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
760 c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER,
764 err = usbd_transfer(c->axe_xfer);
765 if (err != USBD_IN_PROGRESS) {
770 sc->axe_cdata.axe_tx_cnt++;
776 axe_start(struct ifnet *ifp)
778 struct axe_softc *sc = ifp->if_softc;
779 struct mbuf *m_head = NULL;
785 if (ifp->if_flags & IFF_OACTIVE) {
789 m_head = ifq_poll(&ifp->if_snd);
790 if (m_head == NULL) {
794 if (axe_encap(sc, m_head, 0)) {
795 ifp->if_flags |= IFF_OACTIVE;
798 ifq_dequeue(&ifp->if_snd, m_head);
801 * If there's a BPF listener, bounce a copy of this frame
804 BPF_MTAP(ifp, m_head);
806 ifp->if_flags |= IFF_OACTIVE;
809 * Set a timeout in case the chip goes out to lunch.
817 struct axe_softc *sc = xsc;
818 struct ifnet *ifp = &sc->arpcom.ac_if;
823 if (ifp->if_flags & IFF_RUNNING) {
828 * Cancel pending I/O and free all RX/TX buffers.
834 /* Set MAC address */
835 axe_mac(sc, sc->arpcom.ac_enaddr, 1);
838 /* Enable RX logic. */
841 if (axe_tx_list_init(sc) == ENOBUFS) {
842 if_printf(ifp, "tx list init failed\n");
847 if (axe_rx_list_init(sc) == ENOBUFS) {
848 if_printf(ifp, "rx list init failed\n");
852 /* Set transmitter IPG values */
853 axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
854 axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
855 axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
857 /* Enable receiver, set RX mode */
858 rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
860 /* If we want promiscuous mode, set the allframes bit. */
861 if (ifp->if_flags & IFF_PROMISC)
862 rxmode |= AXE_RXCMD_PROMISC;
864 if (ifp->if_flags & IFF_BROADCAST)
865 rxmode |= AXE_RXCMD_BROADCAST;
867 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
869 /* Load the multicast filter. */
872 /* Open RX and TX pipes. */
873 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
874 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
876 if_printf(ifp, "open rx pipe failed: %s\n", usbd_errstr(err));
880 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
881 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
883 if_printf(ifp, "open tx pipe failed: %s\n", usbd_errstr(err));
887 /* Start up the receive pipe. */
888 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
889 c = &sc->axe_cdata.axe_rx_chain[i];
890 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
891 c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
892 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
893 usbd_transfer(c->axe_xfer);
896 ifp->if_flags |= IFF_RUNNING;
897 ifp->if_flags &= ~IFF_OACTIVE;
899 callout_reset(&sc->axe_stat_timer, hz, axe_tick, sc);
903 axe_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
905 struct axe_softc *sc = ifp->if_softc;
906 struct ifreq *ifr = (struct ifreq *)data;
907 struct mii_data *mii;
913 if (ifp->if_flags & IFF_UP) {
914 if (ifp->if_flags & IFF_RUNNING &&
915 ifp->if_flags & IFF_PROMISC &&
916 !(sc->axe_if_flags & IFF_PROMISC)) {
917 axe_cmd(sc, AXE_CMD_RXCTL_READ,
918 0, 0, (void *)&rxmode);
919 rxmode |= AXE_RXCMD_PROMISC;
920 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
923 } else if (ifp->if_flags & IFF_RUNNING &&
924 !(ifp->if_flags & IFF_PROMISC) &&
925 sc->axe_if_flags & IFF_PROMISC) {
926 axe_cmd(sc, AXE_CMD_RXCTL_READ,
927 0, 0, (void *)&rxmode);
928 rxmode &= ~AXE_RXCMD_PROMISC;
929 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
932 } else if (!(ifp->if_flags & IFF_RUNNING))
935 if (ifp->if_flags & IFF_RUNNING)
938 sc->axe_if_flags = ifp->if_flags;
949 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
953 error = ether_ioctl(ifp, command, data);
960 axe_watchdog(struct ifnet *ifp)
962 struct axe_softc *sc = ifp->if_softc;
967 if_printf(ifp, "watchdog timeout\n");
969 c = &sc->axe_cdata.axe_tx_chain[0];
970 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
971 axe_txeof(c->axe_xfer, c, stat);
973 if (!ifq_is_empty(&ifp->if_snd))
978 * Stop the adapter and free any mbufs allocated to the
982 axe_stop(struct axe_softc *sc)
988 ifp = &sc->arpcom.ac_if;
991 callout_stop(&sc->axe_stat_timer);
993 /* Stop transfers. */
994 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
995 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
997 if_printf(ifp, "abort rx pipe failed: %s\n",
1000 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1002 if_printf(ifp, "close rx pipe failed: %s\n",
1005 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1008 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1009 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1011 if_printf(ifp, "abort tx pipe failed: %s\n",
1014 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1016 if_printf(ifp, "close tx pipe failed: %s\n",
1019 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1022 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1023 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1025 if_printf(ifp, "abort intr pipe failed: %s\n",
1028 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1030 if_printf(ifp, "close intr pipe failed: %s\n",
1033 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1038 /* Free RX resources. */
1039 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1040 if (sc->axe_cdata.axe_rx_chain[i].axe_buf != NULL) {
1041 kfree(sc->axe_cdata.axe_rx_chain[i].axe_buf, M_USBDEV);
1042 sc->axe_cdata.axe_rx_chain[i].axe_buf = NULL;
1044 if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1045 m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1046 sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1048 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1049 usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1050 sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1054 /* Free TX resources. */
1055 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1056 if (sc->axe_cdata.axe_tx_chain[i].axe_buf != NULL) {
1057 kfree(sc->axe_cdata.axe_tx_chain[i].axe_buf, M_USBDEV);
1058 sc->axe_cdata.axe_tx_chain[i].axe_buf = NULL;
1060 if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1061 m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1062 sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1064 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1065 usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1066 sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1070 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1075 * Stop all chip I/O so that the kernel's probe routines don't
1076 * get confused by errant DMAs when rebooting.
1079 axe_shutdown(device_t dev)
1081 struct axe_softc *sc;
1083 sc = device_get_softc(dev);