Source-Changes-HG archive

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

[src/isaki-audio2]: src/sys/dev/pad Revive clonify.



details:   https://anonhg.NetBSD.org/src/rev/41fe33380201
branches:  isaki-audio2
changeset: 998544:41fe33380201
user:      isaki <isaki%NetBSD.org@localhost>
date:      Mon May 06 13:40:03 2019 +0000

description:
Revive clonify.
XXX should clean code more.

diffstat:

 sys/dev/pad/pad.c |  453 +++++++++++++++++++++++++++++++++++------------------
 1 files changed, 300 insertions(+), 153 deletions(-)

diffs (truncated from 654 to 300 lines):

diff -r ed5737341f51 -r 41fe33380201 sys/dev/pad/pad.c
--- a/sys/dev/pad/pad.c Sun May 05 05:59:40 2019 +0000
+++ b/sys/dev/pad/pad.c Mon May 06 13:40:03 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pad.c,v 1.58.2.3 2019/05/05 02:01:34 isaki Exp $ */
+/* $NetBSD: pad.c,v 1.58.2.4 2019/05/06 13:40:03 isaki Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -27,23 +27,25 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.58.2.3 2019/05/05 02:01:34 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.58.2.4 2019/05/06 13:40:03 isaki 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>
-#include <sys/atomic.h>
-#include <sys/time.h>
 
 #include <dev/audio/audio_if.h>
 #include <dev/audio/audiovar.h>
@@ -57,6 +59,8 @@
 #define DPRINTF(fmt...) /**/
 #endif
 
+#define MAXDEVS                128
+#define PADCLONER      254
 #define PADUNIT(x)     minor(x)
 
 #define PADFREQ                44100
@@ -64,6 +68,7 @@
 #define PADPREC                16
 
 extern struct cfdriver pad_cd;
+kmutex_t padconfig;
 
 typedef struct pad_block {
        uint8_t         *pb_ptr;
@@ -85,8 +90,8 @@
 
 static int     pad_query_format(void *, audio_format_query_t *);
 static int     pad_set_format(void *, int,
-                               const audio_params_t *, const audio_params_t *,
-                           audio_filter_reg_t *, audio_filter_reg_t *);
+                   const audio_params_t *, const audio_params_t *,
+                   audio_filter_reg_t *, audio_filter_reg_t *);
 static int     pad_start_output(void *, void *, int,
                                    void (*)(void *), void *);
 static int     pad_start_input(void *, void *, int,
@@ -103,7 +108,18 @@
 static void    pad_done_output(void *);
 static void    pad_swvol_codec(audio_filter_arg_t *);
 
-static bool    pad_is_attached;        /* Do we have an audio* child? */
+static int pad_close(struct pad_softc *);
+static int pad_read(struct pad_softc *, off_t *, struct uio *, kauth_cred_t, int);
+
+static int fops_pad_close(struct file *);
+static int fops_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 = {
        .query_format = pad_query_format,
@@ -121,14 +137,9 @@
 };
 
 #define PAD_NFORMATS   1
-#if defined(PAD_SUPPORT_RECORD)
-#define PADMODE        (AUMODE_PLAY | AUMODE_RECORD)
-#else
-#define PADMODE        AUMODE_PLAY
-#endif
 static const struct audio_format pad_formats[PAD_NFORMATS] = {
        {
-               .mode           = PADMODE,
+               .mode           = AUMODE_PLAY,
                .encoding       = AUDIO_ENCODING_SLINEAR_NE,
                .validbits      = PADPREC,
                .precision      = PADPREC,
@@ -144,14 +155,14 @@
 static int     pad_add_block(pad_softc_t *, uint8_t *, int);
 static int     pad_get_block(pad_softc_t *, pad_block_t *, int);
 
-dev_type_open(pad_dev_open);
-dev_type_close(pad_dev_close);
-dev_type_read(pad_dev_read);
+dev_type_open(pad_open);
+dev_type_close(cdev_pad_close);
+dev_type_read(cdev_pad_read);
 
 const struct cdevsw pad_cdevsw = {
-       .d_open = pad_dev_open,
-       .d_close = pad_dev_close,
-       .d_read = pad_dev_read,
+       .d_open = pad_open,
+       .d_close = cdev_pad_close,
+       .d_read = cdev_pad_read,
        .d_write = nowrite,
        .d_ioctl = noioctl,
        .d_stop = nostop,
@@ -163,35 +174,36 @@
        .d_flag = D_OTHER | D_MPSAFE,
 };
 
+const struct fileops pad_fileops = {
+       .fo_name = "pad",
+       .fo_read = fops_pad_read,
+       .fo_write = pad_write,
+       .fo_ioctl = pad_ioctl,
+       .fo_fcntl = fnullop_fcntl,
+       .fo_stat = pad_stat,
+       .fo_poll = pad_poll,
+       .fo_close = fops_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;
+       int error;
 
-       aprint_debug("pad: requested %d units\n", n);
-       DPRINTF("%s: requested %d units\n", __func__, n);
-
-       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);
-       }
+       mutex_init(&padconfig, MUTEX_DEFAULT, IPL_NONE);
 
        return;
 }
@@ -260,14 +272,89 @@
 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;
-       sc->sc_open = 0;
+       return;
+}
+
+static int
+pad_detach(device_t self, int flags)
+{
+       pad_softc_t *sc;
+       int cmaj, mn;
+
+       sc = device_private(self);
+       cmaj = cdevsw_lookup_major(&pad_cdevsw);
+       mn = device_unit(sc->sc_dev);
+       if (!sc->sc_dying)
+               vdevgone(cmaj, mn, mn, VCHR);
+
+       return 0;
+}
+
+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;
+
+       error = 0;
 
