Adding FREEBSD USB Serial Drivers

This commit is contained in:
Kevin Kirspel 2017-05-12 08:16:22 -04:00 committed by Sebastian Huber
parent 9d52d911ec
commit 286c391e39
23 changed files with 20104 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,472 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $OpenBSD: uark.c,v 1.1 2006/08/14 08:30:22 jsg Exp $ */
/*
* Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $FreeBSD$
*/
/*
* NOTE: all function names beginning like "uark_cfg_" can only
* be called from within the config thread function !
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbhid.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR usb_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#define UARK_BUF_SIZE 1024 /* bytes */
#define UARK_SET_DATA_BITS(x) ((x) - 5)
#define UARK_PARITY_NONE 0x00
#define UARK_PARITY_ODD 0x08
#define UARK_PARITY_EVEN 0x18
#define UARK_STOP_BITS_1 0x00
#define UARK_STOP_BITS_2 0x04
#define UARK_BAUD_REF 3000000
#define UARK_WRITE 0x40
#define UARK_READ 0xc0
#define UARK_REQUEST 0xfe
#define UARK_CONFIG_INDEX 0
#define UARK_IFACE_INDEX 0
enum {
UARK_BULK_DT_WR,
UARK_BULK_DT_RD,
UARK_N_TRANSFER,
};
struct uark_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UARK_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint8_t sc_msr;
uint8_t sc_lsr;
};
/* prototypes */
static device_probe_t uark_probe;
static device_attach_t uark_attach;
static device_detach_t uark_detach;
static void uark_free_softc(struct uark_softc *);
static usb_callback_t uark_bulk_write_callback;
static usb_callback_t uark_bulk_read_callback;
static void uark_free(struct ucom_softc *);
static void uark_start_read(struct ucom_softc *);
static void uark_stop_read(struct ucom_softc *);
static void uark_start_write(struct ucom_softc *);
static void uark_stop_write(struct ucom_softc *);
static int uark_pre_param(struct ucom_softc *, struct termios *);
static void uark_cfg_param(struct ucom_softc *, struct termios *);
static void uark_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void uark_cfg_set_break(struct ucom_softc *, uint8_t);
static void uark_cfg_write(struct uark_softc *, uint16_t, uint16_t);
static void uark_poll(struct ucom_softc *ucom);
static const struct usb_config
uark_xfer_config[UARK_N_TRANSFER] = {
[UARK_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UARK_BUF_SIZE,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &uark_bulk_write_callback,
},
[UARK_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UARK_BUF_SIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &uark_bulk_read_callback,
},
};
static const struct ucom_callback uark_callback = {
.ucom_cfg_get_status = &uark_cfg_get_status,
.ucom_cfg_set_break = &uark_cfg_set_break,
.ucom_cfg_param = &uark_cfg_param,
.ucom_pre_param = &uark_pre_param,
.ucom_start_read = &uark_start_read,
.ucom_stop_read = &uark_stop_read,
.ucom_start_write = &uark_start_write,
.ucom_stop_write = &uark_stop_write,
.ucom_poll = &uark_poll,
.ucom_free = &uark_free,
};
static device_method_t uark_methods[] = {
/* Device methods */
DEVMETHOD(device_probe, uark_probe),
DEVMETHOD(device_attach, uark_attach),
DEVMETHOD(device_detach, uark_detach),
DEVMETHOD_END
};
static devclass_t uark_devclass;
static driver_t uark_driver = {
.name = "uark",
.methods = uark_methods,
.size = sizeof(struct uark_softc),
};
static const STRUCT_USB_HOST_ID uark_devs[] = {
{USB_VPI(USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116, 0)},
};
DRIVER_MODULE(uark, uhub, uark_driver, uark_devclass, NULL, 0);
MODULE_DEPEND(uark, ucom, 1, 1, 1);
MODULE_DEPEND(uark, usb, 1, 1, 1);
MODULE_VERSION(uark, 1);
USB_PNP_HOST_INFO(uark_devs);
static int
uark_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 != 0) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UARK_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(uark_devs, sizeof(uark_devs), uaa));
}
static int
uark_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct uark_softc *sc = device_get_softc(dev);
int32_t error;
uint8_t iface_index;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "uark", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_udev = uaa->device;
iface_index = UARK_IFACE_INDEX;
error = usbd_transfer_setup
(uaa->device, &iface_index, sc->sc_xfer,
uark_xfer_config, UARK_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
device_printf(dev, "allocating control USB "
"transfers failed\n");
goto detach;
}
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&uark_callback, &sc->sc_mtx);
if (error) {
DPRINTF("ucom_attach failed\n");
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0); /* success */
detach:
uark_detach(dev);
return (ENXIO); /* failure */
}
static int
uark_detach(device_t dev)
{
struct uark_softc *sc = device_get_softc(dev);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UARK_N_TRANSFER);
device_claim_softc(dev);
uark_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(uark);
static void
uark_free_softc(struct uark_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
uark_free(struct ucom_softc *ucom)
{
uark_free_softc(ucom->sc_parent);
}
static void
uark_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uark_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
UARK_BUF_SIZE, &actlen)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uark_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uark_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uark_start_read(struct ucom_softc *ucom)
{
struct uark_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_RD]);
}
static void
uark_stop_read(struct ucom_softc *ucom)
{
struct uark_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_RD]);
}
static void
uark_start_write(struct ucom_softc *ucom)
{
struct uark_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_WR]);
}
static void
uark_stop_write(struct ucom_softc *ucom)
{
struct uark_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_WR]);
}
static int
uark_pre_param(struct ucom_softc *ucom, struct termios *t)
{
if ((t->c_ospeed < 300) || (t->c_ospeed > 115200))
return (EINVAL);
return (0);
}
static void
uark_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct uark_softc *sc = ucom->sc_parent;
uint32_t speed = t->c_ospeed;
uint16_t data;
/*
* NOTE: When reverse computing the baud rate from the "data" all
* allowed baud rates are within 3% of the initial baud rate.
*/
data = (UARK_BAUD_REF + (speed / 2)) / speed;
uark_cfg_write(sc, 3, 0x83);
uark_cfg_write(sc, 0, data & 0xFF);
uark_cfg_write(sc, 1, data >> 8);
uark_cfg_write(sc, 3, 0x03);
if (t->c_cflag & CSTOPB)
data = UARK_STOP_BITS_2;
else
data = UARK_STOP_BITS_1;
if (t->c_cflag & PARENB) {
if (t->c_cflag & PARODD)
data |= UARK_PARITY_ODD;
else
data |= UARK_PARITY_EVEN;
} else
data |= UARK_PARITY_NONE;
switch (t->c_cflag & CSIZE) {
case CS5:
data |= UARK_SET_DATA_BITS(5);
break;
case CS6:
data |= UARK_SET_DATA_BITS(6);
break;
case CS7:
data |= UARK_SET_DATA_BITS(7);
break;
default:
case CS8:
data |= UARK_SET_DATA_BITS(8);
break;
}
uark_cfg_write(sc, 3, 0x00);
uark_cfg_write(sc, 3, data);
}
static void
uark_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct uark_softc *sc = ucom->sc_parent;
/* XXX Note: sc_lsr is always zero */
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
uark_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct uark_softc *sc = ucom->sc_parent;
DPRINTF("onoff=%d\n", onoff);
uark_cfg_write(sc, 4, onoff ? 0x01 : 0x00);
}
static void
uark_cfg_write(struct uark_softc *sc, uint16_t index, uint16_t value)
{
struct usb_device_request req;
usb_error_t err;
req.bmRequestType = UARK_WRITE;
req.bRequest = UARK_REQUEST;
USETW(req.wValue, value);
USETW(req.wIndex, index);
USETW(req.wLength, 0);
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
if (err) {
DPRINTFN(0, "device request failed, err=%s "
"(ignored)\n", usbd_errstr(err));
}
}
static void
uark_poll(struct ucom_softc *ucom)
{
struct uark_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UARK_N_TRANSFER);
}

View File

@ -0,0 +1,700 @@
#include <machine/rtems-bsd-kernel-space.h>
/*-
* Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Ichiro FUKUHARA (ichiro@ichiro.org).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR ubsa_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int ubsa_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RWTUN,
&ubsa_debug, 0, "ubsa debug level");
#endif
#define UBSA_BSIZE 1024 /* bytes */
#define UBSA_CONFIG_INDEX 0
#define UBSA_IFACE_INDEX 0
#define UBSA_REG_BAUDRATE 0x00
#define UBSA_REG_STOP_BITS 0x01
#define UBSA_REG_DATA_BITS 0x02
#define UBSA_REG_PARITY 0x03
#define UBSA_REG_DTR 0x0A
#define UBSA_REG_RTS 0x0B
#define UBSA_REG_BREAK 0x0C
#define UBSA_REG_FLOW_CTRL 0x10
#define UBSA_PARITY_NONE 0x00
#define UBSA_PARITY_EVEN 0x01
#define UBSA_PARITY_ODD 0x02
#define UBSA_PARITY_MARK 0x03
#define UBSA_PARITY_SPACE 0x04
#define UBSA_FLOW_NONE 0x0000
#define UBSA_FLOW_OCTS 0x0001
#define UBSA_FLOW_ODSR 0x0002
#define UBSA_FLOW_IDSR 0x0004
#define UBSA_FLOW_IDTR 0x0008
#define UBSA_FLOW_IRTS 0x0010
#define UBSA_FLOW_ORTS 0x0020
#define UBSA_FLOW_UNKNOWN 0x0040
#define UBSA_FLOW_OXON 0x0080
#define UBSA_FLOW_IXON 0x0100
/* line status register */
#define UBSA_LSR_TSRE 0x40 /* Transmitter empty: byte sent */
#define UBSA_LSR_TXRDY 0x20 /* Transmitter buffer empty */
#define UBSA_LSR_BI 0x10 /* Break detected */
#define UBSA_LSR_FE 0x08 /* Framing error: bad stop bit */
#define UBSA_LSR_PE 0x04 /* Parity error */
#define UBSA_LSR_OE 0x02 /* Overrun, lost incoming byte */
#define UBSA_LSR_RXRDY 0x01 /* Byte ready in Receive Buffer */
#define UBSA_LSR_RCV_MASK 0x1f /* Mask for incoming data or error */
/* modem status register */
/* All deltas are from the last read of the MSR. */
#define UBSA_MSR_DCD 0x80 /* Current Data Carrier Detect */
#define UBSA_MSR_RI 0x40 /* Current Ring Indicator */
#define UBSA_MSR_DSR 0x20 /* Current Data Set Ready */
#define UBSA_MSR_CTS 0x10 /* Current Clear to Send */
#define UBSA_MSR_DDCD 0x08 /* DCD has changed state */
#define UBSA_MSR_TERI 0x04 /* RI has toggled low to high */
#define UBSA_MSR_DDSR 0x02 /* DSR has changed state */
#define UBSA_MSR_DCTS 0x01 /* CTS has changed state */
enum {
UBSA_BULK_DT_WR,
UBSA_BULK_DT_RD,
UBSA_INTR_DT_RD,
UBSA_N_TRANSFER,
};
struct ubsa_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint8_t sc_iface_no; /* interface number */
uint8_t sc_iface_index; /* interface index */
uint8_t sc_lsr; /* local status register */
uint8_t sc_msr; /* UBSA status register */
};
static device_probe_t ubsa_probe;
static device_attach_t ubsa_attach;
static device_detach_t ubsa_detach;
static void ubsa_free_softc(struct ubsa_softc *);
static usb_callback_t ubsa_write_callback;
static usb_callback_t ubsa_read_callback;
static usb_callback_t ubsa_intr_callback;
static void ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
static void ubsa_free(struct ucom_softc *);
static void ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
static void ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
static int ubsa_pre_param(struct ucom_softc *, struct termios *);
static void ubsa_cfg_param(struct ucom_softc *, struct termios *);
static void ubsa_start_read(struct ucom_softc *);
static void ubsa_stop_read(struct ucom_softc *);
static void ubsa_start_write(struct ucom_softc *);
static void ubsa_stop_write(struct ucom_softc *);
static void ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void ubsa_poll(struct ucom_softc *ucom);
static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
[UBSA_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UBSA_BSIZE, /* bytes */
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &ubsa_write_callback,
},
[UBSA_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UBSA_BSIZE, /* bytes */
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &ubsa_read_callback,
},
[UBSA_INTR_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &ubsa_intr_callback,
},
};
static const struct ucom_callback ubsa_callback = {
.ucom_cfg_get_status = &ubsa_cfg_get_status,
.ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
.ucom_cfg_set_rts = &ubsa_cfg_set_rts,
.ucom_cfg_set_break = &ubsa_cfg_set_break,
.ucom_cfg_param = &ubsa_cfg_param,
.ucom_pre_param = &ubsa_pre_param,
.ucom_start_read = &ubsa_start_read,
.ucom_stop_read = &ubsa_stop_read,
.ucom_start_write = &ubsa_start_write,
.ucom_stop_write = &ubsa_stop_write,
.ucom_poll = &ubsa_poll,
.ucom_free = &ubsa_free,
};
static const STRUCT_USB_HOST_ID ubsa_devs[] = {
/* AnyData ADU-500A */
{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
/* AnyData ADU-E100A/H */
{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
/* Axesstel MV100H */
{USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
/* BELKIN F5U103 */
{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
/* BELKIN F5U120 */
{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
/* GoHubs GO-COM232 */
{USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
/* GoHubs GO-COM232 */
{USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
/* Peracom */
{USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
};
static device_method_t ubsa_methods[] = {
DEVMETHOD(device_probe, ubsa_probe),
DEVMETHOD(device_attach, ubsa_attach),
DEVMETHOD(device_detach, ubsa_detach),
DEVMETHOD_END
};
static devclass_t ubsa_devclass;
static driver_t ubsa_driver = {
.name = "ubsa",
.methods = ubsa_methods,
.size = sizeof(struct ubsa_softc),
};
DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
MODULE_DEPEND(ubsa, usb, 1, 1, 1);
MODULE_VERSION(ubsa, 1);
USB_PNP_HOST_INFO(ubsa_devs);
static int
ubsa_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 != UBSA_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
}
static int
ubsa_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct ubsa_softc *sc = device_get_softc(dev);
int error;
DPRINTF("sc=%p\n", sc);
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_udev = uaa->device;
sc->sc_iface_no = uaa->info.bIfaceNum;
sc->sc_iface_index = UBSA_IFACE_INDEX;
error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
DPRINTF("could not allocate all pipes\n");
goto detach;
}
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&ubsa_callback, &sc->sc_mtx);
if (error) {
DPRINTF("ucom_attach failed\n");
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0);
detach:
ubsa_detach(dev);
return (ENXIO);
}
static int
ubsa_detach(device_t dev)
{
struct ubsa_softc *sc = device_get_softc(dev);
DPRINTF("sc=%p\n", sc);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
device_claim_softc(dev);
ubsa_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(ubsa);
static void
ubsa_free_softc(struct ubsa_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
ubsa_free(struct ucom_softc *ucom)
{
ubsa_free_softc(ucom->sc_parent);
}
static void
ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
{
struct usb_device_request req;
usb_error_t err;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = index;
USETW(req.wValue, value);
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 0);
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
if (err) {
DPRINTFN(0, "device request failed, err=%s "
"(ignored)\n", usbd_errstr(err));
}
}
static void
ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct ubsa_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
}
static void
ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct ubsa_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
}
static void
ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct ubsa_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
}
static int
ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
{
DPRINTF("sc = %p\n", ucom->sc_parent);
switch (t->c_ospeed) {
case B0:
case B300:
case B600:
case B1200:
case B2400:
case B4800:
case B9600:
case B19200:
case B38400:
case B57600:
case B115200:
case B230400:
break;
default:
return (EINVAL);
}
return (0);
}
static void
ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct ubsa_softc *sc = ucom->sc_parent;
uint16_t value = 0;
DPRINTF("sc = %p\n", sc);
switch (t->c_ospeed) {
case B0:
ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
ubsa_cfg_set_rts(&sc->sc_ucom, 0);
break;
case B300:
case B600:
case B1200:
case B2400:
case B4800:
case B9600:
case B19200:
case B38400:
case B57600:
case B115200:
case B230400:
value = B230400 / t->c_ospeed;
ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
break;
default:
return;
}
if (t->c_cflag & PARENB)
value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
else
value = UBSA_PARITY_NONE;
ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
switch (t->c_cflag & CSIZE) {
case CS5:
value = 0;
break;
case CS6:
value = 1;
break;
case CS7:
value = 2;
break;
default:
case CS8:
value = 3;
break;
}
ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
value = (t->c_cflag & CSTOPB) ? 1 : 0;
ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
value = 0;
if (t->c_cflag & CRTSCTS)
value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
if (t->c_iflag & (IXON | IXOFF))
value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
}
static void
ubsa_start_read(struct ucom_softc *ucom)
{
struct ubsa_softc *sc = ucom->sc_parent;
/* start interrupt endpoint */
usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
/* start read endpoint */
usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
}
static void
ubsa_stop_read(struct ucom_softc *ucom)
{
struct ubsa_softc *sc = ucom->sc_parent;
/* stop interrupt endpoint */
usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
/* stop read endpoint */
usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
}
static void
ubsa_start_write(struct ucom_softc *ucom)
{
struct ubsa_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
}
static void
ubsa_stop_write(struct ucom_softc *ucom)
{
struct ubsa_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
}
static void
ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct ubsa_softc *sc = ucom->sc_parent;
DPRINTF("\n");
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ubsa_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
UBSA_BSIZE, &actlen)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ubsa_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ubsa_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[4];
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen >= (int)sizeof(buf)) {
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, sizeof(buf));
/*
* MSR bits need translation from ns16550 to SER_* values.
* LSR bits are ns16550 in hardware and ucom.
*/
sc->sc_msr = 0;
if (buf[3] & UBSA_MSR_CTS)
sc->sc_msr |= SER_CTS;
if (buf[3] & UBSA_MSR_DCD)
sc->sc_msr |= SER_DCD;
if (buf[3] & UBSA_MSR_RI)
sc->sc_msr |= SER_RI;
if (buf[3] & UBSA_MSR_DSR)
sc->sc_msr |= SER_DSR;
sc->sc_lsr = buf[2];
DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
sc->sc_lsr, sc->sc_msr);
ucom_status_change(&sc->sc_ucom);
} else {
DPRINTF("ignoring short packet, %d bytes\n", actlen);
}
/* FALLTHROUGH */
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ubsa_poll(struct ucom_softc *ucom)
{
struct ubsa_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER);
}

View File

@ -0,0 +1,561 @@
#include <machine/rtems-bsd-kernel-space.h>
/*-
* Copyright (c) 2004 Bernd Walter <ticso@FreeBSD.org>
*
* $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
* $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
* $Author: ticso $
* $Rev: 1127 $
*/
/*-
* Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* BWCT serial adapter driver
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR ubser_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#define UBSER_UNIT_MAX 32
/* Vendor Interface Requests */
#define VENDOR_GET_NUMSER 0x01
#define VENDOR_SET_BREAK 0x02
#define VENDOR_CLEAR_BREAK 0x03
#ifdef USB_DEBUG
static int ubser_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RWTUN,
&ubser_debug, 0, "ubser debug level");
#endif
enum {
UBSER_BULK_DT_WR,
UBSER_BULK_DT_RD,
UBSER_N_TRANSFER,
};
struct ubser_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom[UBSER_UNIT_MAX];
struct usb_xfer *sc_xfer[UBSER_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint16_t sc_tx_size;
uint8_t sc_numser;
uint8_t sc_iface_no;
uint8_t sc_iface_index;
uint8_t sc_curr_tx_unit;
};
/* prototypes */
static device_probe_t ubser_probe;
static device_attach_t ubser_attach;
static device_detach_t ubser_detach;
static void ubser_free_softc(struct ubser_softc *);
static usb_callback_t ubser_write_callback;
static usb_callback_t ubser_read_callback;
static void ubser_free(struct ucom_softc *);
static int ubser_pre_param(struct ucom_softc *, struct termios *);
static void ubser_cfg_set_break(struct ucom_softc *, uint8_t);
static void ubser_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void ubser_start_read(struct ucom_softc *);
static void ubser_stop_read(struct ucom_softc *);
static void ubser_start_write(struct ucom_softc *);
static void ubser_stop_write(struct ucom_softc *);
static void ubser_poll(struct ucom_softc *ucom);
static const struct usb_config ubser_config[UBSER_N_TRANSFER] = {
[UBSER_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = 0, /* use wMaxPacketSize */
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &ubser_write_callback,
},
[UBSER_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = 0, /* use wMaxPacketSize */
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &ubser_read_callback,
},
};
static const struct ucom_callback ubser_callback = {
.ucom_cfg_set_break = &ubser_cfg_set_break,
.ucom_cfg_get_status = &ubser_cfg_get_status,
.ucom_pre_param = &ubser_pre_param,
.ucom_start_read = &ubser_start_read,
.ucom_stop_read = &ubser_stop_read,
.ucom_start_write = &ubser_start_write,
.ucom_stop_write = &ubser_stop_write,
.ucom_poll = &ubser_poll,
.ucom_free = &ubser_free,
};
static device_method_t ubser_methods[] = {
DEVMETHOD(device_probe, ubser_probe),
DEVMETHOD(device_attach, ubser_attach),
DEVMETHOD(device_detach, ubser_detach),
DEVMETHOD_END
};
static devclass_t ubser_devclass;
static driver_t ubser_driver = {
.name = "ubser",
.methods = ubser_methods,
.size = sizeof(struct ubser_softc),
};
DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
MODULE_DEPEND(ubser, ucom, 1, 1, 1);
MODULE_DEPEND(ubser, usb, 1, 1, 1);
MODULE_VERSION(ubser, 1);
static int
ubser_probe(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
if (uaa->usb_mode != USB_MODE_HOST) {
return (ENXIO);
}
/* check if this is a BWCT vendor specific ubser interface */
if ((strcmp(usb_get_manufacturer(uaa->device), "BWCT") == 0) &&
(uaa->info.bInterfaceClass == 0xff) &&
(uaa->info.bInterfaceSubClass == 0x00))
return (0);
return (ENXIO);
}
static int
ubser_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct ubser_softc *sc = device_get_softc(dev);
struct usb_device_request req;
uint8_t n;
int error;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_iface_no = uaa->info.bIfaceNum;
sc->sc_iface_index = uaa->info.bIfaceIndex;
sc->sc_udev = uaa->device;
/* get number of serials */
req.bmRequestType = UT_READ_VENDOR_INTERFACE;
req.bRequest = VENDOR_GET_NUMSER;
USETW(req.wValue, 0);
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 1);
error = usbd_do_request_flags(uaa->device, NULL,
&req, &sc->sc_numser,
0, NULL, USB_DEFAULT_TIMEOUT);
if (error || (sc->sc_numser == 0)) {
device_printf(dev, "failed to get number "
"of serial ports: %s\n",
usbd_errstr(error));
goto detach;
}
if (sc->sc_numser > UBSER_UNIT_MAX)
sc->sc_numser = UBSER_UNIT_MAX;
device_printf(dev, "found %i serials\n", sc->sc_numser);
error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
goto detach;
}
sc->sc_tx_size = usbd_xfer_max_len(sc->sc_xfer[UBSER_BULK_DT_WR]);
if (sc->sc_tx_size == 0) {
DPRINTFN(0, "invalid tx_size\n");
goto detach;
}
/* initialize port numbers */
for (n = 0; n < sc->sc_numser; n++) {
sc->sc_ucom[n].sc_portno = n;
}
error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
return (0); /* success */
detach:
ubser_detach(dev);
return (ENXIO); /* failure */
}
static int
ubser_detach(device_t dev)
{
struct ubser_softc *sc = device_get_softc(dev);
DPRINTF("\n");
ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
device_claim_softc(dev);
ubser_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(ubser);
static void
ubser_free_softc(struct ubser_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
ubser_free(struct ucom_softc *ucom)
{
ubser_free_softc(ucom->sc_parent);
}
static int
ubser_pre_param(struct ucom_softc *ucom, struct termios *t)
{
DPRINTF("\n");
/*
* The firmware on our devices can only do 8n1@9600bps
* without handshake.
* We refuse to accept other configurations.
*/
/* ensure 9600bps */
switch (t->c_ospeed) {
case 9600:
break;
default:
return (EINVAL);
}
/* 2 stop bits not possible */
if (t->c_cflag & CSTOPB)
return (EINVAL);
/* XXX parity handling not possible with current firmware */
if (t->c_cflag & PARENB)
return (EINVAL);
/* we can only do 8 data bits */
switch (t->c_cflag & CSIZE) {
case CS8:
break;
default:
return (EINVAL);
}
/* we can't do any kind of hardware handshaking */
if ((t->c_cflag &
(CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
return (EINVAL);
/*
* XXX xon/xoff not supported by the firmware!
* This is handled within FreeBSD only and may overflow buffers
* because of delayed reaction due to device buffering.
*/
return (0);
}
static __inline void
ubser_inc_tx_unit(struct ubser_softc *sc)
{
sc->sc_curr_tx_unit++;
if (sc->sc_curr_tx_unit >= sc->sc_numser) {
sc->sc_curr_tx_unit = 0;
}
}
static void
ubser_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ubser_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[1];
uint8_t first_unit = sc->sc_curr_tx_unit;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
do {
if (ucom_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
pc, 1, sc->sc_tx_size - 1,
&actlen)) {
buf[0] = sc->sc_curr_tx_unit;
usbd_copy_in(pc, 0, buf, 1);
usbd_xfer_set_frame_len(xfer, 0, actlen + 1);
usbd_transfer_submit(xfer);
ubser_inc_tx_unit(sc); /* round robin */
break;
}
ubser_inc_tx_unit(sc);
} while (sc->sc_curr_tx_unit != first_unit);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ubser_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ubser_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[1];
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen < 1) {
DPRINTF("invalid actlen=0!\n");
goto tr_setup;
}
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, 1);
if (buf[0] >= sc->sc_numser) {
DPRINTF("invalid serial number!\n");
goto tr_setup;
}
ucom_put_data(sc->sc_ucom + buf[0], pc, 1, actlen - 1);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ubser_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct ubser_softc *sc = ucom->sc_parent;
uint8_t x = ucom->sc_portno;
struct usb_device_request req;
usb_error_t err;
if (onoff) {
req.bmRequestType = UT_READ_VENDOR_INTERFACE;
req.bRequest = VENDOR_SET_BREAK;
req.wValue[0] = x;
req.wValue[1] = 0;
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 0);
err = ucom_cfg_do_request(sc->sc_udev, ucom,
&req, NULL, 0, 1000);
if (err) {
DPRINTFN(0, "send break failed, error=%s\n",
usbd_errstr(err));
}
}
}
static void
ubser_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
/* fake status bits */
*lsr = 0;
*msr = SER_DCD;
}
static void
ubser_start_read(struct ucom_softc *ucom)
{
struct ubser_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
}
static void
ubser_stop_read(struct ucom_softc *ucom)
{
struct ubser_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
}
static void
ubser_start_write(struct ucom_softc *ucom)
{
struct ubser_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
}
static void
ubser_stop_write(struct ucom_softc *ucom)
{
struct ubser_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
}
static void
ubser_poll(struct ucom_softc *ucom)
{
struct ubser_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UBSER_N_TRANSFER);
}

View File

