#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <net/if.h>
#include <net/if_var.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include "usbdevs.h"
#define USB_DEBUG_VAR cue_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/net/usb_ethernet.h>
#include <dev/usb/net/if_cuereg.h>
static const STRUCT_USB_HOST_ID cue_devs[] = {
#define CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
CUE_DEV(CATC, NETMATE),
CUE_DEV(CATC, NETMATE2),
CUE_DEV(SMARTBRIDGES, SMARTLINK),
#undef CUE_DEV
};
static device_probe_t cue_probe;
static device_attach_t cue_attach;
static device_detach_t cue_detach;
static usb_callback_t cue_bulk_read_callback;
static usb_callback_t cue_bulk_write_callback;
static uether_fn_t cue_attach_post;
static uether_fn_t cue_init;
static uether_fn_t cue_stop;
static uether_fn_t cue_start;
static uether_fn_t cue_tick;
static uether_fn_t cue_setmulti;
static uether_fn_t cue_setpromisc;
static uint8_t cue_csr_read_1(struct cue_softc *, uint16_t);
static uint16_t cue_csr_read_2(struct cue_softc *, uint8_t);
static int cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
static int cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
static int cue_getmac(struct cue_softc *, void *);
static uint32_t cue_mchash(const uint8_t *);
static void cue_reset(struct cue_softc *);
#ifdef USB_DEBUG
static int cue_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"USB cue");
SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RWTUN, &cue_debug, 0,
"Debug level");
#endif
static const struct usb_config cue_config[CUE_N_TRANSFER] = {
[CUE_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = (MCLBYTES + 2),
.flags = {.pipe_bof = 1,},
.callback = cue_bulk_write_callback,
.timeout = 10000,
},
[CUE_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = (MCLBYTES + 2),
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = cue_bulk_read_callback,
},
};
static device_method_t cue_methods[] = {
DEVMETHOD(device_probe, cue_probe),
DEVMETHOD(device_attach, cue_attach),
DEVMETHOD(device_detach, cue_detach),
DEVMETHOD_END
};
static driver_t cue_driver = {
.name = "cue",
.methods = cue_methods,
.size = sizeof(struct cue_softc),
};
DRIVER_MODULE(cue, uhub, cue_driver, NULL, NULL);
MODULE_DEPEND(cue, uether, 1, 1, 1);
MODULE_DEPEND(cue, usb, 1, 1, 1);
MODULE_DEPEND(cue, ether, 1, 1, 1);
MODULE_VERSION(cue, 1);
USB_PNP_HOST_INFO(cue_devs);
static const struct usb_ether_methods cue_ue_methods = {
.ue_attach_post = cue_attach_post,
.ue_start = cue_start,
.ue_init = cue_init,
.ue_stop = cue_stop,
.ue_tick = cue_tick,
.ue_setmulti = cue_setmulti,
.ue_setpromisc = cue_setpromisc,
};
#define CUE_SETBIT(sc, reg, x) \
cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
#define CUE_CLRBIT(sc, reg, x) \
cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
static uint8_t
cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
{
struct usb_device_request req;
uint8_t val;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = CUE_CMD_READREG;
USETW(req.wValue, 0);
USETW(req.wIndex, reg);
USETW(req.wLength, 1);
if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
}
return (val);
}
static uint16_t
cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
{
struct usb_device_request req;
uint16_t val;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = CUE_CMD_READREG;
USETW(req.wValue, 0);
USETW(req.wIndex, reg);
USETW(req.wLength, 2);
(void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
return (le16toh(val));
}
static int
cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
{
struct usb_device_request req;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = CUE_CMD_WRITEREG;
USETW(req.wValue, val);
USETW(req.wIndex, reg);
USETW(req.wLength, 0);
return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
}
static int
cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
{
struct usb_device_request req;
if (cmd == CUE_CMD_READSRAM)
req.bmRequestType = UT_READ_VENDOR_DEVICE;
else
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = cmd;
USETW(req.wValue, 0);
USETW(req.wIndex, addr);
USETW(req.wLength, len);
return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
}
static int
cue_getmac(struct cue_softc *sc, void *buf)
{
struct usb_device_request req;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = CUE_CMD_GET_MACADDR;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, ETHER_ADDR_LEN);
return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
}
#define CUE_BITS 9
static uint32_t
cue_mchash(const uint8_t *addr)
{
uint32_t crc;
crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
return (crc & ((1 << CUE_BITS) - 1));
}
static void
cue_setpromisc(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
if_t ifp = uether_getifp(ue);
CUE_LOCK_ASSERT(sc, MA_OWNED);
if (if_getflags(ifp) & IFF_PROMISC)
CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
else
CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
cue_setmulti(ue);
}
static u_int
cue_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
{
uint8_t *hashtbl = arg;
uint32_t h;
h = cue_mchash(LLADDR(sdl));
hashtbl[h >> 3] |= 1 << (h & 0x7);
return (1);
}
static void
cue_setmulti(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
if_t ifp = uether_getifp(ue);
uint32_t h, i;
uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
CUE_LOCK_ASSERT(sc, MA_OWNED);
if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
for (i = 0; i < 8; i++)
hashtbl[i] = 0xff;
cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
&hashtbl, 8);
return;
}
if_foreach_llmaddr(ifp, cue_hash_maddr, hashtbl);
if (if_getflags(ifp) & IFF_BROADCAST) {
h = cue_mchash(if_getbroadcastaddr(ifp));
hashtbl[h >> 3] |= 1 << (h & 0x7);
}
cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
}
static void
cue_reset(struct cue_softc *sc)
{
struct usb_device_request req;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = CUE_CMD_RESET;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, 0);
if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
}
uether_pause(&sc->sc_ue, hz / 100);
}
static void
cue_attach_post(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
cue_getmac(sc, ue->ue_eaddr);
}
static int
cue_probe(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
if (uaa->usb_mode != USB_MODE_HOST)
return (ENXIO);
if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
return (ENXIO);
if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
return (ENXIO);
return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
}
static int
cue_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct cue_softc *sc = device_get_softc(dev);
struct usb_ether *ue = &sc->sc_ue;
uint8_t iface_index;
int error;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
iface_index = CUE_IFACE_IDX;
error = usbd_transfer_setup(uaa->device, &iface_index,
sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
device_printf(dev, "allocating USB transfers failed\n");
goto detach;
}
ue->ue_sc = sc;
ue->ue_dev = dev;
ue->ue_udev = uaa->device;
ue->ue_mtx = &sc->sc_mtx;
ue->ue_methods = &cue_ue_methods;
error = uether_ifattach(ue);
if (error) {
device_printf(dev, "could not attach interface\n");
goto detach;
}
return (0);
detach:
cue_detach(dev);
return (ENXIO);
}
static int
cue_detach(device_t dev)
{
struct cue_softc *sc = device_get_softc(dev);
struct usb_ether *ue = &sc->sc_ue;
usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
uether_ifdetach(ue);
mtx_destroy(&sc->sc_mtx);
return (0);
}
static void
cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct cue_softc *sc = usbd_xfer_softc(xfer);
struct usb_ether *ue = &sc->sc_ue;
if_t ifp = uether_getifp(ue);
struct usb_page_cache *pc;
uint8_t buf[2];
int len;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen <= (int)(2 + sizeof(struct ether_header))) {
if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
goto tr_setup;
}
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, 2);
actlen -= 2;
len = buf[0] | (buf[1] << 8);
len = min(actlen, len);
uether_rxbuf(ue, pc, 2, len);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
uether_rxflush(ue);
return;
default:
DPRINTF("bulk read error, %s\n",
usbd_errstr(error));
if (error != USB_ERR_CANCELLED) {
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct cue_softc *sc = usbd_xfer_softc(xfer);
if_t ifp = uether_getifp(&sc->sc_ue);
struct usb_page_cache *pc;
struct mbuf *m;
uint8_t buf[2];
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
DPRINTFN(11, "transfer complete\n");
if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
case USB_ST_SETUP:
tr_setup:
m = if_dequeue(ifp);
if (m == NULL)
return;
if (m->m_pkthdr.len > MCLBYTES)
m->m_pkthdr.len = MCLBYTES;
usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
buf[0] = (uint8_t)(m->m_pkthdr.len);
buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_in(pc, 0, buf, 2);
usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
BPF_MTAP(ifp, m);
m_freem(m);
usbd_transfer_submit(xfer);
return;
default:
DPRINTFN(11, "transfer error, %s\n",
usbd_errstr(error));
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
if (error != USB_ERR_CANCELLED) {
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
cue_tick(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
if_t ifp = uether_getifp(ue);
CUE_LOCK_ASSERT(sc, MA_OWNED);
if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_SINGLECOLL));
if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_MULTICOLL));
if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_EXCESSCOLL));
if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
}
static void
cue_start(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
}
static void
cue_init(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
if_t ifp = uether_getifp(ue);
int i;
CUE_LOCK_ASSERT(sc, MA_OWNED);
cue_stop(ue);
#if 0
cue_reset(sc);
#endif
for (i = 0; i < ETHER_ADDR_LEN; i++)
cue_csr_write_1(sc, CUE_PAR0 - i, if_getlladdr(ifp)[i]);
cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
cue_setpromisc(ue);
cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
CUE_AOP_EMBED_RXLEN | 0x01);
cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
cue_start(ue);
}
static void
cue_stop(struct usb_ether *ue)
{
struct cue_softc *sc = uether_getsc(ue);
if_t ifp = uether_getifp(ue);
CUE_LOCK_ASSERT(sc, MA_OWNED);
if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
cue_csr_write_1(sc, CUE_ETHCTL, 0);
cue_reset(sc);
}