usbmixer.c 54.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 *   (Tentative) USB Audio Driver for ALSA
 *
 *   Mixer control part
 *
 *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
 *
 *   Many codes borrowed from audio.c by
 *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
 *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/usb.h>
#include <sound/core.h>
#include <sound/control.h>
37
#include <sound/hwdep.h>
38
#include <sound/info.h>
39
#include <sound/tlv.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
41
42
43
44
45
46
47
48

#include "usbaudio.h"

/*
 */

/* ignore error from controls - for debugging */
/* #define IGNORE_CTL_ERROR */

49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Sound Blaster remote control configuration
 *
 * format of remote control data:
 * Extigy:       xx 00
 * Audigy 2 NX:  06 80 xx 00 00 00
 * Live! 24-bit: 06 80 xx yy 22 83
 */
static const struct rc_config {
	u32 usb_id;
	u8  offset;
	u8  length;
	u8  packet_length;
62
	u8  min_packet_length; /* minimum accepted length of the URB result */
63
64
65
	u8  mute_mixer_id;
	u32 mute_code;
} rc_configs[] = {
66
67
68
	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
69
	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
70
71
};

72
struct usb_mixer_interface {
73
	struct snd_usb_audio *chip;
74
75
76
	unsigned int ctrlif;
	struct list_head list;
	unsigned int ignore_ctl_error;
77
	struct urb *urb;
78
	struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
79
80

	/* Sound Blaster remote control stuff */
81
	const struct rc_config *rc_cfg;
82
83
84
85
86
	u32 rc_code;
	wait_queue_head_t rc_waitq;
	struct urb *rc_urb;
	struct usb_ctrlrequest *rc_setup_packet;
	u8 rc_buffer[6];
87
88

	u8 audigy2nx_leds[3];
89
90
91
};


Linus Torvalds's avatar
Linus Torvalds committed
92
93
94
95
96
97
98
99
100
101
struct usb_audio_term {
	int id;
	int type;
	int channels;
	unsigned int chconfig;
	int name;
};

struct usbmix_name_map;

102
103
struct mixer_build {
	struct snd_usb_audio *chip;
104
	struct usb_mixer_interface *mixer;
Linus Torvalds's avatar
Linus Torvalds committed
105
106
	unsigned char *buffer;
	unsigned int buflen;
107
	DECLARE_BITMAP(unitbitmap, 256);
108
	struct usb_audio_term oterm;
Linus Torvalds's avatar
Linus Torvalds committed
109
	const struct usbmix_name_map *map;
110
	const struct usbmix_selector_map *selector_map;
Linus Torvalds's avatar
Linus Torvalds committed
111
112
};

113
114
#define MAX_CHANNELS	10	/* max logical channels */

Linus Torvalds's avatar
Linus Torvalds committed
115
struct usb_mixer_elem_info {
116
	struct usb_mixer_interface *mixer;
117
118
	struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
	struct snd_ctl_elem_id *elem_id;
Linus Torvalds's avatar
Linus Torvalds committed
119
120
121
122
123
124
	unsigned int id;
	unsigned int control;	/* CS or ICN (high byte) */
	unsigned int cmask; /* channel mask bitmap: 0 = master */
	int channels;
	int val_type;
	int min, max, res;
125
126
	int cached;
	int cache_val[MAX_CHANNELS];
127
	u8 initialized;
Linus Torvalds's avatar
Linus Torvalds committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
};


enum {
	USB_FEATURE_NONE = 0,
	USB_FEATURE_MUTE = 1,
	USB_FEATURE_VOLUME,
	USB_FEATURE_BASS,
	USB_FEATURE_MID,
	USB_FEATURE_TREBLE,
	USB_FEATURE_GEQ,
	USB_FEATURE_AGC,
	USB_FEATURE_DELAY,
	USB_FEATURE_BASSBOOST,
	USB_FEATURE_LOUDNESS
};

enum {
	USB_MIXER_BOOLEAN,
	USB_MIXER_INV_BOOLEAN,
	USB_MIXER_S8,
	USB_MIXER_U8,
	USB_MIXER_S16,
	USB_MIXER_U16,
};

enum {
	USB_PROC_UPDOWN = 1,
	USB_PROC_UPDOWN_SWITCH = 1,
	USB_PROC_UPDOWN_MODE_SEL = 2,

