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