blob: d7778f2bcbf866f046f0b6d43248a841c7ba1c1e [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
29/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
45#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
48#include <linux/log2.h>
49#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
52#include <linux/usb/audio.h>
53#include <linux/usb/audio-v2.h>
54#include <linux/usb/audio-v3.h>
55
56#include <sound/core.h>
57#include <sound/control.h>
58#include <sound/hwdep.h>
59#include <sound/info.h>
60#include <sound/tlv.h>
61
62#include "usbaudio.h"
63#include "mixer.h"
64#include "helper.h"
65#include "mixer_quirks.h"
66#include "power.h"
67
68#define MAX_ID_ELEMS 256
69
70struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
80struct mixer_build {
81 struct snd_usb_audio *chip;
82 struct usb_mixer_interface *mixer;
83 unsigned char *buffer;
84 unsigned int buflen;
85 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
86 DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS);
87 struct usb_audio_term oterm;
88 const struct usbmix_name_map *map;
89 const struct usbmix_selector_map *selector_map;
90};
91
92/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
93enum {
94 USB_XU_CLOCK_RATE = 0xe301,
95 USB_XU_CLOCK_SOURCE = 0xe302,
96 USB_XU_DIGITAL_IO_STATUS = 0xe303,
97 USB_XU_DEVICE_OPTIONS = 0xe304,
98 USB_XU_DIRECT_MONITORING = 0xe305,
99 USB_XU_METERING = 0xe306
100};
101enum {
102 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
103 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
104 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
105 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
106};
107
108/*
109 * manual mapping of mixer names
110 * if the mixer topology is too complicated and the parsed names are
111 * ambiguous, add the entries in usbmixer_maps.c.
112 */
113#include "mixer_maps.c"
114
115static const struct usbmix_name_map *
116find_map(const struct usbmix_name_map *p, int unitid, int control)
117{
118 if (!p)
119 return NULL;
120
121 for (; p->id; p++) {
122 if (p->id == unitid &&
123 (!control || !p->control || control == p->control))
124 return p;
125 }
126 return NULL;
127}
128
129/* get the mapped name if the unit matches */
130static int
131check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
132{
133 if (!p || !p->name)
134 return 0;
135
136 buflen--;
137 return strlcpy(buf, p->name, buflen);
138}
139
140/* ignore the error value if ignore_ctl_error flag is set */
141#define filter_error(cval, err) \
142 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
143
144/* check whether the control should be ignored */
145static inline int
146check_ignored_ctl(const struct usbmix_name_map *p)
147{
148 if (!p || p->name || p->dB)
149 return 0;
150 return 1;
151}
152
153/* dB mapping */
154static inline void check_mapped_dB(const struct usbmix_name_map *p,
155 struct usb_mixer_elem_info *cval)
156{
157 if (p && p->dB) {
158 cval->dBmin = p->dB->min;
159 cval->dBmax = p->dB->max;
160 cval->initialized = 1;
161 }
162}
163
164/* get the mapped selector source name */
165static int check_mapped_selector_name(struct mixer_build *state, int unitid,
166 int index, char *buf, int buflen)
167{
168 const struct usbmix_selector_map *p;
169
170 if (!state->selector_map)
171 return 0;
172 for (p = state->selector_map; p->id; p++) {
173 if (p->id == unitid && index < p->count)
174 return strlcpy(buf, p->names[index], buflen);
175 }
176 return 0;
177}
178
179/*
180 * find an audio control unit with the given unit id
181 */
182static void *find_audio_control_unit(struct mixer_build *state,
183 unsigned char unit)
184{
185 /* we just parse the header */
186 struct uac_feature_unit_descriptor *hdr = NULL;
187
188 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
189 USB_DT_CS_INTERFACE)) != NULL) {
190 if (hdr->bLength >= 4 &&
191 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
192 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
193 hdr->bUnitID == unit)
194 return hdr;
195 }
196
197 return NULL;
198}
199
200/*
201 * copy a string with the given id
202 */
203static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
204 int index, char *buf, int maxlen)
205{
206 int len = usb_string(chip->dev, index, buf, maxlen - 1);
207
208 if (len < 0)
209 return 0;
210
211 buf[len] = 0;
212 return len;
213}
214
215/*
216 * convert from the byte/word on usb descriptor to the zero-based integer
217 */
218static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
219{
220 switch (cval->val_type) {
221 case USB_MIXER_BOOLEAN:
222 return !!val;
223 case USB_MIXER_INV_BOOLEAN:
224 return !val;
225 case USB_MIXER_U8:
226 val &= 0xff;
227 break;
228 case USB_MIXER_S8:
229 val &= 0xff;
230 if (val >= 0x80)
231 val -= 0x100;
232 break;
233 case USB_MIXER_U16:
234 val &= 0xffff;
235 break;
236 case USB_MIXER_S16:
237 val &= 0xffff;
238 if (val >= 0x8000)
239 val -= 0x10000;
240 break;
241 }
242 return val;
243}
244
245/*
246 * convert from the zero-based int to the byte/word for usb descriptor
247 */
248static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
249{
250 switch (cval->val_type) {
251 case USB_MIXER_BOOLEAN:
252 return !!val;
253 case USB_MIXER_INV_BOOLEAN:
254 return !val;
255 case USB_MIXER_S8:
256 case USB_MIXER_U8:
257 return val & 0xff;
258 case USB_MIXER_S16:
259 case USB_MIXER_U16:
260 return val & 0xffff;
261 }
262 return 0; /* not reached */
263}
264
265static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
266{
267 if (!cval->res)
268 cval->res = 1;
269 if (val < cval->min)
270 return 0;
271 else if (val >= cval->max)
272 return (cval->max - cval->min + cval->res - 1) / cval->res;
273 else
274 return (val - cval->min) / cval->res;
275}
276
277static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
278{
279 if (val < 0)
280 return cval->min;
281 if (!cval->res)
282 cval->res = 1;
283 val *= cval->res;
284 val += cval->min;
285 if (val > cval->max)
286 return cval->max;
287 return val;
288}
289
290static int uac2_ctl_value_size(int val_type)
291{
292 switch (val_type) {
293 case USB_MIXER_S32:
294 case USB_MIXER_U32:
295 return 4;
296 case USB_MIXER_S16:
297 case USB_MIXER_U16:
298 return 2;
299 default:
300 return 1;
301 }
302 return 0; /* unreachable */
303}
304
305
306/*
307 * retrieve a mixer value
308 */
309
310static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
311 int validx, int *value_ret)
312{
313 struct snd_usb_audio *chip = cval->head.mixer->chip;
314 unsigned char buf[2];
315 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
316 int timeout = 10;
317 int idx = 0, err;
318
319 err = snd_usb_lock_shutdown(chip);
320 if (err < 0)
321 return -EIO;
322
323 while (timeout-- > 0) {
324 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
325 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
326 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
327 validx, idx, buf, val_len);
328 if (err >= val_len) {
329 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
330 err = 0;
331 goto out;
332 } else if (err == -ETIMEDOUT) {
333 goto out;
334 }
335 }
336 usb_audio_dbg(chip,
337 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
338 request, validx, idx, cval->val_type);
339 err = -EINVAL;
340
341 out:
342 snd_usb_unlock_shutdown(chip);
343 return err;
344}
345
346static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
347 int validx, int *value_ret)
348{
349 struct snd_usb_audio *chip = cval->head.mixer->chip;
350 /* enough space for one range */
351 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
352 unsigned char *val;
353 int idx = 0, ret, val_size, size;
354 __u8 bRequest;
355
356 val_size = uac2_ctl_value_size(cval->val_type);
357
358 if (request == UAC_GET_CUR) {
359 bRequest = UAC2_CS_CUR;
360 size = val_size;
361 } else {
362 bRequest = UAC2_CS_RANGE;
363 size = sizeof(__u16) + 3 * val_size;
364 }
365
366 memset(buf, 0, sizeof(buf));
367
368 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
369 if (ret)
370 goto error;
371
372 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
373 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
374 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
375 validx, idx, buf, size);
376 snd_usb_unlock_shutdown(chip);
377
378 if (ret < 0) {
379error:
380 usb_audio_err(chip,
381 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
382 request, validx, idx, cval->val_type);
383 return ret;
384 }
385
386 /* FIXME: how should we handle multiple triplets here? */
387
388 switch (request) {
389 case UAC_GET_CUR:
390 val = buf;
391 break;
392 case UAC_GET_MIN:
393 val = buf + sizeof(__u16);
394 break;
395 case UAC_GET_MAX:
396 val = buf + sizeof(__u16) + val_size;
397 break;
398 case UAC_GET_RES:
399 val = buf + sizeof(__u16) + val_size * 2;
400 break;
401 default:
402 return -EINVAL;
403 }
404
405 *value_ret = convert_signed_value(cval,
406 snd_usb_combine_bytes(val, val_size));
407
408 return 0;
409}
410
411static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
412 int validx, int *value_ret)
413{
414 validx += cval->idx_off;
415
416 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
417 get_ctl_value_v1(cval, request, validx, value_ret) :
418 get_ctl_value_v2(cval, request, validx, value_ret);
419}
420
421static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
422 int validx, int *value)
423{
424 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
425}
426
427/* channel = 0: master, 1 = first channel */
428static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
429 int channel, int *value)
430{
431 return get_ctl_value(cval, UAC_GET_CUR,
432 (cval->control << 8) | channel,
433 value);
434}
435
436int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
437 int channel, int index, int *value)
438{
439 int err;
440
441 if (cval->cached & (1 << channel)) {
442 *value = cval->cache_val[index];
443 return 0;
444 }
445 err = get_cur_mix_raw(cval, channel, value);
446 if (err < 0) {
447 if (!cval->head.mixer->ignore_ctl_error)
448 usb_audio_dbg(cval->head.mixer->chip,
449 "cannot get current value for control %d ch %d: err = %d\n",
450 cval->control, channel, err);
451 return err;
452 }
453 cval->cached |= 1 << channel;
454 cval->cache_val[index] = *value;
455 return 0;
456}
457
458/*
459 * set a mixer value
460 */
461
462int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
463 int request, int validx, int value_set)
464{
465 struct snd_usb_audio *chip = cval->head.mixer->chip;
466 unsigned char buf[4];
467 int idx = 0, val_len, err, timeout = 10;
468
469 validx += cval->idx_off;
470
471
472 if (cval->head.mixer->protocol == UAC_VERSION_1) {
473 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
474 } else { /* UAC_VERSION_2/3 */
475 val_len = uac2_ctl_value_size(cval->val_type);
476
477 /* FIXME */
478 if (request != UAC_SET_CUR) {
479 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
480 return -EINVAL;
481 }
482
483 request = UAC2_CS_CUR;
484 }
485
486 value_set = convert_bytes_value(cval, value_set);
487 buf[0] = value_set & 0xff;
488 buf[1] = (value_set >> 8) & 0xff;
489 buf[2] = (value_set >> 16) & 0xff;
490 buf[3] = (value_set >> 24) & 0xff;
491
492 err = snd_usb_lock_shutdown(chip);
493 if (err < 0)
494 return -EIO;
495
496 while (timeout-- > 0) {
497 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
498 err = snd_usb_ctl_msg(chip->dev,
499 usb_sndctrlpipe(chip->dev, 0), request,
500 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
501 validx, idx, buf, val_len);
502 if (err >= 0) {
503 err = 0;
504 goto out;
505 } else if (err == -ETIMEDOUT) {
506 goto out;
507 }
508 }
509 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
510 request, validx, idx, cval->val_type, buf[0], buf[1]);
511 err = -EINVAL;
512
513 out:
514 snd_usb_unlock_shutdown(chip);
515 return err;
516}
517
518static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
519 int validx, int value)
520{
521 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
522}
523
524int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
525 int index, int value)
526{
527 int err;
528 unsigned int read_only = (channel == 0) ?
529 cval->master_readonly :
530 cval->ch_readonly & (1 << (channel - 1));
531
532 if (read_only) {
533 usb_audio_dbg(cval->head.mixer->chip,
534 "%s(): channel %d of control %d is read_only\n",
535 __func__, channel, cval->control);
536 return 0;
537 }
538
539 err = snd_usb_mixer_set_ctl_value(cval,
540 UAC_SET_CUR, (cval->control << 8) | channel,
541 value);
542 if (err < 0)
543 return err;
544 cval->cached |= 1 << channel;
545 cval->cache_val[index] = value;
546 return 0;
547}
548
549/*
550 * TLV callback for mixer volume controls
551 */
552int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
553 unsigned int size, unsigned int __user *_tlv)
554{
555 struct usb_mixer_elem_info *cval = kcontrol->private_data;
556 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
557
558 if (size < sizeof(scale))
559 return -ENOMEM;
560 if (cval->min_mute)
561 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
562 scale[2] = cval->dBmin;
563 scale[3] = cval->dBmax;
564 if (copy_to_user(_tlv, scale, sizeof(scale)))
565 return -EFAULT;
566 return 0;
567}
568
569/*
570 * parser routines begin here...
571 */
572
573static int parse_audio_unit(struct mixer_build *state, int unitid);
574
575
576/*
577 * check if the input/output channel routing is enabled on the given bitmap.
578 * used for mixer unit parser
579 */
580static int check_matrix_bitmap(unsigned char *bmap,
581 int ich, int och, int num_outs)
582{
583 int idx = ich * num_outs + och;
584 return bmap[idx >> 3] & (0x80 >> (idx & 7));
585}
586
587/*
588 * add an alsa control element
589 * search and increment the index until an empty slot is found.
590 *
591 * if failed, give up and free the control instance.
592 */
593
594int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
595 struct snd_kcontrol *kctl)
596{
597 struct usb_mixer_interface *mixer = list->mixer;
598 int err;
599
600 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
601 kctl->id.index++;
602 err = snd_ctl_add(mixer->chip->card, kctl);
603 if (err < 0) {
604 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
605 err);
606 return err;
607 }
608 list->kctl = kctl;
609 list->next_id_elem = mixer->id_elems[list->id];
610 mixer->id_elems[list->id] = list;
611 return 0;
612}
613
614/*
615 * get a terminal name string
616 */
617
618static struct iterm_name_combo {
619 int type;
620 char *name;
621} iterm_names[] = {
622 { 0x0300, "Output" },
623 { 0x0301, "Speaker" },
624 { 0x0302, "Headphone" },
625 { 0x0303, "HMD Audio" },
626 { 0x0304, "Desktop Speaker" },
627 { 0x0305, "Room Speaker" },
628 { 0x0306, "Com Speaker" },
629 { 0x0307, "LFE" },
630 { 0x0600, "External In" },
631 { 0x0601, "Analog In" },
632 { 0x0602, "Digital In" },
633 { 0x0603, "Line" },
634 { 0x0604, "Legacy In" },
635 { 0x0605, "IEC958 In" },
636 { 0x0606, "1394 DA Stream" },
637 { 0x0607, "1394 DV Stream" },
638 { 0x0700, "Embedded" },
639 { 0x0701, "Noise Source" },
640 { 0x0702, "Equalization Noise" },
641 { 0x0703, "CD" },
642 { 0x0704, "DAT" },
643 { 0x0705, "DCC" },
644 { 0x0706, "MiniDisk" },
645 { 0x0707, "Analog Tape" },
646 { 0x0708, "Phonograph" },
647 { 0x0709, "VCR Audio" },
648 { 0x070a, "Video Disk Audio" },
649 { 0x070b, "DVD Audio" },
650 { 0x070c, "TV Tuner Audio" },
651 { 0x070d, "Satellite Rec Audio" },
652 { 0x070e, "Cable Tuner Audio" },
653 { 0x070f, "DSS Audio" },
654 { 0x0710, "Radio Receiver" },
655 { 0x0711, "Radio Transmitter" },
656 { 0x0712, "Multi-Track Recorder" },
657 { 0x0713, "Synthesizer" },
658 { 0 },
659};
660
661static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
662 unsigned char *name, int maxlen, int term_only)
663{
664 struct iterm_name_combo *names;
665 int len;
666
667 if (iterm->name) {
668 len = snd_usb_copy_string_desc(chip, iterm->name,
669 name, maxlen);
670 if (len)
671 return len;
672 }
673
674 /* virtual type - not a real terminal */
675 if (iterm->type >> 16) {
676 if (term_only)
677 return 0;
678 switch (iterm->type >> 16) {
679 case UAC3_SELECTOR_UNIT:
680 strcpy(name, "Selector");
681 return 8;
682 case UAC3_PROCESSING_UNIT:
683 strcpy(name, "Process Unit");
684 return 12;
685 case UAC3_EXTENSION_UNIT:
686 strcpy(name, "Ext Unit");
687 return 8;
688 case UAC3_MIXER_UNIT:
689 strcpy(name, "Mixer");
690 return 5;
691 default:
692 return sprintf(name, "Unit %d", iterm->id);
693 }
694 }
695
696 switch (iterm->type & 0xff00) {
697 case 0x0100:
698 strcpy(name, "PCM");
699 return 3;
700 case 0x0200:
701 strcpy(name, "Mic");
702 return 3;
703 case 0x0400:
704 strcpy(name, "Headset");
705 return 7;
706 case 0x0500:
707 strcpy(name, "Phone");
708 return 5;
709 }
710
711 for (names = iterm_names; names->type; names++) {
712 if (names->type == iterm->type) {
713 strcpy(name, names->name);
714 return strlen(names->name);
715 }
716 }
717
718 return 0;
719}
720
721/*
722 * Get logical cluster information for UAC3 devices.
723 */
724static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
725{
726 struct uac3_cluster_header_descriptor c_header;
727 int err;
728
729 err = snd_usb_ctl_msg(state->chip->dev,
730 usb_rcvctrlpipe(state->chip->dev, 0),
731 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
732 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
733 cluster_id,
734 snd_usb_ctrl_intf(state->chip),
735 &c_header, sizeof(c_header));
736 if (err < 0)
737 goto error;
738 if (err != sizeof(c_header)) {
739 err = -EIO;
740 goto error;
741 }
742
743 return c_header.bNrChannels;
744
745error:
746 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
747 return err;
748}
749
750/*
751 * Get number of channels for a Mixer Unit.
752 */
753static int uac_mixer_unit_get_channels(struct mixer_build *state,
754 struct uac_mixer_unit_descriptor *desc)
755{
756 int mu_channels;
757
758 switch (state->mixer->protocol) {
759 case UAC_VERSION_1:
760 case UAC_VERSION_2:
761 default:
762 if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1)
763 return 0; /* no bmControls -> skip */
764 mu_channels = uac_mixer_unit_bNrChannels(desc);
765 break;
766 case UAC_VERSION_3:
767 mu_channels = get_cluster_channels_v3(state,
768 uac3_mixer_unit_wClusterDescrID(desc));
769 break;
770 }
771
772 return mu_channels;
773}
774
775/*
776 * Parse Input Terminal Unit
777 */
778static int __check_input_term(struct mixer_build *state, int id,
779 struct usb_audio_term *term);
780
781static int parse_term_uac1_iterm_unit(struct mixer_build *state,
782 struct usb_audio_term *term,
783 void *p1, int id)
784{
785 struct uac_input_terminal_descriptor *d = p1;
786
787 term->type = le16_to_cpu(d->wTerminalType);
788 term->channels = d->bNrChannels;
789 term->chconfig = le16_to_cpu(d->wChannelConfig);
790 term->name = d->iTerminal;
791 return 0;
792}
793
794static int parse_term_uac2_iterm_unit(struct mixer_build *state,
795 struct usb_audio_term *term,
796 void *p1, int id)
797{
798 struct uac2_input_terminal_descriptor *d = p1;
799 int err;
800
801 /* call recursively to verify the referenced clock entity */
802 err = __check_input_term(state, d->bCSourceID, term);
803 if (err < 0)
804 return err;
805
806 /* save input term properties after recursion,
807 * to ensure they are not overriden by the recursion calls
808 */
809 term->id = id;
810 term->type = le16_to_cpu(d->wTerminalType);
811 term->channels = d->bNrChannels;
812 term->chconfig = le32_to_cpu(d->bmChannelConfig);
813 term->name = d->iTerminal;
814 return 0;
815}
816
817static int parse_term_uac3_iterm_unit(struct mixer_build *state,
818 struct usb_audio_term *term,
819 void *p1, int id)
820{
821 struct uac3_input_terminal_descriptor *d = p1;
822 int err;
823
824 /* call recursively to verify the referenced clock entity */
825 err = __check_input_term(state, d->bCSourceID, term);
826 if (err < 0)
827 return err;
828
829 /* save input term properties after recursion,
830 * to ensure they are not overriden by the recursion calls
831 */
832 term->id = id;
833 term->type = le16_to_cpu(d->wTerminalType);
834
835 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
836 if (err < 0)
837 return err;
838 term->channels = err;
839
840 /* REVISIT: UAC3 IT doesn't have channels cfg */
841 term->chconfig = 0;
842
843 term->name = le16_to_cpu(d->wTerminalDescrStr);
844 return 0;
845}
846
847static int parse_term_mixer_unit(struct mixer_build *state,
848 struct usb_audio_term *term,
849 void *p1, int id)
850{
851 struct uac_mixer_unit_descriptor *d = p1;
852 int protocol = state->mixer->protocol;
853 int err;
854
855 err = uac_mixer_unit_get_channels(state, d);
856 if (err <= 0)
857 return err;
858
859 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
860 term->channels = err;
861 if (protocol != UAC_VERSION_3) {
862 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
863 term->name = uac_mixer_unit_iMixer(d);
864 }
865 return 0;
866}
867
868static int parse_term_selector_unit(struct mixer_build *state,
869 struct usb_audio_term *term,
870 void *p1, int id)
871{
872 struct uac_selector_unit_descriptor *d = p1;
873 int err;
874
875 /* call recursively to retrieve the channel info */
876 err = __check_input_term(state, d->baSourceID[0], term);
877 if (err < 0)
878 return err;
879 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
880 term->id = id;
881 if (state->mixer->protocol != UAC_VERSION_3)
882 term->name = uac_selector_unit_iSelector(d);
883 return 0;
884}
885
886static int parse_term_proc_unit(struct mixer_build *state,
887 struct usb_audio_term *term,
888 void *p1, int id, int vtype)
889{
890 struct uac_processing_unit_descriptor *d = p1;
891 int protocol = state->mixer->protocol;
892 int err;
893
894 if (d->bNrInPins) {
895 /* call recursively to retrieve the channel info */
896 err = __check_input_term(state, d->baSourceID[0], term);
897 if (err < 0)
898 return err;
899 }
900
901 term->type = vtype << 16; /* virtual type */
902 term->id = id;
903
904 if (protocol == UAC_VERSION_3)
905 return 0;
906
907 if (!term->channels) {
908 term->channels = uac_processing_unit_bNrChannels(d);
909 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
910 }
911 term->name = uac_processing_unit_iProcessing(d, protocol);
912 return 0;
913}
914
915static int parse_term_uac2_clock_source(struct mixer_build *state,
916 struct usb_audio_term *term,
917 void *p1, int id)
918{
919 struct uac_clock_source_descriptor *d = p1;
920
921 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
922 term->id = id;
923 term->name = d->iClockSource;
924 return 0;
925}
926
927static int parse_term_uac3_clock_source(struct mixer_build *state,
928 struct usb_audio_term *term,
929 void *p1, int id)
930{
931 struct uac3_clock_source_descriptor *d = p1;
932
933 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
934 term->id = id;
935 term->name = le16_to_cpu(d->wClockSourceStr);
936 return 0;
937}
938
939#define PTYPE(a, b) ((a) << 8 | (b))
940
941/*
942 * parse the source unit recursively until it reaches to a terminal
943 * or a branched unit.
944 */
945static int __check_input_term(struct mixer_build *state, int id,
946 struct usb_audio_term *term)
947{
948 int protocol = state->mixer->protocol;
949 void *p1;
950 unsigned char *hdr;
951
952 for (;;) {
953 /* a loop in the terminal chain? */
954 if (test_and_set_bit(id, state->termbitmap))
955 return -EINVAL;
956
957 p1 = find_audio_control_unit(state, id);
958 if (!p1)
959 break;
960 if (!snd_usb_validate_audio_desc(p1, protocol))
961 break; /* bad descriptor */
962
963 hdr = p1;
964 term->id = id;
965
966 switch (PTYPE(protocol, hdr[2])) {
967 case PTYPE(UAC_VERSION_1, UAC_FEATURE_UNIT):
968 case PTYPE(UAC_VERSION_2, UAC_FEATURE_UNIT):
969 case PTYPE(UAC_VERSION_3, UAC3_FEATURE_UNIT): {
970 /* the header is the same for all versions */
971 struct uac_feature_unit_descriptor *d = p1;
972
973 id = d->bSourceID;
974 break; /* continue to parse */
975 }
976 case PTYPE(UAC_VERSION_1, UAC_INPUT_TERMINAL):
977 return parse_term_uac1_iterm_unit(state, term, p1, id);
978 case PTYPE(UAC_VERSION_2, UAC_INPUT_TERMINAL):
979 return parse_term_uac2_iterm_unit(state, term, p1, id);
980 case PTYPE(UAC_VERSION_3, UAC_INPUT_TERMINAL):
981 return parse_term_uac3_iterm_unit(state, term, p1, id);
982 case PTYPE(UAC_VERSION_1, UAC_MIXER_UNIT):
983 case PTYPE(UAC_VERSION_2, UAC_MIXER_UNIT):
984 case PTYPE(UAC_VERSION_3, UAC3_MIXER_UNIT):
985 return parse_term_mixer_unit(state, term, p1, id);
986 case PTYPE(UAC_VERSION_1, UAC_SELECTOR_UNIT):
987 case PTYPE(UAC_VERSION_2, UAC_SELECTOR_UNIT):
988 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SELECTOR):
989 case PTYPE(UAC_VERSION_3, UAC3_SELECTOR_UNIT):
990 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SELECTOR):
991 return parse_term_selector_unit(state, term, p1, id);
992 case PTYPE(UAC_VERSION_1, UAC1_PROCESSING_UNIT):
993 case PTYPE(UAC_VERSION_2, UAC2_PROCESSING_UNIT_V2):
994 case PTYPE(UAC_VERSION_3, UAC3_PROCESSING_UNIT):
995 return parse_term_proc_unit(state, term, p1, id,
996 UAC3_PROCESSING_UNIT);
997 case PTYPE(UAC_VERSION_2, UAC2_EFFECT_UNIT):
998 case PTYPE(UAC_VERSION_3, UAC3_EFFECT_UNIT):
999 return parse_term_proc_unit(state, term, p1, id,
1000 UAC3_EFFECT_UNIT);
1001 case PTYPE(UAC_VERSION_1, UAC1_EXTENSION_UNIT):
1002 case PTYPE(UAC_VERSION_2, UAC2_EXTENSION_UNIT_V2):
1003 case PTYPE(UAC_VERSION_3, UAC3_EXTENSION_UNIT):
1004 return parse_term_proc_unit(state, term, p1, id,
1005 UAC3_EXTENSION_UNIT);
1006 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SOURCE):
1007 return parse_term_uac2_clock_source(state, term, p1, id);
1008 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SOURCE):
1009 return parse_term_uac3_clock_source(state, term, p1, id);
1010 default:
1011 return -ENODEV;
1012 }
1013 }
1014 return -ENODEV;
1015}
1016
1017
1018static int check_input_term(struct mixer_build *state, int id,
1019 struct usb_audio_term *term)
1020{
1021 memset(term, 0, sizeof(*term));
1022 memset(state->termbitmap, 0, sizeof(state->termbitmap));
1023 return __check_input_term(state, id, term);
1024}
1025
1026/*
1027 * Feature Unit
1028 */
1029
1030/* feature unit control information */
1031struct usb_feature_control_info {
1032 int control;
1033 const char *name;
1034 int type; /* data type for uac1 */
1035 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
1036};
1037
1038static struct usb_feature_control_info audio_feature_info[] = {
1039 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
1040 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
1041 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
1042 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
1043 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
1044 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
1045 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
1046 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
1047 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
1048 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
1049 /* UAC2 specific */
1050 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
1051 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
1052 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
1053};
1054
1055static void usb_mixer_elem_info_free(struct usb_mixer_elem_info *cval)
1056{
1057 kfree(cval);
1058}
1059
1060/* private_free callback */
1061void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
1062{
1063 usb_mixer_elem_info_free(kctl->private_data);
1064 kctl->private_data = NULL;
1065}
1066
1067/*
1068 * interface to ALSA control for feature/mixer units
1069 */
1070
1071/* volume control quirks */
1072static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1073 struct snd_kcontrol *kctl)
1074{
1075 struct snd_usb_audio *chip = cval->head.mixer->chip;
1076 switch (chip->usb_id) {
1077 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1078 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
1079 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1080 cval->min = 0x0000;
1081 cval->max = 0xffff;
1082 cval->res = 0x00e6;
1083 break;
1084 }
1085 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1086 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1087 cval->min = 0x00;
1088 cval->max = 0xff;
1089 break;
1090 }
1091 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1092 cval->min = 0xb706;
1093 cval->max = 0xff7b;
1094 cval->res = 0x0073;
1095 break;
1096 }
1097 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1098 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1099 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1100 cval->max = 0xfcfe;
1101 cval->res = 0x0073;
1102 }
1103 break;
1104
1105 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1106 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1107 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1108 usb_audio_info(chip,
1109 "set quirk for FTU Effect Duration\n");
1110 cval->min = 0x0000;
1111 cval->max = 0x7f00;
1112 cval->res = 0x0100;
1113 break;
1114 }
1115 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1116 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1117 usb_audio_info(chip,
1118 "set quirks for FTU Effect Feedback/Volume\n");
1119 cval->min = 0x00;
1120 cval->max = 0x7f;
1121 break;
1122 }
1123 break;
1124
1125 case USB_ID(0x0d8c, 0x0103):
1126 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1127 usb_audio_info(chip,
1128 "set volume quirk for CM102-A+/102S+\n");
1129 cval->min = -256;
1130 }
1131 break;
1132
1133 case USB_ID(0x0471, 0x0101):
1134 case USB_ID(0x0471, 0x0104):
1135 case USB_ID(0x0471, 0x0105):
1136 case USB_ID(0x0672, 0x1041):
1137 /* quirk for UDA1321/N101.
1138 * note that detection between firmware 2.1.1.7 (N101)
1139 * and later 2.1.1.21 is not very clear from datasheets.
1140 * I hope that the min value is -15360 for newer firmware --jk
1141 */
1142 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1143 cval->min == -15616) {
1144 usb_audio_info(chip,
1145 "set volume quirk for UDA1321/N101 chip\n");
1146 cval->max = -256;
1147 }
1148 break;
1149
1150 case USB_ID(0x046d, 0x09a4):
1151 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1152 usb_audio_info(chip,
1153 "set volume quirk for QuickCam E3500\n");
1154 cval->min = 6080;
1155 cval->max = 8768;
1156 cval->res = 192;
1157 }
1158 break;
1159
1160 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
1161 case USB_ID(0x046d, 0x0808):
1162 case USB_ID(0x046d, 0x0809):
1163 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
1164 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
1165 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
1166 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
1167 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
1168 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
1169 case USB_ID(0x046d, 0x0991):
1170 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
1171 /* Most audio usb devices lie about volume resolution.
1172 * Most Logitech webcams have res = 384.
1173 * Probably there is some logitech magic behind this number --fishor
1174 */
1175 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1176 usb_audio_info(chip,
1177 "set resolution quirk: cval->res = 384\n");
1178 cval->res = 384;
1179 }
1180 break;
1181 }
1182}
1183
1184/*
1185 * retrieve the minimum and maximum values for the specified control
1186 */
1187static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1188 int default_min, struct snd_kcontrol *kctl)
1189{
1190 /* for failsafe */
1191 cval->min = default_min;
1192 cval->max = cval->min + 1;
1193 cval->res = 1;
1194 cval->dBmin = cval->dBmax = 0;
1195
1196 if (cval->val_type == USB_MIXER_BOOLEAN ||
1197 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1198 cval->initialized = 1;
1199 } else {
1200 int minchn = 0;
1201 if (cval->cmask) {
1202 int i;
1203 for (i = 0; i < MAX_CHANNELS; i++)
1204 if (cval->cmask & (1 << i)) {
1205 minchn = i + 1;
1206 break;
1207 }
1208 }
1209 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1210 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
1211 usb_audio_err(cval->head.mixer->chip,
1212 "%d:%d: cannot get min/max values for control %d (id %d)\n",
1213 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1214 cval->control, cval->head.id);
1215 return -EINVAL;
1216 }
1217 if (get_ctl_value(cval, UAC_GET_RES,
1218 (cval->control << 8) | minchn,
1219 &cval->res) < 0) {
1220 cval->res = 1;
1221 } else {
1222 int last_valid_res = cval->res;
1223
1224 while (cval->res > 1) {
1225 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
1226 (cval->control << 8) | minchn,
1227 cval->res / 2) < 0)
1228 break;
1229 cval->res /= 2;
1230 }
1231 if (get_ctl_value(cval, UAC_GET_RES,
1232 (cval->control << 8) | minchn, &cval->res) < 0)
1233 cval->res = last_valid_res;
1234 }
1235 if (cval->res == 0)
1236 cval->res = 1;
1237
1238 /* Additional checks for the proper resolution
1239 *
1240 * Some devices report smaller resolutions than actually
1241 * reacting. They don't return errors but simply clip
1242 * to the lower aligned value.
1243 */
1244 if (cval->min + cval->res < cval->max) {
1245 int last_valid_res = cval->res;
1246 int saved, test, check;
1247 if (get_cur_mix_raw(cval, minchn, &saved) < 0)
1248 goto no_res_check;
1249 for (;;) {
1250 test = saved;
1251 if (test < cval->max)
1252 test += cval->res;
1253 else
1254 test -= cval->res;
1255 if (test < cval->min || test > cval->max ||
1256 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
1257 get_cur_mix_raw(cval, minchn, &check)) {
1258 cval->res = last_valid_res;
1259 break;
1260 }
1261 if (test == check)
1262 break;
1263 cval->res *= 2;
1264 }
1265 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
1266 }
1267
1268no_res_check:
1269 cval->initialized = 1;
1270 }
1271
1272 if (kctl)
1273 volume_control_quirks(cval, kctl);
1274
1275 /* USB descriptions contain the dB scale in 1/256 dB unit
1276 * while ALSA TLV contains in 1/100 dB unit
1277 */
1278 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1279 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1280 if (cval->dBmin > cval->dBmax) {
1281 /* something is wrong; assume it's either from/to 0dB */
1282 if (cval->dBmin < 0)
1283 cval->dBmax = 0;
1284 else if (cval->dBmin > 0)
1285 cval->dBmin = 0;
1286 if (cval->dBmin > cval->dBmax) {
1287 /* totally crap, return an error */
1288 return -EINVAL;
1289 }
1290 }
1291
1292 return 0;
1293}
1294
1295#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
1296
1297/* get a feature/mixer unit info */
1298static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1299 struct snd_ctl_elem_info *uinfo)
1300{
1301 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1302
1303 if (cval->val_type == USB_MIXER_BOOLEAN ||
1304 cval->val_type == USB_MIXER_INV_BOOLEAN)
1305 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1306 else
1307 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1308 uinfo->count = cval->channels;
1309 if (cval->val_type == USB_MIXER_BOOLEAN ||
1310 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1311 uinfo->value.integer.min = 0;
1312 uinfo->value.integer.max = 1;
1313 } else {
1314 if (!cval->initialized) {
1315 get_min_max_with_quirks(cval, 0, kcontrol);
1316 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1317 kcontrol->vd[0].access &=
1318 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1319 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
1320 snd_ctl_notify(cval->head.mixer->chip->card,
1321 SNDRV_CTL_EVENT_MASK_INFO,
1322 &kcontrol->id);
1323 }
1324 }
1325 uinfo->value.integer.min = 0;
1326 uinfo->value.integer.max =
1327 (cval->max - cval->min + cval->res - 1) / cval->res;
1328 }
1329 return 0;
1330}
1331
1332/* get the current value from feature/mixer unit */
1333static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1334 struct snd_ctl_elem_value *ucontrol)
1335{
1336 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1337 int c, cnt, val, err;
1338
1339 ucontrol->value.integer.value[0] = cval->min;
1340 if (cval->cmask) {
1341 cnt = 0;
1342 for (c = 0; c < MAX_CHANNELS; c++) {
1343 if (!(cval->cmask & (1 << c)))
1344 continue;
1345 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
1346 if (err < 0)
1347 return filter_error(cval, err);
1348 val = get_relative_value(cval, val);
1349 ucontrol->value.integer.value[cnt] = val;
1350 cnt++;
1351 }
1352 return 0;
1353 } else {
1354 /* master channel */
1355 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1356 if (err < 0)
1357 return filter_error(cval, err);
1358 val = get_relative_value(cval, val);
1359 ucontrol->value.integer.value[0] = val;
1360 }
1361 return 0;
1362}
1363
1364/* put the current value to feature/mixer unit */
1365static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1366 struct snd_ctl_elem_value *ucontrol)
1367{
1368 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1369 int c, cnt, val, oval, err;
1370 int changed = 0;
1371
1372 if (cval->cmask) {
1373 cnt = 0;
1374 for (c = 0; c < MAX_CHANNELS; c++) {
1375 if (!(cval->cmask & (1 << c)))
1376 continue;
1377 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
1378 if (err < 0)
1379 return filter_error(cval, err);
1380 val = ucontrol->value.integer.value[cnt];
1381 val = get_abs_value(cval, val);
1382 if (oval != val) {
1383 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
1384 changed = 1;
1385 }
1386 cnt++;
1387 }
1388 } else {
1389 /* master channel */
1390 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
1391 if (err < 0)
1392 return filter_error(cval, err);
1393 val = ucontrol->value.integer.value[0];
1394 val = get_abs_value(cval, val);
1395 if (val != oval) {
1396 snd_usb_set_cur_mix_value(cval, 0, 0, val);
1397 changed = 1;
1398 }
1399 }
1400 return changed;
1401}
1402
1403/* get the boolean value from the master channel of a UAC control */
1404static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1405 struct snd_ctl_elem_value *ucontrol)
1406{
1407 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1408 int val, err;
1409
1410 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1411 if (err < 0)
1412 return filter_error(cval, err);
1413 val = (val != 0);
1414 ucontrol->value.integer.value[0] = val;
1415 return 0;
1416}
1417
1418/* get the connectors status and report it as boolean type */
1419static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_value *ucontrol)
1421{
1422 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1423 struct snd_usb_audio *chip = cval->head.mixer->chip;
1424 int idx = 0, validx, ret, val;
1425
1426 validx = cval->control << 8 | 0;
1427
1428 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1429 if (ret)
1430 goto error;
1431
1432 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1433 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1434 struct uac2_connectors_ctl_blk uac2_conn;
1435
1436 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1437 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1438 validx, idx, &uac2_conn, sizeof(uac2_conn));
1439 val = !!uac2_conn.bNrChannels;
1440 } else { /* UAC_VERSION_3 */
1441 struct uac3_insertion_ctl_blk uac3_conn;
1442
1443 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1444 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1445 validx, idx, &uac3_conn, sizeof(uac3_conn));
1446 val = !!uac3_conn.bmConInserted;
1447 }
1448
1449 snd_usb_unlock_shutdown(chip);
1450
1451 if (ret < 0) {
1452error:
1453 usb_audio_err(chip,
1454 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1455 UAC_GET_CUR, validx, idx, cval->val_type);
1456 return ret;
1457 }
1458
1459 ucontrol->value.integer.value[0] = val;
1460 return 0;
1461}
1462
1463static struct snd_kcontrol_new usb_feature_unit_ctl = {
1464 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1465 .name = "", /* will be filled later manually */
1466 .info = mixer_ctl_feature_info,
1467 .get = mixer_ctl_feature_get,
1468 .put = mixer_ctl_feature_put,
1469};
1470
1471/* the read-only variant */
1472static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1473 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1474 .name = "", /* will be filled later manually */
1475 .info = mixer_ctl_feature_info,
1476 .get = mixer_ctl_feature_get,
1477 .put = NULL,
1478};
1479
1480/*
1481 * A control which shows the boolean value from reading a UAC control on
1482 * the master channel.
1483 */
1484static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
1485 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1486 .name = "", /* will be filled later manually */
1487 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1488 .info = snd_ctl_boolean_mono_info,
1489 .get = mixer_ctl_master_bool_get,
1490 .put = NULL,
1491};
1492
1493static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1494 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1495 .name = "", /* will be filled later manually */
1496 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1497 .info = snd_ctl_boolean_mono_info,
1498 .get = mixer_ctl_connector_get,
1499 .put = NULL,
1500};
1501
1502/*
1503 * This symbol is exported in order to allow the mixer quirks to
1504 * hook up to the standard feature unit control mechanism
1505 */
1506struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
1507
1508/*
1509 * build a feature control
1510 */
1511static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1512{
1513 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1514}
1515
1516/*
1517 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1518 * rename it to "Headphone". We determine if something is a headphone
1519 * similar to how udev determines form factor.
1520 */
1521static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1522 struct snd_card *card)
1523{
1524 const char *names_to_check[] = {
1525 "Headset", "headset", "Headphone", "headphone", NULL};
1526 const char **s;
1527 bool found = false;
1528
1529 if (strcmp("Speaker", kctl->id.name))
1530 return;
1531
1532 for (s = names_to_check; *s; s++)
1533 if (strstr(card->shortname, *s)) {
1534 found = true;
1535 break;
1536 }
1537
1538 if (!found)
1539 return;
1540
1541 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1542}
1543
1544static struct usb_feature_control_info *get_feature_control_info(int control)
1545{
1546 int i;
1547
1548 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1549 if (audio_feature_info[i].control == control)
1550 return &audio_feature_info[i];
1551 }
1552 return NULL;
1553}
1554
1555static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1556 const struct usbmix_name_map *imap,
1557 unsigned int ctl_mask, int control,
1558 struct usb_audio_term *iterm,
1559 struct usb_audio_term *oterm,
1560 int unitid, int nameid, int readonly_mask)
1561{
1562 struct usb_feature_control_info *ctl_info;
1563 unsigned int len = 0;
1564 int mapped_name = 0;
1565 struct snd_kcontrol *kctl;
1566 struct usb_mixer_elem_info *cval;
1567 const struct usbmix_name_map *map;
1568 unsigned int range;
1569
1570 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
1571 /* FIXME: not supported yet */
1572 return;
1573 }
1574
1575 map = find_map(imap, unitid, control);
1576 if (check_ignored_ctl(map))
1577 return;
1578
1579 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1580 if (!cval)
1581 return;
1582 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
1583 cval->control = control;
1584 cval->cmask = ctl_mask;
1585
1586 ctl_info = get_feature_control_info(control);
1587 if (!ctl_info) {
1588 usb_mixer_elem_info_free(cval);
1589 return;
1590 }
1591 if (mixer->protocol == UAC_VERSION_1)
1592 cval->val_type = ctl_info->type;
1593 else /* UAC_VERSION_2 */
1594 cval->val_type = ctl_info->type_uac2 >= 0 ?
1595 ctl_info->type_uac2 : ctl_info->type;
1596
1597 if (ctl_mask == 0) {
1598 cval->channels = 1; /* master channel */
1599 cval->master_readonly = readonly_mask;
1600 } else {
1601 int i, c = 0;
1602 for (i = 0; i < 16; i++)
1603 if (ctl_mask & (1 << i))
1604 c++;
1605 cval->channels = c;
1606 cval->ch_readonly = readonly_mask;
1607 }
1608
1609 /*
1610 * If all channels in the mask are marked read-only, make the control
1611 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
1612 * issue write commands to read-only channels.
1613 */
1614 if (cval->channels == readonly_mask)
1615 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1616 else
1617 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1618
1619 if (!kctl) {
1620 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1621 usb_mixer_elem_info_free(cval);
1622 return;
1623 }
1624 kctl->private_free = snd_usb_mixer_elem_free;
1625
1626 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1627 mapped_name = len != 0;
1628 if (!len && nameid)
1629 len = snd_usb_copy_string_desc(mixer->chip, nameid,
1630 kctl->id.name, sizeof(kctl->id.name));
1631
1632 switch (control) {
1633 case UAC_FU_MUTE:
1634 case UAC_FU_VOLUME:
1635 /*
1636 * determine the control name. the rule is:
1637 * - if a name id is given in descriptor, use it.
1638 * - if the connected input can be determined, then use the name
1639 * of terminal type.
1640 * - if the connected output can be determined, use it.
1641 * - otherwise, anonymous name.
1642 */
1643 if (!len) {
1644 if (iterm)
1645 len = get_term_name(mixer->chip, iterm,
1646 kctl->id.name,
1647 sizeof(kctl->id.name), 1);
1648 if (!len && oterm)
1649 len = get_term_name(mixer->chip, oterm,
1650 kctl->id.name,
1651 sizeof(kctl->id.name), 1);
1652 if (!len)
1653 snprintf(kctl->id.name, sizeof(kctl->id.name),
1654 "Feature %d", unitid);
1655 }
1656
1657 if (!mapped_name)
1658 check_no_speaker_on_headset(kctl, mixer->chip->card);
1659
1660 /*
1661 * determine the stream direction:
1662 * if the connected output is USB stream, then it's likely a
1663 * capture stream. otherwise it should be playback (hopefully :)
1664 */
1665 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1666 if ((oterm->type & 0xff00) == 0x0100)
1667 append_ctl_name(kctl, " Capture");
1668 else
1669 append_ctl_name(kctl, " Playback");
1670 }
1671 append_ctl_name(kctl, control == UAC_FU_MUTE ?
1672 " Switch" : " Volume");
1673 break;
1674 default:
1675 if (!len)
1676 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1677 sizeof(kctl->id.name));
1678 break;
1679 }
1680
1681 /* get min/max values */
1682 get_min_max_with_quirks(cval, 0, kctl);
1683
1684 if (control == UAC_FU_VOLUME) {
1685 check_mapped_dB(map, cval);
1686 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1687 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1688 kctl->vd[0].access |=
1689 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1690 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1691 }
1692 }
1693
1694 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
1695
1696 range = (cval->max - cval->min) / cval->res;
1697 /*
1698 * Are there devices with volume range more than 255? I use a bit more
1699 * to be sure. 384 is a resolution magic number found on Logitech
1700 * devices. It will definitively catch all buggy Logitech devices.
1701 */
1702 if (range > 384) {
1703 usb_audio_warn(mixer->chip,
1704 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
1705 range);
1706 usb_audio_warn(mixer->chip,
1707 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
1708 cval->head.id, kctl->id.name, cval->channels,
1709 cval->min, cval->max, cval->res);
1710 }
1711
1712 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1713 cval->head.id, kctl->id.name, cval->channels,
1714 cval->min, cval->max, cval->res);
1715 snd_usb_mixer_add_control(&cval->head, kctl);
1716}
1717
1718static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1719 unsigned int ctl_mask, int control,
1720 struct usb_audio_term *iterm, int unitid,
1721 int readonly_mask)
1722{
1723 struct uac_feature_unit_descriptor *desc = raw_desc;
1724 int nameid = uac_feature_unit_iFeature(desc);
1725
1726 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1727 iterm, &state->oterm, unitid, nameid, readonly_mask);
1728}
1729
1730static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1731 unsigned int ctl_mask, int control, int unitid,
1732 const struct usbmix_name_map *badd_map)
1733{
1734 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1735 NULL, NULL, unitid, 0, 0);
1736}
1737
1738static void get_connector_control_name(struct usb_mixer_interface *mixer,
1739 struct usb_audio_term *term,
1740 bool is_input, char *name, int name_size)
1741{
1742 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
1743
1744 if (name_len == 0)
1745 strlcpy(name, "Unknown", name_size);
1746
1747 /*
1748 * sound/core/ctljack.c has a convention of naming jack controls
1749 * by ending in " Jack". Make it slightly more useful by
1750 * indicating Input or Output after the terminal name.
1751 */
1752 if (is_input)
1753 strlcat(name, " - Input Jack", name_size);
1754 else
1755 strlcat(name, " - Output Jack", name_size);
1756}
1757
1758/* Build a mixer control for a UAC connector control (jack-detect) */
1759static void build_connector_control(struct usb_mixer_interface *mixer,
1760 struct usb_audio_term *term, bool is_input)
1761{
1762 struct snd_kcontrol *kctl;
1763 struct usb_mixer_elem_info *cval;
1764
1765 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1766 if (!cval)
1767 return;
1768 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
1769 /*
1770 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1771 * number of channels connected.
1772 *
1773 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1774 * following byte(s) specifies which connectors are inserted.
1775 *
1776 * This boolean ctl will simply report if any channels are connected
1777 * or not.
1778 */
1779 if (mixer->protocol == UAC_VERSION_2)
1780 cval->control = UAC2_TE_CONNECTOR;
1781 else /* UAC_VERSION_3 */
1782 cval->control = UAC3_TE_INSERTION;
1783
1784 cval->val_type = USB_MIXER_BOOLEAN;
1785 cval->channels = 1; /* report true if any channel is connected */
1786 cval->min = 0;
1787 cval->max = 1;
1788 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
1789 if (!kctl) {
1790 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1791 usb_mixer_elem_info_free(cval);
1792 return;
1793 }
1794 get_connector_control_name(mixer, term, is_input, kctl->id.name,
1795 sizeof(kctl->id.name));
1796 kctl->private_free = snd_usb_mixer_elem_free;
1797 snd_usb_mixer_add_control(&cval->head, kctl);
1798}
1799
1800static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1801 void *_ftr)
1802{
1803 struct uac_clock_source_descriptor *hdr = _ftr;
1804 struct usb_mixer_elem_info *cval;
1805 struct snd_kcontrol *kctl;
1806 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1807 int ret;
1808
1809 if (state->mixer->protocol != UAC_VERSION_2)
1810 return -EINVAL;
1811
1812 /*
1813 * The only property of this unit we are interested in is the
1814 * clock source validity. If that isn't readable, just bail out.
1815 */
1816 if (!uac_v2v3_control_is_readable(hdr->bmControls,
1817 UAC2_CS_CONTROL_CLOCK_VALID))
1818 return 0;
1819
1820 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1821 if (!cval)
1822 return -ENOMEM;
1823
1824 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1825
1826 cval->min = 0;
1827 cval->max = 1;
1828 cval->channels = 1;
1829 cval->val_type = USB_MIXER_BOOLEAN;
1830 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1831
1832 cval->master_readonly = 1;
1833 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1834 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
1835
1836 if (!kctl) {
1837 usb_mixer_elem_info_free(cval);
1838 return -ENOMEM;
1839 }
1840
1841 kctl->private_free = snd_usb_mixer_elem_free;
1842 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
1843 name, sizeof(name));
1844 if (ret > 0)
1845 snprintf(kctl->id.name, sizeof(kctl->id.name),
1846 "%s Validity", name);
1847 else
1848 snprintf(kctl->id.name, sizeof(kctl->id.name),
1849 "Clock Source %d Validity", hdr->bClockID);
1850
1851 return snd_usb_mixer_add_control(&cval->head, kctl);
1852}
1853
1854/*
1855 * parse a feature unit
1856 *
1857 * most of controls are defined here.
1858 */
1859static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1860 void *_ftr)
1861{
1862 int channels, i, j;
1863 struct usb_audio_term iterm;
1864 unsigned int master_bits, first_ch_bits;
1865 int err, csize;
1866 struct uac_feature_unit_descriptor *hdr = _ftr;
1867 __u8 *bmaControls;
1868
1869 if (state->mixer->protocol == UAC_VERSION_1) {
1870 csize = hdr->bControlSize;
1871 channels = (hdr->bLength - 7) / csize - 1;
1872 bmaControls = hdr->bmaControls;
1873 } else if (state->mixer->protocol == UAC_VERSION_2) {
1874 struct uac2_feature_unit_descriptor *ftr = _ftr;
1875 csize = 4;
1876 channels = (hdr->bLength - 6) / 4 - 1;
1877 bmaControls = ftr->bmaControls;
1878 } else { /* UAC_VERSION_3 */
1879 struct uac3_feature_unit_descriptor *ftr = _ftr;
1880
1881 csize = 4;
1882 channels = (ftr->bLength - 7) / 4 - 1;
1883 bmaControls = ftr->bmaControls;
1884 }
1885
1886 /* parse the source unit */
1887 err = parse_audio_unit(state, hdr->bSourceID);
1888 if (err < 0)
1889 return err;
1890
1891 /* determine the input source type and name */
1892 err = check_input_term(state, hdr->bSourceID, &iterm);
1893 if (err < 0)
1894 return err;
1895
1896 master_bits = snd_usb_combine_bytes(bmaControls, csize);
1897 /* master configuration quirks */
1898 switch (state->chip->usb_id) {
1899 case USB_ID(0x08bb, 0x2702):
1900 usb_audio_info(state->chip,
1901 "usbmixer: master volume quirk for PCM2702 chip\n");
1902 /* disable non-functional volume control */
1903 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
1904 break;
1905 case USB_ID(0x1130, 0xf211):
1906 usb_audio_info(state->chip,
1907 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
1908 /* disable non-functional volume control */
1909 channels = 0;
1910 break;
1911
1912 }
1913 if (channels > 0)
1914 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
1915 else
1916 first_ch_bits = 0;
1917
1918 if (state->mixer->protocol == UAC_VERSION_1) {
1919 /* check all control types */
1920 for (i = 0; i < 10; i++) {
1921 unsigned int ch_bits = 0;
1922 int control = audio_feature_info[i].control;
1923
1924 for (j = 0; j < channels; j++) {
1925 unsigned int mask;
1926
1927 mask = snd_usb_combine_bytes(bmaControls +
1928 csize * (j+1), csize);
1929 if (mask & (1 << i))
1930 ch_bits |= (1 << j);
1931 }
1932 /* audio class v1 controls are never read-only */
1933
1934 /*
1935 * The first channel must be set
1936 * (for ease of programming).
1937 */
1938 if (ch_bits & 1)
1939 build_feature_ctl(state, _ftr, ch_bits, control,
1940 &iterm, unitid, 0);
1941 if (master_bits & (1 << i))
1942 build_feature_ctl(state, _ftr, 0, control,
1943 &iterm, unitid, 0);
1944 }
1945 } else { /* UAC_VERSION_2/3 */
1946 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
1947 unsigned int ch_bits = 0;
1948 unsigned int ch_read_only = 0;
1949 int control = audio_feature_info[i].control;
1950
1951 for (j = 0; j < channels; j++) {
1952 unsigned int mask;
1953
1954 mask = snd_usb_combine_bytes(bmaControls +
1955 csize * (j+1), csize);
1956 if (uac_v2v3_control_is_readable(mask, control)) {
1957 ch_bits |= (1 << j);
1958 if (!uac_v2v3_control_is_writeable(mask, control))
1959 ch_read_only |= (1 << j);
1960 }
1961 }
1962
1963 /*
1964 * NOTE: build_feature_ctl() will mark the control
1965 * read-only if all channels are marked read-only in
1966 * the descriptors. Otherwise, the control will be
1967 * reported as writeable, but the driver will not
1968 * actually issue a write command for read-only
1969 * channels.
1970 */
1971
1972 /*
1973 * The first channel must be set
1974 * (for ease of programming).
1975 */
1976 if (ch_bits & 1)
1977 build_feature_ctl(state, _ftr, ch_bits, control,
1978 &iterm, unitid, ch_read_only);
1979 if (uac_v2v3_control_is_readable(master_bits, control))
1980 build_feature_ctl(state, _ftr, 0, control,
1981 &iterm, unitid,
1982 !uac_v2v3_control_is_writeable(master_bits,
1983 control));
1984 }
1985 }
1986
1987 return 0;
1988}
1989
1990/*
1991 * Mixer Unit
1992 */
1993
1994/* check whether the given in/out overflows bmMixerControls matrix */
1995static bool mixer_bitmap_overflow(struct uac_mixer_unit_descriptor *desc,
1996 int protocol, int num_ins, int num_outs)
1997{
1998 u8 *hdr = (u8 *)desc;
1999 u8 *c = uac_mixer_unit_bmControls(desc, protocol);
2000 size_t rest; /* remaining bytes after bmMixerControls */
2001
2002 switch (protocol) {
2003 case UAC_VERSION_1:
2004 default:
2005 rest = 1; /* iMixer */
2006 break;
2007 case UAC_VERSION_2:
2008 rest = 2; /* bmControls + iMixer */
2009 break;
2010 case UAC_VERSION_3:
2011 rest = 6; /* bmControls + wMixerDescrStr */
2012 break;
2013 }
2014
2015 /* overflow? */
2016 return c + (num_ins * num_outs + 7) / 8 + rest > hdr + hdr[0];
2017}
2018
2019/*
2020 * build a mixer unit control
2021 *
2022 * the callbacks are identical with feature unit.
2023 * input channel number (zero based) is given in control field instead.
2024 */
2025static void build_mixer_unit_ctl(struct mixer_build *state,
2026 struct uac_mixer_unit_descriptor *desc,
2027 int in_pin, int in_ch, int num_outs,
2028 int unitid, struct usb_audio_term *iterm)
2029{
2030 struct usb_mixer_elem_info *cval;
2031 unsigned int i, len;
2032 struct snd_kcontrol *kctl;
2033 const struct usbmix_name_map *map;
2034
2035 map = find_map(state->map, unitid, 0);
2036 if (check_ignored_ctl(map))
2037 return;
2038
2039 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2040 if (!cval)
2041 return;
2042
2043 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2044 cval->control = in_ch + 1; /* based on 1 */
2045 cval->val_type = USB_MIXER_S16;
2046 for (i = 0; i < num_outs; i++) {
2047 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2048
2049 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
2050 cval->cmask |= (1 << i);
2051 cval->channels++;
2052 }
2053 }
2054
2055 /* get min/max values */
2056 get_min_max(cval, 0);
2057
2058 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
2059 if (!kctl) {
2060 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2061 usb_mixer_elem_info_free(cval);
2062 return;
2063 }
2064 kctl->private_free = snd_usb_mixer_elem_free;
2065
2066 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2067 if (!len)
2068 len = get_term_name(state->chip, iterm, kctl->id.name,
2069 sizeof(kctl->id.name), 0);
2070 if (!len)
2071 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
2072 append_ctl_name(kctl, " Volume");
2073
2074 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
2075 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2076 snd_usb_mixer_add_control(&cval->head, kctl);
2077}
2078
2079static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2080 void *raw_desc)
2081{
2082 struct usb_audio_term iterm;
2083 unsigned int control, bmctls, term_id;
2084
2085 if (state->mixer->protocol == UAC_VERSION_2) {
2086 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2087 control = UAC2_TE_CONNECTOR;
2088 term_id = d_v2->bTerminalID;
2089 bmctls = le16_to_cpu(d_v2->bmControls);
2090 } else if (state->mixer->protocol == UAC_VERSION_3) {
2091 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2092 control = UAC3_TE_INSERTION;
2093 term_id = d_v3->bTerminalID;
2094 bmctls = le32_to_cpu(d_v3->bmControls);
2095 } else {
2096 return 0; /* UAC1. No Insertion control */
2097 }
2098
2099 check_input_term(state, term_id, &iterm);
2100
2101 /* Check for jack detection. */
2102 if (uac_v2v3_control_is_readable(bmctls, control))
2103 build_connector_control(state->mixer, &iterm, true);
2104
2105 return 0;
2106}
2107
2108/*
2109 * parse a mixer unit
2110 */
2111static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2112 void *raw_desc)
2113{
2114 struct uac_mixer_unit_descriptor *desc = raw_desc;
2115 struct usb_audio_term iterm;
2116 int input_pins, num_ins, num_outs;
2117 int pin, ich, err;
2118
2119 err = uac_mixer_unit_get_channels(state, desc);
2120 if (err < 0) {
2121 usb_audio_err(state->chip,
2122 "invalid MIXER UNIT descriptor %d\n",
2123 unitid);
2124 return err;
2125 }
2126
2127 num_outs = err;
2128 input_pins = desc->bNrInPins;
2129
2130 num_ins = 0;
2131 ich = 0;
2132 for (pin = 0; pin < input_pins; pin++) {
2133 err = parse_audio_unit(state, desc->baSourceID[pin]);
2134 if (err < 0)
2135 continue;
2136 /* no bmControls field (e.g. Maya44) -> ignore */
2137 if (!num_outs)
2138 continue;
2139 err = check_input_term(state, desc->baSourceID[pin], &iterm);
2140 if (err < 0)
2141 return err;
2142 num_ins += iterm.channels;
2143 if (mixer_bitmap_overflow(desc, state->mixer->protocol,
2144 num_ins, num_outs))
2145 break;
2146 for (; ich < num_ins; ich++) {
2147 int och, ich_has_controls = 0;
2148
2149 for (och = 0; och < num_outs; och++) {
2150 __u8 *c = uac_mixer_unit_bmControls(desc,
2151 state->mixer->protocol);
2152
2153 if (check_matrix_bitmap(c, ich, och, num_outs)) {
2154 ich_has_controls = 1;
2155 break;
2156 }
2157 }
2158 if (ich_has_controls)
2159 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
2160 unitid, &iterm);
2161 }
2162 }
2163 return 0;
2164}
2165
2166/*
2167 * Processing Unit / Extension Unit
2168 */
2169
2170/* get callback for processing/extension unit */
2171static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
2173{
2174 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2175 int err, val;
2176
2177 err = get_cur_ctl_value(cval, cval->control << 8, &val);
2178 if (err < 0) {
2179 ucontrol->value.integer.value[0] = cval->min;
2180 return filter_error(cval, err);
2181 }
2182 val = get_relative_value(cval, val);
2183 ucontrol->value.integer.value[0] = val;
2184 return 0;
2185}
2186
2187/* put callback for processing/extension unit */
2188static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2190{
2191 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2192 int val, oval, err;
2193
2194 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2195 if (err < 0)
2196 return filter_error(cval, err);
2197 val = ucontrol->value.integer.value[0];
2198 val = get_abs_value(cval, val);
2199 if (val != oval) {
2200 set_cur_ctl_value(cval, cval->control << 8, val);
2201 return 1;
2202 }
2203 return 0;
2204}
2205
2206/* alsa control interface for processing/extension unit */
2207static const struct snd_kcontrol_new mixer_procunit_ctl = {
2208 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2209 .name = "", /* will be filled later */
2210 .info = mixer_ctl_feature_info,
2211 .get = mixer_ctl_procunit_get,
2212 .put = mixer_ctl_procunit_put,
2213};
2214
2215/*
2216 * predefined data for processing units
2217 */
2218struct procunit_value_info {
2219 int control;
2220 char *suffix;
2221 int val_type;
2222 int min_value;
2223};
2224
2225struct procunit_info {
2226 int type;
2227 char *name;
2228 struct procunit_value_info *values;
2229};
2230
2231static struct procunit_value_info undefined_proc_info[] = {
2232 { 0x00, "Control Undefined", 0 },
2233 { 0 }
2234};
2235
2236static struct procunit_value_info updown_proc_info[] = {
2237 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2238 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2239 { 0 }
2240};
2241static struct procunit_value_info prologic_proc_info[] = {
2242 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2243 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2244 { 0 }
2245};
2246static struct procunit_value_info threed_enh_proc_info[] = {
2247 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2248 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
2249 { 0 }
2250};
2251static struct procunit_value_info reverb_proc_info[] = {
2252 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2253 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2254 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2255 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
2256 { 0 }
2257};
2258static struct procunit_value_info chorus_proc_info[] = {
2259 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2260 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2261 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2262 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
2263 { 0 }
2264};
2265static struct procunit_value_info dcr_proc_info[] = {
2266 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2267 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2268 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2269 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2270 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2271 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
2272 { 0 }
2273};
2274
2275static struct procunit_info procunits[] = {
2276 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2277 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2278 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2279 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2280 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2281 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
2282 { 0 },
2283};
2284
2285static struct procunit_value_info uac3_updown_proc_info[] = {
2286 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2287 { 0 }
2288};
2289static struct procunit_value_info uac3_stereo_ext_proc_info[] = {
2290 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 },
2291 { 0 }
2292};
2293
2294static struct procunit_info uac3_procunits[] = {
2295 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info },
2296 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info },
2297 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info },
2298 { 0 },
2299};
2300
2301/*
2302 * predefined data for extension units
2303 */
2304static struct procunit_value_info clock_rate_xu_info[] = {
2305 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2306 { 0 }
2307};
2308static struct procunit_value_info clock_source_xu_info[] = {
2309 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2310 { 0 }
2311};
2312static struct procunit_value_info spdif_format_xu_info[] = {
2313 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2314 { 0 }
2315};
2316static struct procunit_value_info soft_limit_xu_info[] = {
2317 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2318 { 0 }
2319};
2320static struct procunit_info extunits[] = {
2321 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2322 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2323 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2324 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2325 { 0 }
2326};
2327
2328/*
2329 * build a processing/extension unit
2330 */
2331static int build_audio_procunit(struct mixer_build *state, int unitid,
2332 void *raw_desc, struct procunit_info *list,
2333 bool extension_unit)
2334{
2335 struct uac_processing_unit_descriptor *desc = raw_desc;
2336 int num_ins;
2337 struct usb_mixer_elem_info *cval;
2338 struct snd_kcontrol *kctl;
2339 int i, err, nameid, type, len;
2340 struct procunit_info *info;
2341 struct procunit_value_info *valinfo;
2342 const struct usbmix_name_map *map;
2343 static struct procunit_value_info default_value_info[] = {
2344 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2345 { 0 }
2346 };
2347 static struct procunit_info default_info = {
2348 0, NULL, default_value_info
2349 };
2350 const char *name = extension_unit ?
2351 "Extension Unit" : "Processing Unit";
2352
2353 num_ins = desc->bNrInPins;
2354 for (i = 0; i < num_ins; i++) {
2355 err = parse_audio_unit(state, desc->baSourceID[i]);
2356 if (err < 0)
2357 return err;
2358 }
2359
2360 type = le16_to_cpu(desc->wProcessType);
2361 for (info = list; info && info->type; info++)
2362 if (info->type == type)
2363 break;
2364 if (!info || !info->type)
2365 info = &default_info;
2366
2367 for (valinfo = info->values; valinfo->control; valinfo++) {
2368 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
2369
2370 if (state->mixer->protocol == UAC_VERSION_1) {
2371 if (!(controls[valinfo->control / 8] &
2372 (1 << ((valinfo->control % 8) - 1))))
2373 continue;
2374 } else { /* UAC_VERSION_2/3 */
2375 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8],
2376 valinfo->control))
2377 continue;
2378 }
2379
2380 map = find_map(state->map, unitid, valinfo->control);
2381 if (check_ignored_ctl(map))
2382 continue;
2383 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2384 if (!cval)
2385 return -ENOMEM;
2386 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2387 cval->control = valinfo->control;
2388 cval->val_type = valinfo->val_type;
2389 cval->channels = 1;
2390
2391 if (state->mixer->protocol > UAC_VERSION_1 &&
2392 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8],
2393 valinfo->control))
2394 cval->master_readonly = 1;
2395
2396 /* get min/max values */
2397 switch (type) {
2398 case UAC_PROCESS_UP_DOWNMIX: {
2399 bool mode_sel = false;
2400
2401 switch (state->mixer->protocol) {
2402 case UAC_VERSION_1:
2403 case UAC_VERSION_2:
2404 default:
2405 if (cval->control == UAC_UD_MODE_SELECT)
2406 mode_sel = true;
2407 break;
2408 case UAC_VERSION_3:
2409 if (cval->control == UAC3_UD_MODE_SELECT)
2410 mode_sel = true;
2411 break;
2412 }
2413
2414 if (mode_sel) {
2415 __u8 *control_spec = uac_processing_unit_specific(desc,
2416 state->mixer->protocol);
2417 cval->min = 1;
2418 cval->max = control_spec[0];
2419 cval->res = 1;
2420 cval->initialized = 1;
2421 break;
2422 }
2423
2424 get_min_max(cval, valinfo->min_value);
2425 break;
2426 }
2427 case USB_XU_CLOCK_RATE:
2428 /*
2429 * E-Mu USB 0404/0202/TrackerPre/0204
2430 * samplerate control quirk
2431 */
2432 cval->min = 0;
2433 cval->max = 5;
2434 cval->res = 1;
2435 cval->initialized = 1;
2436 break;
2437 default:
2438 get_min_max(cval, valinfo->min_value);
2439 break;
2440 }
2441
2442 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
2443 if (!kctl) {
2444 usb_mixer_elem_info_free(cval);
2445 return -ENOMEM;
2446 }
2447 kctl->private_free = snd_usb_mixer_elem_free;
2448
2449 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
2450 /* nothing */ ;
2451 } else if (info->name) {
2452 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
2453 } else {
2454 if (extension_unit)
2455 nameid = uac_extension_unit_iExtension(desc, state->mixer->protocol);
2456 else
2457 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
2458 len = 0;
2459 if (nameid)
2460 len = snd_usb_copy_string_desc(state->chip,
2461 nameid,
2462 kctl->id.name,
2463 sizeof(kctl->id.name));
2464 if (!len)
2465 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2466 }
2467 append_ctl_name(kctl, " ");
2468 append_ctl_name(kctl, valinfo->suffix);
2469
2470 usb_audio_dbg(state->chip,
2471 "[%d] PU [%s] ch = %d, val = %d/%d\n",
2472 cval->head.id, kctl->id.name, cval->channels,
2473 cval->min, cval->max);
2474
2475 err = snd_usb_mixer_add_control(&cval->head, kctl);
2476 if (err < 0)
2477 return err;
2478 }
2479 return 0;
2480}
2481
2482static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2483 void *raw_desc)
2484{
2485 switch (state->mixer->protocol) {
2486 case UAC_VERSION_1:
2487 case UAC_VERSION_2:
2488 default:
2489 return build_audio_procunit(state, unitid, raw_desc,
2490 procunits, false);
2491 case UAC_VERSION_3:
2492 return build_audio_procunit(state, unitid, raw_desc,
2493 uac3_procunits, false);
2494 }
2495}
2496
2497static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2498 void *raw_desc)
2499{
2500 /*
2501 * Note that we parse extension units with processing unit descriptors.
2502 * That's ok as the layout is the same.
2503 */
2504 return build_audio_procunit(state, unitid, raw_desc, extunits, true);
2505}
2506
2507/*
2508 * Selector Unit
2509 */
2510
2511/*
2512 * info callback for selector unit
2513 * use an enumerator type for routing
2514 */
2515static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_info *uinfo)
2517{
2518 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2519 const char **itemlist = (const char **)kcontrol->private_value;
2520
2521 if (snd_BUG_ON(!itemlist))
2522 return -EINVAL;
2523 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
2524}
2525
2526/* get callback for selector unit */
2527static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2529{
2530 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2531 int val, err;
2532
2533 err = get_cur_ctl_value(cval, cval->control << 8, &val);
2534 if (err < 0) {
2535 ucontrol->value.enumerated.item[0] = 0;
2536 return filter_error(cval, err);
2537 }
2538 val = get_relative_value(cval, val);
2539 ucontrol->value.enumerated.item[0] = val;
2540 return 0;
2541}
2542
2543/* put callback for selector unit */
2544static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_value *ucontrol)
2546{
2547 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2548 int val, oval, err;
2549
2550 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2551 if (err < 0)
2552 return filter_error(cval, err);
2553 val = ucontrol->value.enumerated.item[0];
2554 val = get_abs_value(cval, val);
2555 if (val != oval) {
2556 set_cur_ctl_value(cval, cval->control << 8, val);
2557 return 1;
2558 }
2559 return 0;
2560}
2561
2562/* alsa control interface for selector unit */
2563static const struct snd_kcontrol_new mixer_selectunit_ctl = {
2564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2565 .name = "", /* will be filled later */
2566 .info = mixer_ctl_selector_info,
2567 .get = mixer_ctl_selector_get,
2568 .put = mixer_ctl_selector_put,
2569};
2570
2571/*
2572 * private free callback.
2573 * free both private_data and private_value
2574 */
2575static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
2576{
2577 int i, num_ins = 0;
2578
2579 if (kctl->private_data) {
2580 struct usb_mixer_elem_info *cval = kctl->private_data;
2581 num_ins = cval->max;
2582 usb_mixer_elem_info_free(cval);
2583 kctl->private_data = NULL;
2584 }
2585 if (kctl->private_value) {
2586 char **itemlist = (char **)kctl->private_value;
2587 for (i = 0; i < num_ins; i++)
2588 kfree(itemlist[i]);
2589 kfree(itemlist);
2590 kctl->private_value = 0;
2591 }
2592}
2593
2594/*
2595 * parse a selector unit
2596 */
2597static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2598 void *raw_desc)
2599{
2600 struct uac_selector_unit_descriptor *desc = raw_desc;
2601 unsigned int i, nameid, len;
2602 int err;
2603 struct usb_mixer_elem_info *cval;
2604 struct snd_kcontrol *kctl;
2605 const struct usbmix_name_map *map;
2606 char **namelist;
2607
2608 for (i = 0; i < desc->bNrInPins; i++) {
2609 err = parse_audio_unit(state, desc->baSourceID[i]);
2610 if (err < 0)
2611 return err;
2612 }
2613
2614 if (desc->bNrInPins == 1) /* only one ? nonsense! */
2615 return 0;
2616
2617 map = find_map(state->map, unitid, 0);
2618 if (check_ignored_ctl(map))
2619 return 0;
2620
2621 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2622 if (!cval)
2623 return -ENOMEM;
2624 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2625 cval->val_type = USB_MIXER_U8;
2626 cval->channels = 1;
2627 cval->min = 1;
2628 cval->max = desc->bNrInPins;
2629 cval->res = 1;
2630 cval->initialized = 1;
2631
2632 switch (state->mixer->protocol) {
2633 case UAC_VERSION_1:
2634 default:
2635 cval->control = 0;
2636 break;
2637 case UAC_VERSION_2:
2638 case UAC_VERSION_3:
2639 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2640 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2641 cval->control = UAC2_CX_CLOCK_SELECTOR;
2642 else /* UAC2/3_SELECTOR_UNIT */
2643 cval->control = UAC2_SU_SELECTOR;
2644 break;
2645 }
2646
2647 namelist = kcalloc(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
2648 if (!namelist) {
2649 err = -ENOMEM;
2650 goto error_cval;
2651 }
2652#define MAX_ITEM_NAME_LEN 64
2653 for (i = 0; i < desc->bNrInPins; i++) {
2654 struct usb_audio_term iterm;
2655 len = 0;
2656 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
2657 if (!namelist[i]) {
2658 err = -ENOMEM;
2659 goto error_name;
2660 }
2661 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2662 MAX_ITEM_NAME_LEN);
2663 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
2664 len = get_term_name(state->chip, &iterm, namelist[i],
2665 MAX_ITEM_NAME_LEN, 0);
2666 if (! len)
2667 sprintf(namelist[i], "Input %u", i);
2668 }
2669
2670 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2671 if (! kctl) {
2672 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2673 err = -ENOMEM;
2674 goto error_name;
2675 }
2676 kctl->private_value = (unsigned long)namelist;
2677 kctl->private_free = usb_mixer_selector_elem_free;
2678
2679 /* check the static mapping table at first */
2680 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2681 if (!len) {
2682 /* no mapping ? */
2683 switch (state->mixer->protocol) {
2684 case UAC_VERSION_1:
2685 case UAC_VERSION_2:
2686 default:
2687 /* if iSelector is given, use it */
2688 nameid = uac_selector_unit_iSelector(desc);
2689 if (nameid)
2690 len = snd_usb_copy_string_desc(state->chip,
2691 nameid, kctl->id.name,
2692 sizeof(kctl->id.name));
2693 break;
2694 case UAC_VERSION_3:
2695 /* TODO: Class-Specific strings not yet supported */
2696 break;
2697 }
2698
2699 /* ... or pick up the terminal name at next */
2700 if (!len)
2701 len = get_term_name(state->chip, &state->oterm,
2702 kctl->id.name, sizeof(kctl->id.name), 0);
2703 /* ... or use the fixed string "USB" as the last resort */
2704 if (!len)
2705 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2706
2707 /* and add the proper suffix */
2708 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2709 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2710 append_ctl_name(kctl, " Clock Source");
2711 else if ((state->oterm.type & 0xff00) == 0x0100)
2712 append_ctl_name(kctl, " Capture Source");
2713 else
2714 append_ctl_name(kctl, " Playback Source");
2715 }
2716
2717 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
2718 cval->head.id, kctl->id.name, desc->bNrInPins);
2719 return snd_usb_mixer_add_control(&cval->head, kctl);
2720
2721 error_name:
2722 for (i = 0; i < desc->bNrInPins; i++)
2723 kfree(namelist[i]);
2724 kfree(namelist);
2725 error_cval:
2726 usb_mixer_elem_info_free(cval);
2727 return err;
2728}
2729
2730/*
2731 * parse an audio unit recursively
2732 */
2733
2734static int parse_audio_unit(struct mixer_build *state, int unitid)
2735{
2736 unsigned char *p1;
2737 int protocol = state->mixer->protocol;
2738
2739 if (test_and_set_bit(unitid, state->unitbitmap))
2740 return 0; /* the unit already visited */
2741
2742 p1 = find_audio_control_unit(state, unitid);
2743 if (!p1) {
2744 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
2745 return -EINVAL;
2746 }
2747
2748 if (!snd_usb_validate_audio_desc(p1, protocol)) {
2749 usb_audio_dbg(state->chip, "invalid unit %d\n", unitid);
2750 return 0; /* skip invalid unit */
2751 }
2752
2753 switch (PTYPE(protocol, p1[2])) {
2754 case PTYPE(UAC_VERSION_1, UAC_INPUT_TERMINAL):
2755 case PTYPE(UAC_VERSION_2, UAC_INPUT_TERMINAL):
2756 case PTYPE(UAC_VERSION_3, UAC_INPUT_TERMINAL):
2757 return parse_audio_input_terminal(state, unitid, p1);
2758 case PTYPE(UAC_VERSION_1, UAC_MIXER_UNIT):
2759 case PTYPE(UAC_VERSION_2, UAC_MIXER_UNIT):
2760 case PTYPE(UAC_VERSION_3, UAC3_MIXER_UNIT):
2761 return parse_audio_mixer_unit(state, unitid, p1);
2762 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SOURCE):
2763 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SOURCE):
2764 return parse_clock_source_unit(state, unitid, p1);
2765 case PTYPE(UAC_VERSION_1, UAC_SELECTOR_UNIT):
2766 case PTYPE(UAC_VERSION_2, UAC_SELECTOR_UNIT):
2767 case PTYPE(UAC_VERSION_3, UAC3_SELECTOR_UNIT):
2768 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SELECTOR):
2769 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SELECTOR):
2770 return parse_audio_selector_unit(state, unitid, p1);
2771 case PTYPE(UAC_VERSION_1, UAC_FEATURE_UNIT):
2772 case PTYPE(UAC_VERSION_2, UAC_FEATURE_UNIT):
2773 case PTYPE(UAC_VERSION_3, UAC3_FEATURE_UNIT):
2774 return parse_audio_feature_unit(state, unitid, p1);
2775 case PTYPE(UAC_VERSION_1, UAC1_PROCESSING_UNIT):
2776 case PTYPE(UAC_VERSION_2, UAC2_PROCESSING_UNIT_V2):
2777 case PTYPE(UAC_VERSION_3, UAC3_PROCESSING_UNIT):
2778 return parse_audio_processing_unit(state, unitid, p1);
2779 case PTYPE(UAC_VERSION_1, UAC1_EXTENSION_UNIT):
2780 case PTYPE(UAC_VERSION_2, UAC2_EXTENSION_UNIT_V2):
2781 case PTYPE(UAC_VERSION_3, UAC3_EXTENSION_UNIT):
2782 return parse_audio_extension_unit(state, unitid, p1);
2783 case PTYPE(UAC_VERSION_2, UAC2_EFFECT_UNIT):
2784 case PTYPE(UAC_VERSION_3, UAC3_EFFECT_UNIT):
2785 return 0; /* FIXME - effect units not implemented yet */
2786 default:
2787 usb_audio_err(state->chip,
2788 "unit %u: unexpected type 0x%02x\n",
2789 unitid, p1[2]);
2790 return -EINVAL;
2791 }
2792}
2793
2794static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2795{
2796 /* kill pending URBs */
2797 snd_usb_mixer_disconnect(mixer);
2798
2799 kfree(mixer->id_elems);
2800 if (mixer->urb) {
2801 kfree(mixer->urb->transfer_buffer);
2802 usb_free_urb(mixer->urb);
2803 }
2804 usb_free_urb(mixer->rc_urb);
2805 kfree(mixer->rc_setup_packet);
2806 kfree(mixer);
2807}
2808
2809static int snd_usb_mixer_dev_free(struct snd_device *device)
2810{
2811 struct usb_mixer_interface *mixer = device->device_data;
2812 snd_usb_mixer_free(mixer);
2813 return 0;
2814}
2815
2816/* UAC3 predefined channels configuration */
2817struct uac3_badd_profile {
2818 int subclass;
2819 const char *name;
2820 int c_chmask; /* capture channels mask */
2821 int p_chmask; /* playback channels mask */
2822 int st_chmask; /* side tone mixing channel mask */
2823};
2824
2825static struct uac3_badd_profile uac3_badd_profiles[] = {
2826 {
2827 /*
2828 * BAIF, BAOF or combination of both
2829 * IN: Mono or Stereo cfg, Mono alt possible
2830 * OUT: Mono or Stereo cfg, Mono alt possible
2831 */
2832 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2833 .name = "GENERIC IO",
2834 .c_chmask = -1, /* dynamic channels */
2835 .p_chmask = -1, /* dynamic channels */
2836 },
2837 {
2838 /* BAOF; Stereo only cfg, Mono alt possible */
2839 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2840 .name = "HEADPHONE",
2841 .p_chmask = 3,
2842 },
2843 {
2844 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2845 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2846 .name = "SPEAKER",
2847 .p_chmask = -1, /* dynamic channels */
2848 },
2849 {
2850 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2851 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2852 .name = "MICROPHONE",
2853 .c_chmask = -1, /* dynamic channels */
2854 },
2855 {
2856 /*
2857 * BAIOF topology
2858 * IN: Mono only
2859 * OUT: Mono or Stereo cfg, Mono alt possible
2860 */
2861 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2862 .name = "HEADSET",
2863 .c_chmask = 1,
2864 .p_chmask = -1, /* dynamic channels */
2865 .st_chmask = 1,
2866 },
2867 {
2868 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2869 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2870 .name = "HEADSET ADAPTER",
2871 .c_chmask = 1,
2872 .p_chmask = 3,
2873 .st_chmask = 1,
2874 },
2875 {
2876 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2877 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2878 .name = "SPEAKERPHONE",
2879 .c_chmask = 1,
2880 .p_chmask = 1,
2881 },
2882 { 0 } /* terminator */
2883};
2884
2885static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2886 struct uac3_badd_profile *f,
2887 int c_chmask, int p_chmask)
2888{
2889 /*
2890 * If both playback/capture channels are dynamic, make sure
2891 * at least one channel is present
2892 */
2893 if (f->c_chmask < 0 && f->p_chmask < 0) {
2894 if (!c_chmask && !p_chmask) {
2895 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2896 f->name);
2897 return false;
2898 }
2899 return true;
2900 }
2901
2902 if ((f->c_chmask < 0 && !c_chmask) ||
2903 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2904 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2905 f->name);
2906 return false;
2907 }
2908 if ((f->p_chmask < 0 && !p_chmask) ||
2909 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2910 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2911 f->name);
2912 return false;
2913 }
2914 return true;
2915}
2916
2917/*
2918 * create mixer controls for UAC3 BADD profiles
2919 *
2920 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2921 *
2922 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2923 */
2924static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2925 int ctrlif)
2926{
2927 struct usb_device *dev = mixer->chip->dev;
2928 struct usb_interface_assoc_descriptor *assoc;
2929 int badd_profile = mixer->chip->badd_profile;
2930 struct uac3_badd_profile *f;
2931 const struct usbmix_ctl_map *map;
2932 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2933 int i;
2934
2935 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2936
2937 /* Detect BADD capture/playback channels from AS EP descriptors */
2938 for (i = 0; i < assoc->bInterfaceCount; i++) {
2939 int intf = assoc->bFirstInterface + i;
2940
2941 struct usb_interface *iface;
2942 struct usb_host_interface *alts;
2943 struct usb_interface_descriptor *altsd;
2944 unsigned int maxpacksize;
2945 char dir_in;
2946 int chmask, num;
2947
2948 if (intf == ctrlif)
2949 continue;
2950
2951 iface = usb_ifnum_to_if(dev, intf);
2952 if (!iface)
2953 continue;
2954
2955 num = iface->num_altsetting;
2956
2957 if (num < 2)
2958 return -EINVAL;
2959
2960 /*
2961 * The number of Channels in an AudioStreaming interface
2962 * and the audio sample bit resolution (16 bits or 24
2963 * bits) can be derived from the wMaxPacketSize field in
2964 * the Standard AS Audio Data Endpoint descriptor in
2965 * Alternate Setting 1
2966 */
2967 alts = &iface->altsetting[1];
2968 altsd = get_iface_desc(alts);
2969
2970 if (altsd->bNumEndpoints < 1)
2971 return -EINVAL;
2972
2973 /* check direction */
2974 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2975 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2976
2977 switch (maxpacksize) {
2978 default:
2979 usb_audio_err(mixer->chip,
2980 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2981 maxpacksize);
2982 return -EINVAL;
2983 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2984 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2985 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2986 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2987 chmask = 1;
2988 break;
2989 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2990 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2991 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2992 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2993 chmask = 3;
2994 break;
2995 }
2996
2997 if (dir_in)
2998 c_chmask = chmask;
2999 else
3000 p_chmask = chmask;
3001 }
3002
3003 usb_audio_dbg(mixer->chip,
3004 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
3005 badd_profile, c_chmask, p_chmask);
3006
3007 /* check the mapping table */
3008 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
3009 if (map->id == badd_profile)
3010 break;
3011 }
3012
3013 if (!map->id)
3014 return -EINVAL;
3015
3016 for (f = uac3_badd_profiles; f->name; f++) {
3017 if (badd_profile == f->subclass)
3018 break;
3019 }
3020 if (!f->name)
3021 return -EINVAL;
3022 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
3023 return -EINVAL;
3024 st_chmask = f->st_chmask;
3025
3026 /* Playback */
3027 if (p_chmask) {
3028 /* Master channel, always writable */
3029 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3030 UAC3_BADD_FU_ID2, map->map);
3031 /* Mono/Stereo volume channels, always writable */
3032 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
3033 UAC3_BADD_FU_ID2, map->map);
3034 }
3035
3036 /* Capture */
3037 if (c_chmask) {
3038 /* Master channel, always writable */
3039 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3040 UAC3_BADD_FU_ID5, map->map);
3041 /* Mono/Stereo volume channels, always writable */
3042 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
3043 UAC3_BADD_FU_ID5, map->map);
3044 }
3045
3046 /* Side tone-mixing */
3047 if (st_chmask) {
3048 /* Master channel, always writable */
3049 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3050 UAC3_BADD_FU_ID7, map->map);
3051 /* Mono volume channel, always writable */
3052 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
3053 UAC3_BADD_FU_ID7, map->map);
3054 }
3055
3056 /* Insertion Control */
3057 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
3058 struct usb_audio_term iterm, oterm;
3059
3060 /* Input Term - Insertion control */
3061 memset(&iterm, 0, sizeof(iterm));
3062 iterm.id = UAC3_BADD_IT_ID4;
3063 iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3064 build_connector_control(mixer, &iterm, true);
3065
3066 /* Output Term - Insertion control */
3067 memset(&oterm, 0, sizeof(oterm));
3068 oterm.id = UAC3_BADD_OT_ID3;
3069 oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3070 build_connector_control(mixer, &oterm, false);
3071 }
3072
3073 return 0;
3074}
3075
3076/*
3077 * create mixer controls
3078 *
3079 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
3080 */
3081static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
3082{
3083 struct mixer_build state;
3084 int err;
3085 const struct usbmix_ctl_map *map;
3086 void *p;
3087
3088 memset(&state, 0, sizeof(state));
3089 state.chip = mixer->chip;
3090 state.mixer = mixer;
3091 state.buffer = mixer->hostif->extra;
3092 state.buflen = mixer->hostif->extralen;
3093
3094 /* check the mapping table */
3095 for (map = usbmix_ctl_maps; map->id; map++) {
3096 if (map->id == state.chip->usb_id) {
3097 state.map = map->map;
3098 state.selector_map = map->selector_map;
3099 mixer->ignore_ctl_error = map->ignore_ctl_error;
3100 break;
3101 }
3102 }
3103
3104 p = NULL;
3105 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3106 mixer->hostif->extralen,
3107 p, UAC_OUTPUT_TERMINAL)) != NULL) {
3108 if (!snd_usb_validate_audio_desc(p, mixer->protocol))
3109 continue; /* skip invalid descriptor */
3110
3111 if (mixer->protocol == UAC_VERSION_1) {
3112 struct uac1_output_terminal_descriptor *desc = p;
3113
3114 /* mark terminal ID as visited */
3115 set_bit(desc->bTerminalID, state.unitbitmap);
3116 state.oterm.id = desc->bTerminalID;
3117 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3118 state.oterm.name = desc->iTerminal;
3119 err = parse_audio_unit(&state, desc->bSourceID);
3120 if (err < 0 && err != -EINVAL)
3121 return err;
3122 } else if (mixer->protocol == UAC_VERSION_2) {
3123 struct uac2_output_terminal_descriptor *desc = p;
3124
3125 /* mark terminal ID as visited */
3126 set_bit(desc->bTerminalID, state.unitbitmap);
3127 state.oterm.id = desc->bTerminalID;
3128 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3129 state.oterm.name = desc->iTerminal;
3130 err = parse_audio_unit(&state, desc->bSourceID);
3131 if (err < 0 && err != -EINVAL)
3132 return err;
3133
3134 /*
3135 * For UAC2, use the same approach to also add the
3136 * clock selectors
3137 */
3138 err = parse_audio_unit(&state, desc->bCSourceID);
3139 if (err < 0 && err != -EINVAL)
3140 return err;
3141
3142 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
3143 UAC2_TE_CONNECTOR)) {
3144 build_connector_control(state.mixer, &state.oterm,
3145 false);
3146 }
3147 } else { /* UAC_VERSION_3 */
3148 struct uac3_output_terminal_descriptor *desc = p;
3149
3150 /* mark terminal ID as visited */
3151 set_bit(desc->bTerminalID, state.unitbitmap);
3152 state.oterm.id = desc->bTerminalID;
3153 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3154 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3155 err = parse_audio_unit(&state, desc->bSourceID);
3156 if (err < 0 && err != -EINVAL)
3157 return err;
3158
3159 /*
3160 * For UAC3, use the same approach to also add the
3161 * clock selectors
3162 */
3163 err = parse_audio_unit(&state, desc->bCSourceID);
3164 if (err < 0 && err != -EINVAL)
3165 return err;
3166
3167 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3168 UAC3_TE_INSERTION)) {
3169 build_connector_control(state.mixer, &state.oterm,
3170 false);
3171 }
3172 }
3173 }
3174
3175 return 0;
3176}
3177
3178void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
3179{
3180 struct usb_mixer_elem_list *list;
3181
3182 for_each_mixer_elem(list, mixer, unitid) {
3183 struct usb_mixer_elem_info *info =
3184 mixer_elem_list_to_info(list);
3185 /* invalidate cache, so the value is read from the device */
3186 info->cached = 0;
3187 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3188 &list->kctl->id);
3189 }
3190}
3191
3192static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
3193 struct usb_mixer_elem_list *list)
3194{
3195 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
3196 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3197 "S8", "U8", "S16", "U16"};
3198 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
3199 "channels=%i, type=\"%s\"\n", cval->head.id,
3200 cval->control, cval->cmask, cval->channels,
3201 val_types[cval->val_type]);
3202 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3203 cval->min, cval->max, cval->dBmin, cval->dBmax);
3204}
3205
3206static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3207 struct snd_info_buffer *buffer)
3208{
3209 struct snd_usb_audio *chip = entry->private_data;
3210 struct usb_mixer_interface *mixer;
3211 struct usb_mixer_elem_list *list;
3212 int unitid;
3213
3214 list_for_each_entry(mixer, &chip->mixer_list, list) {
3215 snd_iprintf(buffer,
3216 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
3217 chip->usb_id, snd_usb_ctrl_intf(chip),
3218 mixer->ignore_ctl_error);
3219 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3220 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
3221 for_each_mixer_elem(list, mixer, unitid) {
3222 snd_iprintf(buffer, " Unit: %i\n", list->id);
3223 if (list->kctl)
3224 snd_iprintf(buffer,
3225 " Control: name=\"%s\", index=%i\n",
3226 list->kctl->id.name,
3227 list->kctl->id.index);
3228 if (list->dump)
3229 list->dump(buffer, list);
3230 }
3231 }
3232 }
3233}
3234
3235static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3236 int attribute, int value, int index)
3237{
3238 struct usb_mixer_elem_list *list;
3239 __u8 unitid = (index >> 8) & 0xff;
3240 __u8 control = (value >> 8) & 0xff;
3241 __u8 channel = value & 0xff;
3242 unsigned int count = 0;
3243
3244 if (channel >= MAX_CHANNELS) {
3245 usb_audio_dbg(mixer->chip,
3246 "%s(): bogus channel number %d\n",
3247 __func__, channel);
3248 return;
3249 }
3250
3251 for_each_mixer_elem(list, mixer, unitid)
3252 count++;
3253
3254 if (count == 0)
3255 return;
3256
3257 for_each_mixer_elem(list, mixer, unitid) {
3258 struct usb_mixer_elem_info *info;
3259
3260 if (!list->kctl)
3261 continue;
3262
3263 info = mixer_elem_list_to_info(list);
3264 if (count > 1 && info->control != control)
3265 continue;
3266
3267 switch (attribute) {
3268 case UAC2_CS_CUR:
3269 /* invalidate cache, so the value is read from the device */
3270 if (channel)
3271 info->cached &= ~(1 << channel);
3272 else /* master channel */
3273 info->cached = 0;
3274
3275 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3276 &info->head.kctl->id);
3277 break;
3278
3279 case UAC2_CS_RANGE:
3280 /* TODO */
3281 break;
3282
3283 case UAC2_CS_MEM:
3284 /* TODO */
3285 break;
3286
3287 default:
3288 usb_audio_dbg(mixer->chip,
3289 "unknown attribute %d in interrupt\n",
3290 attribute);
3291 break;
3292 } /* switch */
3293 }
3294}
3295
3296static void snd_usb_mixer_interrupt(struct urb *urb)
3297{
3298 struct usb_mixer_interface *mixer = urb->context;
3299 int len = urb->actual_length;
3300 int ustatus = urb->status;
3301
3302 if (ustatus != 0)
3303 goto requeue;
3304
3305 if (mixer->protocol == UAC_VERSION_1) {
3306 struct uac1_status_word *status;
3307
3308 for (status = urb->transfer_buffer;
3309 len >= sizeof(*status);
3310 len -= sizeof(*status), status++) {
3311 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
3312 status->bStatusType,
3313 status->bOriginator);
3314
3315 /* ignore any notifications not from the control interface */
3316 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3317 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
3318 continue;
3319
3320 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3321 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
3322 else
3323 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3324 }
3325 } else { /* UAC_VERSION_2 */
3326 struct uac2_interrupt_data_msg *msg;
3327
3328 for (msg = urb->transfer_buffer;
3329 len >= sizeof(*msg);
3330 len -= sizeof(*msg), msg++) {
3331 /* drop vendor specific and endpoint requests */
3332 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3333 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3334 continue;
3335
3336 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3337 le16_to_cpu(msg->wValue),
3338 le16_to_cpu(msg->wIndex));
3339 }
3340 }
3341
3342requeue:
3343 if (ustatus != -ENOENT &&
3344 ustatus != -ECONNRESET &&
3345 ustatus != -ESHUTDOWN) {
3346 urb->dev = mixer->chip->dev;
3347 usb_submit_urb(urb, GFP_ATOMIC);
3348 }
3349}
3350
3351/* create the handler for the optional status interrupt endpoint */
3352static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3353{
3354 struct usb_endpoint_descriptor *ep;
3355 void *transfer_buffer;
3356 int buffer_length;
3357 unsigned int epnum;
3358
3359 /* we need one interrupt input endpoint */
3360 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
3361 return 0;
3362 ep = get_endpoint(mixer->hostif, 0);
3363 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
3364 return 0;
3365
3366 epnum = usb_endpoint_num(ep);
3367 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3368 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3369 if (!transfer_buffer)
3370 return -ENOMEM;
3371 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3372 if (!mixer->urb) {
3373 kfree(transfer_buffer);
3374 return -ENOMEM;
3375 }
3376 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3377 usb_rcvintpipe(mixer->chip->dev, epnum),
3378 transfer_buffer, buffer_length,
3379 snd_usb_mixer_interrupt, mixer, ep->bInterval);
3380 usb_submit_urb(mixer->urb, GFP_KERNEL);
3381 return 0;
3382}
3383
3384static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_value *ucontrol)
3386{
3387 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3388
3389 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3390 return 0;
3391}
3392
3393static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3394 struct snd_ctl_elem_value *ucontrol)
3395{
3396 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3397 bool keep_iface = !!ucontrol->value.integer.value[0];
3398
3399 if (mixer->chip->keep_iface == keep_iface)
3400 return 0;
3401 mixer->chip->keep_iface = keep_iface;
3402 return 1;
3403}
3404
3405static const struct snd_kcontrol_new keep_iface_ctl = {
3406 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3407 .name = "Keep Interface",
3408 .info = snd_ctl_boolean_mono_info,
3409 .get = keep_iface_ctl_get,
3410 .put = keep_iface_ctl_put,
3411};
3412
3413static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3414{
3415 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3416
3417 /* need only one control per card */
3418 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3419 snd_ctl_free_one(kctl);
3420 return 0;
3421 }
3422
3423 return snd_ctl_add(mixer->chip->card, kctl);
3424}
3425
3426int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3427 int ignore_error)
3428{
3429 static struct snd_device_ops dev_ops = {
3430 .dev_free = snd_usb_mixer_dev_free
3431 };
3432 struct usb_mixer_interface *mixer;
3433 struct snd_info_entry *entry;
3434 int err;
3435
3436 strcpy(chip->card->mixername, "USB Mixer");
3437
3438 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
3439 if (!mixer)
3440 return -ENOMEM;
3441 mixer->chip = chip;
3442 mixer->ignore_ctl_error = ignore_error;
3443 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3444 GFP_KERNEL);
3445 if (!mixer->id_elems) {
3446 kfree(mixer);
3447 return -ENOMEM;
3448 }
3449
3450 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3451 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
3452 case UAC_VERSION_1:
3453 default:
3454 mixer->protocol = UAC_VERSION_1;
3455 break;
3456 case UAC_VERSION_2:
3457 mixer->protocol = UAC_VERSION_2;
3458 break;
3459 case UAC_VERSION_3:
3460 mixer->protocol = UAC_VERSION_3;
3461 break;
3462 }
3463
3464 if (mixer->protocol == UAC_VERSION_3 &&
3465 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
3466 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3467 if (err < 0)
3468 goto _error;
3469 } else {
3470 err = snd_usb_mixer_controls(mixer);
3471 if (err < 0)
3472 goto _error;
3473 }
3474
3475 err = snd_usb_mixer_status_create(mixer);
3476 if (err < 0)
3477 goto _error;
3478
3479 err = create_keep_iface_ctl(mixer);
3480 if (err < 0)
3481 goto _error;
3482
3483 snd_usb_mixer_apply_create_quirk(mixer);
3484
3485 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
3486 if (err < 0)
3487 goto _error;
3488
3489 if (list_empty(&chip->mixer_list) &&
3490 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3491 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3492
3493 list_add(&mixer->list, &chip->mixer_list);
3494 return 0;
3495
3496_error:
3497 snd_usb_mixer_free(mixer);
3498 return err;
3499}
3500
3501void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
3502{
3503 if (mixer->disconnected)
3504 return;
3505 if (mixer->urb)
3506 usb_kill_urb(mixer->urb);
3507 if (mixer->rc_urb)
3508 usb_kill_urb(mixer->rc_urb);
3509 mixer->disconnected = true;
3510}
3511
3512#ifdef CONFIG_PM
3513/* stop any bus activity of a mixer */
3514static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3515{
3516 usb_kill_urb(mixer->urb);
3517 usb_kill_urb(mixer->rc_urb);
3518}
3519
3520static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3521{
3522 int err;
3523
3524 if (mixer->urb) {
3525 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3526 if (err < 0)
3527 return err;
3528 }
3529
3530 return 0;
3531}
3532
3533int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3534{
3535 snd_usb_mixer_inactivate(mixer);
3536 return 0;
3537}
3538
3539static int restore_mixer_value(struct usb_mixer_elem_list *list)
3540{
3541 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
3542 int c, err, idx;
3543
3544 if (cval->cmask) {
3545 idx = 0;
3546 for (c = 0; c < MAX_CHANNELS; c++) {
3547 if (!(cval->cmask & (1 << c)))
3548 continue;
3549 if (cval->cached & (1 << (c + 1))) {
3550 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
3551 cval->cache_val[idx]);
3552 if (err < 0)
3553 return err;
3554 }
3555 idx++;
3556 }
3557 } else {
3558 /* master */
3559 if (cval->cached) {
3560 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
3561 if (err < 0)
3562 return err;
3563 }
3564 }
3565
3566 return 0;
3567}
3568
3569int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3570{
3571 struct usb_mixer_elem_list *list;
3572 int id, err;
3573
3574 if (reset_resume) {
3575 /* restore cached mixer values */
3576 for (id = 0; id < MAX_ID_ELEMS; id++) {
3577 for_each_mixer_elem(list, mixer, id) {
3578 if (list->resume) {
3579 err = list->resume(list);
3580 if (err < 0)
3581 return err;
3582 }
3583 }
3584 }
3585 }
3586
3587 snd_usb_mixer_resume_quirk(mixer);
3588
3589 return snd_usb_mixer_activate(mixer);
3590}
3591#endif
3592
3593void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3594 struct usb_mixer_interface *mixer,
3595 int unitid)
3596{
3597 list->mixer = mixer;
3598 list->id = unitid;
3599 list->dump = snd_usb_mixer_dump_cval;
3600#ifdef CONFIG_PM
3601 list->resume = restore_mixer_value;
3602#endif
3603}