	USB_PROC_PROLOGIC = 2,
	USB_PROC_PROLOGIC_SWITCH = 1,
	USB_PROC_PROLOGIC_MODE_SEL = 2,

	USB_PROC_3DENH = 3,
	USB_PROC_3DENH_SWITCH = 1,
	USB_PROC_3DENH_SPACE = 2,

	USB_PROC_REVERB = 4,
	USB_PROC_REVERB_SWITCH = 1,
	USB_PROC_REVERB_LEVEL = 2,
	USB_PROC_REVERB_TIME = 3,
	USB_PROC_REVERB_DELAY = 4,

	USB_PROC_CHORUS = 5,
	USB_PROC_CHORUS_SWITCH = 1,
	USB_PROC_CHORUS_LEVEL = 2,
	USB_PROC_CHORUS_RATE = 3,
	USB_PROC_CHORUS_DEPTH = 4,

	USB_PROC_DCR = 6,
	USB_PROC_DCR_SWITCH = 1,
	USB_PROC_DCR_RATIO = 2,
	USB_PROC_DCR_MAX_AMP = 3,
	USB_PROC_DCR_THRESHOLD = 4,
	USB_PROC_DCR_ATTACK = 5,
	USB_PROC_DCR_RELEASE = 6,
};


/*
 * manual mapping of mixer names
 * if the mixer topology is too complicated and the parsed names are
 * ambiguous, add the entries in usbmixer_maps.c.
 */
#include "usbmixer_maps.c"

/* get the mapped name if the unit matches */
197
static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
Linus Torvalds's avatar
Linus Torvalds committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
{
	const struct usbmix_name_map *p;

	if (! state->map)
		return 0;

	for (p = state->map; p->id; p++) {
		if (p->id == unitid && p->name &&
		    (! control || ! p->control || control == p->control)) {
			buflen--;
			return strlcpy(buf, p->name, buflen);
		}
	}
	return 0;
}

/* check whether the control should be ignored */
215
static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
Linus Torvalds's avatar
Linus Torvalds committed
216
217
218
219
220
221
222
223
{
	const struct usbmix_name_map *p;

	if (! state->map)
		return 0;
	for (p = state->map; p->id; p++) {
		if (p->id == unitid && ! p->name &&
		    (! control || ! p->control || control == p->control)) {
224
225
226
227
			/*
			printk(KERN_DEBUG "ignored control %d:%d\n",
			       unitid, control);
			*/
Linus Torvalds's avatar
Linus Torvalds committed
228
229
230
231
232
233
			return 1;
		}
	}
	return 0;
}

234
/* get the mapped selector source name */
235
static int check_mapped_selector_name(struct mixer_build *state, int unitid,
236
237
238
239
240
241
242
243
244
245
246
247
248
				      int index, char *buf, int buflen)
{
	const struct usbmix_selector_map *p;

	if (! state->selector_map)
		return 0;
	for (p = state->selector_map; p->id; p++) {
		if (p->id == unitid && index < p->count)
			return strlcpy(buf, p->names[index], buflen);
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
249
250
251
/*
 * find an audio control unit with the given unit id
 */
252
static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{
	unsigned char *p;

	p = NULL;
	while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
				      USB_DT_CS_INTERFACE)) != NULL) {
		if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
			return p;
	}
	return NULL;
}


/*
 * copy a string with the given id
 */
269
static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds's avatar
Linus Torvalds committed
270
271
272
273
274
275
276
277
278
{
	int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
	buf[len] = 0;
	return len;
}

/*
 * convert from the byte/word on usb descriptor to the zero-based integer
 */
279
static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds's avatar
Linus Torvalds committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
{
	switch (cval->val_type) {
	case USB_MIXER_BOOLEAN:
		return !!val;
	case USB_MIXER_INV_BOOLEAN:
		return !val;
	case USB_MIXER_U8:
		val &= 0xff;
		break;
	case USB_MIXER_S8:
		val &= 0xff;
		if (val >= 0x80)
			val -= 0x100;
		break;
	case USB_MIXER_U16:
		val &= 0xffff;
		break;
	case USB_MIXER_S16:
		val &= 0xffff;
		if (val >= 0x8000)
			val -= 0x10000;
		break;
	}
	return val;
}

