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