ng_tty.c revision 129823
1158115Sume
2158115Sume/*
3158115Sume * ng_tty.c
4158115Sume *
5158115Sume * Copyright (c) 1996-1999 Whistle Communications, Inc.
6158115Sume * All rights reserved.
7158115Sume *
8158115Sume * Subject to the following obligations and disclaimer of warranty, use and
9158115Sume * redistribution of this software, in source or object code forms, with or
10158115Sume * without modifications are expressly permitted by Whistle Communications;
11158115Sume * provided, however, that:
12158115Sume * 1. Any and all reproductions of the source or object code must include the
13158115Sume *    copyright notice above and the following disclaimer of warranties; and
14158115Sume * 2. No rights are granted, in any manner or form, to use Whistle
15158115Sume *    Communications, Inc. trademarks, including the mark "WHISTLE
16158115Sume *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17158115Sume *    such appears in the above copyright notice or in the software.
18158115Sume *
19158115Sume * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20158115Sume * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21158115Sume * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22158115Sume * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23158115Sume * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24158115Sume * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25158115Sume * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26158115Sume * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27158115Sume * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28158115Sume * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29171795Sbushman * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30171795Sbushman * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31158115Sume * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32158115Sume * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33158115Sume * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34158115Sume * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35158115Sume * OF SUCH DAMAGE.
36158115Sume *
37158115Sume * Author: Archie Cobbs <archie@freebsd.org>
38158115Sume *
39158115Sume * $FreeBSD: head/sys/netgraph/ng_tty.c 129823 2004-05-29 00:51:19Z julian $
40158115Sume * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
41158115Sume */
42158115Sume
43158115Sume/*
44158115Sume * This file implements a terminal line discipline that is also a
45158115Sume * netgraph node. Installing this line discipline on a terminal device
46158115Sume * instantiates a new netgraph node of this type, which allows access
47158115Sume * to the device via the "hook" hook of the node.
48158115Sume *
49158115Sume * Once the line discipline is installed, you can find out the name
50158115Sume * of the corresponding netgraph node via a NGIOCGINFO ioctl().
51158115Sume *
52158115Sume * Incoming characters are delievered to the hook one at a time, each
53158115Sume * in its own mbuf. You may optionally define a ``hotchar,'' which causes
54158115Sume * incoming characters to be buffered up until either the hotchar is
55158115Sume * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
56158115Sume * are immediately delivered.
57158115Sume *
58158115Sume * NOTE: This node operates at spltty().
59158115Sume */
60158115Sume
61158115Sume#include <sys/param.h>
62158115Sume#include <sys/systm.h>
63158115Sume#include <sys/kernel.h>
64158115Sume#include <sys/conf.h>
65158115Sume#include <sys/mbuf.h>
66158115Sume#include <sys/malloc.h>
67158115Sume#include <sys/fcntl.h>
68158115Sume#include <sys/tty.h>
69158115Sume#include <sys/ttycom.h>
70158115Sume#include <sys/syslog.h>
71158115Sume#include <sys/errno.h>
72194112Sdes#include <sys/ioccom.h>
73158115Sume
74158115Sume#include <netgraph/ng_message.h>
75158115Sume#include <netgraph/netgraph.h>
76158115Sume#include <netgraph/ng_tty.h>
77158115Sume
78158115Sume/* Misc defs */
79158115Sume#define MAX_MBUFQ		3	/* Max number of queued mbufs */
80194112Sdes#define NGT_HIWATER		400	/* High water mark on output */
81158115Sume
82158115Sume/* Per-node private info */
83158115Sumestruct ngt_sc {
84158115Sume	struct	tty *tp;		/* Terminal device */
85158115Sume	node_p	node;			/* Netgraph node */
86194112Sdes	hook_p	hook;			/* Netgraph hook */
87194097Sdes	struct	mbuf *m;		/* Incoming data buffer */
88158115Sume	struct	mbuf *qhead, **qtail;	/* Queue of outgoing mbuf's */
89158115Sume	short	qlen;			/* Length of queue */
90158115Sume	short	hotchar;		/* Hotchar, or -1 if none */
91158115Sume	u_int	flags;			/* Flags */
92158115Sume	struct	callout_handle chand;	/* See man timeout(9) */
93158115Sume};
94158115Sumetypedef struct ngt_sc *sc_p;
95238094Sse
96158115Sume/* Flags */
97158115Sume#define FLG_TIMEOUT		0x0001	/* A timeout is pending */
98158115Sume#define FLG_DEBUG		0x0002
99158115Sume
100158115Sume/* Debugging */
101194112Sdes#ifdef INVARIANTS
102194097Sdes#define QUEUECHECK(sc)							\
103158115Sume    do {								\
104158115Sume      struct mbuf	**mp;						\
105158115Sume      int		k;						\
106158115Sume									\
107158115Sume      for (k = 0, mp = &sc->qhead;					\
108158115Sume	k <= MAX_MBUFQ && *mp;						\
109158115Sume	k++, mp = &(*mp)->m_nextpkt);					\
110158115Sume      if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail)	\
111194112Sdes	panic("%s: queue", __func__);					\
112158115Sume    } while (0)
113158115Sume#else
114158115Sume#define QUEUECHECK(sc)	do {} while (0)
115158115Sume#endif
116158115Sume
117158115Sume/* Line discipline methods */
118158115Sumestatic int	ngt_open(dev_t dev, struct tty *tp);
119158115Sumestatic int	ngt_close(struct tty *tp, int flag);
120238094Ssestatic int	ngt_read(struct tty *tp, struct uio *uio, int flag);
121158115Sumestatic int	ngt_write(struct tty *tp, struct uio *uio, int flag);
122158115Sumestatic int	ngt_tioctl(struct tty *tp,
123194112Sdes		    u_long cmd, caddr_t data, int flag, struct thread *);
124158115Sumestatic int	ngt_input(int c, struct tty *tp);
125158115Sumestatic int	ngt_start(struct tty *tp);
126158115Sume
127194112Sdes/* Netgraph methods */
128158115Sumestatic ng_constructor_t	ngt_constructor;
129158115Sumestatic ng_rcvmsg_t	ngt_rcvmsg;
130158115Sumestatic ng_shutdown_t	ngt_shutdown;
131158115Sumestatic ng_newhook_t	ngt_newhook;
132194112Sdesstatic ng_connect_t	ngt_connect;
133158115Sumestatic ng_rcvdata_t	ngt_rcvdata;
134158115Sumestatic ng_disconnect_t	ngt_disconnect;
135158115Sumestatic int	ngt_mod_event(module_t mod, int event, void *data);
136158115Sume
137158115Sume/* Other stuff */
138158115Sumestatic void	ngt_timeout(void *arg);
139158115Sume
140158115Sume#define ERROUT(x)		do { error = (x); goto done; } while (0)
141158115Sume
142158115Sume/* Line discipline descriptor */
143158115Sumestatic struct linesw ngt_disc = {
144158115Sume	ngt_open,
145158115Sume	ngt_close,
146158115Sume	ngt_read,
147158115Sume	ngt_write,
148158115Sume	ngt_tioctl,
149158115Sume	ngt_input,
150158115Sume	ngt_start,
151158115Sume	ttymodem,
152158115Sume	NG_TTY_DFL_HOTCHAR	/* XXX can't change this in serial driver */
153158115Sume};
154158115Sume
155158115Sume/* Netgraph node type descriptor */
156158115Sumestatic struct ng_type typestruct = {
157158115Sume	.version =	NG_ABI_VERSION,
158158115Sume	.name =		NG_TTY_NODE_TYPE,
159158115Sume	.mod_event =	ngt_mod_event,
160194112Sdes	.constructor =	ngt_constructor,
161158115Sume	.rcvmsg =	ngt_rcvmsg,
162158115Sume	.shutdown =	ngt_shutdown,
163158115Sume	.newhook =	ngt_newhook,
164158115Sume	.connect =	ngt_connect,
165158115Sume	.rcvdata =	ngt_rcvdata,
166158115Sume	.disconnect =	ngt_disconnect,
167158115Sume};
168158115SumeNETGRAPH_INIT(tty, &typestruct);
169158115Sume
170158115Sumestatic int ngt_unit;
171158115Sumestatic int ngt_nodeop_ok;	/* OK to create/remove node */
172158115Sumestatic int ngt_ldisc;
173194112Sdes
174158115Sume/******************************************************************
175158115Sume		    LINE DISCIPLINE METHODS
176158115Sume******************************************************************/
177158115Sume
178158115Sume/*
179158115Sume * Set our line discipline on the tty.
180194112Sdes * Called from device open routine or ttioctl() at >= splsofttty()
181158115Sume */
182158115Sumestatic int
183158115Sumengt_open(dev_t dev, struct tty *tp)
184158115Sume{
185158115Sume	struct thread *const td = curthread;	/* XXX */
186158115Sume	char name[sizeof(NG_TTY_NODE_TYPE) + 8];
187158115Sume	sc_p sc;
188158115Sume	int s, error;
189158115Sume
190158115Sume	/* Super-user only */
191158115Sume	if ((error = suser(td)))
192158115Sume		return (error);
193158115Sume	s = splnet();
194158115Sume	(void) spltty();	/* XXX is this necessary? */
195158115Sume
196158115Sume	/* Already installed? */
197158115Sume	if (tp->t_line == NETGRAPHDISC) {
198158115Sume		sc = (sc_p) tp->t_sc;
199158115Sume		if (sc != NULL && sc->tp == tp)
200158115Sume			goto done;
201158115Sume	}
202158115Sume
203158115Sume	/* Initialize private struct */
204158115Sume	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
205158115Sume	if (sc == NULL) {
206158115Sume		error = ENOMEM;
207158115Sume		goto done;
208158115Sume	}
209158115Sume	sc->tp = tp;
210158115Sume	sc->hotchar = NG_TTY_DFL_HOTCHAR;
211194112Sdes	sc->qtail = &sc->qhead;
212158115Sume	QUEUECHECK(sc);
213158115Sume	callout_handle_init(&sc->chand);
214158115Sume
215158115Sume	/* Setup netgraph node */
216158115Sume	ngt_nodeop_ok = 1;
217158115Sume	error = ng_make_node_common(&typestruct, &sc->node);
218158115Sume	ngt_nodeop_ok = 0;
219158115Sume	if (error) {
220158115Sume		FREE(sc, M_NETGRAPH);
221158115Sume		goto done;
222158115Sume	}
223158115Sume	snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
224158115Sume
225158115Sume	/* Assign node its name */
226158115Sume	if ((error = ng_name_node(sc->node, name))) {
227158115Sume		log(LOG_ERR, "%s: node name exists?\n", name);
228158115Sume		ngt_nodeop_ok = 1;
229158115Sume		NG_NODE_UNREF(sc->node);
230158115Sume		ngt_nodeop_ok = 0;
231158115Sume		goto done;
232158115Sume	}
233158115Sume
234158115Sume	/* Set back pointers */
235158115Sume	NG_NODE_SET_PRIVATE(sc->node, sc);
236194112Sdes	tp->t_sc = (caddr_t) sc;
237194112Sdes
238158115Sume	/*
239158115Sume	 * Pre-allocate cblocks to the an appropriate amount.
240194112Sdes	 * I'm not sure what is appropriate.
241194112Sdes	 */
242194112Sdes	ttyflush(tp, FREAD | FWRITE);
243158115Sume	clist_alloc_cblocks(&tp->t_canq, 0, 0);
244158115Sume	clist_alloc_cblocks(&tp->t_rawq, 0, 0);
245194112Sdes	clist_alloc_cblocks(&tp->t_outq,
246194112Sdes	    MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
247158115Sume
248158115Sumedone:
249194112Sdes	/* Done */
250194112Sdes	splx(s);
251194112Sdes	return (error);
252194112Sdes}
253158115Sume
254194112Sdes/*
255194112Sdes * Line specific close routine, called from device close routine
256194112Sdes * and from ttioctl at >= splsofttty(). This causes the node to
257158115Sume * be destroyed as well.
258158115Sume */
259194112Sdesstatic int
260194112Sdesngt_close(struct tty *tp, int flag)
261158115Sume{
262158115Sume	const sc_p sc = (sc_p) tp->t_sc;
263158115Sume	int s;
264
265	s = spltty();
266	ttyflush(tp, FREAD | FWRITE);
267	clist_free_cblocks(&tp->t_outq);
268	tp->t_line = 0;
269	if (sc != NULL) {
270		if (sc->flags & FLG_TIMEOUT) {
271			untimeout(ngt_timeout, sc, sc->chand);
272			sc->flags &= ~FLG_TIMEOUT;
273		}
274		ngt_nodeop_ok = 1;
275		ng_rmnode_self(sc->node);
276		ngt_nodeop_ok = 0;
277		tp->t_sc = NULL;
278	}
279	splx(s);
280	return (0);
281}
282
283/*
284 * Once the device has been turned into a node, we don't allow reading.
285 */
286static int
287ngt_read(struct tty *tp, struct uio *uio, int flag)
288{
289	return (EIO);
290}
291
292/*
293 * Once the device has been turned into a node, we don't allow writing.
294 */
295static int
296ngt_write(struct tty *tp, struct uio *uio, int flag)
297{
298	return (EIO);
299}
300
301/*
302 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
303 */
304static int
305ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
306{
307	const sc_p sc = (sc_p) tp->t_sc;
308	int s, error = 0;
309
310	s = spltty();
311	switch (cmd) {
312	case NGIOCGINFO:
313	    {
314		struct nodeinfo *const ni = (struct nodeinfo *) data;
315		const node_p node = sc->node;
316
317		bzero(ni, sizeof(*ni));
318		if (NG_NODE_HAS_NAME(node))
319			strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
320		strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
321		ni->id = (u_int32_t) ng_node2ID(node);
322		ni->hooks = NG_NODE_NUMHOOKS(node);
323		break;
324	    }
325	default:
326		ERROUT(ENOIOCTL);
327	}
328done:
329	splx(s);
330	return (error);
331}
332
333/*
334 * Receive data coming from the device. We get one character at
335 * a time, which is kindof silly.
336 * Only guaranteed to be at splsofttty() or spltty().
337 */
338static int
339ngt_input(int c, struct tty *tp)
340{
341	const sc_p sc = (sc_p) tp->t_sc;
342	const node_p node = sc->node;
343	struct mbuf *m;
344	int s, error = 0;
345
346	if (!sc || tp != sc->tp)
347		return (0);
348	s = spltty();
349	if (!sc->hook)
350		ERROUT(0);
351
352	/* Check for error conditions */
353	if ((tp->t_state & TS_CONNECTED) == 0) {
354		if (sc->flags & FLG_DEBUG)
355			log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
356		ERROUT(0);
357	}
358	if (c & TTY_ERRORMASK) {
359		/* framing error or overrun on this char */
360		if (sc->flags & FLG_DEBUG)
361			log(LOG_DEBUG, "%s: line error %x\n",
362			    NG_NODE_NAME(node), c & TTY_ERRORMASK);
363		ERROUT(0);
364	}
365	c &= TTY_CHARMASK;
366
367	/* Get a new header mbuf if we need one */
368	if (!(m = sc->m)) {
369		MGETHDR(m, M_DONTWAIT, MT_DATA);
370		if (!m) {
371			if (sc->flags & FLG_DEBUG)
372				log(LOG_ERR,
373				    "%s: can't get mbuf\n", NG_NODE_NAME(node));
374			ERROUT(ENOBUFS);
375		}
376		m->m_len = m->m_pkthdr.len = 0;
377		m->m_pkthdr.rcvif = NULL;
378		sc->m = m;
379	}
380
381	/* Add char to mbuf */
382	*mtod(m, u_char *) = c;
383	m->m_data++;
384	m->m_len++;
385	m->m_pkthdr.len++;
386
387	/* Ship off mbuf if it's time */
388	if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
389		m->m_data = m->m_pktdat;
390		NG_SEND_DATA_ONLY(error, sc->hook, m);
391		sc->m = NULL;
392	}
393done:
394	splx(s);
395	return (error);
396}
397
398/*
399 * This is called when the device driver is ready for more output.
400 * Called from tty system at splsofttty() or spltty().
401 * Also call from ngt_rcv_data() when a new mbuf is available for output.
402 */
403static int
404ngt_start(struct tty *tp)
405{
406	const sc_p sc = (sc_p) tp->t_sc;
407	int s;
408
409	s = spltty();
410	while (tp->t_outq.c_cc < NGT_HIWATER) {	/* XXX 2.2 specific ? */
411		struct mbuf *m = sc->qhead;
412
413		/* Remove first mbuf from queue */
414		if (!m)
415			break;
416		if ((sc->qhead = m->m_nextpkt) == NULL)
417			sc->qtail = &sc->qhead;
418		sc->qlen--;
419		QUEUECHECK(sc);
420
421		/* Send as much of it as possible */
422		while (m) {
423			int     sent;
424
425			sent = m->m_len
426			    - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
427			m->m_data += sent;
428			m->m_len -= sent;
429			if (m->m_len > 0)
430				break;	/* device can't take no more */
431			m = m_free(m);
432		}
433
434		/* Put remainder of mbuf chain (if any) back on queue */
435		if (m) {
436			m->m_nextpkt = sc->qhead;
437			sc->qhead = m;
438			if (sc->qtail == &sc->qhead)
439				sc->qtail = &m->m_nextpkt;
440			sc->qlen++;
441			QUEUECHECK(sc);
442			break;
443		}
444	}
445
446	/* Call output process whether or not there is any output. We are
447	 * being called in lieu of ttstart and must do what it would. */
448	if (tp->t_oproc != NULL)
449		(*tp->t_oproc) (tp);
450
451	/* This timeout is needed for operation on a pseudo-tty, because the
452	 * pty code doesn't call pppstart after it has drained the t_outq. */
453	if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
454		sc->chand = timeout(ngt_timeout, sc, 1);
455		sc->flags |= FLG_TIMEOUT;
456	}
457	splx(s);
458	return (0);
459}
460
461/*
462 * We still have data to output to the device, so try sending more.
463 */
464static void
465ngt_timeout(void *arg)
466{
467	const sc_p sc = (sc_p) arg;
468	int s;
469
470	s = spltty();
471	sc->flags &= ~FLG_TIMEOUT;
472	ngt_start(sc->tp);
473	splx(s);
474}
475
476/******************************************************************
477		    NETGRAPH NODE METHODS
478******************************************************************/
479
480/*
481 * Initialize a new node of this type.
482 *
483 * We only allow nodes to be created as a result of setting
484 * the line discipline on a tty, so always return an error if not.
485 */
486static int
487ngt_constructor(node_p node)
488{
489	return (EOPNOTSUPP);
490}
491
492/*
493 * Add a new hook. There can only be one.
494 */
495static int
496ngt_newhook(node_p node, hook_p hook, const char *name)
497{
498	const sc_p sc = NG_NODE_PRIVATE(node);
499	int s, error = 0;
500
501	if (strcmp(name, NG_TTY_HOOK))
502		return (EINVAL);
503	s = spltty();
504	if (sc->hook)
505		ERROUT(EISCONN);
506	sc->hook = hook;
507done:
508	splx(s);
509	return (error);
510}
511
512/*
513 * Set the hooks into queueing mode (for outgoing packets)
514 * Force single client at a time.
515 */
516static int
517ngt_connect(hook_p hook)
518{
519	/*NG_HOOK_FORCE_WRITER(hook);
520	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));*/
521	return (0);
522}
523
524/*
525 * Disconnect the hook
526 */
527static int
528ngt_disconnect(hook_p hook)
529{
530	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
531	int s;
532
533	s = spltty();
534	if (hook != sc->hook)
535		panic(__func__);
536	sc->hook = NULL;
537	m_freem(sc->m);
538	sc->m = NULL;
539	splx(s);
540	return (0);
541}
542
543/*
544 * Remove this node. The does the netgraph portion of the shutdown.
545 * This should only be called indirectly from ngt_close().
546 */
547static int
548ngt_shutdown(node_p node)
549{
550	const sc_p sc = NG_NODE_PRIVATE(node);
551
552	if (!ngt_nodeop_ok)
553		return (EOPNOTSUPP);
554	NG_NODE_SET_PRIVATE(node, NULL);
555	NG_NODE_UNREF(sc->node);
556	m_freem(sc->qhead);
557	m_freem(sc->m);
558	bzero(sc, sizeof(*sc));
559	FREE(sc, M_NETGRAPH);
560	return (0);
561}
562
563/*
564 * Receive incoming data from netgraph system. Put it on our
565 * output queue and start output if necessary.
566 */
567static int
568ngt_rcvdata(hook_p hook, item_p item)
569{
570	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
571	int s, error = 0;
572	struct mbuf *m;
573
574	if (hook != sc->hook)
575		panic(__func__);
576
577	NGI_GET_M(item, m);
578	NG_FREE_ITEM(item);
579	s = spltty();
580	if (sc->qlen >= MAX_MBUFQ)
581		ERROUT(ENOBUFS);
582	m->m_nextpkt = NULL;
583	*sc->qtail = m;
584	sc->qtail = &m->m_nextpkt;
585	sc->qlen++;
586	QUEUECHECK(sc);
587	m = NULL;
588	if (sc->qlen == 1)
589		ngt_start(sc->tp);
590done:
591	splx(s);
592	if (m)
593		m_freem(m);
594	return (error);
595}
596
597/*
598 * Receive control message
599 */
600static int
601ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
602{
603	const sc_p sc = NG_NODE_PRIVATE(node);
604	struct ng_mesg *resp = NULL;
605	int error = 0;
606	struct ng_mesg *msg;
607
608	NGI_GET_MSG(item, msg);
609	switch (msg->header.typecookie) {
610	case NGM_TTY_COOKIE:
611		switch (msg->header.cmd) {
612		case NGM_TTY_SET_HOTCHAR:
613		    {
614			int     hotchar;
615
616			if (msg->header.arglen != sizeof(int))
617				ERROUT(EINVAL);
618			hotchar = *((int *) msg->data);
619			if (hotchar != (u_char) hotchar && hotchar != -1)
620				ERROUT(EINVAL);
621			sc->hotchar = hotchar;	/* race condition is OK */
622			break;
623		    }
624		case NGM_TTY_GET_HOTCHAR:
625			NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
626			if (!resp)
627				ERROUT(ENOMEM);
628			/* Race condition here is OK */
629			*((int *) resp->data) = sc->hotchar;
630			break;
631		default:
632			ERROUT(EINVAL);
633		}
634		break;
635	default:
636		ERROUT(EINVAL);
637	}
638done:
639	NG_RESPOND_MSG(error, node, item, resp);
640	NG_FREE_MSG(msg);
641	return (error);
642}
643
644/******************************************************************
645		    	INITIALIZATION
646******************************************************************/
647
648/*
649 * Handle loading and unloading for this node type
650 */
651static int
652ngt_mod_event(module_t mod, int event, void *data)
653{
654	/* struct ng_type *const type = data;*/
655	int s, error = 0;
656
657	switch (event) {
658	case MOD_LOAD:
659
660		/* Register line discipline */
661		s = spltty();
662		if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
663			splx(s);
664			log(LOG_ERR, "%s: can't register line discipline",
665			    __func__);
666			return (EIO);
667		}
668		splx(s);
669		break;
670
671	case MOD_UNLOAD:
672
673		/* Unregister line discipline */
674		s = spltty();
675		ldisc_deregister(ngt_ldisc);
676		splx(s);
677		break;
678
679	default:
680		error = EOPNOTSUPP;
681		break;
682	}
683	return (error);
684}
685
686