/*
 * convert from the zero-based int to the byte/word for usb descriptor
 */
309
static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds's avatar
Linus Torvalds committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{
	switch (cval->val_type) {
	case USB_MIXER_BOOLEAN:
		return !!val;
	case USB_MIXER_INV_BOOLEAN:
		return !val;
	case USB_MIXER_S8:
	case USB_MIXER_U8:
		return val & 0xff;
	case USB_MIXER_S16:
	case USB_MIXER_U16:
		return val & 0xffff;
	}
	return 0; /* not reached */
}

326
static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds's avatar
Linus Torvalds committed
327
328
329
330
331
{
	if (! cval->res)
		cval->res = 1;
	if (val < cval->min)
		return 0;
332
333
	else if (val >= cval->max)
		return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds's avatar
Linus Torvalds committed
334
335
336
337
	else
		return (val - cval->min) / cval->res;
}

338
static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds's avatar
Linus Torvalds committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{
	if (val < 0)
		return cval->min;
	if (! cval->res)
		cval->res = 1;
	val *= cval->res;
	val += cval->min;
	if (val > cval->max)
		return cval->max;
	return val;
}


/*
 * retrieve a mixer value
 */

356
static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds's avatar
Linus Torvalds committed
357
358
359
360
361
362
{
	unsigned char buf[2];
	int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
	int timeout = 10;

	while (timeout-- > 0) {
363
364
		if (snd_usb_ctl_msg(cval->mixer->chip->dev,
				    usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds's avatar
Linus Torvalds committed
365
366
				    request,
				    USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
367
				    validx, cval->mixer->ctrlif | (cval->id << 8),
368
				    buf, val_len, 100) >= val_len) {
Linus Torvalds's avatar
Linus Torvalds committed
369
370
371
372
			*value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
			return 0;
		}
	}
373
374
	snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
		    request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds's avatar
Linus Torvalds committed
375
376
377
	return -EINVAL;
}

378
static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds's avatar
Linus Torvalds committed
379
380
381
382
383
{
	return get_ctl_value(cval, GET_CUR, validx, value);
}

/* channel = 0: master, 1 = first channel */
384
385
static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
				  int channel, int *value)
Linus Torvalds's avatar
Linus Torvalds committed
386
387
388
389
{
	return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
}

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
			     int channel, int index, int *value)
{
	int err;

	if (cval->cached & (1 << channel)) {
		*value = cval->cache_val[index];
		return 0;
	}
	err = get_cur_mix_raw(cval, channel, value);
	if (err < 0) {
		if (!cval->mixer->ignore_ctl_error)
			snd_printd(KERN_ERR "cannot get current value for "
				   "control %d ch %d: err = %d\n",
				   cval->control, channel, err);
		return err;
	}
	cval->cached |= 1 << channel;
	cval->cache_val[index] = *value;
	return 0;
}


Linus Torvalds's avatar
Linus Torvalds committed
413
414
415
416
/*
 * set a mixer value
 */

417
static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds's avatar
Linus Torvalds committed
418
419
420
421
422
423
424
425
{
	unsigned char buf[2];
	int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
	int timeout = 10;

	value_set = convert_bytes_value(cval, value_set);
	buf[0] = value_set & 0xff;
	buf[1] = (value_set >> 8) & 0xff;
Viral Mehta's avatar
Viral Mehta committed
426
	while (timeout-- > 0)
427
428
		if (snd_usb_ctl_msg(cval->mixer->chip->dev,
				    usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds's avatar
Linus Torvalds committed
429
430
				    request,
				    USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
431
				    validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds's avatar
Linus Torvalds committed
432
433
				    buf, val_len, 100) >= 0)
			return 0;
434
435
	snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
		    request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds's avatar
Linus Torvalds committed
436
437
438
	return -EINVAL;
}

439
static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds's avatar
Linus Torvalds committed
440
441
442
443
{
	return set_ctl_value(cval, SET_CUR, validx, value);
}

444
445
static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
			     int index, int value)
Linus Torvalds's avatar
Linus Torvalds committed
446
{
447
448
449
450
451
452
453
454
	int err;
	err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
			    value);
	if (err < 0)
		return err;
	cval->cached |= 1 << channel;
	cval->cache_val[index] = value;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
