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 | 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_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 /* maximum number of units */
182 #define UCOM_TTY_PREFIX "U"
184 static struct unrhdr *ucom_unrhdr;
185 static struct mtx ucom_mtx;
186 static int ucom_close_refs;
192 ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
193 mtx_init(&ucom_mtx, "UCOM MTX", NULL, MTX_DEF);
195 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
198 ucom_uninit(void *arg)
209 mtx_destroy(&ucom_mtx);
211 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_uninit, NULL);
214 * Mark a unit number (the X in cuaUX) as in use.
216 * Note that devices using a different naming scheme (see ucom_tty_name()
217 * callback) still use this unit allocation.
220 ucom_unit_alloc(void)
225 if (ucom_unrhdr == NULL) {
226 DPRINTF("ucom_unrhdr is NULL\n");
229 unit = alloc_unr(ucom_unrhdr);
230 DPRINTF("unit %d is allocated\n", unit);
235 * Mark the unit number as not in use.
238 ucom_unit_free(int unit)
241 if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
242 DPRINTF("cannot free unit number\n");
245 DPRINTF("unit %d is freed\n", unit);
246 free_unr(ucom_unrhdr, unit);
250 * Setup a group of one or more serial ports.
252 * The mutex pointed to by "mtx" is applied before all
253 * callbacks are called back. Also "mtx" must be applied
254 * before calling into the ucom-layer!
257 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
258 int subunits, void *parent,
259 const struct ucom_callback *callback, struct mtx *mtx)
266 (callback == NULL) ||
271 /* allocate a uniq unit number */
272 ssc->sc_unit = ucom_unit_alloc();
273 if (ssc->sc_unit == -1)
276 /* generate TTY name string */
277 snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
278 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
280 /* create USB request handling process */
281 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
283 ucom_unit_free(ssc->sc_unit);
286 ssc->sc_subunits = subunits;
287 ssc->sc_flag = UCOM_FLAG_ATTACHED |
290 if (callback->ucom_free == NULL)
291 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
293 /* increment reference count */
296 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
297 sc[subunit].sc_subunit = subunit;
298 sc[subunit].sc_super = ssc;
299 sc[subunit].sc_mtx = mtx;
300 sc[subunit].sc_parent = parent;
301 sc[subunit].sc_callback = callback;
303 error = ucom_attach_tty(ssc, &sc[subunit]);
305 ucom_detach(ssc, &sc[0]);
308 /* increment reference count */
311 /* set subunit attached */
312 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
315 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
316 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
322 * The following function will do nothing if the structure pointed to
323 * by "ssc" and "sc" is zero or has already been detached.
326 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
330 if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
331 return; /* not initialized */
333 if (ssc->sc_sysctl_ttyname != NULL) {
334 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
335 ssc->sc_sysctl_ttyname = NULL;
338 if (ssc->sc_sysctl_ttyports != NULL) {
339 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
340 ssc->sc_sysctl_ttyports = NULL;
343 usb_proc_drain(&ssc->sc_tq);
345 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
346 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
348 ucom_detach_tty(ssc, &sc[subunit]);
350 /* avoid duplicate detach */
351 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
354 usb_proc_free(&ssc->sc_tq);
358 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
361 /* make sure we don't detach twice */
362 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
366 ucom_drain(struct ucom_super_softc *ssc)
369 while (ssc->sc_refs > 0) {
370 printf("ucom: Waiting for a TTY device to close.\n");
371 usb_pause_mtx(&ucom_mtx, hz);
373 mtx_unlock(&ucom_mtx);
377 ucom_drain_all(void *arg)
380 while (ucom_close_refs > 0) {
381 printf("ucom: Waiting for all detached TTY "
382 "devices to have open fds closed.\n");
383 usb_pause_mtx(&ucom_mtx, hz);
385 mtx_unlock(&ucom_mtx);
389 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
392 char buf[32]; /* temporary TTY device name buffer */
394 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
398 /* Check if the client has a custom TTY name */
400 if (sc->sc_callback->ucom_tty_name) {
401 sc->sc_callback->ucom_tty_name(sc, buf,
402 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
405 /* Use default TTY name */
406 if (ssc->sc_subunits > 1) {
407 /* multiple modems in one */
408 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
409 ssc->sc_unit, sc->sc_subunit);
412 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
416 tty_makedev(tp, NULL, "%s", buf);
420 DPRINTF("ttycreate: %s\n", buf);
422 /* Check if this device should be a console */
423 if ((ucom_cons_softc == NULL) &&
424 (ssc->sc_unit == ucom_cons_unit) &&
425 (sc->sc_subunit == ucom_cons_subunit)) {
428 DPRINTF("unit %d subunit %d is console", ssc->sc_unit, sc->sc_subunit);
430 ucom_cons_softc = sc;
432 memset(&t, 0, sizeof(t));
433 t.c_ispeed = ucom_cons_baud;
434 t.c_ospeed = t.c_ispeed;
437 UCOM_MTX_LOCK(ucom_cons_softc);
438 ucom_cons_rx_low = 0;
439 ucom_cons_rx_high = 0;
440 ucom_cons_tx_low = 0;
441 ucom_cons_tx_high = 0;
442 sc->sc_flag |= UCOM_FLAG_CONSOLE;
443 ucom_open(ucom_cons_softc->sc_tty);
444 ucom_param(ucom_cons_softc->sc_tty, &t);
445 UCOM_MTX_UNLOCK(ucom_cons_softc);
452 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
454 struct tty *tp = sc->sc_tty;
456 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
458 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
459 UCOM_MTX_LOCK(ucom_cons_softc);
460 ucom_close(ucom_cons_softc->sc_tty);
461 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
462 UCOM_MTX_UNLOCK(ucom_cons_softc);
463 ucom_cons_softc = NULL;
466 /* the config thread has been stopped when we get here */
469 sc->sc_flag |= UCOM_FLAG_GONE;
470 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
476 mtx_unlock(&ucom_mtx);
480 ucom_close(tp); /* close, if any */
486 * make sure that read and write transfers are stopped
488 if (sc->sc_callback->ucom_stop_read)
489 (sc->sc_callback->ucom_stop_read) (sc);
490 if (sc->sc_callback->ucom_stop_write)
491 (sc->sc_callback->ucom_stop_write) (sc);
497 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
501 struct usb_attach_arg *uaa;
503 snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
504 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
506 /* Store the PNP info in the first interface for the device */
507 uaa = device_get_ivars(dev);
508 iface_index = uaa->info.bIfaceIndex;
510 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
511 device_printf(dev, "Could not set PNP info\n");
514 * The following information is also replicated in the PNP-info
515 * string which is registered above:
517 if (ssc->sc_sysctl_ttyname == NULL) {
518 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
519 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
520 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
521 "TTY device basename");
523 if (ssc->sc_sysctl_ttyports == NULL) {
524 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
525 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
526 OID_AUTO, "ttyports", CTLFLAG_RD,
527 NULL, ssc->sc_subunits, "Number of ports");
532 ucom_queue_command(struct ucom_softc *sc,
533 usb_proc_callback_t *fn, struct termios *pt,
534 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
536 struct ucom_super_softc *ssc = sc->sc_super;
537 struct ucom_param_task *task;
539 UCOM_MTX_ASSERT(sc, MA_OWNED);
541 if (usb_proc_is_gone(&ssc->sc_tq)) {
542 DPRINTF("proc is gone\n");
543 return; /* nothing to do */
546 * NOTE: The task cannot get executed before we drop the
547 * "sc_mtx" mutex. It is safe to update fields in the message
548 * structure after that the message got queued.
550 task = (struct ucom_param_task *)
551 usb_proc_msignal(&ssc->sc_tq, t0, t1);
553 /* Setup callback and softc pointers */
554 task->hdr.pm_callback = fn;
558 * Make a copy of the termios. This field is only present if
559 * the "pt" field is not NULL.
562 task->termios_copy = *pt;
565 * Closing the device should be synchronous.
567 if (fn == ucom_cfg_close)
568 usb_proc_mwait(&ssc->sc_tq, t0, t1);
571 * In case of multiple configure requests,
572 * keep track of the last one!
574 if (fn == ucom_cfg_start_transfers)
575 sc->sc_last_start_xfer = &task->hdr;
579 ucom_shutdown(struct ucom_softc *sc)
581 struct tty *tp = sc->sc_tty;
583 UCOM_MTX_ASSERT(sc, MA_OWNED);
588 * Hang up if necessary:
590 if (tp->t_termios.c_cflag & HUPCL) {
591 ucom_modem(tp, 0, SER_DTR);
598 * else: taskqueue is draining or gone
601 ucom_cfg_is_gone(struct ucom_softc *sc)
603 struct ucom_super_softc *ssc = sc->sc_super;
605 return (usb_proc_is_gone(&ssc->sc_tq));
609 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
611 struct ucom_cfg_task *task =
612 (struct ucom_cfg_task *)_task;
613 struct ucom_softc *sc = task->sc;
615 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
618 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
619 /* TTY device closed */
623 if (_task == sc->sc_last_start_xfer)
624 sc->sc_flag |= UCOM_FLAG_GP_DATA;
626 if (sc->sc_callback->ucom_start_read) {
627 (sc->sc_callback->ucom_start_read) (sc);
629 if (sc->sc_callback->ucom_start_write) {
630 (sc->sc_callback->ucom_start_write) (sc);
635 ucom_start_transfers(struct ucom_softc *sc)
637 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
641 * Make sure that data transfers are started in both
644 if (sc->sc_callback->ucom_start_read) {
645 (sc->sc_callback->ucom_start_read) (sc);
647 if (sc->sc_callback->ucom_start_write) {
648 (sc->sc_callback->ucom_start_write) (sc);
653 ucom_cfg_open(struct usb_proc_msg *_task)
655 struct ucom_cfg_task *task =
656 (struct ucom_cfg_task *)_task;
657 struct ucom_softc *sc = task->sc;
661 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
667 sc->sc_flag |= UCOM_FLAG_LL_READY;
669 if (sc->sc_callback->ucom_cfg_open) {
670 (sc->sc_callback->ucom_cfg_open) (sc);
673 usb_pause_mtx(sc->sc_mtx, hz / 10);
679 ucom_open(struct tty *tp)
681 struct ucom_softc *sc = tty_softc(tp);
684 UCOM_MTX_ASSERT(sc, MA_OWNED);
686 if (sc->sc_flag & UCOM_FLAG_GONE) {
689 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
693 DPRINTF("tp = %p\n", tp);
695 if (sc->sc_callback->ucom_pre_open) {
697 * give the lower layer a chance to disallow TTY open, for
698 * example if the device is not present:
700 error = (sc->sc_callback->ucom_pre_open) (sc);
705 sc->sc_flag |= UCOM_FLAG_HL_READY;
707 /* Disable transfers */
708 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
714 /* reset programmed line state */
719 ucom_queue_command(sc, ucom_cfg_open, NULL,
720 &sc->sc_open_task[0].hdr,
721 &sc->sc_open_task[1].hdr);
723 /* Queue transfer enable command last */
724 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
725 &sc->sc_start_task[0].hdr,
726 &sc->sc_start_task[1].hdr);
728 ucom_modem(tp, SER_DTR | SER_RTS, 0);
734 ucom_status_change(sc);
740 ucom_cfg_close(struct usb_proc_msg *_task)
742 struct ucom_cfg_task *task =
743 (struct ucom_cfg_task *)_task;
744 struct ucom_softc *sc = task->sc;
748 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
749 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
750 if (sc->sc_callback->ucom_cfg_close)
751 (sc->sc_callback->ucom_cfg_close) (sc);
758 ucom_close(struct tty *tp)
760 struct ucom_softc *sc = tty_softc(tp);
762 UCOM_MTX_ASSERT(sc, MA_OWNED);
764 DPRINTF("tp=%p\n", tp);
766 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
767 DPRINTF("tp=%p already closed\n", tp);
772 ucom_queue_command(sc, ucom_cfg_close, NULL,
773 &sc->sc_close_task[0].hdr,
774 &sc->sc_close_task[1].hdr);
776 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
778 if (sc->sc_callback->ucom_stop_read) {
779 (sc->sc_callback->ucom_stop_read) (sc);
784 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
786 struct ucom_softc *sc = tty_softc(tp);
789 UCOM_MTX_ASSERT(sc, MA_OWNED);
791 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
794 DPRINTF("cmd = 0x%08lx\n", cmd);
816 if (sc->sc_callback->ucom_ioctl) {
817 error = (sc->sc_callback->ucom_ioctl)
818 (sc, cmd, data, 0, td);
828 ucom_modem(struct tty *tp, int sigon, int sigoff)
830 struct ucom_softc *sc = tty_softc(tp);
833 UCOM_MTX_ASSERT(sc, MA_OWNED);
835 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
838 if ((sigon == 0) && (sigoff == 0)) {
840 if (sc->sc_mcr & SER_DTR) {
843 if (sc->sc_mcr & SER_RTS) {
846 if (sc->sc_msr & SER_CTS) {
849 if (sc->sc_msr & SER_DCD) {
852 if (sc->sc_msr & SER_DSR) {
855 if (sc->sc_msr & SER_RI) {
860 if (sigon & SER_DTR) {
861 sc->sc_mcr |= SER_DTR;
863 if (sigoff & SER_DTR) {
864 sc->sc_mcr &= ~SER_DTR;
866 if (sigon & SER_RTS) {
867 sc->sc_mcr |= SER_RTS;
869 if (sigoff & SER_RTS) {
870 sc->sc_mcr &= ~SER_RTS;
872 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
875 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
882 ucom_cfg_line_state(struct usb_proc_msg *_task)
884 struct ucom_cfg_task *task =
885 (struct ucom_cfg_task *)_task;
886 struct ucom_softc *sc = task->sc;
893 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
898 /* compute callback mask */
899 if (sc->sc_callback->ucom_cfg_set_dtr)
901 if (sc->sc_callback->ucom_cfg_set_rts)
903 if (sc->sc_callback->ucom_cfg_set_break)
904 mask |= UCOM_LS_BREAK;
905 if (sc->sc_callback->ucom_cfg_set_ring)
906 mask |= UCOM_LS_RING;
908 /* compute the bits we are to program */
909 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
910 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
911 prev_value = sc->sc_pls_curr ^ notch_bits;
912 last_value = sc->sc_pls_curr;
914 /* reset programmed line state */
919 /* ensure that we don't loose any levels */
920 if (notch_bits & UCOM_LS_DTR)
921 sc->sc_callback->ucom_cfg_set_dtr(sc,
922 (prev_value & UCOM_LS_DTR) ? 1 : 0);
923 if (notch_bits & UCOM_LS_RTS)
924 sc->sc_callback->ucom_cfg_set_rts(sc,
925 (prev_value & UCOM_LS_RTS) ? 1 : 0);
926 if (notch_bits & UCOM_LS_BREAK)
927 sc->sc_callback->ucom_cfg_set_break(sc,
928 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
929 if (notch_bits & UCOM_LS_RING)
930 sc->sc_callback->ucom_cfg_set_ring(sc,
931 (prev_value & UCOM_LS_RING) ? 1 : 0);
934 if (any_bits & UCOM_LS_DTR)
935 sc->sc_callback->ucom_cfg_set_dtr(sc,
936 (last_value & UCOM_LS_DTR) ? 1 : 0);
937 if (any_bits & UCOM_LS_RTS)
938 sc->sc_callback->ucom_cfg_set_rts(sc,
939 (last_value & UCOM_LS_RTS) ? 1 : 0);
940 if (any_bits & UCOM_LS_BREAK)
941 sc->sc_callback->ucom_cfg_set_break(sc,
942 (last_value & UCOM_LS_BREAK) ? 1 : 0);
943 if (any_bits & UCOM_LS_RING)
944 sc->sc_callback->ucom_cfg_set_ring(sc,
945 (last_value & UCOM_LS_RING) ? 1 : 0);
949 ucom_line_state(struct ucom_softc *sc,
950 uint8_t set_bits, uint8_t clear_bits)
952 UCOM_MTX_ASSERT(sc, MA_OWNED);
954 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
958 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
960 /* update current programmed line state */
961 sc->sc_pls_curr |= set_bits;
962 sc->sc_pls_curr &= ~clear_bits;
963 sc->sc_pls_set |= set_bits;
964 sc->sc_pls_clr |= clear_bits;
966 /* defer driver programming */
967 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
968 &sc->sc_line_state_task[0].hdr,
969 &sc->sc_line_state_task[1].hdr);
973 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
975 DPRINTF("onoff = %d\n", onoff);
978 ucom_line_state(sc, UCOM_LS_RING, 0);
980 ucom_line_state(sc, 0, UCOM_LS_RING);
984 ucom_break(struct ucom_softc *sc, uint8_t onoff)
986 DPRINTF("onoff = %d\n", onoff);
989 ucom_line_state(sc, UCOM_LS_BREAK, 0);
991 ucom_line_state(sc, 0, UCOM_LS_BREAK);
995 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
997 DPRINTF("onoff = %d\n", onoff);
1000 ucom_line_state(sc, UCOM_LS_DTR, 0);
1002 ucom_line_state(sc, 0, UCOM_LS_DTR);
1006 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1008 DPRINTF("onoff = %d\n", onoff);
1011 ucom_line_state(sc, UCOM_LS_RTS, 0);
1013 ucom_line_state(sc, 0, UCOM_LS_RTS);
1017 ucom_cfg_status_change(struct usb_proc_msg *_task)
1019 struct ucom_cfg_task *task =
1020 (struct ucom_cfg_task *)_task;
1021 struct ucom_softc *sc = task->sc;
1030 UCOM_MTX_ASSERT(sc, MA_OWNED);
1032 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1035 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1043 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1045 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1046 /* TTY device closed */
1049 onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
1050 lsr_delta = (sc->sc_lsr ^ new_lsr);
1052 sc->sc_msr = new_msr;
1053 sc->sc_lsr = new_lsr;
1057 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1059 DPRINTF("DCD changed to %d\n", onoff);
1061 ttydisc_modem(tp, onoff);
1064 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1066 DPRINTF("BREAK detected\n");
1068 ttydisc_rint(tp, 0, TRE_BREAK);
1069 ttydisc_rint_done(tp);
1072 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1074 DPRINTF("Frame error detected\n");
1076 ttydisc_rint(tp, 0, TRE_FRAMING);
1077 ttydisc_rint_done(tp);
1080 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1082 DPRINTF("Parity error detected\n");
1084 ttydisc_rint(tp, 0, TRE_PARITY);
1085 ttydisc_rint_done(tp);
1090 ucom_status_change(struct ucom_softc *sc)
1092 UCOM_MTX_ASSERT(sc, MA_OWNED);
1094 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1095 return; /* not supported */
1097 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1102 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1103 &sc->sc_status_task[0].hdr,
1104 &sc->sc_status_task[1].hdr);
1108 ucom_cfg_param(struct usb_proc_msg *_task)
1110 struct ucom_param_task *task =
1111 (struct ucom_param_task *)_task;
1112 struct ucom_softc *sc = task->sc;
1114 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1117 if (sc->sc_callback->ucom_cfg_param == NULL) {
1121 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1124 usb_pause_mtx(sc->sc_mtx, hz / 10);
1128 ucom_param(struct tty *tp, struct termios *t)
1130 struct ucom_softc *sc = tty_softc(tp);
1134 UCOM_MTX_ASSERT(sc, MA_OWNED);
1139 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1141 /* XXX the TTY layer should call "open()" first! */
1143 error = ucom_open(tp);
1149 DPRINTF("sc = %p\n", sc);
1151 /* Check requested parameters. */
1152 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1153 DPRINTF("mismatch ispeed and ospeed\n");
1157 t->c_ispeed = t->c_ospeed;
1159 if (sc->sc_callback->ucom_pre_param) {
1160 /* Let the lower layer verify the parameters */
1161 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1163 DPRINTF("callback error = %d\n", error);
1168 /* Disable transfers */
1169 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1171 /* Queue baud rate programming command first */
1172 ucom_queue_command(sc, ucom_cfg_param, t,
1173 &sc->sc_param_task[0].hdr,
1174 &sc->sc_param_task[1].hdr);
1176 /* Queue transfer enable command last */
1177 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1178 &sc->sc_start_task[0].hdr,
1179 &sc->sc_start_task[1].hdr);
1181 if (t->c_cflag & CRTS_IFLOW) {
1182 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1183 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1184 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1185 ucom_modem(tp, SER_RTS, 0);
1197 ucom_outwakeup(struct tty *tp)
1199 struct ucom_softc *sc = tty_softc(tp);
1201 UCOM_MTX_ASSERT(sc, MA_OWNED);
1203 DPRINTF("sc = %p\n", sc);
1205 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1206 /* The higher layer is not ready */
1209 ucom_start_transfers(sc);
1212 /*------------------------------------------------------------------------*
1216 * 0: No data is available.
1217 * Else: Data is available.
1218 *------------------------------------------------------------------------*/
1220 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1221 uint32_t offset, uint32_t len, uint32_t *actlen)
1223 struct usb_page_search res;
1224 struct tty *tp = sc->sc_tty;
1226 uint32_t offset_orig;
1228 UCOM_MTX_ASSERT(sc, MA_OWNED);
1230 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1233 /* get total TX length */
1235 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1236 temp %= UCOM_CONS_BUFSIZE;
1238 /* limit TX length */
1240 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1241 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1248 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1250 /* update counters */
1252 ucom_cons_tx_low += temp;
1253 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1255 /* store actual length */
1259 return (temp ? 1 : 0);
1263 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1265 return (0); /* multiport device polling */
1267 offset_orig = offset;
1271 usbd_get_page(pc, offset, &res);
1273 if (res.length > len) {
1276 /* copy data directly into USB buffer */
1277 cnt = ttydisc_getc(tp, res.buffer, res.length);
1282 if (cnt < res.length) {
1288 actlen[0] = offset - offset_orig;
1290 DPRINTF("cnt=%d\n", actlen[0]);
1292 if (actlen[0] == 0) {
1299 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1300 uint32_t offset, uint32_t len)
1302 struct usb_page_search res;
1303 struct tty *tp = sc->sc_tty;
1307 UCOM_MTX_ASSERT(sc, MA_OWNED);
1309 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1312 /* get maximum RX length */
1314 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1315 temp %= UCOM_CONS_BUFSIZE;
1317 /* limit RX length */
1319 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1320 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1327 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1329 /* update counters */
1331 ucom_cons_rx_high += temp;
1332 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1338 return; /* multiport device polling */
1341 return; /* no data */
1343 /* set a flag to prevent recursation ? */
1347 usbd_get_page(pc, offset, &res);
1349 if (res.length > len) {
1353 offset += res.length;
1355 /* pass characters to tty layer */
1360 /* first check if we can pass the buffer directly */
1362 if (ttydisc_can_bypass(tp)) {
1363 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1364 DPRINTF("tp=%p, data lost\n", tp);
1370 for (cnt = 0; cnt != res.length; cnt++) {
1371 if (ttydisc_rint(tp, buf[cnt], 0) == -1) {
1372 /* XXX what should we do? */
1374 DPRINTF("tp=%p, lost %d "
1375 "chars\n", tp, res.length - cnt);
1380 ttydisc_rint_done(tp);
1384 ucom_free(void *xsc)
1386 struct ucom_softc *sc = xsc;
1388 if (sc->sc_callback->ucom_free != NULL)
1389 sc->sc_callback->ucom_free(sc);
1391 ucom_unref(sc->sc_super);
1393 mtx_lock(&ucom_mtx);
1395 mtx_unlock(&ucom_mtx);
1398 static cn_probe_t ucom_cnprobe;
1399 static cn_init_t ucom_cninit;
1400 static cn_term_t ucom_cnterm;
1401 static cn_getc_t ucom_cngetc;
1402 static cn_putc_t ucom_cnputc;
1403 static cn_grab_t ucom_cngrab;
1404 static cn_ungrab_t ucom_cnungrab;
1406 CONSOLE_DRIVER(ucom);
1409 ucom_cnprobe(struct consdev *cp)
1411 if (ucom_cons_unit != -1)
1412 cp->cn_pri = CN_NORMAL;
1414 cp->cn_pri = CN_DEAD;
1416 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1420 ucom_cninit(struct consdev *cp)
1425 ucom_cnterm(struct consdev *cp)
1430 ucom_cngrab(struct consdev *cp)
1435 ucom_cnungrab(struct consdev *cp)
1440 ucom_cngetc(struct consdev *cd)
1442 struct ucom_softc *sc = ucom_cons_softc;
1450 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1451 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1452 ucom_cons_rx_low ++;
1453 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1458 /* start USB transfers */
1459 ucom_outwakeup(sc->sc_tty);
1461 UCOM_MTX_UNLOCK(sc);
1463 /* poll if necessary */
1464 if (kdb_active && sc->sc_callback->ucom_poll)
1465 (sc->sc_callback->ucom_poll) (sc);
1471 ucom_cnputc(struct consdev *cd, int c)
1473 struct ucom_softc *sc = ucom_cons_softc;
1483 /* compute maximum TX length */
1485 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1486 temp %= UCOM_CONS_BUFSIZE;
1489 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1490 ucom_cons_tx_high ++;
1491 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1494 /* start USB transfers */
1495 ucom_outwakeup(sc->sc_tty);
1497 UCOM_MTX_UNLOCK(sc);
1499 /* poll if necessary */
1500 if (kdb_active && sc->sc_callback->ucom_poll) {
1501 (sc->sc_callback->ucom_poll) (sc);
1502 /* simple flow control */
1508 /*------------------------------------------------------------------------*
1511 * This function will increment the super UCOM reference count.
1512 *------------------------------------------------------------------------*/
1514 ucom_ref(struct ucom_super_softc *ssc)
1516 mtx_lock(&ucom_mtx);
1518 mtx_unlock(&ucom_mtx);
1521 /*------------------------------------------------------------------------*
1524 * This function will free the super UCOM's allocated unit
1525 * number. This function can be called on a zero-initialized
1526 * structure. This function can be called multiple times.
1527 *------------------------------------------------------------------------*/
1529 ucom_free_unit(struct ucom_super_softc *ssc)
1531 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1534 ucom_unit_free(ssc->sc_unit);
1536 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1539 /*------------------------------------------------------------------------*
1542 * This function will decrement the super UCOM reference count.
1545 * 0: UCOM structures are still referenced.
1546 * Else: UCOM structures are no longer referenced.
1547 *------------------------------------------------------------------------*/
1549 ucom_unref(struct ucom_super_softc *ssc)
1553 mtx_lock(&ucom_mtx);
1554 retval = (ssc->sc_refs < 2);
1556 mtx_unlock(&ucom_mtx);
1559 ucom_free_unit(ssc);
1566 #include <gdb/gdb.h>
1568 static gdb_probe_f ucom_gdbprobe;
1569 static gdb_init_f ucom_gdbinit;
1570 static gdb_term_f ucom_gdbterm;
1571 static gdb_getc_f ucom_gdbgetc;
1572 static gdb_putc_f ucom_gdbputc;
1574 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1579 return ((ucom_cons_softc != NULL) ? 0 : -1);
1595 ucom_cnputc(NULL, c);
1601 return (ucom_cngetc(NULL));