Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / sys / dev / usbmisc / ucom / ucom.c
1 /*
2  * (MPSAFE)
3  *
4  * $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $
5  * $NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $
6  * $FreeBSD: src/sys/dev/usb/ucom.c,v 1.35 2003/11/16 11:58:21 akiyama Exp $
7  * $DragonFly: src/sys/dev/usbmisc/ucom/ucom.c,v 1.32 2007/11/05 19:09:43 hasso Exp $
8  */
9 /*-
10  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to The NetBSD Foundation
40  * by Lennart Augustsson (lennart@augustsson.net) at
41  * Carlstedt Research & Technology.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *        This product includes software developed by the NetBSD
54  *        Foundation, Inc. and its contributors.
55  * 4. Neither the name of The NetBSD Foundation nor the names of its
56  *    contributors may be used to endorse or promote products derived
57  *    from this software without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69  * POSSIBILITY OF SUCH DAMAGE.
70  */
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/bus.h>
77 #include <sys/fcntl.h>
78 #include <sys/conf.h>
79 #include <sys/tty.h>
80 #include <sys/clist.h>
81 #include <sys/file.h>
82 #include <sys/proc.h>
83 #include <sys/priv.h>
84 #include <sys/sysctl.h>
85 #include <sys/thread2.h>
86
87 #include <bus/usb/usb.h>
88 #include <bus/usb/usbcdc.h>
89
90 #include <bus/usb/usbdi.h>
91 #include <bus/usb/usbdi_util.h>
92 #include <bus/usb/usb_quirks.h>
93
94 #include "ucomvar.h"
95
96 #ifdef USB_DEBUG
97 static int      ucomdebug = 0;
98 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
99 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
100            &ucomdebug, 0, "ucom debug level");
101 #define DPRINTF(x)      do { \
102                                 if (ucomdebug) \
103                                         kprintf x; \
104                         } while (0)
105
106 #define DPRINTFN(n, x)  do { \
107                                 if (ucomdebug > (n)) \
108                                         kprintf x; \
109                         } while (0)
110 #else
111 #define DPRINTF(x)
112 #define DPRINTFN(n, x)
113 #endif
114
115 static d_open_t  ucomopen;
116 static d_close_t ucomclose;
117 static d_read_t  ucomread;
118 static d_write_t ucomwrite;
119 static d_ioctl_t ucomioctl;
120
121 #define UCOM_CDEV_MAJOR  138
122
123 static struct dev_ops ucom_ops = {
124         { "ucom", UCOM_CDEV_MAJOR, D_TTY },
125         .d_open =       ucomopen,
126         .d_close =      ucomclose,
127         .d_read =       ucomread,
128         .d_write =      ucomwrite,
129         .d_ioctl =      ucomioctl,
130         .d_kqfilter =   ttykqfilter,
131         .d_revoke =     ttyrevoke
132 };
133
134 static void ucom_cleanup(struct ucom_softc *);
135 static int ucomctl(struct ucom_softc *, int, int);
136 static int ucomparam(struct tty *, struct termios *);
137 static void ucomstart(struct tty *);
138 static void ucomstop(struct tty *, int);
139 static void ucom_shutdown(struct ucom_softc *);
140 static void ucom_dtr(struct ucom_softc *, int);
141 static void ucom_rts(struct ucom_softc *, int);
142 static void ucom_break(struct ucom_softc *, int);
143 static usbd_status ucomstartread(struct ucom_softc *);
144 static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
145 static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
146 static void ucomstopread(struct ucom_softc *);
147 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
148
149 devclass_t ucom_devclass;
150
151 static moduledata_t ucom_mod = {
152         "ucom",
153         NULL,
154         NULL
155 };
156
157 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
158 MODULE_DEPEND(ucom, usb, 1, 1, 1);
159 MODULE_VERSION(ucom, UCOM_MODVER);
160
161 int
162 ucom_attach(struct ucom_softc *sc)
163 {
164         struct tty *tp;
165         int unit;
166         cdev_t dev;
167
168         lwkt_gettoken(&tty_token);
169         unit = device_get_unit(sc->sc_dev);
170
171         sc->sc_tty = tp = ttymalloc(sc->sc_tty);
172         tp->t_oproc = ucomstart;
173         tp->t_param = ucomparam;
174         tp->t_stop = ucomstop;
175
176         DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp));
177
178         DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit));
179
180         dev = make_dev(&ucom_ops, unit | UCOM_CALLOUT_MASK,
181                         UID_UUCP, GID_DIALER, 0660,
182                         "ucom%d", unit);
183         dev->si_tty = tp;
184         lwkt_reltoken(&tty_token);
185
186         return (0);
187 }
188
189 int
190 ucom_detach(struct ucom_softc *sc)
191 {
192         struct tty *tp = sc->sc_tty;
193         int unit;
194
195         DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
196
197         lwkt_gettoken(&tty_token);
198         sc->sc_dying = 1;
199
200         if (sc->sc_bulkin_pipe != NULL)
201                 usbd_abort_pipe(sc->sc_bulkin_pipe);
202         if (sc->sc_bulkout_pipe != NULL)
203                 usbd_abort_pipe(sc->sc_bulkout_pipe);
204
205         if (tp != NULL) {
206                 if (tp->t_state & TS_ISOPEN) {
207                         device_printf(sc->sc_dev,
208                                       "still open, forcing close\n");
209                         (*linesw[tp->t_line].l_close)(tp, 0);
210                         ttyclose(tp);
211                 }
212         } else {
213                 DPRINTF(("ucom_detach: no tty\n"));
214                 lwkt_reltoken(&tty_token);
215                 return (0);
216         }
217
218         crit_enter();
219         if (--sc->sc_refcnt >= 0) {
220                 /* Wait for processes to go away. */
221                 usb_detach_wait(sc->sc_dev);
222         }
223         crit_exit();
224
225         unit = device_get_unit(sc->sc_dev);
226         dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit);
227
228         lwkt_reltoken(&tty_token);
229         return (0);
230 }
231
232 static void
233 ucom_shutdown(struct ucom_softc *sc)
234 {
235         struct tty *tp = sc->sc_tty;
236
237         lwkt_gettoken(&tty_token);
238         DPRINTF(("ucom_shutdown\n"));
239         /*
240          * Hang up if necessary.  Wait a bit, so the other side has time to
241          * notice even if we immediately open the port again.
242          */
243         if (ISSET(tp->t_cflag, HUPCL)) {
244                 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
245                 (void)tsleep(sc, 0, "ucomsd", hz);
246         }
247         lwkt_reltoken(&tty_token);
248 }
249
250 static int
251 ucomopen(struct dev_open_args *ap)
252 {
253         cdev_t dev = ap->a_head.a_dev;
254         int unit = UCOMUNIT(dev);
255         struct ucom_softc *sc;
256         usbd_status err;
257         struct tty *tp;
258         int error;
259
260         sc = devclass_get_softc(ucom_devclass, unit);
261         if (sc == NULL)
262                 return (ENXIO);
263
264         if (sc->sc_dying)
265                 return (ENXIO);
266
267         lwkt_gettoken(&tty_token);
268         tp = sc->sc_tty;
269
270         DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
271
272         if (ISSET(tp->t_state, TS_ISOPEN) &&
273             ISSET(tp->t_state, TS_XCLUDE) &&
274             priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
275         ) {
276                 lwkt_reltoken(&tty_token);
277                 return (EBUSY);
278         }
279
280         /*
281          * Do the following iff this is a first open.
282          */
283         crit_enter();
284         while (sc->sc_opening)
285                 tsleep(&sc->sc_opening, 0, "ucomop", 0);
286         sc->sc_opening = 1;
287
288         if (!ISSET(tp->t_state, TS_ISOPEN)) {
289                 struct termios t;
290
291                 sc->sc_poll = 0;
292                 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
293
294                 tp->t_dev = reference_dev(dev);
295
296                 /*
297                  * Initialize the termios status to the defaults.  Add in the
298                  * sticky bits from TIOCSFLAGS.
299                  */
300                 t.c_ispeed = 0;
301                 t.c_ospeed = TTYDEF_SPEED;
302                 t.c_cflag = TTYDEF_CFLAG;
303                 /* Make sure ucomparam() will do something. */
304                 tp->t_ospeed = 0;
305                 (void)ucomparam(tp, &t);
306                 tp->t_iflag = TTYDEF_IFLAG;
307                 tp->t_oflag = TTYDEF_OFLAG;
308                 tp->t_lflag = TTYDEF_LFLAG;
309                 ttychars(tp);
310                 ttsetwater(tp);
311
312                 /*
313                  * Turn on DTR.  We must always do this, even if carrier is not
314                  * present, because otherwise we'd have to use TIOCSDTR
315                  * immediately after setting CLOCAL, which applications do not
316                  * expect.  We always assert DTR while the device is open
317                  * unless explicitly requested to deassert it.
318                  */
319                 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
320
321                 /* Device specific open */
322                 if (sc->sc_callback->ucom_open != NULL) {
323                         error = sc->sc_callback->ucom_open(sc->sc_parent,
324                                                            sc->sc_portno);
325                         if (error) {
326                                 ucom_cleanup(sc);
327                                 sc->sc_opening = 0;
328                                 wakeup(&sc->sc_opening);
329                                 crit_exit();
330                                 lwkt_reltoken(&tty_token);
331                                 return (error);
332                         }
333                 }
334
335                 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
336                          sc->sc_bulkin_no, sc->sc_bulkout_no));
337
338                 /* Open the bulk pipes */
339                 /* Bulk-in pipe */
340                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
341                                      &sc->sc_bulkin_pipe);
342                 if (err) {
343                         kprintf("%s: open bulk in error (addr %d): %s\n",
344                                device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
345                                usbd_errstr(err));
346                         error = EIO;
347                         goto fail_0;
348                 }
349                 /* Bulk-out pipe */
350                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
351                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
352                 if (err) {
353                         kprintf("%s: open bulk out error (addr %d): %s\n",
354                                device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
355                                usbd_errstr(err));
356                         error = EIO;
357                         goto fail_1;
358                 }
359
360                 /* Allocate a request and an input buffer and start reading. */
361                 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
362                 if (sc->sc_ixfer == NULL) {
363                         error = ENOMEM;
364                         goto fail_2;
365                 }
366
367                 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
368                                                 sc->sc_ibufsizepad);
369                 if (sc->sc_ibuf == NULL) {
370                         error = ENOMEM;
371                         goto fail_3;
372                 }
373
374                 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
375                 if (sc->sc_oxfer == NULL) {
376                         error = ENOMEM;
377                         goto fail_3;
378                 }
379
380                 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
381                                                 sc->sc_obufsize +
382                                                 sc->sc_opkthdrlen);
383                 if (sc->sc_obuf == NULL) {
384                         error = ENOMEM;
385                         goto fail_4;
386                 }
387
388                 /*
389                  * Handle initial DCD.
390                  */
391                 if (ISSET(sc->sc_msr, UMSR_DCD) ||
392                     (minor(dev) & UCOM_CALLOUT_MASK))
393                         (*linesw[tp->t_line].l_modem)(tp, 1);
394
395                 ucomstartread(sc);
396         }
397
398         sc->sc_opening = 0;
399         wakeup(&sc->sc_opening);
400         crit_exit();
401
402         error = ttyopen(dev, tp);
403         if (error)
404                 goto bad;
405
406         error = (*linesw[tp->t_line].l_open)(dev, tp);
407         if (error)
408                 goto bad;
409
410         disc_optim(tp, &tp->t_termios, sc);
411
412         DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
413
414         sc->sc_poll = 1;
415         sc->sc_refcnt++;
416
417         lwkt_reltoken(&tty_token);
418         return (0);
419
420 fail_4:
421         usbd_free_xfer(sc->sc_oxfer);
422         sc->sc_oxfer = NULL;
423 fail_3:
424         usbd_free_xfer(sc->sc_ixfer);
425         sc->sc_ixfer = NULL;
426 fail_2:
427         usbd_close_pipe(sc->sc_bulkout_pipe);
428         sc->sc_bulkout_pipe = NULL;
429 fail_1:
430         usbd_close_pipe(sc->sc_bulkin_pipe);
431         sc->sc_bulkin_pipe = NULL;
432 fail_0:
433         sc->sc_opening = 0;
434         wakeup(&sc->sc_opening);
435         crit_exit();
436         lwkt_reltoken(&tty_token);
437         return (error);
438
439 bad:
440         if (!ISSET(tp->t_state, TS_ISOPEN)) {
441                 /*
442                  * We failed to open the device, and nobody else had it opened.
443                  * Clean up the state as appropriate.
444                  */
445                 ucom_cleanup(sc);
446         }
447
448         DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
449
450         lwkt_reltoken(&tty_token);
451         return (error);
452 }
453
454 static int
455 ucomclose(struct dev_close_args *ap)
456 {
457         cdev_t dev = ap->a_head.a_dev;
458         struct ucom_softc *sc;
459         struct tty *tp;
460
461         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
462
463         lwkt_gettoken(&tty_token);
464         tp = sc->sc_tty;
465
466         DPRINTF(("%s: ucomclose: unit = %d\n",
467                 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
468
469         if (!ISSET(tp->t_state, TS_ISOPEN))
470                 goto quit;
471
472         crit_enter();
473         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
474         disc_optim(tp, &tp->t_termios, sc);
475         ttyclose(tp);
476         crit_exit();
477
478         if (sc->sc_dying)
479                 goto quit;
480
481         if (!ISSET(tp->t_state, TS_ISOPEN)) {
482                 /*
483                  * Although we got a last close, the device may still be in
484                  * use; e.g. if this was the dialout node, and there are still
485                  * processes waiting for carrier on the non-dialout node.
486                  */
487                 ucom_cleanup(sc);
488         }
489
490         if (sc->sc_callback->ucom_close != NULL)
491                 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
492
493 quit:
494         if (tp->t_dev) {
495                 release_dev(tp->t_dev);
496                 tp->t_dev = NULL;
497         }
498
499         if (--sc->sc_refcnt < 0)
500                 usb_detach_wakeup(sc->sc_dev);
501
502         lwkt_reltoken(&tty_token);
503         return (0);
504 }
505
506 static int
507 ucomread(struct dev_read_args *ap)
508 {
509         cdev_t dev = ap->a_head.a_dev;
510         struct ucom_softc *sc;
511         struct tty *tp;
512         int error;
513
514         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
515         lwkt_gettoken(&tty_token);
516         tp = sc->sc_tty;
517
518         DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
519
520         if (sc->sc_dying) {
521                 lwkt_reltoken(&tty_token);
522                 return (EIO);
523         }
524
525         error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
526
527         DPRINTF(("ucomread: error = %d\n", error));
528
529         lwkt_reltoken(&tty_token);
530         return (error);
531 }
532
533 static int
534 ucomwrite(struct dev_write_args *ap)
535 {
536         cdev_t dev = ap->a_head.a_dev;
537         struct ucom_softc *sc;
538         struct tty *tp;
539         int error;
540
541         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
542         lwkt_gettoken(&tty_token);
543         tp = sc->sc_tty;
544
545         DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
546
547         if (sc->sc_dying) {
548                 lwkt_reltoken(&tty_token);
549                 return (EIO);
550         }
551
552         error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
553
554         DPRINTF(("ucomwrite: error = %d\n", error));
555
556         lwkt_reltoken(&tty_token);
557         return (error);
558 }
559
560 static int
561 ucomioctl(struct dev_ioctl_args *ap)
562 {
563         cdev_t dev = ap->a_head.a_dev;
564         struct ucom_softc *sc;
565         struct tty *tp;
566         int error;
567         int d;
568
569         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
570         lwkt_gettoken(&tty_token);
571         tp = sc->sc_tty;
572
573         if (sc->sc_dying) {
574                 lwkt_reltoken(&tty_token);
575                 return (EIO);
576         }
577
578         DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
579
580         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
581                                               ap->a_fflag, ap->a_cred);
582         if (error != ENOIOCTL) {
583                 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
584                 lwkt_reltoken(&tty_token);
585                 return (error);
586         }
587
588         crit_enter();
589
590         error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
591         disc_optim(tp, &tp->t_termios, sc);
592         if (error != ENOIOCTL) {
593                 crit_exit();
594                 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
595                 lwkt_reltoken(&tty_token);
596                 return (error);
597         }
598
599         if (sc->sc_callback->ucom_ioctl != NULL) {
600                 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
601                                                     sc->sc_portno,
602                                                     ap->a_cmd, ap->a_data,
603                                                     ap->a_fflag, curthread);
604                 if (error >= 0) {
605                         crit_exit();
606                         lwkt_reltoken(&tty_token);
607                         return (error);
608                 }
609         }
610
611         error = 0;
612
613         DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
614
615         switch (ap->a_cmd) {
616         case TIOCSBRK:
617                 DPRINTF(("ucomioctl: TIOCSBRK\n"));
618                 ucom_break(sc, 1);
619                 break;
620         case TIOCCBRK:
621                 DPRINTF(("ucomioctl: TIOCCBRK\n"));
622                 ucom_break(sc, 0);
623                 break;
624
625         case TIOCSDTR:
626                 DPRINTF(("ucomioctl: TIOCSDTR\n"));
627                 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
628                 break;
629         case TIOCCDTR:
630                 DPRINTF(("ucomioctl: TIOCCDTR\n"));
631                 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
632                 break;
633
634         case TIOCMSET:
635                 d = *(int *)ap->a_data;
636                 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
637                 (void)ucomctl(sc, d, DMSET);
638                 break;
639         case TIOCMBIS:
640                 d = *(int *)ap->a_data;
641                 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
642                 (void)ucomctl(sc, d, DMBIS);
643                 break;
644         case TIOCMBIC:
645                 d = *(int *)ap->a_data;
646                 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
647                 (void)ucomctl(sc, d, DMBIC);
648                 break;
649         case TIOCMGET:
650                 d = ucomctl(sc, 0, DMGET);
651                 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
652                 *(int *)ap->a_data = d;
653                 break;
654
655         default:
656                 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
657                 error = ENOTTY;
658                 break;
659         }
660
661         crit_exit();
662
663         lwkt_reltoken(&tty_token);
664         return (error);
665 }
666
667 /*
668  * NOTE: Must be called with tty_token held.
669  */
670 static int
671 ucomctl(struct ucom_softc *sc, int bits, int how)
672 {
673         int     mcr;
674         int     msr;
675         int     onoff;
676
677         ASSERT_LWKT_TOKEN_HELD(&tty_token);
678         DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
679
680         if (how == DMGET) {
681                 SET(bits, TIOCM_LE);            /* always set TIOCM_LE bit */
682                 DPRINTF(("ucomctl: DMGET: LE"));
683
684                 mcr = sc->sc_mcr;
685                 if (ISSET(mcr, UMCR_DTR)) {
686                         SET(bits, TIOCM_DTR);
687                         DPRINTF((" DTR"));
688                 }
689                 if (ISSET(mcr, UMCR_RTS)) {
690                         SET(bits, TIOCM_RTS);
691                         DPRINTF((" RTS"));
692                 }
693
694                 msr = sc->sc_msr;
695                 if (ISSET(msr, UMSR_CTS)) {
696                         SET(bits, TIOCM_CTS);
697                         DPRINTF((" CTS"));
698                 }
699                 if (ISSET(msr, UMSR_DCD)) {
700                         SET(bits, TIOCM_CD);
701                         DPRINTF((" CD"));
702                 }
703                 if (ISSET(msr, UMSR_DSR)) {
704                         SET(bits, TIOCM_DSR);
705                         DPRINTF((" DSR"));
706                 }
707                 if (ISSET(msr, UMSR_RI)) {
708                         SET(bits, TIOCM_RI);
709                         DPRINTF((" RI"));
710                 }
711
712                 DPRINTF(("\n"));
713
714                 return (bits);
715         }
716
717         mcr = 0;
718         if (ISSET(bits, TIOCM_DTR))
719                 SET(mcr, UMCR_DTR);
720         if (ISSET(bits, TIOCM_RTS))
721                 SET(mcr, UMCR_RTS);
722
723         switch (how) {
724         case DMSET:
725                 sc->sc_mcr = mcr;
726                 break;
727         case DMBIS:
728                 sc->sc_mcr |= mcr;
729                 break;
730         case DMBIC:
731                 sc->sc_mcr &= ~mcr;
732                 break;
733         }
734
735         onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
736         ucom_dtr(sc, onoff);
737
738         onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
739         ucom_rts(sc, onoff);
740
741         return (0);
742 }
743
744 /*
745  * NOTE: Must be called with tty_token held.
746  */
747 static void
748 ucom_break(struct ucom_softc *sc, int onoff)
749 {
750         ASSERT_LWKT_TOKEN_HELD(&tty_token);
751         DPRINTF(("ucom_break: onoff = %d\n", onoff));
752
753         if (sc->sc_callback->ucom_set == NULL)
754                 return;
755         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
756                                   UCOM_SET_BREAK, onoff);
757 }
758
759 /*
760  * NOTE: Must be called with tty_token held.
761  */
762 static void
763 ucom_dtr(struct ucom_softc *sc, int onoff)
764 {
765         ASSERT_LWKT_TOKEN_HELD(&tty_token);
766         DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
767
768         if (sc->sc_callback->ucom_set == NULL)
769                 return;
770         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
771                                   UCOM_SET_DTR, onoff);
772 }
773
774 /*
775  * NOTE: Must be called with tty_token held.
776  */
777 static void
778 ucom_rts(struct ucom_softc *sc, int onoff)
779 {
780         ASSERT_LWKT_TOKEN_HELD(&tty_token);
781         DPRINTF(("ucom_rts: onoff = %d\n", onoff));
782
783         if (sc->sc_callback->ucom_set == NULL)
784                 return;
785         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
786                                   UCOM_SET_RTS, onoff);
787 }
788
789 void
790 ucom_status_change(struct ucom_softc *sc)
791 {
792         struct tty *tp = sc->sc_tty;
793         u_char old_msr;
794         int onoff;
795
796         lwkt_gettoken(&tty_token);
797         if (sc->sc_callback->ucom_get_status == NULL) {
798                 sc->sc_lsr = 0;
799                 sc->sc_msr = 0;
800                 lwkt_reltoken(&tty_token);
801                 return;
802         }
803
804         old_msr = sc->sc_msr;
805         sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
806                                          &sc->sc_lsr, &sc->sc_msr);
807         if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
808                 if (sc->sc_poll == 0) {
809                         lwkt_reltoken(&tty_token);
810                         return;
811                 }
812                 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
813                 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
814                 (*linesw[tp->t_line].l_modem)(tp, onoff);
815         }
816         lwkt_reltoken(&tty_token);
817 }
818
819 static int
820 ucomparam(struct tty *tp, struct termios *t)
821 {
822         struct ucom_softc *sc;
823         int error;
824         usbd_status uerr;
825
826         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
827
828         lwkt_gettoken(&tty_token);
829         if (sc->sc_dying) {
830                 lwkt_reltoken(&tty_token);
831                 return (EIO);
832         }
833
834         DPRINTF(("ucomparam: sc = %p\n", sc));
835
836         /* Check requested parameters. */
837         if (t->c_ospeed < 0) {
838                 DPRINTF(("ucomparam: negative ospeed\n"));
839                 lwkt_reltoken(&tty_token);
840                 return (EINVAL);
841         }
842         if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
843                 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
844                 lwkt_reltoken(&tty_token);
845                 return (EINVAL);
846         }
847
848         /*
849          * If there were no changes, don't do anything.  This avoids dropping
850          * input and improves performance when all we did was frob things like
851          * VMIN and VTIME.
852          */
853         if (tp->t_ospeed == t->c_ospeed &&
854             tp->t_cflag == t->c_cflag) {
855                 lwkt_reltoken(&tty_token);
856                 return (0);
857         }
858
859         /* And copy to tty. */
860         tp->t_ispeed = 0;
861         tp->t_ospeed = t->c_ospeed;
862         tp->t_cflag = t->c_cflag;
863
864         if (sc->sc_callback->ucom_param == NULL) {
865                 lwkt_reltoken(&tty_token);
866                 return (0);
867         }
868
869         ucomstopread(sc);
870
871         error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
872         if (error) {
873                 DPRINTF(("ucomparam: callback: error = %d\n", error));
874                 lwkt_reltoken(&tty_token);
875                 return (error);
876         }
877
878         ttsetwater(tp);
879
880         if (t->c_cflag & CRTS_IFLOW) {
881                 sc->sc_state |= UCS_RTS_IFLOW;
882         } else if (sc->sc_state & UCS_RTS_IFLOW) {
883                 sc->sc_state &= ~UCS_RTS_IFLOW;
884                 (void)ucomctl(sc, UMCR_RTS, DMBIS);
885         }
886
887         disc_optim(tp, t, sc);
888
889         uerr = ucomstartread(sc);
890         if (uerr != USBD_NORMAL_COMPLETION) {
891                 lwkt_reltoken(&tty_token);
892                 return (EIO);
893         }
894
895         lwkt_reltoken(&tty_token);
896         return (0);
897 }
898
899 static void
900 ucomstart(struct tty *tp)
901 {
902         struct ucom_softc *sc;
903         struct cblock *cbp;
904         usbd_status err;
905         u_char *data;
906         int cnt;
907
908         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
909         DPRINTF(("ucomstart: sc = %p\n", sc));
910
911         lwkt_gettoken(&tty_token);
912
913         if (sc->sc_dying) {
914                 lwkt_reltoken(&tty_token);
915                 return;
916         }
917
918         crit_enter();
919
920         if (tp->t_state & TS_TBLOCK) {
921                 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
922                     ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
923                         DPRINTF(("ucomstart: clear RTS\n"));
924                         (void)ucomctl(sc, UMCR_RTS, DMBIC);
925                 }
926         } else {
927                 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
928                     tp->t_rawq.c_cc <= tp->t_ilowat &&
929                     ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
930                         DPRINTF(("ucomstart: set RTS\n"));
931                         (void)ucomctl(sc, UMCR_RTS, DMBIS);
932                 }
933         }
934
935         if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
936                 ttwwakeup(tp);
937                 DPRINTF(("ucomstart: stopped\n"));
938                 goto out;
939         }
940
941         if (tp->t_outq.c_cc <= tp->t_olowat) {
942                 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
943                         CLR(tp->t_state, TS_SO_OLOWAT);
944                         wakeup(TSA_OLOWAT(tp));
945                 }
946                 KNOTE(&tp->t_wkq.ki_note, 0);
947                 if (tp->t_outq.c_cc == 0) {
948                         if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
949                             TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
950                                 CLR(tp->t_state, TS_SO_OCOMPLETE);
951                                 wakeup(TSA_OCOMPLETE(tp));
952                         }
953                         goto out;
954                 }
955         }
956
957         /* Grab the first contiguous region of buffer space. */
958         data = tp->t_outq.c_cf;
959         cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
960         cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
961
962         if (cnt == 0) {
963                 DPRINTF(("ucomstart: cnt == 0\n"));
964                 goto out;
965         }
966
967         SET(tp->t_state, TS_BUSY);
968
969         if (cnt > sc->sc_obufsize) {
970                 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
971                 cnt = sc->sc_obufsize;
972         }
973         if (sc->sc_callback->ucom_write != NULL)
974                 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
975                                             sc->sc_obuf, data, &cnt);
976         else
977                 memcpy(sc->sc_obuf, data, cnt);
978
979         DPRINTF(("ucomstart: %d chars\n", cnt));
980         usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
981                         (usbd_private_handle)sc, sc->sc_obuf, cnt,
982                         USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
983         /* What can we do on error? */
984         err = usbd_transfer(sc->sc_oxfer);
985         if (err != USBD_IN_PROGRESS)
986                 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
987
988         ttwwakeup(tp);
989
990     out:
991         crit_exit();
992         lwkt_reltoken(&tty_token);
993 }
994
995 static void
996 ucomstop(struct tty *tp, int flag)
997 {
998         struct ucom_softc *sc;
999
1000         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
1001
1002         DPRINTF(("ucomstop: %d\n", flag));
1003
1004         lwkt_gettoken(&tty_token);
1005         if (flag & FREAD) {
1006                 /*
1007                  * This is just supposed to flush pending receive data,
1008                  * not stop the reception of data entirely!
1009                  */
1010                 DPRINTF(("ucomstop: read\n"));
1011                 ucomstopread(sc);
1012                 ucomstartread(sc);
1013         }
1014
1015         if (flag & FWRITE) {
1016                 DPRINTF(("ucomstop: write\n"));
1017                 crit_enter();
1018                 if (ISSET(tp->t_state, TS_BUSY)) {
1019                         /* XXX do what? */
1020                         if (!ISSET(tp->t_state, TS_TTSTOP))
1021                                 SET(tp->t_state, TS_FLUSH);
1022                 }
1023                 crit_exit();
1024         }
1025
1026         DPRINTF(("ucomstop: done\n"));
1027         lwkt_reltoken(&tty_token);
1028 }
1029
1030 static void
1031 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1032 {
1033         struct ucom_softc *sc = (struct ucom_softc *)p;
1034         struct tty *tp = sc->sc_tty;
1035         u_int32_t cc;
1036
1037         lwkt_gettoken(&tty_token);
1038         DPRINTF(("ucomwritecb: status = %d\n", status));
1039
1040         if (status == USBD_CANCELLED || sc->sc_dying)
1041                 goto error;
1042
1043         if (status != USBD_NORMAL_COMPLETION) {
1044                 kprintf("%s: ucomwritecb: %s\n",
1045                        device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1046                 if (status == USBD_STALLED)
1047                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1048                 /*
1049                  * XXX.  We may need a flag to sequence ucomstopread() and
1050                  * ucomstartread() to handle the case where ucomstartread()
1051                  * is called after ucomstopread() but before the request has
1052                  * been properly canceled?
1053                  */
1054                 goto error;
1055         }
1056
1057         usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1058         DPRINTF(("ucomwritecb: cc = %d\n", cc));
1059         if (cc <= sc->sc_opkthdrlen) {
1060                 kprintf("%s: sent size too small, cc = %d\n",
1061                         device_get_nameunit(sc->sc_dev), cc);
1062                 goto error;
1063         }
1064
1065         /* convert from USB bytes to tty bytes */
1066         cc -= sc->sc_opkthdrlen;
1067
1068         crit_enter();
1069         CLR(tp->t_state, TS_BUSY);
1070         if (ISSET(tp->t_state, TS_FLUSH))
1071                 CLR(tp->t_state, TS_FLUSH);
1072         else
1073                 ndflush(&tp->t_outq, cc);
1074         (*linesw[tp->t_line].l_start)(tp);
1075         crit_exit();
1076
1077         lwkt_reltoken(&tty_token);
1078         return;
1079
1080   error:
1081         crit_enter();
1082         CLR(tp->t_state, TS_BUSY);
1083         crit_exit();
1084         lwkt_reltoken(&tty_token);
1085         return;
1086 }
1087
1088 /*
1089  * NOTE: Must be called with tty_token held
1090  */
1091 static usbd_status
1092 ucomstartread(struct ucom_softc *sc)
1093 {
1094         usbd_status err;
1095
1096         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1097         DPRINTF(("ucomstartread: start\n"));
1098
1099         sc->sc_state &= ~UCS_RXSTOP;
1100
1101         if (sc->sc_bulkin_pipe == NULL)
1102                 return (USBD_NORMAL_COMPLETION);
1103
1104         usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1105                         (usbd_private_handle)sc,
1106                         sc->sc_ibuf, sc->sc_ibufsize,
1107                         USBD_SHORT_XFER_OK | USBD_NO_COPY,
1108                         USBD_NO_TIMEOUT, ucomreadcb);
1109
1110         err = usbd_transfer(sc->sc_ixfer);
1111         if (err != USBD_IN_PROGRESS) {
1112                 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1113                 return (err);
1114         }
1115
1116         return (USBD_NORMAL_COMPLETION);
1117 }
1118
1119 static void
1120 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1121 {
1122         lwkt_gettoken(&tty_token);
1123
1124         struct ucom_softc *sc = (struct ucom_softc *)p;
1125         struct tty *tp = sc->sc_tty;
1126         int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1127         usbd_status err;
1128         u_int32_t cc;
1129         u_char *cp;
1130         int lostcc;
1131
1132         DPRINTF(("ucomreadcb: status = %d\n", status));
1133
1134         if (status != USBD_NORMAL_COMPLETION) {
1135                 if (!(sc->sc_state & UCS_RXSTOP))
1136                         kprintf("%s: ucomreadcb: %s\n",
1137                                device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1138                 if (status == USBD_STALLED)
1139                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1140                 /* XXX we should restart after some delay. */
1141                 lwkt_reltoken(&tty_token);
1142                 return;
1143         }
1144
1145         usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1146         DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1147         if (cc == 0)
1148                 goto resubmit;
1149
1150         if (sc->sc_callback->ucom_read != NULL) {
1151                 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1152                                            &cp, &cc);
1153         }
1154
1155         if (cc > sc->sc_ibufsize) {
1156                 kprintf("%s: invalid receive data size, %d chars\n",
1157                         device_get_nameunit(sc->sc_dev), cc);
1158                 goto resubmit;
1159         }
1160         if (cc < 1)
1161                 goto resubmit;
1162
1163         crit_enter();
1164         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1165                 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1166                     && (sc->sc_state & UCS_RTS_IFLOW
1167                         || tp->t_iflag & IXOFF)
1168                     && !(tp->t_state & TS_TBLOCK))
1169                         ttyblock(tp);
1170                 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1171                 tp->t_rawcc += cc;
1172                 if (sc->hotchar) {
1173                         while (cc) {
1174                                 if (*cp == sc->hotchar)
1175                                         break;
1176                                 --cc;
1177                                 ++cp;
1178                         }
1179                         if (cc)
1180                                 setsofttty();
1181                 }
1182                 ttwakeup(tp);
1183                 if (tp->t_state & TS_TTSTOP
1184                     && (tp->t_iflag & IXANY
1185                         || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1186                         tp->t_state &= ~TS_TTSTOP;
1187                         tp->t_lflag &= ~FLUSHO;
1188                         ucomstart(tp);
1189                 }
1190                 if (lostcc > 0)
1191                         kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1192                                lostcc);
1193         } else {
1194                 /* Give characters to tty layer. */
1195                 while (cc > 0) {
1196                         DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1197                         if ((*rint)(*cp, tp) == -1) {
1198                                 /* XXX what should we do? */
1199                                 kprintf("%s: lost %d chars\n",
1200                                        device_get_nameunit(sc->sc_dev), cc);
1201                                 break;
1202                         }
1203                         cc--;
1204                         cp++;
1205                 }
1206         }
1207         crit_exit();
1208
1209 resubmit:
1210         err = ucomstartread(sc);
1211         if (err) {
1212                 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1213                 /* XXX what should we dow now? */
1214         }
1215
1216         if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1217             && !(tp->t_state & TS_TBLOCK))
1218                 ucomctl(sc, UMCR_RTS, DMBIS);
1219
1220         lwkt_reltoken(&tty_token);
1221 }
1222
1223 /*
1224  * NOTE: Must be called with tty_token held.
1225  */
1226 static void
1227 ucom_cleanup(struct ucom_softc *sc)
1228 {
1229         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1230         DPRINTF(("ucom_cleanup: closing pipes\n"));
1231
1232         ucom_shutdown(sc);
1233         if (sc->sc_bulkin_pipe != NULL) {
1234                 usbd_abort_pipe(sc->sc_bulkin_pipe);
1235                 usbd_close_pipe(sc->sc_bulkin_pipe);
1236                 sc->sc_bulkin_pipe = NULL;
1237         }
1238         if (sc->sc_bulkout_pipe != NULL) {
1239                 usbd_abort_pipe(sc->sc_bulkout_pipe);
1240                 usbd_close_pipe(sc->sc_bulkout_pipe);
1241                 sc->sc_bulkout_pipe = NULL;
1242         }
1243         if (sc->sc_ixfer != NULL) {
1244                 usbd_free_xfer(sc->sc_ixfer);
1245                 sc->sc_ixfer = NULL;
1246         }
1247         if (sc->sc_oxfer != NULL) {
1248                 usbd_free_xfer(sc->sc_oxfer);
1249                 sc->sc_oxfer = NULL;
1250         }
1251 }
1252
1253 /*
1254  * NOTE: Must be called with tty_token held.
1255  */
1256 static void
1257 ucomstopread(struct ucom_softc *sc)
1258 {
1259         usbd_status err;
1260
1261         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1262         DPRINTF(("ucomstopread: enter\n"));
1263
1264         if (!(sc->sc_state & UCS_RXSTOP)) {
1265                 sc->sc_state |= UCS_RXSTOP;
1266                 if (sc->sc_bulkin_pipe == NULL) {
1267                         DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1268                         return;
1269                 }
1270                 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1271                 if (err) {
1272                         DPRINTF(("ucomstopread: err = %s\n",
1273                                  usbd_errstr(err)));
1274                 }
1275         }
1276
1277         DPRINTF(("ucomstopread: leave\n"));
1278 }
1279
1280 /*
1281  * NOTE: Must be called with tty_token held.
1282  */
1283 static void
1284 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1285 {
1286         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1287         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1288             && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1289             && (!(t->c_iflag & PARMRK)
1290                 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1291             && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1292             && linesw[tp->t_line].l_rint == ttyinput) {
1293                 DPRINTF(("disc_optim: bypass l_rint\n"));
1294                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1295         } else {
1296                 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1297                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1298         }
1299         sc->hotchar = linesw[tp->t_line].l_hotchar;
1300 }