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