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/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_poll_t snppoll;
43 static d_clone_t snpclone;
44 DEVFS_DECLARE_CLONE_BITMAP(snp);
47 #define SNP_PREALLOCATED_UNITS 4
49 #define SNP_PREALLOCATED_UNITS NSNP
53 static struct dev_ops snp_ops = {
54 { "snp", CDEV_MAJOR, 0 },
63 static struct linesw snpdisc = {
64 ttyopen, snplclose, ttread, snplwrite,
65 l_nullioctl, ttyinput, ttstart, ttymodem
69 * This is the main snoop per-device structure.
72 LIST_ENTRY(snoop) snp_list; /* List glue. */
73 cdev_t snp_target; /* Target tty device. */
74 struct tty *snp_tty; /* Target tty pointer. */
75 u_long snp_len; /* Possible length. */
76 u_long snp_base; /* Data base. */
77 u_long snp_blen; /* Used length. */
78 caddr_t snp_buf; /* Allocation pointer. */
79 int snp_flags; /* Flags. */
80 struct selinfo snp_sel; /* Select info. */
81 int snp_olddisc; /* Old line discipline. */
87 #define SNOOP_ASYNC 0x0002
88 #define SNOOP_OPEN 0x0004
89 #define SNOOP_RWAIT 0x0008
90 #define SNOOP_OFLOW 0x0010
91 #define SNOOP_DOWN 0x0020
96 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
97 * 4K tested to be the minimum
98 * for which on normal tty
99 * usage there is no need to
102 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
103 * If we grow more,something
104 * really bad in this world..
107 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
109 * The number of the "snoop" line discipline. This gets determined at
112 static int snooplinedisc;
115 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
117 static struct tty *snpdevtotty (cdev_t dev);
118 static int snp_detach (struct snoop *snp);
119 static int snp_down (struct snoop *snp);
120 static int snp_in (struct snoop *snp, char *buf, int n);
121 static int snp_modevent (module_t mod, int what, void *arg);
124 snplclose(struct tty *tp, int flag)
130 error = snp_down(snp);
133 error = ttylclose(tp, flag);
138 snplwrite(struct tty *tp, struct uio *uio, int flag)
149 while (uio->uio_resid > 0) {
150 ilen = (int)szmin(512, uio->uio_resid);
151 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
152 error = uiomove(ibuf, (size_t)ilen, uio);
155 snp_in(snp, ibuf, ilen);
156 /* Hackish, but probably the least of all evils. */
162 uio2.uio_resid = ilen;
163 uio2.uio_segflg = UIO_SYSSPACE;
164 uio2.uio_rw = UIO_WRITE;
165 uio2.uio_td = uio->uio_td;
166 error = ttwrite(tp, &uio2, flag);
178 snpdevtotty(cdev_t dev)
180 if ((dev_dflags(dev) & D_TTY) == 0)
182 return (dev->si_tty);
185 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
186 * interactive mode write is 3 bytes
187 * length for function keys...
191 snpwrite(struct dev_write_args *ap)
193 cdev_t dev = ap->a_head.a_dev;
194 struct uio *uio = ap->a_uio;
198 unsigned char c[SNP_INPUT_BUF];
204 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
205 tp->t_line == snooplinedisc)
208 kprintf("Snoop: attempt to write to bad tty.\n");
212 if (!(tp->t_state & TS_ISOPEN))
215 while (uio->uio_resid > 0) {
216 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
217 if ((error = uiomove(c, (size_t)len, uio)) != 0)
219 for (i=0; i < len; i++) {
220 if (ttyinput(c[i], tp))
229 snpread(struct dev_read_args *ap)
231 cdev_t dev = ap->a_head.a_dev;
232 struct uio *uio = ap->a_uio;
234 int error, len, n, nblen;
239 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
240 ("snoop buffer error"));
242 if (snp->snp_tty == NULL)
245 snp->snp_flags &= ~SNOOP_RWAIT;
248 if (snp->snp_len == 0) {
249 if (ap->a_ioflag & IO_NDELAY)
250 return (EWOULDBLOCK);
251 snp->snp_flags |= SNOOP_RWAIT;
252 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
256 } while (snp->snp_len == 0);
261 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
262 len = (int)szmin(uio->uio_resid, snp->snp_len);
263 from = (caddr_t)(snp->snp_buf + snp->snp_base);
267 error = uiomove(from, (size_t)len, uio);
268 snp->snp_base += len;
271 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
272 snp->snp_flags &= ~SNOOP_OFLOW;
275 nblen = snp->snp_blen;
276 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
277 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
279 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
280 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
281 kfree(snp->snp_buf, M_SNP);
283 snp->snp_blen = nblen;
293 snp_in(struct snoop *snp, char *buf, int n)
300 KASSERT(n >= 0, ("negative snoop char count"));
305 if (snp->snp_flags & SNOOP_DOWN) {
306 kprintf("Snoop: more data to down interface.\n");
310 if (snp->snp_flags & SNOOP_OFLOW) {
311 kprintf("Snoop: buffer overflow.\n");
313 * On overflow we just repeat the standart close
314 * procedure...yes , this is waste of space but.. Then next
315 * read from device will fail if one would recall he is
316 * snooping and retry...
319 return (snp_down(snp));
321 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
322 s_free = snp->snp_blen - snp->snp_len;
327 nblen = snp->snp_blen;
328 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
329 nblen = snp->snp_blen * 2;
330 s_free = nblen - (snp->snp_len + snp->snp_base);
332 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
333 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
334 kfree(snp->snp_buf, M_SNP);
336 snp->snp_blen = nblen;
339 snp->snp_flags |= SNOOP_OFLOW;
340 if (snp->snp_flags & SNOOP_RWAIT) {
341 snp->snp_flags &= ~SNOOP_RWAIT;
342 wakeup((caddr_t)snp);
350 from = (caddr_t)(snp->snp_buf + snp->snp_base);
351 to = (caddr_t)(snp->snp_buf);
353 bcopy(from, to, len);
356 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
360 if (snp->snp_flags & SNOOP_RWAIT) {
361 snp->snp_flags &= ~SNOOP_RWAIT;
362 wakeup((caddr_t)snp);
364 selwakeup(&snp->snp_sel);
365 snp->snp_sel.si_pid = 0;
371 snpopen(struct dev_open_args *ap)
373 cdev_t dev = ap->a_head.a_dev;
376 if (dev->si_drv1 == NULL) {
378 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
379 0600, "snp%d", minor(dev));
381 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
388 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
389 * all previous settings (especially SNOOP_OFLOW) will be cleared.
391 snp->snp_flags = SNOOP_OPEN;
393 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
394 snp->snp_blen = SNOOP_MINLEN;
399 * snp_tty == NULL is for inactive snoop devices.
402 snp->snp_target = NULL;
404 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
410 snp_detach(struct snoop *snp)
418 * If line disc. changed we do not touch this pointer, SLIP/PPP will
425 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
426 tp->t_line == snooplinedisc) {
428 tp->t_state &= ~TS_SNOOP;
429 tp->t_line = snp->snp_olddisc;
431 kprintf("Snoop: bad attached tty data.\n");
434 snp->snp_target = NULL;
437 selwakeup(&snp->snp_sel);
438 snp->snp_sel.si_pid = 0;
439 if ((snp->snp_flags & SNOOP_OPEN) == 0)
446 snpclose(struct dev_close_args *ap)
448 cdev_t dev = ap->a_head.a_dev;
453 LIST_REMOVE(snp, snp_list);
454 kfree(snp->snp_buf, M_SNP);
455 snp->snp_flags &= ~SNOOP_OPEN;
457 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
458 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
461 return (snp_detach(snp));
465 snp_down(struct snoop *snp)
468 if (snp->snp_blen != SNOOP_MINLEN) {
469 kfree(snp->snp_buf, M_SNP);
470 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
471 snp->snp_blen = SNOOP_MINLEN;
473 snp->snp_flags |= SNOOP_DOWN;
475 return (snp_detach(snp));
479 snpioctl(struct dev_ioctl_args *ap)
481 cdev_t dev = ap->a_head.a_dev;
483 struct tty *tp, *tpo;
489 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
491 return (snp_down(snp));
493 tp = snpdevtotty(tdev);
496 if (tp->t_state & TS_SNOOP)
501 if (snp->snp_target == NULL) {
504 tpo->t_state &= ~TS_SNOOP;
507 tp->t_sc = (caddr_t)snp;
508 tp->t_state |= TS_SNOOP;
509 snp->snp_olddisc = tp->t_line;
510 tp->t_line = snooplinedisc;
512 snp->snp_target = tdev;
515 * Clean overflow and down flags -
516 * we'll have a chance to get them in the future :)))
518 snp->snp_flags &= ~SNOOP_OFLOW;
519 snp->snp_flags &= ~SNOOP_DOWN;
525 * We keep snp_target field specially to make
526 * SNPGTTY happy, else we can't know what is device
527 * major/minor for tty.
529 *((cdev_t *)ap->a_data) = snp->snp_target;
533 if (*(int *)ap->a_data)
534 snp->snp_flags |= SNOOP_ASYNC;
536 snp->snp_flags &= ~SNOOP_ASYNC;
541 if (snp->snp_tty != NULL)
542 *(int *)ap->a_data = snp->snp_len;
544 if (snp->snp_flags & SNOOP_DOWN) {
545 if (snp->snp_flags & SNOOP_OFLOW)
546 *(int *)ap->a_data = SNP_OFLOW;
548 *(int *)ap->a_data = SNP_TTYCLOSE;
550 *(int *)ap->a_data = SNP_DETACH;
562 snppoll(struct dev_poll_args *ap)
564 cdev_t dev = ap->a_head.a_dev;
571 * If snoop is down, we don't want to poll() forever so we return 1.
572 * Caller should see if we down via FIONREAD ioctl(). The last should
573 * return -1 to indicate down state.
575 if (ap->a_events & (POLLIN | POLLRDNORM)) {
576 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
577 revents |= ap->a_events & (POLLIN | POLLRDNORM);
579 selrecord(curthread, &snp->snp_sel);
581 ap->a_events = revents;
586 snpclone(struct dev_clone_args *ap)
589 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
590 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
597 snp_modevent(module_t mod, int type, void *data)
603 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
604 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
605 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
607 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
608 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
609 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
613 if (!LIST_EMPTY(&snp_sclist))
615 ldisc_deregister(snooplinedisc);
616 devfs_clone_handler_del("snp");
617 dev_ops_remove_all(&snp_ops);
618 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
626 static moduledata_t snp_mod = {
631 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);