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