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>
33 static l_close_t snplclose;
34 static l_write_t snplwrite;
35 static d_open_t snpopen;
36 static d_close_t snpclose;
37 static d_read_t snpread;
38 static d_write_t snpwrite;
39 static d_ioctl_t snpioctl;
40 static d_poll_t snppoll;
43 static struct dev_ops snp_ops = {
44 { "snp", CDEV_MAJOR, 0 },
53 static struct linesw snpdisc = {
54 ttyopen, snplclose, ttread, snplwrite,
55 l_nullioctl, ttyinput, ttstart, ttymodem
59 * This is the main snoop per-device structure.
62 LIST_ENTRY(snoop) snp_list; /* List glue. */
63 cdev_t snp_target; /* Target tty device. */
64 struct tty *snp_tty; /* Target tty pointer. */
65 u_long snp_len; /* Possible length. */
66 u_long snp_base; /* Data base. */
67 u_long snp_blen; /* Used length. */
68 caddr_t snp_buf; /* Allocation pointer. */
69 int snp_flags; /* Flags. */
70 struct selinfo snp_sel; /* Select info. */
71 int snp_olddisc; /* Old line discipline. */
77 #define SNOOP_ASYNC 0x0002
78 #define SNOOP_OPEN 0x0004
79 #define SNOOP_RWAIT 0x0008
80 #define SNOOP_OFLOW 0x0010
81 #define SNOOP_DOWN 0x0020
86 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
87 * 4K tested to be the minimum
88 * for which on normal tty
89 * usage there is no need to
92 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
93 * If we grow more,something
94 * really bad in this world..
97 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
99 * The number of the "snoop" line discipline. This gets determined at
102 static int snooplinedisc;
105 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
107 static struct tty *snpdevtotty (cdev_t dev);
108 static int snp_detach (struct snoop *snp);
109 static int snp_down (struct snoop *snp);
110 static int snp_in (struct snoop *snp, char *buf, int n);
111 static int snp_modevent (module_t mod, int what, void *arg);
114 snplclose(struct tty *tp, int flag)
120 error = snp_down(snp);
123 error = ttylclose(tp, flag);
128 snplwrite(struct tty *tp, struct uio *uio, int flag)
139 while (uio->uio_resid > 0) {
140 ilen = imin(512, uio->uio_resid);
141 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
142 error = uiomove(ibuf, ilen, uio);
145 snp_in(snp, ibuf, ilen);
146 /* Hackish, but probably the least of all evils. */
152 uio2.uio_resid = ilen;
153 uio2.uio_segflg = UIO_SYSSPACE;
154 uio2.uio_rw = UIO_WRITE;
155 uio2.uio_td = uio->uio_td;
156 error = ttwrite(tp, &uio2, flag);
168 snpdevtotty(cdev_t dev)
170 if ((dev_dflags(dev) & D_TTY) == 0)
172 return (dev->si_tty);
175 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
176 * interactive mode write is 3 bytes
177 * length for function keys...
181 snpwrite(struct dev_write_args *ap)
183 cdev_t dev = ap->a_head.a_dev;
184 struct uio *uio = ap->a_uio;
188 unsigned char c[SNP_INPUT_BUF];
194 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
195 tp->t_line == snooplinedisc)
198 kprintf("Snoop: attempt to write to bad tty.\n");
202 if (!(tp->t_state & TS_ISOPEN))
205 while (uio->uio_resid > 0) {
206 len = imin(uio->uio_resid, SNP_INPUT_BUF);
207 if ((error = uiomove(c, len, uio)) != 0)
209 for (i=0; i < len; i++) {
210 if (ttyinput(c[i], tp))
219 snpread(struct dev_read_args *ap)
221 cdev_t dev = ap->a_head.a_dev;
222 struct uio *uio = ap->a_uio;
224 int error, len, n, nblen;
229 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
230 ("snoop buffer error"));
232 if (snp->snp_tty == NULL)
235 snp->snp_flags &= ~SNOOP_RWAIT;
238 if (snp->snp_len == 0) {
239 if (ap->a_ioflag & IO_NDELAY)
240 return (EWOULDBLOCK);
241 snp->snp_flags |= SNOOP_RWAIT;
242 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
246 } while (snp->snp_len == 0);
251 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
252 len = min((unsigned)uio->uio_resid, snp->snp_len);
253 from = (caddr_t)(snp->snp_buf + snp->snp_base);
257 error = uiomove(from, len, uio);
258 snp->snp_base += len;
261 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
262 snp->snp_flags &= ~SNOOP_OFLOW;
265 nblen = snp->snp_blen;
266 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
267 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
269 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
270 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
271 kfree(snp->snp_buf, M_SNP);
273 snp->snp_blen = nblen;
283 snp_in(struct snoop *snp, char *buf, int n)
290 KASSERT(n >= 0, ("negative snoop char count"));
295 if (snp->snp_flags & SNOOP_DOWN) {
296 kprintf("Snoop: more data to down interface.\n");
300 if (snp->snp_flags & SNOOP_OFLOW) {
301 kprintf("Snoop: buffer overflow.\n");
303 * On overflow we just repeat the standart close
304 * procedure...yes , this is waste of space but.. Then next
305 * read from device will fail if one would recall he is
306 * snooping and retry...
309 return (snp_down(snp));
311 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
312 s_free = snp->snp_blen - snp->snp_len;
317 nblen = snp->snp_blen;
318 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
319 nblen = snp->snp_blen * 2;
320 s_free = nblen - (snp->snp_len + snp->snp_base);
322 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
323 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
324 kfree(snp->snp_buf, M_SNP);
326 snp->snp_blen = nblen;
329 snp->snp_flags |= SNOOP_OFLOW;
330 if (snp->snp_flags & SNOOP_RWAIT) {
331 snp->snp_flags &= ~SNOOP_RWAIT;
332 wakeup((caddr_t)snp);
340 from = (caddr_t)(snp->snp_buf + snp->snp_base);
341 to = (caddr_t)(snp->snp_buf);
343 bcopy(from, to, len);
346 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
350 if (snp->snp_flags & SNOOP_RWAIT) {
351 snp->snp_flags &= ~SNOOP_RWAIT;
352 wakeup((caddr_t)snp);
354 selwakeup(&snp->snp_sel);
355 snp->snp_sel.si_pid = 0;
361 snpopen(struct dev_open_args *ap)
363 cdev_t dev = ap->a_head.a_dev;
366 if (dev->si_drv1 == NULL) {
367 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
368 0600, "snp%d", minor(dev));
369 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
376 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
377 * all previous settings (especially SNOOP_OFLOW) will be cleared.
379 snp->snp_flags = SNOOP_OPEN;
381 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
382 snp->snp_blen = SNOOP_MINLEN;
387 * snp_tty == NULL is for inactive snoop devices.
390 snp->snp_target = NULL;
392 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
398 snp_detach(struct snoop *snp)
406 * If line disc. changed we do not touch this pointer, SLIP/PPP will
413 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
414 tp->t_line == snooplinedisc) {
416 tp->t_state &= ~TS_SNOOP;
417 tp->t_line = snp->snp_olddisc;
419 kprintf("Snoop: bad attached tty data.\n");
422 snp->snp_target = NULL;
425 selwakeup(&snp->snp_sel);
426 snp->snp_sel.si_pid = 0;
427 if ((snp->snp_flags & SNOOP_OPEN) == 0)
434 snpclose(struct dev_close_args *ap)
436 cdev_t dev = ap->a_head.a_dev;
441 LIST_REMOVE(snp, snp_list);
442 kfree(snp->snp_buf, M_SNP);
443 snp->snp_flags &= ~SNOOP_OPEN;
446 return (snp_detach(snp));
450 snp_down(struct snoop *snp)
453 if (snp->snp_blen != SNOOP_MINLEN) {
454 kfree(snp->snp_buf, M_SNP);
455 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
456 snp->snp_blen = SNOOP_MINLEN;
458 snp->snp_flags |= SNOOP_DOWN;
460 return (snp_detach(snp));
464 snpioctl(struct dev_ioctl_args *ap)
466 cdev_t dev = ap->a_head.a_dev;
468 struct tty *tp, *tpo;
474 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
476 return (snp_down(snp));
478 tp = snpdevtotty(tdev);
481 if (tp->t_state & TS_SNOOP)
486 if (snp->snp_target == NULL) {
489 tpo->t_state &= ~TS_SNOOP;
492 tp->t_sc = (caddr_t)snp;
493 tp->t_state |= TS_SNOOP;
494 snp->snp_olddisc = tp->t_line;
495 tp->t_line = snooplinedisc;
497 snp->snp_target = tdev;
500 * Clean overflow and down flags -
501 * we'll have a chance to get them in the future :)))
503 snp->snp_flags &= ~SNOOP_OFLOW;
504 snp->snp_flags &= ~SNOOP_DOWN;
510 * We keep snp_target field specially to make
511 * SNPGTTY happy, else we can't know what is device
512 * major/minor for tty.
514 *((cdev_t *)ap->a_data) = snp->snp_target;
518 if (*(int *)ap->a_data)
519 snp->snp_flags |= SNOOP_ASYNC;
521 snp->snp_flags &= ~SNOOP_ASYNC;
526 if (snp->snp_tty != NULL)
527 *(int *)ap->a_data = snp->snp_len;
529 if (snp->snp_flags & SNOOP_DOWN) {
530 if (snp->snp_flags & SNOOP_OFLOW)
531 *(int *)ap->a_data = SNP_OFLOW;
533 *(int *)ap->a_data = SNP_TTYCLOSE;
535 *(int *)ap->a_data = SNP_DETACH;
547 snppoll(struct dev_poll_args *ap)
549 cdev_t dev = ap->a_head.a_dev;
556 * If snoop is down, we don't want to poll() forever so we return 1.
557 * Caller should see if we down via FIONREAD ioctl(). The last should
558 * return -1 to indicate down state.
560 if (ap->a_events & (POLLIN | POLLRDNORM)) {
561 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
562 revents |= ap->a_events & (POLLIN | POLLRDNORM);
564 selrecord(curthread, &snp->snp_sel);
566 ap->a_events = revents;
571 snp_modevent(module_t mod, int type, void *data)
576 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
577 dev_ops_add(&snp_ops, 0, 0);
580 if (!LIST_EMPTY(&snp_sclist))
582 ldisc_deregister(snooplinedisc);
583 dev_ops_remove(&snp_ops, 0, 0);
591 static moduledata_t snp_mod = {
596 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);