netgraph7: Welcome ng_tty.
[dragonfly.git] / sys / netgraph7 / tty / ng_tty.c
1 /*-
2  * (MPSAFE)
3  *
4  * ng_tty.c
5  *
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>
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
77 #include <netgraph7/netgraph.h>
78 #include <netgraph7/ng_message.h>
79 #include "ng_tty.h"
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 */
86 struct 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 };
96 typedef struct ngt_sc *sc_p;
97
98 /* Flags */
99 #define FLG_DEBUG               0x0002
100 #define FLG_DIE                 0x0004
101
102 /* Line discipline methods */
103 static int      ngt_open(struct cdev *dev, struct tty *tp);
104 static int      ngt_close(struct tty *tp, int flag);
105 static int      ngt_read(struct tty *tp, struct uio *uio, int flag);
106 static int      ngt_write(struct tty *tp, struct uio *uio, int flag);
107 static int      ngt_tioctl(struct tty *tp,
108                     u_long cmd, caddr_t data, int flag, struct ucred *cred);
109 static int      ngt_input(int c, struct tty *tp);
110 static int      ngt_start(struct tty *tp);
111
112 /* Netgraph methods */
113 static ng_constructor_t ngt_constructor;
114 static ng_rcvmsg_t      ngt_rcvmsg;
115 static ng_shutdown_t    ngt_shutdown;
116 static ng_newhook_t     ngt_newhook;
117 static ng_connect_t     ngt_connect;
118 static ng_rcvdata_t     ngt_rcvdata;
119 static ng_disconnect_t  ngt_disconnect;
120 static int              ngt_mod_event(module_t mod, int event, void *data);
121
122 /* Other stuff */
123 static 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 */
128 static 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 */
140 static 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 };
152 NETGRAPH_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
169 #define NGTLOCK(sc)     IF_LOCK(&sc->outq)
170 #define NGTUNLOCK(sc)   IF_UNLOCK(&sc->outq)
171
172 static int ngt_unit;
173 static int ngt_ldisc;
174
175 /******************************************************************
176                     LINE DISCIPLINE METHODS
177 ******************************************************************/
178
179 /*
180  * Set our line discipline on the tty.
181  * Called from device open routine or ttioctl()
182  */
183 static int
184 ngt_open(struct cdev *dev, struct tty *tp)
185 {
186         struct thread *const td = curthread;    /* XXX */
187         char name[sizeof(NG_TTY_NODE_TYPE) + 8];
188         sc_p sc;
189         int error;
190
191         /* Super-user only */
192         error = priv_check(td, PRIV_NETGRAPH_TTY);
193         if (error)
194                 return (error);
195
196         /* Initialize private struct */
197         sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
198
199         crit_enter();
200         lwkt_gettoken(&tty_token);
201         sc->tp = tp;
202         sc->hotchar = NG_TTY_DFL_HOTCHAR;
203         sc->outq.ifq_maxlen = MAX_MBUFQ;
204
205         /* Setup netgraph node */
206         error = ng_make_node_common(&typestruct, &sc->node);
207         if (error) {
208                 kfree(sc, M_NETGRAPH);
209                 lwkt_reltoken(&tty_token);
210                 crit_exit();
211                 return (error);
212         }
213
214         atomic_add_int(&ngt_unit, 1);
215         snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
216
217         /* Assign node its name */
218         if ((error = ng_name_node(sc->node, name))) {
219                 sc->flags |= FLG_DIE;
220                 NG_NODE_UNREF(sc->node);
221                 log(LOG_ERR, "%s: node name exists?\n", name);
222                 lwkt_reltoken(&tty_token);
223                 crit_exit();
224                 return (error);
225         }
226
227         /* Set back pointers */
228         NG_NODE_SET_PRIVATE(sc->node, sc);
229         tp->t_sc = sc;
230
231         callout_init_mp(&sc->chand);
232
233         /*
234          * Pre-allocate cblocks to the an appropriate amount.
235          * I'm not sure what is appropriate.
236          */
237         ttyflush(tp, FREAD | FWRITE);
238         clist_alloc_cblocks(&tp->t_canq, 0, 0);
239         clist_alloc_cblocks(&tp->t_rawq, 0, 0);
240         clist_alloc_cblocks(&tp->t_outq,
241             MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
242
243         lwkt_reltoken(&tty_token);
244         crit_exit();
245         return (error);
246 }
247
248 /*
249  * Line specific close routine, called from device close routine
250  * and from ttioctl. This causes the node to be destroyed as well.
251  */
252 static int
253 ngt_close(struct tty *tp, int flag)
254 {
255         const sc_p sc = (sc_p) tp->t_sc;
256
257         crit_enter();
258         lwkt_gettoken(&tty_token);
259         ttyflush(tp, FREAD | FWRITE);
260         clist_free_cblocks(&tp->t_outq);
261         if (sc != NULL) {
262                 if (callout_pending(&sc->chand))
263                         ng_uncallout(&sc->chand, sc->node);
264                 tp->t_sc = NULL;
265                 sc->flags |= FLG_DIE;
266                 ng_rmnode_self(sc->node);
267         }
268         lwkt_reltoken(&tty_token);
269         crit_exit();
270         return (0);
271 }
272
273 /*
274  * Once the device has been turned into a node, we don't allow reading.
275  */
276 static int
277 ngt_read(struct tty *tp, struct uio *uio, int flag)
278 {
279         return (EIO);
280 }
281
282 /*
283  * Once the device has been turned into a node, we don't allow writing.
284  */
285 static int
286 ngt_write(struct tty *tp, struct uio *uio, int flag)
287 {
288         return (EIO);
289 }
290
291 /*
292  * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
293  */
294 static int
295 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
296 {
297         const sc_p sc = (sc_p) tp->t_sc;
298
299         if (sc == NULL) {
300                 /* No node attached */
301                 return (0);
302         }
303
304         crit_enter();
305         lwkt_gettoken(&tty_token);
306         switch (cmd) {
307         case NGIOCGINFO:
308             {
309                 struct nodeinfo *const ni = (struct nodeinfo *) data;
310                 const node_p node = sc->node;
311
312                 bzero(ni, sizeof(*ni));
313                 if (NG_NODE_HAS_NAME(node))
314                         strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
315                 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
316                 ni->id = (u_int32_t) ng_node2ID(node);
317                 ni->hooks = NG_NODE_NUMHOOKS(node);
318                 break;
319             }
320         default:
321                 lwkt_reltoken(&tty_token);
322                 crit_exit();
323                 return (ENOIOCTL);
324         }
325
326         lwkt_reltoken(&tty_token);
327         crit_exit();
328         return (0);
329 }
330
331 /*
332  * Receive data coming from the device. We get one character at
333  * a time, which is kindof silly.
334  *
335  * Full locking of softc is not required, since we are the only
336  * user of sc->m.
337  */
338 static int
339 ngt_input(int c, struct tty *tp)
340 {
341         sc_p sc;
342         node_p node;
343         struct mbuf *m;
344         int error = 0;
345
346         crit_enter();
347         lwkt_gettoken(&tty_token);
348         sc = (sc_p) tp->t_sc;
349         if (sc == NULL) {
350                 /* No node attached */
351                 lwkt_reltoken(&tty_token);
352                 crit_exit();
353                 return (0);
354         }
355
356         node = sc->node;
357
358         if (tp != sc->tp)
359                 panic("ngt_input");
360
361         /* Check for error conditions */
362         if ((tp->t_state & TS_CONNECTED) == 0) {
363                 if (sc->flags & FLG_DEBUG)
364                         log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
365                 lwkt_reltoken(&tty_token);
366                 crit_exit();
367                 return (0);
368         }
369         if (c & TTY_ERRORMASK) {
370                 /* framing error or overrun on this char */
371                 if (sc->flags & FLG_DEBUG)
372                         log(LOG_DEBUG, "%s: line error %x\n",
373                             NG_NODE_NAME(node), c & TTY_ERRORMASK);
374                 lwkt_reltoken(&tty_token);
375                 crit_exit();
376                 return (0);
377         }
378         c &= TTY_CHARMASK;
379
380         /* Get a new header mbuf if we need one */
381         if (!(m = sc->m)) {
382                 MGETHDR(m, MB_DONTWAIT, MT_DATA);
383                 if (!m) {
384                         if (sc->flags & FLG_DEBUG)
385                                 log(LOG_ERR,
386                                     "%s: can't get mbuf\n", NG_NODE_NAME(node));
387                         lwkt_reltoken(&tty_token);
388                         crit_exit();
389                         return (ENOBUFS);
390                 }
391                 m->m_len = m->m_pkthdr.len = 0;
392                 m->m_pkthdr.rcvif = NULL;
393                 sc->m = m;
394         }
395
396         /* Add char to mbuf */
397         *mtod(m, u_char *) = c;
398         m->m_data++;
399         m->m_len++;
400         m->m_pkthdr.len++;
401
402         /* Ship off mbuf if it's time */
403         if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
404                 m->m_data = m->m_pktdat;
405                 sc->m = NULL;
406
407                 /*
408                  * We have built our mbuf without checking that we actually
409                  * have a hook to send it. This was done to avoid
410                  * acquiring mutex on each character. Check now.
411                  *
412                  */
413
414                 if (sc->hook == NULL) {
415                         m_freem(m);
416                         lwkt_reltoken(&tty_token);
417                         crit_exit();
418                         return (0);             /* XXX: original behavior */
419                 }
420                 NG_SEND_DATA_ONLY(error, sc->hook, m);  /* Will queue */
421         }
422
423         lwkt_reltoken(&tty_token);
424         crit_exit();
425         return (error);
426 }
427
428 /*
429  * This is called when the device driver is ready for more output.
430  * Also called from ngt_rcv_data() when a new mbuf is available for output.
431  */
432 static int
433 ngt_start(struct tty *tp)
434 {
435         const sc_p sc = (sc_p) tp->t_sc;
436
437         crit_enter();
438         lwkt_gettoken(&tty_token);
439         while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
440                 struct mbuf *m;
441
442                 /* Remove first mbuf from queue */
443                 IF_DEQUEUE(&sc->outq, m);
444                 if (m == NULL)
445                         break;
446
447                 /* Send as much of it as possible */
448                 while (m != NULL) {
449                         int     sent;
450
451                         sent = m->m_len
452                             - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
453                         m->m_data += sent;
454                         m->m_len -= sent;
455                         if (m->m_len > 0)
456                                 break;  /* device can't take no more */
457                         m = m_free(m);
458                 }
459
460                 /* Put remainder of mbuf chain (if any) back on queue */
461                 if (m != NULL) {
462                         IF_PREPEND(&sc->outq, m);
463                         break;
464                 }
465         }
466
467         /* Call output process whether or not there is any output. We are
468          * being called in lieu of ttstart and must do what it would. */
469         if (tp->t_oproc != NULL)
470                 (*tp->t_oproc) (tp);
471
472         /* This timeout is needed for operation on a pseudo-tty, because the
473          * pty code doesn't call pppstart after it has drained the t_outq. */
474         /* XXX: outq not locked */
475         if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->chand))
476                 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
477
478         lwkt_reltoken(&tty_token);
479         crit_exit();
480         return (0);
481 }
482
483 /*
484  * We still have data to output to the device, so try sending more.
485  */
486 static void
487 ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
488 {
489         const sc_p sc = NG_NODE_PRIVATE(node);
490
491         crit_enter();
492         lwkt_gettoken(&tty_token);
493         ngt_start(sc->tp);
494         lwkt_reltoken(&tty_token);
495         crit_exit();
496 }
497
498 /******************************************************************
499                     NETGRAPH NODE METHODS
500 ******************************************************************/
501
502 /*
503  * Initialize a new node of this type.
504  *
505  * We only allow nodes to be created as a result of setting
506  * the line discipline on a tty, so always return an error if not.
507  */
508 static int
509 ngt_constructor(node_p node)
510 {
511         return (EOPNOTSUPP);
512 }
513
514 /*
515  * Add a new hook. There can only be one.
516  */
517 static int
518 ngt_newhook(node_p node, hook_p hook, const char *name)
519 {
520         const sc_p sc = NG_NODE_PRIVATE(node);
521
522         if (strcmp(name, NG_TTY_HOOK))
523                 return (EINVAL);
524         crit_enter();
525         lwkt_gettoken(&tty_token);
526         if (sc->hook) {
527                 lwkt_reltoken(&tty_token);
528                 crit_exit();
529                 return (EISCONN);
530         }
531         sc->hook = hook;
532         lwkt_reltoken(&tty_token);
533         crit_exit();
534         return (0);
535 }
536
537 /*
538  * Set the hook into queueing mode (for outgoing packets),
539  * so that we wont deliver mbuf thru the whole graph holding
540  * tty locks.
541  */
542 static int
543 ngt_connect(hook_p hook)
544 {
545         NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
546         /*
547          * XXX: While ngt_start() is Giant-locked, queue incoming
548          * packets, too. Otherwise we acquire Giant holding some
549          * IP stack locks, e.g. divinp, and this makes WITNESS scream.
550          */
551         NG_HOOK_FORCE_QUEUE(hook);
552         return (0);
553 }
554
555 /*
556  * Disconnect the hook
557  */
558 static int
559 ngt_disconnect(hook_p hook)
560 {
561         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
562
563         crit_enter();
564         lwkt_gettoken(&tty_token);
565         if (hook != sc->hook)
566                 panic(__func__);
567         sc->hook = NULL;
568         lwkt_reltoken(&tty_token);
569         crit_exit();
570         return (0);
571 }
572
573 /*
574  * Remove this node. The does the netgraph portion of the shutdown.
575  * This should only be called indirectly from ngt_close().
576  *
577  * tp->t_lsc is already NULL, so we should be protected from
578  * tty calls now.
579  */
580 static int
581 ngt_shutdown(node_p node)
582 {
583         const sc_p sc = NG_NODE_PRIVATE(node);
584
585         crit_enter();
586         lwkt_gettoken(&tty_token);
587         if (!(sc->flags & FLG_DIE)) {
588                 lwkt_reltoken(&tty_token);
589                 crit_exit();
590                 return (EOPNOTSUPP);
591         }
592
593         /* Free resources */
594         IF_DRAIN(&sc->outq);
595         m_freem(sc->m);
596         kfree(sc, M_NETGRAPH);
597         lwkt_reltoken(&tty_token);
598         crit_exit();
599         return (0);
600 }
601
602 /*
603  * Receive incoming data from netgraph system. Put it on our
604  * output queue and start output if necessary.
605  */
606 static int
607 ngt_rcvdata(hook_p hook, item_p item)
608 {
609         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
610         struct mbuf *m;
611         int qlen;
612
613         if (hook != sc->hook)
614                 panic(__func__);
615
616         NGI_GET_M(item, m);
617         NG_FREE_ITEM(item);
618
619         crit_enter();
620         if (IF_QFULL(&sc->outq)) {
621                 IF_DROP(&sc->outq);
622                 crit_exit();
623                 NG_FREE_M(m);
624                 return (ENOBUFS);
625         }
626
627         IF_ENQUEUE(&sc->outq, m);
628         qlen = sc->outq.ifq_len;
629         crit_exit();
630
631         /*
632          * If qlen > 1, then we should already have a scheduled callout.
633          */
634         if (qlen == 1) {
635                 lwkt_gettoken(&tty_token);
636                 ngt_start(sc->tp);
637                 lwkt_reltoken(&tty_token);
638         }
639
640         return (0);
641 }
642
643 /*
644  * Receive control message
645  */
646 static int
647 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
648 {
649         const sc_p sc = NG_NODE_PRIVATE(node);
650         struct ng_mesg *msg, *resp = NULL;
651         int error = 0;
652
653         NGI_GET_MSG(item, msg);
654         switch (msg->header.typecookie) {
655         case NGM_TTY_COOKIE:
656                 switch (msg->header.cmd) {
657                 case NGM_TTY_SET_HOTCHAR:
658                     {
659                         int     hotchar;
660
661                         if (msg->header.arglen != sizeof(int))
662                                 ERROUT(EINVAL);
663                         hotchar = *((int *) msg->data);
664                         if (hotchar != (u_char) hotchar && hotchar != -1)
665                                 ERROUT(EINVAL);
666                         sc->hotchar = hotchar;  /* race condition is OK */
667                         break;
668                     }
669                 case NGM_TTY_GET_HOTCHAR:
670                         NG_MKRESPONSE(resp, msg, sizeof(int), M_WAITOK | M_NULLOK);
671                         if (!resp)
672                                 ERROUT(ENOMEM);
673                         /* Race condition here is OK */
674                         *((int *) resp->data) = sc->hotchar;
675                         break;
676                 default:
677                         ERROUT(EINVAL);
678                 }
679                 break;
680         default:
681                 ERROUT(EINVAL);
682         }
683 done:
684         NG_RESPOND_MSG(error, node, item, resp);
685         NG_FREE_MSG(msg);
686         return (error);
687 }
688
689 /******************************************************************
690                         INITIALIZATION
691 ******************************************************************/
692
693 /*
694  * Handle loading and unloading for this node type
695  */
696 static int
697 ngt_mod_event(module_t mod, int event, void *data)
698 {
699         int error = 0;
700
701         lwkt_gettoken(&tty_token);
702         switch (event) {
703         case MOD_LOAD:
704                 /* Register line discipline */
705                 crit_enter();
706                 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
707                         crit_exit();
708                         log(LOG_ERR, "%s: can't register line discipline",
709                             __func__);
710                         lwkt_reltoken(&tty_token);
711                         return (EIO);
712                 }
713                 crit_exit();
714                 break;
715
716         case MOD_UNLOAD:
717
718                 /* Unregister line discipline */
719                 crit_enter();
720                 ldisc_deregister(ngt_ldisc);
721                 crit_exit();
722                 break;
723
724         default:
725                 error = EOPNOTSUPP;
726                 break;
727         }
728         lwkt_reltoken(&tty_token);
729         return (error);
730 }