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.
15 * $FreeBSD: src/sys/dev/snp/snp.c,v 1.69.2.2 2002/05/06 07:30:02 dd Exp $
16 * $DragonFly: src/sys/dev/misc/snp/snp.c,v 1.2 2003/06/17 04:28:30 dillon Exp $
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/filio.h>
22 #include <sys/malloc.h>
26 #include <sys/kernel.h>
27 #include <sys/queue.h>
28 #include <sys/snoop.h>
29 #include <sys/vnode.h>
31 static l_close_t snplclose;
32 static l_write_t snplwrite;
33 static d_open_t snpopen;
34 static d_close_t snpclose;
35 static d_read_t snpread;
36 static d_write_t snpwrite;
37 static d_ioctl_t snpioctl;
38 static d_poll_t snppoll;
41 static struct cdevsw snp_cdevsw = {
49 /* strategy */ nostrategy,
58 static struct linesw snpdisc = {
59 ttyopen, snplclose, ttread, snplwrite,
60 l_nullioctl, ttyinput, ttstart, ttymodem
64 * This is the main snoop per-device structure.
67 LIST_ENTRY(snoop) snp_list; /* List glue. */
68 dev_t snp_target; /* Target tty device. */
69 struct tty *snp_tty; /* Target tty pointer. */
70 u_long snp_len; /* Possible length. */
71 u_long snp_base; /* Data base. */
72 u_long snp_blen; /* Used length. */
73 caddr_t snp_buf; /* Allocation pointer. */
74 int snp_flags; /* Flags. */
75 struct selinfo snp_sel; /* Select info. */
76 int snp_olddisc; /* Old line discipline. */
82 #define SNOOP_ASYNC 0x0002
83 #define SNOOP_OPEN 0x0004
84 #define SNOOP_RWAIT 0x0008
85 #define SNOOP_OFLOW 0x0010
86 #define SNOOP_DOWN 0x0020
91 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
92 * 4K tested to be the minimum
93 * for which on normal tty
94 * usage there is no need to
97 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
98 * If we grow more,something
99 * really bad in this world..
102 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
104 * The number of the "snoop" line discipline. This gets determined at
107 static int snooplinedisc;
110 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
112 static struct tty *snpdevtotty __P((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_procp = uio->uio_procp;
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 unsigned 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 error = tsleep((caddr_t)snp, (PZERO + 1) | PCATCH,
263 } while (snp->snp_len == 0);
268 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
269 len = min((unsigned)uio->uio_resid, snp->snp_len);
270 from = (caddr_t)(snp->snp_buf + snp->snp_base);
274 error = uiomove(from, len, uio);
275 snp->snp_base += len;
278 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
279 snp->snp_flags &= ~SNOOP_OFLOW;
282 nblen = snp->snp_blen;
283 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
284 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
286 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
287 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
288 free(snp->snp_buf, M_SNP);
290 snp->snp_blen = nblen;
310 KASSERT(n >= 0, ("negative snoop char count"));
315 if (snp->snp_flags & SNOOP_DOWN) {
316 printf("Snoop: more data to down interface.\n");
320 if (snp->snp_flags & SNOOP_OFLOW) {
321 printf("Snoop: buffer overflow.\n");
323 * On overflow we just repeat the standart close
324 * procedure...yes , this is waste of space but.. Then next
325 * read from device will fail if one would recall he is
326 * snooping and retry...
329 return (snp_down(snp));
331 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
332 s_free = snp->snp_blen - snp->snp_len;
337 nblen = snp->snp_blen;
338 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
339 nblen = snp->snp_blen * 2;
340 s_free = nblen - (snp->snp_len + snp->snp_base);
342 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
343 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
344 free(snp->snp_buf, M_SNP);
346 snp->snp_blen = nblen;
349 snp->snp_flags |= SNOOP_OFLOW;
350 if (snp->snp_flags & SNOOP_RWAIT) {
351 snp->snp_flags &= ~SNOOP_RWAIT;
352 wakeup((caddr_t)snp);
360 from = (caddr_t)(snp->snp_buf + snp->snp_base);
361 to = (caddr_t)(snp->snp_buf);
363 bcopy(from, to, len);
366 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
370 if (snp->snp_flags & SNOOP_RWAIT) {
371 snp->snp_flags &= ~SNOOP_RWAIT;
372 wakeup((caddr_t)snp);
374 selwakeup(&snp->snp_sel);
375 snp->snp_sel.si_pid = 0;
381 snpopen(dev, flag, mode, p)
388 if (dev->si_drv1 == NULL) {
389 make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
390 0600, "snp%d", minor(dev));
391 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
397 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
398 * all previous settings (especially SNOOP_OFLOW) will be cleared.
400 snp->snp_flags = SNOOP_OPEN;
402 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
403 snp->snp_blen = SNOOP_MINLEN;
408 * snp_tty == NULL is for inactive snoop devices.
411 snp->snp_target = NODEV;
413 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
428 * If line disc. changed we do not touch this pointer, SLIP/PPP will
435 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
436 tp->t_line == snooplinedisc) {
438 tp->t_state &= ~TS_SNOOP;
439 tp->t_line = snp->snp_olddisc;
441 printf("Snoop: bad attached tty data.\n");
444 snp->snp_target = NODEV;
447 selwakeup(&snp->snp_sel);
448 snp->snp_sel.si_pid = 0;
449 if ((snp->snp_flags & SNOOP_OPEN) == 0)
456 snpclose(dev, flags, fmt, p)
466 LIST_REMOVE(snp, snp_list);
467 free(snp->snp_buf, M_SNP);
468 snp->snp_flags &= ~SNOOP_OPEN;
471 return (snp_detach(snp));
479 if (snp->snp_blen != SNOOP_MINLEN) {
480 free(snp->snp_buf, M_SNP);
481 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
482 snp->snp_blen = SNOOP_MINLEN;
484 snp->snp_flags |= SNOOP_DOWN;
486 return (snp_detach(snp));
490 snpioctl(dev, cmd, data, flags, p)
498 struct tty *tp, *tpo;
505 tdev = udev2dev(*((udev_t *)data), 0);
507 return (snp_down(snp));
509 tp = snpdevtotty(tdev);
512 if (tp->t_state & TS_SNOOP)
517 if (snp->snp_target == NODEV) {
520 tpo->t_state &= ~TS_SNOOP;
523 tp->t_sc = (caddr_t)snp;
524 tp->t_state |= TS_SNOOP;
525 snp->snp_olddisc = tp->t_line;
526 tp->t_line = snooplinedisc;
528 snp->snp_target = tdev;
531 * Clean overflow and down flags -
532 * we'll have a chance to get them in the future :)))
534 snp->snp_flags &= ~SNOOP_OFLOW;
535 snp->snp_flags &= ~SNOOP_DOWN;
541 * We keep snp_target field specially to make
542 * SNPGTTY happy, else we can't know what is device
543 * major/minor for tty.
545 *((dev_t *)data) = snp->snp_target;
553 snp->snp_flags |= SNOOP_ASYNC;
555 snp->snp_flags &= ~SNOOP_ASYNC;
560 if (snp->snp_tty != NULL)
561 *(int *)data = snp->snp_len;
563 if (snp->snp_flags & SNOOP_DOWN) {
564 if (snp->snp_flags & SNOOP_OFLOW)
565 *(int *)data = SNP_OFLOW;
567 *(int *)data = SNP_TTYCLOSE;
569 *(int *)data = SNP_DETACH;
581 snppoll(dev, events, p)
592 * If snoop is down, we don't want to poll() forever so we return 1.
593 * Caller should see if we down via FIONREAD ioctl(). The last should
594 * return -1 to indicate down state.
596 if (events & (POLLIN | POLLRDNORM)) {
597 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
598 revents |= events & (POLLIN | POLLRDNORM);
600 selrecord(p, &snp->snp_sel);
606 snp_modevent(mod, type, data)
614 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
615 cdevsw_add(&snp_cdevsw);
618 if (!LIST_EMPTY(&snp_sclist))
620 ldisc_deregister(snooplinedisc);
621 cdevsw_remove(&snp_cdevsw);
629 static moduledata_t snp_mod = {
634 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);