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 * $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>
76 #include <netgraph/ng_message.h>
77 #include <netgraph/netgraph.h>
81 #define MAX_MBUFQ 3 /* Max number of queued mbufs */
82 #define NGT_HIWATER 400 /* High water mark on output */
84 /* Per-node private info */
86 struct tty *tp; /* Terminal device */
87 node_p node; /* Netgraph node */
88 hook_p hook; /* Netgraph hook */
89 struct mbuf *m; /* Incoming data buffer */
90 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */
91 short qlen; /* Length of queue */
92 short hotchar; /* Hotchar, or -1 if none */
93 u_int flags; /* Flags */
94 struct callout ctimeout; /* See man timeout(9) */
96 typedef struct ngt_sc *sc_p;
99 #define FLG_TIMEOUT 0x0001 /* A timeout is pending */
100 #define FLG_DEBUG 0x0002
104 #define QUEUECHECK(sc) \
109 for (k = 0, mp = &sc->qhead; \
110 k <= MAX_MBUFQ && *mp; \
111 k++, mp = &(*mp)->m_nextpkt); \
112 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \
113 panic("%s: queue", __func__); \
116 #define QUEUECHECK(sc) do {} while (0)
119 /* Line discipline methods */
120 static int ngt_open(cdev_t dev, struct tty *tp);
121 static int ngt_close(struct tty *tp, int flag);
122 static int ngt_read(struct tty *tp, struct uio *uio, int flag);
123 static int ngt_write(struct tty *tp, struct uio *uio, int flag);
124 static int ngt_tioctl(struct tty *tp,
125 u_long cmd, caddr_t data, int flag, struct ucred *cred);
126 static int ngt_input(int c, struct tty *tp);
127 static int ngt_start(struct tty *tp);
129 /* Netgraph methods */
130 static ng_constructor_t ngt_constructor;
131 static ng_rcvmsg_t ngt_rcvmsg;
132 static ng_shutdown_t ngt_shutdown;
133 static ng_newhook_t ngt_newhook;
134 static ng_rcvdata_t ngt_rcvdata;
135 static ng_disconnect_t ngt_disconnect;
136 static int ngt_mod_event(module_t mod, int event, void *data);
139 static void ngt_timeout(void *arg);
141 #define ERROUT(x) do { error = (x); goto done; } while (0)
143 /* Line discipline descriptor */
144 static struct linesw ngt_disc = {
153 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */
156 /* Netgraph node type descriptor */
157 static struct ng_type typestruct = {
172 NETGRAPH_INIT(tty, &typestruct);
175 static int ngt_nodeop_ok; /* OK to create/remove node */
176 static int ngt_ldisc;
178 /******************************************************************
179 LINE DISCIPLINE METHODS
180 ******************************************************************/
183 * Set our line discipline on the tty.
184 * Called from device open routine or ttioctl() at >= splsofttty()
187 ngt_open(cdev_t dev, struct tty *tp)
189 struct thread *td = curthread; /* XXX */
190 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
194 /* Super-user only */
195 if ((error = priv_check(td, PRIV_ROOT)))
197 lwkt_gettoken(&tp->t_token);
199 /* Already installed? */
200 if (tp->t_line == NETGRAPHDISC) {
201 sc = (sc_p) tp->t_sc;
202 if (sc != NULL && sc->tp == tp)
206 /* Initialize private struct */
207 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
209 sc->hotchar = NG_TTY_DFL_HOTCHAR;
210 sc->qtail = &sc->qhead;
212 callout_init_mp(&sc->ctimeout);
214 /* Setup netgraph node */
216 error = ng_make_node_common(&typestruct, &sc->node);
219 kfree(sc, M_NETGRAPH);
222 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
224 /* Set back pointers */
225 sc->node->private = sc;
226 tp->t_sc = (caddr_t) sc;
228 /* Assign node its name */
229 if ((error = ng_name_node(sc->node, name))) {
230 log(LOG_ERR, "%s: node name exists?\n", name);
238 * Pre-allocate cblocks to the an appropriate amount.
239 * I'm not sure what is appropriate.
241 ttyflush(tp, FREAD | FWRITE);
242 clist_alloc_cblocks(&tp->t_canq, 0);
243 clist_alloc_cblocks(&tp->t_rawq, 0);
244 clist_alloc_cblocks(&tp->t_outq, MLEN + NGT_HIWATER);
248 lwkt_reltoken(&tp->t_token);
253 * Line specific close routine, called from device close routine
254 * and from ttioctl at >= splsofttty(). This causes the node to
255 * be destroyed as well.
258 ngt_close(struct tty *tp, int flag)
260 const sc_p sc = (sc_p) tp->t_sc;
262 lwkt_gettoken(&tp->t_token);
263 ttyflush(tp, FREAD | FWRITE);
264 clist_free_cblocks(&tp->t_outq);
267 if (sc->flags & FLG_TIMEOUT) {
268 callout_stop(&sc->ctimeout);
269 sc->flags &= ~FLG_TIMEOUT;
276 lwkt_reltoken(&tp->t_token);
282 * Once the device has been turned into a node, we don't allow reading.
285 ngt_read(struct tty *tp, struct uio *uio, int flag)
291 * Once the device has been turned into a node, we don't allow writing.
294 ngt_write(struct tty *tp, struct uio *uio, int flag)
300 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
303 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
305 const sc_p sc = (sc_p) tp->t_sc;
308 lwkt_gettoken(&tp->t_token);
313 struct nodeinfo *const ni = (struct nodeinfo *) data;
314 const node_p node = sc->node;
316 bzero(ni, sizeof(*ni));
318 strncpy(ni->name, node->name, sizeof(ni->name) - 1);
319 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
320 ni->id = (u_int32_t)(uintptr_t)node;
321 ni->hooks = node->numhooks;
328 lwkt_reltoken(&tp->t_token);
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 ? sc->node : NULL;
346 lwkt_gettoken(&tp->t_token);
347 if (!sc || tp != sc->tp) {
348 lwkt_reltoken(&tp->t_token);
354 /* Check for error conditions */
355 if ((tp->t_state & TS_CONNECTED) == 0) {
356 if (sc->flags & FLG_DEBUG)
357 log(LOG_DEBUG, "%s: no carrier\n", node->name);
360 if (c & TTY_ERRORMASK) {
361 /* framing error or overrun on this char */
362 if (sc->flags & FLG_DEBUG)
363 log(LOG_DEBUG, "%s: line error %x\n",
364 node->name, c & TTY_ERRORMASK);
369 /* Get a new header mbuf if we need one */
371 MGETHDR(m, M_NOWAIT, MT_DATA);
373 if (sc->flags & FLG_DEBUG)
375 "%s: can't get mbuf\n", node->name);
378 m->m_len = m->m_pkthdr.len = 0;
379 m->m_pkthdr.rcvif = NULL;
383 /* Add char to mbuf */
384 *mtod(m, u_char *) = c;
389 /* Ship off mbuf if it's time */
390 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
391 m->m_data = m->m_pktdat;
392 error = ng_queue_data(sc->hook, m, NULL);
396 lwkt_reltoken(&tp->t_token);
402 * This is called when the device driver is ready for more output.
403 * Called from tty system at splsofttty() or spltty().
404 * Also call from ngt_rcv_data() when a new mbuf is available for output.
407 ngt_start(struct tty *tp)
409 const sc_p sc = (sc_p) tp->t_sc;
411 lwkt_gettoken(&tp->t_token);
412 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
413 struct mbuf *m = sc->qhead;
415 /* Remove first mbuf from queue */
418 if ((sc->qhead = m->m_nextpkt) == NULL)
419 sc->qtail = &sc->qhead;
423 /* Send as much of it as possible */
427 sent = m->m_len - clist_btoq(mtod(m, u_char *),
428 m->m_len, &tp->t_outq);
432 break; /* device can't take no more */
436 /* Put remainder of mbuf chain (if any) back on queue */
438 m->m_nextpkt = sc->qhead;
440 if (sc->qtail == &sc->qhead)
441 sc->qtail = &m->m_nextpkt;
448 /* Call output process whether or not there is any output. We are
449 * being called in lieu of ttstart and must do what it would. */
450 if (tp->t_oproc != NULL)
453 /* This timeout is needed for operation on a pseudo-tty, because the
454 * pty code doesn't call pppstart after it has drained the t_outq. */
455 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
456 callout_reset(&sc->ctimeout, 1, ngt_timeout, sc);
457 sc->flags |= FLG_TIMEOUT;
459 lwkt_reltoken(&tp->t_token);
465 * We still have data to output to the device, so try sending more.
468 ngt_timeout(void *arg)
470 const sc_p sc = (sc_p) arg;
471 struct tty *tp = sc->tp;
473 lwkt_gettoken(&tp->t_token);
474 sc->flags &= ~FLG_TIMEOUT;
476 lwkt_reltoken(&tp->t_token);
479 /******************************************************************
480 NETGRAPH NODE METHODS
481 ******************************************************************/
484 * Initialize a new node of this type.
486 * We only allow nodes to be created as a result of setting
487 * the line discipline on a tty, so always return an error if not.
490 ngt_constructor(node_p *nodep)
494 return (ng_make_node_common(&typestruct, nodep));
498 * Add a new hook. There can only be one.
501 ngt_newhook(node_p node, hook_p hook, const char *name)
503 const sc_p sc = node->private;
504 struct tty *tp = sc->tp;
507 if (strcmp(name, NG_TTY_HOOK))
509 lwkt_gettoken(&tp->t_token);
514 lwkt_reltoken(&tp->t_token);
520 * Disconnect the hook
523 ngt_disconnect(hook_p hook)
525 const sc_p sc = hook->node->private;
526 struct tty *tp = sc->tp;
528 lwkt_gettoken(&tp->t_token);
529 if (hook != sc->hook)
534 lwkt_reltoken(&tp->t_token);
540 * Remove this node. The does the netgraph portion of the shutdown.
541 * This should only be called indirectly from ngt_close().
544 ngt_shutdown(node_p node)
546 const sc_p sc = node->private;
547 struct tty *tp = sc->tp;
551 lwkt_gettoken(&tp->t_token);
554 node->private = NULL;
558 bzero(sc, sizeof(*sc));
559 kfree(sc, M_NETGRAPH);
560 lwkt_reltoken(&tp->t_token);
566 * Receive incoming data from netgraph system. Put it on our
567 * output queue and start output if necessary.
570 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
572 const sc_p sc = hook->node->private;
573 struct tty *tp = sc->tp;
576 if (hook != sc->hook)
579 lwkt_gettoken(&tp->t_token);
580 if (sc->qlen >= MAX_MBUFQ)
584 sc->qtail = &m->m_nextpkt;
591 lwkt_reltoken(&tp->t_token);
598 * Receive control message
601 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
602 struct ng_mesg **rptr)
604 const sc_p sc = (sc_p) node->private;
605 struct tty *tp = sc->tp;
606 struct ng_mesg *resp = NULL;
609 lwkt_gettoken(&tp->t_token);
610 switch (msg->header.typecookie) {
612 switch (msg->header.cmd) {
613 case NGM_TTY_SET_HOTCHAR:
617 if (msg->header.arglen != sizeof(int))
619 hotchar = *((int *) msg->data);
620 if (hotchar != (u_char) hotchar && hotchar != -1)
622 sc->hotchar = hotchar; /* race condition is OK */
625 case NGM_TTY_GET_HOTCHAR:
626 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
629 /* Race condition here is OK */
630 *((int *) resp->data) = sc->hotchar;
642 kfree(resp, M_NETGRAPH);
645 kfree(msg, M_NETGRAPH);
646 lwkt_reltoken(&tp->t_token);
651 /******************************************************************
653 ******************************************************************/
656 * Handle loading and unloading for this node type
659 ngt_mod_event(module_t mod, int event, void *data)
661 /* struct ng_type *const type = data;*/
666 /* Register line discipline */
667 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
668 log(LOG_ERR, "%s: can't register line discipline",
676 /* Unregister line discipline */
677 ldisc_deregister(ngt_ldisc);