ALSA: Add a reference counter to card instance
[firefly-linux-kernel-4.4.55.git] / sound / core / oss / mixer_oss.c
1 /*
2  *  OSS emulation layer for the mixer interface
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/string.h>
26 #include <sound/core.h>
27 #include <sound/minors.h>
28 #include <sound/control.h>
29 #include <sound/info.h>
30 #include <sound/mixer_oss.h>
31 #include <linux/soundcard.h>
32
33 #define OSS_ALSAEMULVER         _SIOR ('M', 249, int)
34
35 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
36 MODULE_DESCRIPTION("Mixer OSS emulation for ALSA.");
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MIXER);
39
40 static int snd_mixer_oss_open(struct inode *inode, struct file *file)
41 {
42         struct snd_card *card;
43         struct snd_mixer_oss_file *fmixer;
44         int err;
45
46         err = nonseekable_open(inode, file);
47         if (err < 0)
48                 return err;
49
50         card = snd_lookup_oss_minor_data(iminor(inode),
51                                          SNDRV_OSS_DEVICE_TYPE_MIXER);
52         if (card == NULL)
53                 return -ENODEV;
54         if (card->mixer_oss == NULL) {
55                 snd_card_unref(card);
56                 return -ENODEV;
57         }
58         err = snd_card_file_add(card, file);
59         if (err < 0) {
60                 snd_card_unref(card);
61                 return err;
62         }
63         fmixer = kzalloc(sizeof(*fmixer), GFP_KERNEL);
64         if (fmixer == NULL) {
65                 snd_card_file_remove(card, file);
66                 snd_card_unref(card);
67                 return -ENOMEM;
68         }
69         fmixer->card = card;
70         fmixer->mixer = card->mixer_oss;
71         file->private_data = fmixer;
72         if (!try_module_get(card->module)) {
73                 kfree(fmixer);
74                 snd_card_file_remove(card, file);
75                 snd_card_unref(card);
76                 return -EFAULT;
77         }
78         return 0;
79 }
80
81 static int snd_mixer_oss_release(struct inode *inode, struct file *file)
82 {
83         struct snd_mixer_oss_file *fmixer;
84
85         if (file->private_data) {
86                 fmixer = file->private_data;
87                 module_put(fmixer->card->module);
88                 snd_card_file_remove(fmixer->card, file);
89                 kfree(fmixer);
90         }
91         return 0;
92 }
93
94 static int snd_mixer_oss_info(struct snd_mixer_oss_file *fmixer,
95                               mixer_info __user *_info)
96 {
97         struct snd_card *card = fmixer->card;
98         struct snd_mixer_oss *mixer = fmixer->mixer;
99         struct mixer_info info;
100         
101         memset(&info, 0, sizeof(info));
102         strlcpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id));
103         strlcpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name));
104         info.modify_counter = card->mixer_oss_change_count;
105         if (copy_to_user(_info, &info, sizeof(info)))
106                 return -EFAULT;
107         return 0;
108 }
109
110 static int snd_mixer_oss_info_obsolete(struct snd_mixer_oss_file *fmixer,
111                                        _old_mixer_info __user *_info)
112 {
113         struct snd_card *card = fmixer->card;
114         struct snd_mixer_oss *mixer = fmixer->mixer;
115         _old_mixer_info info;
116         
117         memset(&info, 0, sizeof(info));
118         strlcpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id));
119         strlcpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name));
120         if (copy_to_user(_info, &info, sizeof(info)))
121                 return -EFAULT;
122         return 0;
123 }
124
125 static int snd_mixer_oss_caps(struct snd_mixer_oss_file *fmixer)
126 {
127         struct snd_mixer_oss *mixer = fmixer->mixer;
128         int result = 0;
129
130         if (mixer == NULL)
131                 return -EIO;
132         if (mixer->get_recsrc && mixer->put_recsrc)
133                 result |= SOUND_CAP_EXCL_INPUT;
134         return result;
135 }
136
137 static int snd_mixer_oss_devmask(struct snd_mixer_oss_file *fmixer)
138 {
139         struct snd_mixer_oss *mixer = fmixer->mixer;
140         struct snd_mixer_oss_slot *pslot;
141         int result = 0, chn;
142
143         if (mixer == NULL)
144                 return -EIO;
145         for (chn = 0; chn < 31; chn++) {
146                 pslot = &mixer->slots[chn];
147                 if (pslot->put_volume || pslot->put_recsrc)
148                         result |= 1 << chn;
149         }
150         return result;
151 }
152
153 static int snd_mixer_oss_stereodevs(struct snd_mixer_oss_file *fmixer)
154 {
155         struct snd_mixer_oss *mixer = fmixer->mixer;
156         struct snd_mixer_oss_slot *pslot;
157         int result = 0, chn;
158
159         if (mixer == NULL)
160                 return -EIO;
161         for (chn = 0; chn < 31; chn++) {
162                 pslot = &mixer->slots[chn];
163                 if (pslot->put_volume && pslot->stereo)
164                         result |= 1 << chn;
165         }
166         return result;
167 }
168
169 static int snd_mixer_oss_recmask(struct snd_mixer_oss_file *fmixer)
170 {
171         struct snd_mixer_oss *mixer = fmixer->mixer;
172         int result = 0;
173
174         if (mixer == NULL)
175                 return -EIO;
176         if (mixer->put_recsrc && mixer->get_recsrc) {   /* exclusive */
177                 result = mixer->mask_recsrc;
178         } else {
179                 struct snd_mixer_oss_slot *pslot;
180                 int chn;
181                 for (chn = 0; chn < 31; chn++) {
182                         pslot = &mixer->slots[chn];
183                         if (pslot->put_recsrc)
184                                 result |= 1 << chn;
185                 }
186         }
187         return result;
188 }
189
190 static int snd_mixer_oss_get_recsrc(struct snd_mixer_oss_file *fmixer)
191 {
192         struct snd_mixer_oss *mixer = fmixer->mixer;
193         int result = 0;
194
195         if (mixer == NULL)
196                 return -EIO;
197         if (mixer->put_recsrc && mixer->get_recsrc) {   /* exclusive */
198                 int err;
199                 unsigned int index;
200                 if ((err = mixer->get_recsrc(fmixer, &index)) < 0)
201                         return err;
202                 result = 1 << index;
203         } else {
204                 struct snd_mixer_oss_slot *pslot;
205                 int chn;
206                 for (chn = 0; chn < 31; chn++) {
207                         pslot = &mixer->slots[chn];
208                         if (pslot->get_recsrc) {
209                                 int active = 0;
210                                 pslot->get_recsrc(fmixer, pslot, &active);
211                                 if (active)
212                                         result |= 1 << chn;
213                         }
214                 }
215         }
216         return mixer->oss_recsrc = result;
217 }
218
219 static int snd_mixer_oss_set_recsrc(struct snd_mixer_oss_file *fmixer, int recsrc)
220 {
221         struct snd_mixer_oss *mixer = fmixer->mixer;
222         struct snd_mixer_oss_slot *pslot;
223         int chn, active;
224         unsigned int index;
225         int result = 0;
226
227         if (mixer == NULL)
228                 return -EIO;
229         if (mixer->get_recsrc && mixer->put_recsrc) {   /* exclusive input */
230                 if (recsrc & ~mixer->oss_recsrc)
231                         recsrc &= ~mixer->oss_recsrc;
232                 mixer->put_recsrc(fmixer, ffz(~recsrc));
233                 mixer->get_recsrc(fmixer, &index);
234                 result = 1 << index;
235         }
236         for (chn = 0; chn < 31; chn++) {
237                 pslot = &mixer->slots[chn];
238                 if (pslot->put_recsrc) {
239                         active = (recsrc & (1 << chn)) ? 1 : 0;
240                         pslot->put_recsrc(fmixer, pslot, active);
241                 }
242         }
243         if (! result) {
244                 for (chn = 0; chn < 31; chn++) {
245                         pslot = &mixer->slots[chn];
246                         if (pslot->get_recsrc) {
247                                 active = 0;
248                                 pslot->get_recsrc(fmixer, pslot, &active);
249                                 if (active)
250                                         result |= 1 << chn;
251                         }
252                 }
253         }
254         return result;
255 }
256
257 static int snd_mixer_oss_get_volume(struct snd_mixer_oss_file *fmixer, int slot)
258 {
259         struct snd_mixer_oss *mixer = fmixer->mixer;
260         struct snd_mixer_oss_slot *pslot;
261         int result = 0, left, right;
262
263         if (mixer == NULL || slot > 30)
264                 return -EIO;
265         pslot = &mixer->slots[slot];
266         left = pslot->volume[0];
267         right = pslot->volume[1];
268         if (pslot->get_volume)
269                 result = pslot->get_volume(fmixer, pslot, &left, &right);
270         if (!pslot->stereo)
271                 right = left;
272         if (snd_BUG_ON(left < 0 || left > 100))
273                 return -EIO;
274         if (snd_BUG_ON(right < 0 || right > 100))
275                 return -EIO;
276         if (result >= 0) {
277                 pslot->volume[0] = left;
278                 pslot->volume[1] = right;
279                 result = (left & 0xff) | ((right & 0xff) << 8);
280         }
281         return result;
282 }
283
284 static int snd_mixer_oss_set_volume(struct snd_mixer_oss_file *fmixer,
285                                     int slot, int volume)
286 {
287         struct snd_mixer_oss *mixer = fmixer->mixer;
288         struct snd_mixer_oss_slot *pslot;
289         int result = 0, left = volume & 0xff, right = (volume >> 8) & 0xff;
290
291         if (mixer == NULL || slot > 30)
292                 return -EIO;
293         pslot = &mixer->slots[slot];
294         if (left > 100)
295                 left = 100;
296         if (right > 100)
297                 right = 100;
298         if (!pslot->stereo)
299                 right = left;
300         if (pslot->put_volume)
301                 result = pslot->put_volume(fmixer, pslot, left, right);
302         if (result < 0)
303                 return result;
304         pslot->volume[0] = left;
305         pslot->volume[1] = right;
306         return (left & 0xff) | ((right & 0xff) << 8);
307 }
308
309 static int snd_mixer_oss_ioctl1(struct snd_mixer_oss_file *fmixer, unsigned int cmd, unsigned long arg)
310 {
311         void __user *argp = (void __user *)arg;
312         int __user *p = argp;
313         int tmp;
314
315         if (snd_BUG_ON(!fmixer))
316                 return -ENXIO;
317         if (((cmd >> 8) & 0xff) == 'M') {
318                 switch (cmd) {
319                 case SOUND_MIXER_INFO:
320                         return snd_mixer_oss_info(fmixer, argp);
321                 case SOUND_OLD_MIXER_INFO:
322                         return snd_mixer_oss_info_obsolete(fmixer, argp);
323                 case SOUND_MIXER_WRITE_RECSRC:
324                         if (get_user(tmp, p))
325                                 return -EFAULT;
326                         tmp = snd_mixer_oss_set_recsrc(fmixer, tmp);
327                         if (tmp < 0)
328                                 return tmp;
329                         return put_user(tmp, p);
330                 case OSS_GETVERSION:
331                         return put_user(SNDRV_OSS_VERSION, p);
332                 case OSS_ALSAEMULVER:
333                         return put_user(1, p);
334                 case SOUND_MIXER_READ_DEVMASK:
335                         tmp = snd_mixer_oss_devmask(fmixer);
336                         if (tmp < 0)
337                                 return tmp;
338                         return put_user(tmp, p);
339                 case SOUND_MIXER_READ_STEREODEVS:
340                         tmp = snd_mixer_oss_stereodevs(fmixer);
341                         if (tmp < 0)
342                                 return tmp;
343                         return put_user(tmp, p);
344                 case SOUND_MIXER_READ_RECMASK:
345                         tmp = snd_mixer_oss_recmask(fmixer);
346                         if (tmp < 0)
347                                 return tmp;
348                         return put_user(tmp, p);
349                 case SOUND_MIXER_READ_CAPS:
350                         tmp = snd_mixer_oss_caps(fmixer);
351                         if (tmp < 0)
352                                 return tmp;
353                         return put_user(tmp, p);
354                 case SOUND_MIXER_READ_RECSRC:
355                         tmp = snd_mixer_oss_get_recsrc(fmixer);
356                         if (tmp < 0)
357                                 return tmp;
358                         return put_user(tmp, p);
359                 }
360         }
361         if (cmd & SIOC_IN) {
362                 if (get_user(tmp, p))
363                         return -EFAULT;
364                 tmp = snd_mixer_oss_set_volume(fmixer, cmd & 0xff, tmp);
365                 if (tmp < 0)
366                         return tmp;
367                 return put_user(tmp, p);
368         } else if (cmd & SIOC_OUT) {
369                 tmp = snd_mixer_oss_get_volume(fmixer, cmd & 0xff);
370                 if (tmp < 0)
371                         return tmp;
372                 return put_user(tmp, p);
373         }
374         return -ENXIO;
375 }
376
377 static long snd_mixer_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
378 {
379         return snd_mixer_oss_ioctl1(file->private_data, cmd, arg);
380 }
381
382 int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg)
383 {
384         struct snd_mixer_oss_file fmixer;
385         
386         if (snd_BUG_ON(!card))
387                 return -ENXIO;
388         if (card->mixer_oss == NULL)
389                 return -ENXIO;
390         memset(&fmixer, 0, sizeof(fmixer));
391         fmixer.card = card;
392         fmixer.mixer = card->mixer_oss;
393         return snd_mixer_oss_ioctl1(&fmixer, cmd, arg);
394 }
395
396 #ifdef CONFIG_COMPAT
397 /* all compatible */
398 #define snd_mixer_oss_ioctl_compat      snd_mixer_oss_ioctl
399 #else
400 #define snd_mixer_oss_ioctl_compat      NULL
401 #endif
402
403 /*
404  *  REGISTRATION PART
405  */
406
407 static const struct file_operations snd_mixer_oss_f_ops =
408 {
409         .owner =        THIS_MODULE,
410         .open =         snd_mixer_oss_open,
411         .release =      snd_mixer_oss_release,
412         .llseek =       no_llseek,
413         .unlocked_ioctl =       snd_mixer_oss_ioctl,
414         .compat_ioctl = snd_mixer_oss_ioctl_compat,
415 };
416
417 /*
418  *  utilities
419  */
420
421 static long snd_mixer_oss_conv(long val, long omin, long omax, long nmin, long nmax)
422 {
423         long orange = omax - omin, nrange = nmax - nmin;
424         
425         if (orange == 0)
426                 return 0;
427         return ((nrange * (val - omin)) + (orange / 2)) / orange + nmin;
428 }
429
430 /* convert from alsa native to oss values (0-100) */
431 static long snd_mixer_oss_conv1(long val, long min, long max, int *old)
432 {
433         if (val == snd_mixer_oss_conv(*old, 0, 100, min, max))
434                 return *old;
435         return snd_mixer_oss_conv(val, min, max, 0, 100);
436 }
437
438 /* convert from oss to alsa native values */
439 static long snd_mixer_oss_conv2(long val, long min, long max)
440 {
441         return snd_mixer_oss_conv(val, 0, 100, min, max);
442 }
443
444 #if 0
445 static void snd_mixer_oss_recsrce_set(struct snd_card *card, int slot)
446 {
447         struct snd_mixer_oss *mixer = card->mixer_oss;
448         if (mixer)
449                 mixer->mask_recsrc |= 1 << slot;
450 }
451
452 static int snd_mixer_oss_recsrce_get(struct snd_card *card, int slot)
453 {
454         struct snd_mixer_oss *mixer = card->mixer_oss;
455         if (mixer && (mixer->mask_recsrc & (1 << slot)))
456                 return 1;
457         return 0;
458 }
459 #endif
460
461 #define SNDRV_MIXER_OSS_SIGNATURE               0x65999250
462
463 #define SNDRV_MIXER_OSS_ITEM_GLOBAL     0
464 #define SNDRV_MIXER_OSS_ITEM_GSWITCH    1
465 #define SNDRV_MIXER_OSS_ITEM_GROUTE     2
466 #define SNDRV_MIXER_OSS_ITEM_GVOLUME    3
467 #define SNDRV_MIXER_OSS_ITEM_PSWITCH    4
468 #define SNDRV_MIXER_OSS_ITEM_PROUTE     5
469 #define SNDRV_MIXER_OSS_ITEM_PVOLUME    6
470 #define SNDRV_MIXER_OSS_ITEM_CSWITCH    7
471 #define SNDRV_MIXER_OSS_ITEM_CROUTE     8
472 #define SNDRV_MIXER_OSS_ITEM_CVOLUME    9
473 #define SNDRV_MIXER_OSS_ITEM_CAPTURE    10
474
475 #define SNDRV_MIXER_OSS_ITEM_COUNT      11
476
477 #define SNDRV_MIXER_OSS_PRESENT_GLOBAL  (1<<0)
478 #define SNDRV_MIXER_OSS_PRESENT_GSWITCH (1<<1)
479 #define SNDRV_MIXER_OSS_PRESENT_GROUTE  (1<<2)
480 #define SNDRV_MIXER_OSS_PRESENT_GVOLUME (1<<3)
481 #define SNDRV_MIXER_OSS_PRESENT_PSWITCH (1<<4)
482 #define SNDRV_MIXER_OSS_PRESENT_PROUTE  (1<<5)
483 #define SNDRV_MIXER_OSS_PRESENT_PVOLUME (1<<6)
484 #define SNDRV_MIXER_OSS_PRESENT_CSWITCH (1<<7)
485 #define SNDRV_MIXER_OSS_PRESENT_CROUTE  (1<<8)
486 #define SNDRV_MIXER_OSS_PRESENT_CVOLUME (1<<9)
487 #define SNDRV_MIXER_OSS_PRESENT_CAPTURE (1<<10)
488
489 struct slot {
490         unsigned int signature;
491         unsigned int present;
492         unsigned int channels;
493         unsigned int numid[SNDRV_MIXER_OSS_ITEM_COUNT];
494         unsigned int capture_item;
495         struct snd_mixer_oss_assign_table *assigned;
496         unsigned int allocated: 1;
497 };
498
499 #define ID_UNKNOWN      ((unsigned int)-1)
500
501 static struct snd_kcontrol *snd_mixer_oss_test_id(struct snd_mixer_oss *mixer, const char *name, int index)
502 {
503         struct snd_card *card = mixer->card;
504         struct snd_ctl_elem_id id;
505         
506         memset(&id, 0, sizeof(id));
507         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
508         strcpy(id.name, name);
509         id.index = index;
510         return snd_ctl_find_id(card, &id);
511 }
512
513 static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,
514                                           struct snd_mixer_oss_slot *pslot,
515                                           unsigned int numid,
516                                           int *left, int *right)
517 {
518         struct snd_ctl_elem_info *uinfo;
519         struct snd_ctl_elem_value *uctl;
520         struct snd_kcontrol *kctl;
521         struct snd_card *card = fmixer->card;
522
523         if (numid == ID_UNKNOWN)
524                 return;
525         down_read(&card->controls_rwsem);
526         if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
527                 up_read(&card->controls_rwsem);
528                 return;
529         }
530         uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
531         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
532         if (uinfo == NULL || uctl == NULL)
533                 goto __unalloc;
534         if (kctl->info(kctl, uinfo))
535                 goto __unalloc;
536         if (kctl->get(kctl, uctl))
537                 goto __unalloc;
538         if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN &&
539             uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1)
540                 goto __unalloc;
541         *left = snd_mixer_oss_conv1(uctl->value.integer.value[0], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[0]);
542         if (uinfo->count > 1)
543                 *right = snd_mixer_oss_conv1(uctl->value.integer.value[1], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[1]);
544       __unalloc:
545         up_read(&card->controls_rwsem);
546         kfree(uctl);
547         kfree(uinfo);
548 }
549
550 static void snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file *fmixer,
551                                          struct snd_mixer_oss_slot *pslot,
552                                          unsigned int numid,
553                                          int *left, int *right,
554                                          int route)
555 {
556         struct snd_ctl_elem_info *uinfo;
557         struct snd_ctl_elem_value *uctl;
558         struct snd_kcontrol *kctl;
559         struct snd_card *card = fmixer->card;
560
561         if (numid == ID_UNKNOWN)
562                 return;
563         down_read(&card->controls_rwsem);
564         if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
565                 up_read(&card->controls_rwsem);
566                 return;
567         }
568         uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
569         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
570         if (uinfo == NULL || uctl == NULL)
571                 goto __unalloc;
572         if (kctl->info(kctl, uinfo))
573                 goto __unalloc;
574         if (kctl->get(kctl, uctl))
575                 goto __unalloc;
576         if (!uctl->value.integer.value[0]) {
577                 *left = 0;
578                 if (uinfo->count == 1)
579                         *right = 0;
580         }
581         if (uinfo->count > 1 && !uctl->value.integer.value[route ? 3 : 1])
582                 *right = 0;
583       __unalloc:
584         up_read(&card->controls_rwsem);
585         kfree(uctl);
586         kfree(uinfo);
587 }
588
589 static int snd_mixer_oss_get_volume1(struct snd_mixer_oss_file *fmixer,
590                                      struct snd_mixer_oss_slot *pslot,
591                                      int *left, int *right)
592 {
593         struct slot *slot = pslot->private_data;
594         
595         *left = *right = 100;
596         if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) {
597                 snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right);
598         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) {
599                 snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right);
600         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) {
601                 snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right);
602         }
603         if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
604                 snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
605         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
606                 snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
607         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
608                 snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
609         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
610                 snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
611         }
612         return 0;
613 }
614
615 static void snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file *fmixer,
616                                           struct snd_mixer_oss_slot *pslot,
617                                           unsigned int numid,
618                                           int left, int right)
619 {
620         struct snd_ctl_elem_info *uinfo;
621         struct snd_ctl_elem_value *uctl;
622         struct snd_kcontrol *kctl;
623         struct snd_card *card = fmixer->card;
624         int res;
625
626         if (numid == ID_UNKNOWN)
627                 return;
628         down_read(&card->controls_rwsem);
629         if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
630                 up_read(&card->controls_rwsem);
631                 return;
632         }
633         uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
634         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
635         if (uinfo == NULL || uctl == NULL)
636                 goto __unalloc;
637         if (kctl->info(kctl, uinfo))
638                 goto __unalloc;
639         if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN &&
640             uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1)
641                 goto __unalloc;
642         uctl->value.integer.value[0] = snd_mixer_oss_conv2(left, uinfo->value.integer.min, uinfo->value.integer.max);
643         if (uinfo->count > 1)
644                 uctl->value.integer.value[1] = snd_mixer_oss_conv2(right, uinfo->value.integer.min, uinfo->value.integer.max);
645         if ((res = kctl->put(kctl, uctl)) < 0)
646                 goto __unalloc;
647         if (res > 0)
648                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
649       __unalloc:
650         up_read(&card->controls_rwsem);
651         kfree(uctl);
652         kfree(uinfo);
653 }
654
655 static void snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file *fmixer,
656                                          struct snd_mixer_oss_slot *pslot,
657                                          unsigned int numid,
658                                          int left, int right,
659                                          int route)
660 {
661         struct snd_ctl_elem_info *uinfo;
662         struct snd_ctl_elem_value *uctl;
663         struct snd_kcontrol *kctl;
664         struct snd_card *card = fmixer->card;
665         int res;
666
667         if (numid == ID_UNKNOWN)
668                 return;
669         down_read(&card->controls_rwsem);
670         if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
671                 up_read(&card->controls_rwsem);
672                 return;
673         }
674         uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
675         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
676         if (uinfo == NULL || uctl == NULL)
677                 goto __unalloc;
678         if (kctl->info(kctl, uinfo))
679                 goto __unalloc;
680         if (uinfo->count > 1) {
681                 uctl->value.integer.value[0] = left > 0 ? 1 : 0;
682                 uctl->value.integer.value[route ? 3 : 1] = right > 0 ? 1 : 0;
683                 if (route) {
684                         uctl->value.integer.value[1] =
685                         uctl->value.integer.value[2] = 0;
686                 }
687         } else {
688                 uctl->value.integer.value[0] = (left > 0 || right > 0) ? 1 : 0;
689         }
690         if ((res = kctl->put(kctl, uctl)) < 0)
691                 goto __unalloc;
692         if (res > 0)
693                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
694       __unalloc:
695         up_read(&card->controls_rwsem);
696         kfree(uctl);
697         kfree(uinfo);
698 }
699
700 static int snd_mixer_oss_put_volume1(struct snd_mixer_oss_file *fmixer,
701                                      struct snd_mixer_oss_slot *pslot,
702                                      int left, int right)
703 {
704         struct slot *slot = pslot->private_data;
705         
706         if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) {
707                 snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right);
708                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME)
709                         snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right);
710         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME) {
711                 snd_mixer_oss_put_volume1_vol(fmixer, pslot,
712                         slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right);
713         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) {
714                 snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right);
715         } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) {
716                 snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right);
717         }
718         if (left || right) {
719                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH)
720                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
721                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_CSWITCH)
722                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], left, right, 0);
723                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH)
724                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
725                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE)
726                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
727                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_CROUTE)
728                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], left, right, 1);
729                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE)
730                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
731         } else {
732                 if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
733                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
734                 } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) {
735                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], left, right, 0);
736                 } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
737                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
738                 } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
739                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
740                 } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CROUTE) {
741                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], left, right, 1);
742                 } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
743                         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
744                 }
745         }
746         return 0;
747 }
748
749 static int snd_mixer_oss_get_recsrc1_sw(struct snd_mixer_oss_file *fmixer,
750                                         struct snd_mixer_oss_slot *pslot,
751                                         int *active)
752 {
753         struct slot *slot = pslot->private_data;
754         int left, right;
755         
756         left = right = 1;
757         snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], &left, &right, 0);
758         *active = (left || right) ? 1 : 0;
759         return 0;
760 }
761
762 static int snd_mixer_oss_get_recsrc1_route(struct snd_mixer_oss_file *fmixer,
763                                            struct snd_mixer_oss_slot *pslot,
764                                            int *active)
765 {
766         struct slot *slot = pslot->private_data;
767         int left, right;
768         
769         left = right = 1;
770         snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], &left, &right, 1);
771         *active = (left || right) ? 1 : 0;
772         return 0;
773 }
774
775 static int snd_mixer_oss_put_recsrc1_sw(struct snd_mixer_oss_file *fmixer,
776                                         struct snd_mixer_oss_slot *pslot,
777                                         int active)
778 {
779         struct slot *slot = pslot->private_data;
780         
781         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], active, active, 0);
782         return 0;
783 }
784
785 static int snd_mixer_oss_put_recsrc1_route(struct snd_mixer_oss_file *fmixer,
786                                            struct snd_mixer_oss_slot *pslot,
787                                            int active)
788 {
789         struct slot *slot = pslot->private_data;
790         
791         snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], active, active, 1);
792         return 0;
793 }
794
795 static int snd_mixer_oss_get_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int *active_index)
796 {
797         struct snd_card *card = fmixer->card;
798         struct snd_mixer_oss *mixer = fmixer->mixer;
799         struct snd_kcontrol *kctl;
800         struct snd_mixer_oss_slot *pslot;
801         struct slot *slot;
802         struct snd_ctl_elem_info *uinfo;
803         struct snd_ctl_elem_value *uctl;
804         int err, idx;
805         
806         uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
807         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
808         if (uinfo == NULL || uctl == NULL) {
809                 err = -ENOMEM;
810                 goto __free_only;
811         }
812         down_read(&card->controls_rwsem);
813         kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
814         if (! kctl) {
815                 err = -ENOENT;
816                 goto __unlock;
817         }
818         if ((err = kctl->info(kctl, uinfo)) < 0)
819                 goto __unlock;
820         if ((err = kctl->get(kctl, uctl)) < 0)
821                 goto __unlock;
822         for (idx = 0; idx < 32; idx++) {
823                 if (!(mixer->mask_recsrc & (1 << idx)))
824                         continue;
825                 pslot = &mixer->slots[idx];
826                 slot = pslot->private_data;
827                 if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
828                         continue;
829                 if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
830                         continue;
831                 if (slot->capture_item == uctl->value.enumerated.item[0]) {
832                         *active_index = idx;
833                         break;
834                 }
835         }
836         err = 0;
837       __unlock:
838         up_read(&card->controls_rwsem);
839       __free_only:
840         kfree(uctl);
841         kfree(uinfo);
842         return err;
843 }
844
845 static int snd_mixer_oss_put_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int active_index)
846 {
847         struct snd_card *card = fmixer->card;
848         struct snd_mixer_oss *mixer = fmixer->mixer;
849         struct snd_kcontrol *kctl;
850         struct snd_mixer_oss_slot *pslot;
851         struct slot *slot = NULL;
852         struct snd_ctl_elem_info *uinfo;
853         struct snd_ctl_elem_value *uctl;
854         int err;
855         unsigned int idx;
856
857         uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
858         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
859         if (uinfo == NULL || uctl == NULL) {
860                 err = -ENOMEM;
861                 goto __free_only;
862         }
863         down_read(&card->controls_rwsem);
864         kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
865         if (! kctl) {
866                 err = -ENOENT;
867                 goto __unlock;
868         }
869         if ((err = kctl->info(kctl, uinfo)) < 0)
870                 goto __unlock;
871         for (idx = 0; idx < 32; idx++) {
872                 if (!(mixer->mask_recsrc & (1 << idx)))
873                         continue;
874                 pslot = &mixer->slots[idx];
875                 slot = pslot->private_data;
876                 if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
877                         continue;
878                 if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
879                         continue;
880                 if (idx == active_index)
881                         break;
882                 slot = NULL;
883         }
884         if (! slot)
885                 goto __unlock;
886         for (idx = 0; idx < uinfo->count; idx++)
887                 uctl->value.enumerated.item[idx] = slot->capture_item;
888         err = kctl->put(kctl, uctl);
889         if (err > 0)
890                 snd_ctl_notify(fmixer->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
891         err = 0;
892       __unlock:
893         up_read(&card->controls_rwsem);
894       __free_only:
895         kfree(uctl);
896         kfree(uinfo);
897         return err;
898 }
899
900 struct snd_mixer_oss_assign_table {
901         int oss_id;
902         const char *name;
903         int index;
904 };
905
906 static int snd_mixer_oss_build_test(struct snd_mixer_oss *mixer, struct slot *slot, const char *name, int index, int item)
907 {
908         struct snd_ctl_elem_info *info;
909         struct snd_kcontrol *kcontrol;
910         struct snd_card *card = mixer->card;
911         int err;
912
913         down_read(&card->controls_rwsem);
914         kcontrol = snd_mixer_oss_test_id(mixer, name, index);
915         if (kcontrol == NULL) {
916                 up_read(&card->controls_rwsem);
917                 return 0;
918         }
919         info = kmalloc(sizeof(*info), GFP_KERNEL);
920         if (! info) {
921                 up_read(&card->controls_rwsem);
922                 return -ENOMEM;
923         }
924         if ((err = kcontrol->info(kcontrol, info)) < 0) {
925                 up_read(&card->controls_rwsem);
926                 kfree(info);
927                 return err;
928         }
929         slot->numid[item] = kcontrol->id.numid;
930         up_read(&card->controls_rwsem);
931         if (info->count > slot->channels)
932                 slot->channels = info->count;
933         slot->present |= 1 << item;
934         kfree(info);
935         return 0;
936 }
937
938 static void snd_mixer_oss_slot_free(struct snd_mixer_oss_slot *chn)
939 {
940         struct slot *p = chn->private_data;
941         if (p) {
942                 if (p->allocated && p->assigned) {
943                         kfree(p->assigned->name);
944                         kfree(p->assigned);
945                 }
946                 kfree(p);
947         }
948 }
949
950 static void mixer_slot_clear(struct snd_mixer_oss_slot *rslot)
951 {
952         int idx = rslot->number; /* remember this */
953         if (rslot->private_free)
954                 rslot->private_free(rslot);
955         memset(rslot, 0, sizeof(*rslot));
956         rslot->number = idx;
957 }
958
959 /* In a separate function to keep gcc 3.2 happy - do NOT merge this in
960    snd_mixer_oss_build_input! */
961 static int snd_mixer_oss_build_test_all(struct snd_mixer_oss *mixer,
962                                         struct snd_mixer_oss_assign_table *ptr,
963                                         struct slot *slot)
964 {
965         char str[64];
966         int err;
967
968         err = snd_mixer_oss_build_test(mixer, slot, ptr->name, ptr->index,
969                                        SNDRV_MIXER_OSS_ITEM_GLOBAL);
970         if (err)
971                 return err;
972         sprintf(str, "%s Switch", ptr->name);
973         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
974                                        SNDRV_MIXER_OSS_ITEM_GSWITCH);
975         if (err)
976                 return err;
977         sprintf(str, "%s Route", ptr->name);
978         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
979                                        SNDRV_MIXER_OSS_ITEM_GROUTE);
980         if (err)
981                 return err;
982         sprintf(str, "%s Volume", ptr->name);
983         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
984                                        SNDRV_MIXER_OSS_ITEM_GVOLUME);
985         if (err)
986                 return err;
987         sprintf(str, "%s Playback Switch", ptr->name);
988         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
989                                        SNDRV_MIXER_OSS_ITEM_PSWITCH);
990         if (err)
991                 return err;
992         sprintf(str, "%s Playback Route", ptr->name);
993         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
994                                        SNDRV_MIXER_OSS_ITEM_PROUTE);
995         if (err)
996                 return err;
997         sprintf(str, "%s Playback Volume", ptr->name);
998         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
999                                        SNDRV_MIXER_OSS_ITEM_PVOLUME);
1000         if (err)
1001                 return err;
1002         sprintf(str, "%s Capture Switch", ptr->name);
1003         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
1004                                        SNDRV_MIXER_OSS_ITEM_CSWITCH);
1005         if (err)
1006                 return err;
1007         sprintf(str, "%s Capture Route", ptr->name);
1008         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
1009                                        SNDRV_MIXER_OSS_ITEM_CROUTE);
1010         if (err)
1011                 return err;
1012         sprintf(str, "%s Capture Volume", ptr->name);
1013         err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
1014                                        SNDRV_MIXER_OSS_ITEM_CVOLUME);
1015         if (err)
1016                 return err;
1017
1018         return 0;
1019 }
1020
1021 /*
1022  * build an OSS mixer element.
1023  * ptr_allocated means the entry is dynamically allocated (change via proc file).
1024  * when replace_old = 1, the old entry is replaced with the new one.
1025  */
1026 static int snd_mixer_oss_build_input(struct snd_mixer_oss *mixer, struct snd_mixer_oss_assign_table *ptr, int ptr_allocated, int replace_old)
1027 {
1028         struct slot slot;
1029         struct slot *pslot;
1030         struct snd_kcontrol *kctl;
1031         struct snd_mixer_oss_slot *rslot;
1032         char str[64];   
1033         
1034         /* check if already assigned */
1035         if (mixer->slots[ptr->oss_id].get_volume && ! replace_old)
1036                 return 0;
1037
1038         memset(&slot, 0, sizeof(slot));
1039         memset(slot.numid, 0xff, sizeof(slot.numid)); /* ID_UNKNOWN */
1040         if (snd_mixer_oss_build_test_all(mixer, ptr, &slot))
1041                 return 0;
1042         down_read(&mixer->card->controls_rwsem);
1043         if (ptr->index == 0 && (kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0)) != NULL) {
1044                 struct snd_ctl_elem_info *uinfo;
1045
1046                 uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
1047                 if (! uinfo) {
1048                         up_read(&mixer->card->controls_rwsem);
1049                         return -ENOMEM;
1050                 }
1051                         
1052                 if (kctl->info(kctl, uinfo)) {
1053                         up_read(&mixer->card->controls_rwsem);
1054                         return 0;
1055                 }
1056                 strcpy(str, ptr->name);
1057                 if (!strcmp(str, "Master"))
1058                         strcpy(str, "Mix");
1059                 if (!strcmp(str, "Master Mono"))
1060                         strcpy(str, "Mix Mono");
1061                 slot.capture_item = 0;
1062                 if (!strcmp(uinfo->value.enumerated.name, str)) {
1063                         slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
1064                 } else {
1065                         for (slot.capture_item = 1; slot.capture_item < uinfo->value.enumerated.items; slot.capture_item++) {
1066                                 uinfo->value.enumerated.item = slot.capture_item;
1067                                 if (kctl->info(kctl, uinfo)) {
1068                                         up_read(&mixer->card->controls_rwsem);
1069                                         return 0;
1070                                 }
1071                                 if (!strcmp(uinfo->value.enumerated.name, str)) {
1072                                         slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
1073                                         break;
1074                                 }
1075                         }
1076                 }
1077                 kfree(uinfo);
1078         }
1079         up_read(&mixer->card->controls_rwsem);
1080         if (slot.present != 0) {
1081                 pslot = kmalloc(sizeof(slot), GFP_KERNEL);
1082                 if (! pslot)
1083                         return -ENOMEM;
1084                 *pslot = slot;
1085                 pslot->signature = SNDRV_MIXER_OSS_SIGNATURE;
1086                 pslot->assigned = ptr;
1087                 pslot->allocated = ptr_allocated;
1088                 rslot = &mixer->slots[ptr->oss_id];
1089                 mixer_slot_clear(rslot);
1090                 rslot->stereo = slot.channels > 1 ? 1 : 0;
1091                 rslot->get_volume = snd_mixer_oss_get_volume1;
1092                 rslot->put_volume = snd_mixer_oss_put_volume1;
1093                 /* note: ES18xx have both Capture Source and XX Capture Volume !!! */
1094                 if (slot.present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) {
1095                         rslot->get_recsrc = snd_mixer_oss_get_recsrc1_sw;
1096                         rslot->put_recsrc = snd_mixer_oss_put_recsrc1_sw;
1097                 } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CROUTE) {
1098                         rslot->get_recsrc = snd_mixer_oss_get_recsrc1_route;
1099                         rslot->put_recsrc = snd_mixer_oss_put_recsrc1_route;
1100                 } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CAPTURE) {
1101                         mixer->mask_recsrc |= 1 << ptr->oss_id;
1102                 }
1103                 rslot->private_data = pslot;
1104                 rslot->private_free = snd_mixer_oss_slot_free;
1105                 return 1;
1106         }
1107         return 0;
1108 }
1109
1110 #ifdef CONFIG_PROC_FS
1111 /*
1112  */
1113 #define MIXER_VOL(name) [SOUND_MIXER_##name] = #name
1114 static char *oss_mixer_names[SNDRV_OSS_MAX_MIXERS] = {
1115         MIXER_VOL(VOLUME),
1116         MIXER_VOL(BASS),
1117         MIXER_VOL(TREBLE),
1118         MIXER_VOL(SYNTH),
1119         MIXER_VOL(PCM),
1120         MIXER_VOL(SPEAKER),
1121         MIXER_VOL(LINE),
1122         MIXER_VOL(MIC),
1123         MIXER_VOL(CD),
1124         MIXER_VOL(IMIX),
1125         MIXER_VOL(ALTPCM),
1126         MIXER_VOL(RECLEV),
1127         MIXER_VOL(IGAIN),
1128         MIXER_VOL(OGAIN),
1129         MIXER_VOL(LINE1),
1130         MIXER_VOL(LINE2),
1131         MIXER_VOL(LINE3),
1132         MIXER_VOL(DIGITAL1),
1133         MIXER_VOL(DIGITAL2),
1134         MIXER_VOL(DIGITAL3),
1135         MIXER_VOL(PHONEIN),
1136         MIXER_VOL(PHONEOUT),
1137         MIXER_VOL(VIDEO),
1138         MIXER_VOL(RADIO),
1139         MIXER_VOL(MONITOR),
1140 };
1141         
1142 /*
1143  *  /proc interface
1144  */
1145
1146 static void snd_mixer_oss_proc_read(struct snd_info_entry *entry,
1147                                     struct snd_info_buffer *buffer)
1148 {
1149         struct snd_mixer_oss *mixer = entry->private_data;
1150         int i;
1151
1152         mutex_lock(&mixer->reg_mutex);
1153         for (i = 0; i < SNDRV_OSS_MAX_MIXERS; i++) {
1154                 struct slot *p;
1155
1156                 if (! oss_mixer_names[i])
1157                         continue;
1158                 p = (struct slot *)mixer->slots[i].private_data;
1159                 snd_iprintf(buffer, "%s ", oss_mixer_names[i]);
1160                 if (p && p->assigned)
1161                         snd_iprintf(buffer, "\"%s\" %d\n",
1162                                     p->assigned->name,
1163                                     p->assigned->index);
1164                 else
1165                         snd_iprintf(buffer, "\"\" 0\n");
1166         }
1167         mutex_unlock(&mixer->reg_mutex);
1168 }
1169
1170 static void snd_mixer_oss_proc_write(struct snd_info_entry *entry,
1171                                      struct snd_info_buffer *buffer)
1172 {
1173         struct snd_mixer_oss *mixer = entry->private_data;
1174         char line[128], str[32], idxstr[16];
1175         const char *cptr;
1176         int ch, idx;
1177         struct snd_mixer_oss_assign_table *tbl;
1178         struct slot *slot;
1179
1180         while (!snd_info_get_line(buffer, line, sizeof(line))) {
1181                 cptr = snd_info_get_str(str, line, sizeof(str));
1182                 for (ch = 0; ch < SNDRV_OSS_MAX_MIXERS; ch++)
1183                         if (oss_mixer_names[ch] && strcmp(oss_mixer_names[ch], str) == 0)
1184                                 break;
1185                 if (ch >= SNDRV_OSS_MAX_MIXERS) {
1186                         snd_printk(KERN_ERR "mixer_oss: invalid OSS volume '%s'\n", str);
1187                         continue;
1188                 }
1189                 cptr = snd_info_get_str(str, cptr, sizeof(str));
1190                 if (! *str) {
1191                         /* remove the entry */
1192                         mutex_lock(&mixer->reg_mutex);
1193                         mixer_slot_clear(&mixer->slots[ch]);
1194                         mutex_unlock(&mixer->reg_mutex);
1195                         continue;
1196                 }
1197                 snd_info_get_str(idxstr, cptr, sizeof(idxstr));
1198                 idx = simple_strtoul(idxstr, NULL, 10);
1199                 if (idx >= 0x4000) { /* too big */
1200                         snd_printk(KERN_ERR "mixer_oss: invalid index %d\n", idx);
1201                         continue;
1202                 }
1203                 mutex_lock(&mixer->reg_mutex);
1204                 slot = (struct slot *)mixer->slots[ch].private_data;
1205                 if (slot && slot->assigned &&
1206                     slot->assigned->index == idx && ! strcmp(slot->assigned->name, str))
1207                         /* not changed */
1208                         goto __unlock;
1209                 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
1210                 if (! tbl) {
1211                         snd_printk(KERN_ERR "mixer_oss: no memory\n");
1212                         goto __unlock;
1213                 }
1214                 tbl->oss_id = ch;
1215                 tbl->name = kstrdup(str, GFP_KERNEL);
1216                 if (! tbl->name) {
1217                         kfree(tbl);
1218                         goto __unlock;
1219                 }
1220                 tbl->index = idx;
1221                 if (snd_mixer_oss_build_input(mixer, tbl, 1, 1) <= 0) {
1222                         kfree(tbl->name);
1223                         kfree(tbl);
1224                 }
1225         __unlock:
1226                 mutex_unlock(&mixer->reg_mutex);
1227         }
1228 }
1229
1230 static void snd_mixer_oss_proc_init(struct snd_mixer_oss *mixer)
1231 {
1232         struct snd_info_entry *entry;
1233
1234         entry = snd_info_create_card_entry(mixer->card, "oss_mixer",
1235                                            mixer->card->proc_root);
1236         if (! entry)
1237                 return;
1238         entry->content = SNDRV_INFO_CONTENT_TEXT;
1239         entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
1240         entry->c.text.read = snd_mixer_oss_proc_read;
1241         entry->c.text.write = snd_mixer_oss_proc_write;
1242         entry->private_data = mixer;
1243         if (snd_info_register(entry) < 0) {
1244                 snd_info_free_entry(entry);
1245                 entry = NULL;
1246         }
1247         mixer->proc_entry = entry;
1248 }
1249
1250 static void snd_mixer_oss_proc_done(struct snd_mixer_oss *mixer)
1251 {
1252         snd_info_free_entry(mixer->proc_entry);
1253         mixer->proc_entry = NULL;
1254 }
1255 #else /* !CONFIG_PROC_FS */
1256 #define snd_mixer_oss_proc_init(mix)
1257 #define snd_mixer_oss_proc_done(mix)
1258 #endif /* CONFIG_PROC_FS */
1259
1260 static void snd_mixer_oss_build(struct snd_mixer_oss *mixer)
1261 {
1262         static struct snd_mixer_oss_assign_table table[] = {
1263                 { SOUND_MIXER_VOLUME,   "Master",               0 },
1264                 { SOUND_MIXER_VOLUME,   "Front",                0 }, /* fallback */
1265                 { SOUND_MIXER_BASS,     "Tone Control - Bass",  0 },
1266                 { SOUND_MIXER_TREBLE,   "Tone Control - Treble", 0 },
1267                 { SOUND_MIXER_SYNTH,    "Synth",                0 },
1268                 { SOUND_MIXER_SYNTH,    "FM",                   0 }, /* fallback */
1269                 { SOUND_MIXER_SYNTH,    "Music",                0 }, /* fallback */
1270                 { SOUND_MIXER_PCM,      "PCM",                  0 },
1271                 { SOUND_MIXER_SPEAKER,  "Beep",                 0 },
1272                 { SOUND_MIXER_SPEAKER,  "PC Speaker",           0 }, /* fallback */
1273                 { SOUND_MIXER_SPEAKER,  "Speaker",              0 }, /* fallback */
1274                 { SOUND_MIXER_LINE,     "Line",                 0 },
1275                 { SOUND_MIXER_MIC,      "Mic",                  0 },
1276                 { SOUND_MIXER_CD,       "CD",                   0 },
1277                 { SOUND_MIXER_IMIX,     "Monitor Mix",          0 },
1278                 { SOUND_MIXER_ALTPCM,   "PCM",                  1 },
1279                 { SOUND_MIXER_ALTPCM,   "Headphone",            0 }, /* fallback */
1280                 { SOUND_MIXER_ALTPCM,   "Wave",                 0 }, /* fallback */
1281                 { SOUND_MIXER_RECLEV,   "-- nothing --",        0 },
1282                 { SOUND_MIXER_IGAIN,    "Capture",              0 },
1283                 { SOUND_MIXER_OGAIN,    "Playback",             0 },
1284                 { SOUND_MIXER_LINE1,    "Aux",                  0 },
1285                 { SOUND_MIXER_LINE2,    "Aux",                  1 },
1286                 { SOUND_MIXER_LINE3,    "Aux",                  2 },
1287                 { SOUND_MIXER_DIGITAL1, "Digital",              0 },
1288                 { SOUND_MIXER_DIGITAL1, "IEC958",               0 }, /* fallback */
1289                 { SOUND_MIXER_DIGITAL1, "IEC958 Optical",       0 }, /* fallback */
1290                 { SOUND_MIXER_DIGITAL1, "IEC958 Coaxial",       0 }, /* fallback */
1291                 { SOUND_MIXER_DIGITAL2, "Digital",              1 },
1292                 { SOUND_MIXER_DIGITAL3, "Digital",              2 },
1293                 { SOUND_MIXER_PHONEIN,  "Phone",                0 },
1294                 { SOUND_MIXER_PHONEOUT, "Master Mono",          0 },
1295                 { SOUND_MIXER_PHONEOUT, "Speaker",              0 }, /*fallback*/
1296                 { SOUND_MIXER_PHONEOUT, "Mono",                 0 }, /*fallback*/
1297                 { SOUND_MIXER_PHONEOUT, "Phone",                0 }, /* fallback */
1298                 { SOUND_MIXER_VIDEO,    "Video",                0 },
1299                 { SOUND_MIXER_RADIO,    "Radio",                0 },
1300                 { SOUND_MIXER_MONITOR,  "Monitor",              0 }
1301         };
1302         unsigned int idx;
1303         
1304         for (idx = 0; idx < ARRAY_SIZE(table); idx++)
1305                 snd_mixer_oss_build_input(mixer, &table[idx], 0, 0);
1306         if (mixer->mask_recsrc) {
1307                 mixer->get_recsrc = snd_mixer_oss_get_recsrc2;
1308                 mixer->put_recsrc = snd_mixer_oss_put_recsrc2;
1309         }
1310 }
1311
1312 /*
1313  *
1314  */
1315
1316 static int snd_mixer_oss_free1(void *private)
1317 {
1318         struct snd_mixer_oss *mixer = private;
1319         struct snd_card *card;
1320         int idx;
1321  
1322         if (!mixer)
1323                 return 0;
1324         card = mixer->card;
1325         if (snd_BUG_ON(mixer != card->mixer_oss))
1326                 return -ENXIO;
1327         card->mixer_oss = NULL;
1328         for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++) {
1329                 struct snd_mixer_oss_slot *chn = &mixer->slots[idx];
1330                 if (chn->private_free)
1331                         chn->private_free(chn);
1332         }
1333         kfree(mixer);
1334         return 0;
1335 }
1336
1337 static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd)
1338 {
1339         struct snd_mixer_oss *mixer;
1340
1341         if (cmd == SND_MIXER_OSS_NOTIFY_REGISTER) {
1342                 char name[128];
1343                 int idx, err;
1344
1345                 mixer = kcalloc(2, sizeof(*mixer), GFP_KERNEL);
1346                 if (mixer == NULL)
1347                         return -ENOMEM;
1348                 mutex_init(&mixer->reg_mutex);
1349                 sprintf(name, "mixer%i%i", card->number, 0);
1350                 if ((err = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER,
1351                                                    card, 0,
1352                                                    &snd_mixer_oss_f_ops, card,
1353                                                    name)) < 0) {
1354                         snd_printk(KERN_ERR "unable to register OSS mixer device %i:%i\n",
1355                                    card->number, 0);
1356                         kfree(mixer);
1357                         return err;
1358                 }
1359                 mixer->oss_dev_alloc = 1;
1360                 mixer->card = card;
1361                 if (*card->mixername)
1362                         strlcpy(mixer->name, card->mixername, sizeof(mixer->name));
1363                 else
1364                         strlcpy(mixer->name, name, sizeof(mixer->name));
1365 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
1366                 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIXERS,
1367                                       card->number,
1368                                       mixer->name);
1369 #endif
1370                 for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++)
1371                         mixer->slots[idx].number = idx;
1372                 card->mixer_oss = mixer;
1373                 snd_mixer_oss_build(mixer);
1374                 snd_mixer_oss_proc_init(mixer);
1375         } else {
1376                 mixer = card->mixer_oss;
1377                 if (mixer == NULL)
1378                         return 0;
1379                 if (mixer->oss_dev_alloc) {
1380 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
1381                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
1382 #endif
1383                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
1384                         mixer->oss_dev_alloc = 0;
1385                 }
1386                 if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
1387                         return 0;
1388                 snd_mixer_oss_proc_done(mixer);
1389                 return snd_mixer_oss_free1(mixer);
1390         }
1391         return 0;
1392 }
1393
1394 static int __init alsa_mixer_oss_init(void)
1395 {
1396         int idx;
1397         
1398         snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler;
1399         for (idx = 0; idx < SNDRV_CARDS; idx++) {
1400                 if (snd_cards[idx])
1401                         snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_REGISTER);
1402         }
1403         return 0;
1404 }
1405
1406 static void __exit alsa_mixer_oss_exit(void)
1407 {
1408         int idx;
1409
1410         snd_mixer_oss_notify_callback = NULL;
1411         for (idx = 0; idx < SNDRV_CARDS; idx++) {
1412                 if (snd_cards[idx])
1413                         snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_FREE);
1414         }
1415 }
1416
1417 module_init(alsa_mixer_oss_init)
1418 module_exit(alsa_mixer_oss_exit)
1419
1420 EXPORT_SYMBOL(snd_mixer_oss_ioctl_card);