2 * Copyright (c) 2001 Cameron Grant <gandalf@vilnya.demon.co.uk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/sound/pcm/sndstat.c,v 1.4.2.2 2002/04/22 15:49:36 cg Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/sndstat.c,v 1.4 2003/07/21 05:50:36 dillon Exp $
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/pcm/vchan.h>
33 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/sndstat.c,v 1.4 2003/07/21 05:50:36 dillon Exp $");
35 #define SS_TYPE_MODULE 0
36 #define SS_TYPE_FIRST 1
38 #define SS_TYPE_MIDI 2
39 #define SS_TYPE_SEQUENCER 3
40 #define SS_TYPE_LAST 3
42 static d_open_t sndstat_open;
43 static d_close_t sndstat_close;
44 static d_read_t sndstat_read;
46 static struct cdevsw sndstat_cdevsw = {
48 /* maj */ SND_CDEV_MAJOR,
53 /* open */ sndstat_open,
54 /* close */ sndstat_close,
55 /* read */ sndstat_read,
60 /* strategy */ nostrategy,
65 struct sndstat_entry {
66 SLIST_ENTRY(sndstat_entry) link;
69 sndstat_handler handler;
73 static struct sbuf sndstat_sbuf;
74 static dev_t sndstat_dev = 0;
75 static int sndstat_isopen = 0;
76 static int sndstat_bufptr;
77 static int sndstat_maxunit = -1;
78 static int sndstat_files = 0;
80 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
82 static int sndstat_verbose = 1;
84 TUNABLE_INT("hw.snd.verbose", &sndstat_verbose);
86 TUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose);
89 static int sndstat_prepare(struct sbuf *s);
92 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
97 verbose = sndstat_verbose;
98 error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req);
99 if (error == 0 && req->newptr != NULL) {
101 if (verbose < 0 || verbose > 3)
104 sndstat_verbose = verbose;
109 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
110 0, sizeof(int), sysctl_hw_sndverbose, "I", "");
113 sndstat_open(dev_t i_dev, int flags, int mode, struct thread *td)
119 if (sndstat_isopen) {
123 if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) {
128 err = (sndstat_prepare(&sndstat_sbuf) > 0)? 0 : ENOMEM;
137 sndstat_close(dev_t i_dev, int flags, int mode, struct thread *td)
142 if (!sndstat_isopen) {
146 sbuf_delete(&sndstat_sbuf);
154 sndstat_read(dev_t i_dev, struct uio *buf, int flag)
160 if (!sndstat_isopen) {
164 l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
165 err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
172 /************************************************************************/
174 static struct sndstat_entry *
175 sndstat_find(int type, int unit)
177 struct sndstat_entry *ent;
179 SLIST_FOREACH(ent, &sndstat_devlist, link) {
180 if (ent->type == type && ent->unit == unit)
188 sndstat_register(device_t dev, char *str, sndstat_handler handler)
191 struct sndstat_entry *ent;
196 unit = device_get_unit(dev);
197 devtype = device_get_name(dev);
198 if (!strcmp(devtype, "pcm"))
200 else if (!strcmp(devtype, "midi"))
202 else if (!strcmp(devtype, "sequencer"))
203 type = SS_TYPE_SEQUENCER;
207 type = SS_TYPE_MODULE;
211 ent = malloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK);
219 ent->handler = handler;
222 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
223 if (type == SS_TYPE_MODULE)
225 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
232 sndstat_registerfile(char *str)
234 return sndstat_register(NULL, str, NULL);
238 sndstat_unregister(device_t dev)
241 struct sndstat_entry *ent;
244 SLIST_FOREACH(ent, &sndstat_devlist, link) {
245 if (ent->dev == dev) {
246 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
259 sndstat_unregisterfile(char *str)
262 struct sndstat_entry *ent;
265 SLIST_FOREACH(ent, &sndstat_devlist, link) {
266 if (ent->dev == NULL && ent->str == str) {
267 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
280 /************************************************************************/
283 sndstat_prepare(struct sbuf *s)
285 struct sndstat_entry *ent;
288 sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n");
289 if (SLIST_EMPTY(&sndstat_devlist)) {
290 sbuf_printf(s, "No devices installed.\n");
295 sbuf_printf(s, "Installed devices:\n");
297 for (i = 0; i <= sndstat_maxunit; i++) {
298 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
299 ent = sndstat_find(j, i);
302 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
303 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
304 sbuf_printf(s, " %s", ent->str);
306 ent->handler(s, ent->dev, sndstat_verbose);
308 sbuf_printf(s, " [no handler]");
309 sbuf_printf(s, "\n");
313 if (sndstat_verbose >= 3 && sndstat_files > 0) {
314 sbuf_printf(s, "\nFile Versions:\n");
316 SLIST_FOREACH(ent, &sndstat_devlist, link) {
317 if (ent->dev == NULL && ent->str != NULL)
318 sbuf_printf(s, "%s\n", ent->str);
329 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS, UID_ROOT, GID_WHEEL, 0444, "sndstat");
331 return (sndstat_dev != 0)? 0 : ENXIO;
340 if (sndstat_isopen) {
346 destroy_dev(sndstat_dev);
356 return (sndstat_isopen);
360 sndstat_sysinit(void *p)
366 sndstat_sysuninit(void *p)
371 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
372 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);