Blame view

sound/usb/mixer_us16x08.c 37.6 KB
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
  /*
   *   Tascam US-16x08 ALSA driver
   *
   *   Copyright (c) 2016 by Detlef Urban (onkel@paraair.de)
   *
   *   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.
   *
   */
  
  #include <linux/slab.h>
  #include <linux/usb.h>
  #include <linux/usb/audio-v2.h>
  
  #include <sound/core.h>
  #include <sound/control.h>
  
  #include "usbaudio.h"
  #include "mixer.h"
  #include "helper.h"
  
  #include "mixer_us16x08.h"
  
  /* USB control message templates */
  static const char route_msg[] = {
  	0x61,
  	0x02,
  	0x03, /* input from master (0x02) or input from computer bus (0x03) */
  	0x62,
  	0x02,
  	0x01, /* input index (0x01/0x02 eq. left/right) or bus (0x01-0x08) */
  	0x41,
  	0x01,
  	0x61,
  	0x02,
  	0x01,
  	0x62,
  	0x02,
  	0x01, /* output index (0x01-0x08) */
  	0x42,
  	0x01,
  	0x43,
  	0x01,
  	0x00,
  	0x00
  };
  
  static const char mix_init_msg1[] = {
  	0x71, 0x01, 0x00, 0x00
  };
  
  static const char mix_init_msg2[] = {
  	0x62, 0x02, 0x00, 0x61, 0x02, 0x04, 0xb1, 0x01, 0x00, 0x00
  };
  
  static const char mix_msg_in[] = {
  	/* default message head, equal to all mixers */
  	0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
  	0x81, /* 0x06: Controller ID */
  	0x02, /* 0x07:  */
  	0x00, /* 0x08: Value of common mixer */
  	0x00,
  	0x00
  };
  
  static const char mix_msg_out[] = {
  	/* default message head, equal to all mixers */
  	0x61, 0x02, 0x02, 0x62, 0x02, 0x01,
  	0x81, /* 0x06: Controller ID */
  	0x02, /*                    0x07:  */
  	0x00, /*                    0x08: Value of common mixer */
  	0x00,
  	0x00
  };
  
  static const char bypass_msg_out[] = {
  	0x45,
  	0x02,
  	0x01, /* on/off flag */
  	0x00,
  	0x00
  };
  
  static const char bus_msg_out[] = {
  	0x44,
  	0x02,
  	0x01, /* on/off flag */
  	0x00,
  	0x00
  };
  
  static const char comp_msg[] = {
  	/* default message head, equal to all mixers */
  	0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
  	0x91,
  	0x02,
  	0xf0, /* 0x08: Threshold db (8) (e0 ... 00) (+-0dB -- -32dB) x-32 */
  	0x92,
  	0x02,
  	0x0a, /* 0x0b: Ratio (0a,0b,0d,0f,11,14,19,1e,23,28,32,3c,50,a0,ff)  */
  	0x93,
  	0x02,
  	0x02, /* 0x0e: Attack (0x02 ... 0xc0) (2ms ... 200ms) */
  	0x94,
  	0x02,
  	0x01, /* 0x11: Release (0x01 ... 0x64) (10ms ... 1000ms) x*10  */
  	0x95,
  	0x02,
  	0x03, /* 0x14: gain (0 ... 20) (0dB .. 20dB) */
  	0x96,
  	0x02,
  	0x01,
  	0x97,
  	0x02,
  	0x01, /* 0x1a: main Comp switch (0 ... 1) (off ... on)) */
  	0x00,
  	0x00
  };
  
  static const char eqs_msq[] = {
  	/* default message head, equal to all mixers */
  	0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
  	0x51, /*                0x06: Controller ID  */
  	0x02,
  	0x04, /* 0x08: EQ set num (0x01..0x04) (LOW, LOWMID, HIGHMID, HIGH)) */
  	0x52,
  	0x02,
  	0x0c, /* 0x0b: value dB (0 ... 12) (-12db .. +12db)  x-6 */
  	0x53,
  	0x02,
  	0x0f, /* 0x0e: value freq (32-47) (1.7kHz..18kHz) */
  	0x54,
  	0x02,
  	0x02, /* 0x11: band width (0-6) (Q16-Q0.25)  2^x/4 (EQ xxMID only) */
  	0x55,
  	0x02,
  	0x01, /* 0x14: main EQ switch (0 ... 1) (off ... on)) */
  	0x00,
  	0x00
  };
  
  /* compressor ratio map */
  static const char ratio_map[] = {
  	0x0a, 0x0b, 0x0d, 0x0f, 0x11, 0x14, 0x19, 0x1e,
  	0x23, 0x28, 0x32, 0x3c, 0x50, 0xa0, 0xff
  };
  
  /* route enumeration names */
02ed051fe   Takashi Sakamoto   ALSA: usb-audio: ...
156
  static const char *const route_names[] = {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  	"Master Left", "Master Right", "Output 1", "Output 2", "Output 3",
  	"Output 4", "Output 5", "Output 6", "Output 7", "Output 8",
  };
  
  static int snd_us16x08_recv_urb(struct snd_usb_audio *chip,
  	unsigned char *buf, int size)
  {
  
  	mutex_lock(&chip->mutex);
  	snd_usb_ctl_msg(chip->dev,
  		usb_rcvctrlpipe(chip->dev, 0),
  		SND_US16X08_URB_METER_REQUEST,
  		SND_US16X08_URB_METER_REQUESTTYPE, 0, 0, buf, size);
  	mutex_unlock(&chip->mutex);
  	return 0;
  }
  
  /* wrapper function to send prepared URB buffer to usb device. Return an error
   * code if something went wrong
   */
  static int snd_us16x08_send_urb(struct snd_usb_audio *chip, char *buf, int size)
  {
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
179
  	return snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
180
181
  			SND_US16X08_URB_REQUEST, SND_US16X08_URB_REQUESTTYPE,
  			0, 0, buf, size);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  }
  
  static int snd_us16x08_route_info(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_info *uinfo)
  {
  	return snd_ctl_enum_info(uinfo, 1, 10, route_names);
  }
  
  static int snd_us16x08_route_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	int index = ucontrol->id.index;
  
  	/* route has no bias */
  	ucontrol->value.enumerated.item[0] = elem->cache_val[index];
  
  	return 0;
  }
  
  static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
  	int index = ucontrol->id.index;
  	char buf[sizeof(route_msg)];
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
209
  	int val, val_org, err;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
