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