Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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 /*      $DragonFly: src/sys/dev/usbmisc/ucom/ucom.c,v 1.2 2003/06/17 04:28:32 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 <dev/usb/usb.h>
95 #include <dev/usb/usbcdc.h>
96
97 #include <dev/usb/usbdi.h>
98 #include <dev/usb/usbdi_util.h>
99 #include <dev/usb/usbdevs.h>
100 #include <dev/usb/usb_quirks.h>
101
102 #include <dev/usb/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         /* open */      ucomopen,
133         /* close */     ucomclose,
134         /* read */      ucomread,
135         /* write */     ucomwrite,
136         /* ioctl */     ucomioctl,
137         /* poll */      ttypoll,
138         /* mmap */      nommap,
139         /* strategy */  nostrategy,
140         /* name */      "ucom",
141         /* maj */       UCOM_CDEV_MAJOR,
142         /* dump */      nodump,
143         /* psize */     nopsize,
144         /* flags */     D_TTY | D_KQFILTER,
145 #if __FreeBSD_version < 500014
146         /* bmaj */      -1,
147 #endif
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, TTIPRI, "ucomsd", hz);
258         }
259 }
260
261 Static int
262 ucomopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
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         USB_GET_SC_OPEN(ucom, unit, sc);
272
273         if (sc->sc_dying)
274                 return (ENXIO);
275
276         tp = sc->sc_tty;
277
278         DPRINTF(("%s: ucomopen: tp = %p\n", USBDEVNAME(sc->sc_dev), tp));
279
280         if (ISSET(tp->t_state, TS_ISOPEN) &&
281             ISSET(tp->t_state, TS_XCLUDE) &&
282             suser(p))
283                 return (EBUSY);
284
285         /*
286          * Do the following iff this is a first open.
287          */
288         s = spltty();
289         while (sc->sc_opening)
290                 tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
291         sc->sc_opening = 1;
292
293         if (!ISSET(tp->t_state, TS_ISOPEN)) {
294                 struct termios t;
295
296                 sc->sc_poll = 0;
297                 sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0;
298
299                 tp->t_dev = dev;
300
301                 /*
302                  * Initialize the termios status to the defaults.  Add in the
303                  * sticky bits from TIOCSFLAGS.
304                  */
305                 t.c_ispeed = 0;
306                 t.c_ospeed = TTYDEF_SPEED;
307                 t.c_cflag = TTYDEF_CFLAG;
308                 /* Make sure ucomparam() will do something. */
309                 tp->t_ospeed = 0;
310                 (void)ucomparam(tp, &t);
311                 tp->t_iflag = TTYDEF_IFLAG;
312                 tp->t_oflag = TTYDEF_OFLAG;
313                 tp->t_lflag = TTYDEF_LFLAG;
314                 ttychars(tp);
315                 ttsetwater(tp);
316
317                 /*
318                  * Turn on DTR.  We must always do this, even if carrier is not
319                  * present, because otherwise we'd have to use TIOCSDTR
320                  * immediately after setting CLOCAL, which applications do not
321                  * expect.  We always assert DTR while the device is open
322                  * unless explicitly requested to deassert it.
323                  */
324                 (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS);
325
326                 /* Device specific open */
327                 if (sc->sc_callback->ucom_open != NULL) {
328                         error = sc->sc_callback->ucom_open(sc->sc_parent,
329                                                            sc->sc_portno);
330                         if (error) {
331                                 ucom_cleanup(sc);
332                                 sc->sc_opening = 0;
333                                 wakeup(&sc->sc_opening);
334                                 splx(s);
335                                 return (error);
336                         }
337                 }
338
339                 DPRINTF(("ucomopen: open pipes in = %d out = %d\n",
340                          sc->sc_bulkin_no, sc->sc_bulkout_no));
341
342                 /* Open the bulk pipes */
343                 /* Bulk-in pipe */
344                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
345                                      &sc->sc_bulkin_pipe);
346                 if (err) {
347                         printf("%s: open bulk out error (addr %d): %s\n",
348                                USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no, 
349                                usbd_errstr(err));
350                         error = EIO;
351                         goto fail_0;
352                 }
353                 /* Bulk-out pipe */
354                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
355                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
356                 if (err) {
357                         printf("%s: open bulk in error (addr %d): %s\n",
358                                USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
359                                usbd_errstr(err));
360                         error = EIO;
361                         goto fail_1;
362                 }
363
364                 /* Allocate a request and an input buffer and start reading. */
365                 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
366                 if (sc->sc_ixfer == NULL) {
367                         error = ENOMEM;
368                         goto fail_2;
369                 }
370
371                 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
372                                                 sc->sc_ibufsizepad);
373                 if (sc->sc_ibuf == NULL) {
374                         error = ENOMEM;
375                         goto fail_3;
376                 }
377
378                 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
379                 if (sc->sc_oxfer == NULL) {
380                         error = ENOMEM;
381                         goto fail_3;
382                 }
383
384                 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
385                                                 sc->sc_obufsize +
386                                                 sc->sc_opkthdrlen);
387                 if (sc->sc_obuf == NULL) {
388                         error = ENOMEM;
389                         goto fail_4;
390                 }
391
392                 /*
393                  * Handle initial DCD.
394                  */
395                 if (ISSET(sc->sc_msr, UMSR_DCD) ||
396                     (minor(dev) & UCOM_CALLOUT_MASK))
397                         (*linesw[tp->t_line].l_modem)(tp, 1);
398
399                 ucomstartread(sc);
400         }
401
402         sc->sc_opening = 0;
403         wakeup(&sc->sc_opening);
404         splx(s);
405
406         error = ttyopen(dev, tp);
407         if (error)
408                 goto bad;
409
410         error = (*linesw[tp->t_line].l_open)(dev, tp);
411         if (error)
412                 goto bad;
413
414         disc_optim(tp, &tp->t_termios, sc);
415
416         DPRINTF(("%s: ucomopen: success\n", USBDEVNAME(sc->sc_dev)));
417
418         sc->sc_poll = 1;
419         sc->sc_refcnt++;
420
421         return (0);
422
423 fail_4:
424         usbd_free_xfer(sc->sc_oxfer);
425         sc->sc_oxfer = NULL;
426 fail_3:
427         usbd_free_xfer(sc->sc_ixfer);
428         sc->sc_ixfer = NULL;
429 fail_2:
430         usbd_close_pipe(sc->sc_bulkout_pipe);
431         sc->sc_bulkout_pipe = NULL;
432 fail_1:
433         usbd_close_pipe(sc->sc_bulkin_pipe);
434         sc->sc_bulkin_pipe = NULL;
435 fail_0:
436         sc->sc_opening = 0;
437         wakeup(&sc->sc_opening);
438         splx(s);
439         return (error);
440
441 bad:
442         if (!ISSET(tp->t_state, TS_ISOPEN)) {
443                 /*
444                  * We failed to open the device, and nobody else had it opened.
445                  * Clean up the state as appropriate.
446                  */
447                 ucom_cleanup(sc);
448         }
449
450         DPRINTF(("%s: ucomopen: failed\n", USBDEVNAME(sc->sc_dev)));
451
452         return (error);
453 }
454
455 static int
456 ucomclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
457 {
458         struct ucom_softc *sc;
459         struct tty *tp;
460         int s;
461
462         USB_GET_SC(ucom, UCOMUNIT(dev), sc);
463
464         tp = sc->sc_tty;
465
466         DPRINTF(("%s: ucomclose: unit = %d\n",
467                 USBDEVNAME(sc->sc_dev), UCOMUNIT(dev)));
468
469         if (!ISSET(tp->t_state, TS_ISOPEN))
470                 goto quit;
471
472         s = spltty();
473         (*linesw[tp->t_line].l_close)(tp, flag);
474         disc_optim(tp, &tp->t_termios, sc);
475         ttyclose(tp);
476         splx(s);
477
478         if (sc->sc_dying)
479                 goto quit;
480
481         if (!ISSET(tp->t_state, TS_ISOPEN)) {
482                 /*
483                  * Although we got a last close, the device may still be in
484                  * use; e.g. if this was the dialout node, and there are still
485                  * processes waiting for carrier on the non-dialout node.
486                  */
487                 ucom_cleanup(sc);
488         }
489
490         if (sc->sc_callback->ucom_close != NULL)
491                 sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno);
492
493     quit:
494         if (--sc->sc_refcnt < 0)
495                 usb_detach_wakeup(USBDEV(sc->sc_dev));
496
497         return (0);
498 }
499
500 static int
501 ucomread(dev_t dev, struct uio *uio, int flag)
502 {
503         struct ucom_softc *sc;
504         struct tty *tp;
505         int error;
506
507         USB_GET_SC(ucom, UCOMUNIT(dev), sc);
508         tp = sc->sc_tty;
509
510         DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, flag));
511
512         if (sc->sc_dying)
513                 return (EIO);
514
515         error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
516
517         DPRINTF(("ucomread: error = %d\n", error));
518
519         return (error);
520 }
521
522 static int
523 ucomwrite(dev_t dev, struct uio *uio, int flag)
524 {
525         struct ucom_softc *sc;
526         struct tty *tp;
527         int error;
528
529         USB_GET_SC(ucom, UCOMUNIT(dev), sc);
530         tp = sc->sc_tty;
531
532         DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, flag));
533
534         if (sc->sc_dying)
535                 return (EIO);
536
537         error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
538
539         DPRINTF(("ucomwrite: error = %d\n", error));
540
541         return (error);
542 }
543
544 static int
545 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
546 {
547         struct ucom_softc *sc;
548         struct tty *tp;
549         int error;
550         int s;
551         int d;
552
553         USB_GET_SC(ucom, UCOMUNIT(dev), sc);
554         tp = sc->sc_tty;
555
556         if (sc->sc_dying)
557                 return (EIO);
558
559         DPRINTF(("ucomioctl: cmd = 0x%08lx\n", cmd));
560
561         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
562         if (error >= 0) {
563                 DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error));
564                 return (error);
565         }
566
567         error = ttioctl(tp, cmd, data, flag);
568         disc_optim(tp, &tp->t_termios, sc);
569         if (error >= 0) {
570                 DPRINTF(("ucomioctl: ttioctl: error = %d\n", error));
571                 return (error);
572         }
573
574         if (sc->sc_callback->ucom_ioctl != NULL) {
575                 error = sc->sc_callback->ucom_ioctl(sc->sc_parent,
576                                                     sc->sc_portno,
577                                                     cmd, data, flag, p);
578                 if (error >= 0)
579                         return (error);
580         }
581
582         error = 0;
583
584         DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", cmd));
585
586         s = spltty();
587
588         switch (cmd) {
589         case TIOCSBRK:
590                 DPRINTF(("ucomioctl: TIOCSBRK\n"));
591                 ucom_break(sc, 1);
592                 break;
593         case TIOCCBRK:
594                 DPRINTF(("ucomioctl: TIOCCBRK\n"));
595                 ucom_break(sc, 0);
596                 break;
597
598         case TIOCSDTR:
599                 DPRINTF(("ucomioctl: TIOCSDTR\n"));
600                 (void)ucomctl(sc, TIOCM_DTR, DMBIS);
601                 break;
602         case TIOCCDTR:
603                 DPRINTF(("ucomioctl: TIOCCDTR\n"));
604                 (void)ucomctl(sc, TIOCM_DTR, DMBIC);
605                 break;
606
607         case TIOCMSET:
608                 d = *(int *)data;
609                 DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d));
610                 (void)ucomctl(sc, d, DMSET);
611                 break;
612         case TIOCMBIS:
613                 d = *(int *)data;
614                 DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d));
615                 (void)ucomctl(sc, d, DMBIS);
616                 break;
617         case TIOCMBIC:
618                 d = *(int *)data;
619                 DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d));
620                 (void)ucomctl(sc, d, DMBIC);
621                 break;
622         case TIOCMGET:
623                 d = ucomctl(sc, 0, DMGET);
624                 DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d));
625                 *(int *)data = d;
626                 break;
627
628         default:
629                 DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", cmd));
630                 error = ENOTTY;
631                 break;
632         }
633
634         splx(s);
635
636         return (error);
637 }
638
639 Static int
640 ucomctl(struct ucom_softc *sc, int bits, int how)
641 {
642         int     mcr;
643         int     msr;
644         int     onoff;
645
646         DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how));
647
648         if (how == DMGET) {
649                 SET(bits, TIOCM_LE);            /* always set TIOCM_LE bit */
650                 DPRINTF(("ucomctl: DMGET: LE"));
651
652                 mcr = sc->sc_mcr;
653                 if (ISSET(mcr, UMCR_DTR)) {
654                         SET(bits, TIOCM_DTR);
655                         DPRINTF((" DTR"));
656                 }
657                 if (ISSET(mcr, UMCR_RTS)) {
658                         SET(bits, TIOCM_RTS);
659                         DPRINTF((" RTS"));
660                 }
661
662                 msr = sc->sc_msr;
663                 if (ISSET(msr, UMSR_CTS)) {
664                         SET(bits, TIOCM_CTS);
665                         DPRINTF((" CTS"));
666                 }
667                 if (ISSET(msr, UMSR_DCD)) {
668                         SET(bits, TIOCM_CD);
669                         DPRINTF((" CD"));
670                 }
671                 if (ISSET(msr, UMSR_DSR)) {
672                         SET(bits, TIOCM_DSR);
673                         DPRINTF((" DSR"));
674                 }
675                 if (ISSET(msr, UMSR_RI)) {
676                         SET(bits, TIOCM_RI);
677                         DPRINTF((" RI"));
678                 }
679
680                 DPRINTF(("\n"));
681
682                 return (bits);
683         }
684
685         mcr = 0;
686         if (ISSET(bits, TIOCM_DTR))
687                 SET(mcr, UMCR_DTR);
688         if (ISSET(bits, TIOCM_RTS))
689                 SET(mcr, UMCR_RTS);
690
691         switch (how) {
692         case DMSET:
693                 sc->sc_mcr = mcr;
694                 break;
695         case DMBIS:
696                 sc->sc_mcr |= mcr;
697                 break;
698         case DMBIC:
699                 sc->sc_mcr &= ~mcr;
700                 break;
701         }
702
703         onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0;
704         ucom_dtr(sc, onoff);
705
706         onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0;
707         ucom_rts(sc, onoff);
708
709         return (0);
710 }
711
712 Static void
713 ucom_break(struct ucom_softc *sc, int onoff)
714 {
715         DPRINTF(("ucom_break: onoff = %d\n", onoff));
716
717         if (sc->sc_callback->ucom_set == NULL)
718                 return;
719         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno,
720                                   UCOM_SET_BREAK, onoff);
721 }
722
723 Static void
724 ucom_dtr(struct ucom_softc *sc, int onoff)
725 {
726         DPRINTF(("ucom_dtr: onoff = %d\n", onoff));
727
728         if (sc->sc_callback->ucom_set == NULL)
729                 return;
730         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno, 
731                                   UCOM_SET_DTR, onoff);
732 }
733
734 Static void
735 ucom_rts(struct ucom_softc *sc, int onoff)
736 {
737         DPRINTF(("ucom_rts: onoff = %d\n", onoff));
738
739         if (sc->sc_callback->ucom_set == NULL)
740                 return;
741         sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno, 
742                                   UCOM_SET_RTS, onoff);
743 }
744
745 void
746 ucom_status_change(struct ucom_softc *sc)
747 {
748         struct tty *tp = sc->sc_tty;
749         u_char old_msr;
750         int onoff;
751
752         if (sc->sc_callback->ucom_get_status == NULL) {
753                 sc->sc_lsr = 0;
754                 sc->sc_msr = 0;
755                 return;
756         }
757
758         old_msr = sc->sc_msr;
759         sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno,
760                                          &sc->sc_lsr, &sc->sc_msr);
761         if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
762                 if (sc->sc_poll == 0)
763                         return;
764                 onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0;
765                 DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff));
766                 (*linesw[tp->t_line].l_modem)(tp, onoff);
767         }
768 }
769
770 Static int
771 ucomparam(struct tty *tp, struct termios *t)
772 {
773         struct ucom_softc *sc;
774         int error;
775         usbd_status uerr;
776
777         USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
778
779         if (sc->sc_dying)
780                 return (EIO);
781
782         DPRINTF(("ucomparam: sc = %p\n", sc));
783
784         /* Check requested parameters. */
785         if (t->c_ospeed < 0) {
786                 DPRINTF(("ucomparam: negative ospeed\n"));
787                 return (EINVAL);
788         }
789         if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
790                 DPRINTF(("ucomparam: mismatch ispeed and ospeed\n"));
791                 return (EINVAL);
792         }
793
794         /*
795          * If there were no changes, don't do anything.  This avoids dropping
796          * input and improves performance when all we did was frob things like
797          * VMIN and VTIME.
798          */
799         if (tp->t_ospeed == t->c_ospeed &&
800             tp->t_cflag == t->c_cflag)
801                 return (0);
802
803         /* And copy to tty. */
804         tp->t_ispeed = 0;
805         tp->t_ospeed = t->c_ospeed;
806         tp->t_cflag = t->c_cflag;
807
808         if (sc->sc_callback->ucom_param == NULL)
809                 return (0);
810
811         ucomstopread(sc);
812
813         error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t);
814         if (error) {
815                 DPRINTF(("ucomparam: callback: error = %d\n", error));
816                 return (error);
817         }
818
819         ttsetwater(tp);
820
821         if (t->c_cflag & CRTS_IFLOW) {
822                 sc->sc_state |= UCS_RTS_IFLOW;
823         } else if (sc->sc_state & UCS_RTS_IFLOW) {
824                 sc->sc_state &= ~UCS_RTS_IFLOW;
825                 (void)ucomctl(sc, UMCR_RTS, DMBIS);
826         }
827
828         disc_optim(tp, t, sc);
829
830         uerr = ucomstartread(sc);
831         if (uerr != USBD_NORMAL_COMPLETION)
832                 return (EIO);
833
834         return (0);
835 }
836
837 Static void
838 ucomstart(struct tty *tp)
839 {
840         struct ucom_softc *sc;
841         struct cblock *cbp;
842         usbd_status err;
843         int s;
844         u_char *data;
845         int cnt;
846
847         USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
848         DPRINTF(("ucomstart: sc = %p\n", sc));
849
850         if (sc->sc_dying)
851                 return;
852
853         s = spltty();
854
855         if (tp->t_state & TS_TBLOCK) {
856                 if (ISSET(sc->sc_mcr, UMCR_RTS) &&
857                     ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
858                         DPRINTF(("ucomstart: clear RTS\n"));
859                         (void)ucomctl(sc, UMCR_RTS, DMBIC);
860                 }
861         } else {
862                 if (!ISSET(sc->sc_mcr, UMCR_RTS) &&
863                     tp->t_rawq.c_cc <= tp->t_ilowat &&
864                     ISSET(sc->sc_state, UCS_RTS_IFLOW)) {
865                         DPRINTF(("ucomstart: set RTS\n"));
866                         (void)ucomctl(sc, UMCR_RTS, DMBIS);
867                 }
868         }
869
870         if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
871                 ttwwakeup(tp);
872                 DPRINTF(("ucomstart: stopped\n"));
873                 goto out;
874         }
875
876         if (tp->t_outq.c_cc <= tp->t_olowat) {
877                 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
878                         CLR(tp->t_state, TS_SO_OLOWAT);
879                         wakeup(TSA_OLOWAT(tp));
880                 }
881                 selwakeup(&tp->t_wsel);
882                 if (tp->t_outq.c_cc == 0) {
883                         if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
884                             TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
885                                 CLR(tp->t_state, TS_SO_OCOMPLETE);
886                                 wakeup(TSA_OCOMPLETE(tp));
887                         }
888                         goto out;
889                 }
890         }
891
892         /* Grab the first contiguous region of buffer space. */
893         data = tp->t_outq.c_cf;
894         cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
895         cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
896
897         if (cnt == 0) {
898                 DPRINTF(("ucomstart: cnt == 0\n"));
899                 goto out;
900         }
901
902         SET(tp->t_state, TS_BUSY);
903
904         if (cnt > sc->sc_obufsize) {
905                 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
906                 cnt = sc->sc_obufsize;
907         }
908         if (sc->sc_callback->ucom_write != NULL)
909                 sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno,
910                                             sc->sc_obuf, data, &cnt);
911         else
912                 memcpy(sc->sc_obuf, data, cnt);
913
914         DPRINTF(("ucomstart: %d chars\n", cnt));
915         usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe, 
916                         (usbd_private_handle)sc, sc->sc_obuf, cnt,
917                         USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
918         /* What can we do on error? */
919         err = usbd_transfer(sc->sc_oxfer);
920         if (err != USBD_IN_PROGRESS)
921                 printf("ucomstart: err=%s\n", usbd_errstr(err));
922
923         ttwwakeup(tp);
924
925     out:
926         splx(s);
927 }
928
929 Static void
930 ucomstop(struct tty *tp, int flag)
931 {
932         struct ucom_softc *sc;
933         int s;
934
935         USB_GET_SC(ucom, UCOMUNIT(tp->t_dev), sc);
936
937         DPRINTF(("ucomstop: %d\n", flag));
938
939         if (flag & FREAD) {
940                 DPRINTF(("ucomstop: read\n"));
941                 ucomstopread(sc);
942         }
943
944         if (flag & FWRITE) {
945                 DPRINTF(("ucomstop: write\n"));
946                 s = spltty();
947                 if (ISSET(tp->t_state, TS_BUSY)) {
948                         /* XXX do what? */
949                         if (!ISSET(tp->t_state, TS_TTSTOP))
950                                 SET(tp->t_state, TS_FLUSH);
951                 }
952                 splx(s);
953         }
954
955         DPRINTF(("ucomstop: done\n"));
956 }
957
958 Static void
959 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
960 {
961         struct ucom_softc *sc = (struct ucom_softc *)p;
962         struct tty *tp = sc->sc_tty;
963         u_int32_t cc;
964         int s;
965
966         DPRINTF(("ucomwritecb: status = %d\n", status));
967
968         if (status == USBD_CANCELLED || sc->sc_dying)
969                 goto error;
970
971         if (status != USBD_NORMAL_COMPLETION) {
972                 printf("%s: ucomwritecb: %s\n",
973                        USBDEVNAME(sc->sc_dev), usbd_errstr(status));
974                 if (status == USBD_STALLED)
975                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
976                 /* XXX we should restart after some delay. */
977                 goto error;
978         }
979
980         usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
981         DPRINTF(("ucomwritecb: cc = %d\n", cc));
982
983         /* convert from USB bytes to tty bytes */
984         cc -= sc->sc_opkthdrlen;
985
986         s = spltty();
987         CLR(tp->t_state, TS_BUSY);
988         if (ISSET(tp->t_state, TS_FLUSH))
989                 CLR(tp->t_state, TS_FLUSH);
990         else
991                 ndflush(&tp->t_outq, cc);
992         (*linesw[tp->t_line].l_start)(tp);
993         splx(s);
994
995         return;
996
997   error:
998         s = spltty();
999         CLR(tp->t_state, TS_BUSY);
1000         splx(s);
1001         return;
1002 }
1003
1004 Static usbd_status
1005 ucomstartread(struct ucom_softc *sc)
1006 {
1007         usbd_status err;
1008
1009         DPRINTF(("ucomstartread: start\n"));
1010
1011         sc->sc_state &= ~UCS_RXSTOP;
1012
1013         if (sc->sc_bulkin_pipe == NULL)
1014                 return (USBD_NORMAL_COMPLETION);
1015
1016         usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe, 
1017                         (usbd_private_handle)sc, 
1018                         sc->sc_ibuf, sc->sc_ibufsize,
1019                         USBD_SHORT_XFER_OK | USBD_NO_COPY,
1020                         USBD_NO_TIMEOUT, ucomreadcb);
1021
1022         err = usbd_transfer(sc->sc_ixfer);
1023         if (err != USBD_IN_PROGRESS) {
1024                 DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err)));
1025                 return (err);
1026         }
1027
1028         return (USBD_NORMAL_COMPLETION);
1029 }
1030
1031 Static void
1032 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1033 {
1034         struct ucom_softc *sc = (struct ucom_softc *)p;
1035         struct tty *tp = sc->sc_tty;
1036         int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
1037         usbd_status err;
1038         u_int32_t cc;
1039         u_char *cp;
1040         int lostcc;
1041         int s;
1042
1043         DPRINTF(("ucomreadcb: status = %d\n", status));
1044
1045         if (status != USBD_NORMAL_COMPLETION) {
1046                 if (!(sc->sc_state & UCS_RXSTOP))
1047                         printf("%s: ucomreadcb: %s\n",
1048                                USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1049                 if (status == USBD_STALLED)
1050                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1051                 /* XXX we should restart after some delay. */
1052                 return;
1053         }
1054
1055         usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1056         DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp));
1057         if (sc->sc_callback->ucom_read != NULL)
1058                 sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno,
1059                                            &cp, &cc);
1060
1061         s = spltty();
1062         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1063                 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
1064                     && (sc->sc_state & UCS_RTS_IFLOW
1065                         || tp->t_iflag & IXOFF)
1066                     && !(tp->t_state & TS_TBLOCK))
1067                         ttyblock(tp);
1068                 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
1069                 tp->t_rawcc += cc;
1070                 ttwakeup(tp);
1071                 if (tp->t_state & TS_TTSTOP
1072                     && (tp->t_iflag & IXANY
1073                         || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1074                         tp->t_state &= ~TS_TTSTOP;
1075                         tp->t_lflag &= ~FLUSHO;
1076                         ucomstart(tp);
1077                 }
1078                 if (lostcc > 0)
1079                         printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1080                                lostcc);
1081         } else {
1082                 /* Give characters to tty layer. */
1083                 while (cc-- > 0) {
1084                         DPRINTFN(7,("ucomreadcb: char = 0x%02x\n", *cp));
1085                         if ((*rint)(*cp++, tp) == -1) {
1086                                 /* XXX what should we do? */
1087                                 printf("%s: lost %d chars\n",
1088                                        USBDEVNAME(sc->sc_dev), cc);
1089                                 break;
1090                         }
1091                 }
1092         }
1093         splx(s);
1094
1095         err = ucomstartread(sc);
1096         if (err) {
1097                 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1098                 /* XXX what should we dow now? */
1099         }
1100
1101         if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS)
1102             && !(tp->t_state & TS_TBLOCK))
1103                 ucomctl(sc, UMCR_RTS, DMBIS);
1104 }
1105
1106 Static void
1107 ucom_cleanup(struct ucom_softc *sc)
1108 {
1109         DPRINTF(("ucom_cleanup: closing pipes\n"));
1110
1111         ucom_shutdown(sc);
1112         if (sc->sc_bulkin_pipe != NULL) {
1113                 usbd_abort_pipe(sc->sc_bulkin_pipe);
1114                 usbd_close_pipe(sc->sc_bulkin_pipe);
1115                 sc->sc_bulkin_pipe = NULL;
1116         }
1117         if (sc->sc_bulkout_pipe != NULL) {
1118                 usbd_abort_pipe(sc->sc_bulkout_pipe);
1119                 usbd_close_pipe(sc->sc_bulkout_pipe);
1120                 sc->sc_bulkout_pipe = NULL;
1121         }
1122         if (sc->sc_ixfer != NULL) {
1123                 usbd_free_xfer(sc->sc_ixfer);
1124                 sc->sc_ixfer = NULL;
1125         }
1126         if (sc->sc_oxfer != NULL) {
1127                 usbd_free_xfer(sc->sc_oxfer);
1128                 sc->sc_oxfer = NULL;
1129         }
1130 }
1131
1132 Static void
1133 ucomstopread(struct ucom_softc *sc)
1134 {
1135         usbd_status err;
1136
1137         DPRINTF(("ucomstopread: enter\n"));
1138
1139         if (!(sc->sc_state & UCS_RXSTOP)) {
1140                 if (sc->sc_bulkin_pipe == NULL) {
1141                         DPRINTF(("ucomstopread: bulkin pipe NULL\n"));
1142                         return;
1143                 }
1144                 sc->sc_state |= UCS_RXSTOP;
1145                 err = usbd_abort_pipe(sc->sc_bulkin_pipe);
1146                 if (err) {
1147                         DPRINTF(("ucomstopread: err = %s\n",
1148                                  usbd_errstr(err)));
1149                 }
1150         }
1151
1152         DPRINTF(("ucomstopread: leave\n"));
1153 }
1154
1155 static void
1156 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
1157 {
1158         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1159             && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1160             && (!(t->c_iflag & PARMRK)
1161                 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1162             && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1163             && linesw[tp->t_line].l_rint == ttyinput) {
1164                 DPRINTF(("disc_optim: bypass l_rint\n"));
1165                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1166         } else {
1167                 DPRINTF(("disc_optim: can't bypass l_rint\n"));
1168                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1169         }
1170         sc->hotchar = linesw[tp->t_line].l_hotchar;
1171 }