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, 0 }, 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_bufptr = -1; 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_bufptr != -1) { \ 76 sbuf_delete(&sndstat_sbuf); \ 77 sndstat_bufptr = -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 if (SNDSTAT_PID(i_dev) != 0) { 139 lockmgr(&sndstat_lock, LK_RELEASE); 140 return EBUSY; 141 } 142 SNDSTAT_PID_SET(i_dev, curproc->p_pid); 143 if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) { 144 SNDSTAT_PID_SET(i_dev, 0); 145 lockmgr(&sndstat_lock, LK_RELEASE); 146 return ENXIO; 147 } 148 sndstat_bufptr = 0; 149 lockmgr(&sndstat_lock, LK_RELEASE); 150 return 0; 151 } 152 153 static int 154 sndstat_close(struct dev_close_args *ap) 155 { 156 struct cdev *i_dev = ap->a_head.a_dev; 157 158 if (sndstat_dev == NULL || i_dev != sndstat_dev) 159 return EBADF; 160 161 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 162 if (SNDSTAT_PID(i_dev) == 0) { 163 lockmgr(&sndstat_lock, LK_RELEASE); 164 return EBADF; 165 } 166 167 SNDSTAT_FLUSH(); 168 SNDSTAT_PID_SET(i_dev, 0); 169 170 lockmgr(&sndstat_lock, LK_RELEASE); 171 172 return 0; 173 } 174 175 static int 176 sndstat_read(struct dev_read_args *ap) 177 { 178 struct cdev *i_dev = ap->a_head.a_dev; 179 struct uio *buf = ap->a_uio; 180 int l, err; 181 182 if (sndstat_dev == NULL || i_dev != sndstat_dev) 183 return EBADF; 184 185 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 186 if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid || 187 sndstat_bufptr == -1) { 188 lockmgr(&sndstat_lock, LK_RELEASE); 189 return EBADF; 190 } 191 192 if (sndstat_bufptr == 0) { 193 err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM; 194 if (err) { 195 SNDSTAT_FLUSH(); 196 lockmgr(&sndstat_lock, LK_RELEASE); 197 return err; 198 } 199 } 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; 203 sndstat_bufptr += l; 204 lockmgr(&sndstat_lock, LK_RELEASE); 205 206 return err; 207 } 208 209 /************************************************************************/ 210 211 static struct sndstat_entry * 212 sndstat_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 224 int 225 sndstat_acquire(struct thread *td) 226 { 227 if (sndstat_dev == NULL) 228 return EBADF; 229 230 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 231 if (SNDSTAT_PID(sndstat_dev) != 0) { 232 lockmgr(&sndstat_lock, LK_RELEASE); 233 return EBUSY; 234 } 235 SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid); 236 lockmgr(&sndstat_lock, LK_RELEASE); 237 return 0; 238 } 239 240 int 241 sndstat_release(struct thread *td) 242 { 243 if (sndstat_dev == NULL) 244 return EBADF; 245 246 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 247 if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) { 248 lockmgr(&sndstat_lock, LK_RELEASE); 249 return EBADF; 250 } 251 SNDSTAT_PID_SET(sndstat_dev, 0); 252 lockmgr(&sndstat_lock, LK_RELEASE); 253 return 0; 254 } 255 256 int 257 sndstat_register(device_t dev, char *str, sndstat_handler handler) 258 { 259 struct sndstat_entry *ent; 260 const char *devtype; 261 int type, unit; 262 263 if (dev) { 264 unit = device_get_unit(dev); 265 devtype = device_get_name(dev); 266 if (!strcmp(devtype, "pcm")) 267 type = SS_TYPE_PCM; 268 else if (!strcmp(devtype, "midi")) 269 type = SS_TYPE_MIDI; 270 else if (!strcmp(devtype, "sequencer")) 271 type = SS_TYPE_SEQUENCER; 272 else 273 return EINVAL; 274 } else { 275 type = SS_TYPE_MODULE; 276 unit = -1; 277 } 278 279 ent = kmalloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO); 280 ent->dev = dev; 281 ent->str = str; 282 ent->type = type; 283 ent->unit = unit; 284 ent->handler = handler; 285 286 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 287 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link); 288 if (type == SS_TYPE_MODULE) 289 sndstat_files++; 290 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit; 291 lockmgr(&sndstat_lock, LK_RELEASE); 292 293 return 0; 294 } 295 296 int 297 sndstat_registerfile(char *str) 298 { 299 return sndstat_register(NULL, str, NULL); 300 } 301 302 int 303 sndstat_unregister(device_t dev) 304 { 305 struct sndstat_entry *ent; 306 307 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 308 SLIST_FOREACH(ent, &sndstat_devlist, link) { 309 if (ent->dev == dev) { 310 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link); 311 lockmgr(&sndstat_lock, LK_RELEASE); 312 kfree(ent, M_DEVBUF); 313 314 return 0; 315 } 316 } 317 lockmgr(&sndstat_lock, LK_RELEASE); 318 319 return ENXIO; 320 } 321 322 int 323 sndstat_unregisterfile(char *str) 324 { 325 struct sndstat_entry *ent; 326 327 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 328 SLIST_FOREACH(ent, &sndstat_devlist, link) { 329 if (ent->dev == NULL && ent->str == str) { 330 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link); 331 sndstat_files--; 332 lockmgr(&sndstat_lock, LK_RELEASE); 333 kfree(ent, M_DEVBUF); 334 335 return 0; 336 } 337 } 338 lockmgr(&sndstat_lock, LK_RELEASE); 339 340 return ENXIO; 341 } 342 343 /************************************************************************/ 344 345 static int 346 sndstat_prepare(struct sbuf *s) 347 { 348 struct sndstat_entry *ent; 349 struct snddev_info *d; 350 int i, j; 351 352 if (snd_verbose > 0) { 353 sbuf_printf(s, "FreeBSD Audio Driver (%ubit %d/%s)\n", 354 (u_int)sizeof(intpcm32_t) << 3, SND_DRV_VERSION, 355 MACHINE_ARCH); 356 } 357 358 if (SLIST_EMPTY(&sndstat_devlist)) { 359 sbuf_printf(s, "No devices installed.\n"); 360 sbuf_finish(s); 361 return sbuf_len(s); 362 } 363 364 sbuf_printf(s, "Installed devices:\n"); 365 366 for (i = 0; i <= sndstat_maxunit; i++) { 367 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) { 368 ent = sndstat_find(j, i); 369 if (!ent) 370 continue; 371 d = device_get_softc(ent->dev); 372 if (!PCM_REGISTERED(d)) 373 continue; 374 /* XXX Need Giant magic entry ??? */ 375 PCM_ACQUIRE_QUICK(d); 376 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev)); 377 sbuf_printf(s, " <%s>", device_get_desc(ent->dev)); 378 if (snd_verbose > 0) 379 sbuf_printf(s, " %s", ent->str); 380 if (ent->handler) 381 ent->handler(s, ent->dev, snd_verbose); 382 sbuf_printf(s, "\n"); 383 PCM_RELEASE_QUICK(d); 384 } 385 } 386 387 if (snd_verbose >= 3 && sndstat_files > 0) { 388 sbuf_printf(s, "\nFile Versions:\n"); 389 390 SLIST_FOREACH(ent, &sndstat_devlist, link) { 391 if (ent->dev == NULL && ent->str != NULL) 392 sbuf_printf(s, "%s\n", ent->str); 393 } 394 } 395 396 sbuf_finish(s); 397 return sbuf_len(s); 398 } 399 400 static int 401 sndstat_init(void) 402 { 403 if (sndstat_dev != NULL) 404 return EINVAL; 405 lockinit(&sndstat_lock, "sndstat lock", 0, LK_CANRECURSE); 406 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS, 407 UID_ROOT, GID_WHEEL, 0444, "sndstat"); 408 return 0; 409 } 410 411 static int 412 sndstat_uninit(void) 413 { 414 if (sndstat_dev == NULL) 415 return EINVAL; 416 417 lockmgr(&sndstat_lock, LK_EXCLUSIVE); 418 if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) { 419 lockmgr(&sndstat_lock, LK_RELEASE); 420 return EBUSY; 421 } 422 423 /* XXXPHO: use destroy_dev_sched() */ 424 destroy_dev(sndstat_dev); 425 sndstat_dev = NULL; 426 427 SNDSTAT_FLUSH(); 428 429 lockmgr(&sndstat_lock, LK_RELEASE); 430 lockuninit(&sndstat_lock); 431 return 0; 432 } 433 434 static void 435 sndstat_sysinit(void *p) 436 { 437 sndstat_init(); 438 } 439 440 static void 441 sndstat_sysuninit(void *p) 442 { 443 int error; 444 445 error = sndstat_uninit(); 446 KASSERT(error == 0, ("%s: error = %d", __func__, error)); 447 } 448 449 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL); 450 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL); 451