2001-06-16 21:25:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Cameron Grant <gandalf@vilnya.demon.co.uk>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dev/sound/pcm/sound.h>
|
|
|
|
#include <dev/sound/pcm/vchan.h>
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
SND_DECLARE_FILE("$FreeBSD$");
|
|
|
|
|
|
|
|
#define SS_TYPE_MODULE 0
|
|
|
|
#define SS_TYPE_FIRST 1
|
|
|
|
#define SS_TYPE_PCM 1
|
|
|
|
#define SS_TYPE_MIDI 2
|
|
|
|
#define SS_TYPE_SEQUENCER 3
|
|
|
|
#define SS_TYPE_LAST 3
|
2001-06-16 21:25:10 +00:00
|
|
|
|
|
|
|
static d_open_t sndstat_open;
|
|
|
|
static d_close_t sndstat_close;
|
|
|
|
static d_read_t sndstat_read;
|
|
|
|
|
|
|
|
static struct cdevsw sndstat_cdevsw = {
|
|
|
|
/* open */ sndstat_open,
|
|
|
|
/* close */ sndstat_close,
|
|
|
|
/* read */ sndstat_read,
|
|
|
|
/* write */ nowrite,
|
|
|
|
/* ioctl */ noioctl,
|
|
|
|
/* poll */ nopoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ "sndstat",
|
|
|
|
/* maj */ SND_CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
};
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
struct sndstat_entry {
|
|
|
|
SLIST_ENTRY(sndstat_entry) link;
|
|
|
|
device_t dev;
|
|
|
|
char *str;
|
|
|
|
sndstat_handler handler;
|
|
|
|
int type, unit;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef USING_MUTEX
|
|
|
|
static struct mtx sndstat_lock;
|
|
|
|
#endif
|
2001-06-16 21:25:10 +00:00
|
|
|
static struct sbuf sndstat_sbuf;
|
|
|
|
static dev_t sndstat_dev = 0;
|
|
|
|
static int sndstat_isopen = 0;
|
|
|
|
static int sndstat_bufptr;
|
2001-08-23 11:30:52 +00:00
|
|
|
static int sndstat_maxunit = -1;
|
|
|
|
static int sndstat_files = 0;
|
2001-06-16 21:25:10 +00:00
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
|
|
|
|
|
|
|
|
static int sndstat_verbose = 1;
|
2001-06-23 17:36:51 +00:00
|
|
|
#ifdef USING_MUTEX
|
2001-06-16 21:25:10 +00:00
|
|
|
TUNABLE_INT("hw.snd.verbose", &sndstat_verbose);
|
2001-06-23 17:36:51 +00:00
|
|
|
#else
|
2001-08-23 11:30:52 +00:00
|
|
|
TUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose);
|
2001-06-23 17:36:51 +00:00
|
|
|
#endif
|
2001-06-16 21:25:10 +00:00
|
|
|
|
|
|
|
static int sndstat_prepare(struct sbuf *s);
|
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
2001-08-23 11:30:52 +00:00
|
|
|
intrmask_t s;
|
2001-06-16 21:25:10 +00:00
|
|
|
int error, verbose;
|
|
|
|
|
|
|
|
verbose = sndstat_verbose;
|
|
|
|
error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req);
|
|
|
|
if (error == 0 && req->newptr != NULL) {
|
2001-08-23 11:30:52 +00:00
|
|
|
s = spltty();
|
|
|
|
mtx_lock(&sndstat_lock);
|
|
|
|
if (verbose < 0 || verbose > 3)
|
2001-06-16 21:25:10 +00:00
|
|
|
error = EINVAL;
|
2001-08-23 11:30:52 +00:00
|
|
|
else
|
|
|
|
sndstat_verbose = verbose;
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
|
|
|
|
0, sizeof(int), sysctl_hw_sndverbose, "I", "");
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
sndstat_open(dev_t i_dev, int flags, int mode, struct thread *td)
|
2001-06-16 21:25:10 +00:00
|
|
|
{
|
2001-06-23 17:36:51 +00:00
|
|
|
intrmask_t s;
|
2002-07-25 04:49:45 +00:00
|
|
|
int error;
|
2001-06-16 21:25:10 +00:00
|
|
|
|
2001-06-23 17:36:51 +00:00
|
|
|
s = spltty();
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_lock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
if (sndstat_isopen) {
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
return EBUSY;
|
2001-06-23 17:36:51 +00:00
|
|
|
}
|
2002-07-25 04:49:45 +00:00
|
|
|
sndstat_isopen = 1;
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
2001-06-23 17:36:51 +00:00
|
|
|
if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) {
|
2002-07-25 04:49:45 +00:00
|
|
|
error = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sndstat_bufptr = 0;
|
|
|
|
error = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
|
|
|
|
out:
|
|
|
|
if (error) {
|
|
|
|
s = spltty();
|
|
|
|
mtx_lock(&sndstat_lock);
|
|
|
|
sndstat_isopen = 0;
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
|
|
|
}
|
2002-07-25 04:49:45 +00:00
|
|
|
return (error);
|
2001-06-16 21:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
sndstat_close(dev_t i_dev, int flags, int mode, struct thread *td)
|
2001-06-16 21:25:10 +00:00
|
|
|
{
|
2001-06-23 17:36:51 +00:00
|
|
|
intrmask_t s;
|
|
|
|
|
|
|
|
s = spltty();
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_lock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
if (!sndstat_isopen) {
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
return EBADF;
|
2001-06-23 17:36:51 +00:00
|
|
|
}
|
2001-06-16 21:25:10 +00:00
|
|
|
sbuf_delete(&sndstat_sbuf);
|
|
|
|
sndstat_isopen = 0;
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sndstat_read(dev_t i_dev, struct uio *buf, int flag)
|
|
|
|
{
|
2001-06-23 17:36:51 +00:00
|
|
|
intrmask_t s;
|
2001-06-16 21:25:10 +00:00
|
|
|
int l, err;
|
|
|
|
|
2001-06-23 17:36:51 +00:00
|
|
|
s = spltty();
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_lock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
if (!sndstat_isopen) {
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
return EBADF;
|
2001-06-23 17:36:51 +00:00
|
|
|
}
|
2001-06-16 21:25:10 +00:00
|
|
|
l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
|
|
|
|
err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
|
|
|
|
sndstat_bufptr += l;
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static struct sndstat_entry *
|
|
|
|
sndstat_find(int type, int unit)
|
|
|
|
{
|
|
|
|
struct sndstat_entry *ent;
|
|
|
|
|
|
|
|
SLIST_FOREACH(ent, &sndstat_devlist, link) {
|
|
|
|
if (ent->type == type && ent->unit == unit)
|
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sndstat_register(device_t dev, char *str, sndstat_handler handler)
|
|
|
|
{
|
|
|
|
intrmask_t s;
|
|
|
|
struct sndstat_entry *ent;
|
|
|
|
const char *devtype;
|
|
|
|
int type, unit;
|
|
|
|
|
|
|
|
if (dev) {
|
|
|
|
unit = device_get_unit(dev);
|
|
|
|
devtype = device_get_name(dev);
|
|
|
|
if (!strcmp(devtype, "pcm"))
|
|
|
|
type = SS_TYPE_PCM;
|
|
|
|
else if (!strcmp(devtype, "midi"))
|
|
|
|
type = SS_TYPE_MIDI;
|
|
|
|
else if (!strcmp(devtype, "sequencer"))
|
|
|
|
type = SS_TYPE_SEQUENCER;
|
|
|
|
else
|
|
|
|
return EINVAL;
|
|
|
|
} else {
|
|
|
|
type = SS_TYPE_MODULE;
|
|
|
|
unit = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent = malloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK);
|
|
|
|
if (!ent)
|
|
|
|
return ENOSPC;
|
|
|
|
|
|
|
|
ent->dev = dev;
|
|
|
|
ent->str = str;
|
|
|
|
ent->type = type;
|
|
|
|
ent->unit = unit;
|
|
|
|
ent->handler = handler;
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
mtx_lock(&sndstat_lock);
|
|
|
|
SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
|
|
|
|
if (type == SS_TYPE_MODULE)
|
|
|
|
sndstat_files++;
|
|
|
|
sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sndstat_registerfile(char *str)
|
|
|
|
{
|
|
|
|
return sndstat_register(NULL, str, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sndstat_unregister(device_t dev)
|
|
|
|
{
|
|
|
|
intrmask_t s;
|
|
|
|
struct sndstat_entry *ent;
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
mtx_lock(&sndstat_lock);
|
|
|
|
SLIST_FOREACH(ent, &sndstat_devlist, link) {
|
|
|
|
if (ent->dev == dev) {
|
|
|
|
SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
2002-11-25 17:17:43 +00:00
|
|
|
free(ent, M_DEVBUF);
|
2001-08-23 11:30:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sndstat_unregisterfile(char *str)
|
|
|
|
{
|
|
|
|
intrmask_t s;
|
|
|
|
struct sndstat_entry *ent;
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
mtx_lock(&sndstat_lock);
|
|
|
|
SLIST_FOREACH(ent, &sndstat_devlist, link) {
|
|
|
|
if (ent->dev == NULL && ent->str == str) {
|
|
|
|
SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
|
|
|
|
sndstat_files--;
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
2002-11-25 17:17:43 +00:00
|
|
|
free(ent, M_DEVBUF);
|
2001-08-23 11:30:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_unlock(&sndstat_lock);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
2001-06-16 21:25:10 +00:00
|
|
|
static int
|
|
|
|
sndstat_prepare(struct sbuf *s)
|
|
|
|
{
|
2001-08-23 11:30:52 +00:00
|
|
|
struct sndstat_entry *ent;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n");
|
|
|
|
if (SLIST_EMPTY(&sndstat_devlist)) {
|
2001-06-16 21:25:10 +00:00
|
|
|
sbuf_printf(s, "No devices installed.\n");
|
|
|
|
sbuf_finish(s);
|
|
|
|
return sbuf_len(s);
|
2001-08-23 11:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sbuf_printf(s, "Installed devices:\n");
|
|
|
|
|
|
|
|
for (i = 0; i <= sndstat_maxunit; i++) {
|
|
|
|
for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
|
|
|
|
ent = sndstat_find(j, i);
|
|
|
|
if (!ent)
|
|
|
|
continue;
|
|
|
|
sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
|
|
|
|
sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
|
|
|
|
sbuf_printf(s, " %s", ent->str);
|
|
|
|
if (ent->handler)
|
|
|
|
ent->handler(s, ent->dev, sndstat_verbose);
|
|
|
|
else
|
|
|
|
sbuf_printf(s, " [no handler]");
|
|
|
|
sbuf_printf(s, "\n");
|
|
|
|
}
|
2001-06-16 21:25:10 +00:00
|
|
|
}
|
2001-08-23 11:30:52 +00:00
|
|
|
|
|
|
|
if (sndstat_verbose >= 3 && sndstat_files > 0) {
|
|
|
|
sbuf_printf(s, "\nFile Versions:\n");
|
|
|
|
|
|
|
|
SLIST_FOREACH(ent, &sndstat_devlist, link) {
|
|
|
|
if (ent->dev == NULL && ent->str != NULL)
|
|
|
|
sbuf_printf(s, "%s\n", ent->str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-16 21:25:10 +00:00
|
|
|
sbuf_finish(s);
|
|
|
|
return sbuf_len(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sndstat_init(void)
|
|
|
|
{
|
2002-04-04 21:03:38 +00:00
|
|
|
mtx_init(&sndstat_lock, "sndstat", NULL, 0);
|
2001-06-16 21:25:10 +00:00
|
|
|
sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS, UID_ROOT, GID_WHEEL, 0444, "sndstat");
|
|
|
|
|
|
|
|
return (sndstat_dev != 0)? 0 : ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sndstat_uninit(void)
|
|
|
|
{
|
2001-06-23 17:36:51 +00:00
|
|
|
intrmask_t s;
|
|
|
|
|
|
|
|
s = spltty();
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_lock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
if (sndstat_isopen) {
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_unlock(&sndstat_lock);
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-06-16 21:25:10 +00:00
|
|
|
return EBUSY;
|
2001-06-23 17:36:51 +00:00
|
|
|
}
|
2001-06-16 21:25:10 +00:00
|
|
|
|
|
|
|
if (sndstat_dev)
|
|
|
|
destroy_dev(sndstat_dev);
|
|
|
|
sndstat_dev = 0;
|
|
|
|
|
2001-06-23 17:36:51 +00:00
|
|
|
splx(s);
|
2001-08-23 11:30:52 +00:00
|
|
|
mtx_destroy(&sndstat_lock);
|
2001-06-16 21:25:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-14 20:26:03 +00:00
|
|
|
int
|
|
|
|
sndstat_busy(void)
|
|
|
|
{
|
|
|
|
return (sndstat_isopen);
|
|
|
|
}
|
|
|
|
|
2001-06-16 21:25:10 +00:00
|
|
|
static void
|
|
|
|
sndstat_sysinit(void *p)
|
|
|
|
{
|
|
|
|
sndstat_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sndstat_sysuninit(void *p)
|
|
|
|
{
|
|
|
|
sndstat_uninit();
|
|
|
|
}
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
|
|
|
|
SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
|
2001-06-16 21:25:10 +00:00
|
|
|
|
|
|
|
|