210
211
212
213
214
215
216
  
  	/*  get the new value (no bias for routes) */
  	val = ucontrol->value.enumerated.item[0];
  
  	/* sanity check */
  	if (val < 0 || val > 9)
  		return -EINVAL;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
217
218
  	/* prepare the message buffer from template */
  	memcpy(buf, route_msg, sizeof(route_msg));
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  	if (val < 2) {
  		/* input comes from a master channel */
  		val_org = val;
  		buf[2] = 0x02;
  	} else {
  		/* input comes from a computer channel */
  		buf[2] = 0x03;
  		val_org = val - 2;
  	}
  
  	/* place new route selection in URB message */
  	buf[5] = (unsigned char) (val_org & 0x0f) + 1;
  	/* place route selector in URB message */
  	buf[13] = index + 1;
  
  	err = snd_us16x08_send_urb(chip, buf, sizeof(route_msg));
  
  	if (err > 0) {
  		elem->cached |= 1 << index;
  		elem->cache_val[index] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set routing, err:%d
  ", err);
  	}
  
  	return err > 0 ? 1 : 0;
  }
  
  static int snd_us16x08_master_info(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->count = 1;
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->value.integer.max = SND_US16X08_KCMAX(kcontrol);
  	uinfo->value.integer.min = SND_US16X08_KCMIN(kcontrol);
  	uinfo->value.integer.step = SND_US16X08_KCSTEP(kcontrol);
  	return 0;
  }
  
  static int snd_us16x08_master_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	int index = ucontrol->id.index;
  
  	ucontrol->value.integer.value[0] = elem->cache_val[index];
  
  	return 0;
  }
  
  static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
  	char buf[sizeof(mix_msg_out)];
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
275
  	int val, err;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
276
  	int index = ucontrol->id.index;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
277
278
279
280
281
282
283
  	/* new control value incl. bias*/
  	val = ucontrol->value.integer.value[0];
  
  	/* sanity check */
  	if (val < SND_US16X08_KCMIN(kcontrol)
  		|| val > SND_US16X08_KCMAX(kcontrol))
  		return -EINVAL;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
284
285
  	/* prepare the message buffer from template */
  	memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  	buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
  	buf[6] = elem->head.id;
  
  	/* place channel selector in URB message */
  	buf[5] = index + 1;
  	err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_out));
  
  	if (err > 0) {
  		elem->cached |= 1 << index;
  		elem->cache_val[index] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set master, err:%d
  ", err);
  	}
  
  	return err > 0 ? 1 : 0;
  }
  
  static int snd_us16x08_bus_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
  	char buf[sizeof(mix_msg_out)];
  	int val, err = 0;
  
  	val = ucontrol->value.integer.value[0];
  
  	/* prepare the message buffer from template */
  	switch (elem->head.id) {
  	case SND_US16X08_ID_BYPASS:
  		memcpy(buf, bypass_msg_out, sizeof(bypass_msg_out));
  		buf[2] = val;
  		err = snd_us16x08_send_urb(chip, buf, sizeof(bypass_msg_out));
  		break;
  	case SND_US16X08_ID_BUSS_OUT:
  		memcpy(buf, bus_msg_out, sizeof(bus_msg_out));
  		buf[2] = val;
  		err = snd_us16x08_send_urb(chip, buf, sizeof(bus_msg_out));
  		break;
  	case SND_US16X08_ID_MUTE:
  		memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
  		buf[8] = val;
  		buf[6] = elem->head.id;
  		buf[5] = 1;
  		err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_out));
  		break;
  	}
  
  	if (err > 0) {
  		elem->cached |= 1;
  		elem->cache_val[0] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set buss param, err:%d
  ", err);
  	}
  
  	return err > 0 ? 1 : 0;
  }
  
  static int snd_us16x08_bus_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  
  	switch (elem->head.id) {
  	case SND_US16X08_ID_BUSS_OUT:
  		ucontrol->value.integer.value[0] = elem->cache_val[0];
  		break;
  	case SND_US16X08_ID_BYPASS:
  		ucontrol->value.integer.value[0] = elem->cache_val[0];
  		break;
  	case SND_US16X08_ID_MUTE:
  		ucontrol->value.integer.value[0] = elem->cache_val[0];
  		break;
  	}
  
  	return 0;
  }
  
  /* gets a current mixer value from common store */
  static int snd_us16x08_channel_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	int index = ucontrol->id.index;
  
  	ucontrol->value.integer.value[0] = elem->cache_val[index];
  
  	return 0;
  }
  
  static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
  	char buf[sizeof(mix_msg_in)];
  	int val, err;
  	int index = ucontrol->id.index;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
386
387
388
389
390
391
  	val = ucontrol->value.integer.value[0];
  
  	/* sanity check */
  	if (val < SND_US16X08_KCMIN(kcontrol)
  		|| val > SND_US16X08_KCMAX(kcontrol))
  		return -EINVAL;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
