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.9 2004/05/13 23:49:17 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/vnode.h>
30 #include <sys/device.h>
32 static l_close_t snplclose;
33 static l_write_t snplwrite;
34 static d_open_t snpopen;
35 static d_close_t snpclose;
36 static d_read_t snpread;
37 static d_write_t snpwrite;
38 static d_ioctl_t snpioctl;
39 static d_poll_t snppoll;
42 static struct cdevsw snp_cdevsw = {
56 /* strategy */ nostrategy,
61 static struct linesw snpdisc = {
62 ttyopen, snplclose, ttread, snplwrite,
63 l_nullioctl, ttyinput, ttstart, ttymodem
67 * This is the main snoop per-device structure.
70 LIST_ENTRY(snoop) snp_list; /* List glue. */
71 dev_t snp_target; /* Target tty device. */
72 struct tty *snp_tty; /* Target tty pointer. */
73 u_long snp_len; /* Possible length. */
74 u_long snp_base; /* Data base. */
75 u_long snp_blen; /* Used length. */
76 caddr_t snp_buf; /* Allocation pointer. */
77 int snp_flags; /* Flags. */
78 struct selinfo snp_sel; /* Select info. */
79 int snp_olddisc; /* Old line discipline. */
85 #define SNOOP_ASYNC 0x0002
86 #define SNOOP_OPEN 0x0004
87 #define SNOOP_RWAIT 0x0008
88 #define SNOOP_OFLOW 0x0010
89 #define SNOOP_DOWN 0x0020
94 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
95 * 4K tested to be the minimum
96 * for which on normal tty
97 * usage there is no need to
100 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
101 * If we grow more,something
102 * really bad in this world..
105 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
107 * The number of the "snoop" line discipline. This gets determined at
110 static int snooplinedisc;
113 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
115 static struct tty *snpdevtotty (dev_t dev);
116 static int snp_detach (struct snoop *snp);
117 static int snp_down (struct snoop *snp);
118 static int snp_in (struct snoop *snp, char *buf, int n);
119 static int snp_modevent (module_t mod, int what, void *arg);
130 error = snp_down(snp);
133 error = ttylclose(tp, flag);
138 snplwrite(tp, uio, flag)
152 while (uio->uio_resid > 0) {
153 ilen = imin(512, uio->uio_resid);
154 ibuf = malloc(ilen, M_SNP, M_WAITOK);
155 error = uiomove(ibuf, ilen, uio);
158 snp_in(snp, ibuf, ilen);
159 /* Hackish, but probably the least of all evils. */
165 uio2.uio_resid = ilen;
166 uio2.uio_segflg = UIO_SYSSPACE;
167 uio2.uio_rw = UIO_WRITE;
168 uio2.uio_td = uio->uio_td;
169 error = ttwrite(tp, &uio2, flag);
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(dev, uio, flag)
203 unsigned char c[SNP_INPUT_BUF];
209 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
210 tp->t_line == snooplinedisc)
213 printf("Snoop: attempt to write to bad tty.\n");
217 if (!(tp->t_state & TS_ISOPEN))
220 while (uio->uio_resid > 0) {
221 len = imin(uio->uio_resid, SNP_INPUT_BUF);
222 if ((error = uiomove(c, len, uio)) != 0)
224 for (i=0; i < len; i++) {
225 if (ttyinput(c[i], tp))
234 snpread(dev, uio, flag)
240 int error, len, n, nblen, s;
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 (flag & 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 = min((unsigned)uio->uio_resid, snp->snp_len);
269 from = (caddr_t)(snp->snp_buf + snp->snp_base);
273 error = uiomove(from, 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 = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
286 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
287 free(snp->snp_buf, M_SNP);
289 snp->snp_blen = nblen;
309 KASSERT(n >= 0, ("negative snoop char count"));
314 if (snp->snp_flags & SNOOP_DOWN) {
315 printf("Snoop: more data to down interface.\n");
319 if (snp->snp_flags & SNOOP_OFLOW) {
320 printf("Snoop: buffer overflow.\n");
322 * On overflow we just repeat the standart close
323 * procedure...yes , this is waste of space but.. Then next
324 * read from device will fail if one would recall he is
325 * snooping and retry...
328 return (snp_down(snp));
330 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
331 s_free = snp->snp_blen - snp->snp_len;
336 nblen = snp->snp_blen;
337 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
338 nblen = snp->snp_blen * 2;
339 s_free = nblen - (snp->snp_len + snp->snp_base);
341 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
342 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
343 free(snp->snp_buf, M_SNP);
345 snp->snp_blen = nblen;
348 snp->snp_flags |= SNOOP_OFLOW;
349 if (snp->snp_flags & SNOOP_RWAIT) {
350 snp->snp_flags &= ~SNOOP_RWAIT;
351 wakeup((caddr_t)snp);
359 from = (caddr_t)(snp->snp_buf + snp->snp_base);
360 to = (caddr_t)(snp->snp_buf);
362 bcopy(from, to, len);
365 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
369 if (snp->snp_flags & SNOOP_RWAIT) {
370 snp->snp_flags &= ~SNOOP_RWAIT;
371 wakeup((caddr_t)snp);
373 selwakeup(&snp->snp_sel);
374 snp->snp_sel.si_pid = 0;
380 snpopen(dev_t dev, int flag, int mode, d_thread_t *td)
384 if (dev->si_drv1 == NULL) {
385 make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
386 0600, "snp%d", minor(dev));
387 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
393 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
394 * all previous settings (especially SNOOP_OFLOW) will be cleared.
396 snp->snp_flags = SNOOP_OPEN;
398 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
399 snp->snp_blen = SNOOP_MINLEN;
404 * snp_tty == NULL is for inactive snoop devices.
407 snp->snp_target = NODEV;
409 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
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 printf("Snoop: bad attached tty data.\n");
440 snp->snp_target = NODEV;
443 selwakeup(&snp->snp_sel);
444 snp->snp_sel.si_pid = 0;
445 if ((snp->snp_flags & SNOOP_OPEN) == 0)
452 snpclose(dev_t dev, int flags, int fmt, d_thread_t *td)
458 LIST_REMOVE(snp, snp_list);
459 free(snp->snp_buf, M_SNP);
460 snp->snp_flags &= ~SNOOP_OPEN;
463 return (snp_detach(snp));
471 if (snp->snp_blen != SNOOP_MINLEN) {
472 free(snp->snp_buf, M_SNP);
473 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
474 snp->snp_blen = SNOOP_MINLEN;
476 snp->snp_flags |= SNOOP_DOWN;
478 return (snp_detach(snp));
482 snpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, d_thread_t *td)
485 struct tty *tp, *tpo;
492 tdev = udev2dev(*((udev_t *)data), 0);
494 return (snp_down(snp));
496 tp = snpdevtotty(tdev);
499 if (tp->t_state & TS_SNOOP)
504 if (snp->snp_target == NODEV) {
507 tpo->t_state &= ~TS_SNOOP;
510 tp->t_sc = (caddr_t)snp;
511 tp->t_state |= TS_SNOOP;
512 snp->snp_olddisc = tp->t_line;
513 tp->t_line = snooplinedisc;
515 snp->snp_target = tdev;
518 * Clean overflow and down flags -
519 * we'll have a chance to get them in the future :)))
521 snp->snp_flags &= ~SNOOP_OFLOW;
522 snp->snp_flags &= ~SNOOP_DOWN;
528 * We keep snp_target field specially to make
529 * SNPGTTY happy, else we can't know what is device
530 * major/minor for tty.
532 *((dev_t *)data) = snp->snp_target;
540 snp->snp_flags |= SNOOP_ASYNC;
542 snp->snp_flags &= ~SNOOP_ASYNC;
547 if (snp->snp_tty != NULL)
548 *(int *)data = snp->snp_len;
550 if (snp->snp_flags & SNOOP_DOWN) {
551 if (snp->snp_flags & SNOOP_OFLOW)
552 *(int *)data = SNP_OFLOW;
554 *(int *)data = SNP_TTYCLOSE;
556 *(int *)data = SNP_DETACH;
568 snppoll(dev_t dev, int events, d_thread_t *td)
576 * If snoop is down, we don't want to poll() forever so we return 1.
577 * Caller should see if we down via FIONREAD ioctl(). The last should
578 * return -1 to indicate down state.
580 if (events & (POLLIN | POLLRDNORM)) {
581 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
582 revents |= events & (POLLIN | POLLRDNORM);
584 selrecord(td, &snp->snp_sel);
590 snp_modevent(mod, type, data)
598 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
599 cdevsw_add(&snp_cdevsw);
602 if (!LIST_EMPTY(&snp_sclist))
604 ldisc_deregister(snooplinedisc);
605 cdevsw_remove(&snp_cdevsw);
613 static moduledata_t snp_mod = {
618 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);