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