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 29#include "mixer_if.h" 30
| 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 29#include "mixer_if.h" 30
|
32 33MALLOC_DEFINE(M_MIXER, "mixer", "mixer"); 34 35#define MIXER_NAMELEN 16 36struct snd_mixer { 37 KOBJ_FIELDS; 38 const char *type; 39 void *devinfo; 40 int busy; 41 int hwvol_muted; 42 int hwvol_mixer; 43 int hwvol_step; 44 u_int32_t hwvol_mute_level; 45 u_int32_t devs; 46 u_int32_t recdevs; 47 u_int32_t recsrc; 48 u_int16_t level[32]; 49 char name[MIXER_NAMELEN]; 50 struct mtx *lock; 51}; 52 53static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = { 54 [SOUND_MIXER_VOLUME] = 75, 55 [SOUND_MIXER_BASS] = 50, 56 [SOUND_MIXER_TREBLE] = 50, 57 [SOUND_MIXER_SYNTH] = 75, 58 [SOUND_MIXER_PCM] = 75, 59 [SOUND_MIXER_SPEAKER] = 75, 60 [SOUND_MIXER_LINE] = 75, 61 [SOUND_MIXER_MIC] = 0, 62 [SOUND_MIXER_CD] = 75, 63 [SOUND_MIXER_LINE1] = 75, 64 [SOUND_MIXER_VIDEO] = 75, 65 [SOUND_MIXER_RECLEV] = 0, 66 [SOUND_MIXER_OGAIN] = 50, 67 [SOUND_MIXER_MONITOR] = 75, 68}; 69 70static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES; 71 72static d_open_t mixer_open; 73static d_close_t mixer_close; 74 75static struct cdevsw mixer_cdevsw = { 76 .d_version = D_VERSION, 77 .d_flags = D_NEEDGIANT, 78 .d_open = mixer_open, 79 .d_close = mixer_close, 80 .d_ioctl = mixer_ioctl, 81 .d_name = "mixer", 82 .d_maj = SND_CDEV_MAJOR, 83}; 84 85#ifdef USING_DEVFS 86static eventhandler_tag mixer_ehtag; 87#endif 88 89static struct cdev * 90mixer_get_devt(device_t dev) 91{ 92 struct snddev_info *snddev; 93 94 snddev = device_get_softc(dev); 95 96 return snddev->mixer_dev; 97} 98 99#ifdef SND_DYNSYSCTL 100static int 101mixer_lookup(char *devname) 102{ 103 int i; 104 105 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 106 if (strncmp(devname, snd_mixernames[i], 107 strlen(snd_mixernames[i])) == 0) 108 return i; 109 return -1; 110} 111#endif 112 113static int 114mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev) 115{ 116 unsigned l, r; 117 int v; 118 119 if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev)))) 120 return -1; 121 122 l = min((lev & 0x00ff), 100); 123 r = min(((lev & 0xff00) >> 8), 100); 124 125 v = MIXER_SET(mixer, dev, l, r); 126 if (v < 0) 127 return -1; 128 129 mixer->level[dev] = l | (r << 8); 130 return 0; 131} 132 133static int 134mixer_get(struct snd_mixer *mixer, int dev) 135{ 136 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev))) 137 return mixer->level[dev]; 138 else return -1; 139} 140 141static int 142mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src) 143{ 144 src &= mixer->recdevs; 145 if (src == 0) 146 src = SOUND_MASK_MIC; 147 mixer->recsrc = MIXER_SETRECSRC(mixer, src); 148 return 0; 149} 150 151static int 152mixer_getrecsrc(struct snd_mixer *mixer) 153{ 154 return mixer->recsrc; 155} 156 157void 158mix_setdevs(struct snd_mixer *m, u_int32_t v) 159{ 160 m->devs = v; 161} 162 163void 164mix_setrecdevs(struct snd_mixer *m, u_int32_t v) 165{ 166 m->recdevs = v; 167} 168 169u_int32_t 170mix_getdevs(struct snd_mixer *m) 171{ 172 return m->devs; 173} 174 175u_int32_t 176mix_getrecdevs(struct snd_mixer *m) 177{ 178 return m->recdevs; 179} 180 181void * 182mix_getdevinfo(struct snd_mixer *m) 183{ 184 return m->devinfo; 185} 186 187int 188mixer_init(device_t dev, kobj_class_t cls, void *devinfo) 189{ 190 struct snddev_info *snddev; 191 struct snd_mixer *m; 192 u_int16_t v; 193 struct cdev *pdev; 194 int i, unit; 195 196 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO); 197 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev)); 198 m->lock = snd_mtxcreate(m->name, "pcm mixer"); 199 m->type = cls->name; 200 m->devinfo = devinfo; 201 m->busy = 0; 202 203 if (MIXER_INIT(m)) 204 goto bad; 205 206 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 207 v = snd_mixerdefaults[i]; 208 mixer_set(m, i, v | (v << 8)); 209 } 210 211 mixer_setrecsrc(m, SOUND_MASK_MIC); 212 213 unit = device_get_unit(dev); 214 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0), 215 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit); 216 pdev->si_drv1 = m; 217 snddev = device_get_softc(dev); 218 snddev->mixer_dev = pdev; 219 220 return 0; 221 222bad: 223 snd_mtxlock(m->lock); 224 snd_mtxfree(m->lock); 225 kobj_delete((kobj_t)m, M_MIXER); 226 return -1; 227} 228 229int 230mixer_uninit(device_t dev) 231{ 232 int i; 233 struct snd_mixer *m; 234 struct cdev *pdev; 235 236 pdev = mixer_get_devt(dev); 237 m = pdev->si_drv1; 238 snd_mtxlock(m->lock); 239 240 if (m->busy) { 241 snd_mtxunlock(m->lock); 242 return EBUSY; 243 } 244 245 pdev->si_drv1 = NULL; 246 destroy_dev(pdev); 247 248 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 249 mixer_set(m, i, 0); 250 251 mixer_setrecsrc(m, SOUND_MASK_MIC); 252 253 MIXER_UNINIT(m); 254 255 snd_mtxfree(m->lock); 256 kobj_delete((kobj_t)m, M_MIXER); 257 258 return 0; 259} 260 261int 262mixer_reinit(device_t dev) 263{ 264 struct snd_mixer *m; 265 struct cdev *pdev; 266 int i; 267 268 pdev = mixer_get_devt(dev); 269 m = pdev->si_drv1; 270 snd_mtxlock(m->lock); 271 272 i = MIXER_REINIT(m); 273 if (i) { 274 snd_mtxunlock(m->lock); 275 return i; 276 } 277 278 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 279 mixer_set(m, i, m->level[i]); 280 281 mixer_setrecsrc(m, m->recsrc); 282 snd_mtxunlock(m->lock); 283 284 return 0; 285} 286 287#ifdef SND_DYNSYSCTL 288static int 289sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS) 290{ 291 char devname[32]; 292 int error, dev; 293 struct snd_mixer *m; 294 295 m = oidp->oid_arg1; 296 snd_mtxlock(m->lock); 297 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname)); 298 snd_mtxunlock(m->lock); 299 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req); 300 snd_mtxlock(m->lock); 301 if (error == 0 && req->newptr != NULL) { 302 dev = mixer_lookup(devname); 303 if (dev == -1) { 304 snd_mtxunlock(m->lock); 305 return EINVAL; 306 } 307 else if (dev != m->hwvol_mixer) { 308 m->hwvol_mixer = dev; 309 m->hwvol_muted = 0; 310 } 311 } 312 snd_mtxunlock(m->lock); 313 return error; 314} 315#endif 316 317int 318mixer_hwvol_init(device_t dev) 319{ 320 struct snd_mixer *m; 321 struct cdev *pdev; 322 323 pdev = mixer_get_devt(dev); 324 m = pdev->si_drv1; 325 326 m->hwvol_mixer = SOUND_MIXER_VOLUME; 327 m->hwvol_step = 5; 328#ifdef SND_DYNSYSCTL 329 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)), 330 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, ""); 331 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)), 332 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0, 333 sysctl_hw_snd_hwvol_mixer, "A", ""); 334#endif 335 return 0; 336} 337 338void 339mixer_hwvol_mute(device_t dev) 340{ 341 struct snd_mixer *m; 342 struct cdev *pdev; 343 344 pdev = mixer_get_devt(dev); 345 m = pdev->si_drv1; 346 snd_mtxlock(m->lock); 347 if (m->hwvol_muted) { 348 m->hwvol_muted = 0; 349 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level); 350 } else { 351 m->hwvol_muted++; 352 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer); 353 mixer_set(m, m->hwvol_mixer, 0); 354 } 355 snd_mtxunlock(m->lock); 356} 357 358void 359mixer_hwvol_step(device_t dev, int left_step, int right_step) 360{ 361 struct snd_mixer *m; 362 int level, left, right; 363 struct cdev *pdev; 364 365 pdev = mixer_get_devt(dev); 366 m = pdev->si_drv1; 367 snd_mtxlock(m->lock); 368 if (m->hwvol_muted) { 369 m->hwvol_muted = 0; 370 level = m->hwvol_mute_level; 371 } else 372 level = mixer_get(m, m->hwvol_mixer); 373 if (level != -1) { 374 left = level & 0xff; 375 right = level >> 8; 376 left += left_step * m->hwvol_step; 377 if (left < 0) 378 left = 0; 379 right += right_step * m->hwvol_step; 380 if (right < 0) 381 right = 0; 382 mixer_set(m, m->hwvol_mixer, left | right << 8); 383 } 384 snd_mtxunlock(m->lock); 385} 386 387/* ----------------------------------------------------------------------- */ 388 389static int 390mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td) 391{ 392 struct snd_mixer *m; 393 intrmask_t s; 394 395 m = i_dev->si_drv1; 396 s = spltty(); 397 snd_mtxlock(m->lock); 398 399 m->busy++; 400 401 snd_mtxunlock(m->lock); 402 splx(s); 403 return 0; 404} 405 406static int 407mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td) 408{ 409 struct snd_mixer *m; 410 intrmask_t s; 411 412 m = i_dev->si_drv1; 413 s = spltty(); 414 snd_mtxlock(m->lock); 415 416 if (!m->busy) { 417 snd_mtxunlock(m->lock); 418 splx(s); 419 return EBADF; 420 } 421 m->busy--; 422 423 snd_mtxunlock(m->lock); 424 splx(s); 425 return 0; 426} 427 428int 429mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td) 430{ 431 struct snd_mixer *m; 432 intrmask_t s; 433 int ret, *arg_i = (int *)arg; 434 int v = -1, j = cmd & 0xff; 435 436 m = i_dev->si_drv1; 437 if (!m->busy) 438 return EBADF; 439 440 s = spltty(); 441 snd_mtxlock(m->lock); 442 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) { 443 if (j == SOUND_MIXER_RECSRC) 444 ret = mixer_setrecsrc(m, *arg_i); 445 else 446 ret = mixer_set(m, j, *arg_i); 447 snd_mtxunlock(m->lock); 448 splx(s); 449 return (ret == 0)? 0 : ENXIO; 450 } 451 452 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) { 453 switch (j) { 454 case SOUND_MIXER_DEVMASK: 455 case SOUND_MIXER_CAPS: 456 case SOUND_MIXER_STEREODEVS: 457 v = mix_getdevs(m); 458 break; 459 460 case SOUND_MIXER_RECMASK: 461 v = mix_getrecdevs(m); 462 break; 463 464 case SOUND_MIXER_RECSRC: 465 v = mixer_getrecsrc(m); 466 break; 467 468 default: 469 v = mixer_get(m, j); 470 } 471 *arg_i = v; 472 snd_mtxunlock(m->lock); 473 return (v != -1)? 0 : ENXIO; 474 } 475 snd_mtxunlock(m->lock); 476 splx(s); 477 return ENXIO; 478} 479 480#ifdef USING_DEVFS 481static void 482mixer_clone(void *arg, char *name, int namelen, struct cdev **dev) 483{ 484 struct snddev_info *sd; 485
| 32 33MALLOC_DEFINE(M_MIXER, "mixer", "mixer"); 34 35#define MIXER_NAMELEN 16 36struct snd_mixer { 37 KOBJ_FIELDS; 38 const char *type; 39 void *devinfo; 40 int busy; 41 int hwvol_muted; 42 int hwvol_mixer; 43 int hwvol_step; 44 u_int32_t hwvol_mute_level; 45 u_int32_t devs; 46 u_int32_t recdevs; 47 u_int32_t recsrc; 48 u_int16_t level[32]; 49 char name[MIXER_NAMELEN]; 50 struct mtx *lock; 51}; 52 53static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = { 54 [SOUND_MIXER_VOLUME] = 75, 55 [SOUND_MIXER_BASS] = 50, 56 [SOUND_MIXER_TREBLE] = 50, 57 [SOUND_MIXER_SYNTH] = 75, 58 [SOUND_MIXER_PCM] = 75, 59 [SOUND_MIXER_SPEAKER] = 75, 60 [SOUND_MIXER_LINE] = 75, 61 [SOUND_MIXER_MIC] = 0, 62 [SOUND_MIXER_CD] = 75, 63 [SOUND_MIXER_LINE1] = 75, 64 [SOUND_MIXER_VIDEO] = 75, 65 [SOUND_MIXER_RECLEV] = 0, 66 [SOUND_MIXER_OGAIN] = 50, 67 [SOUND_MIXER_MONITOR] = 75, 68}; 69 70static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES; 71 72static d_open_t mixer_open; 73static d_close_t mixer_close; 74 75static struct cdevsw mixer_cdevsw = { 76 .d_version = D_VERSION, 77 .d_flags = D_NEEDGIANT, 78 .d_open = mixer_open, 79 .d_close = mixer_close, 80 .d_ioctl = mixer_ioctl, 81 .d_name = "mixer", 82 .d_maj = SND_CDEV_MAJOR, 83}; 84 85#ifdef USING_DEVFS 86static eventhandler_tag mixer_ehtag; 87#endif 88 89static struct cdev * 90mixer_get_devt(device_t dev) 91{ 92 struct snddev_info *snddev; 93 94 snddev = device_get_softc(dev); 95 96 return snddev->mixer_dev; 97} 98 99#ifdef SND_DYNSYSCTL 100static int 101mixer_lookup(char *devname) 102{ 103 int i; 104 105 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 106 if (strncmp(devname, snd_mixernames[i], 107 strlen(snd_mixernames[i])) == 0) 108 return i; 109 return -1; 110} 111#endif 112 113static int 114mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev) 115{ 116 unsigned l, r; 117 int v; 118 119 if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev)))) 120 return -1; 121 122 l = min((lev & 0x00ff), 100); 123 r = min(((lev & 0xff00) >> 8), 100); 124 125 v = MIXER_SET(mixer, dev, l, r); 126 if (v < 0) 127 return -1; 128 129 mixer->level[dev] = l | (r << 8); 130 return 0; 131} 132 133static int 134mixer_get(struct snd_mixer *mixer, int dev) 135{ 136 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev))) 137 return mixer->level[dev]; 138 else return -1; 139} 140 141static int 142mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src) 143{ 144 src &= mixer->recdevs; 145 if (src == 0) 146 src = SOUND_MASK_MIC; 147 mixer->recsrc = MIXER_SETRECSRC(mixer, src); 148 return 0; 149} 150 151static int 152mixer_getrecsrc(struct snd_mixer *mixer) 153{ 154 return mixer->recsrc; 155} 156 157void 158mix_setdevs(struct snd_mixer *m, u_int32_t v) 159{ 160 m->devs = v; 161} 162 163void 164mix_setrecdevs(struct snd_mixer *m, u_int32_t v) 165{ 166 m->recdevs = v; 167} 168 169u_int32_t 170mix_getdevs(struct snd_mixer *m) 171{ 172 return m->devs; 173} 174 175u_int32_t 176mix_getrecdevs(struct snd_mixer *m) 177{ 178 return m->recdevs; 179} 180 181void * 182mix_getdevinfo(struct snd_mixer *m) 183{ 184 return m->devinfo; 185} 186 187int 188mixer_init(device_t dev, kobj_class_t cls, void *devinfo) 189{ 190 struct snddev_info *snddev; 191 struct snd_mixer *m; 192 u_int16_t v; 193 struct cdev *pdev; 194 int i, unit; 195 196 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO); 197 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev)); 198 m->lock = snd_mtxcreate(m->name, "pcm mixer"); 199 m->type = cls->name; 200 m->devinfo = devinfo; 201 m->busy = 0; 202 203 if (MIXER_INIT(m)) 204 goto bad; 205 206 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 207 v = snd_mixerdefaults[i]; 208 mixer_set(m, i, v | (v << 8)); 209 } 210 211 mixer_setrecsrc(m, SOUND_MASK_MIC); 212 213 unit = device_get_unit(dev); 214 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0), 215 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit); 216 pdev->si_drv1 = m; 217 snddev = device_get_softc(dev); 218 snddev->mixer_dev = pdev; 219 220 return 0; 221 222bad: 223 snd_mtxlock(m->lock); 224 snd_mtxfree(m->lock); 225 kobj_delete((kobj_t)m, M_MIXER); 226 return -1; 227} 228 229int 230mixer_uninit(device_t dev) 231{ 232 int i; 233 struct snd_mixer *m; 234 struct cdev *pdev; 235 236 pdev = mixer_get_devt(dev); 237 m = pdev->si_drv1; 238 snd_mtxlock(m->lock); 239 240 if (m->busy) { 241 snd_mtxunlock(m->lock); 242 return EBUSY; 243 } 244 245 pdev->si_drv1 = NULL; 246 destroy_dev(pdev); 247 248 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 249 mixer_set(m, i, 0); 250 251 mixer_setrecsrc(m, SOUND_MASK_MIC); 252 253 MIXER_UNINIT(m); 254 255 snd_mtxfree(m->lock); 256 kobj_delete((kobj_t)m, M_MIXER); 257 258 return 0; 259} 260 261int 262mixer_reinit(device_t dev) 263{ 264 struct snd_mixer *m; 265 struct cdev *pdev; 266 int i; 267 268 pdev = mixer_get_devt(dev); 269 m = pdev->si_drv1; 270 snd_mtxlock(m->lock); 271 272 i = MIXER_REINIT(m); 273 if (i) { 274 snd_mtxunlock(m->lock); 275 return i; 276 } 277 278 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 279 mixer_set(m, i, m->level[i]); 280 281 mixer_setrecsrc(m, m->recsrc); 282 snd_mtxunlock(m->lock); 283 284 return 0; 285} 286 287#ifdef SND_DYNSYSCTL 288static int 289sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS) 290{ 291 char devname[32]; 292 int error, dev; 293 struct snd_mixer *m; 294 295 m = oidp->oid_arg1; 296 snd_mtxlock(m->lock); 297 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname)); 298 snd_mtxunlock(m->lock); 299 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req); 300 snd_mtxlock(m->lock); 301 if (error == 0 && req->newptr != NULL) { 302 dev = mixer_lookup(devname); 303 if (dev == -1) { 304 snd_mtxunlock(m->lock); 305 return EINVAL; 306 } 307 else if (dev != m->hwvol_mixer) { 308 m->hwvol_mixer = dev; 309 m->hwvol_muted = 0; 310 } 311 } 312 snd_mtxunlock(m->lock); 313 return error; 314} 315#endif 316 317int 318mixer_hwvol_init(device_t dev) 319{ 320 struct snd_mixer *m; 321 struct cdev *pdev; 322 323 pdev = mixer_get_devt(dev); 324 m = pdev->si_drv1; 325 326 m->hwvol_mixer = SOUND_MIXER_VOLUME; 327 m->hwvol_step = 5; 328#ifdef SND_DYNSYSCTL 329 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)), 330 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, ""); 331 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)), 332 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0, 333 sysctl_hw_snd_hwvol_mixer, "A", ""); 334#endif 335 return 0; 336} 337 338void 339mixer_hwvol_mute(device_t dev) 340{ 341 struct snd_mixer *m; 342 struct cdev *pdev; 343 344 pdev = mixer_get_devt(dev); 345 m = pdev->si_drv1; 346 snd_mtxlock(m->lock); 347 if (m->hwvol_muted) { 348 m->hwvol_muted = 0; 349 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level); 350 } else { 351 m->hwvol_muted++; 352 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer); 353 mixer_set(m, m->hwvol_mixer, 0); 354 } 355 snd_mtxunlock(m->lock); 356} 357 358void 359mixer_hwvol_step(device_t dev, int left_step, int right_step) 360{ 361 struct snd_mixer *m; 362 int level, left, right; 363 struct cdev *pdev; 364 365 pdev = mixer_get_devt(dev); 366 m = pdev->si_drv1; 367 snd_mtxlock(m->lock); 368 if (m->hwvol_muted) { 369 m->hwvol_muted = 0; 370 level = m->hwvol_mute_level; 371 } else 372 level = mixer_get(m, m->hwvol_mixer); 373 if (level != -1) { 374 left = level & 0xff; 375 right = level >> 8; 376 left += left_step * m->hwvol_step; 377 if (left < 0) 378 left = 0; 379 right += right_step * m->hwvol_step; 380 if (right < 0) 381 right = 0; 382 mixer_set(m, m->hwvol_mixer, left | right << 8); 383 } 384 snd_mtxunlock(m->lock); 385} 386 387/* ----------------------------------------------------------------------- */ 388 389static int 390mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td) 391{ 392 struct snd_mixer *m; 393 intrmask_t s; 394 395 m = i_dev->si_drv1; 396 s = spltty(); 397 snd_mtxlock(m->lock); 398 399 m->busy++; 400 401 snd_mtxunlock(m->lock); 402 splx(s); 403 return 0; 404} 405 406static int 407mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td) 408{ 409 struct snd_mixer *m; 410 intrmask_t s; 411 412 m = i_dev->si_drv1; 413 s = spltty(); 414 snd_mtxlock(m->lock); 415 416 if (!m->busy) { 417 snd_mtxunlock(m->lock); 418 splx(s); 419 return EBADF; 420 } 421 m->busy--; 422 423 snd_mtxunlock(m->lock); 424 splx(s); 425 return 0; 426} 427 428int 429mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td) 430{ 431 struct snd_mixer *m; 432 intrmask_t s; 433 int ret, *arg_i = (int *)arg; 434 int v = -1, j = cmd & 0xff; 435 436 m = i_dev->si_drv1; 437 if (!m->busy) 438 return EBADF; 439 440 s = spltty(); 441 snd_mtxlock(m->lock); 442 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) { 443 if (j == SOUND_MIXER_RECSRC) 444 ret = mixer_setrecsrc(m, *arg_i); 445 else 446 ret = mixer_set(m, j, *arg_i); 447 snd_mtxunlock(m->lock); 448 splx(s); 449 return (ret == 0)? 0 : ENXIO; 450 } 451 452 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) { 453 switch (j) { 454 case SOUND_MIXER_DEVMASK: 455 case SOUND_MIXER_CAPS: 456 case SOUND_MIXER_STEREODEVS: 457 v = mix_getdevs(m); 458 break; 459 460 case SOUND_MIXER_RECMASK: 461 v = mix_getrecdevs(m); 462 break; 463 464 case SOUND_MIXER_RECSRC: 465 v = mixer_getrecsrc(m); 466 break; 467 468 default: 469 v = mixer_get(m, j); 470 } 471 *arg_i = v; 472 snd_mtxunlock(m->lock); 473 return (v != -1)? 0 : ENXIO; 474 } 475 snd_mtxunlock(m->lock); 476 splx(s); 477 return ENXIO; 478} 479 480#ifdef USING_DEVFS 481static void 482mixer_clone(void *arg, char *name, int namelen, struct cdev **dev) 483{ 484 struct snddev_info *sd; 485
|