sndstat.c revision 234932
1/*-
2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Copyright (c) 2001 Cameron Grant <cg@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28#ifdef HAVE_KERNEL_OPTION_HEADERS
29#include "opt_snd.h"
30#endif
31
32#include <dev/sound/pcm/sound.h>
33#include <dev/sound/pcm/pcm.h>
34#include <dev/sound/version.h>
35#include <sys/sx.h>
36
37SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/sndstat.c 234932 2012-05-02 19:32:11Z pho $");
38
39#define	SS_TYPE_MODULE		0
40#define	SS_TYPE_FIRST		1
41#define	SS_TYPE_PCM		1
42#define	SS_TYPE_MIDI		2
43#define	SS_TYPE_SEQUENCER	3
44#define	SS_TYPE_LAST		3
45
46static d_open_t sndstat_open;
47static d_close_t sndstat_close;
48static d_read_t sndstat_read;
49
50static struct cdevsw sndstat_cdevsw = {
51	.d_version =	D_VERSION,
52	.d_open =	sndstat_open,
53	.d_close =	sndstat_close,
54	.d_read =	sndstat_read,
55	.d_name =	"sndstat",
56	.d_flags =	D_TRACKCLOSE,
57};
58
59struct sndstat_entry {
60	SLIST_ENTRY(sndstat_entry) link;
61	device_t dev;
62	char *str;
63	sndstat_handler handler;
64	int type, unit;
65};
66
67static struct sx sndstat_lock;
68static struct sbuf sndstat_sbuf;
69static struct cdev *sndstat_dev = NULL;
70static int sndstat_bufptr = -1;
71static int sndstat_maxunit = -1;
72static int sndstat_files = 0;
73
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;					\
80	}								\
81} while (0)
82
83static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(sndstat_devlist);
84
85int snd_verbose = 0;
86TUNABLE_INT("hw.snd.verbose", &snd_verbose);
87
88#ifdef SND_DEBUG
89static int
90sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
91{
92	int err, val;
93
94	if (sndstat_dev == NULL)
95		return (EINVAL);
96
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) {
101		SNDSTAT_FLUSH();
102		SNDSTAT_PID_SET(sndstat_dev, 0);
103	}
104	sx_unlock(&sndstat_lock);
105	return (err);
106}
107SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
108    0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
109#endif
110
111static int sndstat_prepare(struct sbuf *s);
112
113static int
114sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
115{
116	int error, verbose;
117
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)
122			error = EINVAL;
123		else
124			snd_verbose = verbose;
125	}
126	return error;
127}
128SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
129            0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
130
131static int
132sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
133{
134	if (sndstat_dev == NULL || i_dev != sndstat_dev)
135		return EBADF;
136
137	sx_xlock(&sndstat_lock);
138	if (SNDSTAT_PID(i_dev) != 0) {
139		sx_unlock(&sndstat_lock);
140		return EBUSY;
141	}
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);
146		return ENXIO;
147	}
148	sndstat_bufptr = 0;
149	sx_unlock(&sndstat_lock);
150	return 0;
151}
152
153static int
154sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
155{
156	if (sndstat_dev == NULL || i_dev != sndstat_dev)
157		return EBADF;
158
159	sx_xlock(&sndstat_lock);
160	if (SNDSTAT_PID(i_dev) == 0) {
161		sx_unlock(&sndstat_lock);
162		return EBADF;
163	}
164
165	SNDSTAT_FLUSH();
166	SNDSTAT_PID_SET(i_dev, 0);
167
168	sx_unlock(&sndstat_lock);
169
170	return 0;
171}
172
173static int
174sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
175{
176	int l, err;
177
178	if (sndstat_dev == NULL || i_dev != sndstat_dev)
179		return EBADF;
180
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);
185		return EBADF;
186	}
187
188	if (sndstat_bufptr == 0) {
189		err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
190		if (err) {
191			SNDSTAT_FLUSH();
192			sx_unlock(&sndstat_lock);
193			return err;
194		}
195	}
196
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;
199	sndstat_bufptr += l;
200	sx_unlock(&sndstat_lock);
201
202	return err;
203}
204
205/************************************************************************/
206
207static struct sndstat_entry *
208sndstat_find(int type, int unit)
209{
210	struct sndstat_entry *ent;
211
212	SLIST_FOREACH(ent, &sndstat_devlist, link) {
213		if (ent->type == type && ent->unit == unit)
214			return ent;
215	}
216
217	return NULL;
218}
219
220int
221sndstat_acquire(struct thread *td)
222{
223	if (sndstat_dev == NULL)
224		return EBADF;
225
226	sx_xlock(&sndstat_lock);
227	if (SNDSTAT_PID(sndstat_dev) != 0) {
228		sx_unlock(&sndstat_lock);
229		return EBUSY;
230	}
231	SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
232	sx_unlock(&sndstat_lock);
233	return 0;
234}
235
236int
237sndstat_release(struct thread *td)
238{
239	if (sndstat_dev == NULL)
240		return EBADF;
241
242	sx_xlock(&sndstat_lock);
243	if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
244		sx_unlock(&sndstat_lock);
245		return EBADF;
246	}
247	SNDSTAT_PID_SET(sndstat_dev, 0);
248	sx_unlock(&sndstat_lock);
249	return 0;
250}
251
252int
253sndstat_register(device_t dev, char *str, sndstat_handler handler)
254{
255	struct sndstat_entry *ent;
256	const char *devtype;
257	int type, unit;
258
259	if (dev) {
260		unit = device_get_unit(dev);
261		devtype = device_get_name(dev);
262		if (!strcmp(devtype, "pcm"))
263			type = SS_TYPE_PCM;
264		else if (!strcmp(devtype, "midi"))
265			type = SS_TYPE_MIDI;
266		else if (!strcmp(devtype, "sequencer"))
267			type = SS_TYPE_SEQUENCER;
268		else
269			return EINVAL;
270	} else {
271		type = SS_TYPE_MODULE;
272		unit = -1;
273	}
274
275	ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
276	ent->dev = dev;
277	ent->str = str;
278	ent->type = type;
279	ent->unit = unit;
280	ent->handler = handler;
281
282	sx_xlock(&sndstat_lock);
283	SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
284	if (type == SS_TYPE_MODULE)
285		sndstat_files++;
286	sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
287	sx_unlock(&sndstat_lock);
288
289	return 0;
290}
291
292int
293sndstat_registerfile(char *str)
294{
295	return sndstat_register(NULL, str, NULL);
296}
297
298int
299sndstat_unregister(device_t dev)
300{
301	struct sndstat_entry *ent;
302
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);
308			free(ent, M_DEVBUF);
309
310			return 0;
311		}
312	}
313	sx_unlock(&sndstat_lock);
314
315	return ENXIO;
316}
317
318int
319sndstat_unregisterfile(char *str)
320{
321	struct sndstat_entry *ent;
322
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);
327			sndstat_files--;
328			sx_unlock(&sndstat_lock);
329			free(ent, M_DEVBUF);
330
331			return 0;
332		}
333	}
334	sx_unlock(&sndstat_lock);
335
336	return ENXIO;
337}
338
339/************************************************************************/
340
341static int
342sndstat_prepare(struct sbuf *s)
343{
344	struct sndstat_entry *ent;
345	struct snddev_info *d;
346    	int i, j;
347
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");
352		sbuf_finish(s);
353    		return sbuf_len(s);
354	}
355
356	sbuf_printf(s, "Installed devices:\n");
357
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);
361			if (!ent)
362				continue;
363			d = device_get_softc(ent->dev);
364			if (!PCM_REGISTERED(d))
365				continue;
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));
370			if (snd_verbose > 0)
371				sbuf_printf(s, " %s", ent->str);
372			if (ent->handler)
373				ent->handler(s, ent->dev, snd_verbose);
374			sbuf_printf(s, "\n");
375			PCM_RELEASE_QUICK(d);
376		}
377    	}
378
379	if (snd_verbose >= 3 && sndstat_files > 0) {
380		sbuf_printf(s, "\nFile Versions:\n");
381
382		SLIST_FOREACH(ent, &sndstat_devlist, link) {
383			if (ent->dev == NULL && ent->str != NULL)
384				sbuf_printf(s, "%s\n", ent->str);
385		}
386	}
387
388	sbuf_finish(s);
389    	return sbuf_len(s);
390}
391
392static int
393sndstat_init(void)
394{
395	if (sndstat_dev != NULL)
396		return EINVAL;
397	sx_init(&sndstat_lock, "sndstat lock");
398	sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
399	    UID_ROOT, GID_WHEEL, 0444, "sndstat");
400	return 0;
401}
402
403static int
404sndstat_uninit(void)
405{
406	if (sndstat_dev == NULL)
407		return EINVAL;
408
409	sx_xlock(&sndstat_lock);
410	if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
411		sx_unlock(&sndstat_lock);
412		return EBUSY;
413	}
414
415	/* XXXPHO: use destroy_dev_sched() */
416	destroy_dev(sndstat_dev);
417	sndstat_dev = NULL;
418
419	SNDSTAT_FLUSH();
420
421	sx_unlock(&sndstat_lock);
422	sx_destroy(&sndstat_lock);
423	return 0;
424}
425
426static void
427sndstat_sysinit(void *p)
428{
429	sndstat_init();
430}
431
432static void
433sndstat_sysuninit(void *p)
434{
435	int error;
436
437	error = sndstat_uninit();
438	KASSERT(error == 0, ("%s: error = %d", __func__, error));
439}
440
441SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
442SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
443