blob: b5927c3d5bc0b0657360ef8aa923c0aa344422bb [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * (Tentative) USB Audio Driver for ALSA
4 *
5 * Mixer control part
6 *
7 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 *
9 * Many codes borrowed from audio.c by
10 * Alan Cox (alan@lxorguk.ukuu.org.uk)
11 * Thomas Sailer (sailer@ife.ee.ethz.ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Daniel Mack157a57b2010-06-16 17:57:30 +020014/*
15 * TODOs, for both the mixer and the streaming interfaces:
16 *
17 * - support for UAC2 effect units
18 * - support for graphical equalizers
19 * - RANGE and MEM set commands (UAC2)
20 * - RANGE and MEM interrupt dispatchers (UAC2)
21 * - audio channel clustering (UAC2)
22 * - audio sample rate converter units (UAC2)
23 * - proper handling of clock multipliers (UAC2)
24 * - dispatch clock change notifications (UAC2)
25 * - stop PCM streams which use a clock that became invalid
26 * - stop PCM streams which use a clock selector that has changed
27 * - parse available sample rates again when clock sources changed
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/bitops.h>
31#include <linux/init.h>
32#include <linux/list.h>
Daniel Mackcddaafb2016-04-09 11:21:46 +020033#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/slab.h>
35#include <linux/string.h>
36#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010037#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010038#include <linux/usb/audio-v2.h>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020039#include <linux/usb/audio-v3.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <sound/core.h>
42#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020043#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020044#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020045#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010048#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010049#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010050#include "mixer_quirks.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010051#include "power.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020052
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010053#define MAX_ID_ELEMS 256
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct usb_audio_term {
56 int id;
57 int type;
58 int channels;
59 unsigned int chconfig;
60 int name;
61};
62
63struct usbmix_name_map;
64
Takashi Iwai86e07d32005-11-17 15:08:02 +010065struct mixer_build {
66 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020067 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned char *buffer;
69 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010070 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Hui Peng19bce472019-08-15 00:31:34 -040071 DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010072 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020074 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050077/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080078enum {
79 USB_XU_CLOCK_RATE = 0xe301,
80 USB_XU_CLOCK_SOURCE = 0xe302,
81 USB_XU_DIGITAL_IO_STATUS = 0xe303,
82 USB_XU_DEVICE_OPTIONS = 0xe304,
83 USB_XU_DIRECT_MONITORING = 0xe305,
84 USB_XU_METERING = 0xe306
85};
86enum {
87 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
88 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
89 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
90 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
91};
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * manual mapping of mixer names
95 * if the mixer topology is too complicated and the parsed names are
96 * ambiguous, add the entries in usbmixer_maps.c.
97 */
Daniel Mackf0b5e632010-03-11 21:13:23 +010098#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100100static const struct usbmix_name_map *
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300101find_map(const struct usbmix_name_map *p, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100103 if (!p)
104 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300106 for (; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100107 if (p->id == unitid &&
108 (!control || !p->control || control == p->control))
109 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100111 return NULL;
112}
113
114/* get the mapped name if the unit matches */
115static int
116check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
117{
118 if (!p || !p->name)
119 return 0;
120
121 buflen--;
122 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Takashi Iwai5aeee342014-11-18 11:02:14 +0100125/* ignore the error value if ignore_ctl_error flag is set */
126#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100127 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100130static inline int
131check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100133 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100135 return 1;
136}
137
138/* dB mapping */
139static inline void check_mapped_dB(const struct usbmix_name_map *p,
140 struct usb_mixer_elem_info *cval)
141{
142 if (p && p->dB) {
143 cval->dBmin = p->dB->min;
144 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200145 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200149/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100150static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200151 int index, char *buf, int buflen)
152{
153 const struct usbmix_selector_map *p;
154
Daniel Mack6bc170e2014-05-24 10:58:16 +0200155 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200156 return 0;
157 for (p = state->selector_map; p->id; p++) {
158 if (p->id == unitid && index < p->count)
159 return strlcpy(buf, p->names[index], buflen);
160 }
161 return 0;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * find an audio control unit with the given unit id
166 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200167static void *find_audio_control_unit(struct mixer_build *state,
168 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200170 /* we just parse the header */
171 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Daniel Mack67e1daa2010-05-31 13:35:43 +0200173 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
174 USB_DT_CS_INTERFACE)) != NULL) {
175 if (hdr->bLength >= 4 &&
176 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200177 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
Daniel Mack67e1daa2010-05-31 13:35:43 +0200178 hdr->bUnitID == unit)
179 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return NULL;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * copy a string with the given id
187 */
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300188static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200189 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300191 int len = usb_string(chip->dev, index, buf, maxlen - 1);
Jaejoong Kim251552a2017-12-04 15:31:48 +0900192
193 if (len < 0)
194 return 0;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 buf[len] = 0;
197 return len;
198}
199
200/*
201 * convert from the byte/word on usb descriptor to the zero-based integer
202 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100203static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 switch (cval->val_type) {
206 case USB_MIXER_BOOLEAN:
207 return !!val;
208 case USB_MIXER_INV_BOOLEAN:
209 return !val;
210 case USB_MIXER_U8:
211 val &= 0xff;
212 break;
213 case USB_MIXER_S8:
214 val &= 0xff;
215 if (val >= 0x80)
216 val -= 0x100;
217 break;
218 case USB_MIXER_U16:
219 val &= 0xffff;
220 break;
221 case USB_MIXER_S16:
222 val &= 0xffff;
223 if (val >= 0x8000)
224 val -= 0x10000;
225 break;
226 }
227 return val;
228}
229
230/*
231 * convert from the zero-based int to the byte/word for usb descriptor
232 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100233static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 switch (cval->val_type) {
236 case USB_MIXER_BOOLEAN:
237 return !!val;
238 case USB_MIXER_INV_BOOLEAN:
239 return !val;
240 case USB_MIXER_S8:
241 case USB_MIXER_U8:
242 return val & 0xff;
243 case USB_MIXER_S16:
244 case USB_MIXER_U16:
245 return val & 0xffff;
246 }
247 return 0; /* not reached */
248}
249
Takashi Iwai86e07d32005-11-17 15:08:02 +0100250static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200252 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 cval->res = 1;
254 if (val < cval->min)
255 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200256 else if (val >= cval->max)
257 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 else
259 return (val - cval->min) / cval->res;
260}
261
Takashi Iwai86e07d32005-11-17 15:08:02 +0100262static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 if (val < 0)
265 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200266 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cval->res = 1;
268 val *= cval->res;
269 val += cval->min;
270 if (val > cval->max)
271 return cval->max;
272 return val;
273}
274
Julian Scheelbc18e312015-08-14 16:14:45 +0200275static int uac2_ctl_value_size(int val_type)
276{
277 switch (val_type) {
278 case USB_MIXER_S32:
279 case USB_MIXER_U32:
280 return 4;
281 case USB_MIXER_S16:
282 case USB_MIXER_U16:
283 return 2;
284 default:
285 return 1;
286 }
287 return 0; /* unreachable */
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/*
292 * retrieve a mixer value
293 */
294
Daniel Mack6bc170e2014-05-24 10:58:16 +0200295static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
296 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Iwai3360b842014-11-18 11:47:04 +0100298 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned char buf[2];
300 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
301 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200302 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Takashi Iwai47ab1542015-08-25 16:09:00 +0200304 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100305 if (err < 0)
306 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100309 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700310 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
311 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
312 validx, idx, buf, val_len);
313 if (err >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200315 err = 0;
316 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700317 } else if (err == -ETIMEDOUT) {
318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100321 usb_audio_dbg(chip,
322 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
323 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200324 err = -EINVAL;
325
326 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200327 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Daniel Mack6bc170e2014-05-24 10:58:16 +0200331static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
332 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100333{
Takashi Iwai3360b842014-11-18 11:47:04 +0100334 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100335 /* enough space for one range */
336 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100337 unsigned char *val;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100338 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100339 __u8 bRequest;
340
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100341 val_size = uac2_ctl_value_size(cval->val_type);
342
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200343 if (request == UAC_GET_CUR) {
344 bRequest = UAC2_CS_CUR;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100345 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200346 } else {
347 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100348 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200349 }
350
351 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100352
Takashi Iwai47ab1542015-08-25 16:09:00 +0200353 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100354 if (ret)
355 goto error;
356
Takashi Iwai47ab1542015-08-25 16:09:00 +0200357 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
358 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100359 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200360 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200361 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100362
363 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100364error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100365 usb_audio_err(chip,
366 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
367 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100368 return ret;
369 }
370
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200371 /* FIXME: how should we handle multiple triplets here? */
372
Daniel Mack23caaf12010-03-11 21:13:25 +0100373 switch (request) {
374 case UAC_GET_CUR:
375 val = buf;
376 break;
377 case UAC_GET_MIN:
378 val = buf + sizeof(__u16);
379 break;
380 case UAC_GET_MAX:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100381 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100382 break;
383 case UAC_GET_RES:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100384 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100385 break;
386 default:
387 return -EINVAL;
388 }
389
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100390 *value_ret = convert_signed_value(cval,
391 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100392
393 return 0;
394}
395
Daniel Mack6bc170e2014-05-24 10:58:16 +0200396static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
397 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100398{
Eldad Zack9f814102012-11-28 23:55:35 +0100399 validx += cval->idx_off;
400
Takashi Iwai3360b842014-11-18 11:47:04 +0100401 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100402 get_ctl_value_v1(cval, request, validx, value_ret) :
403 get_ctl_value_v2(cval, request, validx, value_ret);
404}
405
Daniel Mack6bc170e2014-05-24 10:58:16 +0200406static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
407 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100409 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100413static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
414 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200416 return get_ctl_value(cval, UAC_GET_CUR,
417 (cval->control << 8) | channel,
418 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Chris J Argeseef90452014-11-12 12:07:01 -0600421int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100422 int channel, int index, int *value)
423{
424 int err;
425
426 if (cval->cached & (1 << channel)) {
427 *value = cval->cache_val[index];
428 return 0;
429 }
430 err = get_cur_mix_raw(cval, channel, value);
431 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100432 if (!cval->head.mixer->ignore_ctl_error)
433 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100434 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200435 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100436 return err;
437 }
438 cval->cached |= 1 << channel;
439 cval->cache_val[index] = *value;
440 return 0;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * set a mixer value
445 */
446
Daniel Mack7b1eda22010-03-11 21:13:22 +0100447int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
448 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai3360b842014-11-18 11:47:04 +0100450 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200451 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200452 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100453
Eldad Zack9f814102012-11-28 23:55:35 +0100454 validx += cval->idx_off;
455
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200456
Takashi Iwai3360b842014-11-18 11:47:04 +0100457 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100458 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200459 } else { /* UAC_VERSION_2/3 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200460 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100461
462 /* FIXME */
463 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100464 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100465 return -EINVAL;
466 }
467
468 request = UAC2_CS_CUR;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 value_set = convert_bytes_value(cval, value_set);
472 buf[0] = value_set & 0xff;
473 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 buf[2] = (value_set >> 16) & 0xff;
475 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200476
477 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100478 if (err < 0)
479 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200480
Takashi Iwai978520b2012-10-12 15:12:55 +0200481 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100482 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700483 err = snd_usb_ctl_msg(chip->dev,
484 usb_sndctrlpipe(chip->dev, 0), request,
485 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
486 validx, idx, buf, val_len);
487 if (err >= 0) {
Takashi Iwai978520b2012-10-12 15:12:55 +0200488 err = 0;
489 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700490 } else if (err == -ETIMEDOUT) {
491 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200493 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100494 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200495 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200496 err = -EINVAL;
497
498 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200499 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Daniel Mack6bc170e2014-05-24 10:58:16 +0200503static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
504 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100506 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Chris J Argeseef90452014-11-12 12:07:01 -0600509int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100510 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Takashi Iwai641b4872009-01-15 17:05:24 +0100512 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200513 unsigned int read_only = (channel == 0) ?
514 cval->master_readonly :
515 cval->ch_readonly & (1 << (channel - 1));
516
517 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100518 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100519 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200520 __func__, channel, cval->control);
521 return 0;
522 }
523
Daniel Mack6bc170e2014-05-24 10:58:16 +0200524 err = snd_usb_mixer_set_ctl_value(cval,
525 UAC_SET_CUR, (cval->control << 8) | channel,
526 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100527 if (err < 0)
528 return err;
529 cval->cached |= 1 << channel;
530 cval->cache_val[index] = value;
531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200534/*
535 * TLV callback for mixer volume controls
536 */
Felix Homann285de9c2012-04-23 20:24:24 +0200537int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200538 unsigned int size, unsigned int __user *_tlv)
539{
540 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200541 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200542
543 if (size < sizeof(scale))
544 return -ENOMEM;
Takashi Iwai0f174b32017-08-16 14:18:37 +0200545 if (cval->min_mute)
546 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100547 scale[2] = cval->dBmin;
548 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200549 if (copy_to_user(_tlv, scale, sizeof(scale)))
550 return -EFAULT;
551 return 0;
552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554/*
555 * parser routines begin here...
556 */
557
Takashi Iwai86e07d32005-11-17 15:08:02 +0100558static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560
561/*
562 * check if the input/output channel routing is enabled on the given bitmap.
563 * used for mixer unit parser
564 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200565static int check_matrix_bitmap(unsigned char *bmap,
566 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int idx = ich * num_outs + och;
569 return bmap[idx >> 3] & (0x80 >> (idx & 7));
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * add an alsa control element
574 * search and increment the index until an empty slot is found.
575 *
576 * if failed, give up and free the control instance.
577 */
578
Takashi Iwai3360b842014-11-18 11:47:04 +0100579int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200580 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai3360b842014-11-18 11:47:04 +0100582 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200584
Daniel Mackef9d5972011-05-25 09:09:00 +0200585 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 kctl->id.index++;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200587 err = snd_ctl_add(mixer->chip->card, kctl);
588 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200589 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
590 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100593 list->kctl = kctl;
594 list->next_id_elem = mixer->id_elems[list->id];
595 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * get a terminal name string
601 */
602
603static struct iterm_name_combo {
604 int type;
605 char *name;
606} iterm_names[] = {
607 { 0x0300, "Output" },
608 { 0x0301, "Speaker" },
609 { 0x0302, "Headphone" },
610 { 0x0303, "HMD Audio" },
611 { 0x0304, "Desktop Speaker" },
612 { 0x0305, "Room Speaker" },
613 { 0x0306, "Com Speaker" },
614 { 0x0307, "LFE" },
615 { 0x0600, "External In" },
616 { 0x0601, "Analog In" },
617 { 0x0602, "Digital In" },
618 { 0x0603, "Line" },
619 { 0x0604, "Legacy In" },
620 { 0x0605, "IEC958 In" },
621 { 0x0606, "1394 DA Stream" },
622 { 0x0607, "1394 DV Stream" },
623 { 0x0700, "Embedded" },
624 { 0x0701, "Noise Source" },
625 { 0x0702, "Equalization Noise" },
626 { 0x0703, "CD" },
627 { 0x0704, "DAT" },
628 { 0x0705, "DCC" },
629 { 0x0706, "MiniDisk" },
630 { 0x0707, "Analog Tape" },
631 { 0x0708, "Phonograph" },
632 { 0x0709, "VCR Audio" },
633 { 0x070a, "Video Disk Audio" },
634 { 0x070b, "DVD Audio" },
635 { 0x070c, "TV Tuner Audio" },
636 { 0x070d, "Satellite Rec Audio" },
637 { 0x070e, "Cable Tuner Audio" },
638 { 0x070f, "DSS Audio" },
639 { 0x0710, "Radio Receiver" },
640 { 0x0711, "Radio Transmitter" },
641 { 0x0712, "Multi-Track Recorder" },
642 { 0x0713, "Synthesizer" },
643 { 0 },
644};
645
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300646static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned char *name, int maxlen, int term_only)
648{
649 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100650 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100652 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300653 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200654 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100655 if (len)
656 return len;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* virtual type - not a real terminal */
660 if (iterm->type >> 16) {
661 if (term_only)
662 return 0;
663 switch (iterm->type >> 16) {
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100664 case UAC3_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200665 strcpy(name, "Selector");
666 return 8;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100667 case UAC3_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200668 strcpy(name, "Process Unit");
669 return 12;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100670 case UAC3_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200671 strcpy(name, "Ext Unit");
672 return 8;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100673 case UAC3_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200674 strcpy(name, "Mixer");
675 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 default:
677 return sprintf(name, "Unit %d", iterm->id);
678 }
679 }
680
681 switch (iterm->type & 0xff00) {
682 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200683 strcpy(name, "PCM");
684 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200686 strcpy(name, "Mic");
687 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200689 strcpy(name, "Headset");
690 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200692 strcpy(name, "Phone");
693 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Daniel Mack6bc170e2014-05-24 10:58:16 +0200696 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (names->type == iterm->type) {
698 strcpy(name, names->name);
699 return strlen(names->name);
700 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100707 * Get logical cluster information for UAC3 devices.
708 */
709static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
710{
711 struct uac3_cluster_header_descriptor c_header;
712 int err;
713
714 err = snd_usb_ctl_msg(state->chip->dev,
715 usb_rcvctrlpipe(state->chip->dev, 0),
716 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
717 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
718 cluster_id,
719 snd_usb_ctrl_intf(state->chip),
720 &c_header, sizeof(c_header));
721 if (err < 0)
722 goto error;
723 if (err != sizeof(c_header)) {
724 err = -EIO;
725 goto error;
726 }
727
728 return c_header.bNrChannels;
729
730error:
731 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
732 return err;
733}
734
735/*
736 * Get number of channels for a Mixer Unit.
737 */
738static int uac_mixer_unit_get_channels(struct mixer_build *state,
739 struct uac_mixer_unit_descriptor *desc)
740{
741 int mu_channels;
Takashi Iwai0bfe5e42018-12-19 14:04:47 +0100742 void *c;
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100743
Takashi Iwai0bfe5e42018-12-19 14:04:47 +0100744 if (desc->bLength < sizeof(*desc))
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100745 return -EINVAL;
746 if (!desc->bNrInPins)
747 return -EINVAL;
Hui Pengdaac0712019-08-13 22:34:04 -0400748 if (desc->bLength < sizeof(*desc) + desc->bNrInPins)
749 return -EINVAL;
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100750
751 switch (state->mixer->protocol) {
752 case UAC_VERSION_1:
753 case UAC_VERSION_2:
754 default:
Takashi Iwai0bfe5e42018-12-19 14:04:47 +0100755 if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1)
756 return 0; /* no bmControls -> skip */
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100757 mu_channels = uac_mixer_unit_bNrChannels(desc);
758 break;
759 case UAC_VERSION_3:
760 mu_channels = get_cluster_channels_v3(state,
761 uac3_mixer_unit_wClusterDescrID(desc));
762 break;
763 }
764
765 if (!mu_channels)
Takashi Iwai0bfe5e42018-12-19 14:04:47 +0100766 return 0;
767
768 c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
769 if (c - (void *)desc + (mu_channels - 1) / 8 >= desc->bLength)
770 return 0; /* no bmControls -> skip */
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100771
772 return mu_channels;
773}
774
775/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * parse the source unit recursively until it reaches to a terminal
777 * or a branched unit.
778 */
Hui Peng19bce472019-08-15 00:31:34 -0400779static int __check_input_term(struct mixer_build *state, int id,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200780 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200782 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200783 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100784 void *p1;
Hui Peng19bce472019-08-15 00:31:34 -0400785 unsigned char *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 memset(term, 0, sizeof(*term));
Hui Peng19bce472019-08-15 00:31:34 -0400788 for (;;) {
789 /* a loop in the terminal chain? */
790 if (test_and_set_bit(id, state->termbitmap))
791 return -EINVAL;
792
793 p1 = find_audio_control_unit(state, id);
794 if (!p1)
795 break;
796
797 hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200799
800 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
801 switch (hdr[2]) {
802 case UAC_INPUT_TERMINAL:
803 if (protocol == UAC_VERSION_1) {
804 struct uac_input_terminal_descriptor *d = p1;
805
806 term->type = le16_to_cpu(d->wTerminalType);
807 term->channels = d->bNrChannels;
808 term->chconfig = le16_to_cpu(d->wChannelConfig);
809 term->name = d->iTerminal;
810 } else { /* UAC_VERSION_2 */
811 struct uac2_input_terminal_descriptor *d = p1;
812
813 /* call recursively to verify that the
814 * referenced clock entity is valid */
Hui Peng19bce472019-08-15 00:31:34 -0400815 err = __check_input_term(state, d->bCSourceID, term);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200816 if (err < 0)
817 return err;
818
819 /* save input term properties after recursion,
820 * to ensure they are not overriden by the
821 * recursion calls */
822 term->id = id;
823 term->type = le16_to_cpu(d->wTerminalType);
824 term->channels = d->bNrChannels;
825 term->chconfig = le32_to_cpu(d->bmChannelConfig);
826 term->name = d->iTerminal;
827 }
828 return 0;
829 case UAC_FEATURE_UNIT: {
830 /* the header is the same for v1 and v2 */
831 struct uac_feature_unit_descriptor *d = p1;
832
833 id = d->bSourceID;
834 break; /* continue to parse */
835 }
836 case UAC_MIXER_UNIT: {
837 struct uac_mixer_unit_descriptor *d = p1;
838
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100839 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200840 term->channels = uac_mixer_unit_bNrChannels(d);
841 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
842 term->name = uac_mixer_unit_iMixer(d);
843 return 0;
844 }
845 case UAC_SELECTOR_UNIT:
846 case UAC2_CLOCK_SELECTOR: {
847 struct uac_selector_unit_descriptor *d = p1;
848 /* call recursively to retrieve the channel info */
Hui Peng19bce472019-08-15 00:31:34 -0400849 err = __check_input_term(state, d->baSourceID[0], term);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200850 if (err < 0)
851 return err;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100852 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200853 term->id = id;
854 term->name = uac_selector_unit_iSelector(d);
855 return 0;
856 }
857 case UAC1_PROCESSING_UNIT:
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100858 /* UAC2_EFFECT_UNIT */
859 if (protocol == UAC_VERSION_1)
860 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
861 else /* UAC_VERSION_2 */
862 term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */
Gustavo A. R. Silva91c6e152018-08-08 17:23:44 -0500863 /* fall through */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200864 case UAC1_EXTENSION_UNIT:
865 /* UAC2_PROCESSING_UNIT_V2 */
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100866 if (protocol == UAC_VERSION_1 && !term->type)
867 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
868 else if (protocol == UAC_VERSION_2 && !term->type)
869 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
Gustavo A. R. Silva91c6e152018-08-08 17:23:44 -0500870 /* fall through */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200871 case UAC2_EXTENSION_UNIT_V2: {
872 struct uac_processing_unit_descriptor *d = p1;
873
874 if (protocol == UAC_VERSION_2 &&
875 hdr[2] == UAC2_EFFECT_UNIT) {
876 /* UAC2/UAC1 unit IDs overlap here in an
877 * uncompatible way. Ignore this unit for now.
878 */
879 return 0;
880 }
881
882 if (d->bNrInPins) {
883 id = d->baSourceID[0];
884 break; /* continue to parse */
885 }
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100886 if (!term->type)
887 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
888
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200889 term->channels = uac_processing_unit_bNrChannels(d);
890 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
891 term->name = uac_processing_unit_iProcessing(d, protocol);
892 return 0;
893 }
894 case UAC2_CLOCK_SOURCE: {
895 struct uac_clock_source_descriptor *d = p1;
896
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100897 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200898 term->id = id;
899 term->name = d->iClockSource;
900 return 0;
901 }
902 default:
903 return -ENODEV;
904 }
905 } else { /* UAC_VERSION_3 */
906 switch (hdr[2]) {
907 case UAC_INPUT_TERMINAL: {
908 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200909
910 /* call recursively to verify that the
911 * referenced clock entity is valid */
Hui Peng19bce472019-08-15 00:31:34 -0400912 err = __check_input_term(state, d->bCSourceID, term);
Julian Scheel9430e542015-08-19 09:28:09 +0200913 if (err < 0)
914 return err;
915
916 /* save input term properties after recursion,
917 * to ensure they are not overriden by the
918 * recursion calls */
919 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100920 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100921
Jorge Sanjuan71066942018-05-14 12:03:42 +0100922 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
923 if (err < 0)
924 return err;
925 term->channels = err;
926
927 /* REVISIT: UAC3 IT doesn't have channels cfg */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200928 term->chconfig = 0;
929
930 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100931 return 0;
932 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200933 case UAC3_FEATURE_UNIT: {
934 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100935
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200936 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break; /* continue to parse */
938 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200939 case UAC3_CLOCK_SOURCE: {
940 struct uac3_clock_source_descriptor *d = p1;
941
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100942 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200943 term->id = id;
944 term->name = le16_to_cpu(d->wClockSourceStr);
945 return 0;
946 }
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100947 case UAC3_MIXER_UNIT: {
948 struct uac_mixer_unit_descriptor *d = p1;
949
950 err = uac_mixer_unit_get_channels(state, d);
Takashi Iwai0bfe5e42018-12-19 14:04:47 +0100951 if (err <= 0)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100952 return err;
953
954 term->channels = err;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100955 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100956
957 return 0;
958 }
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +0100959 case UAC3_SELECTOR_UNIT:
960 case UAC3_CLOCK_SELECTOR: {
961 struct uac_selector_unit_descriptor *d = p1;
962 /* call recursively to retrieve the channel info */
Hui Peng19bce472019-08-15 00:31:34 -0400963 err = __check_input_term(state, d->baSourceID[0], term);
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +0100964 if (err < 0)
965 return err;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100966 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +0100967 term->id = id;
968 term->name = 0; /* TODO: UAC3 Class-specific strings */
969
970 return 0;
971 }
Jorge Sanjuan0f292f02018-07-11 13:37:53 +0100972 case UAC3_PROCESSING_UNIT: {
973 struct uac_processing_unit_descriptor *d = p1;
974
975 if (!d->bNrInPins)
976 return -EINVAL;
977
978 /* call recursively to retrieve the channel info */
Hui Peng19bce472019-08-15 00:31:34 -0400979 err = __check_input_term(state, d->baSourceID[0], term);
Jorge Sanjuan0f292f02018-07-11 13:37:53 +0100980 if (err < 0)
981 return err;
982
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100983 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
Jorge Sanjuan0f292f02018-07-11 13:37:53 +0100984 term->id = id;
985 term->name = 0; /* TODO: UAC3 Class-specific strings */
986
987 return 0;
988 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200989 default:
990 return -ENODEV;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 }
994 return -ENODEV;
995}
996
Hui Peng19bce472019-08-15 00:31:34 -0400997
998static int check_input_term(struct mixer_build *state, int id,
999 struct usb_audio_term *term)
1000{
1001 memset(term, 0, sizeof(*term));
1002 memset(state->termbitmap, 0, sizeof(state->termbitmap));
1003 return __check_input_term(state, id, term);
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
1007 * Feature Unit
1008 */
1009
1010/* feature unit control information */
1011struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001012 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +02001014 int type; /* data type for uac1 */
1015 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016};
1017
1018static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001019 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
1020 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
1021 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
1022 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
1023 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
1024 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
1025 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
1026 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
1027 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
1028 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +02001029 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001030 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
1031 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
1032 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033};
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -06001036void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Jesper Juhl4d572772005-05-30 17:30:32 +02001038 kfree(kctl->private_data);
1039 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042/*
1043 * interface to ALSA control for feature/mixer units
1044 */
1045
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001046/* volume control quirks */
1047static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1048 struct snd_kcontrol *kctl)
1049{
Takashi Iwai3360b842014-11-18 11:47:04 +01001050 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001051 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +01001052 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001053 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +01001054 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1055 cval->min = 0x0000;
1056 cval->max = 0xffff;
1057 cval->res = 0x00e6;
1058 break;
1059 }
1060 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1061 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1062 cval->min = 0x00;
1063 cval->max = 0xff;
1064 break;
1065 }
1066 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1067 cval->min = 0xb706;
1068 cval->max = 0xff7b;
1069 cval->res = 0x0073;
1070 break;
1071 }
1072 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1073 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1074 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1075 cval->max = 0xfcfe;
1076 cval->res = 0x0073;
1077 }
1078 break;
1079
Felix Homannd34bf142012-04-23 20:24:27 +02001080 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1081 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1082 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001083 usb_audio_info(chip,
1084 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001085 cval->min = 0x0000;
1086 cval->max = 0x7f00;
1087 cval->res = 0x0100;
1088 break;
1089 }
1090 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1091 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001092 usb_audio_info(chip,
1093 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001094 cval->min = 0x00;
1095 cval->max = 0x7f;
1096 break;
1097 }
1098 break;
1099
Federico Cuello21493312018-05-09 00:13:38 +02001100 case USB_ID(0x0d8c, 0x0103):
1101 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1102 usb_audio_info(chip,
1103 "set volume quirk for CM102-A+/102S+\n");
1104 cval->min = -256;
1105 }
1106 break;
1107
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001108 case USB_ID(0x0471, 0x0101):
1109 case USB_ID(0x0471, 0x0104):
1110 case USB_ID(0x0471, 0x0105):
1111 case USB_ID(0x0672, 0x1041):
1112 /* quirk for UDA1321/N101.
1113 * note that detection between firmware 2.1.1.7 (N101)
1114 * and later 2.1.1.21 is not very clear from datasheets.
1115 * I hope that the min value is -15360 for newer firmware --jk
1116 */
1117 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1118 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001119 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001120 "set volume quirk for UDA1321/N101 chip\n");
1121 cval->max = -256;
1122 }
1123 break;
1124
1125 case USB_ID(0x046d, 0x09a4):
1126 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001127 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001128 "set volume quirk for QuickCam E3500\n");
1129 cval->min = 6080;
1130 cval->max = 8768;
1131 cval->res = 192;
1132 }
1133 break;
1134
Takashi Iwaie805ca82014-03-05 12:34:39 +01001135 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001136 case USB_ID(0x046d, 0x0808):
1137 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001138 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001139 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001140 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001141 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001142 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001143 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001144 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001145 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001146 /* Most audio usb devices lie about volume resolution.
1147 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001148 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001149 */
1150 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001151 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001152 "set resolution quirk: cval->res = 384\n");
1153 cval->res = 384;
1154 }
1155 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001156 }
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/*
1160 * retrieve the minimum and maximum values for the specified control
1161 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001162static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1163 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 /* for failsafe */
1166 cval->min = default_min;
1167 cval->max = cval->min + 1;
1168 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001169 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (cval->val_type == USB_MIXER_BOOLEAN ||
1172 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1173 cval->initialized = 1;
1174 } else {
1175 int minchn = 0;
1176 if (cval->cmask) {
1177 int i;
1178 for (i = 0; i < MAX_CHANNELS; i++)
1179 if (cval->cmask & (1 << i)) {
1180 minchn = i + 1;
1181 break;
1182 }
1183 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001184 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1185 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001186 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001187 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001188 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1189 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EINVAL;
1191 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001192 if (get_ctl_value(cval, UAC_GET_RES,
1193 (cval->control << 8) | minchn,
1194 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 cval->res = 1;
1196 } else {
1197 int last_valid_res = cval->res;
1198
1199 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001200 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001201 (cval->control << 8) | minchn,
1202 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
1204 cval->res /= 2;
1205 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001206 if (get_ctl_value(cval, UAC_GET_RES,
1207 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 cval->res = last_valid_res;
1209 }
1210 if (cval->res == 0)
1211 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001212
1213 /* Additional checks for the proper resolution
1214 *
1215 * Some devices report smaller resolutions than actually
1216 * reacting. They don't return errors but simply clip
1217 * to the lower aligned value.
1218 */
1219 if (cval->min + cval->res < cval->max) {
1220 int last_valid_res = cval->res;
1221 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001222 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001223 for (;;) {
1224 test = saved;
1225 if (test < cval->max)
1226 test += cval->res;
1227 else
1228 test -= cval->res;
1229 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001230 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001231 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001232 cval->res = last_valid_res;
1233 break;
1234 }
1235 if (test == check)
1236 break;
1237 cval->res *= 2;
1238 }
Chris J Argeseef90452014-11-12 12:07:01 -06001239 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001240 }
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 cval->initialized = 1;
1243 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001244
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001245 if (kctl)
1246 volume_control_quirks(cval, kctl);
1247
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001248 /* USB descriptions contain the dB scale in 1/256 dB unit
1249 * while ALSA TLV contains in 1/100 dB unit
1250 */
1251 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1252 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1253 if (cval->dBmin > cval->dBmax) {
1254 /* something is wrong; assume it's either from/to 0dB */
1255 if (cval->dBmin < 0)
1256 cval->dBmax = 0;
1257 else if (cval->dBmin > 0)
1258 cval->dBmin = 0;
1259 if (cval->dBmin > cval->dBmax) {
1260 /* totally crap, return an error */
1261 return -EINVAL;
1262 }
1263 }
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
1266}
1267
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001268#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001271static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1272 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001274 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (cval->val_type == USB_MIXER_BOOLEAN ||
1277 cval->val_type == USB_MIXER_INV_BOOLEAN)
1278 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1279 else
1280 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1281 uinfo->count = cval->channels;
1282 if (cval->val_type == USB_MIXER_BOOLEAN ||
1283 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1284 uinfo->value.integer.min = 0;
1285 uinfo->value.integer.max = 1;
1286 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001287 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001288 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001289 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1290 kcontrol->vd[0].access &=
1291 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1292 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001293 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001294 SNDRV_CTL_EVENT_MASK_INFO,
1295 &kcontrol->id);
1296 }
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001299 uinfo->value.integer.max =
1300 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 return 0;
1303}
1304
1305/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001306static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1307 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001309 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int c, cnt, val, err;
1311
Takashi Iwai641b4872009-01-15 17:05:24 +01001312 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (cval->cmask) {
1314 cnt = 0;
1315 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001316 if (!(cval->cmask & (1 << c)))
1317 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001318 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001319 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001320 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001321 val = get_relative_value(cval, val);
1322 ucontrol->value.integer.value[cnt] = val;
1323 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 } else {
1327 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001328 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001329 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001330 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 val = get_relative_value(cval, val);
1332 ucontrol->value.integer.value[0] = val;
1333 }
1334 return 0;
1335}
1336
1337/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001338static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1339 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001341 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int c, cnt, val, oval, err;
1343 int changed = 0;
1344
1345 if (cval->cmask) {
1346 cnt = 0;
1347 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001348 if (!(cval->cmask & (1 << c)))
1349 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001350 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001351 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001352 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001353 val = ucontrol->value.integer.value[cnt];
1354 val = get_abs_value(cval, val);
1355 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001356 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001357 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001359 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361 } else {
1362 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001363 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001365 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 val = ucontrol->value.integer.value[0];
1367 val = get_abs_value(cval, val);
1368 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001369 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 changed = 1;
1371 }
1372 }
1373 return changed;
1374}
1375
Andrew Chant568fa7e2018-03-23 19:25:24 -07001376/* get the boolean value from the master channel of a UAC control */
1377static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1378 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001379{
1380 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1381 int val, err;
1382
1383 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1384 if (err < 0)
1385 return filter_error(cval, err);
1386 val = (val != 0);
1387 ucontrol->value.integer.value[0] = val;
1388 return 0;
1389}
1390
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001391/* get the connectors status and report it as boolean type */
1392static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1393 struct snd_ctl_elem_value *ucontrol)
1394{
1395 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1396 struct snd_usb_audio *chip = cval->head.mixer->chip;
1397 int idx = 0, validx, ret, val;
1398
1399 validx = cval->control << 8 | 0;
1400
1401 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1402 if (ret)
1403 goto error;
1404
1405 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1406 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1407 struct uac2_connectors_ctl_blk uac2_conn;
1408
1409 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1410 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1411 validx, idx, &uac2_conn, sizeof(uac2_conn));
1412 val = !!uac2_conn.bNrChannels;
1413 } else { /* UAC_VERSION_3 */
1414 struct uac3_insertion_ctl_blk uac3_conn;
1415
1416 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1417 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1418 validx, idx, &uac3_conn, sizeof(uac3_conn));
1419 val = !!uac3_conn.bmConInserted;
1420 }
1421
1422 snd_usb_unlock_shutdown(chip);
1423
1424 if (ret < 0) {
1425error:
1426 usb_audio_err(chip,
1427 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1428 UAC_GET_CUR, validx, idx, cval->val_type);
1429 return ret;
1430 }
1431
1432 ucontrol->value.integer.value[0] = val;
1433 return 0;
1434}
1435
Takashi Iwai86e07d32005-11-17 15:08:02 +01001436static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1438 .name = "", /* will be filled later manually */
1439 .info = mixer_ctl_feature_info,
1440 .get = mixer_ctl_feature_get,
1441 .put = mixer_ctl_feature_put,
1442};
1443
Daniel Mack23caaf12010-03-11 21:13:25 +01001444/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301445static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1447 .name = "", /* will be filled later manually */
1448 .info = mixer_ctl_feature_info,
1449 .get = mixer_ctl_feature_get,
1450 .put = NULL,
1451};
1452
Andrew Chant568fa7e2018-03-23 19:25:24 -07001453/*
1454 * A control which shows the boolean value from reading a UAC control on
1455 * the master channel.
1456 */
1457static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001458 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1459 .name = "", /* will be filled later manually */
1460 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1461 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001462 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001463 .put = NULL,
1464};
1465
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001466static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1467 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1468 .name = "", /* will be filled later manually */
1469 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1470 .info = snd_ctl_boolean_mono_info,
1471 .get = mixer_ctl_connector_get,
1472 .put = NULL,
1473};
1474
Daniel Mack6bc170e2014-05-24 10:58:16 +02001475/*
1476 * This symbol is exported in order to allow the mixer quirks to
1477 * hook up to the standard feature unit control mechanism
1478 */
Daniel Mack9e386582011-05-25 09:09:01 +02001479struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481/*
1482 * build a feature control
1483 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001484static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1485{
1486 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1487}
1488
Daniel Mack6bc170e2014-05-24 10:58:16 +02001489/*
1490 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1491 * rename it to "Headphone". We determine if something is a headphone
1492 * similar to how udev determines form factor.
1493 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001494static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1495 struct snd_card *card)
1496{
1497 const char *names_to_check[] = {
1498 "Headset", "headset", "Headphone", "headphone", NULL};
1499 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001500 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001501
1502 if (strcmp("Speaker", kctl->id.name))
1503 return;
1504
1505 for (s = names_to_check; *s; s++)
1506 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001507 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001508 break;
1509 }
1510
1511 if (!found)
1512 return;
1513
1514 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1515}
1516
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001517static struct usb_feature_control_info *get_feature_control_info(int control)
1518{
1519 int i;
1520
1521 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1522 if (audio_feature_info[i].control == control)
1523 return &audio_feature_info[i];
1524 }
1525 return NULL;
1526}
1527
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001528static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1529 const struct usbmix_name_map *imap,
1530 unsigned int ctl_mask, int control,
1531 struct usb_audio_term *iterm,
1532 struct usb_audio_term *oterm,
1533 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Julian Scheelbc18e312015-08-14 16:14:45 +02001535 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unsigned int len = 0;
1537 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001538 struct snd_kcontrol *kctl;
1539 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001540 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001541 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Daniel Mack65f25da2010-05-31 13:35:41 +02001543 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* FIXME: not supported yet */
1545 return;
1546 }
1547
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001548 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001549 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return;
1551
Takashi Iwai561b2202005-09-09 14:22:34 +02001552 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001553 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001555 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 cval->control = control;
1557 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001558
1559 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001560 if (!ctl_info) {
1561 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001562 return;
Colin Ian King62376022018-03-27 15:30:01 +01001563 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001564 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001565 cval->val_type = ctl_info->type;
1566 else /* UAC_VERSION_2 */
1567 cval->val_type = ctl_info->type_uac2 >= 0 ?
1568 ctl_info->type_uac2 : ctl_info->type;
1569
Daniel Macka6a33252010-05-31 13:35:37 +02001570 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001572 cval->master_readonly = readonly_mask;
1573 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int i, c = 0;
1575 for (i = 0; i < 16; i++)
1576 if (ctl_mask & (1 << i))
1577 c++;
1578 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001579 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581
Daniel Mack6bc170e2014-05-24 10:58:16 +02001582 /*
1583 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001584 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001585 * issue write commands to read-only channels.
1586 */
Daniel Macka6a33252010-05-31 13:35:37 +02001587 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001588 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1589 else
1590 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1591
Daniel Mack6bc170e2014-05-24 10:58:16 +02001592 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001593 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 kfree(cval);
1595 return;
1596 }
Chris J Argeseef90452014-11-12 12:07:01 -06001597 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001599 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001601 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001602 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001603 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001606 case UAC_FU_MUTE:
1607 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001608 /*
1609 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * - if a name id is given in descriptor, use it.
1611 * - if the connected input can be determined, then use the name
1612 * of terminal type.
1613 * - if the connected output can be determined, use it.
1614 * - otherwise, anonymous name.
1615 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001616 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001617 if (iterm)
1618 len = get_term_name(mixer->chip, iterm,
1619 kctl->id.name,
1620 sizeof(kctl->id.name), 1);
1621 if (!len && oterm)
1622 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001623 kctl->id.name,
1624 sizeof(kctl->id.name), 1);
1625 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001626 snprintf(kctl->id.name, sizeof(kctl->id.name),
1627 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001629
1630 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001631 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001632
Daniel Mack6bc170e2014-05-24 10:58:16 +02001633 /*
1634 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * if the connected output is USB stream, then it's likely a
1636 * capture stream. otherwise it should be playback (hopefully :)
1637 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001638 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1639 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001640 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001641 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001642 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001644 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001645 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001648 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1650 sizeof(kctl->id.name));
1651 break;
1652 }
1653
Takashi Iwaie1825342012-05-14 17:11:06 +02001654 /* get min/max values */
1655 get_min_max_with_quirks(cval, 0, kctl);
1656
1657 if (control == UAC_FU_VOLUME) {
1658 check_mapped_dB(map, cval);
1659 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1660 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1661 kctl->vd[0].access |=
1662 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1663 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1664 }
1665 }
1666
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001667 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001668
Alexey Fisher80aceff2011-03-10 14:53:38 +01001669 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001670 /*
1671 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001672 * to be sure. 384 is a resolution magic number found on Logitech
1673 * devices. It will definitively catch all buggy Logitech devices.
1674 */
1675 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001676 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001677 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001678 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001679 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001680 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001681 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001682 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001683 }
1684
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001685 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001686 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001687 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001688 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001691static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1692 unsigned int ctl_mask, int control,
1693 struct usb_audio_term *iterm, int unitid,
1694 int readonly_mask)
1695{
1696 struct uac_feature_unit_descriptor *desc = raw_desc;
1697 int nameid = uac_feature_unit_iFeature(desc);
1698
1699 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1700 iterm, &state->oterm, unitid, nameid, readonly_mask);
1701}
1702
1703static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1704 unsigned int ctl_mask, int control, int unitid,
1705 const struct usbmix_name_map *badd_map)
1706{
1707 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1708 NULL, NULL, unitid, 0, 0);
1709}
1710
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001711static void get_connector_control_name(struct usb_mixer_interface *mixer,
Andrew Chant5a222e82018-03-23 19:25:23 -07001712 struct usb_audio_term *term,
1713 bool is_input, char *name, int name_size)
1714{
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001715 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001716
1717 if (name_len == 0)
1718 strlcpy(name, "Unknown", name_size);
1719
1720 /*
1721 * sound/core/ctljack.c has a convention of naming jack controls
1722 * by ending in " Jack". Make it slightly more useful by
1723 * indicating Input or Output after the terminal name.
1724 */
1725 if (is_input)
1726 strlcat(name, " - Input Jack", name_size);
1727 else
1728 strlcat(name, " - Output Jack", name_size);
1729}
1730
1731/* Build a mixer control for a UAC connector control (jack-detect) */
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001732static void build_connector_control(struct usb_mixer_interface *mixer,
Andrew Chant5a222e82018-03-23 19:25:23 -07001733 struct usb_audio_term *term, bool is_input)
1734{
1735 struct snd_kcontrol *kctl;
1736 struct usb_mixer_elem_info *cval;
1737
1738 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1739 if (!cval)
1740 return;
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001741 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001742 /*
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001743 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1744 * number of channels connected.
1745 *
1746 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1747 * following byte(s) specifies which connectors are inserted.
1748 *
1749 * This boolean ctl will simply report if any channels are connected
1750 * or not.
Andrew Chant568fa7e2018-03-23 19:25:24 -07001751 */
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001752 if (mixer->protocol == UAC_VERSION_2)
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001753 cval->control = UAC2_TE_CONNECTOR;
1754 else /* UAC_VERSION_3 */
1755 cval->control = UAC3_TE_INSERTION;
1756
Andrew Chant5a222e82018-03-23 19:25:23 -07001757 cval->val_type = USB_MIXER_BOOLEAN;
1758 cval->channels = 1; /* report true if any channel is connected */
1759 cval->min = 0;
1760 cval->max = 1;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001761 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001762 if (!kctl) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001763 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Andrew Chant5a222e82018-03-23 19:25:23 -07001764 kfree(cval);
1765 return;
1766 }
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001767 get_connector_control_name(mixer, term, is_input, kctl->id.name,
Andrew Chant5a222e82018-03-23 19:25:23 -07001768 sizeof(kctl->id.name));
1769 kctl->private_free = snd_usb_mixer_elem_free;
1770 snd_usb_mixer_add_control(&cval->head, kctl);
1771}
1772
Daniel Mackcddaafb2016-04-09 11:21:46 +02001773static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1774 void *_ftr)
1775{
1776 struct uac_clock_source_descriptor *hdr = _ftr;
1777 struct usb_mixer_elem_info *cval;
1778 struct snd_kcontrol *kctl;
1779 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1780 int ret;
1781
1782 if (state->mixer->protocol != UAC_VERSION_2)
1783 return -EINVAL;
1784
1785 if (hdr->bLength != sizeof(*hdr)) {
1786 usb_audio_dbg(state->chip,
1787 "Bogus clock source descriptor length of %d, ignoring.\n",
1788 hdr->bLength);
1789 return 0;
1790 }
1791
1792 /*
1793 * The only property of this unit we are interested in is the
1794 * clock source validity. If that isn't readable, just bail out.
1795 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001796 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001797 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001798 return 0;
1799
1800 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1801 if (!cval)
1802 return -ENOMEM;
1803
1804 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1805
1806 cval->min = 0;
1807 cval->max = 1;
1808 cval->channels = 1;
1809 cval->val_type = USB_MIXER_BOOLEAN;
1810 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1811
Andrew Chant568fa7e2018-03-23 19:25:24 -07001812 cval->master_readonly = 1;
1813 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1814 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001815
1816 if (!kctl) {
1817 kfree(cval);
1818 return -ENOMEM;
1819 }
1820
1821 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001822 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001823 name, sizeof(name));
1824 if (ret > 0)
1825 snprintf(kctl->id.name, sizeof(kctl->id.name),
1826 "%s Validity", name);
1827 else
1828 snprintf(kctl->id.name, sizeof(kctl->id.name),
1829 "Clock Source %d Validity", hdr->bClockID);
1830
1831 return snd_usb_mixer_add_control(&cval->head, kctl);
1832}
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/*
1835 * parse a feature unit
1836 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001837 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001839static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1840 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001843 struct usb_audio_term iterm;
YueHaibing36c346e2019-01-09 02:20:16 +00001844 unsigned int master_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001846 struct uac_feature_unit_descriptor *hdr = _ftr;
1847 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Daniel Mack23caaf12010-03-11 21:13:25 +01001849 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001850 if (hdr->bLength < 7) {
1851 usb_audio_err(state->chip,
1852 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1853 unitid);
1854 return -EINVAL;
1855 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001856 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001857 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001858 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001859 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001860 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001861 return -EINVAL;
1862 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001863 channels = (hdr->bLength - 7) / csize - 1;
1864 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001865 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001866 usb_audio_err(state->chip,
1867 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1868 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001869 return -EINVAL;
1870 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001871 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001872 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001873 if (hdr->bLength < 6) {
1874 usb_audio_err(state->chip,
1875 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1876 unitid);
1877 return -EINVAL;
1878 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001879 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001880 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001881 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001882 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001883 usb_audio_err(state->chip,
1884 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1885 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001886 return -EINVAL;
1887 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001888 } else { /* UAC_VERSION_3 */
1889 struct uac3_feature_unit_descriptor *ftr = _ftr;
1890
1891 if (hdr->bLength < 7) {
1892 usb_audio_err(state->chip,
1893 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1894 unitid);
1895 return -EINVAL;
1896 }
1897 csize = 4;
1898 channels = (ftr->bLength - 7) / 4 - 1;
1899 bmaControls = ftr->bmaControls;
1900 if (hdr->bLength < 7 + csize) {
1901 usb_audio_err(state->chip,
1902 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1903 unitid);
1904 return -EINVAL;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
1908 /* parse the source unit */
Takashi Iwaif25ecf82018-05-27 15:18:22 +02001909 err = parse_audio_unit(state, hdr->bSourceID);
1910 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return err;
1912
1913 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001914 err = check_input_term(state, hdr->bSourceID, &iterm);
1915 if (err < 0)
1916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Daniel Mack23caaf12010-03-11 21:13:25 +01001918 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001919 /* master configuration quirks */
1920 switch (state->chip->usb_id) {
1921 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001922 usb_audio_info(state->chip,
1923 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001924 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001925 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001926 break;
David Henningssonc1051432012-09-20 10:20:41 +02001927 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001928 usb_audio_info(state->chip,
1929 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001930 /* disable non-functional volume control */
1931 channels = 0;
1932 break;
1933
Javier Kohen0c3cee52009-11-17 15:36:13 +01001934 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001935
1936 if (state->mixer->protocol == UAC_VERSION_1) {
1937 /* check all control types */
1938 for (i = 0; i < 10; i++) {
1939 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001940 int control = audio_feature_info[i].control;
1941
Daniel Mack23caaf12010-03-11 21:13:25 +01001942 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001943 unsigned int mask;
1944
1945 mask = snd_usb_combine_bytes(bmaControls +
1946 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001947 if (mask & (1 << i))
1948 ch_bits |= (1 << j);
1949 }
1950 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001951
1952 /*
1953 * The first channel must be set
1954 * (for ease of programming).
1955 */
1956 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001957 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001958 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001959 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001960 build_feature_ctl(state, _ftr, 0, control,
1961 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001963 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001964 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001965 unsigned int ch_bits = 0;
1966 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001967 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001968
1969 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001970 unsigned int mask;
1971
1972 mask = snd_usb_combine_bytes(bmaControls +
1973 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001974 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001975 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001976 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001977 ch_read_only |= (1 << j);
1978 }
1979 }
1980
Daniel Mack6bc170e2014-05-24 10:58:16 +02001981 /*
1982 * NOTE: build_feature_ctl() will mark the control
1983 * read-only if all channels are marked read-only in
1984 * the descriptors. Otherwise, the control will be
1985 * reported as writeable, but the driver will not
1986 * actually issue a write command for read-only
1987 * channels.
1988 */
1989
1990 /*
1991 * The first channel must be set
1992 * (for ease of programming).
1993 */
1994 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001995 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001996 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001997 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001998 build_feature_ctl(state, _ftr, 0, control,
1999 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07002000 !uac_v2v3_control_is_writeable(master_bits,
2001 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004
2005 return 0;
2006}
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008/*
2009 * Mixer Unit
2010 */
2011
2012/*
2013 * build a mixer unit control
2014 *
2015 * the callbacks are identical with feature unit.
2016 * input channel number (zero based) is given in control field instead.
2017 */
Daniel Mack99fc8642010-03-11 21:13:24 +01002018static void build_mixer_unit_ctl(struct mixer_build *state,
2019 struct uac_mixer_unit_descriptor *desc,
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002020 int in_pin, int in_ch, int num_outs,
2021 int unitid, struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002023 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002025 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002026 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002028 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002029 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return;
2031
Takashi Iwai561b2202005-09-09 14:22:34 +02002032 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002033 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return;
2035
Takashi Iwai3360b842014-11-18 11:47:04 +01002036 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 cval->control = in_ch + 1; /* based on 1 */
2038 cval->val_type = USB_MIXER_S16;
2039 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002040 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2041
2042 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 cval->cmask |= (1 << i);
2044 cval->channels++;
2045 }
2046 }
2047
2048 /* get min/max values */
2049 get_min_max(cval, 0);
2050
2051 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002052 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002053 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 kfree(cval);
2055 return;
2056 }
Chris J Argeseef90452014-11-12 12:07:01 -06002057 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002059 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002060 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002061 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002062 sizeof(kctl->id.name), 0);
2063 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02002065 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002067 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002068 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2069 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Andrew Chant5a222e82018-03-23 19:25:23 -07002072static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2073 void *raw_desc)
2074{
2075 struct usb_audio_term iterm;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002076 unsigned int control, bmctls, term_id;
Andrew Chant5a222e82018-03-23 19:25:23 -07002077
Andrew Chant5a222e82018-03-23 19:25:23 -07002078 if (state->mixer->protocol == UAC_VERSION_2) {
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002079 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
Takashi Iwai3e96d722019-01-02 17:12:21 +01002080 if (d_v2->bLength < sizeof(*d_v2))
2081 return -EINVAL;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002082 control = UAC2_TE_CONNECTOR;
2083 term_id = d_v2->bTerminalID;
2084 bmctls = le16_to_cpu(d_v2->bmControls);
2085 } else if (state->mixer->protocol == UAC_VERSION_3) {
2086 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
Takashi Iwai3e96d722019-01-02 17:12:21 +01002087 if (d_v3->bLength < sizeof(*d_v3))
2088 return -EINVAL;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002089 control = UAC3_TE_INSERTION;
2090 term_id = d_v3->bTerminalID;
2091 bmctls = le32_to_cpu(d_v3->bmControls);
2092 } else {
2093 return 0; /* UAC1. No Insertion control */
Andrew Chant5a222e82018-03-23 19:25:23 -07002094 }
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002095
2096 check_input_term(state, term_id, &iterm);
2097
2098 /* Check for jack detection. */
2099 if (uac_v2v3_control_is_readable(bmctls, control))
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01002100 build_connector_control(state->mixer, &iterm, true);
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002101
Andrew Chant5a222e82018-03-23 19:25:23 -07002102 return 0;
2103}
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/*
2106 * parse a mixer unit
2107 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002108static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2109 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Daniel Mack99fc8642010-03-11 21:13:24 +01002111 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002112 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int input_pins, num_ins, num_outs;
2114 int pin, ich, err;
2115
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002116 err = uac_mixer_unit_get_channels(state, desc);
2117 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002118 usb_audio_err(state->chip,
2119 "invalid MIXER UNIT descriptor %d\n",
2120 unitid);
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002124 num_outs = err;
2125 input_pins = desc->bNrInPins;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 num_ins = 0;
2128 ich = 0;
2129 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002130 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002132 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002133 /* no bmControls field (e.g. Maya44) -> ignore */
Takashi Iwai0bfe5e42018-12-19 14:04:47 +01002134 if (!num_outs)
Clemens Ladischea114fc2015-06-03 11:36:51 +02002135 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002136 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (err < 0)
2138 return err;
2139 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002140 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 int och, ich_has_controls = 0;
2142
Daniel Mack6bc170e2014-05-24 10:58:16 +02002143 for (och = 0; och < num_outs; och++) {
2144 __u8 *c = uac_mixer_unit_bmControls(desc,
2145 state->mixer->protocol);
2146
2147 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 ich_has_controls = 1;
2149 break;
2150 }
2151 }
2152 if (ich_has_controls)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002153 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 unitid, &iterm);
2155 }
2156 }
2157 return 0;
2158}
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160/*
2161 * Processing Unit / Extension Unit
2162 */
2163
2164/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002165static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2166 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002168 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int err, val;
2170
2171 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002172 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002174 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 val = get_relative_value(cval, val);
2177 ucontrol->value.integer.value[0] = val;
2178 return 0;
2179}
2180
2181/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002182static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002185 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int val, oval, err;
2187
2188 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002189 if (err < 0)
2190 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 val = ucontrol->value.integer.value[0];
2192 val = get_abs_value(cval, val);
2193 if (val != oval) {
2194 set_cur_ctl_value(cval, cval->control << 8, val);
2195 return 1;
2196 }
2197 return 0;
2198}
2199
2200/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302201static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2203 .name = "", /* will be filled later */
2204 .info = mixer_ctl_feature_info,
2205 .get = mixer_ctl_procunit_get,
2206 .put = mixer_ctl_procunit_put,
2207};
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/*
2210 * predefined data for processing units
2211 */
2212struct procunit_value_info {
2213 int control;
2214 char *suffix;
2215 int val_type;
2216 int min_value;
2217};
2218
2219struct procunit_info {
2220 int type;
2221 char *name;
2222 struct procunit_value_info *values;
2223};
2224
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002225static struct procunit_value_info undefined_proc_info[] = {
2226 { 0x00, "Control Undefined", 0 },
2227 { 0 }
2228};
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002231 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2232 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 { 0 }
2234};
2235static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002236 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2237 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 { 0 }
2239};
2240static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002241 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2242 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 { 0 }
2244};
2245static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002246 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2247 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2248 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2249 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 { 0 }
2251};
2252static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002253 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2254 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2255 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2256 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 { 0 }
2258};
2259static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002260 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2261 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2262 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2263 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2264 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2265 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 { 0 }
2267};
2268
2269static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002270 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2271 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2272 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2273 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2274 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2275 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 { 0 },
2277};
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002278
2279static struct procunit_value_info uac3_updown_proc_info[] = {
2280 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2281 { 0 }
2282};
2283static struct procunit_value_info uac3_stereo_ext_proc_info[] = {
2284 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 },
2285 { 0 }
2286};
2287
2288static struct procunit_info uac3_procunits[] = {
2289 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info },
2290 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info },
2291 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info },
2292 { 0 },
2293};
2294
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002295/*
2296 * predefined data for extension units
2297 */
2298static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002299 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2300 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002301};
2302static struct procunit_value_info clock_source_xu_info[] = {
2303 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2304 { 0 }
2305};
2306static struct procunit_value_info spdif_format_xu_info[] = {
2307 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2308 { 0 }
2309};
2310static struct procunit_value_info soft_limit_xu_info[] = {
2311 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2312 { 0 }
2313};
2314static struct procunit_info extunits[] = {
2315 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2316 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2317 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2318 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2319 { 0 }
2320};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/*
2323 * build a processing/extension unit
2324 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002325static int build_audio_procunit(struct mixer_build *state, int unitid,
2326 void *raw_desc, struct procunit_info *list,
Takashi Iwaica95c7b2019-07-04 16:31:12 +02002327 bool extension_unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Daniel Mack99fc8642010-03-11 21:13:24 +01002329 struct uac_processing_unit_descriptor *desc = raw_desc;
Takashi Iwaif4351a12018-12-19 12:36:27 +01002330 int num_ins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002331 struct usb_mixer_elem_info *cval;
2332 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int i, err, nameid, type, len;
2334 struct procunit_info *info;
2335 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002336 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 static struct procunit_value_info default_value_info[] = {
2338 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2339 { 0 }
2340 };
2341 static struct procunit_info default_info = {
2342 0, NULL, default_value_info
2343 };
Takashi Iwaica95c7b2019-07-04 16:31:12 +02002344 const char *name = extension_unit ?
2345 "Extension Unit" : "Processing Unit";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Takashi Iwaif4351a12018-12-19 12:36:27 +01002347 if (desc->bLength < 13) {
2348 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2349 return -EINVAL;
2350 }
2351
2352 num_ins = desc->bNrInPins;
2353 if (desc->bLength < 13 + num_ins ||
Daniel Mack23caaf12010-03-11 21:13:25 +01002354 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002355 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return -EINVAL;
2357 }
2358
2359 for (i = 0; i < num_ins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002360 err = parse_audio_unit(state, desc->baSourceID[i]);
2361 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return err;
2363 }
2364
Daniel Mack99fc8642010-03-11 21:13:24 +01002365 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 for (info = list; info && info->type; info++)
2367 if (info->type == type)
2368 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002369 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 info = &default_info;
2371
2372 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002373 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002374
Jorge Sanjuan4e887af2018-07-11 13:37:52 +01002375 if (state->mixer->protocol == UAC_VERSION_1) {
2376 if (!(controls[valinfo->control / 8] &
2377 (1 << ((valinfo->control % 8) - 1))))
2378 continue;
2379 } else { /* UAC_VERSION_2/3 */
2380 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8],
2381 valinfo->control))
2382 continue;
2383 }
2384
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002385 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002386 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002388 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002389 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002391 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 cval->control = valinfo->control;
2393 cval->val_type = valinfo->val_type;
2394 cval->channels = 1;
2395
Jorge Sanjuan4e887af2018-07-11 13:37:52 +01002396 if (state->mixer->protocol > UAC_VERSION_1 &&
2397 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8],
2398 valinfo->control))
2399 cval->master_readonly = 1;
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* get min/max values */
Jorge Sanjuan55b8cb42018-07-11 13:37:55 +01002402 switch (type) {
2403 case UAC_PROCESS_UP_DOWNMIX: {
2404 bool mode_sel = false;
2405
2406 switch (state->mixer->protocol) {
2407 case UAC_VERSION_1:
2408 case UAC_VERSION_2:
2409 default:
2410 if (cval->control == UAC_UD_MODE_SELECT)
2411 mode_sel = true;
2412 break;
2413 case UAC_VERSION_3:
2414 if (cval->control == UAC3_UD_MODE_SELECT)
2415 mode_sel = true;
2416 break;
2417 }
2418
2419 if (mode_sel) {
2420 __u8 *control_spec = uac_processing_unit_specific(desc,
2421 state->mixer->protocol);
2422 cval->min = 1;
2423 cval->max = control_spec[0];
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002424 cval->res = 1;
2425 cval->initialized = 1;
Jorge Sanjuan55b8cb42018-07-11 13:37:55 +01002426 break;
2427 }
2428
2429 get_min_max(cval, valinfo->min_value);
2430 break;
2431 }
2432 case USB_XU_CLOCK_RATE:
2433 /*
2434 * E-Mu USB 0404/0202/TrackerPre/0204
2435 * samplerate control quirk
2436 */
2437 cval->min = 0;
2438 cval->max = 5;
2439 cval->res = 1;
2440 cval->initialized = 1;
2441 break;
2442 default:
2443 get_min_max(cval, valinfo->min_value);
2444 break;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002448 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 kfree(cval);
2450 return -ENOMEM;
2451 }
Chris J Argeseef90452014-11-12 12:07:01 -06002452 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Daniel Mack6bc170e2014-05-24 10:58:16 +02002454 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002455 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002456 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002458 } else {
Takashi Iwaica95c7b2019-07-04 16:31:12 +02002459 if (extension_unit)
2460 nameid = uac_extension_unit_iExtension(desc, state->mixer->protocol);
2461 else
2462 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 len = 0;
2464 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002465 len = snd_usb_copy_string_desc(state->chip,
2466 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002467 kctl->id.name,
2468 sizeof(kctl->id.name));
2469 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2471 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002472 append_ctl_name(kctl, " ");
2473 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002475 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002476 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002477 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002478 cval->min, cval->max);
2479
Takashi Iwai3360b842014-11-18 11:47:04 +01002480 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002481 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return err;
2483 }
2484 return 0;
2485}
2486
Daniel Mack6bc170e2014-05-24 10:58:16 +02002487static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2488 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002490 switch (state->mixer->protocol) {
2491 case UAC_VERSION_1:
2492 case UAC_VERSION_2:
2493 default:
2494 return build_audio_procunit(state, unitid, raw_desc,
Takashi Iwaica95c7b2019-07-04 16:31:12 +02002495 procunits, false);
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002496 case UAC_VERSION_3:
2497 return build_audio_procunit(state, unitid, raw_desc,
Takashi Iwaica95c7b2019-07-04 16:31:12 +02002498 uac3_procunits, false);
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Daniel Mack6bc170e2014-05-24 10:58:16 +02002502static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2503 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002505 /*
2506 * Note that we parse extension units with processing unit descriptors.
2507 * That's ok as the layout is the same.
2508 */
Takashi Iwaica95c7b2019-07-04 16:31:12 +02002509 return build_audio_procunit(state, unitid, raw_desc, extunits, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512/*
2513 * Selector Unit
2514 */
2515
Daniel Mack6bc170e2014-05-24 10:58:16 +02002516/*
2517 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 * use an enumerator type for routing
2519 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002520static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002523 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002524 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Takashi Iwai5e246b82008-08-08 17:12:47 +02002526 if (snd_BUG_ON(!itemlist))
2527 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002528 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002532static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2533 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002535 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 int val, err;
2537
Daniel Mack09414202010-05-31 13:35:44 +02002538 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002540 ucontrol->value.enumerated.item[0] = 0;
2541 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543 val = get_relative_value(cval, val);
2544 ucontrol->value.enumerated.item[0] = val;
2545 return 0;
2546}
2547
2548/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002549static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002552 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 int val, oval, err;
2554
Daniel Mack09414202010-05-31 13:35:44 +02002555 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002556 if (err < 0)
2557 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 val = ucontrol->value.enumerated.item[0];
2559 val = get_abs_value(cval, val);
2560 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002561 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 1;
2563 }
2564 return 0;
2565}
2566
2567/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302568static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2570 .name = "", /* will be filled later */
2571 .info = mixer_ctl_selector_info,
2572 .get = mixer_ctl_selector_get,
2573 .put = mixer_ctl_selector_put,
2574};
2575
Daniel Mack6bc170e2014-05-24 10:58:16 +02002576/*
2577 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 * free both private_data and private_value
2579 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002580static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 int i, num_ins = 0;
2583
2584 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002585 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 num_ins = cval->max;
2587 kfree(cval);
2588 kctl->private_data = NULL;
2589 }
2590 if (kctl->private_value) {
2591 char **itemlist = (char **)kctl->private_value;
2592 for (i = 0; i < num_ins; i++)
2593 kfree(itemlist[i]);
2594 kfree(itemlist);
2595 kctl->private_value = 0;
2596 }
2597}
2598
2599/*
2600 * parse a selector unit
2601 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002602static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2603 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Daniel Mack99fc8642010-03-11 21:13:24 +01002605 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 unsigned int i, nameid, len;
2607 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002608 struct usb_mixer_elem_info *cval;
2609 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002610 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 char **namelist;
2612
Takashi Iwaif658f172017-11-21 17:00:32 +01002613 if (desc->bLength < 5 || !desc->bNrInPins ||
2614 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002615 usb_audio_err(state->chip,
2616 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return -EINVAL;
2618 }
2619
Daniel Mack99fc8642010-03-11 21:13:24 +01002620 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002621 err = parse_audio_unit(state, desc->baSourceID[i]);
2622 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return err;
2624 }
2625
Daniel Mack99fc8642010-03-11 21:13:24 +01002626 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return 0;
2628
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002629 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002630 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return 0;
2632
Takashi Iwai561b2202005-09-09 14:22:34 +02002633 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002634 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002636 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 cval->val_type = USB_MIXER_U8;
2638 cval->channels = 1;
2639 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002640 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 cval->res = 1;
2642 cval->initialized = 1;
2643
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002644 switch (state->mixer->protocol) {
2645 case UAC_VERSION_1:
2646 default:
Daniel Mack09414202010-05-31 13:35:44 +02002647 cval->control = 0;
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002648 break;
2649 case UAC_VERSION_2:
2650 case UAC_VERSION_3:
2651 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2652 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2653 cval->control = UAC2_CX_CLOCK_SELECTOR;
2654 else /* UAC2/3_SELECTOR_UNIT */
2655 cval->control = UAC2_SU_SELECTOR;
2656 break;
2657 }
Daniel Mack09414202010-05-31 13:35:44 +02002658
Kees Cook6da2ec52018-06-12 13:55:00 -07002659 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002660 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 kfree(cval);
2662 return -ENOMEM;
2663 }
2664#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002665 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002666 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 len = 0;
2668 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002669 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002670 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 kfree(namelist[i]);
2672 kfree(namelist);
2673 kfree(cval);
2674 return -ENOMEM;
2675 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002676 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2677 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002678 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002679 len = get_term_name(state->chip, &iterm, namelist[i],
2680 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002682 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684
2685 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2686 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002687 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Wenwen Wangcb517352019-04-27 01:06:46 -05002688 for (i = 0; i < desc->bNrInPins; i++)
2689 kfree(namelist[i]);
Jesper Juhl878b4782006-03-20 11:27:13 +01002690 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 kfree(cval);
2692 return -ENOMEM;
2693 }
2694 kctl->private_value = (unsigned long)namelist;
2695 kctl->private_free = usb_mixer_selector_elem_free;
2696
Takashi Iwai5a15f282017-12-18 23:36:57 +01002697 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002698 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002699 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002700 /* no mapping ? */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002701 switch (state->mixer->protocol) {
2702 case UAC_VERSION_1:
2703 case UAC_VERSION_2:
2704 default:
Takashi Iwai5a15f282017-12-18 23:36:57 +01002705 /* if iSelector is given, use it */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002706 nameid = uac_selector_unit_iSelector(desc);
2707 if (nameid)
2708 len = snd_usb_copy_string_desc(state->chip,
2709 nameid, kctl->id.name,
2710 sizeof(kctl->id.name));
2711 break;
2712 case UAC_VERSION_3:
2713 /* TODO: Class-Specific strings not yet supported */
2714 break;
2715 }
2716
Takashi Iwai5a15f282017-12-18 23:36:57 +01002717 /* ... or pick up the terminal name at next */
2718 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002719 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002721 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002722 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2724
Takashi Iwai5a15f282017-12-18 23:36:57 +01002725 /* and add the proper suffix */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002726 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2727 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
Daniel Mack09414202010-05-31 13:35:44 +02002728 append_ctl_name(kctl, " Clock Source");
2729 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002730 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002732 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002735 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002736 cval->head.id, kctl->id.name, desc->bNrInPins);
2737 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740/*
2741 * parse an audio unit recursively
2742 */
2743
Takashi Iwai86e07d32005-11-17 15:08:02 +01002744static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002747 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 if (test_and_set_bit(unitid, state->unitbitmap))
2750 return 0; /* the unit already visited */
2751
2752 p1 = find_audio_control_unit(state, unitid);
2753 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002754 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return -EINVAL;
2756 }
2757
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002758 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2759 switch (p1[2]) {
2760 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002761 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002762 case UAC_MIXER_UNIT:
2763 return parse_audio_mixer_unit(state, unitid, p1);
2764 case UAC2_CLOCK_SOURCE:
2765 return parse_clock_source_unit(state, unitid, p1);
2766 case UAC_SELECTOR_UNIT:
2767 case UAC2_CLOCK_SELECTOR:
2768 return parse_audio_selector_unit(state, unitid, p1);
2769 case UAC_FEATURE_UNIT:
2770 return parse_audio_feature_unit(state, unitid, p1);
2771 case UAC1_PROCESSING_UNIT:
2772 /* UAC2_EFFECT_UNIT has the same value */
2773 if (protocol == UAC_VERSION_1)
2774 return parse_audio_processing_unit(state, unitid, p1);
2775 else
2776 return 0; /* FIXME - effect units not implemented yet */
2777 case UAC1_EXTENSION_UNIT:
2778 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2779 if (protocol == UAC_VERSION_1)
2780 return parse_audio_extension_unit(state, unitid, p1);
2781 else /* UAC_VERSION_2 */
2782 return parse_audio_processing_unit(state, unitid, p1);
2783 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002784 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002785 default:
2786 usb_audio_err(state->chip,
2787 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2788 return -EINVAL;
2789 }
2790 } else { /* UAC_VERSION_3 */
2791 switch (p1[2]) {
2792 case UAC_INPUT_TERMINAL:
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002793 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002794 case UAC3_MIXER_UNIT:
2795 return parse_audio_mixer_unit(state, unitid, p1);
2796 case UAC3_CLOCK_SOURCE:
2797 return parse_clock_source_unit(state, unitid, p1);
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002798 case UAC3_SELECTOR_UNIT:
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002799 case UAC3_CLOCK_SELECTOR:
2800 return parse_audio_selector_unit(state, unitid, p1);
2801 case UAC3_FEATURE_UNIT:
2802 return parse_audio_feature_unit(state, unitid, p1);
2803 case UAC3_EFFECT_UNIT:
2804 return 0; /* FIXME - effect units not implemented yet */
2805 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002806 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002807 case UAC3_EXTENSION_UNIT:
2808 return parse_audio_extension_unit(state, unitid, p1);
2809 default:
2810 usb_audio_err(state->chip,
2811 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2812 return -EINVAL;
2813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815}
2816
Clemens Ladisch84957a82005-04-29 16:23:13 +02002817static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2818{
Takashi Iwai124751d2017-10-10 14:10:32 +02002819 /* kill pending URBs */
2820 snd_usb_mixer_disconnect(mixer);
2821
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002822 kfree(mixer->id_elems);
2823 if (mixer->urb) {
2824 kfree(mixer->urb->transfer_buffer);
2825 usb_free_urb(mixer->urb);
2826 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002827 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002828 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002829 kfree(mixer);
2830}
2831
Takashi Iwai86e07d32005-11-17 15:08:02 +01002832static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002833{
2834 struct usb_mixer_interface *mixer = device->device_data;
2835 snd_usb_mixer_free(mixer);
2836 return 0;
2837}
2838
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002839/* UAC3 predefined channels configuration */
2840struct uac3_badd_profile {
2841 int subclass;
2842 const char *name;
2843 int c_chmask; /* capture channels mask */
2844 int p_chmask; /* playback channels mask */
2845 int st_chmask; /* side tone mixing channel mask */
2846};
2847
2848static struct uac3_badd_profile uac3_badd_profiles[] = {
2849 {
2850 /*
2851 * BAIF, BAOF or combination of both
2852 * IN: Mono or Stereo cfg, Mono alt possible
2853 * OUT: Mono or Stereo cfg, Mono alt possible
2854 */
2855 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2856 .name = "GENERIC IO",
2857 .c_chmask = -1, /* dynamic channels */
2858 .p_chmask = -1, /* dynamic channels */
2859 },
2860 {
2861 /* BAOF; Stereo only cfg, Mono alt possible */
2862 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2863 .name = "HEADPHONE",
2864 .p_chmask = 3,
2865 },
2866 {
2867 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2868 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2869 .name = "SPEAKER",
2870 .p_chmask = -1, /* dynamic channels */
2871 },
2872 {
2873 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2874 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2875 .name = "MICROPHONE",
2876 .c_chmask = -1, /* dynamic channels */
2877 },
2878 {
2879 /*
2880 * BAIOF topology
2881 * IN: Mono only
2882 * OUT: Mono or Stereo cfg, Mono alt possible
2883 */
2884 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2885 .name = "HEADSET",
2886 .c_chmask = 1,
2887 .p_chmask = -1, /* dynamic channels */
2888 .st_chmask = 1,
2889 },
2890 {
2891 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2892 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2893 .name = "HEADSET ADAPTER",
2894 .c_chmask = 1,
2895 .p_chmask = 3,
2896 .st_chmask = 1,
2897 },
2898 {
2899 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2900 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2901 .name = "SPEAKERPHONE",
2902 .c_chmask = 1,
2903 .p_chmask = 1,
2904 },
2905 { 0 } /* terminator */
2906};
2907
2908static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2909 struct uac3_badd_profile *f,
2910 int c_chmask, int p_chmask)
2911{
2912 /*
2913 * If both playback/capture channels are dynamic, make sure
2914 * at least one channel is present
2915 */
2916 if (f->c_chmask < 0 && f->p_chmask < 0) {
2917 if (!c_chmask && !p_chmask) {
2918 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2919 f->name);
2920 return false;
2921 }
2922 return true;
2923 }
2924
2925 if ((f->c_chmask < 0 && !c_chmask) ||
2926 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2927 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2928 f->name);
2929 return false;
2930 }
2931 if ((f->p_chmask < 0 && !p_chmask) ||
2932 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2933 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2934 f->name);
2935 return false;
2936 }
2937 return true;
2938}
2939
2940/*
2941 * create mixer controls for UAC3 BADD profiles
2942 *
2943 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2944 *
2945 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2946 */
2947static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2948 int ctrlif)
2949{
2950 struct usb_device *dev = mixer->chip->dev;
2951 struct usb_interface_assoc_descriptor *assoc;
2952 int badd_profile = mixer->chip->badd_profile;
2953 struct uac3_badd_profile *f;
2954 const struct usbmix_ctl_map *map;
2955 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2956 int i;
2957
2958 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2959
2960 /* Detect BADD capture/playback channels from AS EP descriptors */
2961 for (i = 0; i < assoc->bInterfaceCount; i++) {
2962 int intf = assoc->bFirstInterface + i;
2963
2964 struct usb_interface *iface;
2965 struct usb_host_interface *alts;
2966 struct usb_interface_descriptor *altsd;
2967 unsigned int maxpacksize;
2968 char dir_in;
2969 int chmask, num;
2970
2971 if (intf == ctrlif)
2972 continue;
2973
2974 iface = usb_ifnum_to_if(dev, intf);
2975 num = iface->num_altsetting;
2976
2977 if (num < 2)
2978 return -EINVAL;
2979
2980 /*
2981 * The number of Channels in an AudioStreaming interface
2982 * and the audio sample bit resolution (16 bits or 24
2983 * bits) can be derived from the wMaxPacketSize field in
2984 * the Standard AS Audio Data Endpoint descriptor in
2985 * Alternate Setting 1
2986 */
2987 alts = &iface->altsetting[1];
2988 altsd = get_iface_desc(alts);
2989
2990 if (altsd->bNumEndpoints < 1)
2991 return -EINVAL;
2992
2993 /* check direction */
2994 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2995 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2996
2997 switch (maxpacksize) {
2998 default:
2999 usb_audio_err(mixer->chip,
3000 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
3001 maxpacksize);
3002 return -EINVAL;
3003 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
3004 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
3005 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
3006 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
3007 chmask = 1;
3008 break;
3009 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
3010 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
3011 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
3012 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
3013 chmask = 3;
3014 break;
3015 }
3016
3017 if (dir_in)
3018 c_chmask = chmask;
3019 else
3020 p_chmask = chmask;
3021 }
3022
3023 usb_audio_dbg(mixer->chip,
3024 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
3025 badd_profile, c_chmask, p_chmask);
3026
3027 /* check the mapping table */
3028 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
3029 if (map->id == badd_profile)
3030 break;
3031 }
3032
3033 if (!map->id)
3034 return -EINVAL;
3035
3036 for (f = uac3_badd_profiles; f->name; f++) {
3037 if (badd_profile == f->subclass)
3038 break;
3039 }
3040 if (!f->name)
3041 return -EINVAL;
3042 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
3043 return -EINVAL;
3044 st_chmask = f->st_chmask;
3045
3046 /* Playback */
3047 if (p_chmask) {
3048 /* Master channel, always writable */
3049 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3050 UAC3_BADD_FU_ID2, map->map);
3051 /* Mono/Stereo volume channels, always writable */
3052 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
3053 UAC3_BADD_FU_ID2, map->map);
3054 }
3055
3056 /* Capture */
3057 if (c_chmask) {
3058 /* Master channel, always writable */
3059 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3060 UAC3_BADD_FU_ID5, map->map);
3061 /* Mono/Stereo volume channels, always writable */
3062 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
3063 UAC3_BADD_FU_ID5, map->map);
3064 }
3065
3066 /* Side tone-mixing */
3067 if (st_chmask) {
3068 /* Master channel, always writable */
3069 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3070 UAC3_BADD_FU_ID7, map->map);
3071 /* Mono volume channel, always writable */
3072 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
3073 UAC3_BADD_FU_ID7, map->map);
3074 }
3075
Jorge Sanjuan3528cd82018-06-14 15:05:57 +01003076 /* Insertion Control */
3077 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
3078 struct usb_audio_term iterm, oterm;
3079
3080 /* Input Term - Insertion control */
3081 memset(&iterm, 0, sizeof(iterm));
3082 iterm.id = UAC3_BADD_IT_ID4;
3083 iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3084 build_connector_control(mixer, &iterm, true);
3085
3086 /* Output Term - Insertion control */
3087 memset(&oterm, 0, sizeof(oterm));
3088 oterm.id = UAC3_BADD_OT_ID3;
3089 oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3090 build_connector_control(mixer, &oterm, false);
3091 }
3092
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003093 return 0;
3094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/*
3097 * create mixer controls
3098 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01003099 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02003101static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003103 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 int err;
3105 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01003106 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02003109 state.chip = mixer->chip;
3110 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003111 state.buffer = mixer->hostif->extra;
3112 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003115 for (map = usbmix_ctl_maps; map->id; map++) {
3116 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02003118 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003119 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 break;
3121 }
3122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Daniel Mack23caaf12010-03-11 21:13:25 +01003124 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02003125 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3126 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02003127 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01003128 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02003129 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01003130
3131 if (desc->bLength < sizeof(*desc))
3132 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02003133 /* mark terminal ID as visited */
3134 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01003135 state.oterm.id = desc->bTerminalID;
3136 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3137 state.oterm.name = desc->iTerminal;
3138 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003139 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01003140 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003141 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01003142 struct uac2_output_terminal_descriptor *desc = p;
3143
3144 if (desc->bLength < sizeof(*desc))
3145 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02003146 /* mark terminal ID as visited */
3147 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01003148 state.oterm.id = desc->bTerminalID;
3149 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3150 state.oterm.name = desc->iTerminal;
3151 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003152 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01003153 return err;
Daniel Mack09414202010-05-31 13:35:44 +02003154
Daniel Mack6bc170e2014-05-24 10:58:16 +02003155 /*
3156 * For UAC2, use the same approach to also add the
3157 * clock selectors
3158 */
Daniel Mack09414202010-05-31 13:35:44 +02003159 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003160 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02003161 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07003162
Takashi Iwai2b54f7852018-04-23 15:19:25 +02003163 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07003164 UAC2_TE_CONNECTOR)) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01003165 build_connector_control(state.mixer, &state.oterm,
Andrew Chant5a222e82018-03-23 19:25:23 -07003166 false);
3167 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003168 } else { /* UAC_VERSION_3 */
3169 struct uac3_output_terminal_descriptor *desc = p;
3170
3171 if (desc->bLength < sizeof(*desc))
3172 continue; /* invalid descriptor? */
3173 /* mark terminal ID as visited */
3174 set_bit(desc->bTerminalID, state.unitbitmap);
3175 state.oterm.id = desc->bTerminalID;
3176 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3177 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3178 err = parse_audio_unit(&state, desc->bSourceID);
3179 if (err < 0 && err != -EINVAL)
3180 return err;
3181
3182 /*
3183 * For UAC3, use the same approach to also add the
3184 * clock selectors
3185 */
3186 err = parse_audio_unit(&state, desc->bCSourceID);
3187 if (err < 0 && err != -EINVAL)
3188 return err;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003189
3190 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3191 UAC3_TE_INSERTION)) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01003192 build_connector_control(state.mixer, &state.oterm,
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003193 false);
3194 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return 0;
3199}
Clemens Ladisch84957a82005-04-29 16:23:13 +02003200
Daniel Mack7b1eda22010-03-11 21:13:22 +01003201void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003202{
Takashi Iwai3360b842014-11-18 11:47:04 +01003203 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003204
Takashi Iwai8c558072018-05-03 12:33:32 +02003205 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01003206 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02003207 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01003208 /* invalidate cache, so the value is read from the device */
3209 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003210 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003211 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01003212 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003213}
3214
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003215static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01003216 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003217{
Takashi Iwai8c558072018-05-03 12:33:32 +02003218 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003219 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3220 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003221 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01003222 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003223 cval->control, cval->cmask, cval->channels,
3224 val_types[cval->val_type]);
3225 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3226 cval->min, cval->max, cval->dBmin, cval->dBmax);
3227}
3228
3229static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3230 struct snd_info_buffer *buffer)
3231{
3232 struct snd_usb_audio *chip = entry->private_data;
3233 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01003234 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003235 int unitid;
3236
3237 list_for_each_entry(mixer, &chip->mixer_list, list) {
3238 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003239 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02003240 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003241 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003242 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3243 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003244 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003245 snd_iprintf(buffer, " Unit: %i\n", list->id);
3246 if (list->kctl)
3247 snd_iprintf(buffer,
3248 " Control: name=\"%s\", index=%i\n",
3249 list->kctl->id.name,
3250 list->kctl->id.index);
3251 if (list->dump)
3252 list->dump(buffer, list);
3253 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003254 }
3255 }
3256}
3257
Daniel Macke213e9c2010-05-11 18:13:50 +02003258static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3259 int attribute, int value, int index)
3260{
Takashi Iwai3360b842014-11-18 11:47:04 +01003261 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02003262 __u8 unitid = (index >> 8) & 0xff;
3263 __u8 control = (value >> 8) & 0xff;
3264 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02003265 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02003266
3267 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003268 usb_audio_dbg(mixer->chip,
3269 "%s(): bogus channel number %d\n",
3270 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02003271 return;
3272 }
3273
Takashi Iwai8c558072018-05-03 12:33:32 +02003274 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02003275 count++;
3276
3277 if (count == 0)
3278 return;
3279
Takashi Iwai8c558072018-05-03 12:33:32 +02003280 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003281 struct usb_mixer_elem_info *info;
3282
3283 if (!list->kctl)
3284 continue;
3285
Takashi Iwai8c558072018-05-03 12:33:32 +02003286 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02003287 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02003288 continue;
3289
3290 switch (attribute) {
3291 case UAC2_CS_CUR:
3292 /* invalidate cache, so the value is read from the device */
3293 if (channel)
3294 info->cached &= ~(1 << channel);
3295 else /* master channel */
3296 info->cached = 0;
3297
3298 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003299 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02003300 break;
3301
3302 case UAC2_CS_RANGE:
3303 /* TODO */
3304 break;
3305
3306 case UAC2_CS_MEM:
3307 /* TODO */
3308 break;
3309
3310 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003311 usb_audio_dbg(mixer->chip,
3312 "unknown attribute %d in interrupt\n",
3313 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02003314 break;
3315 } /* switch */
3316 }
3317}
3318
3319static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003320{
3321 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02003322 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01003323 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003324
Oliver Neukumedf7de32011-03-11 13:19:43 +01003325 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02003326 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003327
Daniel Macke213e9c2010-05-11 18:13:50 +02003328 if (mixer->protocol == UAC_VERSION_1) {
3329 struct uac1_status_word *status;
3330
3331 for (status = urb->transfer_buffer;
3332 len >= sizeof(*status);
3333 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003334 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02003335 status->bStatusType,
3336 status->bOriginator);
3337
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003338 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02003339 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3340 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003341 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003342
3343 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3344 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003345 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003346 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3347 }
3348 } else { /* UAC_VERSION_2 */
3349 struct uac2_interrupt_data_msg *msg;
3350
3351 for (msg = urb->transfer_buffer;
3352 len >= sizeof(*msg);
3353 len -= sizeof(*msg), msg++) {
3354 /* drop vendor specific and endpoint requests */
3355 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3356 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3357 continue;
3358
3359 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3360 le16_to_cpu(msg->wValue),
3361 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003362 }
3363 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003364
3365requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003366 if (ustatus != -ENOENT &&
3367 ustatus != -ECONNRESET &&
3368 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003369 urb->dev = mixer->chip->dev;
3370 usb_submit_urb(urb, GFP_ATOMIC);
3371 }
3372}
3373
3374/* create the handler for the optional status interrupt endpoint */
3375static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3376{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003377 struct usb_endpoint_descriptor *ep;
3378 void *transfer_buffer;
3379 int buffer_length;
3380 unsigned int epnum;
3381
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003382 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003383 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003384 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003385 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003386 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003387 return 0;
3388
Julia Lawall42a6e662008-12-29 11:23:02 +01003389 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003390 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3391 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3392 if (!transfer_buffer)
3393 return -ENOMEM;
3394 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3395 if (!mixer->urb) {
3396 kfree(transfer_buffer);
3397 return -ENOMEM;
3398 }
3399 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3400 usb_rcvintpipe(mixer->chip->dev, epnum),
3401 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003402 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003403 usb_submit_urb(mixer->urb, GFP_KERNEL);
3404 return 0;
3405}
3406
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003407static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3408 struct snd_ctl_elem_value *ucontrol)
3409{
3410 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3411
3412 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3413 return 0;
3414}
3415
3416static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
3419 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3420 bool keep_iface = !!ucontrol->value.integer.value[0];
3421
3422 if (mixer->chip->keep_iface == keep_iface)
3423 return 0;
3424 mixer->chip->keep_iface = keep_iface;
3425 return 1;
3426}
3427
3428static const struct snd_kcontrol_new keep_iface_ctl = {
3429 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3430 .name = "Keep Interface",
3431 .info = snd_ctl_boolean_mono_info,
3432 .get = keep_iface_ctl_get,
3433 .put = keep_iface_ctl_put,
3434};
3435
3436static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3437{
3438 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3439
3440 /* need only one control per card */
3441 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3442 snd_ctl_free_one(kctl);
3443 return 0;
3444 }
3445
3446 return snd_ctl_add(mixer->chip->card, kctl);
3447}
3448
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003449int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3450 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003451{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003452 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003453 .dev_free = snd_usb_mixer_dev_free
3454 };
3455 struct usb_mixer_interface *mixer;
Daniel Mack23caaf12010-03-11 21:13:25 +01003456 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003457
3458 strcpy(chip->card->mixername, "USB Mixer");
3459
Takashi Iwai561b2202005-09-09 14:22:34 +02003460 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003461 if (!mixer)
3462 return -ENOMEM;
3463 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003464 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003465 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3466 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003467 if (!mixer->id_elems) {
3468 kfree(mixer);
3469 return -ENOMEM;
3470 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003471
Daniel Mack1faa5d02011-08-04 15:56:28 +02003472 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3473 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003474 case UAC_VERSION_1:
3475 default:
3476 mixer->protocol = UAC_VERSION_1;
3477 break;
3478 case UAC_VERSION_2:
3479 mixer->protocol = UAC_VERSION_2;
3480 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003481 case UAC_VERSION_3:
3482 mixer->protocol = UAC_VERSION_3;
3483 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003484 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003485
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003486 if (mixer->protocol == UAC_VERSION_3 &&
3487 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003488 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3489 if (err < 0)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003490 goto _error;
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003491 } else {
3492 err = snd_usb_mixer_controls(mixer);
3493 if (err < 0)
3494 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003495 }
Jorge Sanjuanad6baae2018-06-14 15:05:58 +01003496
3497 err = snd_usb_mixer_status_create(mixer);
3498 if (err < 0)
3499 goto _error;
3500
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003501 err = create_keep_iface_ctl(mixer);
3502 if (err < 0)
3503 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003504
Takashi Iwai328e9f62019-04-24 13:00:03 +02003505 err = snd_usb_mixer_apply_create_quirk(mixer);
3506 if (err < 0)
3507 goto _error;
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003508
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003509 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003510 if (err < 0)
3511 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003512
Takashi Iwai74490542019-02-04 16:07:35 +01003513 if (list_empty(&chip->mixer_list))
3514 snd_card_ro_proc_new(chip->card, "usbmixer", chip,
3515 snd_usb_mixer_proc_read);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003516
Clemens Ladisch84957a82005-04-29 16:23:13 +02003517 list_add(&mixer->list, &chip->mixer_list);
3518 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003519
3520_error:
3521 snd_usb_mixer_free(mixer);
3522 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003523}
3524
Takashi Iwaia6cece92014-10-31 11:24:32 +01003525void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003526{
Takashi Iwai124751d2017-10-10 14:10:32 +02003527 if (mixer->disconnected)
3528 return;
3529 if (mixer->urb)
3530 usb_kill_urb(mixer->urb);
3531 if (mixer->rc_urb)
3532 usb_kill_urb(mixer->rc_urb);
3533 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003534}
Takashi Iwai400362f2014-01-20 16:51:16 +01003535
3536#ifdef CONFIG_PM
3537/* stop any bus activity of a mixer */
3538static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3539{
3540 usb_kill_urb(mixer->urb);
3541 usb_kill_urb(mixer->rc_urb);
3542}
3543
3544static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3545{
3546 int err;
3547
3548 if (mixer->urb) {
3549 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3550 if (err < 0)
3551 return err;
3552 }
3553
3554 return 0;
3555}
3556
3557int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3558{
3559 snd_usb_mixer_inactivate(mixer);
3560 return 0;
3561}
3562
Takashi Iwai3360b842014-11-18 11:47:04 +01003563static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003564{
Takashi Iwai8c558072018-05-03 12:33:32 +02003565 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003566 int c, err, idx;
3567
3568 if (cval->cmask) {
3569 idx = 0;
3570 for (c = 0; c < MAX_CHANNELS; c++) {
3571 if (!(cval->cmask & (1 << c)))
3572 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003573 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003574 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003575 cval->cache_val[idx]);
3576 if (err < 0)
3577 return err;
3578 }
3579 idx++;
3580 }
3581 } else {
3582 /* master */
3583 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003584 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003585 if (err < 0)
3586 return err;
3587 }
3588 }
3589
3590 return 0;
3591}
3592
3593int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3594{
Takashi Iwai3360b842014-11-18 11:47:04 +01003595 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003596 int id, err;
3597
Takashi Iwai400362f2014-01-20 16:51:16 +01003598 if (reset_resume) {
3599 /* restore cached mixer values */
3600 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003601 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003602 if (list->resume) {
3603 err = list->resume(list);
3604 if (err < 0)
3605 return err;
3606 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003607 }
3608 }
3609 }
3610
Takashi Iwai964af632018-04-27 14:23:37 +02003611 snd_usb_mixer_resume_quirk(mixer);
3612
Takashi Iwai400362f2014-01-20 16:51:16 +01003613 return snd_usb_mixer_activate(mixer);
3614}
3615#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003616
3617void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3618 struct usb_mixer_interface *mixer,
3619 int unitid)
3620{
3621 list->mixer = mixer;
3622 list->id = unitid;
3623 list->dump = snd_usb_mixer_dump_cval;
3624#ifdef CONFIG_PM
3625 list->resume = restore_mixer_value;
3626#endif
3627}