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