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
40 #define SS_TYPE_FIRST 1
42 #define SS_TYPE_MIDI 2
43 #define SS_TYPE_SEQUENCER 3
44 #define SS_TYPE_LAST 3
46 static d_open_t sndstat_open;
47 static d_close_t sndstat_close;
48 static d_read_t sndstat_read;
50 static struct cdevsw sndstat_cdevsw = {
51 .d_version = D_VERSION,
52 .d_open = sndstat_open,
53 .d_close = sndstat_close,
54 .d_read = sndstat_read,
58 struct sndstat_entry {
59 SLIST_ENTRY(sndstat_entry) link;
62 sndstat_handler handler;
66 static struct mtx sndstat_lock;
67 static struct sbuf sndstat_sbuf;
68 static struct cdev *sndstat_dev = NULL;
69 static int sndstat_bufptr = -1;
70 static int sndstat_maxunit = -1;
71 static int sndstat_files = 0;
73 #define SNDSTAT_PID(x) ((pid_t)((intptr_t)((x)->si_drv1)))
74 #define SNDSTAT_PID_SET(x, y) (x)->si_drv1 = (void *)((intptr_t)(y))
75 #define SNDSTAT_FLUSH() do { \
76 if (sndstat_bufptr != -1) { \
77 sbuf_delete(&sndstat_sbuf); \
78 sndstat_bufptr = -1; \
82 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(sndstat_devlist);
85 TUNABLE_INT("hw.snd.verbose", &snd_verbose);
89 sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
93 if (sndstat_dev == NULL)
96 mtx_lock(&sndstat_lock);
97 val = (int)SNDSTAT_PID(sndstat_dev);
98 mtx_unlock(&sndstat_lock);
99 err = sysctl_handle_int(oidp, &val, 0, req);
100 if (err == 0 && req->newptr != NULL && val == 0) {
101 mtx_lock(&sndstat_lock);
103 SNDSTAT_PID_SET(sndstat_dev, 0);
104 mtx_unlock(&sndstat_lock);
108 SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
109 0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
112 static int sndstat_prepare(struct sbuf *s);
115 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
119 verbose = snd_verbose;
120 error = sysctl_handle_int(oidp, &verbose, 0, req);
121 if (error == 0 && req->newptr != NULL) {
122 mtx_lock(&sndstat_lock);
123 if (verbose < 0 || verbose > 4)
126 snd_verbose = verbose;
127 mtx_unlock(&sndstat_lock);
131 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
132 0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
135 sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
137 if (sndstat_dev == NULL || i_dev != sndstat_dev)
140 mtx_lock(&sndstat_lock);
141 if (SNDSTAT_PID(i_dev) != 0) {
142 mtx_unlock(&sndstat_lock);
145 SNDSTAT_PID_SET(i_dev, td->td_proc->p_pid);
146 mtx_unlock(&sndstat_lock);
147 if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
148 mtx_lock(&sndstat_lock);
149 SNDSTAT_PID_SET(i_dev, 0);
150 mtx_unlock(&sndstat_lock);
158 sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
160 if (sndstat_dev == NULL || i_dev != sndstat_dev)
163 mtx_lock(&sndstat_lock);
164 if (SNDSTAT_PID(i_dev) == 0) {
165 mtx_unlock(&sndstat_lock);
170 SNDSTAT_PID_SET(i_dev, 0);
172 mtx_unlock(&sndstat_lock);
178 sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
182 if (sndstat_dev == NULL || i_dev != sndstat_dev)
185 mtx_lock(&sndstat_lock);
186 if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid ||
187 sndstat_bufptr == -1) {
188 mtx_unlock(&sndstat_lock);
191 mtx_unlock(&sndstat_lock);
193 if (sndstat_bufptr == 0) {
194 err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
196 mtx_lock(&sndstat_lock);
198 mtx_unlock(&sndstat_lock);
203 l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
204 err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
210 /************************************************************************/
212 static struct sndstat_entry *
213 sndstat_find(int type, int unit)
215 struct sndstat_entry *ent;
217 SLIST_FOREACH(ent, &sndstat_devlist, link) {
218 if (ent->type == type && ent->unit == unit)
226 sndstat_acquire(struct thread *td)
228 if (sndstat_dev == NULL)
231 mtx_lock(&sndstat_lock);
232 if (SNDSTAT_PID(sndstat_dev) != 0) {
233 mtx_unlock(&sndstat_lock);
236 SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
237 mtx_unlock(&sndstat_lock);
242 sndstat_release(struct thread *td)
244 if (sndstat_dev == NULL)
247 mtx_lock(&sndstat_lock);
248 if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
249 mtx_unlock(&sndstat_lock);
252 SNDSTAT_PID_SET(sndstat_dev, 0);
253 mtx_unlock(&sndstat_lock);
258 sndstat_register(device_t dev, char *str, sndstat_handler handler)
260 struct sndstat_entry *ent;
265 unit = device_get_unit(dev);
266 devtype = device_get_name(dev);
267 if (!strcmp(devtype, "pcm"))
269 else if (!strcmp(devtype, "midi"))
271 else if (!strcmp(devtype, "sequencer"))
272 type = SS_TYPE_SEQUENCER;
276 type = SS_TYPE_MODULE;
280 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
285 ent->handler = handler;
287 mtx_lock(&sndstat_lock);
288 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
289 if (type == SS_TYPE_MODULE)
291 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
292 mtx_unlock(&sndstat_lock);
298 sndstat_registerfile(char *str)
300 return sndstat_register(NULL, str, NULL);
304 sndstat_unregister(device_t dev)
306 struct sndstat_entry *ent;
308 mtx_lock(&sndstat_lock);
309 SLIST_FOREACH(ent, &sndstat_devlist, link) {
310 if (ent->dev == dev) {
311 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
312 mtx_unlock(&sndstat_lock);
318 mtx_unlock(&sndstat_lock);
324 sndstat_unregisterfile(char *str)
326 struct sndstat_entry *ent;
328 mtx_lock(&sndstat_lock);
329 SLIST_FOREACH(ent, &sndstat_devlist, link) {
330 if (ent->dev == NULL && ent->str == str) {
331 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
333 mtx_unlock(&sndstat_lock);
339 mtx_unlock(&sndstat_lock);
344 /************************************************************************/
347 sndstat_prepare(struct sbuf *s)
349 struct sndstat_entry *ent;
350 struct snddev_info *d;
353 sbuf_printf(s, "FreeBSD Audio Driver (newpcm: %ubit %d/%s)\n",
354 (u_int)sizeof(intpcm32_t) << 3, SND_DRV_VERSION, MACHINE_ARCH);
355 if (SLIST_EMPTY(&sndstat_devlist)) {
356 sbuf_printf(s, "No devices installed.\n");
361 sbuf_printf(s, "Installed devices:\n");
363 for (i = 0; i <= sndstat_maxunit; i++) {
364 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
365 ent = sndstat_find(j, i);
368 d = device_get_softc(ent->dev);
369 if (!PCM_REGISTERED(d))
371 /* XXX Need Giant magic entry ??? */
372 PCM_ACQUIRE_QUICK(d);
373 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
374 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
376 sbuf_printf(s, " %s", ent->str);
378 ent->handler(s, ent->dev, snd_verbose);
379 sbuf_printf(s, "\n");
380 PCM_RELEASE_QUICK(d);
384 if (snd_verbose >= 3 && sndstat_files > 0) {
385 sbuf_printf(s, "\nFile Versions:\n");
387 SLIST_FOREACH(ent, &sndstat_devlist, link) {
388 if (ent->dev == NULL && ent->str != NULL)
389 sbuf_printf(s, "%s\n", ent->str);
400 if (sndstat_dev != NULL)
402 mtx_init(&sndstat_lock, "sndstat", "sndstat lock", MTX_DEF);
403 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
404 UID_ROOT, GID_WHEEL, 0444, "sndstat");
411 if (sndstat_dev == NULL)
414 mtx_lock(&sndstat_lock);
415 if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
416 mtx_unlock(&sndstat_lock);
422 mtx_unlock(&sndstat_lock);
424 destroy_dev(sndstat_dev);
427 mtx_destroy(&sndstat_lock);
432 sndstat_sysinit(void *p)
438 sndstat_sysuninit(void *p)
442 error = sndstat_uninit();
443 KASSERT(error == 0, ("%s: error = %d", __func__, error));
446 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
447 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);