ALSA: usb-audio: Fix memory leak and corruption in mixer_us16x08.c
[linux-block.git] / sound / usb / mixer_us16x08.c
CommitLineData
d2bb390a
DU
1/*
2 * Tascam US-16x08 ALSA driver
3 *
4 * Copyright (c) 2016 by Detlef Urban (onkel@paraair.de)
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 */
17
18#include <linux/slab.h>
19#include <linux/usb.h>
20#include <linux/usb/audio-v2.h>
21
22#include <sound/core.h>
23#include <sound/control.h>
24
25#include "usbaudio.h"
26#include "mixer.h"
27#include "helper.h"
28
29#include "mixer_us16x08.h"
30
31/* USB control message templates */
32static const char route_msg[] = {
33 0x61,
34 0x02,
35 0x03, /* input from master (0x02) or input from computer bus (0x03) */
36 0x62,
37 0x02,
38 0x01, /* input index (0x01/0x02 eq. left/right) or bus (0x01-0x08) */
39 0x41,
40 0x01,
41 0x61,
42 0x02,
43 0x01,
44 0x62,
45 0x02,
46 0x01, /* output index (0x01-0x08) */
47 0x42,
48 0x01,
49 0x43,
50 0x01,
51 0x00,
52 0x00
53};
54
55static const char mix_init_msg1[] = {
56 0x71, 0x01, 0x00, 0x00
57};
58
59static const char mix_init_msg2[] = {
60 0x62, 0x02, 0x00, 0x61, 0x02, 0x04, 0xb1, 0x01, 0x00, 0x00
61};
62
63static const char mix_msg_in[] = {
64 /* default message head, equal to all mixers */
65 0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
66 0x81, /* 0x06: Controller ID */
67 0x02, /* 0x07: */
68 0x00, /* 0x08: Value of common mixer */
69 0x00,
70 0x00
71};
72
73static const char mix_msg_out[] = {
74 /* default message head, equal to all mixers */
75 0x61, 0x02, 0x02, 0x62, 0x02, 0x01,
76 0x81, /* 0x06: Controller ID */
77 0x02, /* 0x07: */
78 0x00, /* 0x08: Value of common mixer */
79 0x00,
80 0x00
81};
82
83static const char bypass_msg_out[] = {
84 0x45,
85 0x02,
86 0x01, /* on/off flag */
87 0x00,
88 0x00
89};
90
91static const char bus_msg_out[] = {
92 0x44,
93 0x02,
94 0x01, /* on/off flag */
95 0x00,
96 0x00
97};
98
99static const char comp_msg[] = {
100 /* default message head, equal to all mixers */
101 0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
102 0x91,
103 0x02,
104 0xf0, /* 0x08: Threshold db (8) (e0 ... 00) (+-0dB -- -32dB) x-32 */
105 0x92,
106 0x02,
107 0x0a, /* 0x0b: Ratio (0a,0b,0d,0f,11,14,19,1e,23,28,32,3c,50,a0,ff) */
108 0x93,
109 0x02,
110 0x02, /* 0x0e: Attack (0x02 ... 0xc0) (2ms ... 200ms) */
111 0x94,
112 0x02,
113 0x01, /* 0x11: Release (0x01 ... 0x64) (10ms ... 1000ms) x*10 */
114 0x95,
115 0x02,
116 0x03, /* 0x14: gain (0 ... 20) (0dB .. 20dB) */
117 0x96,
118 0x02,
119 0x01,
120 0x97,
121 0x02,
122 0x01, /* 0x1a: main Comp switch (0 ... 1) (off ... on)) */
123 0x00,
124 0x00
125};
126
127static const char eqs_msq[] = {
128 /* default message head, equal to all mixers */
129 0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
130 0x51, /* 0x06: Controller ID */
131 0x02,
132 0x04, /* 0x08: EQ set num (0x01..0x04) (LOW, LOWMID, HIGHMID, HIGH)) */
133 0x52,
134 0x02,
135 0x0c, /* 0x0b: value dB (0 ... 12) (-12db .. +12db) x-6 */
136 0x53,
137 0x02,
138 0x0f, /* 0x0e: value freq (32-47) (1.7kHz..18kHz) */
139 0x54,
140 0x02,
141 0x02, /* 0x11: band width (0-6) (Q16-Q0.25) 2^x/4 (EQ xxMID only) */
142 0x55,
143 0x02,
144 0x01, /* 0x14: main EQ switch (0 ... 1) (off ... on)) */
145 0x00,
146 0x00
147};
148
149/* compressor ratio map */
150static const char ratio_map[] = {
151 0x0a, 0x0b, 0x0d, 0x0f, 0x11, 0x14, 0x19, 0x1e,
152 0x23, 0x28, 0x32, 0x3c, 0x50, 0xa0, 0xff
153};
154
155/* route enumeration names */
02ed051f 156static const char *const route_names[] = {
d2bb390a
DU
157 "Master Left", "Master Right", "Output 1", "Output 2", "Output 3",
158 "Output 4", "Output 5", "Output 6", "Output 7", "Output 8",
159};
160
161static int snd_us16x08_recv_urb(struct snd_usb_audio *chip,
162 unsigned char *buf, int size)
163{
164
165 mutex_lock(&chip->mutex);
166 snd_usb_ctl_msg(chip->dev,
167 usb_rcvctrlpipe(chip->dev, 0),
168 SND_US16X08_URB_METER_REQUEST,
169 SND_US16X08_URB_METER_REQUESTTYPE, 0, 0, buf, size);
170 mutex_unlock(&chip->mutex);
171 return 0;
172}
173
174/* wrapper function to send prepared URB buffer to usb device. Return an error
175 * code if something went wrong
176 */
177static int snd_us16x08_send_urb(struct snd_usb_audio *chip, char *buf, int size)
178{
179 int err = 0;
180
181 if (chip) {
182 err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
183 SND_US16X08_URB_REQUEST, SND_US16X08_URB_REQUESTTYPE,
184 0, 0, buf, size);
185 }
186
187 return err;
188}
189
190static int snd_us16x08_route_info(struct snd_kcontrol *kcontrol,
191 struct snd_ctl_elem_info *uinfo)
192{
193 return snd_ctl_enum_info(uinfo, 1, 10, route_names);
194}
195
196static int snd_us16x08_route_get(struct snd_kcontrol *kcontrol,
197 struct snd_ctl_elem_value *ucontrol)
198{
199 struct usb_mixer_elem_info *elem = kcontrol->private_data;
200 int index = ucontrol->id.index;
201
202 /* route has no bias */
203 ucontrol->value.enumerated.item[0] = elem->cache_val[index];
204
205 return 0;
206}
207
208static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
209 struct snd_ctl_elem_value *ucontrol)
210{
211 struct usb_mixer_elem_info *elem = kcontrol->private_data;
212 struct snd_usb_audio *chip = elem->head.mixer->chip;
213 int index = ucontrol->id.index;
214 char buf[sizeof(route_msg)];
215 int val, val_org, err = 0;
216
217 /* prepare the message buffer from template */
218 memcpy(buf, route_msg, sizeof(route_msg));
219
220 /* get the new value (no bias for routes) */
221 val = ucontrol->value.enumerated.item[0];
222
223 /* sanity check */
224 if (val < 0 || val > 9)
225 return -EINVAL;
226
227 if (val < 2) {
228 /* input comes from a master channel */
229 val_org = val;
230 buf[2] = 0x02;
231 } else {
232 /* input comes from a computer channel */
233 buf[2] = 0x03;
234 val_org = val - 2;
235 }
236
237 /* place new route selection in URB message */
238 buf[5] = (unsigned char) (val_org & 0x0f) + 1;
239 /* place route selector in URB message */
240 buf[13] = index + 1;
241
242 err = snd_us16x08_send_urb(chip, buf, sizeof(route_msg));
243
244 if (err > 0) {
245 elem->cached |= 1 << index;
246 elem->cache_val[index] = val;
247 } else {
248 usb_audio_dbg(chip, "Failed to set routing, err:%d\n", err);
249 }
250
251 return err > 0 ? 1 : 0;
252}
253
254static int snd_us16x08_master_info(struct snd_kcontrol *kcontrol,
255 struct snd_ctl_elem_info *uinfo)
256{
257 uinfo->count = 1;
258 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
259 uinfo->value.integer.max = SND_US16X08_KCMAX(kcontrol);
260 uinfo->value.integer.min = SND_US16X08_KCMIN(kcontrol);
261 uinfo->value.integer.step = SND_US16X08_KCSTEP(kcontrol);
262 return 0;
263}
264
265static int snd_us16x08_master_get(struct snd_kcontrol *kcontrol,
266 struct snd_ctl_elem_value *ucontrol)
267{
268 struct usb_mixer_elem_info *elem = kcontrol->private_data;
269 int index = ucontrol->id.index;
270
271 ucontrol->value.integer.value[0] = elem->cache_val[index];
272
273 return 0;
274}
275
276static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_value *ucontrol)
278{
279 struct usb_mixer_elem_info *elem = kcontrol->private_data;
280 struct snd_usb_audio *chip = elem->head.mixer->chip;
281 char buf[sizeof(mix_msg_out)];
282 int val, err = 0;
283 int index = ucontrol->id.index;
284
285 /* prepare the message buffer from template */
286 memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
287
288 /* new control value incl. bias*/
289 val = ucontrol->value.integer.value[0];
290
291 /* sanity check */
292 if (val < SND_US16X08_KCMIN(kcontrol)
293 || val > SND_US16X08_KCMAX(kcontrol))
294 return -EINVAL;
295
296 buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
297 buf[6] = elem->head.id;
298
299 /* place channel selector in URB message */
300 buf[5] = index + 1;
301 err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_out));
302
303 if (err > 0) {
304 elem->cached |= 1 << index;
305 elem->cache_val[index] = val;
306 } else {
307 usb_audio_dbg(chip, "Failed to set master, err:%d\n", err);
308 }
309
310 return err > 0 ? 1 : 0;
311}
312
313static int snd_us16x08_bus_put(struct snd_kcontrol *kcontrol,
314 struct snd_ctl_elem_value *ucontrol)
315{
316 struct usb_mixer_elem_info *elem = kcontrol->private_data;
317 struct snd_usb_audio *chip = elem->head.mixer->chip;
318 char buf[sizeof(mix_msg_out)];
319 int val, err = 0;
320
321 val = ucontrol->value.integer.value[0];
322
323 /* prepare the message buffer from template */
324 switch (elem->head.id) {
325 case SND_US16X08_ID_BYPASS:
326 memcpy(buf, bypass_msg_out, sizeof(bypass_msg_out));
327 buf[2] = val;
328 err = snd_us16x08_send_urb(chip, buf, sizeof(bypass_msg_out));
329 break;
330 case SND_US16X08_ID_BUSS_OUT:
331 memcpy(buf, bus_msg_out, sizeof(bus_msg_out));
332 buf[2] = val;
333 err = snd_us16x08_send_urb(chip, buf, sizeof(bus_msg_out));
334 break;
335 case SND_US16X08_ID_MUTE:
336 memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
337 buf[8] = val;
338 buf[6] = elem->head.id;
339 buf[5] = 1;
340 err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_out));
341 break;
342 }
343
344 if (err > 0) {
345 elem->cached |= 1;
346 elem->cache_val[0] = val;
347 } else {
348 usb_audio_dbg(chip, "Failed to set buss param, err:%d\n", err);
349 }
350
351 return err > 0 ? 1 : 0;
352}
353
354static int snd_us16x08_bus_get(struct snd_kcontrol *kcontrol,
355 struct snd_ctl_elem_value *ucontrol)
356{
357 struct usb_mixer_elem_info *elem = kcontrol->private_data;
358
359 switch (elem->head.id) {
360 case SND_US16X08_ID_BUSS_OUT:
361 ucontrol->value.integer.value[0] = elem->cache_val[0];
362 break;
363 case SND_US16X08_ID_BYPASS:
364 ucontrol->value.integer.value[0] = elem->cache_val[0];
365 break;
366 case SND_US16X08_ID_MUTE:
367 ucontrol->value.integer.value[0] = elem->cache_val[0];
368 break;
369 }
370
371 return 0;
372}
373
374/* gets a current mixer value from common store */
375static int snd_us16x08_channel_get(struct snd_kcontrol *kcontrol,
376 struct snd_ctl_elem_value *ucontrol)
377{
378 struct usb_mixer_elem_info *elem = kcontrol->private_data;
379 int index = ucontrol->id.index;
380
381 ucontrol->value.integer.value[0] = elem->cache_val[index];
382
383 return 0;
384}
385
386static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,
387 struct snd_ctl_elem_value *ucontrol)
388{
389 struct usb_mixer_elem_info *elem = kcontrol->private_data;
390 struct snd_usb_audio *chip = elem->head.mixer->chip;
391 char buf[sizeof(mix_msg_in)];
392 int val, err;
393 int index = ucontrol->id.index;
394
395 /* prepare URB message from template */
396 memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
397
398 val = ucontrol->value.integer.value[0];
399
400 /* sanity check */
401 if (val < SND_US16X08_KCMIN(kcontrol)
402 || val > SND_US16X08_KCMAX(kcontrol))
403 return -EINVAL;
404
405 /* add the bias to the new value */
406 buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
407 buf[6] = elem->head.id;
408 buf[5] = index + 1;
409
410 err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_in));
411
412 if (err > 0) {
413 elem->cached |= 1 << index;
414 elem->cache_val[index] = val;
415 } else {
416 usb_audio_dbg(chip, "Failed to set channel, err:%d\n", err);
417 }
418
419 return err > 0 ? 1 : 0;
420}
421
422static int snd_us16x08_mix_info(struct snd_kcontrol *kcontrol,
423 struct snd_ctl_elem_info *uinfo)
424{
425 uinfo->count = 1;
426 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
427 uinfo->value.integer.max = SND_US16X08_KCMAX(kcontrol);
428 uinfo->value.integer.min = SND_US16X08_KCMIN(kcontrol);
429 uinfo->value.integer.step = SND_US16X08_KCSTEP(kcontrol);
430 return 0;
431}
432
433static int snd_us16x08_comp_get(struct snd_kcontrol *kcontrol,
434 struct snd_ctl_elem_value *ucontrol)
435{
436 struct usb_mixer_elem_info *elem = kcontrol->private_data;
437 struct snd_us16x08_comp_store *store =
438 ((struct snd_us16x08_comp_store *) elem->private_data);
439 int index = ucontrol->id.index;
440 int val_idx = COMP_STORE_IDX(elem->head.id);
441
442 ucontrol->value.integer.value[0] = store->val[val_idx][index];
443
444 return 0;
445}
446
447static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
448 struct snd_ctl_elem_value *ucontrol)
449{
450 struct usb_mixer_elem_info *elem = kcontrol->private_data;
451 struct snd_usb_audio *chip = elem->head.mixer->chip;
452 struct snd_us16x08_comp_store *store =
453 ((struct snd_us16x08_comp_store *) elem->private_data);
454 int index = ucontrol->id.index;
455 char buf[sizeof(comp_msg)];
456 int val_idx, val;
457 int err = 0;
458
459 /* prepare compressor URB message from template */
460 memcpy(buf, comp_msg, sizeof(comp_msg));
461
462 /* new control value incl. bias*/
463 val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
464
465 val = ucontrol->value.integer.value[0];
466
467 /* sanity check */
468 if (val < SND_US16X08_KCMIN(kcontrol)
469 || val > SND_US16X08_KCMAX(kcontrol))
470 return -EINVAL;
471
472 store->val[val_idx][index] = ucontrol->value.integer.value[0];
473
474 /* place comp values in message buffer watch bias! */
475 buf[8] = store->val[
476 COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][index]
477 - SND_US16X08_COMP_THRESHOLD_BIAS;
478 buf[11] = ratio_map[store->val[
479 COMP_STORE_IDX(SND_US16X08_ID_COMP_RATIO)][index]];
480 buf[14] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_ATTACK)][index]
481 + SND_US16X08_COMP_ATTACK_BIAS;
482 buf[17] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RELEASE)][index]
483 + SND_US16X08_COMP_RELEASE_BIAS;
484 buf[20] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_GAIN)][index];
485 buf[26] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)][index];
486
487 /* place channel selector in message buffer */
488 buf[5] = index + 1;
489
490 err = snd_us16x08_send_urb(chip, buf, sizeof(comp_msg));
491
492 if (err > 0) {
493 elem->cached |= 1 << index;
494 elem->cache_val[index] = val;
495 } else {
496 usb_audio_dbg(chip, "Failed to set compressor, err:%d\n", err);
497 }
498
499 return 1;
500}
501
502static int snd_us16x08_eqswitch_get(struct snd_kcontrol *kcontrol,
503 struct snd_ctl_elem_value *ucontrol)
504{
505 int val = 0;
506 struct usb_mixer_elem_info *elem = kcontrol->private_data;
507 struct snd_us16x08_eq_store *store =
508 ((struct snd_us16x08_eq_store *) elem->private_data);
509 int index = ucontrol->id.index;
510
511 /* get low switch from cache is enough, cause all bands are together */
512 val = store->val[EQ_STORE_BAND_IDX(elem->head.id)]
513 [EQ_STORE_PARAM_IDX(elem->head.id)][index];
514 ucontrol->value.integer.value[0] = val;
515
516 return 0;
517}
518
519static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,
520 struct snd_ctl_elem_value *ucontrol)
521{
522 struct usb_mixer_elem_info *elem = kcontrol->private_data;
523 struct snd_usb_audio *chip = elem->head.mixer->chip;
524 struct snd_us16x08_eq_store *store =
525 ((struct snd_us16x08_eq_store *) elem->private_data);
526 int index = ucontrol->id.index;
527
528 char buf[sizeof(eqs_msq)];
529 int val, err = 0;
530 int b_idx;
531
532 /* new control value incl. bias*/
533 val = ucontrol->value.integer.value[0] + SND_US16X08_KCBIAS(kcontrol);
534
535 /* prepare URB message from EQ template */
536 memcpy(buf, eqs_msq, sizeof(eqs_msq));
537
538 /* place channel index in URB message */
539 buf[5] = index + 1;
540 for (b_idx = 0; b_idx < SND_US16X08_ID_EQ_BAND_COUNT; b_idx++) {
541 /* all four EQ bands have to be enabled/disabled in once */
542 buf[20] = val;
543 buf[17] = store->val[b_idx][2][index];
544 buf[14] = store->val[b_idx][1][index];
545 buf[11] = store->val[b_idx][0][index];
546 buf[8] = b_idx + 1;
547 err = snd_us16x08_send_urb(chip, buf, sizeof(eqs_msq));
548 if (err < 0)
549 break;
550 store->val[b_idx][3][index] = val;
551 msleep(15);
552 }
553
554 if (err > 0) {
555 elem->cached |= 1 << index;
556 elem->cache_val[index] = val;
557 } else {
558 usb_audio_dbg(chip, "Failed to set eq switch, err:%d\n", err);
559 }
560
561 return 1;
562}
563
564static int snd_us16x08_eq_get(struct snd_kcontrol *kcontrol,
565 struct snd_ctl_elem_value *ucontrol)
566{
567 int val = 0;
568 struct usb_mixer_elem_info *elem = kcontrol->private_data;
569 struct snd_us16x08_eq_store *store =
570 ((struct snd_us16x08_eq_store *) elem->private_data);
571 int index = ucontrol->id.index;
572 int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
573 int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
574
575 val = store->val[b_idx][p_idx][index];
576
577 ucontrol->value.integer.value[0] = val;
578
579 return 0;
580}
581
582static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol,
583 struct snd_ctl_elem_value *ucontrol)
584{
585 struct usb_mixer_elem_info *elem = kcontrol->private_data;
586 struct snd_usb_audio *chip = elem->head.mixer->chip;
587 struct snd_us16x08_eq_store *store =
588 ((struct snd_us16x08_eq_store *) elem->private_data);
589 int index = ucontrol->id.index;
590 char buf[sizeof(eqs_msq)];
591 int val, err = 0;
592 int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
593 int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
594
595 /* copy URB buffer from EQ template */
596 memcpy(buf, eqs_msq, sizeof(eqs_msq));
597
598 val = ucontrol->value.integer.value[0];
599
600 /* sanity check */
601 if (val < SND_US16X08_KCMIN(kcontrol)
602 || val > SND_US16X08_KCMAX(kcontrol))
603 return -EINVAL;
604
605 store->val[b_idx][p_idx][index] = val;
606 buf[20] = store->val[b_idx][3][index];
607 buf[17] = store->val[b_idx][2][index];
608 buf[14] = store->val[b_idx][1][index];
609 buf[11] = store->val[b_idx][0][index];
610
611 /* place channel index in URB buffer */
612 buf[5] = index + 1;
613
614 /* place EQ band in URB buffer */
615 buf[8] = b_idx + 1;
616
617 err = snd_us16x08_send_urb(chip, buf, sizeof(eqs_msq));
618
619 if (err > 0) {
620 /* store new value in EQ band cache */
621 elem->cached |= 1 << index;
622 elem->cache_val[index] = val;
623 } else {
624 usb_audio_dbg(chip, "Failed to set eq param, err:%d\n", err);
625 }
626
627 return 1;
628}
629
630static int snd_us16x08_meter_info(struct snd_kcontrol *kcontrol,
631 struct snd_ctl_elem_info *uinfo)
632{
633 uinfo->count = 1;
634 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
635 uinfo->value.integer.max = 0x7FFF;
636 uinfo->value.integer.min = 0;
637
638 return 0;
639}
640
641/* calculate compressor index for reduction level request */
642static int snd_get_meter_comp_index(struct snd_us16x08_meter_store *store)
643{
644 int ret;
645
646 /* any channel active */
647 if (store->comp_active_index) {
648 /* check for stereo link */
649 if (store->comp_active_index & 0x20) {
650 /* reset comp_index to left channel*/
651 if (store->comp_index -
652 store->comp_active_index > 1)
653 store->comp_index =
654 store->comp_active_index;
655
656 ret = store->comp_index++ & 0x1F;
657 } else {
658 /* no stereo link */
659 ret = store->comp_active_index;
660 }
661 } else {
662 /* skip channels with no compressor active */
663 while (!store->comp_store->val[
664 COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)]
665 [store->comp_index - 1]
666 && store->comp_index <= SND_US16X08_MAX_CHANNELS) {
667 store->comp_index++;
668 }
669 ret = store->comp_index++;
670 if (store->comp_index > SND_US16X08_MAX_CHANNELS)
671 store->comp_index = 1;
672 }
673 return ret;
674}
675
676/* retrieve the meter level values from URB message */
677static void get_meter_levels_from_urb(int s,
678 struct snd_us16x08_meter_store *store,
679 u8 *meter_urb)
680{
681 int val = MUC2(meter_urb, s) + (MUC3(meter_urb, s) << 8);
682
683 if (MUA0(meter_urb, s) == 0x61 && MUA1(meter_urb, s) == 0x02 &&
684 MUA2(meter_urb, s) == 0x04 && MUB0(meter_urb, s) == 0x62) {
685 if (MUC0(meter_urb, s) == 0x72)
686 store->meter_level[MUB2(meter_urb, s) - 1] = val;
687 if (MUC0(meter_urb, s) == 0xb2)
688 store->comp_level[MUB2(meter_urb, s) - 1] = val;
689 }
690 if (MUA0(meter_urb, s) == 0x61 && MUA1(meter_urb, s) == 0x02 &&
691 MUA2(meter_urb, s) == 0x02 && MUB0(meter_urb, s) == 0x62)
692 store->master_level[MUB2(meter_urb, s) - 1] = val;
693}
694
695/* Function to retrieve current meter values from the device.
696 *
697 * The device needs to be polled for meter values with an initial
698 * requests. It will return with a sequence of different meter value
699 * packages. The first request (case 0:) initiate this meter response sequence.
700 * After the third response, an additional request can be placed,
701 * to retrieve compressor reduction level value for given channel. This round
702 * trip channel selector will skip all inactive compressors.
703 * A mixer can interrupt this round-trip by selecting one ore two (stereo-link)
704 * specific channels.
705 */
706static int snd_us16x08_meter_get(struct snd_kcontrol *kcontrol,
707 struct snd_ctl_elem_value *ucontrol)
708{
709 int i, set;
710 struct usb_mixer_elem_info *elem = kcontrol->private_data;
711 struct snd_usb_audio *chip = elem->head.mixer->chip;
712 struct snd_us16x08_meter_store *store = elem->private_data;
713 u8 meter_urb[64];
89b593c3 714 char tmp[sizeof(mix_init_msg2)] = {0};
d2bb390a
DU
715
716 if (elem) {
717 store = (struct snd_us16x08_meter_store *) elem->private_data;
718 chip = elem->head.mixer->chip;
719 } else
720 return 0;
721
722 switch (kcontrol->private_value) {
723 case 0:
89b593c3
TS
724 snd_us16x08_send_urb(chip, (char *)mix_init_msg1,
725 sizeof(mix_init_msg1));
d2bb390a
DU
726 snd_us16x08_recv_urb(chip, meter_urb,
727 sizeof(meter_urb));
728 kcontrol->private_value++;
729 break;
730 case 1:
731 snd_us16x08_recv_urb(chip, meter_urb,
732 sizeof(meter_urb));
733 kcontrol->private_value++;
734 break;
735 case 2:
736 snd_us16x08_recv_urb(chip, meter_urb,
737 sizeof(meter_urb));
738 kcontrol->private_value++;
739 break;
740 case 3:
741 memcpy(tmp, mix_init_msg2, sizeof(mix_init_msg2));
742 tmp[2] = snd_get_meter_comp_index(store);
89b593c3 743 snd_us16x08_send_urb(chip, tmp, sizeof(mix_init_msg2));
d2bb390a
DU
744 snd_us16x08_recv_urb(chip, meter_urb,
745 sizeof(meter_urb));
746 kcontrol->private_value = 0;
747 break;
748 }
749
750 for (set = 0; set < 6; set++)
751 get_meter_levels_from_urb(set, store, meter_urb);
752
753 for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
754 ucontrol->value.integer.value[i] =
755 store ? store->meter_level[i] : 0;
756 }
757
758 ucontrol->value.integer.value[i++] = store ? store->master_level[0] : 0;
759 ucontrol->value.integer.value[i++] = store ? store->master_level[1] : 0;
760
761 for (i = 2; i < SND_US16X08_MAX_CHANNELS + 2; i++)
762 ucontrol->value.integer.value[i + SND_US16X08_MAX_CHANNELS] =
763 store ? store->comp_level[i - 2] : 0;
764
765 return 1;
766}
767
768static int snd_us16x08_meter_put(struct snd_kcontrol *kcontrol,
769 struct snd_ctl_elem_value *ucontrol)
770{
771 struct usb_mixer_elem_info *elem = kcontrol->private_data;
772 struct snd_us16x08_meter_store *store = elem->private_data;
773 int val;
774
775 val = ucontrol->value.integer.value[0];
776
777 /* sanity check */
778 if (val < 0 || val >= SND_US16X08_MAX_CHANNELS)
779 return -EINVAL;
780
781 store->comp_active_index = val;
782 store->comp_index = val;
783
784 return 1;
785}
786
787static struct snd_kcontrol_new snd_us16x08_ch_boolean_ctl = {
788 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
789 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
790 .count = 16,
791 .info = snd_us16x08_switch_info,
792 .get = snd_us16x08_channel_get,
793 .put = snd_us16x08_channel_put,
794 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
795};
796
797static struct snd_kcontrol_new snd_us16x08_ch_int_ctl = {
798 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
800 .count = 16,
801 .info = snd_us16x08_mix_info,
802 .get = snd_us16x08_channel_get,
803 .put = snd_us16x08_channel_put,
804 .private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 133)
805};
806
807static struct snd_kcontrol_new snd_us16x08_pan_int_ctl = {
808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
809 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
810 .count = 16,
811 .info = snd_us16x08_mix_info,
812 .get = snd_us16x08_channel_get,
813 .put = snd_us16x08_channel_put,
814 .private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 255)
815};
816
817static struct snd_kcontrol_new snd_us16x08_master_ctl = {
818 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
819 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
820 .count = 1,
821 .info = snd_us16x08_master_info,
822 .get = snd_us16x08_master_get,
823 .put = snd_us16x08_master_put,
824 .private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 133)
825};
826
827static struct snd_kcontrol_new snd_us16x08_route_ctl = {
828 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
830 .count = 8,
831 .info = snd_us16x08_route_info,
832 .get = snd_us16x08_route_get,
833 .put = snd_us16x08_route_put,
834 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 9)
835};
836
837static struct snd_kcontrol_new snd_us16x08_bus_ctl = {
838 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
839 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
840 .count = 1,
841 .info = snd_us16x08_switch_info,
842 .get = snd_us16x08_bus_get,
843 .put = snd_us16x08_bus_put,
844 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
845};
846
847static struct snd_kcontrol_new snd_us16x08_compswitch_ctl = {
848 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
849 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
850 .count = 16,
851 .info = snd_us16x08_switch_info,
852 .get = snd_us16x08_comp_get,
853 .put = snd_us16x08_comp_put,
854 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
855};
856
857static struct snd_kcontrol_new snd_us16x08_comp_threshold_ctl = {
858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
859 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
860 .count = 16,
861 .info = snd_us16x08_mix_info,
862 .get = snd_us16x08_comp_get,
863 .put = snd_us16x08_comp_put,
864 .private_value = SND_US16X08_KCSET(SND_US16X08_COMP_THRESHOLD_BIAS, 1,
865 0, 0x20)
866};
867
868static struct snd_kcontrol_new snd_us16x08_comp_ratio_ctl = {
869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
870 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
871 .count = 16,
872 .info = snd_us16x08_mix_info,
873 .get = snd_us16x08_comp_get,
874 .put = snd_us16x08_comp_put,
875 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0,
876 sizeof(ratio_map) - 1), /*max*/
877};
878
879static struct snd_kcontrol_new snd_us16x08_comp_gain_ctl = {
880 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
881 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
882 .count = 16,
883 .info = snd_us16x08_mix_info,
884 .get = snd_us16x08_comp_get,
885 .put = snd_us16x08_comp_put,
886 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x14)
887};
888
889static struct snd_kcontrol_new snd_us16x08_comp_attack_ctl = {
890 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
891 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
892 .count = 16,
893 .info = snd_us16x08_mix_info,
894 .get = snd_us16x08_comp_get,
895 .put = snd_us16x08_comp_put,
896 .private_value =
897 SND_US16X08_KCSET(SND_US16X08_COMP_ATTACK_BIAS, 1, 0, 0xc6),
898};
899
900static struct snd_kcontrol_new snd_us16x08_comp_release_ctl = {
901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
902 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
903 .count = 16,
904 .info = snd_us16x08_mix_info,
905 .get = snd_us16x08_comp_get,
906 .put = snd_us16x08_comp_put,
907 .private_value =
908 SND_US16X08_KCSET(SND_US16X08_COMP_RELEASE_BIAS, 1, 0, 0x63),
909};
910
911static struct snd_kcontrol_new snd_us16x08_eq_gain_ctl = {
912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
913 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
914 .count = 16,
915 .info = snd_us16x08_mix_info,
916 .get = snd_us16x08_eq_get,
917 .put = snd_us16x08_eq_put,
918 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 24),
919};
920
921static struct snd_kcontrol_new snd_us16x08_eq_low_freq_ctl = {
922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
923 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
924 .count = 16,
925 .info = snd_us16x08_mix_info,
926 .get = snd_us16x08_eq_get,
927 .put = snd_us16x08_eq_put,
928 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x1F),
929};
930
931static struct snd_kcontrol_new snd_us16x08_eq_mid_freq_ctl = {
932 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
933 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
934 .count = 16,
935 .info = snd_us16x08_mix_info,
936 .get = snd_us16x08_eq_get,
937 .put = snd_us16x08_eq_put,
938 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x3F)
939};
940
941static struct snd_kcontrol_new snd_us16x08_eq_mid_width_ctl = {
942 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
943 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
944 .count = 16,
945 .info = snd_us16x08_mix_info,
946 .get = snd_us16x08_eq_get,
947 .put = snd_us16x08_eq_put,
948 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x06)
949};
950
951static struct snd_kcontrol_new snd_us16x08_eq_high_freq_ctl = {
952 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
953 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
954 .count = 16,
955 .info = snd_us16x08_mix_info,
956 .get = snd_us16x08_eq_get,
957 .put = snd_us16x08_eq_put,
958 .private_value =
959 SND_US16X08_KCSET(SND_US16X08_EQ_HIGHFREQ_BIAS, 1, 0, 0x1F)
960};
961
962static struct snd_kcontrol_new snd_us16x08_eq_switch_ctl = {
963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
964 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
965 .count = 16,
966 .info = snd_us16x08_switch_info,
967 .get = snd_us16x08_eqswitch_get,
968 .put = snd_us16x08_eqswitch_put,
969 .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
970};
971
972static struct snd_kcontrol_new snd_us16x08_meter_ctl = {
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
975 .count = 1,
976 .info = snd_us16x08_meter_info,
977 .get = snd_us16x08_meter_get,
978 .put = snd_us16x08_meter_put
979};
980
981/* control store preparation */
982
983/* setup compressor store and assign default value */
984static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void)
985{
986 int i = 0;
987 struct snd_us16x08_comp_store *tmp =
988 kmalloc(sizeof(struct snd_us16x08_comp_store), GFP_KERNEL);
989
990 if (tmp == NULL)
991 return NULL;
992
993 for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
994 tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][i]
995 = 0x20;
996 tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RATIO)][i] = 0x00;
997 tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_GAIN)][i] = 0x00;
998 tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)][i] = 0x00;
999 tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_ATTACK)][i] = 0x00;
1000 tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RELEASE)][i] = 0x00;
1001 }
1002 return tmp;
1003}
1004
1005/* setup EQ store and assign default values */
1006static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void)
1007{
1008 int i, b_idx;
1009 struct snd_us16x08_eq_store *tmp =
1010 kmalloc(sizeof(struct snd_us16x08_eq_store), GFP_KERNEL);
1011
1012 if (tmp == NULL)
1013 return NULL;
1014
1015 for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
1016 for (b_idx = 0; b_idx < SND_US16X08_ID_EQ_BAND_COUNT; b_idx++) {
1017 tmp->val[b_idx][0][i] = 0x0c;
1018 tmp->val[b_idx][3][i] = 0x00;
1019 switch (b_idx) {
1020 case 0: /* EQ Low */
1021 tmp->val[b_idx][1][i] = 0x05;
1022 tmp->val[b_idx][2][i] = 0xff;
1023 break;
1024 case 1: /* EQ Mid low */
1025 tmp->val[b_idx][1][i] = 0x0e;
1026 tmp->val[b_idx][2][i] = 0x02;
1027 break;
1028 case 2: /* EQ Mid High */
1029 tmp->val[b_idx][1][i] = 0x1b;
1030 tmp->val[b_idx][2][i] = 0x02;
1031 break;
1032 case 3: /* EQ High */
1033 tmp->val[b_idx][1][i] = 0x2f
1034 - SND_US16X08_EQ_HIGHFREQ_BIAS;
1035 tmp->val[b_idx][2][i] = 0xff;
1036 break;
1037 }
1038 }
1039 }
1040 return tmp;
1041}
1042
34371d23 1043static struct snd_us16x08_meter_store *snd_us16x08_create_meter_store(void)
d2bb390a
DU
1044{
1045 struct snd_us16x08_meter_store *tmp =
1046 kzalloc(sizeof(struct snd_us16x08_meter_store), GFP_KERNEL);
1047
1048 if (!tmp)
1049 return NULL;
1050 tmp->comp_index = 1;
1051 tmp->comp_active_index = 0;
1052 return tmp;
1053
1054}
1055
e2810d76
TI
1056/* release elem->private_free as well; called only once for each *_store */
1057static void elem_private_free(struct snd_kcontrol *kctl)
1058{
1059 struct usb_mixer_elem_info *elem = kctl->private_data;
1060
1061 if (elem)
1062 kfree(elem->private_data);
1063 kfree(elem);
1064 kctl->private_data = NULL;
1065}
1066
d2bb390a
DU
1067static int add_new_ctl(struct usb_mixer_interface *mixer,
1068 const struct snd_kcontrol_new *ncontrol,
1069 int index, int val_type, int channels,
1070 const char *name, const void *opt,
e2810d76 1071 bool do_private_free,
d2bb390a
DU
1072 struct usb_mixer_elem_info **elem_ret)
1073{
1074 struct snd_kcontrol *kctl;
1075 struct usb_mixer_elem_info *elem;
1076 int err;
1077
1078 usb_audio_dbg(mixer->chip, "us16x08 add mixer %s\n", name);
1079
1080 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1081 if (!elem)
1082 return -ENOMEM;
1083
1084 elem->head.mixer = mixer;
1085 elem->head.resume = NULL;
1086 elem->control = 0;
1087 elem->idx_off = 0;
1088 elem->head.id = index;
1089 elem->val_type = val_type;
1090 elem->channels = channels;
1091 elem->private_data = (void *) opt;
1092
1093 kctl = snd_ctl_new1(ncontrol, elem);
1094 if (!kctl) {
1095 kfree(elem);
1096 return -ENOMEM;
1097 }
1098
e2810d76
TI
1099 if (do_private_free)
1100 kctl->private_free = elem_private_free;
1101 else
1102 kctl->private_free = snd_usb_mixer_elem_free;
d2bb390a
DU
1103
1104 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1105
1106 err = snd_usb_mixer_add_control(&elem->head, kctl);
1107 if (err < 0)
1108 return err;
1109
1110 if (elem_ret)
1111 *elem_ret = elem;
1112
1113 return 0;
1114}
1115
1116static struct snd_us16x08_control_params control_params;
1117
1118/* table of EQ controls */
1119static struct snd_us16x08_control_params eq_controls[] = {
1120 { /* EQ switch */
1121 .kcontrol_new = &snd_us16x08_eq_switch_ctl,
1122 .control_id = SND_US16X08_ID_EQENABLE,
1123 .type = USB_MIXER_BOOLEAN,
1124 .num_channels = 16,
1125 .name = "EQ Switch",
d2bb390a
DU
1126 },
1127 { /* EQ low gain */
1128 .kcontrol_new = &snd_us16x08_eq_gain_ctl,
1129 .control_id = SND_US16X08_ID_EQLOWLEVEL,
1130 .type = USB_MIXER_U8,
1131 .num_channels = 16,
1132 .name = "EQ Low Volume",
d2bb390a
DU
1133 },
1134 { /* EQ low freq */
1135 .kcontrol_new = &snd_us16x08_eq_low_freq_ctl,
1136 .control_id = SND_US16X08_ID_EQLOWFREQ,
1137 .type = USB_MIXER_U8,
1138 .num_channels = 16,
1139 .name = "EQ Low Frequence",
d2bb390a
DU
1140 },
1141 { /* EQ mid low gain */
1142 .kcontrol_new = &snd_us16x08_eq_gain_ctl,
1143 .control_id = SND_US16X08_ID_EQLOWMIDLEVEL,
1144 .type = USB_MIXER_U8,
1145 .num_channels = 16,
1146 .name = "EQ MidLow Volume",
d2bb390a
DU
1147 },
1148 { /* EQ mid low freq */
1149 .kcontrol_new = &snd_us16x08_eq_mid_freq_ctl,
1150 .control_id = SND_US16X08_ID_EQLOWMIDFREQ,
1151 .type = USB_MIXER_U8,
1152 .num_channels = 16,
1153 .name = "EQ MidLow Frequence",
d2bb390a
DU
1154 },
1155 { /* EQ mid low Q */
1156 .kcontrol_new = &snd_us16x08_eq_mid_width_ctl,
1157 .control_id = SND_US16X08_ID_EQLOWMIDWIDTH,
1158 .type = USB_MIXER_U8,
1159 .num_channels = 16,
1160 .name = "EQ MidQLow Q",
d2bb390a
DU
1161 },
1162 { /* EQ mid high gain */
1163 .kcontrol_new = &snd_us16x08_eq_gain_ctl,
1164 .control_id = SND_US16X08_ID_EQHIGHMIDLEVEL,
1165 .type = USB_MIXER_U8,
1166 .num_channels = 16,
1167 .name = "EQ MidHigh Volume",
d2bb390a
DU
1168 },
1169 { /* EQ mid high freq */
1170 .kcontrol_new = &snd_us16x08_eq_mid_freq_ctl,
1171 .control_id = SND_US16X08_ID_EQHIGHMIDFREQ,
1172 .type = USB_MIXER_U8,
1173 .num_channels = 16,
1174 .name = "EQ MidHigh Frequence",
d2bb390a
DU
1175 },
1176 { /* EQ mid high Q */
1177 .kcontrol_new = &snd_us16x08_eq_mid_width_ctl,
1178 .control_id = SND_US16X08_ID_EQHIGHMIDWIDTH,
1179 .type = USB_MIXER_U8,
1180 .num_channels = 16,
1181 .name = "EQ MidHigh Q",
d2bb390a
DU
1182 },
1183 { /* EQ high gain */
1184 .kcontrol_new = &snd_us16x08_eq_gain_ctl,
1185 .control_id = SND_US16X08_ID_EQHIGHLEVEL,
1186 .type = USB_MIXER_U8,
1187 .num_channels = 16,
1188 .name = "EQ High Volume",
d2bb390a
DU
1189 },
1190 { /* EQ low freq */
1191 .kcontrol_new = &snd_us16x08_eq_high_freq_ctl,
1192 .control_id = SND_US16X08_ID_EQHIGHFREQ,
1193 .type = USB_MIXER_U8,
1194 .num_channels = 16,
1195 .name = "EQ High Frequence",
d2bb390a
DU
1196 },
1197};
1198
1199/* table of compressor controls */
1200static struct snd_us16x08_control_params comp_controls[] = {
1201 { /* Comp enable */
1202 .kcontrol_new = &snd_us16x08_compswitch_ctl,
1203 .control_id = SND_US16X08_ID_COMP_SWITCH,
1204 .type = USB_MIXER_BOOLEAN,
1205 .num_channels = 16,
1206 .name = "Compressor Switch",
d2bb390a
DU
1207 },
1208 { /* Comp threshold */
1209 .kcontrol_new = &snd_us16x08_comp_threshold_ctl,
1210 .control_id = SND_US16X08_ID_COMP_THRESHOLD,
1211 .type = USB_MIXER_U8,
1212 .num_channels = 16,
1213 .name = "Compressor Threshold Volume",
d2bb390a
DU
1214 },
1215 { /* Comp ratio */
1216 .kcontrol_new = &snd_us16x08_comp_ratio_ctl,
1217 .control_id = SND_US16X08_ID_COMP_RATIO,
1218 .type = USB_MIXER_U8,
1219 .num_channels = 16,
1220 .name = "Compressor Ratio",
d2bb390a
DU
1221 },
1222 { /* Comp attack */
1223 .kcontrol_new = &snd_us16x08_comp_attack_ctl,
1224 .control_id = SND_US16X08_ID_COMP_ATTACK,
1225 .type = USB_MIXER_U8,
1226 .num_channels = 16,
1227 .name = "Compressor Attack",
d2bb390a
DU
1228 },
1229 { /* Comp release */
1230 .kcontrol_new = &snd_us16x08_comp_release_ctl,
1231 .control_id = SND_US16X08_ID_COMP_RELEASE,
1232 .type = USB_MIXER_U8,
1233 .num_channels = 16,
1234 .name = "Compressor Release",
d2bb390a
DU
1235 },
1236 { /* Comp gain */
1237 .kcontrol_new = &snd_us16x08_comp_gain_ctl,
1238 .control_id = SND_US16X08_ID_COMP_GAIN,
1239 .type = USB_MIXER_U8,
1240 .num_channels = 16,
1241 .name = "Compressor Volume",
d2bb390a
DU
1242 },
1243};
1244
1245/* table of channel controls */
1246static struct snd_us16x08_control_params channel_controls[] = {
1247 { /* Phase */
1248 .kcontrol_new = &snd_us16x08_ch_boolean_ctl,
1249 .control_id = SND_US16X08_ID_PHASE,
1250 .type = USB_MIXER_BOOLEAN,
1251 .num_channels = 16,
1252 .name = "Phase Switch",
d2bb390a
DU
1253 .default_val = 0
1254 },
1255 { /* Fader */
1256 .kcontrol_new = &snd_us16x08_ch_int_ctl,
1257 .control_id = SND_US16X08_ID_FADER,
1258 .type = USB_MIXER_U8,
1259 .num_channels = 16,
1260 .name = "Line Volume",
d2bb390a
DU
1261 .default_val = 127
1262 },
1263 { /* Mute */
1264 .kcontrol_new = &snd_us16x08_ch_boolean_ctl,
1265 .control_id = SND_US16X08_ID_MUTE,
1266 .type = USB_MIXER_BOOLEAN,
1267 .num_channels = 16,
1268 .name = "Mute Switch",
d2bb390a
DU
1269 .default_val = 0
1270 },
1271 { /* Pan */
1272 .kcontrol_new = &snd_us16x08_pan_int_ctl,
1273 .control_id = SND_US16X08_ID_PAN,
1274 .type = USB_MIXER_U16,
1275 .num_channels = 16,
1276 .name = "Pan Left-Right Volume",
d2bb390a
DU
1277 .default_val = 127
1278 },
1279};
1280
1281/* table of master controls */
1282static struct snd_us16x08_control_params master_controls[] = {
1283 { /* Master */
1284 .kcontrol_new = &snd_us16x08_master_ctl,
1285 .control_id = SND_US16X08_ID_FADER,
1286 .type = USB_MIXER_U8,
1287 .num_channels = 16,
1288 .name = "Master Volume",
d2bb390a
DU
1289 .default_val = 127
1290 },
1291 { /* Bypass */
1292 .kcontrol_new = &snd_us16x08_bus_ctl,
1293 .control_id = SND_US16X08_ID_BYPASS,
1294 .type = USB_MIXER_BOOLEAN,
1295 .num_channels = 16,
1296 .name = "DSP Bypass Switch",
d2bb390a
DU
1297 .default_val = 0
1298 },
1299 { /* Buss out */
1300 .kcontrol_new = &snd_us16x08_bus_ctl,
1301 .control_id = SND_US16X08_ID_BUSS_OUT,
1302 .type = USB_MIXER_BOOLEAN,
1303 .num_channels = 16,
1304 .name = "Buss Out Switch",
d2bb390a
DU
1305 .default_val = 0
1306 },
1307 { /* Master mute */
1308 .kcontrol_new = &snd_us16x08_bus_ctl,
1309 .control_id = SND_US16X08_ID_MUTE,
1310 .type = USB_MIXER_BOOLEAN,
1311 .num_channels = 16,
1312 .name = "Master Mute Switch",
d2bb390a
DU
1313 .default_val = 0
1314 },
1315
1316};
1317
1318int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
1319{
1320 int i, j;
1321 int err;
1322 struct usb_mixer_elem_info *elem;
1323 struct snd_us16x08_comp_store *comp_store;
1324 struct snd_us16x08_meter_store *meter_store;
1325 struct snd_us16x08_eq_store *eq_store;
1326
1327 /* just check for non-MIDI interface */
1328 if (mixer->hostif->desc.bInterfaceNumber == 3) {
1329
d2bb390a
DU
1330 /* add routing control */
1331 err = add_new_ctl(mixer, &snd_us16x08_route_ctl,
1332 SND_US16X08_ID_ROUTE, USB_MIXER_U8, 8, "Line Out Route",
e2810d76 1333 NULL, false, &elem);
d2bb390a
DU
1334 if (err < 0) {
1335 usb_audio_dbg(mixer->chip,
1336 "Failed to create route control, err:%d\n",
1337 err);
1338 return err;
1339 }
1340 for (i = 0; i < 8; i++)
1341 elem->cache_val[i] = i < 2 ? i : i + 2;
1342 elem->cached = 0xff;
1343
e2810d76
TI
1344 /* create compressor mixer elements */
1345 comp_store = snd_us16x08_create_comp_store();
1346 if (!comp_store)
1347 return -ENOMEM;
1348
d2bb390a
DU
1349 /* add master controls */
1350 for (i = 0;
1351 i < sizeof(master_controls)
1352 / sizeof(control_params);
1353 i++) {
1354
1355 err = add_new_ctl(mixer,
1356 master_controls[i].kcontrol_new,
1357 master_controls[i].control_id,
1358 master_controls[i].type,
1359 master_controls[i].num_channels,
1360 master_controls[i].name,
1361 comp_store,
e2810d76
TI
1362 i == 0, /* release comp_store only once */
1363 &elem);
d2bb390a
DU
1364 if (err < 0)
1365 return err;
1366 elem->cache_val[0] = master_controls[i].default_val;
1367 elem->cached = 1;
1368 }
1369
1370 /* add channel controls */
1371 for (i = 0;
1372 i < sizeof(channel_controls)
1373 / sizeof(control_params);
1374 i++) {
1375
1376 err = add_new_ctl(mixer,
1377 channel_controls[i].kcontrol_new,
1378 channel_controls[i].control_id,
1379 channel_controls[i].type,
1380 channel_controls[i].num_channels,
1381 channel_controls[i].name,
1382 comp_store,
e2810d76 1383 false, &elem);
d2bb390a
DU
1384 if (err < 0)
1385 return err;
1386 for (j = 0; j < SND_US16X08_MAX_CHANNELS; j++) {
1387 elem->cache_val[j] =
1388 channel_controls[i].default_val;
1389 }
1390 elem->cached = 0xffff;
1391 }
1392
e2810d76
TI
1393 /* create eq store */
1394 eq_store = snd_us16x08_create_eq_store();
1395 if (!eq_store)
1396 return -ENOMEM;
1397
d2bb390a
DU
1398 /* add EQ controls */
1399 for (i = 0; i < sizeof(eq_controls) /
1400 sizeof(control_params); i++) {
1401
1402 err = add_new_ctl(mixer,
1403 eq_controls[i].kcontrol_new,
1404 eq_controls[i].control_id,
1405 eq_controls[i].type,
1406 eq_controls[i].num_channels,
1407 eq_controls[i].name,
1408 eq_store,
e2810d76
TI
1409 i == 0, /* release eq_store only once */
1410 NULL);
d2bb390a
DU
1411 if (err < 0)
1412 return err;
1413 }
1414
1415 /* add compressor controls */
1416 for (i = 0;
1417 i < sizeof(comp_controls)
1418 / sizeof(control_params);
1419 i++) {
1420
1421 err = add_new_ctl(mixer,
1422 comp_controls[i].kcontrol_new,
1423 comp_controls[i].control_id,
1424 comp_controls[i].type,
1425 comp_controls[i].num_channels,
1426 comp_controls[i].name,
1427 comp_store,
e2810d76 1428 false, NULL);
d2bb390a
DU
1429 if (err < 0)
1430 return err;
1431 }
1432
e2810d76
TI
1433 /* create meters store */
1434 meter_store = snd_us16x08_create_meter_store();
1435 if (!meter_store)
1436 return -ENOMEM;
1437
d2bb390a
DU
1438 /* meter function 'get' must access to compressor store
1439 * so place a reference here
1440 */
1441 meter_store->comp_store = comp_store;
1442 err = add_new_ctl(mixer, &snd_us16x08_meter_ctl,
1443 SND_US16X08_ID_METER, USB_MIXER_U16, 0, "Level Meter",
e2810d76 1444 meter_store, true, NULL);
d2bb390a
DU
1445 if (err < 0)
1446 return err;
1447 }
1448
1449 return 0;
1450}
1451