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.
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
63 #include <sys/stdint.h>
64 #include <sys/stddef.h>
65 #include <sys/param.h>
66 #include <sys/queue.h>
67 #include <sys/types.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
71 #include <sys/module.h>
73 #include <sys/mutex.h>
74 #include <sys/condvar.h>
75 #include <sys/sysctl.h>
77 #include <sys/unistd.h>
78 #include <sys/callout.h>
79 #include <sys/malloc.h>
83 #include <dev/uart/uart_ppstypes.h>
85 #include <dev/usb/usb.h>
86 #include <dev/usb/usbdi.h>
87 #include <dev/usb/usbdi_util.h>
89 #define USB_DEBUG_VAR ucom_debug
90 #include <dev/usb/usb_debug.h>
91 #include <dev/usb/usb_busdma.h>
92 #include <dev/usb/usb_process.h>
94 #include <dev/usb/serial/usb_serial.h>
98 static SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
100 static int ucom_pps_mode;
102 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, pps_mode, CTLFLAG_RWTUN,
104 "pulse capture mode: 0/1/2=disabled/CTS/DCD; add 0x10 to invert");
107 static int ucom_debug = 0;
109 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RWTUN,
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 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_unit, CTLFLAG_RWTUN,
130 &ucom_cons_unit, 0, "console unit number");
131 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_subunit, CTLFLAG_RWTUN,
132 &ucom_cons_subunit, 0, "console subunit number");
133 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RWTUN,
134 &ucom_cons_baud, 0, "console baud rate");
136 static usb_proc_callback_t ucom_cfg_start_transfers;
137 static usb_proc_callback_t ucom_cfg_open;
138 static usb_proc_callback_t ucom_cfg_close;
139 static usb_proc_callback_t ucom_cfg_line_state;
140 static usb_proc_callback_t ucom_cfg_status_change;
141 static usb_proc_callback_t ucom_cfg_param;
143 static int ucom_unit_alloc(void);
144 static void ucom_unit_free(int);
145 static int ucom_attach_tty(struct ucom_super_softc *, struct ucom_softc *);
146 static void ucom_detach_tty(struct ucom_super_softc *, struct ucom_softc *);
147 static void ucom_queue_command(struct ucom_softc *,
148 usb_proc_callback_t *, struct termios *pt,
149 struct usb_proc_msg *t0, struct usb_proc_msg *t1);
150 static void ucom_shutdown(struct ucom_softc *);
151 static void ucom_ring(struct ucom_softc *, uint8_t);
152 static void ucom_break(struct ucom_softc *, uint8_t);
153 static void ucom_dtr(struct ucom_softc *, uint8_t);
154 static void ucom_rts(struct ucom_softc *, uint8_t);
156 static tsw_open_t ucom_open;
157 static tsw_close_t ucom_close;
158 static tsw_ioctl_t ucom_ioctl;
159 static tsw_modem_t ucom_modem;
160 static tsw_param_t ucom_param;
161 static tsw_outwakeup_t ucom_outwakeup;
162 static tsw_inwakeup_t ucom_inwakeup;
163 static tsw_free_t ucom_free;
164 static tsw_busy_t ucom_busy;
166 static struct ttydevsw ucom_class = {
167 .tsw_flags = TF_INITLOCK | TF_CALLOUT,
168 .tsw_open = ucom_open,
169 .tsw_close = ucom_close,
170 .tsw_outwakeup = ucom_outwakeup,
171 .tsw_inwakeup = ucom_inwakeup,
172 .tsw_ioctl = ucom_ioctl,
173 .tsw_param = ucom_param,
174 .tsw_modem = ucom_modem,
175 .tsw_free = ucom_free,
176 .tsw_busy = ucom_busy,
179 MODULE_DEPEND(ucom, usb, 1, 1, 1);
180 MODULE_VERSION(ucom, 1);
182 #define UCOM_UNIT_MAX 128 /* maximum number of units */
183 #define UCOM_TTY_PREFIX "U"
185 static struct unrhdr *ucom_unrhdr;
186 static struct mtx ucom_mtx;
187 static int ucom_close_refs;
193 ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
194 mtx_init(&ucom_mtx, "UCOM MTX", NULL, MTX_DEF);
196 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
199 ucom_uninit(void *arg)
210 mtx_destroy(&ucom_mtx);
212 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 3, SI_ORDER_ANY, ucom_uninit, NULL);
215 * Mark a unit number (the X in cuaUX) as in use.
217 * Note that devices using a different naming scheme (see ucom_tty_name()
218 * callback) still use this unit allocation.
221 ucom_unit_alloc(void)
226 if (ucom_unrhdr == NULL) {
227 DPRINTF("ucom_unrhdr is NULL\n");
230 unit = alloc_unr(ucom_unrhdr);
231 DPRINTF("unit %d is allocated\n", unit);
236 * Mark the unit number as not in use.
239 ucom_unit_free(int unit)
242 if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
243 DPRINTF("cannot free unit number\n");
246 DPRINTF("unit %d is freed\n", unit);
247 free_unr(ucom_unrhdr, unit);
251 * Setup a group of one or more serial ports.
253 * The mutex pointed to by "mtx" is applied before all
254 * callbacks are called back. Also "mtx" must be applied
255 * before calling into the ucom-layer!
258 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
259 int subunits, void *parent,
260 const struct ucom_callback *callback, struct mtx *mtx)
267 (callback == NULL) ||
272 /* allocate a uniq unit number */
273 ssc->sc_unit = ucom_unit_alloc();
274 if (ssc->sc_unit == -1)
277 /* generate TTY name string */
278 snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
279 UCOM_TTY_PREFIX "%d", ssc->sc_unit);
281 /* create USB request handling process */
282 error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
284 ucom_unit_free(ssc->sc_unit);
287 ssc->sc_subunits = subunits;
288 ssc->sc_flag = UCOM_FLAG_ATTACHED |
291 if (callback->ucom_free == NULL)
292 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
294 /* increment reference count */
297 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
298 sc[subunit].sc_subunit = subunit;
299 sc[subunit].sc_super = ssc;
300 sc[subunit].sc_mtx = mtx;
301 sc[subunit].sc_parent = parent;
302 sc[subunit].sc_callback = callback;
304 error = ucom_attach_tty(ssc, &sc[subunit]);
306 ucom_detach(ssc, &sc[0]);
309 /* increment reference count */
312 /* set subunit attached */
313 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
316 DPRINTF("tp = %p, unit = %d, subunits = %d\n",
317 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
323 * The following function will do nothing if the structure pointed to
324 * by "ssc" and "sc" is zero or has already been detached.
327 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
331 if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
332 return; /* not initialized */
334 if (ssc->sc_sysctl_ttyname != NULL) {
335 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
336 ssc->sc_sysctl_ttyname = NULL;
339 if (ssc->sc_sysctl_ttyports != NULL) {
340 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
341 ssc->sc_sysctl_ttyports = NULL;
344 usb_proc_drain(&ssc->sc_tq);
346 for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
347 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
349 ucom_detach_tty(ssc, &sc[subunit]);
351 /* avoid duplicate detach */
352 sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
355 usb_proc_free(&ssc->sc_tq);
359 if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
362 /* make sure we don't detach twice */
363 ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
367 ucom_drain(struct ucom_super_softc *ssc)
370 while (ssc->sc_refs > 0) {
371 printf("ucom: Waiting for a TTY device to close.\n");
372 usb_pause_mtx(&ucom_mtx, hz);
374 mtx_unlock(&ucom_mtx);
378 ucom_drain_all(void *arg)
381 while (ucom_close_refs > 0) {
382 printf("ucom: Waiting for all detached TTY "
383 "devices to have open fds closed.\n");
384 usb_pause_mtx(&ucom_mtx, hz);
386 mtx_unlock(&ucom_mtx);
390 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
393 char buf[32]; /* temporary TTY device name buffer */
395 tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
399 /* Check if the client has a custom TTY name */
401 if (sc->sc_callback->ucom_tty_name) {
402 sc->sc_callback->ucom_tty_name(sc, buf,
403 sizeof(buf), ssc->sc_unit, sc->sc_subunit);
406 /* Use default TTY name */
407 if (ssc->sc_subunits > 1) {
408 /* multiple modems in one */
409 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
410 ssc->sc_unit, sc->sc_subunit);
413 snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
417 tty_makedev(tp, NULL, "%s", buf);
421 sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
422 sc->sc_pps.driver_abi = PPS_ABI_VERSION;
423 sc->sc_pps.driver_mtx = sc->sc_mtx;
424 pps_init_abi(&sc->sc_pps);
426 DPRINTF("ttycreate: %s\n", buf);
428 /* Check if this device should be a console */
429 if ((ucom_cons_softc == NULL) &&
430 (ssc->sc_unit == ucom_cons_unit) &&
431 (sc->sc_subunit == ucom_cons_subunit)) {
433 DPRINTF("unit %d subunit %d is console",
434 ssc->sc_unit, sc->sc_subunit);
436 ucom_cons_softc = sc;
438 tty_init_console(tp, ucom_cons_baud);
440 UCOM_MTX_LOCK(ucom_cons_softc);
441 ucom_cons_rx_low = 0;
442 ucom_cons_rx_high = 0;
443 ucom_cons_tx_low = 0;
444 ucom_cons_tx_high = 0;
445 sc->sc_flag |= UCOM_FLAG_CONSOLE;
446 ucom_open(ucom_cons_softc->sc_tty);
447 ucom_param(ucom_cons_softc->sc_tty, &tp->t_termios_init_in);
448 UCOM_MTX_UNLOCK(ucom_cons_softc);
455 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
457 struct tty *tp = sc->sc_tty;
459 DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
461 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
462 UCOM_MTX_LOCK(ucom_cons_softc);
463 ucom_close(ucom_cons_softc->sc_tty);
464 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
465 UCOM_MTX_UNLOCK(ucom_cons_softc);
466 ucom_cons_softc = NULL;
469 /* the config thread has been stopped when we get here */
472 sc->sc_flag |= UCOM_FLAG_GONE;
473 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
479 mtx_unlock(&ucom_mtx);
483 ucom_close(tp); /* close, if any */
489 * make sure that read and write transfers are stopped
491 if (sc->sc_callback->ucom_stop_read)
492 (sc->sc_callback->ucom_stop_read) (sc);
493 if (sc->sc_callback->ucom_stop_write)
494 (sc->sc_callback->ucom_stop_write) (sc);
500 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
504 struct usb_attach_arg *uaa;
506 snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
507 "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
509 /* Store the PNP info in the first interface for the device */
510 uaa = device_get_ivars(dev);
511 iface_index = uaa->info.bIfaceIndex;
513 if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
514 device_printf(dev, "Could not set PNP info\n");
517 * The following information is also replicated in the PNP-info
518 * string which is registered above:
520 if (ssc->sc_sysctl_ttyname == NULL) {
521 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
522 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
523 OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
524 "TTY device basename");
526 if (ssc->sc_sysctl_ttyports == NULL) {
527 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
528 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
529 OID_AUTO, "ttyports", CTLFLAG_RD,
530 NULL, ssc->sc_subunits, "Number of ports");
535 ucom_queue_command(struct ucom_softc *sc,
536 usb_proc_callback_t *fn, struct termios *pt,
537 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
539 struct ucom_super_softc *ssc = sc->sc_super;
540 struct ucom_param_task *task;
542 UCOM_MTX_ASSERT(sc, MA_OWNED);
544 if (usb_proc_is_gone(&ssc->sc_tq)) {
545 DPRINTF("proc is gone\n");
546 return; /* nothing to do */
549 * NOTE: The task cannot get executed before we drop the
550 * "sc_mtx" mutex. It is safe to update fields in the message
551 * structure after that the message got queued.
553 task = (struct ucom_param_task *)
554 usb_proc_msignal(&ssc->sc_tq, t0, t1);
556 /* Setup callback and softc pointers */
557 task->hdr.pm_callback = fn;
561 * Make a copy of the termios. This field is only present if
562 * the "pt" field is not NULL.
565 task->termios_copy = *pt;
568 * Closing the device should be synchronous.
570 if (fn == ucom_cfg_close)
571 usb_proc_mwait(&ssc->sc_tq, t0, t1);
574 * In case of multiple configure requests,
575 * keep track of the last one!
577 if (fn == ucom_cfg_start_transfers)
578 sc->sc_last_start_xfer = &task->hdr;
582 ucom_shutdown(struct ucom_softc *sc)
584 struct tty *tp = sc->sc_tty;
586 UCOM_MTX_ASSERT(sc, MA_OWNED);
591 * Hang up if necessary:
593 if (tp->t_termios.c_cflag & HUPCL) {
594 ucom_modem(tp, 0, SER_DTR);
601 * else: taskqueue is draining or gone
604 ucom_cfg_is_gone(struct ucom_softc *sc)
606 struct ucom_super_softc *ssc = sc->sc_super;
608 return (usb_proc_is_gone(&ssc->sc_tq));
612 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
614 struct ucom_cfg_task *task =
615 (struct ucom_cfg_task *)_task;
616 struct ucom_softc *sc = task->sc;
618 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
621 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
622 /* TTY device closed */
626 if (_task == sc->sc_last_start_xfer)
627 sc->sc_flag |= UCOM_FLAG_GP_DATA;
629 if (sc->sc_callback->ucom_start_read) {
630 (sc->sc_callback->ucom_start_read) (sc);
632 if (sc->sc_callback->ucom_start_write) {
633 (sc->sc_callback->ucom_start_write) (sc);
638 ucom_start_transfers(struct ucom_softc *sc)
640 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
644 * Make sure that data transfers are started in both
647 if (sc->sc_callback->ucom_start_read) {
648 (sc->sc_callback->ucom_start_read) (sc);
650 if (sc->sc_callback->ucom_start_write) {
651 (sc->sc_callback->ucom_start_write) (sc);
656 ucom_cfg_open(struct usb_proc_msg *_task)
658 struct ucom_cfg_task *task =
659 (struct ucom_cfg_task *)_task;
660 struct ucom_softc *sc = task->sc;
664 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
670 sc->sc_flag |= UCOM_FLAG_LL_READY;
672 if (sc->sc_callback->ucom_cfg_open) {
673 (sc->sc_callback->ucom_cfg_open) (sc);
676 usb_pause_mtx(sc->sc_mtx, hz / 10);
682 ucom_open(struct tty *tp)
684 struct ucom_softc *sc = tty_softc(tp);
687 UCOM_MTX_ASSERT(sc, MA_OWNED);
689 if (sc->sc_flag & UCOM_FLAG_GONE) {
692 if (sc->sc_flag & UCOM_FLAG_HL_READY) {
696 DPRINTF("tp = %p\n", tp);
698 if (sc->sc_callback->ucom_pre_open) {
700 * give the lower layer a chance to disallow TTY open, for
701 * example if the device is not present:
703 error = (sc->sc_callback->ucom_pre_open) (sc);
708 sc->sc_flag |= UCOM_FLAG_HL_READY;
710 /* Disable transfers */
711 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
717 /* reset programmed line state */
722 /* reset jitter buffer */
723 sc->sc_jitterbuf_in = 0;
724 sc->sc_jitterbuf_out = 0;
726 ucom_queue_command(sc, ucom_cfg_open, NULL,
727 &sc->sc_open_task[0].hdr,
728 &sc->sc_open_task[1].hdr);
730 /* Queue transfer enable command last */
731 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
732 &sc->sc_start_task[0].hdr,
733 &sc->sc_start_task[1].hdr);
735 ucom_modem(tp, SER_DTR | SER_RTS, 0);
741 ucom_status_change(sc);
747 ucom_cfg_close(struct usb_proc_msg *_task)
749 struct ucom_cfg_task *task =
750 (struct ucom_cfg_task *)_task;
751 struct ucom_softc *sc = task->sc;
755 if (sc->sc_flag & UCOM_FLAG_LL_READY) {
756 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
757 if (sc->sc_callback->ucom_cfg_close)
758 (sc->sc_callback->ucom_cfg_close) (sc);
765 ucom_close(struct tty *tp)
767 struct ucom_softc *sc = tty_softc(tp);
769 UCOM_MTX_ASSERT(sc, MA_OWNED);
771 DPRINTF("tp=%p\n", tp);
773 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
774 DPRINTF("tp=%p already closed\n", tp);
779 ucom_queue_command(sc, ucom_cfg_close, NULL,
780 &sc->sc_close_task[0].hdr,
781 &sc->sc_close_task[1].hdr);
783 sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
785 if (sc->sc_callback->ucom_stop_read) {
786 (sc->sc_callback->ucom_stop_read) (sc);
791 ucom_inwakeup(struct tty *tp)
793 struct ucom_softc *sc = tty_softc(tp);
799 UCOM_MTX_ASSERT(sc, MA_OWNED);
801 DPRINTF("tp=%p\n", tp);
803 if (ttydisc_can_bypass(tp) != 0 ||
804 (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
805 (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
809 /* prevent recursion */
810 sc->sc_flag |= UCOM_FLAG_INWAKEUP;
812 pos = sc->sc_jitterbuf_out;
814 while (sc->sc_jitterbuf_in != pos) {
817 c = (char)sc->sc_jitterbuf[pos];
819 if (ttydisc_rint(tp, c, 0) == -1)
822 if (pos >= UCOM_JITTERBUF_SIZE)
823 pos -= UCOM_JITTERBUF_SIZE;
826 sc->sc_jitterbuf_out = pos;
828 /* clear RTS in async fashion */
829 if ((sc->sc_jitterbuf_in == pos) &&
830 (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
833 sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
837 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
839 struct ucom_softc *sc = tty_softc(tp);
842 UCOM_MTX_ASSERT(sc, MA_OWNED);
844 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
847 DPRINTF("cmd = 0x%08lx\n", cmd);
869 if (sc->sc_callback->ucom_ioctl) {
870 error = (sc->sc_callback->ucom_ioctl)
871 (sc, cmd, data, 0, td);
875 if (error == ENOIOCTL)
876 error = pps_ioctl(cmd, data, &sc->sc_pps);
883 ucom_modem(struct tty *tp, int sigon, int sigoff)
885 struct ucom_softc *sc = tty_softc(tp);
888 UCOM_MTX_ASSERT(sc, MA_OWNED);
890 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
893 if ((sigon == 0) && (sigoff == 0)) {
895 if (sc->sc_mcr & SER_DTR) {
898 if (sc->sc_mcr & SER_RTS) {
901 if (sc->sc_msr & SER_CTS) {
904 if (sc->sc_msr & SER_DCD) {
907 if (sc->sc_msr & SER_DSR) {
910 if (sc->sc_msr & SER_RI) {
915 if (sigon & SER_DTR) {
916 sc->sc_mcr |= SER_DTR;
918 if (sigoff & SER_DTR) {
919 sc->sc_mcr &= ~SER_DTR;
921 if (sigon & SER_RTS) {
922 sc->sc_mcr |= SER_RTS;
924 if (sigoff & SER_RTS) {
925 sc->sc_mcr &= ~SER_RTS;
927 onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
930 onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
937 ucom_cfg_line_state(struct usb_proc_msg *_task)
939 struct ucom_cfg_task *task =
940 (struct ucom_cfg_task *)_task;
941 struct ucom_softc *sc = task->sc;
948 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
953 /* compute callback mask */
954 if (sc->sc_callback->ucom_cfg_set_dtr)
956 if (sc->sc_callback->ucom_cfg_set_rts)
958 if (sc->sc_callback->ucom_cfg_set_break)
959 mask |= UCOM_LS_BREAK;
960 if (sc->sc_callback->ucom_cfg_set_ring)
961 mask |= UCOM_LS_RING;
963 /* compute the bits we are to program */
964 notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
965 any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
966 prev_value = sc->sc_pls_curr ^ notch_bits;
967 last_value = sc->sc_pls_curr;
969 /* reset programmed line state */
974 /* ensure that we don't lose any levels */
975 if (notch_bits & UCOM_LS_DTR)
976 sc->sc_callback->ucom_cfg_set_dtr(sc,
977 (prev_value & UCOM_LS_DTR) ? 1 : 0);
978 if (notch_bits & UCOM_LS_RTS)
979 sc->sc_callback->ucom_cfg_set_rts(sc,
980 (prev_value & UCOM_LS_RTS) ? 1 : 0);
981 if (notch_bits & UCOM_LS_BREAK)
982 sc->sc_callback->ucom_cfg_set_break(sc,
983 (prev_value & UCOM_LS_BREAK) ? 1 : 0);
984 if (notch_bits & UCOM_LS_RING)
985 sc->sc_callback->ucom_cfg_set_ring(sc,
986 (prev_value & UCOM_LS_RING) ? 1 : 0);
989 if (any_bits & UCOM_LS_DTR)
990 sc->sc_callback->ucom_cfg_set_dtr(sc,
991 (last_value & UCOM_LS_DTR) ? 1 : 0);
992 if (any_bits & UCOM_LS_RTS)
993 sc->sc_callback->ucom_cfg_set_rts(sc,
994 (last_value & UCOM_LS_RTS) ? 1 : 0);
995 if (any_bits & UCOM_LS_BREAK)
996 sc->sc_callback->ucom_cfg_set_break(sc,
997 (last_value & UCOM_LS_BREAK) ? 1 : 0);
998 if (any_bits & UCOM_LS_RING)
999 sc->sc_callback->ucom_cfg_set_ring(sc,
1000 (last_value & UCOM_LS_RING) ? 1 : 0);
1004 ucom_line_state(struct ucom_softc *sc,
1005 uint8_t set_bits, uint8_t clear_bits)
1007 UCOM_MTX_ASSERT(sc, MA_OWNED);
1009 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1013 DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1015 /* update current programmed line state */
1016 sc->sc_pls_curr |= set_bits;
1017 sc->sc_pls_curr &= ~clear_bits;
1018 sc->sc_pls_set |= set_bits;
1019 sc->sc_pls_clr |= clear_bits;
1021 /* defer driver programming */
1022 ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1023 &sc->sc_line_state_task[0].hdr,
1024 &sc->sc_line_state_task[1].hdr);
1028 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1030 DPRINTF("onoff = %d\n", onoff);
1033 ucom_line_state(sc, UCOM_LS_RING, 0);
1035 ucom_line_state(sc, 0, UCOM_LS_RING);
1039 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1041 DPRINTF("onoff = %d\n", onoff);
1044 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1046 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1050 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1052 DPRINTF("onoff = %d\n", onoff);
1055 ucom_line_state(sc, UCOM_LS_DTR, 0);
1057 ucom_line_state(sc, 0, UCOM_LS_DTR);
1061 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1063 DPRINTF("onoff = %d\n", onoff);
1066 ucom_line_state(sc, UCOM_LS_RTS, 0);
1068 ucom_line_state(sc, 0, UCOM_LS_RTS);
1072 ucom_cfg_status_change(struct usb_proc_msg *_task)
1074 struct ucom_cfg_task *task =
1075 (struct ucom_cfg_task *)_task;
1076 struct ucom_softc *sc = task->sc;
1087 UCOM_MTX_ASSERT(sc, MA_OWNED);
1089 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1092 if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1100 (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1102 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1103 /* TTY device closed */
1106 msr_delta = (sc->sc_msr ^ new_msr);
1107 lsr_delta = (sc->sc_lsr ^ new_lsr);
1109 sc->sc_msr = new_msr;
1110 sc->sc_lsr = new_lsr;
1113 * Time pulse counting support.
1115 switch(ucom_pps_mode & UART_PPS_SIGNAL_MASK) {
1117 pps_signal = SER_CTS;
1120 pps_signal = SER_DCD;
1127 if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1128 (msr_delta & pps_signal)) {
1129 pps_capture(&sc->sc_pps);
1130 onoff = (sc->sc_msr & pps_signal) ? 1 : 0;
1131 if (ucom_pps_mode & UART_PPS_INVERT_PULSE)
1133 pps_event(&sc->sc_pps, onoff ? PPS_CAPTUREASSERT :
1137 if (msr_delta & SER_DCD) {
1139 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1141 DPRINTF("DCD changed to %d\n", onoff);
1143 ttydisc_modem(tp, onoff);
1146 if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1148 DPRINTF("BREAK detected\n");
1150 ttydisc_rint(tp, 0, TRE_BREAK);
1151 ttydisc_rint_done(tp);
1154 if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1156 DPRINTF("Frame error detected\n");
1158 ttydisc_rint(tp, 0, TRE_FRAMING);
1159 ttydisc_rint_done(tp);
1162 if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1164 DPRINTF("Parity error detected\n");
1166 ttydisc_rint(tp, 0, TRE_PARITY);
1167 ttydisc_rint_done(tp);
1172 ucom_status_change(struct ucom_softc *sc)
1174 UCOM_MTX_ASSERT(sc, MA_OWNED);
1176 if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1177 return; /* not supported */
1179 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1184 ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1185 &sc->sc_status_task[0].hdr,
1186 &sc->sc_status_task[1].hdr);
1190 ucom_cfg_param(struct usb_proc_msg *_task)
1192 struct ucom_param_task *task =
1193 (struct ucom_param_task *)_task;
1194 struct ucom_softc *sc = task->sc;
1196 if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1199 if (sc->sc_callback->ucom_cfg_param == NULL) {
1203 (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1206 usb_pause_mtx(sc->sc_mtx, hz / 10);
1210 ucom_param(struct tty *tp, struct termios *t)
1212 struct ucom_softc *sc = tty_softc(tp);
1216 UCOM_MTX_ASSERT(sc, MA_OWNED);
1221 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1223 /* XXX the TTY layer should call "open()" first! */
1225 * Not quite: Its ordering is partly backwards, but
1226 * some parameters must be set early in ttydev_open(),
1227 * possibly before calling ttydevsw_open().
1229 error = ucom_open(tp);
1235 DPRINTF("sc = %p\n", sc);
1237 /* Check requested parameters. */
1238 if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1239 /* XXX c_ospeed == 0 is perfectly valid. */
1240 DPRINTF("mismatch ispeed and ospeed\n");
1244 t->c_ispeed = t->c_ospeed;
1246 if (sc->sc_callback->ucom_pre_param) {
1247 /* Let the lower layer verify the parameters */
1248 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1250 DPRINTF("callback error = %d\n", error);
1255 /* Disable transfers */
1256 sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1258 /* Queue baud rate programming command first */
1259 ucom_queue_command(sc, ucom_cfg_param, t,
1260 &sc->sc_param_task[0].hdr,
1261 &sc->sc_param_task[1].hdr);
1263 /* Queue transfer enable command last */
1264 ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1265 &sc->sc_start_task[0].hdr,
1266 &sc->sc_start_task[1].hdr);
1268 if (t->c_cflag & CRTS_IFLOW) {
1269 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1270 } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1271 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1272 ucom_modem(tp, SER_RTS, 0);
1284 ucom_outwakeup(struct tty *tp)
1286 struct ucom_softc *sc = tty_softc(tp);
1288 UCOM_MTX_ASSERT(sc, MA_OWNED);
1290 DPRINTF("sc = %p\n", sc);
1292 if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1293 /* The higher layer is not ready */
1296 ucom_start_transfers(sc);
1300 ucom_busy(struct tty *tp)
1302 struct ucom_softc *sc = tty_softc(tp);
1303 const uint8_t txidle = ULSR_TXRDY | ULSR_TSRE;
1305 UCOM_MTX_ASSERT(sc, MA_OWNED);
1307 DPRINTFN(3, "sc = %p lsr 0x%02x\n", sc, sc->sc_lsr);
1310 * If the driver maintains the txidle bits in LSR, we can use them to
1311 * determine whether the transmitter is busy or idle. Otherwise we have
1312 * to assume it is idle to avoid hanging forever on tcdrain(3).
1314 if (sc->sc_flag & UCOM_FLAG_LSRTXIDLE)
1315 return ((sc->sc_lsr & txidle) != txidle);
1320 /*------------------------------------------------------------------------*
1324 * 0: No data is available.
1325 * Else: Data is available.
1326 *------------------------------------------------------------------------*/
1328 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1329 uint32_t offset, uint32_t len, uint32_t *actlen)
1331 struct usb_page_search res;
1332 struct tty *tp = sc->sc_tty;
1334 uint32_t offset_orig;
1336 UCOM_MTX_ASSERT(sc, MA_OWNED);
1338 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1341 /* get total TX length */
1343 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1344 temp %= UCOM_CONS_BUFSIZE;
1346 /* limit TX length */
1348 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1349 temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1356 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1358 /* update counters */
1360 ucom_cons_tx_low += temp;
1361 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1363 /* store actual length */
1367 return (temp ? 1 : 0);
1371 !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1373 return (0); /* multiport device polling */
1375 offset_orig = offset;
1379 usbd_get_page(pc, offset, &res);
1381 if (res.length > len) {
1384 /* copy data directly into USB buffer */
1385 cnt = ttydisc_getc(tp, res.buffer, res.length);
1390 if (cnt < res.length) {
1396 actlen[0] = offset - offset_orig;
1398 DPRINTF("cnt=%d\n", actlen[0]);
1400 if (actlen[0] == 0) {
1407 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1408 uint32_t offset, uint32_t len)
1410 struct usb_page_search res;
1411 struct tty *tp = sc->sc_tty;
1415 UCOM_MTX_ASSERT(sc, MA_OWNED);
1417 if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1420 /* get maximum RX length */
1422 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1423 temp %= UCOM_CONS_BUFSIZE;
1425 /* limit RX length */
1427 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1428 temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1435 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1437 /* update counters */
1439 ucom_cons_rx_high += temp;
1440 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1446 return; /* multiport device polling */
1449 return; /* no data */
1451 /* set a flag to prevent recursation ? */
1455 usbd_get_page(pc, offset, &res);
1457 if (res.length > len) {
1461 offset += res.length;
1463 /* pass characters to tty layer */
1468 /* first check if we can pass the buffer directly */
1470 if (ttydisc_can_bypass(tp)) {
1472 /* clear any jitter buffer */
1473 sc->sc_jitterbuf_in = 0;
1474 sc->sc_jitterbuf_out = 0;
1476 if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1477 DPRINTF("tp=%p, data lost\n", tp);
1483 for (cnt = 0; cnt != res.length; cnt++) {
1484 if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1485 ttydisc_rint(tp, buf[cnt], 0) == -1) {
1489 pos = sc->sc_jitterbuf_in;
1490 end = sc->sc_jitterbuf_out +
1491 UCOM_JITTERBUF_SIZE - 1;
1492 if (end >= UCOM_JITTERBUF_SIZE)
1493 end -= UCOM_JITTERBUF_SIZE;
1495 for (; cnt != res.length; cnt++) {
1498 sc->sc_jitterbuf[pos] = buf[cnt];
1500 if (pos >= UCOM_JITTERBUF_SIZE)
1501 pos -= UCOM_JITTERBUF_SIZE;
1504 sc->sc_jitterbuf_in = pos;
1506 /* set RTS in async fashion */
1507 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1510 DPRINTF("tp=%p, lost %d "
1511 "chars\n", tp, res.length - cnt);
1516 ttydisc_rint_done(tp);
1520 ucom_free(void *xsc)
1522 struct ucom_softc *sc = xsc;
1524 if (sc->sc_callback->ucom_free != NULL)
1525 sc->sc_callback->ucom_free(sc);
1527 ucom_unref(sc->sc_super);
1529 mtx_lock(&ucom_mtx);
1531 mtx_unlock(&ucom_mtx);
1534 static cn_probe_t ucom_cnprobe;
1535 static cn_init_t ucom_cninit;
1536 static cn_term_t ucom_cnterm;
1537 static cn_getc_t ucom_cngetc;
1538 static cn_putc_t ucom_cnputc;
1539 static cn_grab_t ucom_cngrab;
1540 static cn_ungrab_t ucom_cnungrab;
1542 CONSOLE_DRIVER(ucom);
1545 ucom_cnprobe(struct consdev *cp)
1547 if (ucom_cons_unit != -1)
1548 cp->cn_pri = CN_NORMAL;
1550 cp->cn_pri = CN_DEAD;
1552 strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1556 ucom_cninit(struct consdev *cp)
1561 ucom_cnterm(struct consdev *cp)
1566 ucom_cngrab(struct consdev *cp)
1571 ucom_cnungrab(struct consdev *cp)
1576 ucom_cngetc(struct consdev *cd)
1578 struct ucom_softc *sc = ucom_cons_softc;
1586 if (ucom_cons_rx_low != ucom_cons_rx_high) {
1587 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1588 ucom_cons_rx_low ++;
1589 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1594 /* start USB transfers */
1595 ucom_outwakeup(sc->sc_tty);
1597 UCOM_MTX_UNLOCK(sc);
1599 /* poll if necessary */
1600 if (USB_IN_POLLING_MODE_FUNC() && sc->sc_callback->ucom_poll)
1601 (sc->sc_callback->ucom_poll) (sc);
1607 ucom_cnputc(struct consdev *cd, int c)
1609 struct ucom_softc *sc = ucom_cons_softc;
1619 /* compute maximum TX length */
1621 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1622 temp %= UCOM_CONS_BUFSIZE;
1625 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1626 ucom_cons_tx_high ++;
1627 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1630 /* start USB transfers */
1631 ucom_outwakeup(sc->sc_tty);
1633 UCOM_MTX_UNLOCK(sc);
1635 /* poll if necessary */
1636 if (USB_IN_POLLING_MODE_FUNC() && sc->sc_callback->ucom_poll) {
1637 (sc->sc_callback->ucom_poll) (sc);
1638 /* simple flow control */
1644 /*------------------------------------------------------------------------*
1647 * This function will increment the super UCOM reference count.
1648 *------------------------------------------------------------------------*/
1650 ucom_ref(struct ucom_super_softc *ssc)
1652 mtx_lock(&ucom_mtx);
1654 mtx_unlock(&ucom_mtx);
1657 /*------------------------------------------------------------------------*
1660 * This function will free the super UCOM's allocated unit
1661 * number. This function can be called on a zero-initialized
1662 * structure. This function can be called multiple times.
1663 *------------------------------------------------------------------------*/
1665 ucom_free_unit(struct ucom_super_softc *ssc)
1667 if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1670 ucom_unit_free(ssc->sc_unit);
1672 ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1675 /*------------------------------------------------------------------------*
1678 * This function will decrement the super UCOM reference count.
1681 * 0: UCOM structures are still referenced.
1682 * Else: UCOM structures are no longer referenced.
1683 *------------------------------------------------------------------------*/
1685 ucom_unref(struct ucom_super_softc *ssc)
1689 mtx_lock(&ucom_mtx);
1690 retval = (ssc->sc_refs < 2);
1692 mtx_unlock(&ucom_mtx);
1695 ucom_free_unit(ssc);
1702 #include <gdb/gdb.h>
1704 static gdb_probe_f ucom_gdbprobe;
1705 static gdb_init_f ucom_gdbinit;
1706 static gdb_term_f ucom_gdbterm;
1707 static gdb_getc_f ucom_gdbgetc;
1708 static gdb_putc_f ucom_gdbputc;
1710 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1715 return ((ucom_cons_softc != NULL) ? 0 : -1);
1731 ucom_cnputc(NULL, c);
1737 return (ucom_cngetc(NULL));