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 $
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/thread2.h>
30 #include <sys/vnode.h>
31 #include <sys/device.h>
32 #include <sys/devfs.h>
34 static l_close_t snplclose;
35 static l_write_t snplwrite;
36 static d_open_t snpopen;
37 static d_close_t snpclose;
38 static d_read_t snpread;
39 static d_write_t snpwrite;
40 static d_ioctl_t snpioctl;
41 static d_poll_t snppoll;
42 static d_clone_t snpclone;
43 DEVFS_DECLARE_CLONE_BITMAP(snp);
44 #define SNP_PREALLOCATED_UNITS 4
47 static struct dev_ops snp_ops = {
48 { "snp", CDEV_MAJOR, 0 },
57 static struct linesw snpdisc = {
58 ttyopen, snplclose, ttread, snplwrite,
59 l_nullioctl, ttyinput, ttstart, ttymodem
63 * This is the main snoop per-device structure.
66 LIST_ENTRY(snoop) snp_list; /* List glue. */
67 cdev_t snp_target; /* Target tty device. */
68 struct tty *snp_tty; /* Target tty pointer. */
69 u_long snp_len; /* Possible length. */
70 u_long snp_base; /* Data base. */
71 u_long snp_blen; /* Used length. */
72 caddr_t snp_buf; /* Allocation pointer. */
73 int snp_flags; /* Flags. */
74 struct selinfo snp_sel; /* Select info. */
75 int snp_olddisc; /* Old line discipline. */
81 #define SNOOP_ASYNC 0x0002
82 #define SNOOP_OPEN 0x0004
83 #define SNOOP_RWAIT 0x0008
84 #define SNOOP_OFLOW 0x0010
85 #define SNOOP_DOWN 0x0020
90 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
91 * 4K tested to be the minimum
92 * for which on normal tty
93 * usage there is no need to
96 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
97 * If we grow more,something
98 * really bad in this world..
101 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
103 * The number of the "snoop" line discipline. This gets determined at
106 static int snooplinedisc;
109 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
111 static struct tty *snpdevtotty (cdev_t dev);
112 static int snp_detach (struct snoop *snp);
113 static int snp_down (struct snoop *snp);
114 static int snp_in (struct snoop *snp, char *buf, int n);
115 static int snp_modevent (module_t mod, int what, void *arg);
118 snplclose(struct tty *tp, int flag)
124 error = snp_down(snp);
127 error = ttylclose(tp, flag);
132 snplwrite(struct tty *tp, struct uio *uio, int flag)
143 while (uio->uio_resid > 0) {
144 ilen = (int)szmin(512, uio->uio_resid);
145 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
146 error = uiomove(ibuf, (size_t)ilen, uio);
149 snp_in(snp, ibuf, ilen);
150 /* Hackish, but probably the least of all evils. */
156 uio2.uio_resid = ilen;
157 uio2.uio_segflg = UIO_SYSSPACE;
158 uio2.uio_rw = UIO_WRITE;
159 uio2.uio_td = uio->uio_td;
160 error = ttwrite(tp, &uio2, flag);
172 snpdevtotty(cdev_t dev)
174 if ((dev_dflags(dev) & D_TTY) == 0)
176 return (dev->si_tty);
179 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
180 * interactive mode write is 3 bytes
181 * length for function keys...
185 snpwrite(struct dev_write_args *ap)
187 cdev_t dev = ap->a_head.a_dev;
188 struct uio *uio = ap->a_uio;
192 unsigned char c[SNP_INPUT_BUF];
198 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
199 tp->t_line == snooplinedisc)
202 kprintf("Snoop: attempt to write to bad tty.\n");
206 if (!(tp->t_state & TS_ISOPEN))
209 while (uio->uio_resid > 0) {
210 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
211 if ((error = uiomove(c, (size_t)len, uio)) != 0)
213 for (i=0; i < len; i++) {
214 if (ttyinput(c[i], tp))
223 snpread(struct dev_read_args *ap)
225 cdev_t dev = ap->a_head.a_dev;
226 struct uio *uio = ap->a_uio;
228 int error, len, n, nblen;
233 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
234 ("snoop buffer error"));
236 if (snp->snp_tty == NULL)
239 snp->snp_flags &= ~SNOOP_RWAIT;
242 if (snp->snp_len == 0) {
243 if (ap->a_ioflag & IO_NDELAY)
244 return (EWOULDBLOCK);
245 snp->snp_flags |= SNOOP_RWAIT;
246 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
250 } while (snp->snp_len == 0);
255 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
256 len = (int)szmin(uio->uio_resid, snp->snp_len);
257 from = (caddr_t)(snp->snp_buf + snp->snp_base);
261 error = uiomove(from, (size_t)len, uio);
262 snp->snp_base += len;
265 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
266 snp->snp_flags &= ~SNOOP_OFLOW;
269 nblen = snp->snp_blen;
270 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
271 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
273 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
274 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
275 kfree(snp->snp_buf, M_SNP);
277 snp->snp_blen = nblen;
287 snp_in(struct snoop *snp, char *buf, int n)
294 KASSERT(n >= 0, ("negative snoop char count"));
299 if (snp->snp_flags & SNOOP_DOWN) {
300 kprintf("Snoop: more data to down interface.\n");
304 if (snp->snp_flags & SNOOP_OFLOW) {
305 kprintf("Snoop: buffer overflow.\n");
307 * On overflow we just repeat the standart close
308 * procedure...yes , this is waste of space but.. Then next
309 * read from device will fail if one would recall he is
310 * snooping and retry...
313 return (snp_down(snp));
315 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
316 s_free = snp->snp_blen - snp->snp_len;
321 nblen = snp->snp_blen;
322 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
323 nblen = snp->snp_blen * 2;
324 s_free = nblen - (snp->snp_len + snp->snp_base);
326 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
327 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
328 kfree(snp->snp_buf, M_SNP);
330 snp->snp_blen = nblen;
333 snp->snp_flags |= SNOOP_OFLOW;
334 if (snp->snp_flags & SNOOP_RWAIT) {
335 snp->snp_flags &= ~SNOOP_RWAIT;
336 wakeup((caddr_t)snp);
344 from = (caddr_t)(snp->snp_buf + snp->snp_base);
345 to = (caddr_t)(snp->snp_buf);
347 bcopy(from, to, len);
350 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
354 if (snp->snp_flags & SNOOP_RWAIT) {
355 snp->snp_flags &= ~SNOOP_RWAIT;
356 wakeup((caddr_t)snp);
358 selwakeup(&snp->snp_sel);
359 snp->snp_sel.si_pid = 0;
365 snpopen(struct dev_open_args *ap)
367 cdev_t dev = ap->a_head.a_dev;
370 if (dev->si_drv1 == NULL) {
372 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
373 0600, "snp%d", minor(dev));
375 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
382 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
383 * all previous settings (especially SNOOP_OFLOW) will be cleared.
385 snp->snp_flags = SNOOP_OPEN;
387 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
388 snp->snp_blen = SNOOP_MINLEN;
393 * snp_tty == NULL is for inactive snoop devices.
396 snp->snp_target = NULL;
398 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
404 snp_detach(struct snoop *snp)
412 * If line disc. changed we do not touch this pointer, SLIP/PPP will
419 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
420 tp->t_line == snooplinedisc) {
422 tp->t_state &= ~TS_SNOOP;
423 tp->t_line = snp->snp_olddisc;
425 kprintf("Snoop: bad attached tty data.\n");
428 snp->snp_target = NULL;
431 selwakeup(&snp->snp_sel);
432 snp->snp_sel.si_pid = 0;
433 if ((snp->snp_flags & SNOOP_OPEN) == 0)
440 snpclose(struct dev_close_args *ap)
442 cdev_t dev = ap->a_head.a_dev;
447 LIST_REMOVE(snp, snp_list);
448 kfree(snp->snp_buf, M_SNP);
449 snp->snp_flags &= ~SNOOP_OPEN;
451 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
452 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
455 return (snp_detach(snp));
459 snp_down(struct snoop *snp)
462 if (snp->snp_blen != SNOOP_MINLEN) {
463 kfree(snp->snp_buf, M_SNP);
464 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
465 snp->snp_blen = SNOOP_MINLEN;
467 snp->snp_flags |= SNOOP_DOWN;
469 return (snp_detach(snp));
473 snpioctl(struct dev_ioctl_args *ap)
475 cdev_t dev = ap->a_head.a_dev;
477 struct tty *tp, *tpo;
483 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
485 return (snp_down(snp));
487 tp = snpdevtotty(tdev);
490 if (tp->t_state & TS_SNOOP)
495 if (snp->snp_target == NULL) {
498 tpo->t_state &= ~TS_SNOOP;
501 tp->t_sc = (caddr_t)snp;
502 tp->t_state |= TS_SNOOP;
503 snp->snp_olddisc = tp->t_line;
504 tp->t_line = snooplinedisc;
506 snp->snp_target = tdev;
509 * Clean overflow and down flags -
510 * we'll have a chance to get them in the future :)))
512 snp->snp_flags &= ~SNOOP_OFLOW;
513 snp->snp_flags &= ~SNOOP_DOWN;
519 * We keep snp_target field specially to make
520 * SNPGTTY happy, else we can't know what is device
521 * major/minor for tty.
523 *((cdev_t *)ap->a_data) = snp->snp_target;
527 if (*(int *)ap->a_data)
528 snp->snp_flags |= SNOOP_ASYNC;
530 snp->snp_flags &= ~SNOOP_ASYNC;
535 if (snp->snp_tty != NULL)
536 *(int *)ap->a_data = snp->snp_len;
538 if (snp->snp_flags & SNOOP_DOWN) {
539 if (snp->snp_flags & SNOOP_OFLOW)
540 *(int *)ap->a_data = SNP_OFLOW;
542 *(int *)ap->a_data = SNP_TTYCLOSE;
544 *(int *)ap->a_data = SNP_DETACH;
556 snppoll(struct dev_poll_args *ap)
558 cdev_t dev = ap->a_head.a_dev;
565 * If snoop is down, we don't want to poll() forever so we return 1.
566 * Caller should see if we down via FIONREAD ioctl(). The last should
567 * return -1 to indicate down state.
569 if (ap->a_events & (POLLIN | POLLRDNORM)) {
570 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
571 revents |= ap->a_events & (POLLIN | POLLRDNORM);
573 selrecord(curthread, &snp->snp_sel);
575 ap->a_events = revents;
580 snpclone(struct dev_clone_args *ap)
583 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
584 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
591 snp_modevent(module_t mod, int type, void *data)
597 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
598 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
599 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
601 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
602 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
603 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
607 if (!LIST_EMPTY(&snp_sclist))
609 ldisc_deregister(snooplinedisc);
610 devfs_clone_handler_del("snp");
611 dev_ops_remove_all(&snp_ops);
612 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
620 static moduledata_t snp_mod = {
625 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);