5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_tty.c,v 1.7.2.3 2002/02/13 00:43:12 dillon Exp $
40 * $DragonFly: src/sys/netgraph/tty/ng_tty.c,v 1.19 2008/01/06 16:55:52 swildner Exp $
41 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
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.
50 * Once the line discipline is installed, you can find out the name
51 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
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.
59 * NOTE: This node operates at spltty().
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/fcntl.h>
72 #include <sys/ttycom.h>
73 #include <sys/syslog.h>
74 #include <sys/errno.h>
75 #include <sys/ioccom.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)))
201 /* Already installed? */
202 if (tp->t_line == NETGRAPHDISC) {
203 sc = (sc_p) tp->t_sc;
204 if (sc != NULL && sc->tp == tp)
208 /* Initialize private struct */
209 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
211 sc->hotchar = NG_TTY_DFL_HOTCHAR;
212 sc->qtail = &sc->qhead;
214 callout_init(&sc->ctimeout);
216 /* Setup netgraph node */
218 error = ng_make_node_common(&typestruct, &sc->node);
221 FREE(sc, M_NETGRAPH);
224 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
226 /* Set back pointers */
227 sc->node->private = sc;
228 tp->t_sc = (caddr_t) sc;
230 /* Assign node its name */
231 if ((error = ng_name_node(sc->node, name))) {
232 log(LOG_ERR, "%s: node name exists?\n", name);
240 * Pre-allocate cblocks to the an appropriate amount.
241 * I'm not sure what is appropriate.
243 ttyflush(tp, FREAD | FWRITE);
244 clist_alloc_cblocks(&tp->t_canq, 0, 0);
245 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
246 clist_alloc_cblocks(&tp->t_outq,
247 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
256 * Line specific close routine, called from device close routine
257 * and from ttioctl at >= splsofttty(). This causes the node to
258 * be destroyed as well.
261 ngt_close(struct tty *tp, int flag)
263 const sc_p sc = (sc_p) tp->t_sc;
266 ttyflush(tp, FREAD | FWRITE);
267 clist_free_cblocks(&tp->t_outq);
270 if (sc->flags & FLG_TIMEOUT) {
271 callout_stop(&sc->ctimeout);
272 sc->flags &= ~FLG_TIMEOUT;
284 * Once the device has been turned into a node, we don't allow reading.
287 ngt_read(struct tty *tp, struct uio *uio, int flag)
293 * Once the device has been turned into a node, we don't allow writing.
296 ngt_write(struct tty *tp, struct uio *uio, int flag)
302 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
305 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
307 const sc_p sc = (sc_p) tp->t_sc;
314 struct nodeinfo *const ni = (struct nodeinfo *) data;
315 const node_p node = sc->node;
317 bzero(ni, sizeof(*ni));
319 strncpy(ni->name, node->name, sizeof(ni->name) - 1);
320 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
321 ni->id = (u_int32_t) node;
322 ni->hooks = node->numhooks;
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().
339 ngt_input(int c, struct tty *tp)
341 const sc_p sc = (sc_p) tp->t_sc;
342 const node_p node = sc->node;
346 if (!sc || tp != sc->tp)
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", node->name);
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 node->name, c & TTY_ERRORMASK);
367 /* Get a new header mbuf if we need one */
369 MGETHDR(m, MB_DONTWAIT, MT_DATA);
371 if (sc->flags & FLG_DEBUG)
373 "%s: can't get mbuf\n", node->name);
376 m->m_len = m->m_pkthdr.len = 0;
377 m->m_pkthdr.rcvif = NULL;
381 /* Add char to mbuf */
382 *mtod(m, u_char *) = c;
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 error = ng_queue_data(sc->hook, m, NULL);
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.
404 ngt_start(struct tty *tp)
406 const sc_p sc = (sc_p) tp->t_sc;
409 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
410 struct mbuf *m = sc->qhead;
412 /* Remove first mbuf from queue */
415 if ((sc->qhead = m->m_nextpkt) == NULL)
416 sc->qtail = &sc->qhead;
420 /* Send as much of it as possible */
425 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
429 break; /* device can't take no more */
433 /* Put remainder of mbuf chain (if any) back on queue */
435 m->m_nextpkt = sc->qhead;
437 if (sc->qtail == &sc->qhead)
438 sc->qtail = &m->m_nextpkt;
445 /* Call output process whether or not there is any output. We are
446 * being called in lieu of ttstart and must do what it would. */
447 if (tp->t_oproc != NULL)
450 /* This timeout is needed for operation on a pseudo-tty, because the
451 * pty code doesn't call pppstart after it has drained the t_outq. */
452 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
453 callout_reset(&sc->ctimeout, 1, ngt_timeout, sc);
454 sc->flags |= FLG_TIMEOUT;
461 * We still have data to output to the device, so try sending more.
464 ngt_timeout(void *arg)
466 const sc_p sc = (sc_p) arg;
469 sc->flags &= ~FLG_TIMEOUT;
474 /******************************************************************
475 NETGRAPH NODE METHODS
476 ******************************************************************/
479 * Initialize a new node of this type.
481 * We only allow nodes to be created as a result of setting
482 * the line discipline on a tty, so always return an error if not.
485 ngt_constructor(node_p *nodep)
489 return (ng_make_node_common(&typestruct, nodep));
493 * Add a new hook. There can only be one.
496 ngt_newhook(node_p node, hook_p hook, const char *name)
498 const sc_p sc = node->private;
501 if (strcmp(name, NG_TTY_HOOK))
513 * Disconnect the hook
516 ngt_disconnect(hook_p hook)
518 const sc_p sc = hook->node->private;
521 if (hook != sc->hook)
531 * Remove this node. The does the netgraph portion of the shutdown.
532 * This should only be called indirectly from ngt_close().
535 ngt_shutdown(node_p node)
537 const sc_p sc = node->private;
543 node->private = NULL;
547 bzero(sc, sizeof(*sc));
548 FREE(sc, M_NETGRAPH);
553 * Receive incoming data from netgraph system. Put it on our
554 * output queue and start output if necessary.
557 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
559 const sc_p sc = hook->node->private;
562 if (hook != sc->hook)
566 if (sc->qlen >= MAX_MBUFQ)
570 sc->qtail = &m->m_nextpkt;
584 * Receive control message
587 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
588 struct ng_mesg **rptr)
590 const sc_p sc = (sc_p) node->private;
591 struct ng_mesg *resp = NULL;
594 switch (msg->header.typecookie) {
596 switch (msg->header.cmd) {
597 case NGM_TTY_SET_HOTCHAR:
601 if (msg->header.arglen != sizeof(int))
603 hotchar = *((int *) msg->data);
604 if (hotchar != (u_char) hotchar && hotchar != -1)
606 sc->hotchar = hotchar; /* race condition is OK */
609 case NGM_TTY_GET_HOTCHAR:
610 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
613 /* Race condition here is OK */
614 *((int *) resp->data) = sc->hotchar;
626 FREE(resp, M_NETGRAPH);
629 FREE(msg, M_NETGRAPH);
633 /******************************************************************
635 ******************************************************************/
638 * Handle loading and unloading for this node type
641 ngt_mod_event(module_t mod, int event, void *data)
643 /* struct ng_type *const type = data;*/
648 /* Register line discipline */
650 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
652 log(LOG_ERR, "%s: can't register line discipline",
661 /* Unregister line discipline */
663 ldisc_deregister(ngt_ldisc);