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.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the NetBSD
52 * Foundation, Inc. and its contributors.
53 * 4. Neither the name of The NetBSD Foundation nor the names of its
54 * contributors may be used to endorse or promote products derived
55 * from this software without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
58 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
61 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 * POSSIBILITY OF SUCH DAMAGE.
70 #include <sys/stdint.h>
71 #include <sys/stddef.h>
72 #include <sys/param.h>
73 #include <sys/queue.h>
74 #include <sys/types.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
78 #include <sys/linker_set.h>
79 #include <sys/module.h>
81 #include <sys/mutex.h>
82 #include <sys/condvar.h>
83 #include <sys/sysctl.h>
85 #include <sys/unistd.h>
86 #include <sys/callout.h>
87 #include <sys/malloc.h>
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdi_util.h>
96 #define USB_DEBUG_VAR ucom_debug
97 #include <dev/usb/usb_debug.h>
98 #include <dev/usb/usb_busdma.h>
99 #include <dev/usb/usb_process.h>
101 #include <dev/usb/serial/usb_serial.h>
105 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
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_baud = 9600;
127 static struct ucom_softc *ucom_cons_softc = NULL;
129 TUNABLE_INT("hw.usb.ucom.cons_unit", &ucom_cons_unit);
130 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_unit, CTLFLAG_RW,
131 &ucom_cons_unit, 0, "console unit number");
133 TUNABLE_INT("hw.usb.ucom.cons_baud", &ucom_cons_baud);
134 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RW,
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 uint8_t ucom_units_alloc(uint32_t, uint32_t *);
145 static void ucom_units_free(uint32_t, uint32_t);
146 static int ucom_attach_tty(struct ucom_softc *, uint32_t);
147 static void ucom_detach_tty(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_free_t ucom_free;
165 static struct ttydevsw ucom_class = {
166 .tsw_flags = TF_INITLOCK | TF_CALLOUT,
167 .tsw_open = ucom_open,
168 .tsw_close = ucom_close,
169 .tsw_outwakeup = ucom_outwakeup,
170 .tsw_ioctl = ucom_ioctl,
171 .tsw_param = ucom_param,
172 .tsw_modem = ucom_modem,
173 .tsw_free = ucom_free,
176 MODULE_DEPEND(ucom, usb, 1, 1, 1);
177 MODULE_VERSION(ucom, 1);
179 #define UCOM_UNIT_MAX 0x200 /* exclusive */
180 #define UCOM_SUB_UNIT_MAX 0x100 /* exclusive */
182 static uint8_t ucom_bitmap[(UCOM_UNIT_MAX + 7) / 8];
183 static struct mtx ucom_bitmap_mtx;
184 MTX_SYSINIT(ucom_bitmap_mtx, &ucom_bitmap_mtx, "ucom bitmap", MTX_DEF);
187 ucom_units_alloc(uint32_t sub_units, uint32_t *p_root_unit)
192 uint32_t max = UCOM_UNIT_MAX - (UCOM_UNIT_MAX % sub_units);
195 mtx_lock(&ucom_bitmap_mtx);
197 for (n = 0; n < max; n += sub_units) {
199 /* check for free consecutive bits */
201 for (o = 0; o < sub_units; o++) {
205 if (ucom_bitmap[x / 8] & (1 << (x % 8))) {
212 for (o = 0; o < sub_units; o++) {
216 ucom_bitmap[x / 8] |= (1 << (x % 8));
226 mtx_unlock(&ucom_bitmap_mtx);
229 * Always set the variable pointed to by "p_root_unit" so that
230 * the compiler does not think that it is used uninitialised:
238 ucom_units_free(uint32_t root_unit, uint32_t sub_units)
242 mtx_lock(&ucom_bitmap_mtx);
244 while (sub_units--) {
245 x = root_unit + sub_units;
246 ucom_bitmap[x / 8] &= ~(1 << (x % 8));
249 mtx_unlock(&ucom_bitmap_mtx);
253 * "N" sub_units are setup at a time. All sub-units will
254 * be given sequential unit numbers. The number of
255 * sub-units can be used to differentiate among
256 * different types of devices.
258 * The mutex pointed to by "mtx" is applied before all
259 * callbacks are called back. Also "mtx" must be applied
260 * before calling into the ucom-layer!
263 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
264 uint32_t sub_units, void *parent,
265 const struct ucom_callback *callback, struct mtx *mtx)
273 (sub_units > UCOM_SUB_UNIT_MAX) ||
274 (callback == NULL)) {
278 /* XXX unit management does not really belong here */
279 if (ucom_units_alloc(sub_units, &root_unit)) {
283 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
285 ucom_units_free(root_unit, sub_units);
289 for (n = 0; n != sub_units; n++, sc++) {
290 sc->sc_unit = root_unit + n;
291 sc->sc_local_unit = n;
294 sc->sc_parent = parent;
295 sc->sc_callback = callback;
297 error = ucom_attach_tty(sc, sub_units);
299 ucom_detach(ssc, sc - n, n);
300 ucom_units_free(root_unit + n, sub_units - n);
303 sc->sc_flag |= UCOM_FLAG_ATTACHED;
309 * NOTE: the following function will do nothing if
310 * the structure pointed to by "ssc" and "sc" is zero.
313 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
318 usb_proc_drain(&ssc->sc_tq);
320 for (n = 0; n != sub_units; n++, sc++) {
321 if (sc->sc_flag & UCOM_FLAG_ATTACHED) {
325 ucom_units_free(sc->sc_unit, 1);
327 /* avoid duplicate detach: */
328 sc->sc_flag &= ~UCOM_FLAG_ATTACHED;
331 usb_proc_free(&ssc->sc_tq);
335 ucom_attach_tty(struct ucom_softc *sc, uint32_t sub_units)
339 char buf[32]; /* temporary TTY device name buffer */
341 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
346 DPRINTF("tp = %p, unit = %d\n", tp, sc->sc_unit);
348 buf[0] = 0; /* set some default value */
350 /* Check if the client has a custom TTY name */
351 if (sc->sc_callback->ucom_tty_name) {
352 sc->sc_callback->ucom_tty_name(sc, buf,
353 sizeof(buf), sc->sc_local_unit);
356 /* Use default TTY name */
358 /* multiple modems in one */
359 if (snprintf(buf, sizeof(buf), "U%u.%u",
360 sc->sc_unit - sc->sc_local_unit,
361 sc->sc_local_unit)) {
366 if (snprintf(buf, sizeof(buf), "U%u", sc->sc_unit)) {
371 tty_makedev(tp, NULL, "%s", buf);
375 DPRINTF("ttycreate: %s\n", buf);
376 cv_init(&sc->sc_cv, "ucom");
378 /* Check if this device should be a console */
379 if ((ucom_cons_softc == NULL) &&
380 (sc->sc_unit == ucom_cons_unit)) {
384 ucom_cons_softc = sc;
386 memset(&t, 0, sizeof(t));
387 t.c_ispeed = ucom_cons_baud;
388 t.c_ospeed = t.c_ispeed;
391 mtx_lock(ucom_cons_softc->sc_mtx);
392 ucom_cons_rx_low = 0;
393 ucom_cons_rx_high = 0;
394 ucom_cons_tx_low = 0;
395 ucom_cons_tx_high = 0;
396 sc->sc_flag |= UCOM_FLAG_CONSOLE;
397 ucom_open(ucom_cons_softc->sc_tty);
398 ucom_param(ucom_cons_softc->sc_tty, &t);
399 mtx_unlock(ucom_cons_softc->sc_mtx);
406 ucom_detach_tty(struct ucom_softc *sc)
408 struct tty *tp = sc->sc_tty;
410 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
412 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
413 mtx_lock(ucom_cons_softc->sc_mtx);
414 ucom_close(ucom_cons_softc->sc_tty);
415 mtx_unlock(ucom_cons_softc->sc_mtx);
416 ucom_cons_softc = NULL;
419 /* the config thread has been stopped when we get here */
421 mtx_lock(sc->sc_mtx);
422 sc->sc_flag |= UCOM_FLAG_GONE;
423 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
424 mtx_unlock(sc->sc_mtx);
428 ucom_close(tp); /* close, if any */
432 mtx_lock(sc->sc_mtx);
433 /* Wait for the callback after the TTY is torn down */
434 while (sc->sc_ttyfreed == 0)
435 cv_wait(&sc->sc_cv, sc->sc_mtx);
437 * make sure that read and write transfers are stopped
439 if (sc->sc_callback->ucom_stop_read) {
440 (sc->sc_callback->ucom_stop_read) (sc);
442 if (sc->sc_callback->ucom_stop_write) {
443 (sc->sc_callback->ucom_stop_write) (sc);
445 mtx_unlock(sc->sc_mtx);
447 cv_destroy(&sc->sc_cv);
451 ucom_queue_command(struct ucom_softc *sc,
452 usb_proc_callback_t *fn, struct termios *pt,
453 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
455 struct ucom_super_softc *ssc = sc->sc_super;
456 struct ucom_param_task *task;
458 mtx_assert(sc->sc_mtx, MA_OWNED);
460 if (usb_proc_is_gone(&ssc->sc_tq)) {
461 DPRINTF("proc is gone\n");
462 return; /* nothing to do */
465 * NOTE: The task cannot get executed before we drop the
466 * "sc_mtx" mutex. It is safe to update fields in the message
467 * structure after that the message got queued.
469 task = (struct ucom_param_task *)
470 usb_proc_msignal(&ssc->sc_tq, t0, t1);
472 /* Setup callback and softc pointers */
473 task->hdr.pm_callback = fn;
477 * Make a copy of the termios. This field is only present if
478 * the "pt" field is not NULL.
481 task->termios_copy = *pt;
484 * Closing the device should be synchronous.
486 if (fn == ucom_cfg_close)
487 usb_proc_mwait(&ssc->sc_tq, t0, t1);
490 * In case of multiple configure requests,
491 * keep track of the last one!
493 if (fn == ucom_cfg_start_transfers)
494 sc->sc_last_start_xfer = &task->hdr;
498 ucom_shutdown(struct ucom_softc *sc)
500 struct tty *tp = sc->sc_tty;
502 mtx_assert(sc->sc_mtx, MA_OWNED);
507 * Hang up if necessary:
509 if (tp->t_termios.c_cflag & HUPCL) {
510 ucom_modem(tp, 0, SER_DTR);
517 * else: taskqueue is draining or gone
520 ucom_cfg_is_gone(struct ucom_softc *sc)
522 struct ucom_super_softc *ssc = sc->sc_super;
524 return (usb_proc_is_gone(&ssc->sc_tq));
528 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
530 struct ucom_cfg_task *task =
531 (struct ucom_cfg_task *)_task;
532 struct ucom_softc *sc = task->sc;
534 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
537 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
538 /* TTY device closed */
542 if (_task == sc->sc_last_start_xfer)
543 sc->sc_flag |= UCOM_FLAG_GP_DATA;
545 if (sc->sc_callback->ucom_start_read) {
546 (sc->sc_callback->ucom_start_read) (sc);
548 if (sc->sc_callback->ucom_start_write) {
549 (sc->sc_callback->ucom_start_write) (sc);
554 ucom_start_transfers(struct ucom_softc *sc)
556 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
560 * Make sure that data transfers are started in both
563 if (sc->sc_callback->ucom_start_read) {
564 (sc->sc_callback->ucom_start_read) (sc);
566 if (sc->sc_callback->ucom_start_write) {
567 (sc->sc_callback->ucom_start_write) (sc);
572 ucom_cfg_open(struct usb_proc_msg *_task)
574 struct ucom_cfg_task *task =
575 (struct ucom_cfg_task *)_task;
576 struct ucom_softc *sc = task->sc;
580 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
586 sc->sc_flag |= UCOM_FLAG_LL_READY;
588 if (sc->sc_callback->ucom_cfg_open) {
589 (sc->sc_callback->ucom_cfg_open) (sc);
592 usb_pause_mtx(sc->sc_mtx, hz / 10);
598 ucom_open(struct tty *tp)
600 struct ucom_softc *sc = tty_softc(tp);
603 mtx_assert(sc->sc_mtx, MA_OWNED);
605 if (sc->sc_flag & UCOM_FLAG_GONE) {
608 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
612 DPRINTF("tp = %p\n", tp);
614 if (sc->sc_callback->ucom_pre_open) {
616 * give the lower layer a chance to disallow TTY open, for
617 * example if the device is not present:
619 error = (sc->sc_callback->ucom_pre_open) (sc);
624 sc->sc_flag |= UCOM_FLAG_HL_READY;
626 /* Disable transfers */
627 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
633 /* reset programmed line state */
638 ucom_queue_command(sc, ucom_cfg_open, NULL,
639 &sc->sc_open_task[0].hdr,
640 &sc->sc_open_task[1].hdr);
642 /* Queue transfer enable command last */
643 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
644 &sc->sc_start_task[0].hdr,
645 &sc->sc_start_task[1].hdr);
647 ucom_modem(tp, SER_DTR | SER_RTS, 0);
653 ucom_status_change(sc);
659 ucom_cfg_close(struct usb_proc_msg *_task)
661 struct ucom_cfg_task *task =
662 (struct ucom_cfg_task *)_task;
663 struct ucom_softc *sc = task->sc;
667 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
668 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
669 if (sc->sc_callback->ucom_cfg_close)
670 (sc->sc_callback->ucom_cfg_close) (sc);
677 ucom_close(struct tty *tp)
679 struct ucom_softc *sc = tty_softc(tp);
681 mtx_assert(sc->sc_mtx, MA_OWNED);
683 DPRINTF("tp=%p\n", tp);
685 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
686 DPRINTF("tp=%p already closed\n", tp);
691 ucom_queue_command(sc, ucom_cfg_close, NULL,
692 &sc->sc_close_task[0].hdr,
693 &sc->sc_close_task[1].hdr);
695 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
697 if (sc->sc_callback->ucom_stop_read) {
698 (sc->sc_callback->ucom_stop_read) (sc);
703 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
705 struct ucom_softc *sc = tty_softc(tp);
708 mtx_assert(sc->sc_mtx, MA_OWNED);
710 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
713 DPRINTF("cmd = 0x%08lx\n", cmd);
735 if (sc->sc_callback->ucom_ioctl) {
736 error = (sc->sc_callback->ucom_ioctl)
737 (sc, cmd, data, 0, td);
747 ucom_modem(struct tty *tp, int sigon, int sigoff)
749 struct ucom_softc *sc = tty_softc(tp);
752 mtx_assert(sc->sc_mtx, MA_OWNED);
754 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
757 if ((sigon == 0) && (sigoff == 0)) {
759 if (sc->sc_mcr & SER_DTR) {
762 if (sc->sc_mcr & SER_RTS) {
765 if (sc->sc_msr & SER_CTS) {
768 if (sc->sc_msr & SER_DCD) {
771 if (sc->sc_msr & SER_DSR) {
774 if (sc->sc_msr & SER_RI) {
779 if (sigon & SER_DTR) {
780 sc->sc_mcr |= SER_DTR;
782 if (sigoff & SER_DTR) {
783 sc->sc_mcr &= ~SER_DTR;
785 if (sigon & SER_RTS) {
786 sc->sc_mcr |= SER_RTS;
788 if (sigoff & SER_RTS) {
789 sc->sc_mcr &= ~SER_RTS;
791 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
794 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
801 ucom_cfg_line_state(struct usb_proc_msg *_task)
803 struct ucom_cfg_task *task =
804 (struct ucom_cfg_task *)_task;
805 struct ucom_softc *sc = task->sc;
812 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
817 /* compute callback mask */
818 if (sc->sc_callback->ucom_cfg_set_dtr)
820 if (sc->sc_callback->ucom_cfg_set_rts)
822 if (sc->sc_callback->ucom_cfg_set_break)
823 mask |= UCOM_LS_BREAK;
824 if (sc->sc_callback->ucom_cfg_set_ring)
825 mask |= UCOM_LS_RING;
827 /* compute the bits we are to program */
828 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
829 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
830 prev_value = sc->sc_pls_curr ^ notch_bits;
831 last_value = sc->sc_pls_curr;
833 /* reset programmed line state */
838 /* ensure that we don't loose any levels */
839 if (notch_bits & UCOM_LS_DTR)
840 sc->sc_callback->ucom_cfg_set_dtr(sc,
841 (prev_value & UCOM_LS_DTR) ? 1 : 0);
842 if (notch_bits & UCOM_LS_RTS)
843 sc->sc_callback->ucom_cfg_set_rts(sc,
844 (prev_value & UCOM_LS_RTS) ? 1 : 0);
845 if (notch_bits & UCOM_LS_BREAK)
846 sc->sc_callback->ucom_cfg_set_break(sc,
847 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
848 if (notch_bits & UCOM_LS_RING)
849 sc->sc_callback->ucom_cfg_set_ring(sc,
850 (prev_value & UCOM_LS_RING) ? 1 : 0);
853 if (any_bits & UCOM_LS_DTR)
854 sc->sc_callback->ucom_cfg_set_dtr(sc,
855 (last_value & UCOM_LS_DTR) ? 1 : 0);
856 if (any_bits & UCOM_LS_RTS)
857 sc->sc_callback->ucom_cfg_set_rts(sc,
858 (last_value & UCOM_LS_RTS) ? 1 : 0);
859 if (any_bits & UCOM_LS_BREAK)
860 sc->sc_callback->ucom_cfg_set_break(sc,
861 (last_value & UCOM_LS_BREAK) ? 1 : 0);
862 if (any_bits & UCOM_LS_RING)
863 sc->sc_callback->ucom_cfg_set_ring(sc,
864 (last_value & UCOM_LS_RING) ? 1 : 0);
868 ucom_line_state(struct ucom_softc *sc,
869 uint8_t set_bits, uint8_t clear_bits)
871 mtx_assert(sc->sc_mtx, MA_OWNED);
873 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
877 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
879 /* update current programmed line state */
880 sc->sc_pls_curr |= set_bits;
881 sc->sc_pls_curr &= ~clear_bits;
882 sc->sc_pls_set |= set_bits;
883 sc->sc_pls_clr |= clear_bits;
885 /* defer driver programming */
886 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
887 &sc->sc_line_state_task[0].hdr,
888 &sc->sc_line_state_task[1].hdr);
892 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
894 DPRINTF("onoff = %d\n", onoff);
897 ucom_line_state(sc, UCOM_LS_RING, 0);
899 ucom_line_state(sc, 0, UCOM_LS_RING);
903 ucom_break(struct ucom_softc *sc, uint8_t onoff)
905 DPRINTF("onoff = %d\n", onoff);
908 ucom_line_state(sc, UCOM_LS_BREAK, 0);
910 ucom_line_state(sc, 0, UCOM_LS_BREAK);
914 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
916 DPRINTF("onoff = %d\n", onoff);
919 ucom_line_state(sc, UCOM_LS_DTR, 0);
921 ucom_line_state(sc, 0, UCOM_LS_DTR);
925 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
927 DPRINTF("onoff = %d\n", onoff);
930 ucom_line_state(sc, UCOM_LS_RTS, 0);
932 ucom_line_state(sc, 0, UCOM_LS_RTS);
936 ucom_cfg_status_change(struct usb_proc_msg *_task)
938 struct ucom_cfg_task *task =
939 (struct ucom_cfg_task *)_task;
940 struct ucom_softc *sc = task->sc;
949 mtx_assert(sc->sc_mtx, MA_OWNED);
951 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
954 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
962 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
964 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
965 /* TTY device closed */
968 onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
969 lsr_delta = (sc->sc_lsr ^ new_lsr);
971 sc->sc_msr = new_msr;
972 sc->sc_lsr = new_lsr;
976 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
978 DPRINTF("DCD changed to %d\n", onoff);
980 ttydisc_modem(tp, onoff);
983 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
985 DPRINTF("BREAK detected\n");
987 ttydisc_rint(tp, 0, TRE_BREAK);
988 ttydisc_rint_done(tp);
991 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
993 DPRINTF("Frame error detected\n");
995 ttydisc_rint(tp, 0, TRE_FRAMING);
996 ttydisc_rint_done(tp);
999 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1001 DPRINTF("Parity error detected\n");
1003 ttydisc_rint(tp, 0, TRE_PARITY);
1004 ttydisc_rint_done(tp);
1009 ucom_status_change(struct ucom_softc *sc)
1011 mtx_assert(sc->sc_mtx, MA_OWNED);
1013 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1014 return; /* not supported */
1016 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1021 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1022 &sc->sc_status_task[0].hdr,
1023 &sc->sc_status_task[1].hdr);
1027 ucom_cfg_param(struct usb_proc_msg *_task)
1029 struct ucom_param_task *task =
1030 (struct ucom_param_task *)_task;
1031 struct ucom_softc *sc = task->sc;
1033 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1036 if (sc->sc_callback->ucom_cfg_param == NULL) {
1040 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1043 usb_pause_mtx(sc->sc_mtx, hz / 10);
1047 ucom_param(struct tty *tp, struct termios *t)
1049 struct ucom_softc *sc = tty_softc(tp);
1053 mtx_assert(sc->sc_mtx, MA_OWNED);
1058 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1060 /* XXX the TTY layer should call "open()" first! */
1062 error = ucom_open(tp);
1068 DPRINTF("sc = %p\n", sc);
1070 /* Check requested parameters. */
1071 if (t->c_ospeed < 0) {
1072 DPRINTF("negative ospeed\n");
1076 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1077 DPRINTF("mismatch ispeed and ospeed\n");
1081 t->c_ispeed = t->c_ospeed;
1083 if (sc->sc_callback->ucom_pre_param) {
1084 /* Let the lower layer verify the parameters */
1085 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1087 DPRINTF("callback error = %d\n", error);
1092 /* Disable transfers */
1093 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1095 /* Queue baud rate programming command first */
1096 ucom_queue_command(sc, ucom_cfg_param, t,
1097 &sc->sc_param_task[0].hdr,
1098 &sc->sc_param_task[1].hdr);
1100 /* Queue transfer enable command last */
1101 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1102 &sc->sc_start_task[0].hdr,
1103 &sc->sc_start_task[1].hdr);
1105 if (t->c_cflag & CRTS_IFLOW) {
1106 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1107 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1108 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1109 ucom_modem(tp, SER_RTS, 0);
1121 ucom_outwakeup(struct tty *tp)
1123 struct ucom_softc *sc = tty_softc(tp);
1125 mtx_assert(sc->sc_mtx, MA_OWNED);
1127 DPRINTF("sc = %p\n", sc);
1129 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1130 /* The higher layer is not ready */
1133 ucom_start_transfers(sc);
1136 /*------------------------------------------------------------------------*
1140 * 0: No data is available.
1141 * Else: Data is available.
1142 *------------------------------------------------------------------------*/
1144 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1145 uint32_t offset, uint32_t len, uint32_t *actlen)
1147 struct usb_page_search res;
1148 struct tty *tp = sc->sc_tty;
1150 uint32_t offset_orig;
1152 mtx_assert(sc->sc_mtx, MA_OWNED);
1154 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1157 /* get total TX length */
1159 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1160 temp %= UCOM_CONS_BUFSIZE;
1162 /* limit TX length */
1164 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1165 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1172 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1174 /* update counters */
1176 ucom_cons_tx_low += temp;
1177 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1179 /* store actual length */
1183 return (temp ? 1 : 0);
1187 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1189 return (0); /* multiport device polling */
1191 offset_orig = offset;
1195 usbd_get_page(pc, offset, &res);
1197 if (res.length > len) {
1200 /* copy data directly into USB buffer */
1201 cnt = ttydisc_getc(tp, res.buffer, res.length);
1206 if (cnt < res.length) {
1212 actlen[0] = offset - offset_orig;
1214 DPRINTF("cnt=%d\n", actlen[0]);
1216 if (actlen[0] == 0) {
1223 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1224 uint32_t offset, uint32_t len)
1226 struct usb_page_search res;
1227 struct tty *tp = sc->sc_tty;
1231 mtx_assert(sc->sc_mtx, MA_OWNED);
1233 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1236 /* get maximum RX length */
1238 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1239 temp %= UCOM_CONS_BUFSIZE;
1241 /* limit RX length */
1243 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1244 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1251 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1253 /* update counters */
1255 ucom_cons_rx_high += temp;
1256 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1262 return; /* multiport device polling */
1265 return; /* no data */
1267 /* set a flag to prevent recursation ? */
1271 usbd_get_page(pc, offset, &res);
1273 if (res.length > len) {
1277 offset += res.length;
1279 /* pass characters to tty layer */
1284 /* first check if we can pass the buffer directly */
1286 if (ttydisc_can_bypass(tp)) {
1287 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1288 DPRINTF("tp=%p, data lost\n", tp);
1294 for (cnt = 0; cnt != res.length; cnt++) {
1295 if (ttydisc_rint(tp, buf[cnt], 0) == -1) {
1296 /* XXX what should we do? */
1298 DPRINTF("tp=%p, lost %d "
1299 "chars\n", tp, res.length - cnt);
1304 ttydisc_rint_done(tp);
1308 ucom_free(void *xsc)
1310 struct ucom_softc *sc = xsc;
1312 mtx_lock(sc->sc_mtx);
1313 sc->sc_ttyfreed = 1;
1314 cv_signal(&sc->sc_cv);
1315 mtx_unlock(sc->sc_mtx);
1318 static cn_probe_t ucom_cnprobe;
1319 static cn_init_t ucom_cninit;
1320 static cn_term_t ucom_cnterm;
1321 static cn_getc_t ucom_cngetc;
1322 static cn_putc_t ucom_cnputc;
1324 CONSOLE_DRIVER(ucom);
1327 ucom_cnprobe(struct consdev *cp)
1329 if (ucom_cons_unit != -1)
1330 cp->cn_pri = CN_NORMAL;
1332 cp->cn_pri = CN_DEAD;
1334 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1338 ucom_cninit(struct consdev *cp)
1343 ucom_cnterm(struct consdev *cp)
1348 ucom_cngetc(struct consdev *cd)
1350 struct ucom_softc *sc = ucom_cons_softc;
1356 mtx_lock(sc->sc_mtx);
1358 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1359 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1360 ucom_cons_rx_low ++;
1361 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1366 /* start USB transfers */
1367 ucom_outwakeup(sc->sc_tty);
1369 mtx_unlock(sc->sc_mtx);
1371 /* poll if necessary */
1372 if (kdb_active && sc->sc_callback->ucom_poll)
1373 (sc->sc_callback->ucom_poll) (sc);
1379 ucom_cnputc(struct consdev *cd, int c)
1381 struct ucom_softc *sc = ucom_cons_softc;
1389 mtx_lock(sc->sc_mtx);
1391 /* compute maximum TX length */
1393 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1394 temp %= UCOM_CONS_BUFSIZE;
1397 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1398 ucom_cons_tx_high ++;
1399 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1402 /* start USB transfers */
1403 ucom_outwakeup(sc->sc_tty);
1405 mtx_unlock(sc->sc_mtx);
1407 /* poll if necessary */
1408 if (kdb_active && sc->sc_callback->ucom_poll) {
1409 (sc->sc_callback->ucom_poll) (sc);
1410 /* simple flow control */
1418 #include <gdb/gdb.h>
1420 static gdb_probe_f ucom_gdbprobe;
1421 static gdb_init_f ucom_gdbinit;
1422 static gdb_term_f ucom_gdbterm;
1423 static gdb_getc_f ucom_gdbgetc;
1424 static gdb_putc_f ucom_gdbputc;
1426 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1431 return ((ucom_cons_softc != NULL) ? 0 : -1);
1447 ucom_cnputc(NULL, c);
1453 return (ucom_cngetc(NULL));