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.19 2005/06/10 23:11:54 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.
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/malloc.h>
75 #include <sys/ioccom.h>
76 #include <sys/fcntl.h>
79 #include <sys/clist.h>
81 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
82 #include <sys/selinfo.h>
84 #include <sys/select.h>
87 #include <sys/vnode.h>
89 #include <sys/sysctl.h>
90 #include <sys/thread2.h>
92 #include <bus/usb/usb.h>
93 #include <bus/usb/usbcdc.h>
95 #include <bus/usb/usbdi.h>
96 #include <bus/usb/usbdi_util.h>
97 #include <bus/usb/usbdevs.h>
98 #include <bus/usb/usb_quirks.h>
103 static int ucomdebug = 0;
104 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
105 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
106 &ucomdebug, 0, "ucom debug level");
107 #define DPRINTF(x) do { \
112 #define DPRINTFN(n, x) do { \
113 if (ucomdebug > (n)) \
118 #define DPRINTFN(n, x)
121 Static d_open_t ucomopen;
122 Static d_close_t ucomclose;
123 Static d_read_t ucomread;
124 Static d_write_t ucomwrite;
125 Static d_ioctl_t ucomioctl;
127 #define UCOM_CDEV_MAJOR 138
129 static struct cdevsw ucom_cdevsw = {
131 /* maj */ UCOM_CDEV_MAJOR,
132 /* flags */ D_TTY | D_KQFILTER,
137 /* close */ ucomclose,
139 /* write */ ucomwrite,
140 /* ioctl */ ucomioctl,
143 /* strategy */ nostrategy,
146 /* kqfilter */ ttykqfilter
149 Static void ucom_cleanup(struct ucom_softc *);
150 Static int ucomctl(struct ucom_softc *, int, int);
151 Static int ucomparam(struct tty *, struct termios *);
152 Static void ucomstart(struct tty *);
153 Static void ucomstop(struct tty *, int);
154 Static void ucom_shutdown(struct ucom_softc *);
155 Static void ucom_dtr(struct ucom_softc *, int);
156 Static void ucom_rts(struct ucom_softc *, int);
157 Static void ucom_break(struct ucom_softc *, int);
158 Static usbd_status ucomstartread(struct ucom_softc *);
159 Static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
160 Static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
161 Static void ucomstopread(struct ucom_softc *);
162 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
164 devclass_t ucom_devclass;
166 static moduledata_t ucom_mod = {
172 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
173 MODULE_DEPEND(ucom, usb, 1, 1, 1);
174 MODULE_VERSION(ucom, UCOM_MODVER);
177 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 cdevsw_add(&ucom_cdevsw, UCOMUNIT_MASK, unit);
195 dev = make_dev(&ucom_cdevsw, unit | UCOM_CALLOUT_MASK,
196 UID_UUCP, GID_DIALER, 0660,
204 ucom_detach(struct ucom_softc *sc)
206 struct tty *tp = sc->sc_tty;
209 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
213 if (sc->sc_bulkin_pipe != NULL)
214 usbd_abort_pipe(sc->sc_bulkin_pipe);
215 if (sc->sc_bulkout_pipe != NULL)
216 usbd_abort_pipe(sc->sc_bulkout_pipe);
219 if (tp->t_state & TS_ISOPEN) {
220 device_printf(sc->sc_dev,
221 "still open, forcing close\n");
222 (*linesw[tp->t_line].l_close)(tp, 0);
229 DPRINTF(("ucom_detach: no tty\n"));
234 if (--sc->sc_refcnt >= 0) {
235 /* Wait for processes to go away. */
236 usb_detach_wait(USBDEV(sc->sc_dev));
240 unit = device_get_unit(sc->sc_dev);
241 cdevsw_remove(&ucom_cdevsw, UCOMUNIT_MASK, unit);
247 ucom_shutdown(struct ucom_softc *sc)
249 struct tty *tp = sc->sc_tty;
251 DPRINTF(("ucom_shutdown\n"));
253 * Hang up if necessary. Wait a bit, so the other side has time to
254 * notice even if we immediately open the port again.
256 if (ISSET(tp->t_cflag, HUPCL)) {
257 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
258 (void)tsleep(sc, 0, "ucomsd", hz);
263 ucomopen(dev_t dev, int flag, int mode, usb_proc_ptr td)
265 int unit = UCOMUNIT(dev);
266 struct ucom_softc *sc;
271 KKASSERT(td != NULL);
273 USB_GET_SC_OPEN(ucom, unit, sc);
280 DPRINTF(("%s: ucomopen: tp = %p\n", USBDEVNAME(sc->sc_dev), tp));
282 if (ISSET(tp->t_state, TS_ISOPEN) &&
283 ISSET(tp->t_state, TS_XCLUDE) &&
290 * Do the following iff this is a first open.
293 while (sc->sc_opening)
294 tsleep(&sc->sc_opening, 0, "ucomop", 0);
297 if (!ISSET(tp->t_state, TS_ISOPEN)) {
301 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
303 tp->t_dev = reference_dev(dev);
306 * Initialize the termios status to the defaults. Add in the
307 * sticky bits from TIOCSFLAGS.
310 t.c_ospeed = TTYDEF_SPEED;
311 t.c_cflag = TTYDEF_CFLAG;
312 /* Make sure ucomparam() will do something. */
314 (void)ucomparam(tp, &t);
315 tp->t_iflag = TTYDEF_IFLAG;
316 tp->t_oflag = TTYDEF_OFLAG;
317 tp->t_lflag = TTYDEF_LFLAG;
322 * Turn on DTR. We must always do this, even if carrier is not
323 * present, because otherwise we'd have to use TIOCSDTR
324 * immediately after setting CLOCAL, which applications do not
325 * expect. We always assert DTR while the device is open
326 * unless explicitly requested to deassert it.
328 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
330 /* Device specific open */
331 if (sc->sc_callback->ucom_open != NULL) {
332 error = sc->sc_callback->ucom_open(sc->sc_parent,
337 wakeup(&sc->sc_opening);
343 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
344 sc->sc_bulkin_no, sc->sc_bulkout_no));
346 /* Open the bulk pipes */
348 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
349 &sc->sc_bulkin_pipe);
351 printf("%s: open bulk in error (addr %d): %s\n",
352 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
358 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
359 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
361 printf("%s: open bulk out error (addr %d): %s\n",
362 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
368 /* Allocate a request and an input buffer and start reading. */
369 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
370 if (sc->sc_ixfer == NULL) {
375 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
377 if (sc->sc_ibuf == NULL) {
382 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
383 if (sc->sc_oxfer == NULL) {
388 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
391 if (sc->sc_obuf == NULL) {
397 * Handle initial DCD.
399 if (ISSET(sc->sc_msr, UMSR_DCD) ||
400 (minor(dev) & UCOM_CALLOUT_MASK))
401 (*linesw[tp->t_line].l_modem)(tp, 1);
407 wakeup(&sc->sc_opening);
410 error = ttyopen(dev, tp);
414 error = (*linesw[tp->t_line].l_open)(dev, tp);
418 disc_optim(tp, &tp->t_termios, sc);
420 DPRINTF(("%s: ucomopen: success\n", USBDEVNAME(sc->sc_dev)));
428 usbd_free_xfer(sc->sc_oxfer);
431 usbd_free_xfer(sc->sc_ixfer);
434 usbd_close_pipe(sc->sc_bulkout_pipe);
435 sc->sc_bulkout_pipe = NULL;
437 usbd_close_pipe(sc->sc_bulkin_pipe);
438 sc->sc_bulkin_pipe = NULL;
441 wakeup(&sc->sc_opening);
446 if (!ISSET(tp->t_state, TS_ISOPEN)) {
448 * We failed to open the device, and nobody else had it opened.
449 * Clean up the state as appropriate.
454 DPRINTF(("%s: ucomopen: failed\n", USBDEVNAME(sc->sc_dev)));
460 ucomclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
462 struct ucom_softc *sc;
465 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
469 DPRINTF(("%s: ucomclose: unit = %d\n",
470 USBDEVNAME(sc->sc_dev), UCOMUNIT(dev)));
472 if (!ISSET(tp->t_state, TS_ISOPEN))
476 (*linesw[tp->t_line].l_close)(tp, flag);
477 disc_optim(tp, &tp->t_termios, sc);
484 if (!ISSET(tp->t_state, TS_ISOPEN)) {
486 * Although we got a last close, the device may still be in
487 * use; e.g. if this was the dialout node, and there are still
488 * processes waiting for carrier on the non-dialout node.
493 if (sc->sc_callback->ucom_close != NULL)
494 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
498 release_dev(tp->t_dev);
502 if (--sc->sc_refcnt < 0)
503 usb_detach_wakeup(USBDEV(sc->sc_dev));
509 ucomread(dev_t dev, struct uio *uio, int flag)
511 struct ucom_softc *sc;
515 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
518 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, flag));
523 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
525 DPRINTF(("ucomread: error = %d\n", error));
531 ucomwrite(dev_t dev, struct uio *uio, int flag)
533 struct ucom_softc *sc;
537 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
540 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, flag));
545 error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
547 DPRINTF(("ucomwrite: error = %d\n", error));
553 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
555 struct ucom_softc *sc;
560 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
566 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", cmd));
568 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
569 if (error != ENOIOCTL) {
570 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
576 error = ttioctl(tp, cmd, data, flag);
577 disc_optim(tp, &tp->t_termios, sc);
578 if (error != ENOIOCTL) {
580 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
584 if (sc->sc_callback->ucom_ioctl != NULL) {
585 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
596 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", cmd));
600 DPRINTF(("ucomioctl: TIOCSBRK\n"));
604 DPRINTF(("ucomioctl: TIOCCBRK\n"));
609 DPRINTF(("ucomioctl: TIOCSDTR\n"));
610 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
613 DPRINTF(("ucomioctl: TIOCCDTR\n"));
614 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
619 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
620 (void)ucomctl(sc, d, DMSET);
624 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
625 (void)ucomctl(sc, d, DMBIS);
629 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
630 (void)ucomctl(sc, d, DMBIC);
633 d = ucomctl(sc, 0, DMGET);
634 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
639 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", cmd));
650 ucomctl(struct ucom_softc *sc, int bits, int how)
656 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
659 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
660 DPRINTF(("ucomctl: DMGET: LE"));
663 if (ISSET(mcr, UMCR_DTR)) {
664 SET(bits, TIOCM_DTR);
667 if (ISSET(mcr, UMCR_RTS)) {
668 SET(bits, TIOCM_RTS);
673 if (ISSET(msr, UMSR_CTS)) {
674 SET(bits, TIOCM_CTS);
677 if (ISSET(msr, UMSR_DCD)) {
681 if (ISSET(msr, UMSR_DSR)) {
682 SET(bits, TIOCM_DSR);
685 if (ISSET(msr, UMSR_RI)) {
696 if (ISSET(bits, TIOCM_DTR))
698 if (ISSET(bits, TIOCM_RTS))
713 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
716 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
723 ucom_break(struct ucom_softc *sc, int onoff)
725 DPRINTF(("ucom_break: 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_BREAK, onoff);
734 ucom_dtr(struct ucom_softc *sc, int onoff)
736 DPRINTF(("ucom_dtr: 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_DTR, onoff);
745 ucom_rts(struct ucom_softc *sc, int onoff)
747 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
749 if (sc->sc_callback->ucom_set == NULL)
751 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
752 UCOM_SET_RTS, onoff);
756 ucom_status_change(struct ucom_softc *sc)
758 struct tty *tp = sc->sc_tty;
762 if (sc->sc_callback->ucom_get_status == NULL) {
768 old_msr = sc->sc_msr;
769 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
770 &sc->sc_lsr, &sc->sc_msr);
771 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
772 if (sc->sc_poll == 0)
774 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
775 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
776 (*linesw[tp->t_line].l_modem)(tp, onoff);
781 ucomparam(struct tty *tp, struct termios *t)
783 struct ucom_softc *sc;
787 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
792 DPRINTF(("ucomparam: sc = %p\n", sc));
794 /* Check requested parameters. */
795 if (t->c_ospeed < 0) {
796 DPRINTF(("ucomparam: negative ospeed\n"));
799 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
800 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
805 * If there were no changes, don't do anything. This avoids dropping
806 * input and improves performance when all we did was frob things like
809 if (tp->t_ospeed == t->c_ospeed &&
810 tp->t_cflag == t->c_cflag)
813 /* And copy to tty. */
815 tp->t_ospeed = t->c_ospeed;
816 tp->t_cflag = t->c_cflag;
818 if (sc->sc_callback->ucom_param == NULL)
823 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
825 DPRINTF(("ucomparam: callback: error = %d\n", error));
831 if (t->c_cflag & CRTS_IFLOW) {
832 sc->sc_state |= UCS_RTS_IFLOW;
833 } else if (sc->sc_state & UCS_RTS_IFLOW) {
834 sc->sc_state &= ~UCS_RTS_IFLOW;
835 (void)ucomctl(sc, UMCR_RTS, DMBIS);
838 disc_optim(tp, t, sc);
840 uerr = ucomstartread(sc);
841 if (uerr != USBD_NORMAL_COMPLETION)
848 ucomstart(struct tty *tp)
850 struct ucom_softc *sc;
856 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
857 DPRINTF(("ucomstart: sc = %p\n", sc));
864 if (tp->t_state & TS_TBLOCK) {
865 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
866 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
867 DPRINTF(("ucomstart: clear RTS\n"));
868 (void)ucomctl(sc, UMCR_RTS, DMBIC);
871 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
872 tp->t_rawq.c_cc <= tp->t_ilowat &&
873 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
874 DPRINTF(("ucomstart: set RTS\n"));
875 (void)ucomctl(sc, UMCR_RTS, DMBIS);
879 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
881 DPRINTF(("ucomstart: stopped\n"));
885 if (tp->t_outq.c_cc <= tp->t_olowat) {
886 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
887 CLR(tp->t_state, TS_SO_OLOWAT);
888 wakeup(TSA_OLOWAT(tp));
890 selwakeuppri(&tp->t_wsel, TTIPRI);
891 if (tp->t_outq.c_cc == 0) {
892 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
893 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
894 CLR(tp->t_state, TS_SO_OCOMPLETE);
895 wakeup(TSA_OCOMPLETE(tp));
901 /* Grab the first contiguous region of buffer space. */
902 data = tp->t_outq.c_cf;
903 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
904 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
907 DPRINTF(("ucomstart: cnt == 0\n"));
911 SET(tp->t_state, TS_BUSY);
913 if (cnt > sc->sc_obufsize) {
914 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
915 cnt = sc->sc_obufsize;
917 if (sc->sc_callback->ucom_write != NULL)
918 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
919 sc->sc_obuf, data, &cnt);
921 memcpy(sc->sc_obuf, data, cnt);
923 DPRINTF(("ucomstart: %d chars\n", cnt));
924 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
925 (usbd_private_handle)sc, sc->sc_obuf, cnt,
926 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
927 /* What can we do on error? */
928 err = usbd_transfer(sc->sc_oxfer);
929 if (err != USBD_IN_PROGRESS)
930 printf("ucomstart: err=%s\n", usbd_errstr(err));
939 ucomstop(struct tty *tp, int flag)
941 struct ucom_softc *sc;
943 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
945 DPRINTF(("ucomstop: %d\n", flag));
949 * This is just supposed to flush pending receive data,
950 * not stop the reception of data entirely!
952 DPRINTF(("ucomstop: read\n"));
958 DPRINTF(("ucomstop: write\n"));
960 if (ISSET(tp->t_state, TS_BUSY)) {
962 if (!ISSET(tp->t_state, TS_TTSTOP))
963 SET(tp->t_state, TS_FLUSH);
968 DPRINTF(("ucomstop: done\n"));
972 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
974 struct ucom_softc *sc = (struct ucom_softc *)p;
975 struct tty *tp = sc->sc_tty;
978 DPRINTF(("ucomwritecb: status = %d\n", status));
980 if (status == USBD_CANCELLED || sc->sc_dying)
983 if (status != USBD_NORMAL_COMPLETION) {
984 printf("%s: ucomwritecb: %s\n",
985 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
986 if (status == USBD_STALLED)
987 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
989 * XXX. We may need a flag to sequence ucomstopread() and
990 * ucomstartread() to handle the case where ucomstartread()
991 * is called after ucomstopread() but before the request has
992 * been properly canceled?
997 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
998 DPRINTF(("ucomwritecb: cc = %d\n", cc));
999 if (cc <= sc->sc_opkthdrlen) {
1000 printf("%s: sent size too small, cc = %d\n",
1001 USBDEVNAME(sc->sc_dev), cc);
1005 /* convert from USB bytes to tty bytes */
1006 cc -= sc->sc_opkthdrlen;
1009 CLR(tp->t_state, TS_BUSY);
1010 if (ISSET(tp->t_state, TS_FLUSH))
1011 CLR(tp->t_state, TS_FLUSH);
1013 ndflush(&tp->t_outq, cc);
1014 (*linesw[tp->t_line].l_start)(tp);
1021 CLR(tp->t_state, TS_BUSY);
1027 ucomstartread(struct ucom_softc *sc)
1031 DPRINTF(("ucomstartread: start\n"));
1033 sc->sc_state &= ~UCS_RXSTOP;
1035 if (sc->sc_bulkin_pipe == NULL)
1036 return (USBD_NORMAL_COMPLETION);
1038 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1039 (usbd_private_handle)sc,
1040 sc->sc_ibuf, sc->sc_ibufsize,
1041 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1042 USBD_NO_TIMEOUT, ucomreadcb);
1044 err = usbd_transfer(sc->sc_ixfer);
1045 if (err != USBD_IN_PROGRESS) {
1046 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1050 return (USBD_NORMAL_COMPLETION);
1054 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1056 struct ucom_softc *sc = (struct ucom_softc *)p;
1057 struct tty *tp = sc->sc_tty;
1058 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1064 DPRINTF(("ucomreadcb: status = %d\n", status));
1066 if (status != USBD_NORMAL_COMPLETION) {
1067 if (!(sc->sc_state & UCS_RXSTOP))
1068 printf("%s: ucomreadcb: %s\n",
1069 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1070 if (status == USBD_STALLED)
1071 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1072 /* XXX we should restart after some delay. */
1076 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1077 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1081 if (sc->sc_callback->ucom_read != NULL) {
1082 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1086 if (cc > sc->sc_ibufsize) {
1087 printf("%s: invalid receive data size, %d chars\n",
1088 USBDEVNAME(sc->sc_dev), cc);
1095 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1096 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1097 && (sc->sc_state & UCS_RTS_IFLOW
1098 || tp->t_iflag & IXOFF)
1099 && !(tp->t_state & TS_TBLOCK))
1101 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1105 if (*cp == sc->hotchar)
1114 if (tp->t_state & TS_TTSTOP
1115 && (tp->t_iflag & IXANY
1116 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1117 tp->t_state &= ~TS_TTSTOP;
1118 tp->t_lflag &= ~FLUSHO;
1122 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1125 /* Give characters to tty layer. */
1127 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1128 if ((*rint)(*cp, tp) == -1) {
1129 /* XXX what should we do? */
1130 printf("%s: lost %d chars\n",
1131 USBDEVNAME(sc->sc_dev), cc);
1141 err = ucomstartread(sc);
1143 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1144 /* XXX what should we dow now? */
1147 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1148 && !(tp->t_state & TS_TBLOCK))
1149 ucomctl(sc, UMCR_RTS, DMBIS);
1153 ucom_cleanup(struct ucom_softc *sc)
1155 DPRINTF(("ucom_cleanup: closing pipes\n"));
1158 if (sc->sc_bulkin_pipe != NULL) {
1159 usbd_abort_pipe(sc->sc_bulkin_pipe);
1160 usbd_close_pipe(sc->sc_bulkin_pipe);
1161 sc->sc_bulkin_pipe = NULL;
1163 if (sc->sc_bulkout_pipe != NULL) {
1164 usbd_abort_pipe(sc->sc_bulkout_pipe);
1165 usbd_close_pipe(sc->sc_bulkout_pipe);
1166 sc->sc_bulkout_pipe = NULL;
1168 if (sc->sc_ixfer != NULL) {
1169 usbd_free_xfer(sc->sc_ixfer);
1170 sc->sc_ixfer = NULL;
1172 if (sc->sc_oxfer != NULL) {
1173 usbd_free_xfer(sc->sc_oxfer);
1174 sc->sc_oxfer = NULL;
1179 ucomstopread(struct ucom_softc *sc)
1183 DPRINTF(("ucomstopread: enter\n"));
1185 if (!(sc->sc_state & UCS_RXSTOP)) {
1186 sc->sc_state |= UCS_RXSTOP;
1187 if (sc->sc_bulkin_pipe == NULL) {
1188 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1191 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1193 DPRINTF(("ucomstopread: err = %s\n",
1198 DPRINTF(("ucomstopread: leave\n"));
1202 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1204 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1205 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1206 && (!(t->c_iflag & PARMRK)
1207 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1208 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1209 && linesw[tp->t_line].l_rint == ttyinput) {
1210 DPRINTF(("disc_optim: bypass l_rint\n"));
1211 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1213 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1214 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1216 sc->hotchar = linesw[tp->t_line].l_hotchar;