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