6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_tty.c,v 1.7.2.3 2002/02/13 00:43:12 dillon Exp $
41 * $DragonFly: src/sys/netgraph/tty/ng_tty.c,v 1.19 2008/01/06 16:55:52 swildner Exp $
42 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
46 * This file implements a terminal line discipline that is also a
47 * netgraph node. Installing this line discipline on a terminal device
48 * instantiates a new netgraph node of this type, which allows access
49 * to the device via the "hook" hook of the node.
51 * Once the line discipline is installed, you can find out the name
52 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
54 * Incoming characters are delievered to the hook one at a time, each
55 * in its own mbuf. You may optionally define a ``hotchar,'' which causes
56 * incoming characters to be buffered up until either the hotchar is
57 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
58 * are immediately delivered.
60 * NOTE: This node operates at spltty().
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
70 #include <sys/malloc.h>
71 #include <sys/fcntl.h>
73 #include <sys/ttycom.h>
74 #include <sys/syslog.h>
75 #include <sys/errno.h>
76 #include <sys/thread2.h>
78 #include <netgraph/ng_message.h>
79 #include <netgraph/netgraph.h>
83 #define MAX_MBUFQ 3 /* Max number of queued mbufs */
84 #define NGT_HIWATER 400 /* High water mark on output */
86 /* Per-node private info */
88 struct tty *tp; /* Terminal device */
89 node_p node; /* Netgraph node */
90 hook_p hook; /* Netgraph hook */
91 struct mbuf *m; /* Incoming data buffer */
92 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */
93 short qlen; /* Length of queue */
94 short hotchar; /* Hotchar, or -1 if none */
95 u_int flags; /* Flags */
96 struct callout ctimeout; /* See man timeout(9) */
98 typedef struct ngt_sc *sc_p;
101 #define FLG_TIMEOUT 0x0001 /* A timeout is pending */
102 #define FLG_DEBUG 0x0002
106 #define QUEUECHECK(sc) \
111 for (k = 0, mp = &sc->qhead; \
112 k <= MAX_MBUFQ && *mp; \
113 k++, mp = &(*mp)->m_nextpkt); \
114 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \
115 panic("%s: queue", __func__); \
118 #define QUEUECHECK(sc) do {} while (0)
121 /* Line discipline methods */
122 static int ngt_open(cdev_t dev, struct tty *tp);
123 static int ngt_close(struct tty *tp, int flag);
124 static int ngt_read(struct tty *tp, struct uio *uio, int flag);
125 static int ngt_write(struct tty *tp, struct uio *uio, int flag);
126 static int ngt_tioctl(struct tty *tp,
127 u_long cmd, caddr_t data, int flag, struct ucred *cred);
128 static int ngt_input(int c, struct tty *tp);
129 static int ngt_start(struct tty *tp);
131 /* Netgraph methods */
132 static ng_constructor_t ngt_constructor;
133 static ng_rcvmsg_t ngt_rcvmsg;
134 static ng_shutdown_t ngt_shutdown;
135 static ng_newhook_t ngt_newhook;
136 static ng_rcvdata_t ngt_rcvdata;
137 static ng_disconnect_t ngt_disconnect;
138 static int ngt_mod_event(module_t mod, int event, void *data);
141 static void ngt_timeout(void *arg);
143 #define ERROUT(x) do { error = (x); goto done; } while (0)
145 /* Line discipline descriptor */
146 static struct linesw ngt_disc = {
155 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */
158 /* Netgraph node type descriptor */
159 static struct ng_type typestruct = {
174 NETGRAPH_INIT(tty, &typestruct);
177 static int ngt_nodeop_ok; /* OK to create/remove node */
178 static int ngt_ldisc;
180 /******************************************************************
181 LINE DISCIPLINE METHODS
182 ******************************************************************/
185 * Set our line discipline on the tty.
186 * Called from device open routine or ttioctl() at >= splsofttty()
189 ngt_open(cdev_t dev, struct tty *tp)
191 struct thread *td = curthread; /* XXX */
192 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
196 /* Super-user only */
197 if ((error = priv_check(td, PRIV_ROOT)))
200 lwkt_gettoken(&tty_token);
202 /* Already installed? */
203 if (tp->t_line == NETGRAPHDISC) {
204 sc = (sc_p) tp->t_sc;
205 if (sc != NULL && sc->tp == tp)
209 /* Initialize private struct */
210 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
212 sc->hotchar = NG_TTY_DFL_HOTCHAR;
213 sc->qtail = &sc->qhead;
215 callout_init_mp(&sc->ctimeout);
217 /* Setup netgraph node */
219 error = ng_make_node_common(&typestruct, &sc->node);
222 FREE(sc, M_NETGRAPH);
225 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
227 /* Set back pointers */
228 sc->node->private = sc;
229 tp->t_sc = (caddr_t) sc;
231 /* Assign node its name */
232 if ((error = ng_name_node(sc->node, name))) {
233 log(LOG_ERR, "%s: node name exists?\n", name);
241 * Pre-allocate cblocks to the an appropriate amount.
242 * I'm not sure what is appropriate.
244 ttyflush(tp, FREAD | FWRITE);
245 clist_alloc_cblocks(&tp->t_canq, 0, 0);
246 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
247 clist_alloc_cblocks(&tp->t_outq,
248 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
252 lwkt_reltoken(&tty_token);
258 * Line specific close routine, called from device close routine
259 * and from ttioctl at >= splsofttty(). This causes the node to
260 * be destroyed as well.
263 ngt_close(struct tty *tp, int flag)
265 const sc_p sc = (sc_p) tp->t_sc;
268 lwkt_gettoken(&tty_token);
269 ttyflush(tp, FREAD | FWRITE);
270 clist_free_cblocks(&tp->t_outq);
273 if (sc->flags & FLG_TIMEOUT) {
274 callout_stop(&sc->ctimeout);
275 sc->flags &= ~FLG_TIMEOUT;
282 lwkt_reltoken(&tty_token);
288 * Once the device has been turned into a node, we don't allow reading.
291 ngt_read(struct tty *tp, struct uio *uio, int flag)
297 * Once the device has been turned into a node, we don't allow writing.
300 ngt_write(struct tty *tp, struct uio *uio, int flag)
306 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
309 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
311 const sc_p sc = (sc_p) tp->t_sc;
315 lwkt_gettoken(&tty_token);
319 struct nodeinfo *const ni = (struct nodeinfo *) data;
320 const node_p node = sc->node;
322 bzero(ni, sizeof(*ni));
324 strncpy(ni->name, node->name, sizeof(ni->name) - 1);
325 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
326 ni->id = (u_int32_t)(uintptr_t)node;
327 ni->hooks = node->numhooks;
334 lwkt_reltoken(&tty_token);
340 * Receive data coming from the device. We get one character at
341 * a time, which is kindof silly.
342 * Only guaranteed to be at splsofttty() or spltty().
345 ngt_input(int c, struct tty *tp)
347 const sc_p sc = (sc_p) tp->t_sc;
348 const node_p node = sc->node;
352 lwkt_gettoken(&tty_token);
353 if (!sc || tp != sc->tp) {
354 lwkt_reltoken(&tty_token);
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", node->name);
367 if (c & TTY_ERRORMASK) {
368 /* framing error or overrun on this char */
369 if (sc->flags & FLG_DEBUG)
370 log(LOG_DEBUG, "%s: line error %x\n",
371 node->name, c & TTY_ERRORMASK);
376 /* Get a new header mbuf if we need one */
378 MGETHDR(m, MB_DONTWAIT, MT_DATA);
380 if (sc->flags & FLG_DEBUG)
382 "%s: can't get mbuf\n", node->name);
385 m->m_len = m->m_pkthdr.len = 0;
386 m->m_pkthdr.rcvif = NULL;
390 /* Add char to mbuf */
391 *mtod(m, u_char *) = c;
396 /* Ship off mbuf if it's time */
397 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
398 m->m_data = m->m_pktdat;
399 error = ng_queue_data(sc->hook, m, NULL);
404 lwkt_reltoken(&tty_token);
409 * This is called when the device driver is ready for more output.
410 * Called from tty system at splsofttty() or spltty().
411 * Also call from ngt_rcv_data() when a new mbuf is available for output.
414 ngt_start(struct tty *tp)
416 const sc_p sc = (sc_p) tp->t_sc;
419 lwkt_gettoken(&tty_token);
420 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
421 struct mbuf *m = sc->qhead;
423 /* Remove first mbuf from queue */
426 if ((sc->qhead = m->m_nextpkt) == NULL)
427 sc->qtail = &sc->qhead;
431 /* Send as much of it as possible */
436 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
440 break; /* device can't take no more */
444 /* Put remainder of mbuf chain (if any) back on queue */
446 m->m_nextpkt = sc->qhead;
448 if (sc->qtail == &sc->qhead)
449 sc->qtail = &m->m_nextpkt;
456 /* Call output process whether or not there is any output. We are
457 * being called in lieu of ttstart and must do what it would. */
458 if (tp->t_oproc != NULL)
461 /* This timeout is needed for operation on a pseudo-tty, because the
462 * pty code doesn't call pppstart after it has drained the t_outq. */
463 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
464 callout_reset(&sc->ctimeout, 1, ngt_timeout, sc);
465 sc->flags |= FLG_TIMEOUT;
467 lwkt_reltoken(&tty_token);
473 * We still have data to output to the device, so try sending more.
476 ngt_timeout(void *arg)
478 const sc_p sc = (sc_p) arg;
481 lwkt_gettoken(&tty_token);
482 sc->flags &= ~FLG_TIMEOUT;
484 lwkt_reltoken(&tty_token);
488 /******************************************************************
489 NETGRAPH NODE METHODS
490 ******************************************************************/
493 * Initialize a new node of this type.
495 * We only allow nodes to be created as a result of setting
496 * the line discipline on a tty, so always return an error if not.
499 ngt_constructor(node_p *nodep)
503 return (ng_make_node_common(&typestruct, nodep));
507 * Add a new hook. There can only be one.
510 ngt_newhook(node_p node, hook_p hook, const char *name)
512 const sc_p sc = node->private;
515 if (strcmp(name, NG_TTY_HOOK))
518 lwkt_gettoken(&tty_token);
523 lwkt_reltoken(&tty_token);
529 * Disconnect the hook
532 ngt_disconnect(hook_p hook)
534 const sc_p sc = hook->node->private;
537 lwkt_gettoken(&tty_token);
538 if (hook != sc->hook)
543 lwkt_reltoken(&tty_token);
549 * Remove this node. The does the netgraph portion of the shutdown.
550 * This should only be called indirectly from ngt_close().
553 ngt_shutdown(node_p node)
555 const sc_p sc = node->private;
559 lwkt_gettoken(&tty_token);
562 node->private = NULL;
566 bzero(sc, sizeof(*sc));
567 FREE(sc, M_NETGRAPH);
568 lwkt_reltoken(&tty_token);
573 * Receive incoming data from netgraph system. Put it on our
574 * output queue and start output if necessary.
577 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
579 const sc_p sc = hook->node->private;
582 if (hook != sc->hook)
586 lwkt_gettoken(&tty_token);
587 if (sc->qlen >= MAX_MBUFQ)
591 sc->qtail = &m->m_nextpkt;
598 lwkt_reltoken(&tty_token);
606 * Receive control message
609 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
610 struct ng_mesg **rptr)
612 const sc_p sc = (sc_p) node->private;
613 struct ng_mesg *resp = NULL;
616 lwkt_gettoken(&tty_token);
617 switch (msg->header.typecookie) {
619 switch (msg->header.cmd) {
620 case NGM_TTY_SET_HOTCHAR:
624 if (msg->header.arglen != sizeof(int))
626 hotchar = *((int *) msg->data);
627 if (hotchar != (u_char) hotchar && hotchar != -1)
629 sc->hotchar = hotchar; /* race condition is OK */
632 case NGM_TTY_GET_HOTCHAR:
633 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
636 /* Race condition here is OK */
637 *((int *) resp->data) = sc->hotchar;
649 FREE(resp, M_NETGRAPH);
652 FREE(msg, M_NETGRAPH);
653 lwkt_reltoken(&tty_token);
657 /******************************************************************
659 ******************************************************************/
662 * Handle loading and unloading for this node type
665 ngt_mod_event(module_t mod, int event, void *data)
667 /* struct ng_type *const type = data;*/
670 lwkt_gettoken(&tty_token);
673 /* Register line discipline */
675 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
677 log(LOG_ERR, "%s: can't register line discipline",
686 /* Unregister line discipline */
688 ldisc_deregister(ngt_ldisc);
696 lwkt_reltoken(&tty_token);