@ -0,0 +1,880 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $ */
/*-
* Copyright (c) 2007, Takanori Watanabe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 2007 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Takuya SHIOZAKI (tshiozak@netbsd.org).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* Driver for WinChipHead CH341/340, the worst USB-serial chip in the
* world.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR uchcom_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int uchcom_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RWTUN,
&uchcom_debug, 0, "uchcom debug level");
#endif
#define UCHCOM_IFACE_INDEX 0
#define UCHCOM_CONFIG_INDEX 0
#define UCHCOM_REV_CH340 0x0250
#define UCHCOM_INPUT_BUF_SIZE 8
#define UCHCOM_REQ_GET_VERSION 0x5F
#define UCHCOM_REQ_READ_REG 0x95
#define UCHCOM_REQ_WRITE_REG 0x9A
#define UCHCOM_REQ_RESET 0xA1
#define UCHCOM_REQ_SET_DTRRTS 0xA4
#define UCHCOM_REG_STAT1 0x06
#define UCHCOM_REG_STAT2 0x07
#define UCHCOM_REG_BPS_PRE 0x12
#define UCHCOM_REG_BPS_DIV 0x13
#define UCHCOM_REG_BPS_MOD 0x14
#define UCHCOM_REG_BPS_PAD 0x0F
#define UCHCOM_REG_BREAK1 0x05
#define UCHCOM_REG_BREAK2 0x18
#define UCHCOM_REG_LCR1 0x18
#define UCHCOM_REG_LCR2 0x25
#define UCHCOM_VER_20 0x20
#define UCHCOM_BASE_UNKNOWN 0
#define UCHCOM_BPS_MOD_BASE 20000000
#define UCHCOM_BPS_MOD_BASE_OFS 1100
#define UCHCOM_DTR_MASK 0x20
#define UCHCOM_RTS_MASK 0x40
#define UCHCOM_BRK1_MASK 0x01
#define UCHCOM_BRK2_MASK 0x40
#define UCHCOM_LCR1_MASK 0xAF
#define UCHCOM_LCR2_MASK 0x07
#define UCHCOM_LCR1_PARENB 0x80
#define UCHCOM_LCR2_PAREVEN 0x07
#define UCHCOM_LCR2_PARODD 0x06
#define UCHCOM_LCR2_PARMARK 0x05
#define UCHCOM_LCR2_PARSPACE 0x04
#define UCHCOM_INTR_STAT1 0x02
#define UCHCOM_INTR_STAT2 0x03
#define UCHCOM_INTR_LEAST 4
#define UCHCOM_BULK_BUF_SIZE 1024 /* bytes */
enum {
UCHCOM_BULK_DT_WR,
UCHCOM_BULK_DT_RD,
UCHCOM_INTR_DT_RD,
UCHCOM_N_TRANSFER,
};
struct uchcom_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint8_t sc_dtr; /* local copy */
uint8_t sc_rts; /* local copy */
uint8_t sc_version;
uint8_t sc_msr;
uint8_t sc_lsr; /* local status register */
};
struct uchcom_divider {
uint8_t dv_prescaler;
uint8_t dv_div;
uint8_t dv_mod;
};
struct uchcom_divider_record {
uint32_t dvr_high;
uint32_t dvr_low;
uint32_t dvr_base_clock;
struct uchcom_divider dvr_divider;
};
static const struct uchcom_divider_record dividers[] =
{
{307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
{921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
{2999999, 23530, 6000000, {3, 0, 0}},
{23529, 2942, 750000, {2, 0, 0}},
{2941, 368, 93750, {1, 0, 0}},
{367, 1, 11719, {0, 0, 0}},
};
#define NUM_DIVIDERS nitems(dividers)
static const STRUCT_USB_HOST_ID uchcom_devs[] = {
{USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER_2, 0)},
};
/* protypes */
static void uchcom_free(struct ucom_softc *);
static int uchcom_pre_param(struct ucom_softc *, struct termios *);
static void uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void uchcom_cfg_open(struct ucom_softc *ucom);
static void uchcom_cfg_param(struct ucom_softc *, struct termios *);
static void uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
static void uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
static void uchcom_start_read(struct ucom_softc *);
static void uchcom_start_write(struct ucom_softc *);
static void uchcom_stop_read(struct ucom_softc *);
static void uchcom_stop_write(struct ucom_softc *);
static void uchcom_update_version(struct uchcom_softc *);
static void uchcom_convert_status(struct uchcom_softc *, uint8_t);
static void uchcom_update_status(struct uchcom_softc *);
static void uchcom_set_dtr_rts(struct uchcom_softc *);
static int uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
static void uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
static void uchcom_poll(struct ucom_softc *ucom);
static device_probe_t uchcom_probe;
static device_attach_t uchcom_attach;
static device_detach_t uchcom_detach;
static void uchcom_free_softc(struct uchcom_softc *);
static usb_callback_t uchcom_intr_callback;
static usb_callback_t uchcom_write_callback;
static usb_callback_t uchcom_read_callback;
static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
[UCHCOM_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UCHCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &uchcom_write_callback,
},
[UCHCOM_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UCHCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &uchcom_read_callback,
},
[UCHCOM_INTR_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &uchcom_intr_callback,
},
};
static struct ucom_callback uchcom_callback = {
.ucom_cfg_get_status = &uchcom_cfg_get_status,
.ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
.ucom_cfg_set_rts = &uchcom_cfg_set_rts,
.ucom_cfg_set_break = &uchcom_cfg_set_break,
.ucom_cfg_open = &uchcom_cfg_open,
.ucom_cfg_param = &uchcom_cfg_param,
.ucom_pre_param = &uchcom_pre_param,
.ucom_start_read = &uchcom_start_read,
.ucom_stop_read = &uchcom_stop_read,
.ucom_start_write = &uchcom_start_write,
.ucom_stop_write = &uchcom_stop_write,
.ucom_poll = &uchcom_poll,
.ucom_free = &uchcom_free,
};
/* ----------------------------------------------------------------------
* driver entry points
*/
static int
uchcom_probe(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
DPRINTFN(11, "\n");
if (uaa->usb_mode != USB_MODE_HOST) {
return (ENXIO);
}
if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
}
static int
uchcom_attach(device_t dev)
{
struct uchcom_softc *sc = device_get_softc(dev);
struct usb_attach_arg *uaa = device_get_ivars(dev);
int error;
uint8_t iface_index;
DPRINTFN(11, "\n");
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_udev = uaa->device;
switch (uaa->info.bcdDevice) {
case UCHCOM_REV_CH340:
device_printf(dev, "CH340 detected\n");
break;
default:
device_printf(dev, "CH341 detected\n");
break;
}
iface_index = UCHCOM_IFACE_INDEX;
error = usbd_transfer_setup(uaa->device,
&iface_index, sc->sc_xfer, uchcom_config_data,
UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
DPRINTF("one or more missing USB endpoints, "
"error=%s\n", usbd_errstr(error));
goto detach;
}
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&uchcom_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0);
detach:
uchcom_detach(dev);
return (ENXIO);
}
static int
uchcom_detach(device_t dev)
{
struct uchcom_softc *sc = device_get_softc(dev);
DPRINTFN(11, "\n");
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
device_claim_softc(dev);
uchcom_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(uchcom);
static void
uchcom_free_softc(struct uchcom_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
uchcom_free(struct ucom_softc *ucom)
{
uchcom_free_softc(ucom->sc_parent);
}
/* ----------------------------------------------------------------------
* low level i/o
*/
static void
uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
uint16_t value, uint16_t index)
{
struct usb_device_request req;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = reqno;
USETW(req.wValue, value);
USETW(req.wIndex, index);
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev,
&sc->sc_ucom, &req, NULL, 0, 1000);
}
static void
uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
uint16_t value, uint16_t index, void *buf, uint16_t buflen)
{
struct usb_device_request req;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = reqno;
USETW(req.wValue, value);
USETW(req.wIndex, index);
USETW(req.wLength, buflen);
ucom_cfg_do_request(sc->sc_udev,
&sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
}
static void
uchcom_write_reg(struct uchcom_softc *sc,
uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
{
DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
(unsigned)reg1, (unsigned)val1,
(unsigned)reg2, (unsigned)val2);
uchcom_ctrl_write(
sc, UCHCOM_REQ_WRITE_REG,
reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
}
static void
uchcom_read_reg(struct uchcom_softc *sc,
uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
{
uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
uchcom_ctrl_read(
sc, UCHCOM_REQ_READ_REG,
reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
(unsigned)reg1, (unsigned)buf[0],
(unsigned)reg2, (unsigned)buf[1]);
if (rval1)
*rval1 = buf[0];
if (rval2)
*rval2 = buf[1];
}
static void
uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
{
uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
if (rver)
*rver = buf[0];
}
static void
uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
{
uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
}
static void
uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
{
uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
}
static void
uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
{
uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
}
/* ----------------------------------------------------------------------
* middle layer
*/
static void
uchcom_update_version(struct uchcom_softc *sc)
{
uchcom_get_version(sc, &sc->sc_version);
}
static void
uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
{
sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
cur = ~cur & 0x0F;
sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
}
static void
uchcom_update_status(struct uchcom_softc *sc)
{
uint8_t cur;
uchcom_get_status(sc, &cur);
uchcom_convert_status(sc, cur);
}
static void
uchcom_set_dtr_rts(struct uchcom_softc *sc)
{
uint8_t val = 0;
if (sc->sc_dtr)
val |= UCHCOM_DTR_MASK;
if (sc->sc_rts)
val |= UCHCOM_RTS_MASK;
if (sc->sc_version < UCHCOM_VER_20)
uchcom_set_dtr_rts_10(sc, ~val);
else
uchcom_set_dtr_rts_20(sc, ~val);
}
static void
uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct uchcom_softc *sc = ucom->sc_parent;
uint8_t brk1;
uint8_t brk2;
uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
if (onoff) {
/* on - clear bits */
brk1 &= ~UCHCOM_BRK1_MASK;
brk2 &= ~UCHCOM_BRK2_MASK;
} else {
/* off - set bits */
brk1 |= UCHCOM_BRK1_MASK;
brk2 |= UCHCOM_BRK2_MASK;
}
uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
}
static int
uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
{
const struct uchcom_divider_record *rp;
uint32_t div;
uint32_t rem;
uint32_t mod;
uint8_t i;
/* find record */
for (i = 0; i != NUM_DIVIDERS; i++) {
if (dividers[i].dvr_high >= rate &&
dividers[i].dvr_low <= rate) {
rp = &dividers[i];
goto found;
}
}
return (-1);
found:
dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
dp->dv_div = rp->dvr_divider.dv_div;
else {
div = rp->dvr_base_clock / rate;
rem = rp->dvr_base_clock % rate;
if (div == 0 || div >= 0xFF)
return (-1);
if ((rem << 1) >= rate)
div += 1;
dp->dv_div = (uint8_t)-div;
}
mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
mod = mod + (mod / 2);
dp->dv_mod = (mod + 0xFF) / 0x100;
return (0);
}
static void
uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
{
struct uchcom_divider dv;
if (uchcom_calc_divider_settings(&dv, rate))
return;
uchcom_write_reg(sc,
UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
UCHCOM_REG_BPS_DIV, dv.dv_div);
uchcom_write_reg(sc,
UCHCOM_REG_BPS_MOD, dv.dv_mod,
UCHCOM_REG_BPS_PAD, 0);
}
/* ----------------------------------------------------------------------
* methods for ucom
*/
static void
uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct uchcom_softc *sc = ucom->sc_parent;
DPRINTF("\n");
/* XXX Note: sc_lsr is always zero */
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct uchcom_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
sc->sc_dtr = onoff;
uchcom_set_dtr_rts(sc);
}
static void
uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct uchcom_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
sc->sc_rts = onoff;
uchcom_set_dtr_rts(sc);
}
static void
uchcom_cfg_open(struct ucom_softc *ucom)
{
struct uchcom_softc *sc = ucom->sc_parent;
DPRINTF("\n");
uchcom_update_version(sc);
uchcom_update_status(sc);
}
static int
uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
struct uchcom_divider dv;
switch (t->c_cflag & CSIZE) {
case CS8:
break;
default:
return (EIO);
}
if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
return (EIO);
}
return (0); /* success */
}
static void
uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct uchcom_softc *sc = ucom->sc_parent;
uchcom_get_version(sc, 0);
uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
uchcom_set_baudrate(sc, t->c_ospeed);
uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
uchcom_update_status(sc);
uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
uchcom_set_baudrate(sc, t->c_ospeed);
uchcom_set_dtr_rts(sc);
uchcom_update_status(sc);
}
static void
uchcom_start_read(struct ucom_softc *ucom)
{
struct uchcom_softc *sc = ucom->sc_parent;
/* start interrupt endpoint */
usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
/* start read endpoint */
usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
}
static void
uchcom_stop_read(struct ucom_softc *ucom)
{
struct uchcom_softc *sc = ucom->sc_parent;
/* stop interrupt endpoint */
usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
/* stop read endpoint */
usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
}
static void
uchcom_start_write(struct ucom_softc *ucom)
{
struct uchcom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
}
static void
uchcom_stop_write(struct ucom_softc *ucom)
{
struct uchcom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
}
/* ----------------------------------------------------------------------
* callback when the modem status is changed.
*/
static void
uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uchcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[UCHCOM_INTR_LEAST];
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
DPRINTF("actlen = %u\n", actlen);
if (actlen >= UCHCOM_INTR_LEAST) {
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
(unsigned)buf[0], (unsigned)buf[1],
(unsigned)buf[2], (unsigned)buf[3]);
uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
ucom_status_change(&sc->sc_ucom);
}
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
break;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
break;
}
}
static void
uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uchcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
usbd_xfer_max_len(xfer), &actlen)) {
DPRINTF("actlen = %d\n", actlen);
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
break;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
break;
}
}
static void
uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uchcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen > 0) {
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
}
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
break;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
break;
}
}
static void
uchcom_poll(struct ucom_softc *ucom)
{
struct uchcom_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
}
static device_method_t uchcom_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, uchcom_probe),
DEVMETHOD(device_attach, uchcom_attach),
DEVMETHOD(device_detach, uchcom_detach),
DEVMETHOD_END
};
static driver_t uchcom_driver = {
.name = "uchcom",
.methods = uchcom_methods,
.size = sizeof(struct uchcom_softc)
};
static devclass_t uchcom_devclass;
DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
MODULE_DEPEND(uchcom, usb, 1, 1, 1);
MODULE_VERSION(uchcom, 1);
USB_PNP_HOST_INFO(uchcom_devs);

View File

