2 * $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $
3 * $FreeBSD: src/sys/dev/usb/ucom.c,v 1.24.2.5 2003/11/30 12:48:52 akiyama Exp $
4 * $DragonFly: src/sys/dev/usbmisc/ucom/ucom.c,v 1.11 2003/12/29 18:07:38 dillon 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.
72 * 1. How do I handle hotchar?
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
80 #include <sys/ioccom.h>
81 #include <sys/fcntl.h>
84 #include <sys/clist.h>
86 #if __FreeBSD_version >= 500014
87 #include <sys/selinfo.h>
89 #include <sys/select.h>
92 #include <sys/vnode.h>
94 #include <sys/sysctl.h>
96 #include <bus/usb/usb.h>
97 #include <bus/usb/usbcdc.h>
99 #include <bus/usb/usbdi.h>
100 #include <bus/usb/usbdi_util.h>
101 #include <bus/usb/usbdevs.h>
102 #include <bus/usb/usb_quirks.h>
107 static int ucomdebug = 0;
108 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
109 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
110 &ucomdebug, 0, "ucom debug level");
111 #define DPRINTF(x) do { \
116 #define DPRINTFN(n, x) do { \
117 if (ucomdebug > (n)) \
122 #define DPRINTFN(n, x)
125 Static d_open_t ucomopen;
126 Static d_close_t ucomclose;
127 Static d_read_t ucomread;
128 Static d_write_t ucomwrite;
129 Static d_ioctl_t ucomioctl;
131 #define UCOM_CDEV_MAJOR 138
133 static struct cdevsw ucom_cdevsw = {
135 /* maj */ UCOM_CDEV_MAJOR,
136 /* flags */ D_TTY | D_KQFILTER,
141 /* close */ ucomclose,
143 /* write */ ucomwrite,
144 /* ioctl */ ucomioctl,
147 /* strategy */ nostrategy,
150 /* kqfilter */ ttykqfilter
153 Static void ucom_cleanup(struct ucom_softc *);
154 Static int ucomctl(struct ucom_softc *, int, int);
155 Static int ucomparam(struct tty *, struct termios *);
156 Static void ucomstart(struct tty *);
157 Static void ucomstop(struct tty *, int);
158 Static void ucom_shutdown(struct ucom_softc *);
159 Static void ucom_dtr(struct ucom_softc *, int);
160 Static void ucom_rts(struct ucom_softc *, int);
161 Static void ucom_break(struct ucom_softc *, int);
162 Static usbd_status ucomstartread(struct ucom_softc *);
163 Static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
164 Static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
165 Static void ucomstopread(struct ucom_softc *);
166 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
168 devclass_t ucom_devclass;
170 static moduledata_t ucom_mod = {
176 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
177 MODULE_DEPEND(ucom, usb, 1, 1, 1);
178 MODULE_VERSION(ucom, UCOM_MODVER);
181 ucom_attach(struct ucom_softc *sc)
186 unit = device_get_unit(sc->sc_dev);
188 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
189 tp->t_oproc = ucomstart;
190 tp->t_param = ucomparam;
191 tp->t_stop = ucomstop;
193 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
195 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
197 sc->dev = make_dev(&ucom_cdevsw, unit | UCOM_CALLOUT_MASK,
198 UID_UUCP, GID_DIALER, 0660,
200 sc->dev->si_tty = tp;
206 ucom_detach(struct ucom_softc *sc)
208 struct tty *tp = sc->sc_tty;
211 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
215 if (sc->sc_bulkin_pipe != NULL)
216 usbd_abort_pipe(sc->sc_bulkin_pipe);
217 if (sc->sc_bulkout_pipe != NULL)
218 usbd_abort_pipe(sc->sc_bulkout_pipe);
221 if (tp->t_state & TS_ISOPEN) {
222 device_printf(sc->sc_dev,
223 "still open, forcing close\n");
224 (*linesw[tp->t_line].l_close)(tp, 0);
231 DPRINTF(("ucom_detach: no tty\n"));
236 if (--sc->sc_refcnt >= 0) {
237 /* Wait for processes to go away. */
238 usb_detach_wait(USBDEV(sc->sc_dev));
242 destroy_dev(sc->dev);
248 ucom_shutdown(struct ucom_softc *sc)
250 struct tty *tp = sc->sc_tty;
252 DPRINTF(("ucom_shutdown\n"));
254 * Hang up if necessary. Wait a bit, so the other side has time to
255 * notice even if we immediately open the port again.
257 if (ISSET(tp->t_cflag, HUPCL)) {
258 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
259 (void)tsleep(sc, 0, "ucomsd", hz);
264 ucomopen(dev_t dev, int flag, int mode, usb_proc_ptr td)
266 int unit = UCOMUNIT(dev);
267 struct ucom_softc *sc;
273 KKASSERT(td != NULL);
275 USB_GET_SC_OPEN(ucom, unit, sc);
282 DPRINTF(("%s: ucomopen: tp = %p\n", USBDEVNAME(sc->sc_dev), tp));
284 if (ISSET(tp->t_state, TS_ISOPEN) &&
285 ISSET(tp->t_state, TS_XCLUDE) &&
292 * Do the following iff this is a first open.
295 while (sc->sc_opening)
296 tsleep(&sc->sc_opening, 0, "ucomop", 0);
299 if (!ISSET(tp->t_state, TS_ISOPEN)) {
303 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
308 * Initialize the termios status to the defaults. Add in the
309 * sticky bits from TIOCSFLAGS.
312 t.c_ospeed = TTYDEF_SPEED;
313 t.c_cflag = TTYDEF_CFLAG;
314 /* Make sure ucomparam() will do something. */
316 (void)ucomparam(tp, &t);
317 tp->t_iflag = TTYDEF_IFLAG;
318 tp->t_oflag = TTYDEF_OFLAG;
319 tp->t_lflag = TTYDEF_LFLAG;
324 * Turn on DTR. We must always do this, even if carrier is not
325 * present, because otherwise we'd have to use TIOCSDTR
326 * immediately after setting CLOCAL, which applications do not
327 * expect. We always assert DTR while the device is open
328 * unless explicitly requested to deassert it.
330 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
332 /* Device specific open */
333 if (sc->sc_callback->ucom_open != NULL) {
334 error = sc->sc_callback->ucom_open(sc->sc_parent,
339 wakeup(&sc->sc_opening);
345 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
346 sc->sc_bulkin_no, sc->sc_bulkout_no));
348 /* Open the bulk pipes */
350 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
351 &sc->sc_bulkin_pipe);
353 printf("%s: open bulk in error (addr %d): %s\n",
354 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
360 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
361 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
363 printf("%s: open bulk out error (addr %d): %s\n",
364 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
370 /* Allocate a request and an input buffer and start reading. */
371 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
372 if (sc->sc_ixfer == NULL) {
377 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
379 if (sc->sc_ibuf == NULL) {
384 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
385 if (sc->sc_oxfer == NULL) {
390 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
393 if (sc->sc_obuf == NULL) {
399 * Handle initial DCD.
401 if (ISSET(sc->sc_msr, UMSR_DCD) ||
402 (minor(dev) & UCOM_CALLOUT_MASK))
403 (*linesw[tp->t_line].l_modem)(tp, 1);
409 wakeup(&sc->sc_opening);
412 error = ttyopen(dev, tp);
416 error = (*linesw[tp->t_line].l_open)(dev, tp);
420 disc_optim(tp, &tp->t_termios, sc);
422 DPRINTF(("%s: ucomopen: success\n", USBDEVNAME(sc->sc_dev)));
430 usbd_free_xfer(sc->sc_oxfer);
433 usbd_free_xfer(sc->sc_ixfer);
436 usbd_close_pipe(sc->sc_bulkout_pipe);
437 sc->sc_bulkout_pipe = NULL;
439 usbd_close_pipe(sc->sc_bulkin_pipe);
440 sc->sc_bulkin_pipe = NULL;
443 wakeup(&sc->sc_opening);
448 if (!ISSET(tp->t_state, TS_ISOPEN)) {
450 * We failed to open the device, and nobody else had it opened.
451 * Clean up the state as appropriate.
456 DPRINTF(("%s: ucomopen: failed\n", USBDEVNAME(sc->sc_dev)));
462 ucomclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
464 struct ucom_softc *sc;
468 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
472 DPRINTF(("%s: ucomclose: unit = %d\n",
473 USBDEVNAME(sc->sc_dev), UCOMUNIT(dev)));
475 if (!ISSET(tp->t_state, TS_ISOPEN))
479 (*linesw[tp->t_line].l_close)(tp, flag);
480 disc_optim(tp, &tp->t_termios, sc);
487 if (!ISSET(tp->t_state, TS_ISOPEN)) {
489 * Although we got a last close, the device may still be in
490 * use; e.g. if this was the dialout node, and there are still
491 * processes waiting for carrier on the non-dialout node.
496 if (sc->sc_callback->ucom_close != NULL)
497 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
500 if (--sc->sc_refcnt < 0)
501 usb_detach_wakeup(USBDEV(sc->sc_dev));
507 ucomread(dev_t dev, struct uio *uio, int flag)
509 struct ucom_softc *sc;
513 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
516 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, flag));
521 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
523 DPRINTF(("ucomread: error = %d\n", error));
529 ucomwrite(dev_t dev, struct uio *uio, int flag)
531 struct ucom_softc *sc;
535 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
538 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, flag));
543 error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
545 DPRINTF(("ucomwrite: error = %d\n", error));
551 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
553 struct ucom_softc *sc;
559 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
565 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", cmd));
567 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
568 if (error != ENOIOCTL) {
569 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
575 error = ttioctl(tp, cmd, data, flag);
576 disc_optim(tp, &tp->t_termios, sc);
577 if (error != ENOIOCTL) {
579 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
583 if (sc->sc_callback->ucom_ioctl != NULL) {
585 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
594 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", cmd));
598 DPRINTF(("ucomioctl: TIOCSBRK\n"));
602 DPRINTF(("ucomioctl: TIOCCBRK\n"));
607 DPRINTF(("ucomioctl: TIOCSDTR\n"));
608 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
611 DPRINTF(("ucomioctl: TIOCCDTR\n"));
612 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
617 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
618 (void)ucomctl(sc, d, DMSET);
622 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
623 (void)ucomctl(sc, d, DMBIS);
627 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
628 (void)ucomctl(sc, d, DMBIC);
631 d = ucomctl(sc, 0, DMGET);
632 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
637 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", cmd));
648 ucomctl(struct ucom_softc *sc, int bits, int how)
654 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
657 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
658 DPRINTF(("ucomctl: DMGET: LE"));
661 if (ISSET(mcr, UMCR_DTR)) {
662 SET(bits, TIOCM_DTR);
665 if (ISSET(mcr, UMCR_RTS)) {
666 SET(bits, TIOCM_RTS);
671 if (ISSET(msr, UMSR_CTS)) {
672 SET(bits, TIOCM_CTS);
675 if (ISSET(msr, UMSR_DCD)) {
679 if (ISSET(msr, UMSR_DSR)) {
680 SET(bits, TIOCM_DSR);
683 if (ISSET(msr, UMSR_RI)) {
694 if (ISSET(bits, TIOCM_DTR))
696 if (ISSET(bits, TIOCM_RTS))
711 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
714 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
721 ucom_break(struct ucom_softc *sc, int onoff)
723 DPRINTF(("ucom_break: 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_BREAK, onoff);
732 ucom_dtr(struct ucom_softc *sc, int onoff)
734 DPRINTF(("ucom_dtr: 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_DTR, onoff);
743 ucom_rts(struct ucom_softc *sc, int onoff)
745 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
747 if (sc->sc_callback->ucom_set == NULL)
749 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
750 UCOM_SET_RTS, onoff);
754 ucom_status_change(struct ucom_softc *sc)
756 struct tty *tp = sc->sc_tty;
760 if (sc->sc_callback->ucom_get_status == NULL) {
766 old_msr = sc->sc_msr;
767 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
768 &sc->sc_lsr, &sc->sc_msr);
769 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
770 if (sc->sc_poll == 0)
772 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
773 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
774 (*linesw[tp->t_line].l_modem)(tp, onoff);
779 ucomparam(struct tty *tp, struct termios *t)
781 struct ucom_softc *sc;
785 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
790 DPRINTF(("ucomparam: sc = %p\n", sc));
792 /* Check requested parameters. */
793 if (t->c_ospeed < 0) {
794 DPRINTF(("ucomparam: negative ospeed\n"));
797 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
798 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
803 * If there were no changes, don't do anything. This avoids dropping
804 * input and improves performance when all we did was frob things like
807 if (tp->t_ospeed == t->c_ospeed &&
808 tp->t_cflag == t->c_cflag)
811 /* And copy to tty. */
813 tp->t_ospeed = t->c_ospeed;
814 tp->t_cflag = t->c_cflag;
816 if (sc->sc_callback->ucom_param == NULL)
821 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
823 DPRINTF(("ucomparam: callback: error = %d\n", error));
829 if (t->c_cflag & CRTS_IFLOW) {
830 sc->sc_state |= UCS_RTS_IFLOW;
831 } else if (sc->sc_state & UCS_RTS_IFLOW) {
832 sc->sc_state &= ~UCS_RTS_IFLOW;
833 (void)ucomctl(sc, UMCR_RTS, DMBIS);
836 disc_optim(tp, t, sc);
838 uerr = ucomstartread(sc);
839 if (uerr != USBD_NORMAL_COMPLETION)
846 ucomstart(struct tty *tp)
848 struct ucom_softc *sc;
855 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
856 DPRINTF(("ucomstart: sc = %p\n", sc));
863 if (tp->t_state & TS_TBLOCK) {
864 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
865 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
866 DPRINTF(("ucomstart: clear RTS\n"));
867 (void)ucomctl(sc, UMCR_RTS, DMBIC);
870 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
871 tp->t_rawq.c_cc <= tp->t_ilowat &&
872 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
873 DPRINTF(("ucomstart: set RTS\n"));
874 (void)ucomctl(sc, UMCR_RTS, DMBIS);
878 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
880 DPRINTF(("ucomstart: stopped\n"));
884 if (tp->t_outq.c_cc <= tp->t_olowat) {
885 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
886 CLR(tp->t_state, TS_SO_OLOWAT);
887 wakeup(TSA_OLOWAT(tp));
889 selwakeup(&tp->t_wsel);
890 if (tp->t_outq.c_cc == 0) {
891 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
892 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
893 CLR(tp->t_state, TS_SO_OCOMPLETE);
894 wakeup(TSA_OCOMPLETE(tp));
900 /* Grab the first contiguous region of buffer space. */
901 data = tp->t_outq.c_cf;
902 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
903 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
906 DPRINTF(("ucomstart: cnt == 0\n"));
910 SET(tp->t_state, TS_BUSY);
912 if (cnt > sc->sc_obufsize) {
913 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
914 cnt = sc->sc_obufsize;
916 if (sc->sc_callback->ucom_write != NULL)
917 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
918 sc->sc_obuf, data, &cnt);
920 memcpy(sc->sc_obuf, data, cnt);
922 DPRINTF(("ucomstart: %d chars\n", cnt));
923 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
924 (usbd_private_handle)sc, sc->sc_obuf, cnt,
925 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
926 /* What can we do on error? */
927 err = usbd_transfer(sc->sc_oxfer);
928 if (err != USBD_IN_PROGRESS)
929 printf("ucomstart: err=%s\n", usbd_errstr(err));
938 ucomstop(struct tty *tp, int flag)
940 struct ucom_softc *sc;
943 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
945 DPRINTF(("ucomstop: %d\n", flag));
948 DPRINTF(("ucomstop: read\n"));
953 DPRINTF(("ucomstop: write\n"));
955 if (ISSET(tp->t_state, TS_BUSY)) {
957 if (!ISSET(tp->t_state, TS_TTSTOP))
958 SET(tp->t_state, TS_FLUSH);
963 DPRINTF(("ucomstop: done\n"));
967 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
969 struct ucom_softc *sc = (struct ucom_softc *)p;
970 struct tty *tp = sc->sc_tty;
974 DPRINTF(("ucomwritecb: status = %d\n", status));
976 if (status == USBD_CANCELLED || sc->sc_dying)
979 if (status != USBD_NORMAL_COMPLETION) {
980 printf("%s: ucomwritecb: %s\n",
981 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
982 if (status == USBD_STALLED)
983 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
984 /* XXX we should restart after some delay. */
988 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
989 DPRINTF(("ucomwritecb: cc = %d\n", cc));
990 if (cc <= sc->sc_opkthdrlen) {
991 printf("%s: sent size too small, cc = %d\n",
992 USBDEVNAME(sc->sc_dev), cc);
996 /* convert from USB bytes to tty bytes */
997 cc -= sc->sc_opkthdrlen;
1000 CLR(tp->t_state, TS_BUSY);
1001 if (ISSET(tp->t_state, TS_FLUSH))
1002 CLR(tp->t_state, TS_FLUSH);
1004 ndflush(&tp->t_outq, cc);
1005 (*linesw[tp->t_line].l_start)(tp);
1012 CLR(tp->t_state, TS_BUSY);
1018 ucomstartread(struct ucom_softc *sc)
1022 DPRINTF(("ucomstartread: start\n"));
1024 sc->sc_state &= ~UCS_RXSTOP;
1026 if (sc->sc_bulkin_pipe == NULL)
1027 return (USBD_NORMAL_COMPLETION);
1029 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1030 (usbd_private_handle)sc,
1031 sc->sc_ibuf, sc->sc_ibufsize,
1032 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1033 USBD_NO_TIMEOUT, ucomreadcb);
1035 err = usbd_transfer(sc->sc_ixfer);
1036 if (err != USBD_IN_PROGRESS) {
1037 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1041 return (USBD_NORMAL_COMPLETION);
1045 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1047 struct ucom_softc *sc = (struct ucom_softc *)p;
1048 struct tty *tp = sc->sc_tty;
1049 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1056 DPRINTF(("ucomreadcb: status = %d\n", status));
1058 if (status != USBD_NORMAL_COMPLETION) {
1059 if (!(sc->sc_state & UCS_RXSTOP))
1060 printf("%s: ucomreadcb: %s\n",
1061 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1062 if (status == USBD_STALLED)
1063 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1064 /* XXX we should restart after some delay. */
1068 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1069 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1073 if (sc->sc_callback->ucom_read != NULL) {
1074 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1078 if (cc > sc->sc_ibufsize) {
1079 printf("%s: invalid receive data size, %d chars\n",
1080 USBDEVNAME(sc->sc_dev), cc);
1087 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1088 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1089 && (sc->sc_state & UCS_RTS_IFLOW
1090 || tp->t_iflag & IXOFF)
1091 && !(tp->t_state & TS_TBLOCK))
1093 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1096 if (tp->t_state & TS_TTSTOP
1097 && (tp->t_iflag & IXANY
1098 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1099 tp->t_state &= ~TS_TTSTOP;
1100 tp->t_lflag &= ~FLUSHO;
1104 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1107 /* Give characters to tty layer. */
1109 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1110 if ((*rint)(*cp, tp) == -1) {
1111 /* XXX what should we do? */
1112 printf("%s: lost %d chars\n",
1113 USBDEVNAME(sc->sc_dev), cc);
1123 err = ucomstartread(sc);
1125 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1126 /* XXX what should we dow now? */
1129 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1130 && !(tp->t_state & TS_TBLOCK))
1131 ucomctl(sc, UMCR_RTS, DMBIS);
1135 ucom_cleanup(struct ucom_softc *sc)
1137 DPRINTF(("ucom_cleanup: closing pipes\n"));
1140 if (sc->sc_bulkin_pipe != NULL) {
1141 usbd_abort_pipe(sc->sc_bulkin_pipe);
1142 usbd_close_pipe(sc->sc_bulkin_pipe);
1143 sc->sc_bulkin_pipe = NULL;
1145 if (sc->sc_bulkout_pipe != NULL) {
1146 usbd_abort_pipe(sc->sc_bulkout_pipe);
1147 usbd_close_pipe(sc->sc_bulkout_pipe);
1148 sc->sc_bulkout_pipe = NULL;
1150 if (sc->sc_ixfer != NULL) {
1151 usbd_free_xfer(sc->sc_ixfer);
1152 sc->sc_ixfer = NULL;
1154 if (sc->sc_oxfer != NULL) {
1155 usbd_free_xfer(sc->sc_oxfer);
1156 sc->sc_oxfer = NULL;
1161 ucomstopread(struct ucom_softc *sc)
1165 DPRINTF(("ucomstopread: enter\n"));
1167 if (!(sc->sc_state & UCS_RXSTOP)) {
1168 sc->sc_state |= UCS_RXSTOP;
1169 if (sc->sc_bulkin_pipe == NULL) {
1170 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1173 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1175 DPRINTF(("ucomstopread: err = %s\n",
1180 DPRINTF(("ucomstopread: leave\n"));
1184 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1186 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1187 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1188 && (!(t->c_iflag & PARMRK)
1189 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1190 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1191 && linesw[tp->t_line].l_rint == ttyinput) {
1192 DPRINTF(("disc_optim: bypass l_rint\n"));
1193 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1195 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1196 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1198 sc->hotchar = linesw[tp->t_line].l_hotchar;