• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  OSS emulation layer for the mixer interface
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/string.h>
26 #include <sound/core.h>
27 #include <sound/minors.h>
28 #include <sound/control.h>
29 #include <sound/info.h>
30 #include <sound/mixer_oss.h>
31 #include <linux/soundcard.h>
32 
33 #define OSS_ALSAEMULVER         _SIOR ('M', 249, int)
34 
35 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
36 MODULE_DESCRIPTION("Mixer OSS emulation for ALSA.");
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MIXER);
39 
snd_mixer_oss_open(struct inode * inode,struct file * file)40 static int snd_mixer_oss_open(struct inode *inode, struct file *file)
41 {
42 	struct snd_card *card;
43 	struct snd_mixer_oss_file *fmixer;
44 	int err;
45 
46 	card = snd_lookup_oss_minor_data(iminor(inode),
47 					 SNDRV_OSS_DEVICE_TYPE_MIXER);
48 	if (card == NULL)
49 		return -ENODEV;
50 	if (card->mixer_oss == NULL)
51 		return -ENODEV;
52 	err = snd_card_file_add(card, file);
53 	if (err < 0)
54 		return err;
55 	fmixer = kzalloc(sizeof(*fmixer), GFP_KERNEL);
56 	if (fmixer == NULL) {
57 		snd_card_file_remove(card, file);
58 		return -ENOMEM;
59 	}
60 	fmixer->card = card;
61 	fmixer->mixer = card->mixer_oss;
62 	file->private_data = fmixer;
63 	if (!try_module_get(card->module)) {
64 		kfree(fmixer);
65 		snd_card_file_remove(card, file);
66 		return -EFAULT;
67 	}
68 	return 0;
69 }
70 
snd_mixer_oss_release(struct inode * inode,struct file * file)71 static int snd_mixer_oss_release(struct inode *inode, struct file *file)
72 {
73 	struct snd_mixer_oss_file *fmixer;
74 
75 	if (file->private_data) {
76 		fmixer = (struct snd_mixer_oss_file *) file->private_data;
77 		module_put(fmixer->card->module);
78 		snd_card_file_remove(fmixer->card, file);
79 		kfree(fmixer);
80 	}
81 	return 0;
82 }
83 
snd_mixer_oss_info(struct snd_mixer_oss_file * fmixer,mixer_info __user * _info)84 static int snd_mixer_oss_info(struct snd_mixer_oss_file *fmixer,
85 			      mixer_info __user *_info)
86 {
87 	struct snd_card *card = fmixer->card;
88 	struct snd_mixer_oss *mixer = fmixer->mixer;
89 	struct mixer_info info;
90 
91 	memset(&info, 0, sizeof(info));
92 	strlcpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id));
93 	strlcpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name));
94 	info.modify_counter = card->mixer_oss_change_count;
95 	if (copy_to_user(_info, &info, sizeof(info)))
96 		return -EFAULT;
97 	return 0;
98 }
99 
snd_mixer_oss_info_obsolete(struct snd_mixer_oss_file * fmixer,_old_mixer_info __user * _info)100 static int snd_mixer_oss_info_obsolete(struct snd_mixer_oss_file *fmixer,
101 				       _old_mixer_info __user *_info)
102 {
103 	struct snd_card *card = fmixer->card;
104 	struct snd_mixer_oss *mixer = fmixer->mixer;
105 	_old_mixer_info info;
106 
107 	memset(&info, 0, sizeof(info));
108 	strlcpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id));
109 	strlcpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name));
110 	if (copy_to_user(_info, &info, sizeof(info)))
111 		return -EFAULT;
112 	return 0;
113 }
114 
snd_mixer_oss_caps(struct snd_mixer_oss_file * fmixer)115 static int snd_mixer_oss_caps(struct snd_mixer_oss_file *fmixer)
116 {
117 	struct snd_mixer_oss *mixer = fmixer->mixer;
118 	int result = 0;
119 
120 	if (mixer == NULL)
121 		return -EIO;
122 	if (mixer->get_recsrc && mixer->put_recsrc)
123 		result |= SOUND_CAP_EXCL_INPUT;
124 	return result;
125 }
126 
snd_mixer_oss_devmask(struct snd_mixer_oss_file * fmixer)127 static int snd_mixer_oss_devmask(struct snd_mixer_oss_file *fmixer)
128 {
129 	struct snd_mixer_oss *mixer = fmixer->mixer;
130 	struct snd_mixer_oss_slot *pslot;
131 	int result = 0, chn;
132 
133 	if (mixer == NULL)
134 		return -EIO;
135 	for (chn = 0; chn < 31; chn++) {
136 		pslot = &mixer->slots[chn];
137 		if (pslot->put_volume || pslot->put_recsrc)
138 			result |= 1 << chn;
139 	}
140 	return result;
141 }
142 
snd_mixer_oss_stereodevs(struct snd_mixer_oss_file * fmixer)143 static int snd_mixer_oss_stereodevs(struct snd_mixer_oss_file *fmixer)
144 {
145 	struct snd_mixer_oss *mixer = fmixer->mixer;
146 	struct snd_mixer_oss_slot *pslot;
147 	int result = 0, chn;
148 
149 	if (mixer == NULL)
150 		return -EIO;
151 	for (chn = 0; chn < 31; chn++) {
152 		pslot = &mixer->slots[chn];
153 		if (pslot->put_volume && pslot->stereo)
154 			result |= 1 << chn;
155 	}
156 	return result;
157 }
158 
snd_mixer_oss_recmask(struct snd_mixer_oss_file * fmixer)159 static int snd_mixer_oss_recmask(struct snd_mixer_oss_file *fmixer)
160 {
161 	struct snd_mixer_oss *mixer = fmixer->mixer;
162 	int result = 0;
163 
164 	if (mixer == NULL)
165 		return -EIO;
166 	if (mixer->put_recsrc && mixer->get_recsrc) {	/* exclusive */
167 		result = mixer->mask_recsrc;
168 	} else {
169 		struct snd_mixer_oss_slot *pslot;
170 		int chn;
171 		for (chn = 0; chn < 31; chn++) {
172 			pslot = &mixer->slots[chn];
173 			if (pslot->put_recsrc)
174 				result |= 1 << chn;
175 		}
176 	}
177 	return result;
178 }
179 
snd_mixer_oss_get_recsrc(struct snd_mixer_oss_file * fmixer)180 static int snd_mixer_oss_get_recsrc(struct snd_mixer_oss_file *fmixer)
181 {
182 	struct snd_mixer_oss *mixer = fmixer->mixer;
183 	int result = 0;
184 
185 	if (mixer == NULL)
186 		return -EIO;
187 	if (mixer->put_recsrc && mixer->get_recsrc) {	/* exclusive */
188 		int err;
189 		if ((err = mixer->get_recsrc(fmixer, &result)) < 0)
190 			return err;
191 		result = 1 << result;
192 	} else {
193 		struct snd_mixer_oss_slot *pslot;
194 		int chn;
195 		for (chn = 0; chn < 31; chn++) {
196 			pslot = &mixer->slots[chn];
197 			if (pslot->get_recsrc) {
198 				int active = 0;
199 				pslot->get_recsrc(fmixer, pslot, &active);
200 				if (active)
201 					result |= 1 << chn;
202 			}
203 		}
204 	}
205 	return mixer->oss_recsrc = result;
206 }
207 
snd_mixer_oss_set_recsrc(struct snd_mixer_oss_file * fmixer,int recsrc)208 static int snd_mixer_oss_set_recsrc(struct snd_mixer_oss_file *fmixer, int recsrc)
209 {
210 	struct snd_mixer_oss *mixer = fmixer->mixer;
211 	struct snd_mixer_oss_slot *pslot;
212 	int chn, active;
213 	int result = 0;
214 
215 	if (mixer == NULL)
216 		return -EIO;
217 	if (mixer->get_recsrc && mixer->put_recsrc) {	/* exclusive input */
218 		if (recsrc & ~mixer->oss_recsrc)
219 			recsrc &= ~mixer->oss_recsrc;
220 		mixer->put_recsrc(fmixer, ffz(~recsrc));
221 		mixer->get_recsrc(fmixer, &result);
222 		result = 1 << result;
223 	}
224 	for (chn = 0; chn < 31; chn++) {
225 		pslot = &mixer->slots[chn];
226 		if (pslot->put_recsrc) {
227 			active = (recsrc & (1 << chn)) ? 1 : 0;
228 			pslot->put_recsrc(fmixer, pslot, active);
229 		}
230 	}
231 	if (! result) {
232 		for (chn = 0; chn < 31; chn++) {
233 			pslot = &mixer->slots[chn];
234 			if (pslot->get_recsrc) {
235 				active = 0;
236 				pslot->get_recsrc(fmixer, pslot, &active);
237 				if (active)
238 					result |= 1 << chn;
239 			}
240 		}
241 	}
242 	return result;
243 }
244 
snd_mixer_oss_get_volume(struct snd_mixer_oss_file * fmixer,int slot)245 static int snd_mixer_oss_get_volume(struct snd_mixer_oss_file *fmixer, int slot)
246 {
247 	struct snd_mixer_oss *mixer = fmixer->mixer;
248 	struct snd_mixer_oss_slot *pslot;
249 	int result = 0, left, right;
250 
251 	if (mixer == NULL || slot > 30)
252 		return -EIO;
253 	pslot = &mixer->slots[slot];
254 	left = pslot->volume[0];
255 	right = pslot->volume[1];
256 	if (pslot->get_volume)
257 		result = pslot->get_volume(fmixer, pslot, &left, &right);
258 	if (!pslot->stereo)
259 		right = left;
260 	if (snd_BUG_ON(left < 0 || left > 100))
261 		return -EIO;
262 	if (snd_BUG_ON(right < 0 || right > 100))
263 		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 
snd_mixer_oss_set_volume(struct snd_mixer_oss_file * fmixer,int slot,int volume)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 
snd_mixer_oss_ioctl1(struct snd_mixer_oss_file * fmixer,unsigned int cmd,unsigned long arg)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 	if (snd_BUG_ON(!fmixer))
304 		return -ENXIO;
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 
snd_mixer_oss_ioctl(struct file * file,unsigned int cmd,unsigned long arg)365 static long snd_mixer_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
366 {
367 	return snd_mixer_oss_ioctl1((struct snd_mixer_oss_file *) file->private_data, cmd, arg);
368 }
369 
snd_mixer_oss_ioctl_card(struct snd_card * card,unsigned int cmd,unsigned long arg)370 int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg)
371 {
372 	struct snd_mixer_oss_file fmixer;
373 
374 	if (snd_BUG_ON(!card))
375 		return -ENXIO;
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 
395 static const struct file_operations snd_mixer_oss_f_ops =
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 
404 /*
405  *  utilities
406  */
407 
snd_mixer_oss_conv(long val,long omin,long omax,long nmin,long nmax)408 static 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) */
snd_mixer_oss_conv1(long val,long min,long max,int * old)418 static 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 */
snd_mixer_oss_conv2(long val,long min,long max)426 static 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
432 static void snd_mixer_oss_recsrce_set(struct snd_card *card, int slot)
433 {
434 	struct snd_mixer_oss *mixer = card->mixer_oss;
435 	if (mixer)
436 		mixer->mask_recsrc |= 1 << slot;
437 }
438 
439 static int snd_mixer_oss_recsrce_get(struct snd_card *card, int slot)
440 {
441 	struct snd_mixer_oss *mixer = card->mixer_oss;
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 
476 struct 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 
snd_mixer_oss_test_id(struct snd_mixer_oss * mixer,const char * name,int index)488 static struct snd_kcontrol *snd_mixer_oss_test_id(struct snd_mixer_oss *mixer, const char *name, int index)
489 {
490 	struct snd_card *card = mixer->card;
491 	struct snd_ctl_elem_id id;
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 
snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,unsigned int numid,int * left,int * right)500 static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,
501 					  struct snd_mixer_oss_slot *pslot,
502 					  unsigned int numid,
503 					  int *left, int *right)
504 {
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;
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 	}
517 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
518 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
519 	if (uinfo == NULL || uctl == NULL)
520 		goto __unalloc;
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;
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 
snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,unsigned int numid,int * left,int * right,int route)537 static void snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file *fmixer,
538 					 struct snd_mixer_oss_slot *pslot,
539 					 unsigned int numid,
540 					 int *left, int *right,
541 					 int route)
542 {
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;
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 	}
555 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
556 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
557 	if (uinfo == NULL || uctl == NULL)
558 		goto __unalloc;
559 	if (kctl->info(kctl, uinfo))
560 		goto __unalloc;
561 	if (kctl->get(kctl, uctl))
562 		goto __unalloc;
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 
snd_mixer_oss_get_volume1(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,int * left,int * right)576 static int snd_mixer_oss_get_volume1(struct snd_mixer_oss_file *fmixer,
577 				     struct snd_mixer_oss_slot *pslot,
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 
snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,unsigned int numid,int left,int right)602 static void snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file *fmixer,
603 					  struct snd_mixer_oss_slot *pslot,
604 					  unsigned int numid,
605 					  int left, int right)
606 {
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;
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;
618 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
619 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
620 	if (uinfo == NULL || uctl == NULL)
621 		goto __unalloc;
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;
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);
630 	if ((res = kctl->put(kctl, uctl)) < 0)
631 		goto __unalloc;
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 
snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,unsigned int numid,int left,int right,int route)640 static void snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file *fmixer,
641 					 struct snd_mixer_oss_slot *pslot,
642 					 unsigned int numid,
643 					 int left, int right,
644 					 int route)
645 {
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;
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 	}
659 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
660 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
661 	if (uinfo == NULL || uctl == NULL)
662 		goto __unalloc;
663 	if (kctl->info(kctl, uinfo))
664 		goto __unalloc;
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 	}
675 	if ((res = kctl->put(kctl, uctl)) < 0)
676 		goto __unalloc;
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 
snd_mixer_oss_put_volume1(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,int left,int right)685 static int snd_mixer_oss_put_volume1(struct snd_mixer_oss_file *fmixer,
686 				     struct snd_mixer_oss_slot *pslot,
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);
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);
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);
706 		if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH)
707 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
708 		if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE)
709 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
710 		if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE)
711 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
712 	} else {
713 		if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
714 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
715 		} else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
716 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
717 		} else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
718 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
719 		} else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
720 			snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
721 		}
722 	}
723 	return 0;
724 }
725 
snd_mixer_oss_get_recsrc1_sw(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,int * active)726 static int snd_mixer_oss_get_recsrc1_sw(struct snd_mixer_oss_file *fmixer,
727 					struct snd_mixer_oss_slot *pslot,
728 					int *active)
729 {
730 	struct slot *slot = (struct slot *)pslot->private_data;
731 	int left, right;
732 
733 	left = right = 1;
734 	snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], &left, &right, 0);
735 	*active = (left || right) ? 1 : 0;
736 	return 0;
737 }
738 
snd_mixer_oss_get_recsrc1_route(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,int * active)739 static int snd_mixer_oss_get_recsrc1_route(struct snd_mixer_oss_file *fmixer,
740 					   struct snd_mixer_oss_slot *pslot,
741 					   int *active)
742 {
743 	struct slot *slot = (struct slot *)pslot->private_data;
744 	int left, right;
745 
746 	left = right = 1;
747 	snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], &left, &right, 1);
748 	*active = (left || right) ? 1 : 0;
749 	return 0;
750 }
751 
snd_mixer_oss_put_recsrc1_sw(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,int active)752 static int snd_mixer_oss_put_recsrc1_sw(struct snd_mixer_oss_file *fmixer,
753 					struct snd_mixer_oss_slot *pslot,
754 					int active)
755 {
756 	struct slot *slot = (struct slot *)pslot->private_data;
757 
758 	snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], active, active, 0);
759 	return 0;
760 }
761 
snd_mixer_oss_put_recsrc1_route(struct snd_mixer_oss_file * fmixer,struct snd_mixer_oss_slot * pslot,int active)762 static int snd_mixer_oss_put_recsrc1_route(struct snd_mixer_oss_file *fmixer,
763 					   struct snd_mixer_oss_slot *pslot,
764 					   int active)
765 {
766 	struct slot *slot = (struct slot *)pslot->private_data;
767 
768 	snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], active, active, 1);
769 	return 0;
770 }
771 
snd_mixer_oss_get_recsrc2(struct snd_mixer_oss_file * fmixer,unsigned int * active_index)772 static int snd_mixer_oss_get_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int *active_index)
773 {
774 	struct snd_card *card = fmixer->card;
775 	struct snd_mixer_oss *mixer = fmixer->mixer;
776 	struct snd_kcontrol *kctl;
777 	struct snd_mixer_oss_slot *pslot;
778 	struct slot *slot;
779 	struct snd_ctl_elem_info *uinfo;
780 	struct snd_ctl_elem_value *uctl;
781 	int err, idx;
782 
783 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
784 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
785 	if (uinfo == NULL || uctl == NULL) {
786 		err = -ENOMEM;
787 		goto __unlock;
788 	}
789 	down_read(&card->controls_rwsem);
790 	kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
791 	if (! kctl) {
792 		err = -ENOENT;
793 		goto __unlock;
794 	}
795 	if ((err = kctl->info(kctl, uinfo)) < 0)
796 		goto __unlock;
797 	if ((err = kctl->get(kctl, uctl)) < 0)
798 		goto __unlock;
799 	for (idx = 0; idx < 32; idx++) {
800 		if (!(mixer->mask_recsrc & (1 << idx)))
801 			continue;
802 		pslot = &mixer->slots[idx];
803 		slot = (struct slot *)pslot->private_data;
804 		if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
805 			continue;
806 		if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
807 			continue;
808 		if (slot->capture_item == uctl->value.enumerated.item[0]) {
809 			*active_index = idx;
810 			break;
811 		}
812 	}
813 	err = 0;
814       __unlock:
815      	up_read(&card->controls_rwsem);
816       	kfree(uctl);
817       	kfree(uinfo);
818       	return err;
819 }
820 
snd_mixer_oss_put_recsrc2(struct snd_mixer_oss_file * fmixer,unsigned int active_index)821 static int snd_mixer_oss_put_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int active_index)
822 {
823 	struct snd_card *card = fmixer->card;
824 	struct snd_mixer_oss *mixer = fmixer->mixer;
825 	struct snd_kcontrol *kctl;
826 	struct snd_mixer_oss_slot *pslot;
827 	struct slot *slot = NULL;
828 	struct snd_ctl_elem_info *uinfo;
829 	struct snd_ctl_elem_value *uctl;
830 	int err;
831 	unsigned int idx;
832 
833 	uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
834 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
835 	if (uinfo == NULL || uctl == NULL) {
836 		err = -ENOMEM;
837 		goto __unlock;
838 	}
839 	down_read(&card->controls_rwsem);
840 	kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
841 	if (! kctl) {
842 		err = -ENOENT;
843 		goto __unlock;
844 	}
845 	if ((err = kctl->info(kctl, uinfo)) < 0)
846 		goto __unlock;
847 	for (idx = 0; idx < 32; idx++) {
848 		if (!(mixer->mask_recsrc & (1 << idx)))
849 			continue;
850 		pslot = &mixer->slots[idx];
851 		slot = (struct slot *)pslot->private_data;
852 		if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
853 			continue;
854 		if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
855 			continue;
856 		if (idx == active_index)
857 			break;
858 		slot = NULL;
859 	}
860 	if (! slot)
861 		goto __unlock;
862 	for (idx = 0; idx < uinfo->count; idx++)
863 		uctl->value.enumerated.item[idx] = slot->capture_item;
864 	err = kctl->put(kctl, uctl);
865 	if (err > 0)
866 		snd_ctl_notify(fmixer->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
867 	err = 0;
868       __unlock:
869 	up_read(&card->controls_rwsem);
870 	kfree(uctl);
871 	kfree(uinfo);
872 	return err;
873 }
874 
875 struct snd_mixer_oss_assign_table {
876 	int oss_id;
877 	const char *name;
878 	int index;
879 };
880 
snd_mixer_oss_build_test(struct snd_mixer_oss * mixer,struct slot * slot,const char * name,int index,int item)881 static int snd_mixer_oss_build_test(struct snd_mixer_oss *mixer, struct slot *slot, const char *name, int index, int item)
882 {
883 	struct snd_ctl_elem_info *info;
884 	struct snd_kcontrol *kcontrol;
885 	struct snd_card *card = mixer->card;
886 	int err;
887 
888 	down_read(&card->controls_rwsem);
889 	kcontrol = snd_mixer_oss_test_id(mixer, name, index);
890 	if (kcontrol == NULL) {
891 		up_read(&card->controls_rwsem);
892 		return 0;
893 	}
894 	info = kmalloc(sizeof(*info), GFP_KERNEL);
895 	if (! info) {
896 		up_read(&card->controls_rwsem);
897 		return -ENOMEM;
898 	}
899 	if ((err = kcontrol->info(kcontrol, info)) < 0) {
900 		up_read(&card->controls_rwsem);
901 		kfree(info);
902 		return err;
903 	}
904 	slot->numid[item] = kcontrol->id.numid;
905 	up_read(&card->controls_rwsem);
906 	if (info->count > slot->channels)
907 		slot->channels = info->count;
908 	slot->present |= 1 << item;
909 	kfree(info);
910 	return 0;
911 }
912 
snd_mixer_oss_slot_free(struct snd_mixer_oss_slot * chn)913 static void snd_mixer_oss_slot_free(struct snd_mixer_oss_slot *chn)
914 {
915 	struct slot *p = (struct slot *)chn->private_data;
916 	if (p) {
917 		if (p->allocated && p->assigned) {
918 			kfree(p->assigned->name);
919 			kfree(p->assigned);
920 		}
921 		kfree(p);
922 	}
923 }
924 
mixer_slot_clear(struct snd_mixer_oss_slot * rslot)925 static void mixer_slot_clear(struct snd_mixer_oss_slot *rslot)
926 {
927 	int idx = rslot->number; /* remember this */
928 	if (rslot->private_free)
929 		rslot->private_free(rslot);
930 	memset(rslot, 0, sizeof(*rslot));
931 	rslot->number = idx;
932 }
933 
934 /* In a separate function to keep gcc 3.2 happy - do NOT merge this in
935    snd_mixer_oss_build_input! */
snd_mixer_oss_build_test_all(struct snd_mixer_oss * mixer,struct snd_mixer_oss_assign_table * ptr,struct slot * slot)936 static int snd_mixer_oss_build_test_all(struct snd_mixer_oss *mixer,
937 					struct snd_mixer_oss_assign_table *ptr,
938 					struct slot *slot)
939 {
940 	char str[64];
941 	int err;
942 
943 	err = snd_mixer_oss_build_test(mixer, slot, ptr->name, ptr->index,
944 				       SNDRV_MIXER_OSS_ITEM_GLOBAL);
945 	if (err)
946 		return err;
947 	sprintf(str, "%s Switch", ptr->name);
948 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
949 				       SNDRV_MIXER_OSS_ITEM_GSWITCH);
950 	if (err)
951 		return err;
952 	sprintf(str, "%s Route", ptr->name);
953 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
954 				       SNDRV_MIXER_OSS_ITEM_GROUTE);
955 	if (err)
956 		return err;
957 	sprintf(str, "%s Volume", ptr->name);
958 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
959 				       SNDRV_MIXER_OSS_ITEM_GVOLUME);
960 	if (err)
961 		return err;
962 	sprintf(str, "%s Playback Switch", ptr->name);
963 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
964 				       SNDRV_MIXER_OSS_ITEM_PSWITCH);
965 	if (err)
966 		return err;
967 	sprintf(str, "%s Playback Route", ptr->name);
968 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
969 				       SNDRV_MIXER_OSS_ITEM_PROUTE);
970 	if (err)
971 		return err;
972 	sprintf(str, "%s Playback Volume", ptr->name);
973 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
974 				       SNDRV_MIXER_OSS_ITEM_PVOLUME);
975 	if (err)
976 		return err;
977 	sprintf(str, "%s Capture Switch", ptr->name);
978 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
979 				       SNDRV_MIXER_OSS_ITEM_CSWITCH);
980 	if (err)
981 		return err;
982 	sprintf(str, "%s Capture Route", ptr->name);
983 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
984 				       SNDRV_MIXER_OSS_ITEM_CROUTE);
985 	if (err)
986 		return err;
987 	sprintf(str, "%s Capture Volume", ptr->name);
988 	err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
989 				       SNDRV_MIXER_OSS_ITEM_CVOLUME);
990 	if (err)
991 		return err;
992 
993 	return 0;
994 }
995 
996 /*
997  * build an OSS mixer element.
998  * ptr_allocated means the entry is dynamically allocated (change via proc file).
999  * when replace_old = 1, the old entry is replaced with the new one.
1000  */
snd_mixer_oss_build_input(struct snd_mixer_oss * mixer,struct snd_mixer_oss_assign_table * ptr,int ptr_allocated,int replace_old)1001 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)
1002 {
1003 	struct slot slot;
1004 	struct slot *pslot;
1005 	struct snd_kcontrol *kctl;
1006 	struct snd_mixer_oss_slot *rslot;
1007 	char str[64];
1008 
1009 	/* check if already assigned */
1010 	if (mixer->slots[ptr->oss_id].get_volume && ! replace_old)
1011 		return 0;
1012 
1013 	memset(&slot, 0, sizeof(slot));
1014 	memset(slot.numid, 0xff, sizeof(slot.numid)); /* ID_UNKNOWN */
1015 	if (snd_mixer_oss_build_test_all(mixer, ptr, &slot))
1016 		return 0;
1017 	down_read(&mixer->card->controls_rwsem);
1018 	if (ptr->index == 0 && (kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0)) != NULL) {
1019 		struct snd_ctl_elem_info *uinfo;
1020 
1021 		uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
1022 		if (! uinfo) {
1023 			up_read(&mixer->card->controls_rwsem);
1024 			return -ENOMEM;
1025 		}
1026 
1027 		if (kctl->info(kctl, uinfo)) {
1028 			up_read(&mixer->card->controls_rwsem);
1029 			return 0;
1030 		}
1031 		strcpy(str, ptr->name);
1032 		if (!strcmp(str, "Master"))
1033 			strcpy(str, "Mix");
1034 		if (!strcmp(str, "Master Mono"))
1035 			strcpy(str, "Mix Mono");
1036 		slot.capture_item = 0;
1037 		if (!strcmp(uinfo->value.enumerated.name, str)) {
1038 			slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
1039 		} else {
1040 			for (slot.capture_item = 1; slot.capture_item < uinfo->value.enumerated.items; slot.capture_item++) {
1041 				uinfo->value.enumerated.item = slot.capture_item;
1042 				if (kctl->info(kctl, uinfo)) {
1043 					up_read(&mixer->card->controls_rwsem);
1044 					return 0;
1045 				}
1046 				if (!strcmp(uinfo->value.enumerated.name, str)) {
1047 					slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
1048 					break;
1049 				}
1050 			}
1051 		}
1052 		kfree(uinfo);
1053 	}
1054 	up_read(&mixer->card->controls_rwsem);
1055 	if (slot.present != 0) {
1056 		pslot = kmalloc(sizeof(slot), GFP_KERNEL);
1057 		if (! pslot)
1058 			return -ENOMEM;
1059 		*pslot = slot;
1060 		pslot->signature = SNDRV_MIXER_OSS_SIGNATURE;
1061 		pslot->assigned = ptr;
1062 		pslot->allocated = ptr_allocated;
1063 		rslot = &mixer->slots[ptr->oss_id];
1064 		mixer_slot_clear(rslot);
1065 		rslot->stereo = slot.channels > 1 ? 1 : 0;
1066 		rslot->get_volume = snd_mixer_oss_get_volume1;
1067 		rslot->put_volume = snd_mixer_oss_put_volume1;
1068 		/* note: ES18xx have both Capture Source and XX Capture Volume !!! */
1069 		if (slot.present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) {
1070 			rslot->get_recsrc = snd_mixer_oss_get_recsrc1_sw;
1071 			rslot->put_recsrc = snd_mixer_oss_put_recsrc1_sw;
1072 		} else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CROUTE) {
1073 			rslot->get_recsrc = snd_mixer_oss_get_recsrc1_route;
1074 			rslot->put_recsrc = snd_mixer_oss_put_recsrc1_route;
1075 		} else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CAPTURE) {
1076 			mixer->mask_recsrc |= 1 << ptr->oss_id;
1077 		}
1078 		rslot->private_data = pslot;
1079 		rslot->private_free = snd_mixer_oss_slot_free;
1080 		return 1;
1081 	}
1082 	return 0;
1083 }
1084 
1085 #ifdef CONFIG_PROC_FS
1086 /*
1087  */
1088 #define MIXER_VOL(name) [SOUND_MIXER_##name] = #name
1089 static char *oss_mixer_names[SNDRV_OSS_MAX_MIXERS] = {
1090 	MIXER_VOL(VOLUME),
1091 	MIXER_VOL(BASS),
1092 	MIXER_VOL(TREBLE),
1093 	MIXER_VOL(SYNTH),
1094 	MIXER_VOL(PCM),
1095 	MIXER_VOL(SPEAKER),
1096 	MIXER_VOL(LINE),
1097 	MIXER_VOL(MIC),
1098 	MIXER_VOL(CD),
1099 	MIXER_VOL(IMIX),
1100 	MIXER_VOL(ALTPCM),
1101 	MIXER_VOL(RECLEV),
1102 	MIXER_VOL(IGAIN),
1103 	MIXER_VOL(OGAIN),
1104 	MIXER_VOL(LINE1),
1105 	MIXER_VOL(LINE2),
1106 	MIXER_VOL(LINE3),
1107 	MIXER_VOL(DIGITAL1),
1108 	MIXER_VOL(DIGITAL2),
1109 	MIXER_VOL(DIGITAL3),
1110 	MIXER_VOL(PHONEIN),
1111 	MIXER_VOL(PHONEOUT),
1112 	MIXER_VOL(VIDEO),
1113 	MIXER_VOL(RADIO),
1114 	MIXER_VOL(MONITOR),
1115 };
1116 
1117 /*
1118  *  /proc interface
1119  */
1120 
snd_mixer_oss_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1121 static void snd_mixer_oss_proc_read(struct snd_info_entry *entry,
1122 				    struct snd_info_buffer *buffer)
1123 {
1124 	struct snd_mixer_oss *mixer = entry->private_data;
1125 	int i;
1126 
1127 	mutex_lock(&mixer->reg_mutex);
1128 	for (i = 0; i < SNDRV_OSS_MAX_MIXERS; i++) {
1129 		struct slot *p;
1130 
1131 		if (! oss_mixer_names[i])
1132 			continue;
1133 		p = (struct slot *)mixer->slots[i].private_data;
1134 		snd_iprintf(buffer, "%s ", oss_mixer_names[i]);
1135 		if (p && p->assigned)
1136 			snd_iprintf(buffer, "\"%s\" %d\n",
1137 				    p->assigned->name,
1138 				    p->assigned->index);
1139 		else
1140 			snd_iprintf(buffer, "\"\" 0\n");
1141 	}
1142 	mutex_unlock(&mixer->reg_mutex);
1143 }
1144 
snd_mixer_oss_proc_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1145 static void snd_mixer_oss_proc_write(struct snd_info_entry *entry,
1146 				     struct snd_info_buffer *buffer)
1147 {
1148 	struct snd_mixer_oss *mixer = entry->private_data;
1149 	char line[128], str[32], idxstr[16], *cptr;
1150 	int ch, idx;
1151 	struct snd_mixer_oss_assign_table *tbl;
1152 	struct slot *slot;
1153 
1154 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
1155 		cptr = snd_info_get_str(str, line, sizeof(str));
1156 		for (ch = 0; ch < SNDRV_OSS_MAX_MIXERS; ch++)
1157 			if (oss_mixer_names[ch] && strcmp(oss_mixer_names[ch], str) == 0)
1158 				break;
1159 		if (ch >= SNDRV_OSS_MAX_MIXERS) {
1160 			snd_printk(KERN_ERR "mixer_oss: invalid OSS volume '%s'\n", str);
1161 			continue;
1162 		}
1163 		cptr = snd_info_get_str(str, cptr, sizeof(str));
1164 		if (! *str) {
1165 			/* remove the entry */
1166 			mutex_lock(&mixer->reg_mutex);
1167 			mixer_slot_clear(&mixer->slots[ch]);
1168 			mutex_unlock(&mixer->reg_mutex);
1169 			continue;
1170 		}
1171 		snd_info_get_str(idxstr, cptr, sizeof(idxstr));
1172 		idx = simple_strtoul(idxstr, NULL, 10);
1173 		if (idx >= 0x4000) { /* too big */
1174 			snd_printk(KERN_ERR "mixer_oss: invalid index %d\n", idx);
1175 			continue;
1176 		}
1177 		mutex_lock(&mixer->reg_mutex);
1178 		slot = (struct slot *)mixer->slots[ch].private_data;
1179 		if (slot && slot->assigned &&
1180 		    slot->assigned->index == idx && ! strcmp(slot->assigned->name, str))
1181 			/* not changed */
1182 			goto __unlock;
1183 		tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
1184 		if (! tbl) {
1185 			snd_printk(KERN_ERR "mixer_oss: no memory\n");
1186 			goto __unlock;
1187 		}
1188 		tbl->oss_id = ch;
1189 		tbl->name = kstrdup(str, GFP_KERNEL);
1190 		if (! tbl->name) {
1191 			kfree(tbl);
1192 			goto __unlock;
1193 		}
1194 		tbl->index = idx;
1195 		if (snd_mixer_oss_build_input(mixer, tbl, 1, 1) <= 0) {
1196 			kfree(tbl->name);
1197 			kfree(tbl);
1198 		}
1199 	__unlock:
1200 		mutex_unlock(&mixer->reg_mutex);
1201 	}
1202 }
1203 
snd_mixer_oss_proc_init(struct snd_mixer_oss * mixer)1204 static void snd_mixer_oss_proc_init(struct snd_mixer_oss *mixer)
1205 {
1206 	struct snd_info_entry *entry;
1207 
1208 	entry = snd_info_create_card_entry(mixer->card, "oss_mixer",
1209 					   mixer->card->proc_root);
1210 	if (! entry)
1211 		return;
1212 	entry->content = SNDRV_INFO_CONTENT_TEXT;
1213 	entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
1214 	entry->c.text.read = snd_mixer_oss_proc_read;
1215 	entry->c.text.write = snd_mixer_oss_proc_write;
1216 	entry->private_data = mixer;
1217 	if (snd_info_register(entry) < 0) {
1218 		snd_info_free_entry(entry);
1219 		entry = NULL;
1220 	}
1221 	mixer->proc_entry = entry;
1222 }
1223 
snd_mixer_oss_proc_done(struct snd_mixer_oss * mixer)1224 static void snd_mixer_oss_proc_done(struct snd_mixer_oss *mixer)
1225 {
1226 	snd_info_free_entry(mixer->proc_entry);
1227 	mixer->proc_entry = NULL;
1228 }
1229 #else /* !CONFIG_PROC_FS */
1230 #define snd_mixer_oss_proc_init(mix)
1231 #define snd_mixer_oss_proc_done(mix)
1232 #endif /* CONFIG_PROC_FS */
1233 
snd_mixer_oss_build(struct snd_mixer_oss * mixer)1234 static void snd_mixer_oss_build(struct snd_mixer_oss *mixer)
1235 {
1236 	static struct snd_mixer_oss_assign_table table[] = {
1237 		{ SOUND_MIXER_VOLUME, 	"Master",		0 },
1238 		{ SOUND_MIXER_VOLUME, 	"Front",		0 }, /* fallback */
1239 		{ SOUND_MIXER_BASS,	"Tone Control - Bass",	0 },
1240 		{ SOUND_MIXER_TREBLE,	"Tone Control - Treble", 0 },
1241 		{ SOUND_MIXER_SYNTH,	"Synth",		0 },
1242 		{ SOUND_MIXER_SYNTH,	"FM",			0 }, /* fallback */
1243 		{ SOUND_MIXER_SYNTH,	"Music",		0 }, /* fallback */
1244 		{ SOUND_MIXER_PCM,	"PCM",			0 },
1245 		{ SOUND_MIXER_SPEAKER,	"PC Speaker", 		0 },
1246 		{ SOUND_MIXER_LINE,	"Line", 		0 },
1247 		{ SOUND_MIXER_MIC,	"Mic", 			0 },
1248 		{ SOUND_MIXER_CD,	"CD", 			0 },
1249 		{ SOUND_MIXER_IMIX,	"Monitor Mix", 		0 },
1250 		{ SOUND_MIXER_ALTPCM,	"PCM",			1 },
1251 		{ SOUND_MIXER_ALTPCM,	"Headphone",		0 }, /* fallback */
1252 		{ SOUND_MIXER_ALTPCM,	"Wave",			0 }, /* fallback */
1253 		{ SOUND_MIXER_RECLEV,	"-- nothing --",	0 },
1254 		{ SOUND_MIXER_IGAIN,	"Capture",		0 },
1255 		{ SOUND_MIXER_OGAIN,	"Playback",		0 },
1256 		{ SOUND_MIXER_LINE1,	"Aux",			0 },
1257 		{ SOUND_MIXER_LINE2,	"Aux",			1 },
1258 		{ SOUND_MIXER_LINE3,	"Aux",			2 },
1259 		{ SOUND_MIXER_DIGITAL1,	"Digital",		0 },
1260 		{ SOUND_MIXER_DIGITAL1,	"IEC958",		0 }, /* fallback */
1261 		{ SOUND_MIXER_DIGITAL1,	"IEC958 Optical",	0 }, /* fallback */
1262 		{ SOUND_MIXER_DIGITAL1,	"IEC958 Coaxial",	0 }, /* fallback */
1263 		{ SOUND_MIXER_DIGITAL2,	"Digital",		1 },
1264 		{ SOUND_MIXER_DIGITAL3,	"Digital",		2 },
1265 		{ SOUND_MIXER_PHONEIN,	"Phone",		0 },
1266 		{ SOUND_MIXER_PHONEOUT,	"Master Mono",		0 },
1267 		{ SOUND_MIXER_PHONEOUT,	"Speaker",		0 }, /*fallback*/
1268 		{ SOUND_MIXER_PHONEOUT,	"Mono",			0 }, /*fallback*/
1269 		{ SOUND_MIXER_PHONEOUT,	"Phone",		0 }, /* fallback */
1270 		{ SOUND_MIXER_VIDEO,	"Video",		0 },
1271 		{ SOUND_MIXER_RADIO,	"Radio",		0 },
1272 		{ SOUND_MIXER_MONITOR,	"Monitor",		0 }
1273 	};
1274 	unsigned int idx;
1275 
1276 	for (idx = 0; idx < ARRAY_SIZE(table); idx++)
1277 		snd_mixer_oss_build_input(mixer, &table[idx], 0, 0);
1278 	if (mixer->mask_recsrc) {
1279 		mixer->get_recsrc = snd_mixer_oss_get_recsrc2;
1280 		mixer->put_recsrc = snd_mixer_oss_put_recsrc2;
1281 	}
1282 }
1283 
1284 /*
1285  *
1286  */
1287 
snd_mixer_oss_free1(void * private)1288 static int snd_mixer_oss_free1(void *private)
1289 {
1290 	struct snd_mixer_oss *mixer = private;
1291 	struct snd_card *card;
1292 	int idx;
1293 
1294 	if (!mixer)
1295 		return 0;
1296 	card = mixer->card;
1297 	if (snd_BUG_ON(mixer != card->mixer_oss))
1298 		return -ENXIO;
1299 	card->mixer_oss = NULL;
1300 	for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++) {
1301 		struct snd_mixer_oss_slot *chn = &mixer->slots[idx];
1302 		if (chn->private_free)
1303 			chn->private_free(chn);
1304 	}
1305 	kfree(mixer);
1306 	return 0;
1307 }
1308 
snd_mixer_oss_notify_handler(struct snd_card * card,int cmd)1309 static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd)
1310 {
1311 	struct snd_mixer_oss *mixer;
1312 
1313 	if (cmd == SND_MIXER_OSS_NOTIFY_REGISTER) {
1314 		char name[128];
1315 		int idx, err;
1316 
1317 		mixer = kcalloc(2, sizeof(*mixer), GFP_KERNEL);
1318 		if (mixer == NULL)
1319 			return -ENOMEM;
1320 		mutex_init(&mixer->reg_mutex);
1321 		sprintf(name, "mixer%i%i", card->number, 0);
1322 		if ((err = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER,
1323 						   card, 0,
1324 						   &snd_mixer_oss_f_ops, card,
1325 						   name)) < 0) {
1326 			snd_printk(KERN_ERR "unable to register OSS mixer device %i:%i\n",
1327 				   card->number, 0);
1328 			kfree(mixer);
1329 			return err;
1330 		}
1331 		mixer->oss_dev_alloc = 1;
1332 		mixer->card = card;
1333 		if (*card->mixername)
1334 			strlcpy(mixer->name, card->mixername, sizeof(mixer->name));
1335 		else
1336 			strlcpy(mixer->name, name, sizeof(mixer->name));
1337 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
1338 		snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIXERS,
1339 				      card->number,
1340 				      mixer->name);
1341 #endif
1342 		for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++)
1343 			mixer->slots[idx].number = idx;
1344 		card->mixer_oss = mixer;
1345 		snd_mixer_oss_build(mixer);
1346 		snd_mixer_oss_proc_init(mixer);
1347 	} else {
1348 		mixer = card->mixer_oss;
1349 		if (mixer == NULL)
1350 			return 0;
1351 		if (mixer->oss_dev_alloc) {
1352 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
1353 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
1354 #endif
1355 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
1356 			mixer->oss_dev_alloc = 0;
1357 		}
1358 		if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
1359 			return 0;
1360 		snd_mixer_oss_proc_done(mixer);
1361 		return snd_mixer_oss_free1(mixer);
1362 	}
1363 	return 0;
1364 }
1365 
alsa_mixer_oss_init(void)1366 static int __init alsa_mixer_oss_init(void)
1367 {
1368 	int idx;
1369 
1370 	snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler;
1371 	for (idx = 0; idx < SNDRV_CARDS; idx++) {
1372 		if (snd_cards[idx])
1373 			snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_REGISTER);
1374 	}
1375 	return 0;
1376 }
1377 
alsa_mixer_oss_exit(void)1378 static void __exit alsa_mixer_oss_exit(void)
1379 {
1380 	int idx;
1381 
1382 	snd_mixer_oss_notify_callback = NULL;
1383 	for (idx = 0; idx < SNDRV_CARDS; idx++) {
1384 		if (snd_cards[idx])
1385 			snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_FREE);
1386 	}
1387 }
1388 
1389 module_init(alsa_mixer_oss_init)
1390 module_exit(alsa_mixer_oss_exit)
1391 
1392 EXPORT_SYMBOL(snd_mixer_oss_ioctl_card);
1393