2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1997, 1998, 1999, 2000
5 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
41 * Written by Bill Paul <wpaul@ee.columbia.edu>
42 * Electrical Engineering Department
43 * Columbia University, New York City
47 * The KLSI USB to ethernet adapter chip contains an USB serial interface,
48 * ethernet MAC and embedded microcontroller (called the QT Engine).
49 * The chip must have firmware loaded into it before it will operate.
50 * Packets are passed between the chip and host via bulk transfers.
51 * There is an interrupt endpoint mentioned in the software spec, however
52 * it's currently unused. This device is 10Mbps half-duplex only, hence
53 * there is no media selection logic. The MAC supports a 128 entry
54 * multicast filter, though the exact size of the filter can depend
55 * on the firmware. Curiously, while the software spec describes various
56 * ethernet statistics counters, my sample adapter and firmware combination
57 * claims not to support any statistics counters at all.
59 * Note that once we load the firmware in the device, we have to be
60 * careful not to load it again: if you restart your computer but
61 * leave the adapter attached to the USB controller, it may remain
62 * powered on and retain its firmware. In this case, we don't need
63 * to load the firmware a second time.
65 * Special thanks to Rob Furr for providing an ADS Technologies
66 * adapter for development and testing. No monkeys were harmed during
67 * the development of this driver.
70 #include <sys/stdint.h>
71 #include <sys/stddef.h>
72 #include <sys/param.h>
73 #include <sys/queue.h>
74 #include <sys/types.h>
75 #include <sys/systm.h>
76 #include <sys/socket.h>
77 #include <sys/kernel.h>
79 #include <sys/module.h>
81 #include <sys/mutex.h>
82 #include <sys/condvar.h>
83 #include <sys/sysctl.h>
85 #include <sys/unistd.h>
86 #include <sys/callout.h>
87 #include <sys/malloc.h>
91 #include <net/if_var.h>
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbdi.h>
95 #include <dev/usb/usbdi_util.h>
98 #define USB_DEBUG_VAR kue_debug
99 #include <dev/usb/usb_debug.h>
100 #include <dev/usb/usb_process.h>
102 #include <dev/usb/net/usb_ethernet.h>
103 #include <dev/usb/net/if_kuereg.h>
104 #include <dev/usb/net/if_kuefw.h>
107 * Various supported device vendors/products.
109 static const STRUCT_USB_HOST_ID kue_devs[] = {
110 #define KUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
111 KUE_DEV(3COM, 3C19250),
112 KUE_DEV(3COM, 3C460),
113 KUE_DEV(ABOCOM, URE450),
114 KUE_DEV(ADS, UBS10BT),
115 KUE_DEV(ADS, UBS10BTX),
116 KUE_DEV(AOX, USB101),
118 KUE_DEV(ATEN, DSB650C),
119 KUE_DEV(ATEN, UC10T),
120 KUE_DEV(COREGA, ETHER_USB_T),
121 KUE_DEV(DLINK, DSB650C),
122 KUE_DEV(ENTREGA, E45),
123 KUE_DEV(ENTREGA, XX1),
124 KUE_DEV(ENTREGA, XX2),
125 KUE_DEV(IODATA, USBETT),
127 KUE_DEV(KINGSTON, XX1),
128 KUE_DEV(KLSI, DUH3E10BT),
129 KUE_DEV(KLSI, DUH3E10BTN),
130 KUE_DEV(LINKSYS, USB10T),
131 KUE_DEV(MOBILITY, EA),
132 KUE_DEV(NETGEAR, EA101),
133 KUE_DEV(NETGEAR, EA101X),
134 KUE_DEV(PERACOM, ENET),
135 KUE_DEV(PERACOM, ENET2),
136 KUE_DEV(PERACOM, ENET3),
137 KUE_DEV(PORTGEAR, EA8),
138 KUE_DEV(PORTGEAR, EA9),
139 KUE_DEV(PORTSMITH, EEA),
141 KUE_DEV(SILICOM, GPE),
142 KUE_DEV(SILICOM, U2E),
143 KUE_DEV(SMC, 2102USB),
149 static device_probe_t kue_probe;
150 static device_attach_t kue_attach;
151 static device_detach_t kue_detach;
153 static usb_callback_t kue_bulk_read_callback;
154 static usb_callback_t kue_bulk_write_callback;
156 static uether_fn_t kue_attach_post;
157 static uether_fn_t kue_init;
158 static uether_fn_t kue_stop;
159 static uether_fn_t kue_start;
160 static uether_fn_t kue_setmulti;
161 static uether_fn_t kue_setpromisc;
163 static int kue_do_request(struct kue_softc *,
164 struct usb_device_request *, void *);
165 static int kue_setword(struct kue_softc *, uint8_t, uint16_t);
166 static int kue_ctl(struct kue_softc *, uint8_t, uint8_t, uint16_t,
168 static int kue_load_fw(struct kue_softc *);
169 static void kue_reset(struct kue_softc *);
172 static int kue_debug = 0;
174 static SYSCTL_NODE(_hw_usb, OID_AUTO, kue, CTLFLAG_RW, 0, "USB kue");
175 SYSCTL_INT(_hw_usb_kue, OID_AUTO, debug, CTLFLAG_RWTUN, &kue_debug, 0,
179 static const struct usb_config kue_config[KUE_N_TRANSFER] = {
183 .endpoint = UE_ADDR_ANY,
184 .direction = UE_DIR_OUT,
185 .bufsize = (MCLBYTES + 2 + 64),
186 .flags = {.pipe_bof = 1,},
187 .callback = kue_bulk_write_callback,
188 .timeout = 10000, /* 10 seconds */
193 .endpoint = UE_ADDR_ANY,
194 .direction = UE_DIR_IN,
195 .bufsize = (MCLBYTES + 2),
196 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
197 .callback = kue_bulk_read_callback,
198 .timeout = 0, /* no timeout */
202 static device_method_t kue_methods[] = {
203 /* Device interface */
204 DEVMETHOD(device_probe, kue_probe),
205 DEVMETHOD(device_attach, kue_attach),
206 DEVMETHOD(device_detach, kue_detach),
211 static driver_t kue_driver = {
213 .methods = kue_methods,
214 .size = sizeof(struct kue_softc),
217 static devclass_t kue_devclass;
219 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, NULL, 0);
220 MODULE_DEPEND(kue, uether, 1, 1, 1);
221 MODULE_DEPEND(kue, usb, 1, 1, 1);
222 MODULE_DEPEND(kue, ether, 1, 1, 1);
223 MODULE_VERSION(kue, 1);
224 USB_PNP_HOST_INFO(kue_devs);
226 static const struct usb_ether_methods kue_ue_methods = {
227 .ue_attach_post = kue_attach_post,
228 .ue_start = kue_start,
231 .ue_setmulti = kue_setmulti,
232 .ue_setpromisc = kue_setpromisc,
236 * We have a custom do_request function which is almost like the
237 * regular do_request function, except it has a much longer timeout.
238 * Why? Because we need to make requests over the control endpoint
239 * to download the firmware to the device, which can take longer
240 * than the default timeout.
243 kue_do_request(struct kue_softc *sc, struct usb_device_request *req,
248 err = uether_do_request(&sc->sc_ue, req, data, 60000);
254 kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
256 struct usb_device_request req;
258 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
260 USETW(req.wValue, word);
261 USETW(req.wIndex, 0);
262 USETW(req.wLength, 0);
264 return (kue_do_request(sc, &req, NULL));
268 kue_ctl(struct kue_softc *sc, uint8_t rw, uint8_t breq,
269 uint16_t val, void *data, int len)
271 struct usb_device_request req;
273 if (rw == KUE_CTL_WRITE)
274 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
276 req.bmRequestType = UT_READ_VENDOR_DEVICE;
280 USETW(req.wValue, val);
281 USETW(req.wIndex, 0);
282 USETW(req.wLength, len);
284 return (kue_do_request(sc, &req, data));
288 kue_load_fw(struct kue_softc *sc)
290 struct usb_device_descriptor *dd;
294 dd = usbd_get_device_descriptor(sc->sc_ue.ue_udev);
295 hwrev = UGETW(dd->bcdDevice);
298 * First, check if we even need to load the firmware.
299 * If the device was still attached when the system was
300 * rebooted, it may already have firmware loaded in it.
301 * If this is the case, we don't need to do it again.
302 * And in fact, if we try to load it again, we'll hang,
303 * so we have to avoid this condition if we don't want
306 * We can test this quickly by checking the bcdRevision
307 * code. The NIC will return a different revision code if
308 * it's probed while the firmware is still loaded and
314 /* Load code segment */
315 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
316 0, kue_code_seg, sizeof(kue_code_seg));
318 device_printf(sc->sc_ue.ue_dev, "failed to load code segment: %s\n",
323 /* Load fixup segment */
324 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
325 0, kue_fix_seg, sizeof(kue_fix_seg));
327 device_printf(sc->sc_ue.ue_dev, "failed to load fixup segment: %s\n",
332 /* Send trigger command. */
333 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
334 0, kue_trig_seg, sizeof(kue_trig_seg));
336 device_printf(sc->sc_ue.ue_dev, "failed to load trigger segment: %s\n",
345 kue_setpromisc(struct usb_ether *ue)
347 struct kue_softc *sc = uether_getsc(ue);
348 struct ifnet *ifp = uether_getifp(ue);
350 KUE_LOCK_ASSERT(sc, MA_OWNED);
352 if (ifp->if_flags & IFF_PROMISC)
353 sc->sc_rxfilt |= KUE_RXFILT_PROMISC;
355 sc->sc_rxfilt &= ~KUE_RXFILT_PROMISC;
357 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
361 kue_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
363 struct kue_softc *sc = arg;
365 if (cnt >= KUE_MCFILTCNT(sc))
368 memcpy(KUE_MCFILT(sc, cnt), LLADDR(sdl), ETHER_ADDR_LEN);
374 kue_setmulti(struct usb_ether *ue)
376 struct kue_softc *sc = uether_getsc(ue);
377 struct ifnet *ifp = uether_getifp(ue);
380 KUE_LOCK_ASSERT(sc, MA_OWNED);
382 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
383 sc->sc_rxfilt |= KUE_RXFILT_ALLMULTI;
384 sc->sc_rxfilt &= ~KUE_RXFILT_MULTICAST;
385 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
389 sc->sc_rxfilt &= ~KUE_RXFILT_ALLMULTI;
391 i = if_foreach_llmaddr(ifp, kue_copy_maddr, sc);
393 if (i >= KUE_MCFILTCNT(sc))
394 sc->sc_rxfilt |= KUE_RXFILT_ALLMULTI;
396 sc->sc_rxfilt |= KUE_RXFILT_MULTICAST;
397 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
398 i, sc->sc_mcfilters, i * ETHER_ADDR_LEN);
401 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
405 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
406 * done after the firmware is loaded into the adapter in order to
407 * bring it into proper operation.
410 kue_reset(struct kue_softc *sc)
412 struct usb_config_descriptor *cd;
415 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
417 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
418 cd->bConfigurationValue);
420 DPRINTF("reset failed (ignored)\n");
422 /* wait a little while for the chip to get its brains in order */
423 uether_pause(&sc->sc_ue, hz / 100);
427 kue_attach_post(struct usb_ether *ue)
429 struct kue_softc *sc = uether_getsc(ue);
432 /* load the firmware into the NIC */
433 error = kue_load_fw(sc);
435 device_printf(sc->sc_ue.ue_dev, "could not load firmware\n");
436 /* ignore the error */
439 /* reset the adapter */
442 /* read ethernet descriptor */
443 kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
444 0, &sc->sc_desc, sizeof(sc->sc_desc));
446 /* copy in ethernet address */
447 memcpy(ue->ue_eaddr, sc->sc_desc.kue_macaddr, sizeof(ue->ue_eaddr));
451 * Probe for a KLSI chip.
454 kue_probe(device_t dev)
456 struct usb_attach_arg *uaa = device_get_ivars(dev);
458 if (uaa->usb_mode != USB_MODE_HOST)
460 if (uaa->info.bConfigIndex != KUE_CONFIG_IDX)
462 if (uaa->info.bIfaceIndex != KUE_IFACE_IDX)
465 return (usbd_lookup_id_by_uaa(kue_devs, sizeof(kue_devs), uaa));
469 * Attach the interface. Allocate softc structures, do
470 * setup and ethernet/BPF attach.
473 kue_attach(device_t dev)
475 struct usb_attach_arg *uaa = device_get_ivars(dev);
476 struct kue_softc *sc = device_get_softc(dev);
477 struct usb_ether *ue = &sc->sc_ue;
481 device_set_usb_desc(dev);
482 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
484 iface_index = KUE_IFACE_IDX;
485 error = usbd_transfer_setup(uaa->device, &iface_index,
486 sc->sc_xfer, kue_config, KUE_N_TRANSFER, sc, &sc->sc_mtx);
488 device_printf(dev, "allocating USB transfers failed\n");
492 sc->sc_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
494 if (sc->sc_mcfilters == NULL) {
495 device_printf(dev, "failed allocating USB memory\n");
501 ue->ue_udev = uaa->device;
502 ue->ue_mtx = &sc->sc_mtx;
503 ue->ue_methods = &kue_ue_methods;
505 error = uether_ifattach(ue);
507 device_printf(dev, "could not attach interface\n");
510 return (0); /* success */
514 return (ENXIO); /* failure */
518 kue_detach(device_t dev)
520 struct kue_softc *sc = device_get_softc(dev);
521 struct usb_ether *ue = &sc->sc_ue;
523 usbd_transfer_unsetup(sc->sc_xfer, KUE_N_TRANSFER);
525 mtx_destroy(&sc->sc_mtx);
526 free(sc->sc_mcfilters, M_USBDEV);
532 * A frame has been uploaded: pass the resulting mbuf chain up to
533 * the higher level protocols.
536 kue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
538 struct kue_softc *sc = usbd_xfer_softc(xfer);
539 struct usb_ether *ue = &sc->sc_ue;
540 struct ifnet *ifp = uether_getifp(ue);
541 struct usb_page_cache *pc;
546 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
548 switch (USB_GET_STATE(xfer)) {
549 case USB_ST_TRANSFERRED:
551 if (actlen <= (int)(2 + sizeof(struct ether_header))) {
552 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
555 pc = usbd_xfer_get_frame(xfer, 0);
556 usbd_copy_out(pc, 0, buf, 2);
558 len = buf[0] | (buf[1] << 8);
559 len = min(actlen, len);
561 uether_rxbuf(ue, pc, 2, len);
565 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
566 usbd_transfer_submit(xfer);
571 DPRINTF("bulk read error, %s\n",
574 if (error != USB_ERR_CANCELLED) {
575 /* try to clear stall first */
576 usbd_xfer_set_stall(xfer);
585 kue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
587 struct kue_softc *sc = usbd_xfer_softc(xfer);
588 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
589 struct usb_page_cache *pc;
595 switch (USB_GET_STATE(xfer)) {
596 case USB_ST_TRANSFERRED:
597 DPRINTFN(11, "transfer complete\n");
598 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
603 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
607 if (m->m_pkthdr.len > MCLBYTES)
608 m->m_pkthdr.len = MCLBYTES;
609 temp_len = (m->m_pkthdr.len + 2);
610 total_len = (temp_len + (64 - (temp_len % 64)));
612 /* the first two bytes are the frame length */
614 buf[0] = (uint8_t)(m->m_pkthdr.len);
615 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
617 pc = usbd_xfer_get_frame(xfer, 0);
618 usbd_copy_in(pc, 0, buf, 2);
619 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
621 usbd_frame_zero(pc, temp_len, total_len - temp_len);
622 usbd_xfer_set_frame_len(xfer, 0, total_len);
625 * if there's a BPF listener, bounce a copy
626 * of this frame to him:
632 usbd_transfer_submit(xfer);
637 DPRINTFN(11, "transfer error, %s\n",
640 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
642 if (error != USB_ERR_CANCELLED) {
643 /* try to clear stall first */
644 usbd_xfer_set_stall(xfer);
653 kue_start(struct usb_ether *ue)
655 struct kue_softc *sc = uether_getsc(ue);
658 * start the USB transfers, if not already started:
660 usbd_transfer_start(sc->sc_xfer[KUE_BULK_DT_RD]);
661 usbd_transfer_start(sc->sc_xfer[KUE_BULK_DT_WR]);
665 kue_init(struct usb_ether *ue)
667 struct kue_softc *sc = uether_getsc(ue);
668 struct ifnet *ifp = uether_getifp(ue);
670 KUE_LOCK_ASSERT(sc, MA_OWNED);
672 /* set MAC address */
673 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
674 0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
676 /* I'm not sure how to tune these. */
679 * Leave this one alone for now; setting it
680 * wrong causes lockups on some machines/controllers.
682 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
684 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
686 /* load the multicast filter */
689 usbd_xfer_set_stall(sc->sc_xfer[KUE_BULK_DT_WR]);
691 ifp->if_drv_flags |= IFF_DRV_RUNNING;
696 kue_stop(struct usb_ether *ue)
698 struct kue_softc *sc = uether_getsc(ue);
699 struct ifnet *ifp = uether_getifp(ue);
701 KUE_LOCK_ASSERT(sc, MA_OWNED);
703 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
706 * stop all the transfers, if not already stopped:
708 usbd_transfer_stop(sc->sc_xfer[KUE_BULK_DT_WR]);
709 usbd_transfer_stop(sc->sc_xfer[KUE_BULK_DT_RD]);