4 * $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $
5 * $NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $
6 * $FreeBSD: src/sys/dev/usb/ucom.c,v 1.35 2003/11/16 11:58:21 akiyama Exp $
7 * $DragonFly: src/sys/dev/usbmisc/ucom/ucom.c,v 1.32 2007/11/05 19:09:43 hasso Exp $
10 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
37 * All rights reserved.
39 * This code is derived from software contributed to The NetBSD Foundation
40 * by Lennart Augustsson (lennart@augustsson.net) at
41 * Carlstedt Research & Technology.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the NetBSD
54 * Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/fcntl.h>
80 #include <sys/clist.h>
84 #include <sys/sysctl.h>
85 #include <sys/thread2.h>
87 #include <bus/usb/usb.h>
88 #include <bus/usb/usbcdc.h>
90 #include <bus/usb/usbdi.h>
91 #include <bus/usb/usbdi_util.h>
92 #include <bus/usb/usb_quirks.h>
97 static int ucomdebug = 0;
98 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
99 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
100 &ucomdebug, 0, "ucom debug level");
101 #define DPRINTF(x) do { \
106 #define DPRINTFN(n, x) do { \
107 if (ucomdebug > (n)) \
112 #define DPRINTFN(n, x)
115 static d_open_t ucomopen;
116 static d_close_t ucomclose;
117 static d_read_t ucomread;
118 static d_write_t ucomwrite;
119 static d_ioctl_t ucomioctl;
121 #define UCOM_CDEV_MAJOR 138
123 static struct dev_ops ucom_ops = {
124 { "ucom", UCOM_CDEV_MAJOR, D_TTY },
126 .d_close = ucomclose,
128 .d_write = ucomwrite,
129 .d_ioctl = ucomioctl,
130 .d_kqfilter = ttykqfilter,
131 .d_revoke = ttyrevoke
134 static void ucom_cleanup(struct ucom_softc *);
135 static int ucomctl(struct ucom_softc *, int, int);
136 static int ucomparam(struct tty *, struct termios *);
137 static void ucomstart(struct tty *);
138 static void ucomstop(struct tty *, int);
139 static void ucom_shutdown(struct ucom_softc *);
140 static void ucom_dtr(struct ucom_softc *, int);
141 static void ucom_rts(struct ucom_softc *, int);
142 static void ucom_break(struct ucom_softc *, int);
143 static usbd_status ucomstartread(struct ucom_softc *);
144 static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
145 static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
146 static void ucomstopread(struct ucom_softc *);
147 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
149 devclass_t ucom_devclass;
151 static moduledata_t ucom_mod = {
157 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
158 MODULE_DEPEND(ucom, usb, 1, 1, 1);
159 MODULE_VERSION(ucom, UCOM_MODVER);
162 ucom_attach(struct ucom_softc *sc)
168 lwkt_gettoken(&tty_token);
169 unit = device_get_unit(sc->sc_dev);
171 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
172 tp->t_oproc = ucomstart;
173 tp->t_param = ucomparam;
174 tp->t_stop = ucomstop;
176 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
178 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
180 dev = make_dev(&ucom_ops, unit | UCOM_CALLOUT_MASK,
181 UID_UUCP, GID_DIALER, 0660,
185 lwkt_reltoken(&tty_token);
191 ucom_detach(struct ucom_softc *sc)
193 struct tty *tp = sc->sc_tty;
196 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
198 destroy_dev(sc->dev);
199 lwkt_gettoken(&tty_token);
202 if (sc->sc_bulkin_pipe != NULL)
203 usbd_abort_pipe(sc->sc_bulkin_pipe);
204 if (sc->sc_bulkout_pipe != NULL)
205 usbd_abort_pipe(sc->sc_bulkout_pipe);
208 if (tp->t_state & TS_ISOPEN) {
209 device_printf(sc->sc_dev,
210 "still open, forcing close\n");
211 (*linesw[tp->t_line].l_close)(tp, 0);
215 DPRINTF(("ucom_detach: no tty\n"));
216 lwkt_reltoken(&tty_token);
221 if (--sc->sc_refcnt >= 0) {
222 /* Wait for processes to go away. */
223 usb_detach_wait(sc->sc_dev);
227 unit = device_get_unit(sc->sc_dev);
228 dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit);
230 lwkt_reltoken(&tty_token);
235 ucom_shutdown(struct ucom_softc *sc)
237 struct tty *tp = sc->sc_tty;
239 lwkt_gettoken(&tty_token);
240 DPRINTF(("ucom_shutdown\n"));
242 * Hang up if necessary. Wait a bit, so the other side has time to
243 * notice even if we immediately open the port again.
245 if (ISSET(tp->t_cflag, HUPCL)) {
246 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
247 (void)tsleep(sc, 0, "ucomsd", hz);
249 lwkt_reltoken(&tty_token);
253 ucomopen(struct dev_open_args *ap)
255 cdev_t dev = ap->a_head.a_dev;
256 int unit = UCOMUNIT(dev);
257 struct ucom_softc *sc;
262 sc = devclass_get_softc(ucom_devclass, unit);
269 lwkt_gettoken(&tty_token);
272 DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
274 if (ISSET(tp->t_state, TS_ISOPEN) &&
275 ISSET(tp->t_state, TS_XCLUDE) &&
276 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
278 lwkt_reltoken(&tty_token);
283 * Do the following iff this is a first open.
286 while (sc->sc_opening)
287 tsleep(&sc->sc_opening, 0, "ucomop", 0);
290 if (!ISSET(tp->t_state, TS_ISOPEN)) {
294 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
296 tp->t_dev = reference_dev(dev);
299 * Initialize the termios status to the defaults. Add in the
300 * sticky bits from TIOCSFLAGS.
303 t.c_ospeed = TTYDEF_SPEED;
304 t.c_cflag = TTYDEF_CFLAG;
305 /* Make sure ucomparam() will do something. */
307 (void)ucomparam(tp, &t);
308 tp->t_iflag = TTYDEF_IFLAG;
309 tp->t_oflag = TTYDEF_OFLAG;
310 tp->t_lflag = TTYDEF_LFLAG;
315 * Turn on DTR. We must always do this, even if carrier is not
316 * present, because otherwise we'd have to use TIOCSDTR
317 * immediately after setting CLOCAL, which applications do not
318 * expect. We always assert DTR while the device is open
319 * unless explicitly requested to deassert it.
321 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
323 /* Device specific open */
324 if (sc->sc_callback->ucom_open != NULL) {
325 error = sc->sc_callback->ucom_open(sc->sc_parent,
330 wakeup(&sc->sc_opening);
332 lwkt_reltoken(&tty_token);
337 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
338 sc->sc_bulkin_no, sc->sc_bulkout_no));
340 /* Open the bulk pipes */
342 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
343 &sc->sc_bulkin_pipe);
345 kprintf("%s: open bulk in error (addr %d): %s\n",
346 device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
352 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
353 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
355 kprintf("%s: open bulk out error (addr %d): %s\n",
356 device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
362 /* Allocate a request and an input buffer and start reading. */
363 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
364 if (sc->sc_ixfer == NULL) {
369 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
371 if (sc->sc_ibuf == NULL) {
376 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
377 if (sc->sc_oxfer == NULL) {
382 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
385 if (sc->sc_obuf == NULL) {
391 * Handle initial DCD.
393 if (ISSET(sc->sc_msr, UMSR_DCD) ||
394 (minor(dev) & UCOM_CALLOUT_MASK))
395 (*linesw[tp->t_line].l_modem)(tp, 1);
401 wakeup(&sc->sc_opening);
404 error = ttyopen(dev, tp);
408 error = (*linesw[tp->t_line].l_open)(dev, tp);
412 disc_optim(tp, &tp->t_termios, sc);
414 DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
419 lwkt_reltoken(&tty_token);
423 usbd_free_xfer(sc->sc_oxfer);
426 usbd_free_xfer(sc->sc_ixfer);
429 usbd_close_pipe(sc->sc_bulkout_pipe);
430 sc->sc_bulkout_pipe = NULL;
432 usbd_close_pipe(sc->sc_bulkin_pipe);
433 sc->sc_bulkin_pipe = NULL;
436 wakeup(&sc->sc_opening);
438 lwkt_reltoken(&tty_token);
442 if (!ISSET(tp->t_state, TS_ISOPEN)) {
444 * We failed to open the device, and nobody else had it opened.
445 * Clean up the state as appropriate.
450 DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
452 lwkt_reltoken(&tty_token);
457 ucomclose(struct dev_close_args *ap)
459 cdev_t dev = ap->a_head.a_dev;
460 struct ucom_softc *sc;
463 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
465 lwkt_gettoken(&tty_token);
468 DPRINTF(("%s: ucomclose: unit = %d\n",
469 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
471 if (!ISSET(tp->t_state, TS_ISOPEN))
475 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
476 disc_optim(tp, &tp->t_termios, sc);
483 if (!ISSET(tp->t_state, TS_ISOPEN)) {
485 * Although we got a last close, the device may still be in
486 * use; e.g. if this was the dialout node, and there are still
487 * processes waiting for carrier on the non-dialout node.
492 if (sc->sc_callback->ucom_close != NULL)
493 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
497 release_dev(tp->t_dev);
501 if (--sc->sc_refcnt < 0)
502 usb_detach_wakeup(sc->sc_dev);
504 lwkt_reltoken(&tty_token);
509 ucomread(struct dev_read_args *ap)
511 cdev_t dev = ap->a_head.a_dev;
512 struct ucom_softc *sc;
516 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
517 lwkt_gettoken(&tty_token);
520 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
523 lwkt_reltoken(&tty_token);
527 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
529 DPRINTF(("ucomread: error = %d\n", error));
531 lwkt_reltoken(&tty_token);
536 ucomwrite(struct dev_write_args *ap)
538 cdev_t dev = ap->a_head.a_dev;
539 struct ucom_softc *sc;
543 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
544 lwkt_gettoken(&tty_token);
547 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
550 lwkt_reltoken(&tty_token);
554 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
556 DPRINTF(("ucomwrite: error = %d\n", error));
558 lwkt_reltoken(&tty_token);
563 ucomioctl(struct dev_ioctl_args *ap)
565 cdev_t dev = ap->a_head.a_dev;
566 struct ucom_softc *sc;
571 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
572 lwkt_gettoken(&tty_token);
576 lwkt_reltoken(&tty_token);
580 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
582 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
583 ap->a_fflag, ap->a_cred);
584 if (error != ENOIOCTL) {
585 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
586 lwkt_reltoken(&tty_token);
592 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
593 disc_optim(tp, &tp->t_termios, sc);
594 if (error != ENOIOCTL) {
596 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
597 lwkt_reltoken(&tty_token);
601 if (sc->sc_callback->ucom_ioctl != NULL) {
602 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
604 ap->a_cmd, ap->a_data,
605 ap->a_fflag, curthread);
608 lwkt_reltoken(&tty_token);
615 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
619 DPRINTF(("ucomioctl: TIOCSBRK\n"));
623 DPRINTF(("ucomioctl: TIOCCBRK\n"));
628 DPRINTF(("ucomioctl: TIOCSDTR\n"));
629 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
632 DPRINTF(("ucomioctl: TIOCCDTR\n"));
633 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
637 d = *(int *)ap->a_data;
638 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
639 (void)ucomctl(sc, d, DMSET);
642 d = *(int *)ap->a_data;
643 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
644 (void)ucomctl(sc, d, DMBIS);
647 d = *(int *)ap->a_data;
648 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
649 (void)ucomctl(sc, d, DMBIC);
652 d = ucomctl(sc, 0, DMGET);
653 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
654 *(int *)ap->a_data = d;
658 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
665 lwkt_reltoken(&tty_token);
670 * NOTE: Must be called with tty_token held.
673 ucomctl(struct ucom_softc *sc, int bits, int how)
679 ASSERT_LWKT_TOKEN_HELD(&tty_token);
680 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
683 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
684 DPRINTF(("ucomctl: DMGET: LE"));
687 if (ISSET(mcr, UMCR_DTR)) {
688 SET(bits, TIOCM_DTR);
691 if (ISSET(mcr, UMCR_RTS)) {
692 SET(bits, TIOCM_RTS);
697 if (ISSET(msr, UMSR_CTS)) {
698 SET(bits, TIOCM_CTS);
701 if (ISSET(msr, UMSR_DCD)) {
705 if (ISSET(msr, UMSR_DSR)) {
706 SET(bits, TIOCM_DSR);
709 if (ISSET(msr, UMSR_RI)) {
720 if (ISSET(bits, TIOCM_DTR))
722 if (ISSET(bits, TIOCM_RTS))
737 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
740 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
747 * NOTE: Must be called with tty_token held.
750 ucom_break(struct ucom_softc *sc, int onoff)
752 ASSERT_LWKT_TOKEN_HELD(&tty_token);
753 DPRINTF(("ucom_break: onoff = %d\n", onoff));
755 if (sc->sc_callback->ucom_set == NULL)
757 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
758 UCOM_SET_BREAK, onoff);
762 * NOTE: Must be called with tty_token held.
765 ucom_dtr(struct ucom_softc *sc, int onoff)
767 ASSERT_LWKT_TOKEN_HELD(&tty_token);
768 DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
770 if (sc->sc_callback->ucom_set == NULL)
772 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
773 UCOM_SET_DTR, onoff);
777 * NOTE: Must be called with tty_token held.
780 ucom_rts(struct ucom_softc *sc, int onoff)
782 ASSERT_LWKT_TOKEN_HELD(&tty_token);
783 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
785 if (sc->sc_callback->ucom_set == NULL)
787 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
788 UCOM_SET_RTS, onoff);
792 ucom_status_change(struct ucom_softc *sc)
794 struct tty *tp = sc->sc_tty;
798 lwkt_gettoken(&tty_token);
799 if (sc->sc_callback->ucom_get_status == NULL) {
802 lwkt_reltoken(&tty_token);
806 old_msr = sc->sc_msr;
807 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
808 &sc->sc_lsr, &sc->sc_msr);
809 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
810 if (sc->sc_poll == 0) {
811 lwkt_reltoken(&tty_token);
814 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
815 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
816 (*linesw[tp->t_line].l_modem)(tp, onoff);
818 lwkt_reltoken(&tty_token);
822 ucomparam(struct tty *tp, struct termios *t)
824 struct ucom_softc *sc;
828 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
830 lwkt_gettoken(&tty_token);
832 lwkt_reltoken(&tty_token);
836 DPRINTF(("ucomparam: sc = %p\n", sc));
838 /* Check requested parameters. */
839 if (t->c_ospeed < 0) {
840 DPRINTF(("ucomparam: negative ospeed\n"));
841 lwkt_reltoken(&tty_token);
844 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
845 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
846 lwkt_reltoken(&tty_token);
851 * If there were no changes, don't do anything. This avoids dropping
852 * input and improves performance when all we did was frob things like
855 if (tp->t_ospeed == t->c_ospeed &&
856 tp->t_cflag == t->c_cflag) {
857 lwkt_reltoken(&tty_token);
861 /* And copy to tty. */
863 tp->t_ospeed = t->c_ospeed;
864 tp->t_cflag = t->c_cflag;
866 if (sc->sc_callback->ucom_param == NULL) {
867 lwkt_reltoken(&tty_token);
873 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
875 DPRINTF(("ucomparam: callback: error = %d\n", error));
876 lwkt_reltoken(&tty_token);
882 if (t->c_cflag & CRTS_IFLOW) {
883 sc->sc_state |= UCS_RTS_IFLOW;
884 } else if (sc->sc_state & UCS_RTS_IFLOW) {
885 sc->sc_state &= ~UCS_RTS_IFLOW;
886 (void)ucomctl(sc, UMCR_RTS, DMBIS);
889 disc_optim(tp, t, sc);
891 uerr = ucomstartread(sc);
892 if (uerr != USBD_NORMAL_COMPLETION) {
893 lwkt_reltoken(&tty_token);
897 lwkt_reltoken(&tty_token);
902 ucomstart(struct tty *tp)
904 struct ucom_softc *sc;
910 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
911 DPRINTF(("ucomstart: sc = %p\n", sc));
913 lwkt_gettoken(&tty_token);
916 lwkt_reltoken(&tty_token);
922 if (tp->t_state & TS_TBLOCK) {
923 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
924 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
925 DPRINTF(("ucomstart: clear RTS\n"));
926 (void)ucomctl(sc, UMCR_RTS, DMBIC);
929 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
930 tp->t_rawq.c_cc <= tp->t_ilowat &&
931 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
932 DPRINTF(("ucomstart: set RTS\n"));
933 (void)ucomctl(sc, UMCR_RTS, DMBIS);
937 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
939 DPRINTF(("ucomstart: stopped\n"));
943 if (tp->t_outq.c_cc <= tp->t_olowat) {
944 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
945 CLR(tp->t_state, TS_SO_OLOWAT);
946 wakeup(TSA_OLOWAT(tp));
948 KNOTE(&tp->t_wkq.ki_note, 0);
949 if (tp->t_outq.c_cc == 0) {
950 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
951 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
952 CLR(tp->t_state, TS_SO_OCOMPLETE);
953 wakeup(TSA_OCOMPLETE(tp));
959 /* Grab the first contiguous region of buffer space. */
960 data = tp->t_outq.c_cf;
961 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
962 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
965 DPRINTF(("ucomstart: cnt == 0\n"));
969 SET(tp->t_state, TS_BUSY);
971 if (cnt > sc->sc_obufsize) {
972 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
973 cnt = sc->sc_obufsize;
975 if (sc->sc_callback->ucom_write != NULL)
976 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
977 sc->sc_obuf, data, &cnt);
979 memcpy(sc->sc_obuf, data, cnt);
981 DPRINTF(("ucomstart: %d chars\n", cnt));
982 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
983 (usbd_private_handle)sc, sc->sc_obuf, cnt,
984 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
985 /* What can we do on error? */
986 err = usbd_transfer(sc->sc_oxfer);
987 if (err != USBD_IN_PROGRESS)
988 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
994 lwkt_reltoken(&tty_token);
998 ucomstop(struct tty *tp, int flag)
1000 struct ucom_softc *sc;
1002 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
1004 DPRINTF(("ucomstop: %d\n", flag));
1006 lwkt_gettoken(&tty_token);
1009 * This is just supposed to flush pending receive data,
1010 * not stop the reception of data entirely!
1012 DPRINTF(("ucomstop: read\n"));
1017 if (flag & FWRITE) {
1018 DPRINTF(("ucomstop: write\n"));
1020 if (ISSET(tp->t_state, TS_BUSY)) {
1022 if (!ISSET(tp->t_state, TS_TTSTOP))
1023 SET(tp->t_state, TS_FLUSH);
1028 DPRINTF(("ucomstop: done\n"));
1029 lwkt_reltoken(&tty_token);
1033 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1035 struct ucom_softc *sc = (struct ucom_softc *)p;
1036 struct tty *tp = sc->sc_tty;
1039 lwkt_gettoken(&tty_token);
1040 DPRINTF(("ucomwritecb: status = %d\n", status));
1042 if (status == USBD_CANCELLED || sc->sc_dying)
1045 if (status != USBD_NORMAL_COMPLETION) {
1046 kprintf("%s: ucomwritecb: %s\n",
1047 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1048 if (status == USBD_STALLED)
1049 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1051 * XXX. We may need a flag to sequence ucomstopread() and
1052 * ucomstartread() to handle the case where ucomstartread()
1053 * is called after ucomstopread() but before the request has
1054 * been properly canceled?
1059 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1060 DPRINTF(("ucomwritecb: cc = %d\n", cc));
1061 if (cc <= sc->sc_opkthdrlen) {
1062 kprintf("%s: sent size too small, cc = %d\n",
1063 device_get_nameunit(sc->sc_dev), cc);
1067 /* convert from USB bytes to tty bytes */
1068 cc -= sc->sc_opkthdrlen;
1071 CLR(tp->t_state, TS_BUSY);
1072 if (ISSET(tp->t_state, TS_FLUSH))
1073 CLR(tp->t_state, TS_FLUSH);
1075 ndflush(&tp->t_outq, cc);
1076 (*linesw[tp->t_line].l_start)(tp);
1079 lwkt_reltoken(&tty_token);
1084 CLR(tp->t_state, TS_BUSY);
1086 lwkt_reltoken(&tty_token);
1091 * NOTE: Must be called with tty_token held
1094 ucomstartread(struct ucom_softc *sc)
1098 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1099 DPRINTF(("ucomstartread: start\n"));
1101 sc->sc_state &= ~UCS_RXSTOP;
1103 if (sc->sc_bulkin_pipe == NULL)
1104 return (USBD_NORMAL_COMPLETION);
1106 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1107 (usbd_private_handle)sc,
1108 sc->sc_ibuf, sc->sc_ibufsize,
1109 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1110 USBD_NO_TIMEOUT, ucomreadcb);
1112 err = usbd_transfer(sc->sc_ixfer);
1113 if (err != USBD_IN_PROGRESS) {
1114 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1118 return (USBD_NORMAL_COMPLETION);
1122 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1124 lwkt_gettoken(&tty_token);
1126 struct ucom_softc *sc = (struct ucom_softc *)p;
1127 struct tty *tp = sc->sc_tty;
1128 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1134 DPRINTF(("ucomreadcb: status = %d\n", status));
1136 if (status != USBD_NORMAL_COMPLETION) {
1137 if (!(sc->sc_state & UCS_RXSTOP))
1138 kprintf("%s: ucomreadcb: %s\n",
1139 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1140 if (status == USBD_STALLED)
1141 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1142 /* XXX we should restart after some delay. */
1143 lwkt_reltoken(&tty_token);
1147 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1148 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1152 if (sc->sc_callback->ucom_read != NULL) {
1153 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1157 if (cc > sc->sc_ibufsize) {
1158 kprintf("%s: invalid receive data size, %d chars\n",
1159 device_get_nameunit(sc->sc_dev), cc);
1166 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1167 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1168 && (sc->sc_state & UCS_RTS_IFLOW
1169 || tp->t_iflag & IXOFF)
1170 && !(tp->t_state & TS_TBLOCK))
1172 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1176 if (*cp == sc->hotchar)
1185 if (tp->t_state & TS_TTSTOP
1186 && (tp->t_iflag & IXANY
1187 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1188 tp->t_state &= ~TS_TTSTOP;
1189 tp->t_lflag &= ~FLUSHO;
1193 kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1196 /* Give characters to tty layer. */
1198 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1199 if ((*rint)(*cp, tp) == -1) {
1200 /* XXX what should we do? */
1201 kprintf("%s: lost %d chars\n",
1202 device_get_nameunit(sc->sc_dev), cc);
1212 err = ucomstartread(sc);
1214 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1215 /* XXX what should we dow now? */
1218 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1219 && !(tp->t_state & TS_TBLOCK))
1220 ucomctl(sc, UMCR_RTS, DMBIS);
1222 lwkt_reltoken(&tty_token);
1226 * NOTE: Must be called with tty_token held.
1229 ucom_cleanup(struct ucom_softc *sc)
1231 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1232 DPRINTF(("ucom_cleanup: closing pipes\n"));
1235 if (sc->sc_bulkin_pipe != NULL) {
1236 usbd_abort_pipe(sc->sc_bulkin_pipe);
1237 usbd_close_pipe(sc->sc_bulkin_pipe);
1238 sc->sc_bulkin_pipe = NULL;
1240 if (sc->sc_bulkout_pipe != NULL) {
1241 usbd_abort_pipe(sc->sc_bulkout_pipe);
1242 usbd_close_pipe(sc->sc_bulkout_pipe);
1243 sc->sc_bulkout_pipe = NULL;
1245 if (sc->sc_ixfer != NULL) {
1246 usbd_free_xfer(sc->sc_ixfer);
1247 sc->sc_ixfer = NULL;
1249 if (sc->sc_oxfer != NULL) {
1250 usbd_free_xfer(sc->sc_oxfer);
1251 sc->sc_oxfer = NULL;
1256 * NOTE: Must be called with tty_token held.
1259 ucomstopread(struct ucom_softc *sc)
1263 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1264 DPRINTF(("ucomstopread: enter\n"));
1266 if (!(sc->sc_state & UCS_RXSTOP)) {
1267 sc->sc_state |= UCS_RXSTOP;
1268 if (sc->sc_bulkin_pipe == NULL) {
1269 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1272 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1274 DPRINTF(("ucomstopread: err = %s\n",
1279 DPRINTF(("ucomstopread: leave\n"));
1283 * NOTE: Must be called with tty_token held.
1286 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1288 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1289 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1290 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1291 && (!(t->c_iflag & PARMRK)
1292 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1293 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1294 && linesw[tp->t_line].l_rint == ttyinput) {
1295 DPRINTF(("disc_optim: bypass l_rint\n"));
1296 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1298 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1299 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1301 sc->hotchar = linesw[tp->t_line].l_hotchar;