392
393
  	/* prepare URB message from template */
  	memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  	/* add the bias to the new value */
  	buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
  	buf[6] = elem->head.id;
  	buf[5] = index + 1;
  
  	err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_in));
  
  	if (err > 0) {
  		elem->cached |= 1 << index;
  		elem->cache_val[index] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set channel, err:%d
  ", err);
  	}
  
  	return err > 0 ? 1 : 0;
  }
  
  static int snd_us16x08_mix_info(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->count = 1;
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->value.integer.max = SND_US16X08_KCMAX(kcontrol);
  	uinfo->value.integer.min = SND_US16X08_KCMIN(kcontrol);
  	uinfo->value.integer.step = SND_US16X08_KCSTEP(kcontrol);
  	return 0;
  }
  
  static int snd_us16x08_comp_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
427
  	struct snd_us16x08_comp_store *store = elem->private_data;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
428
429
430
431
432
433
434
435
436
437
438
439
440
  	int index = ucontrol->id.index;
  	int val_idx = COMP_STORE_IDX(elem->head.id);
  
  	ucontrol->value.integer.value[0] = store->val[val_idx][index];
  
  	return 0;
  }
  
  static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
441
  	struct snd_us16x08_comp_store *store = elem->private_data;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
442
443
444
  	int index = ucontrol->id.index;
  	char buf[sizeof(comp_msg)];
  	int val_idx, val;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
445
  	int err;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
446
447
448
449
450
451
452
  
  	val = ucontrol->value.integer.value[0];
  
  	/* sanity check */
  	if (val < SND_US16X08_KCMIN(kcontrol)
  		|| val > SND_US16X08_KCMAX(kcontrol))
  		return -EINVAL;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
453
454
  	/* new control value incl. bias*/
  	val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
455
  	store->val[val_idx][index] = ucontrol->value.integer.value[0];
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
456
457
  	/* prepare compressor URB message from template  */
  	memcpy(buf, comp_msg, sizeof(comp_msg));
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  	/* place comp values in message buffer watch bias! */
  	buf[8] = store->val[
  		COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][index]
  		- SND_US16X08_COMP_THRESHOLD_BIAS;
  	buf[11] = ratio_map[store->val[
  		COMP_STORE_IDX(SND_US16X08_ID_COMP_RATIO)][index]];
  	buf[14] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_ATTACK)][index]
  		+ SND_US16X08_COMP_ATTACK_BIAS;
  	buf[17] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RELEASE)][index]
  		+ SND_US16X08_COMP_RELEASE_BIAS;
  	buf[20] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_GAIN)][index];
  	buf[26] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)][index];
  
  	/* place channel selector in message buffer */
  	buf[5] = index + 1;
  
  	err = snd_us16x08_send_urb(chip, buf, sizeof(comp_msg));
  
  	if (err > 0) {
  		elem->cached |= 1 << index;
  		elem->cache_val[index] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set compressor, err:%d
  ", err);
  	}
  
  	return 1;
  }
  
  static int snd_us16x08_eqswitch_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
490
  	int val;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
491
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
492
  	struct snd_us16x08_eq_store *store = elem->private_data;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  	int index = ucontrol->id.index;
  
  	/* get low switch from cache is enough, cause all bands are together */
  	val = store->val[EQ_STORE_BAND_IDX(elem->head.id)]
  		[EQ_STORE_PARAM_IDX(elem->head.id)][index];
  	ucontrol->value.integer.value[0] = val;
  
  	return 0;
  }
  
  static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
508
  	struct snd_us16x08_eq_store *store = elem->private_data;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
509
  	int index = ucontrol->id.index;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
510
511
512
513
514
515
516
517
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
  	char buf[sizeof(eqs_msq)];
  	int val, err = 0;
  	int b_idx;
  
  	/* new control value incl. bias*/
  	val = ucontrol->value.integer.value[0] + SND_US16X08_KCBIAS(kcontrol);
  
  	/* prepare URB message from EQ template */
  	memcpy(buf, eqs_msq, sizeof(eqs_msq));
  
  	/* place channel index in URB message */
  	buf[5] = index + 1;
  	for (b_idx = 0; b_idx < SND_US16X08_ID_EQ_BAND_COUNT; b_idx++) {
  		/* all four EQ bands have to be enabled/disabled in once */
  		buf[20] = val;
  		buf[17] = store->val[b_idx][2][index];
  		buf[14] = store->val[b_idx][1][index];
  		buf[11] = store->val[b_idx][0][index];
  		buf[8] = b_idx + 1;
  		err = snd_us16x08_send_urb(chip, buf, sizeof(eqs_msq));
  		if (err < 0)
  			break;
  		store->val[b_idx][3][index] = val;
  		msleep(15);
  	}
  
  	if (err > 0) {
  		elem->cached |= 1 << index;
  		elem->cache_val[index] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set eq switch, err:%d
  ", err);
  	}
  
  	return 1;
  }
  
  static int snd_us16x08_eq_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
550
  	int val;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
551
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
552
  	struct snd_us16x08_eq_store *store = elem->private_data;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  	int index = ucontrol->id.index;
  	int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
  	int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
  
  	val = store->val[b_idx][p_idx][index];
  
  	ucontrol->value.integer.value[0] = val;
  
  	return 0;
  }
  
  static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
569
  	struct snd_us16x08_eq_store *store = elem->private_data;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
570
571
  	int index = ucontrol->id.index;
  	char buf[sizeof(eqs_msq)];
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
572
  	int val, err;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
573
574
  	int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
  	int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
575
576
577
578
579
580
  	val = ucontrol->value.integer.value[0];
  
  	/* sanity check */
  	if (val < SND_US16X08_KCMIN(kcontrol)
  		|| val > SND_US16X08_KCMAX(kcontrol))
  		return -EINVAL;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
