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/uart/uart_ppstypes.h>
86 #include <dev/usb/usb.h>
87 #include <dev/usb/usbdi.h>
88 #include <dev/usb/usbdi_util.h>
90 #define USB_DEBUG_VAR ucom_debug
91 #include <dev/usb/usb_debug.h>
92 #include <dev/usb/usb_busdma.h>
93 #include <dev/usb/usb_process.h>
95 #include <dev/usb/serial/usb_serial.h>
99 static SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
101 static int ucom_pps_mode;
103 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, pps_mode, CTLFLAG_RWTUN,
105 "pulse capture mode: 0/1/2=disabled/CTS/DCD; add 0x10 to invert");
108 static int ucom_debug = 0;
110 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
111 &ucom_debug, 0, "ucom debug level");
114 #define UCOM_CONS_BUFSIZE 1024
116 static uint8_t ucom_cons_rx_buf[UCOM_CONS_BUFSIZE];
117 static uint8_t ucom_cons_tx_buf[UCOM_CONS_BUFSIZE];
119 static unsigned int ucom_cons_rx_low = 0;
120 static unsigned int ucom_cons_rx_high = 0;
122 static unsigned int ucom_cons_tx_low = 0;
123 static unsigned int ucom_cons_tx_high = 0;
125 static int ucom_cons_unit = -1;
126 static int ucom_cons_subunit = 0;
127 static int ucom_cons_baud = 9600;
128 static struct ucom_softc *ucom_cons_softc = NULL;
130 TUNABLE_INT("hw.usb.ucom.cons_unit", &ucom_cons_unit);
131 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_unit, CTLFLAG_RW | CTLFLAG_TUN,
132 &ucom_cons_unit, 0, "console unit number");
133 TUNABLE_INT("hw.usb.ucom.cons_subunit", &ucom_cons_subunit);
134 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_subunit, CTLFLAG_RW | CTLFLAG_TUN,
135 &ucom_cons_subunit, 0, "console subunit number");
136 TUNABLE_INT("hw.usb.ucom.cons_baud", &ucom_cons_baud);
137 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RW | CTLFLAG_TUN,
138 &ucom_cons_baud, 0, "console baud rate");
140 static usb_proc_callback_t ucom_cfg_start_transfers;
141 static usb_proc_callback_t ucom_cfg_open;
142 static usb_proc_callback_t ucom_cfg_close;
143 static usb_proc_callback_t ucom_cfg_line_state;
144 static usb_proc_callback_t ucom_cfg_status_change;
145 static usb_proc_callback_t ucom_cfg_param;
147 static int ucom_unit_alloc(void);
148 static void ucom_unit_free(int);
149 static int ucom_attach_tty(struct ucom_super_softc *, struct ucom_softc *);
150 static void ucom_detach_tty(struct ucom_super_softc *, struct ucom_softc *);
151 static void ucom_queue_command(struct ucom_softc *,
152 usb_proc_callback_t *, struct termios *pt,
153 struct usb_proc_msg *t0, struct usb_proc_msg *t1);
154 static void ucom_shutdown(struct ucom_softc *);
155 static void ucom_ring(struct ucom_softc *, uint8_t);
156 static void ucom_break(struct ucom_softc *, uint8_t);
157 static void ucom_dtr(struct ucom_softc *, uint8_t);
158 static void ucom_rts(struct ucom_softc *, uint8_t);
160 static tsw_open_t ucom_open;
161 static tsw_close_t ucom_close;
162 static tsw_ioctl_t ucom_ioctl;
163 static tsw_modem_t ucom_modem;
164 static tsw_param_t ucom_param;
165 static tsw_outwakeup_t ucom_outwakeup;
166 static tsw_inwakeup_t ucom_inwakeup;
167 static tsw_free_t ucom_free;
169 static struct ttydevsw ucom_class = {
170 .tsw_flags = TF_INITLOCK | TF_CALLOUT,
171 .tsw_open = ucom_open,
172 .tsw_close = ucom_close,
173 .tsw_outwakeup = ucom_outwakeup,
174 .tsw_inwakeup = ucom_inwakeup,
175 .tsw_ioctl = ucom_ioctl,
176 .tsw_param = ucom_param,
177 .tsw_modem = ucom_modem,
178 .tsw_free = ucom_free,
181 MODULE_DEPEND(ucom, usb, 1, 1, 1);
182 MODULE_VERSION(ucom, 1);
184 #define UCOM_UNIT_MAX 128 /* maximum number of units */
185 #define UCOM_TTY_PREFIX "U"
187 static struct unrhdr *ucom_unrhdr;
188 static struct mtx ucom_mtx;
189 static int ucom_close_refs;
195 ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
196 mtx_init(&ucom_mtx, "UCOM MTX", NULL, MTX_DEF);
198 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
201 ucom_uninit(void *arg)
212 mtx_destroy(&ucom_mtx);
214 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 3, SI_ORDER_ANY, ucom_uninit, NULL);
217 * Mark a unit number (the X in cuaUX) as in use.
219 * Note that devices using a different naming scheme (see ucom_tty_name()
220 * callback) still use this unit allocation.
223 ucom_unit_alloc(void)
228 if (ucom_unrhdr == NULL) {
229 DPRINTF("ucom_unrhdr is NULL\n");
232 unit = alloc_unr(ucom_unrhdr);
233 DPRINTF("unit %d is allocated\n", unit);
238 * Mark the unit number as not in use.
241 ucom_unit_free(int unit)
244 if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
245 DPRINTF("cannot free unit number\n");
248 DPRINTF("unit %d is freed\n", unit);
249 free_unr(ucom_unrhdr, unit);
253 * Setup a group of one or more serial ports.
255 * The mutex pointed to by "mtx" is applied before all
256 * callbacks are called back. Also "mtx" must be applied
257 * before calling into the ucom-layer!
260 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
261 int subunits, void *parent,
262 const struct ucom_callback *callback, struct mtx *mtx)
269 (callback == NULL) ||
274 /* allocate a uniq unit number */
275 ssc->sc_unit = ucom_unit_alloc();
276 if (ssc->sc_unit == -1)
279 /* generate TTY name string */
280 snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
281 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
283 /* create USB request handling process */
284 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
286 ucom_unit_free(ssc->sc_unit);
289 ssc->sc_subunits = subunits;
290 ssc->sc_flag = UCOM_FLAG_ATTACHED |
293 if (callback->ucom_free == NULL)
294 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
296 /* increment reference count */
299 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
300 sc[subunit].sc_subunit = subunit;
301 sc[subunit].sc_super = ssc;
302 sc[subunit].sc_mtx = mtx;
303 sc[subunit].sc_parent = parent;
304 sc[subunit].sc_callback = callback;
306 error = ucom_attach_tty(ssc, &sc[subunit]);
308 ucom_detach(ssc, &sc[0]);
311 /* increment reference count */
314 /* set subunit attached */
315 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
318 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
319 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
325 * The following function will do nothing if the structure pointed to
326 * by "ssc" and "sc" is zero or has already been detached.
329 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
333 if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
334 return; /* not initialized */
336 if (ssc->sc_sysctl_ttyname != NULL) {
337 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
338 ssc->sc_sysctl_ttyname = NULL;
341 if (ssc->sc_sysctl_ttyports != NULL) {
342 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
343 ssc->sc_sysctl_ttyports = NULL;
346 usb_proc_drain(&ssc->sc_tq);
348 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
349 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
351 ucom_detach_tty(ssc, &sc[subunit]);
353 /* avoid duplicate detach */
354 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
357 usb_proc_free(&ssc->sc_tq);
361 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
364 /* make sure we don't detach twice */
365 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
369 ucom_drain(struct ucom_super_softc *ssc)
372 while (ssc->sc_refs > 0) {
373 printf("ucom: Waiting for a TTY device to close.\n");
374 usb_pause_mtx(&ucom_mtx, hz);
376 mtx_unlock(&ucom_mtx);
380 ucom_drain_all(void *arg)
383 while (ucom_close_refs > 0) {
384 printf("ucom: Waiting for all detached TTY "
385 "devices to have open fds closed.\n");
386 usb_pause_mtx(&ucom_mtx, hz);
388 mtx_unlock(&ucom_mtx);
392 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
395 char buf[32]; /* temporary TTY device name buffer */
397 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
401 /* Check if the client has a custom TTY name */
403 if (sc->sc_callback->ucom_tty_name) {
404 sc->sc_callback->ucom_tty_name(sc, buf,
405 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
408 /* Use default TTY name */
409 if (ssc->sc_subunits > 1) {
410 /* multiple modems in one */
411 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
412 ssc->sc_unit, sc->sc_subunit);
415 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
419 tty_makedev(tp, NULL, "%s", buf);
423 sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
424 sc->sc_pps.driver_abi = PPS_ABI_VERSION;
425 sc->sc_pps.driver_mtx = sc->sc_mtx;
426 pps_init_abi(&sc->sc_pps);
428 DPRINTF("ttycreate: %s\n", buf);
430 /* Check if this device should be a console */
431 if ((ucom_cons_softc == NULL) &&
432 (ssc->sc_unit == ucom_cons_unit) &&
433 (sc->sc_subunit == ucom_cons_subunit)) {
435 DPRINTF("unit %d subunit %d is console",
436 ssc->sc_unit, sc->sc_subunit);
438 ucom_cons_softc = sc;
440 tty_init_console(tp, ucom_cons_baud);
442 UCOM_MTX_LOCK(ucom_cons_softc);
443 ucom_cons_rx_low = 0;
444 ucom_cons_rx_high = 0;
445 ucom_cons_tx_low = 0;
446 ucom_cons_tx_high = 0;
447 sc->sc_flag |= UCOM_FLAG_CONSOLE;
448 ucom_open(ucom_cons_softc->sc_tty);
449 ucom_param(ucom_cons_softc->sc_tty, &tp->t_termios_init_in);
450 UCOM_MTX_UNLOCK(ucom_cons_softc);
457 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
459 struct tty *tp = sc->sc_tty;
461 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
463 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
464 UCOM_MTX_LOCK(ucom_cons_softc);
465 ucom_close(ucom_cons_softc->sc_tty);
466 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
467 UCOM_MTX_UNLOCK(ucom_cons_softc);
468 ucom_cons_softc = NULL;
471 /* the config thread has been stopped when we get here */
474 sc->sc_flag |= UCOM_FLAG_GONE;
475 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
481 mtx_unlock(&ucom_mtx);
485 ucom_close(tp); /* close, if any */
491 * make sure that read and write transfers are stopped
493 if (sc->sc_callback->ucom_stop_read)
494 (sc->sc_callback->ucom_stop_read) (sc);
495 if (sc->sc_callback->ucom_stop_write)
496 (sc->sc_callback->ucom_stop_write) (sc);
502 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
506 struct usb_attach_arg *uaa;
508 snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
509 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
511 /* Store the PNP info in the first interface for the device */
512 uaa = device_get_ivars(dev);
513 iface_index = uaa->info.bIfaceIndex;
515 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
516 device_printf(dev, "Could not set PNP info\n");
519 * The following information is also replicated in the PNP-info
520 * string which is registered above:
522 if (ssc->sc_sysctl_ttyname == NULL) {
523 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
524 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
525 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
526 "TTY device basename");
528 if (ssc->sc_sysctl_ttyports == NULL) {
529 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
530 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
531 OID_AUTO, "ttyports", CTLFLAG_RD,
532 NULL, ssc->sc_subunits, "Number of ports");
537 ucom_queue_command(struct ucom_softc *sc,
538 usb_proc_callback_t *fn, struct termios *pt,
539 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
541 struct ucom_super_softc *ssc = sc->sc_super;
542 struct ucom_param_task *task;
544 UCOM_MTX_ASSERT(sc, MA_OWNED);
546 if (usb_proc_is_gone(&ssc->sc_tq)) {
547 DPRINTF("proc is gone\n");
548 return; /* nothing to do */
551 * NOTE: The task cannot get executed before we drop the
552 * "sc_mtx" mutex. It is safe to update fields in the message
553 * structure after that the message got queued.
555 task = (struct ucom_param_task *)
556 usb_proc_msignal(&ssc->sc_tq, t0, t1);
558 /* Setup callback and softc pointers */
559 task->hdr.pm_callback = fn;
563 * Make a copy of the termios. This field is only present if
564 * the "pt" field is not NULL.
567 task->termios_copy = *pt;
570 * Closing the device should be synchronous.
572 if (fn == ucom_cfg_close)
573 usb_proc_mwait(&ssc->sc_tq, t0, t1);
576 * In case of multiple configure requests,
577 * keep track of the last one!
579 if (fn == ucom_cfg_start_transfers)
580 sc->sc_last_start_xfer = &task->hdr;
584 ucom_shutdown(struct ucom_softc *sc)
586 struct tty *tp = sc->sc_tty;
588 UCOM_MTX_ASSERT(sc, MA_OWNED);
593 * Hang up if necessary:
595 if (tp->t_termios.c_cflag & HUPCL) {
596 ucom_modem(tp, 0, SER_DTR);
603 * else: taskqueue is draining or gone
606 ucom_cfg_is_gone(struct ucom_softc *sc)
608 struct ucom_super_softc *ssc = sc->sc_super;
610 return (usb_proc_is_gone(&ssc->sc_tq));
614 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
616 struct ucom_cfg_task *task =
617 (struct ucom_cfg_task *)_task;
618 struct ucom_softc *sc = task->sc;
620 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
623 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
624 /* TTY device closed */
628 if (_task == sc->sc_last_start_xfer)
629 sc->sc_flag |= UCOM_FLAG_GP_DATA;
631 if (sc->sc_callback->ucom_start_read) {
632 (sc->sc_callback->ucom_start_read) (sc);
634 if (sc->sc_callback->ucom_start_write) {
635 (sc->sc_callback->ucom_start_write) (sc);
640 ucom_start_transfers(struct ucom_softc *sc)
642 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
646 * Make sure that data transfers are started in both
649 if (sc->sc_callback->ucom_start_read) {
650 (sc->sc_callback->ucom_start_read) (sc);
652 if (sc->sc_callback->ucom_start_write) {
653 (sc->sc_callback->ucom_start_write) (sc);
658 ucom_cfg_open(struct usb_proc_msg *_task)
660 struct ucom_cfg_task *task =
661 (struct ucom_cfg_task *)_task;
662 struct ucom_softc *sc = task->sc;
666 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
672 sc->sc_flag |= UCOM_FLAG_LL_READY;
674 if (sc->sc_callback->ucom_cfg_open) {
675 (sc->sc_callback->ucom_cfg_open) (sc);
678 usb_pause_mtx(sc->sc_mtx, hz / 10);
684 ucom_open(struct tty *tp)
686 struct ucom_softc *sc = tty_softc(tp);
689 UCOM_MTX_ASSERT(sc, MA_OWNED);
691 if (sc->sc_flag & UCOM_FLAG_GONE) {
694 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
698 DPRINTF("tp = %p\n", tp);
700 if (sc->sc_callback->ucom_pre_open) {
702 * give the lower layer a chance to disallow TTY open, for
703 * example if the device is not present:
705 error = (sc->sc_callback->ucom_pre_open) (sc);
710 sc->sc_flag |= UCOM_FLAG_HL_READY;
712 /* Disable transfers */
713 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
719 /* reset programmed line state */
724 /* reset jitter buffer */
725 sc->sc_jitterbuf_in = 0;
726 sc->sc_jitterbuf_out = 0;
728 ucom_queue_command(sc, ucom_cfg_open, NULL,
729 &sc->sc_open_task[0].hdr,
730 &sc->sc_open_task[1].hdr);
732 /* Queue transfer enable command last */
733 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
734 &sc->sc_start_task[0].hdr,
735 &sc->sc_start_task[1].hdr);
737 ucom_modem(tp, SER_DTR | SER_RTS, 0);
743 ucom_status_change(sc);
749 ucom_cfg_close(struct usb_proc_msg *_task)
751 struct ucom_cfg_task *task =
752 (struct ucom_cfg_task *)_task;
753 struct ucom_softc *sc = task->sc;
757 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
758 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
759 if (sc->sc_callback->ucom_cfg_close)
760 (sc->sc_callback->ucom_cfg_close) (sc);
767 ucom_close(struct tty *tp)
769 struct ucom_softc *sc = tty_softc(tp);
771 UCOM_MTX_ASSERT(sc, MA_OWNED);
773 DPRINTF("tp=%p\n", tp);
775 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
776 DPRINTF("tp=%p already closed\n", tp);
781 ucom_queue_command(sc, ucom_cfg_close, NULL,
782 &sc->sc_close_task[0].hdr,
783 &sc->sc_close_task[1].hdr);
785 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
787 if (sc->sc_callback->ucom_stop_read) {
788 (sc->sc_callback->ucom_stop_read) (sc);
793 ucom_inwakeup(struct tty *tp)
795 struct ucom_softc *sc = tty_softc(tp);
801 UCOM_MTX_ASSERT(sc, MA_OWNED);
803 DPRINTF("tp=%p\n", tp);
805 if (ttydisc_can_bypass(tp) != 0 ||
806 (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
807 (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
811 /* prevent recursion */
812 sc->sc_flag |= UCOM_FLAG_INWAKEUP;
814 pos = sc->sc_jitterbuf_out;
816 while (sc->sc_jitterbuf_in != pos) {
819 c = (char)sc->sc_jitterbuf[pos];
821 if (ttydisc_rint(tp, c, 0) == -1)
824 if (pos >= UCOM_JITTERBUF_SIZE)
825 pos -= UCOM_JITTERBUF_SIZE;
828 sc->sc_jitterbuf_out = pos;
830 /* clear RTS in async fashion */
831 if ((sc->sc_jitterbuf_in == pos) &&
832 (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
835 sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
839 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
841 struct ucom_softc *sc = tty_softc(tp);
844 UCOM_MTX_ASSERT(sc, MA_OWNED);
846 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
849 DPRINTF("cmd = 0x%08lx\n", cmd);
871 if (sc->sc_callback->ucom_ioctl) {
872 error = (sc->sc_callback->ucom_ioctl)
873 (sc, cmd, data, 0, td);
877 if (error == ENOIOCTL)
878 error = pps_ioctl(cmd, data, &sc->sc_pps);
885 ucom_modem(struct tty *tp, int sigon, int sigoff)
887 struct ucom_softc *sc = tty_softc(tp);
890 UCOM_MTX_ASSERT(sc, MA_OWNED);
892 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
895 if ((sigon == 0) && (sigoff == 0)) {
897 if (sc->sc_mcr & SER_DTR) {
900 if (sc->sc_mcr & SER_RTS) {
903 if (sc->sc_msr & SER_CTS) {
906 if (sc->sc_msr & SER_DCD) {
909 if (sc->sc_msr & SER_DSR) {
912 if (sc->sc_msr & SER_RI) {
917 if (sigon & SER_DTR) {
918 sc->sc_mcr |= SER_DTR;
920 if (sigoff & SER_DTR) {
921 sc->sc_mcr &= ~SER_DTR;
923 if (sigon & SER_RTS) {
924 sc->sc_mcr |= SER_RTS;
926 if (sigoff & SER_RTS) {
927 sc->sc_mcr &= ~SER_RTS;
929 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
932 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
939 ucom_cfg_line_state(struct usb_proc_msg *_task)
941 struct ucom_cfg_task *task =
942 (struct ucom_cfg_task *)_task;
943 struct ucom_softc *sc = task->sc;
950 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
955 /* compute callback mask */
956 if (sc->sc_callback->ucom_cfg_set_dtr)
958 if (sc->sc_callback->ucom_cfg_set_rts)
960 if (sc->sc_callback->ucom_cfg_set_break)
961 mask |= UCOM_LS_BREAK;
962 if (sc->sc_callback->ucom_cfg_set_ring)
963 mask |= UCOM_LS_RING;
965 /* compute the bits we are to program */
966 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
967 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
968 prev_value = sc->sc_pls_curr ^ notch_bits;
969 last_value = sc->sc_pls_curr;
971 /* reset programmed line state */
976 /* ensure that we don't lose any levels */
977 if (notch_bits & UCOM_LS_DTR)
978 sc->sc_callback->ucom_cfg_set_dtr(sc,
979 (prev_value & UCOM_LS_DTR) ? 1 : 0);
980 if (notch_bits & UCOM_LS_RTS)
981 sc->sc_callback->ucom_cfg_set_rts(sc,
982 (prev_value & UCOM_LS_RTS) ? 1 : 0);
983 if (notch_bits & UCOM_LS_BREAK)
984 sc->sc_callback->ucom_cfg_set_break(sc,
985 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
986 if (notch_bits & UCOM_LS_RING)
987 sc->sc_callback->ucom_cfg_set_ring(sc,
988 (prev_value & UCOM_LS_RING) ? 1 : 0);
991 if (any_bits & UCOM_LS_DTR)
992 sc->sc_callback->ucom_cfg_set_dtr(sc,
993 (last_value & UCOM_LS_DTR) ? 1 : 0);
994 if (any_bits & UCOM_LS_RTS)
995 sc->sc_callback->ucom_cfg_set_rts(sc,
996 (last_value & UCOM_LS_RTS) ? 1 : 0);
997 if (any_bits & UCOM_LS_BREAK)
998 sc->sc_callback->ucom_cfg_set_break(sc,
999 (last_value & UCOM_LS_BREAK) ? 1 : 0);
1000 if (any_bits & UCOM_LS_RING)
1001 sc->sc_callback->ucom_cfg_set_ring(sc,
1002 (last_value & UCOM_LS_RING) ? 1 : 0);
1006 ucom_line_state(struct ucom_softc *sc,
1007 uint8_t set_bits, uint8_t clear_bits)
1009 UCOM_MTX_ASSERT(sc, MA_OWNED);
1011 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1015 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1017 /* update current programmed line state */
1018 sc->sc_pls_curr |= set_bits;
1019 sc->sc_pls_curr &= ~clear_bits;
1020 sc->sc_pls_set |= set_bits;
1021 sc->sc_pls_clr |= clear_bits;
1023 /* defer driver programming */
1024 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1025 &sc->sc_line_state_task[0].hdr,
1026 &sc->sc_line_state_task[1].hdr);
1030 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1032 DPRINTF("onoff = %d\n", onoff);
1035 ucom_line_state(sc, UCOM_LS_RING, 0);
1037 ucom_line_state(sc, 0, UCOM_LS_RING);
1041 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1043 DPRINTF("onoff = %d\n", onoff);
1046 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1048 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1052 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1054 DPRINTF("onoff = %d\n", onoff);
1057 ucom_line_state(sc, UCOM_LS_DTR, 0);
1059 ucom_line_state(sc, 0, UCOM_LS_DTR);
1063 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1065 DPRINTF("onoff = %d\n", onoff);
1068 ucom_line_state(sc, UCOM_LS_RTS, 0);
1070 ucom_line_state(sc, 0, UCOM_LS_RTS);
1074 ucom_cfg_status_change(struct usb_proc_msg *_task)
1076 struct ucom_cfg_task *task =
1077 (struct ucom_cfg_task *)_task;
1078 struct ucom_softc *sc = task->sc;
1089 UCOM_MTX_ASSERT(sc, MA_OWNED);
1091 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1094 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1102 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1104 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1105 /* TTY device closed */
1108 msr_delta = (sc->sc_msr ^ new_msr);
1109 lsr_delta = (sc->sc_lsr ^ new_lsr);
1111 sc->sc_msr = new_msr;
1112 sc->sc_lsr = new_lsr;
1115 * Time pulse counting support.
1117 switch(ucom_pps_mode & UART_PPS_SIGNAL_MASK) {
1119 pps_signal = SER_CTS;
1122 pps_signal = SER_DCD;
1129 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1130 (msr_delta & pps_signal)) {
1131 pps_capture(&sc->sc_pps);
1132 onoff = (sc->sc_msr & pps_signal) ? 1 : 0;
1133 if (ucom_pps_mode & UART_PPS_INVERT_PULSE)
1135 pps_event(&sc->sc_pps, onoff ? PPS_CAPTUREASSERT :
1139 if (msr_delta & SER_DCD) {
1141 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1143 DPRINTF("DCD changed to %d\n", onoff);
1145 ttydisc_modem(tp, onoff);
1148 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1150 DPRINTF("BREAK detected\n");
1152 ttydisc_rint(tp, 0, TRE_BREAK);
1153 ttydisc_rint_done(tp);
1156 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1158 DPRINTF("Frame error detected\n");
1160 ttydisc_rint(tp, 0, TRE_FRAMING);
1161 ttydisc_rint_done(tp);
1164 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1166 DPRINTF("Parity error detected\n");
1168 ttydisc_rint(tp, 0, TRE_PARITY);
1169 ttydisc_rint_done(tp);
1174 ucom_status_change(struct ucom_softc *sc)
1176 UCOM_MTX_ASSERT(sc, MA_OWNED);
1178 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1179 return; /* not supported */
1181 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1186 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1187 &sc->sc_status_task[0].hdr,
1188 &sc->sc_status_task[1].hdr);
1192 ucom_cfg_param(struct usb_proc_msg *_task)
1194 struct ucom_param_task *task =
1195 (struct ucom_param_task *)_task;
1196 struct ucom_softc *sc = task->sc;
1198 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1201 if (sc->sc_callback->ucom_cfg_param == NULL) {
1205 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1208 usb_pause_mtx(sc->sc_mtx, hz / 10);
1212 ucom_param(struct tty *tp, struct termios *t)
1214 struct ucom_softc *sc = tty_softc(tp);
1218 UCOM_MTX_ASSERT(sc, MA_OWNED);
1223 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1225 /* XXX the TTY layer should call "open()" first! */
1227 * Not quite: Its ordering is partly backwards, but
1228 * some parameters must be set early in ttydev_open(),
1229 * possibly before calling ttydevsw_open().
1231 error = ucom_open(tp);
1237 DPRINTF("sc = %p\n", sc);
1239 /* Check requested parameters. */
1240 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1241 /* XXX c_ospeed == 0 is perfectly valid. */
1242 DPRINTF("mismatch ispeed and ospeed\n");
1246 t->c_ispeed = t->c_ospeed;
1248 if (sc->sc_callback->ucom_pre_param) {
1249 /* Let the lower layer verify the parameters */
1250 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1252 DPRINTF("callback error = %d\n", error);
1257 /* Disable transfers */
1258 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1260 /* Queue baud rate programming command first */
1261 ucom_queue_command(sc, ucom_cfg_param, t,
1262 &sc->sc_param_task[0].hdr,
1263 &sc->sc_param_task[1].hdr);
1265 /* Queue transfer enable command last */
1266 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1267 &sc->sc_start_task[0].hdr,
1268 &sc->sc_start_task[1].hdr);
1270 if (t->c_cflag & CRTS_IFLOW) {
1271 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1272 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1273 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1274 ucom_modem(tp, SER_RTS, 0);
1286 ucom_outwakeup(struct tty *tp)
1288 struct ucom_softc *sc = tty_softc(tp);
1290 UCOM_MTX_ASSERT(sc, MA_OWNED);
1292 DPRINTF("sc = %p\n", sc);
1294 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1295 /* The higher layer is not ready */
1298 ucom_start_transfers(sc);
1301 /*------------------------------------------------------------------------*
1305 * 0: No data is available.
1306 * Else: Data is available.
1307 *------------------------------------------------------------------------*/
1309 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1310 uint32_t offset, uint32_t len, uint32_t *actlen)
1312 struct usb_page_search res;
1313 struct tty *tp = sc->sc_tty;
1315 uint32_t offset_orig;
1317 UCOM_MTX_ASSERT(sc, MA_OWNED);
1319 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1322 /* get total TX length */
1324 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1325 temp %= UCOM_CONS_BUFSIZE;
1327 /* limit TX length */
1329 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1330 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1337 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1339 /* update counters */
1341 ucom_cons_tx_low += temp;
1342 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1344 /* store actual length */
1348 return (temp ? 1 : 0);
1352 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1354 return (0); /* multiport device polling */
1356 offset_orig = offset;
1360 usbd_get_page(pc, offset, &res);
1362 if (res.length > len) {
1365 /* copy data directly into USB buffer */
1366 cnt = ttydisc_getc(tp, res.buffer, res.length);
1371 if (cnt < res.length) {
1377 actlen[0] = offset - offset_orig;
1379 DPRINTF("cnt=%d\n", actlen[0]);
1381 if (actlen[0] == 0) {
1388 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1389 uint32_t offset, uint32_t len)
1391 struct usb_page_search res;
1392 struct tty *tp = sc->sc_tty;
1396 UCOM_MTX_ASSERT(sc, MA_OWNED);
1398 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1401 /* get maximum RX length */
1403 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1404 temp %= UCOM_CONS_BUFSIZE;
1406 /* limit RX length */
1408 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1409 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1416 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1418 /* update counters */
1420 ucom_cons_rx_high += temp;
1421 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1427 return; /* multiport device polling */
1430 return; /* no data */
1432 /* set a flag to prevent recursation ? */
1436 usbd_get_page(pc, offset, &res);
1438 if (res.length > len) {
1442 offset += res.length;
1444 /* pass characters to tty layer */
1449 /* first check if we can pass the buffer directly */
1451 if (ttydisc_can_bypass(tp)) {
1453 /* clear any jitter buffer */
1454 sc->sc_jitterbuf_in = 0;
1455 sc->sc_jitterbuf_out = 0;
1457 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1458 DPRINTF("tp=%p, data lost\n", tp);
1464 for (cnt = 0; cnt != res.length; cnt++) {
1465 if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1466 ttydisc_rint(tp, buf[cnt], 0) == -1) {
1470 pos = sc->sc_jitterbuf_in;
1471 end = sc->sc_jitterbuf_out +
1472 UCOM_JITTERBUF_SIZE - 1;
1473 if (end >= UCOM_JITTERBUF_SIZE)
1474 end -= UCOM_JITTERBUF_SIZE;
1476 for (; cnt != res.length; cnt++) {
1479 sc->sc_jitterbuf[pos] = buf[cnt];
1481 if (pos >= UCOM_JITTERBUF_SIZE)
1482 pos -= UCOM_JITTERBUF_SIZE;
1485 sc->sc_jitterbuf_in = pos;
1487 /* set RTS in async fashion */
1488 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1491 DPRINTF("tp=%p, lost %d "
1492 "chars\n", tp, res.length - cnt);
1497 ttydisc_rint_done(tp);
1501 ucom_free(void *xsc)
1503 struct ucom_softc *sc = xsc;
1505 if (sc->sc_callback->ucom_free != NULL)
1506 sc->sc_callback->ucom_free(sc);
1508 ucom_unref(sc->sc_super);
1510 mtx_lock(&ucom_mtx);
1512 mtx_unlock(&ucom_mtx);
1515 static cn_probe_t ucom_cnprobe;
1516 static cn_init_t ucom_cninit;
1517 static cn_term_t ucom_cnterm;
1518 static cn_getc_t ucom_cngetc;
1519 static cn_putc_t ucom_cnputc;
1520 static cn_grab_t ucom_cngrab;
1521 static cn_ungrab_t ucom_cnungrab;
1523 CONSOLE_DRIVER(ucom);
1526 ucom_cnprobe(struct consdev *cp)
1528 if (ucom_cons_unit != -1)
1529 cp->cn_pri = CN_NORMAL;
1531 cp->cn_pri = CN_DEAD;
1533 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1537 ucom_cninit(struct consdev *cp)
1542 ucom_cnterm(struct consdev *cp)
1547 ucom_cngrab(struct consdev *cp)
1552 ucom_cnungrab(struct consdev *cp)
1557 ucom_cngetc(struct consdev *cd)
1559 struct ucom_softc *sc = ucom_cons_softc;
1567 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1568 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1569 ucom_cons_rx_low ++;
1570 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1575 /* start USB transfers */
1576 ucom_outwakeup(sc->sc_tty);
1578 UCOM_MTX_UNLOCK(sc);
1580 /* poll if necessary */
1581 if (kdb_active && sc->sc_callback->ucom_poll)
1582 (sc->sc_callback->ucom_poll) (sc);
1588 ucom_cnputc(struct consdev *cd, int c)
1590 struct ucom_softc *sc = ucom_cons_softc;
1600 /* compute maximum TX length */
1602 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1603 temp %= UCOM_CONS_BUFSIZE;
1606 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1607 ucom_cons_tx_high ++;
1608 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1611 /* start USB transfers */
1612 ucom_outwakeup(sc->sc_tty);
1614 UCOM_MTX_UNLOCK(sc);
1616 /* poll if necessary */
1617 if (kdb_active && sc->sc_callback->ucom_poll) {
1618 (sc->sc_callback->ucom_poll) (sc);
1619 /* simple flow control */
1625 /*------------------------------------------------------------------------*
1628 * This function will increment the super UCOM reference count.
1629 *------------------------------------------------------------------------*/
1631 ucom_ref(struct ucom_super_softc *ssc)
1633 mtx_lock(&ucom_mtx);
1635 mtx_unlock(&ucom_mtx);
1638 /*------------------------------------------------------------------------*
1641 * This function will free the super UCOM's allocated unit
1642 * number. This function can be called on a zero-initialized
1643 * structure. This function can be called multiple times.
1644 *------------------------------------------------------------------------*/
1646 ucom_free_unit(struct ucom_super_softc *ssc)
1648 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1651 ucom_unit_free(ssc->sc_unit);
1653 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1656 /*------------------------------------------------------------------------*
1659 * This function will decrement the super UCOM reference count.
1662 * 0: UCOM structures are still referenced.
1663 * Else: UCOM structures are no longer referenced.
1664 *------------------------------------------------------------------------*/
1666 ucom_unref(struct ucom_super_softc *ssc)
1670 mtx_lock(&ucom_mtx);
1671 retval = (ssc->sc_refs < 2);
1673 mtx_unlock(&ucom_mtx);
1676 ucom_free_unit(ssc);
1683 #include <gdb/gdb.h>
1685 static gdb_probe_f ucom_gdbprobe;
1686 static gdb_init_f ucom_gdbinit;
1687 static gdb_term_f ucom_gdbterm;
1688 static gdb_getc_f ucom_gdbgetc;
1689 static gdb_putc_f ucom_gdbputc;
1691 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1696 return ((ucom_cons_softc != NULL) ? 0 : -1);
1712 ucom_cnputc(NULL, c);
1718 return (ucom_cngetc(NULL));