2 * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
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
27 #include <dev/sound/pcm/sound.h>
28 #include <dev/sound/pcm/vchan.h>
29 #include <dev/sound/version.h>
34 SND_DECLARE_FILE("$FreeBSD$");
36 #define SS_TYPE_MODULE 0
37 #define SS_TYPE_FIRST 1
39 #define SS_TYPE_MIDI 2
40 #define SS_TYPE_SEQUENCER 3
41 #define SS_TYPE_LAST 3
43 static d_open_t sndstat_open;
44 static d_close_t sndstat_close;
45 static d_read_t sndstat_read;
47 static struct cdevsw sndstat_cdevsw = {
48 .d_version = D_VERSION,
49 .d_open = sndstat_open,
50 .d_close = sndstat_close,
51 .d_read = sndstat_read,
55 struct sndstat_entry {
56 SLIST_ENTRY(sndstat_entry) link;
59 sndstat_handler handler;
64 static struct mtx sndstat_lock;
66 static struct sbuf sndstat_sbuf;
67 static struct cdev *sndstat_dev = NULL;
68 static int sndstat_bufptr = -1;
69 static int sndstat_maxunit = -1;
70 static int sndstat_files = 0;
72 #define SNDSTAT_PID(x) ((pid_t)((intptr_t)((x)->si_drv1)))
73 #define SNDSTAT_PID_SET(x, y) (x)->si_drv1 = (void *)((intptr_t)(y))
74 #define SNDSTAT_FLUSH() do { \
75 if (sndstat_bufptr != -1) { \
76 sbuf_delete(&sndstat_sbuf); \
77 sndstat_bufptr = -1; \
81 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
85 TUNABLE_INT("hw.snd.verbose", &snd_verbose);
87 TUNABLE_INT_DECL("hw.snd.verbose", 1, snd_verbose);
92 sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
96 if (sndstat_dev == NULL)
99 mtx_lock(&sndstat_lock);
100 val = (int)SNDSTAT_PID(sndstat_dev);
101 mtx_unlock(&sndstat_lock);
102 err = sysctl_handle_int(oidp, &val, 0, req);
103 if (err == 0 && req->newptr != NULL && val == 0) {
104 mtx_lock(&sndstat_lock);
106 SNDSTAT_PID_SET(sndstat_dev, 0);
107 mtx_unlock(&sndstat_lock);
111 SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
112 0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
115 static int sndstat_prepare(struct sbuf *s);
118 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
122 verbose = snd_verbose;
123 error = sysctl_handle_int(oidp, &verbose, 0, req);
124 if (error == 0 && req->newptr != NULL) {
125 mtx_lock(&sndstat_lock);
126 if (verbose < 0 || verbose > 4)
129 snd_verbose = verbose;
130 mtx_unlock(&sndstat_lock);
134 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
135 0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
138 sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
140 if (sndstat_dev == NULL || i_dev != sndstat_dev)
143 mtx_lock(&sndstat_lock);
144 if (SNDSTAT_PID(i_dev) != 0) {
145 mtx_unlock(&sndstat_lock);
148 SNDSTAT_PID_SET(i_dev, td->td_proc->p_pid);
149 mtx_unlock(&sndstat_lock);
150 if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
151 mtx_lock(&sndstat_lock);
152 SNDSTAT_PID_SET(i_dev, 0);
153 mtx_unlock(&sndstat_lock);
161 sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
163 if (sndstat_dev == NULL || i_dev != sndstat_dev)
166 mtx_lock(&sndstat_lock);
167 if (SNDSTAT_PID(i_dev) == 0) {
168 mtx_unlock(&sndstat_lock);
173 SNDSTAT_PID_SET(i_dev, 0);
175 mtx_unlock(&sndstat_lock);
181 sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
185 if (sndstat_dev == NULL || i_dev != sndstat_dev)
188 mtx_lock(&sndstat_lock);
189 if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid ||
190 sndstat_bufptr == -1) {
191 mtx_unlock(&sndstat_lock);
194 mtx_unlock(&sndstat_lock);
196 if (sndstat_bufptr == 0) {
197 err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
199 mtx_lock(&sndstat_lock);
201 mtx_unlock(&sndstat_lock);
206 l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
207 err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
213 /************************************************************************/
215 static struct sndstat_entry *
216 sndstat_find(int type, int unit)
218 struct sndstat_entry *ent;
220 SLIST_FOREACH(ent, &sndstat_devlist, link) {
221 if (ent->type == type && ent->unit == unit)
229 sndstat_acquire(struct thread *td)
231 if (sndstat_dev == NULL)
234 mtx_lock(&sndstat_lock);
235 if (SNDSTAT_PID(sndstat_dev) != 0) {
236 mtx_unlock(&sndstat_lock);
239 SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
240 mtx_unlock(&sndstat_lock);
245 sndstat_release(struct thread *td)
247 if (sndstat_dev == NULL)
250 mtx_lock(&sndstat_lock);
251 if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
252 mtx_unlock(&sndstat_lock);
255 SNDSTAT_PID_SET(sndstat_dev, 0);
256 mtx_unlock(&sndstat_lock);
261 sndstat_register(device_t dev, char *str, sndstat_handler handler)
263 struct sndstat_entry *ent;
268 unit = device_get_unit(dev);
269 devtype = device_get_name(dev);
270 if (!strcmp(devtype, "pcm"))
272 else if (!strcmp(devtype, "midi"))
274 else if (!strcmp(devtype, "sequencer"))
275 type = SS_TYPE_SEQUENCER;
279 type = SS_TYPE_MODULE;
283 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
288 ent->handler = handler;
290 mtx_lock(&sndstat_lock);
291 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
292 if (type == SS_TYPE_MODULE)
294 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
295 mtx_unlock(&sndstat_lock);
301 sndstat_registerfile(char *str)
303 return sndstat_register(NULL, str, NULL);
307 sndstat_unregister(device_t dev)
309 struct sndstat_entry *ent;
311 mtx_lock(&sndstat_lock);
312 SLIST_FOREACH(ent, &sndstat_devlist, link) {
313 if (ent->dev == dev) {
314 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
315 mtx_unlock(&sndstat_lock);
321 mtx_unlock(&sndstat_lock);
327 sndstat_unregisterfile(char *str)
329 struct sndstat_entry *ent;
331 mtx_lock(&sndstat_lock);
332 SLIST_FOREACH(ent, &sndstat_devlist, link) {
333 if (ent->dev == NULL && ent->str == str) {
334 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
336 mtx_unlock(&sndstat_lock);
342 mtx_unlock(&sndstat_lock);
347 /************************************************************************/
350 sndstat_prepare(struct sbuf *s)
352 struct sndstat_entry *ent;
353 struct snddev_info *d;
356 sbuf_printf(s, "FreeBSD Audio Driver (newpcm: %ubit %d/%s)\n",
357 (u_int)sizeof(intpcm_t) << 3, SND_DRV_VERSION, MACHINE_ARCH);
358 if (SLIST_EMPTY(&sndstat_devlist)) {
359 sbuf_printf(s, "No devices installed.\n");
364 sbuf_printf(s, "Installed devices:\n");
366 for (i = 0; i <= sndstat_maxunit; i++) {
367 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
368 ent = sndstat_find(j, i);
371 d = device_get_softc(ent->dev);
372 if (!PCM_REGISTERED(d))
374 /* XXX Need Giant magic entry ??? */
375 PCM_ACQUIRE_QUICK(d);
376 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
377 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
378 sbuf_printf(s, " %s [%s]", ent->str,
379 (d->flags & SD_F_MPSAFE) ? "MPSAFE" : "GIANT");
381 ent->handler(s, ent->dev, snd_verbose);
383 sbuf_printf(s, " [no handler]");
384 sbuf_printf(s, "\n");
385 PCM_RELEASE_QUICK(d);
389 if (snd_verbose >= 3 && sndstat_files > 0) {
390 sbuf_printf(s, "\nFile Versions:\n");
392 SLIST_FOREACH(ent, &sndstat_devlist, link) {
393 if (ent->dev == NULL && ent->str != NULL)
394 sbuf_printf(s, "%s\n", ent->str);
405 if (sndstat_dev != NULL)
407 mtx_init(&sndstat_lock, "sndstat", "sndstat lock", MTX_DEF);
408 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
409 UID_ROOT, GID_WHEEL, 0444, "sndstat");
416 if (sndstat_dev == NULL)
419 mtx_lock(&sndstat_lock);
420 if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
421 mtx_unlock(&sndstat_lock);
427 mtx_unlock(&sndstat_lock);
429 destroy_dev(sndstat_dev);
432 mtx_destroy(&sndstat_lock);
437 sndstat_sysinit(void *p)
443 sndstat_sysuninit(void *p)
447 error = sndstat_uninit();
448 KASSERT(error == 0, ("%s: error = %d", __func__, error));
451 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
452 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);