1 /*- 2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <dev/sound/pcm/sound.h> 28 #include <sys/ctype.h> 29 30 SND_DECLARE_FILE("$FreeBSD$"); 31 32 static int dsp_mmap_allow_prot_exec = 0; 33 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW, 34 &dsp_mmap_allow_prot_exec, 0, "linux mmap compatibility"); 35 36 struct dsp_cdevinfo { 37 struct pcm_channel *rdch, *wrch; 38 int busy, simplex; 39 TAILQ_ENTRY(dsp_cdevinfo) link; 40 }; 41 42 #define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch) 43 #define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch) 44 #define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex) 45 46 #define DSP_CDEVINFO_CACHESIZE 8 47 48 #define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \ 49 (y) != NULL && (y)->si_drv1 != NULL) 50 51 #define OLDPCM_IOCTL 52 53 static d_open_t dsp_open; 54 static d_close_t dsp_close; 55 static d_read_t dsp_read; 56 static d_write_t dsp_write; 57 static d_ioctl_t dsp_ioctl; 58 static d_poll_t dsp_poll; 59 static d_mmap_t dsp_mmap; 60 61 struct cdevsw dsp_cdevsw = { 62 .d_version = D_VERSION, 63 .d_open = dsp_open, 64 .d_close = dsp_close, 65 .d_read = dsp_read, 66 .d_write = dsp_write, 67 .d_ioctl = dsp_ioctl, 68 .d_poll = dsp_poll, 69 .d_mmap = dsp_mmap, 70 .d_name = "dsp", 71 }; 72 73 #ifdef USING_DEVFS 74 static eventhandler_tag dsp_ehtag = NULL; 75 static int dsp_umax = -1; 76 static int dsp_cmax = -1; 77 #endif 78 79 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group); 80 static int dsp_oss_syncstart(int sg_id); 81 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy); 82 #ifdef OSSV4_EXPERIMENT 83 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled); 84 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map); 85 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map); 86 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label); 87 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label); 88 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song); 89 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song); 90 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name); 91 #endif 92 93 static struct snddev_info * 94 dsp_get_info(struct cdev *dev) 95 { 96 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev))); 97 } 98 99 static uint32_t 100 dsp_get_flags(struct cdev *dev) 101 { 102 device_t bdev; 103 104 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev)); 105 106 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff); 107 } 108 109 static void 110 dsp_set_flags(struct cdev *dev, uint32_t flags) 111 { 112 device_t bdev; 113 114 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev)); 115 116 if (bdev != NULL) 117 pcm_setflags(bdev, flags); 118 } 119 120 /* 121 * return the channels associated with an open device instance. 122 * lock channels specified. 123 */ 124 static int 125 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, 126 uint32_t prio) 127 { 128 struct snddev_info *d; 129 struct pcm_channel *ch; 130 uint32_t flags; 131 132 if (PCM_SIMPLEX(dev) != 0) { 133 d = dsp_get_info(dev); 134 if (!PCM_REGISTERED(d)) 135 return (ENXIO); 136 pcm_lock(d); 137 PCM_WAIT(d); 138 PCM_ACQUIRE(d); 139 /* 140 * Note: order is important - 141 * pcm flags -> prio query flags -> wild guess 142 */ 143 ch = NULL; 144 flags = dsp_get_flags(dev); 145 if (flags & SD_F_PRIO_WR) { 146 ch = PCM_RDCH(dev); 147 PCM_RDCH(dev) = NULL; 148 } else if (flags & SD_F_PRIO_RD) { 149 ch = PCM_WRCH(dev); 150 PCM_WRCH(dev) = NULL; 151 } else if (prio & SD_F_PRIO_WR) { 152 ch = PCM_RDCH(dev); 153 PCM_RDCH(dev) = NULL; 154 flags |= SD_F_PRIO_WR; 155 } else if (prio & SD_F_PRIO_RD) { 156 ch = PCM_WRCH(dev); 157 PCM_WRCH(dev) = NULL; 158 flags |= SD_F_PRIO_RD; 159 } else if (PCM_WRCH(dev) != NULL) { 160 ch = PCM_RDCH(dev); 161 PCM_RDCH(dev) = NULL; 162 flags |= SD_F_PRIO_WR; 163 } else if (PCM_RDCH(dev) != NULL) { 164 ch = PCM_WRCH(dev); 165 PCM_WRCH(dev) = NULL; 166 flags |= SD_F_PRIO_RD; 167 } 168 PCM_SIMPLEX(dev) = 0; 169 dsp_set_flags(dev, flags); 170 if (ch != NULL) { 171 CHN_LOCK(ch); 172 pcm_chnref(ch, -1); 173 pcm_chnrelease(ch); 174 } 175 PCM_RELEASE(d); 176 pcm_unlock(d); 177 } 178 179 *rdch = PCM_RDCH(dev); 180 *wrch = PCM_WRCH(dev); 181 182 if (*rdch != NULL && (prio & SD_F_PRIO_RD)) 183 CHN_LOCK(*rdch); 184 if (*wrch != NULL && (prio & SD_F_PRIO_WR)) 185 CHN_LOCK(*wrch); 186 187 return (0); 188 } 189 190 /* unlock specified channels */ 191 static void 192 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, 193 uint32_t prio) 194 { 195 if (wrch != NULL && (prio & SD_F_PRIO_WR)) 196 CHN_UNLOCK(wrch); 197 if (rdch != NULL && (prio & SD_F_PRIO_RD)) 198 CHN_UNLOCK(rdch); 199 } 200 201 static void 202 dsp_cdevinfo_alloc(struct cdev *dev, 203 struct pcm_channel *rdch, struct pcm_channel *wrch) 204 { 205 struct snddev_info *d; 206 struct dsp_cdevinfo *cdi; 207 int simplex; 208 209 d = dsp_get_info(dev); 210 211 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL && 212 rdch != wrch, 213 ("bogus %s(), what are you trying to accomplish here?", __func__)); 214 PCM_BUSYASSERT(d); 215 mtx_assert(d->lock, MA_OWNED); 216 217 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0; 218 219 /* 220 * Scan for free instance entry and put it into the end of list. 221 * Create new one if necessary. 222 */ 223 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) { 224 if (cdi->busy != 0) 225 break; 226 cdi->rdch = rdch; 227 cdi->wrch = wrch; 228 cdi->simplex = simplex; 229 cdi->busy = 1; 230 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link); 231 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link); 232 dev->si_drv1 = cdi; 233 return; 234 } 235 pcm_unlock(d); 236 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO); 237 pcm_lock(d); 238 cdi->rdch = rdch; 239 cdi->wrch = wrch; 240 cdi->simplex = simplex; 241 cdi->busy = 1; 242 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link); 243 dev->si_drv1 = cdi; 244 } 245 246 static void 247 dsp_cdevinfo_free(struct cdev *dev) 248 { 249 struct snddev_info *d; 250 struct dsp_cdevinfo *cdi, *tmp; 251 uint32_t flags; 252 int i; 253 254 d = dsp_get_info(dev); 255 256 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL && 257 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL, 258 ("bogus %s(), what are you trying to accomplish here?", __func__)); 259 PCM_BUSYASSERT(d); 260 mtx_assert(d->lock, MA_OWNED); 261 262 cdi = dev->si_drv1; 263 dev->si_drv1 = NULL; 264 cdi->rdch = NULL; 265 cdi->wrch = NULL; 266 cdi->simplex = 0; 267 cdi->busy = 0; 268 269 /* 270 * Once it is free, move it back to the beginning of list for 271 * faster new entry allocation. 272 */ 273 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link); 274 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link); 275 276 /* 277 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE. 278 * Reset simplex flags. 279 */ 280 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET; 281 i = DSP_CDEVINFO_CACHESIZE; 282 TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) { 283 if (cdi->busy != 0) { 284 if (cdi->simplex == 0) { 285 if (cdi->rdch != NULL) 286 flags |= SD_F_PRIO_RD; 287 if (cdi->wrch != NULL) 288 flags |= SD_F_PRIO_WR; 289 } 290 } else { 291 if (i == 0) { 292 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link); 293 free(cdi, M_DEVBUF); 294 } else 295 i--; 296 } 297 } 298 dsp_set_flags(dev, flags); 299 } 300 301 void 302 dsp_cdevinfo_init(struct snddev_info *d) 303 { 304 struct dsp_cdevinfo *cdi; 305 int i; 306 307 KASSERT(d != NULL, ("NULL snddev_info")); 308 PCM_BUSYASSERT(d); 309 mtx_assert(d->lock, MA_NOTOWNED); 310 311 TAILQ_INIT(&d->dsp_cdevinfo_pool); 312 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) { 313 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO); 314 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link); 315 } 316 } 317 318 void 319 dsp_cdevinfo_flush(struct snddev_info *d) 320 { 321 struct dsp_cdevinfo *cdi, *tmp; 322 323 KASSERT(d != NULL, ("NULL snddev_info")); 324 PCM_BUSYASSERT(d); 325 mtx_assert(d->lock, MA_NOTOWNED); 326 327 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool); 328 while (cdi != NULL) { 329 tmp = TAILQ_NEXT(cdi, link); 330 free(cdi, M_DEVBUF); 331 cdi = tmp; 332 } 333 TAILQ_INIT(&d->dsp_cdevinfo_pool); 334 } 335 336 /* duplex / simplex cdev type */ 337 enum { 338 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */ 339 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */ 340 DSP_CDEV_TYPE_RDWR, /* duplex read, write, or both */ 341 }; 342 343 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE)) 344 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE)) 345 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x)) 346 #define DSP_F_READ(x) ((x) & FREAD) 347 #define DSP_F_WRITE(x) ((x) & FWRITE) 348 349 static const struct { 350 int type; 351 char *name; 352 char *sep; 353 int use_sep; 354 int hw; 355 int max; 356 uint32_t fmt, spd; 357 int query; 358 } dsp_cdevs[] = { 359 { SND_DEV_DSP, "dsp", ".", 0, 0, 0, 360 AFMT_U8, DSP_DEFAULT_SPEED, DSP_CDEV_TYPE_RDWR }, 361 { SND_DEV_AUDIO, "audio", ".", 0, 0, 0, 362 AFMT_MU_LAW, DSP_DEFAULT_SPEED, DSP_CDEV_TYPE_RDWR }, 363 { SND_DEV_DSP16, "dspW", ".", 0, 0, 0, 364 AFMT_S16_LE, DSP_DEFAULT_SPEED, DSP_CDEV_TYPE_RDWR }, 365 { SND_DEV_DSPHW_PLAY, "dsp", ".p", 1, 1, SND_MAXHWCHAN, 366 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_WRONLY }, 367 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", 1, 1, SND_MAXVCHANS, 368 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_WRONLY }, 369 { SND_DEV_DSPHW_REC, "dsp", ".r", 1, 1, SND_MAXHWCHAN, 370 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDONLY }, 371 { SND_DEV_DSPHW_VREC, "dsp", ".vr", 1, 1, SND_MAXVCHANS, 372 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDONLY }, 373 { SND_DEV_DSPHW_CD, "dspcd", ".", 0, 0, 0, 374 AFMT_S16_LE | AFMT_STEREO, 44100, DSP_CDEV_TYPE_RDWR }, 375 { SND_DEV_DSP_MMAP, "dsp_mmap", ".", 0, 0, 0, 376 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDWR }, 377 }; 378 379 #define DSP_FIXUP_ERROR() do { \ 380 prio = dsp_get_flags(i_dev); \ 381 if (!DSP_F_VALID(flags)) \ 382 error = EINVAL; \ 383 if (!DSP_F_DUPLEX(flags) && \ 384 ((DSP_F_READ(flags) && d->reccount == 0) || \ 385 (DSP_F_WRITE(flags) && d->playcount == 0))) \ 386 error = ENOTSUP; \ 387 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \ 388 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \ 389 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \ 390 error = EBUSY; \ 391 else if (DSP_REGISTERED(d, i_dev)) \ 392 error = EBUSY; \ 393 } while(0) 394 395 static int 396 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td) 397 { 398 struct pcm_channel *rdch, *wrch; 399 struct snddev_info *d; 400 uint32_t fmt, spd, prio; 401 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit; 402 403 /* Kind of impossible.. */ 404 if (i_dev == NULL || td == NULL) 405 return (ENODEV); 406 407 d = dsp_get_info(i_dev); 408 if (!PCM_REGISTERED(d)) 409 return (EBADF); 410 411 PCM_GIANT_ENTER(d); 412 413 /* Lock snddev so nobody else can monkey with it. */ 414 pcm_lock(d); 415 PCM_WAIT(d); 416 417 /* 418 * Try to acquire cloned device before someone else pick it. 419 * ENODEV means this is not a cloned droids. 420 */ 421 error = snd_clone_acquire(i_dev); 422 if (!(error == 0 || error == ENODEV)) { 423 DSP_FIXUP_ERROR(); 424 pcm_unlock(d); 425 PCM_GIANT_EXIT(d); 426 return (error); 427 } 428 429 error = 0; 430 DSP_FIXUP_ERROR(); 431 432 if (error != 0) { 433 (void)snd_clone_release(i_dev); 434 pcm_unlock(d); 435 PCM_GIANT_EXIT(d); 436 return (error); 437 } 438 439 /* 440 * That is just enough. Acquire and unlock pcm lock so 441 * the other will just have to wait until we finish doing 442 * everything. 443 */ 444 PCM_ACQUIRE(d); 445 pcm_unlock(d); 446 447 devtype = PCMDEV(i_dev); 448 wdevunit = -1; 449 rdevunit = -1; 450 fmt = 0; 451 spd = 0; 452 453 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) { 454 if (devtype != dsp_cdevs[i].type) 455 continue; 456 if (DSP_F_SIMPLEX(flags) && 457 ((dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY && 458 DSP_F_READ(flags)) || 459 (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY && 460 DSP_F_WRITE(flags)))) { 461 /* 462 * simplex, opposite direction? Please be gone.. 463 */ 464 (void)snd_clone_release(i_dev); 465 PCM_RELEASE_QUICK(d); 466 PCM_GIANT_EXIT(d); 467 return (ENOTSUP); 468 } 469 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) 470 wdevunit = dev2unit(i_dev); 471 else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) 472 rdevunit = dev2unit(i_dev); 473 fmt = dsp_cdevs[i].fmt; 474 spd = dsp_cdevs[i].spd; 475 break; 476 } 477 478 /* No matching devtype? */ 479 if (fmt == 0 || spd == 0) 480 panic("impossible devtype %d", devtype); 481 482 rdch = NULL; 483 wrch = NULL; 484 rderror = 0; 485 wrerror = 0; 486 487 /* 488 * if we get here, the open request is valid- either: 489 * * we were previously not open 490 * * we were open for play xor record and the opener wants 491 * the non-open direction 492 */ 493 if (DSP_F_READ(flags)) { 494 /* open for read */ 495 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC, 496 td->td_proc->p_pid, rdevunit); 497 498 if (rderror == 0 && (chn_reset(rdch, fmt) != 0 || 499 (chn_setspeed(rdch, spd) != 0))) 500 rderror = ENXIO; 501 502 if (rderror != 0) { 503 if (rdch != NULL) 504 pcm_chnrelease(rdch); 505 if (!DSP_F_DUPLEX(flags)) { 506 (void)snd_clone_release(i_dev); 507 PCM_RELEASE_QUICK(d); 508 PCM_GIANT_EXIT(d); 509 return (rderror); 510 } 511 rdch = NULL; 512 } else { 513 if (flags & O_NONBLOCK) 514 rdch->flags |= CHN_F_NBIO; 515 pcm_chnref(rdch, 1); 516 CHN_UNLOCK(rdch); 517 } 518 } 519 520 if (DSP_F_WRITE(flags)) { 521 /* open for write */ 522 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, 523 td->td_proc->p_pid, wdevunit); 524 525 if (wrerror == 0 && (chn_reset(wrch, fmt) != 0 || 526 (chn_setspeed(wrch, spd) != 0))) 527 wrerror = ENXIO; 528 529 if (wrerror != 0) { 530 if (wrch != NULL) 531 pcm_chnrelease(wrch); 532 if (!DSP_F_DUPLEX(flags)) { 533 if (rdch != NULL) { 534 /* 535 * Lock, deref and release previously 536 * created record channel 537 */ 538 CHN_LOCK(rdch); 539 pcm_chnref(rdch, -1); 540 pcm_chnrelease(rdch); 541 } 542 (void)snd_clone_release(i_dev); 543 PCM_RELEASE_QUICK(d); 544 PCM_GIANT_EXIT(d); 545 return (wrerror); 546 } 547 wrch = NULL; 548 } else { 549 if (flags & O_NONBLOCK) 550 wrch->flags |= CHN_F_NBIO; 551 pcm_chnref(wrch, 1); 552 CHN_UNLOCK(wrch); 553 } 554 } 555 556 if (rdch == NULL && wrch == NULL) { 557 (void)snd_clone_release(i_dev); 558 PCM_RELEASE_QUICK(d); 559 PCM_GIANT_EXIT(d); 560 return ((wrerror != 0) ? wrerror : rderror); 561 } 562 563 pcm_lock(d); 564 565 /* 566 * We're done. Allocate channels information for this cdev. 567 */ 568 dsp_cdevinfo_alloc(i_dev, rdch, wrch); 569 570 /* 571 * Increase clone refcount for its automatic garbage collector. 572 */ 573 (void)snd_clone_ref(i_dev); 574 575 PCM_RELEASE(d); 576 pcm_unlock(d); 577 578 PCM_GIANT_LEAVE(d); 579 580 return (0); 581 } 582 583 static int 584 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td) 585 { 586 struct pcm_channel *rdch, *wrch; 587 struct snddev_info *d; 588 int sg_ids, refs; 589 590 d = dsp_get_info(i_dev); 591 if (!DSP_REGISTERED(d, i_dev)) 592 return (EBADF); 593 594 PCM_GIANT_ENTER(d); 595 596 pcm_lock(d); 597 PCM_WAIT(d); 598 599 rdch = PCM_RDCH(i_dev); 600 wrch = PCM_WRCH(i_dev); 601 602 if (rdch || wrch) { 603 PCM_ACQUIRE(d); 604 pcm_unlock(d); 605 606 refs = 0; 607 if (rdch) { 608 /* 609 * The channel itself need not be locked because: 610 * a) Adding a channel to a syncgroup happens only in dsp_ioctl(), 611 * which cannot run concurrently to dsp_close(). 612 * b) The syncmember pointer (sm) is protected by the global 613 * syncgroup list lock. 614 * c) A channel can't just disappear, invalidating pointers, 615 * unless it's closed/dereferenced first. 616 */ 617 PCM_SG_LOCK(); 618 sg_ids = chn_syncdestroy(rdch); 619 PCM_SG_UNLOCK(); 620 if (sg_ids != 0) 621 free_unr(pcmsg_unrhdr, sg_ids); 622 623 CHN_LOCK(rdch); 624 refs += pcm_chnref(rdch, -1); 625 chn_abort(rdch); /* won't sleep */ 626 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD); 627 chn_reset(rdch, 0); 628 pcm_chnrelease(rdch); 629 PCM_RDCH(i_dev) = NULL; 630 } 631 if (wrch) { 632 /* 633 * Please see block above. 634 */ 635 PCM_SG_LOCK(); 636 sg_ids = chn_syncdestroy(wrch); 637 PCM_SG_UNLOCK(); 638 if (sg_ids != 0) 639 free_unr(pcmsg_unrhdr, sg_ids); 640 641 CHN_LOCK(wrch); 642 refs += pcm_chnref(wrch, -1); 643 chn_flush(wrch); /* may sleep */ 644 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD); 645 chn_reset(wrch, 0); 646 pcm_chnrelease(wrch); 647 PCM_WRCH(i_dev) = NULL; 648 } 649 650 pcm_lock(d); 651 /* 652 * If there are no more references, release the channels. 653 */ 654 if (refs == 0 && PCM_RDCH(i_dev) == NULL && 655 PCM_WRCH(i_dev) == NULL) { 656 dsp_cdevinfo_free(i_dev); 657 /* 658 * Release clone busy state and unref it 659 * so the automatic garbage collector will 660 * get the hint and do the remaining cleanup 661 * process. 662 */ 663 (void)snd_clone_release(i_dev); 664 665 /* 666 * destroy_dev() might sleep, so release pcm lock 667 * here and rely on pcm cv serialization. 668 */ 669 pcm_unlock(d); 670 (void)snd_clone_unref(i_dev); 671 pcm_lock(d); 672 } 673 PCM_RELEASE(d); 674 } 675 676 pcm_unlock(d); 677 678 PCM_GIANT_LEAVE(d); 679 680 return (0); 681 } 682 683 static __inline int 684 dsp_io_ops(struct cdev *i_dev, struct uio *buf) 685 { 686 struct snddev_info *d; 687 struct pcm_channel **ch, *rdch, *wrch; 688 int (*chn_io)(struct pcm_channel *, struct uio *); 689 int prio, ret; 690 pid_t runpid; 691 692 KASSERT(i_dev != NULL && buf != NULL && 693 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE), 694 ("%s(): io train wreck!", __func__)); 695 696 d = dsp_get_info(i_dev); 697 if (!DSP_REGISTERED(d, i_dev)) 698 return (EBADF); 699 700 PCM_GIANT_ENTER(d); 701 702 switch (buf->uio_rw) { 703 case UIO_READ: 704 prio = SD_F_PRIO_RD; 705 ch = &rdch; 706 chn_io = chn_read; 707 break; 708 case UIO_WRITE: 709 prio = SD_F_PRIO_WR; 710 ch = &wrch; 711 chn_io = chn_write; 712 break; 713 default: 714 panic("invalid/corrupted uio direction: %d", buf->uio_rw); 715 break; 716 } 717 718 rdch = NULL; 719 wrch = NULL; 720 runpid = buf->uio_td->td_proc->p_pid; 721 722 getchns(i_dev, &rdch, &wrch, prio); 723 724 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) { 725 PCM_GIANT_EXIT(d); 726 return (EBADF); 727 } 728 729 if (((*ch)->flags & (CHN_F_MAPPED | CHN_F_DEAD)) || 730 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) { 731 relchns(i_dev, rdch, wrch, prio); 732 PCM_GIANT_EXIT(d); 733 return (EINVAL); 734 } else if (!((*ch)->flags & CHN_F_RUNNING)) { 735 (*ch)->flags |= CHN_F_RUNNING; 736 (*ch)->pid = runpid; 737 } 738 739 /* 740 * chn_read/write must give up channel lock in order to copy bytes 741 * from/to userland, so up the "in progress" counter to make sure 742 * someone else doesn't come along and muss up the buffer. 743 */ 744 ++(*ch)->inprog; 745 ret = chn_io(*ch, buf); 746 --(*ch)->inprog; 747 748 CHN_BROADCAST(&(*ch)->cv); 749 750 relchns(i_dev, rdch, wrch, prio); 751 752 PCM_GIANT_LEAVE(d); 753 754 return (ret); 755 } 756 757 static int 758 dsp_read(struct cdev *i_dev, struct uio *buf, int flag) 759 { 760 return (dsp_io_ops(i_dev, buf)); 761 } 762 763 static int 764 dsp_write(struct cdev *i_dev, struct uio *buf, int flag) 765 { 766 return (dsp_io_ops(i_dev, buf)); 767 } 768 769 static int 770 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td) 771 { 772 struct pcm_channel *chn, *rdch, *wrch; 773 struct snddev_info *d; 774 int *arg_i, ret, kill, tmp, xcmd; 775 776 d = dsp_get_info(i_dev); 777 if (!DSP_REGISTERED(d, i_dev)) 778 return (EBADF); 779 780 PCM_GIANT_ENTER(d); 781 782 arg_i = (int *)arg; 783 ret = 0; 784 xcmd = 0; 785 786 /* 787 * this is an evil hack to allow broken apps to perform mixer ioctls 788 * on dsp devices. 789 */ 790 if (IOCGROUP(cmd) == 'M') { 791 /* 792 * This is at least, a bug to bug compatible with OSS. 793 */ 794 if (d->mixer_dev != NULL) { 795 PCM_ACQUIRE_QUICK(d); 796 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td, 797 MIXER_CMD_DIRECT); 798 PCM_RELEASE_QUICK(d); 799 } else 800 ret = EBADF; 801 802 PCM_GIANT_EXIT(d); 803 804 return (ret); 805 } 806 807 /* 808 * Certain ioctls may be made on any type of device (audio, mixer, 809 * and MIDI). Handle those special cases here. 810 */ 811 if (IOCGROUP(cmd) == 'X') { 812 PCM_ACQUIRE_QUICK(d); 813 switch(cmd) { 814 case SNDCTL_SYSINFO: 815 sound_oss_sysinfo((oss_sysinfo *)arg); 816 break; 817 case SNDCTL_AUDIOINFO: 818 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg); 819 break; 820 case SNDCTL_MIXERINFO: 821 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg); 822 break; 823 default: 824 ret = EINVAL; 825 } 826 PCM_RELEASE_QUICK(d); 827 PCM_GIANT_EXIT(d); 828 return (ret); 829 } 830 831 getchns(i_dev, &rdch, &wrch, 0); 832 833 kill = 0; 834 if (wrch && (wrch->flags & CHN_F_DEAD)) 835 kill |= 1; 836 if (rdch && (rdch->flags & CHN_F_DEAD)) 837 kill |= 2; 838 if (kill == 3) { 839 relchns(i_dev, rdch, wrch, 0); 840 PCM_GIANT_EXIT(d); 841 return (EINVAL); 842 } 843 if (kill & 1) 844 wrch = NULL; 845 if (kill & 2) 846 rdch = NULL; 847 848 if (wrch == NULL && rdch == NULL) { 849 relchns(i_dev, rdch, wrch, 0); 850 PCM_GIANT_EXIT(d); 851 return (EINVAL); 852 } 853 854 switch(cmd) { 855 #ifdef OLDPCM_IOCTL 856 /* 857 * we start with the new ioctl interface. 858 */ 859 case AIONWRITE: /* how many bytes can write ? */ 860 if (wrch) { 861 CHN_LOCK(wrch); 862 /* 863 if (wrch && wrch->bufhard.dl) 864 while (chn_wrfeed(wrch) == 0); 865 */ 866 *arg_i = sndbuf_getfree(wrch->bufsoft); 867 CHN_UNLOCK(wrch); 868 } else { 869 *arg_i = 0; 870 ret = EINVAL; 871 } 872 break; 873 874 case AIOSSIZE: /* set the current blocksize */ 875 { 876 struct snd_size *p = (struct snd_size *)arg; 877 878 p->play_size = 0; 879 p->rec_size = 0; 880 PCM_ACQUIRE_QUICK(d); 881 if (wrch) { 882 CHN_LOCK(wrch); 883 chn_setblocksize(wrch, 2, p->play_size); 884 p->play_size = sndbuf_getblksz(wrch->bufsoft); 885 CHN_UNLOCK(wrch); 886 } 887 if (rdch) { 888 CHN_LOCK(rdch); 889 chn_setblocksize(rdch, 2, p->rec_size); 890 p->rec_size = sndbuf_getblksz(rdch->bufsoft); 891 CHN_UNLOCK(rdch); 892 } 893 PCM_RELEASE_QUICK(d); 894 } 895 break; 896 case AIOGSIZE: /* get the current blocksize */ 897 { 898 struct snd_size *p = (struct snd_size *)arg; 899 900 if (wrch) { 901 CHN_LOCK(wrch); 902 p->play_size = sndbuf_getblksz(wrch->bufsoft); 903 CHN_UNLOCK(wrch); 904 } 905 if (rdch) { 906 CHN_LOCK(rdch); 907 p->rec_size = sndbuf_getblksz(rdch->bufsoft); 908 CHN_UNLOCK(rdch); 909 } 910 } 911 break; 912 913 case AIOSFMT: 914 case AIOGFMT: 915 { 916 snd_chan_param *p = (snd_chan_param *)arg; 917 918 if (cmd == AIOSFMT && 919 ((p->play_format != 0 && p->play_rate == 0) || 920 (p->rec_format != 0 && p->rec_rate == 0))) { 921 ret = EINVAL; 922 break; 923 } 924 PCM_ACQUIRE_QUICK(d); 925 if (wrch) { 926 CHN_LOCK(wrch); 927 if (cmd == AIOSFMT && p->play_format != 0) { 928 chn_setformat(wrch, p->play_format); 929 chn_setspeed(wrch, p->play_rate); 930 } 931 p->play_rate = wrch->speed; 932 p->play_format = wrch->format; 933 CHN_UNLOCK(wrch); 934 } else { 935 p->play_rate = 0; 936 p->play_format = 0; 937 } 938 if (rdch) { 939 CHN_LOCK(rdch); 940 if (cmd == AIOSFMT && p->rec_format != 0) { 941 chn_setformat(rdch, p->rec_format); 942 chn_setspeed(rdch, p->rec_rate); 943 } 944 p->rec_rate = rdch->speed; 945 p->rec_format = rdch->format; 946 CHN_UNLOCK(rdch); 947 } else { 948 p->rec_rate = 0; 949 p->rec_format = 0; 950 } 951 PCM_RELEASE_QUICK(d); 952 } 953 break; 954 955 case AIOGCAP: /* get capabilities */ 956 { 957 snd_capabilities *p = (snd_capabilities *)arg; 958 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL; 959 struct cdev *pdev; 960 961 pcm_lock(d); 962 if (rdch) { 963 CHN_LOCK(rdch); 964 rcaps = chn_getcaps(rdch); 965 } 966 if (wrch) { 967 CHN_LOCK(wrch); 968 pcaps = chn_getcaps(wrch); 969 } 970 p->rate_min = max(rcaps? rcaps->minspeed : 0, 971 pcaps? pcaps->minspeed : 0); 972 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000, 973 pcaps? pcaps->maxspeed : 1000000); 974 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000, 975 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000); 976 /* XXX bad on sb16 */ 977 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) & 978 (wrch? chn_getformats(wrch) : 0xffffffff); 979 if (rdch && wrch) 980 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX; 981 pdev = d->mixer_dev; 982 p->mixers = 1; /* default: one mixer */ 983 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0; 984 p->left = p->right = 100; 985 if (wrch) 986 CHN_UNLOCK(wrch); 987 if (rdch) 988 CHN_UNLOCK(rdch); 989 pcm_unlock(d); 990 } 991 break; 992 993 case AIOSTOP: 994 if (*arg_i == AIOSYNC_PLAY && wrch) { 995 CHN_LOCK(wrch); 996 *arg_i = chn_abort(wrch); 997 CHN_UNLOCK(wrch); 998 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) { 999 CHN_LOCK(rdch); 1000 *arg_i = chn_abort(rdch); 1001 CHN_UNLOCK(rdch); 1002 } else { 1003 printf("AIOSTOP: bad channel 0x%x\n", *arg_i); 1004 *arg_i = 0; 1005 } 1006 break; 1007 1008 case AIOSYNC: 1009 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n", 1010 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos); 1011 break; 1012 #endif 1013 /* 1014 * here follow the standard ioctls (filio.h etc.) 1015 */ 1016 case FIONREAD: /* get # bytes to read */ 1017 if (rdch) { 1018 CHN_LOCK(rdch); 1019 /* if (rdch && rdch->bufhard.dl) 1020 while (chn_rdfeed(rdch) == 0); 1021 */ 1022 *arg_i = sndbuf_getready(rdch->bufsoft); 1023 CHN_UNLOCK(rdch); 1024 } else { 1025 *arg_i = 0; 1026 ret = EINVAL; 1027 } 1028 break; 1029 1030 case FIOASYNC: /*set/clear async i/o */ 1031 DEB( printf("FIOASYNC\n") ; ) 1032 break; 1033 1034 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */ 1035 case FIONBIO: /* set/clear non-blocking i/o */ 1036 if (rdch) { 1037 CHN_LOCK(rdch); 1038 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i) 1039 rdch->flags |= CHN_F_NBIO; 1040 else 1041 rdch->flags &= ~CHN_F_NBIO; 1042 CHN_UNLOCK(rdch); 1043 } 1044 if (wrch) { 1045 CHN_LOCK(wrch); 1046 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i) 1047 wrch->flags |= CHN_F_NBIO; 1048 else 1049 wrch->flags &= ~CHN_F_NBIO; 1050 CHN_UNLOCK(wrch); 1051 } 1052 break; 1053 1054 /* 1055 * Finally, here is the linux-compatible ioctl interface 1056 */ 1057 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int) 1058 case THE_REAL_SNDCTL_DSP_GETBLKSIZE: 1059 case SNDCTL_DSP_GETBLKSIZE: 1060 chn = wrch ? wrch : rdch; 1061 if (chn) { 1062 CHN_LOCK(chn); 1063 *arg_i = sndbuf_getblksz(chn->bufsoft); 1064 CHN_UNLOCK(chn); 1065 } else { 1066 *arg_i = 0; 1067 ret = EINVAL; 1068 } 1069 break; 1070 1071 case SNDCTL_DSP_SETBLKSIZE: 1072 RANGE(*arg_i, 16, 65536); 1073 PCM_ACQUIRE_QUICK(d); 1074 if (wrch) { 1075 CHN_LOCK(wrch); 1076 chn_setblocksize(wrch, 2, *arg_i); 1077 CHN_UNLOCK(wrch); 1078 } 1079 if (rdch) { 1080 CHN_LOCK(rdch); 1081 chn_setblocksize(rdch, 2, *arg_i); 1082 CHN_UNLOCK(rdch); 1083 } 1084 PCM_RELEASE_QUICK(d); 1085 break; 1086 1087 case SNDCTL_DSP_RESET: 1088 DEB(printf("dsp reset\n")); 1089 if (wrch) { 1090 CHN_LOCK(wrch); 1091 chn_abort(wrch); 1092 chn_resetbuf(wrch); 1093 CHN_UNLOCK(wrch); 1094 } 1095 if (rdch) { 1096 CHN_LOCK(rdch); 1097 chn_abort(rdch); 1098 chn_resetbuf(rdch); 1099 CHN_UNLOCK(rdch); 1100 } 1101 break; 1102 1103 case SNDCTL_DSP_SYNC: 1104 DEB(printf("dsp sync\n")); 1105 /* chn_sync may sleep */ 1106 if (wrch) { 1107 CHN_LOCK(wrch); 1108 chn_sync(wrch, 0); 1109 CHN_UNLOCK(wrch); 1110 } 1111 break; 1112 1113 case SNDCTL_DSP_SPEED: 1114 /* chn_setspeed may sleep */ 1115 tmp = 0; 1116 PCM_ACQUIRE_QUICK(d); 1117 if (wrch) { 1118 CHN_LOCK(wrch); 1119 ret = chn_setspeed(wrch, *arg_i); 1120 tmp = wrch->speed; 1121 CHN_UNLOCK(wrch); 1122 } 1123 if (rdch && ret == 0) { 1124 CHN_LOCK(rdch); 1125 ret = chn_setspeed(rdch, *arg_i); 1126 if (tmp == 0) 1127 tmp = rdch->speed; 1128 CHN_UNLOCK(rdch); 1129 } 1130 PCM_RELEASE_QUICK(d); 1131 *arg_i = tmp; 1132 break; 1133 1134 case SOUND_PCM_READ_RATE: 1135 chn = wrch ? wrch : rdch; 1136 if (chn) { 1137 CHN_LOCK(chn); 1138 *arg_i = chn->speed; 1139 CHN_UNLOCK(chn); 1140 } else { 1141 *arg_i = 0; 1142 ret = EINVAL; 1143 } 1144 break; 1145 1146 case SNDCTL_DSP_STEREO: 1147 tmp = -1; 1148 *arg_i = (*arg_i)? AFMT_STEREO : 0; 1149 PCM_ACQUIRE_QUICK(d); 1150 if (wrch) { 1151 CHN_LOCK(wrch); 1152 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i); 1153 tmp = (wrch->format & AFMT_STEREO)? 1 : 0; 1154 CHN_UNLOCK(wrch); 1155 } 1156 if (rdch && ret == 0) { 1157 CHN_LOCK(rdch); 1158 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i); 1159 if (tmp == -1) 1160 tmp = (rdch->format & AFMT_STEREO)? 1 : 0; 1161 CHN_UNLOCK(rdch); 1162 } 1163 PCM_RELEASE_QUICK(d); 1164 *arg_i = tmp; 1165 break; 1166 1167 case SOUND_PCM_WRITE_CHANNELS: 1168 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */ 1169 if (*arg_i != 0) { 1170 tmp = 0; 1171 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0; 1172 PCM_ACQUIRE_QUICK(d); 1173 if (wrch) { 1174 CHN_LOCK(wrch); 1175 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i); 1176 tmp = (wrch->format & AFMT_STEREO)? 2 : 1; 1177 CHN_UNLOCK(wrch); 1178 } 1179 if (rdch && ret == 0) { 1180 CHN_LOCK(rdch); 1181 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i); 1182 if (tmp == 0) 1183 tmp = (rdch->format & AFMT_STEREO)? 2 : 1; 1184 CHN_UNLOCK(rdch); 1185 } 1186 PCM_RELEASE_QUICK(d); 1187 *arg_i = tmp; 1188 } else { 1189 chn = wrch ? wrch : rdch; 1190 CHN_LOCK(chn); 1191 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1; 1192 CHN_UNLOCK(chn); 1193 } 1194 break; 1195 1196 case SOUND_PCM_READ_CHANNELS: 1197 chn = wrch ? wrch : rdch; 1198 if (chn) { 1199 CHN_LOCK(chn); 1200 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1; 1201 CHN_UNLOCK(chn); 1202 } else { 1203 *arg_i = 0; 1204 ret = EINVAL; 1205 } 1206 break; 1207 1208 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */ 1209 chn = wrch ? wrch : rdch; 1210 if (chn) { 1211 CHN_LOCK(chn); 1212 *arg_i = chn_getformats(chn); 1213 CHN_UNLOCK(chn); 1214 } else { 1215 *arg_i = 0; 1216 ret = EINVAL; 1217 } 1218 break; 1219 1220 case SNDCTL_DSP_SETFMT: /* sets _one_ format */ 1221 if ((*arg_i != AFMT_QUERY)) { 1222 tmp = 0; 1223 PCM_ACQUIRE_QUICK(d); 1224 if (wrch) { 1225 CHN_LOCK(wrch); 1226 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO)); 1227 tmp = wrch->format & ~AFMT_STEREO; 1228 CHN_UNLOCK(wrch); 1229 } 1230 if (rdch && ret == 0) { 1231 CHN_LOCK(rdch); 1232 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO)); 1233 if (tmp == 0) 1234 tmp = rdch->format & ~AFMT_STEREO; 1235 CHN_UNLOCK(rdch); 1236 } 1237 PCM_RELEASE_QUICK(d); 1238 *arg_i = tmp; 1239 } else { 1240 chn = wrch ? wrch : rdch; 1241 CHN_LOCK(chn); 1242 *arg_i = chn->format & ~AFMT_STEREO; 1243 CHN_UNLOCK(chn); 1244 } 1245 break; 1246 1247 case SNDCTL_DSP_SETFRAGMENT: 1248 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg)); 1249 { 1250 uint32_t fragln = (*arg_i) & 0x0000ffff; 1251 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16; 1252 uint32_t fragsz; 1253 uint32_t r_maxfrags, r_fragsz; 1254 1255 RANGE(fragln, 4, 16); 1256 fragsz = 1 << fragln; 1257 1258 if (maxfrags == 0) 1259 maxfrags = CHN_2NDBUFMAXSIZE / fragsz; 1260 if (maxfrags < 2) 1261 maxfrags = 2; 1262 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE) 1263 maxfrags = CHN_2NDBUFMAXSIZE / fragsz; 1264 1265 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz)); 1266 PCM_ACQUIRE_QUICK(d); 1267 if (rdch) { 1268 CHN_LOCK(rdch); 1269 ret = chn_setblocksize(rdch, maxfrags, fragsz); 1270 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft); 1271 r_fragsz = sndbuf_getblksz(rdch->bufsoft); 1272 CHN_UNLOCK(rdch); 1273 } else { 1274 r_maxfrags = maxfrags; 1275 r_fragsz = fragsz; 1276 } 1277 if (wrch && ret == 0) { 1278 CHN_LOCK(wrch); 1279 ret = chn_setblocksize(wrch, maxfrags, fragsz); 1280 maxfrags = sndbuf_getblkcnt(wrch->bufsoft); 1281 fragsz = sndbuf_getblksz(wrch->bufsoft); 1282 CHN_UNLOCK(wrch); 1283 } else { /* use whatever came from the read channel */ 1284 maxfrags = r_maxfrags; 1285 fragsz = r_fragsz; 1286 } 1287 PCM_RELEASE_QUICK(d); 1288 1289 fragln = 0; 1290 while (fragsz > 1) { 1291 fragln++; 1292 fragsz >>= 1; 1293 } 1294 *arg_i = (maxfrags << 16) | fragln; 1295 } 1296 break; 1297 1298 case SNDCTL_DSP_GETISPACE: 1299 /* return the size of data available in the input queue */ 1300 { 1301 audio_buf_info *a = (audio_buf_info *)arg; 1302 if (rdch) { 1303 struct snd_dbuf *bs = rdch->bufsoft; 1304 1305 CHN_LOCK(rdch); 1306 a->bytes = sndbuf_getready(bs); 1307 a->fragments = a->bytes / sndbuf_getblksz(bs); 1308 a->fragstotal = sndbuf_getblkcnt(bs); 1309 a->fragsize = sndbuf_getblksz(bs); 1310 CHN_UNLOCK(rdch); 1311 } else 1312 ret = EINVAL; 1313 } 1314 break; 1315 1316 case SNDCTL_DSP_GETOSPACE: 1317 /* return space available in the output queue */ 1318 { 1319 audio_buf_info *a = (audio_buf_info *)arg; 1320 if (wrch) { 1321 struct snd_dbuf *bs = wrch->bufsoft; 1322 1323 CHN_LOCK(wrch); 1324 /* XXX abusive DMA update: chn_wrupdate(wrch); */ 1325 a->bytes = sndbuf_getfree(bs); 1326 a->fragments = a->bytes / sndbuf_getblksz(bs); 1327 a->fragstotal = sndbuf_getblkcnt(bs); 1328 a->fragsize = sndbuf_getblksz(bs); 1329 CHN_UNLOCK(wrch); 1330 } else 1331 ret = EINVAL; 1332 } 1333 break; 1334 1335 case SNDCTL_DSP_GETIPTR: 1336 { 1337 count_info *a = (count_info *)arg; 1338 if (rdch) { 1339 struct snd_dbuf *bs = rdch->bufsoft; 1340 1341 CHN_LOCK(rdch); 1342 /* XXX abusive DMA update: chn_rdupdate(rdch); */ 1343 a->bytes = sndbuf_gettotal(bs); 1344 a->blocks = sndbuf_getblocks(bs) - rdch->blocks; 1345 a->ptr = sndbuf_getreadyptr(bs); 1346 rdch->blocks = sndbuf_getblocks(bs); 1347 CHN_UNLOCK(rdch); 1348 } else 1349 ret = EINVAL; 1350 } 1351 break; 1352 1353 case SNDCTL_DSP_GETOPTR: 1354 { 1355 count_info *a = (count_info *)arg; 1356 if (wrch) { 1357 struct snd_dbuf *bs = wrch->bufsoft; 1358 1359 CHN_LOCK(wrch); 1360 /* XXX abusive DMA update: chn_wrupdate(wrch); */ 1361 a->bytes = sndbuf_gettotal(bs); 1362 a->blocks = sndbuf_getblocks(bs) - wrch->blocks; 1363 a->ptr = sndbuf_getreadyptr(bs); 1364 wrch->blocks = sndbuf_getblocks(bs); 1365 CHN_UNLOCK(wrch); 1366 } else 1367 ret = EINVAL; 1368 } 1369 break; 1370 1371 case SNDCTL_DSP_GETCAPS: 1372 pcm_lock(d); 1373 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER; 1374 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX)) 1375 *arg_i |= DSP_CAP_DUPLEX; 1376 pcm_unlock(d); 1377 break; 1378 1379 case SOUND_PCM_READ_BITS: 1380 chn = wrch ? wrch : rdch; 1381 if (chn) { 1382 CHN_LOCK(chn); 1383 if (chn->format & AFMT_8BIT) 1384 *arg_i = 8; 1385 else if (chn->format & AFMT_16BIT) 1386 *arg_i = 16; 1387 else if (chn->format & AFMT_24BIT) 1388 *arg_i = 24; 1389 else if (chn->format & AFMT_32BIT) 1390 *arg_i = 32; 1391 else 1392 ret = EINVAL; 1393 CHN_UNLOCK(chn); 1394 } else { 1395 *arg_i = 0; 1396 ret = EINVAL; 1397 } 1398 break; 1399 1400 case SNDCTL_DSP_SETTRIGGER: 1401 if (rdch) { 1402 CHN_LOCK(rdch); 1403 rdch->flags &= ~CHN_F_NOTRIGGER; 1404 if (*arg_i & PCM_ENABLE_INPUT) 1405 chn_start(rdch, 1); 1406 else { 1407 chn_abort(rdch); 1408 chn_resetbuf(rdch); 1409 rdch->flags |= CHN_F_NOTRIGGER; 1410 } 1411 CHN_UNLOCK(rdch); 1412 } 1413 if (wrch) { 1414 CHN_LOCK(wrch); 1415 wrch->flags &= ~CHN_F_NOTRIGGER; 1416 if (*arg_i & PCM_ENABLE_OUTPUT) 1417 chn_start(wrch, 1); 1418 else { 1419 chn_abort(wrch); 1420 chn_resetbuf(wrch); 1421 wrch->flags |= CHN_F_NOTRIGGER; 1422 } 1423 CHN_UNLOCK(wrch); 1424 } 1425 break; 1426 1427 case SNDCTL_DSP_GETTRIGGER: 1428 *arg_i = 0; 1429 if (wrch) { 1430 CHN_LOCK(wrch); 1431 if (wrch->flags & CHN_F_TRIGGERED) 1432 *arg_i |= PCM_ENABLE_OUTPUT; 1433 CHN_UNLOCK(wrch); 1434 } 1435 if (rdch) { 1436 CHN_LOCK(rdch); 1437 if (rdch->flags & CHN_F_TRIGGERED) 1438 *arg_i |= PCM_ENABLE_INPUT; 1439 CHN_UNLOCK(rdch); 1440 } 1441 break; 1442 1443 case SNDCTL_DSP_GETODELAY: 1444 if (wrch) { 1445 struct snd_dbuf *bs = wrch->bufsoft; 1446 1447 CHN_LOCK(wrch); 1448 /* XXX abusive DMA update: chn_wrupdate(wrch); */ 1449 *arg_i = sndbuf_getready(bs); 1450 CHN_UNLOCK(wrch); 1451 } else 1452 ret = EINVAL; 1453 break; 1454 1455 case SNDCTL_DSP_POST: 1456 if (wrch) { 1457 CHN_LOCK(wrch); 1458 wrch->flags &= ~CHN_F_NOTRIGGER; 1459 chn_start(wrch, 1); 1460 CHN_UNLOCK(wrch); 1461 } 1462 break; 1463 1464 case SNDCTL_DSP_SETDUPLEX: 1465 /* 1466 * switch to full-duplex mode if card is in half-duplex 1467 * mode and is able to work in full-duplex mode 1468 */ 1469 pcm_lock(d); 1470 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX)) 1471 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX); 1472 pcm_unlock(d); 1473 break; 1474 1475 /* 1476 * The following four ioctls are simple wrappers around mixer_ioctl 1477 * with no further processing. xcmd is short for "translated 1478 * command". 1479 */ 1480 case SNDCTL_DSP_GETRECVOL: 1481 if (xcmd == 0) 1482 xcmd = SOUND_MIXER_READ_RECLEV; 1483 /* FALLTHROUGH */ 1484 case SNDCTL_DSP_SETRECVOL: 1485 if (xcmd == 0) 1486 xcmd = SOUND_MIXER_WRITE_RECLEV; 1487 /* FALLTHROUGH */ 1488 case SNDCTL_DSP_GETPLAYVOL: 1489 if (xcmd == 0) 1490 xcmd = SOUND_MIXER_READ_PCM; 1491 /* FALLTHROUGH */ 1492 case SNDCTL_DSP_SETPLAYVOL: 1493 if (xcmd == 0) 1494 xcmd = SOUND_MIXER_WRITE_PCM; 1495 1496 if (d->mixer_dev != NULL) { 1497 PCM_ACQUIRE_QUICK(d); 1498 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td, 1499 MIXER_CMD_DIRECT); 1500 PCM_RELEASE_QUICK(d); 1501 } else 1502 ret = ENOTSUP; 1503 break; 1504 1505 case SNDCTL_DSP_GET_RECSRC_NAMES: 1506 case SNDCTL_DSP_GET_RECSRC: 1507 case SNDCTL_DSP_SET_RECSRC: 1508 if (d->mixer_dev != NULL) { 1509 PCM_ACQUIRE_QUICK(d); 1510 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td, 1511 MIXER_CMD_DIRECT); 1512 PCM_RELEASE_QUICK(d); 1513 } else 1514 ret = ENOTSUP; 1515 break; 1516 1517 /* 1518 * The following 3 ioctls aren't very useful at the moment. For 1519 * now, only a single channel is associated with a cdev (/dev/dspN 1520 * instance), so there's only a single output routing to use (i.e., 1521 * the wrch bound to this cdev). 1522 */ 1523 case SNDCTL_DSP_GET_PLAYTGT_NAMES: 1524 { 1525 oss_mixer_enuminfo *ei; 1526 ei = (oss_mixer_enuminfo *)arg; 1527 ei->dev = 0; 1528 ei->ctrl = 0; 1529 ei->version = 0; /* static for now */ 1530 ei->strindex[0] = 0; 1531 1532 if (wrch != NULL) { 1533 ei->nvalues = 1; 1534 strlcpy(ei->strings, wrch->name, 1535 sizeof(ei->strings)); 1536 } else { 1537 ei->nvalues = 0; 1538 ei->strings[0] = '\0'; 1539 } 1540 } 1541 break; 1542 case SNDCTL_DSP_GET_PLAYTGT: 1543 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */ 1544 /* 1545 * Re: SET_PLAYTGT 1546 * OSSv4: "The value that was accepted by the device will 1547 * be returned back in the variable pointed by the 1548 * argument." 1549 */ 1550 if (wrch != NULL) 1551 *arg_i = 0; 1552 else 1553 ret = EINVAL; 1554 break; 1555 1556 case SNDCTL_DSP_SILENCE: 1557 /* 1558 * Flush the software (pre-feed) buffer, but try to minimize playback 1559 * interruption. (I.e., record unplayed samples with intent to 1560 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause" 1561 * functionality. 1562 */ 1563 if (wrch == NULL) 1564 ret = EINVAL; 1565 else { 1566 struct snd_dbuf *bs; 1567 CHN_LOCK(wrch); 1568 while (wrch->inprog != 0) 1569 cv_wait(&wrch->cv, wrch->lock); 1570 bs = wrch->bufsoft; 1571 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) { 1572 bs->sl = sndbuf_getready(bs); 1573 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs)); 1574 sndbuf_fillsilence(bs); 1575 chn_start(wrch, 0); 1576 } 1577 CHN_UNLOCK(wrch); 1578 } 1579 break; 1580 1581 case SNDCTL_DSP_SKIP: 1582 /* 1583 * OSSv4 docs: "This ioctl call discards all unplayed samples in the 1584 * playback buffer by moving the current write position immediately 1585 * before the point where the device is currently reading the samples." 1586 */ 1587 if (wrch == NULL) 1588 ret = EINVAL; 1589 else { 1590 struct snd_dbuf *bs; 1591 CHN_LOCK(wrch); 1592 while (wrch->inprog != 0) 1593 cv_wait(&wrch->cv, wrch->lock); 1594 bs = wrch->bufsoft; 1595 if ((bs->shadbuf != NULL) && (bs->sl > 0)) { 1596 sndbuf_softreset(bs); 1597 sndbuf_acquire(bs, bs->shadbuf, bs->sl); 1598 bs->sl = 0; 1599 chn_start(wrch, 0); 1600 } 1601 CHN_UNLOCK(wrch); 1602 } 1603 break; 1604 1605 case SNDCTL_DSP_CURRENT_OPTR: 1606 case SNDCTL_DSP_CURRENT_IPTR: 1607 /** 1608 * @note Changing formats resets the buffer counters, which differs 1609 * from the 4Front drivers. However, I don't expect this to be 1610 * much of a problem. 1611 * 1612 * @note In a test where @c CURRENT_OPTR is called immediately after write 1613 * returns, this driver is about 32K samples behind whereas 1614 * 4Front's is about 8K samples behind. Should determine source 1615 * of discrepancy, even if only out of curiosity. 1616 * 1617 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR. 1618 */ 1619 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch; 1620 if (chn == NULL) 1621 ret = EINVAL; 1622 else { 1623 struct snd_dbuf *bs; 1624 /* int tmp; */ 1625 1626 oss_count_t *oc = (oss_count_t *)arg; 1627 1628 CHN_LOCK(chn); 1629 bs = chn->bufsoft; 1630 #if 0 1631 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b); 1632 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getbps(b); 1633 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getbps(b); 1634 #else 1635 oc->samples = sndbuf_gettotal(bs) / sndbuf_getbps(bs); 1636 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getbps(bs); 1637 #endif 1638 CHN_UNLOCK(chn); 1639 } 1640 break; 1641 1642 case SNDCTL_DSP_HALT_OUTPUT: 1643 case SNDCTL_DSP_HALT_INPUT: 1644 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch; 1645 if (chn == NULL) 1646 ret = EINVAL; 1647 else { 1648 CHN_LOCK(chn); 1649 chn_abort(chn); 1650 CHN_UNLOCK(chn); 1651 } 1652 break; 1653 1654 case SNDCTL_DSP_LOW_WATER: 1655 /* 1656 * Set the number of bytes required to attract attention by 1657 * select/poll. 1658 */ 1659 if (wrch != NULL) { 1660 CHN_LOCK(wrch); 1661 wrch->lw = (*arg_i > 1) ? *arg_i : 1; 1662 CHN_UNLOCK(wrch); 1663 } 1664 if (rdch != NULL) { 1665 CHN_LOCK(rdch); 1666 rdch->lw = (*arg_i > 1) ? *arg_i : 1; 1667 CHN_UNLOCK(rdch); 1668 } 1669 break; 1670 1671 case SNDCTL_DSP_GETERROR: 1672 /* 1673 * OSSv4 docs: "All errors and counters will automatically be 1674 * cleared to zeroes after the call so each call will return only 1675 * the errors that occurred after the previous invocation. ... The 1676 * play_underruns and rec_overrun fields are the only usefull fields 1677 * returned by OSS 4.0." 1678 */ 1679 { 1680 audio_errinfo *ei = (audio_errinfo *)arg; 1681 1682 bzero((void *)ei, sizeof(*ei)); 1683 1684 if (wrch != NULL) { 1685 CHN_LOCK(wrch); 1686 ei->play_underruns = wrch->xruns; 1687 wrch->xruns = 0; 1688 CHN_UNLOCK(wrch); 1689 } 1690 if (rdch != NULL) { 1691 CHN_LOCK(rdch); 1692 ei->rec_overruns = rdch->xruns; 1693 rdch->xruns = 0; 1694 CHN_UNLOCK(rdch); 1695 } 1696 } 1697 break; 1698 1699 case SNDCTL_DSP_SYNCGROUP: 1700 PCM_ACQUIRE_QUICK(d); 1701 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg); 1702 PCM_RELEASE_QUICK(d); 1703 break; 1704 1705 case SNDCTL_DSP_SYNCSTART: 1706 PCM_ACQUIRE_QUICK(d); 1707 ret = dsp_oss_syncstart(*arg_i); 1708 PCM_RELEASE_QUICK(d); 1709 break; 1710 1711 case SNDCTL_DSP_POLICY: 1712 PCM_ACQUIRE_QUICK(d); 1713 ret = dsp_oss_policy(wrch, rdch, *arg_i); 1714 PCM_RELEASE_QUICK(d); 1715 break; 1716 1717 #ifdef OSSV4_EXPERIMENT 1718 /* 1719 * XXX The following ioctls are not yet supported and just return 1720 * EINVAL. 1721 */ 1722 case SNDCTL_DSP_GETOPEAKS: 1723 case SNDCTL_DSP_GETIPEAKS: 1724 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch; 1725 if (chn == NULL) 1726 ret = EINVAL; 1727 else { 1728 oss_peaks_t *op = (oss_peaks_t *)arg; 1729 int lpeak, rpeak; 1730 1731 CHN_LOCK(chn); 1732 ret = chn_getpeaks(chn, &lpeak, &rpeak); 1733 if (ret == -1) 1734 ret = EINVAL; 1735 else { 1736 (*op)[0] = lpeak; 1737 (*op)[1] = rpeak; 1738 } 1739 CHN_UNLOCK(chn); 1740 } 1741 break; 1742 1743 /* 1744 * XXX Once implemented, revisit this for proper cv protection 1745 * (if necessary). 1746 */ 1747 case SNDCTL_DSP_COOKEDMODE: 1748 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i); 1749 break; 1750 case SNDCTL_DSP_GET_CHNORDER: 1751 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg); 1752 break; 1753 case SNDCTL_DSP_SET_CHNORDER: 1754 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg); 1755 break; 1756 case SNDCTL_GETLABEL: 1757 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg); 1758 break; 1759 case SNDCTL_SETLABEL: 1760 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg); 1761 break; 1762 case SNDCTL_GETSONG: 1763 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg); 1764 break; 1765 case SNDCTL_SETSONG: 1766 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg); 1767 break; 1768 case SNDCTL_SETNAME: 1769 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg); 1770 break; 1771 #if 0 1772 /** 1773 * @note The SNDCTL_CARDINFO ioctl was omitted per 4Front developer 1774 * documentation. "The usability of this call is very limited. It's 1775 * provided only for completeness of the API. OSS API doesn't have 1776 * any concept of card. Any information returned by this ioctl calld 1777 * is reserved exclusively for the utility programs included in the 1778 * OSS package. Applications should not try to use for this 1779 * information in any ways." 1780 */ 1781 case SNDCTL_CARDINFO: 1782 ret = EINVAL; 1783 break; 1784 /** 1785 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and 1786 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of 1787 * 4Front Technologies. 1788 */ 1789 case SNDCTL_DSP_READCTL: 1790 case SNDCTL_DSP_WRITECTL: 1791 ret = EINVAL; 1792 break; 1793 #endif /* !0 (explicitly omitted ioctls) */ 1794 1795 #endif /* !OSSV4_EXPERIMENT */ 1796 case SNDCTL_DSP_MAPINBUF: 1797 case SNDCTL_DSP_MAPOUTBUF: 1798 case SNDCTL_DSP_SETSYNCRO: 1799 /* undocumented */ 1800 1801 case SNDCTL_DSP_SUBDIVIDE: 1802 case SOUND_PCM_WRITE_FILTER: 1803 case SOUND_PCM_READ_FILTER: 1804 /* dunno what these do, don't sound important */ 1805 1806 default: 1807 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd)); 1808 ret = EINVAL; 1809 break; 1810 } 1811 1812 relchns(i_dev, rdch, wrch, 0); 1813 1814 PCM_GIANT_LEAVE(d); 1815 1816 return (ret); 1817 } 1818 1819 static int 1820 dsp_poll(struct cdev *i_dev, int events, struct thread *td) 1821 { 1822 struct snddev_info *d; 1823 struct pcm_channel *wrch, *rdch; 1824 int ret, e; 1825 1826 d = dsp_get_info(i_dev); 1827 if (!DSP_REGISTERED(d, i_dev)) 1828 return (EBADF); 1829 1830 PCM_GIANT_ENTER(d); 1831 1832 wrch = NULL; 1833 rdch = NULL; 1834 ret = 0; 1835 1836 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1837 1838 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) { 1839 e = (events & (POLLOUT | POLLWRNORM)); 1840 if (e) 1841 ret |= chn_poll(wrch, e, td); 1842 } 1843 1844 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) { 1845 e = (events & (POLLIN | POLLRDNORM)); 1846 if (e) 1847 ret |= chn_poll(rdch, e, td); 1848 } 1849 1850 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1851 1852 PCM_GIANT_LEAVE(d); 1853 1854 return (ret); 1855 } 1856 1857 static int 1858 dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 1859 { 1860 struct snddev_info *d; 1861 struct pcm_channel *wrch, *rdch, *c; 1862 1863 /* 1864 * Reject PROT_EXEC by default. It just doesn't makes sense. 1865 * Unfortunately, we have to give up this one due to linux_mmap 1866 * changes. 1867 * 1868 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html 1869 * 1870 */ 1871 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec == 0) 1872 return (-1); 1873 1874 d = dsp_get_info(i_dev); 1875 if (!DSP_REGISTERED(d, i_dev)) 1876 return (-1); 1877 1878 PCM_GIANT_ENTER(d); 1879 1880 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1881 1882 /* 1883 * XXX The linux api uses the nprot to select read/write buffer 1884 * our vm system doesn't allow this, so force write buffer. 1885 * 1886 * This is just a quack to fool full-duplex mmap, so that at 1887 * least playback _or_ recording works. If you really got the 1888 * urge to make _both_ work at the same time, avoid O_RDWR. 1889 * Just open each direction separately and mmap() it. 1890 * 1891 * Failure is not an option due to INVARIANTS check within 1892 * device_pager.c, which means, we have to give up one over 1893 * another. 1894 */ 1895 c = (wrch != NULL) ? wrch : rdch; 1896 1897 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) || 1898 offset >= sndbuf_getsize(c->bufsoft) || 1899 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) || 1900 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) { 1901 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1902 PCM_GIANT_EXIT(d); 1903 return (-1); 1904 } 1905 1906 /* XXX full-duplex quack. */ 1907 if (wrch != NULL) 1908 wrch->flags |= CHN_F_MAPPED; 1909 if (rdch != NULL) 1910 rdch->flags |= CHN_F_MAPPED; 1911 1912 *paddr = vtophys(sndbuf_getbufofs(c->bufsoft, offset)); 1913 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1914 1915 PCM_GIANT_LEAVE(d); 1916 1917 return (0); 1918 } 1919 1920 #ifdef USING_DEVFS 1921 1922 /* So much for dev_stdclone() */ 1923 static int 1924 dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c) 1925 { 1926 size_t len; 1927 1928 len = strlen(namep); 1929 1930 if (bcmp(name, namep, len) != 0) 1931 return (ENODEV); 1932 1933 name += len; 1934 1935 if (isdigit(*name) == 0) 1936 return (ENODEV); 1937 1938 len = strlen(sep); 1939 1940 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0)) 1941 return (ENODEV); 1942 1943 for (*u = 0; isdigit(*name) != 0; name++) { 1944 *u *= 10; 1945 *u += *name - '0'; 1946 if (*u > dsp_umax) 1947 return (ENODEV); 1948 } 1949 1950 if (*name == '\0') 1951 return ((use_sep == 0) ? 0 : ENODEV); 1952 1953 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0) 1954 return (ENODEV); 1955 1956 name += len; 1957 1958 if (*name == '0' && name[1] != '\0') 1959 return (ENODEV); 1960 1961 for (*c = 0; isdigit(*name) != 0; name++) { 1962 *c *= 10; 1963 *c += *name - '0'; 1964 if (*c > dsp_cmax) 1965 return (ENODEV); 1966 } 1967 1968 if (*name != '\0') 1969 return (ENODEV); 1970 1971 return (0); 1972 } 1973 1974 static void 1975 dsp_clone(void *arg, 1976 #if __FreeBSD_version >= 600034 1977 struct ucred *cred, 1978 #endif 1979 char *name, int namelen, struct cdev **dev) 1980 { 1981 struct snddev_info *d; 1982 struct snd_clone_entry *ce; 1983 struct pcm_channel *c; 1984 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax; 1985 char *devname, *devsep; 1986 1987 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!")); 1988 1989 if (*dev != NULL) 1990 return; 1991 1992 unit = -1; 1993 cunit = -1; 1994 devtype = -1; 1995 devhw = 0; 1996 devcmax = -1; 1997 tumax = -1; 1998 devname = NULL; 1999 devsep = NULL; 2000 2001 for (i = 0; unit == -1 && 2002 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) { 2003 devtype = dsp_cdevs[i].type; 2004 devname = dsp_cdevs[i].name; 2005 devsep = dsp_cdevs[i].sep; 2006 devhw = dsp_cdevs[i].hw; 2007 devcmax = dsp_cdevs[i].max - 1; 2008 if (strcmp(name, devname) == 0) 2009 unit = snd_unit; 2010 else if (dsp_stdclone(name, devname, devsep, 2011 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) { 2012 unit = -1; 2013 cunit = -1; 2014 } 2015 } 2016 2017 d = devclass_get_softc(pcm_devclass, unit); 2018 if (!PCM_REGISTERED(d) || d->clones == NULL) 2019 return; 2020 2021 /* XXX Need Giant magic entry ??? */ 2022 2023 pcm_lock(d); 2024 if (snd_clone_disabled(d->clones)) { 2025 pcm_unlock(d); 2026 return; 2027 } 2028 2029 PCM_WAIT(d); 2030 PCM_ACQUIRE(d); 2031 pcm_unlock(d); 2032 2033 udcmask = snd_u2unit(unit) | snd_d2unit(devtype); 2034 2035 if (devhw != 0) { 2036 KASSERT(devcmax <= dsp_cmax, 2037 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax)); 2038 if (cunit > devcmax) { 2039 PCM_RELEASE_QUICK(d); 2040 return; 2041 } 2042 udcmask |= snd_c2unit(cunit); 2043 CHN_FOREACH(c, d, channels.pcm) { 2044 CHN_LOCK(c); 2045 if (c->unit != udcmask) { 2046 CHN_UNLOCK(c); 2047 continue; 2048 } 2049 CHN_UNLOCK(c); 2050 udcmask &= ~snd_c2unit(cunit); 2051 /* 2052 * Temporarily increase clone maxunit to overcome 2053 * vchan flexibility. 2054 * 2055 * # sysctl dev.pcm.0.play.vchans=256 2056 * dev.pcm.0.play.vchans: 1 -> 256 2057 * # cat /dev/zero > /dev/dsp0.vp255 & 2058 * [1] 17296 2059 * # sysctl dev.pcm.0.play.vchans=0 2060 * dev.pcm.0.play.vchans: 256 -> 1 2061 * # fg 2062 * [1] + running cat /dev/zero > /dev/dsp0.vp255 2063 * ^C 2064 * # cat /dev/zero > /dev/dsp0.vp255 2065 * zsh: operation not supported: /dev/dsp0.vp255 2066 */ 2067 tumax = snd_clone_getmaxunit(d->clones); 2068 if (cunit > tumax) 2069 snd_clone_setmaxunit(d->clones, cunit); 2070 else 2071 tumax = -1; 2072 goto dsp_clone_alloc; 2073 } 2074 /* 2075 * Ok, so we're requesting unallocated vchan, but still 2076 * within maximum vchan limit. 2077 */ 2078 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) || 2079 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) && 2080 cunit < snd_maxautovchans) { 2081 udcmask &= ~snd_c2unit(cunit); 2082 tumax = snd_clone_getmaxunit(d->clones); 2083 if (cunit > tumax) 2084 snd_clone_setmaxunit(d->clones, cunit); 2085 else 2086 tumax = -1; 2087 goto dsp_clone_alloc; 2088 } 2089 PCM_RELEASE_QUICK(d); 2090 return; 2091 } 2092 2093 dsp_clone_alloc: 2094 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask); 2095 if (tumax != -1) 2096 snd_clone_setmaxunit(d->clones, tumax); 2097 if (ce != NULL) { 2098 udcmask |= snd_c2unit(cunit); 2099 *dev = make_dev(&dsp_cdevsw, udcmask, 2100 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d", 2101 devname, unit, devsep, cunit); 2102 snd_clone_register(ce, *dev); 2103 } 2104 2105 PCM_RELEASE_QUICK(d); 2106 2107 if (*dev != NULL) 2108 dev_ref(*dev); 2109 } 2110 2111 static void 2112 dsp_sysinit(void *p) 2113 { 2114 if (dsp_ehtag != NULL) 2115 return; 2116 /* initialize unit numbering */ 2117 snd_unit_init(); 2118 dsp_umax = PCMMAXUNIT; 2119 dsp_cmax = PCMMAXCHAN; 2120 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000); 2121 } 2122 2123 static void 2124 dsp_sysuninit(void *p) 2125 { 2126 if (dsp_ehtag == NULL) 2127 return; 2128 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag); 2129 dsp_ehtag = NULL; 2130 } 2131 2132 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL); 2133 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL); 2134 #endif 2135 2136 char * 2137 dsp_unit2name(char *buf, size_t len, int unit) 2138 { 2139 int i, dtype; 2140 2141 KASSERT(buf != NULL && len != 0, 2142 ("bogus buf=%p len=%ju", buf, (uintmax_t)len)); 2143 2144 dtype = snd_unit2d(unit); 2145 2146 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) { 2147 if (dtype != dsp_cdevs[i].type) 2148 continue; 2149 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name, 2150 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit)); 2151 return (buf); 2152 } 2153 2154 return (NULL); 2155 } 2156 2157 /** 2158 * @brief Handler for SNDCTL_AUDIOINFO. 2159 * 2160 * Gathers information about the audio device specified in ai->dev. If 2161 * ai->dev == -1, then this function gathers information about the current 2162 * device. If the call comes in on a non-audio device and ai->dev == -1, 2163 * return EINVAL. 2164 * 2165 * This routine is supposed to go practically straight to the hardware, 2166 * getting capabilities directly from the sound card driver, side-stepping 2167 * the intermediate channel interface. 2168 * 2169 * Note, however, that the usefulness of this command is significantly 2170 * decreased when requesting info about any device other than the one serving 2171 * the request. While each snddev_channel refers to a specific device node, 2172 * the converse is *not* true. Currently, when a sound device node is opened, 2173 * the sound subsystem scans for an available audio channel (or channels, if 2174 * opened in read+write) and then assigns them to the si_drv[12] private 2175 * data fields. As a result, any information returned linking a channel to 2176 * a specific character device isn't necessarily accurate. 2177 * 2178 * @note 2179 * Calling threads must not hold any snddev_info or pcm_channel locks. 2180 * 2181 * @param dev device on which the ioctl was issued 2182 * @param ai ioctl request data container 2183 * 2184 * @retval 0 success 2185 * @retval EINVAL ai->dev specifies an invalid device 2186 * 2187 * @todo Verify correctness of Doxygen tags. ;) 2188 */ 2189 int 2190 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai) 2191 { 2192 struct pcmchan_caps *caps; 2193 struct pcm_channel *ch; 2194 struct snddev_info *d; 2195 uint32_t fmts; 2196 int i, nchan, *rates, minch, maxch; 2197 char *devname, buf[CHN_NAMELEN]; 2198 2199 /* 2200 * If probing the device that received the ioctl, make sure it's a 2201 * DSP device. (Users may use this ioctl with /dev/mixer and 2202 * /dev/midi.) 2203 */ 2204 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw) 2205 return (EINVAL); 2206 2207 ch = NULL; 2208 devname = NULL; 2209 nchan = 0; 2210 bzero(buf, sizeof(buf)); 2211 2212 /* 2213 * Search for the requested audio device (channel). Start by 2214 * iterating over pcm devices. 2215 */ 2216 for (i = 0; pcm_devclass != NULL && 2217 i < devclass_get_maxunit(pcm_devclass); i++) { 2218 d = devclass_get_softc(pcm_devclass, i); 2219 if (!PCM_REGISTERED(d)) 2220 continue; 2221 2222 /* XXX Need Giant magic entry ??? */ 2223 2224 /* See the note in function docblock */ 2225 mtx_assert(d->lock, MA_NOTOWNED); 2226 pcm_lock(d); 2227 2228 CHN_FOREACH(ch, d, channels.pcm) { 2229 mtx_assert(ch->lock, MA_NOTOWNED); 2230 CHN_LOCK(ch); 2231 if (ai->dev == -1) { 2232 if (DSP_REGISTERED(d, i_dev) && 2233 (ch == PCM_RDCH(i_dev) || /* record ch */ 2234 ch == PCM_WRCH(i_dev))) { /* playback ch */ 2235 devname = dsp_unit2name(buf, 2236 sizeof(buf), ch->unit); 2237 } 2238 } else if (ai->dev == nchan) { 2239 devname = dsp_unit2name(buf, sizeof(buf), 2240 ch->unit); 2241 } 2242 if (devname != NULL) 2243 break; 2244 CHN_UNLOCK(ch); 2245 ++nchan; 2246 } 2247 2248 if (devname != NULL) { 2249 /* 2250 * At this point, the following synchronization stuff 2251 * has happened: 2252 * - a specific PCM device is locked. 2253 * - a specific audio channel has been locked, so be 2254 * sure to unlock when exiting; 2255 */ 2256 2257 caps = chn_getcaps(ch); 2258 2259 /* 2260 * With all handles collected, zero out the user's 2261 * container and begin filling in its fields. 2262 */ 2263 bzero((void *)ai, sizeof(oss_audioinfo)); 2264 2265 ai->dev = nchan; 2266 strlcpy(ai->name, ch->name, sizeof(ai->name)); 2267 2268 if ((ch->flags & CHN_F_BUSY) == 0) 2269 ai->busy = 0; 2270 else 2271 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ; 2272 2273 /** 2274 * @note 2275 * @c cmd - OSSv4 docs: "Only supported under Linux at 2276 * this moment." Cop-out, I know, but I'll save 2277 * running around in the process table for later. 2278 * Is there a risk of leaking information? 2279 */ 2280 ai->pid = ch->pid; 2281 2282 /* 2283 * These flags stolen from SNDCTL_DSP_GETCAPS handler. 2284 * Note, however, that a single channel operates in 2285 * only one direction, so DSP_CAP_DUPLEX is out. 2286 */ 2287 /** 2288 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep 2289 * these in pcmchan::caps? 2290 */ 2291 ai->caps = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER; 2292 2293 /* 2294 * Collect formats supported @b natively by the 2295 * device. Also determine min/max channels. (I.e., 2296 * mono, stereo, or both?) 2297 * 2298 * If any channel is stereo, maxch = 2; 2299 * if all channels are stereo, minch = 2, too; 2300 * if any channel is mono, minch = 1; 2301 * and if all channels are mono, maxch = 1. 2302 */ 2303 minch = 0; 2304 maxch = 0; 2305 fmts = 0; 2306 for (i = 0; caps->fmtlist[i]; i++) { 2307 fmts |= caps->fmtlist[i]; 2308 if (caps->fmtlist[i] & AFMT_STEREO) { 2309 minch = (minch == 0) ? 2 : minch; 2310 maxch = 2; 2311 } else { 2312 minch = 1; 2313 maxch = (maxch == 0) ? 1 : maxch; 2314 } 2315 } 2316 2317 if (ch->direction == PCMDIR_PLAY) 2318 ai->oformats = fmts; 2319 else 2320 ai->iformats = fmts; 2321 2322 /** 2323 * @note 2324 * @c magic - OSSv4 docs: "Reserved for internal use 2325 * by OSS." 2326 * 2327 * @par 2328 * @c card_number - OSSv4 docs: "Number of the sound 2329 * card where this device belongs or -1 if this 2330 * information is not available. Applications 2331 * should normally not use this field for any 2332 * purpose." 2333 */ 2334 ai->card_number = -1; 2335 /** 2336 * @todo @c song_name - depends first on 2337 * SNDCTL_[GS]ETSONG @todo @c label - depends 2338 * on SNDCTL_[GS]ETLABEL 2339 * @todo @c port_number - routing information? 2340 */ 2341 ai->port_number = -1; 2342 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1; 2343 /** 2344 * @note 2345 * @c real_device - OSSv4 docs: "Obsolete." 2346 */ 2347 ai->real_device = -1; 2348 strlcpy(ai->devnode, devname, sizeof(ai->devnode)); 2349 ai->enabled = device_is_attached(d->dev) ? 1 : 0; 2350 /** 2351 * @note 2352 * @c flags - OSSv4 docs: "Reserved for future use." 2353 * 2354 * @note 2355 * @c binding - OSSv4 docs: "Reserved for future use." 2356 * 2357 * @todo @c handle - haven't decided how to generate 2358 * this yet; bus, vendor, device IDs? 2359 */ 2360 ai->min_rate = caps->minspeed; 2361 ai->max_rate = caps->maxspeed; 2362 2363 ai->min_channels = minch; 2364 ai->max_channels = maxch; 2365 2366 ai->nrates = chn_getrates(ch, &rates); 2367 if (ai->nrates > OSS_MAX_SAMPLE_RATES) 2368 ai->nrates = OSS_MAX_SAMPLE_RATES; 2369 2370 for (i = 0; i < ai->nrates; i++) 2371 ai->rates[i] = rates[i]; 2372 2373 CHN_UNLOCK(ch); 2374 } 2375 2376 pcm_unlock(d); 2377 2378 if (devname != NULL) 2379 return (0); 2380 } 2381 2382 /* Exhausted the search -- nothing is locked, so return. */ 2383 return (EINVAL); 2384 } 2385 2386 /** 2387 * @brief Assigns a PCM channel to a sync group. 2388 * 2389 * Sync groups are used to enable audio operations on multiple devices 2390 * simultaneously. They may be used with any number of devices and may 2391 * span across applications. Devices are added to groups with 2392 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the 2393 * SNDCTL_DSP_SYNCSTART ioctl. 2394 * 2395 * If the @c id field of the @c group parameter is set to zero, then a new 2396 * sync group is created. Otherwise, wrch and rdch (if set) are added to 2397 * the group specified. 2398 * 2399 * @todo As far as memory allocation, should we assume that things are 2400 * okay and allocate with M_WAITOK before acquiring channel locks, 2401 * freeing later if not? 2402 * 2403 * @param wrch output channel associated w/ device (if any) 2404 * @param rdch input channel associated w/ device (if any) 2405 * @param group Sync group parameters 2406 * 2407 * @retval 0 success 2408 * @retval non-zero error to be propagated upstream 2409 */ 2410 static int 2411 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group) 2412 { 2413 struct pcmchan_syncmember *smrd, *smwr; 2414 struct pcmchan_syncgroup *sg; 2415 int ret, sg_ids[3]; 2416 2417 smrd = NULL; 2418 smwr = NULL; 2419 sg = NULL; 2420 ret = 0; 2421 2422 /* 2423 * Free_unr() may sleep, so store released syncgroup IDs until after 2424 * all locks are released. 2425 */ 2426 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0; 2427 2428 PCM_SG_LOCK(); 2429 2430 /* 2431 * - Insert channel(s) into group's member list. 2432 * - Set CHN_F_NOTRIGGER on channel(s). 2433 * - Stop channel(s). 2434 */ 2435 2436 /* 2437 * If device's channels are already mapped to a group, unmap them. 2438 */ 2439 if (wrch) { 2440 CHN_LOCK(wrch); 2441 sg_ids[0] = chn_syncdestroy(wrch); 2442 } 2443 2444 if (rdch) { 2445 CHN_LOCK(rdch); 2446 sg_ids[1] = chn_syncdestroy(rdch); 2447 } 2448 2449 /* 2450 * Verify that mode matches character device properites. 2451 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL. 2452 * - Bail if PCM_ENABLE_INPUT && rdch == NULL. 2453 */ 2454 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) || 2455 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) { 2456 ret = EINVAL; 2457 goto out; 2458 } 2459 2460 /* 2461 * An id of zero indicates the user wants to create a new 2462 * syncgroup. 2463 */ 2464 if (group->id == 0) { 2465 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT); 2466 if (sg != NULL) { 2467 SLIST_INIT(&sg->members); 2468 sg->id = alloc_unr(pcmsg_unrhdr); 2469 2470 group->id = sg->id; 2471 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link); 2472 } else 2473 ret = ENOMEM; 2474 } else { 2475 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) { 2476 if (sg->id == group->id) 2477 break; 2478 } 2479 if (sg == NULL) 2480 ret = EINVAL; 2481 } 2482 2483 /* Couldn't create or find a syncgroup. Fail. */ 2484 if (sg == NULL) 2485 goto out; 2486 2487 /* 2488 * Allocate a syncmember, assign it and a channel together, and 2489 * insert into syncgroup. 2490 */ 2491 if (group->mode & PCM_ENABLE_INPUT) { 2492 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT); 2493 if (smrd == NULL) { 2494 ret = ENOMEM; 2495 goto out; 2496 } 2497 2498 SLIST_INSERT_HEAD(&sg->members, smrd, link); 2499 smrd->parent = sg; 2500 smrd->ch = rdch; 2501 2502 chn_abort(rdch); 2503 rdch->flags |= CHN_F_NOTRIGGER; 2504 rdch->sm = smrd; 2505 } 2506 2507 if (group->mode & PCM_ENABLE_OUTPUT) { 2508 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT); 2509 if (smwr == NULL) { 2510 ret = ENOMEM; 2511 goto out; 2512 } 2513 2514 SLIST_INSERT_HEAD(&sg->members, smwr, link); 2515 smwr->parent = sg; 2516 smwr->ch = wrch; 2517 2518 chn_abort(wrch); 2519 wrch->flags |= CHN_F_NOTRIGGER; 2520 wrch->sm = smwr; 2521 } 2522 2523 2524 out: 2525 if (ret != 0) { 2526 if (smrd != NULL) 2527 free(smrd, M_DEVBUF); 2528 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) { 2529 sg_ids[2] = sg->id; 2530 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link); 2531 free(sg, M_DEVBUF); 2532 } 2533 2534 if (wrch) 2535 wrch->sm = NULL; 2536 if (rdch) 2537 rdch->sm = NULL; 2538 } 2539 2540 if (wrch) 2541 CHN_UNLOCK(wrch); 2542 if (rdch) 2543 CHN_UNLOCK(rdch); 2544 2545 PCM_SG_UNLOCK(); 2546 2547 if (sg_ids[0]) 2548 free_unr(pcmsg_unrhdr, sg_ids[0]); 2549 if (sg_ids[1]) 2550 free_unr(pcmsg_unrhdr, sg_ids[1]); 2551 if (sg_ids[2]) 2552 free_unr(pcmsg_unrhdr, sg_ids[2]); 2553 2554 return (ret); 2555 } 2556 2557 /** 2558 * @brief Launch a sync group into action 2559 * 2560 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function 2561 * iterates over all members, triggering them along the way. 2562 * 2563 * @note Caller must not hold any channel locks. 2564 * 2565 * @param sg_id sync group identifier 2566 * 2567 * @retval 0 success 2568 * @retval non-zero error worthy of propagating upstream to user 2569 */ 2570 static int 2571 dsp_oss_syncstart(int sg_id) 2572 { 2573 struct pcmchan_syncmember *sm, *sm_tmp; 2574 struct pcmchan_syncgroup *sg; 2575 struct pcm_channel *c; 2576 int ret, needlocks; 2577 2578 /* Get the synclists lock */ 2579 PCM_SG_LOCK(); 2580 2581 do { 2582 ret = 0; 2583 needlocks = 0; 2584 2585 /* Search for syncgroup by ID */ 2586 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) { 2587 if (sg->id == sg_id) 2588 break; 2589 } 2590 2591 /* Return EINVAL if not found */ 2592 if (sg == NULL) { 2593 ret = EINVAL; 2594 break; 2595 } 2596 2597 /* Any removals resulting in an empty group should've handled this */ 2598 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup")); 2599 2600 /* 2601 * Attempt to lock all member channels - if any are already 2602 * locked, unlock those acquired, sleep for a bit, and try 2603 * again. 2604 */ 2605 SLIST_FOREACH(sm, &sg->members, link) { 2606 if (CHN_TRYLOCK(sm->ch) == 0) { 2607 int timo = hz * 5/1000; 2608 if (timo < 1) 2609 timo = 1; 2610 2611 /* Release all locked channels so far, retry */ 2612 SLIST_FOREACH(sm_tmp, &sg->members, link) { 2613 /* sm is the member already locked */ 2614 if (sm == sm_tmp) 2615 break; 2616 CHN_UNLOCK(sm_tmp->ch); 2617 } 2618 2619 /** @todo Is PRIBIO correct/ */ 2620 ret = msleep(sm, &snd_pcm_syncgroups_mtx, 2621 PRIBIO | PCATCH, "pcmsg", timo); 2622 if (ret == EINTR || ret == ERESTART) 2623 break; 2624 2625 needlocks = 1; 2626 ret = 0; /* Assumes ret == EAGAIN... */ 2627 } 2628 } 2629 } while (needlocks && ret == 0); 2630 2631 /* Proceed only if no errors encountered. */ 2632 if (ret == 0) { 2633 /* Launch channels */ 2634 while((sm = SLIST_FIRST(&sg->members)) != NULL) { 2635 SLIST_REMOVE_HEAD(&sg->members, link); 2636 2637 c = sm->ch; 2638 c->sm = NULL; 2639 chn_start(c, 1); 2640 c->flags &= ~CHN_F_NOTRIGGER; 2641 CHN_UNLOCK(c); 2642 2643 free(sm, M_DEVBUF); 2644 } 2645 2646 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link); 2647 free(sg, M_DEVBUF); 2648 } 2649 2650 PCM_SG_UNLOCK(); 2651 2652 /* 2653 * Free_unr() may sleep, so be sure to give up the syncgroup lock 2654 * first. 2655 */ 2656 if (ret == 0) 2657 free_unr(pcmsg_unrhdr, sg_id); 2658 2659 return (ret); 2660 } 2661 2662 /** 2663 * @brief Handler for SNDCTL_DSP_POLICY 2664 * 2665 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment 2666 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user 2667 * specifying those two parameters, s/he simply selects a number from 0..10 2668 * which corresponds to a buffer size. Smaller numbers request smaller 2669 * buffers with lower latencies (at greater overhead from more frequent 2670 * interrupts), while greater numbers behave in the opposite manner. 2671 * 2672 * The 4Front spec states that a value of 5 should be the default. However, 2673 * this implementation deviates slightly by using a linear scale without 2674 * consulting drivers. I.e., even though drivers may have different default 2675 * buffer sizes, a policy argument of 5 will have the same result across 2676 * all drivers. 2677 * 2678 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for 2679 * more information. 2680 * 2681 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to 2682 * work with hardware drivers directly. 2683 * 2684 * @note PCM channel arguments must not be locked by caller. 2685 * 2686 * @param wrch Pointer to opened playback channel (optional; may be NULL) 2687 * @param rdch " recording channel (optional; may be NULL) 2688 * @param policy Integer from [0:10] 2689 * 2690 * @retval 0 constant (for now) 2691 */ 2692 static int 2693 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy) 2694 { 2695 int ret; 2696 2697 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX) 2698 return (EIO); 2699 2700 /* Default: success */ 2701 ret = 0; 2702 2703 if (rdch) { 2704 CHN_LOCK(rdch); 2705 ret = chn_setlatency(rdch, policy); 2706 CHN_UNLOCK(rdch); 2707 } 2708 2709 if (wrch && ret == 0) { 2710 CHN_LOCK(wrch); 2711 ret = chn_setlatency(wrch, policy); 2712 CHN_UNLOCK(wrch); 2713 } 2714 2715 if (ret) 2716 ret = EIO; 2717 2718 return (ret); 2719 } 2720 2721 #ifdef OSSV4_EXPERIMENT 2722 /** 2723 * @brief Enable or disable "cooked" mode 2724 * 2725 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which 2726 * is the default, the sound system handles rate and format conversions 2727 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only 2728 * operates with 44100Hz/16bit/signed samples). 2729 * 2730 * Disabling cooked mode is intended for applications wanting to mmap() 2731 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage 2732 * feeder architecture, presumably to gain as much control over audio 2733 * hardware as possible. 2734 * 2735 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html 2736 * for more details. 2737 * 2738 * @note Currently, this function is just a stub that always returns EINVAL. 2739 * 2740 * @todo Figure out how to and actually implement this. 2741 * 2742 * @param wrch playback channel (optional; may be NULL) 2743 * @param rdch recording channel (optional; may be NULL) 2744 * @param enabled 0 = raw mode, 1 = cooked mode 2745 * 2746 * @retval EINVAL Operation not yet supported. 2747 */ 2748 static int 2749 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled) 2750 { 2751 return (EINVAL); 2752 } 2753 2754 /** 2755 * @brief Retrieve channel interleaving order 2756 * 2757 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER. 2758 * 2759 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html 2760 * for more details. 2761 * 2762 * @note As the ioctl definition is still under construction, FreeBSD 2763 * does not currently support SNDCTL_DSP_GET_CHNORDER. 2764 * 2765 * @param wrch playback channel (optional; may be NULL) 2766 * @param rdch recording channel (optional; may be NULL) 2767 * @param map channel map (result will be stored there) 2768 * 2769 * @retval EINVAL Operation not yet supported. 2770 */ 2771 static int 2772 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map) 2773 { 2774 return (EINVAL); 2775 } 2776 2777 /** 2778 * @brief Specify channel interleaving order 2779 * 2780 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER. 2781 * 2782 * @note As the ioctl definition is still under construction, FreeBSD 2783 * does not currently support @c SNDCTL_DSP_SET_CHNORDER. 2784 * 2785 * @param wrch playback channel (optional; may be NULL) 2786 * @param rdch recording channel (optional; may be NULL) 2787 * @param map channel map 2788 * 2789 * @retval EINVAL Operation not yet supported. 2790 */ 2791 static int 2792 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map) 2793 { 2794 return (EINVAL); 2795 } 2796 2797 /** 2798 * @brief Retrieve an audio device's label 2799 * 2800 * This is a handler for the @c SNDCTL_GETLABEL ioctl. 2801 * 2802 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html 2803 * for more details. 2804 * 2805 * From Hannu@4Front: "For example ossxmix (just like some HW mixer 2806 * consoles) can show variable "labels" for certain controls. By default 2807 * the application name (say quake) is shown as the label but 2808 * applications may change the labels themselves." 2809 * 2810 * @note As the ioctl definition is still under construction, FreeBSD 2811 * does not currently support @c SNDCTL_GETLABEL. 2812 * 2813 * @param wrch playback channel (optional; may be NULL) 2814 * @param rdch recording channel (optional; may be NULL) 2815 * @param label label gets copied here 2816 * 2817 * @retval EINVAL Operation not yet supported. 2818 */ 2819 static int 2820 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label) 2821 { 2822 return (EINVAL); 2823 } 2824 2825 /** 2826 * @brief Specify an audio device's label 2827 * 2828 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the 2829 * comments for @c dsp_oss_getlabel immediately above. 2830 * 2831 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html 2832 * for more details. 2833 * 2834 * @note As the ioctl definition is still under construction, FreeBSD 2835 * does not currently support SNDCTL_SETLABEL. 2836 * 2837 * @param wrch playback channel (optional; may be NULL) 2838 * @param rdch recording channel (optional; may be NULL) 2839 * @param label label gets copied from here 2840 * 2841 * @retval EINVAL Operation not yet supported. 2842 */ 2843 static int 2844 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label) 2845 { 2846 return (EINVAL); 2847 } 2848 2849 /** 2850 * @brief Retrieve name of currently played song 2851 * 2852 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could 2853 * tell the system the name of the currently playing song, which would be 2854 * visible in @c /dev/sndstat. 2855 * 2856 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html 2857 * for more details. 2858 * 2859 * @note As the ioctl definition is still under construction, FreeBSD 2860 * does not currently support SNDCTL_GETSONG. 2861 * 2862 * @param wrch playback channel (optional; may be NULL) 2863 * @param rdch recording channel (optional; may be NULL) 2864 * @param song song name gets copied here 2865 * 2866 * @retval EINVAL Operation not yet supported. 2867 */ 2868 static int 2869 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song) 2870 { 2871 return (EINVAL); 2872 } 2873 2874 /** 2875 * @brief Retrieve name of currently played song 2876 * 2877 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could 2878 * tell the system the name of the currently playing song, which would be 2879 * visible in @c /dev/sndstat. 2880 * 2881 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html 2882 * for more details. 2883 * 2884 * @note As the ioctl definition is still under construction, FreeBSD 2885 * does not currently support SNDCTL_SETSONG. 2886 * 2887 * @param wrch playback channel (optional; may be NULL) 2888 * @param rdch recording channel (optional; may be NULL) 2889 * @param song song name gets copied from here 2890 * 2891 * @retval EINVAL Operation not yet supported. 2892 */ 2893 static int 2894 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song) 2895 { 2896 return (EINVAL); 2897 } 2898 2899 /** 2900 * @brief Rename a device 2901 * 2902 * This is a handler for the @c SNDCTL_SETNAME ioctl. 2903 * 2904 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for 2905 * more details. 2906 * 2907 * From Hannu@4Front: "This call is used to change the device name 2908 * reported in /dev/sndstat and ossinfo. So instead of using some generic 2909 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull 2910 * name depending on the current context (for example 'OSS virtual wave table 2911 * synth' or 'VoIP link to London')." 2912 * 2913 * @note As the ioctl definition is still under construction, FreeBSD 2914 * does not currently support SNDCTL_SETNAME. 2915 * 2916 * @param wrch playback channel (optional; may be NULL) 2917 * @param rdch recording channel (optional; may be NULL) 2918 * @param name new device name gets copied from here 2919 * 2920 * @retval EINVAL Operation not yet supported. 2921 */ 2922 static int 2923 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name) 2924 { 2925 return (EINVAL); 2926 } 2927 #endif /* !OSSV4_EXPERIMENT */ 2928