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