Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Pad is now clones its device, attaching upon open and de...



details:   https://anonhg.NetBSD.org/src/rev/6d105b74f2f9
branches:  trunk
changeset: 825156:6d105b74f2f9
user:      nat <nat%NetBSD.org@localhost>
date:      Sat Jul 01 23:31:19 2017 +0000

description:
Pad is now clones its device, attaching upon open and detaching upon close.
This means that only one pad device is required in /dev.

The code contains a compile time limit of 128 units.

Ok christos@.

diffstat:

 sys/dev/pad/pad.c    |  274 +++++++++++++++++++++++++++++++++++---------------
 sys/dev/pad/padvar.h |    3 +-
 sys/sys/file.h       |    4 +-
 3 files changed, 198 insertions(+), 83 deletions(-)

diffs (truncated from 447 to 300 lines):

diff -r 3b520d905f60 -r 6d105b74f2f9 sys/dev/pad/pad.c
--- a/sys/dev/pad/pad.c Sat Jul 01 23:27:17 2017 +0000
+++ b/sys/dev/pad/pad.c Sat Jul 01 23:31:19 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pad.c,v 1.38 2017/07/01 05:50:10 nat Exp $ */
+/* $NetBSD: pad.c,v 1.39 2017/07/01 23:31:19 nat Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -27,18 +27,23 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.38 2017/07/01 05:50:10 nat Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.39 2017/07/01 23:31:19 nat Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/conf.h>
 #include <sys/buf.h>
+#include <sys/file.h>
+#include <sys/filedesc.h>
+#include <sys/vnode.h>
+#include <sys/kauth.h>
 #include <sys/kmem.h>
 #include <sys/kernel.h>
 #include <sys/device.h>
 #include <sys/proc.h>
 #include <sys/condvar.h>
 #include <sys/select.h>
+#include <sys/stat.h>
 #include <sys/audioio.h>
 #include <sys/vnode.h>
 #include <sys/module.h>
@@ -52,6 +57,7 @@
 
 #include <dev/pad/padvar.h>
 
+#define MAXDEVS                128
 #define PADUNIT(x)     minor(x)
 
 #define PADFREQ                44100
@@ -104,7 +110,15 @@
     const audio_params_t *, const audio_params_t *);
 static void    pad_swvol_dtor(stream_filter_t *);
 
-static bool    pad_is_attached;        /* Do we have an audio* child? */
+static int pad_close(struct file *);
+static int pad_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
+static int pad_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
+static int pad_ioctl(struct file *, u_long, void *);
+static int pad_kqfilter(struct file *, struct knote *);
+static int pad_poll(struct file *, int);
+static int pad_stat(struct file *, struct stat *);
+static int pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
+                          struct uvm_object **, int *);
 
 static const struct audio_hw_if pad_hw_if = {
        .open = pad_audio_open,
@@ -135,13 +149,11 @@
 static int     pad_get_block(pad_softc_t *, pad_block_t *, int);
 
 dev_type_open(pad_open);
-dev_type_close(pad_close);
-dev_type_read(pad_read);
 
 const struct cdevsw pad_cdevsw = {
        .d_open = pad_open,
-       .d_close = pad_close,
-       .d_read = pad_read,
+       .d_close = noclose,
+       .d_read = noread,
        .d_write = nowrite,
        .d_ioctl = noioctl,
        .d_stop = nostop,
@@ -153,35 +165,35 @@
        .d_flag = D_OTHER | D_MPSAFE,
 };
 
+const struct fileops pad_fileops = {
+       .fo_read = pad_read,
+       .fo_write = pad_write,
+       .fo_ioctl = pad_ioctl,
+       .fo_fcntl = fnullop_fcntl,
+       .fo_stat = pad_stat,
+       .fo_poll = pad_poll,
+       .fo_close = pad_close,
+       .fo_mmap = pad_mmap,
+       .fo_kqfilter = pad_kqfilter,
+       .fo_restart = fnullop_restart
+};
+
 CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
     NULL, NULL, pad_childdet);
 
 void
 padattach(int n)
 {
-       int i, err;
-       cfdata_t cf;
-
-       aprint_debug("pad: requested %d units\n", n);
+       int error;
 
-       err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
-       if (err) {
+       error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
+       if (error) {
                aprint_error("%s: couldn't register cfattach: %d\n",
-                   pad_cd.cd_name, err);
+                   pad_cd.cd_name, error);
                config_cfdriver_detach(&pad_cd);
                return;
        }
 
-       for (i = 0; i < n; i++) {
-               cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
-               cf->cf_name = pad_cd.cd_name;
-               cf->cf_atname = pad_cd.cd_name;
-               cf->cf_unit = i;
-               cf->cf_fstate = FSTATE_STAR;
-
-               (void)config_attach_pseudo(cf);
-       }
-
        return;
 }
 
