Deleted Added
sdiff udiff text old ( 130585 ) new ( 130640 )
full compact
1/*
2 * Copyright (c) 1995 Ugen J.S.Antsilevich
3 *
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
6 *
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.
10 *
11 * This software is provided ``AS IS'' without any warranties of any kind.
12 *
13 * Snoop stuff.
14 *
15 */
16
17#include <sys/cdefs.h>
18__FBSDID("$FreeBSD: head/sys/dev/snp/snp.c 130640 2004-06-17 17:16:53Z phk $");
19
20#include <sys/param.h>
21#include <sys/systm.h>
22#include <sys/filio.h>
23#include <sys/malloc.h>
24#include <sys/tty.h>
25#include <sys/conf.h>
26#include <sys/poll.h>
27#include <sys/kernel.h>
28#include <sys/module.h>
29#include <sys/queue.h>
30#include <sys/snoop.h>
31#include <sys/vnode.h>
32
33static l_close_t snplclose;
34static l_write_t snplwrite;
35static d_open_t snpopen;
36static d_close_t snpclose;
37static d_read_t snpread;
38static d_write_t snpwrite;
39static d_ioctl_t snpioctl;
40static d_poll_t snppoll;
41
42static struct cdevsw snp_cdevsw = {
43 .d_version = D_VERSION,
44 .d_flags = D_PSEUDO | D_NEEDGIANT,
45 .d_open = snpopen,
46 .d_close = snpclose,
47 .d_read = snpread,
48 .d_write = snpwrite,
49 .d_ioctl = snpioctl,
50 .d_poll = snppoll,
51 .d_name = "snp",
52};
53
54static struct linesw snpdisc = {
55 ttyopen, snplclose, ttread, snplwrite,
56 l_nullioctl, ttyinput, ttstart, ttymodem
57};
58
59/*
60 * This is the main snoop per-device structure.
61 */
62struct snoop {
63 LIST_ENTRY(snoop) snp_list; /* List glue. */
64 int snp_unit; /* Device number. */
65 struct cdev *snp_target; /* Target tty device. */
66 struct tty *snp_tty; /* Target tty pointer. */
67 u_long snp_len; /* Possible length. */
68 u_long snp_base; /* Data base. */
69 u_long snp_blen; /* Used length. */
70 caddr_t snp_buf; /* Allocation pointer. */
71 int snp_flags; /* Flags. */
72 struct selinfo snp_sel; /* Select info. */
73 int snp_olddisc; /* Old line discipline. */
74};
75
76/*
77 * Possible flags.
78 */
79#define SNOOP_ASYNC 0x0002
80#define SNOOP_OPEN 0x0004
81#define SNOOP_RWAIT 0x0008
82#define SNOOP_OFLOW 0x0010
83#define SNOOP_DOWN 0x0020
84
85/*
86 * Other constants.
87 */
88#define SNOOP_MINLEN (4*1024) /* This should be power of 2.
89 * 4K tested to be the minimum
90 * for which on normal tty
91 * usage there is no need to
92 * allocate more.
93 */
94#define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
95 * If we grow more,something
96 * really bad in this world..
97 */
98
99static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
100/*
101 * The number of the "snoop" line discipline. This gets determined at
102 * module load time.
103 */
104static int snooplinedisc;
105
106static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
107static struct clonedevs *snpclones;
108
109static struct tty *snpdevtotty(struct cdev *dev);
110static void snp_clone(void *arg, char *name,
111 int namelen, struct cdev **dev);
112static int snp_detach(struct snoop *snp);
113static int snp_down(struct snoop *snp);
114static int snp_in(struct snoop *snp, char *buf, int n);
115static int snp_modevent(module_t mod, int what, void *arg);
116
117static int
118snplclose(tp, flag)
119 struct tty *tp;
120 int flag;
121{
122 struct snoop *snp;
123 int error;
124
125 snp = tp->t_sc;
126 error = snp_down(snp);
127 if (error != 0)
128 return (error);
129 error = ttylclose(tp, flag);
130 return (error);
131}
132
133static int
134snplwrite(tp, uio, flag)
135 struct tty *tp;
136 struct uio *uio;
137 int flag;
138{
139 struct iovec iov;
140 struct uio uio2;
141 struct snoop *snp;
142 int error, ilen;
143 char *ibuf;
144
145 error = 0;
146 ibuf = NULL;
147 snp = tp->t_sc;
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);
152 if (error != 0)
153 break;
154 snp_in(snp, ibuf, ilen);
155 /* Hackish, but probably the least of all evils. */
156 iov.iov_base = ibuf;
157 iov.iov_len = ilen;
158 uio2.uio_iov = &iov;
159 uio2.uio_iovcnt = 1;
160 uio2.uio_offset = 0;
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);
166 if (error != 0)
167 break;
168 free(ibuf, M_SNP);
169 ibuf = NULL;
170 }
171 if (ibuf != NULL)
172 free(ibuf, M_SNP);
173 return (error);
174}
175
176static struct tty *
177snpdevtotty(dev)
178 struct cdev *dev;
179{
180 struct cdevsw *cdp;
181
182 cdp = devsw(dev);
183 if (cdp == NULL || (cdp->d_flags & D_TTY) == 0)
184 return (NULL);
185 return (dev->si_tty);
186}
187
188#define SNP_INPUT_BUF 5 /* This is even too much, the maximal
189 * interactive mode write is 3 bytes
190 * length for function keys...
191 */
192
193static int
194snpwrite(dev, uio, flag)
195 struct cdev *dev;
196 struct uio *uio;
197 int flag;
198{
199 struct snoop *snp;
200 struct tty *tp;
201 int error, i, len;
202 unsigned char c[SNP_INPUT_BUF];
203
204 snp = dev->si_drv1;
205 tp = snp->snp_tty;
206 if (tp == NULL)
207 return (EIO);
208 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
209 tp->t_line == snooplinedisc)
210 goto tty_input;
211
212 printf("snp%d: attempt to write to bad tty\n", snp->snp_unit);
213 return (EIO);
214
215tty_input:
216 if (!(tp->t_state & TS_ISOPEN))
217 return (EIO);
218
219 while (uio->uio_resid > 0) {
220 len = imin(uio->uio_resid, SNP_INPUT_BUF);
221 if ((error = uiomove(c, len, uio)) != 0)
222 return (error);
223 for (i=0; i < len; i++) {
224 if (ttyinput(c[i], tp))
225 return (EIO);
226 }
227 }
228 return (0);
229}
230
231
232static int
233snpread(dev, uio, flag)
234 struct cdev *dev;
235 struct uio *uio;
236 int flag;
237{
238 struct snoop *snp;
239 int error, len, n, nblen, s;
240 caddr_t from;
241 char *nbuf;
242
243 snp = dev->si_drv1;
244 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
245 ("snoop buffer error"));
246
247 if (snp->snp_tty == NULL)
248 return (EIO);
249
250 snp->snp_flags &= ~SNOOP_RWAIT;
251
252 do {
253 if (snp->snp_len == 0) {
254 if (flag & IO_NDELAY)
255 return (EWOULDBLOCK);
256 snp->snp_flags |= SNOOP_RWAIT;
257 error = tsleep(snp, (PZERO + 1) | PCATCH,
258 "snprd", 0);
259 if (error != 0)
260 return (error);
261 }
262 } while (snp->snp_len == 0);
263
264 n = snp->snp_len;
265
266 error = 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);
270 if (len == 0)
271 break;
272
273 error = uiomove(from, len, uio);
274 snp->snp_base += len;
275 snp->snp_len -= len;
276 }
277 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
278 snp->snp_flags &= ~SNOOP_OFLOW;
279 }
280 s = spltty();
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)
284 nblen = nblen / 2;
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);
288 snp->snp_buf = nbuf;
289 snp->snp_blen = nblen;
290 snp->snp_base = 0;
291 }
292 }
293 splx(s);
294
295 return (error);
296}
297
298static int
299snp_in(snp, buf, n)
300 struct snoop *snp;
301 char *buf;
302 int n;
303{
304 int s_free, s_tail;
305 int s, len, nblen;
306 caddr_t from, to;
307 char *nbuf;
308
309 KASSERT(n >= 0, ("negative snoop char count"));
310
311 if (n == 0)
312 return (0);
313
314 if (snp->snp_flags & SNOOP_DOWN) {
315 printf("snp%d: more data to down interface\n", snp->snp_unit);
316 return (0);
317 }
318
319 if (snp->snp_flags & SNOOP_OFLOW) {
320 printf("snp%d: buffer overflow\n", snp->snp_unit);
321 /*
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...
326 */
327
328 return (snp_down(snp));
329 }
330 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
331 s_free = snp->snp_blen - snp->snp_len;
332
333
334 if (n > s_free) {
335 s = spltty();
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);
340 }
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);
344 snp->snp_buf = nbuf;
345 snp->snp_blen = nblen;
346 snp->snp_base = 0;
347 } else {
348 snp->snp_flags |= SNOOP_OFLOW;
349 if (snp->snp_flags & SNOOP_RWAIT) {
350 snp->snp_flags &= ~SNOOP_RWAIT;
351 wakeup(snp);
352 }
353 splx(s);
354 return (0);
355 }
356 splx(s);
357 }
358 if (n > s_tail) {
359 from = (caddr_t)(snp->snp_buf + snp->snp_base);
360 to = (caddr_t)(snp->snp_buf);
361 len = snp->snp_len;
362 bcopy(from, to, len);
363 snp->snp_base = 0;
364 }
365 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
366 bcopy(buf, to, n);
367 snp->snp_len += n;
368
369 if (snp->snp_flags & SNOOP_RWAIT) {
370 snp->snp_flags &= ~SNOOP_RWAIT;
371 wakeup(snp);
372 }
373 selwakeuppri(&snp->snp_sel, PZERO + 1);
374
375 return (n);
376}
377
378static int
379snpopen(dev, flag, mode, td)
380 struct cdev *dev;
381 int flag, mode;
382 struct thread *td;
383{
384 struct snoop *snp;
385
386 if (dev->si_drv1 == NULL) {
387 dev->si_flags &= ~SI_CHEAPCLONE;
388 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
389 M_WAITOK | M_ZERO);
390 snp->snp_unit = dev2unit(dev);
391 } else
392 return (EBUSY);
393
394 /*
395 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
396 * all previous settings (especially SNOOP_OFLOW) will be cleared.
397 */
398 snp->snp_flags = SNOOP_OPEN;
399
400 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
401 snp->snp_blen = SNOOP_MINLEN;
402 snp->snp_base = 0;
403 snp->snp_len = 0;
404
405 /*
406 * snp_tty == NULL is for inactive snoop devices.
407 */
408 snp->snp_tty = NULL;
409 snp->snp_target = NULL;
410
411 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
412 return (0);
413}
414
415
416static int
417snp_detach(snp)
418 struct snoop *snp;
419{
420 struct tty *tp;
421
422 snp->snp_base = 0;
423 snp->snp_len = 0;
424
425 /*
426 * If line disc. changed we do not touch this pointer, SLIP/PPP will
427 * change it anyway.
428 */
429 tp = snp->snp_tty;
430 if (tp == NULL)
431 goto detach_notty;
432
433 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
434 tp->t_line == snooplinedisc) {
435 tp->t_sc = NULL;
436 tp->t_state &= ~TS_SNOOP;
437 tp->t_line = snp->snp_olddisc;
438 } else
439 printf("snp%d: bad attached tty data\n", snp->snp_unit);
440
441 snp->snp_tty = NULL;
442 snp->snp_target = NULL;
443
444detach_notty:
445 selwakeuppri(&snp->snp_sel, PZERO + 1);
446 if ((snp->snp_flags & SNOOP_OPEN) == 0)
447 free(snp, M_SNP);
448
449 return (0);
450}
451
452static int
453snpclose(dev, flags, fmt, td)
454 struct cdev *dev;
455 int flags;
456 int fmt;
457 struct thread *td;
458{
459 struct snoop *snp;
460
461 snp = dev->si_drv1;
462 snp->snp_blen = 0;
463 LIST_REMOVE(snp, snp_list);
464 free(snp->snp_buf, M_SNP);
465 snp->snp_flags &= ~SNOOP_OPEN;
466 dev->si_drv1 = NULL;
467 destroy_dev(dev);
468
469 return (snp_detach(snp));
470}
471
472static int
473snp_down(snp)
474 struct snoop *snp;
475{
476
477 if (snp->snp_blen != SNOOP_MINLEN) {
478 free(snp->snp_buf, M_SNP);
479 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
480 snp->snp_blen = SNOOP_MINLEN;
481 }
482 snp->snp_flags |= SNOOP_DOWN;
483
484 return (snp_detach(snp));
485}
486
487static int
488snpioctl(dev, cmd, data, flags, td)
489 struct cdev *dev;
490 u_long cmd;
491 caddr_t data;
492 int flags;
493 struct thread *td;
494{
495 struct snoop *snp;
496 struct tty *tp, *tpo;
497 struct cdev *tdev;
498 int s;
499
500 snp = dev->si_drv1;
501 switch (cmd) {
502 case SNPSTTY:
503 tdev = findcdev(*((dev_t *)data));
504 if (tdev == NULL)
505 return (snp_down(snp));
506
507 tp = snpdevtotty(tdev);
508 if (!tp)
509 return (EINVAL);
510 if (tp->t_state & TS_SNOOP)
511 return (EBUSY);
512
513 s = spltty();
514
515 if (snp->snp_target == NULL) {
516 tpo = snp->snp_tty;
517 if (tpo)
518 tpo->t_state &= ~TS_SNOOP;
519 }
520
521 tp->t_sc = (caddr_t)snp;
522 tp->t_state |= TS_SNOOP;
523 snp->snp_olddisc = tp->t_line;
524 tp->t_line = snooplinedisc;
525 snp->snp_tty = tp;
526 snp->snp_target = tdev;
527
528 /*
529 * Clean overflow and down flags -
530 * we'll have a chance to get them in the future :)))
531 */
532 snp->snp_flags &= ~SNOOP_OFLOW;
533 snp->snp_flags &= ~SNOOP_DOWN;
534 splx(s);
535 break;
536
537 case SNPGTTY:
538 /*
539 * We keep snp_target field specially to make
540 * SNPGTTY happy, else we can't know what is device
541 * major/minor for tty.
542 */
543 *((dev_t *)data) = dev2udev(snp->snp_target);
544 break;
545
546 case FIONBIO:
547 break;
548
549 case FIOASYNC:
550 if (*(int *)data)
551 snp->snp_flags |= SNOOP_ASYNC;
552 else
553 snp->snp_flags &= ~SNOOP_ASYNC;
554 break;
555
556 case FIONREAD:
557 s = spltty();
558 if (snp->snp_tty != NULL)
559 *(int *)data = snp->snp_len;
560 else
561 if (snp->snp_flags & SNOOP_DOWN) {
562 if (snp->snp_flags & SNOOP_OFLOW)
563 *(int *)data = SNP_OFLOW;
564 else
565 *(int *)data = SNP_TTYCLOSE;
566 } else {
567 *(int *)data = SNP_DETACH;
568 }
569 splx(s);
570 break;
571
572 default:
573 return (ENOTTY);
574 }
575 return (0);
576}
577
578static int
579snppoll(dev, events, td)
580 struct cdev *dev;
581 int events;
582 struct thread *td;
583{
584 struct snoop *snp;
585 int revents;
586
587 snp = dev->si_drv1;
588 revents = 0;
589 /*
590 * If snoop is down, we don't want to poll() forever so we return 1.
591 * Caller should see if we down via FIONREAD ioctl(). The last should
592 * return -1 to indicate down state.
593 */
594 if (events & (POLLIN | POLLRDNORM)) {
595 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
596 revents |= events & (POLLIN | POLLRDNORM);
597 else
598 selrecord(td, &snp->snp_sel);
599 }
600 return (revents);
601}
602
603static void
604snp_clone(arg, name, namelen, dev)
605 void *arg;
606 char *name;
607 int namelen;
608 struct cdev **dev;
609{
610 int u, i;
611
612 if (*dev != NULL)
613 return;
614 if (dev_stdclone(name, NULL, "snp", &u) != 1)
615 return;
616 i = clone_create(&snpclones, &snp_cdevsw, &u, dev, 0);
617 if (i)
618 *dev = make_dev(&snp_cdevsw, unit2minor(u),
619 UID_ROOT, GID_WHEEL, 0600, "snp%d", u);
620 if (*dev != NULL)
621 (*dev)->si_flags |= SI_CHEAPCLONE;
622}
623
624static int
625snp_modevent(mod, type, data)
626 module_t mod;
627 int type;
628 void *data;
629{
630 static eventhandler_tag eh_tag;
631
632 switch (type) {
633 case MOD_LOAD:
634 /* XXX error checking. */
635 clone_setup(&snpclones);
636 eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000);
637 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
638 break;
639 case MOD_UNLOAD:
640 if (!LIST_EMPTY(&snp_sclist))
641 return (EBUSY);
642 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
643 clone_cleanup(&snpclones);
644 ldisc_deregister(snooplinedisc);
645 break;
646 default:
647 break;
648 }
649 return (0);
650}
651
652static moduledata_t snp_mod = {
653 "snp",
654 snp_modevent,
655 NULL
656};
657DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);