Deleted Added
full compact
ng_tty.c (59728) ng_tty.c (65557)
1
2/*
3 * ng_tty.c
4 *
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
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.
18 *
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
35 * OF SUCH DAMAGE.
36 *
37 * Author: Archie Cobbs <archie@whistle.com>
38 *
1
2/*
3 * ng_tty.c
4 *
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
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.
18 *
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
35 * OF SUCH DAMAGE.
36 *
37 * Author: Archie Cobbs <archie@whistle.com>
38 *
39 * $FreeBSD: head/sys/netgraph/ng_tty.c 59728 2000-04-28 17:09:00Z julian $
39 * $FreeBSD: head/sys/netgraph/ng_tty.c 65557 2000-09-07 01:33:02Z jasone $
40 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
41 */
42
43/*
44 * This file implements a terminal line discipline that is also a
45 * netgraph node. Installing this line discipline on a terminal device
46 * instantiates a new netgraph node of this type, which allows access
47 * to the device via the "hook" hook of the node.
48 *
49 * Once the line discipline is installed, you can find out the name
50 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
51 *
52 * Incoming characters are delievered to the hook one at a time, each
53 * in its own mbuf. You may optionally define a ``hotchar,'' which causes
54 * incoming characters to be buffered up until either the hotchar is
55 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
56 * are immediately delivered.
57 *
58 * NOTE: This node operates at spltty().
59 */
60
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/kernel.h>
64#include <sys/conf.h>
65#include <sys/proc.h>
66#include <sys/mbuf.h>
67#include <sys/malloc.h>
68#include <sys/fcntl.h>
69#include <sys/tty.h>
70#include <sys/ttycom.h>
71#include <sys/syslog.h>
72#include <sys/errno.h>
73#include <sys/ioccom.h>
74
75#include <netgraph/ng_message.h>
76#include <netgraph/netgraph.h>
77#include <netgraph/ng_tty.h>
78
79#ifdef __i386__ /* fiddle with the spl locking */
40 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
41 */
42
43/*
44 * This file implements a terminal line discipline that is also a
45 * netgraph node. Installing this line discipline on a terminal device
46 * instantiates a new netgraph node of this type, which allows access
47 * to the device via the "hook" hook of the node.
48 *
49 * Once the line discipline is installed, you can find out the name
50 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
51 *
52 * Incoming characters are delievered to the hook one at a time, each
53 * in its own mbuf. You may optionally define a ``hotchar,'' which causes
54 * incoming characters to be buffered up until either the hotchar is
55 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
56 * are immediately delivered.
57 *
58 * NOTE: This node operates at spltty().
59 */
60
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/kernel.h>
64#include <sys/conf.h>
65#include <sys/proc.h>
66#include <sys/mbuf.h>
67#include <sys/malloc.h>
68#include <sys/fcntl.h>
69#include <sys/tty.h>
70#include <sys/ttycom.h>
71#include <sys/syslog.h>
72#include <sys/errno.h>
73#include <sys/ioccom.h>
74
75#include <netgraph/ng_message.h>
76#include <netgraph/netgraph.h>
77#include <netgraph/ng_tty.h>
78
79#ifdef __i386__ /* fiddle with the spl locking */
80#include <sys/bus.h>
80#include <machine/ipl.h>
81#include <i386/isa/intr_machdep.h>
82#endif
83
84/* Misc defs */
85#define MAX_MBUFQ 3 /* Max number of queued mbufs */
86#define NGT_HIWATER 400 /* High water mark on output */
87
88/* Per-node private info */
89struct ngt_sc {
90 struct tty *tp; /* Terminal device */
91 node_p node; /* Netgraph node */
92 hook_p hook; /* Netgraph hook */
93 struct mbuf *m; /* Incoming data buffer */
94 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */
95 short qlen; /* Length of queue */
96 short hotchar; /* Hotchar, or -1 if none */
97 u_int flags; /* Flags */
98 struct callout_handle chand; /* See man timeout(9) */
99};
100typedef struct ngt_sc *sc_p;
101
102/* Flags */
103#define FLG_TIMEOUT 0x0001 /* A timeout is pending */
104#define FLG_DEBUG 0x0002
105
106/* Debugging */
107#ifdef INVARIANTS
108#define QUEUECHECK(sc) \
109 do { \
110 struct mbuf **mp; \
111 int k; \
112 \
113 for (k = 0, mp = &sc->qhead; \
114 k <= MAX_MBUFQ && *mp; \
115 k++, mp = &(*mp)->m_nextpkt); \
116 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \
117 panic(__FUNCTION__ ": queue"); \
118 } while (0)
119#else
120#define QUEUECHECK(sc) do {} while (0)
121#endif
122
123/* Line discipline methods */
124static int ngt_open(dev_t dev, struct tty *tp);
125static int ngt_close(struct tty *tp, int flag);
126static int ngt_read(struct tty *tp, struct uio *uio, int flag);
127static int ngt_write(struct tty *tp, struct uio *uio, int flag);
128static int ngt_tioctl(struct tty *tp,
129 u_long cmd, caddr_t data, int flag, struct proc *);
130static int ngt_input(int c, struct tty *tp);
131static int ngt_start(struct tty *tp);
132
133/* Netgraph methods */
134static ng_constructor_t ngt_constructor;
135static ng_rcvmsg_t ngt_rcvmsg;
136static ng_shutdown_t ngt_shutdown;
137static ng_newhook_t ngt_newhook;
138static ng_rcvdata_t ngt_rcvdata;
139static ng_disconnect_t ngt_disconnect;
140static int ngt_mod_event(module_t mod, int event, void *data);
141
142/* Other stuff */
143static void ngt_timeout(void *arg);
144
145#define ERROUT(x) do { error = (x); goto done; } while (0)
146
147/* Line discipline descriptor */
148static struct linesw ngt_disc = {
149 ngt_open,
150 ngt_close,
151 ngt_read,
152 ngt_write,
153 ngt_tioctl,
154 ngt_input,
155 ngt_start,
156 ttymodem,
157 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */
158};
159
160/* Netgraph node type descriptor */
161static struct ng_type typestruct = {
162 NG_VERSION,
163 NG_TTY_NODE_TYPE,
164 ngt_mod_event,
165 ngt_constructor,
166 ngt_rcvmsg,
167 ngt_shutdown,
168 ngt_newhook,
169 NULL,
170 NULL,
171 ngt_rcvdata,
172 ngt_rcvdata,
173 ngt_disconnect,
174 NULL
175};
176NETGRAPH_INIT(tty, &typestruct);
177
178static int ngt_unit;
179static int ngt_nodeop_ok; /* OK to create/remove node */
180static int ngt_ldisc;
181
182/******************************************************************
183 LINE DISCIPLINE METHODS
184******************************************************************/
185
186/*
187 * Set our line discipline on the tty.
188 * Called from device open routine or ttioctl() at >= splsofttty()
189 */
190static int
191ngt_open(dev_t dev, struct tty *tp)
192{
193 struct proc *const p = curproc; /* XXX */
194 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
195 sc_p sc;
196 int s, error;
197
198 /* Super-user only */
199 if ((error = suser(p)))
200 return (error);
201 s = splnet();
202 (void) spltty(); /* XXX is this necessary? */
203
204 /* Already installed? */
205 if (tp->t_line == NETGRAPHDISC) {
206 sc = (sc_p) tp->t_sc;
207 if (sc != NULL && sc->tp == tp)
208 goto done;
209 }
210
211 /* Initialize private struct */
212 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK);
213 if (sc == NULL) {
214 error = ENOMEM;
215 goto done;
216 }
217 bzero(sc, sizeof(*sc));
218 sc->tp = tp;
219 sc->hotchar = NG_TTY_DFL_HOTCHAR;
220 sc->qtail = &sc->qhead;
221 QUEUECHECK(sc);
222 callout_handle_init(&sc->chand);
223
224 /* Setup netgraph node */
225 ngt_nodeop_ok = 1;
226 error = ng_make_node_common(&typestruct, &sc->node);
227 ngt_nodeop_ok = 0;
228 if (error) {
229 FREE(sc, M_NETGRAPH);
230 goto done;
231 }
232 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
233
234 /* Set back pointers */
235 sc->node->private = sc;
236 tp->t_sc = (caddr_t) sc;
237
238 /* Assign node its name */
239 if ((error = ng_name_node(sc->node, name))) {
240 log(LOG_ERR, "%s: node name exists?\n", name);
241 ngt_nodeop_ok = 1;
242 ng_rmnode(sc->node);
243 ngt_nodeop_ok = 0;
244 goto done;
245 }
246
247 /*
248 * Pre-allocate cblocks to the an appropriate amount.
249 * I'm not sure what is appropriate.
250 */
251 ttyflush(tp, FREAD | FWRITE);
252 clist_alloc_cblocks(&tp->t_canq, 0, 0);
253 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
254 clist_alloc_cblocks(&tp->t_outq,
255 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
256
257done:
258 /* Done */
259 splx(s);
260 return (error);
261}
262
263/*
264 * Line specific close routine, called from device close routine
265 * and from ttioctl at >= splsofttty(). This causes the node to
266 * be destroyed as well.
267 */
268static int
269ngt_close(struct tty *tp, int flag)
270{
271 const sc_p sc = (sc_p) tp->t_sc;
272 int s;
273
274 s = spltty();
275 ttyflush(tp, FREAD | FWRITE);
276 clist_free_cblocks(&tp->t_outq);
277 tp->t_line = 0;
278 if (sc != NULL) {
279 if (sc->flags & FLG_TIMEOUT) {
280 untimeout(ngt_timeout, sc, sc->chand);
281 sc->flags &= ~FLG_TIMEOUT;
282 }
283 ngt_nodeop_ok = 1;
284 ng_rmnode(sc->node);
285 ngt_nodeop_ok = 0;
286 tp->t_sc = NULL;
287 }
288 splx(s);
289 return (0);
290}
291
292/*
293 * Once the device has been turned into a node, we don't allow reading.
294 */
295static int
296ngt_read(struct tty *tp, struct uio *uio, int flag)
297{
298 return (EIO);
299}
300
301/*
302 * Once the device has been turned into a node, we don't allow writing.
303 */
304static int
305ngt_write(struct tty *tp, struct uio *uio, int flag)
306{
307 return (EIO);
308}
309
310/*
311 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
312 */
313static int
314ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p)
315{
316 const sc_p sc = (sc_p) tp->t_sc;
317 int s, error = 0;
318
319 s = spltty();
320 switch (cmd) {
321 case NGIOCGINFO:
322 {
323 struct nodeinfo *const ni = (struct nodeinfo *) data;
324 const node_p node = sc->node;
325
326 bzero(ni, sizeof(*ni));
327 if (node->name)
328 strncpy(ni->name, node->name, sizeof(ni->name) - 1);
329 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
330 ni->id = (u_int32_t) node;
331 ni->hooks = node->numhooks;
332 break;
333 }
334 default:
335 ERROUT(ENOIOCTL);
336 }
337done:
338 splx(s);
339 return (error);
340}
341
342/*
343 * Receive data coming from the device. We get one character at
344 * a time, which is kindof silly.
345 * Only guaranteed to be at splsofttty() or spltty().
346 */
347static int
348ngt_input(int c, struct tty *tp)
349{
350 const sc_p sc = (sc_p) tp->t_sc;
351 const node_p node = sc->node;
352 struct mbuf *m;
353 int s, error = 0;
354
355 if (!sc || tp != sc->tp)
356 return (0);
357 s = spltty();
358 if (!sc->hook)
359 ERROUT(0);
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", node->name);
365 ERROUT(0);
366 }
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);
372 ERROUT(0);
373 }
374 c &= TTY_CHARMASK;
375
376 /* Get a new header mbuf if we need one */
377 if (!(m = sc->m)) {
378 MGETHDR(m, M_DONTWAIT, MT_DATA);
379 if (!m) {
380 if (sc->flags & FLG_DEBUG)
381 log(LOG_ERR,
382 "%s: can't get mbuf\n", node->name);
383 ERROUT(ENOBUFS);
384 }
385 m->m_len = m->m_pkthdr.len = 0;
386 m->m_pkthdr.rcvif = NULL;
387 sc->m = m;
388 }
389
390 /* Add char to mbuf */
391 *mtod(m, u_char *) = c;
392 m->m_data++;
393 m->m_len++;
394 m->m_pkthdr.len++;
395
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);
400 sc->m = NULL;
401 }
402done:
403 splx(s);
404 return (error);
405}
406
407/*
408 * This is called when the device driver is ready for more output.
409 * Called from tty system at splsofttty() or spltty().
410 * Also call from ngt_rcv_data() when a new mbuf is available for output.
411 */
412static int
413ngt_start(struct tty *tp)
414{
415 const sc_p sc = (sc_p) tp->t_sc;
416 int s;
417
418 s = spltty();
419 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
420 struct mbuf *m = sc->qhead;
421
422 /* Remove first mbuf from queue */
423 if (!m)
424 break;
425 if ((sc->qhead = m->m_nextpkt) == NULL)
426 sc->qtail = &sc->qhead;
427 sc->qlen--;
428 QUEUECHECK(sc);
429
430 /* Send as much of it as possible */
431 while (m) {
432 struct mbuf *m2;
433 int sent;
434
435 sent = m->m_len
436 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
437 m->m_data += sent;
438 m->m_len -= sent;
439 if (m->m_len > 0)
440 break; /* device can't take no more */
441 MFREE(m, m2);
442 m = m2;
443 }
444
445 /* Put remainder of mbuf chain (if any) back on queue */
446 if (m) {
447 m->m_nextpkt = sc->qhead;
448 sc->qhead = m;
449 if (sc->qtail == &sc->qhead)
450 sc->qtail = &m->m_nextpkt;
451 sc->qlen++;
452 QUEUECHECK(sc);
453 break;
454 }
455 }
456
457 /* Call output process whether or not there is any output. We are
458 * being called in lieu of ttstart and must do what it would. */
459 if (tp->t_oproc != NULL)
460 (*tp->t_oproc) (tp);
461
462 /* This timeout is needed for operation on a pseudo-tty, because the
463 * pty code doesn't call pppstart after it has drained the t_outq. */
464 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
465 sc->chand = timeout(ngt_timeout, sc, 1);
466 sc->flags |= FLG_TIMEOUT;
467 }
468 splx(s);
469 return (0);
470}
471
472/*
473 * We still have data to output to the device, so try sending more.
474 */
475static void
476ngt_timeout(void *arg)
477{
478 const sc_p sc = (sc_p) arg;
479 int s;
480
481 s = spltty();
482 sc->flags &= ~FLG_TIMEOUT;
483 ngt_start(sc->tp);
484 splx(s);
485}
486
487/******************************************************************
488 NETGRAPH NODE METHODS
489******************************************************************/
490
491/*
492 * Initialize a new node of this type.
493 *
494 * We only allow nodes to be created as a result of setting
495 * the line discipline on a tty, so always return an error if not.
496 */
497static int
498ngt_constructor(node_p *nodep)
499{
500 if (!ngt_nodeop_ok)
501 return (EOPNOTSUPP);
502 return (ng_make_node_common(&typestruct, nodep));
503}
504
505/*
506 * Add a new hook. There can only be one.
507 */
508static int
509ngt_newhook(node_p node, hook_p hook, const char *name)
510{
511 const sc_p sc = node->private;
512 int s, error = 0;
513
514 if (strcmp(name, NG_TTY_HOOK))
515 return (EINVAL);
516 s = spltty();
517 if (sc->hook)
518 ERROUT(EISCONN);
519 sc->hook = hook;
520done:
521 splx(s);
522 return (error);
523}
524
525/*
526 * Disconnect the hook
527 */
528static int
529ngt_disconnect(hook_p hook)
530{
531 const sc_p sc = hook->node->private;
532 int s;
533
534 s = spltty();
535 if (hook != sc->hook)
536 panic(__FUNCTION__);
537 sc->hook = NULL;
538 m_freem(sc->m);
539 sc->m = NULL;
540 splx(s);
541 return (0);
542}
543
544/*
545 * Remove this node. The does the netgraph portion of the shutdown.
546 * This should only be called indirectly from ngt_close().
547 */
548static int
549ngt_shutdown(node_p node)
550{
551 const sc_p sc = node->private;
552
553 if (!ngt_nodeop_ok)
554 return (EOPNOTSUPP);
555 ng_unname(node);
556 ng_cutlinks(node);
557 node->private = NULL;
558 ng_unref(sc->node);
559 m_freem(sc->qhead);
560 m_freem(sc->m);
561 bzero(sc, sizeof(*sc));
562 FREE(sc, M_NETGRAPH);
563 return (0);
564}
565
566/*
567 * Receive incoming data from netgraph system. Put it on our
568 * output queue and start output if necessary.
569 */
570static int
571ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
572 struct mbuf **ret_m, meta_p *ret_meta)
573{
574 const sc_p sc = hook->node->private;
575 int s, error = 0;
576
577 if (hook != sc->hook)
578 panic(__FUNCTION__);
579 NG_FREE_META(meta);
580 s = spltty();
581 if (sc->qlen >= MAX_MBUFQ)
582 ERROUT(ENOBUFS);
583 m->m_nextpkt = NULL;
584 *sc->qtail = m;
585 sc->qtail = &m->m_nextpkt;
586 sc->qlen++;
587 QUEUECHECK(sc);
588 m = NULL;
589 if (sc->qlen == 1)
590 ngt_start(sc->tp);
591done:
592 splx(s);
593 if (m)
594 m_freem(m);
595 return (error);
596}
597
598/*
599 * Receive control message
600 */
601static int
602ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
603 struct ng_mesg **rptr, hook_p lasthook)
604{
605 const sc_p sc = (sc_p) node->private;
606 struct ng_mesg *resp = NULL;
607 int error = 0;
608
609 switch (msg->header.typecookie) {
610 case NGM_TTY_COOKIE:
611 switch (msg->header.cmd) {
612 case NGM_TTY_SET_HOTCHAR:
613 {
614 int hotchar;
615
616 if (msg->header.arglen != sizeof(int))
617 ERROUT(EINVAL);
618 hotchar = *((int *) msg->data);
619 if (hotchar != (u_char) hotchar && hotchar != -1)
620 ERROUT(EINVAL);
621 sc->hotchar = hotchar; /* race condition is OK */
622 break;
623 }
624 case NGM_TTY_GET_HOTCHAR:
625 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
626 if (!resp)
627 ERROUT(ENOMEM);
628 /* Race condition here is OK */
629 *((int *) resp->data) = sc->hotchar;
630 break;
631 default:
632 ERROUT(EINVAL);
633 }
634 break;
635 default:
636 ERROUT(EINVAL);
637 }
638 if (rptr)
639 *rptr = resp;
640 else if (resp)
641 FREE(resp, M_NETGRAPH);
642
643done:
644 FREE(msg, M_NETGRAPH);
645 return (error);
646}
647
648/******************************************************************
649 INITIALIZATION
650******************************************************************/
651
652/*
653 * Handle loading and unloading for this node type
654 */
655static int
656ngt_mod_event(module_t mod, int event, void *data)
657{
658 /* struct ng_type *const type = data;*/
659 int s, error = 0;
660
661 switch (event) {
662 case MOD_LOAD:
81#include <machine/ipl.h>
82#include <i386/isa/intr_machdep.h>
83#endif
84
85/* Misc defs */
86#define MAX_MBUFQ 3 /* Max number of queued mbufs */
87#define NGT_HIWATER 400 /* High water mark on output */
88
89/* Per-node private info */
90struct ngt_sc {
91 struct tty *tp; /* Terminal device */
92 node_p node; /* Netgraph node */
93 hook_p hook; /* Netgraph hook */
94 struct mbuf *m; /* Incoming data buffer */
95 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */
96 short qlen; /* Length of queue */
97 short hotchar; /* Hotchar, or -1 if none */
98 u_int flags; /* Flags */
99 struct callout_handle chand; /* See man timeout(9) */
100};
101typedef struct ngt_sc *sc_p;
102
103/* Flags */
104#define FLG_TIMEOUT 0x0001 /* A timeout is pending */
105#define FLG_DEBUG 0x0002
106
107/* Debugging */
108#ifdef INVARIANTS
109#define QUEUECHECK(sc) \
110 do { \
111 struct mbuf **mp; \
112 int k; \
113 \
114 for (k = 0, mp = &sc->qhead; \
115 k <= MAX_MBUFQ && *mp; \
116 k++, mp = &(*mp)->m_nextpkt); \
117 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \
118 panic(__FUNCTION__ ": queue"); \
119 } while (0)
120#else
121#define QUEUECHECK(sc) do {} while (0)
122#endif
123
124/* Line discipline methods */
125static int ngt_open(dev_t dev, struct tty *tp);
126static int ngt_close(struct tty *tp, int flag);
127static int ngt_read(struct tty *tp, struct uio *uio, int flag);
128static int ngt_write(struct tty *tp, struct uio *uio, int flag);
129static int ngt_tioctl(struct tty *tp,
130 u_long cmd, caddr_t data, int flag, struct proc *);
131static int ngt_input(int c, struct tty *tp);
132static int ngt_start(struct tty *tp);
133
134/* Netgraph methods */
135static ng_constructor_t ngt_constructor;
136static ng_rcvmsg_t ngt_rcvmsg;
137static ng_shutdown_t ngt_shutdown;
138static ng_newhook_t ngt_newhook;
139static ng_rcvdata_t ngt_rcvdata;
140static ng_disconnect_t ngt_disconnect;
141static int ngt_mod_event(module_t mod, int event, void *data);
142
143/* Other stuff */
144static void ngt_timeout(void *arg);
145
146#define ERROUT(x) do { error = (x); goto done; } while (0)
147
148/* Line discipline descriptor */
149static struct linesw ngt_disc = {
150 ngt_open,
151 ngt_close,
152 ngt_read,
153 ngt_write,
154 ngt_tioctl,
155 ngt_input,
156 ngt_start,
157 ttymodem,
158 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */
159};
160
161/* Netgraph node type descriptor */
162static struct ng_type typestruct = {
163 NG_VERSION,
164 NG_TTY_NODE_TYPE,
165 ngt_mod_event,
166 ngt_constructor,
167 ngt_rcvmsg,
168 ngt_shutdown,
169 ngt_newhook,
170 NULL,
171 NULL,
172 ngt_rcvdata,
173 ngt_rcvdata,
174 ngt_disconnect,
175 NULL
176};
177NETGRAPH_INIT(tty, &typestruct);
178
179static int ngt_unit;
180static int ngt_nodeop_ok; /* OK to create/remove node */
181static int ngt_ldisc;
182
183/******************************************************************
184 LINE DISCIPLINE METHODS
185******************************************************************/
186
187/*
188 * Set our line discipline on the tty.
189 * Called from device open routine or ttioctl() at >= splsofttty()
190 */
191static int
192ngt_open(dev_t dev, struct tty *tp)
193{
194 struct proc *const p = curproc; /* XXX */
195 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
196 sc_p sc;
197 int s, error;
198
199 /* Super-user only */
200 if ((error = suser(p)))
201 return (error);
202 s = splnet();
203 (void) spltty(); /* XXX is this necessary? */
204
205 /* Already installed? */
206 if (tp->t_line == NETGRAPHDISC) {
207 sc = (sc_p) tp->t_sc;
208 if (sc != NULL && sc->tp == tp)
209 goto done;
210 }
211
212 /* Initialize private struct */
213 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK);
214 if (sc == NULL) {
215 error = ENOMEM;
216 goto done;
217 }
218 bzero(sc, sizeof(*sc));
219 sc->tp = tp;
220 sc->hotchar = NG_TTY_DFL_HOTCHAR;
221 sc->qtail = &sc->qhead;
222 QUEUECHECK(sc);
223 callout_handle_init(&sc->chand);
224
225 /* Setup netgraph node */
226 ngt_nodeop_ok = 1;
227 error = ng_make_node_common(&typestruct, &sc->node);
228 ngt_nodeop_ok = 0;
229 if (error) {
230 FREE(sc, M_NETGRAPH);
231 goto done;
232 }
233 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
234
235 /* Set back pointers */
236 sc->node->private = sc;
237 tp->t_sc = (caddr_t) sc;
238
239 /* Assign node its name */
240 if ((error = ng_name_node(sc->node, name))) {
241 log(LOG_ERR, "%s: node name exists?\n", name);
242 ngt_nodeop_ok = 1;
243 ng_rmnode(sc->node);
244 ngt_nodeop_ok = 0;
245 goto done;
246 }
247
248 /*
249 * Pre-allocate cblocks to the an appropriate amount.
250 * I'm not sure what is appropriate.
251 */
252 ttyflush(tp, FREAD | FWRITE);
253 clist_alloc_cblocks(&tp->t_canq, 0, 0);
254 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
255 clist_alloc_cblocks(&tp->t_outq,
256 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
257
258done:
259 /* Done */
260 splx(s);
261 return (error);
262}
263
264/*
265 * Line specific close routine, called from device close routine
266 * and from ttioctl at >= splsofttty(). This causes the node to
267 * be destroyed as well.
268 */
269static int
270ngt_close(struct tty *tp, int flag)
271{
272 const sc_p sc = (sc_p) tp->t_sc;
273 int s;
274
275 s = spltty();
276 ttyflush(tp, FREAD | FWRITE);
277 clist_free_cblocks(&tp->t_outq);
278 tp->t_line = 0;
279 if (sc != NULL) {
280 if (sc->flags & FLG_TIMEOUT) {
281 untimeout(ngt_timeout, sc, sc->chand);
282 sc->flags &= ~FLG_TIMEOUT;
283 }
284 ngt_nodeop_ok = 1;
285 ng_rmnode(sc->node);
286 ngt_nodeop_ok = 0;
287 tp->t_sc = NULL;
288 }
289 splx(s);
290 return (0);
291}
292
293/*
294 * Once the device has been turned into a node, we don't allow reading.
295 */
296static int
297ngt_read(struct tty *tp, struct uio *uio, int flag)
298{
299 return (EIO);
300}
301
302/*
303 * Once the device has been turned into a node, we don't allow writing.
304 */
305static int
306ngt_write(struct tty *tp, struct uio *uio, int flag)
307{
308 return (EIO);
309}
310
311/*
312 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
313 */
314static int
315ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p)
316{
317 const sc_p sc = (sc_p) tp->t_sc;
318 int s, error = 0;
319
320 s = spltty();
321 switch (cmd) {
322 case NGIOCGINFO:
323 {
324 struct nodeinfo *const ni = (struct nodeinfo *) data;
325 const node_p node = sc->node;
326
327 bzero(ni, sizeof(*ni));
328 if (node->name)
329 strncpy(ni->name, node->name, sizeof(ni->name) - 1);
330 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
331 ni->id = (u_int32_t) node;
332 ni->hooks = node->numhooks;
333 break;
334 }
335 default:
336 ERROUT(ENOIOCTL);
337 }
338done:
339 splx(s);
340 return (error);
341}
342
343/*
344 * Receive data coming from the device. We get one character at
345 * a time, which is kindof silly.
346 * Only guaranteed to be at splsofttty() or spltty().
347 */
348static int
349ngt_input(int c, struct tty *tp)
350{
351 const sc_p sc = (sc_p) tp->t_sc;
352 const node_p node = sc->node;
353 struct mbuf *m;
354 int s, error = 0;
355
356 if (!sc || tp != sc->tp)
357 return (0);
358 s = spltty();
359 if (!sc->hook)
360 ERROUT(0);
361
362 /* Check for error conditions */
363 if ((tp->t_state & TS_CONNECTED) == 0) {
364 if (sc->flags & FLG_DEBUG)
365 log(LOG_DEBUG, "%s: no carrier\n", node->name);
366 ERROUT(0);
367 }
368 if (c & TTY_ERRORMASK) {
369 /* framing error or overrun on this char */
370 if (sc->flags & FLG_DEBUG)
371 log(LOG_DEBUG, "%s: line error %x\n",
372 node->name, c & TTY_ERRORMASK);
373 ERROUT(0);
374 }
375 c &= TTY_CHARMASK;
376
377 /* Get a new header mbuf if we need one */
378 if (!(m = sc->m)) {
379 MGETHDR(m, M_DONTWAIT, MT_DATA);
380 if (!m) {
381 if (sc->flags & FLG_DEBUG)
382 log(LOG_ERR,
383 "%s: can't get mbuf\n", node->name);
384 ERROUT(ENOBUFS);
385 }
386 m->m_len = m->m_pkthdr.len = 0;
387 m->m_pkthdr.rcvif = NULL;
388 sc->m = m;
389 }
390
391 /* Add char to mbuf */
392 *mtod(m, u_char *) = c;
393 m->m_data++;
394 m->m_len++;
395 m->m_pkthdr.len++;
396
397 /* Ship off mbuf if it's time */
398 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
399 m->m_data = m->m_pktdat;
400 error = ng_queue_data(sc->hook, m, NULL);
401 sc->m = NULL;
402 }
403done:
404 splx(s);
405 return (error);
406}
407
408/*
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.
412 */
413static int
414ngt_start(struct tty *tp)
415{
416 const sc_p sc = (sc_p) tp->t_sc;
417 int s;
418
419 s = spltty();
420 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
421 struct mbuf *m = sc->qhead;
422
423 /* Remove first mbuf from queue */
424 if (!m)
425 break;
426 if ((sc->qhead = m->m_nextpkt) == NULL)
427 sc->qtail = &sc->qhead;
428 sc->qlen--;
429 QUEUECHECK(sc);
430
431 /* Send as much of it as possible */
432 while (m) {
433 struct mbuf *m2;
434 int sent;
435
436 sent = m->m_len
437 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
438 m->m_data += sent;
439 m->m_len -= sent;
440 if (m->m_len > 0)
441 break; /* device can't take no more */
442 MFREE(m, m2);
443 m = m2;
444 }
445
446 /* Put remainder of mbuf chain (if any) back on queue */
447 if (m) {
448 m->m_nextpkt = sc->qhead;
449 sc->qhead = m;
450 if (sc->qtail == &sc->qhead)
451 sc->qtail = &m->m_nextpkt;
452 sc->qlen++;
453 QUEUECHECK(sc);
454 break;
455 }
456 }
457
458 /* Call output process whether or not there is any output. We are
459 * being called in lieu of ttstart and must do what it would. */
460 if (tp->t_oproc != NULL)
461 (*tp->t_oproc) (tp);
462
463 /* This timeout is needed for operation on a pseudo-tty, because the
464 * pty code doesn't call pppstart after it has drained the t_outq. */
465 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
466 sc->chand = timeout(ngt_timeout, sc, 1);
467 sc->flags |= FLG_TIMEOUT;
468 }
469 splx(s);
470 return (0);
471}
472
473/*
474 * We still have data to output to the device, so try sending more.
475 */
476static void
477ngt_timeout(void *arg)
478{
479 const sc_p sc = (sc_p) arg;
480 int s;
481
482 s = spltty();
483 sc->flags &= ~FLG_TIMEOUT;
484 ngt_start(sc->tp);
485 splx(s);
486}
487
488/******************************************************************
489 NETGRAPH NODE METHODS
490******************************************************************/
491
492/*
493 * Initialize a new node of this type.
494 *
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.
497 */
498static int
499ngt_constructor(node_p *nodep)
500{
501 if (!ngt_nodeop_ok)
502 return (EOPNOTSUPP);
503 return (ng_make_node_common(&typestruct, nodep));
504}
505
506/*
507 * Add a new hook. There can only be one.
508 */
509static int
510ngt_newhook(node_p node, hook_p hook, const char *name)
511{
512 const sc_p sc = node->private;
513 int s, error = 0;
514
515 if (strcmp(name, NG_TTY_HOOK))
516 return (EINVAL);
517 s = spltty();
518 if (sc->hook)
519 ERROUT(EISCONN);
520 sc->hook = hook;
521done:
522 splx(s);
523 return (error);
524}
525
526/*
527 * Disconnect the hook
528 */
529static int
530ngt_disconnect(hook_p hook)
531{
532 const sc_p sc = hook->node->private;
533 int s;
534
535 s = spltty();
536 if (hook != sc->hook)
537 panic(__FUNCTION__);
538 sc->hook = NULL;
539 m_freem(sc->m);
540 sc->m = NULL;
541 splx(s);
542 return (0);
543}
544
545/*
546 * Remove this node. The does the netgraph portion of the shutdown.
547 * This should only be called indirectly from ngt_close().
548 */
549static int
550ngt_shutdown(node_p node)
551{
552 const sc_p sc = node->private;
553
554 if (!ngt_nodeop_ok)
555 return (EOPNOTSUPP);
556 ng_unname(node);
557 ng_cutlinks(node);
558 node->private = NULL;
559 ng_unref(sc->node);
560 m_freem(sc->qhead);
561 m_freem(sc->m);
562 bzero(sc, sizeof(*sc));
563 FREE(sc, M_NETGRAPH);
564 return (0);
565}
566
567/*
568 * Receive incoming data from netgraph system. Put it on our
569 * output queue and start output if necessary.
570 */
571static int
572ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
573 struct mbuf **ret_m, meta_p *ret_meta)
574{
575 const sc_p sc = hook->node->private;
576 int s, error = 0;
577
578 if (hook != sc->hook)
579 panic(__FUNCTION__);
580 NG_FREE_META(meta);
581 s = spltty();
582 if (sc->qlen >= MAX_MBUFQ)
583 ERROUT(ENOBUFS);
584 m->m_nextpkt = NULL;
585 *sc->qtail = m;
586 sc->qtail = &m->m_nextpkt;
587 sc->qlen++;
588 QUEUECHECK(sc);
589 m = NULL;
590 if (sc->qlen == 1)
591 ngt_start(sc->tp);
592done:
593 splx(s);
594 if (m)
595 m_freem(m);
596 return (error);
597}
598
599/*
600 * Receive control message
601 */
602static int
603ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
604 struct ng_mesg **rptr, hook_p lasthook)
605{
606 const sc_p sc = (sc_p) node->private;
607 struct ng_mesg *resp = NULL;
608 int error = 0;
609
610 switch (msg->header.typecookie) {
611 case NGM_TTY_COOKIE:
612 switch (msg->header.cmd) {
613 case NGM_TTY_SET_HOTCHAR:
614 {
615 int hotchar;
616
617 if (msg->header.arglen != sizeof(int))
618 ERROUT(EINVAL);
619 hotchar = *((int *) msg->data);
620 if (hotchar != (u_char) hotchar && hotchar != -1)
621 ERROUT(EINVAL);
622 sc->hotchar = hotchar; /* race condition is OK */
623 break;
624 }
625 case NGM_TTY_GET_HOTCHAR:
626 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
627 if (!resp)
628 ERROUT(ENOMEM);
629 /* Race condition here is OK */
630 *((int *) resp->data) = sc->hotchar;
631 break;
632 default:
633 ERROUT(EINVAL);
634 }
635 break;
636 default:
637 ERROUT(EINVAL);
638 }
639 if (rptr)
640 *rptr = resp;
641 else if (resp)
642 FREE(resp, M_NETGRAPH);
643
644done:
645 FREE(msg, M_NETGRAPH);
646 return (error);
647}
648
649/******************************************************************
650 INITIALIZATION
651******************************************************************/
652
653/*
654 * Handle loading and unloading for this node type
655 */
656static int
657ngt_mod_event(module_t mod, int event, void *data)
658{
659 /* struct ng_type *const type = data;*/
660 int s, error = 0;
661
662 switch (event) {
663 case MOD_LOAD:
663#ifdef __i386__
664 /* Insure the soft net "engine" can't run during spltty code */
665 s = splhigh();
666 tty_imask |= softnet_imask; /* spltty() block spl[soft]net() */
667 net_imask |= softtty_imask; /* splimp() block splsofttty() */
668 net_imask |= tty_imask; /* splimp() block spltty() */
669 update_intr_masks();
670 splx(s);
671
664
672 if (bootverbose)
673 log(LOG_DEBUG, "new masks: bio %x, tty %x, net %x\n",
674 bio_imask, tty_imask, net_imask);
675#endif
676
677 /* Register line discipline */
678 s = spltty();
679 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
680 splx(s);
681 log(LOG_ERR, "%s: can't register line discipline",
682 __FUNCTION__);
683 return (EIO);
684 }
685 splx(s);
686 break;
687
688 case MOD_UNLOAD:
689
690 /* Unregister line discipline */
691 s = spltty();
692 ldisc_deregister(ngt_ldisc);
693 splx(s);
694 break;
695
696 default:
697 error = EOPNOTSUPP;
698 break;
699 }
700 return (error);
701}
702
665 /* Register line discipline */
666 s = spltty();
667 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
668 splx(s);
669 log(LOG_ERR, "%s: can't register line discipline",
670 __FUNCTION__);
671 return (EIO);
672 }
673 splx(s);
674 break;
675
676 case MOD_UNLOAD:
677
678 /* Unregister line discipline */
679 s = spltty();
680 ldisc_deregister(ngt_ldisc);
681 splx(s);
682 break;
683
684 default:
685 error = EOPNOTSUPP;
686 break;
687 }
688 return (error);
689}
690