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
26 * $FreeBSD: src/sys/dev/sound/pcm/sndstat.c,v 1.20.2.2 2005/12/30 19:55:54 netchild Exp $
29 #include <dev/sound/pcm/sound.h>
30 #include <dev/sound/pcm/vchan.h>
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 dev_ops sndstat_cdevsw = {
49 /* .d_flags = D_NEEDGIANT, */
50 .d_open = sndstat_open,
51 .d_close = sndstat_close,
52 .d_read = sndstat_read,
55 struct sndstat_entry {
56 SLIST_ENTRY(sndstat_entry) link;
59 sndstat_handler handler;
64 static struct lock sndstat_lock;
66 static struct sbuf sndstat_sbuf;
67 static int sndstat_isopen = 0;
68 static int sndstat_bufptr;
69 static int sndstat_maxunit = -1;
70 static int sndstat_files = 0;
72 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
74 static int sndstat_verbose = 1;
76 TUNABLE_INT("hw.snd.verbose", &sndstat_verbose);
78 TUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose);
81 static int sndstat_prepare(struct sbuf *s);
84 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
88 verbose = sndstat_verbose;
89 error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req);
90 if (error == 0 && req->newptr != NULL) {
91 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
92 if (verbose < 0 || verbose > 3)
95 sndstat_verbose = verbose;
96 lockmgr(&sndstat_lock, LK_RELEASE);
100 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
101 0, sizeof(int), sysctl_hw_sndverbose, "I", "");
104 sndstat_open(struct dev_open_args *ap)
108 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
109 if (sndstat_isopen) {
110 lockmgr(&sndstat_lock, LK_RELEASE);
114 lockmgr(&sndstat_lock, LK_RELEASE);
115 if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) {
120 error = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
123 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
125 lockmgr(&sndstat_lock, LK_RELEASE);
131 sndstat_close(struct dev_close_args *ap)
133 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
134 if (!sndstat_isopen) {
135 lockmgr(&sndstat_lock, LK_RELEASE);
138 sbuf_delete(&sndstat_sbuf);
141 lockmgr(&sndstat_lock, LK_RELEASE);
146 sndstat_read(struct dev_read_args *ap)
148 struct uio *buf = ap->a_uio;
151 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
152 if (!sndstat_isopen) {
153 lockmgr(&sndstat_lock, LK_RELEASE);
156 l = (int)szmin(buf->uio_resid,
157 sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
159 err = uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr,
166 lockmgr(&sndstat_lock, LK_RELEASE);
170 /************************************************************************/
172 static struct sndstat_entry *
173 sndstat_find(int type, int unit)
175 struct sndstat_entry *ent;
177 SLIST_FOREACH(ent, &sndstat_devlist, link) {
178 if (ent->type == type && ent->unit == unit)
186 sndstat_acquire(void)
188 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
189 if (sndstat_isopen) {
190 lockmgr(&sndstat_lock, LK_RELEASE);
194 lockmgr(&sndstat_lock, LK_RELEASE);
199 sndstat_release(void)
201 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
202 if (!sndstat_isopen) {
203 lockmgr(&sndstat_lock, LK_RELEASE);
207 lockmgr(&sndstat_lock, LK_RELEASE);
212 sndstat_register(device_t dev, char *str, sndstat_handler handler)
214 struct sndstat_entry *ent;
219 unit = device_get_unit(dev);
220 devtype = device_get_name(dev);
221 if (!strcmp(devtype, "pcm"))
223 else if (!strcmp(devtype, "midi"))
225 else if (!strcmp(devtype, "sequencer"))
226 type = SS_TYPE_SEQUENCER;
230 type = SS_TYPE_MODULE;
234 ent = kmalloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK);
240 ent->handler = handler;
242 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
243 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
244 if (type == SS_TYPE_MODULE)
246 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
247 lockmgr(&sndstat_lock, LK_RELEASE);
253 sndstat_registerfile(char *str)
255 return sndstat_register(NULL, str, NULL);
259 sndstat_unregister(device_t dev)
261 struct sndstat_entry *ent;
263 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
264 SLIST_FOREACH(ent, &sndstat_devlist, link) {
265 if (ent->dev == dev) {
266 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
267 lockmgr(&sndstat_lock, LK_RELEASE);
268 kfree(ent, M_DEVBUF);
273 lockmgr(&sndstat_lock, LK_RELEASE);
279 sndstat_unregisterfile(char *str)
281 struct sndstat_entry *ent;
283 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
284 SLIST_FOREACH(ent, &sndstat_devlist, link) {
285 if (ent->dev == NULL && ent->str == str) {
286 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
288 lockmgr(&sndstat_lock, LK_RELEASE);
289 kfree(ent, M_DEVBUF);
294 lockmgr(&sndstat_lock, LK_RELEASE);
299 /************************************************************************/
302 sndstat_prepare(struct sbuf *s)
304 struct sndstat_entry *ent;
307 sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n");
308 if (SLIST_EMPTY(&sndstat_devlist)) {
309 sbuf_printf(s, "No devices installed.\n");
314 sbuf_printf(s, "Installed devices:\n");
316 for (i = 0; i <= sndstat_maxunit; i++) {
317 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
318 ent = sndstat_find(j, i);
321 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
322 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
323 sbuf_printf(s, " %s", ent->str);
325 ent->handler(s, ent->dev, sndstat_verbose);
327 sbuf_printf(s, " [no handler]");
328 sbuf_printf(s, "\n");
332 if (sndstat_verbose >= 3 && sndstat_files > 0) {
333 sbuf_printf(s, "\nFile Versions:\n");
335 SLIST_FOREACH(ent, &sndstat_devlist, link) {
336 if (ent->dev == NULL && ent->str != NULL)
337 sbuf_printf(s, "%s\n", ent->str);
348 lockinit(&sndstat_lock, "sndstat", 0, 0);
349 if (make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
350 UID_ROOT, GID_WHEEL, 0444, "sndstat") == NULL) {
359 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
360 if (sndstat_isopen) {
361 lockmgr(&sndstat_lock, LK_RELEASE);
365 //dev_ops_remove(&sndstat_cdevsw, -1, SND_DEV_STATUS);
366 dev_ops_remove_all(&sndstat_cdevsw);
367 lockmgr(&sndstat_lock, LK_RELEASE);
372 sndstat_sysinit(void *p)
378 sndstat_sysuninit(void *p)
382 error = sndstat_uninit();
383 KASSERT(error == 0, ("%s: error = %d", __func__, error));
386 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
387 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);