1 /*-
2 * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/dev/sound/pcm/sndstat.c,v 1.20.2.2 2005/12/30 19:55:54 netchild Exp $
27 */
28
29 #include <dev/sound/pcm/sound.h>
30 #include <dev/sound/pcm/vchan.h>
31 #ifdef USING_MUTEX
32 #include <sys/lock.h>
33 #endif
34
35
36 #define SS_TYPE_MODULE 0
37 #define SS_TYPE_FIRST 1
38 #define SS_TYPE_PCM 1
39 #define SS_TYPE_MIDI 2
40 #define SS_TYPE_SEQUENCER 3
41 #define SS_TYPE_LAST 3
42
43 static d_open_t sndstat_open;
44 static d_close_t sndstat_close;
45 static d_read_t sndstat_read;
46
47 static struct dev_ops sndstat_cdevsw = {
48 { "sndstat", 0, 0 },
49 /* .d_flags = D_NEEDGIANT, */
50 .d_open = sndstat_open,
51 .d_close = sndstat_close,
52 .d_read = sndstat_read,
53 };
54
55 struct sndstat_entry {
56 SLIST_ENTRY(sndstat_entry) link;
57 device_t dev;
58 char *str;
59 sndstat_handler handler;
60 int type, unit;
61 };
62
63 #ifdef USING_MUTEX
64 static struct lock sndstat_lock;
65 #endif
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;
71
72 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
73
74 static int sndstat_verbose = 1;
75 #ifdef USING_MUTEX
76 TUNABLE_INT("hw.snd.verbose", &sndstat_verbose);
77 #else
78 TUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose);
79 #endif
80
81 static int sndstat_prepare(struct sbuf *s);
82
83 static int
84 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
85 {
86 int error, verbose;
87
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)
93 error = EINVAL;
94 else
95 sndstat_verbose = verbose;
96 lockmgr(&sndstat_lock, LK_RELEASE);
97 }
98 return error;
99 }
100 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
101 0, sizeof(int), sysctl_hw_sndverbose, "I", "");
102
103 static int
104 sndstat_open(struct dev_open_args *ap)
105 {
106 int error;
107
108 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
109 if (sndstat_isopen) {
110 lockmgr(&sndstat_lock, LK_RELEASE);
111 return EBUSY;
112 }
113 sndstat_isopen = 1;
114 lockmgr(&sndstat_lock, LK_RELEASE);
115 if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) {
116 error = ENXIO;
117 goto out;
118 }
119 sndstat_bufptr = 0;
120 error = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
121 out:
122 if (error) {
123 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
124 sndstat_isopen = 0;
125 lockmgr(&sndstat_lock, LK_RELEASE);
126 }
127 return (error);
128 }
129
130 static int
131 sndstat_close(struct dev_close_args *ap)
132 {
133 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
134 if (!sndstat_isopen) {
135 lockmgr(&sndstat_lock, LK_RELEASE);
136 return EBADF;
137 }
138 sbuf_delete(&sndstat_sbuf);
139 sndstat_isopen = 0;
140
141 lockmgr(&sndstat_lock, LK_RELEASE);
142 return 0;
143 }
144
145 static int
146 sndstat_read(struct dev_read_args *ap)
147 {
148 struct uio *buf = ap->a_uio;
149 int l, err;
150
151 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
152 if (!sndstat_isopen) {
153 lockmgr(&sndstat_lock, LK_RELEASE);
154 return EBADF;
155 }
156 l = (int)szmin(buf->uio_resid,
157 sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
158 if (l > 0) {
159 err = uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr,
160 l, buf);
161 } else {
162 err = 0;
163 }
164 sndstat_bufptr += l;
165
166 lockmgr(&sndstat_lock, LK_RELEASE);
167 return err;
168 }
169
170 /************************************************************************/
171
172 static struct sndstat_entry *
173 sndstat_find(int type, int unit)
174 {
175 struct sndstat_entry *ent;
176
177 SLIST_FOREACH(ent, &sndstat_devlist, link) {
178 if (ent->type == type && ent->unit == unit)
179 return ent;
180 }
181
182 return NULL;
183 }
184
185 int
186 sndstat_acquire(void)
187 {
188 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
189 if (sndstat_isopen) {
190 lockmgr(&sndstat_lock, LK_RELEASE);
191 return EBUSY;
192 }
193 sndstat_isopen = 1;
194 lockmgr(&sndstat_lock, LK_RELEASE);
195 return 0;
196 }
197
198 int
199 sndstat_release(void)
200 {
201 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
202 if (!sndstat_isopen) {
203 lockmgr(&sndstat_lock, LK_RELEASE);
204 return EBADF;
205 }
206 sndstat_isopen = 0;
207 lockmgr(&sndstat_lock, LK_RELEASE);
208 return 0;
209 }
210
211 int
212 sndstat_register(device_t dev, char *str, sndstat_handler handler)
213 {
214 struct sndstat_entry *ent;
215 const char *devtype;
216 int type, unit;
217
218 if (dev) {
219 unit = device_get_unit(dev);
220 devtype = device_get_name(dev);
221 if (!strcmp(devtype, "pcm"))
222 type = SS_TYPE_PCM;
223 else if (!strcmp(devtype, "midi"))
224 type = SS_TYPE_MIDI;
225 else if (!strcmp(devtype, "sequencer"))
226 type = SS_TYPE_SEQUENCER;
227 else
228 return EINVAL;
229 } else {
230 type = SS_TYPE_MODULE;
231 unit = -1;
232 }
233
234 ent = kmalloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK);
235
236 ent->dev = dev;
237 ent->str = str;
238 ent->type = type;
239 ent->unit = unit;
240 ent->handler = handler;
241
242 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
243 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
244 if (type == SS_TYPE_MODULE)
245 sndstat_files++;
246 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
247 lockmgr(&sndstat_lock, LK_RELEASE);
248
249 return 0;
250 }
251
252 int
253 sndstat_registerfile(char *str)
254 {
255 return sndstat_register(NULL, str, NULL);
256 }
257
258 int
259 sndstat_unregister(device_t dev)
260 {
261 struct sndstat_entry *ent;
262
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);
269
270 return 0;
271 }
272 }
273 lockmgr(&sndstat_lock, LK_RELEASE);
274
275 return ENXIO;
276 }
277
278 int
279 sndstat_unregisterfile(char *str)
280 {
281 struct sndstat_entry *ent;
282
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);
287 sndstat_files--;
288 lockmgr(&sndstat_lock, LK_RELEASE);
289 kfree(ent, M_DEVBUF);
290
291 return 0;
292 }
293 }
294 lockmgr(&sndstat_lock, LK_RELEASE);
295
296 return ENXIO;
297 }
298
299 /************************************************************************/
300
301 static int
302 sndstat_prepare(struct sbuf *s)
303 {
304 struct sndstat_entry *ent;
305 int i, j;
306
307 sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n");
308 if (SLIST_EMPTY(&sndstat_devlist)) {
309 sbuf_printf(s, "No devices installed.\n");
310 sbuf_finish(s);
311 return sbuf_len(s);
312 }
313
314 sbuf_printf(s, "Installed devices:\n");
315
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);
319 if (!ent)
320 continue;
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);
324 if (ent->handler)
325 ent->handler(s, ent->dev, sndstat_verbose);
326 else
327 sbuf_printf(s, " [no handler]");
328 sbuf_printf(s, "\n");
329 }
330 }
331
332 if (sndstat_verbose >= 3 && sndstat_files > 0) {
333 sbuf_printf(s, "\nFile Versions:\n");
334
335 SLIST_FOREACH(ent, &sndstat_devlist, link) {
336 if (ent->dev == NULL && ent->str != NULL)
337 sbuf_printf(s, "%s\n", ent->str);
338 }
339 }
340
341 sbuf_finish(s);
342 return sbuf_len(s);
343 }
344
345 static int
346 sndstat_init(void)
347 {
348 lockinit(&sndstat_lock, "sndstat", 0, 0);
349 if (make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
350 UID_ROOT, GID_WHEEL, 0444, "sndstat") == NULL) {
351 return ENXIO;
352 }
353 return 0;
354 }
355
356 static int
357 sndstat_uninit(void)
358 {
359 lockmgr(&sndstat_lock, LK_EXCLUSIVE);
360 if (sndstat_isopen) {
361 lockmgr(&sndstat_lock, LK_RELEASE);
362 return EBUSY;
363 }
364
365 //dev_ops_remove(&sndstat_cdevsw, -1, SND_DEV_STATUS);
366 dev_ops_remove_all(&sndstat_cdevsw);
367 lockmgr(&sndstat_lock, LK_RELEASE);
368 return 0;
369 }
370
371 static void
372 sndstat_sysinit(void *p)
373 {
374 sndstat_init();
375 }
376
377 static void
378 sndstat_sysuninit(void *p)
379 {
380 int error;
381
382 error = sndstat_uninit();
383 KASSERT(error == 0, ("%s: error = %d", __func__, error));
384 }
385
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);
388
389
Cache object: b87bd16e22c815bc48db879d864610df
|