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