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 static 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 | CTLFLAG_TUN,
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 | CTLFLAG_TUN,
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 | CTLFLAG_TUN,
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_super_softc *, 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_inwakeup_t ucom_inwakeup;
166 static tsw_free_t ucom_free;
168 static struct ttydevsw ucom_class = {
169 .tsw_flags = TF_INITLOCK | TF_CALLOUT,
170 .tsw_open = ucom_open,
171 .tsw_close = ucom_close,
172 .tsw_outwakeup = ucom_outwakeup,
173 .tsw_inwakeup = ucom_inwakeup,
174 .tsw_ioctl = ucom_ioctl,
175 .tsw_param = ucom_param,
176 .tsw_modem = ucom_modem,
177 .tsw_free = ucom_free,
180 MODULE_DEPEND(ucom, usb, 1, 1, 1);
181 MODULE_VERSION(ucom, 1);
183 #define UCOM_UNIT_MAX 128 /* maximum number of units */
184 #define UCOM_TTY_PREFIX "U"
186 static struct unrhdr *ucom_unrhdr;
187 static struct mtx ucom_mtx;
188 static int ucom_close_refs;
194 ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
195 mtx_init(&ucom_mtx, "UCOM MTX", NULL, MTX_DEF);
197 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
200 ucom_uninit(void *arg)
211 mtx_destroy(&ucom_mtx);
213 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_uninit, NULL);
216 * Mark a unit number (the X in cuaUX) as in use.
218 * Note that devices using a different naming scheme (see ucom_tty_name()
219 * callback) still use this unit allocation.
222 ucom_unit_alloc(void)
227 if (ucom_unrhdr == NULL) {
228 DPRINTF("ucom_unrhdr is NULL\n");
231 unit = alloc_unr(ucom_unrhdr);
232 DPRINTF("unit %d is allocated\n", unit);
237 * Mark the unit number as not in use.
240 ucom_unit_free(int unit)
243 if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
244 DPRINTF("cannot free unit number\n");
247 DPRINTF("unit %d is freed\n", unit);
248 free_unr(ucom_unrhdr, unit);
252 * Setup a group of one or more serial ports.
254 * The mutex pointed to by "mtx" is applied before all
255 * callbacks are called back. Also "mtx" must be applied
256 * before calling into the ucom-layer!
259 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
260 int subunits, void *parent,
261 const struct ucom_callback *callback, struct mtx *mtx)
268 (callback == NULL) ||
273 /* allocate a uniq unit number */
274 ssc->sc_unit = ucom_unit_alloc();
275 if (ssc->sc_unit == -1)
278 /* generate TTY name string */
279 snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
280 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
282 /* create USB request handling process */
283 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
285 ucom_unit_free(ssc->sc_unit);
288 ssc->sc_subunits = subunits;
289 ssc->sc_flag = UCOM_FLAG_ATTACHED |
292 if (callback->ucom_free == NULL)
293 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
295 /* increment reference count */
298 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
299 sc[subunit].sc_subunit = subunit;
300 sc[subunit].sc_super = ssc;
301 sc[subunit].sc_mtx = mtx;
302 sc[subunit].sc_parent = parent;
303 sc[subunit].sc_callback = callback;
305 error = ucom_attach_tty(ssc, &sc[subunit]);
307 ucom_detach(ssc, &sc[0]);
310 /* increment reference count */
313 /* set subunit attached */
314 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
317 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
318 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
324 * The following function will do nothing if the structure pointed to
325 * by "ssc" and "sc" is zero or has already been detached.
328 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
332 if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
333 return; /* not initialized */
335 if (ssc->sc_sysctl_ttyname != NULL) {
336 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
337 ssc->sc_sysctl_ttyname = NULL;
340 if (ssc->sc_sysctl_ttyports != NULL) {
341 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
342 ssc->sc_sysctl_ttyports = NULL;
345 usb_proc_drain(&ssc->sc_tq);
347 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
348 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
350 ucom_detach_tty(ssc, &sc[subunit]);
352 /* avoid duplicate detach */
353 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
356 usb_proc_free(&ssc->sc_tq);
360 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
363 /* make sure we don't detach twice */
364 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
368 ucom_drain(struct ucom_super_softc *ssc)
371 while (ssc->sc_refs > 0) {
372 printf("ucom: Waiting for a TTY device to close.\n");
373 usb_pause_mtx(&ucom_mtx, hz);
375 mtx_unlock(&ucom_mtx);
379 ucom_drain_all(void *arg)
382 while (ucom_close_refs > 0) {
383 printf("ucom: Waiting for all detached TTY "
384 "devices to have open fds closed.\n");
385 usb_pause_mtx(&ucom_mtx, hz);
387 mtx_unlock(&ucom_mtx);
391 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
394 char buf[32]; /* temporary TTY device name buffer */
396 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
400 /* Check if the client has a custom TTY name */
402 if (sc->sc_callback->ucom_tty_name) {
403 sc->sc_callback->ucom_tty_name(sc, buf,
404 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
407 /* Use default TTY name */
408 if (ssc->sc_subunits > 1) {
409 /* multiple modems in one */
410 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
411 ssc->sc_unit, sc->sc_subunit);
414 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
418 tty_makedev(tp, NULL, "%s", buf);
422 DPRINTF("ttycreate: %s\n", buf);
424 /* Check if this device should be a console */
425 if ((ucom_cons_softc == NULL) &&
426 (ssc->sc_unit == ucom_cons_unit) &&
427 (sc->sc_subunit == ucom_cons_subunit)) {
429 DPRINTF("unit %d subunit %d is console",
430 ssc->sc_unit, sc->sc_subunit);
432 ucom_cons_softc = sc;
434 tty_init_console(tp, ucom_cons_baud);
436 UCOM_MTX_LOCK(ucom_cons_softc);
437 ucom_cons_rx_low = 0;
438 ucom_cons_rx_high = 0;
439 ucom_cons_tx_low = 0;
440 ucom_cons_tx_high = 0;
441 sc->sc_flag |= UCOM_FLAG_CONSOLE;
442 ucom_open(ucom_cons_softc->sc_tty);
443 ucom_param(ucom_cons_softc->sc_tty, &tp->t_termios_init_in);
444 UCOM_MTX_UNLOCK(ucom_cons_softc);
451 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
453 struct tty *tp = sc->sc_tty;
455 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
457 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
458 UCOM_MTX_LOCK(ucom_cons_softc);
459 ucom_close(ucom_cons_softc->sc_tty);
460 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
461 UCOM_MTX_UNLOCK(ucom_cons_softc);
462 ucom_cons_softc = NULL;
465 /* the config thread has been stopped when we get here */
468 sc->sc_flag |= UCOM_FLAG_GONE;
469 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
475 mtx_unlock(&ucom_mtx);
479 ucom_close(tp); /* close, if any */
485 * make sure that read and write transfers are stopped
487 if (sc->sc_callback->ucom_stop_read)
488 (sc->sc_callback->ucom_stop_read) (sc);
489 if (sc->sc_callback->ucom_stop_write)
490 (sc->sc_callback->ucom_stop_write) (sc);
496 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
500 struct usb_attach_arg *uaa;
502 snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
503 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
505 /* Store the PNP info in the first interface for the device */
506 uaa = device_get_ivars(dev);
507 iface_index = uaa->info.bIfaceIndex;
509 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
510 device_printf(dev, "Could not set PNP info\n");
513 * The following information is also replicated in the PNP-info
514 * string which is registered above:
516 if (ssc->sc_sysctl_ttyname == NULL) {
517 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
518 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
519 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
520 "TTY device basename");
522 if (ssc->sc_sysctl_ttyports == NULL) {
523 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
524 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
525 OID_AUTO, "ttyports", CTLFLAG_RD,
526 NULL, ssc->sc_subunits, "Number of ports");
531 ucom_queue_command(struct ucom_softc *sc,
532 usb_proc_callback_t *fn, struct termios *pt,
533 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
535 struct ucom_super_softc *ssc = sc->sc_super;
536 struct ucom_param_task *task;
538 UCOM_MTX_ASSERT(sc, MA_OWNED);
540 if (usb_proc_is_gone(&ssc->sc_tq)) {
541 DPRINTF("proc is gone\n");
542 return; /* nothing to do */
545 * NOTE: The task cannot get executed before we drop the
546 * "sc_mtx" mutex. It is safe to update fields in the message
547 * structure after that the message got queued.
549 task = (struct ucom_param_task *)
550 usb_proc_msignal(&ssc->sc_tq, t0, t1);
552 /* Setup callback and softc pointers */
553 task->hdr.pm_callback = fn;
557 * Make a copy of the termios. This field is only present if
558 * the "pt" field is not NULL.
561 task->termios_copy = *pt;
564 * Closing the device should be synchronous.
566 if (fn == ucom_cfg_close)
567 usb_proc_mwait(&ssc->sc_tq, t0, t1);
570 * In case of multiple configure requests,
571 * keep track of the last one!
573 if (fn == ucom_cfg_start_transfers)
574 sc->sc_last_start_xfer = &task->hdr;
578 ucom_shutdown(struct ucom_softc *sc)
580 struct tty *tp = sc->sc_tty;
582 UCOM_MTX_ASSERT(sc, MA_OWNED);
587 * Hang up if necessary:
589 if (tp->t_termios.c_cflag & HUPCL) {
590 ucom_modem(tp, 0, SER_DTR);
597 * else: taskqueue is draining or gone
600 ucom_cfg_is_gone(struct ucom_softc *sc)
602 struct ucom_super_softc *ssc = sc->sc_super;
604 return (usb_proc_is_gone(&ssc->sc_tq));
608 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
610 struct ucom_cfg_task *task =
611 (struct ucom_cfg_task *)_task;
612 struct ucom_softc *sc = task->sc;
614 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
617 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
618 /* TTY device closed */
622 if (_task == sc->sc_last_start_xfer)
623 sc->sc_flag |= UCOM_FLAG_GP_DATA;
625 if (sc->sc_callback->ucom_start_read) {
626 (sc->sc_callback->ucom_start_read) (sc);
628 if (sc->sc_callback->ucom_start_write) {
629 (sc->sc_callback->ucom_start_write) (sc);
634 ucom_start_transfers(struct ucom_softc *sc)
636 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
640 * Make sure that data transfers are started in both
643 if (sc->sc_callback->ucom_start_read) {
644 (sc->sc_callback->ucom_start_read) (sc);
646 if (sc->sc_callback->ucom_start_write) {
647 (sc->sc_callback->ucom_start_write) (sc);
652 ucom_cfg_open(struct usb_proc_msg *_task)
654 struct ucom_cfg_task *task =
655 (struct ucom_cfg_task *)_task;
656 struct ucom_softc *sc = task->sc;
660 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
666 sc->sc_flag |= UCOM_FLAG_LL_READY;
668 if (sc->sc_callback->ucom_cfg_open) {
669 (sc->sc_callback->ucom_cfg_open) (sc);
672 usb_pause_mtx(sc->sc_mtx, hz / 10);
678 ucom_open(struct tty *tp)
680 struct ucom_softc *sc = tty_softc(tp);
683 UCOM_MTX_ASSERT(sc, MA_OWNED);
685 if (sc->sc_flag & UCOM_FLAG_GONE) {
688 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
692 DPRINTF("tp = %p\n", tp);
694 if (sc->sc_callback->ucom_pre_open) {
696 * give the lower layer a chance to disallow TTY open, for
697 * example if the device is not present:
699 error = (sc->sc_callback->ucom_pre_open) (sc);
704 sc->sc_flag |= UCOM_FLAG_HL_READY;
706 /* Disable transfers */
707 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
713 /* reset programmed line state */
718 /* reset jitter buffer */
719 sc->sc_jitterbuf_in = 0;
720 sc->sc_jitterbuf_out = 0;
722 ucom_queue_command(sc, ucom_cfg_open, NULL,
723 &sc->sc_open_task[0].hdr,
724 &sc->sc_open_task[1].hdr);
726 /* Queue transfer enable command last */
727 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
728 &sc->sc_start_task[0].hdr,
729 &sc->sc_start_task[1].hdr);
731 ucom_modem(tp, SER_DTR | SER_RTS, 0);
737 ucom_status_change(sc);
743 ucom_cfg_close(struct usb_proc_msg *_task)
745 struct ucom_cfg_task *task =
746 (struct ucom_cfg_task *)_task;
747 struct ucom_softc *sc = task->sc;
751 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
752 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
753 if (sc->sc_callback->ucom_cfg_close)
754 (sc->sc_callback->ucom_cfg_close) (sc);
761 ucom_close(struct tty *tp)
763 struct ucom_softc *sc = tty_softc(tp);
765 UCOM_MTX_ASSERT(sc, MA_OWNED);
767 DPRINTF("tp=%p\n", tp);
769 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
770 DPRINTF("tp=%p already closed\n", tp);
775 ucom_queue_command(sc, ucom_cfg_close, NULL,
776 &sc->sc_close_task[0].hdr,
777 &sc->sc_close_task[1].hdr);
779 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
781 if (sc->sc_callback->ucom_stop_read) {
782 (sc->sc_callback->ucom_stop_read) (sc);
787 ucom_inwakeup(struct tty *tp)
789 struct ucom_softc *sc = tty_softc(tp);
795 UCOM_MTX_ASSERT(sc, MA_OWNED);
797 DPRINTF("tp=%p\n", tp);
799 if (ttydisc_can_bypass(tp) != 0 ||
800 (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
801 (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
805 /* prevent recursion */
806 sc->sc_flag |= UCOM_FLAG_INWAKEUP;
808 pos = sc->sc_jitterbuf_out;
810 while (sc->sc_jitterbuf_in != pos) {
813 c = (char)sc->sc_jitterbuf[pos];
815 if (ttydisc_rint(tp, c, 0) == -1)
818 if (pos >= UCOM_JITTERBUF_SIZE)
819 pos -= UCOM_JITTERBUF_SIZE;
822 sc->sc_jitterbuf_out = pos;
824 /* clear RTS in async fashion */
825 if ((sc->sc_jitterbuf_in == pos) &&
826 (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
829 sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
833 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
835 struct ucom_softc *sc = tty_softc(tp);
838 UCOM_MTX_ASSERT(sc, MA_OWNED);
840 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
843 DPRINTF("cmd = 0x%08lx\n", cmd);
865 if (sc->sc_callback->ucom_ioctl) {
866 error = (sc->sc_callback->ucom_ioctl)
867 (sc, cmd, data, 0, td);
877 ucom_modem(struct tty *tp, int sigon, int sigoff)
879 struct ucom_softc *sc = tty_softc(tp);
882 UCOM_MTX_ASSERT(sc, MA_OWNED);
884 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
887 if ((sigon == 0) && (sigoff == 0)) {
889 if (sc->sc_mcr & SER_DTR) {
892 if (sc->sc_mcr & SER_RTS) {
895 if (sc->sc_msr & SER_CTS) {
898 if (sc->sc_msr & SER_DCD) {
901 if (sc->sc_msr & SER_DSR) {
904 if (sc->sc_msr & SER_RI) {
909 if (sigon & SER_DTR) {
910 sc->sc_mcr |= SER_DTR;
912 if (sigoff & SER_DTR) {
913 sc->sc_mcr &= ~SER_DTR;
915 if (sigon & SER_RTS) {
916 sc->sc_mcr |= SER_RTS;
918 if (sigoff & SER_RTS) {
919 sc->sc_mcr &= ~SER_RTS;
921 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
924 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
931 ucom_cfg_line_state(struct usb_proc_msg *_task)
933 struct ucom_cfg_task *task =
934 (struct ucom_cfg_task *)_task;
935 struct ucom_softc *sc = task->sc;
942 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
947 /* compute callback mask */
948 if (sc->sc_callback->ucom_cfg_set_dtr)
950 if (sc->sc_callback->ucom_cfg_set_rts)
952 if (sc->sc_callback->ucom_cfg_set_break)
953 mask |= UCOM_LS_BREAK;
954 if (sc->sc_callback->ucom_cfg_set_ring)
955 mask |= UCOM_LS_RING;
957 /* compute the bits we are to program */
958 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
959 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
960 prev_value = sc->sc_pls_curr ^ notch_bits;
961 last_value = sc->sc_pls_curr;
963 /* reset programmed line state */
968 /* ensure that we don't lose any levels */
969 if (notch_bits & UCOM_LS_DTR)
970 sc->sc_callback->ucom_cfg_set_dtr(sc,
971 (prev_value & UCOM_LS_DTR) ? 1 : 0);
972 if (notch_bits & UCOM_LS_RTS)
973 sc->sc_callback->ucom_cfg_set_rts(sc,
974 (prev_value & UCOM_LS_RTS) ? 1 : 0);
975 if (notch_bits & UCOM_LS_BREAK)
976 sc->sc_callback->ucom_cfg_set_break(sc,
977 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
978 if (notch_bits & UCOM_LS_RING)
979 sc->sc_callback->ucom_cfg_set_ring(sc,
980 (prev_value & UCOM_LS_RING) ? 1 : 0);
983 if (any_bits & UCOM_LS_DTR)
984 sc->sc_callback->ucom_cfg_set_dtr(sc,
985 (last_value & UCOM_LS_DTR) ? 1 : 0);
986 if (any_bits & UCOM_LS_RTS)
987 sc->sc_callback->ucom_cfg_set_rts(sc,
988 (last_value & UCOM_LS_RTS) ? 1 : 0);
989 if (any_bits & UCOM_LS_BREAK)
990 sc->sc_callback->ucom_cfg_set_break(sc,
991 (last_value & UCOM_LS_BREAK) ? 1 : 0);
992 if (any_bits & UCOM_LS_RING)
993 sc->sc_callback->ucom_cfg_set_ring(sc,
994 (last_value & UCOM_LS_RING) ? 1 : 0);
998 ucom_line_state(struct ucom_softc *sc,
999 uint8_t set_bits, uint8_t clear_bits)
1001 UCOM_MTX_ASSERT(sc, MA_OWNED);
1003 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1007 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1009 /* update current programmed line state */
1010 sc->sc_pls_curr |= set_bits;
1011 sc->sc_pls_curr &= ~clear_bits;
1012 sc->sc_pls_set |= set_bits;
1013 sc->sc_pls_clr |= clear_bits;
1015 /* defer driver programming */
1016 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1017 &sc->sc_line_state_task[0].hdr,
1018 &sc->sc_line_state_task[1].hdr);
1022 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1024 DPRINTF("onoff = %d\n", onoff);
1027 ucom_line_state(sc, UCOM_LS_RING, 0);
1029 ucom_line_state(sc, 0, UCOM_LS_RING);
1033 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1035 DPRINTF("onoff = %d\n", onoff);
1038 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1040 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1044 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1046 DPRINTF("onoff = %d\n", onoff);
1049 ucom_line_state(sc, UCOM_LS_DTR, 0);
1051 ucom_line_state(sc, 0, UCOM_LS_DTR);
1055 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1057 DPRINTF("onoff = %d\n", onoff);
1060 ucom_line_state(sc, UCOM_LS_RTS, 0);
1062 ucom_line_state(sc, 0, UCOM_LS_RTS);
1066 ucom_cfg_status_change(struct usb_proc_msg *_task)
1068 struct ucom_cfg_task *task =
1069 (struct ucom_cfg_task *)_task;
1070 struct ucom_softc *sc = task->sc;
1079 UCOM_MTX_ASSERT(sc, MA_OWNED);
1081 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1084 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1092 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1094 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1095 /* TTY device closed */
1098 onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
1099 lsr_delta = (sc->sc_lsr ^ new_lsr);
1101 sc->sc_msr = new_msr;
1102 sc->sc_lsr = new_lsr;
1106 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1108 DPRINTF("DCD changed to %d\n", onoff);
1110 ttydisc_modem(tp, onoff);
1113 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1115 DPRINTF("BREAK detected\n");
1117 ttydisc_rint(tp, 0, TRE_BREAK);
1118 ttydisc_rint_done(tp);
1121 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1123 DPRINTF("Frame error detected\n");
1125 ttydisc_rint(tp, 0, TRE_FRAMING);
1126 ttydisc_rint_done(tp);
1129 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1131 DPRINTF("Parity error detected\n");
1133 ttydisc_rint(tp, 0, TRE_PARITY);
1134 ttydisc_rint_done(tp);
1139 ucom_status_change(struct ucom_softc *sc)
1141 UCOM_MTX_ASSERT(sc, MA_OWNED);
1143 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1144 return; /* not supported */
1146 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1151 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1152 &sc->sc_status_task[0].hdr,
1153 &sc->sc_status_task[1].hdr);
1157 ucom_cfg_param(struct usb_proc_msg *_task)
1159 struct ucom_param_task *task =
1160 (struct ucom_param_task *)_task;
1161 struct ucom_softc *sc = task->sc;
1163 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1166 if (sc->sc_callback->ucom_cfg_param == NULL) {
1170 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1173 usb_pause_mtx(sc->sc_mtx, hz / 10);
1177 ucom_param(struct tty *tp, struct termios *t)
1179 struct ucom_softc *sc = tty_softc(tp);
1183 UCOM_MTX_ASSERT(sc, MA_OWNED);
1188 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1190 /* XXX the TTY layer should call "open()" first! */
1192 * Not quite: Its ordering is partly backwards, but
1193 * some parameters must be set early in ttydev_open(),
1194 * possibly before calling ttydevsw_open().
1196 error = ucom_open(tp);
1202 DPRINTF("sc = %p\n", sc);
1204 /* Check requested parameters. */
1205 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1206 /* XXX c_ospeed == 0 is perfectly valid. */
1207 DPRINTF("mismatch ispeed and ospeed\n");
1211 t->c_ispeed = t->c_ospeed;
1213 if (sc->sc_callback->ucom_pre_param) {
1214 /* Let the lower layer verify the parameters */
1215 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1217 DPRINTF("callback error = %d\n", error);
1222 /* Disable transfers */
1223 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1225 /* Queue baud rate programming command first */
1226 ucom_queue_command(sc, ucom_cfg_param, t,
1227 &sc->sc_param_task[0].hdr,
1228 &sc->sc_param_task[1].hdr);
1230 /* Queue transfer enable command last */
1231 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1232 &sc->sc_start_task[0].hdr,
1233 &sc->sc_start_task[1].hdr);
1235 if (t->c_cflag & CRTS_IFLOW) {
1236 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1237 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1238 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1239 ucom_modem(tp, SER_RTS, 0);
1251 ucom_outwakeup(struct tty *tp)
1253 struct ucom_softc *sc = tty_softc(tp);
1255 UCOM_MTX_ASSERT(sc, MA_OWNED);
1257 DPRINTF("sc = %p\n", sc);
1259 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1260 /* The higher layer is not ready */
1263 ucom_start_transfers(sc);
1266 /*------------------------------------------------------------------------*
1270 * 0: No data is available.
1271 * Else: Data is available.
1272 *------------------------------------------------------------------------*/
1274 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1275 uint32_t offset, uint32_t len, uint32_t *actlen)
1277 struct usb_page_search res;
1278 struct tty *tp = sc->sc_tty;
1280 uint32_t offset_orig;
1282 UCOM_MTX_ASSERT(sc, MA_OWNED);
1284 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1287 /* get total TX length */
1289 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1290 temp %= UCOM_CONS_BUFSIZE;
1292 /* limit TX length */
1294 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1295 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1302 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1304 /* update counters */
1306 ucom_cons_tx_low += temp;
1307 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1309 /* store actual length */
1313 return (temp ? 1 : 0);
1317 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1319 return (0); /* multiport device polling */
1321 offset_orig = offset;
1325 usbd_get_page(pc, offset, &res);
1327 if (res.length > len) {
1330 /* copy data directly into USB buffer */
1331 cnt = ttydisc_getc(tp, res.buffer, res.length);
1336 if (cnt < res.length) {
1342 actlen[0] = offset - offset_orig;
1344 DPRINTF("cnt=%d\n", actlen[0]);
1346 if (actlen[0] == 0) {
1353 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1354 uint32_t offset, uint32_t len)
1356 struct usb_page_search res;
1357 struct tty *tp = sc->sc_tty;
1361 UCOM_MTX_ASSERT(sc, MA_OWNED);
1363 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1366 /* get maximum RX length */
1368 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1369 temp %= UCOM_CONS_BUFSIZE;
1371 /* limit RX length */
1373 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1374 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1381 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1383 /* update counters */
1385 ucom_cons_rx_high += temp;
1386 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1392 return; /* multiport device polling */
1395 return; /* no data */
1397 /* set a flag to prevent recursation ? */
1401 usbd_get_page(pc, offset, &res);
1403 if (res.length > len) {
1407 offset += res.length;
1409 /* pass characters to tty layer */
1414 /* first check if we can pass the buffer directly */
1416 if (ttydisc_can_bypass(tp)) {
1418 /* clear any jitter buffer */
1419 sc->sc_jitterbuf_in = 0;
1420 sc->sc_jitterbuf_out = 0;
1422 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1423 DPRINTF("tp=%p, data lost\n", tp);
1429 for (cnt = 0; cnt != res.length; cnt++) {
1430 if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1431 ttydisc_rint(tp, buf[cnt], 0) == -1) {
1435 pos = sc->sc_jitterbuf_in;
1436 end = sc->sc_jitterbuf_out +
1437 UCOM_JITTERBUF_SIZE - 1;
1438 if (end >= UCOM_JITTERBUF_SIZE)
1439 end -= UCOM_JITTERBUF_SIZE;
1441 for (; cnt != res.length; cnt++) {
1444 sc->sc_jitterbuf[pos] = buf[cnt];
1446 if (pos >= UCOM_JITTERBUF_SIZE)
1447 pos -= UCOM_JITTERBUF_SIZE;
1450 sc->sc_jitterbuf_in = pos;
1452 /* set RTS in async fashion */
1453 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1456 DPRINTF("tp=%p, lost %d "
1457 "chars\n", tp, res.length - cnt);
1462 ttydisc_rint_done(tp);
1466 ucom_free(void *xsc)
1468 struct ucom_softc *sc = xsc;
1470 if (sc->sc_callback->ucom_free != NULL)
1471 sc->sc_callback->ucom_free(sc);
1473 ucom_unref(sc->sc_super);
1475 mtx_lock(&ucom_mtx);
1477 mtx_unlock(&ucom_mtx);
1480 static cn_probe_t ucom_cnprobe;
1481 static cn_init_t ucom_cninit;
1482 static cn_term_t ucom_cnterm;
1483 static cn_getc_t ucom_cngetc;
1484 static cn_putc_t ucom_cnputc;
1485 static cn_grab_t ucom_cngrab;
1486 static cn_ungrab_t ucom_cnungrab;
1488 CONSOLE_DRIVER(ucom);
1491 ucom_cnprobe(struct consdev *cp)
1493 if (ucom_cons_unit != -1)
1494 cp->cn_pri = CN_NORMAL;
1496 cp->cn_pri = CN_DEAD;
1498 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1502 ucom_cninit(struct consdev *cp)
1507 ucom_cnterm(struct consdev *cp)
1512 ucom_cngrab(struct consdev *cp)
1517 ucom_cnungrab(struct consdev *cp)
1522 ucom_cngetc(struct consdev *cd)
1524 struct ucom_softc *sc = ucom_cons_softc;
1532 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1533 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1534 ucom_cons_rx_low ++;
1535 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1540 /* start USB transfers */
1541 ucom_outwakeup(sc->sc_tty);
1543 UCOM_MTX_UNLOCK(sc);
1545 /* poll if necessary */
1546 if (kdb_active && sc->sc_callback->ucom_poll)
1547 (sc->sc_callback->ucom_poll) (sc);
1553 ucom_cnputc(struct consdev *cd, int c)
1555 struct ucom_softc *sc = ucom_cons_softc;
1565 /* compute maximum TX length */
1567 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1568 temp %= UCOM_CONS_BUFSIZE;
1571 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1572 ucom_cons_tx_high ++;
1573 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1576 /* start USB transfers */
1577 ucom_outwakeup(sc->sc_tty);
1579 UCOM_MTX_UNLOCK(sc);
1581 /* poll if necessary */
1582 if (kdb_active && sc->sc_callback->ucom_poll) {
1583 (sc->sc_callback->ucom_poll) (sc);
1584 /* simple flow control */
1590 /*------------------------------------------------------------------------*
1593 * This function will increment the super UCOM reference count.
1594 *------------------------------------------------------------------------*/
1596 ucom_ref(struct ucom_super_softc *ssc)
1598 mtx_lock(&ucom_mtx);
1600 mtx_unlock(&ucom_mtx);
1603 /*------------------------------------------------------------------------*
1606 * This function will free the super UCOM's allocated unit
1607 * number. This function can be called on a zero-initialized
1608 * structure. This function can be called multiple times.
1609 *------------------------------------------------------------------------*/
1611 ucom_free_unit(struct ucom_super_softc *ssc)
1613 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1616 ucom_unit_free(ssc->sc_unit);
1618 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1621 /*------------------------------------------------------------------------*
1624 * This function will decrement the super UCOM reference count.
1627 * 0: UCOM structures are still referenced.
1628 * Else: UCOM structures are no longer referenced.
1629 *------------------------------------------------------------------------*/
1631 ucom_unref(struct ucom_super_softc *ssc)
1635 mtx_lock(&ucom_mtx);
1636 retval = (ssc->sc_refs < 2);
1638 mtx_unlock(&ucom_mtx);
1641 ucom_free_unit(ssc);
1648 #include <gdb/gdb.h>
1650 static gdb_probe_f ucom_gdbprobe;
1651 static gdb_init_f ucom_gdbinit;
1652 static gdb_term_f ucom_gdbterm;
1653 static gdb_getc_f ucom_gdbgetc;
1654 static gdb_putc_f ucom_gdbputc;
1656 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1661 return ((ucom_cons_softc != NULL) ? 0 : -1);
1677 ucom_cnputc(NULL, c);
1683 return (ucom_cngetc(NULL));