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_kue.c,v 1.17.2.9 2003/04/13 02:39:25 murray Exp $
36 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
38 * Written by Bill Paul <wpaul@ee.columbia.edu>
39 * Electrical Engineering Department
40 * Columbia University, New York City
44 * The KLSI USB to ethernet adapter chip contains an USB serial interface,
45 * ethernet MAC and embedded microcontroller (called the QT Engine).
46 * The chip must have firmware loaded into it before it will operate.
47 * Packets are passed between the chip and host via bulk transfers.
48 * There is an interrupt endpoint mentioned in the software spec, however
49 * it's currently unused. This device is 10Mbps half-duplex only, hence
50 * there is no media selection logic. The MAC supports a 128 entry
51 * multicast filter, though the exact size of the filter can depend
52 * on the firmware. Curiously, while the software spec describes various
53 * ethernet statistics counters, my sample adapter and firmware combination
54 * claims not to support any statistics counters at all.
56 * Note that once we load the firmware in the device, we have to be
57 * careful not to load it again: if you restart your computer but
58 * leave the adapter attached to the USB controller, it may remain
59 * powered on and retain its firmware. In this case, we don't need
60 * to load the firmware a second time.
62 * Special thanks to Rob Furr for providing an ADS Technologies
63 * adapter for development and testing. No monkeys were harmed during
64 * the development of this driver.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/sockio.h>
71 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/socket.h>
77 #include <net/ifq_var.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
84 #include <machine/clock.h>
86 #include <bus/usb/usb.h>
87 #include <bus/usb/usbdi.h>
88 #include <bus/usb/usbdi_util.h>
89 #include <bus/usb/usbdivar.h>
90 #include <bus/usb/usb_ethersubr.h>
92 #include "if_kuereg.h"
93 #include <bus/usb/kue_fw.h>
95 MODULE_DEPEND(kue, usb, 1, 1, 1);
98 * Various supported device vendors/products.
100 static const struct usb_devno kue_devs[] = {
101 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX USB101 */
102 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USBETT */
103 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
104 { USB_DEVICE(0x0557, 0x2002) }, /* ATen UC10T */
105 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Ethernet adapter */
106 { USB_DEVICE(0x0565, 0x0003) }, /* Peracom Ethernet adapter*/
107 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Ethernet adapter */
108 { USB_DEVICE(0x05e9, 0x0008) }, /* AOX (rebranded Kawasaki?) USB101*/
109 { USB_DEVICE(0x05e9, 0x0008) }, /* Kawasaki LSI DUH3E10BT */
110 { USB_DEVICE(0x05e9, 0x0009) }, /* Kawasaki LSI DUH3E10BTN */
111 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
112 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS UBS-10BT */
113 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2102USB */
114 { USB_DEVICE(0x07aa, 0x0001) }, /* Corega USB-T*/
115 { USB_DEVICE(0x07b8, 0x4000) }, /* AboCom URE450 */
116 { USB_DEVICE(0x0846, 0x1001) }, /* Netgear EA101 */
117 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45*/
118 { USB_DEVICE(0x2001, 0x4000) }, /* D-Link DSB650C */
121 static int kue_match(device_t);
122 static int kue_attach(device_t);
123 static int kue_detach(device_t);
124 static void kue_shutdown(device_t);
125 static int kue_tx_list_init(struct kue_softc *);
126 static int kue_rx_list_init(struct kue_softc *);
127 static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
128 static int kue_encap(struct kue_softc *, struct mbuf *, int);
129 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
130 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
131 static void kue_start(struct ifnet *, struct ifaltq_subque *);
132 static void kue_rxstart(struct ifnet *);
133 static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
134 static void kue_init(void *);
135 static void kue_stop(struct kue_softc *);
136 static void kue_watchdog(struct ifnet *);
138 static void kue_setmulti(struct kue_softc *);
139 static void kue_reset(struct kue_softc *);
141 static usbd_status kue_do_request(usbd_device_handle,
142 usb_device_request_t *, void *);
143 static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
144 u_int16_t, char *, int);
145 static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
146 static int kue_load_fw(struct kue_softc *);
148 static device_method_t kue_methods[] = {
149 /* Device interface */
150 DEVMETHOD(device_probe, kue_match),
151 DEVMETHOD(device_attach, kue_attach),
152 DEVMETHOD(device_detach, kue_detach),
153 DEVMETHOD(device_shutdown, kue_shutdown),
158 static driver_t kue_driver = {
161 sizeof(struct kue_softc)
164 static devclass_t kue_devclass;
166 DECLARE_DUMMY_MODULE(if_kue);
167 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, NULL);
170 * We have a custom do_request function which is almost like the
171 * regular do_request function, except it has a much longer timeout.
172 * Why? Because we need to make requests over the control endpoint
173 * to download the firmware to the device, which can take longer
174 * than the default timeout.
177 kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
179 usbd_xfer_handle xfer;
182 xfer = usbd_alloc_xfer(dev);
183 usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
184 data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
185 err = usbd_sync_transfer(xfer);
186 usbd_free_xfer(xfer);
191 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
193 usbd_device_handle dev;
194 usb_device_request_t req;
198 return(USBD_NORMAL_COMPLETION);
204 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
207 USETW(req.wValue, word);
208 USETW(req.wIndex, 0);
209 USETW(req.wLength, 0);
211 err = kue_do_request(dev, &req, NULL);
219 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
222 usbd_device_handle dev;
223 usb_device_request_t req;
229 return(USBD_NORMAL_COMPLETION);
233 if (rw == KUE_CTL_WRITE)
234 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
236 req.bmRequestType = UT_READ_VENDOR_DEVICE;
239 USETW(req.wValue, val);
240 USETW(req.wIndex, 0);
241 USETW(req.wLength, len);
243 err = kue_do_request(dev, &req, data);
251 kue_load_fw(struct kue_softc *sc)
254 usb_device_descriptor_t *dd;
257 dd = &sc->kue_udev->ddesc;
258 hwrev = UGETW(dd->bcdDevice);
261 * First, check if we even need to load the firmware.
262 * If the device was still attached when the system was
263 * rebooted, it may already have firmware loaded in it.
264 * If this is the case, we don't need to do it again.
265 * And in fact, if we try to load it again, we'll hang,
266 * so we have to avoid this condition if we don't want
269 * We can test this quickly by checking the bcdRevision
270 * code. The NIC will return a different revision code if
271 * it's probed while the firmware is still loaded and
277 /* Load code segment */
278 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
279 0, kue_code_seg, sizeof(kue_code_seg));
281 kprintf("kue%d: failed to load code segment: %s\n",
282 sc->kue_unit, usbd_errstr(err));
286 /* Load fixup segment */
287 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
288 0, kue_fix_seg, sizeof(kue_fix_seg));
290 kprintf("kue%d: failed to load fixup segment: %s\n",
291 sc->kue_unit, usbd_errstr(err));
295 /* Send trigger command. */
296 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
297 0, kue_trig_seg, sizeof(kue_trig_seg));
299 kprintf("kue%d: failed to load trigger segment: %s\n",
300 sc->kue_unit, usbd_errstr(err));
308 kue_setmulti(struct kue_softc *sc)
311 struct ifmultiaddr *ifma;
314 ifp = &sc->arpcom.ac_if;
316 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
317 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
318 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
319 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
323 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
325 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
326 if (ifma->ifma_addr->sa_family != AF_LINK)
329 * If there are too many addresses for the
330 * internal filter, switch over to allmulti mode.
332 if (i == KUE_MCFILTCNT(sc))
334 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
335 KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
339 if (i == KUE_MCFILTCNT(sc))
340 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
342 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
343 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
344 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
347 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
353 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
354 * done after the firmware is loaded into the adapter in order to
355 * bring it into proper operation.
358 kue_reset(struct kue_softc *sc)
360 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
361 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
363 kprintf("kue%d: getting interface handle failed\n",
367 /* Wait a little while for the chip to get its brains in order. */
373 * Probe for a KLSI chip.
376 kue_match(device_t self)
378 struct usb_attach_arg *uaa = device_get_ivars(self);
380 if (uaa->iface == NULL)
383 return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ?
384 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
388 * Attach the interface. Allocate softc structures, do
389 * setup and ethernet/BPF attach.
392 kue_attach(device_t self)
394 struct kue_softc *sc = device_get_softc(self);
395 struct usb_attach_arg *uaa = device_get_ivars(self);
397 usb_interface_descriptor_t *id;
398 usb_endpoint_descriptor_t *ed;
401 sc->kue_iface = uaa->iface;
402 sc->kue_udev = uaa->device;
403 sc->kue_unit = device_get_unit(self);
405 id = usbd_get_interface_descriptor(uaa->iface);
407 /* Find endpoints. */
408 for (i = 0; i < id->bNumEndpoints; i++) {
409 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
411 kprintf("kue%d: couldn't get ep %d\n",
415 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
416 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
417 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
418 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
419 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
420 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
421 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
422 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
423 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
427 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
428 mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
429 MTX_DEF | MTX_RECURSE);
433 /* Load the firmware into the NIC. */
434 if (kue_load_fw(sc)) {
436 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
437 mtx_destroy(&sc->kue_mtx);
442 /* Reset the adapter. */
445 /* Read ethernet descriptor */
446 kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
447 0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
449 sc->kue_mcfilters = kmalloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
452 ifp = &sc->arpcom.ac_if;
454 if_initname(ifp, "kue", sc->kue_unit);
455 ifp->if_mtu = ETHERMTU;
456 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
457 ifp->if_ioctl = kue_ioctl;
458 ifp->if_start = kue_start;
459 ifp->if_watchdog = kue_watchdog;
460 ifp->if_init = kue_init;
461 ifp->if_baudrate = 10000000;
462 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
463 ifq_set_ready(&ifp->if_snd);
466 * Call MI attach routine.
468 ether_ifattach(ifp, sc->kue_desc.kue_macaddr, NULL);
469 usb_register_netisr();
478 kue_detach(device_t dev)
480 struct kue_softc *sc;
483 sc = device_get_softc(dev);
485 ifp = &sc->arpcom.ac_if;
492 if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
493 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
494 if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
495 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
496 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
497 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
499 if (sc->kue_mcfilters != NULL)
500 kfree(sc->kue_mcfilters, M_USBDEV);
503 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
504 mtx_destroy(&sc->kue_mtx);
511 * Initialize an RX descriptor and attach an MBUF cluster.
514 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
516 struct mbuf *m_new = NULL;
519 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
521 kprintf("kue%d: no memory for rx list "
522 "-- packet dropped!\n", sc->kue_unit);
526 MCLGET(m_new, MB_DONTWAIT);
527 if (!(m_new->m_flags & M_EXT)) {
528 kprintf("kue%d: no memory for rx list "
529 "-- packet dropped!\n", sc->kue_unit);
533 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
536 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
537 m_new->m_data = m_new->m_ext.ext_buf;
546 kue_rx_list_init(struct kue_softc *sc)
548 struct kue_cdata *cd;
553 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
554 c = &cd->kue_rx_chain[i];
557 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
559 if (c->kue_xfer == NULL) {
560 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
561 if (c->kue_xfer == NULL)
570 kue_tx_list_init(struct kue_softc *sc)
572 struct kue_cdata *cd;
577 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
578 c = &cd->kue_tx_chain[i];
582 if (c->kue_xfer == NULL) {
583 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
584 if (c->kue_xfer == NULL)
587 c->kue_buf = kmalloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
594 kue_rxstart(struct ifnet *ifp)
596 struct kue_softc *sc;
601 c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
603 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
604 IFNET_STAT_INC(ifp, ierrors, 1);
608 /* Setup new transfer. */
609 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
610 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
611 USBD_NO_TIMEOUT, kue_rxeof);
612 usbd_transfer(c->kue_xfer);
620 * A frame has been uploaded: pass the resulting mbuf chain up to
621 * the higher level protocols.
624 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
626 struct kue_softc *sc;
636 ifp = &sc->arpcom.ac_if;
638 if (!(ifp->if_flags & IFF_RUNNING)) {
643 if (status != USBD_NORMAL_COMPLETION) {
644 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
648 if (usbd_ratecheck(&sc->kue_rx_notice))
649 kprintf("kue%d: usb error on rx: %s\n", sc->kue_unit,
650 usbd_errstr(status));
651 if (status == USBD_STALLED)
652 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
656 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
661 len = *mtod(m, u_int16_t *);
662 m_adj(m, sizeof(u_int16_t));
664 /* No errors; receive the packet. */
667 if (len < sizeof(struct ether_header)) {
668 IFNET_STAT_INC(ifp, ierrors, 1);
672 IFNET_STAT_INC(ifp, ipackets, 1);
673 m->m_pkthdr.rcvif = ifp;
674 m->m_pkthdr.len = m->m_len = total_len;
676 /* Put the packet on the special USB input queue. */
685 /* Setup new transfer. */
686 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
687 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
688 USBD_NO_TIMEOUT, kue_rxeof);
689 usbd_transfer(c->kue_xfer);
696 * A frame was downloaded to the chip. It's safe for us to clean up
701 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
703 struct kue_softc *sc;
712 ifp = &sc->arpcom.ac_if;
714 ifq_clr_oactive(&ifp->if_snd);
716 if (status != USBD_NORMAL_COMPLETION) {
717 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
721 kprintf("kue%d: usb error on tx: %s\n", sc->kue_unit,
722 usbd_errstr(status));
723 if (status == USBD_STALLED)
724 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
729 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
731 if (c->kue_mbuf != NULL) {
732 m_freem(c->kue_mbuf);
737 IFNET_STAT_INC(ifp, oerrors, 1);
739 IFNET_STAT_INC(ifp, opackets, 1);
741 if (!ifq_is_empty(&ifp->if_snd))
750 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
756 c = &sc->kue_cdata.kue_tx_chain[idx];
759 * Copy the mbuf data into a contiguous buffer, leaving two
760 * bytes at the beginning to hold the frame length.
762 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
765 total_len = m->m_pkthdr.len + 2;
766 total_len += 64 - (total_len % 64);
768 /* Frame length is specified in the first 2 bytes of the buffer. */
769 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
770 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
772 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
773 c, c->kue_buf, total_len, 0, 10000, kue_txeof);
776 err = usbd_transfer(c->kue_xfer);
777 if (err != USBD_IN_PROGRESS) {
782 sc->kue_cdata.kue_tx_cnt++;
788 kue_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
790 struct kue_softc *sc;
791 struct mbuf *m_head = NULL;
793 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
798 if (ifq_is_oactive(&ifp->if_snd)) {
803 m_head = ifq_dequeue(&ifp->if_snd);
804 if (m_head == NULL) {
809 if (kue_encap(sc, m_head, 0)) {
810 /* kue_encap() will free m_head, if we reach here */
811 ifq_set_oactive(&ifp->if_snd);
817 * If there's a BPF listener, bounce a copy of this frame
820 BPF_MTAP(ifp, m_head);
822 ifq_set_oactive(&ifp->if_snd);
825 * Set a timeout in case the chip goes out to lunch.
836 struct kue_softc *sc = xsc;
837 struct ifnet *ifp = &sc->arpcom.ac_if;
844 if (ifp->if_flags & IFF_RUNNING) {
849 /* Set MAC address */
850 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
851 0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
853 sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
855 /* If we want promiscuous mode, set the allframes bit. */
856 if (ifp->if_flags & IFF_PROMISC)
857 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
859 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
861 /* I'm not sure how to tune these. */
864 * Leave this one alone for now; setting it
865 * wrong causes lockups on some machines/controllers.
867 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
869 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
872 if (kue_tx_list_init(sc) == ENOBUFS) {
873 kprintf("kue%d: tx list init failed\n", sc->kue_unit);
879 if (kue_rx_list_init(sc) == ENOBUFS) {
880 kprintf("kue%d: rx list init failed\n", sc->kue_unit);
885 /* Load the multicast filter. */
888 /* Open RX and TX pipes. */
889 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
890 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
892 kprintf("kue%d: open rx pipe failed: %s\n",
893 sc->kue_unit, usbd_errstr(err));
898 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
899 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
901 kprintf("kue%d: open tx pipe failed: %s\n",
902 sc->kue_unit, usbd_errstr(err));
907 /* Start up the receive pipe. */
908 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
909 c = &sc->kue_cdata.kue_rx_chain[i];
910 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
911 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
912 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
913 usbd_transfer(c->kue_xfer);
916 ifp->if_flags |= IFF_RUNNING;
917 ifq_clr_oactive(&ifp->if_snd)
925 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
927 struct kue_softc *sc = ifp->if_softc;
934 if (ifp->if_flags & IFF_UP) {
935 if (ifp->if_flags & IFF_RUNNING &&
936 ifp->if_flags & IFF_PROMISC &&
937 !(sc->kue_if_flags & IFF_PROMISC)) {
938 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
939 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
941 } else if (ifp->if_flags & IFF_RUNNING &&
942 !(ifp->if_flags & IFF_PROMISC) &&
943 sc->kue_if_flags & IFF_PROMISC) {
944 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
945 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
947 } else if (!(ifp->if_flags & IFF_RUNNING))
950 if (ifp->if_flags & IFF_RUNNING)
953 sc->kue_if_flags = ifp->if_flags;
962 error = ether_ioctl(ifp, command, data);
972 kue_watchdog(struct ifnet *ifp)
974 struct kue_softc *sc;
980 IFNET_STAT_INC(ifp, oerrors, 1);
981 kprintf("kue%d: watchdog timeout\n", sc->kue_unit);
983 c = &sc->kue_cdata.kue_tx_chain[0];
984 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
985 kue_txeof(c->kue_xfer, c, stat);
987 if (!ifq_is_empty(&ifp->if_snd))
995 * Stop the adapter and free any mbufs allocated to the
999 kue_stop(struct kue_softc *sc)
1006 ifp = &sc->arpcom.ac_if;
1009 /* Stop transfers. */
1010 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1011 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1013 kprintf("kue%d: abort rx pipe failed: %s\n",
1014 sc->kue_unit, usbd_errstr(err));
1016 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1018 kprintf("kue%d: close rx pipe failed: %s\n",
1019 sc->kue_unit, usbd_errstr(err));
1021 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1024 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1025 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1027 kprintf("kue%d: abort tx pipe failed: %s\n",
1028 sc->kue_unit, usbd_errstr(err));
1030 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1032 kprintf("kue%d: close tx pipe failed: %s\n",
1033 sc->kue_unit, usbd_errstr(err));
1035 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1038 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1039 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1041 kprintf("kue%d: abort intr pipe failed: %s\n",
1042 sc->kue_unit, usbd_errstr(err));
1044 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1046 kprintf("kue%d: close intr pipe failed: %s\n",
1047 sc->kue_unit, usbd_errstr(err));
1049 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1052 /* Free RX resources. */
1053 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1054 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
1055 kfree(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
1056 sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
1058 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1059 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1060 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1062 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1063 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1064 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1068 /* Free TX resources. */
1069 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1070 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
1071 kfree(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
1072 sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
1074 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1075 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1076 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1078 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1079 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1080 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1084 ifp->if_flags &= ~IFF_RUNNING;
1085 ifq_clr_oactive(&ifp->if_snd);
1092 * Stop all chip I/O so that the kernel's probe routines don't
1093 * get confused by errant DMAs when rebooting.
1096 kue_shutdown(device_t dev)
1098 struct kue_softc *sc;
1100 sc = device_get_softc(dev);