2 * $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $
3 * $NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $
4 * $FreeBSD: src/sys/dev/usb/ucom.c,v 1.35 2003/11/16 11:58:21 akiyama Exp $
5 * $DragonFly: src/sys/dev/usbmisc/ucom/ucom.c,v 1.32 2007/11/05 19:09:43 hasso Exp $
8 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
35 * All rights reserved.
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Lennart Augustsson (lennart@augustsson.net) at
39 * Carlstedt Research & Technology.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the NetBSD
52 * Foundation, Inc. and its contributors.
53 * 4. Neither the name of The NetBSD Foundation nor the names of its
54 * contributors may be used to endorse or promote products derived
55 * from this software without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
58 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
61 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 * POSSIBILITY OF SUCH DAMAGE.
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/malloc.h>
75 #include <sys/fcntl.h>
78 #include <sys/clist.h>
80 #include <sys/select.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 | D_KQFILTER },
126 .d_close = ucomclose,
128 .d_write = ucomwrite,
129 .d_ioctl = ucomioctl,
131 .d_kqfilter = ttykqfilter
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 unit = device_get_unit(sc->sc_dev);
170 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
171 tp->t_oproc = ucomstart;
172 tp->t_param = ucomparam;
173 tp->t_stop = ucomstop;
175 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
177 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
179 dev_ops_add(&ucom_ops, UCOMUNIT_MASK, unit);
180 dev = make_dev(&ucom_ops, unit | UCOM_CALLOUT_MASK,
181 UID_UUCP, GID_DIALER, 0660,
189 ucom_detach(struct ucom_softc *sc)
191 struct tty *tp = sc->sc_tty;
194 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
198 if (sc->sc_bulkin_pipe != NULL)
199 usbd_abort_pipe(sc->sc_bulkin_pipe);
200 if (sc->sc_bulkout_pipe != NULL)
201 usbd_abort_pipe(sc->sc_bulkout_pipe);
204 if (tp->t_state & TS_ISOPEN) {
205 device_printf(sc->sc_dev,
206 "still open, forcing close\n");
207 (*linesw[tp->t_line].l_close)(tp, 0);
211 DPRINTF(("ucom_detach: no tty\n"));
216 if (--sc->sc_refcnt >= 0) {
217 /* Wait for processes to go away. */
218 usb_detach_wait(sc->sc_dev);
222 unit = device_get_unit(sc->sc_dev);
223 dev_ops_remove(&ucom_ops, UCOMUNIT_MASK, unit);
229 ucom_shutdown(struct ucom_softc *sc)
231 struct tty *tp = sc->sc_tty;
233 DPRINTF(("ucom_shutdown\n"));
235 * Hang up if necessary. Wait a bit, so the other side has time to
236 * notice even if we immediately open the port again.
238 if (ISSET(tp->t_cflag, HUPCL)) {
239 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
240 (void)tsleep(sc, 0, "ucomsd", hz);
245 ucomopen(struct dev_open_args *ap)
247 cdev_t dev = ap->a_head.a_dev;
248 int unit = UCOMUNIT(dev);
249 struct ucom_softc *sc;
254 sc = devclass_get_softc(ucom_devclass, unit);
263 DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
265 if (ISSET(tp->t_state, TS_ISOPEN) &&
266 ISSET(tp->t_state, TS_XCLUDE) &&
267 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
273 * Do the following iff this is a first open.
276 while (sc->sc_opening)
277 tsleep(&sc->sc_opening, 0, "ucomop", 0);
280 if (!ISSET(tp->t_state, TS_ISOPEN)) {
284 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
286 tp->t_dev = reference_dev(dev);
289 * Initialize the termios status to the defaults. Add in the
290 * sticky bits from TIOCSFLAGS.
293 t.c_ospeed = TTYDEF_SPEED;
294 t.c_cflag = TTYDEF_CFLAG;
295 /* Make sure ucomparam() will do something. */
297 (void)ucomparam(tp, &t);
298 tp->t_iflag = TTYDEF_IFLAG;
299 tp->t_oflag = TTYDEF_OFLAG;
300 tp->t_lflag = TTYDEF_LFLAG;
305 * Turn on DTR. We must always do this, even if carrier is not
306 * present, because otherwise we'd have to use TIOCSDTR
307 * immediately after setting CLOCAL, which applications do not
308 * expect. We always assert DTR while the device is open
309 * unless explicitly requested to deassert it.
311 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
313 /* Device specific open */
314 if (sc->sc_callback->ucom_open != NULL) {
315 error = sc->sc_callback->ucom_open(sc->sc_parent,
320 wakeup(&sc->sc_opening);
326 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
327 sc->sc_bulkin_no, sc->sc_bulkout_no));
329 /* Open the bulk pipes */
331 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
332 &sc->sc_bulkin_pipe);
334 kprintf("%s: open bulk in error (addr %d): %s\n",
335 device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
341 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
342 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
344 kprintf("%s: open bulk out error (addr %d): %s\n",
345 device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
351 /* Allocate a request and an input buffer and start reading. */
352 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
353 if (sc->sc_ixfer == NULL) {
358 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
360 if (sc->sc_ibuf == NULL) {
365 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
366 if (sc->sc_oxfer == NULL) {
371 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
374 if (sc->sc_obuf == NULL) {
380 * Handle initial DCD.
382 if (ISSET(sc->sc_msr, UMSR_DCD) ||
383 (minor(dev) & UCOM_CALLOUT_MASK))
384 (*linesw[tp->t_line].l_modem)(tp, 1);
390 wakeup(&sc->sc_opening);
393 error = ttyopen(dev, tp);
397 error = (*linesw[tp->t_line].l_open)(dev, tp);
401 disc_optim(tp, &tp->t_termios, sc);
403 DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
411 usbd_free_xfer(sc->sc_oxfer);
414 usbd_free_xfer(sc->sc_ixfer);
417 usbd_close_pipe(sc->sc_bulkout_pipe);
418 sc->sc_bulkout_pipe = NULL;
420 usbd_close_pipe(sc->sc_bulkin_pipe);
421 sc->sc_bulkin_pipe = NULL;
424 wakeup(&sc->sc_opening);
429 if (!ISSET(tp->t_state, TS_ISOPEN)) {
431 * We failed to open the device, and nobody else had it opened.
432 * Clean up the state as appropriate.
437 DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
443 ucomclose(struct dev_close_args *ap)
445 cdev_t dev = ap->a_head.a_dev;
446 struct ucom_softc *sc;
449 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
453 DPRINTF(("%s: ucomclose: unit = %d\n",
454 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
456 if (!ISSET(tp->t_state, TS_ISOPEN))
460 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
461 disc_optim(tp, &tp->t_termios, sc);
468 if (!ISSET(tp->t_state, TS_ISOPEN)) {
470 * Although we got a last close, the device may still be in
471 * use; e.g. if this was the dialout node, and there are still
472 * processes waiting for carrier on the non-dialout node.
477 if (sc->sc_callback->ucom_close != NULL)
478 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
482 release_dev(tp->t_dev);
486 if (--sc->sc_refcnt < 0)
487 usb_detach_wakeup(sc->sc_dev);
493 ucomread(struct dev_read_args *ap)
495 cdev_t dev = ap->a_head.a_dev;
496 struct ucom_softc *sc;
500 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
503 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
508 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
510 DPRINTF(("ucomread: error = %d\n", error));
516 ucomwrite(struct dev_write_args *ap)
518 cdev_t dev = ap->a_head.a_dev;
519 struct ucom_softc *sc;
523 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
526 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
531 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
533 DPRINTF(("ucomwrite: error = %d\n", error));
539 ucomioctl(struct dev_ioctl_args *ap)
541 cdev_t dev = ap->a_head.a_dev;
542 struct ucom_softc *sc;
547 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
553 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
555 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
556 ap->a_fflag, ap->a_cred);
557 if (error != ENOIOCTL) {
558 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
564 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
565 disc_optim(tp, &tp->t_termios, sc);
566 if (error != ENOIOCTL) {
568 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
572 if (sc->sc_callback->ucom_ioctl != NULL) {
573 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
575 ap->a_cmd, ap->a_data,
576 ap->a_fflag, curthread);
585 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
589 DPRINTF(("ucomioctl: TIOCSBRK\n"));
593 DPRINTF(("ucomioctl: TIOCCBRK\n"));
598 DPRINTF(("ucomioctl: TIOCSDTR\n"));
599 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
602 DPRINTF(("ucomioctl: TIOCCDTR\n"));
603 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
607 d = *(int *)ap->a_data;
608 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
609 (void)ucomctl(sc, d, DMSET);
612 d = *(int *)ap->a_data;
613 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
614 (void)ucomctl(sc, d, DMBIS);
617 d = *(int *)ap->a_data;
618 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
619 (void)ucomctl(sc, d, DMBIC);
622 d = ucomctl(sc, 0, DMGET);
623 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
624 *(int *)ap->a_data = d;
628 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
639 ucomctl(struct ucom_softc *sc, int bits, int how)
645 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
648 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
649 DPRINTF(("ucomctl: DMGET: LE"));
652 if (ISSET(mcr, UMCR_DTR)) {
653 SET(bits, TIOCM_DTR);
656 if (ISSET(mcr, UMCR_RTS)) {
657 SET(bits, TIOCM_RTS);
662 if (ISSET(msr, UMSR_CTS)) {
663 SET(bits, TIOCM_CTS);
666 if (ISSET(msr, UMSR_DCD)) {
670 if (ISSET(msr, UMSR_DSR)) {
671 SET(bits, TIOCM_DSR);
674 if (ISSET(msr, UMSR_RI)) {
685 if (ISSET(bits, TIOCM_DTR))
687 if (ISSET(bits, TIOCM_RTS))
702 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
705 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
712 ucom_break(struct ucom_softc *sc, int onoff)
714 DPRINTF(("ucom_break: onoff = %d\n", onoff));
716 if (sc->sc_callback->ucom_set == NULL)
718 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
719 UCOM_SET_BREAK, onoff);
723 ucom_dtr(struct ucom_softc *sc, int onoff)
725 DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
727 if (sc->sc_callback->ucom_set == NULL)
729 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
730 UCOM_SET_DTR, onoff);
734 ucom_rts(struct ucom_softc *sc, int onoff)
736 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
738 if (sc->sc_callback->ucom_set == NULL)
740 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
741 UCOM_SET_RTS, onoff);
745 ucom_status_change(struct ucom_softc *sc)
747 struct tty *tp = sc->sc_tty;
751 if (sc->sc_callback->ucom_get_status == NULL) {
757 old_msr = sc->sc_msr;
758 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
759 &sc->sc_lsr, &sc->sc_msr);
760 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
761 if (sc->sc_poll == 0)
763 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
764 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
765 (*linesw[tp->t_line].l_modem)(tp, onoff);
770 ucomparam(struct tty *tp, struct termios *t)
772 struct ucom_softc *sc;
776 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
781 DPRINTF(("ucomparam: sc = %p\n", sc));
783 /* Check requested parameters. */
784 if (t->c_ospeed < 0) {
785 DPRINTF(("ucomparam: negative ospeed\n"));
788 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
789 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
794 * If there were no changes, don't do anything. This avoids dropping
795 * input and improves performance when all we did was frob things like
798 if (tp->t_ospeed == t->c_ospeed &&
799 tp->t_cflag == t->c_cflag)
802 /* And copy to tty. */
804 tp->t_ospeed = t->c_ospeed;
805 tp->t_cflag = t->c_cflag;
807 if (sc->sc_callback->ucom_param == NULL)
812 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
814 DPRINTF(("ucomparam: callback: error = %d\n", error));
820 if (t->c_cflag & CRTS_IFLOW) {
821 sc->sc_state |= UCS_RTS_IFLOW;
822 } else if (sc->sc_state & UCS_RTS_IFLOW) {
823 sc->sc_state &= ~UCS_RTS_IFLOW;
824 (void)ucomctl(sc, UMCR_RTS, DMBIS);
827 disc_optim(tp, t, sc);
829 uerr = ucomstartread(sc);
830 if (uerr != USBD_NORMAL_COMPLETION)
837 ucomstart(struct tty *tp)
839 struct ucom_softc *sc;
845 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
846 DPRINTF(("ucomstart: sc = %p\n", sc));
853 if (tp->t_state & TS_TBLOCK) {
854 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
855 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
856 DPRINTF(("ucomstart: clear RTS\n"));
857 (void)ucomctl(sc, UMCR_RTS, DMBIC);
860 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
861 tp->t_rawq.c_cc <= tp->t_ilowat &&
862 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
863 DPRINTF(("ucomstart: set RTS\n"));
864 (void)ucomctl(sc, UMCR_RTS, DMBIS);
868 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
870 DPRINTF(("ucomstart: stopped\n"));
874 if (tp->t_outq.c_cc <= tp->t_olowat) {
875 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
876 CLR(tp->t_state, TS_SO_OLOWAT);
877 wakeup(TSA_OLOWAT(tp));
879 selwakeup(&tp->t_wsel);
880 if (tp->t_outq.c_cc == 0) {
881 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
882 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
883 CLR(tp->t_state, TS_SO_OCOMPLETE);
884 wakeup(TSA_OCOMPLETE(tp));
890 /* Grab the first contiguous region of buffer space. */
891 data = tp->t_outq.c_cf;
892 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
893 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
896 DPRINTF(("ucomstart: cnt == 0\n"));
900 SET(tp->t_state, TS_BUSY);
902 if (cnt > sc->sc_obufsize) {
903 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
904 cnt = sc->sc_obufsize;
906 if (sc->sc_callback->ucom_write != NULL)
907 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
908 sc->sc_obuf, data, &cnt);
910 memcpy(sc->sc_obuf, data, cnt);
912 DPRINTF(("ucomstart: %d chars\n", cnt));
913 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
914 (usbd_private_handle)sc, sc->sc_obuf, cnt,
915 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
916 /* What can we do on error? */
917 err = usbd_transfer(sc->sc_oxfer);
918 if (err != USBD_IN_PROGRESS)
919 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
928 ucomstop(struct tty *tp, int flag)
930 struct ucom_softc *sc;
932 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
934 DPRINTF(("ucomstop: %d\n", flag));
938 * This is just supposed to flush pending receive data,
939 * not stop the reception of data entirely!
941 DPRINTF(("ucomstop: read\n"));
947 DPRINTF(("ucomstop: write\n"));
949 if (ISSET(tp->t_state, TS_BUSY)) {
951 if (!ISSET(tp->t_state, TS_TTSTOP))
952 SET(tp->t_state, TS_FLUSH);
957 DPRINTF(("ucomstop: done\n"));
961 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
963 struct ucom_softc *sc = (struct ucom_softc *)p;
964 struct tty *tp = sc->sc_tty;
967 DPRINTF(("ucomwritecb: status = %d\n", status));
969 if (status == USBD_CANCELLED || sc->sc_dying)
972 if (status != USBD_NORMAL_COMPLETION) {
973 kprintf("%s: ucomwritecb: %s\n",
974 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
975 if (status == USBD_STALLED)
976 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
978 * XXX. We may need a flag to sequence ucomstopread() and
979 * ucomstartread() to handle the case where ucomstartread()
980 * is called after ucomstopread() but before the request has
981 * been properly canceled?
986 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
987 DPRINTF(("ucomwritecb: cc = %d\n", cc));
988 if (cc <= sc->sc_opkthdrlen) {
989 kprintf("%s: sent size too small, cc = %d\n",
990 device_get_nameunit(sc->sc_dev), cc);
994 /* convert from USB bytes to tty bytes */
995 cc -= sc->sc_opkthdrlen;
998 CLR(tp->t_state, TS_BUSY);
999 if (ISSET(tp->t_state, TS_FLUSH))
1000 CLR(tp->t_state, TS_FLUSH);
1002 ndflush(&tp->t_outq, cc);
1003 (*linesw[tp->t_line].l_start)(tp);
1010 CLR(tp->t_state, TS_BUSY);
1016 ucomstartread(struct ucom_softc *sc)
1020 DPRINTF(("ucomstartread: start\n"));
1022 sc->sc_state &= ~UCS_RXSTOP;
1024 if (sc->sc_bulkin_pipe == NULL)
1025 return (USBD_NORMAL_COMPLETION);
1027 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1028 (usbd_private_handle)sc,
1029 sc->sc_ibuf, sc->sc_ibufsize,
1030 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1031 USBD_NO_TIMEOUT, ucomreadcb);
1033 err = usbd_transfer(sc->sc_ixfer);
1034 if (err != USBD_IN_PROGRESS) {
1035 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1039 return (USBD_NORMAL_COMPLETION);
1043 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1045 struct ucom_softc *sc = (struct ucom_softc *)p;
1046 struct tty *tp = sc->sc_tty;
1047 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1053 DPRINTF(("ucomreadcb: status = %d\n", status));
1055 if (status != USBD_NORMAL_COMPLETION) {
1056 if (!(sc->sc_state & UCS_RXSTOP))
1057 kprintf("%s: ucomreadcb: %s\n",
1058 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1059 if (status == USBD_STALLED)
1060 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1061 /* XXX we should restart after some delay. */
1065 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1066 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1070 if (sc->sc_callback->ucom_read != NULL) {
1071 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1075 if (cc > sc->sc_ibufsize) {
1076 kprintf("%s: invalid receive data size, %d chars\n",
1077 device_get_nameunit(sc->sc_dev), cc);
1084 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1085 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1086 && (sc->sc_state & UCS_RTS_IFLOW
1087 || tp->t_iflag & IXOFF)
1088 && !(tp->t_state & TS_TBLOCK))
1090 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1094 if (*cp == sc->hotchar)
1103 if (tp->t_state & TS_TTSTOP
1104 && (tp->t_iflag & IXANY
1105 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1106 tp->t_state &= ~TS_TTSTOP;
1107 tp->t_lflag &= ~FLUSHO;
1111 kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1114 /* Give characters to tty layer. */
1116 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1117 if ((*rint)(*cp, tp) == -1) {
1118 /* XXX what should we do? */
1119 kprintf("%s: lost %d chars\n",
1120 device_get_nameunit(sc->sc_dev), cc);
1130 err = ucomstartread(sc);
1132 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1133 /* XXX what should we dow now? */
1136 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1137 && !(tp->t_state & TS_TBLOCK))
1138 ucomctl(sc, UMCR_RTS, DMBIS);
1142 ucom_cleanup(struct ucom_softc *sc)
1144 DPRINTF(("ucom_cleanup: closing pipes\n"));
1147 if (sc->sc_bulkin_pipe != NULL) {
1148 usbd_abort_pipe(sc->sc_bulkin_pipe);
1149 usbd_close_pipe(sc->sc_bulkin_pipe);
1150 sc->sc_bulkin_pipe = NULL;
1152 if (sc->sc_bulkout_pipe != NULL) {
1153 usbd_abort_pipe(sc->sc_bulkout_pipe);
1154 usbd_close_pipe(sc->sc_bulkout_pipe);
1155 sc->sc_bulkout_pipe = NULL;
1157 if (sc->sc_ixfer != NULL) {
1158 usbd_free_xfer(sc->sc_ixfer);
1159 sc->sc_ixfer = NULL;
1161 if (sc->sc_oxfer != NULL) {
1162 usbd_free_xfer(sc->sc_oxfer);
1163 sc->sc_oxfer = NULL;
1168 ucomstopread(struct ucom_softc *sc)
1172 DPRINTF(("ucomstopread: enter\n"));
1174 if (!(sc->sc_state & UCS_RXSTOP)) {
1175 sc->sc_state |= UCS_RXSTOP;
1176 if (sc->sc_bulkin_pipe == NULL) {
1177 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1180 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1182 DPRINTF(("ucomstopread: err = %s\n",
1187 DPRINTF(("ucomstopread: leave\n"));
1191 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1193 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1194 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1195 && (!(t->c_iflag & PARMRK)
1196 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1197 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1198 && linesw[tp->t_line].l_rint == ttyinput) {
1199 DPRINTF(("disc_optim: bypass l_rint\n"));
1200 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1202 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1203 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1205 sc->hotchar = linesw[tp->t_line].l_hotchar;