1 /* $NetBSD: putter.c,v 1.31 2011/02/06 14:29:25 haad Exp $ */
4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by the
7 * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
8 * Research Foundation of Helsinki University of Technology
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Pass-to-Userspace TransporTER: generic kernel-user request-response
34 * transport interface.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/devfs.h>
42 #include <sys/fcntl.h>
43 #include <sys/filio.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/spinlock.h>
48 #include <sys/spinlock2.h>
50 #include <sys/vnode.h>
52 #include <dev/misc/putter/putter_sys.h>
54 static MALLOC_DEFINE(M_PUTTER, "putter", "Putter device data");
57 * Device routines. These are for when /dev/putter is initially
58 * opened before it has been cloned.
61 static d_open_t puttercdopen;
62 static d_read_t putter_fop_read;
63 static d_write_t putter_fop_write;
64 static d_ioctl_t putter_fop_ioctl;
65 static d_close_t putter_fop_close;
66 static d_kqfilter_t putter_fop_kqfilter;
68 DEVFS_DECLARE_CLONE_BITMAP(putter);
71 static struct dev_ops putter_ops = {
73 .d_open = puttercdopen,
74 .d_close = putter_fop_close,
75 .d_read = putter_fop_read,
76 .d_write = putter_fop_write,
77 .d_ioctl = putter_fop_ioctl,
78 .d_kqfilter = putter_fop_kqfilter,
85 * This is static-size for now. Will be redone for devfs.
88 #define PUTTER_CONFSIZE 16
90 static struct putter_config {
92 int (*pc_config)(int, int, int);
93 } putterconf[PUTTER_CONFSIZE];
96 putter_configure(dev_t dev, int flags, int fmt, int fd)
98 struct putter_config *pc;
100 /* are we the catch-all node? */
101 if (minor(dev) == PUTTER_MINOR_WILDCARD
102 || minor(dev) == PUTTER_MINOR_COMPAT)
105 /* nopes? try to configure us */
106 for (pc = putterconf; pc->pc_config; pc++)
107 if (minor(dev) == pc->pc_minor)
108 return pc->pc_config(fd, flags, fmt);
113 putter_register(putter_config_fn pcfn, int minor)
117 for (i = 0; i < PUTTER_CONFSIZE; i++)
118 if (putterconf[i].pc_config == NULL)
120 if (i == PUTTER_CONFSIZE)
123 putterconf[i].pc_minor = minor;
124 putterconf[i].pc_config = pcfn;
130 * putter instance structures. these are always allocated and freed
131 * from the context of the transport user.
133 struct putter_instance {
139 struct putter_ops *pi_pop;
145 TAILQ_ENTRY(putter_instance) pi_entries;
147 #define PUTTER_EMBRYO ((void *)-1) /* before attach */
148 #define PUTTER_DEAD ((void *)-2) /* after detach */
150 static TAILQ_HEAD(, putter_instance) putter_ilist
151 = TAILQ_HEAD_INITIALIZER(putter_ilist);
161 #define DPRINTF(x) if (putterdebug > 0) kprintf x
162 #define DPRINTF_VERBOSE(x) if (putterdebug > 1) kprintf x
165 #define DPRINTF_VERBOSE(x)
169 * public init / deinit
172 /* protects both the list and the contents of the list elements */
173 static struct spinlock pi_mtx = SPINLOCK_INITIALIZER(&pi_mtx, "pi_mtx");
176 * fd routines, for cloner
180 putter_fop_read(struct dev_read_args *ap)
182 cdev_t dev = ap->a_head.a_dev;
183 struct putter_instance *pi = dev->si_drv1;
184 struct uio *uio = ap->a_uio;
185 size_t origres, moved;
188 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
189 kprintf("putter_fop_read: private %d not inited\n", pi->pi_idx);
193 if (pi->pi_curput == NULL) {
194 error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
195 ap->a_ioflag & IO_NDELAY, &pi->pi_curput,
196 &pi->pi_curres, &pi->pi_curopaq);
202 origres = uio->uio_resid;
203 error = uiomove(pi->pi_curput, pi->pi_curres, uio);
204 moved = origres - uio->uio_resid;
205 DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
206 pi, moved, pi->pi_curput, error));
208 KKASSERT(pi->pi_curres >= moved);
209 pi->pi_curres -= moved;
210 pi->pi_curput += moved;
212 if (pi->pi_curres == 0) {
213 pi->pi_pop->pop_releaseout(pi->pi_private,
214 pi->pi_curopaq, error);
215 pi->pi_curput = NULL;
222 putter_fop_write(struct dev_write_args *ap)
224 cdev_t dev = ap->a_head.a_dev;
225 struct putter_instance *pi = dev->si_drv1;
226 struct uio *uio = ap->a_uio;
227 struct putter_hdr pth;
232 DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
233 pi->pi_private, uio->uio_resid));
235 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
236 kprintf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
240 error = uiomove((char *)&pth, sizeof(struct putter_hdr), uio);
245 /* Sorry mate, the kernel doesn't buffer. */
246 frsize = pth.pth_framelen - sizeof(struct putter_hdr);
247 if (uio->uio_resid < frsize) {
251 buf = kmalloc(frsize + sizeof(struct putter_hdr), M_PUTTER, M_WAITOK);
252 memcpy(buf, &pth, sizeof(pth));
253 error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
255 pi->pi_pop->pop_dispatch(pi->pi_private,
256 (struct putter_hdr *)buf);
258 kfree(buf, M_PUTTER);
264 * device close = forced unmount.
266 * unmounting is a frightfully complex operation to avoid races
269 putter_fop_close(struct dev_close_args *ap)
271 cdev_t dev = ap->a_head.a_dev;
272 struct putter_instance *pi = dev->si_drv1;
275 DPRINTF(("putter_fop_close: device closed\n"));
280 * First check if the driver was never born. In that case
281 * remove the instance from the list. If mount is attempted later,
282 * it will simply fail.
284 if (pi->pi_private == PUTTER_EMBRYO) {
285 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
286 spin_unlock(&pi_mtx);
288 DPRINTF(("putter_fop_close: data associated with dev %i was "
289 "embryonic\n", dev->si_uminor));
295 * Next, analyze if unmount was called and the instance is dead.
296 * In this case we can just free the structure and go home, it
297 * was removed from the list by putter_rmprivate().
299 if (pi->pi_private == PUTTER_DEAD) {
300 spin_unlock(&pi_mtx);
302 DPRINTF(("putter_fop_close: putter associated with dev %d "
303 "dead, freeing\n", pi->pi_idx));
309 * So we have a reference. Proceed to unravel the
312 spin_unlock(&pi_mtx);
314 /* hmm? suspicious locking? */
315 while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
320 * Finally, release the instance information. It was already
321 * removed from the list by putter_rmprivate() and we know it's
322 * dead, so no need to lock.
330 putter_fop_ioctl(struct dev_ioctl_args *ap)
334 * work already done in sys_ioctl(). skip sanity checks to enable
335 * setting non-blocking fd on an embryotic driver.
337 if (ap->a_cmd == FIONBIO)
346 filt_putterdetach(struct knote *kn)
348 struct putter_instance *pi = (void *)kn->kn_hook;
351 klist = &pi->pi_kq.ki_note;
352 knote_remove(klist, kn);
356 filt_putter_rd(struct knote *kn, long hint)
358 struct putter_instance *pi = (void *)kn->kn_hook;
363 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
365 spin_unlock(&pi_mtx);
370 kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
371 rv = kn->kn_data != 0;
376 filt_putter_wr(struct knote *kn, long hint)
378 /* Writing is always OK */
383 static struct filterops putter_filtops_rd =
384 { FILTEROP_ISFD, NULL, filt_putterdetach, filt_putter_rd };
385 static struct filterops putter_filtops_wr =
386 { FILTEROP_ISFD, NULL, filt_putterdetach, filt_putter_wr };
389 putter_fop_kqfilter(struct dev_kqfilter_args *ap)
391 cdev_t dev = ap->a_head.a_dev;
392 struct putter_instance *pi = dev->si_drv1;
393 struct knote *kn = ap->a_kn;
396 switch (kn->kn_filter) {
398 kn->kn_fop = &putter_filtops_rd;
399 kn->kn_hook = (char *)pi;
402 kn->kn_fop = &putter_filtops_wr;
403 kn->kn_hook = (char *)pi;
406 ap->a_result = EOPNOTSUPP;
410 klist = &pi->pi_kq.ki_note;
411 knote_insert(klist, kn);
417 puttercdopen(struct dev_open_args *ap)
419 cdev_t dev = ap->a_head.a_dev;
420 struct putter_instance *pi;
422 pi = kmalloc(sizeof(struct putter_instance), M_PUTTER,
426 pi->pi_pid = curproc->p_pid;
427 pi->pi_idx = dev->si_uminor;
428 pi->pi_curput = NULL;
430 pi->pi_curopaq = NULL;
431 pi->pi_private = PUTTER_EMBRYO;
434 TAILQ_INSERT_TAIL(&putter_ilist, pi, pi_entries);
435 spin_unlock(&pi_mtx);
437 DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
444 * Set the private structure for the file descriptor. This is
445 * typically done immediately when the counterpart has knowledge
446 * about the private structure's address and the file descriptor
447 * (e.g. vfs mount routine).
449 * We only want to make sure that the caller had the right to open the
450 * device, we don't so much care about which context it gets in case
451 * the same process opened multiple (since they are equal at this point).
453 struct putter_instance *
454 putter_attach(pid_t pid, int minor, void *ppriv, struct putter_ops *pop)
456 struct putter_instance *pi = NULL;
459 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
460 if (pi->pi_pid == pid && pi->pi_idx == minor &&
461 pi->pi_private == PUTTER_EMBRYO) {
462 pi->pi_private = ppriv;
467 spin_unlock(&pi_mtx);
469 DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
470 pi ? pi->pi_pid : 0, pi ? pi->pi_idx : 0));
476 * Remove fp <-> private mapping.
479 putter_detach(struct putter_instance *pi)
483 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
484 pi->pi_private = PUTTER_DEAD;
485 spin_unlock(&pi_mtx);
487 DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
491 putter_notify(struct putter_instance *pi)
494 KNOTE(&pi->pi_kq.ki_note, 0);
498 putter_clone(struct dev_clone_args *ap)
502 minor = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(putter), 0);
503 ap->a_dev = make_only_dev(&putter_ops, minor, UID_ROOT, GID_WHEEL, 0600,
509 putter_modevent(module_t mod, int type, void *data)
513 make_autoclone_dev(&putter_ops, &DEVFS_CLONE_BITMAP(putter),
514 putter_clone, UID_ROOT, GID_WHEEL, 0600, "putter");
517 devfs_clone_handler_del("putter");
518 dev_ops_remove_all(&putter_ops);
519 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(putter));
527 static moduledata_t putter_mod = {
532 DECLARE_MODULE(putter, putter_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
533 MODULE_VERSION(putter, 1);