455
456
}

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * TLV callback for mixer volume controls
 */
static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
			 unsigned int size, unsigned int __user *_tlv)
{
	struct usb_mixer_elem_info *cval = kcontrol->private_data;
	DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);

	if (size < sizeof(scale))
		return -ENOMEM;
	/* USB descriptions contain the dB scale in 1/256 dB unit
	 * while ALSA TLV contains in 1/100 dB unit
	 */
	scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
	scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
	if (copy_to_user(_tlv, scale, sizeof(scale)))
		return -EFAULT;
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
477
478
479
480
481

/*
 * parser routines begin here...
 */

482
static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds's avatar
Linus Torvalds committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502


/*
 * check if the input/output channel routing is enabled on the given bitmap.
 * used for mixer unit parser
 */
static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
{
	int idx = ich * num_outs + och;
	return bmap[idx >> 3] & (0x80 >> (idx & 7));
}


/*
 * add an alsa control element
 * search and increment the index until an empty slot is found.
 *
 * if failed, give up and free the control instance.
 */

503
static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds's avatar
Linus Torvalds committed
504
{
505
	struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
506
	int err;
507
508

	while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds's avatar
Linus Torvalds committed
509
		kctl->id.index++;
510
	if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
511
		snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
512
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
513
	}
514
515
516
517
	cval->elem_id = &kctl->id;
	cval->next_id_elem = state->mixer->id_elems[cval->id];
	state->mixer->id_elems[cval->id] = cval;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
}


/*
 * get a terminal name string
 */

static struct iterm_name_combo {
	int type;
	char *name;
} iterm_names[] = {
	{ 0x0300, "Output" },
	{ 0x0301, "Speaker" },
	{ 0x0302, "Headphone" },
	{ 0x0303, "HMD Audio" },
	{ 0x0304, "Desktop Speaker" },
	{ 0x0305, "Room Speaker" },
	{ 0x0306, "Com Speaker" },
	{ 0x0307, "LFE" },
	{ 0x0600, "External In" },
	{ 0x0601, "Analog In" },
	{ 0x0602, "Digital In" },
	{ 0x0603, "Line" },
	{ 0x0604, "Legacy In" },
	{ 0x0605, "IEC958 In" },
	{ 0x0606, "1394 DA Stream" },
	{ 0x0607, "1394 DV Stream" },
	{ 0x0700, "Embedded" },
	{ 0x0701, "Noise Source" },
	{ 0x0702, "Equalization Noise" },
	{ 0x0703, "CD" },
	{ 0x0704, "DAT" },
	{ 0x0705, "DCC" },
	{ 0x0706, "MiniDisk" },
	{ 0x0707, "Analog Tape" },
	{ 0x0708, "Phonograph" },
	{ 0x0709, "VCR Audio" },
	{ 0x070a, "Video Disk Audio" },
	{ 0x070b, "DVD Audio" },
	{ 0x070c, "TV Tuner Audio" },
	{ 0x070d, "Satellite Rec Audio" },
	{ 0x070e, "Cable Tuner Audio" },
	{ 0x070f, "DSS Audio" },
	{ 0x0710, "Radio Receiver" },
	{ 0x0711, "Radio Transmitter" },
	{ 0x0712, "Multi-Track Recorder" },
	{ 0x0713, "Synthesizer" },
	{ 0 },
};

568
static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds's avatar
Linus Torvalds committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
			 unsigned char *name, int maxlen, int term_only)
{
	struct iterm_name_combo *names;

	if (iterm->name)
		return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);

	/* virtual type - not a real terminal */
	if (iterm->type >> 16) {
		if (term_only)
			return 0;
		switch (iterm->type >> 16) {
		case SELECTOR_UNIT:
			strcpy(name, "Selector"); return 8;
		case PROCESSING_UNIT:
			strcpy(name, "Process Unit"); return 12;
		case EXTENSION_UNIT:
			strcpy(name, "Ext Unit"); return 8;
		case MIXER_UNIT:
			strcpy(name, "Mixer"); return 5;
		default:
			return sprintf(name, "Unit %d", iterm->id);
		}
	}

	switch (iterm->type & 0xff00) {
	case 0x0100:
		strcpy(name, "PCM"); return 3;
	case 0x0200:
		strcpy(name, "Mic"); return 3;
	case 0x0400:
		strcpy(name, "Headset"); return 7;
	case 0x0500:
		strcpy(name, "Phone"); return 5;
	}

	for (names = iterm_names; names->type; names++)
		if (names->type == iterm->type) {
			strcpy(name, names->name);
			return strlen(names->name);
		}
	return 0;
}


