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.
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/fcntl.h>
23 #include <sys/filio.h>
24 #include <sys/malloc.h>
28 #include <sys/kernel.h>
29 #include <sys/module.h>
30 #include <sys/queue.h>
31 #include <sys/snoop.h>
34 static l_close_t snplclose;
35 static l_write_t snplwrite;
36 static d_open_t snpopen;
37 static d_close_t snpclose;
38 static d_read_t snpread;
39 static d_write_t snpwrite;
40 static d_ioctl_t snpioctl;
41 static d_poll_t snppoll;
43 static struct cdevsw snp_cdevsw = {
44 .d_version = D_VERSION,
45 .d_flags = D_PSEUDO | D_NEEDGIANT,
55 static struct linesw snpdisc = {
60 .l_ioctl = l_nullioctl,
67 * This is the main snoop per-device structure.
70 LIST_ENTRY(snoop) snp_list; /* List glue. */
71 int snp_unit; /* Device number. */
72 struct cdev *snp_target; /* Target tty device. */
73 struct tty *snp_tty; /* Target tty pointer. */
74 u_long snp_len; /* Possible length. */
75 u_long snp_base; /* Data base. */
76 u_long snp_blen; /* Used length. */
77 caddr_t snp_buf; /* Allocation pointer. */
78 int snp_flags; /* Flags. */
79 struct selinfo snp_sel; /* Select info. */
80 int snp_olddisc; /* Old line discipline. */
86 #define SNOOP_ASYNC 0x0002
87 #define SNOOP_OPEN 0x0004
88 #define SNOOP_RWAIT 0x0008
89 #define SNOOP_OFLOW 0x0010
90 #define SNOOP_DOWN 0x0020
95 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
96 * 4K tested to be the minimum
97 * for which on normal tty
98 * usage there is no need to
101 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
102 * If we grow more,something
103 * really bad in this world..
106 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
108 * The number of the "snoop" line discipline. This gets determined at
111 static int snooplinedisc;
113 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
114 static struct clonedevs *snpclones;
116 static struct tty *snpdevtotty(struct cdev *dev);
117 static void snp_clone(void *arg, char *name,
118 int namelen, struct cdev **dev);
119 static int snp_detach(struct snoop *snp);
120 static int snp_down(struct snoop *snp);
121 static int snp_in(struct snoop *snp, char *buf, int n);
122 static int snp_modevent(module_t mod, int what, void *arg);
123 static struct snoop *ttytosnp(struct tty *);
125 static struct snoop *
126 ttytosnp(struct tty *tp)
130 LIST_FOREACH(snp, &snp_sclist, snp_list) {
131 if (snp->snp_tty == tp)
146 error = snp_down(snp);
149 error = ttylclose(tp, flag);
154 snplwrite(tp, uio, flag)
168 while (uio->uio_resid > 0) {
169 ilen = imin(512, uio->uio_resid);
170 ibuf = malloc(ilen, M_SNP, M_WAITOK);
171 error = uiomove(ibuf, ilen, uio);
174 snp_in(snp, ibuf, ilen);
175 /* Hackish, but probably the least of all evils. */
181 uio2.uio_resid = ilen;
182 uio2.uio_segflg = UIO_SYSSPACE;
183 uio2.uio_rw = UIO_WRITE;
184 uio2.uio_td = uio->uio_td;
185 error = ttwrite(tp, &uio2, flag);
203 cdp = dev_refthread(dev);
206 if (!(cdp->d_flags & D_TTY))
214 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
215 * interactive mode write is 3 bytes
216 * length for function keys...
220 snpwrite(dev, uio, flag)
228 unsigned char c[SNP_INPUT_BUF];
234 if ((tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc)
237 printf("snp%d: attempt to write to bad tty\n", snp->snp_unit);
241 if (!(tp->t_state & TS_ISOPEN))
244 while (uio->uio_resid > 0) {
245 len = imin(uio->uio_resid, SNP_INPUT_BUF);
246 if ((error = uiomove(c, len, uio)) != 0)
248 for (i=0; i < len; i++) {
249 if (ttyinput(c[i], tp))
258 snpread(dev, uio, flag)
264 int error, len, n, nblen, s;
269 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
270 ("snoop buffer error"));
272 if (snp->snp_tty == NULL)
275 snp->snp_flags &= ~SNOOP_RWAIT;
278 if (snp->snp_len == 0) {
279 if (flag & O_NONBLOCK)
280 return (EWOULDBLOCK);
281 snp->snp_flags |= SNOOP_RWAIT;
282 error = tsleep(snp, (PZERO + 1) | PCATCH,
287 } while (snp->snp_len == 0);
292 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
293 len = min((unsigned)uio->uio_resid, snp->snp_len);
294 from = (caddr_t)(snp->snp_buf + snp->snp_base);
298 error = uiomove(from, len, uio);
299 snp->snp_base += len;
302 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
303 snp->snp_flags &= ~SNOOP_OFLOW;
306 nblen = snp->snp_blen;
307 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
308 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
310 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
311 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
312 free(snp->snp_buf, M_SNP);
314 snp->snp_blen = nblen;
334 KASSERT(n >= 0, ("negative snoop char count"));
339 if (snp->snp_flags & SNOOP_DOWN) {
340 printf("snp%d: more data to down interface\n", snp->snp_unit);
344 if (snp->snp_flags & SNOOP_OFLOW) {
345 printf("snp%d: buffer overflow\n", snp->snp_unit);
347 * On overflow we just repeat the standart close
348 * procedure...yes , this is waste of space but.. Then next
349 * read from device will fail if one would recall he is
350 * snooping and retry...
353 return (snp_down(snp));
355 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
356 s_free = snp->snp_blen - snp->snp_len;
361 nblen = snp->snp_blen;
362 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
363 nblen = snp->snp_blen * 2;
364 s_free = nblen - (snp->snp_len + snp->snp_base);
366 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
367 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
368 free(snp->snp_buf, M_SNP);
370 snp->snp_blen = nblen;
373 snp->snp_flags |= SNOOP_OFLOW;
374 if (snp->snp_flags & SNOOP_RWAIT) {
375 snp->snp_flags &= ~SNOOP_RWAIT;
384 from = (caddr_t)(snp->snp_buf + snp->snp_base);
385 to = (caddr_t)(snp->snp_buf);
387 bcopy(from, to, len);
390 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
394 if (snp->snp_flags & SNOOP_RWAIT) {
395 snp->snp_flags &= ~SNOOP_RWAIT;
398 selwakeuppri(&snp->snp_sel, PZERO + 1);
404 snpopen(dev, flag, mode, td)
411 if (dev->si_drv1 == NULL) {
412 dev->si_flags &= ~SI_CHEAPCLONE;
413 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
415 snp->snp_unit = dev2unit(dev);
420 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
421 * all previous settings (especially SNOOP_OFLOW) will be cleared.
423 snp->snp_flags = SNOOP_OPEN;
425 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
426 snp->snp_blen = SNOOP_MINLEN;
431 * snp_tty == NULL is for inactive snoop devices.
434 snp->snp_target = NULL;
436 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
451 * If line disc. changed we do not touch this pointer, SLIP/PPP will
458 if ((tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc) {
459 tp->t_state &= ~TS_SNOOP;
460 tp->t_line = snp->snp_olddisc;
462 printf("snp%d: bad attached tty data\n", snp->snp_unit);
465 snp->snp_target = NULL;
468 selwakeuppri(&snp->snp_sel, PZERO + 1);
469 if ((snp->snp_flags & SNOOP_OPEN) == 0)
476 snpclose(dev, flags, fmt, td)
486 LIST_REMOVE(snp, snp_list);
487 free(snp->snp_buf, M_SNP);
488 snp->snp_flags &= ~SNOOP_OPEN;
492 return (snp_detach(snp));
500 if (snp->snp_blen != SNOOP_MINLEN) {
501 free(snp->snp_buf, M_SNP);
502 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
503 snp->snp_blen = SNOOP_MINLEN;
505 snp->snp_flags |= SNOOP_DOWN;
507 return (snp_detach(snp));
511 snpioctl(dev, cmd, data, flags, td)
519 struct tty *tp, *tpo;
527 tdev = findcdev(*((dev_t *)data));
532 return (snp_down(snp));
534 tp = snpdevtotty(tdev);
537 if (tp->t_state & TS_SNOOP)
542 if (snp->snp_target == NULL) {
545 tpo->t_state &= ~TS_SNOOP;
548 tp->t_state |= TS_SNOOP;
549 snp->snp_olddisc = tp->t_line;
550 tp->t_line = snooplinedisc;
552 snp->snp_target = tdev;
555 * Clean overflow and down flags -
556 * we'll have a chance to get them in the future :)))
558 snp->snp_flags &= ~SNOOP_OFLOW;
559 snp->snp_flags &= ~SNOOP_DOWN;
565 * We keep snp_target field specially to make
566 * SNPGTTY happy, else we can't know what is device
567 * major/minor for tty.
569 *((dev_t *)data) = dev2udev(snp->snp_target);
577 snp->snp_flags |= SNOOP_ASYNC;
579 snp->snp_flags &= ~SNOOP_ASYNC;
584 if (snp->snp_tty != NULL)
585 *(int *)data = snp->snp_len;
587 if (snp->snp_flags & SNOOP_DOWN) {
588 if (snp->snp_flags & SNOOP_OFLOW)
589 *(int *)data = SNP_OFLOW;
591 *(int *)data = SNP_TTYCLOSE;
593 *(int *)data = SNP_DETACH;
605 snppoll(dev, events, td)
616 * If snoop is down, we don't want to poll() forever so we return 1.
617 * Caller should see if we down via FIONREAD ioctl(). The last should
618 * return -1 to indicate down state.
620 if (events & (POLLIN | POLLRDNORM)) {
621 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
622 revents |= events & (POLLIN | POLLRDNORM);
624 selrecord(td, &snp->snp_sel);
630 snp_clone(arg, name, namelen, dev)
640 if (dev_stdclone(name, NULL, "snp", &u) != 1)
642 i = clone_create(&snpclones, &snp_cdevsw, &u, dev, 0);
644 *dev = make_dev(&snp_cdevsw, unit2minor(u),
645 UID_ROOT, GID_WHEEL, 0600, "snp%d", u);
648 (*dev)->si_flags |= SI_CHEAPCLONE;
653 snp_modevent(mod, type, data)
658 static eventhandler_tag eh_tag;
662 /* XXX error checking. */
663 clone_setup(&snpclones);
664 eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000);
665 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
668 if (!LIST_EMPTY(&snp_sclist))
670 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
671 clone_cleanup(&snpclones);
672 ldisc_deregister(snooplinedisc);
681 static moduledata_t snp_mod = {
686 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);