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