2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Copyright (c) 2001 Cameron Grant <cg@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #ifdef HAVE_KERNEL_OPTION_HEADERS
32 #include <dev/sound/pcm/sound.h>
33 #include <dev/sound/pcm/pcm.h>
34 #include <dev/sound/version.h>
37 SND_DECLARE_FILE("$FreeBSD$");
39 #define SS_TYPE_MODULE 0
41 #define SS_TYPE_MIDI 2
42 #define SS_TYPE_SEQUENCER 3
44 static d_open_t sndstat_open;
45 static void sndstat_close(void *);
46 static d_read_t sndstat_read;
47 static d_write_t sndstat_write;
49 static struct cdevsw sndstat_cdevsw = {
50 .d_version = D_VERSION,
51 .d_open = sndstat_open,
52 .d_read = sndstat_read,
53 .d_write = sndstat_write,
55 .d_flags = D_TRACKCLOSE,
58 struct sndstat_entry {
59 TAILQ_ENTRY(sndstat_entry) link;
62 sndstat_handler handler;
67 TAILQ_ENTRY(sndstat_file) entry;
73 static struct sx sndstat_lock;
74 static struct cdev *sndstat_dev;
76 #define SNDSTAT_LOCK() sx_xlock(&sndstat_lock)
77 #define SNDSTAT_UNLOCK() sx_xunlock(&sndstat_lock)
79 static TAILQ_HEAD(, sndstat_entry) sndstat_devlist = TAILQ_HEAD_INITIALIZER(sndstat_devlist);
80 static TAILQ_HEAD(, sndstat_file) sndstat_filelist = TAILQ_HEAD_INITIALIZER(sndstat_filelist);
84 static int sndstat_prepare(struct sndstat_file *);
87 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
91 verbose = snd_verbose;
92 error = sysctl_handle_int(oidp, &verbose, 0, req);
93 if (error == 0 && req->newptr != NULL) {
94 if (verbose < 0 || verbose > 4)
97 snd_verbose = verbose;
101 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RWTUN,
102 0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
105 sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
107 struct sndstat_file *pf;
109 pf = malloc(sizeof(*pf), M_DEVBUF, M_WAITOK | M_ZERO);
112 if (sbuf_new(&pf->sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
117 TAILQ_INSERT_TAIL(&sndstat_filelist, pf, entry);
120 devfs_set_cdevpriv(pf, &sndstat_close);
126 sndstat_close(void *sndstat_file)
128 struct sndstat_file *pf = (struct sndstat_file *)sndstat_file;
131 sbuf_delete(&pf->sbuf);
132 TAILQ_REMOVE(&sndstat_filelist, pf, entry);
139 sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
141 struct sndstat_file *pf;
145 err = devfs_get_cdevpriv((void **)&pf);
149 /* skip zero-length reads */
150 if (buf->uio_resid == 0)
154 if (pf->out_offset != 0) {
155 /* don't allow both reading and writing */
158 } else if (pf->in_offset == 0) {
159 err = sndstat_prepare(pf);
165 len = sbuf_len(&pf->sbuf) - pf->in_offset;
166 if (len > buf->uio_resid)
167 len = buf->uio_resid;
169 err = uiomove(sbuf_data(&pf->sbuf) + pf->in_offset, len, buf);
170 pf->in_offset += len;
177 sndstat_write(struct cdev *i_dev, struct uio *buf, int flag)
179 struct sndstat_file *pf;
184 err = devfs_get_cdevpriv((void **)&pf);
188 /* skip zero-length writes */
189 if (buf->uio_resid == 0)
192 /* don't allow writing more than 64Kbytes */
193 if (buf->uio_resid > 65536)
197 if (pf->in_offset != 0) {
198 /* don't allow both reading and writing */
201 /* only remember the last write - allows for updates */
202 sbuf_clear(&pf->sbuf);
205 if (len > buf->uio_resid)
206 len = buf->uio_resid;
208 err = uiomove(temp, len, buf);
214 if (sbuf_bcat(&pf->sbuf, temp, len) < 0) {
219 sbuf_finish(&pf->sbuf);
221 pf->out_offset = sbuf_len(&pf->sbuf);
229 /************************************************************************/
232 sndstat_register(device_t dev, char *str, sndstat_handler handler)
234 struct sndstat_entry *ent;
235 struct sndstat_entry *pre;
240 unit = device_get_unit(dev);
241 devtype = device_get_name(dev);
242 if (!strcmp(devtype, "pcm"))
244 else if (!strcmp(devtype, "midi"))
246 else if (!strcmp(devtype, "sequencer"))
247 type = SS_TYPE_SEQUENCER;
251 type = SS_TYPE_MODULE;
255 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
260 ent->handler = handler;
263 /* sorted list insertion */
264 TAILQ_FOREACH(pre, &sndstat_devlist, link) {
265 if (pre->unit > unit)
267 else if (pre->unit < unit)
269 if (pre->type > type)
271 else if (pre->type < unit)
275 TAILQ_INSERT_TAIL(&sndstat_devlist, ent, link);
277 TAILQ_INSERT_BEFORE(pre, ent, link);
285 sndstat_registerfile(char *str)
287 return (sndstat_register(NULL, str, NULL));
291 sndstat_unregister(device_t dev)
293 struct sndstat_entry *ent;
297 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
298 if (ent->dev == dev) {
299 TAILQ_REMOVE(&sndstat_devlist, ent, link);
311 sndstat_unregisterfile(char *str)
313 struct sndstat_entry *ent;
317 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
318 if (ent->dev == NULL && ent->str == str) {
319 TAILQ_REMOVE(&sndstat_devlist, ent, link);
330 /************************************************************************/
333 sndstat_prepare(struct sndstat_file *pf_self)
335 struct sbuf *s = &pf_self->sbuf;
336 struct sndstat_entry *ent;
337 struct snddev_info *d;
338 struct sndstat_file *pf;
341 /* make sure buffer is reset */
344 if (snd_verbose > 0) {
345 sbuf_printf(s, "FreeBSD Audio Driver (%ubit %d/%s)\n",
346 (u_int)sizeof(intpcm32_t) << 3, SND_DRV_VERSION,
350 /* generate list of installed devices */
352 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
353 if (ent->dev == NULL)
355 d = device_get_softc(ent->dev);
356 if (!PCM_REGISTERED(d))
359 sbuf_printf(s, "Installed devices:\n");
360 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
361 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
363 sbuf_printf(s, " %s", ent->str);
365 /* XXX Need Giant magic entry ??? */
366 PCM_ACQUIRE_QUICK(d);
367 ent->handler(s, ent->dev, snd_verbose);
368 PCM_RELEASE_QUICK(d);
370 sbuf_printf(s, "\n");
373 sbuf_printf(s, "No devices installed.\n");
375 /* append any input from userspace */
377 TAILQ_FOREACH(pf, &sndstat_filelist, entry) {
380 if (pf->out_offset == 0)
383 sbuf_printf(s, "Installed devices from userspace:\n");
384 sbuf_bcat(s, sbuf_data(&pf->sbuf),
385 sbuf_len(&pf->sbuf));
388 sbuf_printf(s, "No devices installed from userspace.\n");
390 /* append any file versions */
391 if (snd_verbose >= 3) {
393 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
394 if (ent->dev == NULL && ent->str != NULL) {
396 sbuf_printf(s, "\nFile Versions:\n");
397 sbuf_printf(s, "%s\n", ent->str);
401 sbuf_printf(s, "\nNo file versions.\n");
404 return (sbuf_len(s));
408 sndstat_sysinit(void *p)
410 sx_init(&sndstat_lock, "sndstat lock");
411 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
412 UID_ROOT, GID_WHEEL, 0644, "sndstat");
414 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
417 sndstat_sysuninit(void *p)
419 if (sndstat_dev != NULL) {
420 /* destroy_dev() will wait for all references to go away */
421 destroy_dev(sndstat_dev);
423 sx_destroy(&sndstat_lock);
425 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);