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