/*
 * parse the source unit recursively until it reaches to a terminal
 * or a branched unit.
 */
618
static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds's avatar
Linus Torvalds committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
{
	unsigned char *p1;

	memset(term, 0, sizeof(*term));
	while ((p1 = find_audio_control_unit(state, id)) != NULL) {
		term->id = id;
		switch (p1[2]) {
		case INPUT_TERMINAL:
			term->type = combine_word(p1 + 4);
			term->channels = p1[7];
			term->chconfig = combine_word(p1 + 8);
			term->name = p1[11];
			return 0;
		case FEATURE_UNIT:
			id = p1[4];
			break; /* continue to parse */
		case MIXER_UNIT:
			term->type = p1[2] << 16; /* virtual type */
			term->channels = p1[5 + p1[4]];
			term->chconfig = combine_word(p1 + 6 + p1[4]);
			term->name = p1[p1[0] - 1];
			return 0;
		case SELECTOR_UNIT:
			/* call recursively to retrieve the channel info */
			if (check_input_term(state, p1[5], term) < 0)
				return -ENODEV;
			term->type = p1[2] << 16; /* virtual type */
			term->id = id;
			term->name = p1[9 + p1[0] - 1];
			return 0;
		case PROCESSING_UNIT:
		case EXTENSION_UNIT:
			if (p1[6] == 1) {
				id = p1[7];
				break; /* continue to parse */
			}
			term->type = p1[2] << 16; /* virtual type */
			term->channels = p1[7 + p1[6]];
			term->chconfig = combine_word(p1 + 8 + p1[6]);
			term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
			return 0;
		default:
			return -ENODEV;
		}
	}
	return -ENODEV;
}


/*
 * Feature Unit
 */

/* feature unit control information */
struct usb_feature_control_info {
	const char *name;
	unsigned int type;	/* control type (mute, volume, etc.) */
};

static struct usb_feature_control_info audio_feature_info[] = {
	{ "Mute",		USB_MIXER_INV_BOOLEAN },
	{ "Volume",		USB_MIXER_S16 },
	{ "Tone Control - Bass",	USB_MIXER_S8 },
	{ "Tone Control - Mid",		USB_MIXER_S8 },
	{ "Tone Control - Treble",	USB_MIXER_S8 },
	{ "Graphic Equalizer",		USB_MIXER_S8 }, /* FIXME: not implemeted yet */
	{ "Auto Gain Control",	USB_MIXER_BOOLEAN },
	{ "Delay Control",	USB_MIXER_U16 },
	{ "Bass Boost",		USB_MIXER_BOOLEAN },
	{ "Loudness",		USB_MIXER_BOOLEAN },
};


/* private_free callback */
693
static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds's avatar
Linus Torvalds committed
694
{
695
696
	kfree(kctl->private_data);
	kctl->private_data = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
697
698
699
700
701
702
703
704
705
706
}


/*
 * interface to ALSA control for feature/mixer units
 */

/*
 * retrieve the minimum and maximum values for the specified control
 */
707
static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds's avatar
Linus Torvalds committed
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
{
	/* for failsafe */
	cval->min = default_min;
	cval->max = cval->min + 1;
	cval->res = 1;

	if (cval->val_type == USB_MIXER_BOOLEAN ||
	    cval->val_type == USB_MIXER_INV_BOOLEAN) {
		cval->initialized = 1;
	} else {
		int minchn = 0;
		if (cval->cmask) {
			int i;
			for (i = 0; i < MAX_CHANNELS; i++)
				if (cval->cmask & (1 << i)) {
					minchn = i + 1;
					break;
				}
		}
		if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
		    get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
729
730
			snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
				   cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds's avatar
Linus Torvalds committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
			return -EINVAL;
		}
		if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
			cval->res = 1;
		} else {
			int last_valid_res = cval->res;

			while (cval->res > 1) {
				if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
					break;
				cval->res /= 2;
			}
			if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
				cval->res = last_valid_res;
		}
		if (cval->res == 0)
			cval->res = 1;
