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.13 2006/06/13 08:12:02 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 cdevsw snp_cdevsw = {
57 /* strategy */ nostrategy,
62 static struct linesw snpdisc = {
63 ttyopen, snplclose, ttread, snplwrite,
64 l_nullioctl, ttyinput, ttstart, ttymodem
68 * This is the main snoop per-device structure.
71 LIST_ENTRY(snoop) snp_list; /* List glue. */
72 dev_t snp_target; /* Target tty device. */
73 struct tty *snp_tty; /* Target tty pointer. */
74 u_long snp_len; /* Possible length. */
75 u_long snp_base; /* Data base. */
76 u_long snp_blen; /* Used length. */
77 caddr_t snp_buf; /* Allocation pointer. */
78 int snp_flags; /* Flags. */
79 struct selinfo snp_sel; /* Select info. */
80 int snp_olddisc; /* Old line discipline. */
86 #define SNOOP_ASYNC 0x0002
87 #define SNOOP_OPEN 0x0004
88 #define SNOOP_RWAIT 0x0008
89 #define SNOOP_OFLOW 0x0010
90 #define SNOOP_DOWN 0x0020
95 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
96 * 4K tested to be the minimum
97 * for which on normal tty
98 * usage there is no need to
101 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
102 * If we grow more,something
103 * really bad in this world..
106 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
108 * The number of the "snoop" line discipline. This gets determined at
111 static int snooplinedisc;
114 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
116 static struct tty *snpdevtotty (dev_t dev);
117 static int snp_detach (struct snoop *snp);
118 static int snp_down (struct snoop *snp);
119 static int snp_in (struct snoop *snp, char *buf, int n);
120 static int snp_modevent (module_t mod, int what, void *arg);
123 snplclose(struct tty *tp, int flag)
129 error = snp_down(snp);
132 error = ttylclose(tp, flag);
137 snplwrite(struct tty *tp, struct uio *uio, int flag)
148 while (uio->uio_resid > 0) {
149 ilen = imin(512, uio->uio_resid);
150 ibuf = malloc(ilen, M_SNP, M_WAITOK);
151 error = uiomove(ibuf, ilen, uio);
154 snp_in(snp, ibuf, ilen);
155 /* Hackish, but probably the least of all evils. */
161 uio2.uio_resid = ilen;
162 uio2.uio_segflg = UIO_SYSSPACE;
163 uio2.uio_rw = UIO_WRITE;
164 uio2.uio_td = uio->uio_td;
165 error = ttwrite(tp, &uio2, flag);
177 snpdevtotty(dev_t dev)
179 if ((dev_dflags(dev) & D_TTY) == 0)
181 return (dev->si_tty);
184 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
185 * interactive mode write is 3 bytes
186 * length for function keys...
190 snpwrite(dev_t dev, struct uio *uio, int flag)
195 unsigned char c[SNP_INPUT_BUF];
201 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
202 tp->t_line == snooplinedisc)
205 printf("Snoop: attempt to write to bad tty.\n");
209 if (!(tp->t_state & TS_ISOPEN))
212 while (uio->uio_resid > 0) {
213 len = imin(uio->uio_resid, SNP_INPUT_BUF);
214 if ((error = uiomove(c, len, uio)) != 0)
216 for (i=0; i < len; i++) {
217 if (ttyinput(c[i], tp))
226 snpread(dev_t dev, struct uio *uio, int flag)
229 int error, len, n, nblen;
234 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
235 ("snoop buffer error"));
237 if (snp->snp_tty == NULL)
240 snp->snp_flags &= ~SNOOP_RWAIT;
243 if (snp->snp_len == 0) {
244 if (flag & IO_NDELAY)
245 return (EWOULDBLOCK);
246 snp->snp_flags |= SNOOP_RWAIT;
247 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
251 } while (snp->snp_len == 0);
256 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
257 len = min((unsigned)uio->uio_resid, snp->snp_len);
258 from = (caddr_t)(snp->snp_buf + snp->snp_base);
262 error = uiomove(from, len, uio);
263 snp->snp_base += len;
266 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
267 snp->snp_flags &= ~SNOOP_OFLOW;
270 nblen = snp->snp_blen;
271 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
272 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
274 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
275 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
276 free(snp->snp_buf, M_SNP);
278 snp->snp_blen = nblen;
288 snp_in(struct snoop *snp, char *buf, int n)
295 KASSERT(n >= 0, ("negative snoop char count"));
300 if (snp->snp_flags & SNOOP_DOWN) {
301 printf("Snoop: more data to down interface.\n");
305 if (snp->snp_flags & SNOOP_OFLOW) {
306 printf("Snoop: buffer overflow.\n");
308 * On overflow we just repeat the standart close
309 * procedure...yes , this is waste of space but.. Then next
310 * read from device will fail if one would recall he is
311 * snooping and retry...
314 return (snp_down(snp));
316 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
317 s_free = snp->snp_blen - snp->snp_len;
322 nblen = snp->snp_blen;
323 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
324 nblen = snp->snp_blen * 2;
325 s_free = nblen - (snp->snp_len + snp->snp_base);
327 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
328 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
329 free(snp->snp_buf, M_SNP);
331 snp->snp_blen = nblen;
334 snp->snp_flags |= SNOOP_OFLOW;
335 if (snp->snp_flags & SNOOP_RWAIT) {
336 snp->snp_flags &= ~SNOOP_RWAIT;
337 wakeup((caddr_t)snp);
345 from = (caddr_t)(snp->snp_buf + snp->snp_base);
346 to = (caddr_t)(snp->snp_buf);
348 bcopy(from, to, len);
351 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
355 if (snp->snp_flags & SNOOP_RWAIT) {
356 snp->snp_flags &= ~SNOOP_RWAIT;
357 wakeup((caddr_t)snp);
359 selwakeup(&snp->snp_sel);
360 snp->snp_sel.si_pid = 0;
366 snpopen(dev_t dev, int flag, int mode, d_thread_t *td)
370 if (dev->si_drv1 == NULL) {
371 make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
372 0600, "snp%d", minor(dev));
373 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
380 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
381 * all previous settings (especially SNOOP_OFLOW) will be cleared.
383 snp->snp_flags = SNOOP_OPEN;
385 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
386 snp->snp_blen = SNOOP_MINLEN;
391 * snp_tty == NULL is for inactive snoop devices.
394 snp->snp_target = NODEV;
396 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
402 snp_detach(struct snoop *snp)
410 * If line disc. changed we do not touch this pointer, SLIP/PPP will
417 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
418 tp->t_line == snooplinedisc) {
420 tp->t_state &= ~TS_SNOOP;
421 tp->t_line = snp->snp_olddisc;
423 printf("Snoop: bad attached tty data.\n");
426 snp->snp_target = NODEV;
429 selwakeup(&snp->snp_sel);
430 snp->snp_sel.si_pid = 0;
431 if ((snp->snp_flags & SNOOP_OPEN) == 0)
438 snpclose(dev_t dev, int flags, int fmt, d_thread_t *td)
444 LIST_REMOVE(snp, snp_list);
445 free(snp->snp_buf, M_SNP);
446 snp->snp_flags &= ~SNOOP_OPEN;
449 return (snp_detach(snp));
453 snp_down(struct snoop *snp)
456 if (snp->snp_blen != SNOOP_MINLEN) {
457 free(snp->snp_buf, M_SNP);
458 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
459 snp->snp_blen = SNOOP_MINLEN;
461 snp->snp_flags |= SNOOP_DOWN;
463 return (snp_detach(snp));
467 snpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, d_thread_t *td)
470 struct tty *tp, *tpo;
476 tdev = udev2dev(*((udev_t *)data), 0);
478 return (snp_down(snp));
480 tp = snpdevtotty(tdev);
483 if (tp->t_state & TS_SNOOP)
488 if (snp->snp_target == NODEV) {
491 tpo->t_state &= ~TS_SNOOP;
494 tp->t_sc = (caddr_t)snp;
495 tp->t_state |= TS_SNOOP;
496 snp->snp_olddisc = tp->t_line;
497 tp->t_line = snooplinedisc;
499 snp->snp_target = tdev;
502 * Clean overflow and down flags -
503 * we'll have a chance to get them in the future :)))
505 snp->snp_flags &= ~SNOOP_OFLOW;
506 snp->snp_flags &= ~SNOOP_DOWN;
512 * We keep snp_target field specially to make
513 * SNPGTTY happy, else we can't know what is device
514 * major/minor for tty.
516 *((dev_t *)data) = snp->snp_target;
521 snp->snp_flags |= SNOOP_ASYNC;
523 snp->snp_flags &= ~SNOOP_ASYNC;
528 if (snp->snp_tty != NULL)
529 *(int *)data = snp->snp_len;
531 if (snp->snp_flags & SNOOP_DOWN) {
532 if (snp->snp_flags & SNOOP_OFLOW)
533 *(int *)data = SNP_OFLOW;
535 *(int *)data = SNP_TTYCLOSE;
537 *(int *)data = SNP_DETACH;
549 snppoll(dev_t dev, int events, d_thread_t *td)
557 * If snoop is down, we don't want to poll() forever so we return 1.
558 * Caller should see if we down via FIONREAD ioctl(). The last should
559 * return -1 to indicate down state.
561 if (events & (POLLIN | POLLRDNORM)) {
562 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
563 revents |= events & (POLLIN | POLLRDNORM);
565 selrecord(td, &snp->snp_sel);
571 snp_modevent(module_t mod, int type, void *data)
576 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
577 cdevsw_add(&snp_cdevsw, 0, 0);
580 if (!LIST_EMPTY(&snp_sclist))
582 ldisc_deregister(snooplinedisc);
583 cdevsw_remove(&snp_cdevsw, 0, 0);
591 static moduledata_t snp_mod = {
596 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);