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>
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 #define UCOM_CDEV_MAJOR 138
122 static struct dev_ops ucom_ops = {
123 { "ucom", UCOM_CDEV_MAJOR, D_TTY | D_KQFILTER },
125 .d_close = ucomclose,
127 .d_write = ucomwrite,
128 .d_ioctl = ucomioctl,
129 .d_kqfilter = ttykqfilter,
130 .d_revoke = ttyrevoke
133 static void ucom_cleanup(struct ucom_softc *);
134 static int ucomctl(struct ucom_softc *, int, int);
135 static int ucomparam(struct tty *, struct termios *);
136 static void ucomstart(struct tty *);
137 static void ucomstop(struct tty *, int);
138 static void ucom_shutdown(struct ucom_softc *);
139 static void ucom_dtr(struct ucom_softc *, int);
140 static void ucom_rts(struct ucom_softc *, int);
141 static void ucom_break(struct ucom_softc *, int);
142 static usbd_status ucomstartread(struct ucom_softc *);
143 static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
144 static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
145 static void ucomstopread(struct ucom_softc *);
146 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
148 devclass_t ucom_devclass;
150 static moduledata_t ucom_mod = {
156 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
157 MODULE_DEPEND(ucom, usb, 1, 1, 1);
158 MODULE_VERSION(ucom, UCOM_MODVER);
161 ucom_attach(struct ucom_softc *sc)
167 unit = device_get_unit(sc->sc_dev);
169 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
170 tp->t_oproc = ucomstart;
171 tp->t_param = ucomparam;
172 tp->t_stop = ucomstop;
174 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
176 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
178 dev = make_dev(&ucom_ops, unit | UCOM_CALLOUT_MASK,
179 UID_UUCP, GID_DIALER, 0660,
187 ucom_detach(struct ucom_softc *sc)
189 struct tty *tp = sc->sc_tty;
192 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
196 if (sc->sc_bulkin_pipe != NULL)
197 usbd_abort_pipe(sc->sc_bulkin_pipe);
198 if (sc->sc_bulkout_pipe != NULL)
199 usbd_abort_pipe(sc->sc_bulkout_pipe);
202 if (tp->t_state & TS_ISOPEN) {
203 device_printf(sc->sc_dev,
204 "still open, forcing close\n");
205 (*linesw[tp->t_line].l_close)(tp, 0);
209 DPRINTF(("ucom_detach: no tty\n"));
214 if (--sc->sc_refcnt >= 0) {
215 /* Wait for processes to go away. */
216 usb_detach_wait(sc->sc_dev);
220 unit = device_get_unit(sc->sc_dev);
221 dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit);
227 ucom_shutdown(struct ucom_softc *sc)
229 struct tty *tp = sc->sc_tty;
231 DPRINTF(("ucom_shutdown\n"));
233 * Hang up if necessary. Wait a bit, so the other side has time to
234 * notice even if we immediately open the port again.
236 if (ISSET(tp->t_cflag, HUPCL)) {
237 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
238 (void)tsleep(sc, 0, "ucomsd", hz);
243 ucomopen(struct dev_open_args *ap)
245 cdev_t dev = ap->a_head.a_dev;
246 int unit = UCOMUNIT(dev);
247 struct ucom_softc *sc;
252 sc = devclass_get_softc(ucom_devclass, unit);
261 DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
263 if (ISSET(tp->t_state, TS_ISOPEN) &&
264 ISSET(tp->t_state, TS_XCLUDE) &&
265 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
271 * Do the following iff this is a first open.
274 while (sc->sc_opening)
275 tsleep(&sc->sc_opening, 0, "ucomop", 0);
278 if (!ISSET(tp->t_state, TS_ISOPEN)) {
282 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
284 tp->t_dev = reference_dev(dev);
287 * Initialize the termios status to the defaults. Add in the
288 * sticky bits from TIOCSFLAGS.
291 t.c_ospeed = TTYDEF_SPEED;
292 t.c_cflag = TTYDEF_CFLAG;
293 /* Make sure ucomparam() will do something. */
295 (void)ucomparam(tp, &t);
296 tp->t_iflag = TTYDEF_IFLAG;
297 tp->t_oflag = TTYDEF_OFLAG;
298 tp->t_lflag = TTYDEF_LFLAG;
303 * Turn on DTR. We must always do this, even if carrier is not
304 * present, because otherwise we'd have to use TIOCSDTR
305 * immediately after setting CLOCAL, which applications do not
306 * expect. We always assert DTR while the device is open
307 * unless explicitly requested to deassert it.
309 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
311 /* Device specific open */
312 if (sc->sc_callback->ucom_open != NULL) {
313 error = sc->sc_callback->ucom_open(sc->sc_parent,
318 wakeup(&sc->sc_opening);
324 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
325 sc->sc_bulkin_no, sc->sc_bulkout_no));
327 /* Open the bulk pipes */
329 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
330 &sc->sc_bulkin_pipe);
332 kprintf("%s: open bulk in error (addr %d): %s\n",
333 device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
339 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
340 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
342 kprintf("%s: open bulk out error (addr %d): %s\n",
343 device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
349 /* Allocate a request and an input buffer and start reading. */
350 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
351 if (sc->sc_ixfer == NULL) {
356 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
358 if (sc->sc_ibuf == NULL) {
363 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
364 if (sc->sc_oxfer == NULL) {
369 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
372 if (sc->sc_obuf == NULL) {
378 * Handle initial DCD.
380 if (ISSET(sc->sc_msr, UMSR_DCD) ||
381 (minor(dev) & UCOM_CALLOUT_MASK))
382 (*linesw[tp->t_line].l_modem)(tp, 1);
388 wakeup(&sc->sc_opening);
391 error = ttyopen(dev, tp);
395 error = (*linesw[tp->t_line].l_open)(dev, tp);
399 disc_optim(tp, &tp->t_termios, sc);
401 DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
409 usbd_free_xfer(sc->sc_oxfer);
412 usbd_free_xfer(sc->sc_ixfer);
415 usbd_close_pipe(sc->sc_bulkout_pipe);
416 sc->sc_bulkout_pipe = NULL;
418 usbd_close_pipe(sc->sc_bulkin_pipe);
419 sc->sc_bulkin_pipe = NULL;
422 wakeup(&sc->sc_opening);
427 if (!ISSET(tp->t_state, TS_ISOPEN)) {
429 * We failed to open the device, and nobody else had it opened.
430 * Clean up the state as appropriate.
435 DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
441 ucomclose(struct dev_close_args *ap)
443 cdev_t dev = ap->a_head.a_dev;
444 struct ucom_softc *sc;
447 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
451 DPRINTF(("%s: ucomclose: unit = %d\n",
452 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
454 if (!ISSET(tp->t_state, TS_ISOPEN))
458 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
459 disc_optim(tp, &tp->t_termios, sc);
466 if (!ISSET(tp->t_state, TS_ISOPEN)) {
468 * Although we got a last close, the device may still be in
469 * use; e.g. if this was the dialout node, and there are still
470 * processes waiting for carrier on the non-dialout node.
475 if (sc->sc_callback->ucom_close != NULL)
476 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
480 release_dev(tp->t_dev);
484 if (--sc->sc_refcnt < 0)
485 usb_detach_wakeup(sc->sc_dev);
491 ucomread(struct dev_read_args *ap)
493 cdev_t dev = ap->a_head.a_dev;
494 struct ucom_softc *sc;
498 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
501 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
506 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
508 DPRINTF(("ucomread: error = %d\n", error));
514 ucomwrite(struct dev_write_args *ap)
516 cdev_t dev = ap->a_head.a_dev;
517 struct ucom_softc *sc;
521 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
524 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
529 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
531 DPRINTF(("ucomwrite: error = %d\n", error));
537 ucomioctl(struct dev_ioctl_args *ap)
539 cdev_t dev = ap->a_head.a_dev;
540 struct ucom_softc *sc;
545 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
551 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
553 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
554 ap->a_fflag, ap->a_cred);
555 if (error != ENOIOCTL) {
556 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
562 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
563 disc_optim(tp, &tp->t_termios, sc);
564 if (error != ENOIOCTL) {
566 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
570 if (sc->sc_callback->ucom_ioctl != NULL) {
571 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
573 ap->a_cmd, ap->a_data,
574 ap->a_fflag, curthread);
583 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
587 DPRINTF(("ucomioctl: TIOCSBRK\n"));
591 DPRINTF(("ucomioctl: TIOCCBRK\n"));
596 DPRINTF(("ucomioctl: TIOCSDTR\n"));
597 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
600 DPRINTF(("ucomioctl: TIOCCDTR\n"));
601 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
605 d = *(int *)ap->a_data;
606 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
607 (void)ucomctl(sc, d, DMSET);
610 d = *(int *)ap->a_data;
611 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
612 (void)ucomctl(sc, d, DMBIS);
615 d = *(int *)ap->a_data;
616 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
617 (void)ucomctl(sc, d, DMBIC);
620 d = ucomctl(sc, 0, DMGET);
621 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
622 *(int *)ap->a_data = d;
626 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
637 ucomctl(struct ucom_softc *sc, int bits, int how)
643 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
646 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
647 DPRINTF(("ucomctl: DMGET: LE"));
650 if (ISSET(mcr, UMCR_DTR)) {
651 SET(bits, TIOCM_DTR);
654 if (ISSET(mcr, UMCR_RTS)) {
655 SET(bits, TIOCM_RTS);
660 if (ISSET(msr, UMSR_CTS)) {
661 SET(bits, TIOCM_CTS);
664 if (ISSET(msr, UMSR_DCD)) {
668 if (ISSET(msr, UMSR_DSR)) {
669 SET(bits, TIOCM_DSR);
672 if (ISSET(msr, UMSR_RI)) {
683 if (ISSET(bits, TIOCM_DTR))
685 if (ISSET(bits, TIOCM_RTS))
700 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
703 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
710 ucom_break(struct ucom_softc *sc, int onoff)
712 DPRINTF(("ucom_break: onoff = %d\n", onoff));
714 if (sc->sc_callback->ucom_set == NULL)
716 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
717 UCOM_SET_BREAK, onoff);
721 ucom_dtr(struct ucom_softc *sc, int onoff)
723 DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
725 if (sc->sc_callback->ucom_set == NULL)
727 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
728 UCOM_SET_DTR, onoff);
732 ucom_rts(struct ucom_softc *sc, int onoff)
734 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
736 if (sc->sc_callback->ucom_set == NULL)
738 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
739 UCOM_SET_RTS, onoff);
743 ucom_status_change(struct ucom_softc *sc)
745 struct tty *tp = sc->sc_tty;
749 if (sc->sc_callback->ucom_get_status == NULL) {
755 old_msr = sc->sc_msr;
756 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
757 &sc->sc_lsr, &sc->sc_msr);
758 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
759 if (sc->sc_poll == 0)
761 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
762 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
763 (*linesw[tp->t_line].l_modem)(tp, onoff);
768 ucomparam(struct tty *tp, struct termios *t)
770 struct ucom_softc *sc;
774 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
779 DPRINTF(("ucomparam: sc = %p\n", sc));
781 /* Check requested parameters. */
782 if (t->c_ospeed < 0) {
783 DPRINTF(("ucomparam: negative ospeed\n"));
786 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
787 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
792 * If there were no changes, don't do anything. This avoids dropping
793 * input and improves performance when all we did was frob things like
796 if (tp->t_ospeed == t->c_ospeed &&
797 tp->t_cflag == t->c_cflag)
800 /* And copy to tty. */
802 tp->t_ospeed = t->c_ospeed;
803 tp->t_cflag = t->c_cflag;
805 if (sc->sc_callback->ucom_param == NULL)
810 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
812 DPRINTF(("ucomparam: callback: error = %d\n", error));
818 if (t->c_cflag & CRTS_IFLOW) {
819 sc->sc_state |= UCS_RTS_IFLOW;
820 } else if (sc->sc_state & UCS_RTS_IFLOW) {
821 sc->sc_state &= ~UCS_RTS_IFLOW;
822 (void)ucomctl(sc, UMCR_RTS, DMBIS);
825 disc_optim(tp, t, sc);
827 uerr = ucomstartread(sc);
828 if (uerr != USBD_NORMAL_COMPLETION)
835 ucomstart(struct tty *tp)
837 struct ucom_softc *sc;
843 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
844 DPRINTF(("ucomstart: sc = %p\n", sc));
851 if (tp->t_state & TS_TBLOCK) {
852 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
853 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
854 DPRINTF(("ucomstart: clear RTS\n"));
855 (void)ucomctl(sc, UMCR_RTS, DMBIC);
858 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
859 tp->t_rawq.c_cc <= tp->t_ilowat &&
860 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
861 DPRINTF(("ucomstart: set RTS\n"));
862 (void)ucomctl(sc, UMCR_RTS, DMBIS);
866 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
868 DPRINTF(("ucomstart: stopped\n"));
872 if (tp->t_outq.c_cc <= tp->t_olowat) {
873 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
874 CLR(tp->t_state, TS_SO_OLOWAT);
875 wakeup(TSA_OLOWAT(tp));
877 KNOTE(&tp->t_wsel.si_note, 0);
878 if (tp->t_outq.c_cc == 0) {
879 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
880 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
881 CLR(tp->t_state, TS_SO_OCOMPLETE);
882 wakeup(TSA_OCOMPLETE(tp));
888 /* Grab the first contiguous region of buffer space. */
889 data = tp->t_outq.c_cf;
890 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
891 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
894 DPRINTF(("ucomstart: cnt == 0\n"));
898 SET(tp->t_state, TS_BUSY);
900 if (cnt > sc->sc_obufsize) {
901 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
902 cnt = sc->sc_obufsize;
904 if (sc->sc_callback->ucom_write != NULL)
905 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
906 sc->sc_obuf, data, &cnt);
908 memcpy(sc->sc_obuf, data, cnt);
910 DPRINTF(("ucomstart: %d chars\n", cnt));
911 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
912 (usbd_private_handle)sc, sc->sc_obuf, cnt,
913 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
914 /* What can we do on error? */
915 err = usbd_transfer(sc->sc_oxfer);
916 if (err != USBD_IN_PROGRESS)
917 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
926 ucomstop(struct tty *tp, int flag)
928 struct ucom_softc *sc;
930 sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
932 DPRINTF(("ucomstop: %d\n", flag));
936 * This is just supposed to flush pending receive data,
937 * not stop the reception of data entirely!
939 DPRINTF(("ucomstop: read\n"));
945 DPRINTF(("ucomstop: write\n"));
947 if (ISSET(tp->t_state, TS_BUSY)) {
949 if (!ISSET(tp->t_state, TS_TTSTOP))
950 SET(tp->t_state, TS_FLUSH);
955 DPRINTF(("ucomstop: done\n"));
959 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
961 struct ucom_softc *sc = (struct ucom_softc *)p;
962 struct tty *tp = sc->sc_tty;
965 DPRINTF(("ucomwritecb: status = %d\n", status));
967 if (status == USBD_CANCELLED || sc->sc_dying)
970 if (status != USBD_NORMAL_COMPLETION) {
971 kprintf("%s: ucomwritecb: %s\n",
972 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
973 if (status == USBD_STALLED)
974 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
976 * XXX. We may need a flag to sequence ucomstopread() and
977 * ucomstartread() to handle the case where ucomstartread()
978 * is called after ucomstopread() but before the request has
979 * been properly canceled?
984 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
985 DPRINTF(("ucomwritecb: cc = %d\n", cc));
986 if (cc <= sc->sc_opkthdrlen) {
987 kprintf("%s: sent size too small, cc = %d\n",
988 device_get_nameunit(sc->sc_dev), cc);
992 /* convert from USB bytes to tty bytes */
993 cc -= sc->sc_opkthdrlen;
996 CLR(tp->t_state, TS_BUSY);
997 if (ISSET(tp->t_state, TS_FLUSH))
998 CLR(tp->t_state, TS_FLUSH);
1000 ndflush(&tp->t_outq, cc);
1001 (*linesw[tp->t_line].l_start)(tp);
1008 CLR(tp->t_state, TS_BUSY);
1014 ucomstartread(struct ucom_softc *sc)
1018 DPRINTF(("ucomstartread: start\n"));
1020 sc->sc_state &= ~UCS_RXSTOP;
1022 if (sc->sc_bulkin_pipe == NULL)
1023 return (USBD_NORMAL_COMPLETION);
1025 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1026 (usbd_private_handle)sc,
1027 sc->sc_ibuf, sc->sc_ibufsize,
1028 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1029 USBD_NO_TIMEOUT, ucomreadcb);
1031 err = usbd_transfer(sc->sc_ixfer);
1032 if (err != USBD_IN_PROGRESS) {
1033 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1037 return (USBD_NORMAL_COMPLETION);
1041 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1043 struct ucom_softc *sc = (struct ucom_softc *)p;
1044 struct tty *tp = sc->sc_tty;
1045 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1051 DPRINTF(("ucomreadcb: status = %d\n", status));
1053 if (status != USBD_NORMAL_COMPLETION) {
1054 if (!(sc->sc_state & UCS_RXSTOP))
1055 kprintf("%s: ucomreadcb: %s\n",
1056 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1057 if (status == USBD_STALLED)
1058 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1059 /* XXX we should restart after some delay. */
1063 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1064 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1068 if (sc->sc_callback->ucom_read != NULL) {
1069 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1073 if (cc > sc->sc_ibufsize) {
1074 kprintf("%s: invalid receive data size, %d chars\n",
1075 device_get_nameunit(sc->sc_dev), cc);
1082 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1083 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1084 && (sc->sc_state & UCS_RTS_IFLOW
1085 || tp->t_iflag & IXOFF)
1086 && !(tp->t_state & TS_TBLOCK))
1088 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1092 if (*cp == sc->hotchar)
1101 if (tp->t_state & TS_TTSTOP
1102 && (tp->t_iflag & IXANY
1103 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1104 tp->t_state &= ~TS_TTSTOP;
1105 tp->t_lflag &= ~FLUSHO;
1109 kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1112 /* Give characters to tty layer. */
1114 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1115 if ((*rint)(*cp, tp) == -1) {
1116 /* XXX what should we do? */
1117 kprintf("%s: lost %d chars\n",
1118 device_get_nameunit(sc->sc_dev), cc);
1128 err = ucomstartread(sc);
1130 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1131 /* XXX what should we dow now? */
1134 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1135 && !(tp->t_state & TS_TBLOCK))
1136 ucomctl(sc, UMCR_RTS, DMBIS);
1140 ucom_cleanup(struct ucom_softc *sc)
1142 DPRINTF(("ucom_cleanup: closing pipes\n"));
1145 if (sc->sc_bulkin_pipe != NULL) {
1146 usbd_abort_pipe(sc->sc_bulkin_pipe);
1147 usbd_close_pipe(sc->sc_bulkin_pipe);
1148 sc->sc_bulkin_pipe = NULL;
1150 if (sc->sc_bulkout_pipe != NULL) {
1151 usbd_abort_pipe(sc->sc_bulkout_pipe);
1152 usbd_close_pipe(sc->sc_bulkout_pipe);
1153 sc->sc_bulkout_pipe = NULL;
1155 if (sc->sc_ixfer != NULL) {
1156 usbd_free_xfer(sc->sc_ixfer);
1157 sc->sc_ixfer = NULL;
1159 if (sc->sc_oxfer != NULL) {
1160 usbd_free_xfer(sc->sc_oxfer);
1161 sc->sc_oxfer = NULL;
1166 ucomstopread(struct ucom_softc *sc)
1170 DPRINTF(("ucomstopread: enter\n"));
1172 if (!(sc->sc_state & UCS_RXSTOP)) {
1173 sc->sc_state |= UCS_RXSTOP;
1174 if (sc->sc_bulkin_pipe == NULL) {
1175 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1178 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1180 DPRINTF(("ucomstopread: err = %s\n",
1185 DPRINTF(("ucomstopread: leave\n"));
1189 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1191 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1192 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1193 && (!(t->c_iflag & PARMRK)
1194 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1195 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1196 && linesw[tp->t_line].l_rint == ttyinput) {
1197 DPRINTF(("disc_optim: bypass l_rint\n"));
1198 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1200 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1201 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1203 sc->hotchar = linesw[tp->t_line].l_hotchar;