581
582
  	/* copy URB buffer from EQ template */
  	memcpy(buf, eqs_msq, sizeof(eqs_msq));
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
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
618
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
  	store->val[b_idx][p_idx][index] = val;
  	buf[20] = store->val[b_idx][3][index];
  	buf[17] = store->val[b_idx][2][index];
  	buf[14] = store->val[b_idx][1][index];
  	buf[11] = store->val[b_idx][0][index];
  
  	/* place channel index in URB buffer */
  	buf[5] = index + 1;
  
  	/* place EQ band in URB buffer */
  	buf[8] = b_idx + 1;
  
  	err = snd_us16x08_send_urb(chip, buf, sizeof(eqs_msq));
  
  	if (err > 0) {
  		/* store new value in EQ band cache */
  		elem->cached |= 1 << index;
  		elem->cache_val[index] = val;
  	} else {
  		usb_audio_dbg(chip, "Failed to set eq param, err:%d
  ", err);
  	}
  
  	return 1;
  }
  
  static int snd_us16x08_meter_info(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->count = 1;
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->value.integer.max = 0x7FFF;
  	uinfo->value.integer.min = 0;
  
  	return 0;
  }
  
  /* calculate compressor index for reduction level request */
  static int snd_get_meter_comp_index(struct snd_us16x08_meter_store *store)
  {
  	int ret;
  
  	/* any channel active */
  	if (store->comp_active_index) {
  		/* check for stereo link */
  		if (store->comp_active_index & 0x20) {
  			/* reset comp_index to left channel*/
  			if (store->comp_index -
  				store->comp_active_index > 1)
  				store->comp_index =
  				store->comp_active_index;
  
  			ret = store->comp_index++ & 0x1F;
  		} else {
  			/* no stereo link */
  			ret = store->comp_active_index;
  		}
  	} else {
  		/* skip channels with no compressor active */
  		while (!store->comp_store->val[
  			COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)]
  			[store->comp_index - 1]
  			&& store->comp_index <= SND_US16X08_MAX_CHANNELS) {
  			store->comp_index++;
  		}
  		ret = store->comp_index++;
  		if (store->comp_index > SND_US16X08_MAX_CHANNELS)
  			store->comp_index = 1;
  	}
  	return ret;
  }
  
  /* retrieve the meter level values from URB message */
  static void get_meter_levels_from_urb(int s,
  	struct snd_us16x08_meter_store *store,
  	u8 *meter_urb)
  {
  	int val = MUC2(meter_urb, s) + (MUC3(meter_urb, s) << 8);
  
  	if (MUA0(meter_urb, s) == 0x61 && MUA1(meter_urb, s) == 0x02 &&
  		MUA2(meter_urb, s) == 0x04 && MUB0(meter_urb, s) == 0x62) {
  		if (MUC0(meter_urb, s) == 0x72)
  			store->meter_level[MUB2(meter_urb, s) - 1] = val;
  		if (MUC0(meter_urb, s) == 0xb2)
  			store->comp_level[MUB2(meter_urb, s) - 1] = val;
  	}
  	if (MUA0(meter_urb, s) == 0x61 && MUA1(meter_urb, s) == 0x02 &&
  		MUA2(meter_urb, s) == 0x02 && MUB0(meter_urb, s) == 0x62)
  		store->master_level[MUB2(meter_urb, s) - 1] = val;
  }
  
  /* Function to retrieve current meter values from the device.
   *
   * The device needs to be polled for meter values with an initial
   * requests. It will return with a sequence of different meter value
   * packages. The first request (case 0:) initiate this meter response sequence.
   * After the third response, an additional request can be placed,
   * to retrieve compressor reduction level value for given channel. This round
   * trip channel selector will skip all inactive compressors.
   * A mixer can interrupt this round-trip by selecting one ore two (stereo-link)
   * specific channels.
   */
  static int snd_us16x08_meter_get(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	int i, set;
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_usb_audio *chip = elem->head.mixer->chip;
  	struct snd_us16x08_meter_store *store = elem->private_data;
  	u8 meter_urb[64];
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
693

d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
694
  	switch (kcontrol->private_value) {
e49a14fa3   Takashi Iwai   ALSA: usb: Avoid ...
695
696
  	case 0: {
  		char tmp[sizeof(mix_init_msg1)];
64188cfbe   Takashi Iwai   Revert "ALSA: usb...
697
698
  		memcpy(tmp, mix_init_msg1, sizeof(mix_init_msg1));
  		snd_us16x08_send_urb(chip, tmp, 4);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
699
700
701
702
  		snd_us16x08_recv_urb(chip, meter_urb,
  			sizeof(meter_urb));
  		kcontrol->private_value++;
  		break;
e49a14fa3   Takashi Iwai   ALSA: usb: Avoid ...
703
  	}
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
704
705
706
707
708
709
710
711
712
713
  	case 1:
  		snd_us16x08_recv_urb(chip, meter_urb,
  			sizeof(meter_urb));
  		kcontrol->private_value++;
  		break;
  	case 2:
  		snd_us16x08_recv_urb(chip, meter_urb,
  			sizeof(meter_urb));
  		kcontrol->private_value++;
  		break;
e49a14fa3   Takashi Iwai   ALSA: usb: Avoid ...
714
715
  	case 3: {
  		char tmp[sizeof(mix_init_msg2)];
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
716
717
  		memcpy(tmp, mix_init_msg2, sizeof(mix_init_msg2));
  		tmp[2] = snd_get_meter_comp_index(store);
64188cfbe   Takashi Iwai   Revert "ALSA: usb...
718
  		snd_us16x08_send_urb(chip, tmp, 10);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
719
720
721
722
723
  		snd_us16x08_recv_urb(chip, meter_urb,
  			sizeof(meter_urb));
  		kcontrol->private_value = 0;
  		break;
  	}
e49a14fa3   Takashi Iwai   ALSA: usb: Avoid ...
724
  	}
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
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
  
  	for (set = 0; set < 6; set++)
  		get_meter_levels_from_urb(set, store, meter_urb);
  
  	for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
  		ucontrol->value.integer.value[i] =
  			store ? store->meter_level[i] : 0;
  	}
  
  	ucontrol->value.integer.value[i++] = store ? store->master_level[0] : 0;
  	ucontrol->value.integer.value[i++] = store ? store->master_level[1] : 0;
  
  	for (i = 2; i < SND_US16X08_MAX_CHANNELS + 2; i++)
  		ucontrol->value.integer.value[i + SND_US16X08_MAX_CHANNELS] =
  		store ? store->comp_level[i - 2] : 0;
  
  	return 1;
  }
  
  static int snd_us16x08_meter_put(struct snd_kcontrol *kcontrol,
  	struct snd_ctl_elem_value *ucontrol)
  {
  	struct usb_mixer_elem_info *elem = kcontrol->private_data;
  	struct snd_us16x08_meter_store *store = elem->private_data;
  	int val;
  
  	val = ucontrol->value.integer.value[0];
  
  	/* sanity check */
  	if (val < 0 || val >= SND_US16X08_MAX_CHANNELS)
  		return -EINVAL;
  
  	store->comp_active_index = val;
  	store->comp_index = val;
  
  	return 1;
  }
  
  static struct snd_kcontrol_new snd_us16x08_ch_boolean_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_switch_info,
  	.get = snd_us16x08_channel_get,
  	.put = snd_us16x08_channel_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  };
  
  static struct snd_kcontrol_new snd_us16x08_ch_int_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_channel_get,
  	.put = snd_us16x08_channel_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 133)
  };
  
  static struct snd_kcontrol_new snd_us16x08_pan_int_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_channel_get,
  	.put = snd_us16x08_channel_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 255)
  };
  
  static struct snd_kcontrol_new snd_us16x08_master_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 1,
  	.info = snd_us16x08_master_info,
  	.get = snd_us16x08_master_get,
  	.put = snd_us16x08_master_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 133)
  };
  
  static struct snd_kcontrol_new snd_us16x08_route_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 8,
  	.info = snd_us16x08_route_info,
  	.get = snd_us16x08_route_get,
  	.put = snd_us16x08_route_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 9)
  };
  
  static struct snd_kcontrol_new snd_us16x08_bus_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 1,
  	.info = snd_us16x08_switch_info,
  	.get = snd_us16x08_bus_get,
  	.put = snd_us16x08_bus_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  };
  
  static struct snd_kcontrol_new snd_us16x08_compswitch_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_switch_info,
  	.get = snd_us16x08_comp_get,
  	.put = snd_us16x08_comp_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  };
  
  static struct snd_kcontrol_new snd_us16x08_comp_threshold_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_comp_get,
  	.put = snd_us16x08_comp_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_COMP_THRESHOLD_BIAS, 1,
  	0, 0x20)
  };
  
  static struct snd_kcontrol_new snd_us16x08_comp_ratio_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_comp_get,
  	.put = snd_us16x08_comp_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0,
  	sizeof(ratio_map) - 1), /*max*/
  };
  
  static struct snd_kcontrol_new snd_us16x08_comp_gain_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_comp_get,
  	.put = snd_us16x08_comp_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x14)
  };
  
  static struct snd_kcontrol_new snd_us16x08_comp_attack_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_comp_get,
  	.put = snd_us16x08_comp_put,
  	.private_value =
  	SND_US16X08_KCSET(SND_US16X08_COMP_ATTACK_BIAS, 1, 0, 0xc6),
  };
  
  static struct snd_kcontrol_new snd_us16x08_comp_release_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_comp_get,
  	.put = snd_us16x08_comp_put,
  	.private_value =
  	SND_US16X08_KCSET(SND_US16X08_COMP_RELEASE_BIAS, 1, 0, 0x63),
  };
  
  static struct snd_kcontrol_new snd_us16x08_eq_gain_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_eq_get,
  	.put = snd_us16x08_eq_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 24),
  };
  
  static struct snd_kcontrol_new snd_us16x08_eq_low_freq_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_eq_get,
  	.put = snd_us16x08_eq_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x1F),
  };
  
  static struct snd_kcontrol_new snd_us16x08_eq_mid_freq_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_eq_get,
  	.put = snd_us16x08_eq_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x3F)
  };
  
  static struct snd_kcontrol_new snd_us16x08_eq_mid_width_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_eq_get,
  	.put = snd_us16x08_eq_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x06)
  };
  
  static struct snd_kcontrol_new snd_us16x08_eq_high_freq_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_mix_info,
  	.get = snd_us16x08_eq_get,
  	.put = snd_us16x08_eq_put,
  	.private_value =
  	SND_US16X08_KCSET(SND_US16X08_EQ_HIGHFREQ_BIAS, 1, 0, 0x1F)
  };
  
  static struct snd_kcontrol_new snd_us16x08_eq_switch_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 16,
  	.info = snd_us16x08_switch_info,
  	.get = snd_us16x08_eqswitch_get,
  	.put = snd_us16x08_eqswitch_put,
  	.private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  };
  
  static struct snd_kcontrol_new snd_us16x08_meter_ctl = {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  	.count = 1,
  	.info = snd_us16x08_meter_info,
  	.get = snd_us16x08_meter_get,
  	.put = snd_us16x08_meter_put
  };
  
  /* control store preparation */
  
  /* setup compressor store and assign default value */
  static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void)
  {
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
962
963
  	int i;
  	struct snd_us16x08_comp_store *tmp;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
964

7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
965
966
  	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  	if (!tmp)
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
  		return NULL;
  
  	for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
  		tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][i]
  			= 0x20;
  		tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RATIO)][i] = 0x00;
  		tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_GAIN)][i] = 0x00;
  		tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)][i] = 0x00;
  		tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_ATTACK)][i] = 0x00;
  		tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RELEASE)][i] = 0x00;
  	}
  	return tmp;
  }
  
  /* setup EQ store and assign default values */
  static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void)
  {
  	int i, b_idx;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
985
  	struct snd_us16x08_eq_store *tmp;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
986

7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
987
988
  	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  	if (!tmp)
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  		return NULL;
  
  	for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
  		for (b_idx = 0; b_idx < SND_US16X08_ID_EQ_BAND_COUNT; b_idx++) {
  			tmp->val[b_idx][0][i] = 0x0c;
  			tmp->val[b_idx][3][i] = 0x00;
  			switch (b_idx) {
  			case 0: /* EQ Low */
  				tmp->val[b_idx][1][i] = 0x05;
  				tmp->val[b_idx][2][i] = 0xff;
  				break;
  			case 1: /* EQ Mid low */
  				tmp->val[b_idx][1][i] = 0x0e;
  				tmp->val[b_idx][2][i] = 0x02;
  				break;
  			case 2: /* EQ Mid High */
  				tmp->val[b_idx][1][i] = 0x1b;
  				tmp->val[b_idx][2][i] = 0x02;
  				break;
  			case 3: /* EQ High */
  				tmp->val[b_idx][1][i] = 0x2f
  					- SND_US16X08_EQ_HIGHFREQ_BIAS;
  				tmp->val[b_idx][2][i] = 0xff;
  				break;
  			}
  		}
  	}
  	return tmp;
  }
