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.19 2007/05/08 02:31:41 dillon Exp $
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/filio.h>
23 #include <sys/malloc.h>
26 #include <sys/event.h>
27 #include <sys/kernel.h>
28 #include <sys/queue.h>
29 #include <sys/snoop.h>
30 #include <sys/thread2.h>
31 #include <sys/vnode.h>
32 #include <sys/device.h>
33 #include <sys/devfs.h>
35 static l_close_t snplclose;
36 static l_write_t snplwrite;
37 static d_open_t snpopen;
38 static d_close_t snpclose;
39 static d_read_t snpread;
40 static d_write_t snpwrite;
41 static d_ioctl_t snpioctl;
42 static d_kqfilter_t snpkqfilter;
43 static d_clone_t snpclone;
44 DEVFS_DECLARE_CLONE_BITMAP(snp);
46 static void snpfilter_detach(struct knote *);
47 static int snpfilter_rd(struct knote *, long);
48 static int snpfilter_wr(struct knote *, long);
51 #define SNP_PREALLOCATED_UNITS 4
53 #define SNP_PREALLOCATED_UNITS NSNP
57 static struct dev_ops snp_ops = {
58 { "snp", CDEV_MAJOR, D_KQFILTER },
64 .d_kqfilter = snpkqfilter
67 static struct linesw snpdisc = {
68 ttyopen, snplclose, ttread, snplwrite,
69 l_nullioctl, ttyinput, ttstart, ttymodem
73 * This is the main snoop per-device structure.
76 LIST_ENTRY(snoop) snp_list; /* List glue. */
77 cdev_t snp_target; /* Target tty device. */
78 struct tty *snp_tty; /* Target tty pointer. */
79 u_long snp_len; /* Possible length. */
80 u_long snp_base; /* Data base. */
81 u_long snp_blen; /* Used length. */
82 caddr_t snp_buf; /* Allocation pointer. */
83 int snp_flags; /* Flags. */
84 struct kqinfo snp_kq; /* Kqueue info. */
85 int snp_olddisc; /* Old line discipline. */
91 #define SNOOP_ASYNC 0x0002
92 #define SNOOP_OPEN 0x0004
93 #define SNOOP_RWAIT 0x0008
94 #define SNOOP_OFLOW 0x0010
95 #define SNOOP_DOWN 0x0020
100 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
101 * 4K tested to be the minimum
102 * for which on normal tty
103 * usage there is no need to
106 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
107 * If we grow more,something
108 * really bad in this world..
111 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
113 * The number of the "snoop" line discipline. This gets determined at
116 static int snooplinedisc;
119 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
121 static struct tty *snpdevtotty (cdev_t dev);
122 static int snp_detach (struct snoop *snp);
123 static int snp_down (struct snoop *snp);
124 static int snp_in (struct snoop *snp, char *buf, int n);
125 static int snp_modevent (module_t mod, int what, void *arg);
128 snplclose(struct tty *tp, int flag)
134 error = snp_down(snp);
137 error = ttylclose(tp, flag);
142 snplwrite(struct tty *tp, struct uio *uio, int flag)
153 while (uio->uio_resid > 0) {
154 ilen = (int)szmin(512, uio->uio_resid);
155 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
156 error = uiomove(ibuf, (size_t)ilen, uio);
159 snp_in(snp, ibuf, ilen);
160 /* Hackish, but probably the least of all evils. */
166 uio2.uio_resid = ilen;
167 uio2.uio_segflg = UIO_SYSSPACE;
168 uio2.uio_rw = UIO_WRITE;
169 uio2.uio_td = uio->uio_td;
170 error = ttwrite(tp, &uio2, flag);
182 snpdevtotty(cdev_t dev)
184 if ((dev_dflags(dev) & 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(struct dev_write_args *ap)
197 cdev_t dev = ap->a_head.a_dev;
198 struct uio *uio = ap->a_uio;
202 unsigned char c[SNP_INPUT_BUF];
208 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
209 tp->t_line == snooplinedisc)
212 kprintf("Snoop: attempt to write to bad tty.\n");
216 if (!(tp->t_state & TS_ISOPEN))
219 while (uio->uio_resid > 0) {
220 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
221 if ((error = uiomove(c, (size_t)len, uio)) != 0)
223 for (i=0; i < len; i++) {
224 if (ttyinput(c[i], tp))
233 snpread(struct dev_read_args *ap)
235 cdev_t dev = ap->a_head.a_dev;
236 struct uio *uio = ap->a_uio;
238 int error, len, n, nblen;
243 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
244 ("snoop buffer error"));
246 if (snp->snp_tty == NULL)
249 snp->snp_flags &= ~SNOOP_RWAIT;
252 if (snp->snp_len == 0) {
253 if (ap->a_ioflag & IO_NDELAY)
254 return (EWOULDBLOCK);
255 snp->snp_flags |= SNOOP_RWAIT;
256 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
260 } while (snp->snp_len == 0);
265 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
266 len = (int)szmin(uio->uio_resid, snp->snp_len);
267 from = (caddr_t)(snp->snp_buf + snp->snp_base);
271 error = uiomove(from, (size_t)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 = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
284 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
285 kfree(snp->snp_buf, M_SNP);
287 snp->snp_blen = nblen;
297 snp_in(struct snoop *snp, char *buf, int n)
304 KASSERT(n >= 0, ("negative snoop char count"));
309 if (snp->snp_flags & SNOOP_DOWN) {
310 kprintf("Snoop: more data to down interface.\n");
314 if (snp->snp_flags & SNOOP_OFLOW) {
315 kprintf("Snoop: buffer overflow.\n");
317 * On overflow we just repeat the standart close
318 * procedure...yes , this is waste of space but.. Then next
319 * read from device will fail if one would recall he is
320 * snooping and retry...
323 return (snp_down(snp));
325 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
326 s_free = snp->snp_blen - snp->snp_len;
331 nblen = snp->snp_blen;
332 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
333 nblen = snp->snp_blen * 2;
334 s_free = nblen - (snp->snp_len + snp->snp_base);
336 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
337 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
338 kfree(snp->snp_buf, M_SNP);
340 snp->snp_blen = nblen;
343 snp->snp_flags |= SNOOP_OFLOW;
344 if (snp->snp_flags & SNOOP_RWAIT) {
345 snp->snp_flags &= ~SNOOP_RWAIT;
346 wakeup((caddr_t)snp);
354 from = (caddr_t)(snp->snp_buf + snp->snp_base);
355 to = (caddr_t)(snp->snp_buf);
357 bcopy(from, to, len);
360 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
364 if (snp->snp_flags & SNOOP_RWAIT) {
365 snp->snp_flags &= ~SNOOP_RWAIT;
366 wakeup((caddr_t)snp);
368 KNOTE(&snp->snp_kq.ki_note, 0);
374 snpopen(struct dev_open_args *ap)
376 cdev_t dev = ap->a_head.a_dev;
379 if (dev->si_drv1 == NULL) {
381 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
382 0600, "snp%d", minor(dev));
384 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
391 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
392 * all previous settings (especially SNOOP_OFLOW) will be cleared.
394 snp->snp_flags = SNOOP_OPEN;
396 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
397 snp->snp_blen = SNOOP_MINLEN;
402 * snp_tty == NULL is for inactive snoop devices.
405 snp->snp_target = NULL;
407 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
413 snp_detach(struct snoop *snp)
421 * If line disc. changed we do not touch this pointer, SLIP/PPP will
428 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
429 tp->t_line == snooplinedisc) {
431 tp->t_state &= ~TS_SNOOP;
432 tp->t_line = snp->snp_olddisc;
434 kprintf("Snoop: bad attached tty data.\n");
437 snp->snp_target = NULL;
440 KNOTE(&snp->snp_kq.ki_note, 0);
441 if ((snp->snp_flags & SNOOP_OPEN) == 0)
448 snpclose(struct dev_close_args *ap)
450 cdev_t dev = ap->a_head.a_dev;
455 LIST_REMOVE(snp, snp_list);
456 kfree(snp->snp_buf, M_SNP);
457 snp->snp_flags &= ~SNOOP_OPEN;
459 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
460 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
463 return (snp_detach(snp));
467 snp_down(struct snoop *snp)
470 if (snp->snp_blen != SNOOP_MINLEN) {
471 kfree(snp->snp_buf, M_SNP);
472 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
473 snp->snp_blen = SNOOP_MINLEN;
475 snp->snp_flags |= SNOOP_DOWN;
477 return (snp_detach(snp));
481 snpioctl(struct dev_ioctl_args *ap)
483 cdev_t dev = ap->a_head.a_dev;
485 struct tty *tp, *tpo;
491 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
493 return (snp_down(snp));
495 tp = snpdevtotty(tdev);
498 if (tp->t_state & TS_SNOOP)
503 if (snp->snp_target == NULL) {
506 tpo->t_state &= ~TS_SNOOP;
509 tp->t_sc = (caddr_t)snp;
510 tp->t_state |= TS_SNOOP;
511 snp->snp_olddisc = tp->t_line;
512 tp->t_line = snooplinedisc;
514 snp->snp_target = tdev;
517 * Clean overflow and down flags -
518 * we'll have a chance to get them in the future :)))
520 snp->snp_flags &= ~SNOOP_OFLOW;
521 snp->snp_flags &= ~SNOOP_DOWN;
527 * We keep snp_target field specially to make
528 * SNPGTTY happy, else we can't know what is device
529 * major/minor for tty.
531 *((cdev_t *)ap->a_data) = snp->snp_target;
535 if (*(int *)ap->a_data)
536 snp->snp_flags |= SNOOP_ASYNC;
538 snp->snp_flags &= ~SNOOP_ASYNC;
543 if (snp->snp_tty != NULL)
544 *(int *)ap->a_data = snp->snp_len;
546 if (snp->snp_flags & SNOOP_DOWN) {
547 if (snp->snp_flags & SNOOP_OFLOW)
548 *(int *)ap->a_data = SNP_OFLOW;
550 *(int *)ap->a_data = SNP_TTYCLOSE;
552 *(int *)ap->a_data = SNP_DETACH;
563 static struct filterops snpfiltops_rd =
564 { 1, NULL, snpfilter_detach, snpfilter_rd };
565 static struct filterops snpfiltops_wr =
566 { 1, NULL, snpfilter_detach, snpfilter_wr };
569 snpkqfilter(struct dev_kqfilter_args *ap)
571 cdev_t dev = ap->a_head.a_dev;
572 struct snoop *snp = dev->si_drv1;
573 struct knote *kn = ap->a_kn;
578 switch (kn->kn_filter) {
580 kn->kn_fop = &snpfiltops_rd;
581 kn->kn_hook = (caddr_t)snp;
584 kn->kn_fop = &snpfiltops_wr;
585 kn->kn_hook = (caddr_t)snp;
588 ap->a_result = EOPNOTSUPP;
592 klist = &snp->snp_kq.ki_note;
593 knote_insert(klist, kn);
599 snpfilter_detach(struct knote *kn)
601 struct snoop *snp = (struct snoop *)kn->kn_hook;
604 klist = &snp->snp_kq.ki_note;
605 knote_insert(klist, kn);
609 snpfilter_rd(struct knote *kn, long hint)
611 struct snoop *snp = (struct snoop *)kn->kn_hook;
615 * If snoop is down, we don't want to poll forever so we return 1.
616 * Caller should see if we down via FIONREAD ioctl(). The last should
617 * return -1 to indicate down state.
619 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
626 snpfilter_wr(struct knote *kn, long hint)
628 /* Writing is always OK */
633 snpclone(struct dev_clone_args *ap)
636 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
637 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
644 snp_modevent(module_t mod, int type, void *data)
650 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
651 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
652 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
654 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
655 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
656 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
660 if (!LIST_EMPTY(&snp_sclist))
662 ldisc_deregister(snooplinedisc);
663 devfs_clone_handler_del("snp");
664 dev_ops_remove_all(&snp_ops);
665 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
673 static moduledata_t snp_mod = {
678 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);