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