2 * Copyright (c) 1995 Ugen J.S.Antsilevich
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
7 * Redistribution in binary form may occur without any restrictions.
8 * Obviously, it would be nice if you gave credit where credit is due
9 * but requiring it would be too onerous.
11 * This software is provided ``AS IS'' without any warranties of any kind.
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/filio.h>
21 #include <sys/malloc.h>
25 #include <sys/kernel.h>
26 #include <sys/queue.h>
27 #include <sys/snoop.h>
28 #include <sys/vnode.h>
30 static l_close_t snplclose;
31 static l_write_t snplwrite;
32 static d_open_t snpopen;
33 static d_close_t snpclose;
34 static d_read_t snpread;
35 static d_write_t snpwrite;
36 static d_ioctl_t snpioctl;
37 static d_poll_t snppoll;
40 static struct cdevsw snp_cdevsw = {
48 /* strategy */ nostrategy,
56 static struct linesw snpdisc = {
57 ttyopen, snplclose, ttread, snplwrite,
58 l_nullioctl, ttyinput, ttstart, ttymodem
62 * This is the main snoop per-device structure.
65 LIST_ENTRY(snoop) snp_list; /* List glue. */
66 dev_t snp_target; /* Target tty device. */
67 struct tty *snp_tty; /* Target tty pointer. */
68 u_long snp_len; /* Possible length. */
69 u_long snp_base; /* Data base. */
70 u_long snp_blen; /* Used length. */
71 caddr_t snp_buf; /* Allocation pointer. */
72 int snp_flags; /* Flags. */
73 struct selinfo snp_sel; /* Select info. */
74 int snp_olddisc; /* Old line discipline. */
80 #define SNOOP_ASYNC 0x0002
81 #define SNOOP_OPEN 0x0004
82 #define SNOOP_RWAIT 0x0008
83 #define SNOOP_OFLOW 0x0010
84 #define SNOOP_DOWN 0x0020
89 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
90 * 4K tested to be the minimum
91 * for which on normal tty
92 * usage there is no need to
95 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
96 * If we grow more,something
97 * really bad in this world..
100 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
102 * The number of the "snoop" line discipline. This gets determined at
105 static int snooplinedisc;
108 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
110 static struct tty *snpdevtotty __P((dev_t dev));
111 static void snp_clone __P((void *arg, char *name,
112 int namelen, dev_t *dev));
113 static int snp_detach __P((struct snoop *snp));
114 static int snp_down __P((struct snoop *snp));
115 static int snp_in __P((struct snoop *snp, char *buf, int n));
116 static int snp_modevent __P((module_t mod, int what, void *arg));
127 error = snp_down(snp);
130 error = ttylclose(tp, flag);
135 snplwrite(tp, uio, flag)
149 while (uio->uio_resid > 0) {
150 ilen = imin(512, uio->uio_resid);
151 ibuf = malloc(ilen, M_SNP, M_WAITOK);
152 error = uiomove(ibuf, ilen, uio);
155 snp_in(snp, ibuf, ilen);
156 /* Hackish, but probably the least of all evils. */
162 uio2.uio_resid = ilen;
163 uio2.uio_segflg = UIO_SYSSPACE;
164 uio2.uio_rw = UIO_WRITE;
165 uio2.uio_td = uio->uio_td;
166 error = ttwrite(tp, &uio2, flag);
184 if (cdp == NULL || (cdp->d_flags & D_TTY) == 0)
186 return (dev->si_tty);
189 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
190 * interactive mode write is 3 bytes
191 * length for function keys...
195 snpwrite(dev, uio, flag)
203 char c[SNP_INPUT_BUF];
209 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
210 tp->t_line == snooplinedisc)
213 printf("Snoop: attempt to write to bad tty.\n");
217 if (!(tp->t_state & TS_ISOPEN))
220 while (uio->uio_resid > 0) {
221 len = imin(uio->uio_resid, SNP_INPUT_BUF);
222 if ((error = uiomove(c, len, uio)) != 0)
224 for (i=0; i < len; i++) {
225 if (ttyinput(c[i], tp))
234 snpread(dev, uio, flag)
240 int error, len, n, nblen, s;
245 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
246 ("snoop buffer error"));
248 if (snp->snp_tty == NULL)
251 snp->snp_flags &= ~SNOOP_RWAIT;
254 if (snp->snp_len == 0) {
255 if (flag & IO_NDELAY)
256 return (EWOULDBLOCK);
257 snp->snp_flags |= SNOOP_RWAIT;
258 tsleep((caddr_t)snp, (PZERO + 1) | PCATCH, "snprd", 0);
260 } while (snp->snp_len == 0);
265 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
266 len = min((unsigned)uio->uio_resid, snp->snp_len);
267 from = (caddr_t)(snp->snp_buf + snp->snp_base);
271 error = uiomove(from, len, uio);
272 snp->snp_base += len;
275 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
276 snp->snp_flags &= ~SNOOP_OFLOW;
279 nblen = snp->snp_blen;
280 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
281 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
283 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
284 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
285 free(snp->snp_buf, M_SNP);
287 snp->snp_blen = nblen;
307 KASSERT(n >= 0, ("negative snoop char count"));
312 if (snp->snp_flags & SNOOP_DOWN) {
313 printf("Snoop: more data to down interface.\n");
317 if (snp->snp_flags & SNOOP_OFLOW) {
318 printf("Snoop: buffer overflow.\n");
320 * On overflow we just repeat the standart close
321 * procedure...yes , this is waste of space but.. Then next
322 * read from device will fail if one would recall he is
323 * snooping and retry...
326 return (snp_down(snp));
328 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
329 s_free = snp->snp_blen - snp->snp_len;
334 nblen = snp->snp_blen;
335 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
336 nblen = snp->snp_blen * 2;
337 s_free = nblen - (snp->snp_len + snp->snp_base);
339 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
340 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
341 free(snp->snp_buf, M_SNP);
343 snp->snp_blen = nblen;
346 snp->snp_flags |= SNOOP_OFLOW;
347 if (snp->snp_flags & SNOOP_RWAIT) {
348 snp->snp_flags &= ~SNOOP_RWAIT;
349 wakeup((caddr_t)snp);
357 from = (caddr_t)(snp->snp_buf + snp->snp_base);
358 to = (caddr_t)(snp->snp_buf);
360 bcopy(from, to, len);
363 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
367 if (snp->snp_flags & SNOOP_RWAIT) {
368 snp->snp_flags &= ~SNOOP_RWAIT;
369 wakeup((caddr_t)snp);
371 selwakeup(&snp->snp_sel);
372 snp->snp_sel.si_pid = 0;
378 snpopen(dev, flag, mode, td)
386 if ((error = suser_td(td)) != 0)
389 if (dev->si_drv1 == NULL) {
390 if (!(dev->si_flags & SI_NAMED))
391 make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
392 0600, "snp%d", dev2unit(dev));
393 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
399 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
400 * all previous settings (especially SNOOP_OFLOW) will be cleared.
402 snp->snp_flags = SNOOP_OPEN;
404 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
405 snp->snp_blen = SNOOP_MINLEN;
410 * snp_tty == NULL is for inactive snoop devices.
413 snp->snp_target = NODEV;
415 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
430 * If line disc. changed we do not touch this pointer, SLIP/PPP will
437 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
438 tp->t_line == snooplinedisc) {
440 tp->t_state &= ~TS_SNOOP;
441 tp->t_line = snp->snp_olddisc;
443 printf("Snoop: bad attached tty data.\n");
446 snp->snp_target = NODEV;
449 selwakeup(&snp->snp_sel);
450 snp->snp_sel.si_pid = 0;
451 if ((snp->snp_flags & SNOOP_OPEN) == 0)
458 snpclose(dev, flags, fmt, td)
468 LIST_REMOVE(snp, snp_list);
469 free(snp->snp_buf, M_SNP);
470 snp->snp_flags &= ~SNOOP_OPEN;
474 return (snp_detach(snp));
482 if (snp->snp_blen != SNOOP_MINLEN) {
483 free(snp->snp_buf, M_SNP);
484 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
485 snp->snp_blen = SNOOP_MINLEN;
487 snp->snp_flags |= SNOOP_DOWN;
489 return (snp_detach(snp));
493 snpioctl(dev, cmd, data, flags, td)
501 struct tty *tp, *tpo;
508 tdev = udev2dev(*((udev_t *)data), 0);
510 return (snp_down(snp));
512 tp = snpdevtotty(tdev);
518 if (snp->snp_target == NODEV) {
521 tpo->t_state &= ~TS_SNOOP;
524 tp->t_sc = (caddr_t)snp;
525 tp->t_state |= TS_SNOOP;
526 snp->snp_olddisc = tp->t_line;
527 tp->t_line = snooplinedisc;
529 snp->snp_target = tdev;
532 * Clean overflow and down flags -
533 * we'll have a chance to get them in the future :)))
535 snp->snp_flags &= ~SNOOP_OFLOW;
536 snp->snp_flags &= ~SNOOP_DOWN;
542 * We keep snp_target field specially to make
543 * SNPGTTY happy, else we can't know what is device
544 * major/minor for tty.
546 *((dev_t *)data) = snp->snp_target;
554 snp->snp_flags |= SNOOP_ASYNC;
556 snp->snp_flags &= ~SNOOP_ASYNC;
561 if (snp->snp_tty != NULL)
562 *(int *)data = snp->snp_len;
564 if (snp->snp_flags & SNOOP_DOWN) {
565 if (snp->snp_flags & SNOOP_OFLOW)
566 *(int *)data = SNP_OFLOW;
568 *(int *)data = SNP_TTYCLOSE;
570 *(int *)data = SNP_DETACH;
582 snppoll(dev, events, td)
593 * If snoop is down, we don't want to poll() forever so we return 1.
594 * Caller should see if we down via FIONREAD ioctl(). The last should
595 * return -1 to indicate down state.
597 if (events & (POLLIN | POLLRDNORM)) {
598 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
599 revents |= events & (POLLIN | POLLRDNORM);
601 selrecord(td, &snp->snp_sel);
607 snp_clone(arg, name, namelen, dev)
617 if (dev_stdclone(name, NULL, "snp", &u) != 1)
619 *dev = make_dev(&snp_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
624 snp_modevent(mod, type, data)
629 static eventhandler_tag eh_tag;
633 /* XXX error checking. */
634 eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000);
635 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
636 cdevsw_add(&snp_cdevsw);
639 if (!LIST_EMPTY(&snp_sclist))
641 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
642 ldisc_deregister(snooplinedisc);
643 cdevsw_remove(&snp_cdevsw);
651 static moduledata_t snp_mod = {
656 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);