@ -0,0 +1,614 @@
#include <machine/rtems-bsd-kernel-space.h>
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* Copyright (c) 2004 Dag-Erling Coïdan Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
* RS232 bridges.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbhid.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR usb_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#define UCYCOM_MAX_IOLEN (1024 + 2) /* bytes */
#define UCYCOM_IFACE_INDEX 0
enum {
UCYCOM_CTRL_RD,
UCYCOM_INTR_RD,
UCYCOM_N_TRANSFER,
};
struct ucycom_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_device *sc_udev;
struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
struct mtx sc_mtx;
uint32_t sc_model;
#define MODEL_CY7C63743 0x63743
#define MODEL_CY7C64013 0x64013
uint16_t sc_flen; /* feature report length */
uint16_t sc_ilen; /* input report length */
uint16_t sc_olen; /* output report length */
uint8_t sc_fid; /* feature report id */
uint8_t sc_iid; /* input report id */
uint8_t sc_oid; /* output report id */
uint8_t sc_cfg;
#define UCYCOM_CFG_RESET 0x80
#define UCYCOM_CFG_PARODD 0x20
#define UCYCOM_CFG_PAREN 0x10
#define UCYCOM_CFG_STOPB 0x08
#define UCYCOM_CFG_DATAB 0x03
uint8_t sc_ist; /* status flags from last input */
uint8_t sc_iface_no;
uint8_t sc_temp_cfg[32];
};
/* prototypes */
static device_probe_t ucycom_probe;
static device_attach_t ucycom_attach;
static device_detach_t ucycom_detach;
static void ucycom_free_softc(struct ucycom_softc *);
static usb_callback_t ucycom_ctrl_write_callback;
static usb_callback_t ucycom_intr_read_callback;
static void ucycom_free(struct ucom_softc *);
static void ucycom_cfg_open(struct ucom_softc *);
static void ucycom_start_read(struct ucom_softc *);
static void ucycom_stop_read(struct ucom_softc *);
static void ucycom_start_write(struct ucom_softc *);
static void ucycom_stop_write(struct ucom_softc *);
static void ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t);
static int ucycom_pre_param(struct ucom_softc *, struct termios *);
static void ucycom_cfg_param(struct ucom_softc *, struct termios *);
static void ucycom_poll(struct ucom_softc *ucom);
static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
[UCYCOM_CTRL_RD] = {
.type = UE_CONTROL,
.endpoint = 0x00, /* Control pipe */
.direction = UE_DIR_ANY,
.bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN),
.callback = &ucycom_ctrl_write_callback,
.timeout = 1000, /* 1 second */
},
[UCYCOM_INTR_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = UCYCOM_MAX_IOLEN,
.callback = &ucycom_intr_read_callback,
},
};
static const struct ucom_callback ucycom_callback = {
.ucom_cfg_param = &ucycom_cfg_param,
.ucom_cfg_open = &ucycom_cfg_open,
.ucom_pre_param = &ucycom_pre_param,
.ucom_start_read = &ucycom_start_read,
.ucom_stop_read = &ucycom_stop_read,
.ucom_start_write = &ucycom_start_write,
.ucom_stop_write = &ucycom_stop_write,
.ucom_poll = &ucycom_poll,
.ucom_free = &ucycom_free,
};
static device_method_t ucycom_methods[] = {
DEVMETHOD(device_probe, ucycom_probe),
DEVMETHOD(device_attach, ucycom_attach),
DEVMETHOD(device_detach, ucycom_detach),
DEVMETHOD_END
};
static devclass_t ucycom_devclass;
static driver_t ucycom_driver = {
.name = "ucycom",
.methods = ucycom_methods,
.size = sizeof(struct ucycom_softc),
};
/*
* Supported devices
*/
static const STRUCT_USB_HOST_ID ucycom_devs[] = {
{USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
};
DRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, NULL, 0);
MODULE_DEPEND(ucycom, ucom, 1, 1, 1);
MODULE_DEPEND(ucycom, usb, 1, 1, 1);
MODULE_VERSION(ucycom, 1);
USB_PNP_HOST_INFO(ucycom_devs);
#define UCYCOM_DEFAULT_RATE 4800
#define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */
static int
ucycom_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 != 0) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
}
static int
ucycom_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct ucycom_softc *sc = device_get_softc(dev);
void *urd_ptr = NULL;
int32_t error;
uint16_t urd_len;
uint8_t iface_index;
sc->sc_udev = uaa->device;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
DPRINTF("\n");
/* get chip model */
sc->sc_model = USB_GET_DRIVER_INFO(uaa);
if (sc->sc_model == 0) {
device_printf(dev, "unsupported device\n");
goto detach;
}
device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
/* get report descriptor */
error = usbd_req_get_hid_desc(uaa->device, NULL,
&urd_ptr, &urd_len, M_USBDEV,
UCYCOM_IFACE_INDEX);
if (error) {
device_printf(dev, "failed to get report "
"descriptor: %s\n",
usbd_errstr(error));
goto detach;
}
/* get report sizes */
sc->sc_flen = hid_report_size(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
sc->sc_ilen = hid_report_size(urd_ptr, urd_len, hid_input, &sc->sc_iid);
sc->sc_olen = hid_report_size(urd_ptr, urd_len, hid_output, &sc->sc_oid);
if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
(sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
(sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
sc->sc_ilen, sc->sc_olen, sc->sc_flen,
UCYCOM_MAX_IOLEN);
goto detach;
}
sc->sc_iface_no = uaa->info.bIfaceNum;
iface_index = UCYCOM_IFACE_INDEX;
error = usbd_transfer_setup(uaa->device, &iface_index,
sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
sc, &sc->sc_mtx);
if (error) {
device_printf(dev, "allocating USB "
"transfers failed\n");
goto detach;
}
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&ucycom_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
if (urd_ptr) {
free(urd_ptr, M_USBDEV);
}
return (0); /* success */
detach:
if (urd_ptr) {
free(urd_ptr, M_USBDEV);
}
ucycom_detach(dev);
return (ENXIO);
}
static int
ucycom_detach(device_t dev)
{
struct ucycom_softc *sc = device_get_softc(dev);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
device_claim_softc(dev);
ucycom_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(ucycom);
static void
ucycom_free_softc(struct ucycom_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
ucycom_free(struct ucom_softc *ucom)
{
ucycom_free_softc(ucom->sc_parent);
}
static void
ucycom_cfg_open(struct ucom_softc *ucom)
{
struct ucycom_softc *sc = ucom->sc_parent;
/* set default configuration */
ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
}
static void
ucycom_start_read(struct ucom_softc *ucom)
{
struct ucycom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
}
static void
ucycom_stop_read(struct ucom_softc *ucom)
{
struct ucycom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
}
static void
ucycom_start_write(struct ucom_softc *ucom)
{
struct ucycom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
}
static void
ucycom_stop_write(struct ucom_softc *ucom)
{
struct ucycom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
}
static void
ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ucycom_softc *sc = usbd_xfer_softc(xfer);
struct usb_device_request req;
struct usb_page_cache *pc0, *pc1;
uint8_t data[2];
uint8_t offset;
uint32_t actlen;
pc0 = usbd_xfer_get_frame(xfer, 0);
pc1 = usbd_xfer_get_frame(xfer, 1);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
tr_transferred:
case USB_ST_SETUP:
switch (sc->sc_model) {
case MODEL_CY7C63743:
offset = 1;
break;
case MODEL_CY7C64013:
offset = 2;
break;
default:
offset = 0;
break;
}
if (ucom_get_data(&sc->sc_ucom, pc1, offset,
sc->sc_olen - offset, &actlen)) {
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UR_SET_REPORT;
USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, sc->sc_olen);
switch (sc->sc_model) {
case MODEL_CY7C63743:
data[0] = actlen;
break;
case MODEL_CY7C64013:
data[0] = 0;
data[1] = actlen;
break;
default:
break;
}
usbd_copy_in(pc0, 0, &req, sizeof(req));
usbd_copy_in(pc1, 0, data, offset);
usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error == USB_ERR_CANCELLED) {
return;
}
DPRINTF("error=%s\n",
usbd_errstr(error));
goto tr_transferred;
}
}
static void
ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
{
struct usb_device_request req;
uint16_t len;
usb_error_t err;
len = sc->sc_flen;
if (len > sizeof(sc->sc_temp_cfg)) {
len = sizeof(sc->sc_temp_cfg);
}
sc->sc_cfg = cfg;
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UR_SET_REPORT;
USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, len);
sc->sc_temp_cfg[0] = (baud & 0xff);
sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
sc->sc_temp_cfg[4] = cfg;
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, sc->sc_temp_cfg, 0, 1000);
if (err) {
DPRINTFN(0, "device request failed, err=%s "
"(ignored)\n", usbd_errstr(err));
}
}
static int
ucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
switch (t->c_ospeed) {
case 600:
case 1200:
case 2400:
case 4800:
case 9600:
case 19200:
case 38400:
case 57600:
#if 0
/*
* Stock chips only support standard baud rates in the 600 - 57600
* range, but higher rates can be achieved using custom firmware.
*/
case 115200:
case 153600:
case 192000:
#endif
break;
default:
return (EINVAL);
}
return (0);
}
static void
ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct ucycom_softc *sc = ucom->sc_parent;
uint8_t cfg;
DPRINTF("\n");
if (t->c_cflag & CIGNORE) {
cfg = sc->sc_cfg;
} else {
cfg = 0;
switch (t->c_cflag & CSIZE) {
default:
case CS8:
++cfg;
case CS7:
++cfg;
case CS6:
++cfg;
case CS5:
break;
}
if (t->c_cflag & CSTOPB)
cfg |= UCYCOM_CFG_STOPB;
if (t->c_cflag & PARENB)
cfg |= UCYCOM_CFG_PAREN;
if (t->c_cflag & PARODD)
cfg |= UCYCOM_CFG_PARODD;
}
ucycom_cfg_write(sc, t->c_ospeed, cfg);
}
static void
ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ucycom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[2];
uint32_t offset;
int len;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
pc = usbd_xfer_get_frame(xfer, 0);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
switch (sc->sc_model) {
case MODEL_CY7C63743:
if (actlen < 1) {
goto tr_setup;
}
usbd_copy_out(pc, 0, buf, 1);
sc->sc_ist = buf[0] & ~0x07;
len = buf[0] & 0x07;
actlen--;
offset = 1;
break;
case MODEL_CY7C64013:
if (actlen < 2) {
goto tr_setup;
}
usbd_copy_out(pc, 0, buf, 2);
sc->sc_ist = buf[0] & ~0x07;
len = buf[1];
actlen -= 2;
offset = 2;
break;
default:
DPRINTFN(0, "unsupported model number\n");
goto tr_setup;
}
if (len > actlen)
len = actlen;
if (len)
ucom_put_data(&sc->sc_ucom, pc, offset, len);
/* FALLTHROUGH */
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ucycom_poll(struct ucom_softc *ucom)
{
struct ucycom_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,314 @@
/* $NetBSD: uftdireg.h,v 1.6 2002/07/11 21:14:28 augustss Exp $ */
/* $FreeBSD$ */
/*
* Definitions for the FTDI USB Single Port Serial Converter -
* known as FTDI_SIO (Serial Input/Output application of the chipset)
*
* The device is based on the FTDI FT8U100AX chip. It has a DB25 on one side,
* USB on the other.
*
* Thanx to FTDI (http://www.ftdi.co.uk) for so kindly providing details
* of the protocol required to talk to the device and ongoing assistence
* during development.
*
* Bill Ryder - bryder@sgi.com of Silicon Graphics, Inc. is the original
* author of this file.
*/
/* Modified by Lennart Augustsson */
/* Vendor Request Interface */
#define FTDI_SIO_RESET 0 /* Reset the port */
#define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */
#define FTDI_SIO_SET_FLOW_CTRL 2 /* Set flow control register */
#define FTDI_SIO_SET_BAUD_RATE 3 /* Set baud rate */
#define FTDI_SIO_SET_DATA 4 /* Set the data characteristics of the
* port */
#define FTDI_SIO_GET_STATUS 5 /* Retrieve current value of status
* reg */
#define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */
#define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */
#define FTDI_SIO_SET_LATENCY 9 /* Set the latency timer */
#define FTDI_SIO_GET_LATENCY 10 /* Read the latency timer */
#define FTDI_SIO_SET_BITMODE 11 /* Set the bit bang I/O mode */
#define FTDI_SIO_GET_BITMODE 12 /* Read pin states from any mode */
#define FTDI_SIO_READ_EEPROM 144 /* Read eeprom word */
#define FTDI_SIO_WRITE_EEPROM 145 /* Write eeprom word */
#define FTDI_SIO_ERASE_EEPROM 146 /* Erase entire eeprom */
/* Port Identifier Table */
#define FTDI_PIT_DEFAULT 0 /* SIOA */
#define FTDI_PIT_SIOA 1 /* SIOA */
#define FTDI_PIT_SIOB 2 /* SIOB */
#define FTDI_PIT_PARALLEL 3 /* Parallel */
/* Values for driver_info */
#define UFTDI_JTAG_IFACE(i) (1 << i) /* Flag interface as jtag */
#define UFTDI_JTAG_IFACES_MAX 8 /* Allow up to 8 jtag intfs */
#define UFTDI_JTAG_CHECK_STRING 0xff /* Check product names table */
#define UFTDI_JTAG_MASK 0xff
/*
* BmRequestType: 0100 0000B
* bRequest: FTDI_SIO_RESET
* wValue: Control Value
* 0 = Reset SIO
* 1 = Purge RX buffer
* 2 = Purge TX buffer
* wIndex: Port
* wLength: 0
* Data: None
*
* The Reset SIO command has this effect:
*
* Sets flow control set to 'none'
* Event char = 0x0d
* Event trigger = disabled
* Purge RX buffer
* Purge TX buffer
* Clear DTR
* Clear RTS
* baud and data format not reset
*
* The Purge RX and TX buffer commands affect nothing except the buffers
*/
/* FTDI_SIO_RESET */
#define FTDI_SIO_RESET_SIO 0
#define FTDI_SIO_RESET_PURGE_RX 1
#define FTDI_SIO_RESET_PURGE_TX 2
/*
* BmRequestType: 0100 0000B
* bRequest: FTDI_SIO_SET_BAUDRATE
* wValue: BaudRate low bits
* wIndex: Port and BaudRate high bits
* wLength: 0
* Data: None
*/
/* FTDI_SIO_SET_BAUDRATE */
/*
* BmRequestType: 0100 0000B
* bRequest: FTDI_SIO_SET_DATA
* wValue: Data characteristics (see below)
* wIndex: Port
* wLength: 0
* Data: No
*
* Data characteristics
*
* B0..7 Number of data bits
* B8..10 Parity
* 0 = None
* 1 = Odd
* 2 = Even
* 3 = Mark
* 4 = Space
* B11..13 Stop Bits
* 0 = 1
* 1 = 1.5
* 2 = 2
* B14..15 Reserved
*
*/
/* FTDI_SIO_SET_DATA */
#define FTDI_SIO_SET_DATA_BITS(n) (n)
#define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8)
#define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8)
#define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8)
#define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8)
#define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8)
#define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11)
#define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11)
#define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11)
#define FTDI_SIO_SET_BREAK (0x1 << 14)
/*
* BmRequestType: 0100 0000B
* bRequest: FTDI_SIO_MODEM_CTRL
* wValue: ControlValue (see below)
* wIndex: Port
* wLength: 0
* Data: None
*
* NOTE: If the device is in RTS/CTS flow control, the RTS set by this
* command will be IGNORED without an error being returned
* Also - you can not set DTR and RTS with one control message
*
* ControlValue
* B0 DTR state
* 0 = reset
* 1 = set
* B1 RTS state
* 0 = reset
* 1 = set
* B2..7 Reserved
* B8 DTR state enable
* 0 = ignore
* 1 = use DTR state
* B9 RTS state enable
* 0 = ignore
* 1 = use RTS state
* B10..15 Reserved
*/
/* FTDI_SIO_MODEM_CTRL */
#define FTDI_SIO_SET_DTR_MASK 0x1
#define FTDI_SIO_SET_DTR_HIGH (1 | ( FTDI_SIO_SET_DTR_MASK << 8))
#define FTDI_SIO_SET_DTR_LOW (0 | ( FTDI_SIO_SET_DTR_MASK << 8))
#define FTDI_SIO_SET_RTS_MASK 0x2
#define FTDI_SIO_SET_RTS_HIGH (2 | ( FTDI_SIO_SET_RTS_MASK << 8))
#define FTDI_SIO_SET_RTS_LOW (0 | ( FTDI_SIO_SET_RTS_MASK << 8))
/*
* BmRequestType: 0100 0000b
* bRequest: FTDI_SIO_SET_FLOW_CTRL
* wValue: Xoff/Xon
* wIndex: Protocol/Port - hIndex is protocol / lIndex is port
* wLength: 0
* Data: None
*
* hIndex protocol is:
* B0 Output handshaking using RTS/CTS
* 0 = disabled
* 1 = enabled
* B1 Output handshaking using DTR/DSR
* 0 = disabled
* 1 = enabled
* B2 Xon/Xoff handshaking
* 0 = disabled
* 1 = enabled
*
* A value of zero in the hIndex field disables handshaking
*
* If Xon/Xoff handshaking is specified, the hValue field should contain the
* XOFF character and the lValue field contains the XON character.
*/
/* FTDI_SIO_SET_FLOW_CTRL */
#define FTDI_SIO_DISABLE_FLOW_CTRL 0x0
#define FTDI_SIO_RTS_CTS_HS 0x1
#define FTDI_SIO_DTR_DSR_HS 0x2
#define FTDI_SIO_XON_XOFF_HS 0x4
/*
* BmRequestType: 0100 0000b
* bRequest: FTDI_SIO_SET_EVENT_CHAR
* wValue: Event Char
* wIndex: Port
* wLength: 0
* Data: None
*
* wValue:
* B0..7 Event Character
* B8 Event Character Processing
* 0 = disabled
* 1 = enabled
* B9..15 Reserved
*
* FTDI_SIO_SET_EVENT_CHAR
*
* Set the special event character for the specified communications port.
* If the device sees this character it will immediately return the
* data read so far - rather than wait 40ms or until 62 bytes are read
* which is what normally happens.
*/
/*
* BmRequestType: 0100 0000b
* bRequest: FTDI_SIO_SET_ERROR_CHAR
* wValue: Error Char
* wIndex: Port
* wLength: 0
* Data: None
*
* Error Char
* B0..7 Error Character
* B8 Error Character Processing
* 0 = disabled
* 1 = enabled
* B9..15 Reserved
* FTDI_SIO_SET_ERROR_CHAR
* Set the parity error replacement character for the specified communications
* port.
*/
/*
* BmRequestType: 1100 0000b
* bRequest: FTDI_SIO_GET_MODEM_STATUS
* wValue: zero
* wIndex: Port
* wLength: 1
* Data: Status
*
* One byte of data is returned
* B0..3 0
* B4 CTS
* 0 = inactive
* 1 = active
* B5 DSR
* 0 = inactive
* 1 = active
* B6 Ring Indicator (RI)
* 0 = inactive
* 1 = active
* B7 Receive Line Signal Detect (RLSD)
* 0 = inactive
* 1 = active
*
* FTDI_SIO_GET_MODEM_STATUS
* Retrieve the current value of the modem status register.
*/
#define FTDI_SIO_CTS_MASK 0x10
#define FTDI_SIO_DSR_MASK 0x20
#define FTDI_SIO_RI_MASK 0x40
#define FTDI_SIO_RLSD_MASK 0x80
/*
* DATA FORMAT
*
* IN Endpoint
*
* The device reserves the first two bytes of data on this endpoint to contain
* the current values of the modem and line status registers. In the absence of
* data, the device generates a message consisting of these two status bytes
* every 40 ms.
*
* Byte 0: Modem Status
* NOTE: 4 upper bits have same layout as the MSR register in a 16550
*
* Offset Description
* B0..3 Port
* B4 Clear to Send (CTS)
* B5 Data Set Ready (DSR)
* B6 Ring Indicator (RI)
* B7 Receive Line Signal Detect (RLSD)
*
* Byte 1: Line Status
* NOTE: same layout as the LSR register in a 16550
*
* Offset Description
* B0 Data Ready (DR)
* B1 Overrun Error (OE)
* B2 Parity Error (PE)
* B3 Framing Error (FE)
* B4 Break Interrupt (BI)
* B5 Transmitter Holding Register (THRE)
* B6 Transmitter Empty (TEMT)
* B7 Error in RCVR FIFO
* OUT Endpoint
*
* This device reserves the first bytes of data on this endpoint contain the
* length and port identifier of the message. For the FTDI USB Serial converter
* the port identifier is always 1.
*
* Byte 0: Port & length
*
* Offset Description
* B0..1 Port
* B2..7 Length of message - (not including Byte 0)
*/
#define FTDI_PORT_MASK 0x0f
#define FTDI_MSR_MASK 0xf0
#define FTDI_GET_MSR(p) (((p)[0]) & FTDI_MSR_MASK)
#define FTDI_GET_LSR(p) ((p)[1])
#define FTDI_LSR_MASK (~0x60) /* interesting bits */
#define FTDI_OUT_TAG(len, port) (((len) << 2) | (port))

View File

@ -0,0 +1,403 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $FreeBSD$ */
/* $NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $ */
/*
* Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Roland C. Dowdeswell <elric@netbsd.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* NOTE: all function names beginning like "ugensa_cfg_" can only
* be called from within the config thread function !
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR usb_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#define UGENSA_BUF_SIZE 2048 /* bytes */
#define UGENSA_CONFIG_INDEX 0
#define UGENSA_IFACE_INDEX 0
#define UGENSA_IFACE_MAX 8 /* exclusivly */
enum {
UGENSA_BULK_DT_WR,
UGENSA_BULK_DT_RD,
UGENSA_N_TRANSFER,
};
struct ugensa_sub_softc {
struct ucom_softc *sc_ucom_ptr;
struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
};
struct ugensa_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
struct mtx sc_mtx;
uint8_t sc_niface;
};
/* prototypes */
static device_probe_t ugensa_probe;
static device_attach_t ugensa_attach;
static device_detach_t ugensa_detach;
static void ugensa_free_softc(struct ugensa_softc *);
static usb_callback_t ugensa_bulk_write_callback;
static usb_callback_t ugensa_bulk_read_callback;
static void ugensa_free(struct ucom_softc *);
static void ugensa_start_read(struct ucom_softc *);
static void ugensa_stop_read(struct ucom_softc *);
static void ugensa_start_write(struct ucom_softc *);
static void ugensa_stop_write(struct ucom_softc *);
static void ugensa_poll(struct ucom_softc *ucom);
static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
[UGENSA_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UGENSA_BUF_SIZE,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &ugensa_bulk_write_callback,
},
[UGENSA_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UGENSA_BUF_SIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &ugensa_bulk_read_callback,
},
};
static const struct ucom_callback ugensa_callback = {
.ucom_start_read = &ugensa_start_read,
.ucom_stop_read = &ugensa_stop_read,
.ucom_start_write = &ugensa_start_write,
.ucom_stop_write = &ugensa_stop_write,
.ucom_poll = &ugensa_poll,
.ucom_free = &ugensa_free,
};
static device_method_t ugensa_methods[] = {
/* Device methods */
DEVMETHOD(device_probe, ugensa_probe),
DEVMETHOD(device_attach, ugensa_attach),
DEVMETHOD(device_detach, ugensa_detach),
DEVMETHOD_END
};
static devclass_t ugensa_devclass;
static driver_t ugensa_driver = {
.name = "ugensa",
.methods = ugensa_methods,
.size = sizeof(struct ugensa_softc),
};
static const STRUCT_USB_HOST_ID ugensa_devs[] = {
{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
};
DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
MODULE_DEPEND(ugensa, usb, 1, 1, 1);
MODULE_VERSION(ugensa, 1);
USB_PNP_HOST_INFO(ugensa_devs);
static int
ugensa_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 != UGENSA_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != 0) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
}
static int
ugensa_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct ugensa_softc *sc = device_get_softc(dev);
struct ugensa_sub_softc *ssc;
struct usb_interface *iface;
int32_t error;
uint8_t iface_index;
int x, cnt;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
/* Figure out how many interfaces this device has got */
for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
(usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
/* we have reached the end */
break;
}
}
if (cnt == 0) {
device_printf(dev, "No interfaces\n");
goto detach;
}
for (x = 0; x < cnt; x++) {
iface = usbd_get_iface(uaa->device, x);
if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
/* Not a serial port, most likely a SD reader */
continue;
ssc = sc->sc_sub + sc->sc_niface;
ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
iface_index = (UGENSA_IFACE_INDEX + x);
error = usbd_transfer_setup(uaa->device,
&iface_index, ssc->sc_xfer, ugensa_xfer_config,
UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
if (error) {
device_printf(dev, "allocating USB "
"transfers failed\n");
goto detach;
}
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
/* initialize port number */
ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
sc->sc_niface++;
if (x != uaa->info.bIfaceIndex)
usbd_set_parent_iface(uaa->device, x,
uaa->info.bIfaceIndex);
}
device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
&ugensa_callback, &sc->sc_mtx);
if (error) {
DPRINTF("attach failed\n");
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0); /* success */
detach:
ugensa_detach(dev);
return (ENXIO); /* failure */
}
static int
ugensa_detach(device_t dev)
{
struct ugensa_softc *sc = device_get_softc(dev);
uint8_t x;
ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
for (x = 0; x < sc->sc_niface; x++) {
usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
}
device_claim_softc(dev);
ugensa_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(ugensa);
static void
ugensa_free_softc(struct ugensa_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
ugensa_free(struct ucom_softc *ucom)
{
ugensa_free_softc(ucom->sc_parent);
}
static void
ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
UGENSA_BUF_SIZE, &actlen)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
ugensa_start_read(struct ucom_softc *ucom)
{
struct ugensa_softc *sc = ucom->sc_parent;
struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
}
static void
ugensa_stop_read(struct ucom_softc *ucom)
{
struct ugensa_softc *sc = ucom->sc_parent;
struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
}
static void
ugensa_start_write(struct ucom_softc *ucom)
{
struct ugensa_softc *sc = ucom->sc_parent;
struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
}
static void
ugensa_stop_write(struct ucom_softc *ucom)
{
struct ugensa_softc *sc = ucom->sc_parent;
struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
}
static void
ugensa_poll(struct ucom_softc *ucom)
{
struct ugensa_softc *sc = ucom->sc_parent;
struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,764 @@
#include <machine/rtems-bsd-kernel-space.h>
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/* $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $ */
/*-
* Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
* Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <sys/syslog.h>
#include <sys/selinfo.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbhid.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR ulpt_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#ifdef USB_DEBUG
static int ulpt_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RWTUN,
&ulpt_debug, 0, "Debug level");
#endif
#define ULPT_BSIZE (1<<15) /* bytes */
#define ULPT_IFQ_MAXLEN 2 /* units */
#define UR_GET_DEVICE_ID 0x00
#define UR_GET_PORT_STATUS 0x01
#define UR_SOFT_RESET 0x02
#define LPS_NERR 0x08 /* printer no error */
#define LPS_SELECT 0x10 /* printer selected */
#define LPS_NOPAPER 0x20 /* printer out of paper */
#define LPS_INVERT (LPS_SELECT|LPS_NERR)
#define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
enum {
ULPT_BULK_DT_WR,
ULPT_BULK_DT_RD,
ULPT_INTR_DT_RD,
ULPT_N_TRANSFER,
};
struct ulpt_softc {
struct usb_fifo_sc sc_fifo;
struct usb_fifo_sc sc_fifo_noreset;
struct mtx sc_mtx;
struct usb_callout sc_watchdog;
device_t sc_dev;
struct usb_device *sc_udev;
struct usb_fifo *sc_fifo_open[2];
struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
int sc_fflags; /* current open flags, FREAD and
* FWRITE */
uint8_t sc_iface_no;
uint8_t sc_last_status;
uint8_t sc_zlps; /* number of consequtive zero length
* packets received */
};
/* prototypes */
static device_probe_t ulpt_probe;
static device_attach_t ulpt_attach;
static device_detach_t ulpt_detach;
static usb_callback_t ulpt_write_callback;
static usb_callback_t ulpt_read_callback;
static usb_callback_t ulpt_status_callback;
static void ulpt_reset(struct ulpt_softc *);
static void ulpt_watchdog(void *);
static usb_fifo_close_t ulpt_close;
static usb_fifo_cmd_t ulpt_start_read;
static usb_fifo_cmd_t ulpt_start_write;
static usb_fifo_cmd_t ulpt_stop_read;
static usb_fifo_cmd_t ulpt_stop_write;
static usb_fifo_ioctl_t ulpt_ioctl;
static usb_fifo_open_t ulpt_open;
static usb_fifo_open_t unlpt_open;
static struct usb_fifo_methods ulpt_fifo_methods = {
.f_close = &ulpt_close,
.f_ioctl = &ulpt_ioctl,
.f_open = &ulpt_open,
.f_start_read = &ulpt_start_read,
.f_start_write = &ulpt_start_write,
.f_stop_read = &ulpt_stop_read,
.f_stop_write = &ulpt_stop_write,
.basename[0] = "ulpt",
};
static struct usb_fifo_methods unlpt_fifo_methods = {
.f_close = &ulpt_close,
.f_ioctl = &ulpt_ioctl,
.f_open = &unlpt_open,
.f_start_read = &ulpt_start_read,
.f_start_write = &ulpt_start_write,
.f_stop_read = &ulpt_stop_read,
.f_stop_write = &ulpt_stop_write,
.basename[0] = "unlpt",
};
static void
ulpt_reset(struct ulpt_softc *sc)
{
struct usb_device_request req;
DPRINTFN(2, "\n");
req.bRequest = UR_SOFT_RESET;
USETW(req.wValue, 0);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
/*
* There was a mistake in the USB printer 1.0 spec that gave the
* request type as UT_WRITE_CLASS_OTHER; it should have been
* UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
* so we try both.
*/
mtx_lock(&sc->sc_mtx);
req.bmRequestType = UT_WRITE_CLASS_OTHER;
if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
&req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.0 */
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
&req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.1 */
/* ignore error */
}
}
mtx_unlock(&sc->sc_mtx);
}
static void
ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ulpt_softc *sc = usbd_xfer_softc(xfer);
struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
struct usb_page_cache *pc;
int actlen, max;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
if (f == NULL) {
/* should not happen */
DPRINTF("no FIFO\n");
return;
}
DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
case USB_ST_SETUP:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
max = usbd_xfer_max_len(xfer);
if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
break;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
break;
}
}
static void
ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ulpt_softc *sc = usbd_xfer_softc(xfer);
struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
if (f == NULL) {
/* should not happen */
DPRINTF("no FIFO\n");
return;
}
DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen == 0) {
if (sc->sc_zlps == 4) {
/* enable BULK throttle */
usbd_xfer_set_interval(xfer, 500); /* ms */
} else {
sc->sc_zlps++;
}
} else {
/* disable BULK throttle */
usbd_xfer_set_interval(xfer, 0);
sc->sc_zlps = 0;
}
pc = usbd_xfer_get_frame(xfer, 0);
usb_fifo_put_data(f, pc, 0, actlen, 1);
case USB_ST_SETUP:
tr_setup:
if (usb_fifo_put_bytes_max(f) != 0) {
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
}
break;
default: /* Error */
/* disable BULK throttle */
usbd_xfer_set_interval(xfer, 0);
sc->sc_zlps = 0;
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
break;
}
}
static void
ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct ulpt_softc *sc = usbd_xfer_softc(xfer);
struct usb_device_request req;
struct usb_page_cache *pc;
uint8_t cur_status;
uint8_t new_status;
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 1);
usbd_copy_out(pc, 0, &cur_status, 1);
cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
new_status = cur_status & ~sc->sc_last_status;
sc->sc_last_status = cur_status;
if (new_status & LPS_SELECT)
log(LOG_NOTICE, "%s: offline\n",
device_get_nameunit(sc->sc_dev));
else if (new_status & LPS_NOPAPER)
log(LOG_NOTICE, "%s: out of paper\n",
device_get_nameunit(sc->sc_dev));
else if (new_status & LPS_NERR)
log(LOG_NOTICE, "%s: output error\n",
device_get_nameunit(sc->sc_dev));
break;
case USB_ST_SETUP:
req.bmRequestType = UT_READ_CLASS_INTERFACE;
req.bRequest = UR_GET_PORT_STATUS;
USETW(req.wValue, 0);
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 1);
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_in(pc, 0, &req, sizeof(req));
usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
usbd_xfer_set_frame_len(xfer, 1, 1);
usbd_xfer_set_frames(xfer, 2);
usbd_transfer_submit(xfer);
break;
default: /* Error */
DPRINTF("error=%s\n", usbd_errstr(error));
if (error != USB_ERR_CANCELLED) {
/* wait for next watchdog timeout */
}
break;
}
}
static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
[ULPT_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = ULPT_BSIZE,
.flags = {.pipe_bof = 1,.proxy_buffer = 1},
.callback = &ulpt_write_callback,
},
[ULPT_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = ULPT_BSIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
.callback = &ulpt_read_callback,
},
[ULPT_INTR_DT_RD] = {
.type = UE_CONTROL,
.endpoint = 0x00, /* Control pipe */
.direction = UE_DIR_ANY,
.bufsize = sizeof(struct usb_device_request) + 1,
.callback = &ulpt_status_callback,
.timeout = 1000, /* 1 second */
},
};
static void
ulpt_start_read(struct usb_fifo *fifo)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
}
static void
ulpt_stop_read(struct usb_fifo *fifo)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
}
static void
ulpt_start_write(struct usb_fifo *fifo)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
}
static void
ulpt_stop_write(struct usb_fifo *fifo)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
}
static int
ulpt_open(struct usb_fifo *fifo, int fflags)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
/* we assume that open is a serial process */
if (sc->sc_fflags == 0) {
/* reset USB parallel port */
ulpt_reset(sc);
}
return (unlpt_open(fifo, fflags));
}
static int
unlpt_open(struct usb_fifo *fifo, int fflags)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
if (sc->sc_fflags & fflags) {
return (EBUSY);
}
if (fflags & FREAD) {
/* clear stall first */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
if (usb_fifo_alloc_buffer(fifo,
usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
ULPT_IFQ_MAXLEN)) {
return (ENOMEM);
}
/* set which FIFO is opened */
sc->sc_fifo_open[USB_FIFO_RX] = fifo;
}
if (fflags & FWRITE) {
/* clear stall first */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
mtx_unlock(&sc->sc_mtx);
if (usb_fifo_alloc_buffer(fifo,
usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
ULPT_IFQ_MAXLEN)) {
return (ENOMEM);
}
/* set which FIFO is opened */
sc->sc_fifo_open[USB_FIFO_TX] = fifo;
}
sc->sc_fflags |= fflags & (FREAD | FWRITE);
return (0);
}
static void
ulpt_close(struct usb_fifo *fifo, int fflags)
{
struct ulpt_softc *sc = usb_fifo_softc(fifo);
sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
if (fflags & (FREAD | FWRITE)) {
usb_fifo_free_buffer(fifo);
}
}
static int
ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
int fflags)
{
return (ENODEV);
}
static const STRUCT_USB_HOST_ID ulpt_devs[] = {
/* Uni-directional USB printer */
{USB_IFACE_CLASS(UICLASS_PRINTER),
USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
/* Bi-directional USB printer */
{USB_IFACE_CLASS(UICLASS_PRINTER),
USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
/* 1284 USB printer */
{USB_IFACE_CLASS(UICLASS_PRINTER),
USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
};
static int
ulpt_probe(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
int error;
DPRINTFN(11, "\n");
if (uaa->usb_mode != USB_MODE_HOST)
return (ENXIO);
error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
if (error)
return (error);
return (BUS_PROBE_GENERIC);
}
static int
ulpt_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct ulpt_softc *sc = device_get_softc(dev);
struct usb_interface_descriptor *id;
int unit = device_get_unit(dev);
int error;
uint8_t iface_index = uaa->info.bIfaceIndex;
uint8_t alt_index;
DPRINTFN(11, "sc=%p\n", sc);
sc->sc_dev = dev;
sc->sc_udev = uaa->device;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
/* search through all the descriptors looking for bidir mode */
id = usbd_get_interface_descriptor(uaa->iface);
alt_index = 0xFF;
while (1) {
if (id == NULL) {
break;
}
if ((id->bDescriptorType == UDESC_INTERFACE) &&
(id->bLength >= sizeof(*id))) {
if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
break;
} else {
alt_index++;
if ((id->bInterfaceClass == UICLASS_PRINTER) &&
(id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
(id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
goto found;
}
}
}
id = (void *)usb_desc_foreach(
usbd_get_config_descriptor(uaa->device), (void *)id);
}
goto detach;
found:
DPRINTF("setting alternate "
"config number: %d\n", alt_index);
if (alt_index) {
error = usbd_set_alt_interface_index
(uaa->device, iface_index, alt_index);
if (error) {
DPRINTF("could not set alternate "
"config, error=%s\n", usbd_errstr(error));
goto detach;
}
}
sc->sc_iface_no = id->bInterfaceNumber;
error = usbd_transfer_setup(uaa->device, &iface_index,
sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
sc, &sc->sc_mtx);
if (error) {
DPRINTF("error=%s\n", usbd_errstr(error));
goto detach;
}
device_printf(sc->sc_dev, "using bi-directional mode\n");
#if 0
/*
* This code is disabled because for some mysterious reason it causes
* printing not to work. But only sometimes, and mostly with
* UHCI and less often with OHCI. *sigh*
*/
{
struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
struct usb_device_request req;
int len, alen;
req.bmRequestType = UT_READ_CLASS_INTERFACE;
req.bRequest = UR_GET_DEVICE_ID;
USETW(req.wValue, cd->bConfigurationValue);
USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
USETW(req.wLength, sizeof devinfo - 1);
error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
&alen, USB_DEFAULT_TIMEOUT);
if (error) {
device_printf(sc->sc_dev, "cannot get device id\n");
} else if (alen <= 2) {
device_printf(sc->sc_dev, "empty device id, no "
"printer connected?\n");
} else {
/* devinfo now contains an IEEE-1284 device ID */
len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
if (len > sizeof devinfo - 3)
len = sizeof devinfo - 3;
devinfo[len] = 0;
printf("%s: device id <", device_get_nameunit(sc->sc_dev));
ieee1284_print_id(devinfo + 2);
printf(">\n");
}
}
#endif
error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
&ulpt_fifo_methods, &sc->sc_fifo,
unit, -1, uaa->info.bIfaceIndex,
UID_ROOT, GID_OPERATOR, 0644);
if (error) {
goto detach;
}
error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
&unlpt_fifo_methods, &sc->sc_fifo_noreset,
unit, -1, uaa->info.bIfaceIndex,
UID_ROOT, GID_OPERATOR, 0644);
if (error) {
goto detach;
}
/* start reading of status */
mtx_lock(&sc->sc_mtx);
ulpt_watchdog(sc);
mtx_unlock(&sc->sc_mtx);
return (0);
detach:
ulpt_detach(dev);
return (ENOMEM);
}
static int
ulpt_detach(device_t dev)
{
struct ulpt_softc *sc = device_get_softc(dev);
DPRINTF("sc=%p\n", sc);
usb_fifo_detach(&sc->sc_fifo);
usb_fifo_detach(&sc->sc_fifo_noreset);
mtx_lock(&sc->sc_mtx);
usb_callout_stop(&sc->sc_watchdog);
mtx_unlock(&sc->sc_mtx);
usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
usb_callout_drain(&sc->sc_watchdog);
mtx_destroy(&sc->sc_mtx);
return (0);
}
#if 0
/* XXX This does not belong here. */
/*
* Compare two strings until the second ends.
*/
static uint8_t
ieee1284_compare(const char *a, const char *b)
{
while (1) {
if (*b == 0) {
break;
}
if (*a != *b) {
return 1;
}
b++;
a++;
}
return 0;
}
/*
* Print select parts of an IEEE 1284 device ID.
*/
void
ieee1284_print_id(char *str)
{
char *p, *q;
for (p = str - 1; p; p = strchr(p, ';')) {
p++; /* skip ';' */
if (ieee1284_compare(p, "MFG:") == 0 ||
ieee1284_compare(p, "MANUFACTURER:") == 0 ||
ieee1284_compare(p, "MDL:") == 0 ||
ieee1284_compare(p, "MODEL:") == 0) {
q = strchr(p, ';');
if (q)
printf("%.*s", (int)(q - p + 1), p);
}
}
}
#endif
static void
ulpt_watchdog(void *arg)
{
struct ulpt_softc *sc = arg;
mtx_assert(&sc->sc_mtx, MA_OWNED);
/*
* Only read status while the device is not opened, due to
* possible hardware or firmware bug in some printers.
*/
if (sc->sc_fflags == 0)
usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
usb_callout_reset(&sc->sc_watchdog,
hz, &ulpt_watchdog, sc);
}
static devclass_t ulpt_devclass;
static device_method_t ulpt_methods[] = {
DEVMETHOD(device_probe, ulpt_probe),
DEVMETHOD(device_attach, ulpt_attach),
DEVMETHOD(device_detach, ulpt_detach),
DEVMETHOD_END
};
static driver_t ulpt_driver = {
.name = "ulpt",
.methods = ulpt_methods,
.size = sizeof(struct ulpt_softc),
};
DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
MODULE_DEPEND(ulpt, usb, 1, 1, 1);
MODULE_VERSION(ulpt, 1);
USB_PNP_HOST_INFO(ulpt_devs);

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,644 @@
/* $FreeBSD$ */
/*-
* Copyright (c) 2010 Lev Serebryakov <lev@FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _UMCS7840_H_
#define _UMCS7840_H_
#define UMCS7840_MAX_PORTS 4
#define UMCS7840_READ_LENGTH 1 /* bytes */
#define UMCS7840_CTRL_TIMEOUT 500 /* ms */
/* Read/Wrtire registers vendor commands */
#define MCS7840_RDREQ 0x0d
#define MCS7840_WRREQ 0x0e
/* Read/Wrtie EEPROM values */
#define MCS7840_EEPROM_RW_WVALUE 0x0900
/*
* All these registers are documented only in full datasheet,
* which can be requested from MosChip tech support.
*/
#define MCS7840_DEV_REG_SP1 0x00 /* Options for for UART 1, R/W */
#define MCS7840_DEV_REG_CONTROL1 0x01 /* Control bits for UART 1,
* R/W */
#define MCS7840_DEV_REG_PINPONGHIGH 0x02 /* High bits of ping-pong
* register, R/W */
#define MCS7840_DEV_REG_PINPONGLOW 0x03 /* Low bits of ping-pong
* register, R/W */
/* DCRx_1 Registers goes here (see below, they are documented) */
#define MCS7840_DEV_REG_GPIO 0x07 /* GPIO_0 and GPIO_1 bits,
* undocumented, see notes
* below R/W */
#define MCS7840_DEV_REG_SP2 0x08 /* Options for for UART 2, R/W */
#define MCS7840_DEV_REG_CONTROL2 0x09 /* Control bits for UART 2,
* R/W */
#define MCS7840_DEV_REG_SP3 0x0a /* Options for for UART 3, R/W */
#define MCS7840_DEV_REG_CONTROL3 0x0b /* Control bits for UART 3,
* R/W */
#define MCS7840_DEV_REG_SP4 0x0c /* Options for for UART 4, R/W */
#define MCS7840_DEV_REG_CONTROL4 0x0d /* Control bits for UART 4,
* R/W */
#define MCS7840_DEV_REG_PLL_DIV_M 0x0e /* Pre-diviedr for PLL, R/W */
#define MCS7840_DEV_REG_UNKNOWN1 0x0f /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_PLL_DIV_N 0x10 /* Loop divider for PLL, R/W */
#define MCS7840_DEV_REG_CLOCK_MUX 0x12 /* PLL input clock & Interrupt
* endpoint control, R/W */
#define MCS7840_DEV_REG_UNKNOWN2 0x11 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_CLOCK_SELECT12 0x13 /* Clock source for ports 1 &
* 2, R/W */
#define MCS7840_DEV_REG_CLOCK_SELECT34 0x14 /* Clock source for ports 3 &
* 4, R/W */
#define MCS7840_DEV_REG_UNKNOWN3 0x15 /* NOT MENTIONED AND NOT USED */
/* DCRx_2-DCRx_4 Registers goes here (see below, they are documented) */
#define MCS7840_DEV_REG_UNKNOWN4 0x1f /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWN5 0x20 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWN6 0x21 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWN7 0x22 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWN8 0x23 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWN9 0x24 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWNA 0x25 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWNB 0x26 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWNC 0x27 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWND 0x28 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWNE 0x29 /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_UNKNOWNF 0x2a /* NOT MENTIONED AND NOT USED */
#define MCS7840_DEV_REG_MODE 0x2b /* Hardware configuration,
* R/Only */
#define MCS7840_DEV_REG_SP1_ICG 0x2c /* Inter character gap
* configuration for Port 1,
* R/W */
#define MCS7840_DEV_REG_SP2_ICG 0x2d /* Inter character gap
* configuration for Port 2,
* R/W */
#define MCS7840_DEV_REG_SP3_ICG 0x2e /* Inter character gap
* configuration for Port 3,
* R/W */
#define MCS7840_DEV_REG_SP4_ICG 0x2f /* Inter character gap
* configuration for Port 4,
* R/W */
#define MCS7840_DEV_REG_RX_SAMPLING12 0x30 /* RX sampling for ports 1 &
* 2, R/W */
#define MCS7840_DEV_REG_RX_SAMPLING34 0x31 /* RX sampling for ports 3 &
* 4, R/W */
#define MCS7840_DEV_REG_BI_FIFO_STAT1 0x32 /* Bulk-In FIFO Stat for Port
* 1, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BO_FIFO_STAT1 0x33 /* Bulk-out FIFO Stat for Port
* 1, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BI_FIFO_STAT2 0x34 /* Bulk-In FIFO Stat for Port
* 2, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BO_FIFO_STAT2 0x35 /* Bulk-out FIFO Stat for Port
* 2, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BI_FIFO_STAT3 0x36 /* Bulk-In FIFO Stat for Port
* 3, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BO_FIFO_STAT3 0x37 /* Bulk-out FIFO Stat for Port
* 3, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BI_FIFO_STAT4 0x38 /* Bulk-In FIFO Stat for Port
* 4, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_BO_FIFO_STAT4 0x39 /* Bulk-out FIFO Stat for Port
* 4, contains number of
* available bytes, R/Only */
#define MCS7840_DEV_REG_ZERO_PERIOD1 0x3a /* Period between zero out
* frames for Port 1, R/W */
#define MCS7840_DEV_REG_ZERO_PERIOD2 0x3b /* Period between zero out
* frames for Port 1, R/W */
#define MCS7840_DEV_REG_ZERO_PERIOD3 0x3c /* Period between zero out
* frames for Port 1, R/W */
#define MCS7840_DEV_REG_ZERO_PERIOD4 0x3d /* Period between zero out
* frames for Port 1, R/W */
#define MCS7840_DEV_REG_ZERO_ENABLE 0x3e /* Enable/disable of zero out
* frames, R/W */
#define MCS7840_DEV_REG_THR_VAL_LOW1 0x3f /* Low 8 bits of threshold
* value for Bulk-Out for Port
* 1, R/W */
#define MCS7840_DEV_REG_THR_VAL_HIGH1 0x40 /* High 1 bit of threshold
* value for Bulk-Out and
* enable flag for Port 1, R/W */
#define MCS7840_DEV_REG_THR_VAL_LOW2 0x41 /* Low 8 bits of threshold
* value for Bulk-Out for Port
* 2, R/W */
#define MCS7840_DEV_REG_THR_VAL_HIGH2 0x42 /* High 1 bit of threshold
* value for Bulk-Out and
* enable flag for Port 2, R/W */
#define MCS7840_DEV_REG_THR_VAL_LOW3 0x43 /* Low 8 bits of threshold
* value for Bulk-Out for Port
* 3, R/W */
#define MCS7840_DEV_REG_THR_VAL_HIGH3 0x44 /* High 1 bit of threshold
* value for Bulk-Out and
* enable flag for Port 3, R/W */
#define MCS7840_DEV_REG_THR_VAL_LOW4 0x45 /* Low 8 bits of threshold
* value for Bulk-Out for Port
* 4, R/W */
#define MCS7840_DEV_REG_THR_VAL_HIGH4 0x46 /* High 1 bit of threshold
* value for Bulk-Out and
* enable flag for Port 4, R/W */
/* Bits for SPx registers */
#define MCS7840_DEV_SPx_LOOP_PIPES 0x01 /* Loop Bulk-Out FIFO to the
* Bulk-In FIFO, default = 0 */
#define MCS7840_DEV_SPx_SKIP_ERR_DATA 0x02 /* Drop data bytes from UART,
* which were recevied with
* errors, default = 0 */
#define MCS7840_DEV_SPx_RESET_OUT_FIFO 0x04 /* Reset Bulk-Out FIFO */
#define MCS7840_DEV_SPx_RESET_IN_FIFO 0x08 /* Reset Bulk-In FIFO */
#define MCS7840_DEV_SPx_CLOCK_MASK 0x70 /* Mask to extract Baud CLK
* source */
#define MCS7840_DEV_SPx_CLOCK_X1 0x00 /* CLK = 1.8432Mhz, max speed
* = 115200 bps, default */
#define MCS7840_DEV_SPx_CLOCK_X2 0x10 /* CLK = 3.6864Mhz, max speed
* = 230400 bps */
#define MCS7840_DEV_SPx_CLOCK_X35 0x20 /* CLK = 6.4512Mhz, max speed
* = 403200 bps */
#define MCS7840_DEV_SPx_CLOCK_X4 0x30 /* CLK = 7.3728Mhz, max speed
* = 460800 bps */
#define MCS7840_DEV_SPx_CLOCK_X7 0x40 /* CLK = 12.9024Mhz, max speed
* = 806400 bps */
#define MCS7840_DEV_SPx_CLOCK_X8 0x50 /* CLK = 14.7456Mhz, max speed
* = 921600 bps */
#define MCS7840_DEV_SPx_CLOCK_24MHZ 0x60 /* CLK = 24.0000Mhz, max speed
* = 1.5 Mbps */
#define MCS7840_DEV_SPx_CLOCK_48MHZ 0x70 /* CLK = 48.0000Mhz, max speed
* = 3.0 Mbps */
#define MCS7840_DEV_SPx_CLOCK_SHIFT 4 /* Value 0..7 can be shifted
* to get clock value */
#define MCS7840_DEV_SPx_UART_RESET 0x80 /* Reset UART */
/* Bits for CONTROLx registers */
#define MCS7840_DEV_CONTROLx_HWFC 0x01 /* Enable hardware flow
* control (when power
* down? It is unclear
* in documents),
* default = 0 */
#define MCS7840_DEV_CONTROLx_UNUNSED1 0x02 /* Reserved */
#define MCS7840_DEV_CONTROLx_CTS_ENABLE 0x04 /* CTS changes are
* translated to MSR,
* default = 0 */
#define MCS7840_DEV_CONTROLx_UNUSED2 0x08 /* Reserved for ports
* 2,3,4 */
#define MCS7840_DEV_CONTROL1_DRIVER_DONE 0x08 /* USB enumerating is
* finished, USB
* enumeration memory
* can be used as FIFOs */
#define MCS7840_DEV_CONTROLx_RX_NEGATE 0x10 /* Negate RX input,
* works for IrDA mode
* only, default = 0 */
#define MCS7840_DEV_CONTROLx_RX_DISABLE 0x20 /* Disable RX logic,
* works only for
* RS-232/RS-485 mode,
* default = 0 */
#define MCS7840_DEV_CONTROLx_FSM_CONTROL 0x40 /* Disable RX FSM when
* TX is in progress,
* works for IrDA mode
* only, default = 0 */
#define MCS7840_DEV_CONTROLx_UNUSED3 0x80 /* Reserved */
/*
* Bits for PINPONGx registers
* These registers control how often two input buffers
* for Bulk-In FIFOs are swapped. One of buffers is used
* for USB trnasfer, other for receiving data from UART.
* Exact meaning of 15 bit value in these registers is unknown
*/
#define MCS7840_DEV_PINPONGHIGH_MULT 128 /* Only 7 bits in PINPONGLOW
* register */
#define MCS7840_DEV_PINPONGLOW_BITS 7 /* Only 7 bits in PINPONGLOW
* register */
/*
* THIS ONE IS UNDOCUMENTED IN FULL DATASHEET, but e-mail from tech support
* confirms, that it is register for GPIO_0 and GPIO_1 data input/output.
* Chips has 2 GPIO, but first one (lower bit) MUST be used by device
* authors as "number of port" indicator, grounded (0) for two-port
* devices and pulled-up to 1 for 4-port devices.
*/
#define MCS7840_DEV_GPIO_4PORTS 0x01 /* Device has 4 ports
* configured */
#define MCS7840_DEV_GPIO_GPIO_0 0x01 /* The same as above */
#define MCS7840_DEV_GPIO_GPIO_1 0x02 /* GPIO_1 data */
/*
* Constants for PLL dividers
* Ouptut frequency of PLL is:
* Fout = (N/M) * Fin.
* Default PLL input frequency Fin is 12Mhz (on-chip).
*/
#define MCS7840_DEV_PLL_DIV_M_BITS 6 /* Number of useful bits for M
* divider */
#define MCS7840_DEV_PLL_DIV_M_MASK 0x3f /* Mask for M divider */
#define MCS7840_DEV_PLL_DIV_M_MIN 1 /* Minimum value for M, 0 is
* forbidden */
#define MCS7840_DEV_PLL_DIV_M_DEF 1 /* Default value for M */
#define MCS7840_DEV_PLL_DIV_M_MAX 63 /* Maximum value for M */
#define MCS7840_DEV_PLL_DIV_N_BITS 6 /* Number of useful bits for N
* divider */
#define MCS7840_DEV_PLL_DIV_N_MASK 0x3f /* Mask for N divider */
#define MCS7840_DEV_PLL_DIV_N_MIN 1 /* Minimum value for N, 0 is
* forbidden */
#define MCS7840_DEV_PLL_DIV_N_DEF 8 /* Default value for N */
#define MCS7840_DEV_PLL_DIV_N_MAX 63 /* Maximum value for N */
/* Bits for CLOCK_MUX register */
#define MCS7840_DEV_CLOCK_MUX_INPUTMASK 0x03 /* Mask to extract PLL clock
* input */
#define MCS7840_DEV_CLOCK_MUX_IN12MHZ 0x00 /* 12Mhz PLL input, default */
#define MCS7840_DEV_CLOCK_MUX_INEXTRN 0x01 /* External (device-depended)
* PLL input */
#define MCS7840_DEV_CLOCK_MUX_INRSV1 0x02 /* Reserved */
#define MCS7840_DEV_CLOCK_MUX_INRSV2 0x03 /* Reserved */
#define MCS7840_DEV_CLOCK_MUX_PLLHIGH 0x04 /* 0 = PLL Output is
* 20MHz-100MHz (default), 1 =
* 100MHz-300MHz range */
#define MCS7840_DEV_CLOCK_MUX_INTRFIFOS 0x08 /* Enable additional 8 bytes
* fro Interrupt USB pipe with
* USB FIFOs statuses, default
* = 0 */
#define MCS7840_DEV_CLOCK_MUX_RESERVED1 0x10 /* Unused */
#define MCS7840_DEV_CLOCK_MUX_RESERVED2 0x20 /* Unused */
#define MCS7840_DEV_CLOCK_MUX_RESERVED3 0x40 /* Unused */
#define MCS7840_DEV_CLOCK_MUX_RESERVED4 0x80 /* Unused */
/* Bits for CLOCK_SELECTxx registers */
#define MCS7840_DEV_CLOCK_SELECT1_MASK 0x07 /* Bits for port 1 in
* CLOCK_SELECT12 */
#define MCS7840_DEV_CLOCK_SELECT1_SHIFT 0 /* Shift for port 1in
* CLOCK_SELECT12 */
#define MCS7840_DEV_CLOCK_SELECT2_MASK 0x38 /* Bits for port 2 in
* CLOCK_SELECT12 */
#define MCS7840_DEV_CLOCK_SELECT2_SHIFT 3 /* Shift for port 2 in
* CLOCK_SELECT12 */
#define MCS7840_DEV_CLOCK_SELECT3_MASK 0x07 /* Bits for port 3 in
* CLOCK_SELECT23 */
#define MCS7840_DEV_CLOCK_SELECT3_SHIFT 0 /* Shift for port 3 in
* CLOCK_SELECT23 */
#define MCS7840_DEV_CLOCK_SELECT4_MASK 0x38 /* Bits for port 4 in
* CLOCK_SELECT23 */
#define MCS7840_DEV_CLOCK_SELECT4_SHIFT 3 /* Shift for port 4 in
* CLOCK_SELECT23 */
#define MCS7840_DEV_CLOCK_SELECT_STD 0x00 /* STANDARD baudrate derived
* from 96Mhz, default for all
* ports */
#define MCS7840_DEV_CLOCK_SELECT_30MHZ 0x01 /* 30Mhz */
#define MCS7840_DEV_CLOCK_SELECT_96MHZ 0x02 /* 96Mhz direct */
#define MCS7840_DEV_CLOCK_SELECT_120MHZ 0x03 /* 120Mhz */
#define MCS7840_DEV_CLOCK_SELECT_PLL 0x04 /* PLL output (see for M and N
* dividers) */
#define MCS7840_DEV_CLOCK_SELECT_EXT 0x05 /* External clock input
* (device-dependend) */
#define MCS7840_DEV_CLOCK_SELECT_RES1 0x06 /* Unused */
#define MCS7840_DEV_CLOCK_SELECT_RES2 0x07 /* Unused */
/* Bits for MODE register */
#define MCS7840_DEV_MODE_RESERVED1 0x01 /* Unused */
#define MCS7840_DEV_MODE_RESET 0x02 /* 0: RESET = Active High
* (default), 1: Reserved (?) */
#define MCS7840_DEV_MODE_SER_PRSNT 0x04 /* 0: Reserved, 1: Do not use
* hardocded values (default)
* (?) */
#define MCS7840_DEV_MODE_PLLBYPASS 0x08 /* 1: PLL output is bypassed,
* default = 0 */
#define MCS7840_DEV_MODE_PORBYPASS 0x10 /* 1: Power-On Reset is
* bypassed, default = 0 */
#define MCS7840_DEV_MODE_SELECT24S 0x20 /* 0: 4 Serial Ports / IrDA
* active, 1: 2 Serial Ports /
* IrDA active */
#define MCS7840_DEV_MODE_EEPROMWR 0x40 /* EEPROM write is enabled,
* default */
#define MCS7840_DEV_MODE_IRDA 0x80 /* IrDA mode is activated
* (could be turned on),
* default */
/* Bits for SPx ICG */
#define MCS7840_DEV_SPx_ICG_DEF 0x24 /* All 8 bits is used as
* number of BAUD clocks of
* pause */
/*
* Bits for RX_SAMPLINGxx registers
* These registers control when bit value will be sampled within
* the baud period.
* 0 is very beginning of period, 15 is very end, 7 is the middle.
*/
#define MCS7840_DEV_RX_SAMPLING1_MASK 0x0f /* Bits for port 1 in
* RX_SAMPLING12 */
#define MCS7840_DEV_RX_SAMPLING1_SHIFT 0 /* Shift for port 1in
* RX_SAMPLING12 */
#define MCS7840_DEV_RX_SAMPLING2_MASK 0xf0 /* Bits for port 2 in
* RX_SAMPLING12 */
#define MCS7840_DEV_RX_SAMPLING2_SHIFT 4 /* Shift for port 2 in
* RX_SAMPLING12 */
#define MCS7840_DEV_RX_SAMPLING3_MASK 0x0f /* Bits for port 3 in
* RX_SAMPLING23 */
#define MCS7840_DEV_RX_SAMPLING3_SHIFT 0 /* Shift for port 3 in
* RX_SAMPLING23 */
#define MCS7840_DEV_RX_SAMPLING4_MASK 0xf0 /* Bits for port 4 in
* RX_SAMPLING23 */
#define MCS7840_DEV_RX_SAMPLING4_SHIFT 4 /* Shift for port 4 in
* RX_SAMPLING23 */
#define MCS7840_DEV_RX_SAMPLINGx_MIN 0 /* Max for any RX Sampling */
#define MCS7840_DEV_RX_SAMPLINGx_DEF 7 /* Default for any RX
* Sampling, center of period */
#define MCS7840_DEV_RX_SAMPLINGx_MAX 15 /* Min for any RX Sampling */
/* Bits for ZERO_PERIODx */
#define MCS7840_DEV_ZERO_PERIODx_DEF 20 /* Number of Bulk-in requests
* befor sending zero-sized
* reply */
/* Bits for ZERO_ENABLE */
#define MCS7840_DEV_ZERO_ENABLE_PORT1 0x01 /* Enable of sending
* zero-sized replies for port
* 1, default */
#define MCS7840_DEV_ZERO_ENABLE_PORT2 0x02 /* Enable of sending
* zero-sized replies for port
* 2, default */
#define MCS7840_DEV_ZERO_ENABLE_PORT3 0x04 /* Enable of sending
* zero-sized replies for port
* 3, default */
#define MCS7840_DEV_ZERO_ENABLE_PORT4 0x08 /* Enable of sending
* zero-sized replies for port
* 4, default */
/* Bits for THR_VAL_HIGHx */
#define MCS7840_DEV_THR_VAL_HIGH_MASK 0x01 /* Only one bit is used */
#define MCS7840_DEV_THR_VAL_HIGH_MUL 256 /* This one bit is means "256" */
#define MCS7840_DEV_THR_VAL_HIGH_SHIFT 8 /* This one bit is means "256" */
#define MCS7840_DEV_THR_VAL_HIGH_ENABLE 0x80 /* Enable threshold */
/* These are documented in "public" datasheet */
#define MCS7840_DEV_REG_DCR0_1 0x04 /* Device contol register 0 for Port
* 1, R/W */
#define MCS7840_DEV_REG_DCR1_1 0x05 /* Device contol register 1 for Port
* 1, R/W */
#define MCS7840_DEV_REG_DCR2_1 0x06 /* Device contol register 2 for Port
* 1, R/W */
#define MCS7840_DEV_REG_DCR0_2 0x16 /* Device contol register 0 for Port
* 2, R/W */
#define MCS7840_DEV_REG_DCR1_2 0x17 /* Device contol register 1 for Port
* 2, R/W */
#define MCS7840_DEV_REG_DCR2_2 0x18 /* Device contol register 2 for Port
* 2, R/W */
#define MCS7840_DEV_REG_DCR0_3 0x19 /* Device contol register 0 for Port
* 3, R/W */
#define MCS7840_DEV_REG_DCR1_3 0x1a /* Device contol register 1 for Port
* 3, R/W */
#define MCS7840_DEV_REG_DCR2_3 0x1b /* Device contol register 2 for Port
* 3, R/W */
#define MCS7840_DEV_REG_DCR0_4 0x1c /* Device contol register 0 for Port
* 4, R/W */
#define MCS7840_DEV_REG_DCR1_4 0x1d /* Device contol register 1 for Port
* 4, R/W */
#define MCS7840_DEV_REG_DCR2_4 0x1e /* Device contol register 2 for Port
* 4, R/W */
/* Bits of DCR0 registers, documented in datasheet */
#define MCS7840_DEV_DCR0_PWRSAVE 0x01 /* Shutdown transiver
* when USB Suspend is
* engaged, default = 1 */
#define MCS7840_DEV_DCR0_RESERVED1 0x02 /* Unused */
#define MCS7840_DEV_DCR0_GPIO_MODE_MASK 0x0c /* GPIO Mode bits, WORKS
* ONLY FOR PORT 1 */
#define MCS7840_DEV_DCR0_GPIO_MODE_IN 0x00 /* GPIO Mode - Input
* (0b00), WORKS ONLY
* FOR PORT 1 */
#define MCS7840_DEV_DCR0_GPIO_MODE_OUT 0x08 /* GPIO Mode - Input
* (0b10), WORKS ONLY
* FOR PORT 1 */
#define MCS7840_DEV_DCR0_RTS_ACTIVE_HIGH 0x10 /* RTS Active is HIGH,
* default = 0 (low) */
#define MCS7840_DEV_DCR0_RTS_AUTO 0x20 /* RTS is controlled by
* state of TX buffer,
* default = 0
* (controlled by MCR) */
#define MCS7840_DEV_DCR0_IRDA 0x40 /* IrDA mode */
#define MCS7840_DEV_DCR0_RESERVED2 0x80 /* Unused */
/* Bits of DCR1 registers, documented in datasheet */
#define MCS7840_DEV_DCR1_GPIO_CURRENT_MASK 0x03 /* Mask to extract GPIO
* current value, WORKS
* ONLY FOR PORT 1 */
#define MCS7840_DEV_DCR1_GPIO_CURRENT_6MA 0x00 /* GPIO output current
* 6mA, WORKS ONLY FOR
* PORT 1 */
#define MCS7840_DEV_DCR1_GPIO_CURRENT_8MA 0x01 /* GPIO output current
* 8mA, defauilt, WORKS
* ONLY FOR PORT 1 */
#define MCS7840_DEV_DCR1_GPIO_CURRENT_10MA 0x02 /* GPIO output current
* 10mA, WORKS ONLY FOR
* PORT 1 */
#define MCS7840_DEV_DCR1_GPIO_CURRENT_12MA 0x03 /* GPIO output current
* 12mA, WORKS ONLY FOR
* PORT 1 */
#define MCS7840_DEV_DCR1_UART_CURRENT_MASK 0x0c /* Mask to extract UART
* signals current value */
#define MCS7840_DEV_DCR1_UART_CURRENT_6MA 0x00 /* UART output current
* 6mA */
#define MCS7840_DEV_DCR1_UART_CURRENT_8MA 0x04 /* UART output current
* 8mA, defauilt */
#define MCS7840_DEV_DCR1_UART_CURRENT_10MA 0x08 /* UART output current
* 10mA */
#define MCS7840_DEV_DCR1_UART_CURRENT_12MA 0x0c /* UART output current
* 12mA */
#define MCS7840_DEV_DCR1_WAKEUP_DISABLE 0x10 /* Disable Remote USB
* Wakeup */
#define MCS7840_DEV_DCR1_PLLPWRDOWN_DISABLE 0x20 /* Disable PLL power
* down when not needed,
* WORKS ONLY FOR PORT 1 */
#define MCS7840_DEV_DCR1_LONG_INTERRUPT 0x40 /* Enable 13 bytes of
* interrupt data, with
* FIFO statistics,
* WORKS ONLY FOR PORT 1 */
#define MCS7840_DEV_DCR1_RESERVED1 0x80 /* Unused */
/*
* Bits of DCR2 registers, documented in datasheet
* Wakeup will work only if DCR0_IRDA = 0 (RS-xxx mode) and
* DCR1_WAKEUP_DISABLE = 0 (wakeup enabled).
*/
#define MCS7840_DEV_DCR2_WAKEUP_CTS 0x01 /* Wakeup on CTS change,
* default = 0 */
#define MCS7840_DEV_DCR2_WAKEUP_DCD 0x02 /* Wakeup on DCD change,
* default = 0 */
#define MCS7840_DEV_DCR2_WAKEUP_RI 0x04 /* Wakeup on RI change,
* default = 1 */
#define MCS7840_DEV_DCR2_WAKEUP_DSR 0x08 /* Wakeup on DSR change,
* default = 0 */
#define MCS7840_DEV_DCR2_WAKEUP_RXD 0x10 /* Wakeup on RX Data change,
* default = 0 */
#define MCS7840_DEV_DCR2_WAKEUP_RESUME 0x20 /* Wakeup issues RESUME
* signal, DISCONNECT
* otherwise, default = 1 */
#define MCS7840_DEV_DCR2_RESERVED1 0x40 /* Unused */
#define MCS7840_DEV_DCR2_SHDN_POLARITY 0x80 /* 0: Pin 12 Active Low, 1:
* Pin 12 Active High, default
* = 0 */
/* Interrupt endpoint bytes & bits */
#define MCS7840_IEP_FIFO_STATUS_INDEX 5
/*
* Thesse can be calculated as "1 << portnumber" for Bulk-out and
* "1 << (portnumber+1)" for Bulk-in
*/
#define MCS7840_IEP_BO_PORT1_HASDATA 0x01
#define MCS7840_IEP_BI_PORT1_HASDATA 0x02
#define MCS7840_IEP_BO_PORT2_HASDATA 0x04
#define MCS7840_IEP_BI_PORT2_HASDATA 0x08
#define MCS7840_IEP_BO_PORT3_HASDATA 0x10
#define MCS7840_IEP_BI_PORT3_HASDATA 0x20
#define MCS7840_IEP_BO_PORT4_HASDATA 0x40
#define MCS7840_IEP_BI_PORT4_HASDATA 0x80
/* Documented UART registers (fully compatible with 16550 UART) */
#define MCS7840_UART_REG_THR 0x00 /* Transmitter Holding
* Register W/Only */
#define MCS7840_UART_REG_RHR 0x00 /* Receiver Holding Register
* R/Only */
#define MCS7840_UART_REG_IER 0x01 /* Interrupt enable register -
* R/W */
#define MCS7840_UART_REG_FCR 0x02 /* FIFO Control register -
* W/Only */
#define MCS7840_UART_REG_ISR 0x02 /* Interrupt Status Registter
* R/Only */
#define MCS7840_UART_REG_LCR 0x03 /* Line control register R/W */
#define MCS7840_UART_REG_MCR 0x04 /* Modem control register R/W */
#define MCS7840_UART_REG_LSR 0x05 /* Line status register R/Only */
#define MCS7840_UART_REG_MSR 0x06 /* Modem status register
* R/Only */
#define MCS7840_UART_REG_SCRATCHPAD 0x07 /* Scratch pad register */
#define MCS7840_UART_REG_DLL 0x00 /* Low bits of BAUD divider */
#define MCS7840_UART_REG_DLM 0x01 /* High bits of BAUD divider */
/* IER bits */
#define MCS7840_UART_IER_RXREADY 0x01 /* RX Ready interrumpt mask */
#define MCS7840_UART_IER_TXREADY 0x02 /* TX Ready interrumpt mask */
#define MCS7840_UART_IER_RXSTAT 0x04 /* RX Status interrumpt mask */
#define MCS7840_UART_IER_MODEM 0x08 /* Modem status change
* interrumpt mask */
#define MCS7840_UART_IER_SLEEP 0x10 /* SLEEP enable */
/* FCR bits */
#define MCS7840_UART_FCR_ENABLE 0x01 /* Enable FIFO */
#define MCS7840_UART_FCR_FLUSHRHR 0x02 /* Flush RHR and FIFO */
#define MCS7840_UART_FCR_FLUSHTHR 0x04 /* Flush THR and FIFO */
#define MCS7840_UART_FCR_RTLMASK 0xa0 /* Mask to select RHR
* Interrupt Trigger level */
#define MCS7840_UART_FCR_RTL_1_1 0x00 /* L1 = 1, L2 = 1 */
#define MCS7840_UART_FCR_RTL_1_4 0x40 /* L1 = 1, L2 = 4 */
#define MCS7840_UART_FCR_RTL_1_8 0x80 /* L1 = 1, L2 = 8 */
#define MCS7840_UART_FCR_RTL_1_14 0xa0 /* L1 = 1, L2 = 14 */
/* ISR bits */
#define MCS7840_UART_ISR_NOPENDING 0x01 /* No interrupt pending */
#define MCS7840_UART_ISR_INTMASK 0x3f /* Mask to select interrupt
* source */
#define MCS7840_UART_ISR_RXERR 0x06 /* Recevir error */
#define MCS7840_UART_ISR_RXHASDATA 0x04 /* Recevier has data */
#define MCS7840_UART_ISR_RXTIMEOUT 0x0c /* Recevier timeout */
#define MCS7840_UART_ISR_TXEMPTY 0x02 /* Transmitter empty */
#define MCS7840_UART_ISR_MSCHANGE 0x00 /* Modem status change */
/* LCR bits */
#define MCS7840_UART_LCR_DATALENMASK 0x03 /* Mask for data length */
#define MCS7840_UART_LCR_DATALEN5 0x00 /* 5 data bits */
#define MCS7840_UART_LCR_DATALEN6 0x01 /* 6 data bits */
#define MCS7840_UART_LCR_DATALEN7 0x02 /* 7 data bits */
#define MCS7840_UART_LCR_DATALEN8 0x03 /* 8 data bits */
#define MCS7840_UART_LCR_STOPBMASK 0x04 /* Mask for stop bits */
#define MCS7840_UART_LCR_STOPB1 0x00 /* 1 stop bit in any case */
#define MCS7840_UART_LCR_STOPB2 0x04 /* 1.5-2 stop bits depends on
* data length */
#define MCS7840_UART_LCR_PARITYMASK 0x38 /* Mask for all parity data */
#define MCS7840_UART_LCR_PARITYON 0x08 /* Parity ON/OFF - ON */
#define MCS7840_UART_LCR_PARITYODD 0x00 /* Parity Odd */
#define MCS7840_UART_LCR_PARITYEVEN 0x10 /* Parity Even */
#define MCS7840_UART_LCR_PARITYODD 0x00 /* Parity Odd */
#define MCS7840_UART_LCR_PARITYFORCE 0x20 /* Force parity odd/even */
#define MCS7840_UART_LCR_BREAK 0x40 /* Send BREAK */
#define MCS7840_UART_LCR_DIVISORS 0x80 /* Map DLL/DLM instead of
* xHR/IER */
/* LSR bits */
#define MCS7840_UART_LSR_RHRAVAIL 0x01 /* Data available for read */
#define MCS7840_UART_LSR_RHROVERRUN 0x02 /* Data FIFO/register overflow */
#define MCS7840_UART_LSR_PARITYERR 0x04 /* Parity error */
#define MCS7840_UART_LSR_FRAMEERR 0x10 /* Framing error */
#define MCS7840_UART_LSR_BREAKERR 0x20 /* BREAK signal received */
#define MCS7840_UART_LSR_THREMPTY 0x40 /* THR register is empty,
* ready for transmit */
#define MCS7840_UART_LSR_HASERR 0x80 /* Has error in receiver FIFO */
/* MCR bits */
#define MCS7840_UART_MCR_DTR 0x01 /* Force DTR to be active
* (low) */
#define MCS7840_UART_MCR_RTS 0x02 /* Force RTS to be active
* (low) */
#define MCS7840_UART_MCR_IE 0x04 /* Enable interrupts (from
* code, not documented) */
#define MCS7840_UART_MCR_LOOPBACK 0x10 /* Enable local loopback test
* mode */
#define MCS7840_UART_MCR_CTSRTS 0x20 /* Enable CTS/RTS flow control
* in 550 (FIFO) mode */
#define MCS7840_UART_MCR_DTRDSR 0x40 /* Enable DTR/DSR flow control
* in 550 (FIFO) mode */
#define MCS7840_UART_MCR_DCD 0x80 /* Enable DCD flow control in
* 550 (FIFO) mode */
/* MSR bits */
#define MCS7840_UART_MSR_DELTACTS 0x01 /* CTS was changed since last
* read */
#define MCS7840_UART_MSR_DELTADSR 0x02 /* DSR was changed since last
* read */
#define MCS7840_UART_MSR_DELTARI 0x04 /* RI was changed from low to
* high since last read */
#define MCS7840_UART_MSR_DELTADCD 0x08 /* DCD was changed since last
* read */
#define MCS7840_UART_MSR_NEGCTS 0x10 /* Negated CTS signal */
#define MCS7840_UART_MSR_NEGDSR 0x20 /* Negated DSR signal */
#define MCS7840_UART_MSR_NEGRI 0x40 /* Negated RI signal */
#define MCS7840_UART_MSR_NEGDCD 0x80 /* Negated DCD signal */
/* SCRATCHPAD bits */
#define MCS7840_UART_SCRATCHPAD_RS232 0x00 /* RS-485 disabled */
#define MCS7840_UART_SCRATCHPAD_RS485_DTRRX 0x80 /* RS-485 mode, DTR High
* = RX */
#define MCS7840_UART_SCRATCHPAD_RS485_DTRTX 0xc0 /* RS-485 mode, DTR High
* = TX */
#define MCS7840_CONFIG_INDEX 0
#define MCS7840_IFACE_INDEX 0
#endif

