xref: /freebsd/sys/dev/usb/serial/usb_serial.c (revision f1951fd745b894fe6586c298874af98544a5e272)
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 	ucom_modem(tp, SER_DTR | SER_RTS, 0);
800 
801 	ucom_ring(sc, 0);
802 
803 	ucom_break(sc, 0);
804 
805 	ucom_status_change(sc);
806 
807 	return (0);
808 }
809 
810 static void
811 ucom_cfg_close(struct usb_proc_msg *_task)
812 {
813 	struct ucom_cfg_task *task =
814 	    (struct ucom_cfg_task *)_task;
815 	struct ucom_softc *sc = task->sc;
816 
817 	DPRINTF("\n");
818 
819 	if (sc->sc_flag & UCOM_FLAG_LL_READY) {
820 		sc->sc_flag &= ~UCOM_FLAG_LL_READY;
821 		if (sc->sc_callback->ucom_cfg_close)
822 			(sc->sc_callback->ucom_cfg_close) (sc);
823 	} else {
824 		/* already closed */
825 	}
826 }
827 
828 static void
829 ucom_close(struct tty *tp)
830 {
831 	struct ucom_softc *sc = tty_softc(tp);
832 
833 	UCOM_MTX_ASSERT(sc, MA_OWNED);
834 
835 	DPRINTF("tp=%p\n", tp);
836 
837 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
838 		DPRINTF("tp=%p already closed\n", tp);
839 		return;
840 	}
841 	ucom_shutdown(sc);
842 
843 	ucom_queue_command(sc, ucom_cfg_close, NULL,
844 	    &sc->sc_close_task[0].hdr,
845 	    &sc->sc_close_task[1].hdr);
846 
847 	sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
848 
849 	if (sc->sc_callback->ucom_stop_read) {
850 		(sc->sc_callback->ucom_stop_read) (sc);
851 	}
852 }
853 
854 static void
855 ucom_inwakeup(struct tty *tp)
856 {
857 	struct ucom_softc *sc = tty_softc(tp);
858 	uint16_t pos;
859 
860 	if (sc == NULL)
861 		return;
862 
863 	UCOM_MTX_ASSERT(sc, MA_OWNED);
864 
865 	DPRINTF("tp=%p\n", tp);
866 
867 	if (ttydisc_can_bypass(tp) != 0 ||
868 	    (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
869 	    (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
870 		return;
871 	}
872 
873 	/* prevent recursion */
874 	sc->sc_flag |= UCOM_FLAG_INWAKEUP;
875 
876 	pos = sc->sc_jitterbuf_out;
877 
878 	while (sc->sc_jitterbuf_in != pos) {
879 		int c;
880 
881 		c = (char)sc->sc_jitterbuf[pos];
882 
883 		if (ttydisc_rint(tp, c, 0) == -1)
884 			break;
885 		pos++;
886 		if (pos >= UCOM_JITTERBUF_SIZE)
887 			pos -= UCOM_JITTERBUF_SIZE;
888 	}
889 
890 	sc->sc_jitterbuf_out = pos;
891 
892 	/* clear RTS in async fashion */
893 	if ((sc->sc_jitterbuf_in == pos) &&
894 	    (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
895 		ucom_rts(sc, 0);
896 
897 	sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
898 }
899 
900 static int
901 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
902 {
903 	struct ucom_softc *sc = tty_softc(tp);
904 	int error;
905 
906 	UCOM_MTX_ASSERT(sc, MA_OWNED);
907 
908 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
909 		return (EIO);
910 	}
911 	DPRINTF("cmd = 0x%08lx\n", cmd);
912 
913 	switch (cmd) {
914 #if 0
915 	case TIOCSRING:
916 		ucom_ring(sc, 1);
917 		error = 0;
918 		break;
919 	case TIOCCRING:
920 		ucom_ring(sc, 0);
921 		error = 0;
922 		break;
923 #endif
924 	case TIOCSBRK:
925 		ucom_break(sc, 1);
926 		error = 0;
927 		break;
928 	case TIOCCBRK:
929 		ucom_break(sc, 0);
930 		error = 0;
931 		break;
932 	default:
933 		if (sc->sc_callback->ucom_ioctl) {
934 			error = (sc->sc_callback->ucom_ioctl)
935 			    (sc, cmd, data, 0, td);
936 		} else {
937 			error = ENOIOCTL;
938 		}
939 		if (error == ENOIOCTL)
940 			error = pps_ioctl(cmd, data, &sc->sc_pps);
941 		break;
942 	}
943 	return (error);
944 }
945 
946 static int
947 ucom_modem(struct tty *tp, int sigon, int sigoff)
948 {
949 	struct ucom_softc *sc = tty_softc(tp);
950 	uint8_t onoff;
951 
952 	UCOM_MTX_ASSERT(sc, MA_OWNED);
953 
954 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
955 		return (0);
956 	}
957 	if ((sigon == 0) && (sigoff == 0)) {
958 
959 		if (sc->sc_mcr & SER_DTR) {
960 			sigon |= SER_DTR;
961 		}
962 		if (sc->sc_mcr & SER_RTS) {
963 			sigon |= SER_RTS;
964 		}
965 		if (sc->sc_msr & SER_CTS) {
966 			sigon |= SER_CTS;
967 		}
968 		if (sc->sc_msr & SER_DCD) {
969 			sigon |= SER_DCD;
970 		}
971 		if (sc->sc_msr & SER_DSR) {
972 			sigon |= SER_DSR;
973 		}
974 		if (sc->sc_msr & SER_RI) {
975 			sigon |= SER_RI;
976 		}
977 		return (sigon);
978 	}
979 	if (sigon & SER_DTR) {
980 		sc->sc_mcr |= SER_DTR;
981 	}
982 	if (sigoff & SER_DTR) {
983 		sc->sc_mcr &= ~SER_DTR;
984 	}
985 	if (sigon & SER_RTS) {
986 		sc->sc_mcr |= SER_RTS;
987 	}
988 	if (sigoff & SER_RTS) {
989 		sc->sc_mcr &= ~SER_RTS;
990 	}
991 	onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
992 	ucom_dtr(sc, onoff);
993 
994 	onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
995 	ucom_rts(sc, onoff);
996 
997 	return (0);
998 }
999 
1000 static void
1001 ucom_cfg_line_state(struct usb_proc_msg *_task)
1002 {
1003 	struct ucom_cfg_task *task =
1004 	    (struct ucom_cfg_task *)_task;
1005 	struct ucom_softc *sc = task->sc;
1006 	uint8_t notch_bits;
1007 	uint8_t any_bits;
1008 	uint8_t prev_value;
1009 	uint8_t last_value;
1010 	uint8_t mask;
1011 
1012 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1013 		return;
1014 	}
1015 
1016 	mask = 0;
1017 	/* compute callback mask */
1018 	if (sc->sc_callback->ucom_cfg_set_dtr)
1019 		mask |= UCOM_LS_DTR;
1020 	if (sc->sc_callback->ucom_cfg_set_rts)
1021 		mask |= UCOM_LS_RTS;
1022 	if (sc->sc_callback->ucom_cfg_set_break)
1023 		mask |= UCOM_LS_BREAK;
1024 	if (sc->sc_callback->ucom_cfg_set_ring)
1025 		mask |= UCOM_LS_RING;
1026 
1027 	/* compute the bits we are to program */
1028 	notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
1029 	any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
1030 	prev_value = sc->sc_pls_curr ^ notch_bits;
1031 	last_value = sc->sc_pls_curr;
1032 
1033 	/* reset programmed line state */
1034 	sc->sc_pls_curr = 0;
1035 	sc->sc_pls_set = 0;
1036 	sc->sc_pls_clr = 0;
1037 
1038 	/* ensure that we don't lose any levels */
1039 	if (notch_bits & UCOM_LS_DTR)
1040 		sc->sc_callback->ucom_cfg_set_dtr(sc,
1041 		    (prev_value & UCOM_LS_DTR) ? 1 : 0);
1042 	if (notch_bits & UCOM_LS_RTS)
1043 		sc->sc_callback->ucom_cfg_set_rts(sc,
1044 		    (prev_value & UCOM_LS_RTS) ? 1 : 0);
1045 	if (notch_bits & UCOM_LS_BREAK)
1046 		sc->sc_callback->ucom_cfg_set_break(sc,
1047 		    (prev_value & UCOM_LS_BREAK) ? 1 : 0);
1048 	if (notch_bits & UCOM_LS_RING)
1049 		sc->sc_callback->ucom_cfg_set_ring(sc,
1050 		    (prev_value & UCOM_LS_RING) ? 1 : 0);
1051 
1052 	/* set last value */
1053 	if (any_bits & UCOM_LS_DTR)
1054 		sc->sc_callback->ucom_cfg_set_dtr(sc,
1055 		    (last_value & UCOM_LS_DTR) ? 1 : 0);
1056 	if (any_bits & UCOM_LS_RTS)
1057 		sc->sc_callback->ucom_cfg_set_rts(sc,
1058 		    (last_value & UCOM_LS_RTS) ? 1 : 0);
1059 	if (any_bits & UCOM_LS_BREAK)
1060 		sc->sc_callback->ucom_cfg_set_break(sc,
1061 		    (last_value & UCOM_LS_BREAK) ? 1 : 0);
1062 	if (any_bits & UCOM_LS_RING)
1063 		sc->sc_callback->ucom_cfg_set_ring(sc,
1064 		    (last_value & UCOM_LS_RING) ? 1 : 0);
1065 }
1066 
1067 static void
1068 ucom_line_state(struct ucom_softc *sc,
1069     uint8_t set_bits, uint8_t clear_bits)
1070 {
1071 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1072 
1073 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1074 		return;
1075 	}
1076 
1077 	DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1078 
1079 	/* update current programmed line state */
1080 	sc->sc_pls_curr |= set_bits;
1081 	sc->sc_pls_curr &= ~clear_bits;
1082 	sc->sc_pls_set |= set_bits;
1083 	sc->sc_pls_clr |= clear_bits;
1084 
1085 	/* defer driver programming */
1086 	ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1087 	    &sc->sc_line_state_task[0].hdr,
1088 	    &sc->sc_line_state_task[1].hdr);
1089 }
1090 
1091 static void
1092 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1093 {
1094 	DPRINTF("onoff = %d\n", onoff);
1095 
1096 	if (onoff)
1097 		ucom_line_state(sc, UCOM_LS_RING, 0);
1098 	else
1099 		ucom_line_state(sc, 0, UCOM_LS_RING);
1100 }
1101 
1102 static void
1103 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1104 {
1105 	DPRINTF("onoff = %d\n", onoff);
1106 
1107 	if (onoff)
1108 		ucom_line_state(sc, UCOM_LS_BREAK, 0);
1109 	else
1110 		ucom_line_state(sc, 0, UCOM_LS_BREAK);
1111 }
1112 
1113 static void
1114 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1115 {
1116 	DPRINTF("onoff = %d\n", onoff);
1117 
1118 	if (onoff)
1119 		ucom_line_state(sc, UCOM_LS_DTR, 0);
1120 	else
1121 		ucom_line_state(sc, 0, UCOM_LS_DTR);
1122 }
1123 
1124 static void
1125 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1126 {
1127 	DPRINTF("onoff = %d\n", onoff);
1128 
1129 	if (onoff)
1130 		ucom_line_state(sc, UCOM_LS_RTS, 0);
1131 	else
1132 		ucom_line_state(sc, 0, UCOM_LS_RTS);
1133 }
1134 
1135 static void
1136 ucom_cfg_status_change(struct usb_proc_msg *_task)
1137 {
1138 	struct ucom_cfg_task *task =
1139 	    (struct ucom_cfg_task *)_task;
1140 	struct ucom_softc *sc = task->sc;
1141 	struct tty *tp;
1142 	int onoff;
1143 	uint8_t new_msr;
1144 	uint8_t new_lsr;
1145 	uint8_t msr_delta;
1146 	uint8_t lsr_delta;
1147 	uint8_t pps_signal;
1148 
1149 	tp = sc->sc_tty;
1150 
1151 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1152 
1153 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1154 		return;
1155 	}
1156 	if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1157 		return;
1158 	}
1159 	/* get status */
1160 
1161 	new_msr = 0;
1162 	new_lsr = 0;
1163 
1164 	(sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1165 
1166 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1167 		/* TTY device closed */
1168 		return;
1169 	}
1170 	msr_delta = (sc->sc_msr ^ new_msr);
1171 	lsr_delta = (sc->sc_lsr ^ new_lsr);
1172 
1173 	sc->sc_msr = new_msr;
1174 	sc->sc_lsr = new_lsr;
1175 
1176 	/*
1177 	 * Time pulse counting support.
1178 	 */
1179 	switch(ucom_pps_mode & UART_PPS_SIGNAL_MASK) {
1180 	case UART_PPS_CTS:
1181 		pps_signal = SER_CTS;
1182 		break;
1183 	case UART_PPS_DCD:
1184 		pps_signal = SER_DCD;
1185 		break;
1186 	default:
1187 		pps_signal = 0;
1188 		break;
1189 	}
1190 
1191 	if ((sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) &&
1192 	    (msr_delta & pps_signal)) {
1193 		pps_capture(&sc->sc_pps);
1194 		onoff = (sc->sc_msr & pps_signal) ? 1 : 0;
1195 		if (ucom_pps_mode & UART_PPS_INVERT_PULSE)
1196 			onoff = !onoff;
1197 		pps_event(&sc->sc_pps, onoff ? PPS_CAPTUREASSERT :
1198 		    PPS_CAPTURECLEAR);
1199 	}
1200 
1201 	if (msr_delta & SER_DCD) {
1202 
1203 		onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1204 
1205 		DPRINTF("DCD changed to %d\n", onoff);
1206 
1207 		ttydisc_modem(tp, onoff);
1208 	}
1209 
1210 	if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1211 
1212 		DPRINTF("BREAK detected\n");
1213 
1214 		ttydisc_rint(tp, 0, TRE_BREAK);
1215 		ttydisc_rint_done(tp);
1216 	}
1217 
1218 	if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1219 
1220 		DPRINTF("Frame error detected\n");
1221 
1222 		ttydisc_rint(tp, 0, TRE_FRAMING);
1223 		ttydisc_rint_done(tp);
1224 	}
1225 
1226 	if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1227 
1228 		DPRINTF("Parity error detected\n");
1229 
1230 		ttydisc_rint(tp, 0, TRE_PARITY);
1231 		ttydisc_rint_done(tp);
1232 	}
1233 }
1234 
1235 void
1236 ucom_status_change(struct ucom_softc *sc)
1237 {
1238 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1239 
1240 	if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1241 		return;		/* not supported */
1242 
1243 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1244 		return;
1245 	}
1246 	DPRINTF("\n");
1247 
1248 	ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1249 	    &sc->sc_status_task[0].hdr,
1250 	    &sc->sc_status_task[1].hdr);
1251 }
1252 
1253 static void
1254 ucom_cfg_param(struct usb_proc_msg *_task)
1255 {
1256 	struct ucom_param_task *task =
1257 	    (struct ucom_param_task *)_task;
1258 	struct ucom_softc *sc = task->sc;
1259 
1260 	if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1261 		return;
1262 	}
1263 	if (sc->sc_callback->ucom_cfg_param == NULL) {
1264 		return;
1265 	}
1266 
1267 	(sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1268 
1269 	/* wait a little */
1270 	usb_pause_mtx(sc->sc_mtx, hz / 10);
1271 }
1272 
1273 static int
1274 ucom_param(struct tty *tp, struct termios *t)
1275 {
1276 	struct ucom_softc *sc = tty_softc(tp);
1277 	uint8_t opened;
1278 	int error;
1279 
1280 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1281 
1282 	opened = 0;
1283 	error = 0;
1284 
1285 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1286 
1287 		/* XXX the TTY layer should call "open()" first! */
1288 		/*
1289 		 * Not quite: Its ordering is partly backwards, but
1290 		 * some parameters must be set early in ttydev_open(),
1291 		 * possibly before calling ttydevsw_open().
1292 		 */
1293 		error = ucom_open(tp);
1294 		if (error)
1295 			goto done;
1296 
1297 		opened = 1;
1298 	}
1299 	DPRINTF("sc = %p\n", sc);
1300 
1301 	/* Check requested parameters. */
1302 	if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1303 		/* XXX c_ospeed == 0 is perfectly valid. */
1304 		DPRINTF("mismatch ispeed and ospeed\n");
1305 		error = EINVAL;
1306 		goto done;
1307 	}
1308 	t->c_ispeed = t->c_ospeed;
1309 
1310 	if (sc->sc_callback->ucom_pre_param) {
1311 		/* Let the lower layer verify the parameters */
1312 		error = (sc->sc_callback->ucom_pre_param) (sc, t);
1313 		if (error) {
1314 			DPRINTF("callback error = %d\n", error);
1315 			goto done;
1316 		}
1317 	}
1318 
1319 	/* Disable transfers */
1320 	sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1321 
1322 	/* Queue baud rate programming command first */
1323 	ucom_queue_command(sc, ucom_cfg_param, t,
1324 	    &sc->sc_param_task[0].hdr,
1325 	    &sc->sc_param_task[1].hdr);
1326 
1327 	/* Queue transfer enable command last */
1328 	ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1329 	    &sc->sc_start_task[0].hdr,
1330 	    &sc->sc_start_task[1].hdr);
1331 
1332 	if (t->c_cflag & CRTS_IFLOW) {
1333 		sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1334 	} else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1335 		sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1336 		ucom_modem(tp, SER_RTS, 0);
1337 	}
1338 done:
1339 	if (error) {
1340 		if (opened) {
1341 			ucom_close(tp);
1342 		}
1343 	}
1344 	return (error);
1345 }
1346 
1347 static void
1348 ucom_outwakeup(struct tty *tp)
1349 {
1350 	struct ucom_softc *sc = tty_softc(tp);
1351 
1352 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1353 
1354 	DPRINTF("sc = %p\n", sc);
1355 
1356 	if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1357 		/* The higher layer is not ready */
1358 		return;
1359 	}
1360 	ucom_start_transfers(sc);
1361 }
1362 
1363 static bool
1364 ucom_busy(struct tty *tp)
1365 {
1366 	struct ucom_softc *sc = tty_softc(tp);
1367 	const uint8_t txidle = ULSR_TXRDY | ULSR_TSRE;
1368 
1369 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1370 
1371 	DPRINTFN(3, "sc = %p lsr 0x%02x\n", sc, sc->sc_lsr);
1372 
1373 	/*
1374 	 * If the driver maintains the txidle bits in LSR, we can use them to
1375 	 * determine whether the transmitter is busy or idle.  Otherwise we have
1376 	 * to assume it is idle to avoid hanging forever on tcdrain(3).
1377 	 */
1378 	if (sc->sc_flag & UCOM_FLAG_LSRTXIDLE)
1379 		return ((sc->sc_lsr & txidle) != txidle);
1380 	else
1381 		return (false);
1382 }
1383 
1384 /*------------------------------------------------------------------------*
1385  *	ucom_get_data
1386  *
1387  * Return values:
1388  * 0: No data is available.
1389  * Else: Data is available.
1390  *------------------------------------------------------------------------*/
1391 uint8_t
1392 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1393     uint32_t offset, uint32_t len, uint32_t *actlen)
1394 {
1395 	struct usb_page_search res;
1396 	struct tty *tp = sc->sc_tty;
1397 	uint32_t cnt;
1398 	uint32_t offset_orig;
1399 
1400 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1401 
1402 	if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1403 		unsigned int temp;
1404 
1405 		/* get total TX length */
1406 
1407 		temp = ucom_cons_tx_high - ucom_cons_tx_low;
1408 		temp %= UCOM_CONS_BUFSIZE;
1409 
1410 		/* limit TX length */
1411 
1412 		if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1413 			temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1414 
1415 		if (temp > len)
1416 			temp = len;
1417 
1418 		/* copy in data */
1419 
1420 		usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1421 
1422 		/* update counters */
1423 
1424 		ucom_cons_tx_low += temp;
1425 		ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1426 
1427 		/* store actual length */
1428 
1429 		*actlen = temp;
1430 
1431 		return (temp ? 1 : 0);
1432 	}
1433 
1434 	if (tty_gone(tp) ||
1435 	    !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1436 		actlen[0] = 0;
1437 		return (0);		/* multiport device polling */
1438 	}
1439 	offset_orig = offset;
1440 
1441 	while (len != 0) {
1442 
1443 		usbd_get_page(pc, offset, &res);
1444 
1445 		if (res.length > len) {
1446 			res.length = len;
1447 		}
1448 		/* copy data directly into USB buffer */
1449 		cnt = ttydisc_getc(tp, res.buffer, res.length);
1450 
1451 		offset += cnt;
1452 		len -= cnt;
1453 
1454 		if (cnt < res.length) {
1455 			/* end of buffer */
1456 			break;
1457 		}
1458 	}
1459 
1460 	actlen[0] = offset - offset_orig;
1461 
1462 	DPRINTF("cnt=%d\n", actlen[0]);
1463 
1464 	if (actlen[0] == 0) {
1465 		return (0);
1466 	}
1467 	return (1);
1468 }
1469 
1470 void
1471 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1472     uint32_t offset, uint32_t len)
1473 {
1474 	struct usb_page_search res;
1475 	struct tty *tp = sc->sc_tty;
1476 	char *buf;
1477 	uint32_t cnt;
1478 
1479 	UCOM_MTX_ASSERT(sc, MA_OWNED);
1480 
1481 	if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1482 		unsigned int temp;
1483 
1484 		/* get maximum RX length */
1485 
1486 		temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1487 		temp %= UCOM_CONS_BUFSIZE;
1488 
1489 		/* limit RX length */
1490 
1491 		if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1492 			temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1493 
1494 		if (temp > len)
1495 			temp = len;
1496 
1497 		/* copy out data */
1498 
1499 		usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1500 
1501 		/* update counters */
1502 
1503 		ucom_cons_rx_high += temp;
1504 		ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1505 
1506 		return;
1507 	}
1508 
1509 	if (tty_gone(tp))
1510 		return;			/* multiport device polling */
1511 
1512 	if (len == 0)
1513 		return;			/* no data */
1514 
1515 	/* set a flag to prevent recursation ? */
1516 
1517 	while (len > 0) {
1518 
1519 		usbd_get_page(pc, offset, &res);
1520 
1521 		if (res.length > len) {
1522 			res.length = len;
1523 		}
1524 		len -= res.length;
1525 		offset += res.length;
1526 
1527 		/* pass characters to tty layer */
1528 
1529 		buf = res.buffer;
1530 		cnt = res.length;
1531 
1532 		/* first check if we can pass the buffer directly */
1533 
1534 		if (ttydisc_can_bypass(tp)) {
1535 
1536 			/* clear any jitter buffer */
1537 			sc->sc_jitterbuf_in = 0;
1538 			sc->sc_jitterbuf_out = 0;
1539 
1540 			if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1541 				DPRINTF("tp=%p, data lost\n", tp);
1542 			}
1543 			continue;
1544 		}
1545 		/* need to loop */
1546 
1547 		for (cnt = 0; cnt != res.length; cnt++) {
1548 			if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1549 			    ttydisc_rint(tp, buf[cnt], 0) == -1) {
1550 				uint16_t end;
1551 				uint16_t pos;
1552 
1553 				pos = sc->sc_jitterbuf_in;
1554 				end = sc->sc_jitterbuf_out +
1555 				    UCOM_JITTERBUF_SIZE - 1;
1556 				if (end >= UCOM_JITTERBUF_SIZE)
1557 					end -= UCOM_JITTERBUF_SIZE;
1558 
1559 				for (; cnt != res.length; cnt++) {
1560 					if (pos == end)
1561 						break;
1562 					sc->sc_jitterbuf[pos] = buf[cnt];
1563 					pos++;
1564 					if (pos >= UCOM_JITTERBUF_SIZE)
1565 						pos -= UCOM_JITTERBUF_SIZE;
1566 				}
1567 
1568 				sc->sc_jitterbuf_in = pos;
1569 
1570 				/* set RTS in async fashion */
1571 				if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1572 					ucom_rts(sc, 1);
1573 
1574 				DPRINTF("tp=%p, lost %d "
1575 				    "chars\n", tp, res.length - cnt);
1576 				break;
1577 			}
1578 		}
1579 	}
1580 	ttydisc_rint_done(tp);
1581 }
1582 
1583 static void
1584 ucom_free(void *xsc)
1585 {
1586 	struct ucom_softc *sc = xsc;
1587 
1588 	if (sc->sc_callback->ucom_free != NULL)
1589 		sc->sc_callback->ucom_free(sc);
1590 	else
1591 		ucom_unref(sc->sc_super);
1592 
1593 	mtx_lock(&ucom_mtx);
1594 	ucom_close_refs--;
1595 	mtx_unlock(&ucom_mtx);
1596 }
1597 
1598 CONSOLE_DRIVER(ucom);
1599 
1600 static void
1601 ucom_cnprobe(struct consdev  *cp)
1602 {
1603 	if (ucom_cons_unit != -1)
1604 		cp->cn_pri = CN_NORMAL;
1605 	else
1606 		cp->cn_pri = CN_DEAD;
1607 
1608 	strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1609 }
1610 
1611 static void
1612 ucom_cninit(struct consdev  *cp)
1613 {
1614 }
1615 
1616 static void
1617 ucom_cnterm(struct consdev  *cp)
1618 {
1619 }
1620 
1621 static void
1622 ucom_cngrab(struct consdev *cp)
1623 {
1624 }
1625 
1626 static void
1627 ucom_cnungrab(struct consdev *cp)
1628 {
1629 }
1630 
1631 static int
1632 ucom_cngetc(struct consdev *cd)
1633 {
1634 	struct ucom_softc *sc = ucom_cons_softc;
1635 	int c;
1636 
1637 	if (sc == NULL)
1638 		return (-1);
1639 
1640 	UCOM_MTX_LOCK(sc);
1641 
1642 	if (ucom_cons_rx_low != ucom_cons_rx_high) {
1643 		c = ucom_cons_rx_buf[ucom_cons_rx_low];
1644 		ucom_cons_rx_low ++;
1645 		ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1646 	} else {
1647 		c = -1;
1648 	}
1649 
1650 	/* start USB transfers */
1651 	ucom_outwakeup(sc->sc_tty);
1652 
1653 	UCOM_MTX_UNLOCK(sc);
1654 
1655 	/* poll if necessary */
1656 	if (USB_IN_POLLING_MODE_FUNC() && sc->sc_callback->ucom_poll)
1657 		(sc->sc_callback->ucom_poll) (sc);
1658 
1659 	return (c);
1660 }
1661 
1662 static void
1663 ucom_cnputc(struct consdev *cd, int c)
1664 {
1665 	struct ucom_softc *sc = ucom_cons_softc;
1666 	unsigned int temp;
1667 
1668 	if (sc == NULL)
1669 		return;
1670 
1671  repeat:
1672 
1673 	UCOM_MTX_LOCK(sc);
1674 
1675 	/* compute maximum TX length */
1676 
1677 	temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1678 	temp %= UCOM_CONS_BUFSIZE;
1679 
1680 	if (temp) {
1681 		ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1682 		ucom_cons_tx_high ++;
1683 		ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1684 	}
1685 
1686 	/* start USB transfers */
1687 	ucom_outwakeup(sc->sc_tty);
1688 
1689 	UCOM_MTX_UNLOCK(sc);
1690 
1691 	/* poll if necessary */
1692 	if (USB_IN_POLLING_MODE_FUNC() && sc->sc_callback->ucom_poll) {
1693 		(sc->sc_callback->ucom_poll) (sc);
1694 		/* simple flow control */
1695 		if (temp == 0)
1696 			goto repeat;
1697 	}
1698 }
1699 
1700 /*------------------------------------------------------------------------*
1701  *	ucom_ref
1702  *
1703  * This function will increment the super UCOM reference count.
1704  *------------------------------------------------------------------------*/
1705 void
1706 ucom_ref(struct ucom_super_softc *ssc)
1707 {
1708 	mtx_lock(&ucom_mtx);
1709 	ssc->sc_refs++;
1710 	mtx_unlock(&ucom_mtx);
1711 }
1712 
1713 /*------------------------------------------------------------------------*
1714  *	ucom_free_unit
1715  *
1716  * This function will free the super UCOM's allocated unit
1717  * number. This function can be called on a zero-initialized
1718  * structure. This function can be called multiple times.
1719  *------------------------------------------------------------------------*/
1720 static void
1721 ucom_free_unit(struct ucom_super_softc *ssc)
1722 {
1723 	if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1724 		return;
1725 
1726 	ucom_unit_free(ssc->sc_unit);
1727 
1728 	ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1729 }
1730 
1731 /*------------------------------------------------------------------------*
1732  *	ucom_unref
1733  *
1734  * This function will decrement the super UCOM reference count.
1735  *
1736  * Return values:
1737  * 0: UCOM structures are still referenced.
1738  * Else: UCOM structures are no longer referenced.
1739  *------------------------------------------------------------------------*/
1740 int
1741 ucom_unref(struct ucom_super_softc *ssc)
1742 {
1743 	int retval;
1744 
1745 	mtx_lock(&ucom_mtx);
1746 	retval = (ssc->sc_refs < 2);
1747 	ssc->sc_refs--;
1748 	mtx_unlock(&ucom_mtx);
1749 
1750 	if (retval)
1751 		ucom_free_unit(ssc);
1752 
1753 	return (retval);
1754 }
1755 
1756 #if defined(GDB)
1757 
1758 #include <gdb/gdb.h>
1759 
1760 static gdb_probe_f ucom_gdbprobe;
1761 static gdb_init_f ucom_gdbinit;
1762 static gdb_term_f ucom_gdbterm;
1763 static gdb_getc_f ucom_gdbgetc;
1764 static gdb_putc_f ucom_gdbputc;
1765 
1766 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1767 
1768 static int
1769 ucom_gdbprobe(void)
1770 {
1771 	return ((ucom_cons_softc != NULL) ? 0 : -1);
1772 }
1773 
1774 static void
1775 ucom_gdbinit(void)
1776 {
1777 }
1778 
1779 static void
1780 ucom_gdbterm(void)
1781 {
1782 }
1783 
1784 static void
1785 ucom_gdbputc(int c)
1786 {
1787         ucom_cnputc(NULL, c);
1788 }
1789 
1790 static int
1791 ucom_gdbgetc(void)
1792 {
1793         return (ucom_cngetc(NULL));
1794 }
1795 
1796 #endif
1797