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