xref: /freebsd/sys/dev/sound/pcm/sndstat.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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 
27 #include <dev/sound/pcm/sound.h>
28 #include <dev/sound/pcm/vchan.h>
29 #ifdef	USING_MUTEX
30 #include <sys/sx.h>
31 #endif
32 
33 SND_DECLARE_FILE("$FreeBSD$");
34 
35 #define	SS_TYPE_MODULE		0
36 #define	SS_TYPE_FIRST		1
37 #define	SS_TYPE_PCM		1
38 #define	SS_TYPE_MIDI		2
39 #define	SS_TYPE_SEQUENCER	3
40 #define	SS_TYPE_LAST		3
41 
42 static d_open_t sndstat_open;
43 static d_close_t sndstat_close;
44 static d_read_t sndstat_read;
45 
46 static struct cdevsw sndstat_cdevsw = {
47 	.d_version =	D_VERSION,
48 	.d_flags =	D_NEEDGIANT,
49 	.d_open =	sndstat_open,
50 	.d_close =	sndstat_close,
51 	.d_read =	sndstat_read,
52 	.d_name =	"sndstat",
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 mtx sndstat_lock;
65 #endif
66 static struct sbuf sndstat_sbuf;
67 static struct cdev *sndstat_dev = 0;
68 static int sndstat_isopen = 0;
69 static int sndstat_bufptr;
70 static int sndstat_maxunit = -1;
71 static int sndstat_files = 0;
72 
73 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
74 
75 #ifdef SND_DEBUG
76 SYSCTL_INT(_hw_snd, OID_AUTO, sndstat_isopen, CTLFLAG_RW,
77 	&sndstat_isopen, 1, "sndstat emergency exit");
78 #endif
79 
80 int snd_verbose = 1;
81 #ifdef	USING_MUTEX
82 TUNABLE_INT("hw.snd.verbose", &snd_verbose);
83 #else
84 TUNABLE_INT_DECL("hw.snd.verbose", 1, snd_verbose);
85 #endif
86 
87 static int sndstat_prepare(struct sbuf *s);
88 
89 static int
90 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
91 {
92 	int error, verbose;
93 
94 	verbose = snd_verbose;
95 	error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req);
96 	if (error == 0 && req->newptr != NULL) {
97 		mtx_lock(&sndstat_lock);
98 		if (verbose < 0 || verbose > 4)
99 			error = EINVAL;
100 		else
101 			snd_verbose = verbose;
102 		mtx_unlock(&sndstat_lock);
103 	}
104 	return error;
105 }
106 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
107             0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
108 
109 static int
110 sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
111 {
112 	int error;
113 
114 	mtx_lock(&sndstat_lock);
115 	if (sndstat_isopen) {
116 		mtx_unlock(&sndstat_lock);
117 		return EBUSY;
118 	}
119 	sndstat_isopen = 1;
120 	mtx_unlock(&sndstat_lock);
121 	if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
122 		error = ENXIO;
123 		goto out;
124 	}
125 	sndstat_bufptr = 0;
126 	error = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
127 out:
128 	if (error) {
129 		mtx_lock(&sndstat_lock);
130 		sndstat_isopen = 0;
131 		mtx_unlock(&sndstat_lock);
132 	}
133 	return (error);
134 }
135 
136 static int
137 sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
138 {
139 	mtx_lock(&sndstat_lock);
140 	if (!sndstat_isopen) {
141 		mtx_unlock(&sndstat_lock);
142 		return EBADF;
143 	}
144 
145 	mtx_unlock(&sndstat_lock);
146 	sbuf_delete(&sndstat_sbuf);
147 
148 	mtx_lock(&sndstat_lock);
149 	sndstat_isopen = 0;
150 	mtx_unlock(&sndstat_lock);
151 
152 	return 0;
153 }
154 
155 static int
156 sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
157 {
158 	int l, err;
159 
160 	mtx_lock(&sndstat_lock);
161 	if (!sndstat_isopen) {
162 		mtx_unlock(&sndstat_lock);
163 		return EBADF;
164 	}
165 	mtx_unlock(&sndstat_lock);
166 
167     	l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
168 	err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
169 	sndstat_bufptr += l;
170 
171 	return err;
172 }
173 
174 /************************************************************************/
175 
176 static struct sndstat_entry *
177 sndstat_find(int type, int unit)
178 {
179 	struct sndstat_entry *ent;
180 
181 	SLIST_FOREACH(ent, &sndstat_devlist, link) {
182 		if (ent->type == type && ent->unit == unit)
183 			return ent;
184 	}
185 
186 	return NULL;
187 }
188 
189 int
190 sndstat_acquire(void)
191 {
192 	mtx_lock(&sndstat_lock);
193 	if (sndstat_isopen) {
194 		mtx_unlock(&sndstat_lock);
195 		return EBUSY;
196 	}
197 	sndstat_isopen = 1;
198 	mtx_unlock(&sndstat_lock);
199 	return 0;
200 }
201 
202 int
203 sndstat_release(void)
204 {
205 	mtx_lock(&sndstat_lock);
206 	if (!sndstat_isopen) {
207 		mtx_unlock(&sndstat_lock);
208 		return EBADF;
209 	}
210 	sndstat_isopen = 0;
211 	mtx_unlock(&sndstat_lock);
212 	return 0;
213 }
214 
215 int
216 sndstat_register(device_t dev, char *str, sndstat_handler handler)
217 {
218 	struct sndstat_entry *ent;
219 	const char *devtype;
220 	int type, unit;
221 
222 	if (dev) {
223 		unit = device_get_unit(dev);
224 		devtype = device_get_name(dev);
225 		if (!strcmp(devtype, "pcm"))
226 			type = SS_TYPE_PCM;
227 		else if (!strcmp(devtype, "midi"))
228 			type = SS_TYPE_MIDI;
229 		else if (!strcmp(devtype, "sequencer"))
230 			type = SS_TYPE_SEQUENCER;
231 		else
232 			return EINVAL;
233 	} else {
234 		type = SS_TYPE_MODULE;
235 		unit = -1;
236 	}
237 
238 	ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
239 	ent->dev = dev;
240 	ent->str = str;
241 	ent->type = type;
242 	ent->unit = unit;
243 	ent->handler = handler;
244 
245 	mtx_lock(&sndstat_lock);
246 	SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
247 	if (type == SS_TYPE_MODULE)
248 		sndstat_files++;
249 	sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
250 	mtx_unlock(&sndstat_lock);
251 
252 	return 0;
253 }
254 
255 int
256 sndstat_registerfile(char *str)
257 {
258 	return sndstat_register(NULL, str, NULL);
259 }
260 
261 int
262 sndstat_unregister(device_t dev)
263 {
264 	struct sndstat_entry *ent;
265 
266 	mtx_lock(&sndstat_lock);
267 	SLIST_FOREACH(ent, &sndstat_devlist, link) {
268 		if (ent->dev == dev) {
269 			SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
270 			mtx_unlock(&sndstat_lock);
271 			free(ent, M_DEVBUF);
272 
273 			return 0;
274 		}
275 	}
276 	mtx_unlock(&sndstat_lock);
277 
278 	return ENXIO;
279 }
280 
281 int
282 sndstat_unregisterfile(char *str)
283 {
284 	struct sndstat_entry *ent;
285 
286 	mtx_lock(&sndstat_lock);
287 	SLIST_FOREACH(ent, &sndstat_devlist, link) {
288 		if (ent->dev == NULL && ent->str == str) {
289 			SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
290 			sndstat_files--;
291 			mtx_unlock(&sndstat_lock);
292 			free(ent, M_DEVBUF);
293 
294 			return 0;
295 		}
296 	}
297 	mtx_unlock(&sndstat_lock);
298 
299 	return ENXIO;
300 }
301 
302 /************************************************************************/
303 
304 static int
305 sndstat_prepare(struct sbuf *s)
306 {
307 	struct sndstat_entry *ent;
308     	int i, j;
309 
310 	sbuf_printf(s, "FreeBSD Audio Driver (newpcm: %ubit)\n",
311 		(unsigned int)sizeof(intpcm_t) << 3);
312 	if (SLIST_EMPTY(&sndstat_devlist)) {
313 		sbuf_printf(s, "No devices installed.\n");
314 		sbuf_finish(s);
315     		return sbuf_len(s);
316 	}
317 
318 	sbuf_printf(s, "Installed devices:\n");
319 
320     	for (i = 0; i <= sndstat_maxunit; i++) {
321 		for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
322 			ent = sndstat_find(j, i);
323 			if (!ent)
324 				continue;
325 			sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
326 			sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
327 			sbuf_printf(s, " %s", ent->str);
328 			if (ent->handler)
329 				ent->handler(s, ent->dev, snd_verbose);
330 			else
331 				sbuf_printf(s, " [no handler]");
332 			sbuf_printf(s, "\n");
333 		}
334     	}
335 
336 	if (snd_verbose >= 3 && sndstat_files > 0) {
337 		sbuf_printf(s, "\nFile Versions:\n");
338 
339 		SLIST_FOREACH(ent, &sndstat_devlist, link) {
340 			if (ent->dev == NULL && ent->str != NULL)
341 				sbuf_printf(s, "%s\n", ent->str);
342 		}
343 	}
344 
345 	sbuf_finish(s);
346     	return sbuf_len(s);
347 }
348 
349 static int
350 sndstat_init(void)
351 {
352 	mtx_init(&sndstat_lock, "sndstat", "sndstat lock", MTX_DEF);
353 	sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS, UID_ROOT, GID_WHEEL, 0444, "sndstat");
354 
355 	return (sndstat_dev != 0)? 0 : ENXIO;
356 }
357 
358 static int
359 sndstat_uninit(void)
360 {
361 	mtx_lock(&sndstat_lock);
362 	if (sndstat_isopen) {
363 		mtx_unlock(&sndstat_lock);
364 		return EBUSY;
365 	}
366 
367 	sndstat_isopen = 1;
368 	mtx_unlock(&sndstat_lock);
369 
370 	if (sndstat_dev)
371 		destroy_dev(sndstat_dev);
372 	sndstat_dev = 0;
373 
374 	mtx_destroy(&sndstat_lock);
375 	return 0;
376 }
377 
378 static void
379 sndstat_sysinit(void *p)
380 {
381 	sndstat_init();
382 }
383 
384 static void
385 sndstat_sysuninit(void *p)
386 {
387 	int error;
388 
389 	error = sndstat_uninit();
390 	KASSERT(error == 0, ("%s: error = %d", __func__, error));
391 }
392 
393 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
394 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
395 
396 
397