4 * Copyright (c) 1995 Ugen J.S.Antsilevich
6 * Redistribution and use in source forms, with and without modification,
7 * are permitted provided that this entire comment appears intact.
9 * Redistribution in binary form may occur without any restrictions.
10 * Obviously, it would be nice if you gave credit where credit is due
11 * but requiring it would be too onerous.
13 * This software is provided ``AS IS'' without any warranties of any kind.
17 * $FreeBSD: src/sys/dev/snp/snp.c,v 1.69.2.2 2002/05/06 07:30:02 dd Exp $
18 * $DragonFly: src/sys/dev/misc/snp/snp.c,v 1.19 2007/05/08 02:31:41 dillon Exp $
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/filio.h>
25 #include <sys/malloc.h>
28 #include <sys/event.h>
29 #include <sys/kernel.h>
30 #include <sys/queue.h>
31 #include <sys/snoop.h>
32 #include <sys/thread2.h>
33 #include <sys/vnode.h>
34 #include <sys/device.h>
35 #include <sys/devfs.h>
37 static l_close_t snplclose;
38 static l_write_t snplwrite;
39 static d_open_t snpopen;
40 static d_close_t snpclose;
41 static d_read_t snpread;
42 static d_write_t snpwrite;
43 static d_ioctl_t snpioctl;
44 static d_kqfilter_t snpkqfilter;
45 static d_clone_t snpclone;
46 DEVFS_DECLARE_CLONE_BITMAP(snp);
48 static void snpfilter_detach(struct knote *);
49 static int snpfilter_rd(struct knote *, long);
50 static int snpfilter_wr(struct knote *, long);
53 #define SNP_PREALLOCATED_UNITS 4
55 #define SNP_PREALLOCATED_UNITS NSNP
59 static struct dev_ops snp_ops = {
60 { "snp", CDEV_MAJOR, 0 },
66 .d_kqfilter = snpkqfilter
69 static struct linesw snpdisc = {
70 ttyopen, snplclose, ttread, snplwrite,
71 l_nullioctl, ttyinput, ttstart, ttymodem
75 * This is the main snoop per-device structure.
78 LIST_ENTRY(snoop) snp_list; /* List glue. */
79 cdev_t snp_target; /* Target tty device. */
80 struct tty *snp_tty; /* Target tty pointer. */
81 u_long snp_len; /* Possible length. */
82 u_long snp_base; /* Data base. */
83 u_long snp_blen; /* Used length. */
84 caddr_t snp_buf; /* Allocation pointer. */
85 int snp_flags; /* Flags. */
86 struct kqinfo snp_kq; /* Kqueue info. */
87 int snp_olddisc; /* Old line discipline. */
93 #define SNOOP_ASYNC 0x0002
94 #define SNOOP_OPEN 0x0004
95 #define SNOOP_RWAIT 0x0008
96 #define SNOOP_OFLOW 0x0010
97 #define SNOOP_DOWN 0x0020
102 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
103 * 4K tested to be the minimum
104 * for which on normal tty
105 * usage there is no need to
108 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
109 * If we grow more,something
110 * really bad in this world..
113 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
115 * The number of the "snoop" line discipline. This gets determined at
118 static int snooplinedisc;
121 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
123 static struct tty *snpdevtotty (cdev_t dev);
124 static int snp_detach (struct snoop *snp);
125 static int snp_down (struct snoop *snp);
126 static int snp_in (struct snoop *snp, char *buf, int n);
127 static int snp_modevent (module_t mod, int what, void *arg);
130 snplclose(struct tty *tp, int flag)
135 lwkt_gettoken(&tty_token);
137 error = snp_down(snp);
139 lwkt_reltoken(&tty_token);
142 error = ttylclose(tp, flag);
143 lwkt_reltoken(&tty_token);
148 snplwrite(struct tty *tp, struct uio *uio, int flag)
156 lwkt_gettoken(&tty_token);
160 while (uio->uio_resid > 0) {
161 ilen = (int)szmin(512, uio->uio_resid);
162 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
163 error = uiomove(ibuf, (size_t)ilen, uio);
166 snp_in(snp, ibuf, ilen);
167 /* Hackish, but probably the least of all evils. */
173 uio2.uio_resid = ilen;
174 uio2.uio_segflg = UIO_SYSSPACE;
175 uio2.uio_rw = UIO_WRITE;
176 uio2.uio_td = uio->uio_td;
177 error = ttwrite(tp, &uio2, flag);
185 lwkt_reltoken(&tty_token);
190 snpdevtotty(cdev_t dev)
192 if ((dev_dflags(dev) & D_TTY) == 0)
194 return (dev->si_tty);
197 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
198 * interactive mode write is 3 bytes
199 * length for function keys...
203 snpwrite(struct dev_write_args *ap)
205 cdev_t dev = ap->a_head.a_dev;
206 struct uio *uio = ap->a_uio;
210 unsigned char c[SNP_INPUT_BUF];
212 lwkt_gettoken(&tty_token);
216 lwkt_reltoken(&tty_token);
219 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
220 tp->t_line == snooplinedisc)
223 kprintf("Snoop: attempt to write to bad tty.\n");
224 lwkt_reltoken(&tty_token);
228 if (!(tp->t_state & TS_ISOPEN)) {
229 lwkt_reltoken(&tty_token);
233 while (uio->uio_resid > 0) {
234 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
235 if ((error = uiomove(c, (size_t)len, uio)) != 0) {
236 lwkt_reltoken(&tty_token);
239 for (i=0; i < len; i++) {
240 if (ttyinput(c[i], tp)) {
241 lwkt_reltoken(&tty_token);
246 lwkt_reltoken(&tty_token);
252 snpread(struct dev_read_args *ap)
254 cdev_t dev = ap->a_head.a_dev;
255 struct uio *uio = ap->a_uio;
257 int error, len, n, nblen;
261 lwkt_gettoken(&tty_token);
263 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
264 ("snoop buffer error"));
266 if (snp->snp_tty == NULL) {
267 lwkt_reltoken(&tty_token);
271 snp->snp_flags &= ~SNOOP_RWAIT;
274 if (snp->snp_len == 0) {
275 if (ap->a_ioflag & IO_NDELAY) {
276 lwkt_reltoken(&tty_token);
277 return (EWOULDBLOCK);
279 snp->snp_flags |= SNOOP_RWAIT;
280 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
282 lwkt_reltoken(&tty_token);
286 } while (snp->snp_len == 0);
291 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
292 len = (int)szmin(uio->uio_resid, snp->snp_len);
293 from = (caddr_t)(snp->snp_buf + snp->snp_base);
297 error = uiomove(from, (size_t)len, uio);
298 snp->snp_base += len;
301 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
302 snp->snp_flags &= ~SNOOP_OFLOW;
305 nblen = snp->snp_blen;
306 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
307 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
309 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
310 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
311 kfree(snp->snp_buf, M_SNP);
313 snp->snp_blen = nblen;
319 lwkt_reltoken(&tty_token);
324 * NOTE: Must be called with tty_token held
327 snp_in(struct snoop *snp, char *buf, int n)
334 ASSERT_LWKT_TOKEN_HELD(&tty_token);
335 KASSERT(n >= 0, ("negative snoop char count"));
340 if (snp->snp_flags & SNOOP_DOWN) {
341 kprintf("Snoop: more data to down interface.\n");
345 if (snp->snp_flags & SNOOP_OFLOW) {
346 kprintf("Snoop: buffer overflow.\n");
348 * On overflow we just repeat the standart close
349 * procedure...yes , this is waste of space but.. Then next
350 * read from device will fail if one would recall he is
351 * snooping and retry...
354 return (snp_down(snp));
356 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
357 s_free = snp->snp_blen - snp->snp_len;
362 nblen = snp->snp_blen;
363 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
364 nblen = snp->snp_blen * 2;
365 s_free = nblen - (snp->snp_len + snp->snp_base);
367 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
368 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
369 kfree(snp->snp_buf, M_SNP);
371 snp->snp_blen = nblen;
374 snp->snp_flags |= SNOOP_OFLOW;
375 if (snp->snp_flags & SNOOP_RWAIT) {
376 snp->snp_flags &= ~SNOOP_RWAIT;
377 wakeup((caddr_t)snp);
385 from = (caddr_t)(snp->snp_buf + snp->snp_base);
386 to = (caddr_t)(snp->snp_buf);
388 bcopy(from, to, len);
391 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
395 if (snp->snp_flags & SNOOP_RWAIT) {
396 snp->snp_flags &= ~SNOOP_RWAIT;
397 wakeup((caddr_t)snp);
399 KNOTE(&snp->snp_kq.ki_note, 0);
405 snpopen(struct dev_open_args *ap)
407 cdev_t dev = ap->a_head.a_dev;
410 lwkt_gettoken(&tty_token);
411 if (dev->si_drv1 == NULL) {
413 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
414 0600, "snp%d", minor(dev));
416 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
419 lwkt_reltoken(&tty_token);
424 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
425 * all previous settings (especially SNOOP_OFLOW) will be cleared.
427 snp->snp_flags = SNOOP_OPEN;
429 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
430 snp->snp_blen = SNOOP_MINLEN;
435 * snp_tty == NULL is for inactive snoop devices.
438 snp->snp_target = NULL;
440 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
441 lwkt_reltoken(&tty_token);
446 * NOTE: Must be called with tty_token held
449 snp_detach(struct snoop *snp)
453 ASSERT_LWKT_TOKEN_HELD(&tty_token);
458 * If line disc. changed we do not touch this pointer, SLIP/PPP will
465 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
466 tp->t_line == snooplinedisc) {
468 tp->t_state &= ~TS_SNOOP;
469 tp->t_line = snp->snp_olddisc;
471 kprintf("Snoop: bad attached tty data.\n");
474 snp->snp_target = NULL;
477 KNOTE(&snp->snp_kq.ki_note, 0);
478 if ((snp->snp_flags & SNOOP_OPEN) == 0)
485 snpclose(struct dev_close_args *ap)
487 cdev_t dev = ap->a_head.a_dev;
491 lwkt_gettoken(&tty_token);
494 LIST_REMOVE(snp, snp_list);
495 kfree(snp->snp_buf, M_SNP);
496 snp->snp_flags &= ~SNOOP_OPEN;
498 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
499 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
502 ret = snp_detach(snp);
503 lwkt_reltoken(&tty_token);
508 * NOTE: Must be called with tty_token held
511 snp_down(struct snoop *snp)
514 ASSERT_LWKT_TOKEN_HELD(&tty_token);
515 if (snp->snp_blen != SNOOP_MINLEN) {
516 kfree(snp->snp_buf, M_SNP);
517 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
518 snp->snp_blen = SNOOP_MINLEN;
520 snp->snp_flags |= SNOOP_DOWN;
522 return (snp_detach(snp));
526 snpioctl(struct dev_ioctl_args *ap)
528 cdev_t dev = ap->a_head.a_dev;
530 struct tty *tp, *tpo;
534 lwkt_gettoken(&tty_token);
538 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
540 lwkt_reltoken(&tty_token);
545 tp = snpdevtotty(tdev);
547 lwkt_reltoken(&tty_token);
550 if (tp->t_state & TS_SNOOP) {
551 lwkt_reltoken(&tty_token);
557 if (snp->snp_target == NULL) {
560 tpo->t_state &= ~TS_SNOOP;
563 tp->t_sc = (caddr_t)snp;
564 tp->t_state |= TS_SNOOP;
565 snp->snp_olddisc = tp->t_line;
566 tp->t_line = snooplinedisc;
568 snp->snp_target = tdev;
571 * Clean overflow and down flags -
572 * we'll have a chance to get them in the future :)))
574 snp->snp_flags &= ~SNOOP_OFLOW;
575 snp->snp_flags &= ~SNOOP_DOWN;
581 * We keep snp_target field specially to make
582 * SNPGTTY happy, else we can't know what is device
583 * major/minor for tty.
585 *((cdev_t *)ap->a_data) = snp->snp_target;
589 if (*(int *)ap->a_data)
590 snp->snp_flags |= SNOOP_ASYNC;
592 snp->snp_flags &= ~SNOOP_ASYNC;
597 if (snp->snp_tty != NULL)
598 *(int *)ap->a_data = snp->snp_len;
600 if (snp->snp_flags & SNOOP_DOWN) {
601 if (snp->snp_flags & SNOOP_OFLOW)
602 *(int *)ap->a_data = SNP_OFLOW;
604 *(int *)ap->a_data = SNP_TTYCLOSE;
606 *(int *)ap->a_data = SNP_DETACH;
612 lwkt_reltoken(&tty_token);
615 lwkt_reltoken(&tty_token);
619 static struct filterops snpfiltops_rd =
620 { FILTEROP_ISFD, NULL, snpfilter_detach, snpfilter_rd };
621 static struct filterops snpfiltops_wr =
622 { FILTEROP_ISFD, NULL, snpfilter_detach, snpfilter_wr };
625 snpkqfilter(struct dev_kqfilter_args *ap)
627 cdev_t dev = ap->a_head.a_dev;
628 struct snoop *snp = dev->si_drv1;
629 struct knote *kn = ap->a_kn;
632 lwkt_gettoken(&tty_token);
635 switch (kn->kn_filter) {
637 kn->kn_fop = &snpfiltops_rd;
638 kn->kn_hook = (caddr_t)snp;
641 kn->kn_fop = &snpfiltops_wr;
642 kn->kn_hook = (caddr_t)snp;
645 ap->a_result = EOPNOTSUPP;
646 lwkt_reltoken(&tty_token);
650 klist = &snp->snp_kq.ki_note;
651 knote_insert(klist, kn);
653 lwkt_reltoken(&tty_token);
658 snpfilter_detach(struct knote *kn)
660 struct snoop *snp = (struct snoop *)kn->kn_hook;
663 klist = &snp->snp_kq.ki_note;
664 knote_insert(klist, kn);
668 snpfilter_rd(struct knote *kn, long hint)
670 struct snoop *snp = (struct snoop *)kn->kn_hook;
673 lwkt_gettoken(&tty_token);
675 * If snoop is down, we don't want to poll forever so we return 1.
676 * Caller should see if we down via FIONREAD ioctl(). The last should
677 * return -1 to indicate down state.
679 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
682 lwkt_reltoken(&tty_token);
687 snpfilter_wr(struct knote *kn, long hint)
689 /* Writing is always OK */
694 snpclone(struct dev_clone_args *ap)
697 lwkt_gettoken(&tty_token);
698 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
699 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
701 lwkt_reltoken(&tty_token);
706 snp_modevent(module_t mod, int type, void *data)
710 lwkt_gettoken(&tty_token);
713 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
714 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
715 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
717 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
718 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
719 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
723 if (!LIST_EMPTY(&snp_sclist))
725 ldisc_deregister(snooplinedisc);
726 devfs_clone_handler_del("snp");
727 dev_ops_remove_all(&snp_ops);
728 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
733 lwkt_reltoken(&tty_token);
737 static moduledata_t snp_mod = {
742 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);