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