34371d236   Takashi Sakamoto   ALSA: usb-audio: ...
1018
  static struct snd_us16x08_meter_store *snd_us16x08_create_meter_store(void)
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1019
  {
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1020
  	struct snd_us16x08_meter_store *tmp;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1021

7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1022
  	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1023
1024
1025
1026
1027
  	if (!tmp)
  		return NULL;
  	tmp->comp_index = 1;
  	tmp->comp_active_index = 0;
  	return tmp;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1028
  }
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  /* release elem->private_free as well; called only once for each *_store */
  static void elem_private_free(struct snd_kcontrol *kctl)
  {
  	struct usb_mixer_elem_info *elem = kctl->private_data;
  
  	if (elem)
  		kfree(elem->private_data);
  	kfree(elem);
  	kctl->private_data = NULL;
  }
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1039
1040
1041
  static int add_new_ctl(struct usb_mixer_interface *mixer,
  	const struct snd_kcontrol_new *ncontrol,
  	int index, int val_type, int channels,
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1042
  	const char *name, void *opt,
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1043
  	bool do_private_free,
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
  	struct usb_mixer_elem_info **elem_ret)
  {
  	struct snd_kcontrol *kctl;
  	struct usb_mixer_elem_info *elem;
  	int err;
  
  	usb_audio_dbg(mixer->chip, "us16x08 add mixer %s
  ", name);
  
  	elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  	if (!elem)
  		return -ENOMEM;
  
  	elem->head.mixer = mixer;
  	elem->head.resume = NULL;
  	elem->control = 0;
  	elem->idx_off = 0;
  	elem->head.id = index;
  	elem->val_type = val_type;
  	elem->channels = channels;
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1064
  	elem->private_data = opt;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1065
1066
1067
1068
1069
1070
  
  	kctl = snd_ctl_new1(ncontrol, elem);
  	if (!kctl) {
  		kfree(elem);
  		return -ENOMEM;
  	}
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1071
1072
1073
1074
  	if (do_private_free)
  		kctl->private_free = elem_private_free;
  	else
  		kctl->private_free = snd_usb_mixer_elem_free;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
  
  	strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  
  	err = snd_usb_mixer_add_control(&elem->head, kctl);
  	if (err < 0)
  		return err;
  
  	if (elem_ret)
  		*elem_ret = elem;
  
  	return 0;
  }
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1087
  /* table of EQ controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1088
  static const struct snd_us16x08_control_params eq_controls[] = {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1089
1090
1091
1092
1093
1094
  	{ /* EQ switch */
  		.kcontrol_new = &snd_us16x08_eq_switch_ctl,
  		.control_id = SND_US16X08_ID_EQENABLE,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "EQ Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1095
1096
1097
1098
1099
1100
1101
  	},
  	{ /* EQ low gain */
  		.kcontrol_new = &snd_us16x08_eq_gain_ctl,
  		.control_id = SND_US16X08_ID_EQLOWLEVEL,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ Low Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1102
1103
1104
1105
1106
1107
1108
  	},
  	{ /* EQ low freq */
  		.kcontrol_new = &snd_us16x08_eq_low_freq_ctl,
  		.control_id = SND_US16X08_ID_EQLOWFREQ,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ Low Frequence",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1109
1110
1111
1112
1113
1114
1115
  	},
  	{ /* EQ mid low gain */
  		.kcontrol_new = &snd_us16x08_eq_gain_ctl,
  		.control_id = SND_US16X08_ID_EQLOWMIDLEVEL,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ MidLow Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1116
1117
1118
1119
1120
1121
1122
  	},
  	{ /* EQ mid low freq */
  		.kcontrol_new = &snd_us16x08_eq_mid_freq_ctl,
  		.control_id = SND_US16X08_ID_EQLOWMIDFREQ,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ MidLow Frequence",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1123
1124
1125
1126
1127
1128
  	},
  	{ /* EQ mid low Q */
  		.kcontrol_new = &snd_us16x08_eq_mid_width_ctl,
  		.control_id = SND_US16X08_ID_EQLOWMIDWIDTH,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
617163fc2   Takashi Iwai   ALSA: usb: Fix a ...
1129
  		.name = "EQ MidLow Q",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1130
1131
1132
1133
1134
1135
1136
  	},
  	{ /* EQ mid high gain */
  		.kcontrol_new = &snd_us16x08_eq_gain_ctl,
  		.control_id = SND_US16X08_ID_EQHIGHMIDLEVEL,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ MidHigh Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1137
1138
1139
1140
1141
1142
1143
  	},
  	{ /* EQ mid high freq */
  		.kcontrol_new = &snd_us16x08_eq_mid_freq_ctl,
  		.control_id = SND_US16X08_ID_EQHIGHMIDFREQ,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ MidHigh Frequence",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1144
1145
1146
1147
1148
1149
1150
  	},
  	{ /* EQ mid high Q */
  		.kcontrol_new = &snd_us16x08_eq_mid_width_ctl,
  		.control_id = SND_US16X08_ID_EQHIGHMIDWIDTH,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ MidHigh Q",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1151
1152
1153
1154
1155
1156
1157
  	},
  	{ /* EQ high gain */
  		.kcontrol_new = &snd_us16x08_eq_gain_ctl,
  		.control_id = SND_US16X08_ID_EQHIGHLEVEL,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ High Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1158
1159
1160
1161
1162
1163
1164
  	},
  	{ /* EQ low freq */
  		.kcontrol_new = &snd_us16x08_eq_high_freq_ctl,
  		.control_id = SND_US16X08_ID_EQHIGHFREQ,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "EQ High Frequence",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1165
1166
1167
1168
  	},
  };
  
  /* table of compressor controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1169
  static const struct snd_us16x08_control_params comp_controls[] = {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1170
1171
1172
1173
1174
1175
  	{ /* Comp enable */
  		.kcontrol_new = &snd_us16x08_compswitch_ctl,
  		.control_id = SND_US16X08_ID_COMP_SWITCH,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "Compressor Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1176
