summaryrefslogtreecommitdiffstats
path: root/sys/dev
diff options
context:
space:
mode:
authorjoe <joe@FreeBSD.org>2002-03-18 18:23:42 +0000
committerjoe <joe@FreeBSD.org>2002-03-18 18:23:42 +0000
commit0f7e6dda9fcfcd6e66f8711b55ffccec88b75409 (patch)
tree4399f2c6661153a1aacc274891fd6ff7b556b6cc /sys/dev
parente79ce2cfe3aeee225edd020e13fbb533e7bff88f (diff)
downloadFreeBSD-src-0f7e6dda9fcfcd6e66f8711b55ffccec88b75409.zip
FreeBSD-src-0f7e6dda9fcfcd6e66f8711b55ffccec88b75409.tar.gz
Add a USB comm driver.
Ported from NetBSD by: akiyama
Diffstat (limited to 'sys/dev')
-rw-r--r--sys/dev/usb/ucom.c1130
-rw-r--r--sys/dev/usb/ucomvar.h189
-rw-r--r--sys/dev/usb/uplcom.c810
-rw-r--r--sys/dev/usb/usbdevs8
-rw-r--r--sys/dev/usb/uvscom.c888
5 files changed, 3025 insertions, 0 deletions
diff --git a/sys/dev/usb/ucom.c b/sys/dev/usb/ucom.c
new file mode 100644
index 0000000..b4d197e
--- /dev/null
+++ b/sys/dev/usb/ucom.c
@@ -0,0 +1,1130 @@
+/* $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 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) 1998, 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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.
+ */
+
+/*
+ * TODO:
+ * 1. How do I handle hotchar?
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/bus.h>
+#include <sys/ioccom.h>
+#include <sys/fcntl.h>
+#include <sys/conf.h>
+#include <sys/tty.h>
+#include <sys/clist.h>
+#include <sys/file.h>
+#if __FreeBSD_version >= 500014
+#include <sys/selinfo.h>
+#else
+#include <sys/select.h>
+#endif
+#include <sys/proc.h>
+#include <sys/vnode.h>
+#include <sys/poll.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbcdc.h>
+
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/usb_quirks.h>
+
+#include <dev/usb/ucomvar.h>
+
+#ifdef UCOM_DEBUG
+#include <sys/sysctl.h>
+
+static int ucomdebug = 1;
+
+SYSCTL_NODE(_debug, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
+SYSCTL_INT(_debug_usb, OID_AUTO, ucom, CTLFLAG_RW,
+ &ucomdebug, 0, "ucom debug level");
+
+#define DPRINTF(x) do { \
+ if (ucomdebug) \
+ logprintf x; \
+ } while (0)
+
+#define DPRINTFN(n, x) do { \
+ if (ucomdebug > (n)) \
+ logprintf x; \
+ } while (0)
+#else
+#define DPRINTF(x)
+#define DPRINTFN(n, x)
+#endif
+
+Static d_open_t ucomopen;
+Static d_close_t ucomclose;
+Static d_read_t ucomread;
+Static d_write_t ucomwrite;
+Static d_ioctl_t ucomioctl;
+
+#define UCOM_CDEV_MAJOR 138
+
+static struct cdevsw ucom_cdevsw = {
+ /* open */ ucomopen,
+ /* close */ ucomclose,
+ /* read */ ucomread,
+ /* write */ ucomwrite,
+ /* ioctl */ ucomioctl,
+ /* poll */ ttypoll,
+ /* mmap */ nommap,
+ /* strategy */ nostrategy,
+ /* name */ "usio",
+ /* maj */ UCOM_CDEV_MAJOR,
+ /* dump */ nodump,
+ /* psize */ nopsize,
+ /* flags */ D_TTY | D_KQFILTER,
+ /* kqfilter */ ttykqfilter,
+};
+
+Static void ucom_cleanup(struct ucom_softc *);
+Static int ucomctl(struct ucom_softc *, int, int);
+Static int ucomparam(struct tty *, struct termios *);
+Static void ucomstart(struct tty *);
+Static void ucomstop(struct tty *, int);
+Static void ucom_shutdown(struct ucom_softc *);
+Static void ucom_dtr(struct ucom_softc *, int);
+Static void ucom_rts(struct ucom_softc *, int);
+Static void ucom_break(struct ucom_softc *, int);
+Static usbd_status ucomstartread(struct ucom_softc *);
+Static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
+Static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
+Static void ucomstopread(struct ucom_softc *);
+static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
+
+devclass_t ucom_devclass;
+
+static moduledata_t ucom_mod = {
+ "ucom",
+ NULL,
+ NULL
+};
+
+DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
+MODULE_VERSION(ucom, UCOM_MODVER);
+
+int
+ucom_attach(struct ucom_softc *sc)
+{
+ struct tty *tp;
+ int unit;
+
+ unit = device_get_unit(sc->sc_dev);
+
+ sc->sc_tty = tp = ttymalloc(sc->sc_tty);
+ tp->t_oproc = ucomstart;
+ tp->t_param = ucomparam;
+ tp->t_stop = ucomstop;
+
+ DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
+
+ DPRINTF(("ucom_attach: make_dev: usio%d\n", unit));
+
+ sc->dev = make_dev(&ucom_cdevsw, unit | UCOM_CALLOUT_MASK,
+ UID_UUCP, GID_DIALER, 0660,
+ "usio%d", unit);
+ sc->dev->si_tty = tp;
+
+ return (0);
+}
+
+int
+ucom_detach(struct ucom_softc *sc)
+{
+ DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
+
+ sc->sc_dying = 1;
+
+ if (sc->sc_bulkin_pipe != NULL)
+ usbd_abort_pipe(sc->sc_bulkin_pipe);
+ if (sc->sc_bulkout_pipe != NULL)
+ usbd_abort_pipe(sc->sc_bulkout_pipe);
+
+ if (sc->sc_tty == NULL) {
+ DPRINTF(("ucom_detach: no tty\n"));
+ return (0);
+ }
+
+ destroy_dev(sc->dev);
+
+ return (0);
+}
+
+Static void
+ucom_shutdown(struct ucom_softc *sc)
+{
+ struct tty *tp = sc->sc_tty;
+
+ DPRINTF(("ucom_shutdown\n"));
+ /*
+ * Hang up if necessary. Wait a bit, so the other side has time to
+ * notice even if we immediately open the port again.
+ */
+ if (ISSET(tp->t_cflag, HUPCL)) {
+ (void)ucomctl(sc, TIOCM_DTR, DMBIC);
+ (void)tsleep(sc, TTIPRI, "ucomsd", hz);
+ }
+}
+
+Static int
+ucomopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
+{
+ int unit = UCOMUNIT(dev);
+ struct ucom_softc *sc;
+ usbd_status err;
+ struct tty *tp;
+ int s;
+ int error;
+
+ USB_GET_SC_OPEN(ucom, unit, sc);
+
+ if (sc->sc_dying)
+ return (EIO);
+
+ tp = sc->sc_tty;
+
+ DPRINTF(("%s: ucomopen: tp = %p\n", USBDEVNAME(sc->sc_dev), tp));
+
+ if (ISSET(tp->t_state, TS_ISOPEN) &&
+ ISSET(tp->t_state, TS_XCLUDE) &&
+ suser_td(p))
+ return (EBUSY);
+
+ /*
+ * Do the following iff this is a first open.
+ */
+ s = spltty();
+ while (sc->sc_opening)
+ tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
+ sc->sc_opening = 1;
+
+ if (!ISSET(tp->t_state, TS_ISOPEN)) {
+ struct termios t;
+
+ sc->sc_poll = 0;
+ sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
+
+ tp->t_dev = dev;
+
+ /*
+ * Initialize the termios status to the defaults. Add in the
+ * sticky bits from TIOCSFLAGS.
+ */
+ t.c_ispeed = 0;
+ t.c_ospeed = TTYDEF_SPEED;
+ t.c_cflag = TTYDEF_CFLAG;
+ /* Make sure ucomparam() will do something. */
+ tp->t_ospeed = 0;
+ (void)ucomparam(tp, &t);
+ tp->t_iflag = TTYDEF_IFLAG;
+ tp->t_oflag = TTYDEF_OFLAG;
+ tp->t_lflag = TTYDEF_LFLAG;
+ ttychars(tp);
+ ttsetwater(tp);
+
+ /*
+ * Turn on DTR. We must always do this, even if carrier is not
+ * present, because otherwise we'd have to use TIOCSDTR
+ * immediately after setting CLOCAL, which applications do not
+ * expect. We always assert DTR while the device is open
+ * unless explicitly requested to deassert it.
+ */
+ (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
+
+ /* Device specific open */
+ if (sc->sc_callback->ucom_open != NULL) {
+ error = sc->sc_callback->ucom_open(sc->sc_parent,
+ sc->sc_portno);
+ if (error) {
+ ucom_cleanup(sc);
+ sc->sc_opening = 0;
+ wakeup(&sc->sc_opening);
+ splx(s);
+ return (error);
+ }
+ }
+
+ DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
+ sc->sc_bulkin_no, sc->sc_bulkout_no));
+
+ /* Open the bulk pipes */
+ /* Bulk-in pipe */
+ err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
+ &sc->sc_bulkin_pipe);
+ if (err) {
+ printf("%s: open bulk out error (addr %d): %s\n",
+ USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
+ usbd_errstr(err));
+ error = EIO;
+ goto fail_0;
+ }
+ /* Bulk-out pipe */
+ err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
+ USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
+ if (err) {
+ printf("%s: open bulk in error (addr %d): %s\n",
+ USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
+ usbd_errstr(err));
+ error = EIO;
+ goto fail_1;
+ }
+
+ /* Allocate a request and an input buffer and start reading. */
+ sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
+ if (sc->sc_ixfer == NULL) {
+ error = ENOMEM;
+ goto fail_2;
+ }
+
+ sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
+ sc->sc_ibufsizepad);
+ if (sc->sc_ibuf == NULL) {
+ error = ENOMEM;
+ goto fail_3;
+ }
+
+ sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
+ if (sc->sc_oxfer == NULL) {
+ error = ENOMEM;
+ goto fail_3;
+ }
+
+ sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
+ sc->sc_obufsize +
+ sc->sc_opkthdrlen);
+ if (sc->sc_obuf == NULL) {
+ error = ENOMEM;
+ goto fail_4;
+ }
+
+ /*
+ * Handle initial DCD.
+ */
+ if (ISSET(sc->sc_msr, UMSR_DCD)
+ || (minor(dev) & UCOM_CALLOUT_MASK))
+ (*linesw[tp->t_line].l_modem)(tp, 1);
+
+ ucomstartread(sc);
+ }
+
+ sc->sc_opening = 0;
+ wakeup(&sc->sc_opening);
+ splx(s);
+
+ error = ttyopen(dev, tp);
+ if (error)
+ goto bad;
+
+ error = (*linesw[tp->t_line].l_open)(dev, tp);
+ if (error)
+ goto bad;
+
+ disc_optim(tp, &tp->t_termios, sc);
+
+ DPRINTF(("%s: ucomopen: success\n", USBDEVNAME(sc->sc_dev)));
+
+ sc->sc_poll = 1;
+
+ return (0);
+
+fail_4:
+ usbd_free_xfer(sc->sc_oxfer);
+ sc->sc_oxfer = NULL;
+fail_3:
+ usbd_free_xfer(sc->sc_ixfer);
+ sc->sc_ixfer = NULL;
+fail_2:
+ usbd_close_pipe(sc->sc_bulkout_pipe);
+ sc->sc_bulkout_pipe = NULL;
+fail_1:
+ usbd_close_pipe(sc->sc_bulkin_pipe);
+ sc->sc_bulkin_pipe = NULL;
+fail_0:
+ sc->sc_opening = 0;
+ wakeup(&sc->sc_opening);
+ splx(s);
+ return (error);
+
+bad:
+ if (!ISSET(tp->t_state, TS_ISOPEN)) {
+ /*
+ * We failed to open the device, and nobody else had it opened.
+ * Clean up the state as appropriate.
+ */
+ ucom_cleanup(sc);
+ }
+
+ DPRINTF(("%s: ucomopen: failed\n", USBDEVNAME(sc->sc_dev)));
+
+ return (error);
+}
+
+static int
+ucomclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
+{
+ struct ucom_softc *sc;
+ struct tty *tp;
+ int s;
+
+ USB_GET_SC(ucom, UCOMUNIT(dev), sc);
+
+ tp = sc->sc_tty;
+
+ DPRINTF(("%s: ucomclose: unit = %d\n",
+ USBDEVNAME(sc->sc_dev), UCOMUNIT(dev)));
+
+ if (!ISSET(tp->t_state, TS_ISOPEN))
+ return (0);
+
+ s = spltty();
+ (*linesw[tp->t_line].l_close)(tp, flag);
+ disc_optim(tp, &tp->t_termios, sc);
+ ttyclose(tp);
+ splx(s);
+
+ if (sc->sc_dying)
+ return (0);
+
+ if (!ISSET(tp->t_state, TS_ISOPEN)) {
+ /*
+ * Although we got a last close, the device may still be in
+ * use; e.g. if this was the dialout node, and there are still
+ * processes waiting for carrier on the non-dialout node.
+ */
+ ucom_cleanup(sc);
+ }
+
+ if (sc->sc_callback->ucom_close != NULL)
+ sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
+
+ return (0);
+}
+
+static int
+ucomread(dev_t dev, struct uio *uio, int flag)
+{
+ struct ucom_softc *sc;
+ struct tty *tp;
+ int error;
+
+ USB_GET_SC(ucom, UCOMUNIT(dev), sc);
+ tp = sc->sc_tty;
+
+ DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, flag));
+
+ if (sc->sc_dying)
+ return (EIO);
+
+ error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
+
+ DPRINTF(("ucomread: error = %d\n", error));
+
+ return (error);
+}
+
+static int
+ucomwrite(dev_t dev, struct uio *uio, int flag)
+{
+ struct ucom_softc *sc;
+ struct tty *tp;
+ int error;
+
+ USB_GET_SC(ucom, UCOMUNIT(dev), sc);
+ tp = sc->sc_tty;
+
+ DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, flag));
+
+ if (sc->sc_dying)
+ return (EIO);
+
+ error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
+
+ DPRINTF(("ucomwrite: error = %d\n", error));
+
+ return (error);
+}
+
+static int
+ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
+{
+ struct ucom_softc *sc;
+ struct tty *tp;
+ int error;
+ int s;
+ int d;
+
+ USB_GET_SC(ucom, UCOMUNIT(dev), sc);
+ tp = sc->sc_tty;
+
+ if (sc->sc_dying)
+ return (EIO);
+
+ DPRINTF(("ucomioctl: cmd = 0x%08lx\n", cmd));
+
+ error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
+ if (error >= 0) {
+ DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
+ return (error);
+ }
+
+ error = ttioctl(tp, cmd, data, flag);
+ disc_optim(tp, &tp->t_termios, sc);
+ if (error >= 0) {
+ DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
+ return (error);
+ }
+
+ if (sc->sc_callback->ucom_ioctl != NULL) {
+ error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
+ sc->sc_portno,
+ cmd, data, flag, p);
+ if (error >= 0)
+ return (error);
+ }
+
+ error = 0;
+
+ DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", cmd));
+
+ s = spltty();
+
+ switch (cmd) {
+ case TIOCSBRK:
+ DPRINTF(("ucomioctl: TIOCSBRK\n"));
+ ucom_break(sc, 1);
+ break;
+ case TIOCCBRK:
+ DPRINTF(("ucomioctl: TIOCCBRK\n"));
+ ucom_break(sc, 0);
+ break;
+
+ case TIOCSDTR:
+ DPRINTF(("ucomioctl: TIOCSDTR\n"));
+ (void)ucomctl(sc, TIOCM_DTR, DMBIS);
+ break;
+ case TIOCCDTR:
+ DPRINTF(("ucomioctl: TIOCCDTR\n"));
+ (void)ucomctl(sc, TIOCM_DTR, DMBIC);
+ break;
+
+ case TIOCMSET:
+ d = *(int *)data;
+ DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
+ (void)ucomctl(sc, d, DMSET);
+ break;
+ case TIOCMBIS:
+ d = *(int *)data;
+ DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
+ (void)ucomctl(sc, d, DMBIS);
+ break;
+ case TIOCMBIC:
+ d = *(int *)data;
+ DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
+ (void)ucomctl(sc, d, DMBIC);
+ break;
+ case TIOCMGET:
+ d = ucomctl(sc, 0, DMGET);
+ DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
+ *(int *)data = d;
+ break;
+
+ default:
+ DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", cmd));
+ error = ENOTTY;
+ break;
+ }
+
+ splx(s);
+
+ return (error);
+}
+
+Static int
+ucomctl(struct ucom_softc *sc, int bits, int how)
+{
+ int mcr;
+ int msr;
+ int onoff;
+
+ DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
+
+ if (how == DMGET) {
+ SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
+ DPRINTF(("ucomctl: DMGET: LE"));
+
+ mcr = sc->sc_mcr;
+ if (ISSET(mcr, UMCR_DTR)) {
+ SET(bits, TIOCM_DTR);
+ DPRINTF((" DTR"));
+ }
+ if (ISSET(mcr, UMCR_RTS)) {
+ SET(bits, TIOCM_RTS);
+ DPRINTF((" RTS"));
+ }
+
+ msr = sc->sc_msr;
+ if (ISSET(msr, UMSR_CTS)) {
+ SET(bits, TIOCM_CTS);
+ DPRINTF((" CTS"));
+ }
+ if (ISSET(msr, UMSR_DCD)) {
+ SET(bits, TIOCM_CD);
+ DPRINTF((" CD"));
+ }
+ if (ISSET(msr, UMSR_DSR)) {
+ SET(bits, TIOCM_DSR);
+ DPRINTF((" DSR"));
+ }
+ if (ISSET(msr, UMSR_RI)) {
+ SET(bits, TIOCM_RI);
+ DPRINTF((" RI"));
+ }
+
+ DPRINTF(("\n"));
+
+ return (bits);
+ }
+
+ mcr = 0;
+ if (ISSET(bits, TIOCM_DTR))
+ SET(mcr, UMCR_DTR);
+ if (ISSET(bits, TIOCM_RTS))
+ SET(mcr, UMCR_RTS);
+
+ switch (how) {
+ case DMSET:
+ sc->sc_mcr = mcr;
+ break;
+ case DMBIS:
+ sc->sc_mcr |= mcr;
+ break;
+ case DMBIC:
+ sc->sc_mcr &= ~mcr;
+ break;
+ }
+
+ onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
+ ucom_dtr(sc, onoff);
+
+ onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
+ ucom_rts(sc, onoff);
+
+ return (0);
+}
+
+Static void
+ucom_break(struct ucom_softc *sc, int onoff)
+{
+ DPRINTF(("ucom_break: onoff = %d\n", onoff));
+
+ if (sc->sc_callback->ucom_set == NULL)
+ return;
+ sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
+ UCOM_SET_BREAK, onoff);
+}
+
+Static void
+ucom_dtr(struct ucom_softc *sc, int onoff)
+{
+ DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
+
+ if (sc->sc_callback->ucom_set == NULL)
+ return;
+ sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
+ UCOM_SET_DTR, onoff);
+}
+
+Static void
+ucom_rts(struct ucom_softc *sc, int onoff)
+{
+ DPRINTF(("ucom_rts: onoff = %d\n", onoff));
+
+ if (sc->sc_callback->ucom_set == NULL)
+ return;
+ sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
+ UCOM_SET_RTS, onoff);
+}
+
+void
+ucom_status_change(struct ucom_softc *sc)
+{
+ struct tty *tp = sc->sc_tty;
+ u_char old_msr;
+ int onoff;
+
+ if (sc->sc_callback->ucom_get_status == NULL) {
+ sc->sc_lsr = 0;
+ sc->sc_msr = 0;
+ return;
+ }
+
+ old_msr = sc->sc_msr;
+ sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
+ &sc->sc_lsr, &sc->sc_msr);
+ if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
+ if (sc->sc_poll == 0)
+ return;
+ onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
+ DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
+ (*linesw[tp->t_line].l_modem)(tp, onoff);
+ }
+}
+
+Static int
+ucomparam(struct tty *tp, struct termios *t)
+{
+ struct ucom_softc *sc;
+ int error;
+ usbd_status uerr;
+
+ USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
+
+ if (sc->sc_dying)
+ return (EIO);
+
+ DPRINTF(("ucomparam: sc = %p\n", sc));
+
+ /* Check requested parameters. */
+ if (t->c_ospeed < 0) {
+ DPRINTF(("ucomparam: negative ospeed\n"));
+ return (EINVAL);
+ }
+ if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
+ DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
+ return (EINVAL);
+ }
+
+ /*
+ * If there were no changes, don't do anything. This avoids dropping
+ * input and improves performance when all we did was frob things like
+ * VMIN and VTIME.
+ */
+ if (tp->t_ospeed == t->c_ospeed &&
+ tp->t_cflag == t->c_cflag)
+ return (0);
+
+ /* And copy to tty. */
+ tp->t_ispeed = 0;
+ tp->t_ospeed = t->c_ospeed;
+ tp->t_cflag = t->c_cflag;
+
+ if (sc->sc_callback->ucom_param == NULL)
+ return (0);
+
+ ucomstopread(sc);
+
+ error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
+ if (error) {
+ DPRINTF(("ucomparam: callback: error = %d\n", error));
+ return (error);
+ }
+
+ ttsetwater(tp);
+
+ if (t->c_cflag & CRTS_IFLOW) {
+ sc->sc_state |= UCS_RTS_IFLOW;
+ } else if (sc->sc_state & UCS_RTS_IFLOW) {
+ sc->sc_state &= ~UCS_RTS_IFLOW;
+ (void)ucomctl(sc, UMCR_RTS, DMBIS);
+ }
+
+ disc_optim(tp, t, sc);
+
+ uerr = ucomstartread(sc);
+ if (uerr != USBD_NORMAL_COMPLETION)
+ return (EIO);
+
+ return (0);
+}
+
+Static void
+ucomstart(struct tty *tp)
+{
+ struct ucom_softc *sc;
+ struct cblock *cbp;
+ usbd_status err;
+ int s;
+ u_char *data;
+ int cnt;
+
+ USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
+ DPRINTF(("ucomstart: sc = %p\n", sc));
+
+ if (sc->sc_dying)
+ return;
+
+ s = spltty();
+
+ if (tp->t_state & TS_TBLOCK) {
+ if (ISSET(sc->sc_mcr, UMCR_RTS) &&
+ ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
+ DPRINTF(("ucomstart: clear RTS\n"));
+ (void)ucomctl(sc, UMCR_RTS, DMBIC);
+ }
+ } else {
+ if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
+ tp->t_rawq.c_cc <= tp->t_ilowat &&
+ ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
+ DPRINTF(("ucomstart: set RTS\n"));
+ (void)ucomctl(sc, UMCR_RTS, DMBIS);
+ }
+ }
+
+ if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
+ ttwwakeup(tp);
+ DPRINTF(("ucomstart: stopped\n"));
+ goto out;
+ }
+
+ if (tp->t_outq.c_cc <= tp->t_olowat) {
+ if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
+ CLR(tp->t_state, TS_SO_OLOWAT);
+ wakeup(TSA_OLOWAT(tp));
+ }
+ selwakeup(&tp->t_wsel);
+ if (tp->t_outq.c_cc == 0) {
+ if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
+ TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
+ CLR(tp->t_state, TS_SO_OCOMPLETE);
+ wakeup(TSA_OCOMPLETE(tp));
+ }
+ goto out;
+ }
+ }
+
+ /* Grab the first contiguous region of buffer space. */
+ data = tp->t_outq.c_cf;
+ cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
+ cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
+
+ if (cnt == 0) {
+ DPRINTF(("ucomstart: cnt == 0\n"));
+ goto out;
+ }
+
+ SET(tp->t_state, TS_BUSY);
+
+ if (cnt > sc->sc_obufsize) {
+ DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
+ cnt = sc->sc_obufsize;
+ }
+ if (sc->sc_callback->ucom_write != NULL)
+ sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
+ sc->sc_obuf, data, &cnt);
+ else
+ memcpy(sc->sc_obuf, data, cnt);
+
+ DPRINTF(("ucomstart: %d chars\n", cnt));
+ usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
+ (usbd_private_handle)sc, sc->sc_obuf, cnt,
+ USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
+ /* What can we do on error? */
+ err = usbd_transfer(sc->sc_oxfer);
+ if (err != USBD_IN_PROGRESS)
+ printf("ucomstart: err=%s\n", usbd_errstr(err));
+
+ ttwwakeup(tp);
+
+ out:
+ splx(s);
+}
+
+Static void
+ucomstop(struct tty *tp, int flag)
+{
+ struct ucom_softc *sc;
+ int s;
+
+ USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
+
+ DPRINTF(("ucomstop: %d\n", flag));
+
+ if (flag & FREAD) {
+ DPRINTF(("ucomstop: read\n"));
+ ucomstopread(sc);
+ }
+
+ if (flag & FWRITE) {
+ DPRINTF(("ucomstop: write\n"));
+ s = spltty();
+ if (ISSET(tp->t_state, TS_BUSY)) {
+ /* XXX do what? */
+ if (!ISSET(tp->t_state, TS_TTSTOP))
+ SET(tp->t_state, TS_FLUSH);
+ }
+ splx(s);
+ }
+}
+
+Static void
+ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
+{
+ struct ucom_softc *sc = (struct ucom_softc *)p;
+ struct tty *tp = sc->sc_tty;
+ u_int32_t cc;
+ int s;
+
+ DPRINTF(("ucomwritecb: status = %d\n", status));
+
+ if (status == USBD_CANCELLED || sc->sc_dying)
+ goto error;
+
+ if (status != USBD_NORMAL_COMPLETION) {
+ printf("%s: ucomwritecb: %s\n",
+ USBDEVNAME(sc->sc_dev), usbd_errstr(status));
+ if (status == USBD_STALLED)
+ usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
+ /* XXX we should restart after some delay. */
+ goto error;
+ }
+
+ usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
+ DPRINTF(("ucomwritecb: cc = %d\n", cc));
+
+ /* convert from USB bytes to tty bytes */
+ cc -= sc->sc_opkthdrlen;
+
+ s = spltty();
+ CLR(tp->t_state, TS_BUSY);
+ if (ISSET(tp->t_state, TS_FLUSH))
+ CLR(tp->t_state, TS_FLUSH);
+ else
+ ndflush(&tp->t_outq, cc);
+ (*linesw[tp->t_line].l_start)(tp);
+ splx(s);
+
+ return;
+
+ error:
+ s = spltty();
+ CLR(tp->t_state, TS_BUSY);
+ splx(s);
+ return;
+}
+
+Static usbd_status
+ucomstartread(struct ucom_softc *sc)
+{
+ usbd_status err;
+
+ DPRINTF(("ucomstartread: start\n"));
+
+ sc->sc_state &= ~UCS_RXSTOP;
+
+ if (sc->sc_bulkin_pipe == NULL)
+ return (USBD_NORMAL_COMPLETION);
+
+ usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
+ (usbd_private_handle)sc,
+ sc->sc_ibuf, sc->sc_ibufsize,
+ USBD_SHORT_XFER_OK | USBD_NO_COPY,
+ USBD_NO_TIMEOUT, ucomreadcb);
+
+ err = usbd_transfer(sc->sc_ixfer);
+ if (err != USBD_IN_PROGRESS) {
+ DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
+ return (err);
+ }
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static void
+ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
+{
+ struct ucom_softc *sc = (struct ucom_softc *)p;
+ struct tty *tp = sc->sc_tty;
+ int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
+ usbd_status err;
+ u_int32_t cc;
+ u_char *cp;
+ int lostcc;
+ int s;
+
+ DPRINTF(("ucomreadcb: status = %d\n", status));
+
+ if (status != USBD_NORMAL_COMPLETION) {
+ if (!(sc->sc_state & UCS_RXSTOP))
+ printf("%s: ucomreadcb: %s\n",
+ USBDEVNAME(sc->sc_dev), usbd_errstr(status));
+ if (status == USBD_STALLED)
+ usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
+ /* XXX we should restart after some delay. */
+ return;
+ }
+
+ usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
+ DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
+ if (sc->sc_callback->ucom_read != NULL)
+ sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
+ &cp, &cc);
+
+ s = spltty();
+ if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
+ if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
+ && (sc->sc_state & UCS_RTS_IFLOW
+ || tp->t_iflag & IXOFF)
+ && !(tp->t_state & TS_TBLOCK))
+ ttyblock(tp);
+ lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
+ tp->t_rawcc += cc;
+ ttwakeup(tp);
+ if (tp->t_state & TS_TTSTOP
+ && (tp->t_iflag & IXANY
+ || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
+ tp->t_state &= ~TS_TTSTOP;
+ tp->t_lflag &= ~FLUSHO;
+ ucomstart(tp);
+ }
+ if (lostcc > 0)
+ printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
+ lostcc);
+ } else {
+ /* Give characters to tty layer. */
+ while (cc-- > 0) {
+ DPRINTFN(7,("ucomreadcb: char = 0x%02x\n", *cp));
+ if ((*rint)(*cp++, tp) == -1) {
+ /* XXX what should we do? */
+ printf("%s: lost %d chars\n",
+ USBDEVNAME(sc->sc_dev), cc);
+ break;
+ }
+ }
+ }
+ splx(s);
+
+ err = ucomstartread(sc);
+ if (err) {
+ printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
+ /* XXX what should we dow now? */
+ }
+
+ if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
+ && !(tp->t_state & TS_TBLOCK))
+ ucomctl(sc, UMCR_RTS, DMBIS);
+}
+
+Static void
+ucom_cleanup(struct ucom_softc *sc)
+{
+ DPRINTF(("ucom_cleanup: closing pipes\n"));
+
+ ucom_shutdown(sc);
+ if (sc->sc_bulkin_pipe != NULL) {
+ usbd_abort_pipe(sc->sc_bulkin_pipe);
+ usbd_close_pipe(sc->sc_bulkin_pipe);
+ sc->sc_bulkin_pipe = NULL;
+ }
+ if (sc->sc_bulkout_pipe != NULL) {
+ usbd_abort_pipe(sc->sc_bulkout_pipe);
+ usbd_close_pipe(sc->sc_bulkout_pipe);
+ sc->sc_bulkout_pipe = NULL;
+ }
+ if (sc->sc_ixfer != NULL) {
+ usbd_free_xfer(sc->sc_ixfer);
+ sc->sc_ixfer = NULL;
+ }
+ if (sc->sc_oxfer != NULL) {
+ usbd_free_xfer(sc->sc_oxfer);
+ sc->sc_oxfer = NULL;
+ }
+}
+
+Static void
+ucomstopread(struct ucom_softc *sc)
+{
+ if (!(sc->sc_state & UCS_RXSTOP)) {
+ if (sc->sc_bulkin_pipe == NULL)
+ return;
+ sc->sc_state |= UCS_RXSTOP;
+ usbd_abort_pipe(sc->sc_bulkin_pipe);
+ }
+}
+
+static void
+disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
+{
+ if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
+ && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
+ && (!(t->c_iflag & PARMRK)
+ || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
+ && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
+ && linesw[tp->t_line].l_rint == ttyinput) {
+ DPRINTF(("disc_optim: bypass l_rint\n"));
+ tp->t_state |= TS_CAN_BYPASS_L_RINT;
+ } else {
+ DPRINTF(("disc_optim: can't bypass l_rint\n"));
+ tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
+ }
+ sc->hotchar = linesw[tp->t_line].l_hotchar;
+}
diff --git a/sys/dev/usb/ucomvar.h b/sys/dev/usb/ucomvar.h
new file mode 100644
index 0000000..8825cb7
--- /dev/null
+++ b/sys/dev/usb/ucomvar.h
@@ -0,0 +1,189 @@
+/* $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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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.
+ */
+
+/* Module interface related macros */
+#define UCOM_MODVER 1
+
+#define UCOM_MINVER 1
+#define UCOM_PREFVER UCOM_MODVER
+#define UCOM_MAXVER 1
+
+/* Macros to clear/set/test flags. */
+#define SET(t, f) (t) |= (f)
+#define CLR(t, f) (t) &= ~((unsigned)(f))
+#define ISSET(t, f) ((t) & (f))
+
+#define UCOM_CALLOUT_MASK 0x80
+
+#define UCOMUNIT_MASK 0x3ff7f
+#define UCOMDIALOUT_MASK 0x80000
+#define UCOMCALLUNIT_MASK 0x40000
+
+#define UCOMUNIT(x) (minor(x) & UCOMUNIT_MASK)
+#define UCOMDIALOUT(x) (minor(x) & UCOMDIALOUT_MASK)
+#define UCOMCALLUNIT(x) (minor(x) & UCOMCALLUNIT_MASK)
+
+#define UCOM_UNK_PORTNO -1 /* XXX */
+
+struct ucom_softc;
+
+struct ucom_callback {
+ void (*ucom_get_status)(void *, int, u_char *, u_char *);
+ void (*ucom_set)(void *, int, int, int);
+#define UCOM_SET_DTR 1
+#define UCOM_SET_RTS 2
+#define UCOM_SET_BREAK 3
+ int (*ucom_param)(void *, int, struct termios *);
+ int (*ucom_ioctl)(void *, int, u_long, caddr_t, int, usb_proc_ptr);
+ int (*ucom_open)(void *, int);
+ void (*ucom_close)(void *, int);
+ void (*ucom_read)(void *, int, u_char **, u_int32_t *);
+ void (*ucom_write)(void *, int, u_char *, u_char *, u_int32_t *);
+};
+
+/* modem control register */
+#define UMCR_RTS 0x02 /* Request To Send */
+#define UMCR_DTR 0x01 /* Data Terminal Ready */
+
+/* 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 */
+
+/* modem status register */
+/* All deltas are from the last read of the MSR. */
+#define UMSR_DCD 0x80 /* Current Data Carrier Detect */
+#define UMSR_RI 0x40 /* Current Ring Indicator */
+#define UMSR_DSR 0x20 /* Current Data Set Ready */
+#define UMSR_CTS 0x10 /* Current Clear to Send */
+#define UMSR_DDCD 0x08 /* DCD has changed state */
+#define UMSR_TERI 0x04 /* RI has toggled low to high */
+#define UMSR_DDSR 0x02 /* DSR has changed state */
+#define UMSR_DCTS 0x01 /* CTS has changed state */
+
+/* ucom state declarations */
+#define UCS_RXSTOP 0x0001 /* Rx stopped */
+#define UCS_RTS_IFLOW 0x0008 /* use RTS input flow control */
+
+struct ucom_softc {
+ USBBASEDEVICE sc_dev; /* base device */
+ usbd_device_handle sc_udev; /* USB device */
+ usbd_interface_handle sc_iface; /* data interface */
+
+ int sc_bulkin_no; /* bulk in endpoint address */
+ usbd_pipe_handle sc_bulkin_pipe; /* bulk in pipe */
+ usbd_xfer_handle sc_ixfer; /* read request */
+ u_char *sc_ibuf; /* read buffer */
+ u_int sc_ibufsize; /* read buffer size */
+ u_int sc_ibufsizepad; /* read buffer size padded */
+
+ int sc_bulkout_no; /* bulk out endpoint address */
+ usbd_pipe_handle sc_bulkout_pipe;/* bulk out pipe */
+ usbd_xfer_handle sc_oxfer; /* write request */
+ u_char *sc_obuf; /* write buffer */
+ u_int sc_obufsize; /* write buffer size */
+ u_int sc_opkthdrlen; /* header length of
+ output packet */
+
+ struct ucom_callback *sc_callback;
+ void *sc_parent;
+ int sc_portno;
+
+ struct tty *sc_tty; /* our tty */
+
+ int sc_state;
+
+ int sc_poll;
+ u_char hotchar;
+
+ u_char sc_lsr;
+ u_char sc_msr;
+ u_char sc_mcr;
+
+ u_char sc_opening; /* lock during open */
+ int sc_refcnt;
+ u_char sc_dying; /* disconnecting */
+
+ dev_t dev; /* special device node */
+};
+
+extern devclass_t ucom_devclass;
+
+int ucom_attach(struct ucom_softc *);
+int ucom_detach(struct ucom_softc *);
+void ucom_status_change(struct ucom_softc *);
+
+#if 0
+#define UCOM_DEBUG 1
+#define UPLCOM_DEBUG 1
+#define UVSCOM_DEBUG 1
+#endif
diff --git a/sys/dev/usb/uplcom.c b/sys/dev/usb/uplcom.c
new file mode 100644
index 0000000..8c7467a
--- /dev/null
+++ b/sys/dev/usb/uplcom.c
@@ -0,0 +1,810 @@
+/* $NetBSD: uplcom.c,v 1.20 2001/07/31 12:33:11 ichiro 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) 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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.
+ */
+
+/*
+ * Simple datasheet
+ * http://www.prolific.com.tw/download/DataSheet/pl2303_ds11.PDF
+ * http://www.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
+ * (english)
+ *
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/bus.h>
+#include <sys/ioccom.h>
+#include <sys/fcntl.h>
+#include <sys/conf.h>
+#include <sys/tty.h>
+#include <sys/file.h>
+#if __FreeBSD_version >= 500014
+#include <sys/selinfo.h>
+#else
+#include <sys/select.h>
+#endif
+#include <sys/proc.h>
+#include <sys/vnode.h>
+#include <sys/poll.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbcdc.h>
+
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/usb_quirks.h>
+
+#include <dev/usb/ucomvar.h>
+
+#ifdef UPLCOM_DEBUG
+#include <sys/sysctl.h>
+
+static int uplcomdebug = 1;
+
+SYSCTL_DECL(_debug_usb);
+SYSCTL_INT(_debug_usb, OID_AUTO, uplcom, CTLFLAG_RW,
+ &uplcomdebug, 0, "uplcom debug level");
+
+#define DPRINTFN(n, x) do { \
+ if (uplcomdebug > (n)) \
+ logprintf x; \
+ } while (0)
+#else
+#define DPRINTFN(n, x)
+#endif
+#define DPRINTF(x) DPRINTFN(0, x)
+
+#define UPLCOM_MODVER 1 /* module version */
+
+#define UPLCOM_CONFIG_INDEX 0
+#define UPLCOM_IFACE_INDEX 0
+#define UPLCOM_SECOND_IFACE_INDEX 1
+
+#define UPLCOM_INTR_INTERVAL 100 /* ms */
+
+#define UPLCOM_SET_REQUEST 0x01
+#define UPLCOM_SET_CRTSCTS 0x41
+#define RSAQ_STATUS_DSR 0x02
+#define RSAQ_STATUS_DCD 0x01
+
+struct uplcom_softc {
+ struct ucom_softc sc_ucom;
+
+ int sc_iface_number; /* interface number */
+
+ usbd_interface_handle sc_intr_iface; /* interrupt interface */
+ int sc_intr_number; /* interrupt number */
+ usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
+ u_char *sc_intr_buf; /* interrupt buffer */
+ int sc_isize;
+
+ usb_cdc_line_state_t sc_line_state; /* current line state */
+ u_char sc_dtr; /* current DTR state */
+ u_char sc_rts; /* current RTS state */
+ u_char sc_status;
+
+ u_char sc_lsr; /* Local status register */
+ u_char sc_msr; /* uplcom status register */
+};
+
+/*
+ * These are the maximum number of bytes transferred per frame.
+ * The output buffer size cannot be increased due to the size encoding.
+ */
+#define UPLCOMIBUFSIZE 256
+#define UPLCOMOBUFSIZE 256
+
+Static usbd_status uplcom_reset(struct uplcom_softc *);
+Static usbd_status uplcom_set_line_coding(struct uplcom_softc *,
+ usb_cdc_line_state_t *);
+Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
+Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
+
+Static void uplcom_set(void *, int, int, int);
+Static void uplcom_dtr(struct uplcom_softc *, int);
+Static void uplcom_rts(struct uplcom_softc *, int);
+Static void uplcom_break(struct uplcom_softc *, int);
+Static void uplcom_set_line_state(struct uplcom_softc *);
+Static void uplcom_get_status(void *, int, u_char *, u_char *);
+#if TODO
+Static int uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr);
+#endif
+Static int uplcom_param(void *, int, struct termios *);
+Static int uplcom_open(void *, int);
+Static void uplcom_close(void *, int);
+
+struct ucom_callback uplcom_callback = {
+ uplcom_get_status,
+ uplcom_set,
+ uplcom_param,
+ NULL, /* uplcom_ioctl, TODO */
+ uplcom_open,
+ uplcom_close,
+ NULL,
+ NULL
+};
+
+static const struct uplcom_product {
+ uint16_t vendor;
+ uint16_t product;
+} uplcom_products [] = {
+ /* I/O DATA USB-RSAQ */
+ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
+ /* I/O DATA USB-RSAQ2 */
+ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
+ /* PLANEX USB-RS232 URS-03 */
+ { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
+ /* IOGEAR/ATEN UC-232A */
+ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
+ /* TDK USB-PHS Adapter UHA6400 */
+ { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
+ /* RATOC REX-USB60 */
+ { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
+ { 0, 0 }
+};
+
+Static device_probe_t uplcom_match;
+Static device_attach_t uplcom_attach;
+Static device_detach_t uplcom_detach;
+
+Static device_method_t uplcom_methods[] = {
+ /* Device interface */
+ DEVMETHOD(device_probe, uplcom_match),
+ DEVMETHOD(device_attach, uplcom_attach),
+ DEVMETHOD(device_detach, uplcom_detach),
+ { 0, 0 }
+};
+
+Static driver_t uplcom_driver = {
+ "usio",
+ uplcom_methods,
+ sizeof (struct uplcom_softc)
+};
+
+DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, 0);
+MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
+MODULE_VERSION(uplcom, UPLCOM_MODVER);
+
+USB_MATCH(uplcom)
+{
+ USB_MATCH_START(uplcom, uaa);
+ int i;
+
+ if (uaa->iface != NULL)
+ return (UMATCH_NONE);
+
+ for (i = 0; uplcom_products[i].vendor != 0; i++) {
+ if (uplcom_products[i].vendor == uaa->vendor &&
+ uplcom_products[i].product == uaa->product) {
+ return (UMATCH_VENDOR_PRODUCT);
+ }
+ }
+ return (UMATCH_NONE);
+}
+
+USB_ATTACH(uplcom)
+{
+ USB_ATTACH_START(uplcom, sc, uaa);
+ usbd_device_handle dev = uaa->device;
+ struct ucom_softc *ucom;
+ usb_config_descriptor_t *cdesc;
+ usb_interface_descriptor_t *id;
+ usb_endpoint_descriptor_t *ed;
+ char *devinfo;
+ const char *devname;
+ usbd_status err;
+ int i;
+
+ devinfo = malloc(1024, M_USBDEV, M_WAITOK);
+ ucom = &sc->sc_ucom;
+
+ bzero(sc, sizeof (struct uplcom_softc));
+
+ usbd_devinfo(dev, 0, devinfo);
+ /* USB_ATTACH_SETUP; */
+ ucom->sc_dev = self;
+ device_set_desc_copy(self, devinfo);
+ /* USB_ATTACH_SETUP; */
+
+ ucom->sc_udev = dev;
+ ucom->sc_iface = uaa->iface;
+
+ devname = USBDEVNAME(ucom->sc_dev);
+ printf("%s: %s\n", devname, devinfo);
+
+ DPRINTF(("uplcom attach: sc = %p\n", sc));
+
+ /* initialize endpoints */
+ ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
+ sc->sc_intr_number = -1;
+ sc->sc_intr_pipe = NULL;
+
+ /* Move the device into the configured state. */
+ err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
+ if (err) {
+ printf("%s: failed to set configuration: %s\n",
+ devname, usbd_errstr(err));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ /* get the config descriptor */
+ cdesc = usbd_get_config_descriptor(ucom->sc_udev);
+
+ if (cdesc == NULL) {
+ printf("%s: failed to get configuration descriptor\n",
+ USBDEVNAME(ucom->sc_dev));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ /* get the (first/common) interface */
+ err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
+ &ucom->sc_iface);
+ if (err) {
+ printf("%s: failed to get interface: %s\n",
+ devname, usbd_errstr(err));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ /* Find the interrupt endpoints */
+
+ id = usbd_get_interface_descriptor(ucom->sc_iface);
+ sc->sc_iface_number = id->bInterfaceNumber;
+
+ for (i = 0; i < id->bNumEndpoints; i++) {
+ ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
+ if (ed == NULL) {
+ printf("%s: no endpoint descriptor for %d\n",
+ USBDEVNAME(ucom->sc_dev), i);
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
+ sc->sc_intr_number = ed->bEndpointAddress;
+ sc->sc_isize = UGETW(ed->wMaxPacketSize);
+ }
+ }
+
+ if (sc->sc_intr_number == -1) {
+ printf("%s: Could not find interrupt in\n",
+ USBDEVNAME(ucom->sc_dev));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ /* keep interface for interrupt */
+ sc->sc_intr_iface = ucom->sc_iface;
+
+ /*
+ * 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) |
+ */
+ if (cdesc->bNumInterface == 2) {
+ err = usbd_device2interface_handle(dev,
+ UPLCOM_SECOND_IFACE_INDEX,
+ &ucom->sc_iface);
+ if (err) {
+ printf("%s: failed to get second interface: %s\n",
+ devname, usbd_errstr(err));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+ }
+
+ /* Find the bulk{in,out} endpoints */
+
+ id = usbd_get_interface_descriptor(ucom->sc_iface);
+ sc->sc_iface_number = id->bInterfaceNumber;
+
+ for (i = 0; i < id->bNumEndpoints; i++) {
+ ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
+ if (ed == NULL) {
+ printf("%s: no endpoint descriptor for %d\n",
+ USBDEVNAME(ucom->sc_dev), i);
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ ucom->sc_bulkin_no = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ ucom->sc_bulkout_no = ed->bEndpointAddress;
+ }
+ }
+
+ if (ucom->sc_bulkin_no == -1) {
+ printf("%s: Could not find data bulk in\n",
+ USBDEVNAME(ucom->sc_dev));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ if (ucom->sc_bulkout_no == -1) {
+ printf("%s: Could not find data bulk out\n",
+ USBDEVNAME(ucom->sc_dev));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ sc->sc_dtr = sc->sc_rts = -1;
+ ucom->sc_parent = sc;
+ ucom->sc_portno = UCOM_UNK_PORTNO;
+ /* bulkin, bulkout set above */
+ ucom->sc_ibufsize = UPLCOMIBUFSIZE;
+ ucom->sc_obufsize = UPLCOMOBUFSIZE;
+ ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
+ ucom->sc_opkthdrlen = 0;
+ ucom->sc_callback = &uplcom_callback;
+
+ err = uplcom_reset(sc);
+
+ if (err) {
+ printf("%s: reset failed: %s\n",
+ USBDEVNAME(ucom->sc_dev), usbd_errstr(err));
+ ucom->sc_dying = 1;
+ goto error;
+ }
+
+ DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
+ ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
+
+ ucom_attach(&sc->sc_ucom);
+
+ free(devinfo, M_USBDEV);
+ USB_ATTACH_SUCCESS_RETURN;
+
+error:
+ free(devinfo, M_USBDEV);
+ USB_ATTACH_ERROR_RETURN;
+}
+
+USB_DETACH(uplcom)
+{
+ USB_DETACH_START(uplcom, sc);
+ int rv = 0;
+
+ DPRINTF(("uplcom_detach: sc = %p\n", sc));
+
+ if (sc->sc_intr_pipe != NULL) {
+ usbd_abort_pipe(sc->sc_intr_pipe);
+ usbd_close_pipe(sc->sc_intr_pipe);
+ free(sc->sc_intr_buf, M_USBDEV);
+ sc->sc_intr_pipe = NULL;
+ }
+
+ sc->sc_ucom.sc_dying = 1;
+
+ rv = ucom_detach(&sc->sc_ucom);
+
+ return (rv);
+}
+
+Static usbd_status
+uplcom_reset(struct uplcom_softc *sc)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = UPLCOM_SET_REQUEST;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, sc->sc_iface_number);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
+ if (err) {
+ printf("%s: uplcom_reset: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (EIO);
+ }
+
+ return (0);
+}
+
+Static void
+uplcom_set_line_state(struct uplcom_softc *sc)
+{
+ usb_device_request_t req;
+ int ls;
+ usbd_status err;
+
+ ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
+ (sc->sc_rts ? UCDC_LINE_RTS : 0);
+ req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
+ req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
+ USETW(req.wValue, ls);
+ USETW(req.wIndex, sc->sc_iface_number);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
+ if (err)
+ printf("%s: uplcom_set_line_status: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+}
+
+Static void
+uplcom_set(void *addr, int portno, int reg, int onoff)
+{
+ struct uplcom_softc *sc = addr;
+
+ switch (reg) {
+ case UCOM_SET_DTR:
+ uplcom_dtr(sc, onoff);
+ break;
+ case UCOM_SET_RTS:
+ uplcom_rts(sc, onoff);
+ break;
+ case UCOM_SET_BREAK:
+ uplcom_break(sc, onoff);
+ break;
+ default:
+ break;
+ }
+}
+
+Static void
+uplcom_dtr(struct uplcom_softc *sc, int onoff)
+{
+ DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
+
+ if (sc->sc_dtr == onoff)
+ return;
+ sc->sc_dtr = onoff;
+
+ uplcom_set_line_state(sc);
+}
+
+Static void
+uplcom_rts(struct uplcom_softc *sc, int onoff)
+{
+ DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
+
+ if (sc->sc_rts == onoff)
+ return;
+ sc->sc_rts = onoff;
+
+ uplcom_set_line_state(sc);
+}
+
+Static void
+uplcom_break(struct uplcom_softc *sc, int onoff)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ DPRINTF(("uplcom_break: onoff = %d\n", onoff));
+
+ req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
+ req.bRequest = UCDC_SEND_BREAK;
+ USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
+ USETW(req.wIndex, sc->sc_iface_number);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
+ if (err)
+ printf("%s: uplcom_break: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+}
+
+Static usbd_status
+uplcom_set_crtscts(struct uplcom_softc *sc)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ DPRINTF(("uplcom_set_crtscts: on\n"));
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = UPLCOM_SET_REQUEST;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
+ if (err) {
+ printf("%s: uplcom_set_crtscts: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static usbd_status
+uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ DPRINTF((
+"uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
+ UGETDW(state->dwDTERate), state->bCharFormat,
+ state->bParityType, state->bDataBits));
+
+ if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
+ DPRINTF(("uplcom_set_line_coding: already set\n"));
+ return (USBD_NORMAL_COMPLETION);
+ }
+
+ req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
+ req.bRequest = UCDC_SET_LINE_CODING;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, sc->sc_iface_number);
+ USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
+ if (err) {
+ printf("%s: uplcom_set_line_coding: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ sc->sc_line_state = *state;
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static int
+uplcom_param(void *addr, int portno, struct termios *t)
+{
+ struct uplcom_softc *sc = addr;
+ usbd_status err;
+ usb_cdc_line_state_t ls;
+
+ DPRINTF(("uplcom_param: sc = %p\n", sc));
+
+ USETDW(ls.dwDTERate, t->c_ospeed);
+ if (ISSET(t->c_cflag, CSTOPB))
+ ls.bCharFormat = UCDC_STOP_BIT_2;
+ else
+ ls.bCharFormat = UCDC_STOP_BIT_1;
+ if (ISSET(t->c_cflag, PARENB)) {
+ if (ISSET(t->c_cflag, PARODD))
+ ls.bParityType = UCDC_PARITY_ODD;
+ else
+ ls.bParityType = UCDC_PARITY_EVEN;
+ } else
+ ls.bParityType = UCDC_PARITY_NONE;
+ switch (ISSET(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;
+ }
+
+ err = uplcom_set_line_coding(sc, &ls);
+ if (err)
+ return (EIO);
+
+ if (ISSET(t->c_cflag, CRTSCTS)) {
+ err = uplcom_set_crtscts(sc);
+ if (err)
+ return (EIO);
+ }
+
+ return (0);
+}
+
+Static int
+uplcom_open(void *addr, int portno)
+{
+ struct uplcom_softc *sc = addr;
+ int err;
+
+ if (sc->sc_ucom.sc_dying)
+ return (EIO);
+
+ DPRINTF(("uplcom_open: sc = %p\n", sc));
+
+ if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
+ sc->sc_status = 0; /* clear status bit */
+ sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
+ err = usbd_open_pipe_intr(sc->sc_intr_iface,
+ sc->sc_intr_number,
+ USBD_SHORT_XFER_OK,
+ &sc->sc_intr_pipe,
+ sc,
+ sc->sc_intr_buf,
+ sc->sc_isize,
+ uplcom_intr,
+ UPLCOM_INTR_INTERVAL);
+ if (err) {
+ printf("%s: cannot open interrupt pipe (addr %d)\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ sc->sc_intr_number);
+ return (EIO);
+ }
+ }
+
+ return (0);
+}
+
+Static void
+uplcom_close(void *addr, int portno)
+{
+ struct uplcom_softc *sc = addr;
+ int err;
+
+ if (sc->sc_ucom.sc_dying)
+ return;
+
+ DPRINTF(("uplcom_close: close\n"));
+
+ if (sc->sc_intr_pipe != NULL) {
+ err = usbd_abort_pipe(sc->sc_intr_pipe);
+ if (err)
+ printf("%s: abort interrupt pipe failed: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ usbd_errstr(err));
+ err = usbd_close_pipe(sc->sc_intr_pipe);
+ if (err)
+ printf("%s: close interrupt pipe failed: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ usbd_errstr(err));
+ free(sc->sc_intr_buf, M_USBDEV);
+ sc->sc_intr_pipe = NULL;
+ }
+}
+
+Static void
+uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
+{
+ struct uplcom_softc *sc = priv;
+ u_char *buf = sc->sc_intr_buf;
+ u_char pstatus;
+
+ if (sc->sc_ucom.sc_dying)
+ return;
+
+ if (status != USBD_NORMAL_COMPLETION) {
+ if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
+ return;
+
+ DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ usbd_errstr(status)));
+ usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
+ return;
+ }
+
+ DPRINTF(("%s: uplcom status = %02x\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), buf[8]));
+
+ sc->sc_lsr = sc->sc_msr = 0;
+ pstatus = buf[8];
+ if (ISSET(pstatus, RSAQ_STATUS_DSR))
+ sc->sc_msr |= UMSR_DSR;
+ if (ISSET(pstatus, RSAQ_STATUS_DCD))
+ sc->sc_msr |= UMSR_DCD;
+ ucom_status_change(&sc->sc_ucom);
+}
+
+Static void
+uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
+{
+ struct uplcom_softc *sc = addr;
+
+ DPRINTF(("uplcom_get_status:\n"));
+
+ if (lsr != NULL)
+ *lsr = sc->sc_lsr;
+ if (msr != NULL)
+ *msr = sc->sc_msr;
+}
+
+#if TODO
+Static int
+uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
+ usb_proc_ptr p)
+{
+ struct uplcom_softc *sc = addr;
+ int error = 0;
+
+ if (sc->sc_ucom.sc_dying)
+ return (EIO);
+
+ DPRINTF(("uplcom_ioctl: cmd = 0x%08lx\n", cmd));
+
+ switch (cmd) {
+ case TIOCNOTTY:
+ case TIOCMGET:
+ case TIOCMSET:
+ case USB_GET_CM_OVER_DATA:
+ case USB_SET_CM_OVER_DATA:
+ break;
+
+ default:
+ DPRINTF(("uplcom_ioctl: unknown\n"));
+ error = ENOTTY;
+ break;
+ }
+
+ return (error);
+}
+#endif
diff --git a/sys/dev/usb/usbdevs b/sys/dev/usb/usbdevs
index 784e471..158ec6b 100644
--- a/sys/dev/usb/usbdevs
+++ b/sys/dev/usb/usbdevs
@@ -197,6 +197,7 @@ vendor BRAINBOXES 0x05d1 Brainboxes Limited
vendor ULTIMA 0x05d8 Ultima
vendor AXIOHM 0x05d9 Axiohm Transaction Solutions
vendor MICROTEK 0x05da Microtek
+vendor SUNTAC 0x05db SUN Corporation
vendor LEXAR 0x05dc Lexar Media
vendor SYMBOL 0x05e0 Symbol Technologies
vendor GENESYS 0x05e3 Genesys Logic
@@ -900,6 +901,9 @@ product ROLAND UM1 0x0009 UM-1 MIDI I/F
/* Rockfire products */
product ROCKFIRE GAMEPAD 0x2033 gamepad 203USB
+/* RATOC Systems, Inc. */
+product RATOC REXUSB60 0xb000 USB serial adapter REX-USB60
+
/* SanDisk products */
product SANDISK SDDR05A 0x0001 ImageMate SDDR-05a
product SANDISK SDDR31 0x0002 ImageMate SDDR-31
@@ -957,6 +961,10 @@ product SUN KEYBOARD 0x0005 Type 6 USB keyboard
/* XXX The above is a North American PC style keyboard possibly */
product SUN MOUSE 0x0100 Type 6 USB mouse
+/* SUN Corporation */
+product SUNTAC PS64P1 0x0005 SUNTAC U-Cable type P1
+product SUNTAC VS10U 0x0009 SUNTAC Slipper U
+
/* Supra products */
product DIAMOND2 SUPRAEXPRESS56K 0x07da Supra Express 56K modem
product DIAMOND2 SUPRA2890 0x0b4a SupraMax 2890 56K Modem
diff --git a/sys/dev/usb/uvscom.c b/sys/dev/usb/uvscom.c
new file mode 100644
index 0000000..bdcf31a
--- /dev/null
+++ b/sys/dev/usb/uvscom.c
@@ -0,0 +1,888 @@
+/*-
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * 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/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/bus.h>
+#include <sys/ioccom.h>
+#include <sys/fcntl.h>
+#include <sys/conf.h>
+#include <sys/tty.h>
+#include <sys/file.h>
+#if __FreeBSD_version >= 500014
+#include <sys/selinfo.h>
+#else
+#include <sys/select.h>
+#endif
+#include <sys/proc.h>
+#include <sys/vnode.h>
+#include <sys/poll.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbcdc.h>
+
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/usb_quirks.h>
+
+#include <dev/usb/ucomvar.h>
+
+#ifdef UVSCOM_DEBUG
+#include <sys/sysctl.h>
+
+static int uvscomdebug = 1;
+
+SYSCTL_DECL(_debug_usb);
+SYSCTL_INT(_debug_usb, OID_AUTO, uvscom, CTLFLAG_RW,
+ &uvscomdebug, 0, "uvscom debug level");
+
+#define DPRINTFN(n, x) do { \
+ if (uvscomdebug > (n)) \
+ logprintf x; \
+ } while (0)
+#else
+#define DPRINTFN(n, x)
+#endif
+#define DPRINTF(x) DPRINTFN(0, x)
+
+#define UVSCOM_MODVER 1 /* module version */
+
+#define UVSCOM_CONFIG_INDEX 0
+#define UVSCOM_IFACE_INDEX 0
+
+#define UVSCOM_INTR_INTERVAL 100 /* mS */
+
+#define UVSCOM_UNIT_WAIT 5
+
+/* 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
+#if 0
+#define UVSCOM_PARITY_UNK 0x10
+#endif
+#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)
+
+struct uvscom_softc {
+ struct ucom_softc sc_ucom;
+
+ int sc_iface_number;/* interface number */
+
+ usbd_interface_handle sc_intr_iface; /* interrupt interface */
+ int sc_intr_number; /* interrupt number */
+ usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
+ u_char *sc_intr_buf; /* interrupt buffer */
+ int sc_isize;
+
+ u_char sc_dtr; /* current DTR state */
+ u_char sc_rts; /* current RTS state */
+
+ u_char sc_lsr; /* Local status register */
+ u_char sc_msr; /* uvscom status register */
+
+ uint16_t sc_lcr; /* Line control */
+ u_char sc_usr; /* unit status */
+};
+
+/*
+ * These are the maximum number of bytes transferred per frame.
+ * The output buffer size cannot be increased due to the size encoding.
+ */
+#define UVSCOMIBUFSIZE 512
+#define UVSCOMOBUFSIZE 64
+
+Static usbd_status uvscom_shutdown(struct uvscom_softc *);
+Static usbd_status uvscom_reset(struct uvscom_softc *);
+Static usbd_status uvscom_set_line_coding(struct uvscom_softc *,
+ uint16_t, uint16_t);
+Static usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t);
+Static usbd_status uvscom_set_crtscts(struct uvscom_softc *);
+Static void uvscom_get_status(void *, int, u_char *, u_char *);
+Static void uvscom_dtr(struct uvscom_softc *, int);
+Static void uvscom_rts(struct uvscom_softc *, int);
+Static void uvscom_break(struct uvscom_softc *, int);
+
+Static void uvscom_set(void *, int, int, int);
+Static void uvscom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
+#if TODO
+Static int uvscom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr);
+#endif
+Static int uvscom_param(void *, int, struct termios *);
+Static int uvscom_open(void *, int);
+Static void uvscom_close(void *, int);
+
+struct ucom_callback uvscom_callback = {
+ uvscom_get_status,
+ uvscom_set,
+ uvscom_param,
+ NULL, /* uvscom_ioctl, TODO */
+ uvscom_open,
+ uvscom_close,
+ NULL,
+ NULL
+};
+
+static const struct uvscom_product {
+ uint16_t vendor;
+ uint16_t product;
+} uvscom_products [] = {
+ /* SUNTAC U-Cable type P1 */
+ { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
+ /* SUNTAC Slipper U */
+ { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
+ { 0, 0 }
+};
+
+Static device_probe_t uvscom_match;
+Static device_attach_t uvscom_attach;
+Static device_detach_t uvscom_detach;
+
+Static device_method_t uvscom_methods[] = {
+ /* Device interface */
+ DEVMETHOD(device_probe, uvscom_match),
+ DEVMETHOD(device_attach, uvscom_attach),
+ DEVMETHOD(device_detach, uvscom_detach),
+ { 0, 0 }
+};
+
+Static driver_t uvscom_driver = {
+ "usio",
+ uvscom_methods,
+ sizeof (struct uvscom_softc)
+};
+
+DRIVER_MODULE(uvscom, uhub, uvscom_driver, ucom_devclass, usbd_driver_load, 0);
+MODULE_DEPEND(uvscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
+MODULE_VERSION(uvscom, UVSCOM_MODVER);
+
+USB_MATCH(uvscom)
+{
+ USB_MATCH_START(uvscom, uaa);
+ int i;
+
+ if (uaa->iface != NULL)
+ return (UMATCH_NONE);
+
+ for (i = 0; uvscom_products[i].vendor != 0; i++) {
+ if (uvscom_products[i].vendor == uaa->vendor &&
+ uvscom_products[i].product == uaa->product) {
+ return (UMATCH_VENDOR_PRODUCT);
+ }
+ }
+ return (UMATCH_NONE);
+}
+
+USB_ATTACH(uvscom)
+{
+ USB_ATTACH_START(uvscom, sc, uaa);
+ usbd_device_handle dev = uaa->device;
+ struct ucom_softc *ucom;
+ usb_config_descriptor_t *cdesc;
+ usb_interface_descriptor_t *id;
+ usb_endpoint_descriptor_t *ed;
+ char *devinfo;
+ const char *devname;
+ usbd_status err;
+ int i;
+
+ devinfo = malloc(1024, M_USBDEV, M_WAITOK);
+ ucom = &sc->sc_ucom;
+
+ bzero(sc, sizeof (struct uvscom_softc));
+
+ usbd_devinfo(dev, 0, devinfo);
+ /* USB_ATTACH_SETUP; */
+ ucom->sc_dev = self;
+ device_set_desc_copy(self, devinfo);
+ /* USB_ATTACH_SETUP; */
+
+ ucom->sc_udev = dev;
+ ucom->sc_iface = uaa->iface;
+
+ devname = USBDEVNAME(ucom->sc_dev);
+ printf("%s: %s\n", devname, devinfo);
+
+ DPRINTF(("uvscom attach: sc = %p\n", sc));
+
+ /* initialize endpoints */
+ ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
+ sc->sc_intr_number = -1;
+ sc->sc_intr_pipe = NULL;
+
+ /* Move the device into the configured state. */
+ err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
+ if (err) {
+ printf("%s: failed to set configuration, err=%s\n",
+ devname, usbd_errstr(err));
+ goto error;
+ }
+
+ /* get the config descriptor */
+ cdesc = usbd_get_config_descriptor(ucom->sc_udev);
+
+ if (cdesc == NULL) {
+ printf("%s: failed to get configuration descriptor\n",
+ USBDEVNAME(ucom->sc_dev));
+ goto error;
+ }
+
+ /* get the common interface */
+ err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
+ &ucom->sc_iface);
+ if (err) {
+ printf("%s: failed to get interface, err=%s\n",
+ devname, usbd_errstr(err));
+ goto error;
+ }
+
+ id = usbd_get_interface_descriptor(ucom->sc_iface);
+ sc->sc_iface_number = id->bInterfaceNumber;
+
+ /* Find endpoints */
+ for (i = 0; i < id->bNumEndpoints; i++) {
+ ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
+ if (ed == NULL) {
+ printf("%s: no endpoint descriptor for %d\n",
+ USBDEVNAME(ucom->sc_dev), i);
+ goto error;
+ }
+
+ if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ ucom->sc_bulkin_no = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ ucom->sc_bulkout_no = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
+ sc->sc_intr_number = ed->bEndpointAddress;
+ sc->sc_isize = UGETW(ed->wMaxPacketSize);
+ }
+ }
+
+ if (ucom->sc_bulkin_no == -1) {
+ printf("%s: Could not find data bulk in\n",
+ USBDEVNAME(ucom->sc_dev));
+ goto error;
+ }
+ if (ucom->sc_bulkout_no == -1) {
+ printf("%s: Could not find data bulk out\n",
+ USBDEVNAME(ucom->sc_dev));
+ goto error;
+ }
+ if (sc->sc_intr_number == -1) {
+ printf("%s: Could not find interrupt in\n",
+ USBDEVNAME(ucom->sc_dev));
+ goto error;
+ }
+
+ sc->sc_dtr = sc->sc_rts = 0;
+ sc->sc_lcr = UVSCOM_LINE_INIT;
+
+ ucom->sc_parent = sc;
+ ucom->sc_portno = UCOM_UNK_PORTNO;
+ /* bulkin, bulkout set above */
+ ucom->sc_ibufsize = UVSCOMIBUFSIZE;
+ ucom->sc_obufsize = UVSCOMOBUFSIZE;
+ ucom->sc_ibufsizepad = UVSCOMIBUFSIZE;
+ ucom->sc_opkthdrlen = 0;
+ ucom->sc_callback = &uvscom_callback;
+
+ err = uvscom_reset(sc);
+
+ if (err) {
+ printf("%s: reset failed, %s\n", USBDEVNAME(ucom->sc_dev),
+ usbd_errstr(err));
+ goto error;
+ }
+
+ DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
+ ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
+
+ ucom_attach(&sc->sc_ucom);
+
+ free(devinfo, M_USBDEV);
+ USB_ATTACH_SUCCESS_RETURN;
+
+error:
+ ucom->sc_dying = 1;
+ free(devinfo, M_USBDEV);
+ USB_ATTACH_ERROR_RETURN;
+}
+
+USB_DETACH(uvscom)
+{
+ USB_DETACH_START(uvscom, sc);
+ int rv = 0;
+
+ DPRINTF(("uvscom_detach: sc = %p\n", sc));
+
+ sc->sc_ucom.sc_dying = 1;
+
+ if (sc->sc_intr_pipe != NULL) {
+ usbd_abort_pipe(sc->sc_intr_pipe);
+ usbd_close_pipe(sc->sc_intr_pipe);
+ free(sc->sc_intr_buf, M_USBDEV);
+ sc->sc_intr_pipe = NULL;
+ }
+
+ rv = ucom_detach(&sc->sc_ucom);
+
+ return (rv);
+}
+
+Static usbd_status
+uvscom_readstat(struct uvscom_softc *sc)
+{
+ usb_device_request_t req;
+ usbd_status err;
+ uint16_t r;
+
+ DPRINTF(("%s: send readstat\n", USBDEVNAME(sc->sc_ucom.sc_dev)));
+
+ req.bmRequestType = UT_READ_VENDOR_DEVICE;
+ req.bRequest = UVSCOM_READ_STATUS;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, 0);
+ USETW(req.wLength, 2);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &r);
+ if (err) {
+ printf("%s: uvscom_readstat: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ DPRINTF(("%s: uvscom_readstat: r = %d\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), r));
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static usbd_status
+uvscom_shutdown(struct uvscom_softc *sc)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ DPRINTF(("%s: send shutdown\n", USBDEVNAME(sc->sc_ucom.sc_dev)));
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = UVSCOM_SHUTDOWN;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, 0);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
+ if (err) {
+ printf("%s: uvscom_shutdown: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static usbd_status
+uvscom_reset(struct uvscom_softc *sc)
+{
+ DPRINTF(("%s: uvscom_reset\n", USBDEVNAME(sc->sc_ucom.sc_dev)));
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static usbd_status
+uvscom_set_crtscts(struct uvscom_softc *sc)
+{
+ DPRINTF(("%s: uvscom_set_crtscts\n", USBDEVNAME(sc->sc_ucom.sc_dev)));
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static usbd_status
+uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ DPRINTF(("%s: uvscom_set_line: %04x\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), line));
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = UVSCOM_LINE_CTL;
+ USETW(req.wValue, line);
+ USETW(req.wIndex, 0);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
+ if (err) {
+ printf("%s: uvscom_set_line: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static usbd_status
+uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), lsp, ls));
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = UVSCOM_SET_SPEED;
+ USETW(req.wValue, lsp);
+ USETW(req.wIndex, 0);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
+ if (err) {
+ printf("%s: uvscom_set_line_coding: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = UVSCOM_SET_PARAM;
+ USETW(req.wValue, ls);
+ USETW(req.wIndex, 0);
+ USETW(req.wLength, 0);
+
+ err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
+ if (err) {
+ printf("%s: uvscom_set_line_coding: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
+ return (err);
+ }
+
+ return (USBD_NORMAL_COMPLETION);
+}
+
+Static void
+uvscom_dtr(struct uvscom_softc *sc, int onoff)
+{
+ DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), onoff));
+
+ if (sc->sc_dtr == onoff)
+ return; /* no change */
+
+ sc->sc_dtr = onoff;
+
+ if (onoff)
+ SET(sc->sc_lcr, UVSCOM_DTR);
+ else
+ CLR(sc->sc_lcr, UVSCOM_DTR);
+
+ uvscom_set_line(sc, sc->sc_lcr);
+}
+
+Static void
+uvscom_rts(struct uvscom_softc *sc, int onoff)
+{
+ DPRINTF(("%s: uvscom_rts: onoff = %d\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), onoff));
+
+ if (sc->sc_rts == onoff)
+ return; /* no change */
+
+ sc->sc_rts = onoff;
+
+ if (onoff)
+ SET(sc->sc_lcr, UVSCOM_RTS);
+ else
+ CLR(sc->sc_lcr, UVSCOM_RTS);
+
+ uvscom_set_line(sc, sc->sc_lcr);
+}
+
+Static void
+uvscom_break(struct uvscom_softc *sc, int onoff)
+{
+ DPRINTF(("%s: uvscom_break: onoff = %d\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), onoff));
+
+ if (onoff)
+ uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
+}
+
+Static void
+uvscom_set(void *addr, int portno, int reg, int onoff)
+{
+ struct uvscom_softc *sc = addr;
+
+ switch (reg) {
+ case UCOM_SET_DTR:
+ uvscom_dtr(sc, onoff);
+ break;
+ case UCOM_SET_RTS:
+ uvscom_rts(sc, onoff);
+ break;
+ case UCOM_SET_BREAK:
+ uvscom_break(sc, onoff);
+ break;
+ default:
+ break;
+ }
+}
+
+Static int
+uvscom_param(void *addr, int portno, struct termios *t)
+{
+ struct uvscom_softc *sc = addr;
+ usbd_status err;
+ uint16_t lsp;
+ uint16_t ls;
+
+ DPRINTF(("%s: uvscom_param: sc = %p\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), sc));
+
+ ls = 0;
+
+ switch (t->c_ospeed) {
+ case B150:
+ lsp = UVSCOM_SPEED_150BPS;
+ break;
+ case B300:
+ lsp = UVSCOM_SPEED_300BPS;
+ break;
+ case B600:
+ lsp = UVSCOM_SPEED_600BPS;
+ break;
+ case B1200:
+ lsp = UVSCOM_SPEED_1200BPS;
+ break;
+ case B2400:
+ lsp = UVSCOM_SPEED_2400BPS;
+ break;
+ case B4800:
+ lsp = UVSCOM_SPEED_4800BPS;
+ break;
+ case B9600:
+ lsp = UVSCOM_SPEED_9600BPS;
+ break;
+ case B19200:
+ lsp = UVSCOM_SPEED_19200BPS;
+ break;
+ case B38400:
+ lsp = UVSCOM_SPEED_38400BPS;
+ break;
+ case B57600:
+ lsp = UVSCOM_SPEED_57600BPS;
+ break;
+ case B115200:
+ lsp = UVSCOM_SPEED_115200BPS;
+ break;
+ default:
+ return (EIO);
+ }
+
+ if (ISSET(t->c_cflag, CSTOPB))
+ SET(ls, UVSCOM_STOP_BIT_2);
+ else
+ SET(ls, UVSCOM_STOP_BIT_1);
+
+ if (ISSET(t->c_cflag, PARENB)) {
+ if (ISSET(t->c_cflag, PARODD))
+ SET(ls, UVSCOM_PARITY_ODD);
+ else
+ SET(ls, UVSCOM_PARITY_EVEN);
+ } else
+ SET(ls, UVSCOM_PARITY_NONE);
+
+ switch (ISSET(t->c_cflag, CSIZE)) {
+ case CS5:
+ SET(ls, UVSCOM_DATA_BIT_5);
+ break;
+ case CS6:
+ SET(ls, UVSCOM_DATA_BIT_6);
+ break;
+ case CS7:
+ SET(ls, UVSCOM_DATA_BIT_7);
+ break;
+ case CS8:
+ SET(ls, UVSCOM_DATA_BIT_8);
+ break;
+ default:
+ return (EIO);
+ }
+
+ err = uvscom_set_line_coding(sc, lsp, ls);
+ if (err)
+ return (EIO);
+
+ if (ISSET(t->c_cflag, CRTSCTS)) {
+ err = uvscom_set_crtscts(sc);
+ if (err)
+ return (EIO);
+ }
+
+ return (0);
+}
+
+Static int
+uvscom_open(void *addr, int portno)
+{
+ struct uvscom_softc *sc = addr;
+ int err;
+ int i;
+
+ if (sc->sc_ucom.sc_dying)
+ return (EIO);
+
+ DPRINTF(("uvscom_open: sc = %p\n", sc));
+
+ if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
+ DPRINTF(("uvscom_open: open interrupt pipe.\n"));
+
+ sc->sc_usr = 0; /* clear unit status */
+
+ err = uvscom_readstat(sc);
+ if (err) {
+ DPRINTF(("%s: uvscom_open: readstat faild\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev)));
+ return (EIO);
+ }
+
+ sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
+ err = usbd_open_pipe_intr(sc->sc_ucom.sc_iface,
+ sc->sc_intr_number,
+ USBD_SHORT_XFER_OK,
+ &sc->sc_intr_pipe,
+ sc,
+ sc->sc_intr_buf,
+ sc->sc_isize,
+ uvscom_intr,
+ UVSCOM_INTR_INTERVAL);
+ if (err) {
+ printf("%s: cannot open interrupt pipe (addr %d)\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ sc->sc_intr_number);
+ return (EIO);
+ }
+ } else {
+ DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
+ }
+
+ if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
+ /* unit is not ready */
+
+ for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
+ tsleep(&err, TTIPRI, "uvsop", hz); /* XXX */
+ if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
+ break;
+ }
+ if (i == 0) {
+ DPRINTF(("%s: unit is not ready\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev)));
+ return (EIO);
+ }
+
+ /* check PC card was inserted */
+ if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
+ DPRINTF(("%s: no card\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev)));
+ return (EIO);
+ }
+ }
+
+ return (0);
+}
+
+Static void
+uvscom_close(void *addr, int portno)
+{
+ struct uvscom_softc *sc = addr;
+ int err;
+
+ if (sc->sc_ucom.sc_dying)
+ return;
+
+ DPRINTF(("uvscom_close: close\n"));
+
+ uvscom_shutdown(sc);
+
+ if (sc->sc_intr_pipe != NULL) {
+ err = usbd_abort_pipe(sc->sc_intr_pipe);
+ if (err)
+ printf("%s: abort interrupt pipe failed: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ usbd_errstr(err));
+ err = usbd_close_pipe(sc->sc_intr_pipe);
+ if (err)
+ printf("%s: close interrupt pipe failed: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ usbd_errstr(err));
+ free(sc->sc_intr_buf, M_USBDEV);
+ sc->sc_intr_pipe = NULL;
+ }
+}
+
+Static void
+uvscom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
+{
+ struct uvscom_softc *sc = priv;
+ u_char *buf = sc->sc_intr_buf;
+ u_char pstatus;
+
+ if (sc->sc_ucom.sc_dying)
+ return;
+
+ if (status != USBD_NORMAL_COMPLETION) {
+ if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
+ return;
+
+ printf("%s: uvscom_intr: abnormal status: %s\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev),
+ usbd_errstr(status));
+ usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
+ return;
+ }
+
+ DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
+ USBDEVNAME(sc->sc_ucom.sc_dev), buf[0], buf[1]));
+
+ sc->sc_lsr = sc->sc_msr = 0;
+ sc->sc_usr = buf[1];
+
+ pstatus = buf[0];
+ if (ISSET(pstatus, UVSCOM_TXRDY))
+ SET(sc->sc_lsr, ULSR_TXRDY);
+ if (ISSET(pstatus, UVSCOM_RXRDY))
+ SET(sc->sc_lsr, ULSR_RXRDY);
+
+ pstatus = buf[1];
+ if (ISSET(pstatus, UVSCOM_CTS))
+ SET(sc->sc_msr, UMSR_CTS);
+ if (ISSET(pstatus, UVSCOM_DSR))
+ SET(sc->sc_msr, UMSR_DSR);
+ if (ISSET(pstatus, UVSCOM_DCD))
+ SET(sc->sc_msr, UMSR_DCD);
+
+ ucom_status_change(&sc->sc_ucom);
+}
+
+Static void
+uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
+{
+ struct uvscom_softc *sc = addr;
+
+ if (lsr != NULL)
+ *lsr = sc->sc_lsr;
+ if (msr != NULL)
+ *msr = sc->sc_msr;
+}
+
+#if TODO
+Static int
+uvscom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
+ usb_proc_ptr p)
+{
+ struct uvscom_softc *sc = addr;
+ int error = 0;
+
+ if (sc->sc_ucom.sc_dying)
+ return (EIO);
+
+ DPRINTF(("uvscom_ioctl: cmd = 0x%08lx\n", cmd));
+
+ switch (cmd) {
+ case TIOCNOTTY:
+ case TIOCMGET:
+ case TIOCMSET:
+ break;
+
+ default:
+ DPRINTF(("uvscom_ioctl: unknown\n"));
+ error = ENOTTY;
+ break;
+ }
+
+ return (error);
+}
+#endif
OpenPOWER on IntegriCloud