1 /* $NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $ */
4 * Copyright (c) 2001-2003, 2005, 2008
5 * Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Lennart Augustsson (lennart@augustsson.net) at
36 * Carlstedt Research & Technology.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the NetBSD
49 * Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
69 * This is a Frankenstein of FreeBSD's usb4bsd ucom and Dragonfly's old ucom
70 * module. There might be bugs lurking everywhere still
72 * In particular serial console on ucom is completely untested and likely broken
73 * as well as anyting that requires the modem control lines.
76 #include <sys/stdint.h>
77 #include <sys/param.h>
78 #include <sys/queue.h>
79 #include <sys/types.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
83 #include <sys/module.h>
85 #include <sys/condvar.h>
86 #include <sys/sysctl.h>
87 #include <sys/unistd.h>
88 #include <sys/callout.h>
89 #include <sys/malloc.h>
92 #include <sys/serial.h>
93 #include <sys/thread2.h>
95 #include <sys/clist.h>
97 #include <bus/u4b/usb.h>
98 #include <bus/u4b/usbdi.h>
99 #include <bus/u4b/usbdi_util.h>
101 #define USB_DEBUG_VAR ucom_debug
102 #include <bus/u4b/usb_debug.h>
103 #include <bus/u4b/usb_busdma.h>
104 #include <bus/u4b/usb_process.h>
106 #include <bus/u4b/serial/usb_serial.h>
108 /* #include "opt_gdb.h" */
110 static SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
112 static int ucom_pps_mode;
114 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, pps_mode, CTLFLAG_RW,
115 &ucom_pps_mode, 0, "pulse capturing mode - 0/1/2 - disabled/CTS/DCD");
116 TUNABLE_INT("hw.usb.ucom.pss_mode", &ucom_pps_mode);
120 static int ucom_debug = 0;
122 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
123 &ucom_debug, 0, "ucom debug level");
126 #define UCOM_CONS_BUFSIZE 1024
128 static uint8_t ucom_cons_rx_buf[UCOM_CONS_BUFSIZE];
129 static uint8_t ucom_cons_tx_buf[UCOM_CONS_BUFSIZE];
131 static unsigned int ucom_cons_rx_low = 0;
132 static unsigned int ucom_cons_rx_high = 0;
134 static unsigned int ucom_cons_tx_low = 0;
135 static unsigned int ucom_cons_tx_high = 0;
137 static int ucom_cons_unit = -1;
138 static int ucom_cons_subunit = 0;
139 static int ucom_cons_baud = 9600;
140 static struct ucom_softc *ucom_cons_softc = NULL;
142 TUNABLE_INT("hw.usb.ucom.cons_unit", &ucom_cons_unit);
143 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_unit, CTLFLAG_RW,
144 &ucom_cons_unit, 0, "console unit number");
145 TUNABLE_INT("hw.usb.ucom.cons_subunit", &ucom_cons_subunit);
146 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_subunit, CTLFLAG_RW,
147 &ucom_cons_subunit, 0, "console subunit number");
148 TUNABLE_INT("hw.usb.ucom.cons_baud", &ucom_cons_baud);
149 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RW,
150 &ucom_cons_baud, 0, "console baud rate");
152 static usb_proc_callback_t ucom_cfg_start_transfers;
153 static usb_proc_callback_t ucom_cfg_open;
154 static usb_proc_callback_t ucom_cfg_close;
155 static usb_proc_callback_t ucom_cfg_line_state;
156 static usb_proc_callback_t ucom_cfg_status_change;
157 static usb_proc_callback_t ucom_cfg_param;
159 static int ucom_unit_alloc(void);
160 static void ucom_unit_free(int);
161 static int ucom_attach_tty(struct ucom_super_softc *, struct ucom_softc *);
162 static void ucom_detach_tty(struct ucom_super_softc *, struct ucom_softc *);
163 static void ucom_queue_command(struct ucom_softc *,
164 usb_proc_callback_t *, struct termios *pt,
165 struct usb_proc_msg *t0, struct usb_proc_msg *t1);
166 static void ucom_shutdown(struct ucom_softc *);
167 static void ucom_ring(struct ucom_softc *, uint8_t);
168 static void ucom_break(struct ucom_softc *, uint8_t);
169 static void ucom_dtr(struct ucom_softc *, uint8_t);
170 static void ucom_rts(struct ucom_softc *, uint8_t);
172 static int ucom_open(struct ucom_softc *sc);
173 static int ucom_close(struct ucom_softc *sc);
174 static void ucom_start(struct tty *tp);
175 static void ucom_stop(struct tty *tp, int);
176 static int ucom_param(struct tty *tp, struct termios *t);
177 static int ucom_modem(struct tty *tp, int sigon, int sigoff);
179 static int ucom_fromtio(int);
180 static int ucom_totio(int);
182 static void disc_optim(struct tty *, struct termios *, struct ucom_softc *);
184 static d_open_t ucom_dev_open;
185 static d_close_t ucom_dev_close;
186 static d_read_t ucom_dev_read;
187 static d_write_t ucom_dev_write;
188 static d_ioctl_t ucom_dev_ioctl;
190 static struct dev_ops ucom_ops = {
191 { "ucom", 0, D_MPSAFE | D_TTY },
192 .d_open = ucom_dev_open,
193 .d_close = ucom_dev_close,
194 .d_read = ucom_dev_read,
195 .d_write = ucom_dev_write,
196 .d_ioctl = ucom_dev_ioctl,
197 .d_kqfilter = ttykqfilter,
198 .d_revoke = ttyrevoke
201 static moduledata_t ucom_mod = {
207 DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
208 MODULE_DEPEND(ucom, usb, 1, 1, 1);
209 MODULE_VERSION(ucom, UCOM_MODVER);
212 #define tty_gone(tp) ((tp->t_state) & (TS_ZOMBIE))
214 #define UCOM_UNIT_MAX 128 /* maximum number of units */
215 #define UCOM_TTY_PREFIX "ucom"
218 #define CALLOUT_MASK 0x80
219 #define CONTROL_MASK 0x60
220 #define CONTROL_INIT_STATE 0x20
221 #define CONTROL_LOCK_STATE 0x40
223 static struct unrhdr *ucom_unrhdr;
224 static struct lock ucom_lock;
225 static int ucom_close_refs;
231 ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
232 lockinit(&ucom_lock, "UCOM LOCK", 0, 0);
234 SYSINIT(ucom_init, SI_BOOT2_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
237 ucom_uninit(void *arg)
248 lockuninit(&ucom_lock);
250 SYSUNINIT(ucom_uninit, SI_BOOT2_KLD - 2, SI_ORDER_ANY, ucom_uninit, NULL);
253 * Mark a unit number (the X in cuaUX) as in use.
255 * Note that devices using a different naming scheme (see ucom_tty_name()
256 * callback) still use this unit allocation.
259 ucom_unit_alloc(void)
264 if (ucom_unrhdr == NULL) {
265 DPRINTF("ucom_unrhdr is NULL\n");
268 unit = alloc_unr(ucom_unrhdr);
269 DPRINTF("unit %d is allocated\n", unit);
274 * Mark the unit number as not in use.
277 ucom_unit_free(int unit)
280 if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
281 DPRINTF("cannot free unit number\n");
284 DPRINTF("unit %d is freed\n", unit);
285 free_unr(ucom_unrhdr, unit);
289 * Setup a group of one or more serial ports.
291 * The lock pointed to by "lock" is applied before all
292 * callbacks are called back. Also "lock" must be applied
293 * before calling into the ucom-layer!
296 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
297 int subunits, void *parent,
298 const struct ucom_callback *callback, struct lock *lock)
305 (callback == NULL) ||
310 /* XXX Do we want our own lock here maybe */
313 /* allocate a uniq unit number */
314 ssc->sc_unit = ucom_unit_alloc();
315 if (ssc->sc_unit == -1)
318 /* generate TTY name string */
319 ksnprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
320 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
322 /* create USB request handling process */
323 error = usb_proc_create(&ssc->sc_tq, lock, "ucom", USB_PRI_MED);
325 ucom_unit_free(ssc->sc_unit);
328 ssc->sc_subunits = subunits;
329 ssc->sc_flag = UCOM_FLAG_ATTACHED |
332 if (callback->ucom_free == NULL)
333 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
335 /* increment reference count */
338 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
339 sc[subunit].sc_subunit = subunit;
340 sc[subunit].sc_super = ssc;
341 sc[subunit].sc_lock = lock;
342 sc[subunit].sc_parent = parent;
343 sc[subunit].sc_callback = callback;
345 error = ucom_attach_tty(ssc, &sc[subunit]);
347 ucom_detach(ssc, &sc[0]);
350 /* increment reference count */
353 /* set subunit attached */
354 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
357 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
358 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
364 * The following function will do nothing if the structure pointed to
365 * by "ssc" and "sc" is zero or has already been detached.
368 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
372 if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
373 return; /* not initialized */
375 destroy_dev(sc->sc_cdev);
376 destroy_dev(sc->sc_cdev_init);
377 destroy_dev(sc->sc_cdev_lock);
378 destroy_dev(sc->sc_cdev2);
379 destroy_dev(sc->sc_cdev2_init);
380 destroy_dev(sc->sc_cdev2_lock);
382 lwkt_gettoken(&tty_token);
384 if (ssc->sc_sysctl_ttyname != NULL) {
385 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
386 ssc->sc_sysctl_ttyname = NULL;
389 if (ssc->sc_sysctl_ttyports != NULL) {
390 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
391 ssc->sc_sysctl_ttyports = NULL;
394 usb_proc_drain(&ssc->sc_tq);
396 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
397 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
399 ucom_detach_tty(ssc, &sc[subunit]);
401 /* avoid duplicate detach */
402 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
405 usb_proc_free(&ssc->sc_tq);
409 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
412 /* make sure we don't detach twice */
413 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
415 lwkt_reltoken(&tty_token);
419 ucom_drain(struct ucom_super_softc *ssc)
421 lockmgr(&ucom_lock, LK_EXCLUSIVE);
422 while (ssc->sc_refs > 0) {
423 kprintf("ucom: Waiting for a TTY device to close.\n");
424 usb_pause_mtx(&ucom_lock, hz);
426 lockmgr(&ucom_lock, LK_RELEASE);
430 ucom_drain_all(void *arg)
432 lockmgr(&ucom_lock, LK_EXCLUSIVE);
433 while (ucom_close_refs > 0) {
434 kprintf("ucom: Waiting for all detached TTY "
435 "devices to have open fds closed.\n");
436 usb_pause_mtx(&ucom_lock, hz);
438 lockmgr(&ucom_lock, LK_RELEASE);
442 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
445 char buf[32]; /* temporary TTY device name buffer */
447 lwkt_gettoken(&tty_token);
449 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
452 lwkt_reltoken(&tty_token);
456 tp->t_sc = (void *)sc;
458 tp->t_oproc = ucom_start;
459 tp->t_param = ucom_param;
460 tp->t_stop = ucom_stop;
462 /* Check if the client has a custom TTY name */
464 if (sc->sc_callback->ucom_tty_name) {
465 sc->sc_callback->ucom_tty_name(sc, buf,
466 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
469 /* Use default TTY name */
470 if (ssc->sc_subunits > 1) {
471 /* multiple modems in one */
472 ksnprintf(buf, sizeof(buf), "%u.%u",
473 ssc->sc_unit, sc->sc_subunit);
476 ksnprintf(buf, sizeof(buf), "%u",
481 sc->sc_cdev = make_dev(&ucom_ops, ssc->sc_unit,
482 UID_ROOT, GID_WHEEL, 0600, "ttyU%s", buf);
483 sc->sc_cdev_init = make_dev(&ucom_ops, ssc->sc_unit | CONTROL_INIT_STATE,
484 UID_ROOT, GID_WHEEL, 0600, "ttyiU%s", buf);
485 sc->sc_cdev_lock = make_dev(&ucom_ops, ssc->sc_unit | CONTROL_LOCK_STATE,
486 UID_ROOT, GID_WHEEL, 0600, "ttylU%s", buf);
487 sc->sc_cdev2 = make_dev(&ucom_ops, ssc->sc_unit | CALLOUT_MASK,
488 UID_UUCP, GID_DIALER, 0660, "cuaU%s", buf);
489 sc->sc_cdev2_init = make_dev(&ucom_ops, ssc->sc_unit | CALLOUT_MASK | CONTROL_INIT_STATE,
490 UID_UUCP, GID_DIALER, 0660, "cuaiU%s", buf);
491 sc->sc_cdev2_lock = make_dev(&ucom_ops, ssc->sc_unit | CALLOUT_MASK | CONTROL_LOCK_STATE,
492 UID_UUCP, GID_DIALER, 0660, "cualU%s", buf);
494 sc->sc_cdev->si_tty = tp;
495 sc->sc_cdev_init->si_tty = tp;
496 sc->sc_cdev_lock->si_tty = tp;
498 sc->sc_cdev->si_drv1 = sc;
499 sc->sc_cdev_init->si_drv1 = sc;
500 sc->sc_cdev_lock->si_drv1 = sc;
502 sc->sc_cdev2->si_tty = tp;
503 sc->sc_cdev2_init->si_tty = tp;
504 sc->sc_cdev2_lock->si_tty = tp;
506 sc->sc_cdev2->si_drv1 = sc;
507 sc->sc_cdev2_init->si_drv1 = sc;
508 sc->sc_cdev2_lock->si_drv1 = sc;
512 DPRINTF("ttycreate: %s\n", buf);
514 /* Check if this device should be a console */
515 if ((ucom_cons_softc == NULL) &&
516 (ssc->sc_unit == ucom_cons_unit) &&
517 (sc->sc_subunit == ucom_cons_subunit)) {
519 DPRINTF("unit %d subunit %d is console",
520 ssc->sc_unit, sc->sc_subunit);
522 ucom_cons_softc = sc;
525 tty_init_console(tp, ucom_cons_baud);
527 tp->t_termios.c_ispeed = ucom_cons_baud;
528 tp->t_termios.c_ospeed = ucom_cons_baud;
530 UCOM_MTX_LOCK(ucom_cons_softc);
531 ucom_cons_rx_low = 0;
532 ucom_cons_rx_high = 0;
533 ucom_cons_tx_low = 0;
534 ucom_cons_tx_high = 0;
535 sc->sc_flag |= UCOM_FLAG_CONSOLE;
536 ucom_open(ucom_cons_softc);
537 ucom_param(tp, &tp->t_termios);
538 UCOM_MTX_UNLOCK(ucom_cons_softc);
541 lwkt_reltoken(&tty_token);
546 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
548 struct tty *tp = sc->sc_tty;
550 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
552 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
553 UCOM_MTX_LOCK(ucom_cons_softc);
554 ucom_close(ucom_cons_softc);
555 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
556 UCOM_MTX_UNLOCK(ucom_cons_softc);
557 ucom_cons_softc = NULL;
560 /* the config thread has been stopped when we get here */
563 sc->sc_flag |= UCOM_FLAG_GONE;
564 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
567 lwkt_gettoken(&tty_token);
572 if (tp->t_state & TS_ISOPEN) {
573 kprintf("device still open, forcing close\n");
574 (*linesw[tp->t_line].l_close)(tp, 0);
577 ucom_close(sc); /* close, if any */
580 * make sure that read and write transfers are stopped
582 if (sc->sc_callback->ucom_stop_read) {
583 (sc->sc_callback->ucom_stop_read) (sc);
585 if (sc->sc_callback->ucom_stop_write) {
586 (sc->sc_callback->ucom_stop_write) (sc);
593 dev_ops_remove_minor(&ucom_ops,ssc->sc_unit);
595 lwkt_reltoken(&tty_token);
600 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
604 struct usb_attach_arg *uaa;
606 ksnprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
607 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
609 /* Store the PNP info in the first interface for the device */
610 uaa = device_get_ivars(dev);
611 iface_index = uaa->info.bIfaceIndex;
613 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
614 device_printf(dev, "Could not set PNP info\n");
617 * The following information is also replicated in the PNP-info
618 * string which is registered above:
620 if (ssc->sc_sysctl_ttyname == NULL) {
622 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
623 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
624 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
625 "TTY device basename");
628 if (ssc->sc_sysctl_ttyports == NULL) {
630 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
631 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
632 OID_AUTO, "ttyports", CTLFLAG_RD,
633 NULL, ssc->sc_subunits, "Number of ports");
639 ucom_queue_command(struct ucom_softc *sc,
640 usb_proc_callback_t *fn, struct termios *pt,
641 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
643 struct ucom_super_softc *ssc = sc->sc_super;
644 struct ucom_param_task *task;
646 UCOM_MTX_ASSERT(sc, MA_OWNED);
648 if (usb_proc_is_gone(&ssc->sc_tq)) {
649 DPRINTF("proc is gone\n");
650 return; /* nothing to do */
653 * NOTE: The task cannot get executed before we drop the
654 * "sc_lock" lock. It is safe to update fields in the message
655 * structure after that the message got queued.
657 task = (struct ucom_param_task *)
658 usb_proc_msignal(&ssc->sc_tq, t0, t1);
660 /* Setup callback and softc pointers */
661 task->hdr.pm_callback = fn;
665 * Make a copy of the termios. This field is only present if
666 * the "pt" field is not NULL.
669 task->termios_copy = *pt;
672 * Closing the device should be synchronous.
674 if (fn == ucom_cfg_close)
675 usb_proc_mwait(&ssc->sc_tq, t0, t1);
678 * In case of multiple configure requests,
679 * keep track of the last one!
681 if (fn == ucom_cfg_start_transfers)
682 sc->sc_last_start_xfer = &task->hdr;
686 ucom_shutdown(struct ucom_softc *sc)
688 struct tty *tp = sc->sc_tty;
690 UCOM_MTX_ASSERT(sc, MA_OWNED);
695 * Hang up if necessary:
697 if (tp->t_termios.c_cflag & HUPCL) {
698 ucom_modem(tp, 0, SER_DTR);
705 * else: taskqueue is draining or gone
708 ucom_cfg_is_gone(struct ucom_softc *sc)
710 struct ucom_super_softc *ssc = sc->sc_super;
712 return (usb_proc_is_gone(&ssc->sc_tq));
716 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
718 struct ucom_cfg_task *task =
719 (struct ucom_cfg_task *)_task;
720 struct ucom_softc *sc = task->sc;
722 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
725 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
726 /* TTY device closed */
730 if (_task == sc->sc_last_start_xfer)
731 sc->sc_flag |= UCOM_FLAG_GP_DATA;
733 if (sc->sc_callback->ucom_start_read) {
734 (sc->sc_callback->ucom_start_read) (sc);
736 if (sc->sc_callback->ucom_start_write) {
737 (sc->sc_callback->ucom_start_write) (sc);
742 ucom_start_transfers(struct ucom_softc *sc)
744 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
748 * Make sure that data transfers are started in both
751 if (sc->sc_callback->ucom_start_read) {
752 (sc->sc_callback->ucom_start_read) (sc);
754 if (sc->sc_callback->ucom_start_write) {
755 (sc->sc_callback->ucom_start_write) (sc);
760 ucom_cfg_open(struct usb_proc_msg *_task)
762 struct ucom_cfg_task *task =
763 (struct ucom_cfg_task *)_task;
764 struct ucom_softc *sc = task->sc;
768 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
774 sc->sc_flag |= UCOM_FLAG_LL_READY;
776 if (sc->sc_callback->ucom_cfg_open) {
777 (sc->sc_callback->ucom_cfg_open) (sc);
780 usb_pause_mtx(sc->sc_lock, hz / 10);
786 ucom_dev_open(struct dev_open_args *ap)
788 cdev_t dev = ap->a_head.a_dev;
789 struct ucom_softc *sc = (struct ucom_softc *)dev->si_drv1;
797 error = ucom_open(sc);
804 ucom_open(struct ucom_softc *sc)
811 mynor = minor(sc->sc_cdev);
813 if (sc->sc_flag & UCOM_FLAG_GONE) {
816 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
820 DPRINTF("tp = %p\n", sc->sc_tty);
822 if (sc->sc_callback->ucom_pre_open) {
824 * give the lower layer a chance to disallow TTY open, for
825 * example if the device is not present:
827 error = (sc->sc_callback->ucom_pre_open) (sc);
832 sc->sc_flag |= UCOM_FLAG_HL_READY;
834 lwkt_gettoken(&tty_token);
839 if (!ISSET(tp->t_state, TS_ISOPEN)) {
842 tp->t_dev = reference_dev(sc->sc_cdev);
844 t = mynor & CALLOUT_MASK ? sc->sc_it_out : sc->sc_it_in;
848 tp->t_iflag = TTYDEF_IFLAG;
849 tp->t_oflag = TTYDEF_OFLAG;
850 tp->t_lflag = TTYDEF_LFLAG;
854 /* Disable transfers */
855 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
861 /* reset programmed line state */
866 /* reset jitter buffer */
867 sc->sc_jitterbuf_in = 0;
868 sc->sc_jitterbuf_out = 0;
870 ucom_queue_command(sc, ucom_cfg_open, NULL,
871 &sc->sc_open_task[0].hdr,
872 &sc->sc_open_task[1].hdr);
874 /* Queue transfer enable command last */
875 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
876 &sc->sc_start_task[0].hdr,
877 &sc->sc_start_task[1].hdr);
879 ucom_modem(sc->sc_tty, SER_DTR | SER_RTS, 0);
885 ucom_status_change(sc);
887 if (ISSET(sc->sc_msr, SER_DCD)) {
888 (*linesw[tp->t_line].l_modem)(tp, 1);
893 error = ttyopen(sc->sc_cdev, tp);
895 lwkt_reltoken(&tty_token);
899 error = (*linesw[tp->t_line].l_open)(sc->sc_cdev, tp);
901 lwkt_reltoken(&tty_token);
905 disc_optim(tp, &tp->t_termios, sc);
907 lwkt_reltoken(&tty_token);
913 ucom_cfg_close(struct usb_proc_msg *_task)
915 struct ucom_cfg_task *task =
916 (struct ucom_cfg_task *)_task;
917 struct ucom_softc *sc = task->sc;
921 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
922 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
923 if (sc->sc_callback->ucom_cfg_close)
924 (sc->sc_callback->ucom_cfg_close) (sc);
931 ucom_dev_close(struct dev_close_args *ap)
933 cdev_t dev = ap->a_head.a_dev;
934 struct ucom_softc *sc = (struct ucom_softc *)dev->si_drv1;
938 error = ucom_close(sc);
945 ucom_close(struct ucom_softc *sc)
947 struct tty *tp = sc->sc_tty;
950 DPRINTF("tp=%p\n", tp);
952 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
953 DPRINTF("tp=%p already closed\n", tp);
956 if (!ISSET(tp->t_state, TS_ISOPEN)) {
961 ucom_queue_command(sc, ucom_cfg_close, NULL,
962 &sc->sc_close_task[0].hdr,
963 &sc->sc_close_task[1].hdr);
965 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
967 if (sc->sc_callback->ucom_stop_read) {
968 (sc->sc_callback->ucom_stop_read) (sc);
971 lwkt_gettoken(&tty_token);
973 (*linesw[tp->t_line].l_close)(tp, 0); /* XXX: flags */
974 disc_optim(tp, &tp->t_termios, sc);
979 release_dev(tp->t_dev);
982 /* XXX: Detach wakeup */
983 lwkt_reltoken(&tty_token);
990 ucom_inwakeup(struct tty *tp)
992 struct ucom_softc *sc = tty_softc(tp);
998 UCOM_MTX_ASSERT(sc, MA_OWNED);
1000 DPRINTF("tp=%p\n", tp);
1002 if (ttydisc_can_bypass(tp) != 0 ||
1003 (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
1004 (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
1008 /* prevent recursion */
1009 sc->sc_flag |= UCOM_FLAG_INWAKEUP;
1011 pos = sc->sc_jitterbuf_out;
1013 while (sc->sc_jitterbuf_in != pos) {
1016 c = (char)sc->sc_jitterbuf[pos];
1018 if (ttydisc_rint(tp, c, 0) == -1)
1021 if (pos >= UCOM_JITTERBUF_SIZE)
1022 pos -= UCOM_JITTERBUF_SIZE;
1025 sc->sc_jitterbuf_out = pos;
1027 /* clear RTS in async fashion */
1028 if ((sc->sc_jitterbuf_in == pos) &&
1029 (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
1032 sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
1037 ucom_dev_read(struct dev_read_args *ap)
1039 cdev_t dev = ap->a_head.a_dev;
1040 struct ucom_softc *sc;
1046 lwkt_gettoken(&tty_token);
1053 DPRINTF("tp = %p, flag = 0x%x\n", tp, ap->a_ioflag);
1055 /* must not be locked in case it blocks */
1056 /*UCOM_MTX_LOCK(sc);*/
1057 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
1058 /*UCOM_MTX_UNLOCK(sc);*/
1060 DPRINTF("error = %d\n", error);
1062 lwkt_reltoken(&tty_token);
1067 ucom_dev_write(struct dev_write_args *ap)
1069 cdev_t dev = ap->a_head.a_dev;
1070 struct ucom_softc *sc;
1074 lwkt_gettoken(&tty_token);
1080 DPRINTF("tp = %p, flag = 0x%x\n", tp, ap->a_ioflag);
1082 /* must not be locked in case it blocks */
1083 /*UCOM_MTX_LOCK(sc);*/
1084 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
1085 /*UCOM_MTX_UNLOCK(sc);*/
1087 DPRINTF("ucomwrite: error = %d\n", error);
1089 lwkt_reltoken(&tty_token);
1094 ucom_dev_ioctl(struct dev_ioctl_args *ap)
1096 cdev_t dev = ap->a_head.a_dev;
1097 struct ucom_softc *sc = (struct ucom_softc *)dev->si_drv1;
1098 u_long cmd = ap->a_cmd;
1099 caddr_t data = ap->a_data;
1100 struct tty *tp = sc->sc_tty;
1106 lwkt_gettoken(&tty_token);
1110 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1111 lwkt_reltoken(&tty_token);
1112 UCOM_MTX_UNLOCK(sc);
1115 DPRINTF("cmd = 0x%08lx\n", cmd);
1116 if (mynor & CONTROL_MASK) {
1119 switch (mynor & CONTROL_MASK) {
1120 case CONTROL_INIT_STATE:
1121 ct = mynor & CALLOUT_MASK ? &sc->sc_it_out : &sc->sc_it_in;
1123 case CONTROL_LOCK_STATE:
1124 ct = mynor & CALLOUT_MASK ? &sc->sc_lt_out : &sc->sc_lt_in;
1127 lwkt_reltoken(&tty_token);
1128 UCOM_MTX_UNLOCK(sc);
1129 return (ENODEV); /* /dev/nodev */
1131 switch (ap->a_cmd) {
1133 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1135 lwkt_reltoken(&tty_token);
1136 UCOM_MTX_UNLOCK(sc);
1139 *ct = *(struct termios *)data;
1140 lwkt_reltoken(&tty_token);
1141 UCOM_MTX_UNLOCK(sc);
1144 *(struct termios *)data = *ct;
1145 lwkt_reltoken(&tty_token);
1146 UCOM_MTX_UNLOCK(sc);
1149 *(int *)data = TTYDISC;
1150 lwkt_reltoken(&tty_token);
1151 UCOM_MTX_UNLOCK(sc);
1154 bzero(data, sizeof(struct winsize));
1155 lwkt_reltoken(&tty_token);
1156 UCOM_MTX_UNLOCK(sc);
1159 lwkt_reltoken(&tty_token);
1160 UCOM_MTX_UNLOCK(sc);
1165 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1166 ap->a_fflag, ap->a_cred);
1168 if (error != ENOIOCTL) {
1169 DPRINTF("ucomioctl: l_ioctl: error = %d\n", error);
1170 lwkt_reltoken(&tty_token);
1171 UCOM_MTX_UNLOCK(sc);
1177 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1178 disc_optim(tp, &tp->t_termios, sc);
1179 if (error != ENOIOCTL) {
1181 DPRINTF("ucomioctl: ttioctl: error = %d\n", error);
1182 lwkt_reltoken(&tty_token);
1183 UCOM_MTX_UNLOCK(sc);
1216 d = *(int *)ap->a_data;
1217 DPRINTF("ucomioctl: TIOCMSET, 0x%x\n", d);
1218 ucom_modem(tp, ucom_fromtio(d), 0);
1221 d = ucom_modem(tp, 0, 0);
1222 DPRINTF("ucomioctl: TIOCMGET, 0x%x\n", d);
1223 *(int *)ap->a_data = ucom_totio(d);
1224 ucom_status_change(sc);
1227 d = *(int *)ap->a_data;
1228 ucom_modem(tp, ucom_fromtio(d), 0);
1231 d = *(int *)ap->a_data;
1232 ucom_modem(tp, 0, ucom_fromtio(d));
1235 if (sc->sc_callback->ucom_ioctl) {
1236 error = (sc->sc_callback->ucom_ioctl)
1237 (sc, cmd, data, 0, curthread);
1241 lwkt_reltoken(&tty_token);
1242 UCOM_MTX_UNLOCK(sc);
1249 if (error == ENOIOCTL)
1250 error = pps_ioctl(cmd, data, &sc->sc_pps);
1255 lwkt_reltoken(&tty_token);
1256 UCOM_MTX_UNLOCK(sc);
1262 ucom_totio(int bits)
1266 SET(bits, TIOCM_LE);
1268 if (ISSET(bits, SER_DTR)) {
1269 SET(rbits, TIOCM_DTR);
1271 if (ISSET(bits, SER_RTS)) {
1272 SET(rbits, TIOCM_RTS);
1274 if (ISSET(bits, SER_CTS)) {
1275 SET(rbits, TIOCM_CTS);
1277 if (ISSET(bits, SER_DCD)) {
1278 SET(rbits, TIOCM_CD);
1280 if (ISSET(bits, SER_DSR)) {
1281 SET(rbits, TIOCM_DSR);
1283 if (ISSET(bits, SER_RI)) {
1284 SET(rbits, TIOCM_RI);
1291 ucom_fromtio(int bits)
1295 if (ISSET(bits, TIOCM_DTR)) {
1296 SET(rbits, SER_DTR);
1298 if (ISSET(bits, TIOCM_RTS)) {
1299 SET(rbits, SER_RTS);
1301 if (ISSET(bits, TIOCM_CTS)) {
1302 SET(rbits, SER_CTS);
1304 if (ISSET(bits, TIOCM_CD)) {
1305 SET(rbits, SER_DCD);
1307 if (ISSET(bits, TIOCM_DSR)) {
1308 SET(rbits, SER_DSR);
1310 if (ISSET(bits, TIOCM_RI)) {
1318 ucom_modem(struct tty *tp, int sigon, int sigoff)
1320 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1323 UCOM_MTX_ASSERT(sc, MA_OWNED);
1325 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1328 if ((sigon == 0) && (sigoff == 0)) {
1330 if (sc->sc_mcr & SER_DTR) {
1333 if (sc->sc_mcr & SER_RTS) {
1336 if (sc->sc_msr & SER_CTS) {
1339 if (sc->sc_msr & SER_DCD) {
1342 if (sc->sc_msr & SER_DSR) {
1345 if (sc->sc_msr & SER_RI) {
1350 if (sigon & SER_DTR) {
1351 sc->sc_mcr |= SER_DTR;
1353 if (sigoff & SER_DTR) {
1354 sc->sc_mcr &= ~SER_DTR;
1356 if (sigon & SER_RTS) {
1357 sc->sc_mcr |= SER_RTS;
1359 if (sigoff & SER_RTS) {
1360 sc->sc_mcr &= ~SER_RTS;
1362 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
1363 ucom_dtr(sc, onoff);
1365 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
1366 ucom_rts(sc, onoff);
1372 ucom_cfg_line_state(struct usb_proc_msg *_task)
1374 struct ucom_cfg_task *task =
1375 (struct ucom_cfg_task *)_task;
1376 struct ucom_softc *sc = task->sc;
1383 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1388 /* compute callback mask */
1389 if (sc->sc_callback->ucom_cfg_set_dtr)
1390 mask |= UCOM_LS_DTR;
1391 if (sc->sc_callback->ucom_cfg_set_rts)
1392 mask |= UCOM_LS_RTS;
1393 if (sc->sc_callback->ucom_cfg_set_break)
1394 mask |= UCOM_LS_BREAK;
1395 if (sc->sc_callback->ucom_cfg_set_ring)
1396 mask |= UCOM_LS_RING;
1398 /* compute the bits we are to program */
1399 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
1400 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
1401 prev_value = sc->sc_pls_curr ^ notch_bits;
1402 last_value = sc->sc_pls_curr;
1404 /* reset programmed line state */
1405 sc->sc_pls_curr = 0;
1409 /* ensure that we don't lose any levels */
1410 if (notch_bits & UCOM_LS_DTR)
1411 sc->sc_callback->ucom_cfg_set_dtr(sc,
1412 (prev_value & UCOM_LS_DTR) ? 1 : 0);
1413 if (notch_bits & UCOM_LS_RTS)
1414 sc->sc_callback->ucom_cfg_set_rts(sc,
1415 (prev_value & UCOM_LS_RTS) ? 1 : 0);
1416 if (notch_bits & UCOM_LS_BREAK)
1417 sc->sc_callback->ucom_cfg_set_break(sc,
1418 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
1419 if (notch_bits & UCOM_LS_RING)
1420 sc->sc_callback->ucom_cfg_set_ring(sc,
1421 (prev_value & UCOM_LS_RING) ? 1 : 0);
1423 /* set last value */
1424 if (any_bits & UCOM_LS_DTR)
1425 sc->sc_callback->ucom_cfg_set_dtr(sc,
1426 (last_value & UCOM_LS_DTR) ? 1 : 0);
1427 if (any_bits & UCOM_LS_RTS)
1428 sc->sc_callback->ucom_cfg_set_rts(sc,
1429 (last_value & UCOM_LS_RTS) ? 1 : 0);
1430 if (any_bits & UCOM_LS_BREAK)
1431 sc->sc_callback->ucom_cfg_set_break(sc,
1432 (last_value & UCOM_LS_BREAK) ? 1 : 0);
1433 if (any_bits & UCOM_LS_RING)
1434 sc->sc_callback->ucom_cfg_set_ring(sc,
1435 (last_value & UCOM_LS_RING) ? 1 : 0);
1439 ucom_line_state(struct ucom_softc *sc,
1440 uint8_t set_bits, uint8_t clear_bits)
1442 UCOM_MTX_ASSERT(sc, MA_OWNED);
1444 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1448 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1450 /* update current programmed line state */
1451 sc->sc_pls_curr |= set_bits;
1452 sc->sc_pls_curr &= ~clear_bits;
1453 sc->sc_pls_set |= set_bits;
1454 sc->sc_pls_clr |= clear_bits;
1456 /* defer driver programming */
1457 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1458 &sc->sc_line_state_task[0].hdr,
1459 &sc->sc_line_state_task[1].hdr);
1463 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1465 DPRINTF("onoff = %d\n", onoff);
1468 ucom_line_state(sc, UCOM_LS_RING, 0);
1470 ucom_line_state(sc, 0, UCOM_LS_RING);
1474 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1476 DPRINTF("onoff = %d\n", onoff);
1479 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1481 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1485 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1487 DPRINTF("onoff = %d\n", onoff);
1490 ucom_line_state(sc, UCOM_LS_DTR, 0);
1492 ucom_line_state(sc, 0, UCOM_LS_DTR);
1496 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1498 DPRINTF("onoff = %d\n", onoff);
1501 ucom_line_state(sc, UCOM_LS_RTS, 0);
1503 ucom_line_state(sc, 0, UCOM_LS_RTS);
1507 ucom_cfg_status_change(struct usb_proc_msg *_task)
1509 struct ucom_cfg_task *task =
1510 (struct ucom_cfg_task *)_task;
1511 struct ucom_softc *sc = task->sc;
1520 UCOM_MTX_ASSERT(sc, MA_OWNED);
1522 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1525 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1533 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1535 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1536 /* TTY device closed */
1539 msr_delta = (sc->sc_msr ^ new_msr);
1540 lsr_delta = (sc->sc_lsr ^ new_lsr);
1542 sc->sc_msr = new_msr;
1543 sc->sc_lsr = new_lsr;
1545 #if 0 /* missing pps_capture */
1547 * Time pulse counting support. Note that both CTS and DCD are
1548 * active-low signals. The status bit is high to indicate that
1549 * the signal on the line is low, which corresponds to a PPS
1552 switch(ucom_pps_mode) {
1554 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1555 (msr_delta & SER_CTS)) {
1556 pps_capture(&sc->sc_pps);
1557 pps_event(&sc->sc_pps, (sc->sc_msr & SER_CTS) ?
1558 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
1562 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1563 (msr_delta & SER_DCD)) {
1564 pps_capture(&sc->sc_pps);
1565 pps_event(&sc->sc_pps, (sc->sc_msr & SER_DCD) ?
1566 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
1574 if (msr_delta & SER_DCD) {
1576 int onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1578 DPRINTF("DCD changed to %d\n", onoff);
1580 (*linesw[tp->t_line].l_modem)(tp, onoff);
1583 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1585 DPRINTF("BREAK detected\n");
1586 (*linesw[tp->t_line].l_rint)(0, tp);
1589 ttydisc_rint(tp, 0, TRE_BREAK);
1590 ttydisc_rint_done(tp);
1594 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1596 DPRINTF("Frame error detected\n");
1597 (*linesw[tp->t_line].l_rint)(0, tp);
1600 ttydisc_rint(tp, 0, TRE_FRAMING);
1601 ttydisc_rint_done(tp);
1605 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1607 DPRINTF("Parity error detected\n");
1608 (*linesw[tp->t_line].l_rint)(0, tp);
1610 ttydisc_rint(tp, 0, TRE_PARITY);
1611 ttydisc_rint_done(tp);
1617 ucom_status_change(struct ucom_softc *sc)
1619 UCOM_MTX_ASSERT(sc, MA_OWNED);
1621 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1622 return; /* not supported */
1624 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1629 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1630 &sc->sc_status_task[0].hdr,
1631 &sc->sc_status_task[1].hdr);
1635 ucom_cfg_param(struct usb_proc_msg *_task)
1637 struct ucom_param_task *task =
1638 (struct ucom_param_task *)_task;
1639 struct ucom_softc *sc = task->sc;
1641 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1644 if (sc->sc_callback->ucom_cfg_param == NULL) {
1648 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1651 usb_pause_mtx(sc->sc_lock, hz / 10);
1655 ucom_param(struct tty *tp, struct termios *t)
1657 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1661 lwkt_gettoken(&tty_token);
1662 UCOM_MTX_ASSERT(sc, MA_OWNED);
1667 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1669 /* XXX the TTY layer should call "open()" first! */
1671 * Not quite: Its ordering is partly backwards, but
1672 * some parameters must be set early in ttydev_open(),
1673 * possibly before calling ttydevsw_open().
1675 error = ucom_open(sc);
1682 DPRINTF("sc = %p\n", sc);
1684 /* Check requested parameters. */
1685 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1686 /* XXX c_ospeed == 0 is perfectly valid. */
1687 DPRINTF("mismatch ispeed and ospeed\n");
1691 t->c_ispeed = t->c_ospeed;
1693 if (sc->sc_callback->ucom_pre_param) {
1694 /* Let the lower layer verify the parameters */
1695 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1697 DPRINTF("callback error = %d\n", error);
1702 /* Disable transfers */
1703 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1705 /* Queue baud rate programming command first */
1706 ucom_queue_command(sc, ucom_cfg_param, t,
1707 &sc->sc_param_task[0].hdr,
1708 &sc->sc_param_task[1].hdr);
1710 /* Queue transfer enable command last */
1711 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1712 &sc->sc_start_task[0].hdr,
1713 &sc->sc_start_task[1].hdr);
1715 if (t->c_cflag & CRTS_IFLOW) {
1716 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1717 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1718 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1719 ucom_modem(tp, SER_RTS, 0);
1728 lwkt_reltoken(&tty_token);
1733 ucom_start(struct tty *tp)
1735 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1738 /* may be called locked or unlocked */
1739 if (lockowned(sc->sc_lock)) {
1745 /* UCOM_MTX_ASSERT(sc, MA_OWNED); */
1747 DPRINTF("sc = %p\n", sc);
1749 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1750 /* The higher layer is not ready */
1752 UCOM_MTX_UNLOCK(sc);
1756 lwkt_gettoken(&tty_token);
1759 if (tp->t_state & TS_TBLOCK) {
1760 if (ISSET(sc->sc_mcr, SER_RTS) &&
1761 ISSET(sc->sc_flag, UCOM_FLAG_RTS_IFLOW)) {
1762 DPRINTF("ucom_start: clear RTS\n");
1763 (void)ucom_modem(tp, 0, SER_RTS);
1766 if (!ISSET(sc->sc_mcr, SER_RTS) &&
1767 tp->t_rawq.c_cc <= tp->t_ilowat &&
1768 ISSET(sc->sc_flag, UCOM_FLAG_RTS_IFLOW)) {
1769 DPRINTF("ucom_start: set RTS\n");
1770 (void)ucom_modem(tp, SER_RTS, 0);
1774 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
1776 DPRINTF("ucom_start: stopped\n");
1780 if (tp->t_outq.c_cc <= tp->t_olowat) {
1781 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
1782 CLR(tp->t_state, TS_SO_OLOWAT);
1783 wakeup(TSA_OLOWAT(tp));
1785 KNOTE(&tp->t_wkq.ki_note, 0);
1786 if (tp->t_outq.c_cc == 0) {
1787 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
1788 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
1789 CLR(tp->t_state, TS_SO_OCOMPLETE);
1790 wakeup(TSA_OCOMPLETE(tp));
1796 DPRINTF("about to start write?\n");
1797 ucom_start_transfers(sc);
1803 lwkt_reltoken(&tty_token);
1805 UCOM_MTX_UNLOCK(sc);
1809 ucom_stop(struct tty *tp, int flag)
1811 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1813 DPRINTF("sc = %p, x = 0x%x\n", sc, flag);
1815 lwkt_gettoken(&tty_token);
1818 * This is just supposed to flush pending receive data,
1819 * not stop the reception of data entirely!
1822 if (sc->sc_callback->ucom_stop_read) {
1823 (sc->sc_callback->ucom_stop_read) (sc);
1825 if (sc->sc_callback->ucom_start_read) {
1826 (sc->sc_callback->ucom_start_read) (sc);
1834 if (flag & FWRITE) {
1837 if (ISSET(tp->t_state, TS_BUSY)) {
1839 if (!ISSET(tp->t_state, TS_TTSTOP))
1840 SET(tp->t_state, TS_FLUSH);
1846 lwkt_reltoken(&tty_token);
1849 /*------------------------------------------------------------------------*
1852 * len: maximum length of data to get
1854 * Get data from the TTY layer
1857 * 0: No data is available.
1858 * Else: Data is available.
1859 *------------------------------------------------------------------------*/
1861 /* Copy data from the tty layer to usb */
1863 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1864 uint32_t offset, uint32_t len, uint32_t *actlen)
1866 struct usb_page_search res;
1867 struct tty *tp = sc->sc_tty;
1869 uint32_t offset_orig;
1873 UCOM_MTX_ASSERT(sc, MA_OWNED);
1875 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1878 /* get total TX length */
1880 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1881 temp %= UCOM_CONS_BUFSIZE;
1883 /* limit TX length */
1885 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1886 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1893 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1895 /* update counters */
1897 ucom_cons_tx_low += temp;
1898 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1900 /* store actual length */
1904 return (temp ? 1 : 0);
1908 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1910 return (0); /* multiport device polling */
1913 offset_orig = offset;
1915 lwkt_gettoken(&tty_token);
1918 usbd_get_page(pc, offset, &res);
1920 /* Buffer bigger than max requested data */
1921 if (res.length > len) {
1924 /* copy data directly into USB buffer */
1925 SET(tp->t_state, TS_BUSY);
1926 cnt = q_to_b(&tp->t_outq, res.buffer, len);
1928 DPRINTF("ucom_get_data: cnt == 0\n");
1929 CLR(tp->t_state, TS_BUSY);
1933 CLR(tp->t_state, TS_BUSY);
1935 /* XXX mp: This breaks avrdude,
1936 does the flush need to happen
1938 if (ISSET(tp->t_state, TS_FLUSH))
1939 CLR(tp->t_state, TS_FLUSH);
1941 ndflush(&tp->t_outq,cnt);
1947 if (cnt < res.length) {
1953 lwkt_reltoken(&tty_token);
1955 actlen[0] = offset - offset_orig;
1957 DPRINTF("cnt=%d\n", actlen[0]);
1959 if (actlen[0] == 0) {
1966 * Write data to the tty layer
1970 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1971 uint32_t offset, uint32_t len)
1973 struct usb_page_search res;
1974 struct tty *tp = sc->sc_tty;
1981 UCOM_MTX_ASSERT(sc, MA_OWNED);
1982 lwkt_gettoken(&tty_token);
1984 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1987 /* get maximum RX length */
1989 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1990 temp %= UCOM_CONS_BUFSIZE;
1992 /* limit RX length */
1994 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1995 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
2002 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
2004 /* update counters */
2006 ucom_cons_rx_high += temp;
2007 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
2009 lwkt_reltoken(&tty_token);
2014 lwkt_reltoken(&tty_token);
2015 return; /* multiport device polling */
2018 lwkt_reltoken(&tty_token);
2019 return; /* no data */
2022 /* set a flag to prevent recursation ? */
2026 usbd_get_page(pc, offset, &res);
2028 if (res.length > len) {
2032 offset += res.length;
2034 /* pass characters to tty layer */
2039 /* first check if we can pass the buffer directly */
2041 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
2042 /* clear any jitter buffer */
2043 sc->sc_jitterbuf_in = 0;
2044 sc->sc_jitterbuf_out = 0;
2046 if (tp->t_rawq.c_cc + cnt > tp->t_ihiwat
2047 && (sc->sc_flag & UCOM_FLAG_RTS_IFLOW
2048 || tp->t_iflag & IXOFF)
2049 && !(tp->t_state & TS_TBLOCK))
2051 lostcc = b_to_q((char *)buf, cnt, &tp->t_rawq);
2055 if (*buf == sc->hotchar)
2064 if (tp->t_state & TS_TTSTOP
2065 && (tp->t_iflag & IXANY
2066 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
2067 tp->t_state &= ~TS_TTSTOP;
2068 tp->t_lflag &= ~FLUSHO;
2072 kprintf("lost %d chars\n", lostcc);
2075 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
2076 DPRINTF("tp=%p, data lost\n", tp);
2082 for (cnt = 0; cnt != res.length; cnt++) {
2083 if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
2084 (*linesw[tp->t_line].l_rint)((unsigned char)buf[cnt], tp) == -1) {
2088 pos = sc->sc_jitterbuf_in;
2089 end = sc->sc_jitterbuf_out +
2090 UCOM_JITTERBUF_SIZE - 1;
2092 if (end >= UCOM_JITTERBUF_SIZE)
2093 end -= UCOM_JITTERBUF_SIZE;
2095 for (; cnt != res.length; cnt++) {
2098 sc->sc_jitterbuf[pos] = buf[cnt];
2100 if (pos >= UCOM_JITTERBUF_SIZE)
2101 pos -= UCOM_JITTERBUF_SIZE;
2104 sc->sc_jitterbuf_in = pos;
2106 /* set RTS in async fashion */
2107 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
2110 DPRINTF("tp=%p, lost %d "
2111 "chars\n", tp, res.length - cnt);
2118 lwkt_reltoken(&tty_token);
2120 ttydisc_rint_done(tp);
2126 ucom_free(void *xsc)
2128 struct ucom_softc *sc = xsc;
2130 if (sc->sc_callback->ucom_free != NULL)
2131 sc->sc_callback->ucom_free(sc);
2133 /*ucom_unref(sc->sc_super) XXX hack, see end of ucom_detach_tty() */;
2135 lockmgr(&ucom_lock, LK_EXCLUSIVE);
2137 lockmgr(&ucom_lock, LK_RELEASE);
2140 static cn_probe_t ucom_cnprobe;
2141 static cn_init_t ucom_cninit;
2142 static cn_term_t ucom_cnterm;
2143 static cn_getc_t ucom_cngetc;
2144 static cn_putc_t ucom_cnputc;
2147 static cn_grab_t ucom_cngrab;
2148 static cn_ungrab_t ucom_cnungrab;
2149 CONSOLE_DRIVER(ucom);
2153 ucom_cnprobe(struct consdev *cp)
2155 if (ucom_cons_unit != -1)
2156 cp->cn_pri = CN_NORMAL;
2158 cp->cn_pri = CN_DEAD;
2161 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
2166 ucom_cninit(struct consdev *cp)
2171 ucom_cnterm(struct consdev *cp)
2176 ucom_cngrab(struct consdev *cp)
2181 ucom_cnungrab(struct consdev *cp)
2186 ucom_cngetc(struct consdev *cd)
2188 struct ucom_softc *sc = ucom_cons_softc;
2196 if (ucom_cons_rx_low != ucom_cons_rx_high) {
2197 c = ucom_cons_rx_buf[ucom_cons_rx_low];
2198 ucom_cons_rx_low ++;
2199 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
2204 /* start USB transfers */
2205 ucom_outwakeup(sc->sc_tty);
2207 UCOM_MTX_UNLOCK(sc);
2209 /* poll if necessary */
2211 if (kdb_active && sc->sc_callback->ucom_poll)
2212 (sc->sc_callback->ucom_poll) (sc);
2218 ucom_cnputc(void *cd, int c)
2220 ucom_cnputc(struct consdev *cd, int c)
2224 struct ucom_softc *sc = ucom_cons_softc;
2234 /* compute maximum TX length */
2236 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
2237 temp %= UCOM_CONS_BUFSIZE;
2240 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
2241 ucom_cons_tx_high ++;
2242 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
2245 /* start USB transfers */
2246 ucom_outwakeup(sc->sc_tty);
2248 UCOM_MTX_UNLOCK(sc);
2250 /* poll if necessary */
2252 if (kdb_active && sc->sc_callback->ucom_poll) {
2253 (sc->sc_callback->ucom_poll) (sc);
2254 /* simple flow control */
2261 /*------------------------------------------------------------------------*
2264 * This function will increment the super UCOM reference count.
2265 *------------------------------------------------------------------------*/
2267 ucom_ref(struct ucom_super_softc *ssc)
2269 lockmgr(&ucom_lock, LK_EXCLUSIVE);
2271 lockmgr(&ucom_lock, LK_RELEASE);
2274 /*------------------------------------------------------------------------*
2277 * This function will free the super UCOM's allocated unit
2278 * number. This function can be called on a zero-initialized
2279 * structure. This function can be called multiple times.
2280 *------------------------------------------------------------------------*/
2282 ucom_free_unit(struct ucom_super_softc *ssc)
2284 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
2287 ucom_unit_free(ssc->sc_unit);
2289 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
2292 /*------------------------------------------------------------------------*
2295 * This function will decrement the super UCOM reference count.
2298 * 0: UCOM structures are still referenced.
2299 * Else: UCOM structures are no longer referenced.
2300 *------------------------------------------------------------------------*/
2302 ucom_unref(struct ucom_super_softc *ssc)
2306 lockmgr(&ucom_lock, LK_EXCLUSIVE);
2307 retval = (ssc->sc_refs < 2);
2309 lockmgr(&ucom_lock, LK_RELEASE);
2312 ucom_free_unit(ssc);
2318 * NOTE: Must be called with tty_token held.
2321 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
2323 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2324 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2325 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2326 && (!(t->c_iflag & PARMRK)
2327 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2328 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2329 && linesw[tp->t_line].l_rint == ttyinput) {
2330 DPRINTF("disc_optim: bypass l_rint\n");
2331 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2333 DPRINTF("disc_optim: can't bypass l_rint\n");
2334 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2336 sc->hotchar = linesw[tp->t_line].l_hotchar;
2341 #include <gdb/gdb.h>
2343 static gdb_probe_f ucom_gdbprobe;
2344 static gdb_init_f ucom_gdbinit;
2345 static gdb_term_f ucom_gdbterm;
2346 static gdb_getc_f ucom_gdbgetc;
2347 static gdb_putc_f ucom_gdbputc;
2349 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
2354 return ((ucom_cons_softc != NULL) ? 0 : -1);
2370 ucom_cnputc(NULL, c);
2376 return (ucom_cngetc(NULL));