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 selinfo snp_sel; /* Select 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 selwakeup(&snp->snp_sel);
369 snp->snp_sel.si_pid = 0;
375 snpopen(struct dev_open_args *ap)
377 cdev_t dev = ap->a_head.a_dev;
380 if (dev->si_drv1 == NULL) {
382 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
383 0600, "snp%d", minor(dev));
385 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
392 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
393 * all previous settings (especially SNOOP_OFLOW) will be cleared.
395 snp->snp_flags = SNOOP_OPEN;
397 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
398 snp->snp_blen = SNOOP_MINLEN;
403 * snp_tty == NULL is for inactive snoop devices.
406 snp->snp_target = NULL;
408 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
414 snp_detach(struct snoop *snp)
422 * If line disc. changed we do not touch this pointer, SLIP/PPP will
429 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
430 tp->t_line == snooplinedisc) {
432 tp->t_state &= ~TS_SNOOP;
433 tp->t_line = snp->snp_olddisc;
435 kprintf("Snoop: bad attached tty data.\n");
438 snp->snp_target = NULL;
441 selwakeup(&snp->snp_sel);
442 snp->snp_sel.si_pid = 0;
443 if ((snp->snp_flags & SNOOP_OPEN) == 0)
450 snpclose(struct dev_close_args *ap)
452 cdev_t dev = ap->a_head.a_dev;
457 LIST_REMOVE(snp, snp_list);
458 kfree(snp->snp_buf, M_SNP);
459 snp->snp_flags &= ~SNOOP_OPEN;
461 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
462 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
465 return (snp_detach(snp));
469 snp_down(struct snoop *snp)
472 if (snp->snp_blen != SNOOP_MINLEN) {
473 kfree(snp->snp_buf, M_SNP);
474 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
475 snp->snp_blen = SNOOP_MINLEN;
477 snp->snp_flags |= SNOOP_DOWN;
479 return (snp_detach(snp));
483 snpioctl(struct dev_ioctl_args *ap)
485 cdev_t dev = ap->a_head.a_dev;
487 struct tty *tp, *tpo;
493 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
495 return (snp_down(snp));
497 tp = snpdevtotty(tdev);
500 if (tp->t_state & TS_SNOOP)
505 if (snp->snp_target == NULL) {
508 tpo->t_state &= ~TS_SNOOP;
511 tp->t_sc = (caddr_t)snp;
512 tp->t_state |= TS_SNOOP;
513 snp->snp_olddisc = tp->t_line;
514 tp->t_line = snooplinedisc;
516 snp->snp_target = tdev;
519 * Clean overflow and down flags -
520 * we'll have a chance to get them in the future :)))
522 snp->snp_flags &= ~SNOOP_OFLOW;
523 snp->snp_flags &= ~SNOOP_DOWN;
529 * We keep snp_target field specially to make
530 * SNPGTTY happy, else we can't know what is device
531 * major/minor for tty.
533 *((cdev_t *)ap->a_data) = snp->snp_target;
537 if (*(int *)ap->a_data)
538 snp->snp_flags |= SNOOP_ASYNC;
540 snp->snp_flags &= ~SNOOP_ASYNC;
545 if (snp->snp_tty != NULL)
546 *(int *)ap->a_data = snp->snp_len;
548 if (snp->snp_flags & SNOOP_DOWN) {
549 if (snp->snp_flags & SNOOP_OFLOW)
550 *(int *)ap->a_data = SNP_OFLOW;
552 *(int *)ap->a_data = SNP_TTYCLOSE;
554 *(int *)ap->a_data = SNP_DETACH;
565 static struct filterops snpfiltops_rd =
566 { 1, NULL, snpfilter_detach, snpfilter_rd };
567 static struct filterops snpfiltops_wr =
568 { 1, NULL, snpfilter_detach, snpfilter_wr };
571 snpkqfilter(struct dev_kqfilter_args *ap)
573 cdev_t dev = ap->a_head.a_dev;
574 struct snoop *snp = dev->si_drv1;
575 struct knote *kn = ap->a_kn;
580 switch (kn->kn_filter) {
582 kn->kn_fop = &snpfiltops_rd;
583 kn->kn_hook = (caddr_t)snp;
586 kn->kn_fop = &snpfiltops_wr;
587 kn->kn_hook = (caddr_t)snp;
590 ap->a_result = EOPNOTSUPP;
595 klist = &snp->snp_sel.si_note;
596 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
603 snpfilter_detach(struct knote *kn)
605 struct snoop *snp = (struct snoop *)kn->kn_hook;
609 klist = &snp->snp_sel.si_note;
610 SLIST_REMOVE(klist, kn, knote, kn_selnext);
615 snpfilter_rd(struct knote *kn, long hint)
617 struct snoop *snp = (struct snoop *)kn->kn_hook;
621 * If snoop is down, we don't want to poll forever so we return 1.
622 * Caller should see if we down via FIONREAD ioctl(). The last should
623 * return -1 to indicate down state.
625 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
632 snpfilter_wr(struct knote *kn, long hint)
634 /* Writing is always OK */
639 snpclone(struct dev_clone_args *ap)
642 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
643 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
650 snp_modevent(module_t mod, int type, void *data)
656 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
657 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
658 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
660 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
661 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
662 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
666 if (!LIST_EMPTY(&snp_sclist))
668 ldisc_deregister(snooplinedisc);
669 devfs_clone_handler_del("snp");
670 dev_ops_remove_all(&snp_ops);
671 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
679 static moduledata_t snp_mod = {
684 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);