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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transfered using a single bulk
51 * transaction, which helps performance a great deal.
54 #include <sys/stdint.h>
55 #include <sys/stddef.h>
56 #include <sys/param.h>
57 #include <sys/queue.h>
58 #include <sys/types.h>
59 #include <sys/systm.h>
60 #include <sys/socket.h>
61 #include <sys/kernel.h>
63 #include <sys/module.h>
65 #include <sys/mutex.h>
66 #include <sys/condvar.h>
67 #include <sys/sysctl.h>
69 #include <sys/unistd.h>
70 #include <sys/callout.h>
71 #include <sys/malloc.h>
75 #include <net/if_var.h>
77 #include <dev/usb/usb.h>
78 #include <dev/usb/usbdi.h>
79 #include <dev/usb/usbdi_util.h>
82 #define USB_DEBUG_VAR cue_debug
83 #include <dev/usb/usb_debug.h>
84 #include <dev/usb/usb_process.h>
86 #include <dev/usb/net/usb_ethernet.h>
87 #include <dev/usb/net/if_cuereg.h>
90 * Various supported device vendors/products.
93 /* Belkin F5U111 adapter covered by NETMATE entry */
95 static const STRUCT_USB_HOST_ID cue_devs[] = {
96 #define CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
97 CUE_DEV(CATC, NETMATE),
98 CUE_DEV(CATC, NETMATE2),
99 CUE_DEV(SMARTBRIDGES, SMARTLINK),
105 static device_probe_t cue_probe;
106 static device_attach_t cue_attach;
107 static device_detach_t cue_detach;
109 static usb_callback_t cue_bulk_read_callback;
110 static usb_callback_t cue_bulk_write_callback;
112 static uether_fn_t cue_attach_post;
113 static uether_fn_t cue_init;
114 static uether_fn_t cue_stop;
115 static uether_fn_t cue_start;
116 static uether_fn_t cue_tick;
117 static uether_fn_t cue_setmulti;
118 static uether_fn_t cue_setpromisc;
120 static uint8_t cue_csr_read_1(struct cue_softc *, uint16_t);
121 static uint16_t cue_csr_read_2(struct cue_softc *, uint8_t);
122 static int cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
123 static int cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
124 static int cue_getmac(struct cue_softc *, void *);
125 static uint32_t cue_mchash(const uint8_t *);
126 static void cue_reset(struct cue_softc *);
129 static int cue_debug = 0;
131 static SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW, 0, "USB cue");
132 SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RWTUN, &cue_debug, 0,
136 static const struct usb_config cue_config[CUE_N_TRANSFER] = {
140 .endpoint = UE_ADDR_ANY,
141 .direction = UE_DIR_OUT,
142 .bufsize = (MCLBYTES + 2),
143 .flags = {.pipe_bof = 1,},
144 .callback = cue_bulk_write_callback,
145 .timeout = 10000, /* 10 seconds */
150 .endpoint = UE_ADDR_ANY,
151 .direction = UE_DIR_IN,
152 .bufsize = (MCLBYTES + 2),
153 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
154 .callback = cue_bulk_read_callback,
158 static device_method_t cue_methods[] = {
159 /* Device interface */
160 DEVMETHOD(device_probe, cue_probe),
161 DEVMETHOD(device_attach, cue_attach),
162 DEVMETHOD(device_detach, cue_detach),
167 static driver_t cue_driver = {
169 .methods = cue_methods,
170 .size = sizeof(struct cue_softc),
173 static devclass_t cue_devclass;
175 DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, NULL, 0);
176 MODULE_DEPEND(cue, uether, 1, 1, 1);
177 MODULE_DEPEND(cue, usb, 1, 1, 1);
178 MODULE_DEPEND(cue, ether, 1, 1, 1);
179 MODULE_VERSION(cue, 1);
181 static const struct usb_ether_methods cue_ue_methods = {
182 .ue_attach_post = cue_attach_post,
183 .ue_start = cue_start,
187 .ue_setmulti = cue_setmulti,
188 .ue_setpromisc = cue_setpromisc,
191 #define CUE_SETBIT(sc, reg, x) \
192 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
194 #define CUE_CLRBIT(sc, reg, x) \
195 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
198 cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
200 struct usb_device_request req;
203 req.bmRequestType = UT_READ_VENDOR_DEVICE;
204 req.bRequest = CUE_CMD_READREG;
205 USETW(req.wValue, 0);
206 USETW(req.wIndex, reg);
207 USETW(req.wLength, 1);
209 if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
210 /* ignore any errors */
216 cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
218 struct usb_device_request req;
221 req.bmRequestType = UT_READ_VENDOR_DEVICE;
222 req.bRequest = CUE_CMD_READREG;
223 USETW(req.wValue, 0);
224 USETW(req.wIndex, reg);
225 USETW(req.wLength, 2);
227 (void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
228 return (le16toh(val));
232 cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
234 struct usb_device_request req;
236 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
237 req.bRequest = CUE_CMD_WRITEREG;
238 USETW(req.wValue, val);
239 USETW(req.wIndex, reg);
240 USETW(req.wLength, 0);
242 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
246 cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
248 struct usb_device_request req;
250 if (cmd == CUE_CMD_READSRAM)
251 req.bmRequestType = UT_READ_VENDOR_DEVICE;
253 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
255 USETW(req.wValue, 0);
256 USETW(req.wIndex, addr);
257 USETW(req.wLength, len);
259 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
263 cue_getmac(struct cue_softc *sc, void *buf)
265 struct usb_device_request req;
267 req.bmRequestType = UT_READ_VENDOR_DEVICE;
268 req.bRequest = CUE_CMD_GET_MACADDR;
269 USETW(req.wValue, 0);
270 USETW(req.wIndex, 0);
271 USETW(req.wLength, ETHER_ADDR_LEN);
273 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
279 cue_mchash(const uint8_t *addr)
283 /* Compute CRC for the address value. */
284 crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
286 return (crc & ((1 << CUE_BITS) - 1));
290 cue_setpromisc(struct usb_ether *ue)
292 struct cue_softc *sc = uether_getsc(ue);
293 struct ifnet *ifp = uether_getifp(ue);
295 CUE_LOCK_ASSERT(sc, MA_OWNED);
297 /* if we want promiscuous mode, set the allframes bit */
298 if (ifp->if_flags & IFF_PROMISC)
299 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
301 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
303 /* write multicast hash-bits */
308 cue_setmulti(struct usb_ether *ue)
310 struct cue_softc *sc = uether_getsc(ue);
311 struct ifnet *ifp = uether_getifp(ue);
312 struct ifmultiaddr *ifma;
314 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
316 CUE_LOCK_ASSERT(sc, MA_OWNED);
318 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
319 for (i = 0; i < 8; i++)
321 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
326 /* now program new ones */
328 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
330 if (ifma->ifma_addr->sa_family != AF_LINK)
332 h = cue_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
333 hashtbl[h >> 3] |= 1 << (h & 0x7);
335 if_maddr_runlock(ifp);
338 * Also include the broadcast address in the filter
339 * so we can receive broadcast frames.
341 if (ifp->if_flags & IFF_BROADCAST) {
342 h = cue_mchash(ifp->if_broadcastaddr);
343 hashtbl[h >> 3] |= 1 << (h & 0x7);
346 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
350 cue_reset(struct cue_softc *sc)
352 struct usb_device_request req;
354 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
355 req.bRequest = CUE_CMD_RESET;
356 USETW(req.wValue, 0);
357 USETW(req.wIndex, 0);
358 USETW(req.wLength, 0);
360 if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
361 /* ignore any errors */
365 * wait a little while for the chip to get its brains in order:
367 uether_pause(&sc->sc_ue, hz / 100);
371 cue_attach_post(struct usb_ether *ue)
373 struct cue_softc *sc = uether_getsc(ue);
375 cue_getmac(sc, ue->ue_eaddr);
379 cue_probe(device_t dev)
381 struct usb_attach_arg *uaa = device_get_ivars(dev);
383 if (uaa->usb_mode != USB_MODE_HOST)
385 if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
387 if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
390 return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
394 * Attach the interface. Allocate softc structures, do ifmedia
395 * setup and ethernet/BPF attach.
398 cue_attach(device_t dev)
400 struct usb_attach_arg *uaa = device_get_ivars(dev);
401 struct cue_softc *sc = device_get_softc(dev);
402 struct usb_ether *ue = &sc->sc_ue;
406 device_set_usb_desc(dev);
407 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
409 iface_index = CUE_IFACE_IDX;
410 error = usbd_transfer_setup(uaa->device, &iface_index,
411 sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
413 device_printf(dev, "allocating USB transfers failed\n");
419 ue->ue_udev = uaa->device;
420 ue->ue_mtx = &sc->sc_mtx;
421 ue->ue_methods = &cue_ue_methods;
423 error = uether_ifattach(ue);
425 device_printf(dev, "could not attach interface\n");
428 return (0); /* success */
432 return (ENXIO); /* failure */
436 cue_detach(device_t dev)
438 struct cue_softc *sc = device_get_softc(dev);
439 struct usb_ether *ue = &sc->sc_ue;
441 usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
443 mtx_destroy(&sc->sc_mtx);
449 cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
451 struct cue_softc *sc = usbd_xfer_softc(xfer);
452 struct usb_ether *ue = &sc->sc_ue;
453 struct ifnet *ifp = uether_getifp(ue);
454 struct usb_page_cache *pc;
459 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
461 switch (USB_GET_STATE(xfer)) {
462 case USB_ST_TRANSFERRED:
464 if (actlen <= (int)(2 + sizeof(struct ether_header))) {
465 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
468 pc = usbd_xfer_get_frame(xfer, 0);
469 usbd_copy_out(pc, 0, buf, 2);
471 len = buf[0] | (buf[1] << 8);
472 len = min(actlen, len);
474 uether_rxbuf(ue, pc, 2, len);
478 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
479 usbd_transfer_submit(xfer);
484 DPRINTF("bulk read error, %s\n",
487 if (error != USB_ERR_CANCELLED) {
488 /* try to clear stall first */
489 usbd_xfer_set_stall(xfer);
498 cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
500 struct cue_softc *sc = usbd_xfer_softc(xfer);
501 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
502 struct usb_page_cache *pc;
506 switch (USB_GET_STATE(xfer)) {
507 case USB_ST_TRANSFERRED:
508 DPRINTFN(11, "transfer complete\n");
509 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
514 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
518 if (m->m_pkthdr.len > MCLBYTES)
519 m->m_pkthdr.len = MCLBYTES;
520 usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
522 /* the first two bytes are the frame length */
524 buf[0] = (uint8_t)(m->m_pkthdr.len);
525 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
527 pc = usbd_xfer_get_frame(xfer, 0);
528 usbd_copy_in(pc, 0, buf, 2);
529 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
532 * If there's a BPF listener, bounce a copy of this frame
539 usbd_transfer_submit(xfer);
544 DPRINTFN(11, "transfer error, %s\n",
547 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
549 if (error != USB_ERR_CANCELLED) {
550 /* try to clear stall first */
551 usbd_xfer_set_stall(xfer);
559 cue_tick(struct usb_ether *ue)
561 struct cue_softc *sc = uether_getsc(ue);
562 struct ifnet *ifp = uether_getifp(ue);
564 CUE_LOCK_ASSERT(sc, MA_OWNED);
566 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_SINGLECOLL));
567 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_MULTICOLL));
568 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_EXCESSCOLL));
570 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
571 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
575 cue_start(struct usb_ether *ue)
577 struct cue_softc *sc = uether_getsc(ue);
580 * start the USB transfers, if not already started:
582 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
583 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
587 cue_init(struct usb_ether *ue)
589 struct cue_softc *sc = uether_getsc(ue);
590 struct ifnet *ifp = uether_getifp(ue);
593 CUE_LOCK_ASSERT(sc, MA_OWNED);
596 * Cancel pending I/O and free all RX/TX buffers.
602 /* Set MAC address */
603 for (i = 0; i < ETHER_ADDR_LEN; i++)
604 cue_csr_write_1(sc, CUE_PAR0 - i, IF_LLADDR(ifp)[i]);
606 /* Enable RX logic. */
607 cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
609 /* Load the multicast filter */
613 * Set the number of RX and TX buffers that we want
614 * to reserve inside the ASIC.
616 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
617 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
619 /* Set advanced operation modes. */
620 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
621 CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
623 /* Program the LED operation. */
624 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
626 usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
628 ifp->if_drv_flags |= IFF_DRV_RUNNING;
633 * Stop the adapter and free any mbufs allocated to the
637 cue_stop(struct usb_ether *ue)
639 struct cue_softc *sc = uether_getsc(ue);
640 struct ifnet *ifp = uether_getifp(ue);
642 CUE_LOCK_ASSERT(sc, MA_OWNED);
644 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
647 * stop all the transfers, if not already stopped:
649 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
650 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
652 cue_csr_write_1(sc, CUE_ETHCTL, 0);