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