1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org> 5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006 6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #ifdef HAVE_KERNEL_OPTION_HEADERS 32 #include "opt_snd.h" 33 #endif 34 35 #include <dev/sound/pcm/sound.h> 36 37 #include "feeder_if.h" 38 #include "mixer_if.h" 39 40 static MALLOC_DEFINE(M_MIXER, "mixer", "mixer"); 41 42 static int mixer_bypass = 1; 43 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_mixer_bypass, CTLFLAG_RWTUN, 44 &mixer_bypass, 0, 45 "control channel pcm/rec volume, bypassing real mixer device"); 46 47 #define MIXER_NAMELEN 16 48 struct snd_mixer { 49 KOBJ_FIELDS; 50 void *devinfo; 51 int busy; 52 int hwvol_mixer; 53 int hwvol_step; 54 int type; 55 device_t dev; 56 u_int32_t devs; 57 u_int32_t mutedevs; 58 u_int32_t recdevs; 59 u_int32_t recsrc; 60 u_int16_t level[32]; 61 u_int16_t level_muted[32]; 62 u_int8_t parent[32]; 63 u_int32_t child[32]; 64 u_int8_t realdev[32]; 65 char name[MIXER_NAMELEN]; 66 struct mtx *lock; 67 oss_mixer_enuminfo enuminfo; 68 /** 69 * Counter is incremented when applications change any of this 70 * mixer's controls. A change in value indicates that persistent 71 * mixer applications should update their displays. 72 */ 73 int modify_counter; 74 }; 75 76 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = { 77 [SOUND_MIXER_VOLUME] = 75, 78 [SOUND_MIXER_BASS] = 50, 79 [SOUND_MIXER_TREBLE] = 50, 80 [SOUND_MIXER_SYNTH] = 75, 81 [SOUND_MIXER_PCM] = 75, 82 [SOUND_MIXER_SPEAKER] = 75, 83 [SOUND_MIXER_LINE] = 75, 84 [SOUND_MIXER_MIC] = 25, 85 [SOUND_MIXER_CD] = 75, 86 [SOUND_MIXER_IGAIN] = 0, 87 [SOUND_MIXER_LINE1] = 75, 88 [SOUND_MIXER_VIDEO] = 75, 89 [SOUND_MIXER_RECLEV] = 75, 90 [SOUND_MIXER_OGAIN] = 50, 91 [SOUND_MIXER_MONITOR] = 75, 92 }; 93 94 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES; 95 96 static d_open_t mixer_open; 97 static d_close_t mixer_close; 98 static d_ioctl_t mixer_ioctl; 99 100 static struct cdevsw mixer_cdevsw = { 101 .d_version = D_VERSION, 102 .d_open = mixer_open, 103 .d_close = mixer_close, 104 .d_ioctl = mixer_ioctl, 105 .d_name = "mixer", 106 }; 107 108 /** 109 * Keeps a count of mixer devices; used only by OSSv4 SNDCTL_SYSINFO ioctl. 110 */ 111 int mixer_count = 0; 112 113 static eventhandler_tag mixer_ehtag = NULL; 114 115 static struct cdev * 116 mixer_get_devt(device_t dev) 117 { 118 struct snddev_info *snddev; 119 120 snddev = device_get_softc(dev); 121 122 return snddev->mixer_dev; 123 } 124 125 static int 126 mixer_lookup(char *devname) 127 { 128 int i; 129 130 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 131 if (strncmp(devname, snd_mixernames[i], 132 strlen(snd_mixernames[i])) == 0) 133 return i; 134 return -1; 135 } 136 137 #define MIXER_SET_UNLOCK(x, y) do { \ 138 if ((y) != 0) \ 139 snd_mtxunlock((x)->lock); \ 140 } while (0) 141 142 #define MIXER_SET_LOCK(x, y) do { \ 143 if ((y) != 0) \ 144 snd_mtxlock((x)->lock); \ 145 } while (0) 146 147 static int 148 mixer_set_softpcmvol(struct snd_mixer *m, struct snddev_info *d, 149 u_int left, u_int right) 150 { 151 struct pcm_channel *c; 152 int dropmtx, acquiremtx; 153 154 if (!PCM_REGISTERED(d) || PCM_DETACHING(d)) 155 return (EINVAL); 156 157 if (mtx_owned(m->lock)) 158 dropmtx = 1; 159 else 160 dropmtx = 0; 161 162 if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 0) 163 acquiremtx = 0; 164 else 165 acquiremtx = 1; 166 167 /* 168 * Be careful here. If we're coming from cdev ioctl, it is OK to 169 * not doing locking AT ALL (except on individual channel) since 170 * we've been heavily guarded by pcm cv, or if we're still 171 * under Giant influence. Since we also have mix_* calls, we cannot 172 * assume such protection and just do the lock as usuall. 173 */ 174 MIXER_SET_UNLOCK(m, dropmtx); 175 MIXER_SET_LOCK(d, acquiremtx); 176 177 CHN_FOREACH(c, d, channels.pcm.busy) { 178 CHN_LOCK(c); 179 if (c->direction == PCMDIR_PLAY && 180 (c->feederflags & (1 << FEEDER_VOLUME))) 181 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, 182 (left + right) >> 1); 183 CHN_UNLOCK(c); 184 } 185 186 MIXER_SET_UNLOCK(d, acquiremtx); 187 MIXER_SET_LOCK(m, dropmtx); 188 189 return (0); 190 } 191 192 static int 193 mixer_set_eq(struct snd_mixer *m, struct snddev_info *d, 194 u_int dev, u_int level) 195 { 196 struct pcm_channel *c; 197 struct pcm_feeder *f; 198 int tone, dropmtx, acquiremtx; 199 200 if (dev == SOUND_MIXER_TREBLE) 201 tone = FEEDEQ_TREBLE; 202 else if (dev == SOUND_MIXER_BASS) 203 tone = FEEDEQ_BASS; 204 else 205 return (EINVAL); 206 207 if (!PCM_REGISTERED(d) || PCM_DETACHING(d)) 208 return (EINVAL); 209 210 if (mtx_owned(m->lock)) 211 dropmtx = 1; 212 else 213 dropmtx = 0; 214 215 if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 0) 216 acquiremtx = 0; 217 else 218 acquiremtx = 1; 219 220 /* 221 * Be careful here. If we're coming from cdev ioctl, it is OK to 222 * not doing locking AT ALL (except on individual channel) since 223 * we've been heavily guarded by pcm cv, or if we're still 224 * under Giant influence. Since we also have mix_* calls, we cannot 225 * assume such protection and just do the lock as usuall. 226 */ 227 MIXER_SET_UNLOCK(m, dropmtx); 228 MIXER_SET_LOCK(d, acquiremtx); 229 230 CHN_FOREACH(c, d, channels.pcm.busy) { 231 CHN_LOCK(c); 232 f = chn_findfeeder(c, FEEDER_EQ); 233 if (f != NULL) 234 (void)FEEDER_SET(f, tone, level); 235 CHN_UNLOCK(c); 236 } 237 238 MIXER_SET_UNLOCK(d, acquiremtx); 239 MIXER_SET_LOCK(m, dropmtx); 240 241 return (0); 242 } 243 244 static int 245 mixer_set(struct snd_mixer *m, u_int dev, u_int32_t muted, u_int lev) 246 { 247 struct snddev_info *d; 248 u_int l, r, tl, tr; 249 u_int32_t parent = SOUND_MIXER_NONE, child = 0; 250 u_int32_t realdev; 251 int i, dropmtx; 252 253 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES || 254 (0 == (m->devs & (1 << dev)))) 255 return (-1); 256 257 l = min((lev & 0x00ff), 100); 258 r = min(((lev & 0xff00) >> 8), 100); 259 realdev = m->realdev[dev]; 260 261 d = device_get_softc(m->dev); 262 if (d == NULL) 263 return (-1); 264 265 /* It is safe to drop this mutex due to Giant. */ 266 if (!(d->flags & SD_F_MPSAFE) && mtx_owned(m->lock) != 0) 267 dropmtx = 1; 268 else 269 dropmtx = 0; 270 271 /* Allow the volume to be "changed" while muted. */ 272 if (muted & (1 << dev)) { 273 m->level_muted[dev] = l | (r << 8); 274 return (0); 275 } 276 MIXER_SET_UNLOCK(m, dropmtx); 277 278 /* TODO: recursive handling */ 279 parent = m->parent[dev]; 280 if (parent >= SOUND_MIXER_NRDEVICES) 281 parent = SOUND_MIXER_NONE; 282 if (parent == SOUND_MIXER_NONE) 283 child = m->child[dev]; 284 285 if (parent != SOUND_MIXER_NONE) { 286 tl = (l * (m->level[parent] & 0x00ff)) / 100; 287 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100; 288 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL)) 289 (void)mixer_set_softpcmvol(m, d, tl, tr); 290 else if (realdev != SOUND_MIXER_NONE && 291 MIXER_SET(m, realdev, tl, tr) < 0) { 292 MIXER_SET_LOCK(m, dropmtx); 293 return (-1); 294 } 295 } else if (child != 0) { 296 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 297 if (!(child & (1 << i)) || m->parent[i] != dev) 298 continue; 299 realdev = m->realdev[i]; 300 tl = (l * (m->level[i] & 0x00ff)) / 100; 301 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100; 302 if (i == SOUND_MIXER_PCM && 303 (d->flags & SD_F_SOFTPCMVOL)) 304 (void)mixer_set_softpcmvol(m, d, tl, tr); 305 else if (realdev != SOUND_MIXER_NONE) 306 MIXER_SET(m, realdev, tl, tr); 307 } 308 realdev = m->realdev[dev]; 309 if (realdev != SOUND_MIXER_NONE && 310 MIXER_SET(m, realdev, l, r) < 0) { 311 MIXER_SET_LOCK(m, dropmtx); 312 return (-1); 313 } 314 } else { 315 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL)) 316 (void)mixer_set_softpcmvol(m, d, l, r); 317 else if ((dev == SOUND_MIXER_TREBLE || 318 dev == SOUND_MIXER_BASS) && (d->flags & SD_F_EQ)) 319 (void)mixer_set_eq(m, d, dev, (l + r) >> 1); 320 else if (realdev != SOUND_MIXER_NONE && 321 MIXER_SET(m, realdev, l, r) < 0) { 322 MIXER_SET_LOCK(m, dropmtx); 323 return (-1); 324 } 325 } 326 327 MIXER_SET_LOCK(m, dropmtx); 328 329 m->level[dev] = l | (r << 8); 330 m->modify_counter++; 331 332 return (0); 333 } 334 335 static int 336 mixer_get(struct snd_mixer *mixer, int dev) 337 { 338 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev))) { 339 if (mixer->mutedevs & (1 << dev)) 340 return (mixer->level_muted[dev]); 341 else 342 return (mixer->level[dev]); 343 } else { 344 return (-1); 345 } 346 } 347 348 void 349 mix_setmutedevs(struct snd_mixer *mixer, u_int32_t mutedevs) 350 { 351 u_int32_t delta; 352 353 /* Filter out invalid values. */ 354 mutedevs &= mixer->devs; 355 delta = (mixer->mutedevs ^ mutedevs) & mixer->devs; 356 mixer->mutedevs = mutedevs; 357 358 for (int i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 359 if (!(delta & (1 << i))) 360 continue; 361 if (mutedevs & (1 << i)) { 362 mixer->level_muted[i] = mixer->level[i]; 363 mixer_set(mixer, i, 0, 0); 364 } else { 365 mixer_set(mixer, i, 0, mixer->level_muted[i]); 366 } 367 } 368 } 369 370 static int 371 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src) 372 { 373 struct snddev_info *d; 374 u_int32_t recsrc; 375 int dropmtx; 376 377 d = device_get_softc(mixer->dev); 378 if (d == NULL) 379 return -1; 380 if (!(d->flags & SD_F_MPSAFE) && mtx_owned(mixer->lock) != 0) 381 dropmtx = 1; 382 else 383 dropmtx = 0; 384 src &= mixer->recdevs; 385 if (src == 0) 386 src = mixer->recdevs & SOUND_MASK_MIC; 387 if (src == 0) 388 src = mixer->recdevs & SOUND_MASK_MONITOR; 389 if (src == 0) 390 src = mixer->recdevs & SOUND_MASK_LINE; 391 if (src == 0 && mixer->recdevs != 0) 392 src = (1 << (ffs(mixer->recdevs) - 1)); 393 /* It is safe to drop this mutex due to Giant. */ 394 MIXER_SET_UNLOCK(mixer, dropmtx); 395 recsrc = MIXER_SETRECSRC(mixer, src); 396 MIXER_SET_LOCK(mixer, dropmtx); 397 398 mixer->recsrc = recsrc; 399 400 return 0; 401 } 402 403 static int 404 mixer_getrecsrc(struct snd_mixer *mixer) 405 { 406 return mixer->recsrc; 407 } 408 409 /** 410 * @brief Retrieve the route number of the current recording device 411 * 412 * OSSv4 assigns routing numbers to recording devices, unlike the previous 413 * API which relied on a fixed table of device numbers and names. This 414 * function returns the routing number of the device currently selected 415 * for recording. 416 * 417 * For now, this function is kind of a goofy compatibility stub atop the 418 * existing sound system. (For example, in theory, the old sound system 419 * allows multiple recording devices to be specified via a bitmask.) 420 * 421 * @param m mixer context container thing 422 * 423 * @retval 0 success 424 * @retval EIDRM no recording device found (generally not possible) 425 * @todo Ask about error code 426 */ 427 static int 428 mixer_get_recroute(struct snd_mixer *m, int *route) 429 { 430 int i, cnt; 431 432 cnt = 0; 433 434 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 435 /** @todo can user set a multi-device mask? (== or &?) */ 436 if ((1 << i) == m->recsrc) 437 break; 438 if ((1 << i) & m->recdevs) 439 ++cnt; 440 } 441 442 if (i == SOUND_MIXER_NRDEVICES) 443 return EIDRM; 444 445 *route = cnt; 446 return 0; 447 } 448 449 /** 450 * @brief Select a device for recording 451 * 452 * This function sets a recording source based on a recording device's 453 * routing number. Said number is translated to an old school recdev 454 * mask and passed over mixer_setrecsrc. 455 * 456 * @param m mixer context container thing 457 * 458 * @retval 0 success(?) 459 * @retval EINVAL User specified an invalid device number 460 * @retval otherwise error from mixer_setrecsrc 461 */ 462 static int 463 mixer_set_recroute(struct snd_mixer *m, int route) 464 { 465 int i, cnt, ret; 466 467 ret = 0; 468 cnt = 0; 469 470 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 471 if ((1 << i) & m->recdevs) { 472 if (route == cnt) 473 break; 474 ++cnt; 475 } 476 } 477 478 if (i == SOUND_MIXER_NRDEVICES) 479 ret = EINVAL; 480 else 481 ret = mixer_setrecsrc(m, (1 << i)); 482 483 return ret; 484 } 485 486 void 487 mix_setdevs(struct snd_mixer *m, u_int32_t v) 488 { 489 struct snddev_info *d; 490 int i; 491 492 if (m == NULL) 493 return; 494 495 d = device_get_softc(m->dev); 496 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL)) 497 v |= SOUND_MASK_PCM; 498 if (d != NULL && (d->flags & SD_F_EQ)) 499 v |= SOUND_MASK_TREBLE | SOUND_MASK_BASS; 500 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 501 if (m->parent[i] < SOUND_MIXER_NRDEVICES) 502 v |= 1 << m->parent[i]; 503 v |= m->child[i]; 504 } 505 m->devs = v; 506 } 507 508 /** 509 * @brief Record mask of available recording devices 510 * 511 * Calling functions are responsible for defining the mask of available 512 * recording devices. This function records that value in a structure 513 * used by the rest of the mixer code. 514 * 515 * This function also populates a structure used by the SNDCTL_DSP_*RECSRC* 516 * family of ioctls that are part of OSSV4. All recording device labels 517 * are concatenated in ascending order corresponding to their routing 518 * numbers. (Ex: a system might have 0 => 'vol', 1 => 'cd', 2 => 'line', 519 * etc.) For now, these labels are just the standard recording device 520 * names (cd, line1, etc.), but will eventually be fully dynamic and user 521 * controlled. 522 * 523 * @param m mixer device context container thing 524 * @param v mask of recording devices 525 */ 526 void 527 mix_setrecdevs(struct snd_mixer *m, u_int32_t v) 528 { 529 oss_mixer_enuminfo *ei; 530 char *loc; 531 int i, nvalues, nwrote, nleft, ncopied; 532 533 ei = &m->enuminfo; 534 535 nvalues = 0; 536 nwrote = 0; 537 nleft = sizeof(ei->strings); 538 loc = ei->strings; 539 540 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 541 if ((1 << i) & v) { 542 ei->strindex[nvalues] = nwrote; 543 ncopied = strlcpy(loc, snd_mixernames[i], nleft) + 1; 544 /* strlcpy retval doesn't include terminator */ 545 546 nwrote += ncopied; 547 nleft -= ncopied; 548 nvalues++; 549 550 /* 551 * XXX I don't think this should ever be possible. 552 * Even with a move to dynamic device/channel names, 553 * each label is limited to ~16 characters, so that'd 554 * take a LOT to fill this buffer. 555 */ 556 if ((nleft <= 0) || (nvalues >= OSS_ENUM_MAXVALUE)) { 557 device_printf(m->dev, 558 "mix_setrecdevs: Not enough room to store device names--please file a bug report.\n"); 559 device_printf(m->dev, 560 "mix_setrecdevs: Please include details about your sound hardware, OS version, etc.\n"); 561 break; 562 } 563 564 loc = &ei->strings[nwrote]; 565 } 566 } 567 568 /* 569 * NB: The SNDCTL_DSP_GET_RECSRC_NAMES ioctl ignores the dev 570 * and ctrl fields. 571 */ 572 ei->nvalues = nvalues; 573 m->recdevs = v; 574 } 575 576 void 577 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs) 578 { 579 u_int32_t mask = 0; 580 int i; 581 582 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES) 583 return; 584 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 585 if (i == parent) 586 continue; 587 if (childs & (1 << i)) { 588 mask |= 1 << i; 589 if (m->parent[i] < SOUND_MIXER_NRDEVICES) 590 m->child[m->parent[i]] &= ~(1 << i); 591 m->parent[i] = parent; 592 m->child[i] = 0; 593 } 594 } 595 mask &= ~(1 << parent); 596 m->child[parent] = mask; 597 } 598 599 void 600 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev) 601 { 602 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES || 603 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES)) 604 return; 605 m->realdev[dev] = realdev; 606 } 607 608 u_int32_t 609 mix_getparent(struct snd_mixer *m, u_int32_t dev) 610 { 611 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES) 612 return SOUND_MIXER_NONE; 613 return m->parent[dev]; 614 } 615 616 u_int32_t 617 mix_getchild(struct snd_mixer *m, u_int32_t dev) 618 { 619 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES) 620 return 0; 621 return m->child[dev]; 622 } 623 624 u_int32_t 625 mix_getdevs(struct snd_mixer *m) 626 { 627 return m->devs; 628 } 629 630 u_int32_t 631 mix_getmutedevs(struct snd_mixer *m) 632 { 633 return m->mutedevs; 634 } 635 636 u_int32_t 637 mix_getrecdevs(struct snd_mixer *m) 638 { 639 return m->recdevs; 640 } 641 642 void * 643 mix_getdevinfo(struct snd_mixer *m) 644 { 645 return m->devinfo; 646 } 647 648 static struct snd_mixer * 649 mixer_obj_create(device_t dev, kobj_class_t cls, void *devinfo, 650 int type, const char *desc) 651 { 652 struct snd_mixer *m; 653 int i; 654 655 KASSERT(dev != NULL && cls != NULL && devinfo != NULL, 656 ("%s(): NULL data dev=%p cls=%p devinfo=%p", 657 __func__, dev, cls, devinfo)); 658 KASSERT(type == MIXER_TYPE_PRIMARY || type == MIXER_TYPE_SECONDARY, 659 ("invalid mixer type=%d", type)); 660 661 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO); 662 snprintf(m->name, sizeof(m->name), "%s:mixer", 663 device_get_nameunit(dev)); 664 if (desc != NULL) { 665 strlcat(m->name, ":", sizeof(m->name)); 666 strlcat(m->name, desc, sizeof(m->name)); 667 } 668 m->lock = snd_mtxcreate(m->name, (type == MIXER_TYPE_PRIMARY) ? 669 "primary pcm mixer" : "secondary pcm mixer"); 670 m->type = type; 671 m->devinfo = devinfo; 672 m->busy = 0; 673 m->dev = dev; 674 for (i = 0; i < (sizeof(m->parent) / sizeof(m->parent[0])); i++) { 675 m->parent[i] = SOUND_MIXER_NONE; 676 m->child[i] = 0; 677 m->realdev[i] = i; 678 } 679 680 if (MIXER_INIT(m)) { 681 snd_mtxlock(m->lock); 682 snd_mtxfree(m->lock); 683 kobj_delete((kobj_t)m, M_MIXER); 684 return (NULL); 685 } 686 687 return (m); 688 } 689 690 int 691 mixer_delete(struct snd_mixer *m) 692 { 693 KASSERT(m != NULL, ("NULL snd_mixer")); 694 KASSERT(m->type == MIXER_TYPE_SECONDARY, 695 ("%s(): illegal mixer type=%d", __func__, m->type)); 696 697 /* mixer uninit can sleep --hps */ 698 699 MIXER_UNINIT(m); 700 701 snd_mtxfree(m->lock); 702 kobj_delete((kobj_t)m, M_MIXER); 703 704 --mixer_count; 705 706 return (0); 707 } 708 709 struct snd_mixer * 710 mixer_create(device_t dev, kobj_class_t cls, void *devinfo, const char *desc) 711 { 712 struct snd_mixer *m; 713 714 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_SECONDARY, desc); 715 716 if (m != NULL) 717 ++mixer_count; 718 719 return (m); 720 } 721 722 int 723 mixer_init(device_t dev, kobj_class_t cls, void *devinfo) 724 { 725 struct snddev_info *snddev; 726 struct snd_mixer *m; 727 u_int16_t v; 728 struct cdev *pdev; 729 int i, unit, devunit, val; 730 731 snddev = device_get_softc(dev); 732 if (snddev == NULL) 733 return (-1); 734 735 if (resource_int_value(device_get_name(dev), 736 device_get_unit(dev), "eq", &val) == 0 && val != 0) { 737 snddev->flags |= SD_F_EQ; 738 if ((val & SD_F_EQ_MASK) == val) 739 snddev->flags |= val; 740 else 741 snddev->flags |= SD_F_EQ_DEFAULT; 742 snddev->eqpreamp = 0; 743 } 744 745 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_PRIMARY, NULL); 746 if (m == NULL) 747 return (-1); 748 749 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 750 v = snd_mixerdefaults[i]; 751 752 if (resource_int_value(device_get_name(dev), 753 device_get_unit(dev), snd_mixernames[i], &val) == 0) { 754 if (val >= 0 && val <= 100) { 755 v = (u_int16_t) val; 756 } 757 } 758 759 mixer_set(m, i, 0, v | (v << 8)); 760 } 761 762 mixer_setrecsrc(m, 0); /* Set default input. */ 763 764 unit = device_get_unit(dev); 765 devunit = snd_mkunit(unit, SND_DEV_CTL, 0); 766 pdev = make_dev(&mixer_cdevsw, PCMMINOR(devunit), 767 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit); 768 pdev->si_drv1 = m; 769 snddev->mixer_dev = pdev; 770 771 ++mixer_count; 772 773 if (bootverbose) { 774 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 775 if (!(m->devs & (1 << i))) 776 continue; 777 if (m->realdev[i] != i) { 778 device_printf(dev, "Mixer \"%s\" -> \"%s\":", 779 snd_mixernames[i], 780 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ? 781 snd_mixernames[m->realdev[i]] : "none"); 782 } else { 783 device_printf(dev, "Mixer \"%s\":", 784 snd_mixernames[i]); 785 } 786 if (m->parent[i] < SOUND_MIXER_NRDEVICES) 787 printf(" parent=\"%s\"", 788 snd_mixernames[m->parent[i]]); 789 if (m->child[i] != 0) 790 printf(" child=0x%08x", m->child[i]); 791 printf("\n"); 792 } 793 if (snddev->flags & SD_F_SOFTPCMVOL) 794 device_printf(dev, "Soft PCM mixer ENABLED\n"); 795 if (snddev->flags & SD_F_EQ) 796 device_printf(dev, "EQ Treble/Bass ENABLED\n"); 797 } 798 799 return (0); 800 } 801 802 int 803 mixer_uninit(device_t dev) 804 { 805 int i; 806 struct snddev_info *d; 807 struct snd_mixer *m; 808 struct cdev *pdev; 809 810 d = device_get_softc(dev); 811 pdev = mixer_get_devt(dev); 812 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL) 813 return EBADF; 814 815 m = pdev->si_drv1; 816 KASSERT(m != NULL, ("NULL snd_mixer")); 817 KASSERT(m->type == MIXER_TYPE_PRIMARY, 818 ("%s(): illegal mixer type=%d", __func__, m->type)); 819 820 pdev->si_drv1 = NULL; 821 destroy_dev(pdev); 822 823 snd_mtxlock(m->lock); 824 825 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 826 mixer_set(m, i, 0, 0); 827 828 mixer_setrecsrc(m, SOUND_MASK_MIC); 829 830 snd_mtxunlock(m->lock); 831 832 /* mixer uninit can sleep --hps */ 833 834 MIXER_UNINIT(m); 835 836 snd_mtxfree(m->lock); 837 kobj_delete((kobj_t)m, M_MIXER); 838 839 d->mixer_dev = NULL; 840 841 --mixer_count; 842 843 return 0; 844 } 845 846 int 847 mixer_reinit(device_t dev) 848 { 849 struct snd_mixer *m; 850 struct cdev *pdev; 851 int i; 852 853 pdev = mixer_get_devt(dev); 854 m = pdev->si_drv1; 855 snd_mtxlock(m->lock); 856 857 i = MIXER_REINIT(m); 858 if (i) { 859 snd_mtxunlock(m->lock); 860 return i; 861 } 862 863 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 864 if (m->mutedevs & (1 << i)) 865 mixer_set(m, i, 0, 0); 866 else 867 mixer_set(m, i, 0, m->level[i]); 868 } 869 870 mixer_setrecsrc(m, m->recsrc); 871 snd_mtxunlock(m->lock); 872 873 return 0; 874 } 875 876 static int 877 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS) 878 { 879 char devname[32]; 880 int error, dev; 881 struct snd_mixer *m; 882 883 m = oidp->oid_arg1; 884 snd_mtxlock(m->lock); 885 strlcpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname)); 886 snd_mtxunlock(m->lock); 887 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req); 888 snd_mtxlock(m->lock); 889 if (error == 0 && req->newptr != NULL) { 890 dev = mixer_lookup(devname); 891 if (dev == -1) { 892 snd_mtxunlock(m->lock); 893 return EINVAL; 894 } else { 895 m->hwvol_mixer = dev; 896 } 897 } 898 snd_mtxunlock(m->lock); 899 return error; 900 } 901 902 int 903 mixer_hwvol_init(device_t dev) 904 { 905 struct snd_mixer *m; 906 struct cdev *pdev; 907 908 pdev = mixer_get_devt(dev); 909 m = pdev->si_drv1; 910 911 m->hwvol_mixer = SOUND_MIXER_VOLUME; 912 m->hwvol_step = 5; 913 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 914 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 915 OID_AUTO, "hwvol_step", CTLFLAG_RWTUN, &m->hwvol_step, 0, ""); 916 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 917 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 918 "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 919 m, 0, sysctl_hw_snd_hwvol_mixer, "A", ""); 920 return 0; 921 } 922 923 void 924 mixer_hwvol_mute_locked(struct snd_mixer *m) 925 { 926 mix_setmutedevs(m, m->mutedevs ^ (1 << m->hwvol_mixer)); 927 } 928 929 void 930 mixer_hwvol_mute(device_t dev) 931 { 932 struct snd_mixer *m; 933 struct cdev *pdev; 934 935 pdev = mixer_get_devt(dev); 936 m = pdev->si_drv1; 937 snd_mtxlock(m->lock); 938 mixer_hwvol_mute_locked(m); 939 snd_mtxunlock(m->lock); 940 } 941 942 void 943 mixer_hwvol_step_locked(struct snd_mixer *m, int left_step, int right_step) 944 { 945 int level, left, right; 946 947 level = mixer_get(m, m->hwvol_mixer); 948 949 if (level != -1) { 950 left = level & 0xff; 951 right = (level >> 8) & 0xff; 952 left += left_step * m->hwvol_step; 953 if (left < 0) 954 left = 0; 955 else if (left > 100) 956 left = 100; 957 right += right_step * m->hwvol_step; 958 if (right < 0) 959 right = 0; 960 else if (right > 100) 961 right = 100; 962 963 mixer_set(m, m->hwvol_mixer, m->mutedevs, left | right << 8); 964 } 965 } 966 967 void 968 mixer_hwvol_step(device_t dev, int left_step, int right_step) 969 { 970 struct snd_mixer *m; 971 struct cdev *pdev; 972 973 pdev = mixer_get_devt(dev); 974 m = pdev->si_drv1; 975 snd_mtxlock(m->lock); 976 mixer_hwvol_step_locked(m, left_step, right_step); 977 snd_mtxunlock(m->lock); 978 } 979 980 int 981 mixer_busy(struct snd_mixer *m) 982 { 983 KASSERT(m != NULL, ("NULL snd_mixer")); 984 985 return (m->busy); 986 } 987 988 int 989 mix_set(struct snd_mixer *m, u_int dev, u_int left, u_int right) 990 { 991 int ret; 992 993 KASSERT(m != NULL, ("NULL snd_mixer")); 994 995 snd_mtxlock(m->lock); 996 ret = mixer_set(m, dev, m->mutedevs, left | (right << 8)); 997 snd_mtxunlock(m->lock); 998 999 return ((ret != 0) ? ENXIO : 0); 1000 } 1001 1002 int 1003 mix_get(struct snd_mixer *m, u_int dev) 1004 { 1005 int ret; 1006 1007 KASSERT(m != NULL, ("NULL snd_mixer")); 1008 1009 snd_mtxlock(m->lock); 1010 ret = mixer_get(m, dev); 1011 snd_mtxunlock(m->lock); 1012 1013 return (ret); 1014 } 1015 1016 int 1017 mix_setrecsrc(struct snd_mixer *m, u_int32_t src) 1018 { 1019 int ret; 1020 1021 KASSERT(m != NULL, ("NULL snd_mixer")); 1022 1023 snd_mtxlock(m->lock); 1024 ret = mixer_setrecsrc(m, src); 1025 snd_mtxunlock(m->lock); 1026 1027 return ((ret != 0) ? ENXIO : 0); 1028 } 1029 1030 u_int32_t 1031 mix_getrecsrc(struct snd_mixer *m) 1032 { 1033 u_int32_t ret; 1034 1035 KASSERT(m != NULL, ("NULL snd_mixer")); 1036 1037 snd_mtxlock(m->lock); 1038 ret = mixer_getrecsrc(m); 1039 snd_mtxunlock(m->lock); 1040 1041 return (ret); 1042 } 1043 1044 int 1045 mix_get_type(struct snd_mixer *m) 1046 { 1047 KASSERT(m != NULL, ("NULL snd_mixer")); 1048 1049 return (m->type); 1050 } 1051 1052 device_t 1053 mix_get_dev(struct snd_mixer *m) 1054 { 1055 KASSERT(m != NULL, ("NULL snd_mixer")); 1056 1057 return (m->dev); 1058 } 1059 1060 /* ----------------------------------------------------------------------- */ 1061 1062 static int 1063 mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td) 1064 { 1065 struct snddev_info *d; 1066 struct snd_mixer *m; 1067 1068 if (i_dev == NULL || i_dev->si_drv1 == NULL) 1069 return (EBADF); 1070 1071 m = i_dev->si_drv1; 1072 d = device_get_softc(m->dev); 1073 if (!PCM_REGISTERED(d) || PCM_DETACHING(d)) 1074 return (EBADF); 1075 1076 /* XXX Need Giant magic entry ??? */ 1077 1078 snd_mtxlock(m->lock); 1079 m->busy = 1; 1080 snd_mtxunlock(m->lock); 1081 1082 return (0); 1083 } 1084 1085 static int 1086 mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td) 1087 { 1088 struct snddev_info *d; 1089 struct snd_mixer *m; 1090 int ret; 1091 1092 if (i_dev == NULL || i_dev->si_drv1 == NULL) 1093 return (EBADF); 1094 1095 m = i_dev->si_drv1; 1096 d = device_get_softc(m->dev); 1097 if (!PCM_REGISTERED(d)) 1098 return (EBADF); 1099 1100 /* XXX Need Giant magic entry ??? */ 1101 1102 snd_mtxlock(m->lock); 1103 ret = (m->busy == 0) ? EBADF : 0; 1104 m->busy = 0; 1105 snd_mtxunlock(m->lock); 1106 1107 return (ret); 1108 } 1109 1110 static int 1111 mixer_ioctl_channel(struct cdev *dev, u_long cmd, caddr_t arg, int mode, 1112 struct thread *td, int from) 1113 { 1114 struct snddev_info *d; 1115 struct snd_mixer *m; 1116 struct pcm_channel *c, *rdch, *wrch; 1117 pid_t pid; 1118 int j, ret; 1119 1120 if (td == NULL || td->td_proc == NULL) 1121 return (-1); 1122 1123 m = dev->si_drv1; 1124 d = device_get_softc(m->dev); 1125 j = cmd & 0xff; 1126 1127 switch (j) { 1128 case SOUND_MIXER_PCM: 1129 case SOUND_MIXER_RECLEV: 1130 case SOUND_MIXER_DEVMASK: 1131 case SOUND_MIXER_CAPS: 1132 case SOUND_MIXER_STEREODEVS: 1133 break; 1134 default: 1135 return (-1); 1136 break; 1137 } 1138 1139 pid = td->td_proc->p_pid; 1140 rdch = NULL; 1141 wrch = NULL; 1142 c = NULL; 1143 ret = -1; 1144 1145 /* 1146 * This is unfair. Imagine single proc opening multiple 1147 * instances of same direction. What we do right now 1148 * is looking for the first matching proc/pid, and just 1149 * that. Nothing more. Consider it done. 1150 * 1151 * The better approach of controlling specific channel 1152 * pcm or rec volume is by doing mixer ioctl 1153 * (SNDCTL_DSP_[SET|GET][PLAY|REC]VOL / SOUND_MIXER_[PCM|RECLEV] 1154 * on its open fd, rather than cracky mixer bypassing here. 1155 */ 1156 CHN_FOREACH(c, d, channels.pcm.opened) { 1157 CHN_LOCK(c); 1158 if (c->pid != pid || 1159 !(c->feederflags & (1 << FEEDER_VOLUME))) { 1160 CHN_UNLOCK(c); 1161 continue; 1162 } 1163 if (rdch == NULL && c->direction == PCMDIR_REC) { 1164 rdch = c; 1165 if (j == SOUND_MIXER_RECLEV) 1166 goto mixer_ioctl_channel_proc; 1167 } else if (wrch == NULL && c->direction == PCMDIR_PLAY) { 1168 wrch = c; 1169 if (j == SOUND_MIXER_PCM) 1170 goto mixer_ioctl_channel_proc; 1171 } 1172 CHN_UNLOCK(c); 1173 if (rdch != NULL && wrch != NULL) 1174 break; 1175 } 1176 1177 if (rdch == NULL && wrch == NULL) 1178 return (-1); 1179 1180 if ((j == SOUND_MIXER_DEVMASK || j == SOUND_MIXER_CAPS || 1181 j == SOUND_MIXER_STEREODEVS) && 1182 (cmd & ~0xff) == MIXER_READ(0)) { 1183 snd_mtxlock(m->lock); 1184 *(int *)arg = mix_getdevs(m); 1185 snd_mtxunlock(m->lock); 1186 if (rdch != NULL) 1187 *(int *)arg |= SOUND_MASK_RECLEV; 1188 if (wrch != NULL) 1189 *(int *)arg |= SOUND_MASK_PCM; 1190 ret = 0; 1191 } 1192 1193 return (ret); 1194 1195 mixer_ioctl_channel_proc: 1196 1197 KASSERT(c != NULL, ("%s(): NULL channel", __func__)); 1198 CHN_LOCKASSERT(c); 1199 1200 if ((cmd & ~0xff) == MIXER_WRITE(0)) { 1201 int left, right, center; 1202 1203 left = *(int *)arg & 0x7f; 1204 right = (*(int *)arg >> 8) & 0x7f; 1205 center = (left + right) >> 1; 1206 chn_setvolume_multi(c, SND_VOL_C_PCM, left, right, center); 1207 } else if ((cmd & ~0xff) == MIXER_READ(0)) { 1208 *(int *)arg = CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FL); 1209 *(int *)arg |= 1210 CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FR) << 8; 1211 } 1212 1213 CHN_UNLOCK(c); 1214 1215 return (0); 1216 } 1217 1218 static int 1219 mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, 1220 struct thread *td) 1221 { 1222 struct snddev_info *d; 1223 int ret; 1224 1225 if (i_dev == NULL || i_dev->si_drv1 == NULL) 1226 return (EBADF); 1227 1228 d = device_get_softc(((struct snd_mixer *)i_dev->si_drv1)->dev); 1229 if (!PCM_REGISTERED(d) || PCM_DETACHING(d)) 1230 return (EBADF); 1231 1232 PCM_GIANT_ENTER(d); 1233 PCM_ACQUIRE_QUICK(d); 1234 1235 ret = -1; 1236 1237 if (mixer_bypass != 0 && (d->flags & SD_F_VPC)) 1238 ret = mixer_ioctl_channel(i_dev, cmd, arg, mode, td, 1239 MIXER_CMD_CDEV); 1240 1241 if (ret == -1) 1242 ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td, 1243 MIXER_CMD_CDEV); 1244 1245 PCM_RELEASE_QUICK(d); 1246 PCM_GIANT_LEAVE(d); 1247 1248 return (ret); 1249 } 1250 1251 static void 1252 mixer_mixerinfo(struct snd_mixer *m, mixer_info *mi) 1253 { 1254 bzero((void *)mi, sizeof(*mi)); 1255 strlcpy(mi->id, m->name, sizeof(mi->id)); 1256 strlcpy(mi->name, device_get_desc(m->dev), sizeof(mi->name)); 1257 mi->modify_counter = m->modify_counter; 1258 } 1259 1260 /* 1261 * XXX Make sure you can guarantee concurrency safety before calling this 1262 * function, be it through Giant, PCM_*, etc ! 1263 */ 1264 int 1265 mixer_ioctl_cmd(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, 1266 struct thread *td, int from) 1267 { 1268 struct snd_mixer *m; 1269 int ret = EINVAL, *arg_i = (int *)arg; 1270 int v = -1, j = cmd & 0xff; 1271 1272 /* 1273 * Certain ioctls may be made on any type of device (audio, mixer, 1274 * and MIDI). Handle those special cases here. 1275 */ 1276 if (IOCGROUP(cmd) == 'X') { 1277 switch (cmd) { 1278 case SNDCTL_SYSINFO: 1279 sound_oss_sysinfo((oss_sysinfo *)arg); 1280 return (0); 1281 case SNDCTL_CARDINFO: 1282 return (sound_oss_card_info((oss_card_info *)arg)); 1283 case SNDCTL_AUDIOINFO: 1284 case SNDCTL_AUDIOINFO_EX: 1285 case SNDCTL_ENGINEINFO: 1286 return (dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg)); 1287 case SNDCTL_MIXERINFO: 1288 return (mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg)); 1289 } 1290 return (EINVAL); 1291 } 1292 1293 m = i_dev->si_drv1; 1294 1295 if (m == NULL) 1296 return (EBADF); 1297 1298 snd_mtxlock(m->lock); 1299 if (from == MIXER_CMD_CDEV && !m->busy) { 1300 snd_mtxunlock(m->lock); 1301 return (EBADF); 1302 } 1303 switch (cmd) { 1304 case SNDCTL_DSP_GET_RECSRC_NAMES: 1305 bcopy((void *)&m->enuminfo, arg, sizeof(oss_mixer_enuminfo)); 1306 ret = 0; 1307 goto done; 1308 case SNDCTL_DSP_GET_RECSRC: 1309 ret = mixer_get_recroute(m, arg_i); 1310 goto done; 1311 case SNDCTL_DSP_SET_RECSRC: 1312 ret = mixer_set_recroute(m, *arg_i); 1313 goto done; 1314 case OSS_GETVERSION: 1315 *arg_i = SOUND_VERSION; 1316 ret = 0; 1317 goto done; 1318 case SOUND_MIXER_INFO: 1319 mixer_mixerinfo(m, (mixer_info *)arg); 1320 ret = 0; 1321 goto done; 1322 } 1323 if ((cmd & ~0xff) == MIXER_WRITE(0)) { 1324 switch (j) { 1325 case SOUND_MIXER_RECSRC: 1326 ret = mixer_setrecsrc(m, *arg_i); 1327 break; 1328 case SOUND_MIXER_MUTE: 1329 mix_setmutedevs(m, *arg_i); 1330 ret = 0; 1331 break; 1332 default: 1333 ret = mixer_set(m, j, m->mutedevs, *arg_i); 1334 break; 1335 } 1336 snd_mtxunlock(m->lock); 1337 return ((ret == 0) ? 0 : ENXIO); 1338 } 1339 if ((cmd & ~0xff) == MIXER_READ(0)) { 1340 switch (j) { 1341 case SOUND_MIXER_DEVMASK: 1342 case SOUND_MIXER_CAPS: 1343 case SOUND_MIXER_STEREODEVS: 1344 v = mix_getdevs(m); 1345 break; 1346 case SOUND_MIXER_MUTE: 1347 v = mix_getmutedevs(m); 1348 break; 1349 case SOUND_MIXER_RECMASK: 1350 v = mix_getrecdevs(m); 1351 break; 1352 case SOUND_MIXER_RECSRC: 1353 v = mixer_getrecsrc(m); 1354 break; 1355 default: 1356 v = mixer_get(m, j); 1357 break; 1358 } 1359 *arg_i = v; 1360 snd_mtxunlock(m->lock); 1361 return ((v != -1) ? 0 : ENXIO); 1362 } 1363 done: 1364 snd_mtxunlock(m->lock); 1365 return (ret); 1366 } 1367 1368 static void 1369 mixer_clone(void *arg, 1370 struct ucred *cred, 1371 char *name, int namelen, struct cdev **dev) 1372 { 1373 struct snddev_info *d; 1374 1375 if (*dev != NULL) 1376 return; 1377 if (strcmp(name, "mixer") == 0) { 1378 d = devclass_get_softc(pcm_devclass, snd_unit); 1379 if (PCM_REGISTERED(d) && d->mixer_dev != NULL) { 1380 *dev = d->mixer_dev; 1381 dev_ref(*dev); 1382 } 1383 } 1384 } 1385 1386 static void 1387 mixer_sysinit(void *p) 1388 { 1389 if (mixer_ehtag != NULL) 1390 return; 1391 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000); 1392 } 1393 1394 static void 1395 mixer_sysuninit(void *p) 1396 { 1397 if (mixer_ehtag == NULL) 1398 return; 1399 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag); 1400 mixer_ehtag = NULL; 1401 } 1402 1403 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL); 1404 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL); 1405 1406 /** 1407 * @brief Handler for SNDCTL_MIXERINFO 1408 * 1409 * This function searches for a mixer based on the numeric ID stored 1410 * in oss_miserinfo::dev. If set to -1, then information about the 1411 * current mixer handling the request is provided. Note, however, that 1412 * this ioctl may be made with any sound device (audio, mixer, midi). 1413 * 1414 * @note Caller must not hold any PCM device, channel, or mixer locks. 1415 * 1416 * See http://manuals.opensound.com/developer/SNDCTL_MIXERINFO.html for 1417 * more information. 1418 * 1419 * @param i_dev character device on which the ioctl arrived 1420 * @param arg user argument (oss_mixerinfo *) 1421 * 1422 * @retval EINVAL oss_mixerinfo::dev specified a bad value 1423 * @retval 0 success 1424 */ 1425 int 1426 mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi) 1427 { 1428 struct snddev_info *d; 1429 struct snd_mixer *m; 1430 int nmix, i; 1431 1432 /* 1433 * If probing the device handling the ioctl, make sure it's a mixer 1434 * device. (This ioctl is valid on audio, mixer, and midi devices.) 1435 */ 1436 if (mi->dev == -1 && i_dev->si_devsw != &mixer_cdevsw) 1437 return (EINVAL); 1438 1439 d = NULL; 1440 m = NULL; 1441 nmix = 0; 1442 1443 /* 1444 * There's a 1:1 relationship between mixers and PCM devices, so 1445 * begin by iterating over PCM devices and search for our mixer. 1446 */ 1447 for (i = 0; pcm_devclass != NULL && 1448 i < devclass_get_maxunit(pcm_devclass); i++) { 1449 d = devclass_get_softc(pcm_devclass, i); 1450 if (!PCM_REGISTERED(d) || PCM_DETACHING(d)) 1451 continue; 1452 1453 /* XXX Need Giant magic entry */ 1454 1455 /* See the note in function docblock. */ 1456 PCM_UNLOCKASSERT(d); 1457 PCM_LOCK(d); 1458 1459 if (d->mixer_dev != NULL && d->mixer_dev->si_drv1 != NULL && 1460 ((mi->dev == -1 && d->mixer_dev == i_dev) || 1461 mi->dev == nmix)) { 1462 m = d->mixer_dev->si_drv1; 1463 mtx_lock(m->lock); 1464 1465 /* 1466 * At this point, the following synchronization stuff 1467 * has happened: 1468 * - a specific PCM device is locked. 1469 * - a specific mixer device has been locked, so be 1470 * sure to unlock when existing. 1471 */ 1472 bzero((void *)mi, sizeof(*mi)); 1473 mi->dev = nmix; 1474 snprintf(mi->id, sizeof(mi->id), "mixer%d", i); 1475 strlcpy(mi->name, m->name, sizeof(mi->name)); 1476 mi->modify_counter = m->modify_counter; 1477 mi->card_number = i; 1478 /* 1479 * Currently, FreeBSD assumes 1:1 relationship between 1480 * a pcm and mixer devices, so this is hardcoded to 0. 1481 */ 1482 mi->port_number = 0; 1483 1484 /** 1485 * @todo Fill in @sa oss_mixerinfo::mixerhandle. 1486 * @note From 4Front: "mixerhandle is an arbitrary 1487 * string that identifies the mixer better than 1488 * the device number (mixerinfo.dev). Device 1489 * numbers may change depending on the order the 1490 * drivers are loaded. However the handle should 1491 * remain the same provided that the sound card 1492 * is not moved to another PCI slot." 1493 */ 1494 1495 /** 1496 * @note 1497 * @sa oss_mixerinfo::magic is a reserved field. 1498 * 1499 * @par 1500 * From 4Front: "magic is usually 0. However some 1501 * devices may have dedicated setup utilities and the 1502 * magic field may contain an unique driver specific 1503 * value (managed by [4Front])." 1504 */ 1505 1506 mi->enabled = device_is_attached(m->dev) ? 1 : 0; 1507 /** 1508 * The only flag for @sa oss_mixerinfo::caps is 1509 * currently MIXER_CAP_VIRTUAL, which I'm not sure we 1510 * really worry about. 1511 */ 1512 /** 1513 * Mixer extensions currently aren't supported, so 1514 * leave @sa oss_mixerinfo::nrext blank for now. 1515 */ 1516 /** 1517 * @todo Fill in @sa oss_mixerinfo::priority (requires 1518 * touching drivers?) 1519 * @note The priority field is for mixer applets to 1520 * determine which mixer should be the default, with 0 1521 * being least preferred and 10 being most preferred. 1522 * From 4Front: "OSS drivers like ICH use higher 1523 * values (10) because such chips are known to be used 1524 * only on motherboards. Drivers for high end pro 1525 * devices use 0 because they will never be the 1526 * default mixer. Other devices use values 1 to 9 1527 * depending on the estimated probability of being the 1528 * default device. 1529 * 1530 * XXX Described by Hannu@4Front, but not found in 1531 * soundcard.h. 1532 strlcpy(mi->devnode, devtoname(d->mixer_dev), 1533 sizeof(mi->devnode)); 1534 mi->legacy_device = i; 1535 */ 1536 mtx_unlock(m->lock); 1537 } else 1538 ++nmix; 1539 1540 PCM_UNLOCK(d); 1541 1542 if (m != NULL) 1543 return (0); 1544 } 1545 1546 return (EINVAL); 1547 } 1548 1549 /* 1550 * Allow the sound driver to use the mixer lock to protect its mixer 1551 * data: 1552 */ 1553 struct mtx * 1554 mixer_get_lock(struct snd_mixer *m) 1555 { 1556 if (m->lock == NULL) { 1557 return (&Giant); 1558 } 1559 return (m->lock); 1560 } 1561 1562 int 1563 mix_get_locked(struct snd_mixer *m, u_int dev, int *pleft, int *pright) 1564 { 1565 int level; 1566 1567 level = mixer_get(m, dev); 1568 if (level < 0) { 1569 *pright = *pleft = -1; 1570 return (-1); 1571 } 1572 1573 *pleft = level & 0xFF; 1574 *pright = (level >> 8) & 0xFF; 1575 1576 return (0); 1577 } 1578 1579 int 1580 mix_set_locked(struct snd_mixer *m, u_int dev, int left, int right) 1581 { 1582 int level; 1583 1584 level = (left & 0xFF) | ((right & 0xFF) << 8); 1585 1586 return (mixer_set(m, dev, m->mutedevs, level)); 1587 } 1588