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,
56 .d_flags = D_TRACKCLOSE,
59 struct sndstat_entry {
60 SLIST_ENTRY(sndstat_entry) link;
63 sndstat_handler handler;
67 static struct sx sndstat_lock;
68 static struct sbuf sndstat_sbuf;
69 static struct cdev *sndstat_dev = NULL;
70 static int sndstat_bufptr = -1;
71 static int sndstat_maxunit = -1;
72 static int sndstat_files = 0;
74 #define SNDSTAT_PID(x) ((pid_t)((intptr_t)((x)->si_drv1)))
75 #define SNDSTAT_PID_SET(x, y) (x)->si_drv1 = (void *)((intptr_t)(y))
76 #define SNDSTAT_FLUSH() do { \
77 if (sndstat_bufptr != -1) { \
78 sbuf_delete(&sndstat_sbuf); \
79 sndstat_bufptr = -1; \
83 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(sndstat_devlist);
86 TUNABLE_INT("hw.snd.verbose", &snd_verbose);
90 sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
94 if (sndstat_dev == NULL)
97 sx_xlock(&sndstat_lock);
98 val = (int)SNDSTAT_PID(sndstat_dev);
99 err = sysctl_handle_int(oidp, &val, 0, req);
100 if (err == 0 && req->newptr != NULL && val == 0) {
102 SNDSTAT_PID_SET(sndstat_dev, 0);
104 sx_unlock(&sndstat_lock);
107 SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
108 0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
111 static int sndstat_prepare(struct sbuf *s);
114 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
118 verbose = snd_verbose;
119 error = sysctl_handle_int(oidp, &verbose, 0, req);
120 if (error == 0 && req->newptr != NULL) {
121 if (verbose < 0 || verbose > 4)
124 snd_verbose = verbose;
128 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
129 0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
132 sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
134 if (sndstat_dev == NULL || i_dev != sndstat_dev)
137 sx_xlock(&sndstat_lock);
138 if (SNDSTAT_PID(i_dev) != 0) {
139 sx_unlock(&sndstat_lock);
142 SNDSTAT_PID_SET(i_dev, td->td_proc->p_pid);
143 if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
144 SNDSTAT_PID_SET(i_dev, 0);
145 sx_unlock(&sndstat_lock);
149 sx_unlock(&sndstat_lock);
154 sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
156 if (sndstat_dev == NULL || i_dev != sndstat_dev)
159 sx_xlock(&sndstat_lock);
160 if (SNDSTAT_PID(i_dev) == 0) {
161 sx_unlock(&sndstat_lock);
166 SNDSTAT_PID_SET(i_dev, 0);
168 sx_unlock(&sndstat_lock);
174 sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
178 if (sndstat_dev == NULL || i_dev != sndstat_dev)
181 sx_xlock(&sndstat_lock);
182 if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid ||
183 sndstat_bufptr == -1) {
184 sx_unlock(&sndstat_lock);
188 if (sndstat_bufptr == 0) {
189 err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
192 sx_unlock(&sndstat_lock);
197 l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
198 err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
200 sx_unlock(&sndstat_lock);
205 /************************************************************************/
207 static struct sndstat_entry *
208 sndstat_find(int type, int unit)
210 struct sndstat_entry *ent;
212 SLIST_FOREACH(ent, &sndstat_devlist, link) {
213 if (ent->type == type && ent->unit == unit)
221 sndstat_acquire(struct thread *td)
223 if (sndstat_dev == NULL)
226 sx_xlock(&sndstat_lock);
227 if (SNDSTAT_PID(sndstat_dev) != 0) {
228 sx_unlock(&sndstat_lock);
231 SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
232 sx_unlock(&sndstat_lock);
237 sndstat_release(struct thread *td)
239 if (sndstat_dev == NULL)
242 sx_xlock(&sndstat_lock);
243 if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
244 sx_unlock(&sndstat_lock);
247 SNDSTAT_PID_SET(sndstat_dev, 0);
248 sx_unlock(&sndstat_lock);
253 sndstat_register(device_t dev, char *str, sndstat_handler handler)
255 struct sndstat_entry *ent;
260 unit = device_get_unit(dev);
261 devtype = device_get_name(dev);
262 if (!strcmp(devtype, "pcm"))
264 else if (!strcmp(devtype, "midi"))
266 else if (!strcmp(devtype, "sequencer"))
267 type = SS_TYPE_SEQUENCER;
271 type = SS_TYPE_MODULE;
275 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
280 ent->handler = handler;
282 sx_xlock(&sndstat_lock);
283 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
284 if (type == SS_TYPE_MODULE)
286 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
287 sx_unlock(&sndstat_lock);
293 sndstat_registerfile(char *str)
295 return sndstat_register(NULL, str, NULL);
299 sndstat_unregister(device_t dev)
301 struct sndstat_entry *ent;
303 sx_xlock(&sndstat_lock);
304 SLIST_FOREACH(ent, &sndstat_devlist, link) {
305 if (ent->dev == dev) {
306 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
307 sx_unlock(&sndstat_lock);
313 sx_unlock(&sndstat_lock);
319 sndstat_unregisterfile(char *str)
321 struct sndstat_entry *ent;
323 sx_xlock(&sndstat_lock);
324 SLIST_FOREACH(ent, &sndstat_devlist, link) {
325 if (ent->dev == NULL && ent->str == str) {
326 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
328 sx_unlock(&sndstat_lock);
334 sx_unlock(&sndstat_lock);
339 /************************************************************************/
342 sndstat_prepare(struct sbuf *s)
344 struct sndstat_entry *ent;
345 struct snddev_info *d;
348 sbuf_printf(s, "FreeBSD Audio Driver (newpcm: %ubit %d/%s)\n",
349 (u_int)sizeof(intpcm32_t) << 3, SND_DRV_VERSION, MACHINE_ARCH);
350 if (SLIST_EMPTY(&sndstat_devlist)) {
351 sbuf_printf(s, "No devices installed.\n");
356 sbuf_printf(s, "Installed devices:\n");
358 for (i = 0; i <= sndstat_maxunit; i++) {
359 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
360 ent = sndstat_find(j, i);
363 d = device_get_softc(ent->dev);
364 if (!PCM_REGISTERED(d))
366 /* XXX Need Giant magic entry ??? */
367 PCM_ACQUIRE_QUICK(d);
368 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
369 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
371 sbuf_printf(s, " %s", ent->str);
373 ent->handler(s, ent->dev, snd_verbose);
374 sbuf_printf(s, "\n");
375 PCM_RELEASE_QUICK(d);
379 if (snd_verbose >= 3 && sndstat_files > 0) {
380 sbuf_printf(s, "\nFile Versions:\n");
382 SLIST_FOREACH(ent, &sndstat_devlist, link) {
383 if (ent->dev == NULL && ent->str != NULL)
384 sbuf_printf(s, "%s\n", ent->str);
395 if (sndstat_dev != NULL)
397 sx_init(&sndstat_lock, "sndstat lock");
398 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
399 UID_ROOT, GID_WHEEL, 0444, "sndstat");
406 if (sndstat_dev == NULL)
409 sx_xlock(&sndstat_lock);
410 if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
411 sx_unlock(&sndstat_lock);
415 /* XXXPHO: use destroy_dev_sched() */
416 destroy_dev(sndstat_dev);
421 sx_unlock(&sndstat_lock);
422 sx_destroy(&sndstat_lock);
427 sndstat_sysinit(void *p)
433 sndstat_sysuninit(void *p)
437 error = sndstat_uninit();
438 KASSERT(error == 0, ("%s: error = %d", __func__, error));
441 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
442 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);