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 int subunits, void *parent,
240 const struct ucom_callback *callback, struct mtx *mtx)
247 (callback == NULL)) {
251 /* allocate a uniq unit number */
252 ssc->sc_unit = ucom_unit_alloc();
253 if (ssc->sc_unit == -1)
256 /* generate TTY name string */
257 snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
258 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
260 /* create USB request handling process */
261 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
263 ucom_unit_free(ssc->sc_unit);
266 ssc->sc_subunits = subunits;
268 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
269 sc[subunit].sc_subunit = subunit;
270 sc[subunit].sc_super = ssc;
271 sc[subunit].sc_mtx = mtx;
272 sc[subunit].sc_parent = parent;
273 sc[subunit].sc_callback = callback;
275 error = ucom_attach_tty(ssc, &sc[subunit]);
277 ucom_detach(ssc, &sc[0]);
280 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
283 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
284 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
290 * NOTE: the following function will do nothing if
291 * the structure pointed to by "ssc" and "sc" is zero.
294 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
298 if (ssc->sc_subunits == 0)
299 return; /* not initialized */
301 if (ssc->sc_sysctl_ttyname != NULL) {
302 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
303 ssc->sc_sysctl_ttyname = NULL;
306 if (ssc->sc_sysctl_ttyports != NULL) {
307 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
308 ssc->sc_sysctl_ttyports = NULL;
311 usb_proc_drain(&ssc->sc_tq);
313 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
314 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
316 ucom_detach_tty(&sc[subunit]);
318 /* avoid duplicate detach */
319 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
322 ucom_unit_free(ssc->sc_unit);
323 usb_proc_free(&ssc->sc_tq);
327 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
330 char buf[32]; /* temporary TTY device name buffer */
332 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
336 /* Check if the client has a custom TTY name */
338 if (sc->sc_callback->ucom_tty_name) {
339 sc->sc_callback->ucom_tty_name(sc, buf,
340 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
343 /* Use default TTY name */
344 if (ssc->sc_subunits > 1) {
345 /* multiple modems in one */
346 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
347 ssc->sc_unit, sc->sc_subunit);
350 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
354 tty_makedev(tp, NULL, "%s", buf);
358 DPRINTF("ttycreate: %s\n", buf);
359 cv_init(&sc->sc_cv, "ucom");
361 /* Check if this device should be a console */
362 if ((ucom_cons_softc == NULL) &&
363 (ssc->sc_unit == ucom_cons_unit) &&
364 (sc->sc_subunit == ucom_cons_subunit)) {
367 DPRINTF("unit %d subunit %d is console", ssc->sc_unit, sc->sc_subunit);
369 ucom_cons_softc = sc;
371 memset(&t, 0, sizeof(t));
372 t.c_ispeed = ucom_cons_baud;
373 t.c_ospeed = t.c_ispeed;
376 mtx_lock(ucom_cons_softc->sc_mtx);
377 ucom_cons_rx_low = 0;
378 ucom_cons_rx_high = 0;
379 ucom_cons_tx_low = 0;
380 ucom_cons_tx_high = 0;
381 sc->sc_flag |= UCOM_FLAG_CONSOLE;
382 ucom_open(ucom_cons_softc->sc_tty);
383 ucom_param(ucom_cons_softc->sc_tty, &t);
384 mtx_unlock(ucom_cons_softc->sc_mtx);
391 ucom_detach_tty(struct ucom_softc *sc)
393 struct tty *tp = sc->sc_tty;
395 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
397 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
398 mtx_lock(ucom_cons_softc->sc_mtx);
399 ucom_close(ucom_cons_softc->sc_tty);
400 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
401 mtx_unlock(ucom_cons_softc->sc_mtx);
402 ucom_cons_softc = NULL;
405 /* the config thread has been stopped when we get here */
407 mtx_lock(sc->sc_mtx);
408 sc->sc_flag |= UCOM_FLAG_GONE;
409 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
410 mtx_unlock(sc->sc_mtx);
414 ucom_close(tp); /* close, if any */
418 mtx_lock(sc->sc_mtx);
419 /* Wait for the callback after the TTY is torn down */
420 while (sc->sc_ttyfreed == 0)
421 cv_wait(&sc->sc_cv, sc->sc_mtx);
423 * make sure that read and write transfers are stopped
425 if (sc->sc_callback->ucom_stop_read) {
426 (sc->sc_callback->ucom_stop_read) (sc);
428 if (sc->sc_callback->ucom_stop_write) {
429 (sc->sc_callback->ucom_stop_write) (sc);
431 mtx_unlock(sc->sc_mtx);
433 cv_destroy(&sc->sc_cv);
437 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
441 struct usb_attach_arg *uaa;
443 snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
444 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
446 /* Store the PNP info in the first interface for the device */
447 uaa = device_get_ivars(dev);
448 iface_index = uaa->info.bIfaceIndex;
450 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
451 device_printf(dev, "Could not set PNP info\n");
454 * The following information is also replicated in the PNP-info
455 * string which is registered above:
457 if (ssc->sc_sysctl_ttyname == NULL) {
458 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
459 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
460 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
461 "TTY device basename");
463 if (ssc->sc_sysctl_ttyports == NULL) {
464 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
465 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
466 OID_AUTO, "ttyports", CTLFLAG_RD,
467 NULL, ssc->sc_subunits, "Number of ports");
472 ucom_queue_command(struct ucom_softc *sc,
473 usb_proc_callback_t *fn, struct termios *pt,
474 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
476 struct ucom_super_softc *ssc = sc->sc_super;
477 struct ucom_param_task *task;
479 mtx_assert(sc->sc_mtx, MA_OWNED);
481 if (usb_proc_is_gone(&ssc->sc_tq)) {
482 DPRINTF("proc is gone\n");
483 return; /* nothing to do */
486 * NOTE: The task cannot get executed before we drop the
487 * "sc_mtx" mutex. It is safe to update fields in the message
488 * structure after that the message got queued.
490 task = (struct ucom_param_task *)
491 usb_proc_msignal(&ssc->sc_tq, t0, t1);
493 /* Setup callback and softc pointers */
494 task->hdr.pm_callback = fn;
498 * Make a copy of the termios. This field is only present if
499 * the "pt" field is not NULL.
502 task->termios_copy = *pt;
505 * Closing the device should be synchronous.
507 if (fn == ucom_cfg_close)
508 usb_proc_mwait(&ssc->sc_tq, t0, t1);
511 * In case of multiple configure requests,
512 * keep track of the last one!
514 if (fn == ucom_cfg_start_transfers)
515 sc->sc_last_start_xfer = &task->hdr;
519 ucom_shutdown(struct ucom_softc *sc)
521 struct tty *tp = sc->sc_tty;
523 mtx_assert(sc->sc_mtx, MA_OWNED);
528 * Hang up if necessary:
530 if (tp->t_termios.c_cflag & HUPCL) {
531 ucom_modem(tp, 0, SER_DTR);
538 * else: taskqueue is draining or gone
541 ucom_cfg_is_gone(struct ucom_softc *sc)
543 struct ucom_super_softc *ssc = sc->sc_super;
545 return (usb_proc_is_gone(&ssc->sc_tq));
549 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
551 struct ucom_cfg_task *task =
552 (struct ucom_cfg_task *)_task;
553 struct ucom_softc *sc = task->sc;
555 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
558 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
559 /* TTY device closed */
563 if (_task == sc->sc_last_start_xfer)
564 sc->sc_flag |= UCOM_FLAG_GP_DATA;
566 if (sc->sc_callback->ucom_start_read) {
567 (sc->sc_callback->ucom_start_read) (sc);
569 if (sc->sc_callback->ucom_start_write) {
570 (sc->sc_callback->ucom_start_write) (sc);
575 ucom_start_transfers(struct ucom_softc *sc)
577 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
581 * Make sure that data transfers are started in both
584 if (sc->sc_callback->ucom_start_read) {
585 (sc->sc_callback->ucom_start_read) (sc);
587 if (sc->sc_callback->ucom_start_write) {
588 (sc->sc_callback->ucom_start_write) (sc);
593 ucom_cfg_open(struct usb_proc_msg *_task)
595 struct ucom_cfg_task *task =
596 (struct ucom_cfg_task *)_task;
597 struct ucom_softc *sc = task->sc;
601 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
607 sc->sc_flag |= UCOM_FLAG_LL_READY;
609 if (sc->sc_callback->ucom_cfg_open) {
610 (sc->sc_callback->ucom_cfg_open) (sc);
613 usb_pause_mtx(sc->sc_mtx, hz / 10);
619 ucom_open(struct tty *tp)
621 struct ucom_softc *sc = tty_softc(tp);
624 mtx_assert(sc->sc_mtx, MA_OWNED);
626 if (sc->sc_flag & UCOM_FLAG_GONE) {
629 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
633 DPRINTF("tp = %p\n", tp);
635 if (sc->sc_callback->ucom_pre_open) {
637 * give the lower layer a chance to disallow TTY open, for
638 * example if the device is not present:
640 error = (sc->sc_callback->ucom_pre_open) (sc);
645 sc->sc_flag |= UCOM_FLAG_HL_READY;
647 /* Disable transfers */
648 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
654 /* reset programmed line state */
659 ucom_queue_command(sc, ucom_cfg_open, NULL,
660 &sc->sc_open_task[0].hdr,
661 &sc->sc_open_task[1].hdr);
663 /* Queue transfer enable command last */
664 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
665 &sc->sc_start_task[0].hdr,
666 &sc->sc_start_task[1].hdr);
668 ucom_modem(tp, SER_DTR | SER_RTS, 0);
674 ucom_status_change(sc);
680 ucom_cfg_close(struct usb_proc_msg *_task)
682 struct ucom_cfg_task *task =
683 (struct ucom_cfg_task *)_task;
684 struct ucom_softc *sc = task->sc;
688 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
689 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
690 if (sc->sc_callback->ucom_cfg_close)
691 (sc->sc_callback->ucom_cfg_close) (sc);
698 ucom_close(struct tty *tp)
700 struct ucom_softc *sc = tty_softc(tp);
702 mtx_assert(sc->sc_mtx, MA_OWNED);
704 DPRINTF("tp=%p\n", tp);
706 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
707 DPRINTF("tp=%p already closed\n", tp);
712 ucom_queue_command(sc, ucom_cfg_close, NULL,
713 &sc->sc_close_task[0].hdr,
714 &sc->sc_close_task[1].hdr);
716 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
718 if (sc->sc_callback->ucom_stop_read) {
719 (sc->sc_callback->ucom_stop_read) (sc);
724 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
726 struct ucom_softc *sc = tty_softc(tp);
729 mtx_assert(sc->sc_mtx, MA_OWNED);
731 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
734 DPRINTF("cmd = 0x%08lx\n", cmd);
756 if (sc->sc_callback->ucom_ioctl) {
757 error = (sc->sc_callback->ucom_ioctl)
758 (sc, cmd, data, 0, td);
768 ucom_modem(struct tty *tp, int sigon, int sigoff)
770 struct ucom_softc *sc = tty_softc(tp);
773 mtx_assert(sc->sc_mtx, MA_OWNED);
775 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
778 if ((sigon == 0) && (sigoff == 0)) {
780 if (sc->sc_mcr & SER_DTR) {
783 if (sc->sc_mcr & SER_RTS) {
786 if (sc->sc_msr & SER_CTS) {
789 if (sc->sc_msr & SER_DCD) {
792 if (sc->sc_msr & SER_DSR) {
795 if (sc->sc_msr & SER_RI) {
800 if (sigon & SER_DTR) {
801 sc->sc_mcr |= SER_DTR;
803 if (sigoff & SER_DTR) {
804 sc->sc_mcr &= ~SER_DTR;
806 if (sigon & SER_RTS) {
807 sc->sc_mcr |= SER_RTS;
809 if (sigoff & SER_RTS) {
810 sc->sc_mcr &= ~SER_RTS;
812 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
815 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
822 ucom_cfg_line_state(struct usb_proc_msg *_task)
824 struct ucom_cfg_task *task =
825 (struct ucom_cfg_task *)_task;
826 struct ucom_softc *sc = task->sc;
833 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
838 /* compute callback mask */
839 if (sc->sc_callback->ucom_cfg_set_dtr)
841 if (sc->sc_callback->ucom_cfg_set_rts)
843 if (sc->sc_callback->ucom_cfg_set_break)
844 mask |= UCOM_LS_BREAK;
845 if (sc->sc_callback->ucom_cfg_set_ring)
846 mask |= UCOM_LS_RING;
848 /* compute the bits we are to program */
849 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
850 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
851 prev_value = sc->sc_pls_curr ^ notch_bits;
852 last_value = sc->sc_pls_curr;
854 /* reset programmed line state */
859 /* ensure that we don't loose any levels */
860 if (notch_bits & UCOM_LS_DTR)
861 sc->sc_callback->ucom_cfg_set_dtr(sc,
862 (prev_value & UCOM_LS_DTR) ? 1 : 0);
863 if (notch_bits & UCOM_LS_RTS)
864 sc->sc_callback->ucom_cfg_set_rts(sc,
865 (prev_value & UCOM_LS_RTS) ? 1 : 0);
866 if (notch_bits & UCOM_LS_BREAK)
867 sc->sc_callback->ucom_cfg_set_break(sc,
868 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
869 if (notch_bits & UCOM_LS_RING)
870 sc->sc_callback->ucom_cfg_set_ring(sc,
871 (prev_value & UCOM_LS_RING) ? 1 : 0);
874 if (any_bits & UCOM_LS_DTR)
875 sc->sc_callback->ucom_cfg_set_dtr(sc,
876 (last_value & UCOM_LS_DTR) ? 1 : 0);
877 if (any_bits & UCOM_LS_RTS)
878 sc->sc_callback->ucom_cfg_set_rts(sc,
879 (last_value & UCOM_LS_RTS) ? 1 : 0);
880 if (any_bits & UCOM_LS_BREAK)
881 sc->sc_callback->ucom_cfg_set_break(sc,
882 (last_value & UCOM_LS_BREAK) ? 1 : 0);
883 if (any_bits & UCOM_LS_RING)
884 sc->sc_callback->ucom_cfg_set_ring(sc,
885 (last_value & UCOM_LS_RING) ? 1 : 0);
889 ucom_line_state(struct ucom_softc *sc,
890 uint8_t set_bits, uint8_t clear_bits)
892 mtx_assert(sc->sc_mtx, MA_OWNED);
894 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
898 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
900 /* update current programmed line state */
901 sc->sc_pls_curr |= set_bits;
902 sc->sc_pls_curr &= ~clear_bits;
903 sc->sc_pls_set |= set_bits;
904 sc->sc_pls_clr |= clear_bits;
906 /* defer driver programming */
907 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
908 &sc->sc_line_state_task[0].hdr,
909 &sc->sc_line_state_task[1].hdr);
913 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
915 DPRINTF("onoff = %d\n", onoff);
918 ucom_line_state(sc, UCOM_LS_RING, 0);
920 ucom_line_state(sc, 0, UCOM_LS_RING);
924 ucom_break(struct ucom_softc *sc, uint8_t onoff)
926 DPRINTF("onoff = %d\n", onoff);
929 ucom_line_state(sc, UCOM_LS_BREAK, 0);
931 ucom_line_state(sc, 0, UCOM_LS_BREAK);
935 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
937 DPRINTF("onoff = %d\n", onoff);
940 ucom_line_state(sc, UCOM_LS_DTR, 0);
942 ucom_line_state(sc, 0, UCOM_LS_DTR);
946 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
948 DPRINTF("onoff = %d\n", onoff);
951 ucom_line_state(sc, UCOM_LS_RTS, 0);
953 ucom_line_state(sc, 0, UCOM_LS_RTS);
957 ucom_cfg_status_change(struct usb_proc_msg *_task)
959 struct ucom_cfg_task *task =
960 (struct ucom_cfg_task *)_task;
961 struct ucom_softc *sc = task->sc;
970 mtx_assert(sc->sc_mtx, MA_OWNED);
972 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
975 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
983 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
985 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
986 /* TTY device closed */
989 onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
990 lsr_delta = (sc->sc_lsr ^ new_lsr);
992 sc->sc_msr = new_msr;
993 sc->sc_lsr = new_lsr;
997 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
999 DPRINTF("DCD changed to %d\n", onoff);
1001 ttydisc_modem(tp, onoff);
1004 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1006 DPRINTF("BREAK detected\n");
1008 ttydisc_rint(tp, 0, TRE_BREAK);
1009 ttydisc_rint_done(tp);
1012 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1014 DPRINTF("Frame error detected\n");
1016 ttydisc_rint(tp, 0, TRE_FRAMING);
1017 ttydisc_rint_done(tp);
1020 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1022 DPRINTF("Parity error detected\n");
1024 ttydisc_rint(tp, 0, TRE_PARITY);
1025 ttydisc_rint_done(tp);
1030 ucom_status_change(struct ucom_softc *sc)
1032 mtx_assert(sc->sc_mtx, MA_OWNED);
1034 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1035 return; /* not supported */
1037 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1042 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1043 &sc->sc_status_task[0].hdr,
1044 &sc->sc_status_task[1].hdr);
1048 ucom_cfg_param(struct usb_proc_msg *_task)
1050 struct ucom_param_task *task =
1051 (struct ucom_param_task *)_task;
1052 struct ucom_softc *sc = task->sc;
1054 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1057 if (sc->sc_callback->ucom_cfg_param == NULL) {
1061 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1064 usb_pause_mtx(sc->sc_mtx, hz / 10);
1068 ucom_param(struct tty *tp, struct termios *t)
1070 struct ucom_softc *sc = tty_softc(tp);
1074 mtx_assert(sc->sc_mtx, MA_OWNED);
1079 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1081 /* XXX the TTY layer should call "open()" first! */
1083 error = ucom_open(tp);
1089 DPRINTF("sc = %p\n", sc);
1091 /* Check requested parameters. */
1092 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1093 DPRINTF("mismatch ispeed and ospeed\n");
1097 t->c_ispeed = t->c_ospeed;
1099 if (sc->sc_callback->ucom_pre_param) {
1100 /* Let the lower layer verify the parameters */
1101 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1103 DPRINTF("callback error = %d\n", error);
1108 /* Disable transfers */
1109 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1111 /* Queue baud rate programming command first */
1112 ucom_queue_command(sc, ucom_cfg_param, t,
1113 &sc->sc_param_task[0].hdr,
1114 &sc->sc_param_task[1].hdr);
1116 /* Queue transfer enable command last */
1117 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1118 &sc->sc_start_task[0].hdr,
1119 &sc->sc_start_task[1].hdr);
1121 if (t->c_cflag & CRTS_IFLOW) {
1122 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1123 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1124 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1125 ucom_modem(tp, SER_RTS, 0);
1137 ucom_outwakeup(struct tty *tp)
1139 struct ucom_softc *sc = tty_softc(tp);
1141 mtx_assert(sc->sc_mtx, MA_OWNED);
1143 DPRINTF("sc = %p\n", sc);
1145 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1146 /* The higher layer is not ready */
1149 ucom_start_transfers(sc);
1152 /*------------------------------------------------------------------------*
1156 * 0: No data is available.
1157 * Else: Data is available.
1158 *------------------------------------------------------------------------*/
1160 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1161 uint32_t offset, uint32_t len, uint32_t *actlen)
1163 struct usb_page_search res;
1164 struct tty *tp = sc->sc_tty;
1166 uint32_t offset_orig;
1168 mtx_assert(sc->sc_mtx, MA_OWNED);
1170 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1173 /* get total TX length */
1175 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1176 temp %= UCOM_CONS_BUFSIZE;
1178 /* limit TX length */
1180 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1181 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1188 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1190 /* update counters */
1192 ucom_cons_tx_low += temp;
1193 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1195 /* store actual length */
1199 return (temp ? 1 : 0);
1203 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1205 return (0); /* multiport device polling */
1207 offset_orig = offset;
1211 usbd_get_page(pc, offset, &res);
1213 if (res.length > len) {
1216 /* copy data directly into USB buffer */
1217 cnt = ttydisc_getc(tp, res.buffer, res.length);
1222 if (cnt < res.length) {
1228 actlen[0] = offset - offset_orig;
1230 DPRINTF("cnt=%d\n", actlen[0]);
1232 if (actlen[0] == 0) {
1239 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1240 uint32_t offset, uint32_t len)
1242 struct usb_page_search res;
1243 struct tty *tp = sc->sc_tty;
1247 mtx_assert(sc->sc_mtx, MA_OWNED);
1249 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1252 /* get maximum RX length */
1254 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1255 temp %= UCOM_CONS_BUFSIZE;
1257 /* limit RX length */
1259 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1260 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1267 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1269 /* update counters */
1271 ucom_cons_rx_high += temp;
1272 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1278 return; /* multiport device polling */
1281 return; /* no data */
1283 /* set a flag to prevent recursation ? */
1287 usbd_get_page(pc, offset, &res);
1289 if (res.length > len) {
1293 offset += res.length;
1295 /* pass characters to tty layer */
1300 /* first check if we can pass the buffer directly */
1302 if (ttydisc_can_bypass(tp)) {
1303 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1304 DPRINTF("tp=%p, data lost\n", tp);
1310 for (cnt = 0; cnt != res.length; cnt++) {
1311 if (ttydisc_rint(tp, buf[cnt], 0) == -1) {
1312 /* XXX what should we do? */
1314 DPRINTF("tp=%p, lost %d "
1315 "chars\n", tp, res.length - cnt);
1320 ttydisc_rint_done(tp);
1324 ucom_free(void *xsc)
1326 struct ucom_softc *sc = xsc;
1328 mtx_lock(sc->sc_mtx);
1329 sc->sc_ttyfreed = 1;
1330 cv_signal(&sc->sc_cv);
1331 mtx_unlock(sc->sc_mtx);
1334 static cn_probe_t ucom_cnprobe;
1335 static cn_init_t ucom_cninit;
1336 static cn_term_t ucom_cnterm;
1337 static cn_getc_t ucom_cngetc;
1338 static cn_putc_t ucom_cnputc;
1340 CONSOLE_DRIVER(ucom);
1343 ucom_cnprobe(struct consdev *cp)
1345 if (ucom_cons_unit != -1)
1346 cp->cn_pri = CN_NORMAL;
1348 cp->cn_pri = CN_DEAD;
1350 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1354 ucom_cninit(struct consdev *cp)
1359 ucom_cnterm(struct consdev *cp)
1364 ucom_cngetc(struct consdev *cd)
1366 struct ucom_softc *sc = ucom_cons_softc;
1372 mtx_lock(sc->sc_mtx);
1374 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1375 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1376 ucom_cons_rx_low ++;
1377 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1382 /* start USB transfers */
1383 ucom_outwakeup(sc->sc_tty);
1385 mtx_unlock(sc->sc_mtx);
1387 /* poll if necessary */
1388 if (kdb_active && sc->sc_callback->ucom_poll)
1389 (sc->sc_callback->ucom_poll) (sc);
1395 ucom_cnputc(struct consdev *cd, int c)
1397 struct ucom_softc *sc = ucom_cons_softc;
1405 mtx_lock(sc->sc_mtx);
1407 /* compute maximum TX length */
1409 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1410 temp %= UCOM_CONS_BUFSIZE;
1413 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1414 ucom_cons_tx_high ++;
1415 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1418 /* start USB transfers */
1419 ucom_outwakeup(sc->sc_tty);
1421 mtx_unlock(sc->sc_mtx);
1423 /* poll if necessary */
1424 if (kdb_active && sc->sc_callback->ucom_poll) {
1425 (sc->sc_callback->ucom_poll) (sc);
1426 /* simple flow control */
1434 #include <gdb/gdb.h>
1436 static gdb_probe_f ucom_gdbprobe;
1437 static gdb_init_f ucom_gdbinit;
1438 static gdb_term_f ucom_gdbterm;
1439 static gdb_getc_f ucom_gdbgetc;
1440 static gdb_putc_f ucom_gdbputc;
1442 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1447 return ((ucom_cons_softc != NULL) ? 0 : -1);
1463 ucom_cnputc(NULL, c);
1469 return (ucom_cngetc(NULL));