xref: /freebsd/sys/netgraph/ng_tty.c (revision 053359b7f43aa87c06bef8be6d4fbea48bae4ca9)
14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer  * ng_tty.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
64cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
74cf49a43SJulian Elischer  * All rights reserved.
84cf49a43SJulian Elischer  *
94cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
104cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
114cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
124cf49a43SJulian Elischer  * provided, however, that:
134cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
144cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
154cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
164cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
174cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
184cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
194cf49a43SJulian Elischer  *
204cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
214cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
224cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
234cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
244cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
254cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
264cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
274cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
284cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
294cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
304cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
314cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
324cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
334cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
354cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
364cf49a43SJulian Elischer  * OF SUCH DAMAGE.
374cf49a43SJulian Elischer  *
38cc3bbd68SJulian Elischer  * Author: Archie Cobbs <archie@freebsd.org>
394cf49a43SJulian Elischer  *
409087c349SAndrew Thompson  * Updated by Andrew Thompson <thompsa@FreeBSD.org> for MPSAFE TTY.
419087c349SAndrew Thompson  *
424cf49a43SJulian Elischer  * $FreeBSD$
4374f5c6aaSJulian Elischer  * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
444cf49a43SJulian Elischer  */
454cf49a43SJulian Elischer 
464cf49a43SJulian Elischer /*
479087c349SAndrew Thompson  * This file implements TTY hooks to link in to the netgraph system.  The node
489087c349SAndrew Thompson  * is created and then passed the callers opened TTY file descriptor number to
499087c349SAndrew Thompson  * NGM_TTY_SET_TTY, this will hook the tty via ttyhook_register().
504cf49a43SJulian Elischer  *
519087c349SAndrew Thompson  * Incoming data is delivered directly to ng_tty via the TTY bypass hook as a
529087c349SAndrew Thompson  * buffer pointer and length, this is converted to a mbuf and passed to the
539087c349SAndrew Thompson  * peer.
544cf49a43SJulian Elischer  *
559087c349SAndrew Thompson  * If the TTY device does not support bypass then incoming characters are
569087c349SAndrew Thompson  * delivered to the hook one at a time, each in its own mbuf. You may
579087c349SAndrew Thompson  * optionally define a ``hotchar,'' which causes incoming characters to be
589087c349SAndrew Thompson  * buffered up until either the hotchar is seen or the mbuf is full (MHLEN
599087c349SAndrew Thompson  * bytes). Then all buffered characters are immediately delivered.
604cf49a43SJulian Elischer  */
614cf49a43SJulian Elischer 
624cf49a43SJulian Elischer #include <sys/param.h>
634cf49a43SJulian Elischer #include <sys/systm.h>
644cf49a43SJulian Elischer #include <sys/conf.h>
65d1426d7fSGleb Smirnoff #include <sys/errno.h>
664cf49a43SJulian Elischer #include <sys/fcntl.h>
67d1426d7fSGleb Smirnoff #include <sys/ioccom.h>
68d1426d7fSGleb Smirnoff #include <sys/kernel.h>
69d1426d7fSGleb Smirnoff #include <sys/malloc.h>
70d1426d7fSGleb Smirnoff #include <sys/mbuf.h>
71acd3428bSRobert Watson #include <sys/priv.h>
72d1426d7fSGleb Smirnoff #include <sys/socket.h>
73d1426d7fSGleb Smirnoff #include <sys/syslog.h>
744cf49a43SJulian Elischer #include <sys/tty.h>
75b58a8a3bSJulian Elischer #include <sys/ttycom.h>
7645056382SAlexander Motin #include <sys/proc.h>
77d1426d7fSGleb Smirnoff 
78d1426d7fSGleb Smirnoff #include <net/if.h>
79d1426d7fSGleb Smirnoff #include <net/if_var.h>
804cf49a43SJulian Elischer 
814cf49a43SJulian Elischer #include <netgraph/ng_message.h>
824cf49a43SJulian Elischer #include <netgraph/netgraph.h>
834cf49a43SJulian Elischer #include <netgraph/ng_tty.h>
844cf49a43SJulian Elischer 
854cf49a43SJulian Elischer /* Per-node private info */
869087c349SAndrew Thompson struct ngt_softc {
874cf49a43SJulian Elischer 	struct tty	*tp;		/* Terminal device */
884cf49a43SJulian Elischer 	node_p		node;		/* Netgraph node */
894cf49a43SJulian Elischer 	hook_p		hook;		/* Netgraph hook */
90d1426d7fSGleb Smirnoff 	struct ifqueue	outq;		/* Queue of outgoing data */
919087c349SAndrew Thompson 	size_t		outqlen;	/* Number of bytes in outq */
929087c349SAndrew Thompson 	struct mbuf	*m;		/* Incoming non-bypass data buffer */
934cf49a43SJulian Elischer 	short		hotchar;	/* Hotchar, or -1 if none */
944cf49a43SJulian Elischer 	u_int		flags;		/* Flags */
954cf49a43SJulian Elischer };
969087c349SAndrew Thompson typedef struct ngt_softc *sc_p;
979087c349SAndrew Thompson 
984cf49a43SJulian Elischer /* Flags */
994cf49a43SJulian Elischer #define FLG_DEBUG		0x0002
1004cf49a43SJulian Elischer 
1014cf49a43SJulian Elischer /* Netgraph methods */
10274f5c6aaSJulian Elischer static ng_constructor_t		ngt_constructor;
10374f5c6aaSJulian Elischer static ng_rcvmsg_t		ngt_rcvmsg;
10474f5c6aaSJulian Elischer static ng_shutdown_t		ngt_shutdown;
10574f5c6aaSJulian Elischer static ng_newhook_t		ngt_newhook;
106859a4d16SJulian Elischer static ng_connect_t		ngt_connect;
10774f5c6aaSJulian Elischer static ng_rcvdata_t		ngt_rcvdata;
10874f5c6aaSJulian Elischer static ng_disconnect_t		ngt_disconnect;
1094cf49a43SJulian Elischer 
1104cf49a43SJulian Elischer #define ERROUT(x)		do { error = (x); goto done; } while (0)
1114cf49a43SJulian Elischer 
1129087c349SAndrew Thompson static th_getc_inject_t		ngt_getc_inject;
1139087c349SAndrew Thompson static th_getc_poll_t		ngt_getc_poll;
1149087c349SAndrew Thompson static th_rint_t		ngt_rint;
1159087c349SAndrew Thompson static th_rint_bypass_t		ngt_rint_bypass;
1169087c349SAndrew Thompson static th_rint_poll_t		ngt_rint_poll;
1179087c349SAndrew Thompson 
1189087c349SAndrew Thompson static struct ttyhook ngt_hook = {
1199087c349SAndrew Thompson 	.th_getc_inject = ngt_getc_inject,
1209087c349SAndrew Thompson 	.th_getc_poll = ngt_getc_poll,
1219087c349SAndrew Thompson 	.th_rint = ngt_rint,
1229087c349SAndrew Thompson 	.th_rint_bypass = ngt_rint_bypass,
1239087c349SAndrew Thompson 	.th_rint_poll = ngt_rint_poll,
1244cf49a43SJulian Elischer };
1254cf49a43SJulian Elischer 
1264cf49a43SJulian Elischer /* Netgraph node type descriptor */
1274cf49a43SJulian Elischer static struct ng_type typestruct = {
128f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
129f8aae777SJulian Elischer 	.name =		NG_TTY_NODE_TYPE,
130f8aae777SJulian Elischer 	.constructor =	ngt_constructor,
131f8aae777SJulian Elischer 	.rcvmsg =	ngt_rcvmsg,
132f8aae777SJulian Elischer 	.shutdown =	ngt_shutdown,
133f8aae777SJulian Elischer 	.newhook =	ngt_newhook,
134f8aae777SJulian Elischer 	.connect =	ngt_connect,
135f8aae777SJulian Elischer 	.rcvdata =	ngt_rcvdata,
136f8aae777SJulian Elischer 	.disconnect =	ngt_disconnect,
1374cf49a43SJulian Elischer };
1384cf49a43SJulian Elischer NETGRAPH_INIT(tty, &typestruct);
1394cf49a43SJulian Elischer 
140d1426d7fSGleb Smirnoff #define	NGTLOCK(sc)	IF_LOCK(&sc->outq)
141d1426d7fSGleb Smirnoff #define	NGTUNLOCK(sc)	IF_UNLOCK(&sc->outq)
142d1426d7fSGleb Smirnoff 
1434cf49a43SJulian Elischer /******************************************************************
1444cf49a43SJulian Elischer 		    NETGRAPH NODE METHODS
1454cf49a43SJulian Elischer ******************************************************************/
1464cf49a43SJulian Elischer 
1474cf49a43SJulian Elischer /*
1484cf49a43SJulian Elischer  * Initialize a new node of this type.
1494cf49a43SJulian Elischer  *
1504cf49a43SJulian Elischer  * We only allow nodes to be created as a result of setting
1514cf49a43SJulian Elischer  * the line discipline on a tty, so always return an error if not.
1524cf49a43SJulian Elischer  */
1534cf49a43SJulian Elischer static int
154069154d5SJulian Elischer ngt_constructor(node_p node)
1554cf49a43SJulian Elischer {
1569087c349SAndrew Thompson 	sc_p sc;
1579087c349SAndrew Thompson 
1589087c349SAndrew Thompson 	/* Allocate private structure */
159674d86bfSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
1609087c349SAndrew Thompson 
1619087c349SAndrew Thompson 	NG_NODE_SET_PRIVATE(node, sc);
1629087c349SAndrew Thompson 	sc->node = node;
1639087c349SAndrew Thompson 
1649087c349SAndrew Thompson 	mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
165e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&sc->outq, ifqmaxlen);
1669087c349SAndrew Thompson 
1679087c349SAndrew Thompson 	return (0);
1684cf49a43SJulian Elischer }
1694cf49a43SJulian Elischer 
1704cf49a43SJulian Elischer /*
1714cf49a43SJulian Elischer  * Add a new hook. There can only be one.
1724cf49a43SJulian Elischer  */
1734cf49a43SJulian Elischer static int
1744cf49a43SJulian Elischer ngt_newhook(node_p node, hook_p hook, const char *name)
1754cf49a43SJulian Elischer {
17630400f03SJulian Elischer 	const sc_p sc = NG_NODE_PRIVATE(node);
1774cf49a43SJulian Elischer 
1784cf49a43SJulian Elischer 	if (strcmp(name, NG_TTY_HOOK))
1794cf49a43SJulian Elischer 		return (EINVAL);
180d1426d7fSGleb Smirnoff 
1814cf49a43SJulian Elischer 	if (sc->hook)
182d1426d7fSGleb Smirnoff 		return (EISCONN);
183d1426d7fSGleb Smirnoff 
184d1426d7fSGleb Smirnoff 	NGTLOCK(sc);
1854cf49a43SJulian Elischer 	sc->hook = hook;
186d1426d7fSGleb Smirnoff 	NGTUNLOCK(sc);
187d1426d7fSGleb Smirnoff 
188d1426d7fSGleb Smirnoff 	return (0);
1894cf49a43SJulian Elischer }
1904cf49a43SJulian Elischer 
1914cf49a43SJulian Elischer /*
192d1426d7fSGleb Smirnoff  * Set the hook into queueing mode (for outgoing packets),
193*053359b7SPedro F. Giffuni  * so that we wont deliver mbuf through the whole graph holding
194d1426d7fSGleb Smirnoff  * tty locks.
195859a4d16SJulian Elischer  */
196859a4d16SJulian Elischer static int
197859a4d16SJulian Elischer ngt_connect(hook_p hook)
198859a4d16SJulian Elischer {
199d1426d7fSGleb Smirnoff 	NG_HOOK_FORCE_QUEUE(hook);
200859a4d16SJulian Elischer 	return (0);
201859a4d16SJulian Elischer }
202859a4d16SJulian Elischer 
203859a4d16SJulian Elischer /*
2044cf49a43SJulian Elischer  * Disconnect the hook
2054cf49a43SJulian Elischer  */
2064cf49a43SJulian Elischer static int
2074cf49a43SJulian Elischer ngt_disconnect(hook_p hook)
2084cf49a43SJulian Elischer {
20930400f03SJulian Elischer 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
2104cf49a43SJulian Elischer 
2114cf49a43SJulian Elischer 	if (hook != sc->hook)
212d28843a4SRui Paulo 		panic("%s", __func__);
213d1426d7fSGleb Smirnoff 
214d1426d7fSGleb Smirnoff 	NGTLOCK(sc);
2154cf49a43SJulian Elischer 	sc->hook = NULL;
216d1426d7fSGleb Smirnoff 	NGTUNLOCK(sc);
217d1426d7fSGleb Smirnoff 
2184cf49a43SJulian Elischer 	return (0);
2194cf49a43SJulian Elischer }
2204cf49a43SJulian Elischer 
2214cf49a43SJulian Elischer /*
2224cf49a43SJulian Elischer  * Remove this node. The does the netgraph portion of the shutdown.
2234cf49a43SJulian Elischer  */
2244cf49a43SJulian Elischer static int
2254cf49a43SJulian Elischer ngt_shutdown(node_p node)
2264cf49a43SJulian Elischer {
22730400f03SJulian Elischer 	const sc_p sc = NG_NODE_PRIVATE(node);
2289087c349SAndrew Thompson 	struct tty *tp;
2294cf49a43SJulian Elischer 
2309087c349SAndrew Thompson 	tp = sc->tp;
2319087c349SAndrew Thompson 	if (tp != NULL) {
2329087c349SAndrew Thompson 		tty_lock(tp);
2339087c349SAndrew Thompson 		ttyhook_unregister(tp);
234d1426d7fSGleb Smirnoff 	}
235d1426d7fSGleb Smirnoff 	/* Free resources */
2369087c349SAndrew Thompson 	IF_DRAIN(&sc->outq);
237d1426d7fSGleb Smirnoff 	mtx_destroy(&(sc)->outq.ifq_mtx);
238d1426d7fSGleb Smirnoff 	NG_NODE_UNREF(sc->node);
2391ede983cSDag-Erling Smørgrav 	free(sc, M_NETGRAPH);
240d1426d7fSGleb Smirnoff 
2414cf49a43SJulian Elischer 	return (0);
2424cf49a43SJulian Elischer }
2434cf49a43SJulian Elischer 
2444cf49a43SJulian Elischer /*
2454cf49a43SJulian Elischer  * Receive control message
2464cf49a43SJulian Elischer  */
2474cf49a43SJulian Elischer static int
248069154d5SJulian Elischer ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
2494cf49a43SJulian Elischer {
25045056382SAlexander Motin 	struct proc *p;
25130400f03SJulian Elischer 	const sc_p sc = NG_NODE_PRIVATE(node);
252d1426d7fSGleb Smirnoff 	struct ng_mesg *msg, *resp = NULL;
2534cf49a43SJulian Elischer 	int error = 0;
2544cf49a43SJulian Elischer 
255069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
2564cf49a43SJulian Elischer 	switch (msg->header.typecookie) {
2574cf49a43SJulian Elischer 	case NGM_TTY_COOKIE:
2584cf49a43SJulian Elischer 		switch (msg->header.cmd) {
2599087c349SAndrew Thompson 		case NGM_TTY_SET_TTY:
2609087c349SAndrew Thompson 			if (sc->tp != NULL)
2619087c349SAndrew Thompson 				return (EBUSY);
26245056382SAlexander Motin 
26345056382SAlexander Motin 			p = pfind(((int *)msg->data)[0]);
264a9385ad1SAlexander Motin 			if (p == NULL || (p->p_flag & P_WEXIT))
26545056382SAlexander Motin 				return (ESRCH);
266a9385ad1SAlexander Motin 			_PHOLD(p);
26745056382SAlexander Motin 			PROC_UNLOCK(p);
268a9385ad1SAlexander Motin 			error = ttyhook_register(&sc->tp, p, ((int *)msg->data)[1],
269a9385ad1SAlexander Motin 			    &ngt_hook, sc);
270a9385ad1SAlexander Motin 			PRELE(p);
2719087c349SAndrew Thompson 			if (error != 0)
2729087c349SAndrew Thompson 				return (error);
2739087c349SAndrew Thompson 			break;
2744cf49a43SJulian Elischer 		case NGM_TTY_SET_HOTCHAR:
2754cf49a43SJulian Elischer 		    {
2764cf49a43SJulian Elischer 			int     hotchar;
2774cf49a43SJulian Elischer 
2784cf49a43SJulian Elischer 			if (msg->header.arglen != sizeof(int))
2794cf49a43SJulian Elischer 				ERROUT(EINVAL);
2804cf49a43SJulian Elischer 			hotchar = *((int *) msg->data);
2814cf49a43SJulian Elischer 			if (hotchar != (u_char) hotchar && hotchar != -1)
2824cf49a43SJulian Elischer 				ERROUT(EINVAL);
2834cf49a43SJulian Elischer 			sc->hotchar = hotchar;	/* race condition is OK */
2844cf49a43SJulian Elischer 			break;
2854cf49a43SJulian Elischer 		    }
2864cf49a43SJulian Elischer 		case NGM_TTY_GET_HOTCHAR:
2874cf49a43SJulian Elischer 			NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
2884cf49a43SJulian Elischer 			if (!resp)
2894cf49a43SJulian Elischer 				ERROUT(ENOMEM);
2904cf49a43SJulian Elischer 			/* Race condition here is OK */
2914cf49a43SJulian Elischer 			*((int *) resp->data) = sc->hotchar;
2924cf49a43SJulian Elischer 			break;
2934cf49a43SJulian Elischer 		default:
2944cf49a43SJulian Elischer 			ERROUT(EINVAL);
2954cf49a43SJulian Elischer 		}
2964cf49a43SJulian Elischer 		break;
2974cf49a43SJulian Elischer 	default:
2984cf49a43SJulian Elischer 		ERROUT(EINVAL);
2994cf49a43SJulian Elischer 	}
3004cf49a43SJulian Elischer done:
301069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
302069154d5SJulian Elischer 	NG_FREE_MSG(msg);
3034cf49a43SJulian Elischer 	return (error);
3044cf49a43SJulian Elischer }
3054cf49a43SJulian Elischer 
3064cf49a43SJulian Elischer /*
3079087c349SAndrew Thompson  * Receive incoming data from netgraph system. Put it on our
3089087c349SAndrew Thompson  * output queue and start output if necessary.
3094cf49a43SJulian Elischer  */
3104cf49a43SJulian Elischer static int
3119087c349SAndrew Thompson ngt_rcvdata(hook_p hook, item_p item)
3124cf49a43SJulian Elischer {
3139087c349SAndrew Thompson 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
3149087c349SAndrew Thompson 	struct tty *tp = sc->tp;
3159087c349SAndrew Thompson 	struct mbuf *m;
3169087c349SAndrew Thompson 
3179087c349SAndrew Thompson 	if (hook != sc->hook)
318d28843a4SRui Paulo 		panic("%s", __func__);
3199087c349SAndrew Thompson 
3209087c349SAndrew Thompson 	NGI_GET_M(item, m);
3219087c349SAndrew Thompson 	NG_FREE_ITEM(item);
3229087c349SAndrew Thompson 
3239087c349SAndrew Thompson 	if (tp == NULL) {
3249087c349SAndrew Thompson 		NG_FREE_M(m);
3259087c349SAndrew Thompson 		return (ENXIO);
3269087c349SAndrew Thompson 	}
3279087c349SAndrew Thompson 
3289087c349SAndrew Thompson 	IF_LOCK(&sc->outq);
3299087c349SAndrew Thompson 	if (_IF_QFULL(&sc->outq)) {
3309087c349SAndrew Thompson 		IF_UNLOCK(&sc->outq);
3319087c349SAndrew Thompson 		NG_FREE_M(m);
3329087c349SAndrew Thompson 		return (ENOBUFS);
3339087c349SAndrew Thompson 	}
3349087c349SAndrew Thompson 
3359087c349SAndrew Thompson 	_IF_ENQUEUE(&sc->outq, m);
3369087c349SAndrew Thompson 	sc->outqlen += m->m_pkthdr.len;
3379087c349SAndrew Thompson 	IF_UNLOCK(&sc->outq);
3389087c349SAndrew Thompson 
3399087c349SAndrew Thompson 	/* notify the TTY that data is ready */
3409087c349SAndrew Thompson 	tty_lock(tp);
3419087c349SAndrew Thompson 	if (!tty_gone(tp))
3429087c349SAndrew Thompson 		ttydevsw_outwakeup(tp);
3439087c349SAndrew Thompson 	tty_unlock(tp);
3449087c349SAndrew Thompson 
3459087c349SAndrew Thompson 	return (0);
3469087c349SAndrew Thompson }
3479087c349SAndrew Thompson 
3489087c349SAndrew Thompson static size_t
3499087c349SAndrew Thompson ngt_getc_inject(struct tty *tp, void *buf, size_t len)
3509087c349SAndrew Thompson {
3519087c349SAndrew Thompson 	sc_p sc = ttyhook_softc(tp);
3529087c349SAndrew Thompson 	size_t total = 0;
3539087c349SAndrew Thompson 	int length;
3549087c349SAndrew Thompson 
3559087c349SAndrew Thompson 	while (len) {
3569087c349SAndrew Thompson 		struct mbuf *m;
3579087c349SAndrew Thompson 
3589087c349SAndrew Thompson 		/* Remove first mbuf from queue */
3599087c349SAndrew Thompson 		IF_DEQUEUE(&sc->outq, m);
3609087c349SAndrew Thompson 		if (m == NULL)
3619087c349SAndrew Thompson 			break;
3629087c349SAndrew Thompson 
3639087c349SAndrew Thompson 		/* Send as much of it as possible */
3649087c349SAndrew Thompson 		while (m != NULL) {
3659087c349SAndrew Thompson 			length = min(m->m_len, len);
3669087c349SAndrew Thompson 			memcpy((char *)buf + total, mtod(m, char *), length);
3679087c349SAndrew Thompson 
3689087c349SAndrew Thompson 			m->m_data += length;
3699087c349SAndrew Thompson 			m->m_len -= length;
3709087c349SAndrew Thompson 			total += length;
3719087c349SAndrew Thompson 			len -= length;
3729087c349SAndrew Thompson 
3739087c349SAndrew Thompson 			if (m->m_len > 0)
3749087c349SAndrew Thompson 				break;	/* device can't take any more */
3759087c349SAndrew Thompson 			m = m_free(m);
3769087c349SAndrew Thompson 		}
3779087c349SAndrew Thompson 
3789087c349SAndrew Thompson 		/* Put remainder of mbuf chain (if any) back on queue */
3799087c349SAndrew Thompson 		if (m != NULL) {
3809087c349SAndrew Thompson 			IF_PREPEND(&sc->outq, m);
3819087c349SAndrew Thompson 			break;
3829087c349SAndrew Thompson 		}
3839087c349SAndrew Thompson 	}
3849087c349SAndrew Thompson 	IF_LOCK(&sc->outq);
3859087c349SAndrew Thompson 	sc->outqlen -= total;
3869087c349SAndrew Thompson 	IF_UNLOCK(&sc->outq);
3879087c349SAndrew Thompson 	MPASS(sc->outqlen >= 0);
3889087c349SAndrew Thompson 
3899087c349SAndrew Thompson 	return (total);
3909087c349SAndrew Thompson }
3919087c349SAndrew Thompson 
3929087c349SAndrew Thompson static size_t
3939087c349SAndrew Thompson ngt_getc_poll(struct tty *tp)
3949087c349SAndrew Thompson {
3959087c349SAndrew Thompson 	sc_p sc = ttyhook_softc(tp);
3969087c349SAndrew Thompson 
3979087c349SAndrew Thompson 	return (sc->outqlen);
3989087c349SAndrew Thompson }
3999087c349SAndrew Thompson 
4009087c349SAndrew Thompson /*
4019087c349SAndrew Thompson  * Optimised TTY input.
4029087c349SAndrew Thompson  *
4039087c349SAndrew Thompson  * We get a buffer pointer to hopefully a complete data frame. Do not check for
4049087c349SAndrew Thompson  * the hotchar, just pass it on.
4059087c349SAndrew Thompson  */
4069087c349SAndrew Thompson static size_t
4079087c349SAndrew Thompson ngt_rint_bypass(struct tty *tp, const void *buf, size_t len)
4089087c349SAndrew Thompson {
4099087c349SAndrew Thompson 	sc_p sc = ttyhook_softc(tp);
4109087c349SAndrew Thompson 	node_p node = sc->node;
4119087c349SAndrew Thompson 	struct mbuf *m, *mb;
4129087c349SAndrew Thompson 	size_t total = 0;
4139087c349SAndrew Thompson 	int error = 0, length;
4149087c349SAndrew Thompson 
4159087c349SAndrew Thompson 	tty_lock_assert(tp, MA_OWNED);
4169087c349SAndrew Thompson 
4179087c349SAndrew Thompson 	if (sc->hook == NULL)
4189087c349SAndrew Thompson 		return (0);
4199087c349SAndrew Thompson 
420eb1b1807SGleb Smirnoff 	m = m_getm2(NULL, len, M_NOWAIT, MT_DATA, M_PKTHDR);
4219087c349SAndrew Thompson 	if (m == NULL) {
4229087c349SAndrew Thompson 		if (sc->flags & FLG_DEBUG)
4239087c349SAndrew Thompson 			log(LOG_ERR,
4249087c349SAndrew Thompson 			    "%s: can't get mbuf\n", NG_NODE_NAME(node));
4259087c349SAndrew Thompson 		return (0);
4269087c349SAndrew Thompson 	}
4279087c349SAndrew Thompson 	m->m_pkthdr.rcvif = NULL;
4289087c349SAndrew Thompson 
4299087c349SAndrew Thompson 	for (mb = m; mb != NULL; mb = mb->m_next) {
4309087c349SAndrew Thompson 		length = min(M_TRAILINGSPACE(mb), len - total);
4319087c349SAndrew Thompson 
4329087c349SAndrew Thompson 		memcpy(mtod(m, char *), (const char *)buf + total, length);
4339087c349SAndrew Thompson 		mb->m_len = length;
4349087c349SAndrew Thompson 		total += length;
4359087c349SAndrew Thompson 		m->m_pkthdr.len += length;
4369087c349SAndrew Thompson 	}
4379087c349SAndrew Thompson 	if (sc->m != NULL) {
4389087c349SAndrew Thompson 		/*
4399087c349SAndrew Thompson 		 * Odd, we have changed from non-bypass to bypass. It is
4409087c349SAndrew Thompson 		 * unlikely but not impossible, flush the data first.
4419087c349SAndrew Thompson 		 */
4429087c349SAndrew Thompson 		sc->m->m_data = sc->m->m_pktdat;
4439087c349SAndrew Thompson 		NG_SEND_DATA_ONLY(error, sc->hook, sc->m);
4449087c349SAndrew Thompson 		sc->m = NULL;
4459087c349SAndrew Thompson 	}
4469087c349SAndrew Thompson 	NG_SEND_DATA_ONLY(error, sc->hook, m);
4479087c349SAndrew Thompson 
4489087c349SAndrew Thompson 	return (total);
4499087c349SAndrew Thompson }
4509087c349SAndrew Thompson 
4519087c349SAndrew Thompson /*
4529087c349SAndrew Thompson  * Receive data coming from the device one char at a time, when it is not in
4539087c349SAndrew Thompson  * bypass mode.
4549087c349SAndrew Thompson  */
4559087c349SAndrew Thompson static int
4569087c349SAndrew Thompson ngt_rint(struct tty *tp, char c, int flags)
4579087c349SAndrew Thompson {
4589087c349SAndrew Thompson 	sc_p sc = ttyhook_softc(tp);
4599087c349SAndrew Thompson 	node_p node = sc->node;
4609087c349SAndrew Thompson 	struct mbuf *m;
461d1426d7fSGleb Smirnoff 	int error = 0;
4624cf49a43SJulian Elischer 
4639087c349SAndrew Thompson 	tty_lock_assert(tp, MA_OWNED);
4644cf49a43SJulian Elischer 
4659087c349SAndrew Thompson 	if (sc->hook == NULL)
4669087c349SAndrew Thompson 		return (0);
4679087c349SAndrew Thompson 
4689087c349SAndrew Thompson 	if (flags != 0) {
4699087c349SAndrew Thompson 		/* framing error or overrun on this char */
4709087c349SAndrew Thompson 		if (sc->flags & FLG_DEBUG)
4719087c349SAndrew Thompson 			log(LOG_DEBUG, "%s: line error %x\n",
4729087c349SAndrew Thompson 			    NG_NODE_NAME(node), flags);
4739087c349SAndrew Thompson 		return (0);
4744cf49a43SJulian Elischer 	}
4754cf49a43SJulian Elischer 
4769087c349SAndrew Thompson 	/* Get a new header mbuf if we need one */
4779087c349SAndrew Thompson 	if (!(m = sc->m)) {
478eb1b1807SGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_DATA);
4799087c349SAndrew Thompson 		if (!m) {
4809087c349SAndrew Thompson 			if (sc->flags & FLG_DEBUG)
4819087c349SAndrew Thompson 				log(LOG_ERR,
4829087c349SAndrew Thompson 				    "%s: can't get mbuf\n", NG_NODE_NAME(node));
4839087c349SAndrew Thompson 			return (ENOBUFS);
4844cf49a43SJulian Elischer 		}
4859087c349SAndrew Thompson 		m->m_len = m->m_pkthdr.len = 0;
4869087c349SAndrew Thompson 		m->m_pkthdr.rcvif = NULL;
4879087c349SAndrew Thompson 		sc->m = m;
4889087c349SAndrew Thompson 	}
4899087c349SAndrew Thompson 
4909087c349SAndrew Thompson 	/* Add char to mbuf */
4919087c349SAndrew Thompson 	*mtod(m, u_char *) = c;
4929087c349SAndrew Thompson 	m->m_data++;
4939087c349SAndrew Thompson 	m->m_len++;
4949087c349SAndrew Thompson 	m->m_pkthdr.len++;
4959087c349SAndrew Thompson 
4969087c349SAndrew Thompson 	/* Ship off mbuf if it's time */
4979087c349SAndrew Thompson 	if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
4989087c349SAndrew Thompson 		m->m_data = m->m_pktdat;
4999087c349SAndrew Thompson 		sc->m = NULL;
5009087c349SAndrew Thompson 		NG_SEND_DATA_ONLY(error, sc->hook, m);	/* Will queue */
5019087c349SAndrew Thompson 	}
5029087c349SAndrew Thompson 
5034cf49a43SJulian Elischer 	return (error);
5044cf49a43SJulian Elischer }
5059087c349SAndrew Thompson 
5069087c349SAndrew Thompson static size_t
5079087c349SAndrew Thompson ngt_rint_poll(struct tty *tp)
5089087c349SAndrew Thompson {
5099087c349SAndrew Thompson 	/* We can always accept input */
5109087c349SAndrew Thompson 	return (1);
5119087c349SAndrew Thompson }
5129087c349SAndrew Thompson 
513