xref: /dragonfly/sys/dev/sound/pcm/sndstat.c (revision 8d6aeca2)
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/device.h>
36 
37 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/sndstat.c 248381 2013-03-16 17:57:00Z joel $");
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 
46 static d_open_t sndstat_open;
47 static d_close_t sndstat_close;
48 static d_read_t sndstat_read;
49 
50 static struct dev_ops sndstat_cdevsw = {
51 	{ "sndstat", 0, D_MPSAFE },
52 	.d_open =	sndstat_open,
53 	.d_close =	sndstat_close,
54 	.d_read =	sndstat_read,
55 };
56 
57 struct sndstat_entry {
58 	SLIST_ENTRY(sndstat_entry) link;
59 	device_t dev;
60 	char *str;
61 	sndstat_handler handler;
62 	int type, unit;
63 };
64 
65 static struct lock sndstat_lock;
66 static struct sbuf sndstat_sbuf;
67 static struct cdev *sndstat_dev = NULL;
68 static int sndstat_buflen = 0;
69 static int sndstat_maxunit = -1;
70 static int sndstat_files = 0;
71 
72 #define SNDSTAT_PID(x)		((pid_t)((intptr_t)((x)->si_drv1)))
73 #define SNDSTAT_PID_SET(x, y)	(x)->si_drv1 = (void *)((intptr_t)(y))
74 #define SNDSTAT_FLUSH()		do {					\
75 	if (sndstat_buflen != -1) {					\
76 		sbuf_delete(&sndstat_sbuf);				\
77 		sndstat_buflen = -1;					\
78 	}								\
79 } while (0)
80 
81 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(sndstat_devlist);
82 
83 int snd_verbose = 0;
84 TUNABLE_INT("hw.snd.verbose", &snd_verbose);
85 
86 #ifdef SND_DEBUG
87 static int
88 sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
89 {
90 	int err, val;
91 
92 	if (sndstat_dev == NULL)
93 		return (EINVAL);
94 
95 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
96 	val = (int)SNDSTAT_PID(sndstat_dev);
97 	err = sysctl_handle_int(oidp, &val, 0, req);
98 	if (err == 0 && req->newptr != NULL && val == 0) {
99 		SNDSTAT_FLUSH();
100 		SNDSTAT_PID_SET(sndstat_dev, 0);
101 	}
102 	lockmgr(&sndstat_lock, LK_RELEASE);
103 	return (err);
104 }
105 SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
106     0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
107 #endif
108 
109 static int sndstat_prepare(struct sbuf *s);
110 
111 static int
112 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
113 {
114 	int error, verbose;
115 
116 	verbose = snd_verbose;
117 	error = sysctl_handle_int(oidp, &verbose, 0, req);
118 	if (error == 0 && req->newptr != NULL) {
119 		if (verbose < 0 || verbose > 4)
120 			error = EINVAL;
121 		else
122 			snd_verbose = verbose;
123 	}
124 	return error;
125 }
126 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
127             0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
128 
129 static int
130 sndstat_open(struct dev_open_args *ap)
131 {
132 	struct cdev *i_dev = ap->a_head.a_dev;
133 
134 	if (sndstat_dev == NULL || i_dev != sndstat_dev)
135 		return EBADF;
136 
137 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
138 	/* Deal with pulseaudio opening /dev/sndstat twice. */
139 	if (SNDSTAT_PID(i_dev) == curproc->p_pid) {
140 		/* Nothing */
141 	} else if (SNDSTAT_PID(i_dev) != 0) {
142 		lockmgr(&sndstat_lock, LK_RELEASE);
143 		return EBUSY;
144 	} else {
145 		SNDSTAT_PID_SET(i_dev, curproc->p_pid);
146 		if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
147 			SNDSTAT_PID_SET(i_dev, 0);
148 			lockmgr(&sndstat_lock, LK_RELEASE);
149 			return ENXIO;
150 		}
151 		sndstat_buflen = 0;
152 	}
153 	lockmgr(&sndstat_lock, LK_RELEASE);
154 	return 0;
155 }
156 
157 static int
158 sndstat_close(struct dev_close_args *ap)
159 {
160 	struct cdev *i_dev = ap->a_head.a_dev;
161 
162 	if (sndstat_dev == NULL || i_dev != sndstat_dev)
163 		return EBADF;
164 
165 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
166 	if (SNDSTAT_PID(i_dev) == 0) {
167 		lockmgr(&sndstat_lock, LK_RELEASE);
168 		return EBADF;
169 	}
170 
171 	SNDSTAT_FLUSH();
172 	SNDSTAT_PID_SET(i_dev, 0);
173 
174 	lockmgr(&sndstat_lock, LK_RELEASE);
175 
176 	return 0;
177 }
178 
179 static int
180 sndstat_read(struct dev_read_args *ap)
181 {
182 	struct cdev *i_dev = ap->a_head.a_dev;
183 	struct uio *buf = ap->a_uio;
184 	int l, err;
185 
186 	if (sndstat_dev == NULL || i_dev != sndstat_dev)
187 		return EBADF;
188 
189 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
190 	if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid ||
191 	    sndstat_buflen == -1) {
192 		lockmgr(&sndstat_lock, LK_RELEASE);
193 		return EBADF;
194 	}
195 
196 	if (sndstat_buflen == 0) {
197 		err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
198 		if (err) {
199 			SNDSTAT_FLUSH();
200 			lockmgr(&sndstat_lock, LK_RELEASE);
201 			return err;
202 		}
203 		sndstat_buflen = sbuf_len(&sndstat_sbuf);
204 	}
205 
206 	l = min(buf->uio_resid, sndstat_buflen - buf->uio_offset);
207 	err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + buf->uio_offset, l, buf) : 0;
208 	lockmgr(&sndstat_lock, LK_RELEASE);
209 
210 	return err;
211 }
212 
213 /************************************************************************/
214 
215 static struct sndstat_entry *
216 sndstat_find(int type, int unit)
217 {
218 	struct sndstat_entry *ent;
219 
220 	SLIST_FOREACH(ent, &sndstat_devlist, link) {
221 		if (ent->type == type && ent->unit == unit)
222 			return ent;
223 	}
224 
225 	return NULL;
226 }
227 
228 /* XXX profmakx
229  * The following two functions are called from
230  * pcm_register and pcm_unregister in kernel
231  * which means that td_proc of curthread can be NULL
232  * because this is called from a kernel thread.
233  * We handle this as a separate case for the time
234  * being.
235  *
236  * Should the kernel be able to override ownership of sndstat?
237  */
238 
239 int
240 sndstat_acquire(struct thread *td)
241 {
242 	if (sndstat_dev == NULL)
243 		return EBADF;
244 
245 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
246 	if (SNDSTAT_PID(sndstat_dev) != 0) {
247 		lockmgr(&sndstat_lock, LK_RELEASE);
248 		return EBUSY;
249 	}
250 	if (td->td_proc == NULL) {
251 		SNDSTAT_PID_SET(sndstat_dev, -1);
252 	} else {
253 		SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
254 	}
255 	lockmgr(&sndstat_lock, LK_RELEASE);
256 	return 0;
257 }
258 
259 int
260 sndstat_release(struct thread *td)
261 {
262 	if (sndstat_dev == NULL)
263 		return EBADF;
264 
265 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
266 	if (td->td_proc == NULL) {
267 		if (SNDSTAT_PID(sndstat_dev) != -1) {
268 			lockmgr(&sndstat_lock, LK_RELEASE);
269 			return EBADF;
270 		}
271 	} else {
272 		if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
273 			lockmgr(&sndstat_lock, LK_RELEASE);
274 			return EBADF;
275 		}
276 	}
277 	SNDSTAT_PID_SET(sndstat_dev, 0);
278 	lockmgr(&sndstat_lock, LK_RELEASE);
279 	return 0;
280 }
281 
282 int
283 sndstat_register(device_t dev, char *str, sndstat_handler handler)
284 {
285 	struct sndstat_entry *ent;
286 	const char *devtype;
287 	int type, unit;
288 
289 	if (dev) {
290 		unit = device_get_unit(dev);
291 		devtype = device_get_name(dev);
292 		if (!strcmp(devtype, "pcm"))
293 			type = SS_TYPE_PCM;
294 		else if (!strcmp(devtype, "midi"))
295 			type = SS_TYPE_MIDI;
296 		else if (!strcmp(devtype, "sequencer"))
297 			type = SS_TYPE_SEQUENCER;
298 		else
299 			return EINVAL;
300 	} else {
301 		type = SS_TYPE_MODULE;
302 		unit = -1;
303 	}
304 
305 	ent = kmalloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
306 	ent->dev = dev;
307 	ent->str = str;
308 	ent->type = type;
309 	ent->unit = unit;
310 	ent->handler = handler;
311 
312 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
313 	SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
314 	if (type == SS_TYPE_MODULE)
315 		sndstat_files++;
316 	sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
317 	lockmgr(&sndstat_lock, LK_RELEASE);
318 
319 	return 0;
320 }
321 
322 int
323 sndstat_registerfile(char *str)
324 {
325 	return sndstat_register(NULL, str, NULL);
326 }
327 
328 int
329 sndstat_unregister(device_t dev)
330 {
331 	struct sndstat_entry *ent;
332 
333 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
334 	SLIST_FOREACH(ent, &sndstat_devlist, link) {
335 		if (ent->dev == dev) {
336 			SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
337 			lockmgr(&sndstat_lock, LK_RELEASE);
338 			kfree(ent, M_DEVBUF);
339 
340 			return 0;
341 		}
342 	}
343 	lockmgr(&sndstat_lock, LK_RELEASE);
344 
345 	return ENXIO;
346 }
347 
348 int
349 sndstat_unregisterfile(char *str)
350 {
351 	struct sndstat_entry *ent;
352 
353 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
354 	SLIST_FOREACH(ent, &sndstat_devlist, link) {
355 		if (ent->dev == NULL && ent->str == str) {
356 			SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
357 			sndstat_files--;
358 			lockmgr(&sndstat_lock, LK_RELEASE);
359 			kfree(ent, M_DEVBUF);
360 
361 			return 0;
362 		}
363 	}
364 	lockmgr(&sndstat_lock, LK_RELEASE);
365 
366 	return ENXIO;
367 }
368 
369 /************************************************************************/
370 
371 static int
372 sndstat_prepare(struct sbuf *s)
373 {
374 	struct sndstat_entry *ent;
375 	struct snddev_info *d;
376     	int i, j;
377 
378 	if (snd_verbose > 0) {
379 		sbuf_printf(s, "FreeBSD Audio Driver (%ubit %d/%s)\n",
380 		    (u_int)sizeof(intpcm32_t) << 3, SND_DRV_VERSION,
381 		    MACHINE_ARCH);
382 	}
383 
384 	if (SLIST_EMPTY(&sndstat_devlist)) {
385 		sbuf_printf(s, "No devices installed.\n");
386 		sbuf_finish(s);
387     		return sbuf_len(s);
388 	}
389 
390 	sbuf_printf(s, "Installed devices:\n");
391 
392     	for (i = 0; i <= sndstat_maxunit; i++) {
393 		for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
394 			ent = sndstat_find(j, i);
395 			if (!ent)
396 				continue;
397 			d = device_get_softc(ent->dev);
398 			if (!PCM_REGISTERED(d))
399 				continue;
400 			/* XXX Need Giant magic entry ??? */
401 			PCM_ACQUIRE_QUICK(d);
402 			sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
403 			sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
404 			if (snd_verbose > 0)
405 				sbuf_printf(s, " %s", ent->str);
406 			if (ent->handler)
407 				ent->handler(s, ent->dev, snd_verbose);
408 			sbuf_printf(s, "\n");
409 			PCM_RELEASE_QUICK(d);
410 		}
411     	}
412 
413 	if (snd_verbose >= 3 && sndstat_files > 0) {
414 		sbuf_printf(s, "\nFile Versions:\n");
415 
416 		SLIST_FOREACH(ent, &sndstat_devlist, link) {
417 			if (ent->dev == NULL && ent->str != NULL)
418 				sbuf_printf(s, "%s\n", ent->str);
419 		}
420 	}
421 
422 	sbuf_finish(s);
423     	return sbuf_len(s);
424 }
425 
426 static int
427 sndstat_init(void)
428 {
429 	if (sndstat_dev != NULL)
430 		return EINVAL;
431 	lockinit(&sndstat_lock, "sndstat lock", 0, LK_CANRECURSE);
432 	sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
433 	    UID_ROOT, GID_WHEEL, 0444, "sndstat");
434 	return 0;
435 }
436 
437 static int
438 sndstat_uninit(void)
439 {
440 	if (sndstat_dev == NULL)
441 		return EINVAL;
442 
443 	lockmgr(&sndstat_lock, LK_EXCLUSIVE);
444 	if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
445 		lockmgr(&sndstat_lock, LK_RELEASE);
446 		return EBUSY;
447 	}
448 
449 	/* XXXPHO: use destroy_dev_sched() */
450 	destroy_dev(sndstat_dev);
451 	sndstat_dev = NULL;
452 
453 	SNDSTAT_FLUSH();
454 
455 	lockmgr(&sndstat_lock, LK_RELEASE);
456 	lockuninit(&sndstat_lock);
457 	return 0;
458 }
459 
460 static void
461 sndstat_sysinit(void *p)
462 {
463 	sndstat_init();
464 }
465 
466 static void
467 sndstat_sysuninit(void *p)
468 {
469 	int error;
470 
471 	error = sndstat_uninit();
472 	KASSERT(error == 0, ("%s: error = %d", __func__, error));
473 }
474 
475 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
476 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
477