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