1177
1178
1179
1180
1181
1182
  	},
  	{ /* Comp threshold */
  		.kcontrol_new = &snd_us16x08_comp_threshold_ctl,
  		.control_id = SND_US16X08_ID_COMP_THRESHOLD,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Compressor Threshold Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1183
1184
1185
1186
1187
1188
1189
  	},
  	{ /* Comp ratio */
  		.kcontrol_new = &snd_us16x08_comp_ratio_ctl,
  		.control_id = SND_US16X08_ID_COMP_RATIO,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Compressor Ratio",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1190
1191
1192
1193
1194
1195
1196
  	},
  	{ /* Comp attack */
  		.kcontrol_new = &snd_us16x08_comp_attack_ctl,
  		.control_id = SND_US16X08_ID_COMP_ATTACK,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Compressor Attack",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1197
1198
1199
1200
1201
1202
1203
  	},
  	{ /* Comp release */
  		.kcontrol_new = &snd_us16x08_comp_release_ctl,
  		.control_id = SND_US16X08_ID_COMP_RELEASE,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Compressor Release",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1204
1205
1206
1207
1208
1209
1210
  	},
  	{ /* Comp gain */
  		.kcontrol_new = &snd_us16x08_comp_gain_ctl,
  		.control_id = SND_US16X08_ID_COMP_GAIN,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Compressor Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1211
1212
1213
1214
  	},
  };
  
  /* table of channel controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1215
  static const struct snd_us16x08_control_params channel_controls[] = {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1216
1217
1218
1219
1220
1221
  	{ /* Phase */
  		.kcontrol_new = &snd_us16x08_ch_boolean_ctl,
  		.control_id = SND_US16X08_ID_PHASE,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "Phase Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1222
