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