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 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
40 * adapters and others.
42 * Written by Bill Paul <wpaul@ee.columbia.edu>
43 * Electrical Engineering Department
44 * Columbia University, New York City
48 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
49 * RX filter uses a 512-bit multicast hash table, single perfect entry
50 * for the station address, and promiscuous mode. Unlike the ADMtek
51 * and KLSI chips, the CATC ASIC supports read and write combining
52 * mode where multiple packets can be transferred using a single bulk
53 * transaction, which helps performance a great deal.
56 #include <sys/stdint.h>
57 #include <sys/stddef.h>
58 #include <sys/param.h>
59 #include <sys/queue.h>
60 #include <sys/types.h>
61 #include <sys/systm.h>
62 #include <sys/socket.h>
63 #include <sys/kernel.h>
65 #include <sys/module.h>
67 #include <sys/mutex.h>
68 #include <sys/condvar.h>
69 #include <sys/sysctl.h>
71 #include <sys/unistd.h>
72 #include <sys/callout.h>
73 #include <sys/malloc.h>
77 #include <net/if_var.h>
79 #include <dev/usb/usb.h>
80 #include <dev/usb/usbdi.h>
81 #include <dev/usb/usbdi_util.h>
84 #define USB_DEBUG_VAR cue_debug
85 #include <dev/usb/usb_debug.h>
86 #include <dev/usb/usb_process.h>
88 #include <dev/usb/net/usb_ethernet.h>
89 #include <dev/usb/net/if_cuereg.h>
92 * Various supported device vendors/products.
95 /* Belkin F5U111 adapter covered by NETMATE entry */
97 static const STRUCT_USB_HOST_ID cue_devs[] = {
98 #define CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
99 CUE_DEV(CATC, NETMATE),
100 CUE_DEV(CATC, NETMATE2),
101 CUE_DEV(SMARTBRIDGES, SMARTLINK),
107 static device_probe_t cue_probe;
108 static device_attach_t cue_attach;
109 static device_detach_t cue_detach;
111 static usb_callback_t cue_bulk_read_callback;
112 static usb_callback_t cue_bulk_write_callback;
114 static uether_fn_t cue_attach_post;
115 static uether_fn_t cue_init;
116 static uether_fn_t cue_stop;
117 static uether_fn_t cue_start;
118 static uether_fn_t cue_tick;
119 static uether_fn_t cue_setmulti;
120 static uether_fn_t cue_setpromisc;
122 static uint8_t cue_csr_read_1(struct cue_softc *, uint16_t);
123 static uint16_t cue_csr_read_2(struct cue_softc *, uint8_t);
124 static int cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
125 static int cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
126 static int cue_getmac(struct cue_softc *, void *);
127 static uint32_t cue_mchash(const uint8_t *);
128 static void cue_reset(struct cue_softc *);
131 static int cue_debug = 0;
133 static SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
135 SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RWTUN, &cue_debug, 0,
139 static const struct usb_config cue_config[CUE_N_TRANSFER] = {
143 .endpoint = UE_ADDR_ANY,
144 .direction = UE_DIR_OUT,
145 .bufsize = (MCLBYTES + 2),
146 .flags = {.pipe_bof = 1,},
147 .callback = cue_bulk_write_callback,
148 .timeout = 10000, /* 10 seconds */
153 .endpoint = UE_ADDR_ANY,
154 .direction = UE_DIR_IN,
155 .bufsize = (MCLBYTES + 2),
156 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
157 .callback = cue_bulk_read_callback,
161 static device_method_t cue_methods[] = {
162 /* Device interface */
163 DEVMETHOD(device_probe, cue_probe),
164 DEVMETHOD(device_attach, cue_attach),
165 DEVMETHOD(device_detach, cue_detach),
170 static driver_t cue_driver = {
172 .methods = cue_methods,
173 .size = sizeof(struct cue_softc),
176 static devclass_t cue_devclass;
178 DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, NULL, 0);
179 MODULE_DEPEND(cue, uether, 1, 1, 1);
180 MODULE_DEPEND(cue, usb, 1, 1, 1);
181 MODULE_DEPEND(cue, ether, 1, 1, 1);
182 MODULE_VERSION(cue, 1);
183 USB_PNP_HOST_INFO(cue_devs);
185 static const struct usb_ether_methods cue_ue_methods = {
186 .ue_attach_post = cue_attach_post,
187 .ue_start = cue_start,
191 .ue_setmulti = cue_setmulti,
192 .ue_setpromisc = cue_setpromisc,
195 #define CUE_SETBIT(sc, reg, x) \
196 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
198 #define CUE_CLRBIT(sc, reg, x) \
199 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
202 cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
204 struct usb_device_request req;
207 req.bmRequestType = UT_READ_VENDOR_DEVICE;
208 req.bRequest = CUE_CMD_READREG;
209 USETW(req.wValue, 0);
210 USETW(req.wIndex, reg);
211 USETW(req.wLength, 1);
213 if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
214 /* ignore any errors */
220 cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
222 struct usb_device_request req;
225 req.bmRequestType = UT_READ_VENDOR_DEVICE;
226 req.bRequest = CUE_CMD_READREG;
227 USETW(req.wValue, 0);
228 USETW(req.wIndex, reg);
229 USETW(req.wLength, 2);
231 (void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
232 return (le16toh(val));
236 cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
238 struct usb_device_request req;
240 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
241 req.bRequest = CUE_CMD_WRITEREG;
242 USETW(req.wValue, val);
243 USETW(req.wIndex, reg);
244 USETW(req.wLength, 0);
246 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
250 cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
252 struct usb_device_request req;
254 if (cmd == CUE_CMD_READSRAM)
255 req.bmRequestType = UT_READ_VENDOR_DEVICE;
257 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
259 USETW(req.wValue, 0);
260 USETW(req.wIndex, addr);
261 USETW(req.wLength, len);
263 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
267 cue_getmac(struct cue_softc *sc, void *buf)
269 struct usb_device_request req;
271 req.bmRequestType = UT_READ_VENDOR_DEVICE;
272 req.bRequest = CUE_CMD_GET_MACADDR;
273 USETW(req.wValue, 0);
274 USETW(req.wIndex, 0);
275 USETW(req.wLength, ETHER_ADDR_LEN);
277 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
283 cue_mchash(const uint8_t *addr)
287 /* Compute CRC for the address value. */
288 crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
290 return (crc & ((1 << CUE_BITS) - 1));
294 cue_setpromisc(struct usb_ether *ue)
296 struct cue_softc *sc = uether_getsc(ue);
297 struct ifnet *ifp = uether_getifp(ue);
299 CUE_LOCK_ASSERT(sc, MA_OWNED);
301 /* if we want promiscuous mode, set the allframes bit */
302 if (ifp->if_flags & IFF_PROMISC)
303 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
305 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
307 /* write multicast hash-bits */
312 cue_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
314 uint8_t *hashtbl = arg;
317 h = cue_mchash(LLADDR(sdl));
318 hashtbl[h >> 3] |= 1 << (h & 0x7);
324 cue_setmulti(struct usb_ether *ue)
326 struct cue_softc *sc = uether_getsc(ue);
327 struct ifnet *ifp = uether_getifp(ue);
329 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
331 CUE_LOCK_ASSERT(sc, MA_OWNED);
333 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
334 for (i = 0; i < 8; i++)
336 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
341 /* now program new ones */
342 if_foreach_llmaddr(ifp, cue_hash_maddr, hashtbl);
345 * Also include the broadcast address in the filter
346 * so we can receive broadcast frames.
348 if (ifp->if_flags & IFF_BROADCAST) {
349 h = cue_mchash(ifp->if_broadcastaddr);
350 hashtbl[h >> 3] |= 1 << (h & 0x7);
353 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
357 cue_reset(struct cue_softc *sc)
359 struct usb_device_request req;
361 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
362 req.bRequest = CUE_CMD_RESET;
363 USETW(req.wValue, 0);
364 USETW(req.wIndex, 0);
365 USETW(req.wLength, 0);
367 if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
368 /* ignore any errors */
372 * wait a little while for the chip to get its brains in order:
374 uether_pause(&sc->sc_ue, hz / 100);
378 cue_attach_post(struct usb_ether *ue)
380 struct cue_softc *sc = uether_getsc(ue);
382 cue_getmac(sc, ue->ue_eaddr);
386 cue_probe(device_t dev)
388 struct usb_attach_arg *uaa = device_get_ivars(dev);
390 if (uaa->usb_mode != USB_MODE_HOST)
392 if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
394 if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
397 return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
401 * Attach the interface. Allocate softc structures, do ifmedia
402 * setup and ethernet/BPF attach.
405 cue_attach(device_t dev)
407 struct usb_attach_arg *uaa = device_get_ivars(dev);
408 struct cue_softc *sc = device_get_softc(dev);
409 struct usb_ether *ue = &sc->sc_ue;
413 device_set_usb_desc(dev);
414 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
416 iface_index = CUE_IFACE_IDX;
417 error = usbd_transfer_setup(uaa->device, &iface_index,
418 sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
420 device_printf(dev, "allocating USB transfers failed\n");
426 ue->ue_udev = uaa->device;
427 ue->ue_mtx = &sc->sc_mtx;
428 ue->ue_methods = &cue_ue_methods;
430 error = uether_ifattach(ue);
432 device_printf(dev, "could not attach interface\n");
435 return (0); /* success */
439 return (ENXIO); /* failure */
443 cue_detach(device_t dev)
445 struct cue_softc *sc = device_get_softc(dev);
446 struct usb_ether *ue = &sc->sc_ue;
448 usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
450 mtx_destroy(&sc->sc_mtx);
456 cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
458 struct cue_softc *sc = usbd_xfer_softc(xfer);
459 struct usb_ether *ue = &sc->sc_ue;
460 struct ifnet *ifp = uether_getifp(ue);
461 struct usb_page_cache *pc;
466 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
468 switch (USB_GET_STATE(xfer)) {
469 case USB_ST_TRANSFERRED:
471 if (actlen <= (int)(2 + sizeof(struct ether_header))) {
472 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
475 pc = usbd_xfer_get_frame(xfer, 0);
476 usbd_copy_out(pc, 0, buf, 2);
478 len = buf[0] | (buf[1] << 8);
479 len = min(actlen, len);
481 uether_rxbuf(ue, pc, 2, len);
485 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
486 usbd_transfer_submit(xfer);
491 DPRINTF("bulk read error, %s\n",
494 if (error != USB_ERR_CANCELLED) {
495 /* try to clear stall first */
496 usbd_xfer_set_stall(xfer);
505 cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
507 struct cue_softc *sc = usbd_xfer_softc(xfer);
508 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
509 struct usb_page_cache *pc;
513 switch (USB_GET_STATE(xfer)) {
514 case USB_ST_TRANSFERRED:
515 DPRINTFN(11, "transfer complete\n");
516 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
521 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
525 if (m->m_pkthdr.len > MCLBYTES)
526 m->m_pkthdr.len = MCLBYTES;
527 usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
529 /* the first two bytes are the frame length */
531 buf[0] = (uint8_t)(m->m_pkthdr.len);
532 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
534 pc = usbd_xfer_get_frame(xfer, 0);
535 usbd_copy_in(pc, 0, buf, 2);
536 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
539 * If there's a BPF listener, bounce a copy of this frame
546 usbd_transfer_submit(xfer);
551 DPRINTFN(11, "transfer error, %s\n",
554 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
556 if (error != USB_ERR_CANCELLED) {
557 /* try to clear stall first */
558 usbd_xfer_set_stall(xfer);
566 cue_tick(struct usb_ether *ue)
568 struct cue_softc *sc = uether_getsc(ue);
569 struct ifnet *ifp = uether_getifp(ue);
571 CUE_LOCK_ASSERT(sc, MA_OWNED);
573 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_SINGLECOLL));
574 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_MULTICOLL));
575 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_EXCESSCOLL));
577 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
578 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
582 cue_start(struct usb_ether *ue)
584 struct cue_softc *sc = uether_getsc(ue);
587 * start the USB transfers, if not already started:
589 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
590 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
594 cue_init(struct usb_ether *ue)
596 struct cue_softc *sc = uether_getsc(ue);
597 struct ifnet *ifp = uether_getifp(ue);
600 CUE_LOCK_ASSERT(sc, MA_OWNED);
603 * Cancel pending I/O and free all RX/TX buffers.
609 /* Set MAC address */
610 for (i = 0; i < ETHER_ADDR_LEN; i++)
611 cue_csr_write_1(sc, CUE_PAR0 - i, IF_LLADDR(ifp)[i]);
613 /* Enable RX logic. */
614 cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
616 /* Load the multicast filter */
620 * Set the number of RX and TX buffers that we want
621 * to reserve inside the ASIC.
623 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
624 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
626 /* Set advanced operation modes. */
627 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
628 CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
630 /* Program the LED operation. */
631 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
633 usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
635 ifp->if_drv_flags |= IFF_DRV_RUNNING;
640 * Stop the adapter and free any mbufs allocated to the
644 cue_stop(struct usb_ether *ue)
646 struct cue_softc *sc = uether_getsc(ue);
647 struct ifnet *ifp = uether_getifp(ue);
649 CUE_LOCK_ASSERT(sc, MA_OWNED);
651 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
654 * stop all the transfers, if not already stopped:
656 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
657 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
659 cue_csr_write_1(sc, CUE_ETHCTL, 0);