748
749
750
751
752
753
754
755
756
757

		/* Additional checks for the proper resolution
		 *
		 * Some devices report smaller resolutions than actually
		 * reacting.  They don't return errors but simply clip
		 * to the lower aligned value.
		 */
		if (cval->min + cval->res < cval->max) {
			int last_valid_res = cval->res;
			int saved, test, check;
758
			get_cur_mix_raw(cval, minchn, &saved);
759
760
761
762
763
764
765
			for (;;) {
				test = saved;
				if (test < cval->max)
					test += cval->res;
				else
					test -= cval->res;
				if (test < cval->min || test > cval->max ||
766
767
				    set_cur_mix_value(cval, minchn, 0, test) ||
				    get_cur_mix_raw(cval, minchn, &check)) {
768
769
770
771
772
773
774
					cval->res = last_valid_res;
					break;
				}
				if (test == check)
					break;
				cval->res *= 2;
			}
775
			set_cur_mix_value(cval, minchn, 0, saved);
776
777
		}

Linus Torvalds's avatar
Linus Torvalds committed
778
779
780
781
782
783
784
		cval->initialized = 1;
	}
	return 0;
}


/* get a feature/mixer unit info */
785
static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
786
{
787
	struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802

	if (cval->val_type == USB_MIXER_BOOLEAN ||
	    cval->val_type == USB_MIXER_INV_BOOLEAN)
		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	else
		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = cval->channels;
	if (cval->val_type == USB_MIXER_BOOLEAN ||
	    cval->val_type == USB_MIXER_INV_BOOLEAN) {
		uinfo->value.integer.min = 0;
		uinfo->value.integer.max = 1;
	} else {
		if (! cval->initialized)
			get_min_max(cval,  0);
		uinfo->value.integer.min = 0;
803
804
		uinfo->value.integer.max =
			(cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds's avatar
Linus Torvalds committed
805
806
807
808
809
	}
	return 0;
}

/* get the current value from feature/mixer unit */
810
static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
811
{
812
	struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
813
814
	int c, cnt, val, err;

815
	ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds's avatar
Linus Torvalds committed
816
817
818
	if (cval->cmask) {
		cnt = 0;
		for (c = 0; c < MAX_CHANNELS; c++) {
819
820
821
822
823
824
825
826
			if (!(cval->cmask & (1 << c)))
				continue;
			err = get_cur_mix_value(cval, c + 1, cnt, &val);
			if (err < 0)
				return cval->mixer->ignore_ctl_error ? 0 : err;
			val = get_relative_value(cval, val);
			ucontrol->value.integer.value[cnt] = val;
			cnt++;
Linus Torvalds's avatar
Linus Torvalds committed
827
		}
828
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
829
830
	} else {
		/* master channel */
831
832
833
		err = get_cur_mix_value(cval, 0, 0, &val);
		if (err < 0)
			return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds's avatar
Linus Torvalds committed
834
835
836
837
838
839
840
		val = get_relative_value(cval, val);
		ucontrol->value.integer.value[0] = val;
	}
	return 0;
}

/* put the current value to feature/mixer unit */
841
static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
842
{
843
	struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
844
845
846
847
848
849
	int c, cnt, val, oval, err;
	int changed = 0;

	if (cval->cmask) {
		cnt = 0;
		for (c = 0; c < MAX_CHANNELS; c++) {
850
851
852
853
854
855
856
857
858
859
			if (!(cval->cmask & (1 << c)))
				continue;
			err = get_cur_mix_value(cval, c + 1, cnt, &oval);
			if (err < 0)
				return cval->mixer->ignore_ctl_error ? 0 : err;
			val = ucontrol->value.integer.value[cnt];
			val = get_abs_value(cval, val);
			if (oval != val) {
				set_cur_mix_value(cval, c + 1, cnt, val);
				changed = 1;
Linus Torvalds's avatar
Linus Torvalds committed
860
			}
861
			cnt++;
Linus Torvalds's avatar
Linus Torvalds committed
862
863
864
		}
	} else {
		/* master channel */
865
		err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds's avatar
Linus Torvalds committed
866
		if (err < 0)
867
			return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds's avatar
Linus Torvalds committed
868
869
870
		val = ucontrol->value.integer.value[0];
		val = get_abs_value(cval, val);
		if (val != oval) {
871
			set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds's avatar
Linus Torvalds committed
872
873
874
875
876
877
			changed = 1;
		}
	}
	return changed;
}

878
static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds's avatar
Linus Torvalds committed
879
880
881
882
883
884
885
886
887
888
889
890
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "", /* will be filled later manually */
	.info = mixer_ctl_feature_info,
	.get = mixer_ctl_feature_get,
	.put = mixer_ctl_feature_put,
};


