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/module.h>
80 #include <sys/mutex.h>
81 #include <sys/condvar.h>
82 #include <sys/sysctl.h>
84 #include <sys/unistd.h>
85 #include <sys/callout.h>
86 #include <sys/malloc.h>
91 #include <dev/usb/usb.h>
92 #include <dev/usb/usbdi.h>
93 #include <dev/usb/usbdi_util.h>
95 #define USB_DEBUG_VAR ucom_debug
96 #include <dev/usb/usb_debug.h>
97 #include <dev/usb/usb_busdma.h>
98 #include <dev/usb/usb_process.h>
100 #include <dev/usb/serial/usb_serial.h>
104 SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
107 static int ucom_debug = 0;
109 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
110 &ucom_debug, 0, "ucom debug level");
113 #define UCOM_CONS_BUFSIZE 1024
115 static uint8_t ucom_cons_rx_buf[UCOM_CONS_BUFSIZE];
116 static uint8_t ucom_cons_tx_buf[UCOM_CONS_BUFSIZE];
118 static unsigned int ucom_cons_rx_low = 0;
119 static unsigned int ucom_cons_rx_high = 0;
121 static unsigned int ucom_cons_tx_low = 0;
122 static unsigned int ucom_cons_tx_high = 0;
124 static int ucom_cons_unit = -1;
125 static int ucom_cons_subunit = 0;
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");
132 TUNABLE_INT("hw.usb.ucom.cons_subunit", &ucom_cons_subunit);
133 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_subunit, CTLFLAG_RW,
134 &ucom_cons_subunit, 0, "console subunit number");
135 TUNABLE_INT("hw.usb.ucom.cons_baud", &ucom_cons_baud);
136 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RW,
137 &ucom_cons_baud, 0, "console baud rate");
139 static usb_proc_callback_t ucom_cfg_start_transfers;
140 static usb_proc_callback_t ucom_cfg_open;
141 static usb_proc_callback_t ucom_cfg_close;
142 static usb_proc_callback_t ucom_cfg_line_state;
143 static usb_proc_callback_t ucom_cfg_status_change;
144 static usb_proc_callback_t ucom_cfg_param;
146 static int ucom_unit_alloc(void);
147 static void ucom_unit_free(int);
148 static int ucom_attach_tty(struct ucom_super_softc *, struct ucom_softc *);
149 static void ucom_detach_tty(struct ucom_softc *);
150 static void ucom_queue_command(struct ucom_softc *,
151 usb_proc_callback_t *, struct termios *pt,
152 struct usb_proc_msg *t0, struct usb_proc_msg *t1);
153 static void ucom_shutdown(struct ucom_softc *);
154 static void ucom_ring(struct ucom_softc *, uint8_t);
155 static void ucom_break(struct ucom_softc *, uint8_t);
156 static void ucom_dtr(struct ucom_softc *, uint8_t);
157 static void ucom_rts(struct ucom_softc *, uint8_t);
159 static tsw_open_t ucom_open;
160 static tsw_close_t ucom_close;
161 static tsw_ioctl_t ucom_ioctl;
162 static tsw_modem_t ucom_modem;
163 static tsw_param_t ucom_param;
164 static tsw_outwakeup_t ucom_outwakeup;
165 static tsw_free_t ucom_free;
167 static struct ttydevsw ucom_class = {
168 .tsw_flags = TF_INITLOCK | TF_CALLOUT,
169 .tsw_open = ucom_open,
170 .tsw_close = ucom_close,
171 .tsw_outwakeup = ucom_outwakeup,
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 /* limits size of ucom_bitmap */
183 static uint8_t ucom_bitmap[(UCOM_UNIT_MAX + 7) / 8];
184 static struct mtx ucom_bitmap_mtx;
185 MTX_SYSINIT(ucom_bitmap_mtx, &ucom_bitmap_mtx, "ucom bitmap", MTX_DEF);
187 #define UCOM_TTY_PREFIX "U"
190 * Mark a unit number (the X in cuaUX) as in use.
192 * Note that devices using a different naming scheme (see ucom_tty_name()
193 * callback) still use this unit allocation.
196 ucom_unit_alloc(void)
200 mtx_lock(&ucom_bitmap_mtx);
202 for (unit = 0; unit < UCOM_UNIT_MAX; unit++) {
203 if ((ucom_bitmap[unit / 8] & (1 << (unit % 8))) == 0) {
204 ucom_bitmap[unit / 8] |= (1 << (unit % 8));
209 mtx_unlock(&ucom_bitmap_mtx);
211 if (unit == UCOM_UNIT_MAX)
218 * Mark the unit number as not in use.
221 ucom_unit_free(int unit)
223 mtx_lock(&ucom_bitmap_mtx);
225 ucom_bitmap[unit / 8] &= ~(1 << (unit % 8));
227 mtx_unlock(&ucom_bitmap_mtx);
231 * Setup a group of one or more serial ports.
233 * The mutex pointed to by "mtx" is applied before all
234 * callbacks are called back. Also "mtx" must be applied
235 * before calling into the ucom-layer!
238 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
239 uint32_t subunits, void *parent,
240 const struct ucom_callback *callback, struct mtx *mtx)
247 (callback == NULL)) {
251 ssc->sc_unit = ucom_unit_alloc();
252 if (ssc->sc_unit == -1)
255 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
257 ucom_unit_free(ssc->sc_unit);
260 ssc->sc_subunits = subunits;
262 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
263 sc[subunit].sc_subunit = subunit;
264 sc[subunit].sc_super = ssc;
265 sc[subunit].sc_mtx = mtx;
266 sc[subunit].sc_parent = parent;
267 sc[subunit].sc_callback = callback;
269 error = ucom_attach_tty(ssc, &sc[subunit]);
271 ucom_detach(ssc, &sc[0]);
274 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
277 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
278 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
284 * NOTE: the following function will do nothing if
285 * the structure pointed to by "ssc" and "sc" is zero.
288 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
292 usb_proc_drain(&ssc->sc_tq);
294 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
295 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
297 ucom_detach_tty(&sc[subunit]);
299 /* avoid duplicate detach */
300 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
303 ucom_unit_free(ssc->sc_unit);
304 usb_proc_free(&ssc->sc_tq);
308 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
311 char buf[32]; /* temporary TTY device name buffer */
313 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
317 /* Check if the client has a custom TTY name */
319 if (sc->sc_callback->ucom_tty_name) {
320 sc->sc_callback->ucom_tty_name(sc, buf,
321 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
324 /* Use default TTY name */
325 if (ssc->sc_subunits > 1) {
326 /* multiple modems in one */
327 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
328 ssc->sc_unit, sc->sc_subunit);
331 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
335 tty_makedev(tp, NULL, "%s", buf);
339 DPRINTF("ttycreate: %s\n", buf);
340 cv_init(&sc->sc_cv, "ucom");
342 /* Check if this device should be a console */
343 if ((ucom_cons_softc == NULL) &&
344 (ssc->sc_unit == ucom_cons_unit) &&
345 (sc->sc_subunit == ucom_cons_subunit)) {
348 DPRINTF("unit %d subunit %d is console", ssc->sc_unit, sc->sc_subunit);
350 ucom_cons_softc = sc;
352 memset(&t, 0, sizeof(t));
353 t.c_ispeed = ucom_cons_baud;
354 t.c_ospeed = t.c_ispeed;
357 mtx_lock(ucom_cons_softc->sc_mtx);
358 ucom_cons_rx_low = 0;
359 ucom_cons_rx_high = 0;
360 ucom_cons_tx_low = 0;
361 ucom_cons_tx_high = 0;
362 sc->sc_flag |= UCOM_FLAG_CONSOLE;
363 ucom_open(ucom_cons_softc->sc_tty);
364 ucom_param(ucom_cons_softc->sc_tty, &t);
365 mtx_unlock(ucom_cons_softc->sc_mtx);
372 ucom_detach_tty(struct ucom_softc *sc)
374 struct tty *tp = sc->sc_tty;
376 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
378 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
379 mtx_lock(ucom_cons_softc->sc_mtx);
380 ucom_close(ucom_cons_softc->sc_tty);
381 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
382 mtx_unlock(ucom_cons_softc->sc_mtx);
383 ucom_cons_softc = NULL;
386 /* the config thread has been stopped when we get here */
388 mtx_lock(sc->sc_mtx);
389 sc->sc_flag |= UCOM_FLAG_GONE;
390 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
391 mtx_unlock(sc->sc_mtx);
395 ucom_close(tp); /* close, if any */
399 mtx_lock(sc->sc_mtx);
400 /* Wait for the callback after the TTY is torn down */
401 while (sc->sc_ttyfreed == 0)
402 cv_wait(&sc->sc_cv, sc->sc_mtx);
404 * make sure that read and write transfers are stopped
406 if (sc->sc_callback->ucom_stop_read) {
407 (sc->sc_callback->ucom_stop_read) (sc);
409 if (sc->sc_callback->ucom_stop_write) {
410 (sc->sc_callback->ucom_stop_write) (sc);
412 mtx_unlock(sc->sc_mtx);
414 cv_destroy(&sc->sc_cv);
418 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
422 struct usb_attach_arg *uaa;
424 snprintf(buf, sizeof(buf), "ttyname=%s%d ttyports=%d",
425 UCOM_TTY_PREFIX, ssc->sc_unit, ssc->sc_subunits);
427 /* Store the PNP info in the first interface for the dev */
428 uaa = device_get_ivars(dev);
429 iface_index = uaa->info.bIfaceIndex;
431 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
432 device_printf(dev, "Could not set PNP info\n");
436 ucom_queue_command(struct ucom_softc *sc,
437 usb_proc_callback_t *fn, struct termios *pt,
438 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
440 struct ucom_super_softc *ssc = sc->sc_super;
441 struct ucom_param_task *task;
443 mtx_assert(sc->sc_mtx, MA_OWNED);
445 if (usb_proc_is_gone(&ssc->sc_tq)) {
446 DPRINTF("proc is gone\n");
447 return; /* nothing to do */
450 * NOTE: The task cannot get executed before we drop the
451 * "sc_mtx" mutex. It is safe to update fields in the message
452 * structure after that the message got queued.
454 task = (struct ucom_param_task *)
455 usb_proc_msignal(&ssc->sc_tq, t0, t1);
457 /* Setup callback and softc pointers */
458 task->hdr.pm_callback = fn;
462 * Make a copy of the termios. This field is only present if
463 * the "pt" field is not NULL.
466 task->termios_copy = *pt;
469 * Closing the device should be synchronous.
471 if (fn == ucom_cfg_close)
472 usb_proc_mwait(&ssc->sc_tq, t0, t1);
475 * In case of multiple configure requests,
476 * keep track of the last one!
478 if (fn == ucom_cfg_start_transfers)
479 sc->sc_last_start_xfer = &task->hdr;
483 ucom_shutdown(struct ucom_softc *sc)
485 struct tty *tp = sc->sc_tty;
487 mtx_assert(sc->sc_mtx, MA_OWNED);
492 * Hang up if necessary:
494 if (tp->t_termios.c_cflag & HUPCL) {
495 ucom_modem(tp, 0, SER_DTR);
502 * else: taskqueue is draining or gone
505 ucom_cfg_is_gone(struct ucom_softc *sc)
507 struct ucom_super_softc *ssc = sc->sc_super;
509 return (usb_proc_is_gone(&ssc->sc_tq));
513 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
515 struct ucom_cfg_task *task =
516 (struct ucom_cfg_task *)_task;
517 struct ucom_softc *sc = task->sc;
519 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
522 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
523 /* TTY device closed */
527 if (_task == sc->sc_last_start_xfer)
528 sc->sc_flag |= UCOM_FLAG_GP_DATA;
530 if (sc->sc_callback->ucom_start_read) {
531 (sc->sc_callback->ucom_start_read) (sc);
533 if (sc->sc_callback->ucom_start_write) {
534 (sc->sc_callback->ucom_start_write) (sc);
539 ucom_start_transfers(struct ucom_softc *sc)
541 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
545 * Make sure that data transfers are started in both
548 if (sc->sc_callback->ucom_start_read) {
549 (sc->sc_callback->ucom_start_read) (sc);
551 if (sc->sc_callback->ucom_start_write) {
552 (sc->sc_callback->ucom_start_write) (sc);
557 ucom_cfg_open(struct usb_proc_msg *_task)
559 struct ucom_cfg_task *task =
560 (struct ucom_cfg_task *)_task;
561 struct ucom_softc *sc = task->sc;
565 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
571 sc->sc_flag |= UCOM_FLAG_LL_READY;
573 if (sc->sc_callback->ucom_cfg_open) {
574 (sc->sc_callback->ucom_cfg_open) (sc);
577 usb_pause_mtx(sc->sc_mtx, hz / 10);
583 ucom_open(struct tty *tp)
585 struct ucom_softc *sc = tty_softc(tp);
588 mtx_assert(sc->sc_mtx, MA_OWNED);
590 if (sc->sc_flag & UCOM_FLAG_GONE) {
593 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
597 DPRINTF("tp = %p\n", tp);
599 if (sc->sc_callback->ucom_pre_open) {
601 * give the lower layer a chance to disallow TTY open, for
602 * example if the device is not present:
604 error = (sc->sc_callback->ucom_pre_open) (sc);
609 sc->sc_flag |= UCOM_FLAG_HL_READY;
611 /* Disable transfers */
612 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
618 /* reset programmed line state */
623 ucom_queue_command(sc, ucom_cfg_open, NULL,
624 &sc->sc_open_task[0].hdr,
625 &sc->sc_open_task[1].hdr);
627 /* Queue transfer enable command last */
628 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
629 &sc->sc_start_task[0].hdr,
630 &sc->sc_start_task[1].hdr);
632 ucom_modem(tp, SER_DTR | SER_RTS, 0);
638 ucom_status_change(sc);
644 ucom_cfg_close(struct usb_proc_msg *_task)
646 struct ucom_cfg_task *task =
647 (struct ucom_cfg_task *)_task;
648 struct ucom_softc *sc = task->sc;
652 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
653 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
654 if (sc->sc_callback->ucom_cfg_close)
655 (sc->sc_callback->ucom_cfg_close) (sc);
662 ucom_close(struct tty *tp)
664 struct ucom_softc *sc = tty_softc(tp);
666 mtx_assert(sc->sc_mtx, MA_OWNED);
668 DPRINTF("tp=%p\n", tp);
670 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
671 DPRINTF("tp=%p already closed\n", tp);
676 ucom_queue_command(sc, ucom_cfg_close, NULL,
677 &sc->sc_close_task[0].hdr,
678 &sc->sc_close_task[1].hdr);
680 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
682 if (sc->sc_callback->ucom_stop_read) {
683 (sc->sc_callback->ucom_stop_read) (sc);
688 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
690 struct ucom_softc *sc = tty_softc(tp);
693 mtx_assert(sc->sc_mtx, MA_OWNED);
695 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
698 DPRINTF("cmd = 0x%08lx\n", cmd);
720 if (sc->sc_callback->ucom_ioctl) {
721 error = (sc->sc_callback->ucom_ioctl)
722 (sc, cmd, data, 0, td);
732 ucom_modem(struct tty *tp, int sigon, int sigoff)
734 struct ucom_softc *sc = tty_softc(tp);
737 mtx_assert(sc->sc_mtx, MA_OWNED);
739 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
742 if ((sigon == 0) && (sigoff == 0)) {
744 if (sc->sc_mcr & SER_DTR) {
747 if (sc->sc_mcr & SER_RTS) {
750 if (sc->sc_msr & SER_CTS) {
753 if (sc->sc_msr & SER_DCD) {
756 if (sc->sc_msr & SER_DSR) {
759 if (sc->sc_msr & SER_RI) {
764 if (sigon & SER_DTR) {
765 sc->sc_mcr |= SER_DTR;
767 if (sigoff & SER_DTR) {
768 sc->sc_mcr &= ~SER_DTR;
770 if (sigon & SER_RTS) {
771 sc->sc_mcr |= SER_RTS;
773 if (sigoff & SER_RTS) {
774 sc->sc_mcr &= ~SER_RTS;
776 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
779 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
786 ucom_cfg_line_state(struct usb_proc_msg *_task)
788 struct ucom_cfg_task *task =
789 (struct ucom_cfg_task *)_task;
790 struct ucom_softc *sc = task->sc;
797 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
802 /* compute callback mask */
803 if (sc->sc_callback->ucom_cfg_set_dtr)
805 if (sc->sc_callback->ucom_cfg_set_rts)
807 if (sc->sc_callback->ucom_cfg_set_break)
808 mask |= UCOM_LS_BREAK;
809 if (sc->sc_callback->ucom_cfg_set_ring)
810 mask |= UCOM_LS_RING;
812 /* compute the bits we are to program */
813 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
814 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
815 prev_value = sc->sc_pls_curr ^ notch_bits;
816 last_value = sc->sc_pls_curr;
818 /* reset programmed line state */
823 /* ensure that we don't loose any levels */
824 if (notch_bits & UCOM_LS_DTR)
825 sc->sc_callback->ucom_cfg_set_dtr(sc,
826 (prev_value & UCOM_LS_DTR) ? 1 : 0);
827 if (notch_bits & UCOM_LS_RTS)
828 sc->sc_callback->ucom_cfg_set_rts(sc,
829 (prev_value & UCOM_LS_RTS) ? 1 : 0);
830 if (notch_bits & UCOM_LS_BREAK)
831 sc->sc_callback->ucom_cfg_set_break(sc,
832 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
833 if (notch_bits & UCOM_LS_RING)
834 sc->sc_callback->ucom_cfg_set_ring(sc,
835 (prev_value & UCOM_LS_RING) ? 1 : 0);
838 if (any_bits & UCOM_LS_DTR)
839 sc->sc_callback->ucom_cfg_set_dtr(sc,
840 (last_value & UCOM_LS_DTR) ? 1 : 0);
841 if (any_bits & UCOM_LS_RTS)
842 sc->sc_callback->ucom_cfg_set_rts(sc,
843 (last_value & UCOM_LS_RTS) ? 1 : 0);
844 if (any_bits & UCOM_LS_BREAK)
845 sc->sc_callback->ucom_cfg_set_break(sc,
846 (last_value & UCOM_LS_BREAK) ? 1 : 0);
847 if (any_bits & UCOM_LS_RING)
848 sc->sc_callback->ucom_cfg_set_ring(sc,
849 (last_value & UCOM_LS_RING) ? 1 : 0);
853 ucom_line_state(struct ucom_softc *sc,
854 uint8_t set_bits, uint8_t clear_bits)
856 mtx_assert(sc->sc_mtx, MA_OWNED);
858 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
862 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
864 /* update current programmed line state */
865 sc->sc_pls_curr |= set_bits;
866 sc->sc_pls_curr &= ~clear_bits;
867 sc->sc_pls_set |= set_bits;
868 sc->sc_pls_clr |= clear_bits;
870 /* defer driver programming */
871 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
872 &sc->sc_line_state_task[0].hdr,
873 &sc->sc_line_state_task[1].hdr);
877 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
879 DPRINTF("onoff = %d\n", onoff);
882 ucom_line_state(sc, UCOM_LS_RING, 0);
884 ucom_line_state(sc, 0, UCOM_LS_RING);
888 ucom_break(struct ucom_softc *sc, uint8_t onoff)
890 DPRINTF("onoff = %d\n", onoff);
893 ucom_line_state(sc, UCOM_LS_BREAK, 0);
895 ucom_line_state(sc, 0, UCOM_LS_BREAK);
899 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
901 DPRINTF("onoff = %d\n", onoff);
904 ucom_line_state(sc, UCOM_LS_DTR, 0);
906 ucom_line_state(sc, 0, UCOM_LS_DTR);
910 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
912 DPRINTF("onoff = %d\n", onoff);
915 ucom_line_state(sc, UCOM_LS_RTS, 0);
917 ucom_line_state(sc, 0, UCOM_LS_RTS);
921 ucom_cfg_status_change(struct usb_proc_msg *_task)
923 struct ucom_cfg_task *task =
924 (struct ucom_cfg_task *)_task;
925 struct ucom_softc *sc = task->sc;
934 mtx_assert(sc->sc_mtx, MA_OWNED);
936 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
939 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
947 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
949 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
950 /* TTY device closed */
953 onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
954 lsr_delta = (sc->sc_lsr ^ new_lsr);
956 sc->sc_msr = new_msr;
957 sc->sc_lsr = new_lsr;
961 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
963 DPRINTF("DCD changed to %d\n", onoff);
965 ttydisc_modem(tp, onoff);
968 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
970 DPRINTF("BREAK detected\n");
972 ttydisc_rint(tp, 0, TRE_BREAK);
973 ttydisc_rint_done(tp);
976 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
978 DPRINTF("Frame error detected\n");
980 ttydisc_rint(tp, 0, TRE_FRAMING);
981 ttydisc_rint_done(tp);
984 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
986 DPRINTF("Parity error detected\n");
988 ttydisc_rint(tp, 0, TRE_PARITY);
989 ttydisc_rint_done(tp);
994 ucom_status_change(struct ucom_softc *sc)
996 mtx_assert(sc->sc_mtx, MA_OWNED);
998 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
999 return; /* not supported */
1001 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1006 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1007 &sc->sc_status_task[0].hdr,
1008 &sc->sc_status_task[1].hdr);
1012 ucom_cfg_param(struct usb_proc_msg *_task)
1014 struct ucom_param_task *task =
1015 (struct ucom_param_task *)_task;
1016 struct ucom_softc *sc = task->sc;
1018 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1021 if (sc->sc_callback->ucom_cfg_param == NULL) {
1025 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1028 usb_pause_mtx(sc->sc_mtx, hz / 10);
1032 ucom_param(struct tty *tp, struct termios *t)
1034 struct ucom_softc *sc = tty_softc(tp);
1038 mtx_assert(sc->sc_mtx, MA_OWNED);
1043 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1045 /* XXX the TTY layer should call "open()" first! */
1047 error = ucom_open(tp);
1053 DPRINTF("sc = %p\n", sc);
1055 /* Check requested parameters. */
1056 if (t->c_ospeed < 0) {
1057 DPRINTF("negative ospeed\n");
1061 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1062 DPRINTF("mismatch ispeed and ospeed\n");
1066 t->c_ispeed = t->c_ospeed;
1068 if (sc->sc_callback->ucom_pre_param) {
1069 /* Let the lower layer verify the parameters */
1070 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1072 DPRINTF("callback error = %d\n", error);
1077 /* Disable transfers */
1078 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1080 /* Queue baud rate programming command first */
1081 ucom_queue_command(sc, ucom_cfg_param, t,
1082 &sc->sc_param_task[0].hdr,
1083 &sc->sc_param_task[1].hdr);
1085 /* Queue transfer enable command last */
1086 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1087 &sc->sc_start_task[0].hdr,
1088 &sc->sc_start_task[1].hdr);
1090 if (t->c_cflag & CRTS_IFLOW) {
1091 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1092 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1093 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1094 ucom_modem(tp, SER_RTS, 0);
1106 ucom_outwakeup(struct tty *tp)
1108 struct ucom_softc *sc = tty_softc(tp);
1110 mtx_assert(sc->sc_mtx, MA_OWNED);
1112 DPRINTF("sc = %p\n", sc);
1114 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1115 /* The higher layer is not ready */
1118 ucom_start_transfers(sc);
1121 /*------------------------------------------------------------------------*
1125 * 0: No data is available.
1126 * Else: Data is available.
1127 *------------------------------------------------------------------------*/
1129 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1130 uint32_t offset, uint32_t len, uint32_t *actlen)
1132 struct usb_page_search res;
1133 struct tty *tp = sc->sc_tty;
1135 uint32_t offset_orig;
1137 mtx_assert(sc->sc_mtx, MA_OWNED);
1139 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1142 /* get total TX length */
1144 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1145 temp %= UCOM_CONS_BUFSIZE;
1147 /* limit TX length */
1149 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1150 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1157 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1159 /* update counters */
1161 ucom_cons_tx_low += temp;
1162 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1164 /* store actual length */
1168 return (temp ? 1 : 0);
1172 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1174 return (0); /* multiport device polling */
1176 offset_orig = offset;
1180 usbd_get_page(pc, offset, &res);
1182 if (res.length > len) {
1185 /* copy data directly into USB buffer */
1186 cnt = ttydisc_getc(tp, res.buffer, res.length);
1191 if (cnt < res.length) {
1197 actlen[0] = offset - offset_orig;
1199 DPRINTF("cnt=%d\n", actlen[0]);
1201 if (actlen[0] == 0) {
1208 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1209 uint32_t offset, uint32_t len)
1211 struct usb_page_search res;
1212 struct tty *tp = sc->sc_tty;
1216 mtx_assert(sc->sc_mtx, MA_OWNED);
1218 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1221 /* get maximum RX length */
1223 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1224 temp %= UCOM_CONS_BUFSIZE;
1226 /* limit RX length */
1228 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1229 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1236 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1238 /* update counters */
1240 ucom_cons_rx_high += temp;
1241 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1247 return; /* multiport device polling */
1250 return; /* no data */
1252 /* set a flag to prevent recursation ? */
1256 usbd_get_page(pc, offset, &res);
1258 if (res.length > len) {
1262 offset += res.length;
1264 /* pass characters to tty layer */
1269 /* first check if we can pass the buffer directly */
1271 if (ttydisc_can_bypass(tp)) {
1272 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1273 DPRINTF("tp=%p, data lost\n", tp);
1279 for (cnt = 0; cnt != res.length; cnt++) {
1280 if (ttydisc_rint(tp, buf[cnt], 0) == -1) {
1281 /* XXX what should we do? */
1283 DPRINTF("tp=%p, lost %d "
1284 "chars\n", tp, res.length - cnt);
1289 ttydisc_rint_done(tp);
1293 ucom_free(void *xsc)
1295 struct ucom_softc *sc = xsc;
1297 mtx_lock(sc->sc_mtx);
1298 sc->sc_ttyfreed = 1;
1299 cv_signal(&sc->sc_cv);
1300 mtx_unlock(sc->sc_mtx);
1303 static cn_probe_t ucom_cnprobe;
1304 static cn_init_t ucom_cninit;
1305 static cn_term_t ucom_cnterm;
1306 static cn_getc_t ucom_cngetc;
1307 static cn_putc_t ucom_cnputc;
1309 CONSOLE_DRIVER(ucom);
1312 ucom_cnprobe(struct consdev *cp)
1314 if (ucom_cons_unit != -1)
1315 cp->cn_pri = CN_NORMAL;
1317 cp->cn_pri = CN_DEAD;
1319 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1323 ucom_cninit(struct consdev *cp)
1328 ucom_cnterm(struct consdev *cp)
1333 ucom_cngetc(struct consdev *cd)
1335 struct ucom_softc *sc = ucom_cons_softc;
1341 mtx_lock(sc->sc_mtx);
1343 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1344 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1345 ucom_cons_rx_low ++;
1346 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1351 /* start USB transfers */
1352 ucom_outwakeup(sc->sc_tty);
1354 mtx_unlock(sc->sc_mtx);
1356 /* poll if necessary */
1357 if (kdb_active && sc->sc_callback->ucom_poll)
1358 (sc->sc_callback->ucom_poll) (sc);
1364 ucom_cnputc(struct consdev *cd, int c)
1366 struct ucom_softc *sc = ucom_cons_softc;
1374 mtx_lock(sc->sc_mtx);
1376 /* compute maximum TX length */
1378 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1379 temp %= UCOM_CONS_BUFSIZE;
1382 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1383 ucom_cons_tx_high ++;
1384 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1387 /* start USB transfers */
1388 ucom_outwakeup(sc->sc_tty);
1390 mtx_unlock(sc->sc_mtx);
1392 /* poll if necessary */
1393 if (kdb_active && sc->sc_callback->ucom_poll) {
1394 (sc->sc_callback->ucom_poll) (sc);
1395 /* simple flow control */
1403 #include <gdb/gdb.h>
1405 static gdb_probe_f ucom_gdbprobe;
1406 static gdb_init_f ucom_gdbinit;
1407 static gdb_term_f ucom_gdbterm;
1408 static gdb_getc_f ucom_gdbgetc;
1409 static gdb_putc_f ucom_gdbputc;
1411 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1416 return ((ucom_cons_softc != NULL) ? 0 : -1);
1432 ucom_cnputc(NULL, c);
1438 return (ucom_cngetc(NULL));