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