1223
1224
1225
1226
1227
1228
1229
  		.default_val = 0
  	},
  	{ /* Fader */
  		.kcontrol_new = &snd_us16x08_ch_int_ctl,
  		.control_id = SND_US16X08_ID_FADER,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Line Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1230
1231
1232
1233
1234
1235
1236
1237
  		.default_val = 127
  	},
  	{ /* Mute */
  		.kcontrol_new = &snd_us16x08_ch_boolean_ctl,
  		.control_id = SND_US16X08_ID_MUTE,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "Mute Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1238
1239
1240
1241
1242
1243
1244
1245
  		.default_val = 0
  	},
  	{ /* Pan */
  		.kcontrol_new = &snd_us16x08_pan_int_ctl,
  		.control_id = SND_US16X08_ID_PAN,
  		.type = USB_MIXER_U16,
  		.num_channels = 16,
  		.name = "Pan Left-Right Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1246
1247
1248
1249
1250
  		.default_val = 127
  	},
  };
  
  /* table of master controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1251
  static const struct snd_us16x08_control_params master_controls[] = {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1252
1253
1254
1255
1256
1257
  	{ /* Master */
  		.kcontrol_new = &snd_us16x08_master_ctl,
  		.control_id = SND_US16X08_ID_FADER,
  		.type = USB_MIXER_U8,
  		.num_channels = 16,
  		.name = "Master Volume",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1258