@@ -258,19 +270,91 @@
 static void
 pad_attach(device_t parent, device_t self, void *opaque)
 {
-       pad_softc_t *sc = device_private(self);
-
        aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
 
-       sc->sc_dev = self;
+       return;
+}
+
+static int
+pad_detach(device_t self, int flags)
+{
+       pad_softc_t *sc;
+       int cmaj, mn, rc;
+
+       sc = device_private(self);
+       config_deactivate(sc->sc_audiodev);
+       
+       /* Start draining existing accessors of the device. */
+       if ((rc = config_detach_children(self, flags)) != 0)
+               return rc;
+
+       mutex_enter(&sc->sc_lock);
+       sc->sc_dying = true;
+       cv_broadcast(&sc->sc_condvar);
+       mutex_exit(&sc->sc_lock);
+
+       KASSERT(sc->sc_open > 0);
        sc->sc_open = 0;
+
+       cmaj = cdevsw_lookup_major(&pad_cdevsw);
+       mn = device_unit(sc->sc_dev);
+       vdevgone(cmaj, mn, mn, VCHR);
+
+       pmf_device_deregister(sc->sc_dev);
+
+       mutex_destroy(&sc->sc_lock);
+       mutex_destroy(&sc->sc_intr_lock);
+       cv_destroy(&sc->sc_condvar);
+
+       auconv_delete_encodings(sc->sc_encodings);
+
+       return rc;
+}
+
+int
+pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
+{
+       pad_softc_t *sc;
+       struct file *fp;
+       device_t paddev;
+       cfdata_t cf;
+       int error, fd, i;
+
+       for (i = 0; i < MAXDEVS; i++) {
+               if (device_lookup(&pad_cd, i) == NULL)
+                       break;
+       }
+       if (i == MAXDEVS)
+               return ENXIO;
+
+       cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
+       cf->cf_name = pad_cd.cd_name;
+       cf->cf_atname = pad_cd.cd_name;
+       cf->cf_unit = i;
+       cf->cf_fstate = FSTATE_STAR;
+
+       paddev = config_attach_pseudo(cf);
+       sc = device_private(paddev);
+       sc->sc_dev = paddev;
+       sc->sc_dying = false;
+
+       if (sc->sc_open == 1)
+               return EBUSY;
+
+       error = fd_allocfile(&fp, &fd);
+       if (error) {
+               config_detach(sc->sc_dev, 0);
+               return error;
+       }
+
        if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
            &sc->sc_encodings) != 0) {
-               aprint_error_dev(self, "couldn't create encodings\n");
-               return;
+               aprint_error_dev(sc->sc_dev, "couldn't create encodings\n");
+               config_detach(sc->sc_dev, 0);
+               return EINVAL;
        }
 
-       cv_init(&sc->sc_condvar, device_xname(self));
+       cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
        mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
        mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
 
@@ -279,77 +363,94 @@
        sc->sc_rpos = sc->sc_wpos = 0;
        sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
 
-       if (!pmf_device_register(self, NULL, NULL))
-               aprint_error_dev(self, "couldn't establish power handler\n");
+       if (!pmf_device_register(sc->sc_dev, NULL, NULL))
+               aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
+
+       error = fd_clone(fp, fd, flags, &pad_fileops, sc);
+       KASSERT(error == EMOVEFD);
+       
+       sc->sc_open = 1;
+
+       return error;
+}
+
+static int
+pad_close(struct file *fp)
+{
+       pad_softc_t *sc;
 
-       pad_is_attached = true;
-       return;
+       sc = fp->f_pad;
+       if (sc == NULL)
+               return ENXIO;
+
+       config_detach(sc->sc_dev, DETACH_FORCE);
+
+       fp->f_pad = NULL;
+
+       return 0;
+}
+
+static int
+pad_poll(struct file *fp, int events)
+{
+       return ENODEV;
 }
 
 static int
-pad_detach(device_t self, int flags)
+pad_kqfilter(struct file *fp, struct knote *kn)
 {
-       pad_softc_t *sc = device_private(self);
-       int cmaj, mn, rc;
+       struct pad_softc *sc;
+       dev_t dev;
+       
+       sc = fp->f_pad;
+       if (sc == NULL)
+               return EIO;
 
-       if (!pad_is_attached)
-               return ENXIO;
+       dev = makedev(cdevsw_lookup_major(&pad_cdevsw), device_unit(sc->sc_dev));
 
-       cmaj = cdevsw_lookup_major(&pad_cdevsw);
-       mn = device_unit(self);
-       vdevgone(cmaj, mn, mn, VCHR);
+       return seltrue_kqfilter(dev, kn);
+}
 
-       if ((rc = config_detach_children(self, flags)) != 0)
-               return rc;
-
-       pmf_device_deregister(self);
+static int
+pad_ioctl(struct file *fp, u_long cmd, void *data)
+{
+       return ENODEV;
+}
 



Home | Main Index | Thread Index | Old Index