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,
184 lwkt_reltoken(&tty_token);
190 ucom_detach(struct ucom_softc *sc)
192 struct tty *tp = sc->sc_tty;
195 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
197 lwkt_gettoken(&tty_token);
200 if (sc->sc_bulkin_pipe != NULL)
201 usbd_abort_pipe(sc->sc_bulkin_pipe);
202 if (sc->sc_bulkout_pipe != NULL)
203 usbd_abort_pipe(sc->sc_bulkout_pipe);
206 if (tp->t_state & TS_ISOPEN) {
207 device_printf(sc->sc_dev,
208 "still open, forcing close\n");
209 (*linesw[tp->t_line].l_close)(tp, 0);
213 DPRINTF(("ucom_detach: no tty\n"));
214 lwkt_reltoken(&tty_token);
219 if (--sc->sc_refcnt >= 0) {
220 /* Wait for processes to go away. */
221 usb_detach_wait(sc->sc_dev);
225 unit = device_get_unit(sc->sc_dev);
226 dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit);
228 lwkt_reltoken(&tty_token);
233 ucom_shutdown(struct ucom_softc *sc)
235 struct tty *tp = sc->sc_tty;
237 lwkt_gettoken(&tty_token);
238 DPRINTF(("ucom_shutdown\n"));
240 * Hang up if necessary. Wait a bit, so the other side has time to
241 * notice even if we immediately open the port again.
243 if (ISSET(tp->t_cflag, HUPCL)) {
244 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
245 (void)tsleep(sc, 0, "ucomsd", hz);
247 lwkt_reltoken(&tty_token);
251 ucomopen(struct dev_open_args *ap)
253 cdev_t dev = ap->a_head.a_dev;
254 int unit = UCOMUNIT(dev);
255 struct ucom_softc *sc;
260 sc = devclass_get_softc(ucom_devclass, unit);
267 lwkt_gettoken(&tty_token);
270 DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
272 if (ISSET(tp->t_state, TS_ISOPEN) &&
273 ISSET(tp->t_state, TS_XCLUDE) &&
274 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
276 lwkt_reltoken(&tty_token);
281 * Do the following iff this is a first open.
284 while (sc->sc_opening)
285 tsleep(&sc->sc_opening, 0, "ucomop", 0);
288 if (!ISSET(tp->t_state, TS_ISOPEN)) {
292 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
294 tp->t_dev = reference_dev(dev);
297 * Initialize the termios status to the defaults. Add in the
298 * sticky bits from TIOCSFLAGS.
301 t.c_ospeed = TTYDEF_SPEED;
302 t.c_cflag = TTYDEF_CFLAG;
303 /* Make sure ucomparam() will do something. */
305 (void)ucomparam(tp, &t);
306 tp->t_iflag = TTYDEF_IFLAG;
307 tp->t_oflag = TTYDEF_OFLAG;
308 tp->t_lflag = TTYDEF_LFLAG;
313 * Turn on DTR. We must always do this, even if carrier is not
314 * present, because otherwise we'd have to use TIOCSDTR
315 * immediately after setting CLOCAL, which applications do not
316 * expect. We always assert DTR while the device is open
317 * unless explicitly requested to deassert it.
319 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
321 /* Device specific open */
322 if (sc->sc_callback->ucom_open != NULL) {
323 error = sc->sc_callback->ucom_open(sc->sc_parent,
328 wakeup(&sc->sc_opening);
330 lwkt_reltoken(&tty_token);
335 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
336 sc->sc_bulkin_no, sc->sc_bulkout_no));
338 /* Open the bulk pipes */
340 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
341 &sc->sc_bulkin_pipe);
343 kprintf("%s: open bulk in error (addr %d): %s\n",
344 device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
350 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
351 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
353 kprintf("%s: open bulk out error (addr %d): %s\n",
354 device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
360 /* Allocate a request and an input buffer and start reading. */
361 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
362 if (sc->sc_ixfer == NULL) {
367 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
369 if (sc->sc_ibuf == NULL) {
374 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
375 if (sc->sc_oxfer == NULL) {
380 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
383 if (sc->sc_obuf == NULL) {
389 * Handle initial DCD.
391 if (ISSET(sc->sc_msr, UMSR_DCD) ||
392 (minor(dev) & UCOM_CALLOUT_MASK))
393 (*linesw[tp->t_line].l_modem)(tp, 1);
399 wakeup(&sc->sc_opening);
402 error = ttyopen(dev, tp);
406 error = (*linesw[tp->t_line].l_open)(dev, tp);
410 disc_optim(tp, &tp->t_termios, sc);
412 DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
417 lwkt_reltoken(&tty_token);
421 usbd_free_xfer(sc->sc_oxfer);
424 usbd_free_xfer(sc->sc_ixfer);
427 usbd_close_pipe(sc->sc_bulkout_pipe);
428 sc->sc_bulkout_pipe = NULL;
430 usbd_close_pipe(sc->sc_bulkin_pipe);
431 sc->sc_bulkin_pipe = NULL;
434 wakeup(&sc->sc_opening);
436 lwkt_reltoken(&tty_token);
440 if (!ISSET(tp->t_state, TS_ISOPEN)) {
442 * We failed to open the device, and nobody else had it opened.
443 * Clean up the state as appropriate.
448 DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
450 lwkt_reltoken(&tty_token);
455 ucomclose(struct dev_close_args *ap)
457 cdev_t dev = ap->a_head.a_dev;
458 struct ucom_softc *sc;
461 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
463 lwkt_gettoken(&tty_token);
466 DPRINTF(("%s: ucomclose: unit = %d\n",
467 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
469 if (!ISSET(tp->t_state, TS_ISOPEN))
473 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
474 disc_optim(tp, &tp->t_termios, sc);
481 if (!ISSET(tp->t_state, TS_ISOPEN)) {
483 * Although we got a last close, the device may still be in
484 * use; e.g. if this was the dialout node, and there are still
485 * processes waiting for carrier on the non-dialout node.
490 if (sc->sc_callback->ucom_close != NULL)
491 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
495 release_dev(tp->t_dev);
499 if (--sc->sc_refcnt < 0)
500 usb_detach_wakeup(sc->sc_dev);
502 lwkt_reltoken(&tty_token);
507 ucomread(struct dev_read_args *ap)
509 cdev_t dev = ap->a_head.a_dev;
510 struct ucom_softc *sc;
514 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
515 lwkt_gettoken(&tty_token);
518 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
521 lwkt_reltoken(&tty_token);
525 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
527 DPRINTF(("ucomread: error = %d\n", error));
529 lwkt_reltoken(&tty_token);
534 ucomwrite(struct dev_write_args *ap)
536 cdev_t dev = ap->a_head.a_dev;
537 struct ucom_softc *sc;
541 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
542 lwkt_gettoken(&tty_token);
545 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
548 lwkt_reltoken(&tty_token);
552 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
554 DPRINTF(("ucomwrite: error = %d\n", error));
556 lwkt_reltoken(&tty_token);
561 ucomioctl(struct dev_ioctl_args *ap)
563 cdev_t dev = ap->a_head.a_dev;
564 struct ucom_softc *sc;
569 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
570 lwkt_gettoken(&tty_token);
574 lwkt_reltoken(&tty_token);
578 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
580 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
581 ap->a_fflag, ap->a_cred);
582 if (error != ENOIOCTL) {
583 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
584 lwkt_reltoken(&tty_token);
590 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
591 disc_optim(tp, &tp->t_termios, sc);
592 if (error != ENOIOCTL) {
594 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
595 lwkt_reltoken(&tty_token);
599 if (sc->sc_callback->ucom_ioctl != NULL) {
600 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
602 ap->a_cmd, ap->a_data,
603 ap->a_fflag, curthread);
606 lwkt_reltoken(&tty_token);
613 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
617 DPRINTF(("ucomioctl: TIOCSBRK\n"));
621 DPRINTF(("ucomioctl: TIOCCBRK\n"));
626 DPRINTF(("ucomioctl: TIOCSDTR\n"));
627 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
630 DPRINTF(("ucomioctl: TIOCCDTR\n"));
631 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
635 d = *(int *)ap->a_data;
636 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
637 (void)ucomctl(sc, d, DMSET);
640 d = *(int *)ap->a_data;
641 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
642 (void)ucomctl(sc, d, DMBIS);
645 d = *(int *)ap->a_data;
646 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
647 (void)ucomctl(sc, d, DMBIC);
650 d = ucomctl(sc, 0, DMGET);
651 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
652 *(int *)ap->a_data = d;
656 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
663 lwkt_reltoken(&tty_token);
668 * NOTE: Must be called with tty_token held.
671 ucomctl(struct ucom_softc *sc, int bits, int how)
677 ASSERT_LWKT_TOKEN_HELD(&tty_token);
678 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
681 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
682 DPRINTF(("ucomctl: DMGET: LE"));
685 if (ISSET(mcr, UMCR_DTR)) {
686 SET(bits, TIOCM_DTR);
689 if (ISSET(mcr, UMCR_RTS)) {
690 SET(bits, TIOCM_RTS);
695 if (ISSET(msr, UMSR_CTS)) {
696 SET(bits, TIOCM_CTS);
699 if (ISSET(msr, UMSR_DCD)) {
703 if (ISSET(msr, UMSR_DSR)) {
704 SET(bits, TIOCM_DSR);
707 if (ISSET(msr, UMSR_RI)) {
718 if (ISSET(bits, TIOCM_DTR))
720 if (ISSET(bits, TIOCM_RTS))
735 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
738 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
745 * NOTE: Must be called with tty_token held.
748 ucom_break(struct ucom_softc *sc, int onoff)
750 ASSERT_LWKT_TOKEN_HELD(&tty_token);
751 DPRINTF(("ucom_break: onoff = %d\n", onoff));
753 if (sc->sc_callback->ucom_set == NULL)
755 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
756 UCOM_SET_BREAK, onoff);
760 * NOTE: Must be called with tty_token held.
763 ucom_dtr(struct ucom_softc *sc, int onoff)
765 ASSERT_LWKT_TOKEN_HELD(&tty_token);
766 DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
768 if (sc->sc_callback->ucom_set == NULL)
770 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
771 UCOM_SET_DTR, onoff);
775 * NOTE: Must be called with tty_token held.
778 ucom_rts(struct ucom_softc *sc, int onoff)
780 ASSERT_LWKT_TOKEN_HELD(&tty_token);
781 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
783 if (sc->sc_callback->ucom_set == NULL)
785 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
786 UCOM_SET_RTS, onoff);
790 ucom_status_change(struct ucom_softc *sc)
792 struct tty *tp = sc->sc_tty;
796 lwkt_gettoken(&tty_token);
797 if (sc->sc_callback->ucom_get_status == NULL) {
800 lwkt_reltoken(&tty_token);
804 old_msr = sc->sc_msr;
805 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
806 &sc->sc_lsr, &sc->sc_msr);
807 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
808 if (sc->sc_poll == 0) {
809 lwkt_reltoken(&tty_token);
812 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
813 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
814 (*linesw[tp->t_line].l_modem)(tp, onoff);
816 lwkt_reltoken(&tty_token);
820 ucomparam(struct tty *tp, struct termios *t)
822 struct ucom_softc *sc;
826 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
828 lwkt_gettoken(&tty_token);
830 lwkt_reltoken(&tty_token);
834 DPRINTF(("ucomparam: sc = %p\n", sc));
836 /* Check requested parameters. */
837 if (t->c_ospeed < 0) {
838 DPRINTF(("ucomparam: negative ospeed\n"));
839 lwkt_reltoken(&tty_token);
842 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
843 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
844 lwkt_reltoken(&tty_token);
849 * If there were no changes, don't do anything. This avoids dropping
850 * input and improves performance when all we did was frob things like
853 if (tp->t_ospeed == t->c_ospeed &&
854 tp->t_cflag == t->c_cflag) {
855 lwkt_reltoken(&tty_token);
859 /* And copy to tty. */
861 tp->t_ospeed = t->c_ospeed;
862 tp->t_cflag = t->c_cflag;
864 if (sc->sc_callback->ucom_param == NULL) {
865 lwkt_reltoken(&tty_token);
871 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
873 DPRINTF(("ucomparam: callback: error = %d\n", error));
874 lwkt_reltoken(&tty_token);
880 if (t->c_cflag & CRTS_IFLOW) {
881 sc->sc_state |= UCS_RTS_IFLOW;
882 } else if (sc->sc_state & UCS_RTS_IFLOW) {
883 sc->sc_state &= ~UCS_RTS_IFLOW;
884 (void)ucomctl(sc, UMCR_RTS, DMBIS);
887 disc_optim(tp, t, sc);
889 uerr = ucomstartread(sc);
890 if (uerr != USBD_NORMAL_COMPLETION) {
891 lwkt_reltoken(&tty_token);
895 lwkt_reltoken(&tty_token);
900 ucomstart(struct tty *tp)
902 struct ucom_softc *sc;
908 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
909 DPRINTF(("ucomstart: sc = %p\n", sc));
911 lwkt_gettoken(&tty_token);
914 lwkt_reltoken(&tty_token);
920 if (tp->t_state & TS_TBLOCK) {
921 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
922 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
923 DPRINTF(("ucomstart: clear RTS\n"));
924 (void)ucomctl(sc, UMCR_RTS, DMBIC);
927 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
928 tp->t_rawq.c_cc <= tp->t_ilowat &&
929 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
930 DPRINTF(("ucomstart: set RTS\n"));
931 (void)ucomctl(sc, UMCR_RTS, DMBIS);
935 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
937 DPRINTF(("ucomstart: stopped\n"));
941 if (tp->t_outq.c_cc <= tp->t_olowat) {
942 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
943 CLR(tp->t_state, TS_SO_OLOWAT);
944 wakeup(TSA_OLOWAT(tp));
946 KNOTE(&tp->t_wkq.ki_note, 0);
947 if (tp->t_outq.c_cc == 0) {
948 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
949 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
950 CLR(tp->t_state, TS_SO_OCOMPLETE);
951 wakeup(TSA_OCOMPLETE(tp));
957 /* Grab the first contiguous region of buffer space. */
958 data = tp->t_outq.c_cf;
959 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
960 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
963 DPRINTF(("ucomstart: cnt == 0\n"));
967 SET(tp->t_state, TS_BUSY);
969 if (cnt > sc->sc_obufsize) {
970 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
971 cnt = sc->sc_obufsize;
973 if (sc->sc_callback->ucom_write != NULL)
974 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
975 sc->sc_obuf, data, &cnt);
977 memcpy(sc->sc_obuf, data, cnt);
979 DPRINTF(("ucomstart: %d chars\n", cnt));
980 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
981 (usbd_private_handle)sc, sc->sc_obuf, cnt,
982 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
983 /* What can we do on error? */
984 err = usbd_transfer(sc->sc_oxfer);
985 if (err != USBD_IN_PROGRESS)
986 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
992 lwkt_reltoken(&tty_token);
996 ucomstop(struct tty *tp, int flag)
998 struct ucom_softc *sc;
1000 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
1002 DPRINTF(("ucomstop: %d\n", flag));
1004 lwkt_gettoken(&tty_token);
1007 * This is just supposed to flush pending receive data,
1008 * not stop the reception of data entirely!
1010 DPRINTF(("ucomstop: read\n"));
1015 if (flag & FWRITE) {
1016 DPRINTF(("ucomstop: write\n"));
1018 if (ISSET(tp->t_state, TS_BUSY)) {
1020 if (!ISSET(tp->t_state, TS_TTSTOP))
1021 SET(tp->t_state, TS_FLUSH);
1026 DPRINTF(("ucomstop: done\n"));
1027 lwkt_reltoken(&tty_token);
1031 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1033 struct ucom_softc *sc = (struct ucom_softc *)p;
1034 struct tty *tp = sc->sc_tty;
1037 lwkt_gettoken(&tty_token);
1038 DPRINTF(("ucomwritecb: status = %d\n", status));
1040 if (status == USBD_CANCELLED || sc->sc_dying)
1043 if (status != USBD_NORMAL_COMPLETION) {
1044 kprintf("%s: ucomwritecb: %s\n",
1045 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1046 if (status == USBD_STALLED)
1047 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1049 * XXX. We may need a flag to sequence ucomstopread() and
1050 * ucomstartread() to handle the case where ucomstartread()
1051 * is called after ucomstopread() but before the request has
1052 * been properly canceled?
1057 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1058 DPRINTF(("ucomwritecb: cc = %d\n", cc));
1059 if (cc <= sc->sc_opkthdrlen) {
1060 kprintf("%s: sent size too small, cc = %d\n",
1061 device_get_nameunit(sc->sc_dev), cc);
1065 /* convert from USB bytes to tty bytes */
1066 cc -= sc->sc_opkthdrlen;
1069 CLR(tp->t_state, TS_BUSY);
1070 if (ISSET(tp->t_state, TS_FLUSH))
1071 CLR(tp->t_state, TS_FLUSH);
1073 ndflush(&tp->t_outq, cc);
1074 (*linesw[tp->t_line].l_start)(tp);
1077 lwkt_reltoken(&tty_token);
1082 CLR(tp->t_state, TS_BUSY);
1084 lwkt_reltoken(&tty_token);
1089 * NOTE: Must be called with tty_token held
1092 ucomstartread(struct ucom_softc *sc)
1096 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1097 DPRINTF(("ucomstartread: start\n"));
1099 sc->sc_state &= ~UCS_RXSTOP;
1101 if (sc->sc_bulkin_pipe == NULL)
1102 return (USBD_NORMAL_COMPLETION);
1104 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1105 (usbd_private_handle)sc,
1106 sc->sc_ibuf, sc->sc_ibufsize,
1107 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1108 USBD_NO_TIMEOUT, ucomreadcb);
1110 err = usbd_transfer(sc->sc_ixfer);
1111 if (err != USBD_IN_PROGRESS) {
1112 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1116 return (USBD_NORMAL_COMPLETION);
1120 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1122 lwkt_gettoken(&tty_token);
1124 struct ucom_softc *sc = (struct ucom_softc *)p;
1125 struct tty *tp = sc->sc_tty;
1126 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1132 DPRINTF(("ucomreadcb: status = %d\n", status));
1134 if (status != USBD_NORMAL_COMPLETION) {
1135 if (!(sc->sc_state & UCS_RXSTOP))
1136 kprintf("%s: ucomreadcb: %s\n",
1137 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1138 if (status == USBD_STALLED)
1139 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1140 /* XXX we should restart after some delay. */
1141 lwkt_reltoken(&tty_token);
1145 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1146 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1150 if (sc->sc_callback->ucom_read != NULL) {
1151 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1155 if (cc > sc->sc_ibufsize) {
1156 kprintf("%s: invalid receive data size, %d chars\n",
1157 device_get_nameunit(sc->sc_dev), cc);
1164 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1165 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1166 && (sc->sc_state & UCS_RTS_IFLOW
1167 || tp->t_iflag & IXOFF)
1168 && !(tp->t_state & TS_TBLOCK))
1170 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1174 if (*cp == sc->hotchar)
1183 if (tp->t_state & TS_TTSTOP
1184 && (tp->t_iflag & IXANY
1185 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1186 tp->t_state &= ~TS_TTSTOP;
1187 tp->t_lflag &= ~FLUSHO;
1191 kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1194 /* Give characters to tty layer. */
1196 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1197 if ((*rint)(*cp, tp) == -1) {
1198 /* XXX what should we do? */
1199 kprintf("%s: lost %d chars\n",
1200 device_get_nameunit(sc->sc_dev), cc);
1210 err = ucomstartread(sc);
1212 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1213 /* XXX what should we dow now? */
1216 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1217 && !(tp->t_state & TS_TBLOCK))
1218 ucomctl(sc, UMCR_RTS, DMBIS);
1220 lwkt_reltoken(&tty_token);
1224 * NOTE: Must be called with tty_token held.
1227 ucom_cleanup(struct ucom_softc *sc)
1229 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1230 DPRINTF(("ucom_cleanup: closing pipes\n"));
1233 if (sc->sc_bulkin_pipe != NULL) {
1234 usbd_abort_pipe(sc->sc_bulkin_pipe);
1235 usbd_close_pipe(sc->sc_bulkin_pipe);
1236 sc->sc_bulkin_pipe = NULL;
1238 if (sc->sc_bulkout_pipe != NULL) {
1239 usbd_abort_pipe(sc->sc_bulkout_pipe);
1240 usbd_close_pipe(sc->sc_bulkout_pipe);
1241 sc->sc_bulkout_pipe = NULL;
1243 if (sc->sc_ixfer != NULL) {
1244 usbd_free_xfer(sc->sc_ixfer);
1245 sc->sc_ixfer = NULL;
1247 if (sc->sc_oxfer != NULL) {
1248 usbd_free_xfer(sc->sc_oxfer);
1249 sc->sc_oxfer = NULL;
1254 * NOTE: Must be called with tty_token held.
1257 ucomstopread(struct ucom_softc *sc)
1261 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1262 DPRINTF(("ucomstopread: enter\n"));
1264 if (!(sc->sc_state & UCS_RXSTOP)) {
1265 sc->sc_state |= UCS_RXSTOP;
1266 if (sc->sc_bulkin_pipe == NULL) {
1267 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1270 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1272 DPRINTF(("ucomstopread: err = %s\n",
1277 DPRINTF(("ucomstopread: leave\n"));
1281 * NOTE: Must be called with tty_token held.
1284 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1286 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1287 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1288 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1289 && (!(t->c_iflag & PARMRK)
1290 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1291 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1292 && linesw[tp->t_line].l_rint == ttyinput) {
1293 DPRINTF(("disc_optim: bypass l_rint\n"));
1294 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1296 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1297 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1299 sc->hotchar = linesw[tp->t_line].l_hotchar;