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