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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
35 * All rights reserved.
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Lennart Augustsson (lennart@augustsson.net) at
39 * Carlstedt Research & Technology.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
63 #include <sys/stdint.h>
64 #include <sys/stddef.h>
65 #include <sys/param.h>
66 #include <sys/queue.h>
67 #include <sys/types.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
71 #include <sys/module.h>
73 #include <sys/mutex.h>
74 #include <sys/condvar.h>
75 #include <sys/sysctl.h>
77 #include <sys/unistd.h>
78 #include <sys/callout.h>
79 #include <sys/malloc.h>
84 #include <dev/usb/usb.h>
85 #include <dev/usb/usbdi.h>
86 #include <dev/usb/usbdi_util.h>
88 #define USB_DEBUG_VAR ucom_debug
89 #include <dev/usb/usb_debug.h>
90 #include <dev/usb/usb_busdma.h>
91 #include <dev/usb/usb_process.h>
93 #include <dev/usb/serial/usb_serial.h>
97 static SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
99 static int ucom_pps_mode;
101 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, pps_mode, CTLFLAG_RWTUN,
102 &ucom_pps_mode, 0, "pulse capturing mode - 0/1/2 - disabled/CTS/DCD");
105 static int ucom_debug = 0;
107 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
108 &ucom_debug, 0, "ucom debug level");
111 #define UCOM_CONS_BUFSIZE 1024
113 static uint8_t ucom_cons_rx_buf[UCOM_CONS_BUFSIZE];
114 static uint8_t ucom_cons_tx_buf[UCOM_CONS_BUFSIZE];
116 static unsigned int ucom_cons_rx_low = 0;
117 static unsigned int ucom_cons_rx_high = 0;
119 static unsigned int ucom_cons_tx_low = 0;
120 static unsigned int ucom_cons_tx_high = 0;
122 static int ucom_cons_unit = -1;
123 static int ucom_cons_subunit = 0;
124 static int ucom_cons_baud = 9600;
125 static struct ucom_softc *ucom_cons_softc = NULL;
127 TUNABLE_INT("hw.usb.ucom.cons_unit", &ucom_cons_unit);
128 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_unit, CTLFLAG_RW | CTLFLAG_TUN,
129 &ucom_cons_unit, 0, "console unit number");
130 TUNABLE_INT("hw.usb.ucom.cons_subunit", &ucom_cons_subunit);
131 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_subunit, CTLFLAG_RW | CTLFLAG_TUN,
132 &ucom_cons_subunit, 0, "console subunit number");
133 TUNABLE_INT("hw.usb.ucom.cons_baud", &ucom_cons_baud);
134 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RW | CTLFLAG_TUN,
135 &ucom_cons_baud, 0, "console baud rate");
137 static usb_proc_callback_t ucom_cfg_start_transfers;
138 static usb_proc_callback_t ucom_cfg_open;
139 static usb_proc_callback_t ucom_cfg_close;
140 static usb_proc_callback_t ucom_cfg_line_state;
141 static usb_proc_callback_t ucom_cfg_status_change;
142 static usb_proc_callback_t ucom_cfg_param;
144 static int ucom_unit_alloc(void);
145 static void ucom_unit_free(int);
146 static int ucom_attach_tty(struct ucom_super_softc *, struct ucom_softc *);
147 static void ucom_detach_tty(struct ucom_super_softc *, struct ucom_softc *);
148 static void ucom_queue_command(struct ucom_softc *,
149 usb_proc_callback_t *, struct termios *pt,
150 struct usb_proc_msg *t0, struct usb_proc_msg *t1);
151 static void ucom_shutdown(struct ucom_softc *);
152 static void ucom_ring(struct ucom_softc *, uint8_t);
153 static void ucom_break(struct ucom_softc *, uint8_t);
154 static void ucom_dtr(struct ucom_softc *, uint8_t);
155 static void ucom_rts(struct ucom_softc *, uint8_t);
157 static tsw_open_t ucom_open;
158 static tsw_close_t ucom_close;
159 static tsw_ioctl_t ucom_ioctl;
160 static tsw_modem_t ucom_modem;
161 static tsw_param_t ucom_param;
162 static tsw_outwakeup_t ucom_outwakeup;
163 static tsw_inwakeup_t ucom_inwakeup;
164 static tsw_free_t ucom_free;
166 static struct ttydevsw ucom_class = {
167 .tsw_flags = TF_INITLOCK | TF_CALLOUT,
168 .tsw_open = ucom_open,
169 .tsw_close = ucom_close,
170 .tsw_outwakeup = ucom_outwakeup,
171 .tsw_inwakeup = ucom_inwakeup,
172 .tsw_ioctl = ucom_ioctl,
173 .tsw_param = ucom_param,
174 .tsw_modem = ucom_modem,
175 .tsw_free = ucom_free,
178 MODULE_DEPEND(ucom, usb, 1, 1, 1);
179 MODULE_VERSION(ucom, 1);
181 #define UCOM_UNIT_MAX 128 /* maximum number of units */
182 #define UCOM_TTY_PREFIX "U"
184 static struct unrhdr *ucom_unrhdr;
185 static struct mtx ucom_mtx;
186 static int ucom_close_refs;
192 ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
193 mtx_init(&ucom_mtx, "UCOM MTX", NULL, MTX_DEF);
195 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
198 ucom_uninit(void *arg)
209 mtx_destroy(&ucom_mtx);
211 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 3, SI_ORDER_ANY, ucom_uninit, NULL);
214 * Mark a unit number (the X in cuaUX) as in use.
216 * Note that devices using a different naming scheme (see ucom_tty_name()
217 * callback) still use this unit allocation.
220 ucom_unit_alloc(void)
225 if (ucom_unrhdr == NULL) {
226 DPRINTF("ucom_unrhdr is NULL\n");
229 unit = alloc_unr(ucom_unrhdr);
230 DPRINTF("unit %d is allocated\n", unit);
235 * Mark the unit number as not in use.
238 ucom_unit_free(int unit)
241 if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
242 DPRINTF("cannot free unit number\n");
245 DPRINTF("unit %d is freed\n", unit);
246 free_unr(ucom_unrhdr, unit);
250 * Setup a group of one or more serial ports.
252 * The mutex pointed to by "mtx" is applied before all
253 * callbacks are called back. Also "mtx" must be applied
254 * before calling into the ucom-layer!
257 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
258 int subunits, void *parent,
259 const struct ucom_callback *callback, struct mtx *mtx)
266 (callback == NULL) ||
271 /* allocate a uniq unit number */
272 ssc->sc_unit = ucom_unit_alloc();
273 if (ssc->sc_unit == -1)
276 /* generate TTY name string */
277 snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
278 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
280 /* create USB request handling process */
281 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
283 ucom_unit_free(ssc->sc_unit);
286 ssc->sc_subunits = subunits;
287 ssc->sc_flag = UCOM_FLAG_ATTACHED |
290 if (callback->ucom_free == NULL)
291 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
293 /* increment reference count */
296 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
297 sc[subunit].sc_subunit = subunit;
298 sc[subunit].sc_super = ssc;
299 sc[subunit].sc_mtx = mtx;
300 sc[subunit].sc_parent = parent;
301 sc[subunit].sc_callback = callback;
303 error = ucom_attach_tty(ssc, &sc[subunit]);
305 ucom_detach(ssc, &sc[0]);
308 /* increment reference count */
311 /* set subunit attached */
312 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
315 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
316 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
322 * The following function will do nothing if the structure pointed to
323 * by "ssc" and "sc" is zero or has already been detached.
326 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
330 if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
331 return; /* not initialized */
333 if (ssc->sc_sysctl_ttyname != NULL) {
334 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
335 ssc->sc_sysctl_ttyname = NULL;
338 if (ssc->sc_sysctl_ttyports != NULL) {
339 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
340 ssc->sc_sysctl_ttyports = NULL;
343 usb_proc_drain(&ssc->sc_tq);
345 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
346 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
348 ucom_detach_tty(ssc, &sc[subunit]);
350 /* avoid duplicate detach */
351 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
354 usb_proc_free(&ssc->sc_tq);
358 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
361 /* make sure we don't detach twice */
362 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
366 ucom_drain(struct ucom_super_softc *ssc)
369 while (ssc->sc_refs > 0) {
370 printf("ucom: Waiting for a TTY device to close.\n");
371 usb_pause_mtx(&ucom_mtx, hz);
373 mtx_unlock(&ucom_mtx);
377 ucom_drain_all(void *arg)
380 while (ucom_close_refs > 0) {
381 printf("ucom: Waiting for all detached TTY "
382 "devices to have open fds closed.\n");
383 usb_pause_mtx(&ucom_mtx, hz);
385 mtx_unlock(&ucom_mtx);
389 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
392 char buf[32]; /* temporary TTY device name buffer */
394 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
398 /* Check if the client has a custom TTY name */
400 if (sc->sc_callback->ucom_tty_name) {
401 sc->sc_callback->ucom_tty_name(sc, buf,
402 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
405 /* Use default TTY name */
406 if (ssc->sc_subunits > 1) {
407 /* multiple modems in one */
408 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
409 ssc->sc_unit, sc->sc_subunit);
412 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
416 tty_makedev(tp, NULL, "%s", buf);
420 sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
421 sc->sc_pps.driver_abi = PPS_ABI_VERSION;
422 sc->sc_pps.driver_mtx = sc->sc_mtx;
423 pps_init_abi(&sc->sc_pps);
425 DPRINTF("ttycreate: %s\n", buf);
427 /* Check if this device should be a console */
428 if ((ucom_cons_softc == NULL) &&
429 (ssc->sc_unit == ucom_cons_unit) &&
430 (sc->sc_subunit == ucom_cons_subunit)) {
432 DPRINTF("unit %d subunit %d is console",
433 ssc->sc_unit, sc->sc_subunit);
435 ucom_cons_softc = sc;
437 tty_init_console(tp, ucom_cons_baud);
439 UCOM_MTX_LOCK(ucom_cons_softc);
440 ucom_cons_rx_low = 0;
441 ucom_cons_rx_high = 0;
442 ucom_cons_tx_low = 0;
443 ucom_cons_tx_high = 0;
444 sc->sc_flag |= UCOM_FLAG_CONSOLE;
445 ucom_open(ucom_cons_softc->sc_tty);
446 ucom_param(ucom_cons_softc->sc_tty, &tp->t_termios_init_in);
447 UCOM_MTX_UNLOCK(ucom_cons_softc);
454 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
456 struct tty *tp = sc->sc_tty;
458 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
460 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
461 UCOM_MTX_LOCK(ucom_cons_softc);
462 ucom_close(ucom_cons_softc->sc_tty);
463 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
464 UCOM_MTX_UNLOCK(ucom_cons_softc);
465 ucom_cons_softc = NULL;
468 /* the config thread has been stopped when we get here */
471 sc->sc_flag |= UCOM_FLAG_GONE;
472 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
478 mtx_unlock(&ucom_mtx);
482 ucom_close(tp); /* close, if any */
488 * make sure that read and write transfers are stopped
490 if (sc->sc_callback->ucom_stop_read)
491 (sc->sc_callback->ucom_stop_read) (sc);
492 if (sc->sc_callback->ucom_stop_write)
493 (sc->sc_callback->ucom_stop_write) (sc);
499 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
503 struct usb_attach_arg *uaa;
505 snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
506 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
508 /* Store the PNP info in the first interface for the device */
509 uaa = device_get_ivars(dev);
510 iface_index = uaa->info.bIfaceIndex;
512 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
513 device_printf(dev, "Could not set PNP info\n");
516 * The following information is also replicated in the PNP-info
517 * string which is registered above:
519 if (ssc->sc_sysctl_ttyname == NULL) {
520 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
521 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
522 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
523 "TTY device basename");
525 if (ssc->sc_sysctl_ttyports == NULL) {
526 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
527 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
528 OID_AUTO, "ttyports", CTLFLAG_RD,
529 NULL, ssc->sc_subunits, "Number of ports");
534 ucom_queue_command(struct ucom_softc *sc,
535 usb_proc_callback_t *fn, struct termios *pt,
536 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
538 struct ucom_super_softc *ssc = sc->sc_super;
539 struct ucom_param_task *task;
541 UCOM_MTX_ASSERT(sc, MA_OWNED);
543 if (usb_proc_is_gone(&ssc->sc_tq)) {
544 DPRINTF("proc is gone\n");
545 return; /* nothing to do */
548 * NOTE: The task cannot get executed before we drop the
549 * "sc_mtx" mutex. It is safe to update fields in the message
550 * structure after that the message got queued.
552 task = (struct ucom_param_task *)
553 usb_proc_msignal(&ssc->sc_tq, t0, t1);
555 /* Setup callback and softc pointers */
556 task->hdr.pm_callback = fn;
560 * Make a copy of the termios. This field is only present if
561 * the "pt" field is not NULL.
564 task->termios_copy = *pt;
567 * Closing the device should be synchronous.
569 if (fn == ucom_cfg_close)
570 usb_proc_mwait(&ssc->sc_tq, t0, t1);
573 * In case of multiple configure requests,
574 * keep track of the last one!
576 if (fn == ucom_cfg_start_transfers)
577 sc->sc_last_start_xfer = &task->hdr;
581 ucom_shutdown(struct ucom_softc *sc)
583 struct tty *tp = sc->sc_tty;
585 UCOM_MTX_ASSERT(sc, MA_OWNED);
590 * Hang up if necessary:
592 if (tp->t_termios.c_cflag & HUPCL) {
593 ucom_modem(tp, 0, SER_DTR);
600 * else: taskqueue is draining or gone
603 ucom_cfg_is_gone(struct ucom_softc *sc)
605 struct ucom_super_softc *ssc = sc->sc_super;
607 return (usb_proc_is_gone(&ssc->sc_tq));
611 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
613 struct ucom_cfg_task *task =
614 (struct ucom_cfg_task *)_task;
615 struct ucom_softc *sc = task->sc;
617 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
620 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
621 /* TTY device closed */
625 if (_task == sc->sc_last_start_xfer)
626 sc->sc_flag |= UCOM_FLAG_GP_DATA;
628 if (sc->sc_callback->ucom_start_read) {
629 (sc->sc_callback->ucom_start_read) (sc);
631 if (sc->sc_callback->ucom_start_write) {
632 (sc->sc_callback->ucom_start_write) (sc);
637 ucom_start_transfers(struct ucom_softc *sc)
639 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
643 * Make sure that data transfers are started in both
646 if (sc->sc_callback->ucom_start_read) {
647 (sc->sc_callback->ucom_start_read) (sc);
649 if (sc->sc_callback->ucom_start_write) {
650 (sc->sc_callback->ucom_start_write) (sc);
655 ucom_cfg_open(struct usb_proc_msg *_task)
657 struct ucom_cfg_task *task =
658 (struct ucom_cfg_task *)_task;
659 struct ucom_softc *sc = task->sc;
663 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
669 sc->sc_flag |= UCOM_FLAG_LL_READY;
671 if (sc->sc_callback->ucom_cfg_open) {
672 (sc->sc_callback->ucom_cfg_open) (sc);
675 usb_pause_mtx(sc->sc_mtx, hz / 10);
681 ucom_open(struct tty *tp)
683 struct ucom_softc *sc = tty_softc(tp);
686 UCOM_MTX_ASSERT(sc, MA_OWNED);
688 if (sc->sc_flag & UCOM_FLAG_GONE) {
691 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
695 DPRINTF("tp = %p\n", tp);
697 if (sc->sc_callback->ucom_pre_open) {
699 * give the lower layer a chance to disallow TTY open, for
700 * example if the device is not present:
702 error = (sc->sc_callback->ucom_pre_open) (sc);
707 sc->sc_flag |= UCOM_FLAG_HL_READY;
709 /* Disable transfers */
710 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
716 /* reset programmed line state */
721 /* reset jitter buffer */
722 sc->sc_jitterbuf_in = 0;
723 sc->sc_jitterbuf_out = 0;
725 ucom_queue_command(sc, ucom_cfg_open, NULL,
726 &sc->sc_open_task[0].hdr,
727 &sc->sc_open_task[1].hdr);
729 /* Queue transfer enable command last */
730 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
731 &sc->sc_start_task[0].hdr,
732 &sc->sc_start_task[1].hdr);
734 ucom_modem(tp, SER_DTR | SER_RTS, 0);
740 ucom_status_change(sc);
746 ucom_cfg_close(struct usb_proc_msg *_task)
748 struct ucom_cfg_task *task =
749 (struct ucom_cfg_task *)_task;
750 struct ucom_softc *sc = task->sc;
754 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
755 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
756 if (sc->sc_callback->ucom_cfg_close)
757 (sc->sc_callback->ucom_cfg_close) (sc);
764 ucom_close(struct tty *tp)
766 struct ucom_softc *sc = tty_softc(tp);
768 UCOM_MTX_ASSERT(sc, MA_OWNED);
770 DPRINTF("tp=%p\n", tp);
772 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
773 DPRINTF("tp=%p already closed\n", tp);
778 ucom_queue_command(sc, ucom_cfg_close, NULL,
779 &sc->sc_close_task[0].hdr,
780 &sc->sc_close_task[1].hdr);
782 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
784 if (sc->sc_callback->ucom_stop_read) {
785 (sc->sc_callback->ucom_stop_read) (sc);
790 ucom_inwakeup(struct tty *tp)
792 struct ucom_softc *sc = tty_softc(tp);
798 UCOM_MTX_ASSERT(sc, MA_OWNED);
800 DPRINTF("tp=%p\n", tp);
802 if (ttydisc_can_bypass(tp) != 0 ||
803 (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
804 (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
808 /* prevent recursion */
809 sc->sc_flag |= UCOM_FLAG_INWAKEUP;
811 pos = sc->sc_jitterbuf_out;
813 while (sc->sc_jitterbuf_in != pos) {
816 c = (char)sc->sc_jitterbuf[pos];
818 if (ttydisc_rint(tp, c, 0) == -1)
821 if (pos >= UCOM_JITTERBUF_SIZE)
822 pos -= UCOM_JITTERBUF_SIZE;
825 sc->sc_jitterbuf_out = pos;
827 /* clear RTS in async fashion */
828 if ((sc->sc_jitterbuf_in == pos) &&
829 (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
832 sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
836 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
838 struct ucom_softc *sc = tty_softc(tp);
841 UCOM_MTX_ASSERT(sc, MA_OWNED);
843 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
846 DPRINTF("cmd = 0x%08lx\n", cmd);
868 if (sc->sc_callback->ucom_ioctl) {
869 error = (sc->sc_callback->ucom_ioctl)
870 (sc, cmd, data, 0, td);
874 if (error == ENOIOCTL)
875 error = pps_ioctl(cmd, data, &sc->sc_pps);
882 ucom_modem(struct tty *tp, int sigon, int sigoff)
884 struct ucom_softc *sc = tty_softc(tp);
887 UCOM_MTX_ASSERT(sc, MA_OWNED);
889 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
892 if ((sigon == 0) && (sigoff == 0)) {
894 if (sc->sc_mcr & SER_DTR) {
897 if (sc->sc_mcr & SER_RTS) {
900 if (sc->sc_msr & SER_CTS) {
903 if (sc->sc_msr & SER_DCD) {
906 if (sc->sc_msr & SER_DSR) {
909 if (sc->sc_msr & SER_RI) {
914 if (sigon & SER_DTR) {
915 sc->sc_mcr |= SER_DTR;
917 if (sigoff & SER_DTR) {
918 sc->sc_mcr &= ~SER_DTR;
920 if (sigon & SER_RTS) {
921 sc->sc_mcr |= SER_RTS;
923 if (sigoff & SER_RTS) {
924 sc->sc_mcr &= ~SER_RTS;
926 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
929 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
936 ucom_cfg_line_state(struct usb_proc_msg *_task)
938 struct ucom_cfg_task *task =
939 (struct ucom_cfg_task *)_task;
940 struct ucom_softc *sc = task->sc;
947 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
952 /* compute callback mask */
953 if (sc->sc_callback->ucom_cfg_set_dtr)
955 if (sc->sc_callback->ucom_cfg_set_rts)
957 if (sc->sc_callback->ucom_cfg_set_break)
958 mask |= UCOM_LS_BREAK;
959 if (sc->sc_callback->ucom_cfg_set_ring)
960 mask |= UCOM_LS_RING;
962 /* compute the bits we are to program */
963 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
964 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
965 prev_value = sc->sc_pls_curr ^ notch_bits;
966 last_value = sc->sc_pls_curr;
968 /* reset programmed line state */
973 /* ensure that we don't lose any levels */
974 if (notch_bits & UCOM_LS_DTR)
975 sc->sc_callback->ucom_cfg_set_dtr(sc,
976 (prev_value & UCOM_LS_DTR) ? 1 : 0);
977 if (notch_bits & UCOM_LS_RTS)
978 sc->sc_callback->ucom_cfg_set_rts(sc,
979 (prev_value & UCOM_LS_RTS) ? 1 : 0);
980 if (notch_bits & UCOM_LS_BREAK)
981 sc->sc_callback->ucom_cfg_set_break(sc,
982 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
983 if (notch_bits & UCOM_LS_RING)
984 sc->sc_callback->ucom_cfg_set_ring(sc,
985 (prev_value & UCOM_LS_RING) ? 1 : 0);
988 if (any_bits & UCOM_LS_DTR)
989 sc->sc_callback->ucom_cfg_set_dtr(sc,
990 (last_value & UCOM_LS_DTR) ? 1 : 0);
991 if (any_bits & UCOM_LS_RTS)
992 sc->sc_callback->ucom_cfg_set_rts(sc,
993 (last_value & UCOM_LS_RTS) ? 1 : 0);
994 if (any_bits & UCOM_LS_BREAK)
995 sc->sc_callback->ucom_cfg_set_break(sc,
996 (last_value & UCOM_LS_BREAK) ? 1 : 0);
997 if (any_bits & UCOM_LS_RING)
998 sc->sc_callback->ucom_cfg_set_ring(sc,
999 (last_value & UCOM_LS_RING) ? 1 : 0);
1003 ucom_line_state(struct ucom_softc *sc,
1004 uint8_t set_bits, uint8_t clear_bits)
1006 UCOM_MTX_ASSERT(sc, MA_OWNED);
1008 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1012 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1014 /* update current programmed line state */
1015 sc->sc_pls_curr |= set_bits;
1016 sc->sc_pls_curr &= ~clear_bits;
1017 sc->sc_pls_set |= set_bits;
1018 sc->sc_pls_clr |= clear_bits;
1020 /* defer driver programming */
1021 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1022 &sc->sc_line_state_task[0].hdr,
1023 &sc->sc_line_state_task[1].hdr);
1027 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1029 DPRINTF("onoff = %d\n", onoff);
1032 ucom_line_state(sc, UCOM_LS_RING, 0);
1034 ucom_line_state(sc, 0, UCOM_LS_RING);
1038 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1040 DPRINTF("onoff = %d\n", onoff);
1043 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1045 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1049 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1051 DPRINTF("onoff = %d\n", onoff);
1054 ucom_line_state(sc, UCOM_LS_DTR, 0);
1056 ucom_line_state(sc, 0, UCOM_LS_DTR);
1060 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1062 DPRINTF("onoff = %d\n", onoff);
1065 ucom_line_state(sc, UCOM_LS_RTS, 0);
1067 ucom_line_state(sc, 0, UCOM_LS_RTS);
1071 ucom_cfg_status_change(struct usb_proc_msg *_task)
1073 struct ucom_cfg_task *task =
1074 (struct ucom_cfg_task *)_task;
1075 struct ucom_softc *sc = task->sc;
1084 UCOM_MTX_ASSERT(sc, MA_OWNED);
1086 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1089 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1097 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1099 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1100 /* TTY device closed */
1103 msr_delta = (sc->sc_msr ^ new_msr);
1104 lsr_delta = (sc->sc_lsr ^ new_lsr);
1106 sc->sc_msr = new_msr;
1107 sc->sc_lsr = new_lsr;
1110 * Time pulse counting support. Note that both CTS and DCD are
1111 * active-low signals. The status bit is high to indicate that
1112 * the signal on the line is low, which corresponds to a PPS
1115 switch(ucom_pps_mode) {
1117 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1118 (msr_delta & SER_CTS)) {
1119 pps_capture(&sc->sc_pps);
1120 pps_event(&sc->sc_pps, (sc->sc_msr & SER_CTS) ?
1121 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
1125 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1126 (msr_delta & SER_DCD)) {
1127 pps_capture(&sc->sc_pps);
1128 pps_event(&sc->sc_pps, (sc->sc_msr & SER_DCD) ?
1129 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
1136 if (msr_delta & SER_DCD) {
1138 int onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1140 DPRINTF("DCD changed to %d\n", onoff);
1142 ttydisc_modem(tp, onoff);
1145 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1147 DPRINTF("BREAK detected\n");
1149 ttydisc_rint(tp, 0, TRE_BREAK);
1150 ttydisc_rint_done(tp);
1153 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1155 DPRINTF("Frame error detected\n");
1157 ttydisc_rint(tp, 0, TRE_FRAMING);
1158 ttydisc_rint_done(tp);
1161 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1163 DPRINTF("Parity error detected\n");
1165 ttydisc_rint(tp, 0, TRE_PARITY);
1166 ttydisc_rint_done(tp);
1171 ucom_status_change(struct ucom_softc *sc)
1173 UCOM_MTX_ASSERT(sc, MA_OWNED);
1175 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1176 return; /* not supported */
1178 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1183 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1184 &sc->sc_status_task[0].hdr,
1185 &sc->sc_status_task[1].hdr);
1189 ucom_cfg_param(struct usb_proc_msg *_task)
1191 struct ucom_param_task *task =
1192 (struct ucom_param_task *)_task;
1193 struct ucom_softc *sc = task->sc;
1195 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1198 if (sc->sc_callback->ucom_cfg_param == NULL) {
1202 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1205 usb_pause_mtx(sc->sc_mtx, hz / 10);
1209 ucom_param(struct tty *tp, struct termios *t)
1211 struct ucom_softc *sc = tty_softc(tp);
1215 UCOM_MTX_ASSERT(sc, MA_OWNED);
1220 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1222 /* XXX the TTY layer should call "open()" first! */
1224 * Not quite: Its ordering is partly backwards, but
1225 * some parameters must be set early in ttydev_open(),
1226 * possibly before calling ttydevsw_open().
1228 error = ucom_open(tp);
1234 DPRINTF("sc = %p\n", sc);
1236 /* Check requested parameters. */
1237 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1238 /* XXX c_ospeed == 0 is perfectly valid. */
1239 DPRINTF("mismatch ispeed and ospeed\n");
1243 t->c_ispeed = t->c_ospeed;
1245 if (sc->sc_callback->ucom_pre_param) {
1246 /* Let the lower layer verify the parameters */
1247 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1249 DPRINTF("callback error = %d\n", error);
1254 /* Disable transfers */
1255 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1257 /* Queue baud rate programming command first */
1258 ucom_queue_command(sc, ucom_cfg_param, t,
1259 &sc->sc_param_task[0].hdr,
1260 &sc->sc_param_task[1].hdr);
1262 /* Queue transfer enable command last */
1263 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1264 &sc->sc_start_task[0].hdr,
1265 &sc->sc_start_task[1].hdr);
1267 if (t->c_cflag & CRTS_IFLOW) {
1268 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1269 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1270 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1271 ucom_modem(tp, SER_RTS, 0);
1283 ucom_outwakeup(struct tty *tp)
1285 struct ucom_softc *sc = tty_softc(tp);
1287 UCOM_MTX_ASSERT(sc, MA_OWNED);
1289 DPRINTF("sc = %p\n", sc);
1291 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1292 /* The higher layer is not ready */
1295 ucom_start_transfers(sc);
1298 /*------------------------------------------------------------------------*
1302 * 0: No data is available.
1303 * Else: Data is available.
1304 *------------------------------------------------------------------------*/
1306 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1307 uint32_t offset, uint32_t len, uint32_t *actlen)
1309 struct usb_page_search res;
1310 struct tty *tp = sc->sc_tty;
1312 uint32_t offset_orig;
1314 UCOM_MTX_ASSERT(sc, MA_OWNED);
1316 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1319 /* get total TX length */
1321 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1322 temp %= UCOM_CONS_BUFSIZE;
1324 /* limit TX length */
1326 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1327 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1334 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1336 /* update counters */
1338 ucom_cons_tx_low += temp;
1339 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1341 /* store actual length */
1345 return (temp ? 1 : 0);
1349 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1351 return (0); /* multiport device polling */
1353 offset_orig = offset;
1357 usbd_get_page(pc, offset, &res);
1359 if (res.length > len) {
1362 /* copy data directly into USB buffer */
1363 cnt = ttydisc_getc(tp, res.buffer, res.length);
1368 if (cnt < res.length) {
1374 actlen[0] = offset - offset_orig;
1376 DPRINTF("cnt=%d\n", actlen[0]);
1378 if (actlen[0] == 0) {
1385 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1386 uint32_t offset, uint32_t len)
1388 struct usb_page_search res;
1389 struct tty *tp = sc->sc_tty;
1393 UCOM_MTX_ASSERT(sc, MA_OWNED);
1395 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1398 /* get maximum RX length */
1400 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1401 temp %= UCOM_CONS_BUFSIZE;
1403 /* limit RX length */
1405 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1406 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1413 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1415 /* update counters */
1417 ucom_cons_rx_high += temp;
1418 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1424 return; /* multiport device polling */
1427 return; /* no data */
1429 /* set a flag to prevent recursation ? */
1433 usbd_get_page(pc, offset, &res);
1435 if (res.length > len) {
1439 offset += res.length;
1441 /* pass characters to tty layer */
1446 /* first check if we can pass the buffer directly */
1448 if (ttydisc_can_bypass(tp)) {
1450 /* clear any jitter buffer */
1451 sc->sc_jitterbuf_in = 0;
1452 sc->sc_jitterbuf_out = 0;
1454 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1455 DPRINTF("tp=%p, data lost\n", tp);
1461 for (cnt = 0; cnt != res.length; cnt++) {
1462 if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1463 ttydisc_rint(tp, buf[cnt], 0) == -1) {
1467 pos = sc->sc_jitterbuf_in;
1468 end = sc->sc_jitterbuf_out +
1469 UCOM_JITTERBUF_SIZE - 1;
1470 if (end >= UCOM_JITTERBUF_SIZE)
1471 end -= UCOM_JITTERBUF_SIZE;
1473 for (; cnt != res.length; cnt++) {
1476 sc->sc_jitterbuf[pos] = buf[cnt];
1478 if (pos >= UCOM_JITTERBUF_SIZE)
1479 pos -= UCOM_JITTERBUF_SIZE;
1482 sc->sc_jitterbuf_in = pos;
1484 /* set RTS in async fashion */
1485 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1488 DPRINTF("tp=%p, lost %d "
1489 "chars\n", tp, res.length - cnt);
1494 ttydisc_rint_done(tp);
1498 ucom_free(void *xsc)
1500 struct ucom_softc *sc = xsc;
1502 if (sc->sc_callback->ucom_free != NULL)
1503 sc->sc_callback->ucom_free(sc);
1505 ucom_unref(sc->sc_super);
1507 mtx_lock(&ucom_mtx);
1509 mtx_unlock(&ucom_mtx);
1512 static cn_probe_t ucom_cnprobe;
1513 static cn_init_t ucom_cninit;
1514 static cn_term_t ucom_cnterm;
1515 static cn_getc_t ucom_cngetc;
1516 static cn_putc_t ucom_cnputc;
1517 static cn_grab_t ucom_cngrab;
1518 static cn_ungrab_t ucom_cnungrab;
1520 CONSOLE_DRIVER(ucom);
1523 ucom_cnprobe(struct consdev *cp)
1525 if (ucom_cons_unit != -1)
1526 cp->cn_pri = CN_NORMAL;
1528 cp->cn_pri = CN_DEAD;
1530 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1534 ucom_cninit(struct consdev *cp)
1539 ucom_cnterm(struct consdev *cp)
1544 ucom_cngrab(struct consdev *cp)
1549 ucom_cnungrab(struct consdev *cp)
1554 ucom_cngetc(struct consdev *cd)
1556 struct ucom_softc *sc = ucom_cons_softc;
1564 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1565 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1566 ucom_cons_rx_low ++;
1567 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1572 /* start USB transfers */
1573 ucom_outwakeup(sc->sc_tty);
1575 UCOM_MTX_UNLOCK(sc);
1577 /* poll if necessary */
1578 if (kdb_active && sc->sc_callback->ucom_poll)
1579 (sc->sc_callback->ucom_poll) (sc);
1585 ucom_cnputc(struct consdev *cd, int c)
1587 struct ucom_softc *sc = ucom_cons_softc;
1597 /* compute maximum TX length */
1599 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1600 temp %= UCOM_CONS_BUFSIZE;
1603 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1604 ucom_cons_tx_high ++;
1605 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1608 /* start USB transfers */
1609 ucom_outwakeup(sc->sc_tty);
1611 UCOM_MTX_UNLOCK(sc);
1613 /* poll if necessary */
1614 if (kdb_active && sc->sc_callback->ucom_poll) {
1615 (sc->sc_callback->ucom_poll) (sc);
1616 /* simple flow control */
1622 /*------------------------------------------------------------------------*
1625 * This function will increment the super UCOM reference count.
1626 *------------------------------------------------------------------------*/
1628 ucom_ref(struct ucom_super_softc *ssc)
1630 mtx_lock(&ucom_mtx);
1632 mtx_unlock(&ucom_mtx);
1635 /*------------------------------------------------------------------------*
1638 * This function will free the super UCOM's allocated unit
1639 * number. This function can be called on a zero-initialized
1640 * structure. This function can be called multiple times.
1641 *------------------------------------------------------------------------*/
1643 ucom_free_unit(struct ucom_super_softc *ssc)
1645 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1648 ucom_unit_free(ssc->sc_unit);
1650 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1653 /*------------------------------------------------------------------------*
1656 * This function will decrement the super UCOM reference count.
1659 * 0: UCOM structures are still referenced.
1660 * Else: UCOM structures are no longer referenced.
1661 *------------------------------------------------------------------------*/
1663 ucom_unref(struct ucom_super_softc *ssc)
1667 mtx_lock(&ucom_mtx);
1668 retval = (ssc->sc_refs < 2);
1670 mtx_unlock(&ucom_mtx);
1673 ucom_free_unit(ssc);
1680 #include <gdb/gdb.h>
1682 static gdb_probe_f ucom_gdbprobe;
1683 static gdb_init_f ucom_gdbinit;
1684 static gdb_term_f ucom_gdbterm;
1685 static gdb_getc_f ucom_gdbgetc;
1686 static gdb_putc_f ucom_gdbputc;
1688 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1693 return ((ucom_cons_softc != NULL) ? 0 : -1);
1709 ucom_cnputc(NULL, c);
1715 return (ucom_cngetc(NULL));