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 $
33 * $DragonFly: src/sys/dev/netif/kue/if_kue.c,v 1.21 2006/09/05 03:48:10 dillon Exp $
37 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
39 * Written by Bill Paul <wpaul@ee.columbia.edu>
40 * Electrical Engineering Department
41 * Columbia University, New York City
45 * The KLSI USB to ethernet adapter chip contains an USB serial interface,
46 * ethernet MAC and embedded microcontroller (called the QT Engine).
47 * The chip must have firmware loaded into it before it will operate.
48 * Packets are passed between the chip and host via bulk transfers.
49 * There is an interrupt endpoint mentioned in the software spec, however
50 * it's currently unused. This device is 10Mbps half-duplex only, hence
51 * there is no media selection logic. The MAC supports a 128 entry
52 * multicast filter, though the exact size of the filter can depend
53 * on the firmware. Curiously, while the software spec describes various
54 * ethernet statistics counters, my sample adapter and firmware combination
55 * claims not to support any statistics counters at all.
57 * Note that once we load the firmware in the device, we have to be
58 * careful not to load it again: if you restart your computer but
59 * leave the adapter attached to the USB controller, it may remain
60 * powered on and retain its firmware. In this case, we don't need
61 * to load the firmware a second time.
63 * Special thanks to Rob Furr for providing an ADS Technologies
64 * adapter for development and testing. No monkeys were harmed during
65 * the development of this driver.
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/sockio.h>
72 #include <sys/malloc.h>
73 #include <sys/kernel.h>
74 #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>
86 #include <machine/bus.h>
87 #if defined(__DragonFly__) || __FreeBSD_version < 500000
88 #include <machine/clock.h>
91 #include <bus/usb/usb.h>
92 #include <bus/usb/usbdi.h>
93 #include <bus/usb/usbdi_util.h>
94 #include <bus/usb/usbdivar.h>
95 #include <bus/usb/usbdevs.h>
96 #include <bus/usb/usb_ethersubr.h>
98 #include "if_kuereg.h"
99 #include <bus/usb/kue_fw.h>
101 MODULE_DEPEND(kue, usb, 1, 1, 1);
104 * Various supported device vendors/products.
106 Static struct kue_type kue_devs[] = {
107 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
108 { USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101 },
109 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
110 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
111 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
112 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
113 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
114 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
115 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
116 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
117 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
118 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
119 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
120 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
121 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
122 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
123 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
124 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
128 Static int kue_match(device_ptr_t);
129 Static int kue_attach(device_ptr_t);
130 Static int kue_detach(device_ptr_t);
131 Static void kue_shutdown(device_ptr_t);
132 Static int kue_tx_list_init(struct kue_softc *);
133 Static int kue_rx_list_init(struct kue_softc *);
134 Static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
135 Static int kue_encap(struct kue_softc *, struct mbuf *, int);
136 Static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
137 Static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
138 Static void kue_start(struct ifnet *);
139 Static void kue_rxstart(struct ifnet *);
140 Static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
141 Static void kue_init(void *);
142 Static void kue_stop(struct kue_softc *);
143 Static void kue_watchdog(struct ifnet *);
145 Static void kue_setmulti(struct kue_softc *);
146 Static void kue_reset(struct kue_softc *);
148 Static usbd_status kue_do_request(usbd_device_handle,
149 usb_device_request_t *, void *);
150 Static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
151 u_int16_t, char *, int);
152 Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
153 Static int kue_load_fw(struct kue_softc *);
155 Static device_method_t kue_methods[] = {
156 /* Device interface */
157 DEVMETHOD(device_probe, kue_match),
158 DEVMETHOD(device_attach, kue_attach),
159 DEVMETHOD(device_detach, kue_detach),
160 DEVMETHOD(device_shutdown, kue_shutdown),
165 Static driver_t kue_driver = {
168 sizeof(struct kue_softc)
171 Static devclass_t kue_devclass;
173 DECLARE_DUMMY_MODULE(if_kue);
174 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
177 * We have a custom do_request function which is almost like the
178 * regular do_request function, except it has a much longer timeout.
179 * Why? Because we need to make requests over the control endpoint
180 * to download the firmware to the device, which can take longer
181 * than the default timeout.
184 kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
186 usbd_xfer_handle xfer;
189 xfer = usbd_alloc_xfer(dev);
190 usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
191 data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
192 err = usbd_sync_transfer(xfer);
193 usbd_free_xfer(xfer);
198 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
200 usbd_device_handle dev;
201 usb_device_request_t req;
205 return(USBD_NORMAL_COMPLETION);
211 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
214 USETW(req.wValue, word);
215 USETW(req.wIndex, 0);
216 USETW(req.wLength, 0);
218 err = kue_do_request(dev, &req, NULL);
226 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
229 usbd_device_handle dev;
230 usb_device_request_t req;
236 return(USBD_NORMAL_COMPLETION);
240 if (rw == KUE_CTL_WRITE)
241 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
243 req.bmRequestType = UT_READ_VENDOR_DEVICE;
246 USETW(req.wValue, val);
247 USETW(req.wIndex, 0);
248 USETW(req.wLength, len);
250 err = kue_do_request(dev, &req, data);
258 kue_load_fw(struct kue_softc *sc)
261 usb_device_descriptor_t *dd;
264 dd = &sc->kue_udev->ddesc;
265 hwrev = UGETW(dd->bcdDevice);
268 * First, check if we even need to load the firmware.
269 * If the device was still attached when the system was
270 * rebooted, it may already have firmware loaded in it.
271 * If this is the case, we don't need to do it again.
272 * And in fact, if we try to load it again, we'll hang,
273 * so we have to avoid this condition if we don't want
276 * We can test this quickly by checking the bcdRevision
277 * code. The NIC will return a different revision code if
278 * it's probed while the firmware is still loaded and
284 /* Load code segment */
285 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
286 0, kue_code_seg, sizeof(kue_code_seg));
288 printf("kue%d: failed to load code segment: %s\n",
289 sc->kue_unit, usbd_errstr(err));
293 /* Load fixup segment */
294 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
295 0, kue_fix_seg, sizeof(kue_fix_seg));
297 printf("kue%d: failed to load fixup segment: %s\n",
298 sc->kue_unit, usbd_errstr(err));
302 /* Send trigger command. */
303 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
304 0, kue_trig_seg, sizeof(kue_trig_seg));
306 printf("kue%d: failed to load trigger segment: %s\n",
307 sc->kue_unit, usbd_errstr(err));
315 kue_setmulti(struct kue_softc *sc)
318 struct ifmultiaddr *ifma;
321 ifp = &sc->arpcom.ac_if;
323 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
324 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
325 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
326 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
330 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
332 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
333 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
335 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
338 if (ifma->ifma_addr->sa_family != AF_LINK)
341 * If there are too many addresses for the
342 * internal filter, switch over to allmulti mode.
344 if (i == KUE_MCFILTCNT(sc))
346 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
347 KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
351 if (i == KUE_MCFILTCNT(sc))
352 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
354 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
355 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
356 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
359 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
365 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
366 * done after the firmware is loaded into the adapter in order to
367 * bring it into proper operation.
370 kue_reset(struct kue_softc *sc)
372 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
373 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
375 printf("kue%d: getting interface handle failed\n",
379 /* Wait a little while for the chip to get its brains in order. */
385 * Probe for a KLSI chip.
389 USB_MATCH_START(kue, uaa);
397 if (uaa->vendor == t->kue_vid &&
398 uaa->product == t->kue_did) {
399 return(UMATCH_VENDOR_PRODUCT);
408 * Attach the interface. Allocate softc structures, do
409 * setup and ethernet/BPF attach.
413 USB_ATTACH_START(kue, sc, uaa);
417 usb_interface_descriptor_t *id;
418 usb_endpoint_descriptor_t *ed;
421 sc->kue_iface = uaa->iface;
422 sc->kue_udev = uaa->device;
423 sc->kue_unit = device_get_unit(self);
425 id = usbd_get_interface_descriptor(uaa->iface);
427 usbd_devinfo(uaa->device, 0, devinfo);
428 device_set_desc_copy(self, devinfo);
429 printf("%s: %s\n", USBDEVNAME(self), devinfo);
431 /* Find endpoints. */
432 for (i = 0; i < id->bNumEndpoints; i++) {
433 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
435 printf("kue%d: couldn't get ep %d\n",
437 USB_ATTACH_ERROR_RETURN;
439 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
440 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
441 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
442 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
443 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
444 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
445 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
446 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
447 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
451 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
452 mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
453 MTX_DEF | MTX_RECURSE);
457 /* Load the firmware into the NIC. */
458 if (kue_load_fw(sc)) {
460 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
461 mtx_destroy(&sc->kue_mtx);
463 USB_ATTACH_ERROR_RETURN;
466 /* Reset the adapter. */
469 /* Read ethernet descriptor */
470 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
471 0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
473 sc->kue_mcfilters = kmalloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
476 ifp = &sc->arpcom.ac_if;
478 if_initname(ifp, "kue", sc->kue_unit);
479 ifp->if_mtu = ETHERMTU;
480 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
481 ifp->if_ioctl = kue_ioctl;
482 ifp->if_start = kue_start;
483 ifp->if_watchdog = kue_watchdog;
484 ifp->if_init = kue_init;
485 ifp->if_baudrate = 10000000;
486 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
487 ifq_set_ready(&ifp->if_snd);
490 * Call MI attach routine.
492 ether_ifattach(ifp, sc->kue_desc.kue_macaddr, NULL);
493 usb_register_netisr();
498 USB_ATTACH_SUCCESS_RETURN;
502 kue_detach(device_ptr_t dev)
504 struct kue_softc *sc;
507 sc = device_get_softc(dev);
509 ifp = &sc->arpcom.ac_if;
516 if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
517 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
518 if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
519 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
520 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
521 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
523 if (sc->kue_mcfilters != NULL)
524 kfree(sc->kue_mcfilters, M_USBDEV);
527 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
528 mtx_destroy(&sc->kue_mtx);
535 * Initialize an RX descriptor and attach an MBUF cluster.
538 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
540 struct mbuf *m_new = NULL;
543 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
545 printf("kue%d: no memory for rx list "
546 "-- packet dropped!\n", sc->kue_unit);
550 MCLGET(m_new, MB_DONTWAIT);
551 if (!(m_new->m_flags & M_EXT)) {
552 printf("kue%d: no memory for rx list "
553 "-- packet dropped!\n", sc->kue_unit);
557 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
560 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
561 m_new->m_data = m_new->m_ext.ext_buf;
570 kue_rx_list_init(struct kue_softc *sc)
572 struct kue_cdata *cd;
577 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
578 c = &cd->kue_rx_chain[i];
581 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
583 if (c->kue_xfer == NULL) {
584 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
585 if (c->kue_xfer == NULL)
594 kue_tx_list_init(struct kue_softc *sc)
596 struct kue_cdata *cd;
601 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
602 c = &cd->kue_tx_chain[i];
606 if (c->kue_xfer == NULL) {
607 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
608 if (c->kue_xfer == NULL)
611 c->kue_buf = kmalloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
612 if (c->kue_buf == NULL)
620 kue_rxstart(struct ifnet *ifp)
622 struct kue_softc *sc;
627 c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
629 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
634 /* Setup new transfer. */
635 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
636 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
637 USBD_NO_TIMEOUT, kue_rxeof);
638 usbd_transfer(c->kue_xfer);
646 * A frame has been uploaded: pass the resulting mbuf chain up to
647 * the higher level protocols.
650 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
652 struct kue_softc *sc;
662 ifp = &sc->arpcom.ac_if;
664 if (!(ifp->if_flags & IFF_RUNNING)) {
669 if (status != USBD_NORMAL_COMPLETION) {
670 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
674 if (usbd_ratecheck(&sc->kue_rx_notice))
675 printf("kue%d: usb error on rx: %s\n", sc->kue_unit,
676 usbd_errstr(status));
677 if (status == USBD_STALLED)
678 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
682 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
687 len = *mtod(m, u_int16_t *);
688 m_adj(m, sizeof(u_int16_t));
690 /* No errors; receive the packet. */
693 if (len < sizeof(struct ether_header)) {
699 m->m_pkthdr.rcvif = ifp;
700 m->m_pkthdr.len = m->m_len = total_len;
702 /* Put the packet on the special USB input queue. */
711 /* Setup new transfer. */
712 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
713 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
714 USBD_NO_TIMEOUT, kue_rxeof);
715 usbd_transfer(c->kue_xfer);
722 * A frame was downloaded to the chip. It's safe for us to clean up
727 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
729 struct kue_softc *sc;
738 ifp = &sc->arpcom.ac_if;
740 ifp->if_flags &= ~IFF_OACTIVE;
742 if (status != USBD_NORMAL_COMPLETION) {
743 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
747 printf("kue%d: usb error on tx: %s\n", sc->kue_unit,
748 usbd_errstr(status));
749 if (status == USBD_STALLED)
750 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
755 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
757 if (c->kue_mbuf != NULL) {
758 m_freem(c->kue_mbuf);
767 if (!ifq_is_empty(&ifp->if_snd))
768 (*ifp->if_start)(ifp);
776 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
782 c = &sc->kue_cdata.kue_tx_chain[idx];
785 * Copy the mbuf data into a contiguous buffer, leaving two
786 * bytes at the beginning to hold the frame length.
788 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
791 total_len = m->m_pkthdr.len + 2;
792 total_len += 64 - (total_len % 64);
794 /* Frame length is specified in the first 2 bytes of the buffer. */
795 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
796 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
798 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
799 c, c->kue_buf, total_len, 0, 10000, kue_txeof);
802 err = usbd_transfer(c->kue_xfer);
803 if (err != USBD_IN_PROGRESS) {
808 sc->kue_cdata.kue_tx_cnt++;
814 kue_start(struct ifnet *ifp)
816 struct kue_softc *sc;
817 struct mbuf *m_head = NULL;
822 if (ifp->if_flags & IFF_OACTIVE) {
827 m_head = ifq_poll(&ifp->if_snd);
828 if (m_head == NULL) {
833 if (kue_encap(sc, m_head, 0)) {
834 ifp->if_flags |= IFF_OACTIVE;
838 ifq_dequeue(&ifp->if_snd, m_head);
841 * If there's a BPF listener, bounce a copy of this frame
844 BPF_MTAP(ifp, m_head);
846 ifp->if_flags |= IFF_OACTIVE;
849 * Set a timeout in case the chip goes out to lunch.
860 struct kue_softc *sc = xsc;
861 struct ifnet *ifp = &sc->arpcom.ac_if;
868 if (ifp->if_flags & IFF_RUNNING) {
873 /* Set MAC address */
874 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
875 0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
877 sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
879 /* If we want promiscuous mode, set the allframes bit. */
880 if (ifp->if_flags & IFF_PROMISC)
881 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
883 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
885 /* I'm not sure how to tune these. */
888 * Leave this one alone for now; setting it
889 * wrong causes lockups on some machines/controllers.
891 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
893 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
896 if (kue_tx_list_init(sc) == ENOBUFS) {
897 printf("kue%d: tx list init failed\n", sc->kue_unit);
903 if (kue_rx_list_init(sc) == ENOBUFS) {
904 printf("kue%d: rx list init failed\n", sc->kue_unit);
909 /* Load the multicast filter. */
912 /* Open RX and TX pipes. */
913 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
914 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
916 printf("kue%d: open rx pipe failed: %s\n",
917 sc->kue_unit, usbd_errstr(err));
922 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
923 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
925 printf("kue%d: open tx pipe failed: %s\n",
926 sc->kue_unit, usbd_errstr(err));
931 /* Start up the receive pipe. */
932 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
933 c = &sc->kue_cdata.kue_rx_chain[i];
934 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
935 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
936 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
937 usbd_transfer(c->kue_xfer);
940 ifp->if_flags |= IFF_RUNNING;
941 ifp->if_flags &= ~IFF_OACTIVE;
949 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
951 struct kue_softc *sc = ifp->if_softc;
958 if (ifp->if_flags & IFF_UP) {
959 if (ifp->if_flags & IFF_RUNNING &&
960 ifp->if_flags & IFF_PROMISC &&
961 !(sc->kue_if_flags & IFF_PROMISC)) {
962 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
963 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
965 } else if (ifp->if_flags & IFF_RUNNING &&
966 !(ifp->if_flags & IFF_PROMISC) &&
967 sc->kue_if_flags & IFF_PROMISC) {
968 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
969 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
971 } else if (!(ifp->if_flags & IFF_RUNNING))
974 if (ifp->if_flags & IFF_RUNNING)
977 sc->kue_if_flags = ifp->if_flags;
986 error = ether_ioctl(ifp, command, data);
996 kue_watchdog(struct ifnet *ifp)
998 struct kue_softc *sc;
1005 printf("kue%d: watchdog timeout\n", sc->kue_unit);
1007 c = &sc->kue_cdata.kue_tx_chain[0];
1008 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1009 kue_txeof(c->kue_xfer, c, stat);
1011 if (ifq_is_empty(&ifp->if_snd))
1019 * Stop the adapter and free any mbufs allocated to the
1023 kue_stop(struct kue_softc *sc)
1030 ifp = &sc->arpcom.ac_if;
1033 /* Stop transfers. */
1034 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1035 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1037 printf("kue%d: abort rx pipe failed: %s\n",
1038 sc->kue_unit, usbd_errstr(err));
1040 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1042 printf("kue%d: close rx pipe failed: %s\n",
1043 sc->kue_unit, usbd_errstr(err));
1045 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1048 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1049 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1051 printf("kue%d: abort tx pipe failed: %s\n",
1052 sc->kue_unit, usbd_errstr(err));
1054 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1056 printf("kue%d: close tx pipe failed: %s\n",
1057 sc->kue_unit, usbd_errstr(err));
1059 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1062 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1063 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1065 printf("kue%d: abort intr pipe failed: %s\n",
1066 sc->kue_unit, usbd_errstr(err));
1068 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1070 printf("kue%d: close intr pipe failed: %s\n",
1071 sc->kue_unit, usbd_errstr(err));
1073 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1076 /* Free RX resources. */
1077 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1078 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
1079 kfree(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
1080 sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
1082 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1083 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1084 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1086 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1087 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1088 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1092 /* Free TX resources. */
1093 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1094 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
1095 kfree(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
1096 sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
1098 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1099 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1100 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1102 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1103 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1104 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1108 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1115 * Stop all chip I/O so that the kernel's probe routines don't
1116 * get confused by errant DMAs when rebooting.
1119 kue_shutdown(device_ptr_t dev)
1121 struct kue_softc *sc;
1123 sc = device_get_softc(dev);