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