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