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 $
9 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
36 * All rights reserved.
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Lennart Augustsson (lennart@augustsson.net) at
40 * Carlstedt Research & Technology.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
76 #include <sys/fcntl.h>
79 #include <sys/clist.h>
83 #include <sys/sysctl.h>
84 #include <sys/thread2.h>
86 #include <bus/usb/usb.h>
87 #include <bus/usb/usbcdc.h>
89 #include <bus/usb/usbdi.h>
90 #include <bus/usb/usbdi_util.h>
91 #include <bus/usb/usb_quirks.h>
96 static int ucomdebug = 0;
97 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
98 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
99 &ucomdebug, 0, "ucom debug level");
100 #define DPRINTF(x) do { \
105 #define DPRINTFN(n, x) do { \
106 if (ucomdebug > (n)) \
111 #define DPRINTFN(n, x)
114 static d_open_t ucomopen;
115 static d_close_t ucomclose;
116 static d_read_t ucomread;
117 static d_write_t ucomwrite;
118 static d_ioctl_t ucomioctl;
120 static struct dev_ops ucom_ops = {
121 { "ucom", 0, D_TTY },
123 .d_close = ucomclose,
125 .d_write = ucomwrite,
126 .d_ioctl = ucomioctl,
127 .d_kqfilter = ttykqfilter,
128 .d_revoke = ttyrevoke
131 static void ucom_cleanup(struct ucom_softc *);
132 static int ucomctl(struct ucom_softc *, int, int);
133 static int ucomparam(struct tty *, struct termios *);
134 static void ucomstart(struct tty *);
135 static void ucomstop(struct tty *, int);
136 static void ucom_shutdown(struct ucom_softc *);
137 static void ucom_dtr(struct ucom_softc *, int);
138 static void ucom_rts(struct ucom_softc *, int);
139 static void ucom_break(struct ucom_softc *, int);
140 static usbd_status ucomstartread(struct ucom_softc *);
141 static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
142 static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
143 static void ucomstopread(struct ucom_softc *);
144 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
146 devclass_t ucom_devclass;
148 static moduledata_t ucom_mod = {
154 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
155 MODULE_DEPEND(ucom, usb, 1, 1, 1);
156 MODULE_VERSION(ucom, UCOM_MODVER);
159 ucom_attach(struct ucom_softc *sc)
165 lwkt_gettoken(&tty_token);
166 unit = device_get_unit(sc->sc_dev);
168 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
169 tp->t_oproc = ucomstart;
170 tp->t_param = ucomparam;
171 tp->t_stop = ucomstop;
173 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
175 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
177 dev = make_dev(&ucom_ops, unit | UCOM_CALLOUT_MASK,
178 UID_UUCP, GID_DIALER, 0660,
182 lwkt_reltoken(&tty_token);
188 ucom_detach(struct ucom_softc *sc)
190 struct tty *tp = sc->sc_tty;
193 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
195 destroy_dev(sc->dev);
196 lwkt_gettoken(&tty_token);
199 if (sc->sc_bulkin_pipe != NULL)
200 usbd_abort_pipe(sc->sc_bulkin_pipe);
201 if (sc->sc_bulkout_pipe != NULL)
202 usbd_abort_pipe(sc->sc_bulkout_pipe);
205 if (tp->t_state & TS_ISOPEN) {
206 device_printf(sc->sc_dev,
207 "still open, forcing close\n");
208 (*linesw[tp->t_line].l_close)(tp, 0);
212 DPRINTF(("ucom_detach: no tty\n"));
213 lwkt_reltoken(&tty_token);
218 if (--sc->sc_refcnt >= 0) {
219 /* Wait for processes to go away. */
220 usb_detach_wait(sc->sc_dev);
224 unit = device_get_unit(sc->sc_dev);
225 dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit);
227 lwkt_reltoken(&tty_token);
232 ucom_shutdown(struct ucom_softc *sc)
234 struct tty *tp = sc->sc_tty;
236 lwkt_gettoken(&tty_token);
237 DPRINTF(("ucom_shutdown\n"));
239 * Hang up if necessary. Wait a bit, so the other side has time to
240 * notice even if we immediately open the port again.
242 if (ISSET(tp->t_cflag, HUPCL)) {
243 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
244 (void)tsleep(sc, 0, "ucomsd", hz);
246 lwkt_reltoken(&tty_token);
250 ucomopen(struct dev_open_args *ap)
252 cdev_t dev = ap->a_head.a_dev;
253 int unit = UCOMUNIT(dev);
254 struct ucom_softc *sc;
259 sc = devclass_get_softc(ucom_devclass, unit);
266 lwkt_gettoken(&tty_token);
269 DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
271 if (ISSET(tp->t_state, TS_ISOPEN) &&
272 ISSET(tp->t_state, TS_XCLUDE) &&
273 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
275 lwkt_reltoken(&tty_token);
280 * Do the following iff this is a first open.
283 while (sc->sc_opening)
284 tsleep(&sc->sc_opening, 0, "ucomop", 0);
287 if (!ISSET(tp->t_state, TS_ISOPEN)) {
291 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
293 tp->t_dev = reference_dev(dev);
296 * Initialize the termios status to the defaults. Add in the
297 * sticky bits from TIOCSFLAGS.
300 t.c_ospeed = TTYDEF_SPEED;
301 t.c_cflag = TTYDEF_CFLAG;
302 /* Make sure ucomparam() will do something. */
304 (void)ucomparam(tp, &t);
305 tp->t_iflag = TTYDEF_IFLAG;
306 tp->t_oflag = TTYDEF_OFLAG;
307 tp->t_lflag = TTYDEF_LFLAG;
312 * Turn on DTR. We must always do this, even if carrier is not
313 * present, because otherwise we'd have to use TIOCSDTR
314 * immediately after setting CLOCAL, which applications do not
315 * expect. We always assert DTR while the device is open
316 * unless explicitly requested to deassert it.
318 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
320 /* Device specific open */
321 if (sc->sc_callback->ucom_open != NULL) {
322 error = sc->sc_callback->ucom_open(sc->sc_parent,
327 wakeup(&sc->sc_opening);
329 lwkt_reltoken(&tty_token);
334 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
335 sc->sc_bulkin_no, sc->sc_bulkout_no));
337 /* Open the bulk pipes */
339 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
340 &sc->sc_bulkin_pipe);
342 kprintf("%s: open bulk in error (addr %d): %s\n",
343 device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
349 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
350 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
352 kprintf("%s: open bulk out error (addr %d): %s\n",
353 device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
359 /* Allocate a request and an input buffer and start reading. */
360 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
361 if (sc->sc_ixfer == NULL) {
366 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
368 if (sc->sc_ibuf == NULL) {
373 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
374 if (sc->sc_oxfer == NULL) {
379 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
382 if (sc->sc_obuf == NULL) {
388 * Handle initial DCD.
390 if (ISSET(sc->sc_msr, UMSR_DCD) ||
391 (minor(dev) & UCOM_CALLOUT_MASK))
392 (*linesw[tp->t_line].l_modem)(tp, 1);
398 wakeup(&sc->sc_opening);
401 error = ttyopen(dev, tp);
405 error = (*linesw[tp->t_line].l_open)(dev, tp);
409 disc_optim(tp, &tp->t_termios, sc);
411 DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
416 lwkt_reltoken(&tty_token);
420 usbd_free_xfer(sc->sc_oxfer);
423 usbd_free_xfer(sc->sc_ixfer);
426 usbd_close_pipe(sc->sc_bulkout_pipe);
427 sc->sc_bulkout_pipe = NULL;
429 usbd_close_pipe(sc->sc_bulkin_pipe);
430 sc->sc_bulkin_pipe = NULL;
433 wakeup(&sc->sc_opening);
435 lwkt_reltoken(&tty_token);
439 if (!ISSET(tp->t_state, TS_ISOPEN)) {
441 * We failed to open the device, and nobody else had it opened.
442 * Clean up the state as appropriate.
447 DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
449 lwkt_reltoken(&tty_token);
454 ucomclose(struct dev_close_args *ap)
456 cdev_t dev = ap->a_head.a_dev;
457 struct ucom_softc *sc;
460 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
462 lwkt_gettoken(&tty_token);
465 DPRINTF(("%s: ucomclose: unit = %d\n",
466 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
468 if (!ISSET(tp->t_state, TS_ISOPEN))
472 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
473 disc_optim(tp, &tp->t_termios, sc);
480 if (!ISSET(tp->t_state, TS_ISOPEN)) {
482 * Although we got a last close, the device may still be in
483 * use; e.g. if this was the dialout node, and there are still
484 * processes waiting for carrier on the non-dialout node.
489 if (sc->sc_callback->ucom_close != NULL)
490 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
494 release_dev(tp->t_dev);
498 if (--sc->sc_refcnt < 0)
499 usb_detach_wakeup(sc->sc_dev);
501 lwkt_reltoken(&tty_token);
506 ucomread(struct dev_read_args *ap)
508 cdev_t dev = ap->a_head.a_dev;
509 struct ucom_softc *sc;
513 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
514 lwkt_gettoken(&tty_token);
517 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
520 lwkt_reltoken(&tty_token);
524 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
526 DPRINTF(("ucomread: error = %d\n", error));
528 lwkt_reltoken(&tty_token);
533 ucomwrite(struct dev_write_args *ap)
535 cdev_t dev = ap->a_head.a_dev;
536 struct ucom_softc *sc;
540 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
541 lwkt_gettoken(&tty_token);
544 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
547 lwkt_reltoken(&tty_token);
551 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
553 DPRINTF(("ucomwrite: error = %d\n", error));
555 lwkt_reltoken(&tty_token);
560 ucomioctl(struct dev_ioctl_args *ap)
562 cdev_t dev = ap->a_head.a_dev;
563 struct ucom_softc *sc;
568 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
569 lwkt_gettoken(&tty_token);
573 lwkt_reltoken(&tty_token);
577 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
579 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
580 ap->a_fflag, ap->a_cred);
581 if (error != ENOIOCTL) {
582 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
583 lwkt_reltoken(&tty_token);
589 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
590 disc_optim(tp, &tp->t_termios, sc);
591 if (error != ENOIOCTL) {
593 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
594 lwkt_reltoken(&tty_token);
598 if (sc->sc_callback->ucom_ioctl != NULL) {
599 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
601 ap->a_cmd, ap->a_data,
602 ap->a_fflag, curthread);
605 lwkt_reltoken(&tty_token);
612 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
616 DPRINTF(("ucomioctl: TIOCSBRK\n"));
620 DPRINTF(("ucomioctl: TIOCCBRK\n"));
625 DPRINTF(("ucomioctl: TIOCSDTR\n"));
626 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
629 DPRINTF(("ucomioctl: TIOCCDTR\n"));
630 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
634 d = *(int *)ap->a_data;
635 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
636 (void)ucomctl(sc, d, DMSET);
639 d = *(int *)ap->a_data;
640 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
641 (void)ucomctl(sc, d, DMBIS);
644 d = *(int *)ap->a_data;
645 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
646 (void)ucomctl(sc, d, DMBIC);
649 d = ucomctl(sc, 0, DMGET);
650 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
651 *(int *)ap->a_data = d;
655 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
662 lwkt_reltoken(&tty_token);
667 * NOTE: Must be called with tty_token held.
670 ucomctl(struct ucom_softc *sc, int bits, int how)
676 ASSERT_LWKT_TOKEN_HELD(&tty_token);
677 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
680 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
681 DPRINTF(("ucomctl: DMGET: LE"));
684 if (ISSET(mcr, UMCR_DTR)) {
685 SET(bits, TIOCM_DTR);
688 if (ISSET(mcr, UMCR_RTS)) {
689 SET(bits, TIOCM_RTS);
694 if (ISSET(msr, UMSR_CTS)) {
695 SET(bits, TIOCM_CTS);
698 if (ISSET(msr, UMSR_DCD)) {
702 if (ISSET(msr, UMSR_DSR)) {
703 SET(bits, TIOCM_DSR);
706 if (ISSET(msr, UMSR_RI)) {
717 if (ISSET(bits, TIOCM_DTR))
719 if (ISSET(bits, TIOCM_RTS))
734 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
737 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
744 * NOTE: Must be called with tty_token held.
747 ucom_break(struct ucom_softc *sc, int onoff)
749 ASSERT_LWKT_TOKEN_HELD(&tty_token);
750 DPRINTF(("ucom_break: onoff = %d\n", onoff));
752 if (sc->sc_callback->ucom_set == NULL)
754 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
755 UCOM_SET_BREAK, onoff);
759 * NOTE: Must be called with tty_token held.
762 ucom_dtr(struct ucom_softc *sc, int onoff)
764 ASSERT_LWKT_TOKEN_HELD(&tty_token);
765 DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
767 if (sc->sc_callback->ucom_set == NULL)
769 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
770 UCOM_SET_DTR, onoff);
774 * NOTE: Must be called with tty_token held.
777 ucom_rts(struct ucom_softc *sc, int onoff)
779 ASSERT_LWKT_TOKEN_HELD(&tty_token);
780 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
782 if (sc->sc_callback->ucom_set == NULL)
784 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
785 UCOM_SET_RTS, onoff);
789 ucom_status_change(struct ucom_softc *sc)
791 struct tty *tp = sc->sc_tty;
795 lwkt_gettoken(&tty_token);
796 if (sc->sc_callback->ucom_get_status == NULL) {
799 lwkt_reltoken(&tty_token);
803 old_msr = sc->sc_msr;
804 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
805 &sc->sc_lsr, &sc->sc_msr);
806 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
807 if (sc->sc_poll == 0) {
808 lwkt_reltoken(&tty_token);
811 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
812 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
813 (*linesw[tp->t_line].l_modem)(tp, onoff);
815 lwkt_reltoken(&tty_token);
819 ucomparam(struct tty *tp, struct termios *t)
821 struct ucom_softc *sc;
825 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
827 lwkt_gettoken(&tty_token);
829 lwkt_reltoken(&tty_token);
833 DPRINTF(("ucomparam: sc = %p\n", sc));
835 /* Check requested parameters. */
836 if (t->c_ospeed < 0) {
837 DPRINTF(("ucomparam: negative ospeed\n"));
838 lwkt_reltoken(&tty_token);
841 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
842 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
843 lwkt_reltoken(&tty_token);
848 * If there were no changes, don't do anything. This avoids dropping
849 * input and improves performance when all we did was frob things like
852 if (tp->t_ospeed == t->c_ospeed &&
853 tp->t_cflag == t->c_cflag) {
854 lwkt_reltoken(&tty_token);
858 /* And copy to tty. */
860 tp->t_ospeed = t->c_ospeed;
861 tp->t_cflag = t->c_cflag;
863 if (sc->sc_callback->ucom_param == NULL) {
864 lwkt_reltoken(&tty_token);
870 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
872 DPRINTF(("ucomparam: callback: error = %d\n", error));
873 lwkt_reltoken(&tty_token);
879 if (t->c_cflag & CRTS_IFLOW) {
880 sc->sc_state |= UCS_RTS_IFLOW;
881 } else if (sc->sc_state & UCS_RTS_IFLOW) {
882 sc->sc_state &= ~UCS_RTS_IFLOW;
883 (void)ucomctl(sc, UMCR_RTS, DMBIS);
886 disc_optim(tp, t, sc);
888 uerr = ucomstartread(sc);
889 if (uerr != USBD_NORMAL_COMPLETION) {
890 lwkt_reltoken(&tty_token);
894 lwkt_reltoken(&tty_token);
899 ucomstart(struct tty *tp)
901 struct ucom_softc *sc;
907 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
908 DPRINTF(("ucomstart: sc = %p\n", sc));
910 lwkt_gettoken(&tty_token);
913 lwkt_reltoken(&tty_token);
919 if (tp->t_state & TS_TBLOCK) {
920 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
921 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
922 DPRINTF(("ucomstart: clear RTS\n"));
923 (void)ucomctl(sc, UMCR_RTS, DMBIC);
926 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
927 tp->t_rawq.c_cc <= tp->t_ilowat &&
928 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
929 DPRINTF(("ucomstart: set RTS\n"));
930 (void)ucomctl(sc, UMCR_RTS, DMBIS);
934 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
936 DPRINTF(("ucomstart: stopped\n"));
940 if (tp->t_outq.c_cc <= tp->t_olowat) {
941 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
942 CLR(tp->t_state, TS_SO_OLOWAT);
943 wakeup(TSA_OLOWAT(tp));
945 KNOTE(&tp->t_wkq.ki_note, 0);
946 if (tp->t_outq.c_cc == 0) {
947 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
948 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
949 CLR(tp->t_state, TS_SO_OCOMPLETE);
950 wakeup(TSA_OCOMPLETE(tp));
956 /* Grab the first contiguous region of buffer space. */
957 data = tp->t_outq.c_cf;
958 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
959 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
962 DPRINTF(("ucomstart: cnt == 0\n"));
966 SET(tp->t_state, TS_BUSY);
968 if (cnt > sc->sc_obufsize) {
969 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
970 cnt = sc->sc_obufsize;
972 if (sc->sc_callback->ucom_write != NULL)
973 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
974 sc->sc_obuf, data, &cnt);
976 memcpy(sc->sc_obuf, data, cnt);
978 DPRINTF(("ucomstart: %d chars\n", cnt));
979 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
980 (usbd_private_handle)sc, sc->sc_obuf, cnt,
981 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
982 /* What can we do on error? */
983 err = usbd_transfer(sc->sc_oxfer);
984 if (err != USBD_IN_PROGRESS)
985 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
991 lwkt_reltoken(&tty_token);
995 ucomstop(struct tty *tp, int flag)
997 struct ucom_softc *sc;
999 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
1001 DPRINTF(("ucomstop: %d\n", flag));
1003 lwkt_gettoken(&tty_token);
1006 * This is just supposed to flush pending receive data,
1007 * not stop the reception of data entirely!
1009 DPRINTF(("ucomstop: read\n"));
1014 if (flag & FWRITE) {
1015 DPRINTF(("ucomstop: write\n"));
1017 if (ISSET(tp->t_state, TS_BUSY)) {
1019 if (!ISSET(tp->t_state, TS_TTSTOP))
1020 SET(tp->t_state, TS_FLUSH);
1025 DPRINTF(("ucomstop: done\n"));
1026 lwkt_reltoken(&tty_token);
1030 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1032 struct ucom_softc *sc = (struct ucom_softc *)p;
1033 struct tty *tp = sc->sc_tty;
1036 lwkt_gettoken(&tty_token);
1037 DPRINTF(("ucomwritecb: status = %d\n", status));
1039 if (status == USBD_CANCELLED || sc->sc_dying)
1042 if (status != USBD_NORMAL_COMPLETION) {
1043 kprintf("%s: ucomwritecb: %s\n",
1044 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1045 if (status == USBD_STALLED)
1046 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1048 * XXX. We may need a flag to sequence ucomstopread() and
1049 * ucomstartread() to handle the case where ucomstartread()
1050 * is called after ucomstopread() but before the request has
1051 * been properly canceled?
1056 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1057 DPRINTF(("ucomwritecb: cc = %d\n", cc));
1058 if (cc <= sc->sc_opkthdrlen) {
1059 kprintf("%s: sent size too small, cc = %d\n",
1060 device_get_nameunit(sc->sc_dev), cc);
1064 /* convert from USB bytes to tty bytes */
1065 cc -= sc->sc_opkthdrlen;
1068 CLR(tp->t_state, TS_BUSY);
1069 if (ISSET(tp->t_state, TS_FLUSH))
1070 CLR(tp->t_state, TS_FLUSH);
1072 ndflush(&tp->t_outq, cc);
1073 (*linesw[tp->t_line].l_start)(tp);
1076 lwkt_reltoken(&tty_token);
1081 CLR(tp->t_state, TS_BUSY);
1083 lwkt_reltoken(&tty_token);
1088 * NOTE: Must be called with tty_token held
1091 ucomstartread(struct ucom_softc *sc)
1095 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1096 DPRINTF(("ucomstartread: start\n"));
1098 sc->sc_state &= ~UCS_RXSTOP;
1100 if (sc->sc_bulkin_pipe == NULL)
1101 return (USBD_NORMAL_COMPLETION);
1103 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1104 (usbd_private_handle)sc,
1105 sc->sc_ibuf, sc->sc_ibufsize,
1106 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1107 USBD_NO_TIMEOUT, ucomreadcb);
1109 err = usbd_transfer(sc->sc_ixfer);
1110 if (err != USBD_IN_PROGRESS) {
1111 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1115 return (USBD_NORMAL_COMPLETION);
1119 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1121 lwkt_gettoken(&tty_token);
1123 struct ucom_softc *sc = (struct ucom_softc *)p;
1124 struct tty *tp = sc->sc_tty;
1125 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1131 DPRINTF(("ucomreadcb: status = %d\n", status));
1133 if (status != USBD_NORMAL_COMPLETION) {
1134 if (!(sc->sc_state & UCS_RXSTOP))
1135 kprintf("%s: ucomreadcb: %s\n",
1136 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1137 if (status == USBD_STALLED)
1138 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1139 /* XXX we should restart after some delay. */
1140 lwkt_reltoken(&tty_token);
1144 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1145 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1149 if (sc->sc_callback->ucom_read != NULL) {
1150 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1154 if (cc > sc->sc_ibufsize) {
1155 kprintf("%s: invalid receive data size, %d chars\n",
1156 device_get_nameunit(sc->sc_dev), cc);
1163 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1164 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1165 && (sc->sc_state & UCS_RTS_IFLOW
1166 || tp->t_iflag & IXOFF)
1167 && !(tp->t_state & TS_TBLOCK))
1169 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1173 if (*cp == sc->hotchar)
1182 if (tp->t_state & TS_TTSTOP
1183 && (tp->t_iflag & IXANY
1184 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1185 tp->t_state &= ~TS_TTSTOP;
1186 tp->t_lflag &= ~FLUSHO;
1190 kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1193 /* Give characters to tty layer. */
1195 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1196 if ((*rint)(*cp, tp) == -1) {
1197 /* XXX what should we do? */
1198 kprintf("%s: lost %d chars\n",
1199 device_get_nameunit(sc->sc_dev), cc);
1209 err = ucomstartread(sc);
1211 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1212 /* XXX what should we dow now? */
1215 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1216 && !(tp->t_state & TS_TBLOCK))
1217 ucomctl(sc, UMCR_RTS, DMBIS);
1219 lwkt_reltoken(&tty_token);
1223 * NOTE: Must be called with tty_token held.
1226 ucom_cleanup(struct ucom_softc *sc)
1228 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1229 DPRINTF(("ucom_cleanup: closing pipes\n"));
1232 if (sc->sc_bulkin_pipe != NULL) {
1233 usbd_abort_pipe(sc->sc_bulkin_pipe);
1234 usbd_close_pipe(sc->sc_bulkin_pipe);
1235 sc->sc_bulkin_pipe = NULL;
1237 if (sc->sc_bulkout_pipe != NULL) {
1238 usbd_abort_pipe(sc->sc_bulkout_pipe);
1239 usbd_close_pipe(sc->sc_bulkout_pipe);
1240 sc->sc_bulkout_pipe = NULL;
1242 if (sc->sc_ixfer != NULL) {
1243 usbd_free_xfer(sc->sc_ixfer);
1244 sc->sc_ixfer = NULL;
1246 if (sc->sc_oxfer != NULL) {
1247 usbd_free_xfer(sc->sc_oxfer);
1248 sc->sc_oxfer = NULL;
1253 * NOTE: Must be called with tty_token held.
1256 ucomstopread(struct ucom_softc *sc)
1260 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1261 DPRINTF(("ucomstopread: enter\n"));
1263 if (!(sc->sc_state & UCS_RXSTOP)) {
1264 sc->sc_state |= UCS_RXSTOP;
1265 if (sc->sc_bulkin_pipe == NULL) {
1266 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1269 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1271 DPRINTF(("ucomstopread: err = %s\n",
1276 DPRINTF(("ucomstopread: leave\n"));
1280 * NOTE: Must be called with tty_token held.
1283 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1285 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1286 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1287 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1288 && (!(t->c_iflag & PARMRK)
1289 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1290 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1291 && linesw[tp->t_line].l_rint == ttyinput) {
1292 DPRINTF(("disc_optim: bypass l_rint\n"));
1293 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1295 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1296 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1298 sc->hotchar = linesw[tp->t_line].l_hotchar;