xref: /freebsd/sys/dev/usb/serial/usb_serial.c (revision c6499eccad497913a5025fbde8ae76da70e08043)
1 /*	$NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001-2003, 2005, 2008
5  *	Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*-
34  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
35  * All rights reserved.
36  *
37  * This code is derived from software contributed to The NetBSD Foundation
38  * by Lennart Augustsson (lennart@augustsson.net) at
39  * Carlstedt Research & Technology.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
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.
56  *
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.
68  */
69 
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>
77 #include <sys/bus.h>
78 #include <sys/module.h>
79 #include <sys/lock.h>
80 #include <sys/mutex.h>
81 #include <sys/condvar.h>
82 #include <sys/sysctl.h>
83 #include <sys/sx.h>
84 #include <sys/unistd.h>
85 #include <sys/callout.h>
86 #include <sys/malloc.h>
87 #include <sys/priv.h>
88 #include <sys/cons.h>
89 #include <sys/kdb.h>
90 
91 #include <dev/usb/usb.h>
92 #include <dev/usb/usbdi.h>
93 #include <dev/usb/usbdi_util.h>
94 
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>
99 
100 #include <dev/usb/serial/usb_serial.h>
101 
102 #include "opt_gdb.h"
103 
104 static SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
105 
106 #ifdef USB_DEBUG
107 static int ucom_debug = 0;
108 
109 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
110     &ucom_debug, 0, "ucom debug level");
111 #endif
112 
113 #define	UCOM_CONS_BUFSIZE 1024
114 
115 static uint8_t ucom_cons_rx_buf[UCOM_CONS_BUFSIZE];
116 static uint8_t ucom_cons_tx_buf[UCOM_CONS_BUFSIZE];
117 
118 static unsigned int ucom_cons_rx_low = 0;
119 static unsigned int ucom_cons_rx_high = 0;
120 
121 static unsigned int ucom_cons_tx_low = 0;
122 static unsigned int ucom_cons_tx_high = 0;
123 
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;
128 
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");
138 
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;
145 
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);
158 
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;
167 
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,
178 };
179 
180 MODULE_DEPEND(ucom, usb, 1, 1, 1);
181 MODULE_VERSION(ucom, 1);
182 
183 #define	UCOM_UNIT_MAX 		128	/* maximum number of units */
184 #define	UCOM_TTY_PREFIX		"U"
185 
186 static struct unrhdr *ucom_unrhdr;
187 static struct mtx ucom_mtx;
188 static int ucom_close_refs;
189 
190 static void
191 ucom_init(void *arg)
192 {
193 	DPRINTF("\n");
194 	ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
195 	mtx_init(&ucom_mtx, "UCOM MTX", NULL, MTX_DEF);
196 }
197 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
198 
199 static void
200 ucom_uninit(void *arg)
201 {
202 	struct unrhdr *hdr;
203 	hdr = ucom_unrhdr;
204 	ucom_unrhdr = NULL;
205 
206 	DPRINTF("\n");
207 
208 	if (hdr != NULL)
209 		delete_unrhdr(hdr);
210 
211 	mtx_destroy(&ucom_mtx);
212 }
213 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_uninit, NULL);
214 
215 /*
216  * Mark a unit number (the X in cuaUX) as in use.
217  *
218  * Note that devices using a different naming scheme (see ucom_tty_name()
219  * callback) still use this unit allocation.
220  */
221 static int
222 ucom_unit_alloc(void)
223 {
224 	int unit;
225 
226 	/* sanity checks */
227 	if (ucom_unrhdr == NULL) {
228 		DPRINTF("ucom_unrhdr is NULL\n");
229 		return (-1);
230 	}
231 	unit = alloc_unr(ucom_unrhdr);
232 	DPRINTF("unit %d is allocated\n", unit);
233 	return (unit);
234 }
235 
236 /*
237  * Mark the unit number as not in use.
238  */
239 static void
240 ucom_unit_free(int unit)
241 {
242 	/* sanity checks */
243 	if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
244 		DPRINTF("cannot free unit number\n");
245 		return;
246 	}
247 	DPRINTF("unit %d is freed\n", unit);
248 	free_unr(ucom_unrhdr, unit);
249 }
250 
251 /*
252  * Setup a group of one or more serial ports.
253  *
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!
257  */
258 int
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)
262 {
263 	int subunit;
264 	int error = 0;
265 
266 	if ((sc == NULL) ||
267 	    (subunits <= 0) ||
268 	    (callback == NULL) ||
269 	    (mtx == NULL)) {
270 		return (EINVAL);
271 	}
272 
273 	/* allocate a uniq unit number */
274 	ssc->sc_unit = ucom_unit_alloc();
275 	if (ssc->sc_unit == -1)
276 		return (ENOMEM);
277 
278 	/* generate TTY name string */
279 	snprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
280 	    UCOM_TTY_PREFIX "%d", ssc->sc_unit);
281 
282 	/* create USB request handling process */
283 	error = usb_proc_create(&ssc->sc_tq, mtx, "ucom", USB_PRI_MED);
284 	if (error) {
285 		ucom_unit_free(ssc->sc_unit);
286 		return (error);
287 	}
288 	ssc->sc_subunits = subunits;
289 	ssc->sc_flag = UCOM_FLAG_ATTACHED |
290 	    UCOM_FLAG_FREE_UNIT;
291 
292 	if (callback->ucom_free == NULL)
293 		ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
294 
295 	/* increment reference count */
296 	ucom_ref(ssc);
297 
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;
304 
305 		error = ucom_attach_tty(ssc, &sc[subunit]);
306 		if (error) {
307 			ucom_detach(ssc, &sc[0]);
308 			return (error);
309 		}
310 		/* increment reference count */
311 		ucom_ref(ssc);
312 
313 		/* set subunit attached */
314 		sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
315 	}
316 
317 	DPRINTF("tp = %p, unit = %d, subunits = %d\n",
318 		sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
319 
320 	return (0);
321 }
322 
323 /*
324  * The following function will do nothing if the structure pointed to
325  * by "ssc" and "sc" is zero or has already been detached.
326  */
327 void
328 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
329 {
330 	int subunit;
331 
332 	if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
333 		return;		/* not initialized */
334 
335 	if (ssc->sc_sysctl_ttyname != NULL) {
336 		sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
337 		ssc->sc_sysctl_ttyname = NULL;
338 	}
339 
340 	if (ssc->sc_sysctl_ttyports != NULL) {
341 		sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
342 		ssc->sc_sysctl_ttyports = NULL;
343 	}
344 
345 	usb_proc_drain(&ssc->sc_tq);
346 
347 	for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
348 		if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
349 
350 			ucom_detach_tty(ssc, &sc[subunit]);
351 
352 			/* avoid duplicate detach */
353 			sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
354 		}
355 	}
356 	usb_proc_free(&ssc->sc_tq);
357 
358 	ucom_unref(ssc);
359 
360 	if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
361 		ucom_drain(ssc);
362 
363 	/* make sure we don't detach twice */
364 	ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
365 }
366 
367 void
368 ucom_drain(struct ucom_super_softc *ssc)
369 {
370 	mtx_lock(&ucom_mtx);
371 	while (ssc->sc_refs > 0) {
372 		printf("ucom: Waiting for a TTY device to close.\n");
373 		usb_pause_mtx(&ucom_mtx, hz);
374 	}
375 	mtx_unlock(&ucom_mtx);
376 }
377 
378 void
379 ucom_drain_all(void *arg)
380 {
381 	mtx_lock(&ucom_mtx);
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);
386 	}
387 	mtx_unlock(&ucom_mtx);
388 }
389 
390 static int
391 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
392 {
393 	struct tty *tp;
394 	char buf[32];			/* temporary TTY device name buffer */
395 
396 	tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_mtx);
397 	if (tp == NULL)
398 		return (ENOMEM);
399 
400 	/* Check if the client has a custom TTY name */
401 	buf[0] = '\0';
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);
405 	}
406 	if (buf[0] == 0) {
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);
412 		} else {
413 			/* single modem */
414 			snprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
415 			    ssc->sc_unit);
416 		}
417 	}
418 	tty_makedev(tp, NULL, "%s", buf);
419 
420 	sc->sc_tty = tp;
421 
422 	DPRINTF("ttycreate: %s\n", buf);
423 
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)) {
428 
429 		DPRINTF("unit %d subunit %d is console",
430 		    ssc->sc_unit, sc->sc_subunit);
431 
432 		ucom_cons_softc = sc;
433 
434 		tty_init_console(tp, ucom_cons_baud);
435 
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);
445 	}
446 
447 	return (0);
448 }
449 
450 static void
451 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
452 {
453 	struct tty *tp = sc->sc_tty;
454 
455 	DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
456 
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;
463 	}
464 
465 	/* the config thread has been stopped when we get here */
466 
467 	UCOM_MTX_LOCK(sc);
468 	sc->sc_flag |= UCOM_FLAG_GONE;
469 	sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
470 	UCOM_MTX_UNLOCK(sc);
471 
472 	if (tp) {
473 		mtx_lock(&ucom_mtx);
474 		ucom_close_refs++;
475 		mtx_unlock(&ucom_mtx);
476 
477 		tty_lock(tp);
478 
479 		ucom_close(tp);	/* close, if any */
480 
481 		tty_rel_gone(tp);
482 
483 		UCOM_MTX_LOCK(sc);
484 		/*
485 		 * make sure that read and write transfers are stopped
486 		 */
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);
491 		UCOM_MTX_UNLOCK(sc);
492 	}
493 }
494 
495 void
496 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
497 {
498 	char buf[64];
499 	uint8_t iface_index;
500 	struct usb_attach_arg *uaa;
501 
502 	snprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
503 	    "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
504 
505 	/* Store the PNP info in the first interface for the device */
506 	uaa = device_get_ivars(dev);
507 	iface_index = uaa->info.bIfaceIndex;
508 
509 	if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
510 		device_printf(dev, "Could not set PNP info\n");
511 
512 	/*
513 	 * The following information is also replicated in the PNP-info
514 	 * string which is registered above:
515 	 */
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");
521 	}
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");
527 	}
528 }
529 
530 static void
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)
534 {
535 	struct ucom_super_softc *ssc = sc->sc_super;
536 	struct ucom_param_task *task;
537 
538 	UCOM_MTX_ASSERT(sc, MA_OWNED);
539 
540 	if (usb_proc_is_gone(&ssc->sc_tq)) {
541 		DPRINTF("proc is gone\n");
542 		return;         /* nothing to do */
543 	}
544 	/*
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.
548 	 */
549 	task = (struct ucom_param_task *)
550 	  usb_proc_msignal(&ssc->sc_tq, t0, t1);
551 
552 	/* Setup callback and softc pointers */
553 	task->hdr.pm_callback = fn;
554 	task->sc = sc;
555 
556 	/*
557 	 * Make a copy of the termios. This field is only present if
558 	 * the "pt" field is not NULL.
559 	 */
560 	if (pt != NULL)
561 		task->termios_copy = *pt;
562 
563 	/*
564 	 * Closing the device should be synchronous.
565 	 */
566 	if (fn == ucom_cfg_close)
567 		usb_proc_mwait(&ssc->sc_tq, t0, t1);
568 
569 	/*
570 	 * In case of multiple configure requests,
571 	 * keep track of the last one!
572 	 */
573 	if (fn == ucom_cfg_start_transfers)
574 		sc->sc_last_start_xfer = &task->hdr;
575 }
576 
577 static void
578 ucom_shutdown(struct ucom_softc *sc)
579 {
580 	struct tty *tp = sc->sc_tty;
581 
582 	UCOM_MTX_ASSERT(sc, MA_OWNED);
583 
584 	DPRINTF("\n");
585 
586 	/*
587 	 * Hang up if necessary:
588 	 */
589 	if (tp->t_termios.c_cflag & HUPCL) {
590 		ucom_modem(tp, 0, SER_DTR);
591 	}
592 }
593 
594 /*
595  * Return values:
596  *    0: normal
597  * else: taskqueue is draining or gone
598  */
599 uint8_t
600 ucom_cfg_is_gone(struct ucom_softc *sc)
601 {
602 	struct ucom_super_softc *ssc = sc->sc_super;
603 
604 	return (usb_proc_is_gone(&ssc->sc_tq));
605 }
606 
607 static void
608 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
609 {
610 	struct ucom_cfg_task *task =
611 	    (struct ucom_cfg_task *)_task;
612 	struct ucom_softc *sc = task->sc;
613 
614 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
615 		return;
616 	}
617 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
618 		/* TTY device closed */
619 		return;
620 	}
621 
622 	if (_task == sc->sc_last_start_xfer)
623 		sc->sc_flag |= UCOM_FLAG_GP_DATA;
624 
625 	if (sc->sc_callback->ucom_start_read) {
626 		(sc->sc_callback->ucom_start_read) (sc);
627 	}
628 	if (sc->sc_callback->ucom_start_write) {
629 		(sc->sc_callback->ucom_start_write) (sc);
630 	}
631 }
632 
633 static void
634 ucom_start_transfers(struct ucom_softc *sc)
635 {
636 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
637 		return;
638 	}
639 	/*
640 	 * Make sure that data transfers are started in both
641 	 * directions:
642 	 */
643 	if (sc->sc_callback->ucom_start_read) {
644 		(sc->sc_callback->ucom_start_read) (sc);
645 	}
646 	if (sc->sc_callback->ucom_start_write) {
647 		(sc->sc_callback->ucom_start_write) (sc);
648 	}
649 }
650 
651 static void
652 ucom_cfg_open(struct usb_proc_msg *_task)
653 {
654 	struct ucom_cfg_task *task =
655 	    (struct ucom_cfg_task *)_task;
656 	struct ucom_softc *sc = task->sc;
657 
658 	DPRINTF("\n");
659 
660 	if (sc->sc_flag & UCOM_FLAG_LL_READY) {
661 
662 		/* already opened */
663 
664 	} else {
665 
666 		sc->sc_flag |= UCOM_FLAG_LL_READY;
667 
668 		if (sc->sc_callback->ucom_cfg_open) {
669 			(sc->sc_callback->ucom_cfg_open) (sc);
670 
671 			/* wait a little */
672 			usb_pause_mtx(sc->sc_mtx, hz / 10);
673 		}
674 	}
675 }
676 
677 static int
678 ucom_open(struct tty *tp)
679 {
680 	struct ucom_softc *sc = tty_softc(tp);
681 	int error;
682 
683 	UCOM_MTX_ASSERT(sc, MA_OWNED);
684 
685 	if (sc->sc_flag & UCOM_FLAG_GONE) {
686 		return (ENXIO);
687 	}
688 	if (sc->sc_flag & UCOM_FLAG_HL_READY) {
689 		/* already opened */
690 		return (0);
691 	}
692 	DPRINTF("tp = %p\n", tp);
693 
694 	if (sc->sc_callback->ucom_pre_open) {
695 		/*
696 		 * give the lower layer a chance to disallow TTY open, for
697 		 * example if the device is not present:
698 		 */
699 		error = (sc->sc_callback->ucom_pre_open) (sc);
700 		if (error) {
701 			return (error);
702 		}
703 	}
704 	sc->sc_flag |= UCOM_FLAG_HL_READY;
705 
706 	/* Disable transfers */
707 	sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
708 
709 	sc->sc_lsr = 0;
710 	sc->sc_msr = 0;
711 	sc->sc_mcr = 0;
712 
713 	/* reset programmed line state */
714 	sc->sc_pls_curr = 0;
715 	sc->sc_pls_set = 0;
716 	sc->sc_pls_clr = 0;
717 
718 	/* reset jitter buffer */
719 	sc->sc_jitterbuf_in = 0;
720 	sc->sc_jitterbuf_out = 0;
721 
722 	ucom_queue_command(sc, ucom_cfg_open, NULL,
723 	    &sc->sc_open_task[0].hdr,
724 	    &sc->sc_open_task[1].hdr);
725 
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);
730 
731 	ucom_modem(tp, SER_DTR | SER_RTS, 0);
732 
733 	ucom_ring(sc, 0);
734 
735 	ucom_break(sc, 0);
736 
737 	ucom_status_change(sc);
738 
739 	return (0);
740 }
741 
742 static void
743 ucom_cfg_close(struct usb_proc_msg *_task)
744 {
745 	struct ucom_cfg_task *task =
746 	    (struct ucom_cfg_task *)_task;
747 	struct ucom_softc *sc = task->sc;
748 
749 	DPRINTF("\n");
750 
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);
755 	} else {
756 		/* already closed */
757 	}
758 }
759 
760 static void
761 ucom_close(struct tty *tp)
762 {
763 	struct ucom_softc *sc = tty_softc(tp);
764 
765 	UCOM_MTX_ASSERT(sc, MA_OWNED);
766 
767 	DPRINTF("tp=%p\n", tp);
768 
769 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
770 		DPRINTF("tp=%p already closed\n", tp);
771 		return;
772 	}
773 	ucom_shutdown(sc);
774 
775 	ucom_queue_command(sc, ucom_cfg_close, NULL,
776 	    &sc->sc_close_task[0].hdr,
777 	    &sc->sc_close_task[1].hdr);
778 
779 	sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
780 
781 	if (sc->sc_callback->ucom_stop_read) {
782 		(sc->sc_callback->ucom_stop_read) (sc);
783 	}
784 }
785 
786 static void
787 ucom_inwakeup(struct tty *tp)
788 {
789 	struct ucom_softc *sc = tty_softc(tp);
790 	uint16_t pos;
791 
792 	if (sc == NULL)
793 		return;
794 
795 	UCOM_MTX_ASSERT(sc, MA_OWNED);
796 
797 	DPRINTF("tp=%p\n", tp);
798 
799 	if (ttydisc_can_bypass(tp) != 0 ||
800 	    (sc->sc_flag & UCOM_FLAG_HL_READY) == 0) {
801 		return;
802 	}
803 
804 	pos = sc->sc_jitterbuf_out;
805 
806 	while (sc->sc_jitterbuf_in != pos) {
807 		int c;
808 
809 		c = (char)sc->sc_jitterbuf[pos];
810 
811 		if (ttydisc_rint(tp, c, 0) == -1)
812 			break;
813 		pos++;
814 		if (pos >= UCOM_JITTERBUF_SIZE)
815 			pos -= UCOM_JITTERBUF_SIZE;
816 	}
817 
818 	sc->sc_jitterbuf_out = pos;
819 
820 	/* clear RTS in async fashion */
821 	if ((sc->sc_jitterbuf_in == pos) &&
822 	    (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
823 		ucom_rts(sc, 0);
824 }
825 
826 static int
827 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
828 {
829 	struct ucom_softc *sc = tty_softc(tp);
830 	int error;
831 
832 	UCOM_MTX_ASSERT(sc, MA_OWNED);
833 
834 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
835 		return (EIO);
836 	}
837 	DPRINTF("cmd = 0x%08lx\n", cmd);
838 
839 	switch (cmd) {
840 #if 0
841 	case TIOCSRING:
842 		ucom_ring(sc, 1);
843 		error = 0;
844 		break;
845 	case TIOCCRING:
846 		ucom_ring(sc, 0);
847 		error = 0;
848 		break;
849 #endif
850 	case TIOCSBRK:
851 		ucom_break(sc, 1);
852 		error = 0;
853 		break;
854 	case TIOCCBRK:
855 		ucom_break(sc, 0);
856 		error = 0;
857 		break;
858 	default:
859 		if (sc->sc_callback->ucom_ioctl) {
860 			error = (sc->sc_callback->ucom_ioctl)
861 			    (sc, cmd, data, 0, td);
862 		} else {
863 			error = ENOIOCTL;
864 		}
865 		break;
866 	}
867 	return (error);
868 }
869 
870 static int
871 ucom_modem(struct tty *tp, int sigon, int sigoff)
872 {
873 	struct ucom_softc *sc = tty_softc(tp);
874 	uint8_t onoff;
875 
876 	UCOM_MTX_ASSERT(sc, MA_OWNED);
877 
878 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
879 		return (0);
880 	}
881 	if ((sigon == 0) && (sigoff == 0)) {
882 
883 		if (sc->sc_mcr & SER_DTR) {
884 			sigon |= SER_DTR;
885 		}
886 		if (sc->sc_mcr & SER_RTS) {
887 			sigon |= SER_RTS;
888 		}
889 		if (sc->sc_msr & SER_CTS) {
890 			sigon |= SER_CTS;
891 		}
892 		if (sc->sc_msr & SER_DCD) {
893 			sigon |= SER_DCD;
894 		}
895 		if (sc->sc_msr & SER_DSR) {
896 			sigon |= SER_DSR;
897 		}
898 		if (sc->sc_msr & SER_RI) {
899 			sigon |= SER_RI;
900 		}
901 		return (sigon);
902 	}
903 	if (sigon & SER_DTR) {
904 		sc->sc_mcr |= SER_DTR;
905 	}
906 	if (sigoff & SER_DTR) {
907 		sc->sc_mcr &= ~SER_DTR;
908 	}
909 	if (sigon & SER_RTS) {
910 		sc->sc_mcr |= SER_RTS;
911 	}
912 	if (sigoff & SER_RTS) {
913 		sc->sc_mcr &= ~SER_RTS;
914 	}
915 	onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
916 	ucom_dtr(sc, onoff);
917 
918 	onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
919 	ucom_rts(sc, onoff);
920 
921 	return (0);
922 }
923 
924 static void
925 ucom_cfg_line_state(struct usb_proc_msg *_task)
926 {
927 	struct ucom_cfg_task *task =
928 	    (struct ucom_cfg_task *)_task;
929 	struct ucom_softc *sc = task->sc;
930 	uint8_t notch_bits;
931 	uint8_t any_bits;
932 	uint8_t prev_value;
933 	uint8_t last_value;
934 	uint8_t mask;
935 
936 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
937 		return;
938 	}
939 
940 	mask = 0;
941 	/* compute callback mask */
942 	if (sc->sc_callback->ucom_cfg_set_dtr)
943 		mask |= UCOM_LS_DTR;
944 	if (sc->sc_callback->ucom_cfg_set_rts)
945 		mask |= UCOM_LS_RTS;
946 	if (sc->sc_callback->ucom_cfg_set_break)
947 		mask |= UCOM_LS_BREAK;
948 	if (sc->sc_callback->ucom_cfg_set_ring)
949 		mask |= UCOM_LS_RING;
950 
951 	/* compute the bits we are to program */
952 	notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
953 	any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
954 	prev_value = sc->sc_pls_curr ^ notch_bits;
955 	last_value = sc->sc_pls_curr;
956 
957 	/* reset programmed line state */
958 	sc->sc_pls_curr = 0;
959 	sc->sc_pls_set = 0;
960 	sc->sc_pls_clr = 0;
961 
962 	/* ensure that we don't loose any levels */
963 	if (notch_bits & UCOM_LS_DTR)
964 		sc->sc_callback->ucom_cfg_set_dtr(sc,
965 		    (prev_value & UCOM_LS_DTR) ? 1 : 0);
966 	if (notch_bits & UCOM_LS_RTS)
967 		sc->sc_callback->ucom_cfg_set_rts(sc,
968 		    (prev_value & UCOM_LS_RTS) ? 1 : 0);
969 	if (notch_bits & UCOM_LS_BREAK)
970 		sc->sc_callback->ucom_cfg_set_break(sc,
971 		    (prev_value & UCOM_LS_BREAK) ? 1 : 0);
972 	if (notch_bits & UCOM_LS_RING)
973 		sc->sc_callback->ucom_cfg_set_ring(sc,
974 		    (prev_value & UCOM_LS_RING) ? 1 : 0);
975 
976 	/* set last value */
977 	if (any_bits & UCOM_LS_DTR)
978 		sc->sc_callback->ucom_cfg_set_dtr(sc,
979 		    (last_value & UCOM_LS_DTR) ? 1 : 0);
980 	if (any_bits & UCOM_LS_RTS)
981 		sc->sc_callback->ucom_cfg_set_rts(sc,
982 		    (last_value & UCOM_LS_RTS) ? 1 : 0);
983 	if (any_bits & UCOM_LS_BREAK)
984 		sc->sc_callback->ucom_cfg_set_break(sc,
985 		    (last_value & UCOM_LS_BREAK) ? 1 : 0);
986 	if (any_bits & UCOM_LS_RING)
987 		sc->sc_callback->ucom_cfg_set_ring(sc,
988 		    (last_value & UCOM_LS_RING) ? 1 : 0);
989 }
990 
991 static void
992 ucom_line_state(struct ucom_softc *sc,
993     uint8_t set_bits, uint8_t clear_bits)
994 {
995 	UCOM_MTX_ASSERT(sc, MA_OWNED);
996 
997 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
998 		return;
999 	}
1000 
1001 	DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1002 
1003 	/* update current programmed line state */
1004 	sc->sc_pls_curr |= set_bits;
1005 	sc->sc_pls_curr &= ~clear_bits;
1006 	sc->sc_pls_set |= set_bits;
1007 	sc->sc_pls_clr |= clear_bits;
1008 
1009 	/* defer driver programming */
1010 	ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1011 	    &sc->sc_line_state_task[0].hdr,
1012 	    &sc->sc_line_state_task[1].hdr);
1013 }
1014 
1015 static void
1016 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1017 {
1018 	DPRINTF("onoff = %d\n", onoff);
1019 
1020 	if (onoff)
1021 		ucom_line_state(sc, UCOM_LS_RING, 0);
1022 	else
1023 		ucom_line_state(sc, 0, UCOM_LS_RING);
1024 }
1025 
1026 static void
1027 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1028 {
1029 	DPRINTF("onoff = %d\n", onoff);
1030 
1031 	if (onoff)
1032 		ucom_line_state(sc, UCOM_LS_BREAK, 0);
1033 	else
1034 		ucom_line_state(sc, 0, UCOM_LS_BREAK);
1035 }
1036 
1037 static void
1038 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1039 {
1040 	DPRINTF("onoff = %d\n", onoff);
1041 
1042 	if (onoff)
1043 		ucom_line_state(sc, UCOM_LS_DTR, 0);
1044 	else
1045 		ucom_line_state(sc, 0, UCOM_LS_DTR);
1046 }
1047 
1048 static void
1049 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1050 {
1051 	DPRINTF("onoff = %d\n", onoff);
1052 
1053 	if (onoff)
1054 		ucom_line_state(sc, UCOM_LS_RTS, 0);
1055 	else
1056 		ucom_line_state(sc, 0, UCOM_LS_RTS);
1057 }
1058 
1059 static void
1060 ucom_cfg_status_change(struct usb_proc_msg *_task)
1061 {
1062 	struct ucom_cfg_task *task =
1063 	    (struct ucom_cfg_task *)_task;
1064 	struct ucom_softc *sc = task->sc;
1065 	struct tty *tp;
1066 	uint8_t new_msr;
1067 	uint8_t new_lsr;
1068 	uint8_t onoff;
1069 	uint8_t lsr_delta;
1070 
1071 	tp = sc->sc_tty;
1072 
1073 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1074 
1075 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1076 		return;
1077 	}
1078 	if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1079 		return;
1080 	}
1081 	/* get status */
1082 
1083 	new_msr = 0;
1084 	new_lsr = 0;
1085 
1086 	(sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1087 
1088 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1089 		/* TTY device closed */
1090 		return;
1091 	}
1092 	onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
1093 	lsr_delta = (sc->sc_lsr ^ new_lsr);
1094 
1095 	sc->sc_msr = new_msr;
1096 	sc->sc_lsr = new_lsr;
1097 
1098 	if (onoff) {
1099 
1100 		onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1101 
1102 		DPRINTF("DCD changed to %d\n", onoff);
1103 
1104 		ttydisc_modem(tp, onoff);
1105 	}
1106 
1107 	if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1108 
1109 		DPRINTF("BREAK detected\n");
1110 
1111 		ttydisc_rint(tp, 0, TRE_BREAK);
1112 		ttydisc_rint_done(tp);
1113 	}
1114 
1115 	if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1116 
1117 		DPRINTF("Frame error detected\n");
1118 
1119 		ttydisc_rint(tp, 0, TRE_FRAMING);
1120 		ttydisc_rint_done(tp);
1121 	}
1122 
1123 	if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1124 
1125 		DPRINTF("Parity error detected\n");
1126 
1127 		ttydisc_rint(tp, 0, TRE_PARITY);
1128 		ttydisc_rint_done(tp);
1129 	}
1130 }
1131 
1132 void
1133 ucom_status_change(struct ucom_softc *sc)
1134 {
1135 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1136 
1137 	if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1138 		return;		/* not supported */
1139 
1140 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1141 		return;
1142 	}
1143 	DPRINTF("\n");
1144 
1145 	ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1146 	    &sc->sc_status_task[0].hdr,
1147 	    &sc->sc_status_task[1].hdr);
1148 }
1149 
1150 static void
1151 ucom_cfg_param(struct usb_proc_msg *_task)
1152 {
1153 	struct ucom_param_task *task =
1154 	    (struct ucom_param_task *)_task;
1155 	struct ucom_softc *sc = task->sc;
1156 
1157 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1158 		return;
1159 	}
1160 	if (sc->sc_callback->ucom_cfg_param == NULL) {
1161 		return;
1162 	}
1163 
1164 	(sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1165 
1166 	/* wait a little */
1167 	usb_pause_mtx(sc->sc_mtx, hz / 10);
1168 }
1169 
1170 static int
1171 ucom_param(struct tty *tp, struct termios *t)
1172 {
1173 	struct ucom_softc *sc = tty_softc(tp);
1174 	uint8_t opened;
1175 	int error;
1176 
1177 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1178 
1179 	opened = 0;
1180 	error = 0;
1181 
1182 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1183 
1184 		/* XXX the TTY layer should call "open()" first! */
1185 		/*
1186 		 * Not quite: Its ordering is partly backwards, but
1187 		 * some parameters must be set early in ttydev_open(),
1188 		 * possibly before calling ttydevsw_open().
1189 		 */
1190 		error = ucom_open(tp);
1191 		if (error)
1192 			goto done;
1193 
1194 		opened = 1;
1195 	}
1196 	DPRINTF("sc = %p\n", sc);
1197 
1198 	/* Check requested parameters. */
1199 	if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1200 		/* XXX c_ospeed == 0 is perfectly valid. */
1201 		DPRINTF("mismatch ispeed and ospeed\n");
1202 		error = EINVAL;
1203 		goto done;
1204 	}
1205 	t->c_ispeed = t->c_ospeed;
1206 
1207 	if (sc->sc_callback->ucom_pre_param) {
1208 		/* Let the lower layer verify the parameters */
1209 		error = (sc->sc_callback->ucom_pre_param) (sc, t);
1210 		if (error) {
1211 			DPRINTF("callback error = %d\n", error);
1212 			goto done;
1213 		}
1214 	}
1215 
1216 	/* Disable transfers */
1217 	sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1218 
1219 	/* Queue baud rate programming command first */
1220 	ucom_queue_command(sc, ucom_cfg_param, t,
1221 	    &sc->sc_param_task[0].hdr,
1222 	    &sc->sc_param_task[1].hdr);
1223 
1224 	/* Queue transfer enable command last */
1225 	ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1226 	    &sc->sc_start_task[0].hdr,
1227 	    &sc->sc_start_task[1].hdr);
1228 
1229 	if (t->c_cflag & CRTS_IFLOW) {
1230 		sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1231 	} else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1232 		sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1233 		ucom_modem(tp, SER_RTS, 0);
1234 	}
1235 done:
1236 	if (error) {
1237 		if (opened) {
1238 			ucom_close(tp);
1239 		}
1240 	}
1241 	return (error);
1242 }
1243 
1244 static void
1245 ucom_outwakeup(struct tty *tp)
1246 {
1247 	struct ucom_softc *sc = tty_softc(tp);
1248 
1249 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1250 
1251 	DPRINTF("sc = %p\n", sc);
1252 
1253 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1254 		/* The higher layer is not ready */
1255 		return;
1256 	}
1257 	ucom_start_transfers(sc);
1258 }
1259 
1260 /*------------------------------------------------------------------------*
1261  *	ucom_get_data
1262  *
1263  * Return values:
1264  * 0: No data is available.
1265  * Else: Data is available.
1266  *------------------------------------------------------------------------*/
1267 uint8_t
1268 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1269     uint32_t offset, uint32_t len, uint32_t *actlen)
1270 {
1271 	struct usb_page_search res;
1272 	struct tty *tp = sc->sc_tty;
1273 	uint32_t cnt;
1274 	uint32_t offset_orig;
1275 
1276 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1277 
1278 	if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1279 		unsigned int temp;
1280 
1281 		/* get total TX length */
1282 
1283 		temp = ucom_cons_tx_high - ucom_cons_tx_low;
1284 		temp %= UCOM_CONS_BUFSIZE;
1285 
1286 		/* limit TX length */
1287 
1288 		if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1289 			temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1290 
1291 		if (temp > len)
1292 			temp = len;
1293 
1294 		/* copy in data */
1295 
1296 		usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1297 
1298 		/* update counters */
1299 
1300 		ucom_cons_tx_low += temp;
1301 		ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1302 
1303 		/* store actual length */
1304 
1305 		*actlen = temp;
1306 
1307 		return (temp ? 1 : 0);
1308 	}
1309 
1310 	if (tty_gone(tp) ||
1311 	    !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1312 		actlen[0] = 0;
1313 		return (0);		/* multiport device polling */
1314 	}
1315 	offset_orig = offset;
1316 
1317 	while (len != 0) {
1318 
1319 		usbd_get_page(pc, offset, &res);
1320 
1321 		if (res.length > len) {
1322 			res.length = len;
1323 		}
1324 		/* copy data directly into USB buffer */
1325 		cnt = ttydisc_getc(tp, res.buffer, res.length);
1326 
1327 		offset += cnt;
1328 		len -= cnt;
1329 
1330 		if (cnt < res.length) {
1331 			/* end of buffer */
1332 			break;
1333 		}
1334 	}
1335 
1336 	actlen[0] = offset - offset_orig;
1337 
1338 	DPRINTF("cnt=%d\n", actlen[0]);
1339 
1340 	if (actlen[0] == 0) {
1341 		return (0);
1342 	}
1343 	return (1);
1344 }
1345 
1346 void
1347 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1348     uint32_t offset, uint32_t len)
1349 {
1350 	struct usb_page_search res;
1351 	struct tty *tp = sc->sc_tty;
1352 	char *buf;
1353 	uint32_t cnt;
1354 
1355 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1356 
1357 	if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1358 		unsigned int temp;
1359 
1360 		/* get maximum RX length */
1361 
1362 		temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1363 		temp %= UCOM_CONS_BUFSIZE;
1364 
1365 		/* limit RX length */
1366 
1367 		if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1368 			temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1369 
1370 		if (temp > len)
1371 			temp = len;
1372 
1373 		/* copy out data */
1374 
1375 		usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1376 
1377 		/* update counters */
1378 
1379 		ucom_cons_rx_high += temp;
1380 		ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1381 
1382 		return;
1383 	}
1384 
1385 	if (tty_gone(tp))
1386 		return;			/* multiport device polling */
1387 
1388 	if (len == 0)
1389 		return;			/* no data */
1390 
1391 	/* set a flag to prevent recursation ? */
1392 
1393 	while (len > 0) {
1394 
1395 		usbd_get_page(pc, offset, &res);
1396 
1397 		if (res.length > len) {
1398 			res.length = len;
1399 		}
1400 		len -= res.length;
1401 		offset += res.length;
1402 
1403 		/* pass characters to tty layer */
1404 
1405 		buf = res.buffer;
1406 		cnt = res.length;
1407 
1408 		/* first check if we can pass the buffer directly */
1409 
1410 		if (ttydisc_can_bypass(tp)) {
1411 
1412 			/* clear any jitter buffer */
1413 			sc->sc_jitterbuf_in = 0;
1414 			sc->sc_jitterbuf_out = 0;
1415 
1416 			if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1417 				DPRINTF("tp=%p, data lost\n", tp);
1418 			}
1419 			continue;
1420 		}
1421 		/* need to loop */
1422 
1423 		for (cnt = 0; cnt != res.length; cnt++) {
1424 			if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1425 			    ttydisc_rint(tp, buf[cnt], 0) == -1) {
1426 				uint16_t end;
1427 				uint16_t pos;
1428 
1429 				pos = sc->sc_jitterbuf_in;
1430 				end = sc->sc_jitterbuf_out +
1431 				    UCOM_JITTERBUF_SIZE - 1;
1432 				if (end >= UCOM_JITTERBUF_SIZE)
1433 					end -= UCOM_JITTERBUF_SIZE;
1434 
1435 				for (; cnt != res.length; cnt++) {
1436 					if (pos == end)
1437 						break;
1438 					sc->sc_jitterbuf[pos] = buf[cnt];
1439 					pos++;
1440 					if (pos >= UCOM_JITTERBUF_SIZE)
1441 						pos -= UCOM_JITTERBUF_SIZE;
1442 				}
1443 
1444 				sc->sc_jitterbuf_in = pos;
1445 
1446 				/* set RTS in async fashion */
1447 				if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1448 					ucom_rts(sc, 1);
1449 
1450 				DPRINTF("tp=%p, lost %d "
1451 				    "chars\n", tp, res.length - cnt);
1452 				break;
1453 			}
1454 		}
1455 	}
1456 	ttydisc_rint_done(tp);
1457 }
1458 
1459 static void
1460 ucom_free(void *xsc)
1461 {
1462 	struct ucom_softc *sc = xsc;
1463 
1464 	if (sc->sc_callback->ucom_free != NULL)
1465 		sc->sc_callback->ucom_free(sc);
1466 	else
1467 		ucom_unref(sc->sc_super);
1468 
1469 	mtx_lock(&ucom_mtx);
1470 	ucom_close_refs--;
1471 	mtx_unlock(&ucom_mtx);
1472 }
1473 
1474 static cn_probe_t ucom_cnprobe;
1475 static cn_init_t ucom_cninit;
1476 static cn_term_t ucom_cnterm;
1477 static cn_getc_t ucom_cngetc;
1478 static cn_putc_t ucom_cnputc;
1479 static cn_grab_t ucom_cngrab;
1480 static cn_ungrab_t ucom_cnungrab;
1481 
1482 CONSOLE_DRIVER(ucom);
1483 
1484 static void
1485 ucom_cnprobe(struct consdev  *cp)
1486 {
1487 	if (ucom_cons_unit != -1)
1488 		cp->cn_pri = CN_NORMAL;
1489 	else
1490 		cp->cn_pri = CN_DEAD;
1491 
1492 	strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1493 }
1494 
1495 static void
1496 ucom_cninit(struct consdev  *cp)
1497 {
1498 }
1499 
1500 static void
1501 ucom_cnterm(struct consdev  *cp)
1502 {
1503 }
1504 
1505 static void
1506 ucom_cngrab(struct consdev *cp)
1507 {
1508 }
1509 
1510 static void
1511 ucom_cnungrab(struct consdev *cp)
1512 {
1513 }
1514 
1515 static int
1516 ucom_cngetc(struct consdev *cd)
1517 {
1518 	struct ucom_softc *sc = ucom_cons_softc;
1519 	int c;
1520 
1521 	if (sc == NULL)
1522 		return (-1);
1523 
1524 	UCOM_MTX_LOCK(sc);
1525 
1526 	if (ucom_cons_rx_low != ucom_cons_rx_high) {
1527 		c = ucom_cons_rx_buf[ucom_cons_rx_low];
1528 		ucom_cons_rx_low ++;
1529 		ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1530 	} else {
1531 		c = -1;
1532 	}
1533 
1534 	/* start USB transfers */
1535 	ucom_outwakeup(sc->sc_tty);
1536 
1537 	UCOM_MTX_UNLOCK(sc);
1538 
1539 	/* poll if necessary */
1540 	if (kdb_active && sc->sc_callback->ucom_poll)
1541 		(sc->sc_callback->ucom_poll) (sc);
1542 
1543 	return (c);
1544 }
1545 
1546 static void
1547 ucom_cnputc(struct consdev *cd, int c)
1548 {
1549 	struct ucom_softc *sc = ucom_cons_softc;
1550 	unsigned int temp;
1551 
1552 	if (sc == NULL)
1553 		return;
1554 
1555  repeat:
1556 
1557 	UCOM_MTX_LOCK(sc);
1558 
1559 	/* compute maximum TX length */
1560 
1561 	temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1562 	temp %= UCOM_CONS_BUFSIZE;
1563 
1564 	if (temp) {
1565 		ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1566 		ucom_cons_tx_high ++;
1567 		ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1568 	}
1569 
1570 	/* start USB transfers */
1571 	ucom_outwakeup(sc->sc_tty);
1572 
1573 	UCOM_MTX_UNLOCK(sc);
1574 
1575 	/* poll if necessary */
1576 	if (kdb_active && sc->sc_callback->ucom_poll) {
1577 		(sc->sc_callback->ucom_poll) (sc);
1578 		/* simple flow control */
1579 		if (temp == 0)
1580 			goto repeat;
1581 	}
1582 }
1583 
1584 /*------------------------------------------------------------------------*
1585  *	ucom_ref
1586  *
1587  * This function will increment the super UCOM reference count.
1588  *------------------------------------------------------------------------*/
1589 void
1590 ucom_ref(struct ucom_super_softc *ssc)
1591 {
1592 	mtx_lock(&ucom_mtx);
1593 	ssc->sc_refs++;
1594 	mtx_unlock(&ucom_mtx);
1595 }
1596 
1597 /*------------------------------------------------------------------------*
1598  *	ucom_free_unit
1599  *
1600  * This function will free the super UCOM's allocated unit
1601  * number. This function can be called on a zero-initialized
1602  * structure. This function can be called multiple times.
1603  *------------------------------------------------------------------------*/
1604 static void
1605 ucom_free_unit(struct ucom_super_softc *ssc)
1606 {
1607 	if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1608 		return;
1609 
1610 	ucom_unit_free(ssc->sc_unit);
1611 
1612 	ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1613 }
1614 
1615 /*------------------------------------------------------------------------*
1616  *	ucom_unref
1617  *
1618  * This function will decrement the super UCOM reference count.
1619  *
1620  * Return values:
1621  * 0: UCOM structures are still referenced.
1622  * Else: UCOM structures are no longer referenced.
1623  *------------------------------------------------------------------------*/
1624 int
1625 ucom_unref(struct ucom_super_softc *ssc)
1626 {
1627 	int retval;
1628 
1629 	mtx_lock(&ucom_mtx);
1630 	retval = (ssc->sc_refs < 2);
1631 	ssc->sc_refs--;
1632 	mtx_unlock(&ucom_mtx);
1633 
1634 	if (retval)
1635 		ucom_free_unit(ssc);
1636 
1637 	return (retval);
1638 }
1639 
1640 #if defined(GDB)
1641 
1642 #include <gdb/gdb.h>
1643 
1644 static gdb_probe_f ucom_gdbprobe;
1645 static gdb_init_f ucom_gdbinit;
1646 static gdb_term_f ucom_gdbterm;
1647 static gdb_getc_f ucom_gdbgetc;
1648 static gdb_putc_f ucom_gdbputc;
1649 
1650 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1651 
1652 static int
1653 ucom_gdbprobe(void)
1654 {
1655 	return ((ucom_cons_softc != NULL) ? 0 : -1);
1656 }
1657 
1658 static void
1659 ucom_gdbinit(void)
1660 {
1661 }
1662 
1663 static void
1664 ucom_gdbterm(void)
1665 {
1666 }
1667 
1668 static void
1669 ucom_gdbputc(int c)
1670 {
1671         ucom_cnputc(NULL, c);
1672 }
1673 
1674 static int
1675 ucom_gdbgetc(void)
1676 {
1677         return (ucom_cngetc(NULL));
1678 }
1679 
1680 #endif
1681