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.16 2004/06/06 18:58:09 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 defined(__FreeBSD__) && __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)
187 unit = device_get_unit(sc->sc_dev);
189 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
190 tp->t_oproc = ucomstart;
191 tp->t_param = ucomparam;
192 tp->t_stop = ucomstop;
194 DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
196 DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
198 cdevsw_add(&ucom_cdevsw, UCOMUNIT_MASK, unit);
199 dev = make_dev(&ucom_cdevsw, unit | UCOM_CALLOUT_MASK,
200 UID_UUCP, GID_DIALER, 0660,
208 ucom_detach(struct ucom_softc *sc)
210 struct tty *tp = sc->sc_tty;
214 DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
218 if (sc->sc_bulkin_pipe != NULL)
219 usbd_abort_pipe(sc->sc_bulkin_pipe);
220 if (sc->sc_bulkout_pipe != NULL)
221 usbd_abort_pipe(sc->sc_bulkout_pipe);
224 if (tp->t_state & TS_ISOPEN) {
225 device_printf(sc->sc_dev,
226 "still open, forcing close\n");
227 (*linesw[tp->t_line].l_close)(tp, 0);
234 DPRINTF(("ucom_detach: no tty\n"));
239 if (--sc->sc_refcnt >= 0) {
240 /* Wait for processes to go away. */
241 usb_detach_wait(USBDEV(sc->sc_dev));
245 unit = device_get_unit(sc->sc_dev);
246 cdevsw_remove(&ucom_cdevsw, UCOMUNIT_MASK, unit);
252 ucom_shutdown(struct ucom_softc *sc)
254 struct tty *tp = sc->sc_tty;
256 DPRINTF(("ucom_shutdown\n"));
258 * Hang up if necessary. Wait a bit, so the other side has time to
259 * notice even if we immediately open the port again.
261 if (ISSET(tp->t_cflag, HUPCL)) {
262 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
263 (void)tsleep(sc, 0, "ucomsd", hz);
268 ucomopen(dev_t dev, int flag, int mode, usb_proc_ptr td)
270 int unit = UCOMUNIT(dev);
271 struct ucom_softc *sc;
277 KKASSERT(td != NULL);
279 USB_GET_SC_OPEN(ucom, unit, sc);
286 DPRINTF(("%s: ucomopen: tp = %p\n", USBDEVNAME(sc->sc_dev), tp));
288 if (ISSET(tp->t_state, TS_ISOPEN) &&
289 ISSET(tp->t_state, TS_XCLUDE) &&
296 * Do the following iff this is a first open.
299 while (sc->sc_opening)
300 tsleep(&sc->sc_opening, 0, "ucomop", 0);
303 if (!ISSET(tp->t_state, TS_ISOPEN)) {
307 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
309 tp->t_dev = reference_dev(dev);
312 * Initialize the termios status to the defaults. Add in the
313 * sticky bits from TIOCSFLAGS.
316 t.c_ospeed = TTYDEF_SPEED;
317 t.c_cflag = TTYDEF_CFLAG;
318 /* Make sure ucomparam() will do something. */
320 (void)ucomparam(tp, &t);
321 tp->t_iflag = TTYDEF_IFLAG;
322 tp->t_oflag = TTYDEF_OFLAG;
323 tp->t_lflag = TTYDEF_LFLAG;
328 * Turn on DTR. We must always do this, even if carrier is not
329 * present, because otherwise we'd have to use TIOCSDTR
330 * immediately after setting CLOCAL, which applications do not
331 * expect. We always assert DTR while the device is open
332 * unless explicitly requested to deassert it.
334 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
336 /* Device specific open */
337 if (sc->sc_callback->ucom_open != NULL) {
338 error = sc->sc_callback->ucom_open(sc->sc_parent,
343 wakeup(&sc->sc_opening);
349 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
350 sc->sc_bulkin_no, sc->sc_bulkout_no));
352 /* Open the bulk pipes */
354 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
355 &sc->sc_bulkin_pipe);
357 printf("%s: open bulk in error (addr %d): %s\n",
358 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
364 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
365 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
367 printf("%s: open bulk out error (addr %d): %s\n",
368 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
374 /* Allocate a request and an input buffer and start reading. */
375 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
376 if (sc->sc_ixfer == NULL) {
381 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
383 if (sc->sc_ibuf == NULL) {
388 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
389 if (sc->sc_oxfer == NULL) {
394 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
397 if (sc->sc_obuf == NULL) {
403 * Handle initial DCD.
405 if (ISSET(sc->sc_msr, UMSR_DCD) ||
406 (minor(dev) & UCOM_CALLOUT_MASK))
407 (*linesw[tp->t_line].l_modem)(tp, 1);
413 wakeup(&sc->sc_opening);
416 error = ttyopen(dev, tp);
420 error = (*linesw[tp->t_line].l_open)(dev, tp);
424 disc_optim(tp, &tp->t_termios, sc);
426 DPRINTF(("%s: ucomopen: success\n", USBDEVNAME(sc->sc_dev)));
434 usbd_free_xfer(sc->sc_oxfer);
437 usbd_free_xfer(sc->sc_ixfer);
440 usbd_close_pipe(sc->sc_bulkout_pipe);
441 sc->sc_bulkout_pipe = NULL;
443 usbd_close_pipe(sc->sc_bulkin_pipe);
444 sc->sc_bulkin_pipe = NULL;
447 wakeup(&sc->sc_opening);
452 if (!ISSET(tp->t_state, TS_ISOPEN)) {
454 * We failed to open the device, and nobody else had it opened.
455 * Clean up the state as appropriate.
460 DPRINTF(("%s: ucomopen: failed\n", USBDEVNAME(sc->sc_dev)));
466 ucomclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
468 struct ucom_softc *sc;
472 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
476 DPRINTF(("%s: ucomclose: unit = %d\n",
477 USBDEVNAME(sc->sc_dev), UCOMUNIT(dev)));
479 if (!ISSET(tp->t_state, TS_ISOPEN))
483 (*linesw[tp->t_line].l_close)(tp, flag);
484 disc_optim(tp, &tp->t_termios, sc);
491 if (!ISSET(tp->t_state, TS_ISOPEN)) {
493 * Although we got a last close, the device may still be in
494 * use; e.g. if this was the dialout node, and there are still
495 * processes waiting for carrier on the non-dialout node.
500 if (sc->sc_callback->ucom_close != NULL)
501 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
505 release_dev(tp->t_dev);
509 if (--sc->sc_refcnt < 0)
510 usb_detach_wakeup(USBDEV(sc->sc_dev));
516 ucomread(dev_t dev, struct uio *uio, int flag)
518 struct ucom_softc *sc;
522 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
525 DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, flag));
530 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
532 DPRINTF(("ucomread: error = %d\n", error));
538 ucomwrite(dev_t dev, struct uio *uio, int flag)
540 struct ucom_softc *sc;
544 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
547 DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, flag));
552 error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
554 DPRINTF(("ucomwrite: error = %d\n", error));
560 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
562 struct ucom_softc *sc;
568 USB_GET_SC(ucom, UCOMUNIT(dev), sc);
574 DPRINTF(("ucomioctl: cmd = 0x%08lx\n", cmd));
576 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
577 if (error != ENOIOCTL) {
578 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
584 error = ttioctl(tp, cmd, data, flag);
585 disc_optim(tp, &tp->t_termios, sc);
586 if (error != ENOIOCTL) {
588 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
592 if (sc->sc_callback->ucom_ioctl != NULL) {
594 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
603 DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", cmd));
607 DPRINTF(("ucomioctl: TIOCSBRK\n"));
611 DPRINTF(("ucomioctl: TIOCCBRK\n"));
616 DPRINTF(("ucomioctl: TIOCSDTR\n"));
617 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
620 DPRINTF(("ucomioctl: TIOCCDTR\n"));
621 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
626 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
627 (void)ucomctl(sc, d, DMSET);
631 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
632 (void)ucomctl(sc, d, DMBIS);
636 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
637 (void)ucomctl(sc, d, DMBIC);
640 d = ucomctl(sc, 0, DMGET);
641 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
646 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", cmd));
657 ucomctl(struct ucom_softc *sc, int bits, int how)
663 DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
666 SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */
667 DPRINTF(("ucomctl: DMGET: LE"));
670 if (ISSET(mcr, UMCR_DTR)) {
671 SET(bits, TIOCM_DTR);
674 if (ISSET(mcr, UMCR_RTS)) {
675 SET(bits, TIOCM_RTS);
680 if (ISSET(msr, UMSR_CTS)) {
681 SET(bits, TIOCM_CTS);
684 if (ISSET(msr, UMSR_DCD)) {
688 if (ISSET(msr, UMSR_DSR)) {
689 SET(bits, TIOCM_DSR);
692 if (ISSET(msr, UMSR_RI)) {
703 if (ISSET(bits, TIOCM_DTR))
705 if (ISSET(bits, TIOCM_RTS))
720 onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
723 onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
730 ucom_break(struct ucom_softc *sc, int onoff)
732 DPRINTF(("ucom_break: onoff = %d\n", onoff));
734 if (sc->sc_callback->ucom_set == NULL)
736 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
737 UCOM_SET_BREAK, onoff);
741 ucom_dtr(struct ucom_softc *sc, int onoff)
743 DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
745 if (sc->sc_callback->ucom_set == NULL)
747 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
748 UCOM_SET_DTR, onoff);
752 ucom_rts(struct ucom_softc *sc, int onoff)
754 DPRINTF(("ucom_rts: onoff = %d\n", onoff));
756 if (sc->sc_callback->ucom_set == NULL)
758 sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
759 UCOM_SET_RTS, onoff);
763 ucom_status_change(struct ucom_softc *sc)
765 struct tty *tp = sc->sc_tty;
769 if (sc->sc_callback->ucom_get_status == NULL) {
775 old_msr = sc->sc_msr;
776 sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
777 &sc->sc_lsr, &sc->sc_msr);
778 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
779 if (sc->sc_poll == 0)
781 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
782 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
783 (*linesw[tp->t_line].l_modem)(tp, onoff);
788 ucomparam(struct tty *tp, struct termios *t)
790 struct ucom_softc *sc;
794 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
799 DPRINTF(("ucomparam: sc = %p\n", sc));
801 /* Check requested parameters. */
802 if (t->c_ospeed < 0) {
803 DPRINTF(("ucomparam: negative ospeed\n"));
806 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
807 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
812 * If there were no changes, don't do anything. This avoids dropping
813 * input and improves performance when all we did was frob things like
816 if (tp->t_ospeed == t->c_ospeed &&
817 tp->t_cflag == t->c_cflag)
820 /* And copy to tty. */
822 tp->t_ospeed = t->c_ospeed;
823 tp->t_cflag = t->c_cflag;
825 if (sc->sc_callback->ucom_param == NULL)
830 error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
832 DPRINTF(("ucomparam: callback: error = %d\n", error));
838 if (t->c_cflag & CRTS_IFLOW) {
839 sc->sc_state |= UCS_RTS_IFLOW;
840 } else if (sc->sc_state & UCS_RTS_IFLOW) {
841 sc->sc_state &= ~UCS_RTS_IFLOW;
842 (void)ucomctl(sc, UMCR_RTS, DMBIS);
845 disc_optim(tp, t, sc);
847 uerr = ucomstartread(sc);
848 if (uerr != USBD_NORMAL_COMPLETION)
855 ucomstart(struct tty *tp)
857 struct ucom_softc *sc;
864 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
865 DPRINTF(("ucomstart: sc = %p\n", sc));
872 if (tp->t_state & TS_TBLOCK) {
873 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
874 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
875 DPRINTF(("ucomstart: clear RTS\n"));
876 (void)ucomctl(sc, UMCR_RTS, DMBIC);
879 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
880 tp->t_rawq.c_cc <= tp->t_ilowat &&
881 ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
882 DPRINTF(("ucomstart: set RTS\n"));
883 (void)ucomctl(sc, UMCR_RTS, DMBIS);
887 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
889 DPRINTF(("ucomstart: stopped\n"));
893 if (tp->t_outq.c_cc <= tp->t_olowat) {
894 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
895 CLR(tp->t_state, TS_SO_OLOWAT);
896 wakeup(TSA_OLOWAT(tp));
898 selwakeuppri(&tp->t_wsel, TTIPRI);
899 if (tp->t_outq.c_cc == 0) {
900 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
901 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
902 CLR(tp->t_state, TS_SO_OCOMPLETE);
903 wakeup(TSA_OCOMPLETE(tp));
909 /* Grab the first contiguous region of buffer space. */
910 data = tp->t_outq.c_cf;
911 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
912 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
915 DPRINTF(("ucomstart: cnt == 0\n"));
919 SET(tp->t_state, TS_BUSY);
921 if (cnt > sc->sc_obufsize) {
922 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
923 cnt = sc->sc_obufsize;
925 if (sc->sc_callback->ucom_write != NULL)
926 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
927 sc->sc_obuf, data, &cnt);
929 memcpy(sc->sc_obuf, data, cnt);
931 DPRINTF(("ucomstart: %d chars\n", cnt));
932 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
933 (usbd_private_handle)sc, sc->sc_obuf, cnt,
934 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
935 /* What can we do on error? */
936 err = usbd_transfer(sc->sc_oxfer);
937 if (err != USBD_IN_PROGRESS)
938 printf("ucomstart: err=%s\n", usbd_errstr(err));
947 ucomstop(struct tty *tp, int flag)
949 struct ucom_softc *sc;
952 USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
954 DPRINTF(("ucomstop: %d\n", flag));
957 DPRINTF(("ucomstop: read\n"));
962 DPRINTF(("ucomstop: write\n"));
964 if (ISSET(tp->t_state, TS_BUSY)) {
966 if (!ISSET(tp->t_state, TS_TTSTOP))
967 SET(tp->t_state, TS_FLUSH);
972 DPRINTF(("ucomstop: done\n"));
976 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
978 struct ucom_softc *sc = (struct ucom_softc *)p;
979 struct tty *tp = sc->sc_tty;
983 DPRINTF(("ucomwritecb: status = %d\n", status));
985 if (status == USBD_CANCELLED || sc->sc_dying)
988 if (status != USBD_NORMAL_COMPLETION) {
989 printf("%s: ucomwritecb: %s\n",
990 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
991 if (status == USBD_STALLED)
992 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
993 /* XXX we should restart after some delay. */
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;
1065 DPRINTF(("ucomreadcb: status = %d\n", status));
1067 if (status != USBD_NORMAL_COMPLETION) {
1068 if (!(sc->sc_state & UCS_RXSTOP))
1069 printf("%s: ucomreadcb: %s\n",
1070 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1071 if (status == USBD_STALLED)
1072 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1073 /* XXX we should restart after some delay. */
1077 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1078 DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1082 if (sc->sc_callback->ucom_read != NULL) {
1083 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1087 if (cc > sc->sc_ibufsize) {
1088 printf("%s: invalid receive data size, %d chars\n",
1089 USBDEVNAME(sc->sc_dev), cc);
1096 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1097 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1098 && (sc->sc_state & UCS_RTS_IFLOW
1099 || tp->t_iflag & IXOFF)
1100 && !(tp->t_state & TS_TBLOCK))
1102 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1105 if (tp->t_state & TS_TTSTOP
1106 && (tp->t_iflag & IXANY
1107 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1108 tp->t_state &= ~TS_TTSTOP;
1109 tp->t_lflag &= ~FLUSHO;
1113 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1116 /* Give characters to tty layer. */
1118 DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1119 if ((*rint)(*cp, tp) == -1) {
1120 /* XXX what should we do? */
1121 printf("%s: lost %d chars\n",
1122 USBDEVNAME(sc->sc_dev), cc);
1132 err = ucomstartread(sc);
1134 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1135 /* XXX what should we dow now? */
1138 if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1139 && !(tp->t_state & TS_TBLOCK))
1140 ucomctl(sc, UMCR_RTS, DMBIS);
1144 ucom_cleanup(struct ucom_softc *sc)
1146 DPRINTF(("ucom_cleanup: closing pipes\n"));
1149 if (sc->sc_bulkin_pipe != NULL) {
1150 usbd_abort_pipe(sc->sc_bulkin_pipe);
1151 usbd_close_pipe(sc->sc_bulkin_pipe);
1152 sc->sc_bulkin_pipe = NULL;
1154 if (sc->sc_bulkout_pipe != NULL) {
1155 usbd_abort_pipe(sc->sc_bulkout_pipe);
1156 usbd_close_pipe(sc->sc_bulkout_pipe);
1157 sc->sc_bulkout_pipe = NULL;
1159 if (sc->sc_ixfer != NULL) {
1160 usbd_free_xfer(sc->sc_ixfer);
1161 sc->sc_ixfer = NULL;
1163 if (sc->sc_oxfer != NULL) {
1164 usbd_free_xfer(sc->sc_oxfer);
1165 sc->sc_oxfer = NULL;
1170 ucomstopread(struct ucom_softc *sc)
1174 DPRINTF(("ucomstopread: enter\n"));
1176 if (!(sc->sc_state & UCS_RXSTOP)) {
1177 sc->sc_state |= UCS_RXSTOP;
1178 if (sc->sc_bulkin_pipe == NULL) {
1179 DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1182 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1184 DPRINTF(("ucomstopread: err = %s\n",
1189 DPRINTF(("ucomstopread: leave\n"));
1193 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1195 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1196 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1197 && (!(t->c_iflag & PARMRK)
1198 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1199 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1200 && linesw[tp->t_line].l_rint == ttyinput) {
1201 DPRINTF(("disc_optim: bypass l_rint\n"));
1202 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1204 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1205 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1207 sc->hotchar = linesw[tp->t_line].l_hotchar;