netgraph7/ng_tty: Remove two unused #define's.
[dragonfly.git] / sys / netgraph7 / tty / ng_tty.c
CommitLineData
b06ebda0 1/*-
22ff886e
AH
2 * (MPSAFE)
3 *
4 * ng_tty.c
5 *
b06ebda0
MD
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
19 *
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Author: Archie Cobbs <archie@freebsd.org>
39 *
40 * $FreeBSD: src/sys/netgraph/ng_tty.c,v 1.37 2006/11/06 13:42:03 rwatson Exp $
41 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
42 */
43
44/*
45 * This file implements a terminal line discipline that is also a
46 * netgraph node. Installing this line discipline on a terminal device
47 * instantiates a new netgraph node of this type, which allows access
48 * to the device via the "hook" hook of the node.
49 *
50 * Once the line discipline is installed, you can find out the name
51 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
52 *
53 * Incoming characters are delievered to the hook one at a time, each
54 * in its own mbuf. You may optionally define a ``hotchar,'' which causes
55 * incoming characters to be buffered up until either the hotchar is
56 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
57 * are immediately delivered.
58 */
59
60#include <sys/param.h>
61#include <sys/systm.h>
62#include <sys/conf.h>
63#include <sys/errno.h>
64#include <sys/fcntl.h>
b06ebda0
MD
65#include <sys/kernel.h>
66#include <sys/malloc.h>
67#include <sys/mbuf.h>
68#include <sys/priv.h>
69#include <sys/socket.h>
70#include <sys/syslog.h>
71#include <sys/tty.h>
72#include <sys/ttycom.h>
73
74#include <net/if.h>
75#include <net/if_var.h>
76
e71e017b
NA
77#include <netgraph7/netgraph.h>
78#include <netgraph7/ng_message.h>
5a975a3d 79#include "ng_tty.h"
b06ebda0
MD
80
81/* Misc defs */
82#define MAX_MBUFQ 3 /* Max number of queued mbufs */
83#define NGT_HIWATER 400 /* High water mark on output */
84
85/* Per-node private info */
86struct ngt_sc {
87 struct tty *tp; /* Terminal device */
88 node_p node; /* Netgraph node */
89 hook_p hook; /* Netgraph hook */
90 struct ifqueue outq; /* Queue of outgoing data */
91 struct mbuf *m; /* Incoming data buffer */
92 short hotchar; /* Hotchar, or -1 if none */
93 u_int flags; /* Flags */
94 struct callout chand; /* See man timeout(9) */
95};
96typedef struct ngt_sc *sc_p;
97
98/* Flags */
99#define FLG_DEBUG 0x0002
100#define FLG_DIE 0x0004
101
102/* Line discipline methods */
103static int ngt_open(struct cdev *dev, struct tty *tp);
104static int ngt_close(struct tty *tp, int flag);
105static int ngt_read(struct tty *tp, struct uio *uio, int flag);
106static int ngt_write(struct tty *tp, struct uio *uio, int flag);
107static int ngt_tioctl(struct tty *tp,
e71e017b 108 u_long cmd, caddr_t data, int flag, struct ucred *cred);
b06ebda0
MD
109static int ngt_input(int c, struct tty *tp);
110static int ngt_start(struct tty *tp);
111
112/* Netgraph methods */
113static ng_constructor_t ngt_constructor;
114static ng_rcvmsg_t ngt_rcvmsg;
115static ng_shutdown_t ngt_shutdown;
116static ng_newhook_t ngt_newhook;
117static ng_connect_t ngt_connect;
118static ng_rcvdata_t ngt_rcvdata;
119static ng_disconnect_t ngt_disconnect;
120static int ngt_mod_event(module_t mod, int event, void *data);
121
122/* Other stuff */
123static void ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2);
124
125#define ERROUT(x) do { error = (x); goto done; } while (0)
126
127/* Line discipline descriptor */
128static struct linesw ngt_disc = {
129 .l_open = ngt_open,
130 .l_close = ngt_close,
131 .l_read = ngt_read,
132 .l_write = ngt_write,
133 .l_ioctl = ngt_tioctl,
134 .l_rint = ngt_input,
135 .l_start = ngt_start,
136 .l_modem = ttymodem,
137};
138
139/* Netgraph node type descriptor */
140static struct ng_type typestruct = {
141 .version = NG_ABI_VERSION,
142 .name = NG_TTY_NODE_TYPE,
143 .mod_event = ngt_mod_event,
144 .constructor = ngt_constructor,
145 .rcvmsg = ngt_rcvmsg,
146 .shutdown = ngt_shutdown,
147 .newhook = ngt_newhook,
148 .connect = ngt_connect,
149 .rcvdata = ngt_rcvdata,
150 .disconnect = ngt_disconnect,
151};
152NETGRAPH_INIT(tty, &typestruct);
153
154/*
155 * Locking:
156 *
157 * - node private data and tp->t_lsc is protected by mutex in struct
158 * ifqueue, locking is done using IF_XXX() macros.
159 * - in all tty methods we should acquire node ifqueue mutex, when accessing
160 * private data.
161 * - in _rcvdata() we should use locked versions of IF_{EN,DE}QUEUE() since
162 * we may have multiple _rcvdata() threads.
163 * - when calling any of tty methods from netgraph methods, we should
164 * acquire tty locking (now Giant).
165 *
166 * - ngt_unit is incremented atomically.
167 */
168
b06ebda0
MD
169static int ngt_unit;
170static int ngt_ldisc;
171
172/******************************************************************
173 LINE DISCIPLINE METHODS
174******************************************************************/
175
176/*
177 * Set our line discipline on the tty.
178 * Called from device open routine or ttioctl()
179 */
180static int
181ngt_open(struct cdev *dev, struct tty *tp)
182{
183 struct thread *const td = curthread; /* XXX */
184 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
185 sc_p sc;
186 int error;
187
188 /* Super-user only */
189 error = priv_check(td, PRIV_NETGRAPH_TTY);
190 if (error)
191 return (error);
192
193 /* Initialize private struct */
fc025606 194 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
b06ebda0 195
e71e017b 196 crit_enter();
22ff886e 197 lwkt_gettoken(&tty_token);
b06ebda0 198 sc->tp = tp;
e71e017b
NA
199 sc->hotchar = NG_TTY_DFL_HOTCHAR;
200 sc->outq.ifq_maxlen = MAX_MBUFQ;
b06ebda0
MD
201
202 /* Setup netgraph node */
203 error = ng_make_node_common(&typestruct, &sc->node);
204 if (error) {
fc025606 205 kfree(sc, M_NETGRAPH);
22ff886e 206 lwkt_reltoken(&tty_token);
e71e017b 207 crit_exit();
b06ebda0
MD
208 return (error);
209 }
210
211 atomic_add_int(&ngt_unit, 1);
212 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
213
214 /* Assign node its name */
215 if ((error = ng_name_node(sc->node, name))) {
216 sc->flags |= FLG_DIE;
b06ebda0
MD
217 NG_NODE_UNREF(sc->node);
218 log(LOG_ERR, "%s: node name exists?\n", name);
22ff886e 219 lwkt_reltoken(&tty_token);
e71e017b 220 crit_exit();
b06ebda0
MD
221 return (error);
222 }
223
224 /* Set back pointers */
225 NG_NODE_SET_PRIVATE(sc->node, sc);
e71e017b 226 tp->t_sc = sc;
b06ebda0 227
e71e017b 228 callout_init_mp(&sc->chand);
b06ebda0
MD
229
230 /*
231 * Pre-allocate cblocks to the an appropriate amount.
232 * I'm not sure what is appropriate.
233 */
234 ttyflush(tp, FREAD | FWRITE);
235 clist_alloc_cblocks(&tp->t_canq, 0, 0);
236 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
237 clist_alloc_cblocks(&tp->t_outq,
238 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
239
22ff886e 240 lwkt_reltoken(&tty_token);
e71e017b
NA
241 crit_exit();
242 return (error);
b06ebda0
MD
243}
244
245/*
246 * Line specific close routine, called from device close routine
247 * and from ttioctl. This causes the node to be destroyed as well.
248 */
249static int
250ngt_close(struct tty *tp, int flag)
251{
e71e017b 252 const sc_p sc = (sc_p) tp->t_sc;
b06ebda0 253
e71e017b 254 crit_enter();
22ff886e 255 lwkt_gettoken(&tty_token);
b06ebda0
MD
256 ttyflush(tp, FREAD | FWRITE);
257 clist_free_cblocks(&tp->t_outq);
258 if (sc != NULL) {
b06ebda0
MD
259 if (callout_pending(&sc->chand))
260 ng_uncallout(&sc->chand, sc->node);
e71e017b 261 tp->t_sc = NULL;
b06ebda0 262 sc->flags |= FLG_DIE;
b06ebda0
MD
263 ng_rmnode_self(sc->node);
264 }
22ff886e 265 lwkt_reltoken(&tty_token);
e71e017b 266 crit_exit();
b06ebda0
MD
267 return (0);
268}
269
270/*
271 * Once the device has been turned into a node, we don't allow reading.
272 */
273static int
274ngt_read(struct tty *tp, struct uio *uio, int flag)
275{
276 return (EIO);
277}
278
279/*
280 * Once the device has been turned into a node, we don't allow writing.
281 */
282static int
283ngt_write(struct tty *tp, struct uio *uio, int flag)
284{
285 return (EIO);
286}
287
288/*
289 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
290 */
291static int
e71e017b 292ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
b06ebda0 293{
e71e017b 294 const sc_p sc = (sc_p) tp->t_sc;
b06ebda0 295
e71e017b 296 if (sc == NULL) {
b06ebda0
MD
297 /* No node attached */
298 return (0);
e71e017b 299 }
b06ebda0 300
e71e017b 301 crit_enter();
22ff886e 302 lwkt_gettoken(&tty_token);
b06ebda0
MD
303 switch (cmd) {
304 case NGIOCGINFO:
305 {
306 struct nodeinfo *const ni = (struct nodeinfo *) data;
307 const node_p node = sc->node;
308
309 bzero(ni, sizeof(*ni));
b06ebda0
MD
310 if (NG_NODE_HAS_NAME(node))
311 strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
312 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
313 ni->id = (u_int32_t) ng_node2ID(node);
314 ni->hooks = NG_NODE_NUMHOOKS(node);
b06ebda0
MD
315 break;
316 }
317 default:
22ff886e 318 lwkt_reltoken(&tty_token);
e71e017b 319 crit_exit();
b06ebda0
MD
320 return (ENOIOCTL);
321 }
322
22ff886e 323 lwkt_reltoken(&tty_token);
e71e017b 324 crit_exit();
b06ebda0
MD
325 return (0);
326}
327
328/*
329 * Receive data coming from the device. We get one character at
330 * a time, which is kindof silly.
331 *
332 * Full locking of softc is not required, since we are the only
333 * user of sc->m.
334 */
335static int
336ngt_input(int c, struct tty *tp)
337{
338 sc_p sc;
339 node_p node;
340 struct mbuf *m;
341 int error = 0;
342
e71e017b 343 crit_enter();
22ff886e 344 lwkt_gettoken(&tty_token);
e71e017b 345 sc = (sc_p) tp->t_sc;
22ff886e 346 if (sc == NULL) {
b06ebda0 347 /* No node attached */
22ff886e 348 lwkt_reltoken(&tty_token);
e71e017b 349 crit_exit();
b06ebda0 350 return (0);
22ff886e 351 }
b06ebda0
MD
352
353 node = sc->node;
354
355 if (tp != sc->tp)
356 panic("ngt_input");
357
358 /* Check for error conditions */
359 if ((tp->t_state & TS_CONNECTED) == 0) {
360 if (sc->flags & FLG_DEBUG)
361 log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
22ff886e 362 lwkt_reltoken(&tty_token);
e71e017b 363 crit_exit();
b06ebda0
MD
364 return (0);
365 }
366 if (c & TTY_ERRORMASK) {
367 /* framing error or overrun on this char */
368 if (sc->flags & FLG_DEBUG)
369 log(LOG_DEBUG, "%s: line error %x\n",
370 NG_NODE_NAME(node), c & TTY_ERRORMASK);
22ff886e 371 lwkt_reltoken(&tty_token);
e71e017b 372 crit_exit();
b06ebda0
MD
373 return (0);
374 }
375 c &= TTY_CHARMASK;
376
377 /* Get a new header mbuf if we need one */
378 if (!(m = sc->m)) {
5a975a3d 379 MGETHDR(m, MB_DONTWAIT, MT_DATA);
b06ebda0
MD
380 if (!m) {
381 if (sc->flags & FLG_DEBUG)
382 log(LOG_ERR,
383 "%s: can't get mbuf\n", NG_NODE_NAME(node));
22ff886e 384 lwkt_reltoken(&tty_token);
e71e017b 385 crit_exit();
b06ebda0
MD
386 return (ENOBUFS);
387 }
388 m->m_len = m->m_pkthdr.len = 0;
389 m->m_pkthdr.rcvif = NULL;
390 sc->m = m;
391 }
392
393 /* Add char to mbuf */
394 *mtod(m, u_char *) = c;
395 m->m_data++;
396 m->m_len++;
397 m->m_pkthdr.len++;
398
399 /* Ship off mbuf if it's time */
400 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
401 m->m_data = m->m_pktdat;
402 sc->m = NULL;
403
404 /*
405 * We have built our mbuf without checking that we actually
406 * have a hook to send it. This was done to avoid
407 * acquiring mutex on each character. Check now.
408 *
409 */
410
b06ebda0 411 if (sc->hook == NULL) {
b06ebda0 412 m_freem(m);
22ff886e 413 lwkt_reltoken(&tty_token);
e71e017b 414 crit_exit();
b06ebda0
MD
415 return (0); /* XXX: original behavior */
416 }
417 NG_SEND_DATA_ONLY(error, sc->hook, m); /* Will queue */
b06ebda0
MD
418 }
419
22ff886e 420 lwkt_reltoken(&tty_token);
e71e017b 421 crit_exit();
b06ebda0
MD
422 return (error);
423}
424
425/*
426 * This is called when the device driver is ready for more output.
427 * Also called from ngt_rcv_data() when a new mbuf is available for output.
428 */
429static int
430ngt_start(struct tty *tp)
431{
e71e017b 432 const sc_p sc = (sc_p) tp->t_sc;
b06ebda0 433
e71e017b 434 crit_enter();
22ff886e 435 lwkt_gettoken(&tty_token);
b06ebda0
MD
436 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
437 struct mbuf *m;
438
439 /* Remove first mbuf from queue */
440 IF_DEQUEUE(&sc->outq, m);
441 if (m == NULL)
442 break;
443
444 /* Send as much of it as possible */
445 while (m != NULL) {
446 int sent;
447
448 sent = m->m_len
449 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
450 m->m_data += sent;
451 m->m_len -= sent;
452 if (m->m_len > 0)
453 break; /* device can't take no more */
454 m = m_free(m);
455 }
456
457 /* Put remainder of mbuf chain (if any) back on queue */
458 if (m != NULL) {
459 IF_PREPEND(&sc->outq, m);
460 break;
461 }
462 }
463
464 /* Call output process whether or not there is any output. We are
465 * being called in lieu of ttstart and must do what it would. */
e71e017b
NA
466 if (tp->t_oproc != NULL)
467 (*tp->t_oproc) (tp);
b06ebda0
MD
468
469 /* This timeout is needed for operation on a pseudo-tty, because the
470 * pty code doesn't call pppstart after it has drained the t_outq. */
471 /* XXX: outq not locked */
e71e017b 472 if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->chand))
b06ebda0
MD
473 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
474
22ff886e 475 lwkt_reltoken(&tty_token);
e71e017b 476 crit_exit();
b06ebda0
MD
477 return (0);
478}
479
480/*
481 * We still have data to output to the device, so try sending more.
482 */
483static void
484ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
485{
486 const sc_p sc = NG_NODE_PRIVATE(node);
487
e71e017b
NA
488 crit_enter();
489 lwkt_gettoken(&tty_token);
b06ebda0 490 ngt_start(sc->tp);
e71e017b
NA
491 lwkt_reltoken(&tty_token);
492 crit_exit();
b06ebda0
MD
493}
494
495/******************************************************************
496 NETGRAPH NODE METHODS
497******************************************************************/
498
499/*
500 * Initialize a new node of this type.
501 *
502 * We only allow nodes to be created as a result of setting
503 * the line discipline on a tty, so always return an error if not.
504 */
505static int
506ngt_constructor(node_p node)
507{
508 return (EOPNOTSUPP);
509}
510
511/*
512 * Add a new hook. There can only be one.
513 */
514static int
515ngt_newhook(node_p node, hook_p hook, const char *name)
516{
517 const sc_p sc = NG_NODE_PRIVATE(node);
518
519 if (strcmp(name, NG_TTY_HOOK))
520 return (EINVAL);
e71e017b
NA
521 crit_enter();
522 lwkt_gettoken(&tty_token);
523 if (sc->hook) {
524 lwkt_reltoken(&tty_token);
525 crit_exit();
b06ebda0 526 return (EISCONN);
e71e017b 527 }
b06ebda0 528 sc->hook = hook;
e71e017b
NA
529 lwkt_reltoken(&tty_token);
530 crit_exit();
b06ebda0
MD
531 return (0);
532}
533
534/*
535 * Set the hook into queueing mode (for outgoing packets),
536 * so that we wont deliver mbuf thru the whole graph holding
537 * tty locks.
538 */
539static int
540ngt_connect(hook_p hook)
541{
542 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
543 /*
544 * XXX: While ngt_start() is Giant-locked, queue incoming
545 * packets, too. Otherwise we acquire Giant holding some
546 * IP stack locks, e.g. divinp, and this makes WITNESS scream.
547 */
548 NG_HOOK_FORCE_QUEUE(hook);
549 return (0);
550}
551
552/*
553 * Disconnect the hook
554 */
555static int
556ngt_disconnect(hook_p hook)
557{
558 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
559
e71e017b
NA
560 crit_enter();
561 lwkt_gettoken(&tty_token);
b06ebda0
MD
562 if (hook != sc->hook)
563 panic(__func__);
b06ebda0 564 sc->hook = NULL;
e71e017b
NA
565 lwkt_reltoken(&tty_token);
566 crit_exit();
b06ebda0
MD
567 return (0);
568}
569
570/*
571 * Remove this node. The does the netgraph portion of the shutdown.
572 * This should only be called indirectly from ngt_close().
573 *
574 * tp->t_lsc is already NULL, so we should be protected from
575 * tty calls now.
576 */
577static int
578ngt_shutdown(node_p node)
579{
580 const sc_p sc = NG_NODE_PRIVATE(node);
581
e71e017b
NA
582 crit_enter();
583 lwkt_gettoken(&tty_token);
b06ebda0 584 if (!(sc->flags & FLG_DIE)) {
e71e017b
NA
585 lwkt_reltoken(&tty_token);
586 crit_exit();
b06ebda0
MD
587 return (EOPNOTSUPP);
588 }
b06ebda0
MD
589
590 /* Free resources */
e71e017b 591 IF_DRAIN(&sc->outq);
b06ebda0 592 m_freem(sc->m);
fc025606 593 kfree(sc, M_NETGRAPH);
e71e017b
NA
594 lwkt_reltoken(&tty_token);
595 crit_exit();
b06ebda0
MD
596 return (0);
597}
598
599/*
600 * Receive incoming data from netgraph system. Put it on our
601 * output queue and start output if necessary.
602 */
603static int
604ngt_rcvdata(hook_p hook, item_p item)
605{
606 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
607 struct mbuf *m;
608 int qlen;
609
610 if (hook != sc->hook)
611 panic(__func__);
612
613 NGI_GET_M(item, m);
614 NG_FREE_ITEM(item);
615
e71e017b
NA
616 crit_enter();
617 if (IF_QFULL(&sc->outq)) {
618 IF_DROP(&sc->outq);
619 crit_exit();
b06ebda0
MD
620 NG_FREE_M(m);
621 return (ENOBUFS);
622 }
623
e71e017b 624 IF_ENQUEUE(&sc->outq, m);
b06ebda0 625 qlen = sc->outq.ifq_len;
e71e017b 626 crit_exit();
b06ebda0
MD
627
628 /*
629 * If qlen > 1, then we should already have a scheduled callout.
630 */
631 if (qlen == 1) {
e71e017b 632 lwkt_gettoken(&tty_token);
b06ebda0 633 ngt_start(sc->tp);
e71e017b 634 lwkt_reltoken(&tty_token);
b06ebda0
MD
635 }
636
637 return (0);
638}
639
640/*
641 * Receive control message
642 */
643static int
644ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
645{
646 const sc_p sc = NG_NODE_PRIVATE(node);
647 struct ng_mesg *msg, *resp = NULL;
648 int error = 0;
649
650 NGI_GET_MSG(item, msg);
651 switch (msg->header.typecookie) {
652 case NGM_TTY_COOKIE:
653 switch (msg->header.cmd) {
654 case NGM_TTY_SET_HOTCHAR:
655 {
656 int hotchar;
657
658 if (msg->header.arglen != sizeof(int))
659 ERROUT(EINVAL);
660 hotchar = *((int *) msg->data);
661 if (hotchar != (u_char) hotchar && hotchar != -1)
662 ERROUT(EINVAL);
663 sc->hotchar = hotchar; /* race condition is OK */
664 break;
665 }
666 case NGM_TTY_GET_HOTCHAR:
5a975a3d 667 NG_MKRESPONSE(resp, msg, sizeof(int), M_WAITOK | M_NULLOK);
b06ebda0
MD
668 if (!resp)
669 ERROUT(ENOMEM);
670 /* Race condition here is OK */
671 *((int *) resp->data) = sc->hotchar;
672 break;
673 default:
674 ERROUT(EINVAL);
675 }
676 break;
677 default:
678 ERROUT(EINVAL);
679 }
680done:
681 NG_RESPOND_MSG(error, node, item, resp);
682 NG_FREE_MSG(msg);
683 return (error);
684}
685
686/******************************************************************
687 INITIALIZATION
688******************************************************************/
689
690/*
691 * Handle loading and unloading for this node type
692 */
693static int
694ngt_mod_event(module_t mod, int event, void *data)
695{
696 int error = 0;
697
e71e017b 698 lwkt_gettoken(&tty_token);
b06ebda0
MD
699 switch (event) {
700 case MOD_LOAD:
b06ebda0 701 /* Register line discipline */
e71e017b 702 crit_enter();
b06ebda0 703 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
e71e017b 704 crit_exit();
b06ebda0
MD
705 log(LOG_ERR, "%s: can't register line discipline",
706 __func__);
e71e017b 707 lwkt_reltoken(&tty_token);
b06ebda0
MD
708 return (EIO);
709 }
e71e017b 710 crit_exit();
b06ebda0
MD
711 break;
712
713 case MOD_UNLOAD:
714
715 /* Unregister line discipline */
e71e017b 716 crit_enter();
b06ebda0 717 ldisc_deregister(ngt_ldisc);
e71e017b 718 crit_exit();
b06ebda0
MD
719 break;
720
721 default:
722 error = EOPNOTSUPP;
723 break;
724 }
e71e017b 725 lwkt_reltoken(&tty_token);
b06ebda0
MD
726 return (error);
727}