kernel - Remove D_KQFILTER flag
[dragonfly.git] / sys / dev / misc / snp / snp.c
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  * $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 $
17  */
18
19 #include "use_snp.h"
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/event.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>
34
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_kqfilter_t    snpkqfilter;
43 static d_clone_t        snpclone;
44 DEVFS_DECLARE_CLONE_BITMAP(snp);
45
46 static void snpfilter_detach(struct knote *);
47 static int snpfilter_rd(struct knote *, long);
48 static int snpfilter_wr(struct knote *, long);
49
50 #if NSNP <= 1
51 #define SNP_PREALLOCATED_UNITS  4
52 #else
53 #define SNP_PREALLOCATED_UNITS  NSNP
54 #endif
55
56 #define CDEV_MAJOR 53
57 static struct dev_ops snp_ops = {
58         { "snp", CDEV_MAJOR, 0 },
59         .d_open =       snpopen,
60         .d_close =      snpclose,
61         .d_read =       snpread,
62         .d_write =      snpwrite,
63         .d_ioctl =      snpioctl,
64         .d_kqfilter =   snpkqfilter
65 };
66
67 static struct linesw snpdisc = {
68         ttyopen,        snplclose,      ttread,         snplwrite,
69         l_nullioctl,    ttyinput,       ttstart,        ttymodem
70 };
71
72 /*
73  * This is the main snoop per-device structure.
74  */
75 struct snoop {
76         LIST_ENTRY(snoop)       snp_list;       /* List glue. */
77         cdev_t                  snp_target;     /* Target tty device. */
78         struct tty              *snp_tty;       /* Target tty pointer. */
79         u_long                   snp_len;       /* Possible length. */
80         u_long                   snp_base;      /* Data base. */
81         u_long                   snp_blen;      /* Used length. */
82         caddr_t                  snp_buf;       /* Allocation pointer. */
83         int                      snp_flags;     /* Flags. */
84         struct kqinfo            snp_kq;        /* Kqueue info. */
85         int                      snp_olddisc;   /* Old line discipline. */
86 };
87
88 /*
89  * Possible flags.
90  */
91 #define SNOOP_ASYNC             0x0002
92 #define SNOOP_OPEN              0x0004
93 #define SNOOP_RWAIT             0x0008
94 #define SNOOP_OFLOW             0x0010
95 #define SNOOP_DOWN              0x0020
96
97 /*
98  * Other constants.
99  */
100 #define SNOOP_MINLEN            (4*1024)        /* This should be power of 2.
101                                                  * 4K tested to be the minimum
102                                                  * for which on normal tty
103                                                  * usage there is no need to
104                                                  * allocate more.
105                                                  */
106 #define SNOOP_MAXLEN            (64*1024)       /* This one also,64K enough
107                                                  * If we grow more,something
108                                                  * really bad in this world..
109                                                  */
110
111 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
112 /*
113  * The number of the "snoop" line discipline.  This gets determined at
114  * module load time.
115  */
116 static int snooplinedisc;
117
118
119 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
120
121 static struct tty       *snpdevtotty (cdev_t dev);
122 static int              snp_detach (struct snoop *snp);
123 static int              snp_down (struct snoop *snp);
124 static int              snp_in (struct snoop *snp, char *buf, int n);
125 static int              snp_modevent (module_t mod, int what, void *arg);
126
127 static int
128 snplclose(struct tty *tp, int flag)
129 {
130         struct snoop *snp;
131         int error;
132
133         snp = tp->t_sc;
134         error = snp_down(snp);
135         if (error != 0)
136                 return (error);
137         error = ttylclose(tp, flag);
138         return (error);
139 }
140
141 static int
142 snplwrite(struct tty *tp, struct uio *uio, int flag)
143 {
144         struct iovec iov;
145         struct uio uio2;
146         struct snoop *snp;
147         int error, ilen;
148         char *ibuf;
149
150         error = 0;
151         ibuf = NULL;
152         snp = tp->t_sc;
153         while (uio->uio_resid > 0) {
154                 ilen = (int)szmin(512, uio->uio_resid);
155                 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
156                 error = uiomove(ibuf, (size_t)ilen, uio);
157                 if (error != 0)
158                         break;
159                 snp_in(snp, ibuf, ilen);
160                 /* Hackish, but probably the least of all evils. */
161                 iov.iov_base = ibuf;
162                 iov.iov_len = ilen;
163                 uio2.uio_iov = &iov;
164                 uio2.uio_iovcnt = 1;
165                 uio2.uio_offset = 0;
166                 uio2.uio_resid = ilen;
167                 uio2.uio_segflg = UIO_SYSSPACE;
168                 uio2.uio_rw = UIO_WRITE;
169                 uio2.uio_td = uio->uio_td;
170                 error = ttwrite(tp, &uio2, flag);
171                 if (error != 0)
172                         break;
173                 kfree(ibuf, M_SNP);
174                 ibuf = NULL;
175         }
176         if (ibuf != NULL)
177                 kfree(ibuf, M_SNP);
178         return (error);
179 }
180
181 static struct tty *
182 snpdevtotty(cdev_t dev)
183 {
184         if ((dev_dflags(dev) & D_TTY) == 0)
185                 return (NULL);
186         return (dev->si_tty);
187 }
188
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...
192                                  */
193
194 static int
195 snpwrite(struct dev_write_args *ap)
196 {
197         cdev_t dev = ap->a_head.a_dev;
198         struct uio *uio = ap->a_uio;
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         kprintf("Snoop: attempt to write to bad tty.\n");
213         return (EIO);
214
215 tty_input:
216         if (!(tp->t_state & TS_ISOPEN))
217                 return (EIO);
218
219         while (uio->uio_resid > 0) {
220                 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
221                 if ((error = uiomove(c, (size_t)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
232 static int
233 snpread(struct dev_read_args *ap)
234 {
235         cdev_t dev = ap->a_head.a_dev;
236         struct uio *uio = ap->a_uio;
237         struct snoop *snp;
238         int error, len, n, nblen;
239         caddr_t from;
240         char *nbuf;
241
242         snp = dev->si_drv1;
243         KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
244             ("snoop buffer error"));
245
246         if (snp->snp_tty == NULL)
247                 return (EIO);
248
249         snp->snp_flags &= ~SNOOP_RWAIT;
250
251         do {
252                 if (snp->snp_len == 0) {
253                         if (ap->a_ioflag & IO_NDELAY)
254                                 return (EWOULDBLOCK);
255                         snp->snp_flags |= SNOOP_RWAIT;
256                         error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
257                         if (error != 0)
258                                 return (error);
259                 }
260         } while (snp->snp_len == 0);
261
262         n = snp->snp_len;
263
264         error = 0;
265         while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
266                 len = (int)szmin(uio->uio_resid, snp->snp_len);
267                 from = (caddr_t)(snp->snp_buf + snp->snp_base);
268                 if (len == 0)
269                         break;
270
271                 error = uiomove(from, (size_t)len, uio);
272                 snp->snp_base += len;
273                 snp->snp_len -= len;
274         }
275         if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
276                 snp->snp_flags &= ~SNOOP_OFLOW;
277         }
278         crit_enter();
279         nblen = snp->snp_blen;
280         if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
281                 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
282                         nblen = nblen / 2;
283                 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
284                         bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
285                         kfree(snp->snp_buf, M_SNP);
286                         snp->snp_buf = nbuf;
287                         snp->snp_blen = nblen;
288                         snp->snp_base = 0;
289                 }
290         }
291         crit_exit();
292
293         return (error);
294 }
295
296 static int
297 snp_in(struct snoop *snp, char *buf, int n)
298 {
299         int s_free, s_tail;
300         int len, nblen;
301         caddr_t from, to;
302         char *nbuf;
303
304         KASSERT(n >= 0, ("negative snoop char count"));
305
306         if (n == 0)
307                 return (0);
308
309         if (snp->snp_flags & SNOOP_DOWN) {
310                 kprintf("Snoop: more data to down interface.\n");
311                 return (0);
312         }
313
314         if (snp->snp_flags & SNOOP_OFLOW) {
315                 kprintf("Snoop: buffer overflow.\n");
316                 /*
317                  * On overflow we just repeat the standart close
318                  * procedure...yes , this is waste of space but.. Then next
319                  * read from device will fail if one would recall he is
320                  * snooping and retry...
321                  */
322
323                 return (snp_down(snp));
324         }
325         s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
326         s_free = snp->snp_blen - snp->snp_len;
327
328
329         if (n > s_free) {
330                 crit_enter();
331                 nblen = snp->snp_blen;
332                 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
333                         nblen = snp->snp_blen * 2;
334                         s_free = nblen - (snp->snp_len + snp->snp_base);
335                 }
336                 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
337                         bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
338                         kfree(snp->snp_buf, M_SNP);
339                         snp->snp_buf = nbuf;
340                         snp->snp_blen = nblen;
341                         snp->snp_base = 0;
342                 } else {
343                         snp->snp_flags |= SNOOP_OFLOW;
344                         if (snp->snp_flags & SNOOP_RWAIT) {
345                                 snp->snp_flags &= ~SNOOP_RWAIT;
346                                 wakeup((caddr_t)snp);
347                         }
348                         crit_exit();
349                         return (0);
350                 }
351                 crit_exit();
352         }
353         if (n > s_tail) {
354                 from = (caddr_t)(snp->snp_buf + snp->snp_base);
355                 to = (caddr_t)(snp->snp_buf);
356                 len = snp->snp_len;
357                 bcopy(from, to, len);
358                 snp->snp_base = 0;
359         }
360         to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
361         bcopy(buf, to, n);
362         snp->snp_len += n;
363
364         if (snp->snp_flags & SNOOP_RWAIT) {
365                 snp->snp_flags &= ~SNOOP_RWAIT;
366                 wakeup((caddr_t)snp);
367         }
368         KNOTE(&snp->snp_kq.ki_note, 0);
369
370         return (n);
371 }
372
373 static int
374 snpopen(struct dev_open_args *ap)
375 {
376         cdev_t dev = ap->a_head.a_dev;
377         struct snoop *snp;
378
379         if (dev->si_drv1 == NULL) {
380 #if 0
381                 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
382                     0600, "snp%d", minor(dev));
383 #endif
384                 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
385                     M_WAITOK | M_ZERO);
386         } else {
387                 return (EBUSY);
388         }
389
390         /*
391          * We intentionally do not OR flags with SNOOP_OPEN, but set them so
392          * all previous settings (especially SNOOP_OFLOW) will be cleared.
393          */
394         snp->snp_flags = SNOOP_OPEN;
395
396         snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
397         snp->snp_blen = SNOOP_MINLEN;
398         snp->snp_base = 0;
399         snp->snp_len = 0;
400
401         /*
402          * snp_tty == NULL  is for inactive snoop devices.
403          */
404         snp->snp_tty = NULL;
405         snp->snp_target = NULL;
406
407         LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
408         return (0);
409 }
410
411
412 static int
413 snp_detach(struct snoop *snp)
414 {
415         struct tty *tp;
416
417         snp->snp_base = 0;
418         snp->snp_len = 0;
419
420         /*
421          * If line disc. changed we do not touch this pointer, SLIP/PPP will
422          * change it anyway.
423          */
424         tp = snp->snp_tty;
425         if (tp == NULL)
426                 goto detach_notty;
427
428         if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
429             tp->t_line == snooplinedisc) {
430                 tp->t_sc = NULL;
431                 tp->t_state &= ~TS_SNOOP;
432                 tp->t_line = snp->snp_olddisc;
433         } else
434                 kprintf("Snoop: bad attached tty data.\n");
435
436         snp->snp_tty = NULL;
437         snp->snp_target = NULL;
438
439 detach_notty:
440         KNOTE(&snp->snp_kq.ki_note, 0);
441         if ((snp->snp_flags & SNOOP_OPEN) == 0) 
442                 kfree(snp, M_SNP);
443
444         return (0);
445 }
446
447 static int
448 snpclose(struct dev_close_args *ap)
449 {
450         cdev_t dev = ap->a_head.a_dev;
451         struct snoop *snp;
452
453         snp = dev->si_drv1;
454         snp->snp_blen = 0;
455         LIST_REMOVE(snp, snp_list);
456         kfree(snp->snp_buf, M_SNP);
457         snp->snp_flags &= ~SNOOP_OPEN;
458         dev->si_drv1 = NULL;
459         if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) {
460                 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor);
461                 destroy_dev(dev);
462         }
463         return (snp_detach(snp));
464 }
465
466 static int
467 snp_down(struct snoop *snp)
468 {
469
470         if (snp->snp_blen != SNOOP_MINLEN) {
471                 kfree(snp->snp_buf, M_SNP);
472                 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
473                 snp->snp_blen = SNOOP_MINLEN;
474         }
475         snp->snp_flags |= SNOOP_DOWN;
476
477         return (snp_detach(snp));
478 }
479
480 static int
481 snpioctl(struct dev_ioctl_args *ap)
482 {
483         cdev_t dev = ap->a_head.a_dev;
484         struct snoop *snp;
485         struct tty *tp, *tpo;
486         cdev_t tdev;
487
488         snp = dev->si_drv1;
489         switch (ap->a_cmd) {
490         case SNPSTTY:
491                 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
492                 if (tdev == NULL)
493                         return (snp_down(snp));
494
495                 tp = snpdevtotty(tdev);
496                 if (!tp)
497                         return (EINVAL);
498                 if (tp->t_state & TS_SNOOP)
499                         return (EBUSY);
500
501                 crit_enter();
502
503                 if (snp->snp_target == NULL) {
504                         tpo = snp->snp_tty;
505                         if (tpo)
506                                 tpo->t_state &= ~TS_SNOOP;
507                 }
508
509                 tp->t_sc = (caddr_t)snp;
510                 tp->t_state |= TS_SNOOP;
511                 snp->snp_olddisc = tp->t_line;
512                 tp->t_line = snooplinedisc;
513                 snp->snp_tty = tp;
514                 snp->snp_target = tdev;
515
516                 /*
517                  * Clean overflow and down flags -
518                  * we'll have a chance to get them in the future :)))
519                  */
520                 snp->snp_flags &= ~SNOOP_OFLOW;
521                 snp->snp_flags &= ~SNOOP_DOWN;
522                 crit_exit();
523                 break;
524
525         case SNPGTTY:
526                 /*
527                  * We keep snp_target field specially to make
528                  * SNPGTTY happy, else we can't know what is device
529                  * major/minor for tty.
530                  */
531                 *((cdev_t *)ap->a_data) = snp->snp_target;
532                 break;
533
534         case FIOASYNC:
535                 if (*(int *)ap->a_data)
536                         snp->snp_flags |= SNOOP_ASYNC;
537                 else
538                         snp->snp_flags &= ~SNOOP_ASYNC;
539                 break;
540
541         case FIONREAD:
542                 crit_enter();
543                 if (snp->snp_tty != NULL)
544                         *(int *)ap->a_data = snp->snp_len;
545                 else
546                         if (snp->snp_flags & SNOOP_DOWN) {
547                                 if (snp->snp_flags & SNOOP_OFLOW)
548                                         *(int *)ap->a_data = SNP_OFLOW;
549                                 else
550                                         *(int *)ap->a_data = SNP_TTYCLOSE;
551                         } else {
552                                 *(int *)ap->a_data = SNP_DETACH;
553                         }
554                 crit_exit();
555                 break;
556
557         default:
558                 return (ENOTTY);
559         }
560         return (0);
561 }
562
563 static struct filterops snpfiltops_rd =
564         { FILTEROP_ISFD, NULL, snpfilter_detach, snpfilter_rd };
565 static struct filterops snpfiltops_wr =
566         { FILTEROP_ISFD, NULL, snpfilter_detach, snpfilter_wr };
567
568 static int
569 snpkqfilter(struct dev_kqfilter_args *ap)
570 {
571         cdev_t dev = ap->a_head.a_dev;
572         struct snoop *snp = dev->si_drv1;
573         struct knote *kn = ap->a_kn;
574         struct klist *klist;
575
576         ap->a_result = 0;
577
578         switch (kn->kn_filter) {
579         case EVFILT_READ:
580                 kn->kn_fop = &snpfiltops_rd;
581                 kn->kn_hook = (caddr_t)snp;
582                 break;
583         case EVFILT_WRITE:
584                 kn->kn_fop = &snpfiltops_wr;
585                 kn->kn_hook = (caddr_t)snp;
586                 break;
587         default:
588                 ap->a_result = EOPNOTSUPP;
589                 return (0);
590         }
591
592         klist = &snp->snp_kq.ki_note;
593         knote_insert(klist, kn);
594
595         return (0);
596 }
597
598 static void
599 snpfilter_detach(struct knote *kn)
600 {
601         struct snoop *snp = (struct snoop *)kn->kn_hook;
602         struct klist *klist;
603
604         klist = &snp->snp_kq.ki_note;
605         knote_insert(klist, kn);
606 }
607
608 static int
609 snpfilter_rd(struct knote *kn, long hint)
610 {
611         struct snoop *snp = (struct snoop *)kn->kn_hook;
612         int ready = 0;
613
614         /*
615          * If snoop is down, we don't want to poll forever so we return 1.
616          * Caller should see if we down via FIONREAD ioctl().  The last should
617          * return -1 to indicate down state.
618          */
619         if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
620                 ready = 1;
621
622         return (ready);
623 }
624
625 static int
626 snpfilter_wr(struct knote *kn, long hint)
627 {
628         /* Writing is always OK */
629         return (1);
630 }
631
632 static int
633 snpclone(struct dev_clone_args *ap)
634 {
635         int unit;
636         unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0);
637         ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600,
638                                                         "snp%d", unit);
639
640         return 0;
641 }
642
643 static int
644 snp_modevent(module_t mod, int type, void *data)
645 {
646         int i;
647
648         switch (type) {
649         case MOD_LOAD:
650                 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
651                 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
652                         snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
653
654                 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) {
655                         make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i);
656                         devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i);
657                 }
658                 break;
659         case MOD_UNLOAD:
660                 if (!LIST_EMPTY(&snp_sclist))
661                         return (EBUSY);
662                 ldisc_deregister(snooplinedisc);
663                 devfs_clone_handler_del("snp");
664                 dev_ops_remove_all(&snp_ops);
665                 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
666                 break;
667         default:
668                 break;
669         }
670         return (0);
671 }
672
673 static moduledata_t snp_mod = {
674         "snp",
675         snp_modevent,
676         NULL
677 };
678 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);