1259
1260
1261
1262
1263
1264
1265
  		.default_val = 127
  	},
  	{ /* Bypass */
  		.kcontrol_new = &snd_us16x08_bus_ctl,
  		.control_id = SND_US16X08_ID_BYPASS,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "DSP Bypass Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1266
1267
1268
1269
1270
1271
1272
1273
  		.default_val = 0
  	},
  	{ /* Buss out */
  		.kcontrol_new = &snd_us16x08_bus_ctl,
  		.control_id = SND_US16X08_ID_BUSS_OUT,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "Buss Out Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1274
1275
1276
1277
1278
1279
1280
1281
  		.default_val = 0
  	},
  	{ /* Master mute */
  		.kcontrol_new = &snd_us16x08_bus_ctl,
  		.control_id = SND_US16X08_ID_MUTE,
  		.type = USB_MIXER_BOOLEAN,
  		.num_channels = 16,
  		.name = "Master Mute Switch",
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
  		.default_val = 0
  	},
  
  };
  
  int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
  {
  	int i, j;
  	int err;
  	struct usb_mixer_elem_info *elem;
  	struct snd_us16x08_comp_store *comp_store;
  	struct snd_us16x08_meter_store *meter_store;
  	struct snd_us16x08_eq_store *eq_store;
  
  	/* just check for non-MIDI interface */
  	if (mixer->hostif->desc.bInterfaceNumber == 3) {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1298
1299
1300
  		/* add routing control */
  		err = add_new_ctl(mixer, &snd_us16x08_route_ctl,
  			SND_US16X08_ID_ROUTE, USB_MIXER_U8, 8, "Line Out Route",
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1301
  			NULL, false, &elem);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
  		if (err < 0) {
  			usb_audio_dbg(mixer->chip,
  				"Failed to create route control, err:%d
  ",
  				err);
  			return err;
  		}
  		for (i = 0; i < 8; i++)
  			elem->cache_val[i] = i < 2 ? i : i + 2;
  		elem->cached = 0xff;
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1312
1313
1314
1315
  		/* create compressor mixer elements */
  		comp_store = snd_us16x08_create_comp_store();
  		if (!comp_store)
  			return -ENOMEM;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1316
  		/* add master controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1317
  		for (i = 0; i < ARRAY_SIZE(master_controls); i++) {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1318
1319
1320
1321
1322
1323
1324
1325
  
  			err = add_new_ctl(mixer,
  				master_controls[i].kcontrol_new,
  				master_controls[i].control_id,
  				master_controls[i].type,
  				master_controls[i].num_channels,
  				master_controls[i].name,
  				comp_store,
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1326
1327
  				i == 0, /* release comp_store only once */
  				&elem);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1328
1329
1330
1331
1332
1333
1334
  			if (err < 0)
  				return err;
  			elem->cache_val[0] = master_controls[i].default_val;
  			elem->cached = 1;
  		}
  
  		/* add channel controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1335
  		for (i = 0; i < ARRAY_SIZE(channel_controls); i++) {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1336
1337
1338
1339
1340
1341
1342
1343
  
  			err = add_new_ctl(mixer,
  				channel_controls[i].kcontrol_new,
  				channel_controls[i].control_id,
  				channel_controls[i].type,
  				channel_controls[i].num_channels,
  				channel_controls[i].name,
  				comp_store,
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1344
  				false, &elem);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1345
1346
1347
1348
1349
1350
1351
1352
  			if (err < 0)
  				return err;
  			for (j = 0; j < SND_US16X08_MAX_CHANNELS; j++) {
  				elem->cache_val[j] =
  					channel_controls[i].default_val;
  			}
  			elem->cached = 0xffff;
  		}
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1353
1354
1355
1356
  		/* create eq store */
  		eq_store = snd_us16x08_create_eq_store();
  		if (!eq_store)
  			return -ENOMEM;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1357
  		/* add EQ controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1358
  		for (i = 0; i < ARRAY_SIZE(eq_controls); i++) {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1359
1360
1361
1362
1363
1364
1365
1366
  
  			err = add_new_ctl(mixer,
  				eq_controls[i].kcontrol_new,
  				eq_controls[i].control_id,
  				eq_controls[i].type,
  				eq_controls[i].num_channels,
  				eq_controls[i].name,
  				eq_store,
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1367
1368
  				i == 0, /* release eq_store only once */
  				NULL);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1369
1370
1371
1372
1373
  			if (err < 0)
  				return err;
  		}
  
  		/* add compressor controls */
7086b7b3d   Takashi Iwai   ALSA: usb-audio: ...
1374
  		for (i = 0; i < ARRAY_SIZE(comp_controls); i++) {
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1375
1376
1377
1378
1379
1380
1381
1382
  
  			err = add_new_ctl(mixer,
  				comp_controls[i].kcontrol_new,
  				comp_controls[i].control_id,
  				comp_controls[i].type,
  				comp_controls[i].num_channels,
  				comp_controls[i].name,
  				comp_store,
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1383
  				false, NULL);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1384
1385
1386
  			if (err < 0)
  				return err;
  		}
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1387
1388
1389
1390
  		/* create meters store */
  		meter_store = snd_us16x08_create_meter_store();
  		if (!meter_store)
  			return -ENOMEM;
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1391
1392
1393
1394
1395
1396
  		/* meter function 'get' must access to compressor store
  		 * so place a reference here
  		 */
  		meter_store->comp_store = comp_store;
  		err = add_new_ctl(mixer, &snd_us16x08_meter_ctl,
  			SND_US16X08_ID_METER, USB_MIXER_U16, 0, "Level Meter",
e2810d76c   Takashi Iwai   ALSA: usb-audio: ...
1397
  			meter_store, true, NULL);
d2bb390a2   Detlef Urban   ALSA: usb-audio: ...
1398
1399
1400
1401
1402
1403
  		if (err < 0)
  			return err;
  	}
  
  	return 0;
  }