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