View File

@ -0,0 +1,683 @@
#include <machine/rtems-bsd-kernel-space.h>
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* Copyright (c) 2003 Scott Long
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
* Based on the superb documentation from the linux mct_u232 driver by
* Wolfgang Grandeggar <wolfgang@cec.ch>.
* This device smells a lot like the Belkin F5U103, except that it has
* suffered some mild brain-damage. This driver is based off of the ubsa.c
* driver from Alexander Kabaev <kan@FreeBSD.org>. Merging the two together
* might be useful, though the subtle differences might lead to lots of
* #ifdef's.
*/
/*
* NOTE: all function names beginning like "umct_cfg_" can only
* be called from within the config thread function !
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR usb_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
/* The UMCT advertises the standard 8250 UART registers */
#define UMCT_GET_MSR 2 /* Get Modem Status Register */
#define UMCT_GET_MSR_SIZE 1
#define UMCT_GET_LCR 6 /* Get Line Control Register */
#define UMCT_GET_LCR_SIZE 1
#define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */
#define UMCT_SET_BAUD_SIZE 4
#define UMCT_SET_LCR 7 /* Set Line Control Register */
#define UMCT_SET_LCR_SIZE 1
#define UMCT_SET_MCR 10 /* Set Modem Control Register */
#define UMCT_SET_MCR_SIZE 1
#define UMCT_MSR_CTS_CHG 0x01
#define UMCT_MSR_DSR_CHG 0x02
#define UMCT_MSR_RI_CHG 0x04
#define UMCT_MSR_CD_CHG 0x08
#define UMCT_MSR_CTS 0x10
#define UMCT_MSR_RTS 0x20
#define UMCT_MSR_RI 0x40
#define UMCT_MSR_CD 0x80
#define UMCT_INTR_INTERVAL 100
#define UMCT_IFACE_INDEX 0
#define UMCT_CONFIG_INDEX 0
enum {
UMCT_BULK_DT_WR,
UMCT_BULK_DT_RD,
UMCT_INTR_DT_RD,
UMCT_N_TRANSFER,
};
struct umct_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_device *sc_udev;
struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
struct mtx sc_mtx;
uint32_t sc_unit;
uint16_t sc_obufsize;
uint8_t sc_lsr;
uint8_t sc_msr;
uint8_t sc_lcr;
uint8_t sc_mcr;
uint8_t sc_iface_no;
uint8_t sc_swap_cb;
};
/* prototypes */
static device_probe_t umct_probe;
static device_attach_t umct_attach;
static device_detach_t umct_detach;
static void umct_free_softc(struct umct_softc *);
static usb_callback_t umct_intr_callback;
static usb_callback_t umct_intr_callback_sub;
static usb_callback_t umct_read_callback;
static usb_callback_t umct_read_callback_sub;
static usb_callback_t umct_write_callback;
static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
uint16_t len, uint32_t value);
static void umct_free(struct ucom_softc *);
static void umct_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void umct_cfg_set_break(struct ucom_softc *, uint8_t);
static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void umct_cfg_set_rts(struct ucom_softc *, uint8_t);
static uint8_t umct_calc_baud(uint32_t);
static int umct_pre_param(struct ucom_softc *, struct termios *);
static void umct_cfg_param(struct ucom_softc *, struct termios *);
static void umct_start_read(struct ucom_softc *);
static void umct_stop_read(struct ucom_softc *);
static void umct_start_write(struct ucom_softc *);
static void umct_stop_write(struct ucom_softc *);
static void umct_poll(struct ucom_softc *ucom);
static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
[UMCT_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = 0, /* use wMaxPacketSize */
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &umct_write_callback,
},
[UMCT_BULK_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &umct_read_callback,
.ep_index = 0, /* first interrupt endpoint */
},
[UMCT_INTR_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &umct_intr_callback,
.ep_index = 1, /* second interrupt endpoint */
},
};
static const struct ucom_callback umct_callback = {
.ucom_cfg_get_status = &umct_cfg_get_status,
.ucom_cfg_set_dtr = &umct_cfg_set_dtr,
.ucom_cfg_set_rts = &umct_cfg_set_rts,
.ucom_cfg_set_break = &umct_cfg_set_break,
.ucom_cfg_param = &umct_cfg_param,
.ucom_pre_param = &umct_pre_param,
.ucom_start_read = &umct_start_read,
.ucom_stop_read = &umct_stop_read,
.ucom_start_write = &umct_start_write,
.ucom_stop_write = &umct_stop_write,
.ucom_poll = &umct_poll,
.ucom_free = &umct_free,
};
static const STRUCT_USB_HOST_ID umct_devs[] = {
{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
};
static device_method_t umct_methods[] = {
DEVMETHOD(device_probe, umct_probe),
DEVMETHOD(device_attach, umct_attach),
DEVMETHOD(device_detach, umct_detach),
DEVMETHOD_END
};
static devclass_t umct_devclass;
static driver_t umct_driver = {
.name = "umct",
.methods = umct_methods,
.size = sizeof(struct umct_softc),
};
DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
MODULE_DEPEND(umct, ucom, 1, 1, 1);
MODULE_DEPEND(umct, usb, 1, 1, 1);
MODULE_VERSION(umct, 1);
USB_PNP_HOST_INFO(umct_devs);
static int
umct_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 != UMCT_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
}
static int
umct_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct umct_softc *sc = device_get_softc(dev);
int32_t error;
uint16_t maxp;
uint8_t iface_index;
sc->sc_udev = uaa->device;
sc->sc_unit = device_get_unit(dev);
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_iface_no = uaa->info.bIfaceNum;
iface_index = UMCT_IFACE_INDEX;
error = usbd_transfer_setup(uaa->device, &iface_index,
sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
device_printf(dev, "allocating USB "
"transfers failed\n");
goto detach;
}
/*
* The real bulk-in endpoint is also marked as an interrupt.
* The only way to differentiate it from the real interrupt
* endpoint is to look at the wMaxPacketSize field.
*/
maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
if (maxp == 0x2) {
/* guessed wrong - switch around endpoints */
struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
sc->sc_swap_cb = 1;
}
sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
if (sc->sc_obufsize > 16) {
sc->sc_obufsize = 16;
}
}
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&umct_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0); /* success */
detach:
umct_detach(dev);
return (ENXIO); /* failure */
}
static int
umct_detach(device_t dev)
{
struct umct_softc *sc = device_get_softc(dev);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
device_claim_softc(dev);
umct_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(umct);
static void
umct_free_softc(struct umct_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
umct_free(struct ucom_softc *ucom)
{
umct_free_softc(ucom->sc_parent);
}
static void
umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
uint16_t len, uint32_t value)
{
struct usb_device_request req;
usb_error_t err;
uint8_t temp[4];
if (len > 4)
len = 4;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = request;
USETW(req.wValue, 0);
req.wIndex[0] = sc->sc_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, len);
USETDW(temp, value);
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, temp, 0, 1000);
if (err) {
DPRINTFN(0, "device request failed, err=%s "
"(ignored)\n", usbd_errstr(err));
}
return;
}
static void
umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
{
struct umct_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[2];
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen < 2) {
DPRINTF("too short message\n");
goto tr_setup;
}
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, sizeof(buf));
/*
* MSR bits need translation from ns16550 to SER_* values.
* LSR bits are ns16550 in hardware and ucom.
*/
sc->sc_msr = 0;
if (buf[0] & UMCT_MSR_CTS)
sc->sc_msr |= SER_CTS;
if (buf[0] & UMCT_MSR_CD)
sc->sc_msr |= SER_DCD;
if (buf[0] & UMCT_MSR_RI)
sc->sc_msr |= SER_RI;
if (buf[0] & UMCT_MSR_RTS)
sc->sc_msr |= SER_DSR;
sc->sc_lsr = buf[1];
ucom_status_change(&sc->sc_ucom);
/* FALLTHROUGH */
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct umct_softc *sc = ucom->sc_parent;
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct umct_softc *sc = ucom->sc_parent;
if (onoff)
sc->sc_lcr |= 0x40;
else
sc->sc_lcr &= ~0x40;
umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
}
static void
umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct umct_softc *sc = ucom->sc_parent;
if (onoff)
sc->sc_mcr |= 0x01;
else
sc->sc_mcr &= ~0x01;
umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
}
static void
umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct umct_softc *sc = ucom->sc_parent;
if (onoff)
sc->sc_mcr |= 0x02;
else
sc->sc_mcr &= ~0x02;
umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
}
static uint8_t
umct_calc_baud(uint32_t baud)
{
switch (baud) {
case B300:return (0x1);
case B600:
return (0x2);
case B1200:
return (0x3);
case B2400:
return (0x4);
case B4800:
return (0x6);
case B9600:
return (0x8);
case B19200:
return (0x9);
case B38400:
return (0xa);
case B57600:
return (0xb);
case 115200:
return (0xc);
case B0:
default:
break;
}
return (0x0);
}
static int
umct_pre_param(struct ucom_softc *ucom, struct termios *t)
{
return (0); /* we accept anything */
}
static void
umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct umct_softc *sc = ucom->sc_parent;
uint32_t value;
value = umct_calc_baud(t->c_ospeed);
umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
value = (sc->sc_lcr & 0x40);
switch (t->c_cflag & CSIZE) {
case CS5:
value |= 0x0;
break;
case CS6:
value |= 0x1;
break;
case CS7:
value |= 0x2;
break;
default:
case CS8:
value |= 0x3;
break;
}
value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
if (t->c_cflag & PARENB) {
value |= 0x8;
value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
}
/*
* XXX There doesn't seem to be a way to tell the device
* to use flow control.
*/
sc->sc_lcr = value;
umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
}
static void
umct_start_read(struct ucom_softc *ucom)
{
struct umct_softc *sc = ucom->sc_parent;
/* start interrupt endpoint */
usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
/* start read endpoint */
usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
}
static void
umct_stop_read(struct ucom_softc *ucom)
{
struct umct_softc *sc = ucom->sc_parent;
/* stop interrupt endpoint */
usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
/* stop read endpoint */
usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
}
static void
umct_start_write(struct ucom_softc *ucom)
{
struct umct_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
}
static void
umct_stop_write(struct ucom_softc *ucom)
{
struct umct_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
}
static void
umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct umct_softc *sc = usbd_xfer_softc(xfer);
if (sc->sc_swap_cb)
umct_intr_callback_sub(xfer, error);
else
umct_read_callback_sub(xfer, error);
}
static void
umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct umct_softc *sc = usbd_xfer_softc(xfer);
if (sc->sc_swap_cb)
umct_read_callback_sub(xfer, error);
else
umct_intr_callback_sub(xfer, error);
}
static void
umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct umct_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
sc->sc_obufsize, &actlen)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
{
struct umct_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
umct_poll(struct ucom_softc *ucom)
{
struct umct_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,736 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $FreeBSD$ */
/* $OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $ */
/*
* Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR umoscom_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int umoscom_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RWTUN,
&umoscom_debug, 0, "Debug level");
#endif
#define UMOSCOM_BUFSIZE 1024 /* bytes */
#define UMOSCOM_CONFIG_INDEX 0
#define UMOSCOM_IFACE_INDEX 0
/* interrupt packet */
#define UMOSCOM_IIR_RLS 0x06
#define UMOSCOM_IIR_RDA 0x04
#define UMOSCOM_IIR_CTI 0x0c
#define UMOSCOM_IIR_THR 0x02
#define UMOSCOM_IIR_MS 0x00
/* registers */
#define UMOSCOM_READ 0x0d
#define UMOSCOM_WRITE 0x0e
#define UMOSCOM_UART_REG 0x0300
#define UMOSCOM_VEND_REG 0x0000
#define UMOSCOM_TXBUF 0x00 /* Write */
#define UMOSCOM_RXBUF 0x00 /* Read */
#define UMOSCOM_INT 0x01
#define UMOSCOM_FIFO 0x02 /* Write */
#define UMOSCOM_ISR 0x02 /* Read */
#define UMOSCOM_LCR 0x03
#define UMOSCOM_MCR 0x04
#define UMOSCOM_LSR 0x05
#define UMOSCOM_MSR 0x06
#define UMOSCOM_SCRATCH 0x07
#define UMOSCOM_DIV_LO 0x08
#define UMOSCOM_DIV_HI 0x09
#define UMOSCOM_EFR 0x0a
#define UMOSCOM_XON1 0x0b
#define UMOSCOM_XON2 0x0c
#define UMOSCOM_XOFF1 0x0d
#define UMOSCOM_XOFF2 0x0e
#define UMOSCOM_BAUDLO 0x00
#define UMOSCOM_BAUDHI 0x01
#define UMOSCOM_INT_RXEN 0x01
#define UMOSCOM_INT_TXEN 0x02
#define UMOSCOM_INT_RSEN 0x04
#define UMOSCOM_INT_MDMEM 0x08
#define UMOSCOM_INT_SLEEP 0x10
#define UMOSCOM_INT_XOFF 0x20
#define UMOSCOM_INT_RTS 0x40
#define UMOSCOM_FIFO_EN 0x01
#define UMOSCOM_FIFO_RXCLR 0x02
#define UMOSCOM_FIFO_TXCLR 0x04
#define UMOSCOM_FIFO_DMA_BLK 0x08
#define UMOSCOM_FIFO_TXLVL_MASK 0x30
#define UMOSCOM_FIFO_TXLVL_8 0x00
#define UMOSCOM_FIFO_TXLVL_16 0x10
#define UMOSCOM_FIFO_TXLVL_32 0x20
#define UMOSCOM_FIFO_TXLVL_56 0x30
#define UMOSCOM_FIFO_RXLVL_MASK 0xc0
#define UMOSCOM_FIFO_RXLVL_8 0x00
#define UMOSCOM_FIFO_RXLVL_16 0x40
#define UMOSCOM_FIFO_RXLVL_56 0x80
#define UMOSCOM_FIFO_RXLVL_80 0xc0
#define UMOSCOM_ISR_MDM 0x00
#define UMOSCOM_ISR_NONE 0x01
#define UMOSCOM_ISR_TX 0x02
#define UMOSCOM_ISR_RX 0x04
#define UMOSCOM_ISR_LINE 0x06
#define UMOSCOM_ISR_RXTIMEOUT 0x0c
#define UMOSCOM_ISR_RX_XOFF 0x10
#define UMOSCOM_ISR_RTSCTS 0x20
#define UMOSCOM_ISR_FIFOEN 0xc0
#define UMOSCOM_LCR_DBITS(x) ((x) - 5)
#define UMOSCOM_LCR_STOP_BITS_1 0x00
#define UMOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
#define UMOSCOM_LCR_PARITY_NONE 0x00
#define UMOSCOM_LCR_PARITY_ODD 0x08
#define UMOSCOM_LCR_PARITY_EVEN 0x18
#define UMOSCOM_LCR_BREAK 0x40
#define UMOSCOM_LCR_DIVLATCH_EN 0x80
#define UMOSCOM_MCR_DTR 0x01
#define UMOSCOM_MCR_RTS 0x02
#define UMOSCOM_MCR_LOOP 0x04
#define UMOSCOM_MCR_INTEN 0x08
#define UMOSCOM_MCR_LOOPBACK 0x10
#define UMOSCOM_MCR_XONANY 0x20
#define UMOSCOM_MCR_IRDA_EN 0x40
#define UMOSCOM_MCR_BAUD_DIV4 0x80
#define UMOSCOM_LSR_RXDATA 0x01
#define UMOSCOM_LSR_RXOVER 0x02
#define UMOSCOM_LSR_RXPAR_ERR 0x04
#define UMOSCOM_LSR_RXFRM_ERR 0x08
#define UMOSCOM_LSR_RXBREAK 0x10
#define UMOSCOM_LSR_TXEMPTY 0x20
#define UMOSCOM_LSR_TXALLEMPTY 0x40
#define UMOSCOM_LSR_TXFIFO_ERR 0x80
#define UMOSCOM_MSR_CTS_CHG 0x01
#define UMOSCOM_MSR_DSR_CHG 0x02
#define UMOSCOM_MSR_RI_CHG 0x04
#define UMOSCOM_MSR_CD_CHG 0x08
#define UMOSCOM_MSR_CTS 0x10
#define UMOSCOM_MSR_RTS 0x20
#define UMOSCOM_MSR_RI 0x40
#define UMOSCOM_MSR_CD 0x80
#define UMOSCOM_BAUD_REF 115200
enum {
UMOSCOM_BULK_DT_WR,
UMOSCOM_BULK_DT_RD,
UMOSCOM_INTR_DT_RD,
UMOSCOM_N_TRANSFER,
};
struct umoscom_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint8_t sc_mcr;
uint8_t sc_lcr;
};
/* prototypes */
static device_probe_t umoscom_probe;
static device_attach_t umoscom_attach;
static device_detach_t umoscom_detach;
static void umoscom_free_softc(struct umoscom_softc *);
static usb_callback_t umoscom_write_callback;
static usb_callback_t umoscom_read_callback;
static usb_callback_t umoscom_intr_callback;
static void umoscom_free(struct ucom_softc *);
static void umoscom_cfg_open(struct ucom_softc *);
static void umoscom_cfg_close(struct ucom_softc *);
static void umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
static void umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
static int umoscom_pre_param(struct ucom_softc *, struct termios *);
static void umoscom_cfg_param(struct ucom_softc *, struct termios *);
static void umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
static uint8_t umoscom_cfg_read(struct umoscom_softc *, uint16_t);
static void umoscom_start_read(struct ucom_softc *);
static void umoscom_stop_read(struct ucom_softc *);
static void umoscom_start_write(struct ucom_softc *);
static void umoscom_stop_write(struct ucom_softc *);
static void umoscom_poll(struct ucom_softc *ucom);
static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {
[UMOSCOM_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UMOSCOM_BUFSIZE,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &umoscom_write_callback,
},
[UMOSCOM_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UMOSCOM_BUFSIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &umoscom_read_callback,
},
[UMOSCOM_INTR_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &umoscom_intr_callback,
},
};
static const struct ucom_callback umoscom_callback = {
/* configuration callbacks */
.ucom_cfg_get_status = &umoscom_cfg_get_status,
.ucom_cfg_set_dtr = &umoscom_cfg_set_dtr,
.ucom_cfg_set_rts = &umoscom_cfg_set_rts,
.ucom_cfg_set_break = &umoscom_cfg_set_break,
.ucom_cfg_param = &umoscom_cfg_param,
.ucom_cfg_open = &umoscom_cfg_open,
.ucom_cfg_close = &umoscom_cfg_close,
/* other callbacks */
.ucom_pre_param = &umoscom_pre_param,
.ucom_start_read = &umoscom_start_read,
.ucom_stop_read = &umoscom_stop_read,
.ucom_start_write = &umoscom_start_write,
.ucom_stop_write = &umoscom_stop_write,
.ucom_poll = &umoscom_poll,
.ucom_free = &umoscom_free,
};
static device_method_t umoscom_methods[] = {
DEVMETHOD(device_probe, umoscom_probe),
DEVMETHOD(device_attach, umoscom_attach),
DEVMETHOD(device_detach, umoscom_detach),
DEVMETHOD_END
};
static devclass_t umoscom_devclass;
static driver_t umoscom_driver = {
.name = "umoscom",
.methods = umoscom_methods,
.size = sizeof(struct umoscom_softc),
};
static const STRUCT_USB_HOST_ID umoscom_devs[] = {
{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
};
DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
MODULE_DEPEND(umoscom, usb, 1, 1, 1);
MODULE_VERSION(umoscom, 1);
USB_PNP_HOST_INFO(umoscom_devs);
static int
umoscom_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 != UMOSCOM_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
}
static int
umoscom_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct umoscom_softc *sc = device_get_softc(dev);
int error;
uint8_t iface_index;
sc->sc_udev = uaa->device;
sc->sc_mcr = 0x08; /* enable interrupts */
/* XXX the device doesn't provide any ID string, so set a static one */
device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
iface_index = UMOSCOM_IFACE_INDEX;
error = usbd_transfer_setup(uaa->device, &iface_index,
sc->sc_xfer, umoscom_config_data,
UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
goto detach;
}
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&umoscom_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0);
detach:
device_printf(dev, "attach error: %s\n", usbd_errstr(error));
umoscom_detach(dev);
return (ENXIO);
}
static int
umoscom_detach(device_t dev)
{
struct umoscom_softc *sc = device_get_softc(dev);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);
device_claim_softc(dev);
umoscom_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(umoscom);
static void
umoscom_free_softc(struct umoscom_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
umoscom_free(struct ucom_softc *ucom)
{
umoscom_free_softc(ucom->sc_parent);
}
static void
umoscom_cfg_open(struct ucom_softc *ucom)
{
struct umoscom_softc *sc = ucom->sc_parent;
DPRINTF("\n");
/* Purge FIFOs or odd things happen */
umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
/* Enable FIFO */
umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
UMOSCOM_UART_REG);
/* Enable Interrupt Registers */
umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
/* Magic */
umoscom_cfg_write(sc, 0x01, 0x08);
/* Magic */
umoscom_cfg_write(sc, 0x00, 0x02);
}
static void
umoscom_cfg_close(struct ucom_softc *ucom)
{
return;
}
static void
umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct umoscom_softc *sc = ucom->sc_parent;
uint16_t val;
val = sc->sc_lcr;
if (onoff)
val |= UMOSCOM_LCR_BREAK;
umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
}
static void
umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct umoscom_softc *sc = ucom->sc_parent;
if (onoff)
sc->sc_mcr |= UMOSCOM_MCR_DTR;
else
sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
}
static void
umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct umoscom_softc *sc = ucom->sc_parent;
if (onoff)
sc->sc_mcr |= UMOSCOM_MCR_RTS;
else
sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
}
static int
umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
return (EINVAL);
return (0);
}
static void
umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct umoscom_softc *sc = ucom->sc_parent;
uint16_t data;
DPRINTF("speed=%d\n", t->c_ospeed);
data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
if (data == 0) {
DPRINTF("invalid baud rate!\n");
return;
}
umoscom_cfg_write(sc, UMOSCOM_LCR,
UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
(data & 0xFF) | UMOSCOM_UART_REG);
umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
if (t->c_cflag & CSTOPB)
data = UMOSCOM_LCR_STOP_BITS_2;
else
data = UMOSCOM_LCR_STOP_BITS_1;
if (t->c_cflag & PARENB) {
if (t->c_cflag & PARODD)
data |= UMOSCOM_LCR_PARITY_ODD;
else
data |= UMOSCOM_LCR_PARITY_EVEN;
} else
data |= UMOSCOM_LCR_PARITY_NONE;
switch (t->c_cflag & CSIZE) {
case CS5:
data |= UMOSCOM_LCR_DBITS(5);
break;
case CS6:
data |= UMOSCOM_LCR_DBITS(6);
break;
case CS7:
data |= UMOSCOM_LCR_DBITS(7);
break;
case CS8:
data |= UMOSCOM_LCR_DBITS(8);
break;
}
sc->sc_lcr = data;
umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
}
static void
umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
{
struct umoscom_softc *sc = ucom->sc_parent;
uint8_t msr;
DPRINTFN(5, "\n");
/*
* Read status registers. MSR bits need translation from ns16550 to
* SER_* values. LSR bits are ns16550 in hardware and ucom.
*/
*p_lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
/* translate bits */
if (msr & UMOSCOM_MSR_CTS)
*p_msr |= SER_CTS;
if (msr & UMOSCOM_MSR_CD)
*p_msr |= SER_DCD;
if (msr & UMOSCOM_MSR_RI)
*p_msr |= SER_RI;
if (msr & UMOSCOM_MSR_RTS)
*p_msr |= SER_DSR;
}
static void
umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
{
struct usb_device_request req;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = UMOSCOM_WRITE;
USETW(req.wValue, val);
USETW(req.wIndex, reg);
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
}
static uint8_t
umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
{
struct usb_device_request req;
uint8_t val;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = UMOSCOM_READ;
USETW(req.wValue, 0);
USETW(req.wIndex, reg);
USETW(req.wLength, 1);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, &val, 0, 1000);
DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
return (val);
}
static void
umoscom_start_read(struct ucom_softc *ucom)
{
struct umoscom_softc *sc = ucom->sc_parent;
#if 0
/* start interrupt endpoint */
usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
#endif
/* start read endpoint */
usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
}
static void
umoscom_stop_read(struct ucom_softc *ucom)
{
struct umoscom_softc *sc = ucom->sc_parent;
/* stop interrupt transfer */
usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
/* stop read endpoint */
usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
}
static void
umoscom_start_write(struct ucom_softc *ucom)
{
struct umoscom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
}
static void
umoscom_stop_write(struct ucom_softc *ucom)
{
struct umoscom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
}
static void
umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct umoscom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
DPRINTF("\n");
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
UMOSCOM_BUFSIZE, &actlen)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
DPRINTFN(0, "transfer failed\n");
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct umoscom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
DPRINTF("got %d bytes\n", actlen);
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
DPRINTF("\n");
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
DPRINTFN(0, "transfer failed\n");
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct umoscom_softc *sc = usbd_xfer_softc(xfer);
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen < 2) {
DPRINTF("too short message\n");
goto tr_setup;
}
ucom_status_change(&sc->sc_ucom);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
DPRINTFN(0, "transfer failed\n");
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
umoscom_poll(struct ucom_softc *ucom)
{
struct umoscom_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER);
}

