ucom - destroy device node on detach
[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         sc->dev = dev;
185         lwkt_reltoken(&tty_token);
186
187         return (0);
188 }
189
190 int
191 ucom_detach(struct ucom_softc *sc)
192 {
193         struct tty *tp = sc->sc_tty;
194         int unit;
195
196         DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty));
197
198         destroy_dev(sc->dev);
199         lwkt_gettoken(&tty_token);
200         sc->sc_dying = 1;
201
202         if (sc->sc_bulkin_pipe != NULL)
203                 usbd_abort_pipe(sc->sc_bulkin_pipe);
204         if (sc->sc_bulkout_pipe != NULL)
205                 usbd_abort_pipe(sc->sc_bulkout_pipe);
206
207         if (tp != NULL) {
208                 if (tp->t_state & TS_ISOPEN) {
209                         device_printf(sc->sc_dev,
210                                       "still open, forcing close\n");
211                         (*linesw[tp->t_line].l_close)(tp, 0);
212                         ttyclose(tp);
213                 }
214         } else {
215                 DPRINTF(("ucom_detach: no tty\n"));
216                 lwkt_reltoken(&tty_token);
217                 return (0);
218         }
219
220         crit_enter();
221         if (--sc->sc_refcnt >= 0) {
222                 /* Wait for processes to go away. */
223                 usb_detach_wait(sc->sc_dev);
224         }
225         crit_exit();
226
227         unit = device_get_unit(sc->sc_dev);
228         dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit);
229
230         lwkt_reltoken(&tty_token);
231         return (0);
232 }
233
234 static void
235 ucom_shutdown(struct ucom_softc *sc)
236 {
237         struct tty *tp = sc->sc_tty;
238
239         lwkt_gettoken(&tty_token);
240         DPRINTF(("ucom_shutdown\n"));
241         /*
242          * Hang up if necessary.  Wait a bit, so the other side has time to
243          * notice even if we immediately open the port again.
244          */
245         if (ISSET(tp->t_cflag, HUPCL)) {
246                 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
247                 (void)tsleep(sc, 0, "ucomsd", hz);
248         }
249         lwkt_reltoken(&tty_token);
250 }
251
252 static int
253 ucomopen(struct dev_open_args *ap)
254 {
255         cdev_t dev = ap->a_head.a_dev;
256         int unit = UCOMUNIT(dev);
257         struct ucom_softc *sc;
258         usbd_status err;
259         struct tty *tp;
260         int error;
261
262         sc = devclass_get_softc(ucom_devclass, unit);
263         if (sc == NULL)
264                 return (ENXIO);
265
266         if (sc->sc_dying)
267                 return (ENXIO);
268
269         lwkt_gettoken(&tty_token);
270         tp = sc->sc_tty;
271
272         DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp));
273
274         if (ISSET(tp->t_state, TS_ISOPEN) &&
275             ISSET(tp->t_state, TS_XCLUDE) &&
276             priv_check_cred(ap->a_cred, PRIV_ROOT, 0)
277         ) {
278                 lwkt_reltoken(&tty_token);
279                 return (EBUSY);
280         }
281
282         /*
283          * Do the following iff this is a first open.
284          */
285         crit_enter();
286         while (sc->sc_opening)
287                 tsleep(&sc->sc_opening, 0, "ucomop", 0);
288         sc->sc_opening = 1;
289
290         if (!ISSET(tp->t_state, TS_ISOPEN)) {
291                 struct termios t;
292
293                 sc->sc_poll = 0;
294                 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
295
296                 tp->t_dev = reference_dev(dev);
297
298                 /*
299                  * Initialize the termios status to the defaults.  Add in the
300                  * sticky bits from TIOCSFLAGS.
301                  */
302                 t.c_ispeed = 0;
303                 t.c_ospeed = TTYDEF_SPEED;
304                 t.c_cflag = TTYDEF_CFLAG;
305                 /* Make sure ucomparam() will do something. */
306                 tp->t_ospeed = 0;
307                 (void)ucomparam(tp, &t);
308                 tp->t_iflag = TTYDEF_IFLAG;
309                 tp->t_oflag = TTYDEF_OFLAG;
310                 tp->t_lflag = TTYDEF_LFLAG;
311                 ttychars(tp);
312                 ttsetwater(tp);
313
314                 /*
315                  * Turn on DTR.  We must always do this, even if carrier is not
316                  * present, because otherwise we'd have to use TIOCSDTR
317                  * immediately after setting CLOCAL, which applications do not
318                  * expect.  We always assert DTR while the device is open
319                  * unless explicitly requested to deassert it.
320                  */
321                 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
322
323                 /* Device specific open */
324                 if (sc->sc_callback->ucom_open != NULL) {
325                         error = sc->sc_callback->ucom_open(sc->sc_parent,
326                                                            sc->sc_portno);
327                         if (error) {
328                                 ucom_cleanup(sc);
329                                 sc->sc_opening = 0;
330                                 wakeup(&sc->sc_opening);
331                                 crit_exit();
332                                 lwkt_reltoken(&tty_token);
333                                 return (error);
334                         }
335                 }
336
337                 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
338                          sc->sc_bulkin_no, sc->sc_bulkout_no));
339
340                 /* Open the bulk pipes */
341                 /* Bulk-in pipe */
342                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
343                                      &sc->sc_bulkin_pipe);
344                 if (err) {
345                         kprintf("%s: open bulk in error (addr %d): %s\n",
346                                device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no,
347                                usbd_errstr(err));
348                         error = EIO;
349                         goto fail_0;
350                 }
351                 /* Bulk-out pipe */
352                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
353                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
354                 if (err) {
355                         kprintf("%s: open bulk out error (addr %d): %s\n",
356                                device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no,
357                                usbd_errstr(err));
358                         error = EIO;
359                         goto fail_1;
360                 }
361
362                 /* Allocate a request and an input buffer and start reading. */
363                 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
364                 if (sc->sc_ixfer == NULL) {
365                         error = ENOMEM;
366                         goto fail_2;
367                 }
368
369                 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
370                                                 sc->sc_ibufsizepad);
371                 if (sc->sc_ibuf == NULL) {
372                         error = ENOMEM;
373                         goto fail_3;
374                 }
375
376                 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
377                 if (sc->sc_oxfer == NULL) {
378                         error = ENOMEM;
379                         goto fail_3;
380                 }
381
382                 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
383                                                 sc->sc_obufsize +
384                                                 sc->sc_opkthdrlen);
385                 if (sc->sc_obuf == NULL) {
386                         error = ENOMEM;
387                         goto fail_4;
388                 }
389
390                 /*
391                  * Handle initial DCD.
392                  */
393                 if (ISSET(sc->sc_msr, UMSR_DCD) ||
394                     (minor(dev) & UCOM_CALLOUT_MASK))
395                         (*linesw[tp->t_line].l_modem)(tp, 1);
396
397                 ucomstartread(sc);
398         }
399
400         sc->sc_opening = 0;
401         wakeup(&sc->sc_opening);
402         crit_exit();
403
404         error = ttyopen(dev, tp);
405         if (error)
406                 goto bad;
407
408         error = (*linesw[tp->t_line].l_open)(dev, tp);
409         if (error)
410                 goto bad;
411
412         disc_optim(tp, &tp->t_termios, sc);
413
414         DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev)));
415
416         sc->sc_poll = 1;
417         sc->sc_refcnt++;
418
419         lwkt_reltoken(&tty_token);
420         return (0);
421
422 fail_4:
423         usbd_free_xfer(sc->sc_oxfer);
424         sc->sc_oxfer = NULL;
425 fail_3:
426         usbd_free_xfer(sc->sc_ixfer);
427         sc->sc_ixfer = NULL;
428 fail_2:
429         usbd_close_pipe(sc->sc_bulkout_pipe);
430         sc->sc_bulkout_pipe = NULL;
431 fail_1:
432         usbd_close_pipe(sc->sc_bulkin_pipe);
433         sc->sc_bulkin_pipe = NULL;
434 fail_0:
435         sc->sc_opening = 0;
436         wakeup(&sc->sc_opening);
437         crit_exit();
438         lwkt_reltoken(&tty_token);
439         return (error);
440
441 bad:
442         if (!ISSET(tp->t_state, TS_ISOPEN)) {
443                 /*
444                  * We failed to open the device, and nobody else had it opened.
445                  * Clean up the state as appropriate.
446                  */
447                 ucom_cleanup(sc);
448         }
449
450         DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev)));
451
452         lwkt_reltoken(&tty_token);
453         return (error);
454 }
455
456 static int
457 ucomclose(struct dev_close_args *ap)
458 {
459         cdev_t dev = ap->a_head.a_dev;
460         struct ucom_softc *sc;
461         struct tty *tp;
462
463         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
464
465         lwkt_gettoken(&tty_token);
466         tp = sc->sc_tty;
467
468         DPRINTF(("%s: ucomclose: unit = %d\n",
469                 device_get_nameunit(sc->sc_dev), UCOMUNIT(dev)));
470
471         if (!ISSET(tp->t_state, TS_ISOPEN))
472                 goto quit;
473
474         crit_enter();
475         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
476         disc_optim(tp, &tp->t_termios, sc);
477         ttyclose(tp);
478         crit_exit();
479
480         if (sc->sc_dying)
481                 goto quit;
482
483         if (!ISSET(tp->t_state, TS_ISOPEN)) {
484                 /*
485                  * Although we got a last close, the device may still be in
486                  * use; e.g. if this was the dialout node, and there are still
487                  * processes waiting for carrier on the non-dialout node.
488                  */
489                 ucom_cleanup(sc);
490         }
491
492         if (sc->sc_callback->ucom_close != NULL)
493                 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
494
495 quit:
496         if (tp->t_dev) {
497                 release_dev(tp->t_dev);
498                 tp->t_dev = NULL;
499         }
500
501         if (--sc->sc_refcnt < 0)
502                 usb_detach_wakeup(sc->sc_dev);
503
504         lwkt_reltoken(&tty_token);
505         return (0);
506 }
507
508 static int
509 ucomread(struct dev_read_args *ap)
510 {
511         cdev_t dev = ap->a_head.a_dev;
512         struct ucom_softc *sc;
513         struct tty *tp;
514         int error;
515
516         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
517         lwkt_gettoken(&tty_token);
518         tp = sc->sc_tty;
519
520         DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
521
522         if (sc->sc_dying) {
523                 lwkt_reltoken(&tty_token);
524                 return (EIO);
525         }
526
527         error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
528
529         DPRINTF(("ucomread: error = %d\n", error));
530
531         lwkt_reltoken(&tty_token);
532         return (error);
533 }
534
535 static int
536 ucomwrite(struct dev_write_args *ap)
537 {
538         cdev_t dev = ap->a_head.a_dev;
539         struct ucom_softc *sc;
540         struct tty *tp;
541         int error;
542
543         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
544         lwkt_gettoken(&tty_token);
545         tp = sc->sc_tty;
546
547         DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag));
548
549         if (sc->sc_dying) {
550                 lwkt_reltoken(&tty_token);
551                 return (EIO);
552         }
553
554         error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
555
556         DPRINTF(("ucomwrite: error = %d\n", error));
557
558         lwkt_reltoken(&tty_token);
559         return (error);
560 }
561
562 static int
563 ucomioctl(struct dev_ioctl_args *ap)
564 {
565         cdev_t dev = ap->a_head.a_dev;
566         struct ucom_softc *sc;
567         struct tty *tp;
568         int error;
569         int d;
570
571         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev));
572         lwkt_gettoken(&tty_token);
573         tp = sc->sc_tty;
574
575         if (sc->sc_dying) {
576                 lwkt_reltoken(&tty_token);
577                 return (EIO);
578         }
579
580         DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd));
581
582         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
583                                               ap->a_fflag, ap->a_cred);
584         if (error != ENOIOCTL) {
585                 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
586                 lwkt_reltoken(&tty_token);
587                 return (error);
588         }
589
590         crit_enter();
591
592         error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
593         disc_optim(tp, &tp->t_termios, sc);
594         if (error != ENOIOCTL) {
595                 crit_exit();
596                 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
597                 lwkt_reltoken(&tty_token);
598                 return (error);
599         }
600
601         if (sc->sc_callback->ucom_ioctl != NULL) {
602                 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
603                                                     sc->sc_portno,
604                                                     ap->a_cmd, ap->a_data,
605                                                     ap->a_fflag, curthread);
606                 if (error >= 0) {
607                         crit_exit();
608                         lwkt_reltoken(&tty_token);
609                         return (error);
610                 }
611         }
612
613         error = 0;
614
615         DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd));
616
617         switch (ap->a_cmd) {
618         case TIOCSBRK:
619                 DPRINTF(("ucomioctl: TIOCSBRK\n"));
620                 ucom_break(sc, 1);
621                 break;
622         case TIOCCBRK:
623                 DPRINTF(("ucomioctl: TIOCCBRK\n"));
624                 ucom_break(sc, 0);
625                 break;
626
627         case TIOCSDTR:
628                 DPRINTF(("ucomioctl: TIOCSDTR\n"));
629                 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
630                 break;
631         case TIOCCDTR:
632                 DPRINTF(("ucomioctl: TIOCCDTR\n"));
633                 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
634                 break;
635
636         case TIOCMSET:
637                 d = *(int *)ap->a_data;
638                 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
639                 (void)ucomctl(sc, d, DMSET);
640                 break;
641         case TIOCMBIS:
642                 d = *(int *)ap->a_data;
643                 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
644                 (void)ucomctl(sc, d, DMBIS);
645                 break;
646         case TIOCMBIC:
647                 d = *(int *)ap->a_data;
648                 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
649                 (void)ucomctl(sc, d, DMBIC);
650                 break;
651         case TIOCMGET:
652                 d = ucomctl(sc, 0, DMGET);
653                 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
654                 *(int *)ap->a_data = d;
655                 break;
656
657         default:
658                 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd));
659                 error = ENOTTY;
660                 break;
661         }
662
663         crit_exit();
664
665         lwkt_reltoken(&tty_token);
666         return (error);
667 }
668
669 /*
670  * NOTE: Must be called with tty_token held.
671  */
672 static int
673 ucomctl(struct ucom_softc *sc, int bits, int how)
674 {
675         int     mcr;
676         int     msr;
677         int     onoff;
678
679         ASSERT_LWKT_TOKEN_HELD(&tty_token);
680         DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
681
682         if (how == DMGET) {
683                 SET(bits, TIOCM_LE);            /* always set TIOCM_LE bit */
684                 DPRINTF(("ucomctl: DMGET: LE"));
685
686                 mcr = sc->sc_mcr;
687                 if (ISSET(mcr, UMCR_DTR)) {
688                         SET(bits, TIOCM_DTR);
689                         DPRINTF((" DTR"));
690                 }
691                 if (ISSET(mcr, UMCR_RTS)) {
692                         SET(bits, TIOCM_RTS);
693                         DPRINTF((" RTS"));
694                 }
695
696                 msr = sc->sc_msr;
697                 if (ISSET(msr, UMSR_CTS)) {
698                         SET(bits, TIOCM_CTS);
699                         DPRINTF((" CTS"));
700                 }
701                 if (ISSET(msr, UMSR_DCD)) {
702                         SET(bits, TIOCM_CD);
703                         DPRINTF((" CD"));
704                 }
705                 if (ISSET(msr, UMSR_DSR)) {
706                         SET(bits, TIOCM_DSR);
707                         DPRINTF((" DSR"));
708                 }
709                 if (ISSET(msr, UMSR_RI)) {
710                         SET(bits, TIOCM_RI);
711                         DPRINTF((" RI"));
712                 }
713
714                 DPRINTF(("\n"));
715
716                 return (bits);
717         }
718
719         mcr = 0;
720         if (ISSET(bits, TIOCM_DTR))
721                 SET(mcr, UMCR_DTR);
722         if (ISSET(bits, TIOCM_RTS))
723                 SET(mcr, UMCR_RTS);
724
725         switch (how) {
726         case DMSET:
727                 sc->sc_mcr = mcr;
728                 break;
729         case DMBIS:
730                 sc->sc_mcr |= mcr;
731                 break;
732         case DMBIC:
733                 sc->sc_mcr &= ~mcr;
734                 break;
735         }
736
737         onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
738         ucom_dtr(sc, onoff);
739
740         onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
741         ucom_rts(sc, onoff);
742
743         return (0);
744 }
745
746 /*
747  * NOTE: Must be called with tty_token held.
748  */
749 static void
750 ucom_break(struct ucom_softc *sc, int onoff)
751 {
752         ASSERT_LWKT_TOKEN_HELD(&tty_token);
753         DPRINTF(("ucom_break: onoff = %d\n", onoff));
754
755         if (sc->sc_callback->ucom_set == NULL)
756                 return;
757         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
758                                   UCOM_SET_BREAK, onoff);
759 }
760
761 /*
762  * NOTE: Must be called with tty_token held.
763  */
764 static void
765 ucom_dtr(struct ucom_softc *sc, int onoff)
766 {
767         ASSERT_LWKT_TOKEN_HELD(&tty_token);
768         DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
769
770         if (sc->sc_callback->ucom_set == NULL)
771                 return;
772         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
773                                   UCOM_SET_DTR, onoff);
774 }
775
776 /*
777  * NOTE: Must be called with tty_token held.
778  */
779 static void
780 ucom_rts(struct ucom_softc *sc, int onoff)
781 {
782         ASSERT_LWKT_TOKEN_HELD(&tty_token);
783         DPRINTF(("ucom_rts: onoff = %d\n", onoff));
784
785         if (sc->sc_callback->ucom_set == NULL)
786                 return;
787         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
788                                   UCOM_SET_RTS, onoff);
789 }
790
791 void
792 ucom_status_change(struct ucom_softc *sc)
793 {
794         struct tty *tp = sc->sc_tty;
795         u_char old_msr;
796         int onoff;
797
798         lwkt_gettoken(&tty_token);
799         if (sc->sc_callback->ucom_get_status == NULL) {
800                 sc->sc_lsr = 0;
801                 sc->sc_msr = 0;
802                 lwkt_reltoken(&tty_token);
803                 return;
804         }
805
806         old_msr = sc->sc_msr;
807         sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
808                                          &sc->sc_lsr, &sc->sc_msr);
809         if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
810                 if (sc->sc_poll == 0) {
811                         lwkt_reltoken(&tty_token);
812                         return;
813                 }
814                 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
815                 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
816                 (*linesw[tp->t_line].l_modem)(tp, onoff);
817         }
818         lwkt_reltoken(&tty_token);
819 }
820
821 static int
822 ucomparam(struct tty *tp, struct termios *t)
823 {
824         struct ucom_softc *sc;
825         int error;
826         usbd_status uerr;
827
828         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
829
830         lwkt_gettoken(&tty_token);
831         if (sc->sc_dying) {
832                 lwkt_reltoken(&tty_token);
833                 return (EIO);
834         }
835
836         DPRINTF(("ucomparam: sc = %p\n", sc));
837
838         /* Check requested parameters. */
839         if (t->c_ospeed < 0) {
840                 DPRINTF(("ucomparam: negative ospeed\n"));
841                 lwkt_reltoken(&tty_token);
842                 return (EINVAL);
843         }
844         if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
845                 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
846                 lwkt_reltoken(&tty_token);
847                 return (EINVAL);
848         }
849
850         /*
851          * If there were no changes, don't do anything.  This avoids dropping
852          * input and improves performance when all we did was frob things like
853          * VMIN and VTIME.
854          */
855         if (tp->t_ospeed == t->c_ospeed &&
856             tp->t_cflag == t->c_cflag) {
857                 lwkt_reltoken(&tty_token);
858                 return (0);
859         }
860
861         /* And copy to tty. */
862         tp->t_ispeed = 0;
863         tp->t_ospeed = t->c_ospeed;
864         tp->t_cflag = t->c_cflag;
865
866         if (sc->sc_callback->ucom_param == NULL) {
867                 lwkt_reltoken(&tty_token);
868                 return (0);
869         }
870
871         ucomstopread(sc);
872
873         error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
874         if (error) {
875                 DPRINTF(("ucomparam: callback: error = %d\n", error));
876                 lwkt_reltoken(&tty_token);
877                 return (error);
878         }
879
880         ttsetwater(tp);
881
882         if (t->c_cflag & CRTS_IFLOW) {
883                 sc->sc_state |= UCS_RTS_IFLOW;
884         } else if (sc->sc_state & UCS_RTS_IFLOW) {
885                 sc->sc_state &= ~UCS_RTS_IFLOW;
886                 (void)ucomctl(sc, UMCR_RTS, DMBIS);
887         }
888
889         disc_optim(tp, t, sc);
890
891         uerr = ucomstartread(sc);
892         if (uerr != USBD_NORMAL_COMPLETION) {
893                 lwkt_reltoken(&tty_token);
894                 return (EIO);
895         }
896
897         lwkt_reltoken(&tty_token);
898         return (0);
899 }
900
901 static void
902 ucomstart(struct tty *tp)
903 {
904         struct ucom_softc *sc;
905         struct cblock *cbp;
906         usbd_status err;
907         u_char *data;
908         int cnt;
909
910         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
911         DPRINTF(("ucomstart: sc = %p\n", sc));
912
913         lwkt_gettoken(&tty_token);
914
915         if (sc->sc_dying) {
916                 lwkt_reltoken(&tty_token);
917                 return;
918         }
919
920         crit_enter();
921
922         if (tp->t_state & TS_TBLOCK) {
923                 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
924                     ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
925                         DPRINTF(("ucomstart: clear RTS\n"));
926                         (void)ucomctl(sc, UMCR_RTS, DMBIC);
927                 }
928         } else {
929                 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
930                     tp->t_rawq.c_cc <= tp->t_ilowat &&
931                     ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
932                         DPRINTF(("ucomstart: set RTS\n"));
933                         (void)ucomctl(sc, UMCR_RTS, DMBIS);
934                 }
935         }
936
937         if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
938                 ttwwakeup(tp);
939                 DPRINTF(("ucomstart: stopped\n"));
940                 goto out;
941         }
942
943         if (tp->t_outq.c_cc <= tp->t_olowat) {
944                 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
945                         CLR(tp->t_state, TS_SO_OLOWAT);
946                         wakeup(TSA_OLOWAT(tp));
947                 }
948                 KNOTE(&tp->t_wkq.ki_note, 0);
949                 if (tp->t_outq.c_cc == 0) {
950                         if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
951                             TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
952                                 CLR(tp->t_state, TS_SO_OCOMPLETE);
953                                 wakeup(TSA_OCOMPLETE(tp));
954                         }
955                         goto out;
956                 }
957         }
958
959         /* Grab the first contiguous region of buffer space. */
960         data = tp->t_outq.c_cf;
961         cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
962         cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
963
964         if (cnt == 0) {
965                 DPRINTF(("ucomstart: cnt == 0\n"));
966                 goto out;
967         }
968
969         SET(tp->t_state, TS_BUSY);
970
971         if (cnt > sc->sc_obufsize) {
972                 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
973                 cnt = sc->sc_obufsize;
974         }
975         if (sc->sc_callback->ucom_write != NULL)
976                 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
977                                             sc->sc_obuf, data, &cnt);
978         else
979                 memcpy(sc->sc_obuf, data, cnt);
980
981         DPRINTF(("ucomstart: %d chars\n", cnt));
982         usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
983                         (usbd_private_handle)sc, sc->sc_obuf, cnt,
984                         USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
985         /* What can we do on error? */
986         err = usbd_transfer(sc->sc_oxfer);
987         if (err != USBD_IN_PROGRESS)
988                 kprintf("ucomstart: err=%s\n", usbd_errstr(err));
989
990         ttwwakeup(tp);
991
992     out:
993         crit_exit();
994         lwkt_reltoken(&tty_token);
995 }
996
997 static void
998 ucomstop(struct tty *tp, int flag)
999 {
1000         struct ucom_softc *sc;
1001
1002         sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev));
1003
1004         DPRINTF(("ucomstop: %d\n", flag));
1005
1006         lwkt_gettoken(&tty_token);
1007         if (flag & FREAD) {
1008                 /*
1009                  * This is just supposed to flush pending receive data,
1010                  * not stop the reception of data entirely!
1011                  */
1012                 DPRINTF(("ucomstop: read\n"));
1013                 ucomstopread(sc);
1014                 ucomstartread(sc);
1015         }
1016
1017         if (flag & FWRITE) {
1018                 DPRINTF(("ucomstop: write\n"));
1019                 crit_enter();
1020                 if (ISSET(tp->t_state, TS_BUSY)) {
1021                         /* XXX do what? */
1022                         if (!ISSET(tp->t_state, TS_TTSTOP))
1023                                 SET(tp->t_state, TS_FLUSH);
1024                 }
1025                 crit_exit();
1026         }
1027
1028         DPRINTF(("ucomstop: done\n"));
1029         lwkt_reltoken(&tty_token);
1030 }
1031
1032 static void
1033 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1034 {
1035         struct ucom_softc *sc = (struct ucom_softc *)p;
1036         struct tty *tp = sc->sc_tty;
1037         u_int32_t cc;
1038
1039         lwkt_gettoken(&tty_token);
1040         DPRINTF(("ucomwritecb: status = %d\n", status));
1041
1042         if (status == USBD_CANCELLED || sc->sc_dying)
1043                 goto error;
1044
1045         if (status != USBD_NORMAL_COMPLETION) {
1046                 kprintf("%s: ucomwritecb: %s\n",
1047                        device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1048                 if (status == USBD_STALLED)
1049                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1050                 /*
1051                  * XXX.  We may need a flag to sequence ucomstopread() and
1052                  * ucomstartread() to handle the case where ucomstartread()
1053                  * is called after ucomstopread() but before the request has
1054                  * been properly canceled?
1055                  */
1056                 goto error;
1057         }
1058
1059         usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1060         DPRINTF(("ucomwritecb: cc = %d\n", cc));
1061         if (cc <= sc->sc_opkthdrlen) {
1062                 kprintf("%s: sent size too small, cc = %d\n",
1063                         device_get_nameunit(sc->sc_dev), cc);
1064                 goto error;
1065         }
1066
1067         /* convert from USB bytes to tty bytes */
1068         cc -= sc->sc_opkthdrlen;
1069
1070         crit_enter();
1071         CLR(tp->t_state, TS_BUSY);
1072         if (ISSET(tp->t_state, TS_FLUSH))
1073                 CLR(tp->t_state, TS_FLUSH);
1074         else
1075                 ndflush(&tp->t_outq, cc);
1076         (*linesw[tp->t_line].l_start)(tp);
1077         crit_exit();
1078
1079         lwkt_reltoken(&tty_token);
1080         return;
1081
1082   error:
1083         crit_enter();
1084         CLR(tp->t_state, TS_BUSY);
1085         crit_exit();
1086         lwkt_reltoken(&tty_token);
1087         return;
1088 }
1089
1090 /*
1091  * NOTE: Must be called with tty_token held
1092  */
1093 static usbd_status
1094 ucomstartread(struct ucom_softc *sc)
1095 {
1096         usbd_status err;
1097
1098         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1099         DPRINTF(("ucomstartread: start\n"));
1100
1101         sc->sc_state &= ~UCS_RXSTOP;
1102
1103         if (sc->sc_bulkin_pipe == NULL)
1104                 return (USBD_NORMAL_COMPLETION);
1105
1106         usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1107                         (usbd_private_handle)sc,
1108                         sc->sc_ibuf, sc->sc_ibufsize,
1109                         USBD_SHORT_XFER_OK | USBD_NO_COPY,
1110                         USBD_NO_TIMEOUT, ucomreadcb);
1111
1112         err = usbd_transfer(sc->sc_ixfer);
1113         if (err != USBD_IN_PROGRESS) {
1114                 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1115                 return (err);
1116         }
1117
1118         return (USBD_NORMAL_COMPLETION);
1119 }
1120
1121 static void
1122 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1123 {
1124         lwkt_gettoken(&tty_token);
1125
1126         struct ucom_softc *sc = (struct ucom_softc *)p;
1127         struct tty *tp = sc->sc_tty;
1128         int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1129         usbd_status err;
1130         u_int32_t cc;
1131         u_char *cp;
1132         int lostcc;
1133
1134         DPRINTF(("ucomreadcb: status = %d\n", status));
1135
1136         if (status != USBD_NORMAL_COMPLETION) {
1137                 if (!(sc->sc_state & UCS_RXSTOP))
1138                         kprintf("%s: ucomreadcb: %s\n",
1139                                device_get_nameunit(sc->sc_dev), usbd_errstr(status));
1140                 if (status == USBD_STALLED)
1141                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1142                 /* XXX we should restart after some delay. */
1143                 lwkt_reltoken(&tty_token);
1144                 return;
1145         }
1146
1147         usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1148         DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1149         if (cc == 0)
1150                 goto resubmit;
1151
1152         if (sc->sc_callback->ucom_read != NULL) {
1153                 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1154                                            &cp, &cc);
1155         }
1156
1157         if (cc > sc->sc_ibufsize) {
1158                 kprintf("%s: invalid receive data size, %d chars\n",
1159                         device_get_nameunit(sc->sc_dev), cc);
1160                 goto resubmit;
1161         }
1162         if (cc < 1)
1163                 goto resubmit;
1164
1165         crit_enter();
1166         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1167                 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1168                     && (sc->sc_state & UCS_RTS_IFLOW
1169                         || tp->t_iflag & IXOFF)
1170                     && !(tp->t_state & TS_TBLOCK))
1171                         ttyblock(tp);
1172                 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1173                 tp->t_rawcc += cc;
1174                 if (sc->hotchar) {
1175                         while (cc) {
1176                                 if (*cp == sc->hotchar)
1177                                         break;
1178                                 --cc;
1179                                 ++cp;
1180                         }
1181                         if (cc)
1182                                 setsofttty();
1183                 }
1184                 ttwakeup(tp);
1185                 if (tp->t_state & TS_TTSTOP
1186                     && (tp->t_iflag & IXANY
1187                         || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1188                         tp->t_state &= ~TS_TTSTOP;
1189                         tp->t_lflag &= ~FLUSHO;
1190                         ucomstart(tp);
1191                 }
1192                 if (lostcc > 0)
1193                         kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev),
1194                                lostcc);
1195         } else {
1196                 /* Give characters to tty layer. */
1197                 while (cc > 0) {
1198                         DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp));
1199                         if ((*rint)(*cp, tp) == -1) {
1200                                 /* XXX what should we do? */
1201                                 kprintf("%s: lost %d chars\n",
1202                                        device_get_nameunit(sc->sc_dev), cc);
1203                                 break;
1204                         }
1205                         cc--;
1206                         cp++;
1207                 }
1208         }
1209         crit_exit();
1210
1211 resubmit:
1212         err = ucomstartread(sc);
1213         if (err) {
1214                 kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev));
1215                 /* XXX what should we dow now? */
1216         }
1217
1218         if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1219             && !(tp->t_state & TS_TBLOCK))
1220                 ucomctl(sc, UMCR_RTS, DMBIS);
1221
1222         lwkt_reltoken(&tty_token);
1223 }
1224
1225 /*
1226  * NOTE: Must be called with tty_token held.
1227  */
1228 static void
1229 ucom_cleanup(struct ucom_softc *sc)
1230 {
1231         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1232         DPRINTF(("ucom_cleanup: closing pipes\n"));
1233
1234         ucom_shutdown(sc);
1235         if (sc->sc_bulkin_pipe != NULL) {
1236                 usbd_abort_pipe(sc->sc_bulkin_pipe);
1237                 usbd_close_pipe(sc->sc_bulkin_pipe);
1238                 sc->sc_bulkin_pipe = NULL;
1239         }
1240         if (sc->sc_bulkout_pipe != NULL) {
1241                 usbd_abort_pipe(sc->sc_bulkout_pipe);
1242                 usbd_close_pipe(sc->sc_bulkout_pipe);
1243                 sc->sc_bulkout_pipe = NULL;
1244         }
1245         if (sc->sc_ixfer != NULL) {
1246                 usbd_free_xfer(sc->sc_ixfer);
1247                 sc->sc_ixfer = NULL;
1248         }
1249         if (sc->sc_oxfer != NULL) {
1250                 usbd_free_xfer(sc->sc_oxfer);
1251                 sc->sc_oxfer = NULL;
1252         }
1253 }
1254
1255 /*
1256  * NOTE: Must be called with tty_token held.
1257  */
1258 static void
1259 ucomstopread(struct ucom_softc *sc)
1260 {
1261         usbd_status err;
1262
1263         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1264         DPRINTF(("ucomstopread: enter\n"));
1265
1266         if (!(sc->sc_state & UCS_RXSTOP)) {
1267                 sc->sc_state |= UCS_RXSTOP;
1268                 if (sc->sc_bulkin_pipe == NULL) {
1269                         DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1270                         return;
1271                 }
1272                 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1273                 if (err) {
1274                         DPRINTF(("ucomstopread: err = %s\n",
1275                                  usbd_errstr(err)));
1276                 }
1277         }
1278
1279         DPRINTF(("ucomstopread: leave\n"));
1280 }
1281
1282 /*
1283  * NOTE: Must be called with tty_token held.
1284  */
1285 static void
1286 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1287 {
1288         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1289         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1290             && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1291             && (!(t->c_iflag & PARMRK)
1292                 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1293             && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1294             && linesw[tp->t_line].l_rint == ttyinput) {
1295                 DPRINTF(("disc_optim: bypass l_rint\n"));
1296                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1297         } else {
1298                 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1299                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1300         }
1301         sc->hotchar = linesw[tp->t_line].l_hotchar;
1302 }