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>
27 #include <sys/event.h>
28 #include <sys/kernel.h>
29 #include <sys/queue.h>
30 #include <sys/snoop.h>
31 #include <sys/thread2.h>
32 #include <sys/vnode.h>
33 #include <sys/device.h>
34 #include <sys/devfs.h>
36 static l_close_t snplclose;
37 static l_write_t snplwrite;
38 static d_open_t snpopen;
39 static d_close_t snpclose;
40 static d_read_t snpread;
41 static d_write_t snpwrite;
42 static d_ioctl_t snpioctl;
43 static d_poll_t snppoll;
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(struct knote *, long);
52 #define SNP_PREALLOCATED_UNITS 4
54 #define SNP_PREALLOCATED_UNITS NSNP
58 static struct dev_ops snp_ops = {
59 { "snp", CDEV_MAJOR, D_KQFILTER },
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 selinfo snp_sel; /* Select 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)
136 error = snp_down(snp);
139 error = ttylclose(tp, flag);
144 snplwrite(struct tty *tp, struct uio *uio, int flag)
155 while (uio->uio_resid > 0) {
156 ilen = (int)szmin(512, uio->uio_resid);
157 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
158 error = uiomove(ibuf, (size_t)ilen, uio);
161 snp_in(snp, ibuf, ilen);
162 /* Hackish, but probably the least of all evils. */
168 uio2.uio_resid = ilen;
169 uio2.uio_segflg = UIO_SYSSPACE;
170 uio2.uio_rw = UIO_WRITE;
171 uio2.uio_td = uio->uio_td;
172 error = ttwrite(tp, &uio2, flag);
184 snpdevtotty(cdev_t dev)
186 if ((dev_dflags(dev) & D_TTY) == 0)
188 return (dev->si_tty);
191 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
192 * interactive mode write is 3 bytes
193 * length for function keys...
197 snpwrite(struct dev_write_args *ap)
199 cdev_t dev = ap->a_head.a_dev;
200 struct uio *uio = ap->a_uio;
204 unsigned char c[SNP_INPUT_BUF];
210 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
211 tp->t_line == snooplinedisc)
214 kprintf("Snoop: attempt to write to bad tty.\n");
218 if (!(tp->t_state & TS_ISOPEN))
221 while (uio->uio_resid > 0) {
222 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
223 if ((error = uiomove(c, (size_t)len, uio)) != 0)
225 for (i=0; i < len; i++) {
226 if (ttyinput(c[i], tp))
235 snpread(struct dev_read_args *ap)
237 cdev_t dev = ap->a_head.a_dev;
238 struct uio *uio = ap->a_uio;
240 int error, len, n, nblen;
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 (ap->a_ioflag & IO_NDELAY)
256 return (EWOULDBLOCK);
257 snp->snp_flags |= SNOOP_RWAIT;
258 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
262 } while (snp->snp_len == 0);
267 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
268 len = (int)szmin(uio->uio_resid, snp->snp_len);
269 from = (caddr_t)(snp->snp_buf + snp->snp_base);
273 error = uiomove(from, (size_t)len, uio);
274 snp->snp_base += len;
277 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
278 snp->snp_flags &= ~SNOOP_OFLOW;
281 nblen = snp->snp_blen;
282 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
283 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
285 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
286 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
287 kfree(snp->snp_buf, M_SNP);
289 snp->snp_blen = nblen;
299 snp_in(struct snoop *snp, char *buf, int n)
306 KASSERT(n >= 0, ("negative snoop char count"));
311 if (snp->snp_flags & SNOOP_DOWN) {
312 kprintf("Snoop: more data to down interface.\n");
316 if (snp->snp_flags & SNOOP_OFLOW) {
317 kprintf("Snoop: buffer overflow.\n");
319 * On overflow we just repeat the standart close
320 * procedure...yes , this is waste of space but.. Then next
321 * read from device will fail if one would recall he is
322 * snooping and retry...
325 return (snp_down(snp));
327 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
328 s_free = snp->snp_blen - snp->snp_len;
333 nblen = snp->snp_blen;
334 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
335 nblen = snp->snp_blen * 2;
336 s_free = nblen - (snp->snp_len + snp->snp_base);
338 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
339 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
340 kfree(snp->snp_buf, M_SNP);
342 snp->snp_blen = nblen;
345 snp->snp_flags |= SNOOP_OFLOW;
346 if (snp->snp_flags & SNOOP_RWAIT) {
347 snp->snp_flags &= ~SNOOP_RWAIT;
348 wakeup((caddr_t)snp);
356 from = (caddr_t)(snp->snp_buf + snp->snp_base);
357 to = (caddr_t)(snp->snp_buf);
359 bcopy(from, to, len);
362 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
366 if (snp->snp_flags & SNOOP_RWAIT) {
367 snp->snp_flags &= ~SNOOP_RWAIT;
368 wakeup((caddr_t)snp);
370 selwakeup(&snp->snp_sel);
371 snp->snp_sel.si_pid = 0;
377 snpopen(struct dev_open_args *ap)
379 cdev_t dev = ap->a_head.a_dev;
382 if (dev->si_drv1 == NULL) {
384 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
385 0600, "snp%d", minor(dev));
387 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
394 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
395 * all previous settings (especially SNOOP_OFLOW) will be cleared.
397 snp->snp_flags = SNOOP_OPEN;
399 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
400 snp->snp_blen = SNOOP_MINLEN;
405 * snp_tty == NULL is for inactive snoop devices.
408 snp->snp_target = NULL;
410 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
416 snp_detach(struct snoop *snp)
424 * If line disc. changed we do not touch this pointer, SLIP/PPP will
431 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
432 tp->t_line == snooplinedisc) {
434 tp->t_state &= ~TS_SNOOP;
435 tp->t_line = snp->snp_olddisc;
437 kprintf("Snoop: bad attached tty data.\n");
440 snp->snp_target = NULL;
443 selwakeup(&snp->snp_sel);
444 snp->snp_sel.si_pid = 0;
445 if ((snp->snp_flags & SNOOP_OPEN) == 0)
452 snpclose(struct dev_close_args *ap)
454 cdev_t dev = ap->a_head.a_dev;
459 LIST_REMOVE(snp, snp_list);
460 kfree(snp->snp_buf, M_SNP);
461 snp->snp_flags &= ~SNOOP_OPEN;
463 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
464 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
467 return (snp_detach(snp));
471 snp_down(struct snoop *snp)
474 if (snp->snp_blen != SNOOP_MINLEN) {
475 kfree(snp->snp_buf, M_SNP);
476 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
477 snp->snp_blen = SNOOP_MINLEN;
479 snp->snp_flags |= SNOOP_DOWN;
481 return (snp_detach(snp));
485 snpioctl(struct dev_ioctl_args *ap)
487 cdev_t dev = ap->a_head.a_dev;
489 struct tty *tp, *tpo;
495 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
497 return (snp_down(snp));
499 tp = snpdevtotty(tdev);
502 if (tp->t_state & TS_SNOOP)
507 if (snp->snp_target == NULL) {
510 tpo->t_state &= ~TS_SNOOP;
513 tp->t_sc = (caddr_t)snp;
514 tp->t_state |= TS_SNOOP;
515 snp->snp_olddisc = tp->t_line;
516 tp->t_line = snooplinedisc;
518 snp->snp_target = tdev;
521 * Clean overflow and down flags -
522 * we'll have a chance to get them in the future :)))
524 snp->snp_flags &= ~SNOOP_OFLOW;
525 snp->snp_flags &= ~SNOOP_DOWN;
531 * We keep snp_target field specially to make
532 * SNPGTTY happy, else we can't know what is device
533 * major/minor for tty.
535 *((cdev_t *)ap->a_data) = snp->snp_target;
539 if (*(int *)ap->a_data)
540 snp->snp_flags |= SNOOP_ASYNC;
542 snp->snp_flags &= ~SNOOP_ASYNC;
547 if (snp->snp_tty != NULL)
548 *(int *)ap->a_data = snp->snp_len;
550 if (snp->snp_flags & SNOOP_DOWN) {
551 if (snp->snp_flags & SNOOP_OFLOW)
552 *(int *)ap->a_data = SNP_OFLOW;
554 *(int *)ap->a_data = SNP_TTYCLOSE;
556 *(int *)ap->a_data = SNP_DETACH;
568 snppoll(struct dev_poll_args *ap)
570 cdev_t dev = ap->a_head.a_dev;
577 * If snoop is down, we don't want to poll() forever so we return 1.
578 * Caller should see if we down via FIONREAD ioctl(). The last should
579 * return -1 to indicate down state.
581 if (ap->a_events & (POLLIN | POLLRDNORM)) {
582 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
583 revents |= ap->a_events & (POLLIN | POLLRDNORM);
585 selrecord(curthread, &snp->snp_sel);
587 ap->a_events = revents;
591 static struct filterops snpfiltops =
592 { 1, NULL, snpfilter_detach, snpfilter };
595 snpkqfilter(struct dev_kqfilter_args *ap)
597 cdev_t dev = ap->a_head.a_dev;
598 struct snoop *snp = dev->si_drv1;
599 struct knote *kn = ap->a_kn;
604 switch (kn->kn_filter) {
606 kn->kn_fop = &snpfiltops;
607 kn->kn_hook = (caddr_t)snp;
615 klist = &snp->snp_sel.si_note;
616 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
623 snpfilter_detach(struct knote *kn)
625 struct snoop *snp = (struct snoop *)kn->kn_hook;
629 klist = &snp->snp_sel.si_note;
630 SLIST_REMOVE(klist, kn, knote, kn_selnext);
635 snpfilter(struct knote *kn, long hint)
637 struct snoop *snp = (struct snoop *)kn->kn_hook;
641 * If snoop is down, we don't want to poll forever so we return 1.
642 * Caller should see if we down via FIONREAD ioctl(). The last should
643 * return -1 to indicate down state.
645 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
652 snpclone(struct dev_clone_args *ap)
655 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
656 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
663 snp_modevent(module_t mod, int type, void *data)
669 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
670 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
671 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
673 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
674 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
675 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
679 if (!LIST_EMPTY(&snp_sclist))
681 ldisc_deregister(snooplinedisc);
682 devfs_clone_handler_del("snp");
683 dev_ops_remove_all(&snp_ops);
684 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
692 static moduledata_t snp_mod = {
697 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);