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