-       cv_init(&sc->sc_condvar, device_xname(self));
+       mutex_enter(&padconfig);
+       if (PADUNIT(dev) == PADCLONER) {
+               for (i = 0; i < MAXDEVS; i++) {
+                       if (device_lookup(&pad_cd, i) == NULL)
+                               break;
+               }
+               if (i == MAXDEVS)
+                       goto bad;
+       } else {
+               if (PADUNIT(dev) >= MAXDEVS)
+                       goto bad;
+               i = PADUNIT(dev);
+       }
+
+       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;
+
+       bool existing = false;
+       paddev = device_lookup(&pad_cd, minor(dev));
+       if (paddev == NULL)
+               paddev = config_attach_pseudo(cf);
+       else
+               existing = true;
+       if (paddev == NULL)
+               goto bad;
+
+       sc = device_private(paddev);
+       if (sc == NULL)
+               goto bad;
+
+       if (sc->sc_open == 1) {
+               mutex_exit(&padconfig);
+               return EBUSY;
+       }
+
+       sc->sc_dev = paddev;
+       sc->sc_dying = false;
+
+       if (PADUNIT(dev) == PADCLONER) {
+               error = fd_allocfile(&fp, &fd);
+               if (error) {
+                       if (existing == false)
+                               config_detach(sc->sc_dev, 0);
+                       mutex_exit(&padconfig);
+                       return error;
+               }
+       }
+
+       cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
        mutex_init(&sc->sc_cond_lock, MUTEX_DEFAULT, IPL_NONE);
        mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
        mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
@@ -278,86 +365,172 @@
        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");
+
+       if (PADUNIT(dev) == PADCLONER) {
+               error = fd_clone(fp, fd, flags, &pad_fileops, sc);
+               KASSERT(error == EMOVEFD);
+       }       
+       sc->sc_open = 1;
+       mutex_exit(&padconfig);
+
+       return error;
+bad:
+       mutex_exit(&padconfig);
+       return ENXIO;
+}
+
+static int
+pad_close(struct pad_softc *sc)
+{
+       int rc;



Home | Main Index | Thread Index | Old Index