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);
383 lwkt_gettoken(&sc->sc_tty->t_token);
385 if (ssc->sc_sysctl_ttyname != NULL) {
386 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
387 ssc->sc_sysctl_ttyname = NULL;
390 if (ssc->sc_sysctl_ttyports != NULL) {
391 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
392 ssc->sc_sysctl_ttyports = NULL;
395 usb_proc_drain(&ssc->sc_tq);
397 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
398 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
400 ucom_detach_tty(ssc, &sc[subunit]);
402 /* avoid duplicate detach */
403 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
406 usb_proc_free(&ssc->sc_tq);
410 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
413 /* make sure we don't detach twice */
414 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
417 lwkt_reltoken(&sc->sc_tty->t_token);
421 ucom_drain(struct ucom_super_softc *ssc)
423 lockmgr(&ucom_lock, LK_EXCLUSIVE);
424 while (ssc->sc_refs > 0) {
425 kprintf("ucom: Waiting for a TTY device to close.\n");
426 usb_pause_mtx(&ucom_lock, hz);
428 lockmgr(&ucom_lock, LK_RELEASE);
432 ucom_drain_all(void *arg)
434 lockmgr(&ucom_lock, LK_EXCLUSIVE);
435 while (ucom_close_refs > 0) {
436 kprintf("ucom: Waiting for all detached TTY "
437 "devices to have open fds closed.\n");
438 usb_pause_mtx(&ucom_lock, hz);
440 lockmgr(&ucom_lock, LK_RELEASE);
444 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
447 char buf[32]; /* temporary TTY device name buffer */
449 tp = ttymalloc(&sc->sc_tty);
451 lwkt_gettoken(&tp->t_token);
453 tp->t_sc = (void *)sc;
455 tp->t_oproc = ucom_start;
456 tp->t_param = ucom_param;
457 tp->t_stop = ucom_stop;
459 /* Check if the client has a custom TTY name */
461 if (sc->sc_callback->ucom_tty_name) {
462 sc->sc_callback->ucom_tty_name(sc, buf,
463 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
466 /* Use default TTY name */
467 if (ssc->sc_subunits > 1) {
468 /* multiple modems in one */
469 ksnprintf(buf, sizeof(buf), "%u.%u",
470 ssc->sc_unit, sc->sc_subunit);
473 ksnprintf(buf, sizeof(buf), "%u",
478 sc->sc_cdev = make_dev(&ucom_ops, ssc->sc_unit,
479 UID_ROOT, GID_WHEEL, 0600, "ttyU%s", buf);
480 sc->sc_cdev_init = make_dev(&ucom_ops, ssc->sc_unit | CONTROL_INIT_STATE,
481 UID_ROOT, GID_WHEEL, 0600, "ttyiU%s", buf);
482 sc->sc_cdev_lock = make_dev(&ucom_ops, ssc->sc_unit | CONTROL_LOCK_STATE,
483 UID_ROOT, GID_WHEEL, 0600, "ttylU%s", buf);
484 sc->sc_cdev2 = make_dev(&ucom_ops, ssc->sc_unit | CALLOUT_MASK,
485 UID_UUCP, GID_DIALER, 0660, "cuaU%s", buf);
486 sc->sc_cdev2_init = make_dev(&ucom_ops, ssc->sc_unit | CALLOUT_MASK | CONTROL_INIT_STATE,
487 UID_UUCP, GID_DIALER, 0660, "cuaiU%s", buf);
488 sc->sc_cdev2_lock = make_dev(&ucom_ops, ssc->sc_unit | CALLOUT_MASK | CONTROL_LOCK_STATE,
489 UID_UUCP, GID_DIALER, 0660, "cualU%s", buf);
491 sc->sc_cdev->si_tty = tp;
492 sc->sc_cdev_init->si_tty = tp;
493 sc->sc_cdev_lock->si_tty = tp;
495 sc->sc_cdev->si_drv1 = sc;
496 sc->sc_cdev_init->si_drv1 = sc;
497 sc->sc_cdev_lock->si_drv1 = sc;
499 sc->sc_cdev2->si_tty = tp;
500 sc->sc_cdev2_init->si_tty = tp;
501 sc->sc_cdev2_lock->si_tty = tp;
503 sc->sc_cdev2->si_drv1 = sc;
504 sc->sc_cdev2_init->si_drv1 = sc;
505 sc->sc_cdev2_lock->si_drv1 = sc;
509 DPRINTF("ttycreate: %s\n", buf);
511 /* Check if this device should be a console */
512 if ((ucom_cons_softc == NULL) &&
513 (ssc->sc_unit == ucom_cons_unit) &&
514 (sc->sc_subunit == ucom_cons_subunit)) {
516 DPRINTF("unit %d subunit %d is console",
517 ssc->sc_unit, sc->sc_subunit);
519 ucom_cons_softc = sc;
522 tty_init_console(tp, ucom_cons_baud);
524 tp->t_termios.c_ispeed = ucom_cons_baud;
525 tp->t_termios.c_ospeed = ucom_cons_baud;
527 UCOM_MTX_LOCK(ucom_cons_softc);
528 ucom_cons_rx_low = 0;
529 ucom_cons_rx_high = 0;
530 ucom_cons_tx_low = 0;
531 ucom_cons_tx_high = 0;
532 sc->sc_flag |= UCOM_FLAG_CONSOLE;
533 ucom_open(ucom_cons_softc);
534 ucom_param(tp, &tp->t_termios);
535 UCOM_MTX_UNLOCK(ucom_cons_softc);
538 lwkt_reltoken(&tp->t_token);
544 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
546 struct tty *tp = sc->sc_tty;
548 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
550 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
551 UCOM_MTX_LOCK(ucom_cons_softc);
552 ucom_close(ucom_cons_softc);
553 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
554 UCOM_MTX_UNLOCK(ucom_cons_softc);
555 ucom_cons_softc = NULL;
558 /* the config thread has been stopped when we get here */
561 sc->sc_flag |= UCOM_FLAG_GONE;
562 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
566 lwkt_gettoken(&tp->t_token);
570 if (tp->t_state & TS_ISOPEN) {
571 kprintf("device still open, forcing close\n");
572 (*linesw[tp->t_line].l_close)(tp, 0);
575 ucom_close(sc); /* close, if any */
578 * make sure that read and write transfers are stopped
580 if (sc->sc_callback->ucom_stop_read) {
581 (sc->sc_callback->ucom_stop_read) (sc);
583 if (sc->sc_callback->ucom_stop_write) {
584 (sc->sc_callback->ucom_stop_write) (sc);
587 lwkt_reltoken(&tp->t_token);
592 dev_ops_remove_minor(&ucom_ops,ssc->sc_unit);
598 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
602 struct usb_attach_arg *uaa;
604 ksnprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
605 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
607 /* Store the PNP info in the first interface for the device */
608 uaa = device_get_ivars(dev);
609 iface_index = uaa->info.bIfaceIndex;
611 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
612 device_printf(dev, "Could not set PNP info\n");
615 * The following information is also replicated in the PNP-info
616 * string which is registered above:
618 if (ssc->sc_sysctl_ttyname == NULL) {
620 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
621 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
622 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
623 "TTY device basename");
626 if (ssc->sc_sysctl_ttyports == NULL) {
628 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
629 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
630 OID_AUTO, "ttyports", CTLFLAG_RD,
631 NULL, ssc->sc_subunits, "Number of ports");
637 ucom_queue_command(struct ucom_softc *sc,
638 usb_proc_callback_t *fn, struct termios *pt,
639 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
641 struct ucom_super_softc *ssc = sc->sc_super;
642 struct ucom_param_task *task;
644 UCOM_MTX_ASSERT(sc, MA_OWNED);
646 if (usb_proc_is_gone(&ssc->sc_tq)) {
647 DPRINTF("proc is gone\n");
648 return; /* nothing to do */
651 * NOTE: The task cannot get executed before we drop the
652 * "sc_lock" lock. It is safe to update fields in the message
653 * structure after that the message got queued.
655 task = (struct ucom_param_task *)
656 usb_proc_msignal(&ssc->sc_tq, t0, t1);
658 /* Setup callback and softc pointers */
659 task->hdr.pm_callback = fn;
663 * Make a copy of the termios. This field is only present if
664 * the "pt" field is not NULL.
667 task->termios_copy = *pt;
670 * Closing the device should be synchronous.
672 if (fn == ucom_cfg_close)
673 usb_proc_mwait(&ssc->sc_tq, t0, t1);
676 * In case of multiple configure requests,
677 * keep track of the last one!
679 if (fn == ucom_cfg_start_transfers)
680 sc->sc_last_start_xfer = &task->hdr;
684 ucom_shutdown(struct ucom_softc *sc)
686 struct tty *tp = sc->sc_tty;
688 UCOM_MTX_ASSERT(sc, MA_OWNED);
693 * Hang up if necessary:
695 if (tp->t_termios.c_cflag & HUPCL) {
696 ucom_modem(tp, 0, SER_DTR);
703 * else: taskqueue is draining or gone
706 ucom_cfg_is_gone(struct ucom_softc *sc)
708 struct ucom_super_softc *ssc = sc->sc_super;
710 return (usb_proc_is_gone(&ssc->sc_tq));
714 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
716 struct ucom_cfg_task *task =
717 (struct ucom_cfg_task *)_task;
718 struct ucom_softc *sc = task->sc;
720 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
723 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
724 /* TTY device closed */
728 if (_task == sc->sc_last_start_xfer)
729 sc->sc_flag |= UCOM_FLAG_GP_DATA;
731 if (sc->sc_callback->ucom_start_read) {
732 (sc->sc_callback->ucom_start_read) (sc);
734 if (sc->sc_callback->ucom_start_write) {
735 (sc->sc_callback->ucom_start_write) (sc);
740 ucom_start_transfers(struct ucom_softc *sc)
742 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
746 * Make sure that data transfers are started in both
749 if (sc->sc_callback->ucom_start_read) {
750 (sc->sc_callback->ucom_start_read) (sc);
752 if (sc->sc_callback->ucom_start_write) {
753 (sc->sc_callback->ucom_start_write) (sc);
758 ucom_cfg_open(struct usb_proc_msg *_task)
760 struct ucom_cfg_task *task =
761 (struct ucom_cfg_task *)_task;
762 struct ucom_softc *sc = task->sc;
766 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
772 sc->sc_flag |= UCOM_FLAG_LL_READY;
774 if (sc->sc_callback->ucom_cfg_open) {
775 (sc->sc_callback->ucom_cfg_open) (sc);
778 usb_pause_mtx(sc->sc_lock, hz / 10);
784 ucom_dev_open(struct dev_open_args *ap)
786 cdev_t dev = ap->a_head.a_dev;
787 struct ucom_softc *sc = (struct ucom_softc *)dev->si_drv1;
795 error = ucom_open(sc);
802 ucom_open(struct ucom_softc *sc)
809 mynor = minor(sc->sc_cdev);
811 if (sc->sc_flag & UCOM_FLAG_GONE) {
814 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
818 DPRINTF("tp = %p\n", sc->sc_tty);
820 if (sc->sc_callback->ucom_pre_open) {
822 * give the lower layer a chance to disallow TTY open, for
823 * example if the device is not present:
825 error = (sc->sc_callback->ucom_pre_open) (sc);
830 sc->sc_flag |= UCOM_FLAG_HL_READY;
833 lwkt_gettoken(&tp->t_token);
835 if (!ISSET(tp->t_state, TS_ISOPEN)) {
838 tp->t_dev = reference_dev(sc->sc_cdev);
840 t = mynor & CALLOUT_MASK ? sc->sc_it_out : sc->sc_it_in;
844 tp->t_iflag = TTYDEF_IFLAG;
845 tp->t_oflag = TTYDEF_OFLAG;
846 tp->t_lflag = TTYDEF_LFLAG;
850 /* Disable transfers */
851 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
857 /* reset programmed line state */
862 /* reset jitter buffer */
863 sc->sc_jitterbuf_in = 0;
864 sc->sc_jitterbuf_out = 0;
866 ucom_queue_command(sc, ucom_cfg_open, NULL,
867 &sc->sc_open_task[0].hdr,
868 &sc->sc_open_task[1].hdr);
870 /* Queue transfer enable command last */
871 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
872 &sc->sc_start_task[0].hdr,
873 &sc->sc_start_task[1].hdr);
875 ucom_modem(sc->sc_tty, SER_DTR | SER_RTS, 0);
881 ucom_status_change(sc);
883 if (ISSET(sc->sc_msr, SER_DCD)) {
884 (*linesw[tp->t_line].l_modem)(tp, 1);
888 error = ttyopen(sc->sc_cdev, tp);
890 lwkt_reltoken(&tp->t_token);
894 error = (*linesw[tp->t_line].l_open)(sc->sc_cdev, tp);
896 lwkt_reltoken(&tp->t_token);
900 disc_optim(tp, &tp->t_termios, sc);
902 lwkt_reltoken(&tp->t_token);
908 ucom_cfg_close(struct usb_proc_msg *_task)
910 struct ucom_cfg_task *task =
911 (struct ucom_cfg_task *)_task;
912 struct ucom_softc *sc = task->sc;
916 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
917 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
918 if (sc->sc_callback->ucom_cfg_close)
919 (sc->sc_callback->ucom_cfg_close) (sc);
926 ucom_dev_close(struct dev_close_args *ap)
928 cdev_t dev = ap->a_head.a_dev;
929 struct ucom_softc *sc = (struct ucom_softc *)dev->si_drv1;
933 error = ucom_close(sc);
940 ucom_close(struct ucom_softc *sc)
942 struct tty *tp = sc->sc_tty;
945 DPRINTF("tp=%p\n", tp);
947 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
948 DPRINTF("tp=%p already closed\n", tp);
951 if (!ISSET(tp->t_state, TS_ISOPEN)) {
956 ucom_queue_command(sc, ucom_cfg_close, NULL,
957 &sc->sc_close_task[0].hdr,
958 &sc->sc_close_task[1].hdr);
960 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
962 if (sc->sc_callback->ucom_stop_read) {
963 (sc->sc_callback->ucom_stop_read) (sc);
966 lwkt_gettoken(&tp->t_token);
967 (*linesw[tp->t_line].l_close)(tp, 0); /* XXX: flags */
968 disc_optim(tp, &tp->t_termios, sc);
972 release_dev(tp->t_dev);
975 /* XXX: Detach wakeup */
976 lwkt_reltoken(&tp->t_token);
983 ucom_inwakeup(struct tty *tp)
985 struct ucom_softc *sc = tty_softc(tp);
991 UCOM_MTX_ASSERT(sc, MA_OWNED);
993 DPRINTF("tp=%p\n", tp);
995 if (ttydisc_can_bypass(tp) != 0 ||
996 (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
997 (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
1001 /* prevent recursion */
1002 sc->sc_flag |= UCOM_FLAG_INWAKEUP;
1004 pos = sc->sc_jitterbuf_out;
1006 while (sc->sc_jitterbuf_in != pos) {
1009 c = (char)sc->sc_jitterbuf[pos];
1011 if (ttydisc_rint(tp, c, 0) == -1)
1014 if (pos >= UCOM_JITTERBUF_SIZE)
1015 pos -= UCOM_JITTERBUF_SIZE;
1018 sc->sc_jitterbuf_out = pos;
1020 /* clear RTS in async fashion */
1021 if ((sc->sc_jitterbuf_in == pos) &&
1022 (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
1025 sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
1030 ucom_dev_read(struct dev_read_args *ap)
1032 cdev_t dev = ap->a_head.a_dev;
1033 struct ucom_softc *sc;
1040 lwkt_gettoken(&tp->t_token);
1045 DPRINTF("tp = %p, flag = 0x%x\n", tp, ap->a_ioflag);
1047 /* must not be locked in case it blocks */
1048 /*UCOM_MTX_LOCK(sc);*/
1049 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
1050 /*UCOM_MTX_UNLOCK(sc);*/
1052 lwkt_reltoken(&tp->t_token);
1053 DPRINTF("error = %d\n", error);
1059 ucom_dev_write(struct dev_write_args *ap)
1061 cdev_t dev = ap->a_head.a_dev;
1062 struct ucom_softc *sc;
1067 lwkt_gettoken(&tp->t_token);
1072 DPRINTF("tp = %p, flag = 0x%x\n", tp, ap->a_ioflag);
1074 /* must not be locked in case it blocks */
1075 /*UCOM_MTX_LOCK(sc);*/
1076 error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
1077 /*UCOM_MTX_UNLOCK(sc);*/
1079 lwkt_reltoken(&tp->t_token);
1080 DPRINTF("ucomwrite: error = %d\n", error);
1086 ucom_dev_ioctl(struct dev_ioctl_args *ap)
1088 cdev_t dev = ap->a_head.a_dev;
1089 struct ucom_softc *sc = (struct ucom_softc *)dev->si_drv1;
1090 u_long cmd = ap->a_cmd;
1091 caddr_t data = ap->a_data;
1092 struct tty *tp = sc->sc_tty;
1098 lwkt_gettoken(&tp->t_token);
1102 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1103 lwkt_reltoken(&tp->t_token);
1104 UCOM_MTX_UNLOCK(sc);
1107 DPRINTF("cmd = 0x%08lx\n", cmd);
1108 if (mynor & CONTROL_MASK) {
1111 switch (mynor & CONTROL_MASK) {
1112 case CONTROL_INIT_STATE:
1113 ct = mynor & CALLOUT_MASK ? &sc->sc_it_out : &sc->sc_it_in;
1115 case CONTROL_LOCK_STATE:
1116 ct = mynor & CALLOUT_MASK ? &sc->sc_lt_out : &sc->sc_lt_in;
1119 lwkt_reltoken(&tp->t_token);
1120 UCOM_MTX_UNLOCK(sc);
1121 return (ENODEV); /* /dev/nodev */
1123 switch (ap->a_cmd) {
1125 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1127 lwkt_reltoken(&tp->t_token);
1128 UCOM_MTX_UNLOCK(sc);
1131 *ct = *(struct termios *)data;
1132 lwkt_reltoken(&tp->t_token);
1133 UCOM_MTX_UNLOCK(sc);
1136 *(struct termios *)data = *ct;
1137 lwkt_reltoken(&tp->t_token);
1138 UCOM_MTX_UNLOCK(sc);
1141 *(int *)data = TTYDISC;
1142 lwkt_reltoken(&tp->t_token);
1143 UCOM_MTX_UNLOCK(sc);
1146 bzero(data, sizeof(struct winsize));
1147 lwkt_reltoken(&tp->t_token);
1148 UCOM_MTX_UNLOCK(sc);
1151 lwkt_reltoken(&tp->t_token);
1152 UCOM_MTX_UNLOCK(sc);
1157 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1158 ap->a_fflag, ap->a_cred);
1160 if (error != ENOIOCTL) {
1161 DPRINTF("ucomioctl: l_ioctl: error = %d\n", error);
1162 lwkt_reltoken(&tp->t_token);
1163 UCOM_MTX_UNLOCK(sc);
1167 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1168 disc_optim(tp, &tp->t_termios, sc);
1169 if (error != ENOIOCTL) {
1170 DPRINTF("ucomioctl: ttioctl: error = %d\n", error);
1171 lwkt_reltoken(&tp->t_token);
1172 UCOM_MTX_UNLOCK(sc);
1205 d = *(int *)ap->a_data;
1206 DPRINTF("ucomioctl: TIOCMSET, 0x%x\n", d);
1207 ucom_modem(tp, ucom_fromtio(d), 0);
1210 d = ucom_modem(tp, 0, 0);
1211 DPRINTF("ucomioctl: TIOCMGET, 0x%x\n", d);
1212 *(int *)ap->a_data = ucom_totio(d);
1213 ucom_status_change(sc);
1216 d = *(int *)ap->a_data;
1217 ucom_modem(tp, ucom_fromtio(d), 0);
1220 d = *(int *)ap->a_data;
1221 ucom_modem(tp, 0, ucom_fromtio(d));
1224 if (sc->sc_callback->ucom_ioctl) {
1225 error = (sc->sc_callback->ucom_ioctl)
1226 (sc, cmd, data, 0, curthread);
1228 lwkt_reltoken(&tp->t_token);
1229 UCOM_MTX_UNLOCK(sc);
1236 if (error == ENOIOCTL)
1237 error = pps_ioctl(cmd, data, &sc->sc_pps);
1240 lwkt_reltoken(&tp->t_token);
1241 UCOM_MTX_UNLOCK(sc);
1247 ucom_totio(int bits)
1251 SET(bits, TIOCM_LE);
1253 if (ISSET(bits, SER_DTR)) {
1254 SET(rbits, TIOCM_DTR);
1256 if (ISSET(bits, SER_RTS)) {
1257 SET(rbits, TIOCM_RTS);
1259 if (ISSET(bits, SER_CTS)) {
1260 SET(rbits, TIOCM_CTS);
1262 if (ISSET(bits, SER_DCD)) {
1263 SET(rbits, TIOCM_CD);
1265 if (ISSET(bits, SER_DSR)) {
1266 SET(rbits, TIOCM_DSR);
1268 if (ISSET(bits, SER_RI)) {
1269 SET(rbits, TIOCM_RI);
1276 ucom_fromtio(int bits)
1280 if (ISSET(bits, TIOCM_DTR)) {
1281 SET(rbits, SER_DTR);
1283 if (ISSET(bits, TIOCM_RTS)) {
1284 SET(rbits, SER_RTS);
1286 if (ISSET(bits, TIOCM_CTS)) {
1287 SET(rbits, SER_CTS);
1289 if (ISSET(bits, TIOCM_CD)) {
1290 SET(rbits, SER_DCD);
1292 if (ISSET(bits, TIOCM_DSR)) {
1293 SET(rbits, SER_DSR);
1295 if (ISSET(bits, TIOCM_RI)) {
1303 ucom_modem(struct tty *tp, int sigon, int sigoff)
1305 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1308 UCOM_MTX_ASSERT(sc, MA_OWNED);
1310 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1313 if ((sigon == 0) && (sigoff == 0)) {
1315 if (sc->sc_mcr & SER_DTR) {
1318 if (sc->sc_mcr & SER_RTS) {
1321 if (sc->sc_msr & SER_CTS) {
1324 if (sc->sc_msr & SER_DCD) {
1327 if (sc->sc_msr & SER_DSR) {
1330 if (sc->sc_msr & SER_RI) {
1335 if (sigon & SER_DTR) {
1336 sc->sc_mcr |= SER_DTR;
1338 if (sigoff & SER_DTR) {
1339 sc->sc_mcr &= ~SER_DTR;
1341 if (sigon & SER_RTS) {
1342 sc->sc_mcr |= SER_RTS;
1344 if (sigoff & SER_RTS) {
1345 sc->sc_mcr &= ~SER_RTS;
1347 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
1348 ucom_dtr(sc, onoff);
1350 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
1351 ucom_rts(sc, onoff);
1357 ucom_cfg_line_state(struct usb_proc_msg *_task)
1359 struct ucom_cfg_task *task =
1360 (struct ucom_cfg_task *)_task;
1361 struct ucom_softc *sc = task->sc;
1368 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1373 /* compute callback mask */
1374 if (sc->sc_callback->ucom_cfg_set_dtr)
1375 mask |= UCOM_LS_DTR;
1376 if (sc->sc_callback->ucom_cfg_set_rts)
1377 mask |= UCOM_LS_RTS;
1378 if (sc->sc_callback->ucom_cfg_set_break)
1379 mask |= UCOM_LS_BREAK;
1380 if (sc->sc_callback->ucom_cfg_set_ring)
1381 mask |= UCOM_LS_RING;
1383 /* compute the bits we are to program */
1384 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
1385 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
1386 prev_value = sc->sc_pls_curr ^ notch_bits;
1387 last_value = sc->sc_pls_curr;
1389 /* reset programmed line state */
1390 sc->sc_pls_curr = 0;
1394 /* ensure that we don't lose any levels */
1395 if (notch_bits & UCOM_LS_DTR)
1396 sc->sc_callback->ucom_cfg_set_dtr(sc,
1397 (prev_value & UCOM_LS_DTR) ? 1 : 0);
1398 if (notch_bits & UCOM_LS_RTS)
1399 sc->sc_callback->ucom_cfg_set_rts(sc,
1400 (prev_value & UCOM_LS_RTS) ? 1 : 0);
1401 if (notch_bits & UCOM_LS_BREAK)
1402 sc->sc_callback->ucom_cfg_set_break(sc,
1403 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
1404 if (notch_bits & UCOM_LS_RING)
1405 sc->sc_callback->ucom_cfg_set_ring(sc,
1406 (prev_value & UCOM_LS_RING) ? 1 : 0);
1408 /* set last value */
1409 if (any_bits & UCOM_LS_DTR)
1410 sc->sc_callback->ucom_cfg_set_dtr(sc,
1411 (last_value & UCOM_LS_DTR) ? 1 : 0);
1412 if (any_bits & UCOM_LS_RTS)
1413 sc->sc_callback->ucom_cfg_set_rts(sc,
1414 (last_value & UCOM_LS_RTS) ? 1 : 0);
1415 if (any_bits & UCOM_LS_BREAK)
1416 sc->sc_callback->ucom_cfg_set_break(sc,
1417 (last_value & UCOM_LS_BREAK) ? 1 : 0);
1418 if (any_bits & UCOM_LS_RING)
1419 sc->sc_callback->ucom_cfg_set_ring(sc,
1420 (last_value & UCOM_LS_RING) ? 1 : 0);
1424 ucom_line_state(struct ucom_softc *sc,
1425 uint8_t set_bits, uint8_t clear_bits)
1427 UCOM_MTX_ASSERT(sc, MA_OWNED);
1429 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1433 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1435 /* update current programmed line state */
1436 sc->sc_pls_curr |= set_bits;
1437 sc->sc_pls_curr &= ~clear_bits;
1438 sc->sc_pls_set |= set_bits;
1439 sc->sc_pls_clr |= clear_bits;
1441 /* defer driver programming */
1442 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1443 &sc->sc_line_state_task[0].hdr,
1444 &sc->sc_line_state_task[1].hdr);
1448 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1450 DPRINTF("onoff = %d\n", onoff);
1453 ucom_line_state(sc, UCOM_LS_RING, 0);
1455 ucom_line_state(sc, 0, UCOM_LS_RING);
1459 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1461 DPRINTF("onoff = %d\n", onoff);
1464 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1466 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1470 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1472 DPRINTF("onoff = %d\n", onoff);
1475 ucom_line_state(sc, UCOM_LS_DTR, 0);
1477 ucom_line_state(sc, 0, UCOM_LS_DTR);
1481 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1483 DPRINTF("onoff = %d\n", onoff);
1486 ucom_line_state(sc, UCOM_LS_RTS, 0);
1488 ucom_line_state(sc, 0, UCOM_LS_RTS);
1492 ucom_cfg_status_change(struct usb_proc_msg *_task)
1494 struct ucom_cfg_task *task =
1495 (struct ucom_cfg_task *)_task;
1496 struct ucom_softc *sc = task->sc;
1505 UCOM_MTX_ASSERT(sc, MA_OWNED);
1507 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1510 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1518 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1520 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1521 /* TTY device closed */
1524 msr_delta = (sc->sc_msr ^ new_msr);
1525 lsr_delta = (sc->sc_lsr ^ new_lsr);
1527 sc->sc_msr = new_msr;
1528 sc->sc_lsr = new_lsr;
1530 #if 0 /* missing pps_capture */
1532 * Time pulse counting support. Note that both CTS and DCD are
1533 * active-low signals. The status bit is high to indicate that
1534 * the signal on the line is low, which corresponds to a PPS
1537 switch(ucom_pps_mode) {
1539 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1540 (msr_delta & SER_CTS)) {
1541 pps_capture(&sc->sc_pps);
1542 pps_event(&sc->sc_pps, (sc->sc_msr & SER_CTS) ?
1543 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
1547 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1548 (msr_delta & SER_DCD)) {
1549 pps_capture(&sc->sc_pps);
1550 pps_event(&sc->sc_pps, (sc->sc_msr & SER_DCD) ?
1551 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
1559 if (msr_delta & SER_DCD) {
1561 int onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1563 DPRINTF("DCD changed to %d\n", onoff);
1565 (*linesw[tp->t_line].l_modem)(tp, onoff);
1568 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1570 DPRINTF("BREAK detected\n");
1571 (*linesw[tp->t_line].l_rint)(0, tp);
1574 ttydisc_rint(tp, 0, TRE_BREAK);
1575 ttydisc_rint_done(tp);
1579 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1581 DPRINTF("Frame error detected\n");
1582 (*linesw[tp->t_line].l_rint)(0, tp);
1585 ttydisc_rint(tp, 0, TRE_FRAMING);
1586 ttydisc_rint_done(tp);
1590 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1592 DPRINTF("Parity error detected\n");
1593 (*linesw[tp->t_line].l_rint)(0, tp);
1595 ttydisc_rint(tp, 0, TRE_PARITY);
1596 ttydisc_rint_done(tp);
1602 ucom_status_change(struct ucom_softc *sc)
1604 UCOM_MTX_ASSERT(sc, MA_OWNED);
1606 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1607 return; /* not supported */
1609 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1614 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1615 &sc->sc_status_task[0].hdr,
1616 &sc->sc_status_task[1].hdr);
1620 ucom_cfg_param(struct usb_proc_msg *_task)
1622 struct ucom_param_task *task =
1623 (struct ucom_param_task *)_task;
1624 struct ucom_softc *sc = task->sc;
1626 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1629 if (sc->sc_callback->ucom_cfg_param == NULL) {
1633 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1636 usb_pause_mtx(sc->sc_lock, hz / 10);
1640 ucom_param(struct tty *tp, struct termios *t)
1642 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1646 lwkt_gettoken(&tp->t_token);
1647 UCOM_MTX_ASSERT(sc, MA_OWNED);
1652 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1654 /* XXX the TTY layer should call "open()" first! */
1656 * Not quite: Its ordering is partly backwards, but
1657 * some parameters must be set early in ttydev_open(),
1658 * possibly before calling ttydevsw_open().
1660 error = ucom_open(sc);
1667 DPRINTF("sc = %p\n", sc);
1669 /* Check requested parameters. */
1670 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1671 /* XXX c_ospeed == 0 is perfectly valid. */
1672 DPRINTF("mismatch ispeed and ospeed\n");
1676 t->c_ispeed = t->c_ospeed;
1678 if (sc->sc_callback->ucom_pre_param) {
1679 /* Let the lower layer verify the parameters */
1680 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1682 DPRINTF("callback error = %d\n", error);
1687 /* Disable transfers */
1688 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1690 /* Queue baud rate programming command first */
1691 ucom_queue_command(sc, ucom_cfg_param, t,
1692 &sc->sc_param_task[0].hdr,
1693 &sc->sc_param_task[1].hdr);
1695 /* Queue transfer enable command last */
1696 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1697 &sc->sc_start_task[0].hdr,
1698 &sc->sc_start_task[1].hdr);
1700 if (t->c_cflag & CRTS_IFLOW) {
1701 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1702 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1703 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1704 ucom_modem(tp, SER_RTS, 0);
1712 lwkt_reltoken(&tp->t_token);
1718 ucom_start(struct tty *tp)
1720 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1723 /* may be called locked or unlocked */
1724 if (lockowned(sc->sc_lock)) {
1730 /* UCOM_MTX_ASSERT(sc, MA_OWNED); */
1732 DPRINTF("sc = %p\n", sc);
1734 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1735 /* The higher layer is not ready */
1737 UCOM_MTX_UNLOCK(sc);
1741 lwkt_gettoken(&tp->t_token);
1743 if (tp->t_state & TS_TBLOCK) {
1744 if (ISSET(sc->sc_mcr, SER_RTS) &&
1745 ISSET(sc->sc_flag, UCOM_FLAG_RTS_IFLOW)) {
1746 DPRINTF("ucom_start: clear RTS\n");
1747 (void)ucom_modem(tp, 0, SER_RTS);
1750 if (!ISSET(sc->sc_mcr, SER_RTS) &&
1751 tp->t_rawq.c_cc <= tp->t_ilowat &&
1752 ISSET(sc->sc_flag, UCOM_FLAG_RTS_IFLOW)) {
1753 DPRINTF("ucom_start: set RTS\n");
1754 (void)ucom_modem(tp, SER_RTS, 0);
1758 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
1760 DPRINTF("ucom_start: stopped\n");
1764 if (tp->t_outq.c_cc <= tp->t_olowat) {
1765 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
1766 CLR(tp->t_state, TS_SO_OLOWAT);
1767 wakeup(TSA_OLOWAT(tp));
1769 KNOTE(&tp->t_wkq.ki_note, 0);
1770 if (tp->t_outq.c_cc == 0) {
1771 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
1772 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
1773 CLR(tp->t_state, TS_SO_OCOMPLETE);
1774 wakeup(TSA_OCOMPLETE(tp));
1780 DPRINTF("about to start write?\n");
1781 ucom_start_transfers(sc);
1786 lwkt_reltoken(&tp->t_token);
1788 UCOM_MTX_UNLOCK(sc);
1792 ucom_stop(struct tty *tp, int flag)
1794 struct ucom_softc *sc = (struct ucom_softc *)tp->t_sc;
1796 DPRINTF("sc = %p, x = 0x%x\n", sc, flag);
1798 lwkt_gettoken(&tp->t_token);
1801 * This is just supposed to flush pending receive data,
1802 * not stop the reception of data entirely!
1805 if (sc->sc_callback->ucom_stop_read) {
1806 (sc->sc_callback->ucom_stop_read) (sc);
1808 if (sc->sc_callback->ucom_start_read) {
1809 (sc->sc_callback->ucom_start_read) (sc);
1817 if (flag & FWRITE) {
1819 if (ISSET(tp->t_state, TS_BUSY)) {
1821 if (!ISSET(tp->t_state, TS_TTSTOP))
1822 SET(tp->t_state, TS_FLUSH);
1827 lwkt_reltoken(&tp->t_token);
1830 /*------------------------------------------------------------------------*
1833 * len: maximum length of data to get
1835 * Get data from the TTY layer
1838 * 0: No data is available.
1839 * Else: Data is available.
1840 *------------------------------------------------------------------------*/
1842 /* Copy data from the tty layer to usb */
1844 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1845 uint32_t offset, uint32_t len, uint32_t *actlen)
1847 struct usb_page_search res;
1848 struct tty *tp = sc->sc_tty;
1850 uint32_t offset_orig;
1854 UCOM_MTX_ASSERT(sc, MA_OWNED);
1856 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1859 /* get total TX length */
1861 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1862 temp %= UCOM_CONS_BUFSIZE;
1864 /* limit TX length */
1866 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1867 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1874 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1876 /* update counters */
1878 ucom_cons_tx_low += temp;
1879 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1881 /* store actual length */
1885 return (temp ? 1 : 0);
1889 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1891 return (0); /* multiport device polling */
1894 offset_orig = offset;
1896 lwkt_gettoken(&tp->t_token);
1898 usbd_get_page(pc, offset, &res);
1900 /* Buffer bigger than max requested data */
1901 if (res.length > len) {
1904 /* copy data directly into USB buffer */
1905 SET(tp->t_state, TS_BUSY);
1906 cnt = q_to_b(&tp->t_outq, res.buffer, len);
1908 DPRINTF("ucom_get_data: cnt == 0\n");
1909 CLR(tp->t_state, TS_BUSY);
1913 CLR(tp->t_state, TS_BUSY);
1915 /* XXX mp: This breaks avrdude,
1916 does the flush need to happen
1918 if (ISSET(tp->t_state, TS_FLUSH))
1919 CLR(tp->t_state, TS_FLUSH);
1921 ndflush(&tp->t_outq,cnt);
1927 if (cnt < res.length) {
1932 lwkt_reltoken(&tp->t_token);
1934 actlen[0] = offset - offset_orig;
1936 DPRINTF("cnt=%d\n", actlen[0]);
1938 if (actlen[0] == 0) {
1945 * Write data to the tty layer
1949 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1950 uint32_t offset, uint32_t len)
1952 struct usb_page_search res;
1953 struct tty *tp = sc->sc_tty;
1960 UCOM_MTX_ASSERT(sc, MA_OWNED);
1961 lwkt_gettoken(&tp->t_token);
1963 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1966 /* get maximum RX length */
1968 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1969 temp %= UCOM_CONS_BUFSIZE;
1971 /* limit RX length */
1973 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1974 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1981 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1983 /* update counters */
1985 ucom_cons_rx_high += temp;
1986 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1988 lwkt_reltoken(&tp->t_token);
1993 lwkt_reltoken(&tp->t_token);
1994 return; /* multiport device polling */
1997 lwkt_reltoken(&tp->t_token);
1998 return; /* no data */
2001 /* set a flag to prevent recursation ? */
2004 usbd_get_page(pc, offset, &res);
2006 if (res.length > len) {
2010 offset += res.length;
2012 /* pass characters to tty layer */
2017 /* first check if we can pass the buffer directly */
2019 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
2020 /* clear any jitter buffer */
2021 sc->sc_jitterbuf_in = 0;
2022 sc->sc_jitterbuf_out = 0;
2024 if (tp->t_rawq.c_cc + cnt > tp->t_ihiwat
2025 && (sc->sc_flag & UCOM_FLAG_RTS_IFLOW
2026 || tp->t_iflag & IXOFF)
2027 && !(tp->t_state & TS_TBLOCK))
2029 lostcc = b_to_q((char *)buf, cnt, &tp->t_rawq);
2033 if (*buf == sc->hotchar)
2042 if (tp->t_state & TS_TTSTOP
2043 && (tp->t_iflag & IXANY
2044 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
2045 tp->t_state &= ~TS_TTSTOP;
2046 tp->t_lflag &= ~FLUSHO;
2050 kprintf("lost %d chars\n", lostcc);
2053 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
2054 DPRINTF("tp=%p, data lost\n", tp);
2060 for (cnt = 0; cnt != res.length; cnt++) {
2061 if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
2062 (*linesw[tp->t_line].l_rint)((unsigned char)buf[cnt], tp) == -1) {
2066 pos = sc->sc_jitterbuf_in;
2067 end = sc->sc_jitterbuf_out +
2068 UCOM_JITTERBUF_SIZE - 1;
2070 if (end >= UCOM_JITTERBUF_SIZE)
2071 end -= UCOM_JITTERBUF_SIZE;
2073 for (; cnt != res.length; cnt++) {
2076 sc->sc_jitterbuf[pos] = buf[cnt];
2078 if (pos >= UCOM_JITTERBUF_SIZE)
2079 pos -= UCOM_JITTERBUF_SIZE;
2082 sc->sc_jitterbuf_in = pos;
2084 /* set RTS in async fashion */
2085 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
2088 DPRINTF("tp=%p, lost %d "
2089 "chars\n", tp, res.length - cnt);
2095 lwkt_reltoken(&tp->t_token);
2097 ttydisc_rint_done(tp);
2103 ucom_free(void *xsc)
2105 struct ucom_softc *sc = xsc;
2107 if (sc->sc_callback->ucom_free != NULL)
2108 sc->sc_callback->ucom_free(sc);
2110 /*ucom_unref(sc->sc_super) XXX hack, see end of ucom_detach_tty() */;
2112 lockmgr(&ucom_lock, LK_EXCLUSIVE);
2114 lockmgr(&ucom_lock, LK_RELEASE);
2117 static cn_probe_t ucom_cnprobe;
2118 static cn_init_t ucom_cninit;
2119 static cn_term_t ucom_cnterm;
2120 static cn_getc_t ucom_cngetc;
2121 static cn_putc_t ucom_cnputc;
2124 static cn_grab_t ucom_cngrab;
2125 static cn_ungrab_t ucom_cnungrab;
2126 CONSOLE_DRIVER(ucom);
2130 ucom_cnprobe(struct consdev *cp)
2132 if (ucom_cons_unit != -1)
2133 cp->cn_pri = CN_NORMAL;
2135 cp->cn_pri = CN_DEAD;
2138 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
2143 ucom_cninit(struct consdev *cp)
2148 ucom_cnterm(struct consdev *cp)
2153 ucom_cngrab(struct consdev *cp)
2158 ucom_cnungrab(struct consdev *cp)
2163 ucom_cngetc(struct consdev *cd)
2165 struct ucom_softc *sc = ucom_cons_softc;
2173 if (ucom_cons_rx_low != ucom_cons_rx_high) {
2174 c = ucom_cons_rx_buf[ucom_cons_rx_low];
2175 ucom_cons_rx_low ++;
2176 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
2181 /* start USB transfers */
2182 ucom_outwakeup(sc->sc_tty);
2184 UCOM_MTX_UNLOCK(sc);
2186 /* poll if necessary */
2188 if (kdb_active && sc->sc_callback->ucom_poll)
2189 (sc->sc_callback->ucom_poll) (sc);
2195 ucom_cnputc(void *cd, int c)
2197 ucom_cnputc(struct consdev *cd, int c)
2201 struct ucom_softc *sc = ucom_cons_softc;
2211 /* compute maximum TX length */
2213 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
2214 temp %= UCOM_CONS_BUFSIZE;
2217 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
2218 ucom_cons_tx_high ++;
2219 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
2222 /* start USB transfers */
2223 ucom_outwakeup(sc->sc_tty);
2225 UCOM_MTX_UNLOCK(sc);
2227 /* poll if necessary */
2229 if (kdb_active && sc->sc_callback->ucom_poll) {
2230 (sc->sc_callback->ucom_poll) (sc);
2231 /* simple flow control */
2238 /*------------------------------------------------------------------------*
2241 * This function will increment the super UCOM reference count.
2242 *------------------------------------------------------------------------*/
2244 ucom_ref(struct ucom_super_softc *ssc)
2246 lockmgr(&ucom_lock, LK_EXCLUSIVE);
2248 lockmgr(&ucom_lock, LK_RELEASE);
2251 /*------------------------------------------------------------------------*
2254 * This function will free the super UCOM's allocated unit
2255 * number. This function can be called on a zero-initialized
2256 * structure. This function can be called multiple times.
2257 *------------------------------------------------------------------------*/
2259 ucom_free_unit(struct ucom_super_softc *ssc)
2261 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
2264 ucom_unit_free(ssc->sc_unit);
2266 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
2269 /*------------------------------------------------------------------------*
2272 * This function will decrement the super UCOM reference count.
2275 * 0: UCOM structures are still referenced.
2276 * Else: UCOM structures are no longer referenced.
2277 *------------------------------------------------------------------------*/
2279 ucom_unref(struct ucom_super_softc *ssc)
2283 lockmgr(&ucom_lock, LK_EXCLUSIVE);
2284 retval = (ssc->sc_refs < 2);
2286 lockmgr(&ucom_lock, LK_RELEASE);
2289 ucom_free_unit(ssc);
2295 * NOTE: Must be called with tp->t_token held.
2298 disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc)
2300 ASSERT_LWKT_TOKEN_HELD(&tp->t_token);
2301 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2302 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2303 && (!(t->c_iflag & PARMRK)
2304 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2305 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2306 && linesw[tp->t_line].l_rint == ttyinput) {
2307 DPRINTF("disc_optim: bypass l_rint\n");
2308 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2310 DPRINTF("disc_optim: can't bypass l_rint\n");
2311 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2313 sc->hotchar = linesw[tp->t_line].l_hotchar;
2318 #include <gdb/gdb.h>
2320 static gdb_probe_f ucom_gdbprobe;
2321 static gdb_init_f ucom_gdbinit;
2322 static gdb_term_f ucom_gdbterm;
2323 static gdb_getc_f ucom_gdbgetc;
2324 static gdb_putc_f ucom_gdbputc;
2326 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
2331 return ((ucom_cons_softc != NULL) ? 0 : -1);
2347 ucom_cnputc(NULL, c);
2353 return (ucom_cngetc(NULL));