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