/*
 * build a feature control
 */

891
static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds's avatar
Linus Torvalds committed
892
			      unsigned int ctl_mask, int control,
893
			      struct usb_audio_term *iterm, int unitid)
Linus Torvalds's avatar
Linus Torvalds committed
894
895
896
897
{
	unsigned int len = 0;
	int mapped_name = 0;
	int nameid = desc[desc[0] - 1];
898
899
	struct snd_kcontrol *kctl;
	struct usb_mixer_elem_info *cval;
Linus Torvalds's avatar
Linus Torvalds committed
900
901
902
903
904
905
906
907
908
909
910

	control++; /* change from zero-based to 1-based value */

	if (control == USB_FEATURE_GEQ) {
		/* FIXME: not supported yet */
		return;
	}

	if (check_ignored_ctl(state, unitid, control))
		return;

911
	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
912
913
914
915
	if (! cval) {
		snd_printk(KERN_ERR "cannot malloc kcontrol\n");
		return;
	}
916
	cval->mixer = state->mixer;
Linus Torvalds's avatar
Linus Torvalds committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
	cval->id = unitid;
	cval->control = control;
	cval->cmask = ctl_mask;
	cval->val_type = audio_feature_info[control-1].type;
	if (ctl_mask == 0)
		cval->channels = 1;	/* master channel */
	else {
		int i, c = 0;
		for (i = 0; i < 16; i++)
			if (ctl_mask & (1 << i))
				c++;
		cval->channels = c;
	}

	/* get min/max values */
	get_min_max(cval, 0);

	kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
	if (! kctl) {
		snd_printk(KERN_ERR "cannot malloc kcontrol\n");
		kfree(cval);
		return;
	}
	kctl->private_free = usb_mixer_elem_free;

	len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
	mapped_name = len != 0;
	if (! len && nameid)
		len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));

	switch (control) {
	case USB_FEATURE_MUTE:
	case USB_FEATURE_VOLUME:
		/* determine the control name.  the rule is:
		 * - if a name id is given in descriptor, use it.
		 * - if the connected input can be determined, then use the name
		 *   of terminal type.
		 * - if the connected output can be determined, use it.
		 * - otherwise, anonymous name.
		 */
		if (! len) {
			len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
			if (! len)
				len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
			if (! len)
				len = snprintf(kctl->id.name, sizeof(kctl->id.name),
					       "Feature %d", unitid);
		}
		/* determine the stream direction:
		 * if the connected output is USB stream, then it's likely a
		 * capture stream.  otherwise it should be playback (hopefully :)
		 */
		if (! mapped_name && ! (state->oterm.type >> 16)) {
			if ((state->oterm.type & 0xff00) == 0x0100) {
				len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
			} else {
				len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
			}
		}
		strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
			sizeof(kctl->id.name));
978
979
980
981
982
983
		if (control == USB_FEATURE_VOLUME) {
			kctl->tlv.c = mixer_vol_tlv;
			kctl->vd[0].access |= 
				SNDRV_CTL_ELEM_ACCESS_TLV_READ |
				SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
		}
Linus Torvalds's avatar
Linus Torvalds committed
984
985
986
987
988
989
990
991
992
993
994
995
996
		break;

	default:
		if (! len)
			strlcpy(kctl->id.name, audio_feature_info[control-1].name,
				sizeof(kctl->id.name));
		break;
	}

	/* quirk for UDA1321/N101 */
	/* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
	/* is not very clear from datasheets */
	/* I hope that the min value is -15360 for newer firmware --jk */
997
998
999
1000
	switch (state->chip->usb_id) {
	case USB_ID(0x0471, 0x0101):
	case USB_ID(0x0471, 0x0104):
	case USB_ID(0x0471, 0x0105):