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.
22 #include "opt_compat.h"
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/filio.h>
26 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
27 #include <sys/ioctl_compat.h>
30 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/snoop.h>
36 #include <sys/vnode.h>
38 static d_open_t snpopen;
39 static d_close_t snpclose;
40 static d_read_t snpread;
41 static d_write_t snpwrite;
42 static d_ioctl_t snpioctl;
43 static d_poll_t snppoll;
46 static struct cdevsw snp_cdevsw = {
54 /* strategy */ nostrategy,
65 #define MIN(a,b) (((a)<(b))?(a):(b))
68 static struct snoop snoopsw[NSNP];
70 static struct tty *snpdevtotty __P((dev_t dev));
71 static int snp_detach __P((struct snoop *snp));
80 if (cdp && cdp->d_flags & D_TTY)
85 #define SNP_INPUT_BUF 5 /* This is even too much,the maximal
86 * interactive mode write is 3 bytes
87 * length for function keys...
91 snpwrite(dev, uio, flag)
96 int unit = minor(dev), len, i, error;
97 struct snoop *snp = &snoopsw[unit];
99 char c[SNP_INPUT_BUF];
101 if (snp->snp_tty == NULL)
106 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
107 (tp->t_line == OTTYDISC || tp->t_line == NTTYDISC))
110 printf("Snoop: attempt to write to bad tty.\n");
114 if (!(tp->t_state & TS_ISOPEN))
117 while (uio->uio_resid > 0) {
118 len = MIN(uio->uio_resid,SNP_INPUT_BUF);
119 if ((error = uiomove(c, len, uio)) != 0)
121 for (i=0;i<len;i++) {
122 if (ttyinput(c[i] , tp))
132 snpread(dev, uio, flag)
137 int unit = minor(dev), s;
138 struct snoop *snp = &snoopsw[unit];
139 int len, n, nblen, error = 0;
143 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
144 ("snoop buffer error"));
146 if (snp->snp_tty == NULL)
149 snp->snp_flags &= ~SNOOP_RWAIT;
152 if (snp->snp_len == 0) {
153 if (flag & IO_NDELAY)
154 return (EWOULDBLOCK);
155 snp->snp_flags |= SNOOP_RWAIT;
156 tsleep((caddr_t) snp, (PZERO + 1) | PCATCH, "snoopread", 0);
158 } while (snp->snp_len == 0);
162 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
163 len = MIN(uio->uio_resid, snp->snp_len);
164 from = (caddr_t) (snp->snp_buf + snp->snp_base);
168 error = uiomove(from, len, uio);
169 snp->snp_base += len;
172 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
173 snp->snp_flags &= ~SNOOP_OFLOW;
176 nblen = snp->snp_blen;
177 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
178 while (((nblen / 2) >= snp->snp_len) && ((nblen / 2) >= SNOOP_MINLEN))
180 if ((nbuf = malloc(nblen, M_TTYS, M_NOWAIT)) != NULL) {
181 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
182 free(snp->snp_buf, M_TTYS);
184 snp->snp_blen = nblen;
194 snpinc(struct snoop *snp, char c)
199 return (snpin(snp,buf,1));
214 KASSERT(n >= 0, ("negative snoop char count"));
220 if (!(snp->snp_flags & SNOOP_OPEN)) {
221 printf("Snoop: data coming to closed device.\n");
225 if (snp->snp_flags & SNOOP_DOWN) {
226 printf("Snoop: more data to down interface.\n");
230 if (snp->snp_flags & SNOOP_OFLOW) {
231 printf("Snoop: buffer overflow.\n");
233 * On overflow we just repeat the standart close
234 * procedure...yes , this is waste of space but.. Then next
235 * read from device will fail if one would recall he is
236 * snooping and retry...
239 return (snpdown(snp));
241 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
242 s_free = snp->snp_blen - snp->snp_len;
247 nblen = snp->snp_blen;
248 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
249 nblen = snp->snp_blen * 2;
250 s_free = nblen - (snp->snp_len + snp->snp_base);
252 if ((n <= s_free) && (nbuf = malloc(nblen, M_TTYS, M_NOWAIT))) {
253 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
254 free(snp->snp_buf, M_TTYS);
256 snp->snp_blen = nblen;
259 snp->snp_flags |= SNOOP_OFLOW;
260 if (snp->snp_flags & SNOOP_RWAIT) {
261 snp->snp_flags &= ~SNOOP_RWAIT;
262 wakeup((caddr_t) snp);
270 from = (caddr_t) (snp->snp_buf + snp->snp_base);
271 to = (caddr_t) (snp->snp_buf);
273 bcopy(from, to, len);
276 to = (caddr_t) (snp->snp_buf + snp->snp_base + snp->snp_len);
280 if (snp->snp_flags & SNOOP_RWAIT) {
281 snp->snp_flags &= ~SNOOP_RWAIT;
282 wakeup((caddr_t) snp);
284 selwakeup(&snp->snp_sel);
285 snp->snp_sel.si_pid = 0;
291 snpopen(dev, flag, mode, p)
297 register int unit, error;
299 if ((error = suser(p)) != 0)
302 if ((unit = minor(dev)) >= NSNP)
305 snp = &snoopsw[unit];
307 if (snp->snp_flags & SNOOP_OPEN)
311 * We intentionally do not OR flags with SNOOP_OPEN,but set them so
312 * all previous settings (especially SNOOP_OFLOW) will be cleared.
314 snp->snp_flags = SNOOP_OPEN;
316 snp->snp_buf = malloc(SNOOP_MINLEN, M_TTYS, M_WAITOK);
317 snp->snp_blen = SNOOP_MINLEN;
322 * snp_tty == NULL is for inactive snoop devices.
325 snp->snp_target = NODEV;
340 * If line disc. changed we do not touch this pointer,SLIP/PPP will
344 if (snp->snp_tty == NULL)
349 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
350 (tp->t_line == OTTYDISC || tp->t_line == NTTYDISC)) {
352 tp->t_state &= ~TS_SNOOP;
354 printf("Snoop: bad attached tty data.\n");
357 snp->snp_target = NODEV;
360 selwakeup(&snp->snp_sel);
361 snp->snp_sel.si_pid = 0;
367 snpclose(dev, flags, fmt, p)
373 register int unit = minor(dev);
374 struct snoop *snp = &snoopsw[unit];
377 free(snp->snp_buf, M_TTYS);
378 snp->snp_flags &= ~SNOOP_OPEN;
380 return (snp_detach(snp));
387 snp->snp_blen = SNOOP_MINLEN;
388 free(snp->snp_buf, M_TTYS);
389 snp->snp_buf = malloc(SNOOP_MINLEN, M_TTYS, M_WAITOK);
390 snp->snp_flags |= SNOOP_DOWN;
392 return (snp_detach(snp));
397 snpioctl(dev, cmd, data, flags, p)
404 int unit = minor(dev), s;
406 struct snoop *snp = &snoopsw[unit];
407 struct tty *tp, *tpo;
411 tdev = udev2dev(*((udev_t *) data), 0);
413 return (snpdown(snp));
415 tp = snpdevtotty(tdev);
419 if ((tp->t_sc != (caddr_t) snp) && (tp->t_state & TS_SNOOP))
422 if ((tp->t_line != OTTYDISC) && (tp->t_line != NTTYDISC))
427 if (snp->snp_target == NODEV) {
430 tpo->t_state &= ~TS_SNOOP;
433 tp->t_sc = (caddr_t) snp;
434 tp->t_state |= TS_SNOOP;
436 snp->snp_target = tdev;
439 * Clean overflow and down flags -
440 * we'll have a chance to get them in the future :)))
442 snp->snp_flags &= ~SNOOP_OFLOW;
443 snp->snp_flags &= ~SNOOP_DOWN;
449 * We keep snp_target field specially to make
450 * SNPGTTY happy,else we can't know what is device
451 * major/minor for tty.
453 *((dev_t *) data) = snp->snp_target;
461 snp->snp_flags |= SNOOP_ASYNC;
463 snp->snp_flags &= ~SNOOP_ASYNC;
468 if (snp->snp_tty != NULL)
469 *(int *) data = snp->snp_len;
471 if (snp->snp_flags & SNOOP_DOWN) {
472 if (snp->snp_flags & SNOOP_OFLOW)
473 *(int *) data = SNP_OFLOW;
475 *(int *) data = SNP_TTYCLOSE;
477 *(int *) data = SNP_DETACH;
490 snppoll(dev, events, p)
495 int unit = minor(dev);
496 struct snoop *snp = &snoopsw[unit];
501 * If snoop is down,we don't want to poll() forever so we return 1.
502 * Caller should see if we down via FIONREAD ioctl().The last should
503 * return -1 to indicate down state.
505 if (events & (POLLIN | POLLRDNORM)) {
506 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
507 revents |= events & (POLLIN | POLLRDNORM);
509 selrecord(p, &snp->snp_sel);
514 static void snp_drvinit __P((void *unused));
522 cdevsw_add(&snp_cdevsw);
523 for ( i = 0 ; i < NSNP ; i++)
524 make_dev(&snp_cdevsw, i, 0, 0, 0600, "snp%d", i);
527 SYSINIT(snpdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,snp_drvinit,NULL)