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