View File

@ -0,0 +1,936 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Ichiro FUKUHARA (ichiro@ichiro.org).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This driver supports several USB-to-RS232 serial adapters driven by
* Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
* bridge chip. The adapters are sold under many different brand
* names.
*
* Datasheets are available at Prolific www site at
* http://www.prolific.com.tw. The datasheets don't contain full
* programming information for the chip.
*
* PL-2303HX is probably programmed the same as PL-2303X.
*
* There are several differences between PL-2303 and PL-2303(H)X.
* PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
* different command for controlling CRTSCTS and needs special
* sequence of commands for initialization which aren't also
* documented in the datasheet.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usb_cdc.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR uplcom_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int uplcom_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
&uplcom_debug, 0, "Debug level");
#endif
#define UPLCOM_MODVER 1 /* module version */
#define UPLCOM_CONFIG_INDEX 0
#define UPLCOM_IFACE_INDEX 0
#define UPLCOM_SECOND_IFACE_INDEX 1
#ifndef UPLCOM_INTR_INTERVAL
#define UPLCOM_INTR_INTERVAL 0 /* default */
#endif
#define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
#define UPLCOM_SET_REQUEST 0x01
#define UPLCOM_SET_CRTSCTS 0x41
#define UPLCOM_SET_CRTSCTS_PL2303X 0x61
#define RSAQ_STATUS_CTS 0x80
#define RSAQ_STATUS_DSR 0x02
#define RSAQ_STATUS_DCD 0x01
#define TYPE_PL2303 0
#define TYPE_PL2303HX 1
enum {
UPLCOM_BULK_DT_WR,
UPLCOM_BULK_DT_RD,
UPLCOM_INTR_DT_RD,
UPLCOM_N_TRANSFER,
};
struct uplcom_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint16_t sc_line;
uint8_t sc_lsr; /* local status register */
uint8_t sc_msr; /* uplcom status register */
uint8_t sc_chiptype; /* type of chip */
uint8_t sc_ctrl_iface_no;
uint8_t sc_data_iface_no;
uint8_t sc_iface_index[2];
};
/* prototypes */
static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
uint16_t, uint16_t, uint16_t);
static int uplcom_pl2303_init(struct usb_device *, uint8_t);
static void uplcom_free(struct ucom_softc *);
static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
static int uplcom_pre_param(struct ucom_softc *, struct termios *);
static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
static void uplcom_start_read(struct ucom_softc *);
static void uplcom_stop_read(struct ucom_softc *);
static void uplcom_start_write(struct ucom_softc *);
static void uplcom_stop_write(struct ucom_softc *);
static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void uplcom_poll(struct ucom_softc *ucom);
static device_probe_t uplcom_probe;
static device_attach_t uplcom_attach;
static device_detach_t uplcom_detach;
static void uplcom_free_softc(struct uplcom_softc *);
static usb_callback_t uplcom_intr_callback;
static usb_callback_t uplcom_write_callback;
static usb_callback_t uplcom_read_callback;
static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
[UPLCOM_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UPLCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &uplcom_write_callback,
.if_index = 0,
},
[UPLCOM_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UPLCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &uplcom_read_callback,
.if_index = 0,
},
[UPLCOM_INTR_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &uplcom_intr_callback,
.if_index = 1,
},
};
static struct ucom_callback uplcom_callback = {
.ucom_cfg_get_status = &uplcom_cfg_get_status,
.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
.ucom_cfg_set_break = &uplcom_cfg_set_break,
.ucom_cfg_param = &uplcom_cfg_param,
.ucom_pre_param = &uplcom_pre_param,
.ucom_start_read = &uplcom_start_read,
.ucom_stop_read = &uplcom_stop_read,
.ucom_start_write = &uplcom_start_write,
.ucom_stop_write = &uplcom_stop_write,
.ucom_poll = &uplcom_poll,
.ucom_free = &uplcom_free,
};
#define UPLCOM_DEV(v,p) \
{ USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
static const STRUCT_USB_HOST_ID uplcom_devs[] = {
UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */
UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */
UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */
UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */
UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */
UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */
UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */
UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */
UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */
UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */
UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */
UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */
UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */
UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */
UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */
UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */
UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */
UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */
UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */
UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */
UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */
UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */
UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */
UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */
UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */
UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */
UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */
UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */
UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */
UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */
UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */
UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */
UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */
UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */
UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */
UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */
UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */
UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */
UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */
UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */
UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */
UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */
UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */
UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */
UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */
UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */
UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */
UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */
UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */
UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */
UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */
UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */
UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */
UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */
UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */
UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */
UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */
UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */
UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */
UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */
};
#undef UPLCOM_DEV
static device_method_t uplcom_methods[] = {
DEVMETHOD(device_probe, uplcom_probe),
DEVMETHOD(device_attach, uplcom_attach),
DEVMETHOD(device_detach, uplcom_detach),
DEVMETHOD_END
};
static devclass_t uplcom_devclass;
static driver_t uplcom_driver = {
.name = "uplcom",
.methods = uplcom_methods,
.size = sizeof(struct uplcom_softc),
};
DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
MODULE_DEPEND(uplcom, usb, 1, 1, 1);
MODULE_VERSION(uplcom, UPLCOM_MODVER);
USB_PNP_HOST_INFO(uplcom_devs);
static int
uplcom_probe(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
DPRINTFN(11, "\n");
if (uaa->usb_mode != USB_MODE_HOST) {
return (ENXIO);
}
if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
}
static int
uplcom_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct uplcom_softc *sc = device_get_softc(dev);
struct usb_interface *iface;
struct usb_interface_descriptor *id;
struct usb_device_descriptor *dd;
int error;
DPRINTFN(11, "\n");
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
DPRINTF("sc = %p\n", sc);
sc->sc_udev = uaa->device;
/* Determine the chip type. This algorithm is taken from Linux. */
dd = usbd_get_device_descriptor(sc->sc_udev);
if (dd->bDeviceClass == 0x02)
sc->sc_chiptype = TYPE_PL2303;
else if (dd->bMaxPacketSize == 0x40)
sc->sc_chiptype = TYPE_PL2303HX;
else
sc->sc_chiptype = TYPE_PL2303;
DPRINTF("chiptype: %s\n",
(sc->sc_chiptype == TYPE_PL2303HX) ?
"2303X" : "2303");
/*
* USB-RSAQ1 has two interface
*
* USB-RSAQ1 | USB-RSAQ2
* -----------------+-----------------
* Interface 0 |Interface 0
* Interrupt(0x81) | Interrupt(0x81)
* -----------------+ BulkIN(0x02)
* Interface 1 | BulkOUT(0x83)
* BulkIN(0x02) |
* BulkOUT(0x83) |
*/
sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
if (iface) {
id = usbd_get_interface_descriptor(iface);
if (id == NULL) {
device_printf(dev, "no interface descriptor (2)\n");
goto detach;
}
sc->sc_data_iface_no = id->bInterfaceNumber;
sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
usbd_set_parent_iface(uaa->device,
UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
} else {
sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
}
error = usbd_transfer_setup(uaa->device,
sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
DPRINTF("one or more missing USB endpoints, "
"error=%s\n", usbd_errstr(error));
goto detach;
}
error = uplcom_reset(sc, uaa->device);
if (error) {
device_printf(dev, "reset failed, error=%s\n",
usbd_errstr(error));
goto detach;
}
if (sc->sc_chiptype != TYPE_PL2303HX) {
/* HX variants seem to lock up after a clear stall request. */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
} else {
if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
UPLCOM_SET_REQUEST, 8, 0, 0) ||
uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
UPLCOM_SET_REQUEST, 9, 0, 0)) {
goto detach;
}
}
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&uplcom_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
/*
* do the initialization during attach so that the system does not
* sleep during open:
*/
if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
device_printf(dev, "init failed\n");
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0);
detach:
uplcom_detach(dev);
return (ENXIO);
}
static int
uplcom_detach(device_t dev)
{
struct uplcom_softc *sc = device_get_softc(dev);
DPRINTF("sc=%p\n", sc);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
device_claim_softc(dev);
uplcom_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(uplcom);
static void
uplcom_free_softc(struct uplcom_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
uplcom_free(struct ucom_softc *ucom)
{
uplcom_free_softc(ucom->sc_parent);
}
static usb_error_t
uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
{
struct usb_device_request req;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = UPLCOM_SET_REQUEST;
USETW(req.wValue, 0);
req.wIndex[0] = sc->sc_data_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 0);
return (usbd_do_request(udev, NULL, &req, NULL));
}
static usb_error_t
uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
uint16_t value, uint16_t index, uint16_t length)
{
struct usb_device_request req;
usb_error_t err;
uint8_t buf[4];
req.bmRequestType = req_type;
req.bRequest = request;
USETW(req.wValue, value);
USETW(req.wIndex, index);
USETW(req.wLength, length);
err = usbd_do_request(udev, NULL, &req, buf);
if (err) {
DPRINTF("error=%s\n", usbd_errstr(err));
return (1);
}
return (0);
}
static int
uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
{
int err;
if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
|| uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
|| uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
|| uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
|| uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
|| uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
|| uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
|| uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
|| uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
|| uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
return (EIO);
if (chiptype == TYPE_PL2303HX)
err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
else
err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
if (err)
return (EIO);
return (0);
}
static void
uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct uplcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
DPRINTF("onoff = %d\n", onoff);
if (onoff)
sc->sc_line |= UCDC_LINE_DTR;
else
sc->sc_line &= ~UCDC_LINE_DTR;
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
USETW(req.wValue, sc->sc_line);
req.wIndex[0] = sc->sc_data_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
}
static void
uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct uplcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
DPRINTF("onoff = %d\n", onoff);
if (onoff)
sc->sc_line |= UCDC_LINE_RTS;
else
sc->sc_line &= ~UCDC_LINE_RTS;
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
USETW(req.wValue, sc->sc_line);
req.wIndex[0] = sc->sc_data_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
}
static void
uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct uplcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
uint16_t temp;
DPRINTF("onoff = %d\n", onoff);
temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UCDC_SEND_BREAK;
USETW(req.wValue, temp);
req.wIndex[0] = sc->sc_data_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
}
static const uint32_t uplcom_rates[] = {
75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
19200, 28800, 38400, 57600, 115200,
/*
* Higher speeds are probably possible. PL2303X supports up to
* 6Mb and can set any rate
*/
230400, 460800, 614400, 921600, 1228800
};
#define N_UPLCOM_RATES nitems(uplcom_rates)
static int
uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
struct uplcom_softc *sc = ucom->sc_parent;
uint8_t i;
DPRINTF("\n");
/**
* Check requested baud rate.
*
* The PL2303 can only set specific baud rates, up to 1228800 baud.
* The PL2303X can set any baud rate up to 6Mb.
* The PL2303HX rev. D can set any baud rate up to 12Mb.
*
* XXX: We currently cannot identify the PL2303HX rev. D, so treat
* it the same as the PL2303X.
*/
if (sc->sc_chiptype != TYPE_PL2303HX) {
for (i = 0; i < N_UPLCOM_RATES; i++) {
if (uplcom_rates[i] == t->c_ospeed)
return (0);
}
} else {
if (t->c_ospeed <= 6000000)
return (0);
}
DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
return (EIO);
}
static void
uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct uplcom_softc *sc = ucom->sc_parent;
struct usb_cdc_line_state ls;
struct usb_device_request req;
DPRINTF("sc = %p\n", sc);
memset(&ls, 0, sizeof(ls));
USETDW(ls.dwDTERate, t->c_ospeed);
if (t->c_cflag & CSTOPB) {
ls.bCharFormat = UCDC_STOP_BIT_2;
} else {
ls.bCharFormat = UCDC_STOP_BIT_1;
}
if (t->c_cflag & PARENB) {
if (t->c_cflag & PARODD) {
ls.bParityType = UCDC_PARITY_ODD;
} else {
ls.bParityType = UCDC_PARITY_EVEN;
}
} else {
ls.bParityType = UCDC_PARITY_NONE;
}
switch (t->c_cflag & CSIZE) {
case CS5:
ls.bDataBits = 5;
break;
case CS6:
ls.bDataBits = 6;
break;
case CS7:
ls.bDataBits = 7;
break;
case CS8:
ls.bDataBits = 8;
break;
}
DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
UGETDW(ls.dwDTERate), ls.bCharFormat,
ls.bParityType, ls.bDataBits);
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UCDC_SET_LINE_CODING;
USETW(req.wValue, 0);
req.wIndex[0] = sc->sc_data_iface_no;
req.wIndex[1] = 0;
USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, &ls, 0, 1000);
if (t->c_cflag & CRTSCTS) {
DPRINTF("crtscts = on\n");
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = UPLCOM_SET_REQUEST;
USETW(req.wValue, 0);
if (sc->sc_chiptype == TYPE_PL2303HX)
USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
else
USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
} else {
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = UPLCOM_SET_REQUEST;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, 0);
ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
}
}
static void
uplcom_start_read(struct ucom_softc *ucom)
{
struct uplcom_softc *sc = ucom->sc_parent;
/* start interrupt endpoint */
usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
/* start read endpoint */
usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
}
static void
uplcom_stop_read(struct ucom_softc *ucom)
{
struct uplcom_softc *sc = ucom->sc_parent;
/* stop interrupt endpoint */
usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
/* stop read endpoint */
usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
}
static void
uplcom_start_write(struct ucom_softc *ucom)
{
struct uplcom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
}
static void
uplcom_stop_write(struct ucom_softc *ucom)
{
struct uplcom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
}
static void
uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct uplcom_softc *sc = ucom->sc_parent;
DPRINTF("\n");
/* XXX Note: sc_lsr is always zero */
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uplcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[9];
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
DPRINTF("actlen = %u\n", actlen);
if (actlen >= 9) {
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, sizeof(buf));
DPRINTF("status = 0x%02x\n", buf[8]);
sc->sc_lsr = 0;
sc->sc_msr = 0;
if (buf[8] & RSAQ_STATUS_CTS) {
sc->sc_msr |= SER_CTS;
}
if (buf[8] & RSAQ_STATUS_DSR) {
sc->sc_msr |= SER_DSR;
}
if (buf[8] & RSAQ_STATUS_DCD) {
sc->sc_msr |= SER_DCD;
}
ucom_status_change(&sc->sc_ucom);
}
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uplcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
UPLCOM_BULK_BUF_SIZE, &actlen)) {
DPRINTF("actlen = %d\n", actlen);
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uplcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uplcom_poll(struct ucom_softc *ucom)
{
struct uplcom_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,230 @@
/* $NetBSD: ucomvar.h,v 1.9 2001/01/23 21:56:17 augustss Exp $ */
/* $FreeBSD$ */
/*-
* Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _USB_SERIAL_H_
#define _USB_SERIAL_H_
#include <sys/tty.h>
#include <sys/serial.h>
#include <sys/fcntl.h>
#include <sys/sysctl.h>
#include <sys/timepps.h>
/* Module interface related macros */
#define UCOM_MODVER 1
#define UCOM_MINVER 1
#define UCOM_PREFVER UCOM_MODVER
#define UCOM_MAXVER 1
#define UCOM_JITTERBUF_SIZE 128 /* bytes */
struct usb_device;
struct ucom_softc;
struct usb_device_request;
struct thread;
/*
* NOTE: There is no guarantee that "ucom_cfg_close()" will
* be called after "ucom_cfg_open()" if the device is detached
* while it is open!
*/
struct ucom_callback {
void (*ucom_cfg_get_status) (struct ucom_softc *, uint8_t *plsr, uint8_t *pmsr);
void (*ucom_cfg_set_dtr) (struct ucom_softc *, uint8_t);
void (*ucom_cfg_set_rts) (struct ucom_softc *, uint8_t);
void (*ucom_cfg_set_break) (struct ucom_softc *, uint8_t);
void (*ucom_cfg_set_ring) (struct ucom_softc *, uint8_t);
void (*ucom_cfg_param) (struct ucom_softc *, struct termios *);
void (*ucom_cfg_open) (struct ucom_softc *);
void (*ucom_cfg_close) (struct ucom_softc *);
int (*ucom_pre_open) (struct ucom_softc *);
int (*ucom_pre_param) (struct ucom_softc *, struct termios *);
int (*ucom_ioctl) (struct ucom_softc *, uint32_t, caddr_t, int, struct thread *);
void (*ucom_start_read) (struct ucom_softc *);
void (*ucom_stop_read) (struct ucom_softc *);
void (*ucom_start_write) (struct ucom_softc *);
void (*ucom_stop_write) (struct ucom_softc *);
void (*ucom_tty_name) (struct ucom_softc *, char *pbuf, uint16_t buflen, uint16_t unit, uint16_t subunit);
void (*ucom_poll) (struct ucom_softc *);
void (*ucom_free) (struct ucom_softc *);
};
/* Line status register */
#define ULSR_RCV_FIFO 0x80
#define ULSR_TSRE 0x40 /* Transmitter empty: byte sent */
#define ULSR_TXRDY 0x20 /* Transmitter buffer empty */
#define ULSR_BI 0x10 /* Break detected */
#define ULSR_FE 0x08 /* Framing error: bad stop bit */
#define ULSR_PE 0x04 /* Parity error */
#define ULSR_OE 0x02 /* Overrun, lost incoming byte */
#define ULSR_RXRDY 0x01 /* Byte ready in Receive Buffer */
#define ULSR_RCV_MASK 0x1f /* Mask for incoming data or error */
struct ucom_cfg_task {
struct usb_proc_msg hdr;
struct ucom_softc *sc;
};
struct ucom_param_task {
struct usb_proc_msg hdr;
struct ucom_softc *sc;
struct termios termios_copy;
};
struct ucom_super_softc {
struct usb_process sc_tq;
int sc_unit;
int sc_subunits;
int sc_refs;
int sc_flag; /* see UCOM_FLAG_XXX */
struct sysctl_oid *sc_sysctl_ttyname;
struct sysctl_oid *sc_sysctl_ttyports;
char sc_ttyname[16];
};
struct ucom_softc {
/*
* NOTE: To avoid losing level change information we use two
* tasks instead of one for all commands.
*
* Level changes are transitions like:
*
* ON->OFF
* OFF->ON
* OPEN->CLOSE
* CLOSE->OPEN
*/
struct ucom_cfg_task sc_start_task[2];
struct ucom_cfg_task sc_open_task[2];
struct ucom_cfg_task sc_close_task[2];
struct ucom_cfg_task sc_line_state_task[2];
struct ucom_cfg_task sc_status_task[2];
struct ucom_param_task sc_param_task[2];
/* pulse capturing support, PPS */
struct pps_state sc_pps;
/* Used to set "UCOM_FLAG_GP_DATA" flag: */
struct usb_proc_msg *sc_last_start_xfer;
const struct ucom_callback *sc_callback;
struct ucom_super_softc *sc_super;
struct tty *sc_tty;
struct mtx *sc_mtx;
void *sc_parent;
int sc_subunit;
uint16_t sc_jitterbuf_in;
uint16_t sc_jitterbuf_out;
uint16_t sc_portno;
uint16_t sc_flag;
#define UCOM_FLAG_RTS_IFLOW 0x01 /* use RTS input flow control */
#define UCOM_FLAG_GONE 0x02 /* the device is gone */
#define UCOM_FLAG_ATTACHED 0x04 /* set if attached */
#define UCOM_FLAG_GP_DATA 0x08 /* set if get and put data is possible */
#define UCOM_FLAG_LL_READY 0x20 /* set if low layer is ready */
#define UCOM_FLAG_HL_READY 0x40 /* set if high layer is ready */
#define UCOM_FLAG_CONSOLE 0x80 /* set if device is a console */
#define UCOM_FLAG_WAIT_REFS 0x0100 /* set if we must wait for refs */
#define UCOM_FLAG_FREE_UNIT 0x0200 /* set if we must free the unit */
#define UCOM_FLAG_INWAKEUP 0x0400 /* set if we are in the tsw_inwakeup callback */
#define UCOM_FLAG_LSRTXIDLE 0x0800 /* set if sc_lsr bits ULSR_TSRE+TXRDY work */
uint8_t sc_lsr;
uint8_t sc_msr;
uint8_t sc_mcr;
/* programmed line state bits */
uint8_t sc_pls_set; /* set bits */
uint8_t sc_pls_clr; /* cleared bits */
uint8_t sc_pls_curr; /* last state */
#define UCOM_LS_DTR 0x01
#define UCOM_LS_RTS 0x02
#define UCOM_LS_BREAK 0x04
#define UCOM_LS_RING 0x08
uint8_t sc_jitterbuf[UCOM_JITTERBUF_SIZE];
};
#define UCOM_MTX_ASSERT(sc, what) USB_MTX_ASSERT((sc)->sc_mtx, what)
#define UCOM_MTX_LOCK(sc) USB_MTX_LOCK((sc)->sc_mtx)
#define UCOM_MTX_UNLOCK(sc) USB_MTX_UNLOCK((sc)->sc_mtx)
#define UCOM_UNLOAD_DRAIN(x) \
SYSUNINIT(var, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_drain_all, 0)
#define ucom_cfg_do_request(udev,com,req,ptr,flags,timo) \
usbd_do_request_proc(udev,&(com)->sc_super->sc_tq,req,ptr,flags,NULL,timo)
int ucom_attach(struct ucom_super_softc *,
struct ucom_softc *, int, void *,
const struct ucom_callback *callback, struct mtx *);
void ucom_detach(struct ucom_super_softc *, struct ucom_softc *);
void ucom_set_pnpinfo_usb(struct ucom_super_softc *, device_t);
void ucom_status_change(struct ucom_softc *);
uint8_t ucom_get_data(struct ucom_softc *, struct usb_page_cache *,
uint32_t, uint32_t, uint32_t *);
void ucom_put_data(struct ucom_softc *, struct usb_page_cache *,
uint32_t, uint32_t);
uint8_t ucom_cfg_is_gone(struct ucom_softc *);
void ucom_drain(struct ucom_super_softc *);
void ucom_drain_all(void *);
void ucom_ref(struct ucom_super_softc *);
int ucom_unref(struct ucom_super_softc *);
static inline void
ucom_use_lsr_txbits(struct ucom_softc *sc)
{
sc->sc_flag |= UCOM_FLAG_LSRTXIDLE;
}
#endif /* _USB_SERIAL_H_ */

View File

@ -0,0 +1,946 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Driver for Silicon Laboratories CP2101/CP2102/CP2103/CP2104/CP2105
* USB-Serial adapters. Based on datasheet AN571, publicly available from
* http://www.silabs.com/Support%20Documents/TechnicalDocs/AN571.pdf
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usb_ioctl.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR uslcom_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int uslcom_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom");
SYSCTL_INT(_hw_usb_uslcom, OID_AUTO, debug, CTLFLAG_RWTUN,
&uslcom_debug, 0, "Debug level");
#endif
#define USLCOM_BULK_BUF_SIZE 1024
#define USLCOM_CONFIG_INDEX 0
/* Request types */
#define USLCOM_WRITE 0x41
#define USLCOM_READ 0xc1
/* Request codes */
#define USLCOM_IFC_ENABLE 0x00
#define USLCOM_SET_BAUDDIV 0x01
#define USLCOM_SET_LINE_CTL 0x03
#define USLCOM_SET_BREAK 0x05
#define USLCOM_SET_MHS 0x07
#define USLCOM_GET_MDMSTS 0x08
#define USLCOM_SET_FLOW 0x13
#define USLCOM_SET_BAUDRATE 0x1e
#define USLCOM_VENDOR_SPECIFIC 0xff
/* USLCOM_IFC_ENABLE values */
#define USLCOM_IFC_ENABLE_DIS 0x00
#define USLCOM_IFC_ENABLE_EN 0x01
/* USLCOM_SET_MHS/USLCOM_GET_MDMSTS values */
#define USLCOM_MHS_DTR_ON 0x0001
#define USLCOM_MHS_DTR_SET 0x0100
#define USLCOM_MHS_RTS_ON 0x0002
#define USLCOM_MHS_RTS_SET 0x0200
#define USLCOM_MHS_CTS 0x0010
#define USLCOM_MHS_DSR 0x0020
#define USLCOM_MHS_RI 0x0040
#define USLCOM_MHS_DCD 0x0080
/* USLCOM_SET_BAUDDIV values */
#define USLCOM_BAUDDIV_REF 3686400 /* 3.6864 MHz */
/* USLCOM_SET_LINE_CTL values */
#define USLCOM_STOP_BITS_1 0x00
#define USLCOM_STOP_BITS_2 0x02
#define USLCOM_PARITY_NONE 0x00
#define USLCOM_PARITY_ODD 0x10
#define USLCOM_PARITY_EVEN 0x20
#define USLCOM_SET_DATA_BITS(x) ((x) << 8)
/* USLCOM_SET_BREAK values */
#define USLCOM_SET_BREAK_OFF 0x00
#define USLCOM_SET_BREAK_ON 0x01
/* USLCOM_SET_FLOW values - 1st word */
#define USLCOM_FLOW_DTR_ON 0x00000001 /* DTR static active */
#define USLCOM_FLOW_CTS_HS 0x00000008 /* CTS handshake */
/* USLCOM_SET_FLOW values - 2nd word */
#define USLCOM_FLOW_RTS_ON 0x00000040 /* RTS static active */
#define USLCOM_FLOW_RTS_HS 0x00000080 /* RTS handshake */
/* USLCOM_VENDOR_SPECIFIC values */
#define USLCOM_GET_PARTNUM 0x370B
#define USLCOM_WRITE_LATCH 0x37E1
#define USLCOM_READ_LATCH 0x00C2
/* USLCOM_GET_PARTNUM values from hardware */
#define USLCOM_PARTNUM_CP2101 1
#define USLCOM_PARTNUM_CP2102 2
#define USLCOM_PARTNUM_CP2103 3
#define USLCOM_PARTNUM_CP2104 4
#define USLCOM_PARTNUM_CP2105 5
enum {
USLCOM_BULK_DT_WR,
USLCOM_BULK_DT_RD,
USLCOM_CTRL_DT_RD,
USLCOM_N_TRANSFER,
};
struct uslcom_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_callout sc_watchdog;
struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint8_t sc_msr;
uint8_t sc_lsr;
uint8_t sc_iface_no;
uint8_t sc_partnum;
};
static device_probe_t uslcom_probe;
static device_attach_t uslcom_attach;
static device_detach_t uslcom_detach;
static void uslcom_free_softc(struct uslcom_softc *);
static usb_callback_t uslcom_write_callback;
static usb_callback_t uslcom_read_callback;
static usb_callback_t uslcom_control_callback;
static void uslcom_free(struct ucom_softc *);
static void uslcom_open(struct ucom_softc *);
static void uslcom_close(struct ucom_softc *);
static uint8_t uslcom_get_partnum(struct uslcom_softc *);
static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
static void uslcom_set_rts(struct ucom_softc *, uint8_t);
static void uslcom_set_break(struct ucom_softc *, uint8_t);
static int uslcom_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
struct thread *);
static int uslcom_pre_param(struct ucom_softc *, struct termios *);
static void uslcom_param(struct ucom_softc *, struct termios *);
static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
static void uslcom_start_read(struct ucom_softc *);
static void uslcom_stop_read(struct ucom_softc *);
static void uslcom_start_write(struct ucom_softc *);
static void uslcom_stop_write(struct ucom_softc *);
static void uslcom_poll(struct ucom_softc *ucom);
static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
[USLCOM_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = USLCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,},
.callback = &uslcom_write_callback,
},
[USLCOM_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = USLCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &uslcom_read_callback,
},
[USLCOM_CTRL_DT_RD] = {
.type = UE_CONTROL,
.endpoint = 0x00,
.direction = UE_DIR_ANY,
.bufsize = sizeof(struct usb_device_request) + 8,
.flags = {.pipe_bof = 1,},
.callback = &uslcom_control_callback,
.timeout = 1000, /* 1 second timeout */
},
};
static struct ucom_callback uslcom_callback = {
.ucom_cfg_open = &uslcom_open,
.ucom_cfg_close = &uslcom_close,
.ucom_cfg_get_status = &uslcom_get_status,
.ucom_cfg_set_dtr = &uslcom_set_dtr,
.ucom_cfg_set_rts = &uslcom_set_rts,
.ucom_cfg_set_break = &uslcom_set_break,
.ucom_ioctl = &uslcom_ioctl,
.ucom_cfg_param = &uslcom_param,
.ucom_pre_param = &uslcom_pre_param,
.ucom_start_read = &uslcom_start_read,
.ucom_stop_read = &uslcom_stop_read,
.ucom_start_write = &uslcom_start_write,
.ucom_stop_write = &uslcom_stop_write,
.ucom_poll = &uslcom_poll,
.ucom_free = &uslcom_free,
};
static const STRUCT_USB_HOST_ID uslcom_devs[] = {
#define USLCOM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
USLCOM_DEV(BALTECH, CARDREADER),
USLCOM_DEV(CLIPSAL, 5000CT2),
USLCOM_DEV(CLIPSAL, 5500PACA),
USLCOM_DEV(CLIPSAL, 5500PCU),
USLCOM_DEV(CLIPSAL, 560884),
USLCOM_DEV(CLIPSAL, 5800PC),
USLCOM_DEV(CLIPSAL, C5000CT2),
USLCOM_DEV(CLIPSAL, L51xx),
USLCOM_DEV(DATAAPEX, MULTICOM),
USLCOM_DEV(DELL, DW700),
USLCOM_DEV(DIGIANSWER, ZIGBEE802154),
USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2),
USLCOM_DEV(DYNASTREAM, ANT2USB),
USLCOM_DEV(ELV, USBI2C),
USLCOM_DEV(FESTO, CMSP),
USLCOM_DEV(FESTO, CPX_USB),
USLCOM_DEV(FOXCONN, PIRELLI_DP_L10),
USLCOM_DEV(FOXCONN, TCOM_TC_300),
USLCOM_DEV(GEMALTO, PROXPU),
USLCOM_DEV(JABLOTRON, PC60B),
USLCOM_DEV(KAMSTRUP, OPTICALEYE),
USLCOM_DEV(KAMSTRUP, MBUS_250D),
USLCOM_DEV(LAKESHORE, 121),
USLCOM_DEV(LAKESHORE, 218A),
USLCOM_DEV(LAKESHORE, 219),
USLCOM_DEV(LAKESHORE, 233),
USLCOM_DEV(LAKESHORE, 235),
USLCOM_DEV(LAKESHORE, 335),
USLCOM_DEV(LAKESHORE, 336),
USLCOM_DEV(LAKESHORE, 350),
USLCOM_DEV(LAKESHORE, 371),
USLCOM_DEV(LAKESHORE, 411),
USLCOM_DEV(LAKESHORE, 425),
USLCOM_DEV(LAKESHORE, 455A),
USLCOM_DEV(LAKESHORE, 465),
USLCOM_DEV(LAKESHORE, 475A),
USLCOM_DEV(LAKESHORE, 625A),
USLCOM_DEV(LAKESHORE, 642A),
USLCOM_DEV(LAKESHORE, 648),
USLCOM_DEV(LAKESHORE, 737),
USLCOM_DEV(LAKESHORE, 776),
USLCOM_DEV(LINKINSTRUMENTS, MSO19),
USLCOM_DEV(LINKINSTRUMENTS, MSO28),
USLCOM_DEV(LINKINSTRUMENTS, MSO28_2),
USLCOM_DEV(MEI, CASHFLOW_SC),
USLCOM_DEV(MEI, S2000),
USLCOM_DEV(NETGEAR, M4100),
USLCOM_DEV(OWEN, AC4),
USLCOM_DEV(OWL, CM_160),
USLCOM_DEV(PHILIPS, ACE1001),
USLCOM_DEV(PLX, CA42),
USLCOM_DEV(RENESAS, RX610),
USLCOM_DEV(SEL, C662),
USLCOM_DEV(SILABS, AC_SERV_CAN),
USLCOM_DEV(SILABS, AC_SERV_CIS),
USLCOM_DEV(SILABS, AC_SERV_IBUS),
USLCOM_DEV(SILABS, AC_SERV_OBD),
USLCOM_DEV(SILABS, AEROCOMM),
USLCOM_DEV(SILABS, AMBER_AMB2560),
USLCOM_DEV(SILABS, ARGUSISP),
USLCOM_DEV(SILABS, ARKHAM_DS101_A),
USLCOM_DEV(SILABS, ARKHAM_DS101_M),
USLCOM_DEV(SILABS, ARYGON_MIFARE),
USLCOM_DEV(SILABS, AVIT_USB_TTL),
USLCOM_DEV(SILABS, B_G_H3000),
USLCOM_DEV(SILABS, BALLUFF_RFID),
USLCOM_DEV(SILABS, BEI_VCP),
USLCOM_DEV(SILABS, BSM7DUSB),
USLCOM_DEV(SILABS, BURNSIDE),
USLCOM_DEV(SILABS, C2_EDGE_MODEM),
USLCOM_DEV(SILABS, CP2102),
USLCOM_DEV(SILABS, CP210X_2),
USLCOM_DEV(SILABS, CP210X_3),
USLCOM_DEV(SILABS, CP210X_4),
USLCOM_DEV(SILABS, CRUMB128),
USLCOM_DEV(SILABS, CYGNAL),
USLCOM_DEV(SILABS, CYGNAL_DEBUG),
USLCOM_DEV(SILABS, CYGNAL_GPS),
USLCOM_DEV(SILABS, DEGREE),
USLCOM_DEV(SILABS, DEKTEK_DTAPLUS),
USLCOM_DEV(SILABS, EMS_C1007),
USLCOM_DEV(SILABS, HAMLINKUSB),
USLCOM_DEV(SILABS, HELICOM),
USLCOM_DEV(SILABS, IMS_USB_RS422),
USLCOM_DEV(SILABS, INFINITY_MIC),
USLCOM_DEV(SILABS, INGENI_ZIGBEE),
USLCOM_DEV(SILABS, INSYS_MODEM),
USLCOM_DEV(SILABS, IRZ_SG10),
USLCOM_DEV(SILABS, KYOCERA_GPS),
USLCOM_DEV(SILABS, LIPOWSKY_HARP),
USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
USLCOM_DEV(SILABS, LIPOWSKY_LIN),
USLCOM_DEV(SILABS, MC35PU),
USLCOM_DEV(SILABS, MMB_ZIGBEE),
USLCOM_DEV(SILABS, MJS_TOSLINK),
USLCOM_DEV(SILABS, MSD_DASHHAWK),
USLCOM_DEV(SILABS, MULTIPLEX_RC),
USLCOM_DEV(SILABS, OPTRIS_MSPRO),
USLCOM_DEV(SILABS, POLOLU),
USLCOM_DEV(SILABS, PROCYON_AVS),
USLCOM_DEV(SILABS, SB_PARAMOUNT_ME),
USLCOM_DEV(SILABS, SUUNTO),
USLCOM_DEV(SILABS, TAMSMASTER),
USLCOM_DEV(SILABS, TELEGESIS_ETRX2),
USLCOM_DEV(SILABS, TRACIENT),
USLCOM_DEV(SILABS, TRAQMATE),
USLCOM_DEV(SILABS, USBCOUNT50),
USLCOM_DEV(SILABS, USBPULSE100),
USLCOM_DEV(SILABS, USBSCOPE50),
USLCOM_DEV(SILABS, USBWAVE12),
USLCOM_DEV(SILABS, V_PREON32),
USLCOM_DEV(SILABS, VSTABI),
USLCOM_DEV(SILABS, WAVIT),
USLCOM_DEV(SILABS, WMRBATT),
USLCOM_DEV(SILABS, WMRRIGBLASTER),
USLCOM_DEV(SILABS, WMRRIGTALK),
USLCOM_DEV(SILABS, ZEPHYR_BIO),
USLCOM_DEV(SILABS2, DCU11CLONE),
USLCOM_DEV(SILABS3, GPRS_MODEM),
USLCOM_DEV(SILABS4, 100EU_MODEM),
USLCOM_DEV(SYNTECH, CYPHERLAB100),
USLCOM_DEV(USI, MC60),
USLCOM_DEV(VAISALA, CABLE),
USLCOM_DEV(WAGO, SERVICECABLE),
USLCOM_DEV(WAVESENSE, JAZZ),
USLCOM_DEV(WESTMOUNTAIN, RIGBLASTER_ADVANTAGE),
USLCOM_DEV(WIENERPLEINBAUS, PL512),
USLCOM_DEV(WIENERPLEINBAUS, RCM),
USLCOM_DEV(WIENERPLEINBAUS, MPOD),
USLCOM_DEV(WIENERPLEINBAUS, CML),
#undef USLCOM_DEV
};
static device_method_t uslcom_methods[] = {
DEVMETHOD(device_probe, uslcom_probe),
DEVMETHOD(device_attach, uslcom_attach),
DEVMETHOD(device_detach, uslcom_detach),
DEVMETHOD_END
};
static devclass_t uslcom_devclass;
static driver_t uslcom_driver = {
.name = "uslcom",
.methods = uslcom_methods,
.size = sizeof(struct uslcom_softc),
};
DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
MODULE_DEPEND(uslcom, usb, 1, 1, 1);
MODULE_VERSION(uslcom, 1);
USB_PNP_HOST_INFO(uslcom_devs);
static void
uslcom_watchdog(void *arg)
{
struct uslcom_softc *sc = arg;
mtx_assert(&sc->sc_mtx, MA_OWNED);
usbd_transfer_start(sc->sc_xfer[USLCOM_CTRL_DT_RD]);
usb_callout_reset(&sc->sc_watchdog,
hz / 4, &uslcom_watchdog, sc);
}
static int
uslcom_probe(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
DPRINTFN(11, "\n");
if (uaa->usb_mode != USB_MODE_HOST) {
return (ENXIO);
}
if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
}
static int
uslcom_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct uslcom_softc *sc = device_get_softc(dev);
int error;
DPRINTFN(11, "\n");
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
sc->sc_udev = uaa->device;
/* use the interface number from the USB interface descriptor */
sc->sc_iface_no = uaa->info.bIfaceNum;
error = usbd_transfer_setup(uaa->device,
&uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
DPRINTF("one or more missing USB endpoints, "
"error=%s\n", usbd_errstr(error));
goto detach;
}
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
sc->sc_partnum = uslcom_get_partnum(sc);
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&uslcom_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0);
detach:
uslcom_detach(dev);
return (ENXIO);
}
static int
uslcom_detach(device_t dev)
{
struct uslcom_softc *sc = device_get_softc(dev);
DPRINTF("sc=%p\n", sc);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
usb_callout_drain(&sc->sc_watchdog);
device_claim_softc(dev);
uslcom_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(uslcom);
static void
uslcom_free_softc(struct uslcom_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
uslcom_free(struct ucom_softc *ucom)
{
uslcom_free_softc(ucom->sc_parent);
}
static void
uslcom_open(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_IFC_ENABLE;
USETW(req.wValue, USLCOM_IFC_ENABLE_EN);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("UART enable failed (ignored)\n");
}
/* start polling status */
uslcom_watchdog(sc);
}
static void
uslcom_close(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
/* stop polling status */
usb_callout_stop(&sc->sc_watchdog);
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_IFC_ENABLE;
USETW(req.wValue, USLCOM_IFC_ENABLE_DIS);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("UART disable failed (ignored)\n");
}
}
static uint8_t
uslcom_get_partnum(struct uslcom_softc *sc)
{
struct usb_device_request req;
uint8_t partnum;
/* Find specific chip type */
partnum = 0;
req.bmRequestType = USLCOM_READ;
req.bRequest = USLCOM_VENDOR_SPECIFIC;
USETW(req.wValue, USLCOM_GET_PARTNUM);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, sizeof(partnum));
if (usbd_do_request_flags(sc->sc_udev, NULL,
&req, &partnum, 0, NULL, 1000)) {
DPRINTF("GET_PARTNUM failed\n");
}
return(partnum);
}
static void
uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
uint16_t ctl;
DPRINTF("onoff = %d\n", onoff);
ctl = onoff ? USLCOM_MHS_DTR_ON : 0;
ctl |= USLCOM_MHS_DTR_SET;
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_SET_MHS;
USETW(req.wValue, ctl);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("Setting DTR failed (ignored)\n");
}
}
static void
uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
uint16_t ctl;
DPRINTF("onoff = %d\n", onoff);
ctl = onoff ? USLCOM_MHS_RTS_ON : 0;
ctl |= USLCOM_MHS_RTS_SET;
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_SET_MHS;
USETW(req.wValue, ctl);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("Setting DTR failed (ignored)\n");
}
}
static int
uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
return (EINVAL);
return (0);
}
static void
uslcom_param(struct ucom_softc *ucom, struct termios *t)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
uint32_t baudrate, flowctrl[4];
uint16_t data;
DPRINTF("\n");
baudrate = t->c_ospeed;
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_SET_BAUDRATE;
USETW(req.wValue, 0);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, sizeof(baudrate));
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, &baudrate, 0, 1000)) {
DPRINTF("Set baudrate failed (ignored)\n");
}
if (t->c_cflag & CSTOPB)
data = USLCOM_STOP_BITS_2;
else
data = USLCOM_STOP_BITS_1;
if (t->c_cflag & PARENB) {
if (t->c_cflag & PARODD)
data |= USLCOM_PARITY_ODD;
else
data |= USLCOM_PARITY_EVEN;
} else
data |= USLCOM_PARITY_NONE;
switch (t->c_cflag & CSIZE) {
case CS5:
data |= USLCOM_SET_DATA_BITS(5);
break;
case CS6:
data |= USLCOM_SET_DATA_BITS(6);
break;
case CS7:
data |= USLCOM_SET_DATA_BITS(7);
break;
case CS8:
data |= USLCOM_SET_DATA_BITS(8);
break;
}
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_SET_LINE_CTL;
USETW(req.wValue, data);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("Set format failed (ignored)\n");
}
if (t->c_cflag & CRTSCTS) {
flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON | USLCOM_FLOW_CTS_HS);
flowctrl[1] = htole32(USLCOM_FLOW_RTS_HS);
} else {
flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON);
flowctrl[1] = htole32(USLCOM_FLOW_RTS_ON);
}
flowctrl[2] = 0;
flowctrl[3] = 0;
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_SET_FLOW;
USETW(req.wValue, 0);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, sizeof(flowctrl));
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, flowctrl, 0, 1000)) {
DPRINTF("Set flowcontrol failed (ignored)\n");
}
}
static void
uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct uslcom_softc *sc = ucom->sc_parent;
DPRINTF("\n");
/* XXX Note: sc_lsr is always zero */
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
uint16_t brk = onoff ? USLCOM_SET_BREAK_ON : USLCOM_SET_BREAK_OFF;
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_SET_BREAK;
USETW(req.wValue, brk);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("Set BREAK failed (ignored)\n");
}
}
static int
uslcom_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
int flag, struct thread *td)
{
struct uslcom_softc *sc = ucom->sc_parent;
struct usb_device_request req;
int error = 0;
uint8_t latch;
DPRINTF("cmd=0x%08x\n", cmd);
switch (cmd) {
case USB_GET_GPIO:
if (sc->sc_partnum < USLCOM_PARTNUM_CP2103) {
error = ENODEV;
break;
}
req.bmRequestType = USLCOM_READ;
req.bRequest = USLCOM_VENDOR_SPECIFIC;
USETW(req.wValue, USLCOM_READ_LATCH);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, sizeof(latch));
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, &latch, 0, 1000)) {
DPRINTF("Get LATCH failed\n");
error = EIO;
}
*(int *)data = latch;
break;
case USB_SET_GPIO:
if (sc->sc_partnum < USLCOM_PARTNUM_CP2103)
error = ENODEV;
else if ((sc->sc_partnum == USLCOM_PARTNUM_CP2103) ||
(sc->sc_partnum == USLCOM_PARTNUM_CP2104)) {
req.bmRequestType = USLCOM_WRITE;
req.bRequest = USLCOM_VENDOR_SPECIFIC;
USETW(req.wValue, USLCOM_WRITE_LATCH);
USETW(req.wIndex, (*(int *)data));
USETW(req.wLength, 0);
if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000)) {
DPRINTF("Set LATCH failed\n");
error = EIO;
}
} else
error = ENODEV; /* Not yet */
break;
default:
DPRINTF("Unknown IOCTL\n");
error = ENOIOCTL;
break;
}
return (error);
}
static void
uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uslcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
USLCOM_BULK_BUF_SIZE, &actlen)) {
DPRINTF("actlen = %d\n", actlen);
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uslcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uslcom_control_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uslcom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
struct usb_device_request req;
uint8_t msr = 0;
uint8_t buf;
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 1);
usbd_copy_out(pc, 0, &buf, sizeof(buf));
if (buf & USLCOM_MHS_CTS)
msr |= SER_CTS;
if (buf & USLCOM_MHS_DSR)
msr |= SER_DSR;
if (buf & USLCOM_MHS_RI)
msr |= SER_RI;
if (buf & USLCOM_MHS_DCD)
msr |= SER_DCD;
if (msr != sc->sc_msr) {
DPRINTF("status change msr=0x%02x "
"(was 0x%02x)\n", msr, sc->sc_msr);
sc->sc_msr = msr;
ucom_status_change(&sc->sc_ucom);
}
break;
case USB_ST_SETUP:
req.bmRequestType = USLCOM_READ;
req.bRequest = USLCOM_GET_MDMSTS;
USETW(req.wValue, 0);
USETW(req.wIndex, sc->sc_iface_no);
USETW(req.wLength, sizeof(buf));
usbd_xfer_set_frames(xfer, 2);
usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_in(pc, 0, &req, sizeof(req));
usbd_transfer_submit(xfer);
break;
default: /* error */
if (error != USB_ERR_CANCELLED)
DPRINTF("error=%s\n", usbd_errstr(error));
break;
}
}
static void
uslcom_start_read(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
/* start read endpoint */
usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
}
static void
uslcom_stop_read(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
/* stop read endpoint */
usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
}
static void
uslcom_start_write(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
}
static void
uslcom_stop_write(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
}
static void
uslcom_poll(struct ucom_softc *ucom)
{
struct uslcom_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
}

