sndstat.c revision 111815
10SN/A/*
22362SN/A * Copyright (c) 2001 Cameron Grant <gandalf@vilnya.demon.co.uk>
30SN/A * All rights reserved.
40SN/A *
50SN/A * Redistribution and use in source and binary forms, with or without
60SN/A * modification, are permitted provided that the following conditions
72362SN/A * are met:
80SN/A * 1. Redistributions of source code must retain the above copyright
92362SN/A *    notice, this list of conditions and the following disclaimer.
100SN/A * 2. Redistributions in binary form must reproduce the above copyright
110SN/A *    notice, this list of conditions and the following disclaimer in the
120SN/A *    documentation and/or other materials provided with the distribution.
130SN/A *
140SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212362SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222362SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232362SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240SN/A * SUCH DAMAGE.
250SN/A */
260SN/A
270SN/A#include <dev/sound/pcm/sound.h>
280SN/A#include <dev/sound/pcm/vchan.h>
290SN/A
300SN/ASND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/sndstat.c 111815 2003-03-03 12:15:54Z phk $");
310SN/A
320SN/A#define	SS_TYPE_MODULE		0
330SN/A#define	SS_TYPE_FIRST		1
340SN/A#define	SS_TYPE_PCM		1
350SN/A#define	SS_TYPE_MIDI		2
360SN/A#define	SS_TYPE_SEQUENCER	3
370SN/A#define	SS_TYPE_LAST		3
380SN/A
390SN/Astatic d_open_t sndstat_open;
400SN/Astatic d_close_t sndstat_close;
410SN/Astatic d_read_t sndstat_read;
420SN/A
430SN/Astatic struct cdevsw sndstat_cdevsw = {
440SN/A	.d_open =	sndstat_open,
450SN/A	.d_close =	sndstat_close,
460SN/A	.d_read =	sndstat_read,
470SN/A	.d_name =	"sndstat",
480SN/A	.d_maj =	SND_CDEV_MAJOR,
490SN/A};
500SN/A
510SN/Astruct sndstat_entry {
520SN/A	SLIST_ENTRY(sndstat_entry) link;
530SN/A	device_t dev;
540SN/A	char *str;
550SN/A	sndstat_handler handler;
560SN/A	int type, unit;
570SN/A};
580SN/A
590SN/A#ifdef	USING_MUTEX
600SN/Astatic struct mtx sndstat_lock;
610SN/A#endif
620SN/Astatic struct sbuf sndstat_sbuf;
630SN/Astatic dev_t sndstat_dev = 0;
640SN/Astatic int sndstat_isopen = 0;
650SN/Astatic int sndstat_bufptr;
660SN/Astatic int sndstat_maxunit = -1;
670SN/Astatic int sndstat_files = 0;
680SN/A
690SN/Astatic SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
700SN/A
710SN/Astatic int sndstat_verbose = 1;
720SN/A#ifdef	USING_MUTEX
730SN/ATUNABLE_INT("hw.snd.verbose", &sndstat_verbose);
740SN/A#else
750SN/ATUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose);
760SN/A#endif
770SN/A
780SN/Astatic int sndstat_prepare(struct sbuf *s);
790SN/A
800SN/Astatic int
810SN/Asysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
820SN/A{
830SN/A	intrmask_t s;
840SN/A	int error, verbose;
850SN/A
860SN/A	verbose = sndstat_verbose;
870SN/A	error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req);
880SN/A	if (error == 0 && req->newptr != NULL) {
890SN/A		s = spltty();
900SN/A		mtx_lock(&sndstat_lock);
910SN/A		if (verbose < 0 || verbose > 3)
920SN/A			error = EINVAL;
930SN/A		else
940SN/A			sndstat_verbose = verbose;
950SN/A		mtx_unlock(&sndstat_lock);
960SN/A		splx(s);
970SN/A	}
980SN/A	return error;
990SN/A}
1000SN/ASYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
1010SN/A            0, sizeof(int), sysctl_hw_sndverbose, "I", "");
1020SN/A
1030SN/Astatic int
1040SN/Asndstat_open(dev_t i_dev, int flags, int mode, struct thread *td)
1050SN/A{
1060SN/A	intrmask_t s;
1070SN/A	int error;
1080SN/A
1090SN/A	s = spltty();
1100SN/A	mtx_lock(&sndstat_lock);
1110SN/A	if (sndstat_isopen) {
1120SN/A		mtx_unlock(&sndstat_lock);
1130SN/A		splx(s);
1140SN/A		return EBUSY;
1150SN/A	}
1160SN/A	sndstat_isopen = 1;
1170SN/A	mtx_unlock(&sndstat_lock);
1180SN/A	splx(s);
1190SN/A	if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) {
1200SN/A		error = ENXIO;
1210SN/A		goto out;
1220SN/A	}
1230SN/A	sndstat_bufptr = 0;
1240SN/A	error = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
1250SN/Aout:
1260SN/A	if (error) {
1270SN/A		s = spltty();
1280SN/A		mtx_lock(&sndstat_lock);
1290SN/A		sndstat_isopen = 0;
1300SN/A		mtx_unlock(&sndstat_lock);
1310SN/A		splx(s);
1320SN/A	}
1330SN/A	return (error);
1340SN/A}
1350SN/A
1360SN/Astatic int
1370SN/Asndstat_close(dev_t i_dev, int flags, int mode, struct thread *td)
1380SN/A{
1390SN/A	intrmask_t s;
1400SN/A
1410SN/A	s = spltty();
1420SN/A	mtx_lock(&sndstat_lock);
1430SN/A	if (!sndstat_isopen) {
1440SN/A		mtx_unlock(&sndstat_lock);
1450SN/A		splx(s);
1460SN/A		return EBADF;
1470SN/A	}
1480SN/A	sbuf_delete(&sndstat_sbuf);
1490SN/A	sndstat_isopen = 0;
1500SN/A
1510SN/A	mtx_unlock(&sndstat_lock);
1520SN/A	splx(s);
1530SN/A	return 0;
1540SN/A}
1550SN/A
1560SN/Astatic int
1570SN/Asndstat_read(dev_t i_dev, struct uio *buf, int flag)
1580SN/A{
1590SN/A	intrmask_t s;
1600SN/A	int l, err;
1610SN/A
1620SN/A	s = spltty();
1630SN/A	mtx_lock(&sndstat_lock);
1640SN/A	if (!sndstat_isopen) {
1650SN/A		mtx_unlock(&sndstat_lock);
1660SN/A		splx(s);
1670SN/A		return EBADF;
1680SN/A	}
169    	l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
170	err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
171	sndstat_bufptr += l;
172
173	mtx_unlock(&sndstat_lock);
174	splx(s);
175	return err;
176}
177
178/************************************************************************/
179
180static struct sndstat_entry *
181sndstat_find(int type, int unit)
182{
183	struct sndstat_entry *ent;
184
185	SLIST_FOREACH(ent, &sndstat_devlist, link) {
186		if (ent->type == type && ent->unit == unit)
187			return ent;
188	}
189
190	return NULL;
191}
192
193int
194sndstat_register(device_t dev, char *str, sndstat_handler handler)
195{
196	intrmask_t s;
197	struct sndstat_entry *ent;
198	const char *devtype;
199	int type, unit;
200
201	if (dev) {
202		unit = device_get_unit(dev);
203		devtype = device_get_name(dev);
204		if (!strcmp(devtype, "pcm"))
205			type = SS_TYPE_PCM;
206		else if (!strcmp(devtype, "midi"))
207			type = SS_TYPE_MIDI;
208		else if (!strcmp(devtype, "sequencer"))
209			type = SS_TYPE_SEQUENCER;
210		else
211			return EINVAL;
212	} else {
213		type = SS_TYPE_MODULE;
214		unit = -1;
215	}
216
217	ent = malloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK);
218	if (!ent)
219		return ENOSPC;
220
221	ent->dev = dev;
222	ent->str = str;
223	ent->type = type;
224	ent->unit = unit;
225	ent->handler = handler;
226
227	s = spltty();
228	mtx_lock(&sndstat_lock);
229	SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
230	if (type == SS_TYPE_MODULE)
231		sndstat_files++;
232	sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
233	mtx_unlock(&sndstat_lock);
234	splx(s);
235
236	return 0;
237}
238
239int
240sndstat_registerfile(char *str)
241{
242	return sndstat_register(NULL, str, NULL);
243}
244
245int
246sndstat_unregister(device_t dev)
247{
248	intrmask_t s;
249	struct sndstat_entry *ent;
250
251	s = spltty();
252	mtx_lock(&sndstat_lock);
253	SLIST_FOREACH(ent, &sndstat_devlist, link) {
254		if (ent->dev == dev) {
255			SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
256			mtx_unlock(&sndstat_lock);
257			splx(s);
258			free(ent, M_DEVBUF);
259
260			return 0;
261		}
262	}
263	mtx_unlock(&sndstat_lock);
264	splx(s);
265
266	return ENXIO;
267}
268
269int
270sndstat_unregisterfile(char *str)
271{
272	intrmask_t s;
273	struct sndstat_entry *ent;
274
275	s = spltty();
276	mtx_lock(&sndstat_lock);
277	SLIST_FOREACH(ent, &sndstat_devlist, link) {
278		if (ent->dev == NULL && ent->str == str) {
279			SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
280			sndstat_files--;
281			mtx_unlock(&sndstat_lock);
282			splx(s);
283			free(ent, M_DEVBUF);
284
285			return 0;
286		}
287	}
288	mtx_unlock(&sndstat_lock);
289	splx(s);
290
291	return ENXIO;
292}
293
294/************************************************************************/
295
296static int
297sndstat_prepare(struct sbuf *s)
298{
299	struct sndstat_entry *ent;
300    	int i, j;
301
302	sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n");
303	if (SLIST_EMPTY(&sndstat_devlist)) {
304		sbuf_printf(s, "No devices installed.\n");
305		sbuf_finish(s);
306    		return sbuf_len(s);
307	}
308
309	sbuf_printf(s, "Installed devices:\n");
310
311    	for (i = 0; i <= sndstat_maxunit; i++) {
312		for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
313			ent = sndstat_find(j, i);
314			if (!ent)
315				continue;
316			sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
317			sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
318			sbuf_printf(s, " %s", ent->str);
319			if (ent->handler)
320				ent->handler(s, ent->dev, sndstat_verbose);
321			else
322				sbuf_printf(s, " [no handler]");
323			sbuf_printf(s, "\n");
324		}
325    	}
326
327	if (sndstat_verbose >= 3 && sndstat_files > 0) {
328		sbuf_printf(s, "\nFile Versions:\n");
329
330		SLIST_FOREACH(ent, &sndstat_devlist, link) {
331			if (ent->dev == NULL && ent->str != NULL)
332				sbuf_printf(s, "%s\n", ent->str);
333		}
334	}
335
336	sbuf_finish(s);
337    	return sbuf_len(s);
338}
339
340static int
341sndstat_init(void)
342{
343	mtx_init(&sndstat_lock, "sndstat", NULL, 0);
344	sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS, UID_ROOT, GID_WHEEL, 0444, "sndstat");
345
346	return (sndstat_dev != 0)? 0 : ENXIO;
347}
348
349static int
350sndstat_uninit(void)
351{
352	intrmask_t s;
353
354	s = spltty();
355	mtx_lock(&sndstat_lock);
356	if (sndstat_isopen) {
357		mtx_unlock(&sndstat_lock);
358		splx(s);
359		return EBUSY;
360	}
361
362	if (sndstat_dev)
363		destroy_dev(sndstat_dev);
364	sndstat_dev = 0;
365
366	splx(s);
367	mtx_destroy(&sndstat_lock);
368	return 0;
369}
370
371int
372sndstat_busy(void)
373{
374	return (sndstat_isopen);
375}
376
377static void
378sndstat_sysinit(void *p)
379{
380	sndstat_init();
381}
382
383static void
384sndstat_sysuninit(void *p)
385{
386	sndstat_uninit();
387}
388
389SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
390SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
391
392
393