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