View File

@ -0,0 +1,679 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $ */
/* $FreeBSD$ */
/* Also already merged from NetBSD:
* $NetBSD: uvisor.c,v 1.12 2001/11/13 06:24:57 lukem Exp $
* $NetBSD: uvisor.c,v 1.13 2002/02/11 15:11:49 augustss Exp $
* $NetBSD: uvisor.c,v 1.14 2002/02/27 23:00:03 augustss Exp $
* $NetBSD: uvisor.c,v 1.15 2002/06/16 15:01:31 augustss Exp $
* $NetBSD: uvisor.c,v 1.16 2002/07/11 21:14:36 augustss Exp $
* $NetBSD: uvisor.c,v 1.17 2002/08/13 11:38:15 augustss Exp $
* $NetBSD: uvisor.c,v 1.18 2003/02/05 00:50:14 augustss Exp $
* $NetBSD: uvisor.c,v 1.19 2003/02/07 18:12:37 augustss Exp $
* $NetBSD: uvisor.c,v 1.20 2003/04/11 01:30:10 simonb Exp $
*/
/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Handspring Visor (Palmpilot compatible PDA) driver
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR uvisor_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int uvisor_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RWTUN,
&uvisor_debug, 0, "Debug level");
#endif
#define UVISOR_CONFIG_INDEX 0
#define UVISOR_IFACE_INDEX 0
/*
* The following buffer sizes are hardcoded due to the way the Palm
* firmware works. It looks like the device is not short terminating
* the data transferred.
*/
#define UVISORIBUFSIZE 0 /* Use wMaxPacketSize */
#define UVISOROBUFSIZE 32 /* bytes */
#define UVISOROFRAMES 32 /* units */
/* From the Linux driver */
/*
* UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
* are available to be transferred to the host for the specified endpoint.
* Currently this is not used, and always returns 0x0001
*/
#define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
/*
* UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
* is now closing the pipe. An empty packet is sent in response.
*/
#define UVISOR_CLOSE_NOTIFICATION 0x02
/*
* UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
* get the endpoints used by the connection.
*/
#define UVISOR_GET_CONNECTION_INFORMATION 0x03
/*
* UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
*/
#define UVISOR_MAX_CONN 8
struct uvisor_connection_info {
uWord num_ports;
struct {
uByte port_function_id;
uByte port;
} __packed connections[UVISOR_MAX_CONN];
} __packed;
#define UVISOR_CONNECTION_INFO_SIZE 18
/* struct uvisor_connection_info.connection[x].port defines: */
#define UVISOR_ENDPOINT_1 0x01
#define UVISOR_ENDPOINT_2 0x02
/* struct uvisor_connection_info.connection[x].port_function_id defines: */
#define UVISOR_FUNCTION_GENERIC 0x00
#define UVISOR_FUNCTION_DEBUGGER 0x01
#define UVISOR_FUNCTION_HOTSYNC 0x02
#define UVISOR_FUNCTION_CONSOLE 0x03
#define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
/*
* Unknown PalmOS stuff.
*/
#define UVISOR_GET_PALM_INFORMATION 0x04
#define UVISOR_GET_PALM_INFORMATION_LEN 0x44
struct uvisor_palm_connection_info {
uByte num_ports;
uByte endpoint_numbers_different;
uWord reserved1;
struct {
uDWord port_function_id;
uByte port;
uByte end_point_info;
uWord reserved;
} __packed connections[UVISOR_MAX_CONN];
} __packed;
enum {
UVISOR_BULK_DT_WR,
UVISOR_BULK_DT_RD,
UVISOR_N_TRANSFER,
};
struct uvisor_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UVISOR_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint16_t sc_flag;
#define UVISOR_FLAG_PALM4 0x0001
#define UVISOR_FLAG_VISOR 0x0002
#define UVISOR_FLAG_PALM35 0x0004
#define UVISOR_FLAG_SEND_NOTIFY 0x0008
uint8_t sc_iface_no;
uint8_t sc_iface_index;
};
/* prototypes */
static device_probe_t uvisor_probe;
static device_attach_t uvisor_attach;
static device_detach_t uvisor_detach;
static void uvisor_free_softc(struct uvisor_softc *);
static usb_callback_t uvisor_write_callback;
static usb_callback_t uvisor_read_callback;
static usb_error_t uvisor_init(struct uvisor_softc *, struct usb_device *,
struct usb_config *);
static void uvisor_free(struct ucom_softc *);
static void uvisor_cfg_open(struct ucom_softc *);
static void uvisor_cfg_close(struct ucom_softc *);
static void uvisor_start_read(struct ucom_softc *);
static void uvisor_stop_read(struct ucom_softc *);
static void uvisor_start_write(struct ucom_softc *);
static void uvisor_stop_write(struct ucom_softc *);
static const struct usb_config uvisor_config[UVISOR_N_TRANSFER] = {
[UVISOR_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UVISOROBUFSIZE * UVISOROFRAMES,
.frames = UVISOROFRAMES,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &uvisor_write_callback,
},
[UVISOR_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UVISORIBUFSIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &uvisor_read_callback,
},
};
static const struct ucom_callback uvisor_callback = {
.ucom_cfg_open = &uvisor_cfg_open,
.ucom_cfg_close = &uvisor_cfg_close,
.ucom_start_read = &uvisor_start_read,
.ucom_stop_read = &uvisor_stop_read,
.ucom_start_write = &uvisor_start_write,
.ucom_stop_write = &uvisor_stop_write,
.ucom_free = &uvisor_free,
};
static device_method_t uvisor_methods[] = {
DEVMETHOD(device_probe, uvisor_probe),
DEVMETHOD(device_attach, uvisor_attach),
DEVMETHOD(device_detach, uvisor_detach),
DEVMETHOD_END
};
static devclass_t uvisor_devclass;
static driver_t uvisor_driver = {
.name = "uvisor",
.methods = uvisor_methods,
.size = sizeof(struct uvisor_softc),
};
static const STRUCT_USB_HOST_ID uvisor_devs[] = {
#define UVISOR_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
UVISOR_DEV(ACEECA, MEZ1000, UVISOR_FLAG_PALM4),
UVISOR_DEV(ALPHASMART, DANA_SYNC, UVISOR_FLAG_PALM4),
UVISOR_DEV(GARMIN, IQUE_3600, UVISOR_FLAG_PALM4),
UVISOR_DEV(FOSSIL, WRISTPDA, UVISOR_FLAG_PALM4),
UVISOR_DEV(HANDSPRING, VISOR, UVISOR_FLAG_VISOR),
UVISOR_DEV(HANDSPRING, TREO, UVISOR_FLAG_PALM4),
UVISOR_DEV(HANDSPRING, TREO600, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, M500, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, M505, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, M515, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, I705, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, M125, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, M130, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, TUNGSTEN_Z, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, TUNGSTEN_T, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, ZIRE, UVISOR_FLAG_PALM4),
UVISOR_DEV(PALM, ZIRE31, UVISOR_FLAG_PALM4),
UVISOR_DEV(SAMSUNG, I500, UVISOR_FLAG_PALM4),
UVISOR_DEV(SONY, CLIE_40, 0),
UVISOR_DEV(SONY, CLIE_41, 0),
UVISOR_DEV(SONY, CLIE_S360, UVISOR_FLAG_PALM4),
UVISOR_DEV(SONY, CLIE_NX60, UVISOR_FLAG_PALM4),
UVISOR_DEV(SONY, CLIE_35, UVISOR_FLAG_PALM35),
/* UVISOR_DEV(SONY, CLIE_25, UVISOR_FLAG_PALM4 ), */
UVISOR_DEV(SONY, CLIE_TJ37, UVISOR_FLAG_PALM4),
/* UVISOR_DEV(SONY, CLIE_TH55, UVISOR_FLAG_PALM4 ), See PR 80935 */
UVISOR_DEV(TAPWAVE, ZODIAC, UVISOR_FLAG_PALM4),
#undef UVISOR_DEV
};
DRIVER_MODULE(uvisor, uhub, uvisor_driver, uvisor_devclass, NULL, 0);
MODULE_DEPEND(uvisor, ucom, 1, 1, 1);
MODULE_DEPEND(uvisor, usb, 1, 1, 1);
MODULE_VERSION(uvisor, 1);
USB_PNP_HOST_INFO(uvisor_devs);
static int
uvisor_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 != UVISOR_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UVISOR_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(uvisor_devs, sizeof(uvisor_devs), uaa));
}
static int
uvisor_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct uvisor_softc *sc = device_get_softc(dev);
struct usb_config uvisor_config_copy[UVISOR_N_TRANSFER];
int error;
DPRINTF("sc=%p\n", sc);
memcpy(uvisor_config_copy, uvisor_config,
sizeof(uvisor_config_copy));
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "uvisor", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_udev = uaa->device;
/* configure the device */
sc->sc_flag = USB_GET_DRIVER_INFO(uaa);
sc->sc_iface_no = uaa->info.bIfaceNum;
sc->sc_iface_index = UVISOR_IFACE_INDEX;
error = uvisor_init(sc, uaa->device, uvisor_config_copy);
if (error) {
DPRINTF("init failed, error=%s\n",
usbd_errstr(error));
goto detach;
}
error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
sc->sc_xfer, uvisor_config_copy, UVISOR_N_TRANSFER,
sc, &sc->sc_mtx);
if (error) {
DPRINTF("could not allocate all pipes\n");
goto detach;
}
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&uvisor_callback, &sc->sc_mtx);
if (error) {
DPRINTF("ucom_attach failed\n");
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
return (0);
detach:
uvisor_detach(dev);
return (ENXIO);
}
static int
uvisor_detach(device_t dev)
{
struct uvisor_softc *sc = device_get_softc(dev);
DPRINTF("sc=%p\n", sc);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UVISOR_N_TRANSFER);
device_claim_softc(dev);
uvisor_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(uvisor);
static void
uvisor_free_softc(struct uvisor_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
uvisor_free(struct ucom_softc *ucom)
{
uvisor_free_softc(ucom->sc_parent);
}
static usb_error_t
uvisor_init(struct uvisor_softc *sc, struct usb_device *udev, struct usb_config *config)
{
usb_error_t err = 0;
struct usb_device_request req;
struct uvisor_connection_info coninfo;
struct uvisor_palm_connection_info pconinfo;
uint16_t actlen;
uint8_t buffer[256];
if (sc->sc_flag & UVISOR_FLAG_VISOR) {
DPRINTF("getting connection info\n");
req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
err = usbd_do_request_flags(udev, NULL,
&req, &coninfo, USB_SHORT_XFER_OK,
&actlen, USB_DEFAULT_TIMEOUT);
if (err) {
goto done;
}
}
#ifdef USB_DEBUG
if (sc->sc_flag & UVISOR_FLAG_VISOR) {
uint16_t i, np;
const char *desc;
np = UGETW(coninfo.num_ports);
if (np > UVISOR_MAX_CONN) {
np = UVISOR_MAX_CONN;
}
DPRINTF("Number of ports: %d\n", np);
for (i = 0; i < np; ++i) {
switch (coninfo.connections[i].port_function_id) {
case UVISOR_FUNCTION_GENERIC:
desc = "Generic";
break;
case UVISOR_FUNCTION_DEBUGGER:
desc = "Debugger";
break;
case UVISOR_FUNCTION_HOTSYNC:
desc = "HotSync";
break;
case UVISOR_FUNCTION_REMOTE_FILE_SYS:
desc = "Remote File System";
break;
default:
desc = "unknown";
break;
}
DPRINTF("Port %d is for %s\n",
coninfo.connections[i].port, desc);
}
}
#endif
if (sc->sc_flag & UVISOR_FLAG_PALM4) {
uint8_t port;
/* Palm OS 4.0 Hack */
req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
req.bRequest = UVISOR_GET_PALM_INFORMATION;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
err = usbd_do_request_flags
(udev, NULL, &req, &pconinfo, USB_SHORT_XFER_OK,
&actlen, USB_DEFAULT_TIMEOUT);
if (err) {
goto done;
}
if (actlen < 12) {
DPRINTF("too little data\n");
err = USB_ERR_INVAL;
goto done;
}
if (pconinfo.endpoint_numbers_different) {
port = pconinfo.connections[0].end_point_info;
config[0].endpoint = (port & 0xF); /* output */
config[1].endpoint = (port >> 4); /* input */
} else {
port = pconinfo.connections[0].port;
config[0].endpoint = (port & 0xF); /* output */
config[1].endpoint = (port & 0xF); /* input */
}
#if 0
req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
req.bRequest = UVISOR_GET_PALM_INFORMATION;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
err = usbd_do_request(udev, &req, buffer);
if (err) {
goto done;
}
#endif
}
if (sc->sc_flag & UVISOR_FLAG_PALM35) {
/* get the config number */
DPRINTF("getting config info\n");
req.bmRequestType = UT_READ;
req.bRequest = UR_GET_CONFIG;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, 1);
err = usbd_do_request(udev, NULL, &req, buffer);
if (err) {
goto done;
}
/* get the interface number */
DPRINTF("get the interface number\n");
req.bmRequestType = UT_READ_DEVICE;
req.bRequest = UR_GET_INTERFACE;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, 1);
err = usbd_do_request(udev, NULL, &req, buffer);
if (err) {
goto done;
}
}
#if 0
uWord wAvail;
DPRINTF("getting available bytes\n");
req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
USETW(req.wValue, 0);
USETW(req.wIndex, 5);
USETW(req.wLength, sizeof(wAvail));
err = usbd_do_request(udev, NULL, &req, &wAvail);
if (err) {
goto done;
}
DPRINTF("avail=%d\n", UGETW(wAvail));
#endif
DPRINTF("done\n");
done:
return (err);
}
static void
uvisor_cfg_open(struct ucom_softc *ucom)
{
return;
}
static void
uvisor_cfg_close(struct ucom_softc *ucom)
{
struct uvisor_softc *sc = ucom->sc_parent;
uint8_t buffer[UVISOR_CONNECTION_INFO_SIZE];
struct usb_device_request req;
usb_error_t err;
req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
req.bRequest = UVISOR_CLOSE_NOTIFICATION;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, buffer, 0, 1000);
if (err) {
DPRINTFN(0, "close notification failed, error=%s\n",
usbd_errstr(err));
}
}
static void
uvisor_start_read(struct ucom_softc *ucom)
{
struct uvisor_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_RD]);
}
static void
uvisor_stop_read(struct ucom_softc *ucom)
{
struct uvisor_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_RD]);
}
static void
uvisor_start_write(struct ucom_softc *ucom)
{
struct uvisor_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UVISOR_BULK_DT_WR]);
}
static void
uvisor_stop_write(struct ucom_softc *ucom)
{
struct uvisor_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UVISOR_BULK_DT_WR]);
}
static void
uvisor_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uvisor_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
uint8_t x;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
for (x = 0; x != UVISOROFRAMES; x++) {
usbd_xfer_set_frame_offset(xfer,
x * UVISOROBUFSIZE, x);
pc = usbd_xfer_get_frame(xfer, x);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
UVISOROBUFSIZE, &actlen)) {
usbd_xfer_set_frame_len(xfer, x, actlen);
} else {
break;
}
}
/* check for data */
if (x != 0) {
usbd_xfer_set_frames(xfer, x);
usbd_transfer_submit(xfer);
}
break;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
break;
}
}
static void
uvisor_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uvisor_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}

