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