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