View File

@ -0,0 +1,773 @@
#include <machine/rtems-bsd-kernel-space.h>
/* $NetBSD: usb/uvscom.c,v 1.1 2002/03/19 15:08:42 augustss Exp $ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* uvscom: SUNTAC Slipper U VS-10U driver.
* Slipper U is a PC Card to USB converter for data communication card
* adapter. It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
* P-in m@ater and various data communication card adapters.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>
#define USB_DEBUG_VAR uvscom_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/serial/usb_serial.h>
#ifdef USB_DEBUG
static int uvscom_debug = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, uvscom, CTLFLAG_RW, 0, "USB uvscom");
SYSCTL_INT(_hw_usb_uvscom, OID_AUTO, debug, CTLFLAG_RWTUN,
&uvscom_debug, 0, "Debug level");
#endif
#define UVSCOM_MODVER 1 /* module version */
#define UVSCOM_CONFIG_INDEX 0
#define UVSCOM_IFACE_INDEX 0
/* Request */
#define UVSCOM_SET_SPEED 0x10
#define UVSCOM_LINE_CTL 0x11
#define UVSCOM_SET_PARAM 0x12
#define UVSCOM_READ_STATUS 0xd0
#define UVSCOM_SHUTDOWN 0xe0
/* UVSCOM_SET_SPEED parameters */
#define UVSCOM_SPEED_150BPS 0x00
#define UVSCOM_SPEED_300BPS 0x01
#define UVSCOM_SPEED_600BPS 0x02
#define UVSCOM_SPEED_1200BPS 0x03
#define UVSCOM_SPEED_2400BPS 0x04
#define UVSCOM_SPEED_4800BPS 0x05
#define UVSCOM_SPEED_9600BPS 0x06
#define UVSCOM_SPEED_19200BPS 0x07
#define UVSCOM_SPEED_38400BPS 0x08
#define UVSCOM_SPEED_57600BPS 0x09
#define UVSCOM_SPEED_115200BPS 0x0a
/* UVSCOM_LINE_CTL parameters */
#define UVSCOM_BREAK 0x40
#define UVSCOM_RTS 0x02
#define UVSCOM_DTR 0x01
#define UVSCOM_LINE_INIT 0x08
/* UVSCOM_SET_PARAM parameters */
#define UVSCOM_DATA_MASK 0x03
#define UVSCOM_DATA_BIT_8 0x03
#define UVSCOM_DATA_BIT_7 0x02
#define UVSCOM_DATA_BIT_6 0x01
#define UVSCOM_DATA_BIT_5 0x00
#define UVSCOM_STOP_MASK 0x04
#define UVSCOM_STOP_BIT_2 0x04
#define UVSCOM_STOP_BIT_1 0x00
#define UVSCOM_PARITY_MASK 0x18
#define UVSCOM_PARITY_EVEN 0x18
#define UVSCOM_PARITY_ODD 0x08
#define UVSCOM_PARITY_NONE 0x00
/* Status bits */
#define UVSCOM_TXRDY 0x04
#define UVSCOM_RXRDY 0x01
#define UVSCOM_DCD 0x08
#define UVSCOM_NOCARD 0x04
#define UVSCOM_DSR 0x02
#define UVSCOM_CTS 0x01
#define UVSCOM_USTAT_MASK (UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
#define UVSCOM_BULK_BUF_SIZE 1024 /* bytes */
enum {
UVSCOM_BULK_DT_WR,
UVSCOM_BULK_DT_RD,
UVSCOM_INTR_DT_RD,
UVSCOM_N_TRANSFER,
};
struct uvscom_softc {
struct ucom_super_softc sc_super_ucom;
struct ucom_softc sc_ucom;
struct usb_xfer *sc_xfer[UVSCOM_N_TRANSFER];
struct usb_device *sc_udev;
struct mtx sc_mtx;
uint16_t sc_line; /* line control register */
uint8_t sc_iface_no; /* interface number */
uint8_t sc_iface_index; /* interface index */
uint8_t sc_lsr; /* local status register */
uint8_t sc_msr; /* uvscom status register */
uint8_t sc_unit_status; /* unit status */
};
/* prototypes */
static device_probe_t uvscom_probe;
static device_attach_t uvscom_attach;
static device_detach_t uvscom_detach;
static void uvscom_free_softc(struct uvscom_softc *);
static usb_callback_t uvscom_write_callback;
static usb_callback_t uvscom_read_callback;
static usb_callback_t uvscom_intr_callback;
static void uvscom_free(struct ucom_softc *);
static void uvscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void uvscom_cfg_set_rts(struct ucom_softc *, uint8_t);
static void uvscom_cfg_set_break(struct ucom_softc *, uint8_t);
static int uvscom_pre_param(struct ucom_softc *, struct termios *);
static void uvscom_cfg_param(struct ucom_softc *, struct termios *);
static int uvscom_pre_open(struct ucom_softc *);
static void uvscom_cfg_open(struct ucom_softc *);
static void uvscom_cfg_close(struct ucom_softc *);
static void uvscom_start_read(struct ucom_softc *);
static void uvscom_stop_read(struct ucom_softc *);
static void uvscom_start_write(struct ucom_softc *);
static void uvscom_stop_write(struct ucom_softc *);
static void uvscom_cfg_get_status(struct ucom_softc *, uint8_t *,
uint8_t *);
static void uvscom_cfg_write(struct uvscom_softc *, uint8_t, uint16_t);
static uint16_t uvscom_cfg_read_status(struct uvscom_softc *);
static void uvscom_poll(struct ucom_softc *ucom);
static const struct usb_config uvscom_config[UVSCOM_N_TRANSFER] = {
[UVSCOM_BULK_DT_WR] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = UVSCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
.callback = &uvscom_write_callback,
},
[UVSCOM_BULK_DT_RD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = UVSCOM_BULK_BUF_SIZE,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.callback = &uvscom_read_callback,
},
[UVSCOM_INTR_DT_RD] = {
.type = UE_INTERRUPT,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
.bufsize = 0, /* use wMaxPacketSize */
.callback = &uvscom_intr_callback,
},
};
static const struct ucom_callback uvscom_callback = {
.ucom_cfg_get_status = &uvscom_cfg_get_status,
.ucom_cfg_set_dtr = &uvscom_cfg_set_dtr,
.ucom_cfg_set_rts = &uvscom_cfg_set_rts,
.ucom_cfg_set_break = &uvscom_cfg_set_break,
.ucom_cfg_param = &uvscom_cfg_param,
.ucom_cfg_open = &uvscom_cfg_open,
.ucom_cfg_close = &uvscom_cfg_close,
.ucom_pre_open = &uvscom_pre_open,
.ucom_pre_param = &uvscom_pre_param,
.ucom_start_read = &uvscom_start_read,
.ucom_stop_read = &uvscom_stop_read,
.ucom_start_write = &uvscom_start_write,
.ucom_stop_write = &uvscom_stop_write,
.ucom_poll = &uvscom_poll,
.ucom_free = &uvscom_free,
};
static const STRUCT_USB_HOST_ID uvscom_devs[] = {
/* SUNTAC U-Cable type A4 */
{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4, 0)},
/* SUNTAC U-Cable type D2 */
{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L, 0)},
/* SUNTAC Ir-Trinity */
{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U, 0)},
/* SUNTAC U-Cable type P1 */
{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1, 0)},
/* SUNTAC Slipper U */
{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U, 0)},
};
static device_method_t uvscom_methods[] = {
DEVMETHOD(device_probe, uvscom_probe),
DEVMETHOD(device_attach, uvscom_attach),
DEVMETHOD(device_detach, uvscom_detach),
DEVMETHOD_END
};
static devclass_t uvscom_devclass;
static driver_t uvscom_driver = {
.name = "uvscom",
.methods = uvscom_methods,
.size = sizeof(struct uvscom_softc),
};
DRIVER_MODULE(uvscom, uhub, uvscom_driver, uvscom_devclass, NULL, 0);
MODULE_DEPEND(uvscom, ucom, 1, 1, 1);
MODULE_DEPEND(uvscom, usb, 1, 1, 1);
MODULE_VERSION(uvscom, UVSCOM_MODVER);
USB_PNP_HOST_INFO(uvscom_devs);
static int
uvscom_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 != UVSCOM_CONFIG_INDEX) {
return (ENXIO);
}
if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
return (ENXIO);
}
return (usbd_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
}
static int
uvscom_attach(device_t dev)
{
struct usb_attach_arg *uaa = device_get_ivars(dev);
struct uvscom_softc *sc = device_get_softc(dev);
int error;
device_set_usb_desc(dev);
mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
ucom_ref(&sc->sc_super_ucom);
sc->sc_udev = uaa->device;
DPRINTF("sc=%p\n", sc);
sc->sc_iface_no = uaa->info.bIfaceNum;
sc->sc_iface_index = UVSCOM_IFACE_INDEX;
error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
if (error) {
DPRINTF("could not allocate all USB transfers!\n");
goto detach;
}
sc->sc_line = UVSCOM_LINE_INIT;
/* clear stall at first run */
mtx_lock(&sc->sc_mtx);
usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
mtx_unlock(&sc->sc_mtx);
error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
&uvscom_callback, &sc->sc_mtx);
if (error) {
goto detach;
}
ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
/* start interrupt pipe */
mtx_lock(&sc->sc_mtx);
usbd_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
mtx_unlock(&sc->sc_mtx);
return (0);
detach:
uvscom_detach(dev);
return (ENXIO);
}
static int
uvscom_detach(device_t dev)
{
struct uvscom_softc *sc = device_get_softc(dev);
DPRINTF("sc=%p\n", sc);
/* stop interrupt pipe */
if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
usbd_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
usbd_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
device_claim_softc(dev);
uvscom_free_softc(sc);
return (0);
}
UCOM_UNLOAD_DRAIN(uvscom);
static void
uvscom_free_softc(struct uvscom_softc *sc)
{
if (ucom_unref(&sc->sc_super_ucom)) {
mtx_destroy(&sc->sc_mtx);
device_free_softc(sc);
}
}
static void
uvscom_free(struct ucom_softc *ucom)
{
uvscom_free_softc(ucom->sc_parent);
}
static void
uvscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uvscom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint32_t actlen;
switch (USB_GET_STATE(xfer)) {
case USB_ST_SETUP:
case USB_ST_TRANSFERRED:
tr_setup:
pc = usbd_xfer_get_frame(xfer, 0);
if (ucom_get_data(&sc->sc_ucom, pc, 0,
UVSCOM_BULK_BUF_SIZE, &actlen)) {
usbd_xfer_set_frame_len(xfer, 0, actlen);
usbd_transfer_submit(xfer);
}
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uvscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uvscom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
pc = usbd_xfer_get_frame(xfer, 0);
ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
struct uvscom_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
uint8_t buf[2];
int actlen;
usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
switch (USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
if (actlen >= 2) {
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_out(pc, 0, buf, sizeof(buf));
sc->sc_lsr = 0;
sc->sc_msr = 0;
sc->sc_unit_status = buf[1];
if (buf[0] & UVSCOM_TXRDY) {
sc->sc_lsr |= ULSR_TXRDY;
}
if (buf[0] & UVSCOM_RXRDY) {
sc->sc_lsr |= ULSR_RXRDY;
}
if (buf[1] & UVSCOM_CTS) {
sc->sc_msr |= SER_CTS;
}
if (buf[1] & UVSCOM_DSR) {
sc->sc_msr |= SER_DSR;
}
if (buf[1] & UVSCOM_DCD) {
sc->sc_msr |= SER_DCD;
}
/*
* the UCOM layer will ignore this call if the TTY
* device is closed!
*/
ucom_status_change(&sc->sc_ucom);
}
case USB_ST_SETUP:
tr_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
return;
default: /* Error */
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto tr_setup;
}
return;
}
}
static void
uvscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
struct uvscom_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
if (onoff)
sc->sc_line |= UVSCOM_DTR;
else
sc->sc_line &= ~UVSCOM_DTR;
uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
}
static void
uvscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
struct uvscom_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
if (onoff)
sc->sc_line |= UVSCOM_RTS;
else
sc->sc_line &= ~UVSCOM_RTS;
uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
}
static void
uvscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
struct uvscom_softc *sc = ucom->sc_parent;
DPRINTF("onoff = %d\n", onoff);
if (onoff)
sc->sc_line |= UVSCOM_BREAK;
else
sc->sc_line &= ~UVSCOM_BREAK;
uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
}
static int
uvscom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
switch (t->c_ospeed) {
case B150:
case B300:
case B600:
case B1200:
case B2400:
case B4800:
case B9600:
case B19200:
case B38400:
case B57600:
case B115200:
default:
return (EINVAL);
}
return (0);
}
static void
uvscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
struct uvscom_softc *sc = ucom->sc_parent;
uint16_t value;
DPRINTF("\n");
switch (t->c_ospeed) {
case B150:
value = UVSCOM_SPEED_150BPS;
break;
case B300:
value = UVSCOM_SPEED_300BPS;
break;
case B600:
value = UVSCOM_SPEED_600BPS;
break;
case B1200:
value = UVSCOM_SPEED_1200BPS;
break;
case B2400:
value = UVSCOM_SPEED_2400BPS;
break;
case B4800:
value = UVSCOM_SPEED_4800BPS;
break;
case B9600:
value = UVSCOM_SPEED_9600BPS;
break;
case B19200:
value = UVSCOM_SPEED_19200BPS;
break;
case B38400:
value = UVSCOM_SPEED_38400BPS;
break;
case B57600:
value = UVSCOM_SPEED_57600BPS;
break;
case B115200:
value = UVSCOM_SPEED_115200BPS;
break;
default:
return;
}
uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
value = 0;
if (t->c_cflag & CSTOPB) {
value |= UVSCOM_STOP_BIT_2;
}
if (t->c_cflag & PARENB) {
if (t->c_cflag & PARODD) {
value |= UVSCOM_PARITY_ODD;
} else {
value |= UVSCOM_PARITY_EVEN;
}
} else {
value |= UVSCOM_PARITY_NONE;
}
switch (t->c_cflag & CSIZE) {
case CS5:
value |= UVSCOM_DATA_BIT_5;
break;
case CS6:
value |= UVSCOM_DATA_BIT_6;
break;
case CS7:
value |= UVSCOM_DATA_BIT_7;
break;
default:
case CS8:
value |= UVSCOM_DATA_BIT_8;
break;
}
uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
}
static int
uvscom_pre_open(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
DPRINTF("sc = %p\n", sc);
/* check if PC card was inserted */
if (sc->sc_unit_status & UVSCOM_NOCARD) {
DPRINTF("no PC card!\n");
return (ENXIO);
}
return (0);
}
static void
uvscom_cfg_open(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
DPRINTF("sc = %p\n", sc);
uvscom_cfg_read_status(sc);
}
static void
uvscom_cfg_close(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
DPRINTF("sc=%p\n", sc);
uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
}
static void
uvscom_start_read(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
}
static void
uvscom_stop_read(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
}
static void
uvscom_start_write(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
}
static void
uvscom_stop_write(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
}
static void
uvscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
struct uvscom_softc *sc = ucom->sc_parent;
*lsr = sc->sc_lsr;
*msr = sc->sc_msr;
}
static void
uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
{
struct usb_device_request req;
usb_error_t err;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = index;
USETW(req.wValue, value);
USETW(req.wIndex, 0);
USETW(req.wLength, 0);
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, NULL, 0, 1000);
if (err) {
DPRINTFN(0, "device request failed, err=%s "
"(ignored)\n", usbd_errstr(err));
}
}
static uint16_t
uvscom_cfg_read_status(struct uvscom_softc *sc)
{
struct usb_device_request req;
usb_error_t err;
uint8_t data[2];
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = UVSCOM_READ_STATUS;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, 2);
err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
&req, data, 0, 1000);
if (err) {
DPRINTFN(0, "device request failed, err=%s "
"(ignored)\n", usbd_errstr(err));
}
return (data[0] | (data[1] << 8));
}
static void
uvscom_poll(struct ucom_softc *ucom)
{
struct uvscom_softc *sc = ucom->sc_parent;
usbd_transfer_poll(sc->sc_xfer, UVSCOM_N_TRANSFER);
}