1 /* $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $ */
5 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Lennart Augustsson (lennart@augustsson.net) at
36 * Carlstedt Research & Technology.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the NetBSD
49 * Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
69 * 1. How do I handle hotchar?
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/ioccom.h>
78 #include <sys/fcntl.h>
81 #include <sys/clist.h>
83 #if __FreeBSD_version >= 500014
84 #include <sys/selinfo.h>
86 #include <sys/select.h>
89 #include <sys/vnode.h>
91 #include <sys/sysctl.h>
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbcdc.h>
96 #include <dev/usb/usbdi.h>
97 #include <dev/usb/usbdi_util.h>
98 #include <dev/usb/usbdevs.h>
99 #include <dev/usb/usb_quirks.h>
101 #include <dev/usb/ucomvar.h>
104 static int ucomdebug = 0;
105 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
106 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
107 &ucomdebug, 0, "ucom debug level");
108 #define DPRINTF(x) do { \
113 #define DPRINTFN(n, x) do { \
114 if (ucomdebug > (n)) \
119 #define DPRINTFN(n, x)
122 Static d_open_t ucomopen;
123 Static d_close_t ucomclose;
124 Static d_read_t ucomread;
125 Static d_write_t ucomwrite;
126 Static d_ioctl_t ucomioctl;
128 #define UCOM_CDEV_MAJOR 138
130 static struct cdevsw ucom_cdevsw = {
132 /* close */ ucomclose,
134 /* write */ ucomwrite,
135 /* ioctl */ ucomioctl,
138 /* strategy */ nostrategy,
140 /* maj */ UCOM_CDEV_MAJOR,
143 /* flags */ D_TTY | D_KQFILTER,
144 #if __FreeBSD_version < 500014
147 /* kqfilter */ ttykqfilter,
150 Static void ucom_cleanup(struct ucom_softc *);
151 Static int ucomctl(struct ucom_softc *, int, int);
152 Static int ucomparam(struct tty *, struct termios *);
153 Static void ucomstart(struct tty *);
154 Static void ucomstop(struct tty *, int);
155 Static void ucom_shutdown(struct ucom_softc *);
156 Static void ucom_dtr(struct ucom_softc *, int);
157 Static void ucom_rts(struct ucom_softc *, int);
158 Static void ucom_break(struct ucom_softc *, int);
159 Static usbd_status ucomstartread(struct ucom_softc *);
160 Static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
161 Static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
162 Static void ucomstopread(struct ucom_softc *);
163 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
165 devclass_t ucom_devclass;
167 static moduledata_t ucom_mod = {
173 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
174 MODULE_DEPEND(ucom, usb, 1, 1, 1);
175 MODULE_VERSION(ucom, UCOM_MODVER);
178 ucom_attach(struct ucom_softc *sc)
183 unit = device_get_unit(sc->sc_dev);
185 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
186 tp->t_oproc = ucomstart;
187 tp->t_param = ucomparam;
188 tp->t_stop = ucomstop;
190 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
192 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
194 sc->dev = make_dev(&ucom_cdevsw, unit | UCOM_CALLOUT_MASK,
195 UID_UUCP, GID_DIALER, 0660,
197 sc->dev->si_tty = tp;
203 ucom_detach(struct ucom_softc *sc)
205 struct tty *tp = sc->sc_tty;
208 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
212 if (sc->sc_bulkin_pipe != NULL)
213 usbd_abort_pipe(sc->sc_bulkin_pipe);
214 if (sc->sc_bulkout_pipe != NULL)
215 usbd_abort_pipe(sc->sc_bulkout_pipe);
218 if (tp->t_state & TS_ISOPEN) {
219 device_printf(sc->sc_dev,
220 "still open, focing close\n");
221 (*linesw[tp->t_line].l_close)(tp, 0);
228 DPRINTF(("ucom_detach: no tty\n"));
233 if (--sc->sc_refcnt >= 0) {
234 /* Wait for processes to go away. */
235 usb_detach_wait(USBDEV(sc->sc_dev));
239 destroy_dev(sc->dev);
245 ucom_shutdown(struct ucom_softc *sc)
247 struct tty *tp = sc->sc_tty;
249 DPRINTF(("ucom_shutdown\n"));
251 * Hang up if necessary. Wait a bit, so the other side has time to
252 * notice even if we immediately open the port again.
254 if (ISSET(tp->t_cflag, HUPCL)) {
255 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
256 (void)tsleep(sc, TTIPRI, "ucomsd", hz);
261 ucomopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
263 int unit = UCOMUNIT(dev);
264 struct ucom_softc *sc;
270 USB_GET_SC_OPEN(ucom, unit, sc);
277 DPRINTF(("%s: ucomopen: tp = %p\n", USBDEVNAME(sc->sc_dev), tp));
279 if (ISSET(tp->t_state, TS_ISOPEN) &&
280 ISSET(tp->t_state, TS_XCLUDE) &&
285 * Do the following iff this is a first open.
288 while (sc->sc_opening)
289 tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
292 if (!ISSET(tp->t_state, TS_ISOPEN)) {
296 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
301 * Initialize the termios status to the defaults. Add in the
302 * sticky bits from TIOCSFLAGS.
305 t.c_ospeed = TTYDEF_SPEED;
306 t.c_cflag = TTYDEF_CFLAG;
307 /* Make sure ucomparam() will do something. */
309 (void)ucomparam(tp, &t);
310 tp->t_iflag = TTYDEF_IFLAG;
311 tp->t_oflag = TTYDEF_OFLAG;
312 tp->t_lflag = TTYDEF_LFLAG;
317 * Turn on DTR. We must always do this, even if carrier is not
318 * present, because otherwise we'd have to use TIOCSDTR
319 * immediately after setting CLOCAL, which applications do not
320 * expect. We always assert DTR while the device is open
321 * unless explicitly requested to deassert it.
323 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
325 /* Device specific open */
326 if (sc->sc_callback->ucom_open != NULL) {
327 error = sc->sc_callback->ucom_open(sc->sc_parent,
332 wakeup(&sc->sc_opening);
338 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
339 sc->sc_bulkin_no, sc->sc_bulkout_no));
341 /* Open the bulk pipes */
343 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
344 &sc->sc_bulkin_pipe);
346 printf("%s: open bulk out error (addr %d): %s\n",
347 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
353 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
354 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
356 printf("%s: open bulk in error (addr %d): %s\n",
357 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
363 /* Allocate a request and an input buffer and start reading. */
364 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
365 if (sc->sc_ixfer == NULL) {
370 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
372 if (sc->sc_ibuf == NULL) {
377 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
378 if (sc->sc_oxfer == NULL) {
383 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
386 if (sc->sc_obuf == NULL) {
392 * Handle initial DCD.
394 if (ISSET(sc->sc_msr, UMSR_DCD) ||
395 (minor(dev) & UCOM_CALLOUT_MASK))
396 (*linesw[tp->t_line].l_modem)(tp, 1);
402 wakeup(&sc->sc_opening);
405 error = ttyopen(dev, tp);
409 error = (*linesw[tp->t_line].l_open)(dev, tp);
413 disc_optim(tp, &tp->t_termios, sc);
415 DPRINTF(("%s: ucomopen: success\n", USBDEVNAME(sc->sc_dev)));
423 usbd_free_xfer(sc->sc_oxfer);
426 usbd_free_xfer(sc->sc_ixfer);
429 usbd_close_pipe(sc->sc_bulkout_pipe);
430 sc->sc_bulkout_pipe = NULL;
432 usbd_close_pipe(sc->sc_bulkin_pipe);
433 sc->sc_bulkin_pipe = NULL;
436 wakeup(&sc->sc_opening);
441 if (!ISSET(tp->t_state, TS_ISOPEN)) {
443 * We failed to open the device, and nobody else had it opened.
444 * Clean up the state as appropriate.
449 DPRINTF(("%s: ucomopen: failed\n", USBDEVNAME(sc->sc_dev)));
455 ucomclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
457 struct ucom_softc *sc;
461 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
465 DPRINTF(("%s: ucomclose: unit = %d\n",
466 USBDEVNAME(sc->sc_dev), UCOMUNIT(dev)));
468 if (!ISSET(tp->t_state, TS_ISOPEN))
472 (*linesw[tp->t_line].l_close)(tp, flag);
473 disc_optim(tp, &tp->t_termios, sc);
480 if (!ISSET(tp->t_state, TS_ISOPEN)) {
482 * Although we got a last close, the device may still be in
483 * use; e.g. if this was the dialout node, and there are still
484 * processes waiting for carrier on the non-dialout node.
489 if (sc->sc_callback->ucom_close != NULL)
490 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
493 if (--sc->sc_refcnt < 0)
494 usb_detach_wakeup(USBDEV(sc->sc_dev));
500 ucomread(dev_t dev, struct uio *uio, int flag)
502 struct ucom_softc *sc;
506 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
509 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, flag));
514 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
516 DPRINTF(("ucomread: error = %d\n", error));
522 ucomwrite(dev_t dev, struct uio *uio, int flag)
524 struct ucom_softc *sc;
528 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
531 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, flag));
536 error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
538 DPRINTF(("ucomwrite: error = %d\n", error));
544 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
546 struct ucom_softc *sc;
552 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
558 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", cmd));
560 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
562 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
566 error = ttioctl(tp, cmd, data, flag);
567 disc_optim(tp, &tp->t_termios, sc);
569 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
573 if (sc->sc_callback->ucom_ioctl != NULL) {
574 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
583 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", 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);
608 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
609 (void)ucomctl(sc, d, DMSET);
613 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
614 (void)ucomctl(sc, d, DMBIS);
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));
628 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", 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 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
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;
846 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
847 DPRINTF(("ucomstart: sc = %p\n", sc));
854 if (tp->t_state & TS_TBLOCK) {
855 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
856 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
857 DPRINTF(("ucomstart: clear RTS\n"));
858 (void)ucomctl(sc, UMCR_RTS, DMBIC);
861 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
862 tp->t_rawq.c_cc <= tp->t_ilowat &&
863 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
864 DPRINTF(("ucomstart: set RTS\n"));
865 (void)ucomctl(sc, UMCR_RTS, DMBIS);
869 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
871 DPRINTF(("ucomstart: stopped\n"));
875 if (tp->t_outq.c_cc <= tp->t_olowat) {
876 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
877 CLR(tp->t_state, TS_SO_OLOWAT);
878 wakeup(TSA_OLOWAT(tp));
880 selwakeup(&tp->t_wsel);
881 if (tp->t_outq.c_cc == 0) {
882 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
883 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
884 CLR(tp->t_state, TS_SO_OCOMPLETE);
885 wakeup(TSA_OCOMPLETE(tp));
891 /* Grab the first contiguous region of buffer space. */
892 data = tp->t_outq.c_cf;
893 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
894 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
897 DPRINTF(("ucomstart: cnt == 0\n"));
901 SET(tp->t_state, TS_BUSY);
903 if (cnt > sc->sc_obufsize) {
904 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
905 cnt = sc->sc_obufsize;
907 if (sc->sc_callback->ucom_write != NULL)
908 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
909 sc->sc_obuf, data, &cnt);
911 memcpy(sc->sc_obuf, data, cnt);
913 DPRINTF(("ucomstart: %d chars\n", cnt));
914 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
915 (usbd_private_handle)sc, sc->sc_obuf, cnt,
916 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
917 /* What can we do on error? */
918 err = usbd_transfer(sc->sc_oxfer);
919 if (err != USBD_IN_PROGRESS)
920 printf("ucomstart: err=%s\n", usbd_errstr(err));
929 ucomstop(struct tty *tp, int flag)
931 struct ucom_softc *sc;
934 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
936 DPRINTF(("ucomstop: %d\n", flag));
939 DPRINTF(("ucomstop: read\n"));
944 DPRINTF(("ucomstop: write\n"));
946 if (ISSET(tp->t_state, TS_BUSY)) {
948 if (!ISSET(tp->t_state, TS_TTSTOP))
949 SET(tp->t_state, TS_FLUSH);
954 DPRINTF(("ucomstop: done\n"));
958 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
960 struct ucom_softc *sc = (struct ucom_softc *)p;
961 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 printf("%s: ucomwritecb: %s\n",
972 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
973 if (status == USBD_STALLED)
974 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
975 /* XXX we should restart after some delay. */
979 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
980 DPRINTF(("ucomwritecb: cc = %d\n", cc));
982 /* convert from USB bytes to tty bytes */
983 cc -= sc->sc_opkthdrlen;
986 CLR(tp->t_state, TS_BUSY);
987 if (ISSET(tp->t_state, TS_FLUSH))
988 CLR(tp->t_state, TS_FLUSH);
990 ndflush(&tp->t_outq, cc);
991 (*linesw[tp->t_line].l_start)(tp);
998 CLR(tp->t_state, TS_BUSY);
1004 ucomstartread(struct ucom_softc *sc)
1008 DPRINTF(("ucomstartread: start\n"));
1010 sc->sc_state &= ~UCS_RXSTOP;
1012 if (sc->sc_bulkin_pipe == NULL)
1013 return (USBD_NORMAL_COMPLETION);
1015 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1016 (usbd_private_handle)sc,
1017 sc->sc_ibuf, sc->sc_ibufsize,
1018 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1019 USBD_NO_TIMEOUT, ucomreadcb);
1021 err = usbd_transfer(sc->sc_ixfer);
1022 if (err != USBD_IN_PROGRESS) {
1023 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1027 return (USBD_NORMAL_COMPLETION);
1031 ucomreadcb(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;
1035 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1042 DPRINTF(("ucomreadcb: status = %d\n", status));
1044 if (status != USBD_NORMAL_COMPLETION) {
1045 if (!(sc->sc_state & UCS_RXSTOP))
1046 printf("%s: ucomreadcb: %s\n",
1047 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1048 if (status == USBD_STALLED)
1049 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1050 /* XXX we should restart after some delay. */
1054 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1055 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1056 if (sc->sc_callback->ucom_read != NULL)
1057 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1061 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1062 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1063 && (sc->sc_state & UCS_RTS_IFLOW
1064 || tp->t_iflag & IXOFF)
1065 && !(tp->t_state & TS_TBLOCK))
1067 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1070 if (tp->t_state & TS_TTSTOP
1071 && (tp->t_iflag & IXANY
1072 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1073 tp->t_state &= ~TS_TTSTOP;
1074 tp->t_lflag &= ~FLUSHO;
1078 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1081 /* Give characters to tty layer. */
1083 DPRINTFN(7,("ucomreadcb: char = 0x%02x\n", *cp));
1084 if ((*rint)(*cp++, tp) == -1) {
1085 /* XXX what should we do? */
1086 printf("%s: lost %d chars\n",
1087 USBDEVNAME(sc->sc_dev), cc);
1094 err = ucomstartread(sc);
1096 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1097 /* XXX what should we dow now? */
1100 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1101 && !(tp->t_state & TS_TBLOCK))
1102 ucomctl(sc, UMCR_RTS, DMBIS);
1106 ucom_cleanup(struct ucom_softc *sc)
1108 DPRINTF(("ucom_cleanup: closing pipes\n"));
1111 if (sc->sc_bulkin_pipe != NULL) {
1112 usbd_abort_pipe(sc->sc_bulkin_pipe);
1113 usbd_close_pipe(sc->sc_bulkin_pipe);
1114 sc->sc_bulkin_pipe = NULL;
1116 if (sc->sc_bulkout_pipe != NULL) {
1117 usbd_abort_pipe(sc->sc_bulkout_pipe);
1118 usbd_close_pipe(sc->sc_bulkout_pipe);
1119 sc->sc_bulkout_pipe = NULL;
1121 if (sc->sc_ixfer != NULL) {
1122 usbd_free_xfer(sc->sc_ixfer);
1123 sc->sc_ixfer = NULL;
1125 if (sc->sc_oxfer != NULL) {
1126 usbd_free_xfer(sc->sc_oxfer);
1127 sc->sc_oxfer = NULL;
1132 ucomstopread(struct ucom_softc *sc)
1136 DPRINTF(("ucomstopread: enter\n"));
1138 if (!(sc->sc_state & UCS_RXSTOP)) {
1139 if (sc->sc_bulkin_pipe == NULL) {
1140 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1143 sc->sc_state |= UCS_RXSTOP;
1144 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1146 DPRINTF(("ucomstopread: err = %s\n",
1151 DPRINTF(("ucomstopread: leave\n"));
1155 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1157 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1158 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1159 && (!(t->c_iflag & PARMRK)
1160 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1161 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1162 && linesw[tp->t_line].l_rint == ttyinput) {
1163 DPRINTF(("disc_optim: bypass l_rint\n"));
1164 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1166 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1167 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1169 sc->hotchar = linesw[tp->t_line].l_hotchar;