dev - Add kq support to snp
[dragonfly.git] / sys / dev / misc / snp / snp.c
CommitLineData
984263bc
MD
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 $
028066b1 16 * $DragonFly: src/sys/dev/misc/snp/snp.c,v 1.19 2007/05/08 02:31:41 dillon Exp $
984263bc
MD
17 */
18
95db3aac 19#include "use_snp.h"
984263bc
MD
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>
5d7a9a94 27#include <sys/event.h>
984263bc
MD
28#include <sys/kernel.h>
29#include <sys/queue.h>
30#include <sys/snoop.h>
12cc2857 31#include <sys/thread2.h>
984263bc 32#include <sys/vnode.h>
f15db79e 33#include <sys/device.h>
2c1e28dd 34#include <sys/devfs.h>
984263bc
MD
35
36static l_close_t snplclose;
37static l_write_t snplwrite;
38static d_open_t snpopen;
39static d_close_t snpclose;
40static d_read_t snpread;
41static d_write_t snpwrite;
42static d_ioctl_t snpioctl;
43static d_poll_t snppoll;
5d7a9a94 44static d_kqfilter_t snpkqfilter;
8be7edad
MD
45static d_clone_t snpclone;
46DEVFS_DECLARE_CLONE_BITMAP(snp);
95db3aac 47
5d7a9a94
SG
48static void snpfilter_detach(struct knote *);
49static int snpfilter(struct knote *, long);
50
95db3aac 51#if NSNP <= 1
8be7edad 52#define SNP_PREALLOCATED_UNITS 4
95db3aac
AH
53#else
54#define SNP_PREALLOCATED_UNITS NSNP
55#endif
984263bc
MD
56
57#define CDEV_MAJOR 53
fef8985e 58static struct dev_ops snp_ops = {
5d7a9a94 59 { "snp", CDEV_MAJOR, D_KQFILTER },
fef8985e
MD
60 .d_open = snpopen,
61 .d_close = snpclose,
62 .d_read = snpread,
63 .d_write = snpwrite,
64 .d_ioctl = snpioctl,
65 .d_poll = snppoll,
5d7a9a94 66 .d_kqfilter = snpkqfilter
984263bc
MD
67};
68
69static 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 */
77struct snoop {
78 LIST_ENTRY(snoop) snp_list; /* List glue. */
b13267a5 79 cdev_t snp_target; /* Target tty device. */
984263bc
MD
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
113static 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 */
118static int snooplinedisc;
119
120
121static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
122
b13267a5 123static struct tty *snpdevtotty (cdev_t dev);
38e94a25
RG
124static int snp_detach (struct snoop *snp);
125static int snp_down (struct snoop *snp);
126static int snp_in (struct snoop *snp, char *buf, int n);
127static int snp_modevent (module_t mod, int what, void *arg);
984263bc
MD
128
129static int
c436375a 130snplclose(struct tty *tp, int flag)
984263bc
MD
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
143static int
c436375a 144snplwrite(struct tty *tp, struct uio *uio, int flag)
984263bc
MD
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) {
e54488bb 156 ilen = (int)szmin(512, uio->uio_resid);
efda3bd0 157 ibuf = kmalloc(ilen, M_SNP, M_WAITOK);
e54488bb 158 error = uiomove(ibuf, (size_t)ilen, uio);
984263bc
MD
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;
dadab5e9 171 uio2.uio_td = uio->uio_td;
984263bc
MD
172 error = ttwrite(tp, &uio2, flag);
173 if (error != 0)
174 break;
efda3bd0 175 kfree(ibuf, M_SNP);
984263bc
MD
176 ibuf = NULL;
177 }
178 if (ibuf != NULL)
efda3bd0 179 kfree(ibuf, M_SNP);
984263bc
MD
180 return (error);
181}
182
183static struct tty *
b13267a5 184snpdevtotty(cdev_t dev)
984263bc 185{
f15db79e 186 if ((dev_dflags(dev) & D_TTY) == 0)
984263bc
MD
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
196static int
fef8985e 197snpwrite(struct dev_write_args *ap)
984263bc 198{
b13267a5 199 cdev_t dev = ap->a_head.a_dev;
fef8985e 200 struct uio *uio = ap->a_uio;
984263bc
MD
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
e3869ec7 214 kprintf("Snoop: attempt to write to bad tty.\n");
984263bc
MD
215 return (EIO);
216
217tty_input:
218 if (!(tp->t_state & TS_ISOPEN))
219 return (EIO);
220
221 while (uio->uio_resid > 0) {
e54488bb
MD
222 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF);
223 if ((error = uiomove(c, (size_t)len, uio)) != 0)
984263bc
MD
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
234static int
fef8985e 235snpread(struct dev_read_args *ap)
984263bc 236{
b13267a5 237 cdev_t dev = ap->a_head.a_dev;
fef8985e 238 struct uio *uio = ap->a_uio;
984263bc 239 struct snoop *snp;
12cc2857 240 int error, len, n, nblen;
984263bc
MD
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) {
fef8985e 255 if (ap->a_ioflag & IO_NDELAY)
984263bc
MD
256 return (EWOULDBLOCK);
257 snp->snp_flags |= SNOOP_RWAIT;
377d4740 258 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0);
984263bc
MD
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) {
e54488bb 268 len = (int)szmin(uio->uio_resid, snp->snp_len);
984263bc
MD
269 from = (caddr_t)(snp->snp_buf + snp->snp_base);
270 if (len == 0)
271 break;
272
e54488bb 273 error = uiomove(from, (size_t)len, uio);
984263bc
MD
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 }
12cc2857 280 crit_enter();
984263bc
MD
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;
efda3bd0 285 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
984263bc 286 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
efda3bd0 287 kfree(snp->snp_buf, M_SNP);
984263bc
MD
288 snp->snp_buf = nbuf;
289 snp->snp_blen = nblen;
290 snp->snp_base = 0;
291 }
292 }
12cc2857 293 crit_exit();
984263bc
MD
294
295 return (error);
296}
297
298static int
c436375a 299snp_in(struct snoop *snp, char *buf, int n)
984263bc
MD
300{
301 int s_free, s_tail;
12cc2857 302 int len, nblen;
984263bc
MD
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) {
e3869ec7 312 kprintf("Snoop: more data to down interface.\n");
984263bc
MD
313 return (0);
314 }
315
316 if (snp->snp_flags & SNOOP_OFLOW) {
e3869ec7 317 kprintf("Snoop: buffer overflow.\n");
984263bc
MD
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) {
12cc2857 332 crit_enter();
984263bc
MD
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 }
efda3bd0 338 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) {
984263bc 339 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
efda3bd0 340 kfree(snp->snp_buf, M_SNP);
984263bc
MD
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 }
12cc2857 350 crit_exit();
984263bc
MD
351 return (0);
352 }
12cc2857 353 crit_exit();
984263bc
MD
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
376static int
fef8985e 377snpopen(struct dev_open_args *ap)
984263bc 378{
b13267a5 379 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
380 struct snoop *snp;
381
382 if (dev->si_drv1 == NULL) {
8be7edad 383#if 0
fef8985e 384 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL,
984263bc 385 0600, "snp%d", minor(dev));
8be7edad 386#endif
efda3bd0 387 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP,
984263bc 388 M_WAITOK | M_ZERO);
e4c9c0c8 389 } else {
984263bc 390 return (EBUSY);
e4c9c0c8 391 }
984263bc
MD
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
efda3bd0 399 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
984263bc
MD
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;
028066b1 408 snp->snp_target = NULL;
984263bc
MD
409
410 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
411 return (0);
412}
413
414
415static int
c436375a 416snp_detach(struct snoop *snp)
984263bc
MD
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
e3869ec7 437 kprintf("Snoop: bad attached tty data.\n");
984263bc
MD
438
439 snp->snp_tty = NULL;
028066b1 440 snp->snp_target = NULL;
984263bc
MD
441
442detach_notty:
443 selwakeup(&snp->snp_sel);
444 snp->snp_sel.si_pid = 0;
445 if ((snp->snp_flags & SNOOP_OPEN) == 0)
efda3bd0 446 kfree(snp, M_SNP);
984263bc
MD
447
448 return (0);
449}
450
451static int
fef8985e 452snpclose(struct dev_close_args *ap)
984263bc 453{
b13267a5 454 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
455 struct snoop *snp;
456
457 snp = dev->si_drv1;
458 snp->snp_blen = 0;
459 LIST_REMOVE(snp, snp_list);
efda3bd0 460 kfree(snp->snp_buf, M_SNP);
984263bc
MD
461 snp->snp_flags &= ~SNOOP_OPEN;
462 dev->si_drv1 = NULL;
8be7edad
MD
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 }
984263bc
MD
467 return (snp_detach(snp));
468}
469
470static int
c436375a 471snp_down(struct snoop *snp)
984263bc
MD
472{
473
474 if (snp->snp_blen != SNOOP_MINLEN) {
efda3bd0
MD
475 kfree(snp->snp_buf, M_SNP);
476 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
984263bc
MD
477 snp->snp_blen = SNOOP_MINLEN;
478 }
479 snp->snp_flags |= SNOOP_DOWN;
480
481 return (snp_detach(snp));
482}
483
484static int
fef8985e 485snpioctl(struct dev_ioctl_args *ap)
984263bc 486{
b13267a5 487 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
488 struct snoop *snp;
489 struct tty *tp, *tpo;
b13267a5 490 cdev_t tdev;
984263bc
MD
491
492 snp = dev->si_drv1;
fef8985e 493 switch (ap->a_cmd) {
984263bc 494 case SNPSTTY:
fef8985e 495 tdev = udev2dev(*((udev_t *)ap->a_data), 0);
028066b1 496 if (tdev == NULL)
984263bc
MD
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
12cc2857 505 crit_enter();
984263bc 506
028066b1 507 if (snp->snp_target == NULL) {
984263bc
MD
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;
12cc2857 526 crit_exit();
984263bc
MD
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 */
b13267a5 535 *((cdev_t *)ap->a_data) = snp->snp_target;
984263bc
MD
536 break;
537
984263bc 538 case FIOASYNC:
fef8985e 539 if (*(int *)ap->a_data)
984263bc
MD
540 snp->snp_flags |= SNOOP_ASYNC;
541 else
542 snp->snp_flags &= ~SNOOP_ASYNC;
543 break;
544
545 case FIONREAD:
12cc2857 546 crit_enter();
984263bc 547 if (snp->snp_tty != NULL)
fef8985e 548 *(int *)ap->a_data = snp->snp_len;
984263bc
MD
549 else
550 if (snp->snp_flags & SNOOP_DOWN) {
551 if (snp->snp_flags & SNOOP_OFLOW)
fef8985e 552 *(int *)ap->a_data = SNP_OFLOW;
984263bc 553 else
fef8985e 554 *(int *)ap->a_data = SNP_TTYCLOSE;
984263bc 555 } else {
fef8985e 556 *(int *)ap->a_data = SNP_DETACH;
984263bc 557 }
12cc2857 558 crit_exit();
984263bc
MD
559 break;
560
561 default:
562 return (ENOTTY);
563 }
564 return (0);
565}
566
567static int
fef8985e 568snppoll(struct dev_poll_args *ap)
984263bc 569{
b13267a5 570 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
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 */
fef8985e 581 if (ap->a_events & (POLLIN | POLLRDNORM)) {
984263bc 582 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
fef8985e 583 revents |= ap->a_events & (POLLIN | POLLRDNORM);
984263bc 584 else
fef8985e 585 selrecord(curthread, &snp->snp_sel);
984263bc 586 }
fef8985e
MD
587 ap->a_events = revents;
588 return (0);
984263bc
MD
589}
590
5d7a9a94
SG
591static struct filterops snpfiltops =
592 { 1, NULL, snpfilter_detach, snpfilter };
593
594static int
595snpkqfilter(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
622static void
623snpfilter_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
634static int
635snpfilter(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
984263bc 651static int
8be7edad
MD
652snpclone(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
662static int
c436375a 663snp_modevent(module_t mod, int type, void *data)
984263bc 664{
8be7edad 665 int i;
984263bc
MD
666
667 switch (type) {
668 case MOD_LOAD:
669 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
b96f3782
AH
670 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp),
671 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp");
8be7edad
MD
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 }
984263bc
MD
677 break;
678 case MOD_UNLOAD:
679 if (!LIST_EMPTY(&snp_sclist))
680 return (EBUSY);
681 ldisc_deregister(snooplinedisc);
8be7edad 682 devfs_clone_handler_del("snp");
cd29885a 683 dev_ops_remove_all(&snp_ops);
8be7edad 684 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp));
984263bc
MD
685 break;
686 default:
687 break;
688 }
689 return (0);
690}
691
692static moduledata_t snp_mod = {
693 "snp",
694 snp_modevent,
695 NULL
696};
697DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);