blob: e2cbdd080d33440ff7e20afd06369200447099f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Daniel Mack157a57b2010-06-16 17:57:30 +020029/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
Daniel Mackcddaafb2016-04-09 11:21:46 +020048#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010052#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010053#include <linux/usb/audio-v2.h>
Ajay Agarwal045e3742017-02-07 18:41:34 +053054#include <linux/usb/audio-v3.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <sound/core.h>
57#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020058#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020059#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020060#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010063#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010064#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010065#include "mixer_quirks.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010066#include "power.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020067
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010068#define MAX_ID_ELEMS 256
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
Takashi Iwai86e07d32005-11-17 15:08:02 +010080struct mixer_build {
81 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020082 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned char *buffer;
84 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010085 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Hui Peng2bac3a32019-08-15 00:31:34 -040086 DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010087 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020089 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050092/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080093enum {
94 USB_XU_CLOCK_RATE = 0xe301,
95 USB_XU_CLOCK_SOURCE = 0xe302,
96 USB_XU_DIGITAL_IO_STATUS = 0xe303,
97 USB_XU_DEVICE_OPTIONS = 0xe304,
98 USB_XU_DIRECT_MONITORING = 0xe305,
99 USB_XU_METERING = 0xe306
100};
101enum {
102 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
103 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
104 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
105 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * manual mapping of mixer names
110 * if the mixer topology is too complicated and the parsed names are
111 * ambiguous, add the entries in usbmixer_maps.c.
112 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100113#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100115static const struct usbmix_name_map *
116find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100118 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100120 if (!p)
121 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100124 if (p->id == unitid &&
125 (!control || !p->control || control == p->control))
126 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100128 return NULL;
129}
130
131/* get the mapped name if the unit matches */
132static int
133check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
134{
135 if (!p || !p->name)
136 return 0;
137
138 buflen--;
139 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Takashi Iwai5aeee342014-11-18 11:02:14 +0100142/* ignore the error value if ignore_ctl_error flag is set */
143#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100144 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147static inline int
148check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100150 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100152 return 1;
153}
154
155/* dB mapping */
156static inline void check_mapped_dB(const struct usbmix_name_map *p,
157 struct usb_mixer_elem_info *cval)
158{
159 if (p && p->dB) {
160 cval->dBmin = p->dB->min;
161 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200162 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200166/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100167static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200168 int index, char *buf, int buflen)
169{
170 const struct usbmix_selector_map *p;
171
Daniel Mack6bc170e2014-05-24 10:58:16 +0200172 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200173 return 0;
174 for (p = state->selector_map; p->id; p++) {
175 if (p->id == unitid && index < p->count)
176 return strlcpy(buf, p->names[index], buflen);
177 }
178 return 0;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * find an audio control unit with the given unit id
183 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200184static void *find_audio_control_unit(struct mixer_build *state,
185 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200187 /* we just parse the header */
188 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Ajay Agarwal045e3742017-02-07 18:41:34 +0530190 if (state->mixer->protocol == UAC_VERSION_3) {
191 int i;
192
193 for (i = 0; i < NUM_BADD_DESCS; i++) {
194 hdr = (void *)badd_desc_list[i];
195 if (hdr->bUnitID == unit)
196 return hdr;
197 }
198
199 return NULL;
200 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200201 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
202 USB_DT_CS_INTERFACE)) != NULL) {
203 if (hdr->bLength >= 4 &&
204 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
205 hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER &&
206 hdr->bUnitID == unit)
207 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return NULL;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * copy a string with the given id
215 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200216static int snd_usb_copy_string_desc(struct mixer_build *state,
217 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
Jaejoong Kimc40457c2017-12-04 15:31:48 +0900220
221 if (len < 0)
222 return 0;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 buf[len] = 0;
225 return len;
226}
227
228/*
229 * convert from the byte/word on usb descriptor to the zero-based integer
230 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100231static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 switch (cval->val_type) {
234 case USB_MIXER_BOOLEAN:
235 return !!val;
236 case USB_MIXER_INV_BOOLEAN:
237 return !val;
238 case USB_MIXER_U8:
239 val &= 0xff;
240 break;
241 case USB_MIXER_S8:
242 val &= 0xff;
243 if (val >= 0x80)
244 val -= 0x100;
245 break;
246 case USB_MIXER_U16:
247 val &= 0xffff;
248 break;
249 case USB_MIXER_S16:
250 val &= 0xffff;
251 if (val >= 0x8000)
252 val -= 0x10000;
253 break;
254 }
255 return val;
256}
257
258/*
259 * convert from the zero-based int to the byte/word for usb descriptor
260 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100261static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 switch (cval->val_type) {
264 case USB_MIXER_BOOLEAN:
265 return !!val;
266 case USB_MIXER_INV_BOOLEAN:
267 return !val;
268 case USB_MIXER_S8:
269 case USB_MIXER_U8:
270 return val & 0xff;
271 case USB_MIXER_S16:
272 case USB_MIXER_U16:
273 return val & 0xffff;
274 }
275 return 0; /* not reached */
276}
277
Takashi Iwai86e07d32005-11-17 15:08:02 +0100278static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200280 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cval->res = 1;
282 if (val < cval->min)
283 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200284 else if (val >= cval->max)
285 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 else
287 return (val - cval->min) / cval->res;
288}
289
Takashi Iwai86e07d32005-11-17 15:08:02 +0100290static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 if (val < 0)
293 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200294 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 cval->res = 1;
296 val *= cval->res;
297 val += cval->min;
298 if (val > cval->max)
299 return cval->max;
300 return val;
301}
302
Julian Scheelbc18e312015-08-14 16:14:45 +0200303static int uac2_ctl_value_size(int val_type)
304{
305 switch (val_type) {
306 case USB_MIXER_S32:
307 case USB_MIXER_U32:
308 return 4;
309 case USB_MIXER_S16:
310 case USB_MIXER_U16:
311 return 2;
312 default:
313 return 1;
314 }
315 return 0; /* unreachable */
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/*
320 * retrieve a mixer value
321 */
322
Daniel Mack6bc170e2014-05-24 10:58:16 +0200323static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
324 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwai3360b842014-11-18 11:47:04 +0100326 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned char buf[2];
328 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
329 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200330 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Takashi Iwai47ab1542015-08-25 16:09:00 +0200332 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100333 if (err < 0)
334 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100337 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200338 if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200340 validx, idx, buf, val_len) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200342 err = 0;
343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100346 usb_audio_dbg(chip,
347 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
348 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200349 err = -EINVAL;
350
351 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200352 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Daniel Mack6bc170e2014-05-24 10:58:16 +0200356static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
357 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100358{
Takashi Iwai3360b842014-11-18 11:47:04 +0100359 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100360 /* enough space for one range */
361 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100362 unsigned char *val;
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100363 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100364 __u8 bRequest;
365
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100366 val_size = uac2_ctl_value_size(cval->val_type);
367
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200368 if (request == UAC_GET_CUR) {
369 bRequest = UAC2_CS_CUR;
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100370 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200371 } else {
372 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100373 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200374 }
375
376 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100377
Takashi Iwai47ab1542015-08-25 16:09:00 +0200378 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100379 if (ret)
380 goto error;
381
Takashi Iwai47ab1542015-08-25 16:09:00 +0200382 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
383 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100384 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200385 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200386 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100387
388 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100389error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100390 usb_audio_err(chip,
391 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
392 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100393 return ret;
394 }
395
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200396 /* FIXME: how should we handle multiple triplets here? */
397
Daniel Mack23caaf12010-03-11 21:13:25 +0100398 switch (request) {
399 case UAC_GET_CUR:
400 val = buf;
401 break;
402 case UAC_GET_MIN:
403 val = buf + sizeof(__u16);
404 break;
405 case UAC_GET_MAX:
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100406 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100407 break;
408 case UAC_GET_RES:
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100409 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100410 break;
411 default:
412 return -EINVAL;
413 }
414
Kirill Marinushkin31cb8df2018-01-29 06:37:55 +0100415 *value_ret = convert_signed_value(cval,
416 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100417
418 return 0;
419}
420
Daniel Mack6bc170e2014-05-24 10:58:16 +0200421static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
422 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100423{
Eldad Zack9f814102012-11-28 23:55:35 +0100424 validx += cval->idx_off;
425
Takashi Iwai3360b842014-11-18 11:47:04 +0100426 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100427 get_ctl_value_v1(cval, request, validx, value_ret) :
428 get_ctl_value_v2(cval, request, validx, value_ret);
429}
430
Daniel Mack6bc170e2014-05-24 10:58:16 +0200431static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
432 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100434 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100438static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
439 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200441 return get_ctl_value(cval, UAC_GET_CUR,
442 (cval->control << 8) | channel,
443 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Chris J Argeseef90452014-11-12 12:07:01 -0600446int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100447 int channel, int index, int *value)
448{
449 int err;
450
451 if (cval->cached & (1 << channel)) {
452 *value = cval->cache_val[index];
453 return 0;
454 }
455 err = get_cur_mix_raw(cval, channel, value);
456 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100457 if (!cval->head.mixer->ignore_ctl_error)
458 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100459 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200460 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100461 return err;
462 }
463 cval->cached |= 1 << channel;
464 cval->cache_val[index] = *value;
465 return 0;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * set a mixer value
470 */
471
Daniel Mack7b1eda22010-03-11 21:13:22 +0100472int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
473 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Takashi Iwai3360b842014-11-18 11:47:04 +0100475 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200476 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200477 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100478
Eldad Zack9f814102012-11-28 23:55:35 +0100479 validx += cval->idx_off;
480
Takashi Iwai3360b842014-11-18 11:47:04 +0100481 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100482 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
483 } else { /* UAC_VERSION_2 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200484 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100485
486 /* FIXME */
487 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100488 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100489 return -EINVAL;
490 }
491
492 request = UAC2_CS_CUR;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 value_set = convert_bytes_value(cval, value_set);
496 buf[0] = value_set & 0xff;
497 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200498 buf[2] = (value_set >> 16) & 0xff;
499 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200500
501 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100502 if (err < 0)
503 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200504
Takashi Iwai978520b2012-10-12 15:12:55 +0200505 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100506 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200507 if (snd_usb_ctl_msg(chip->dev,
508 usb_sndctrlpipe(chip->dev, 0), request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 validx, idx, buf, val_len) >= 0) {
511 err = 0;
512 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100513 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200514 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100515 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 +0200516 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200517 err = -EINVAL;
518
519 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200520 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Daniel Mack6bc170e2014-05-24 10:58:16 +0200524static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
525 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100527 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Chris J Argeseef90452014-11-12 12:07:01 -0600530int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100531 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Takashi Iwai641b4872009-01-15 17:05:24 +0100533 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200534 unsigned int read_only = (channel == 0) ?
535 cval->master_readonly :
536 cval->ch_readonly & (1 << (channel - 1));
537
538 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100539 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100540 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200541 __func__, channel, cval->control);
542 return 0;
543 }
544
Daniel Mack6bc170e2014-05-24 10:58:16 +0200545 err = snd_usb_mixer_set_ctl_value(cval,
546 UAC_SET_CUR, (cval->control << 8) | channel,
547 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100548 if (err < 0)
549 return err;
550 cval->cached |= 1 << channel;
551 cval->cache_val[index] = value;
552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200555/*
556 * TLV callback for mixer volume controls
557 */
Felix Homann285de9c2012-04-23 20:24:24 +0200558int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200559 unsigned int size, unsigned int __user *_tlv)
560{
561 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200562 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200563
564 if (size < sizeof(scale))
565 return -ENOMEM;
Takashi Iwaid3e6e592017-08-16 14:18:37 +0200566 if (cval->min_mute)
567 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100568 scale[2] = cval->dBmin;
569 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200570 if (copy_to_user(_tlv, scale, sizeof(scale)))
571 return -EFAULT;
572 return 0;
573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575/*
576 * parser routines begin here...
577 */
578
Takashi Iwai86e07d32005-11-17 15:08:02 +0100579static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581
582/*
583 * check if the input/output channel routing is enabled on the given bitmap.
584 * used for mixer unit parser
585 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200586static int check_matrix_bitmap(unsigned char *bmap,
587 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 int idx = ich * num_outs + och;
590 return bmap[idx >> 3] & (0x80 >> (idx & 7));
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * add an alsa control element
595 * search and increment the index until an empty slot is found.
596 *
597 * if failed, give up and free the control instance.
598 */
599
Takashi Iwai3360b842014-11-18 11:47:04 +0100600int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200601 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwai3360b842014-11-18 11:47:04 +0100603 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200605
Daniel Mackef9d5972011-05-25 09:09:00 +0200606 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 kctl->id.index++;
Daniel Mackef9d5972011-05-25 09:09:00 +0200608 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200609 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
610 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100613 list->kctl = kctl;
614 list->next_id_elem = mixer->id_elems[list->id];
615 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * get a terminal name string
621 */
622
623static struct iterm_name_combo {
624 int type;
625 char *name;
626} iterm_names[] = {
627 { 0x0300, "Output" },
628 { 0x0301, "Speaker" },
629 { 0x0302, "Headphone" },
630 { 0x0303, "HMD Audio" },
631 { 0x0304, "Desktop Speaker" },
632 { 0x0305, "Room Speaker" },
633 { 0x0306, "Com Speaker" },
634 { 0x0307, "LFE" },
635 { 0x0600, "External In" },
636 { 0x0601, "Analog In" },
637 { 0x0602, "Digital In" },
638 { 0x0603, "Line" },
639 { 0x0604, "Legacy In" },
640 { 0x0605, "IEC958 In" },
641 { 0x0606, "1394 DA Stream" },
642 { 0x0607, "1394 DV Stream" },
643 { 0x0700, "Embedded" },
644 { 0x0701, "Noise Source" },
645 { 0x0702, "Equalization Noise" },
646 { 0x0703, "CD" },
647 { 0x0704, "DAT" },
648 { 0x0705, "DCC" },
649 { 0x0706, "MiniDisk" },
650 { 0x0707, "Analog Tape" },
651 { 0x0708, "Phonograph" },
652 { 0x0709, "VCR Audio" },
653 { 0x070a, "Video Disk Audio" },
654 { 0x070b, "DVD Audio" },
655 { 0x070c, "TV Tuner Audio" },
656 { 0x070d, "Satellite Rec Audio" },
657 { 0x070e, "Cable Tuner Audio" },
658 { 0x070f, "DSS Audio" },
659 { 0x0710, "Radio Receiver" },
660 { 0x0711, "Radio Transmitter" },
661 { 0x0712, "Multi-Track Recorder" },
662 { 0x0713, "Synthesizer" },
663 { 0 },
664};
665
Takashi Iwai86e07d32005-11-17 15:08:02 +0100666static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 unsigned char *name, int maxlen, int term_only)
668{
669 struct iterm_name_combo *names;
670
671 if (iterm->name)
Daniel Mack6bc170e2014-05-24 10:58:16 +0200672 return snd_usb_copy_string_desc(state, iterm->name,
673 name, maxlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /* virtual type - not a real terminal */
676 if (iterm->type >> 16) {
677 if (term_only)
678 return 0;
679 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100680 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200681 strcpy(name, "Selector");
682 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200683 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200684 strcpy(name, "Process Unit");
685 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200686 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200687 strcpy(name, "Ext Unit");
688 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100689 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200690 strcpy(name, "Mixer");
691 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 default:
693 return sprintf(name, "Unit %d", iterm->id);
694 }
695 }
696
697 switch (iterm->type & 0xff00) {
698 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200699 strcpy(name, "PCM");
700 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200702 strcpy(name, "Mic");
703 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200705 strcpy(name, "Headset");
706 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200708 strcpy(name, "Phone");
709 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Daniel Mack6bc170e2014-05-24 10:58:16 +0200712 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (names->type == iterm->type) {
714 strcpy(name, names->name);
715 return strlen(names->name);
716 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200717 }
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
723 * parse the source unit recursively until it reaches to a terminal
724 * or a branched unit.
725 */
Hui Peng2bac3a32019-08-15 00:31:34 -0400726static int __check_input_term(struct mixer_build *state, int id,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200727 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Daniel Mack09414202010-05-31 13:35:44 +0200729 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100730 void *p1;
Hui Peng2bac3a32019-08-15 00:31:34 -0400731 unsigned char *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 memset(term, 0, sizeof(*term));
Hui Peng2bac3a32019-08-15 00:31:34 -0400734 for (;;) {
735 /* a loop in the terminal chain? */
736 if (test_and_set_bit(id, state->termbitmap))
737 return -EINVAL;
738
739 p1 = find_audio_control_unit(state, id);
740 if (!p1)
741 break;
742
743 hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100745 switch (hdr[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100746 case UAC_INPUT_TERMINAL:
Daniel Mack23caaf12010-03-11 21:13:25 +0100747 if (state->mixer->protocol == UAC_VERSION_1) {
748 struct uac_input_terminal_descriptor *d = p1;
749 term->type = le16_to_cpu(d->wTerminalType);
750 term->channels = d->bNrChannels;
751 term->chconfig = le16_to_cpu(d->wChannelConfig);
752 term->name = d->iTerminal;
Ajay Agarwal045e3742017-02-07 18:41:34 +0530753 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100754 struct uac2_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200755
756 /* call recursively to verify that the
757 * referenced clock entity is valid */
Hui Peng2bac3a32019-08-15 00:31:34 -0400758 err = __check_input_term(state, d->bCSourceID, term);
Julian Scheel9430e542015-08-19 09:28:09 +0200759 if (err < 0)
760 return err;
761
762 /* save input term properties after recursion,
763 * to ensure they are not overriden by the
764 * recursion calls */
765 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100766 term->type = le16_to_cpu(d->wTerminalType);
767 term->channels = d->bNrChannels;
768 term->chconfig = le32_to_cpu(d->bmChannelConfig);
769 term->name = d->iTerminal;
Ajay Agarwal045e3742017-02-07 18:41:34 +0530770 } else { /* UAC_VERSION_3 */
771 struct uac3_input_terminal_descriptor *d = p1;
772
jianzhou6fdca0f2019-10-23 13:25:13 +0800773 err = __check_input_term(state,
Ajay Agarwal045e3742017-02-07 18:41:34 +0530774 d->bCSourceID, term);
775 if (err < 0)
776 return err;
777
778 term->id = id;
779 term->type = d->wTerminalType;
780 if (d->wClusterDescrID == CLUSTER_ID_MONO) {
781 term->channels = NUM_CHANNELS_MONO;
782 term->chconfig = BADD_CH_CONFIG_MONO;
783 } else {
784 term->channels = NUM_CHANNELS_STEREO;
785 term->chconfig = BADD_CH_CONFIG_STEREO;
786 }
787 term->name = d->wTerminalDescrStr;
Daniel Mack23caaf12010-03-11 21:13:25 +0100788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100790 case UAC_FEATURE_UNIT: {
791 /* the header is the same for v1 and v2 */
792 struct uac_feature_unit_descriptor *d = p1;
Daniel Mack5e688882010-05-08 11:24:56 +0200793 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break; /* continue to parse */
Daniel Mack23caaf12010-03-11 21:13:25 +0100795 }
796 case UAC_MIXER_UNIT: {
797 struct uac_mixer_unit_descriptor *d = p1;
798 term->type = d->bDescriptorSubtype << 16; /* virtual type */
799 term->channels = uac_mixer_unit_bNrChannels(d);
800 term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
801 term->name = uac_mixer_unit_iMixer(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100803 }
jianzhou6fdca0f2019-10-23 13:25:13 +0800804 case UAC_SELECTOR_UNIT: {
Daniel Mack23caaf12010-03-11 21:13:25 +0100805 struct uac_selector_unit_descriptor *d = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* call recursively to retrieve the channel info */
Hui Peng2bac3a32019-08-15 00:31:34 -0400807 err = __check_input_term(state, d->baSourceID[0], term);
Daniel Mack4d7b86c2013-03-19 21:09:24 +0100808 if (err < 0)
809 return err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100810 term->type = d->bDescriptorSubtype << 16; /* virtual type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100812 term->name = uac_selector_unit_iSelector(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100814 }
Ajay Agarwal045e3742017-02-07 18:41:34 +0530815 /* UAC3_MIXER_UNIT_V3 */
816 case UAC2_CLOCK_SELECTOR:
817 /* UAC3_CLOCK_SOURCE */ {
818 if (state->mixer->protocol == UAC_VERSION_3
819 && hdr[2] == UAC3_CLOCK_SOURCE) {
820 struct uac3_clock_source_descriptor *d = p1;
821
822 term->type = d->bDescriptorSubtype << 16;
823 term->id = id;
824 term->name = d->wClockSourceStr;
825 } else if (state->mixer->protocol == UAC_VERSION_3
826 && hdr[2] == UAC3_MIXER_UNIT_V3) {
827 struct uac3_mixer_unit_descriptor *d = p1;
828
829 term->type = d->bDescriptorSubtype << 16;
830 if (d->wClusterDescrID == CLUSTER_ID_MONO) {
831 term->channels = NUM_CHANNELS_MONO;
832 term->chconfig = BADD_CH_CONFIG_MONO;
833 } else {
834 term->channels = NUM_CHANNELS_STEREO;
835 term->chconfig = BADD_CH_CONFIG_STEREO;
836 }
837 term->name = d->wMixerDescrStr;
838 } else {
839 struct uac_selector_unit_descriptor *d = p1;
840 /* call recursively to retrieve channel info */
jianzhou6fdca0f2019-10-23 13:25:13 +0800841 err = __check_input_term(state,
Ajay Agarwal045e3742017-02-07 18:41:34 +0530842 d->baSourceID[0], term);
843 if (err < 0)
844 return err;
845 /* virtual type */
846 term->type = d->bDescriptorSubtype << 16;
847 term->id = id;
848 term->name = uac_selector_unit_iSelector(d);
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 0;
851 }
Daniel Mack69da9bc2010-06-16 17:57:28 +0200852 case UAC1_PROCESSING_UNIT:
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100853 case UAC1_EXTENSION_UNIT:
854 /* UAC2_PROCESSING_UNIT_V2 */
Torstein Hegge61ac5132013-03-19 17:12:14 +0100855 /* UAC2_EFFECT_UNIT */
Ajay Agarwal045e3742017-02-07 18:41:34 +0530856 /* UAC3_FEATURE_UNIT_V3 */
Torstein Hegge61ac5132013-03-19 17:12:14 +0100857 case UAC2_EXTENSION_UNIT_V2: {
Ajay Agarwal045e3742017-02-07 18:41:34 +0530858 if (state->mixer->protocol == UAC_VERSION_3) {
859 struct uac_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100860
Ajay Agarwal045e3742017-02-07 18:41:34 +0530861 id = d->bSourceID;
862 } else {
863 struct uac_processing_unit_descriptor *d = p1;
864
865 if (state->mixer->protocol == UAC_VERSION_2 &&
866 hdr[2] == UAC2_EFFECT_UNIT) {
867 /* UAC2/UAC1 unit IDs overlap here in an
868 * uncompatible way. Ignore this unit
869 * for now.
870 */
871 return 0;
872 }
873
874 if (d->bNrInPins) {
875 id = d->baSourceID[0];
876 break; /* continue to parse */
877 }
878 /* virtual type */
879 term->type = d->bDescriptorSubtype << 16;
880 term->channels =
881 uac_processing_unit_bNrChannels(d);
882 term->chconfig =
883 uac_processing_unit_wChannelConfig(
884 d, state->mixer->protocol);
885 term->name = uac_processing_unit_iProcessing(
886 d, state->mixer->protocol);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100887 return 0;
888 }
Ajay Agarwal045e3742017-02-07 18:41:34 +0530889 break;
Daniel Mack23caaf12010-03-11 21:13:25 +0100890 }
Daniel Mack09414202010-05-31 13:35:44 +0200891 case UAC2_CLOCK_SOURCE: {
892 struct uac_clock_source_descriptor *d = p1;
893 term->type = d->bDescriptorSubtype << 16; /* virtual type */
894 term->id = id;
895 term->name = d->iClockSource;
896 return 0;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 default:
899 return -ENODEV;
900 }
901 }
902 return -ENODEV;
903}
904
Hui Peng2bac3a32019-08-15 00:31:34 -0400905
906static int check_input_term(struct mixer_build *state, int id,
907 struct usb_audio_term *term)
908{
909 memset(term, 0, sizeof(*term));
910 memset(state->termbitmap, 0, sizeof(state->termbitmap));
911 return __check_input_term(state, id, term);
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914/*
915 * Feature Unit
916 */
917
918/* feature unit control information */
919struct usb_feature_control_info {
920 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200921 int type; /* data type for uac1 */
922 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
924
925static struct usb_feature_control_info audio_feature_info[] = {
Julian Scheelbc18e312015-08-14 16:14:45 +0200926 { "Mute", USB_MIXER_INV_BOOLEAN, -1 },
927 { "Volume", USB_MIXER_S16, -1 },
928 { "Tone Control - Bass", USB_MIXER_S8, -1 },
929 { "Tone Control - Mid", USB_MIXER_S8, -1 },
930 { "Tone Control - Treble", USB_MIXER_S8, -1 },
931 { "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */
932 { "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
933 { "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
934 { "Bass Boost", USB_MIXER_BOOLEAN, -1 },
935 { "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200936 /* UAC2 specific */
Julian Scheelbc18e312015-08-14 16:14:45 +0200937 { "Input Gain Control", USB_MIXER_S16, -1 },
938 { "Input Gain Pad Control", USB_MIXER_S16, -1 },
939 { "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940};
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600943void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jesper Juhl4d572772005-05-30 17:30:32 +0200945 kfree(kctl->private_data);
946 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/*
950 * interface to ALSA control for feature/mixer units
951 */
952
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100953/* volume control quirks */
954static void volume_control_quirks(struct usb_mixer_elem_info *cval,
955 struct snd_kcontrol *kctl)
956{
Takashi Iwai3360b842014-11-18 11:47:04 +0100957 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100958 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +0100959 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500960 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +0100961 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
962 cval->min = 0x0000;
963 cval->max = 0xffff;
964 cval->res = 0x00e6;
965 break;
966 }
967 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
968 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
969 cval->min = 0x00;
970 cval->max = 0xff;
971 break;
972 }
973 if (strstr(kctl->id.name, "Effect Return") != NULL) {
974 cval->min = 0xb706;
975 cval->max = 0xff7b;
976 cval->res = 0x0073;
977 break;
978 }
979 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
980 (strstr(kctl->id.name, "Effect Send") != NULL)) {
981 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
982 cval->max = 0xfcfe;
983 cval->res = 0x0073;
984 }
985 break;
986
Felix Homannd34bf142012-04-23 20:24:27 +0200987 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
988 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
989 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100990 usb_audio_info(chip,
991 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +0200992 cval->min = 0x0000;
993 cval->max = 0x7f00;
994 cval->res = 0x0100;
995 break;
996 }
997 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
998 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100999 usb_audio_info(chip,
1000 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001001 cval->min = 0x00;
1002 cval->max = 0x7f;
1003 break;
1004 }
1005 break;
1006
Federico Cuello639a74b2018-05-09 00:13:38 +02001007 case USB_ID(0x0d8c, 0x0103):
1008 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1009 usb_audio_info(chip,
1010 "set volume quirk for CM102-A+/102S+\n");
1011 cval->min = -256;
1012 }
1013 break;
1014
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001015 case USB_ID(0x0471, 0x0101):
1016 case USB_ID(0x0471, 0x0104):
1017 case USB_ID(0x0471, 0x0105):
1018 case USB_ID(0x0672, 0x1041):
1019 /* quirk for UDA1321/N101.
1020 * note that detection between firmware 2.1.1.7 (N101)
1021 * and later 2.1.1.21 is not very clear from datasheets.
1022 * I hope that the min value is -15360 for newer firmware --jk
1023 */
1024 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1025 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001026 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001027 "set volume quirk for UDA1321/N101 chip\n");
1028 cval->max = -256;
1029 }
1030 break;
1031
1032 case USB_ID(0x046d, 0x09a4):
1033 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001034 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001035 "set volume quirk for QuickCam E3500\n");
1036 cval->min = 6080;
1037 cval->max = 8768;
1038 cval->res = 192;
1039 }
1040 break;
1041
Takashi Iwaie805ca82014-03-05 12:34:39 +01001042 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001043 case USB_ID(0x046d, 0x0808):
1044 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001045 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001046 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001047 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001048 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001049 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001050 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001051 case USB_ID(0x046d, 0x0991):
Con Kolivas205d3de2016-12-09 15:15:57 +11001052 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001053 /* Most audio usb devices lie about volume resolution.
1054 * Most Logitech webcams have res = 384.
Con Kolivas205d3de2016-12-09 15:15:57 +11001055 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001056 */
1057 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001058 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001059 "set resolution quirk: cval->res = 384\n");
1060 cval->res = 384;
1061 }
1062 break;
Ajay Agarwal5a19df02017-06-22 11:09:33 +05301063
1064 case USB_ID(0x1130, 0x1620): /* Logitech Speakers S150 */
1065 /* This audio device has 2 channels and it explicitly requires the
1066 * host to send SET_CUR command on the volume control of both the
1067 * channels. 7936 = 0x1F00 is the default value.
1068 */
1069 if (cval->channels == 2)
1070 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1071 (cval->control << 8) | 2, 7936);
1072 break;
1073
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001074 }
1075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/*
1078 * retrieve the minimum and maximum values for the specified control
1079 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001080static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1081 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 /* for failsafe */
1084 cval->min = default_min;
1085 cval->max = cval->min + 1;
1086 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001087 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (cval->val_type == USB_MIXER_BOOLEAN ||
1090 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1091 cval->initialized = 1;
1092 } else {
1093 int minchn = 0;
1094 if (cval->cmask) {
1095 int i;
1096 for (i = 0; i < MAX_CHANNELS; i++)
1097 if (cval->cmask & (1 << i)) {
1098 minchn = i + 1;
1099 break;
1100 }
1101 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001102 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1103 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001104 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001105 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001106 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1107 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -EINVAL;
1109 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001110 if (get_ctl_value(cval, UAC_GET_RES,
1111 (cval->control << 8) | minchn,
1112 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 cval->res = 1;
1114 } else {
1115 int last_valid_res = cval->res;
1116
1117 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001118 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001119 (cval->control << 8) | minchn,
1120 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122 cval->res /= 2;
1123 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001124 if (get_ctl_value(cval, UAC_GET_RES,
1125 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 cval->res = last_valid_res;
1127 }
1128 if (cval->res == 0)
1129 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001130
1131 /* Additional checks for the proper resolution
1132 *
1133 * Some devices report smaller resolutions than actually
1134 * reacting. They don't return errors but simply clip
1135 * to the lower aligned value.
1136 */
1137 if (cval->min + cval->res < cval->max) {
1138 int last_valid_res = cval->res;
1139 int saved, test, check;
Takashi Iwai40157312019-11-09 19:16:58 +01001140 if (get_cur_mix_raw(cval, minchn, &saved) < 0)
1141 goto no_res_check;
Takashi Iwai14790f12006-03-28 17:58:28 +02001142 for (;;) {
1143 test = saved;
1144 if (test < cval->max)
1145 test += cval->res;
1146 else
1147 test -= cval->res;
1148 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001149 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001150 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001151 cval->res = last_valid_res;
1152 break;
1153 }
1154 if (test == check)
1155 break;
1156 cval->res *= 2;
1157 }
Chris J Argeseef90452014-11-12 12:07:01 -06001158 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001159 }
1160
Takashi Iwai40157312019-11-09 19:16:58 +01001161no_res_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 cval->initialized = 1;
1163 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001164
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001165 if (kctl)
1166 volume_control_quirks(cval, kctl);
1167
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001168 /* USB descriptions contain the dB scale in 1/256 dB unit
1169 * while ALSA TLV contains in 1/100 dB unit
1170 */
Deepa Madiregama8b7f7fe2013-08-21 17:54:08 +05301171 cval->dBmin =
1172 (convert_signed_value(cval, cval->min) * 100) / (cval->res);
1173 cval->dBmax =
1174 (convert_signed_value(cval, cval->max) * 100) / (cval->res);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001175 if (cval->dBmin > cval->dBmax) {
1176 /* something is wrong; assume it's either from/to 0dB */
1177 if (cval->dBmin < 0)
1178 cval->dBmax = 0;
1179 else if (cval->dBmin > 0)
1180 cval->dBmin = 0;
1181 if (cval->dBmin > cval->dBmax) {
1182 /* totally crap, return an error */
1183 return -EINVAL;
1184 }
1185 }
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
1189
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001190#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001193static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1194 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001196 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (cval->val_type == USB_MIXER_BOOLEAN ||
1199 cval->val_type == USB_MIXER_INV_BOOLEAN)
1200 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1201 else
1202 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1203 uinfo->count = cval->channels;
1204 if (cval->val_type == USB_MIXER_BOOLEAN ||
1205 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1206 uinfo->value.integer.min = 0;
1207 uinfo->value.integer.max = 1;
1208 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001209 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001210 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001211 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1212 kcontrol->vd[0].access &=
1213 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1214 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001215 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001216 SNDRV_CTL_EVENT_MASK_INFO,
1217 &kcontrol->id);
1218 }
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001221 uinfo->value.integer.max =
1222 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 return 0;
1225}
1226
1227/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001228static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1229 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001231 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int c, cnt, val, err;
1233
Takashi Iwai641b4872009-01-15 17:05:24 +01001234 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (cval->cmask) {
1236 cnt = 0;
1237 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001238 if (!(cval->cmask & (1 << c)))
1239 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001240 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001241 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001242 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001243 val = get_relative_value(cval, val);
1244 ucontrol->value.integer.value[cnt] = val;
1245 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 } else {
1249 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001250 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001251 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001252 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 val = get_relative_value(cval, val);
1254 ucontrol->value.integer.value[0] = val;
1255 }
1256 return 0;
1257}
1258
1259/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001260static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1261 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001263 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int c, cnt, val, oval, err;
1265 int changed = 0;
1266
1267 if (cval->cmask) {
1268 cnt = 0;
1269 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001270 if (!(cval->cmask & (1 << c)))
1271 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001272 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001273 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001274 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001275 val = ucontrol->value.integer.value[cnt];
1276 val = get_abs_value(cval, val);
1277 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001278 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001279 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001281 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 } else {
1284 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001285 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001287 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 val = ucontrol->value.integer.value[0];
1289 val = get_abs_value(cval, val);
1290 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001291 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 changed = 1;
1293 }
1294 }
1295 return changed;
1296}
1297
Takashi Iwai86e07d32005-11-17 15:08:02 +01001298static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1300 .name = "", /* will be filled later manually */
1301 .info = mixer_ctl_feature_info,
1302 .get = mixer_ctl_feature_get,
1303 .put = mixer_ctl_feature_put,
1304};
1305
Daniel Mack23caaf12010-03-11 21:13:25 +01001306/* the read-only variant */
1307static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1309 .name = "", /* will be filled later manually */
1310 .info = mixer_ctl_feature_info,
1311 .get = mixer_ctl_feature_get,
1312 .put = NULL,
1313};
1314
Daniel Mack6bc170e2014-05-24 10:58:16 +02001315/*
1316 * This symbol is exported in order to allow the mixer quirks to
1317 * hook up to the standard feature unit control mechanism
1318 */
Daniel Mack9e386582011-05-25 09:09:01 +02001319struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321/*
1322 * build a feature control
1323 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001324static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1325{
1326 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1327}
1328
Daniel Mack6bc170e2014-05-24 10:58:16 +02001329/*
1330 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1331 * rename it to "Headphone". We determine if something is a headphone
1332 * similar to how udev determines form factor.
1333 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001334static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1335 struct snd_card *card)
1336{
1337 const char *names_to_check[] = {
1338 "Headset", "headset", "Headphone", "headphone", NULL};
1339 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001340 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001341
1342 if (strcmp("Speaker", kctl->id.name))
1343 return;
1344
1345 for (s = names_to_check; *s; s++)
1346 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001347 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001348 break;
1349 }
1350
1351 if (!found)
1352 return;
1353
1354 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1355}
1356
Daniel Mack99fc8642010-03-11 21:13:24 +01001357static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 unsigned int ctl_mask, int control,
Daniel Mack23caaf12010-03-11 21:13:25 +01001359 struct usb_audio_term *iterm, int unitid,
Daniel Macka6a33252010-05-31 13:35:37 +02001360 int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Daniel Mack99fc8642010-03-11 21:13:24 +01001362 struct uac_feature_unit_descriptor *desc = raw_desc;
Julian Scheelbc18e312015-08-14 16:14:45 +02001363 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 unsigned int len = 0;
1365 int mapped_name = 0;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301366 int nameid;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001367 struct snd_kcontrol *kctl;
1368 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001369 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001370 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Ajay Agarwal045e3742017-02-07 18:41:34 +05301372 if (state->mixer->protocol == UAC_VERSION_3)
1373 nameid = ((struct uac3_feature_unit_descriptor *)
1374 raw_desc)->wFeatureDescrStr;
1375 else
1376 nameid = uac_feature_unit_iFeature(desc);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 control++; /* change from zero-based to 1-based value */
1379
Daniel Mack65f25da2010-05-31 13:35:41 +02001380 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* FIXME: not supported yet */
1382 return;
1383 }
1384
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001385 map = find_map(state, unitid, control);
1386 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return;
1388
Takashi Iwai561b2202005-09-09 14:22:34 +02001389 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001390 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return;
Takashi Iwai3360b842014-11-18 11:47:04 +01001392 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 cval->control = control;
1394 cval->cmask = ctl_mask;
Julian Scheelbc18e312015-08-14 16:14:45 +02001395 ctl_info = &audio_feature_info[control-1];
1396 if (state->mixer->protocol == UAC_VERSION_1)
1397 cval->val_type = ctl_info->type;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301398 else /* UAC_VERSION_2 or UAC_VERSION_3*/
Julian Scheelbc18e312015-08-14 16:14:45 +02001399 cval->val_type = ctl_info->type_uac2 >= 0 ?
1400 ctl_info->type_uac2 : ctl_info->type;
1401
Daniel Macka6a33252010-05-31 13:35:37 +02001402 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001404 cval->master_readonly = readonly_mask;
1405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int i, c = 0;
1407 for (i = 0; i < 16; i++)
1408 if (ctl_mask & (1 << i))
1409 c++;
1410 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001411 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413
Daniel Mack6bc170e2014-05-24 10:58:16 +02001414 /*
1415 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001416 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001417 * issue write commands to read-only channels.
1418 */
Daniel Macka6a33252010-05-31 13:35:37 +02001419 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001420 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1421 else
1422 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1423
Daniel Mack6bc170e2014-05-24 10:58:16 +02001424 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001425 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 kfree(cval);
1427 return;
1428 }
Chris J Argeseef90452014-11-12 12:07:01 -06001429 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001431 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001433 if (!len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001434 len = snd_usb_copy_string_desc(state, nameid,
1435 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001438 case UAC_FU_MUTE:
1439 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001440 /*
1441 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * - if a name id is given in descriptor, use it.
1443 * - if the connected input can be determined, then use the name
1444 * of terminal type.
1445 * - if the connected output can be determined, use it.
1446 * - otherwise, anonymous name.
1447 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001448 if (!len) {
1449 len = get_term_name(state, iterm, kctl->id.name,
1450 sizeof(kctl->id.name), 1);
1451 if (!len)
1452 len = get_term_name(state, &state->oterm,
1453 kctl->id.name,
1454 sizeof(kctl->id.name), 1);
1455 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001456 snprintf(kctl->id.name, sizeof(kctl->id.name),
1457 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001459
1460 if (!mapped_name)
1461 check_no_speaker_on_headset(kctl, state->mixer->chip->card);
1462
Daniel Mack6bc170e2014-05-24 10:58:16 +02001463 /*
1464 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 * if the connected output is USB stream, then it's likely a
1466 * capture stream. otherwise it should be playback (hopefully :)
1467 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001468 if (!mapped_name && !(state->oterm.type >> 16)) {
1469 if ((state->oterm.type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001470 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001471 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001472 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001474 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001475 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001478 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1480 sizeof(kctl->id.name));
1481 break;
1482 }
1483
Takashi Iwaie1825342012-05-14 17:11:06 +02001484 /* get min/max values */
1485 get_min_max_with_quirks(cval, 0, kctl);
1486
1487 if (control == UAC_FU_VOLUME) {
1488 check_mapped_dB(map, cval);
1489 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1490 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1491 kctl->vd[0].access |=
1492 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1493 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1494 }
1495 }
1496
Anssi Hannula42e31212015-12-13 20:49:58 +02001497 snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
1498
Alexey Fisher80aceff2011-03-10 14:53:38 +01001499 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001500 /*
1501 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001502 * to be sure. 384 is a resolution magic number found on Logitech
1503 * devices. It will definitively catch all buggy Logitech devices.
1504 */
1505 if (range > 384) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001506 usb_audio_warn(state->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001507 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001508 range);
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001509 usb_audio_warn(state->chip,
1510 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001511 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001512 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001513 }
1514
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001515 usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001516 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001517 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001518 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Daniel Mackcddaafb2016-04-09 11:21:46 +02001521static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1522 void *_ftr)
1523{
1524 struct uac_clock_source_descriptor *hdr = _ftr;
1525 struct usb_mixer_elem_info *cval;
1526 struct snd_kcontrol *kctl;
1527 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1528 int ret;
1529
1530 if (state->mixer->protocol != UAC_VERSION_2)
1531 return -EINVAL;
1532
1533 if (hdr->bLength != sizeof(*hdr)) {
1534 usb_audio_dbg(state->chip,
1535 "Bogus clock source descriptor length of %d, ignoring.\n",
1536 hdr->bLength);
1537 return 0;
1538 }
1539
1540 /*
1541 * The only property of this unit we are interested in is the
1542 * clock source validity. If that isn't readable, just bail out.
1543 */
1544 if (!uac2_control_is_readable(hdr->bmControls,
1545 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1546 return 0;
1547
1548 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1549 if (!cval)
1550 return -ENOMEM;
1551
1552 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1553
1554 cval->min = 0;
1555 cval->max = 1;
1556 cval->channels = 1;
1557 cval->val_type = USB_MIXER_BOOLEAN;
1558 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1559
1560 if (uac2_control_is_writeable(hdr->bmControls,
1561 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1562 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1563 else {
1564 cval->master_readonly = 1;
1565 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1566 }
1567
1568 if (!kctl) {
1569 kfree(cval);
1570 return -ENOMEM;
1571 }
1572
1573 kctl->private_free = snd_usb_mixer_elem_free;
1574 ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
1575 name, sizeof(name));
1576 if (ret > 0)
1577 snprintf(kctl->id.name, sizeof(kctl->id.name),
1578 "%s Validity", name);
1579 else
1580 snprintf(kctl->id.name, sizeof(kctl->id.name),
1581 "Clock Source %d Validity", hdr->bClockID);
1582
1583 return snd_usb_mixer_add_control(&cval->head, kctl);
1584}
1585
Ajay Agarwal045e3742017-02-07 18:41:34 +05301586static int find_num_channels(struct mixer_build *state, int dir)
1587{
1588 int num_ch = -EINVAL, num, i, j, wMaxPacketSize;
1589 int ctrlif = get_iface_desc(state->mixer->hostif)->bInterfaceNumber;
1590 struct usb_interface *usb_iface =
1591 usb_ifnum_to_if(state->mixer->chip->dev, ctrlif);
1592 struct usb_interface_assoc_descriptor *assoc = usb_iface->intf_assoc;
1593 struct usb_host_interface *alts;
1594
1595 for (i = 0; i < assoc->bInterfaceCount; i++) {
1596 int intf = assoc->bFirstInterface + i;
1597
1598 if (intf != ctrlif) {
1599 struct usb_interface *iface =
1600 usb_ifnum_to_if(state->mixer->chip->dev, intf);
1601
1602 alts = &iface->altsetting[1];
1603 if (dir == USB_DIR_OUT &&
1604 get_endpoint(alts, 0)->bEndpointAddress &
1605 USB_DIR_IN)
1606 continue;
1607 if (dir == USB_DIR_IN &&
1608 !(get_endpoint(alts, 0)->bEndpointAddress &
1609 USB_DIR_IN))
1610 continue;
1611 num = iface->num_altsetting;
1612 for (j = 1; j < num; j++) {
1613 num_ch = NUM_CHANNELS_MONO;
1614 alts = &iface->altsetting[j];
1615 wMaxPacketSize = le16_to_cpu(
1616 get_endpoint(alts, 0)->
1617 wMaxPacketSize);
1618 switch (wMaxPacketSize) {
1619 case BADD_MAXPSIZE_SYNC_MONO_16:
1620 case BADD_MAXPSIZE_SYNC_MONO_24:
1621 case BADD_MAXPSIZE_ASYNC_MONO_16:
1622 case BADD_MAXPSIZE_ASYNC_MONO_24:
1623 break;
1624 case BADD_MAXPSIZE_SYNC_STEREO_16:
1625 case BADD_MAXPSIZE_SYNC_STEREO_24:
1626 case BADD_MAXPSIZE_ASYNC_STEREO_16:
1627 case BADD_MAXPSIZE_ASYNC_STEREO_24:
1628 num_ch = NUM_CHANNELS_STEREO;
1629 break;
1630 }
1631 if (num_ch == NUM_CHANNELS_MONO)
1632 continue;
1633 else
1634 break;
1635 }
1636 }
1637 }
1638
1639 return num_ch;
1640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/*
1643 * parse a feature unit
1644 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001645 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001647static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1648 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001651 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 unsigned int master_bits, first_ch_bits;
1653 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001654 struct uac_feature_unit_descriptor *hdr = _ftr;
1655 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Daniel Mack23caaf12010-03-11 21:13:25 +01001657 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwai85cc0202017-11-21 16:55:51 +01001658 if (hdr->bLength < 7) {
1659 usb_audio_err(state->chip,
1660 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1661 unitid);
1662 return -EINVAL;
1663 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001664 csize = hdr->bControlSize;
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001665 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001666 usb_audio_dbg(state->chip,
1667 "unit %u: invalid bControlSize == 0\n",
1668 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001669 return -EINVAL;
1670 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001671 channels = (hdr->bLength - 7) / csize - 1;
1672 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001673 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001674 usb_audio_err(state->chip,
1675 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1676 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001677 return -EINVAL;
1678 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301679 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001680 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwai85cc0202017-11-21 16:55:51 +01001681 if (hdr->bLength < 6) {
1682 usb_audio_err(state->chip,
1683 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1684 unitid);
1685 return -EINVAL;
1686 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001687 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001688 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001689 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001690 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001691 usb_audio_err(state->chip,
1692 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1693 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001694 return -EINVAL;
1695 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301696 } else {
1697 struct usb_interface *usb_iface =
1698 usb_ifnum_to_if(state->mixer->chip->dev,
1699 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1700 struct usb_interface_assoc_descriptor *assoc =
1701 usb_iface->intf_assoc;
1702
1703 csize = 4;
1704 switch (unitid) {
1705 case BADD_FU_ID_BAIOF:
1706 channels = NUM_CHANNELS_MONO;
1707 bmaControls = monoControls;
1708 badd_baif_in_term_desc.wClusterDescrID =
1709 CLUSTER_ID_MONO;
1710 break;
1711
1712 case BADD_FU_ID_BAOF:
1713 switch (assoc->bFunctionSubClass) {
1714 case PROF_HEADPHONE:
1715 case PROF_HEADSET_ADAPTER:
1716 channels = NUM_CHANNELS_STEREO;
1717 bmaControls = stereoControls;
1718 badd_baiof_mu_desc.wClusterDescrID =
1719 CLUSTER_ID_MONO;
1720 break;
1721 case PROF_SPEAKERPHONE:
1722 channels = NUM_CHANNELS_MONO;
1723 bmaControls = monoControls;
1724 badd_baof_in_term_desc.wClusterDescrID =
1725 CLUSTER_ID_MONO;
1726 break;
1727 default:
1728 channels = find_num_channels(state,
1729 USB_DIR_OUT);
1730 if (channels < 0) {
1731 usb_audio_err(state->chip,
1732 "unit %u: Cant find num of channels\n",
1733 unitid);
1734 return channels;
1735 }
1736
1737 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1738 monoControls : stereoControls;
1739 badd_baof_in_term_desc.wClusterDescrID =
1740 (channels == NUM_CHANNELS_MONO) ?
1741 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1742 break;
1743 }
1744 break;
1745
1746 case BADD_FU_ID_BAIF:
1747 switch (assoc->bFunctionSubClass) {
1748 case PROF_HEADSET:
1749 case PROF_HEADSET_ADAPTER:
1750 case PROF_SPEAKERPHONE:
1751 channels = NUM_CHANNELS_MONO;
1752 bmaControls = monoControls;
1753 badd_baif_in_term_desc.wClusterDescrID =
1754 CLUSTER_ID_MONO;
1755 break;
1756 default:
1757 channels = find_num_channels(state, USB_DIR_IN);
1758 if (channels < 0) {
1759 usb_audio_err(state->chip,
1760 "unit %u: Cant find num of channels\n",
1761 unitid);
1762 return channels;
1763 }
1764
1765 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1766 monoControls : stereoControls;
1767 badd_baif_in_term_desc.wClusterDescrID =
1768 (channels == NUM_CHANNELS_MONO) ?
1769 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1770 break;
1771 }
1772 break;
1773
1774 default:
1775 usb_audio_err(state->chip, "Invalid unit %u\n", unitid);
1776 return -EINVAL;
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779
1780 /* parse the source unit */
Ajay Agarwal045e3742017-02-07 18:41:34 +05301781 if (state->mixer->protocol != UAC_VERSION_3) {
1782 err = parse_audio_unit(state, hdr->bSourceID);
1783 if (err < 0)
1784 return err;
1785 } else {
1786 struct usb_interface *usb_iface =
1787 usb_ifnum_to_if(state->mixer->chip->dev,
1788 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1789 struct usb_interface_assoc_descriptor *assoc =
1790 usb_iface->intf_assoc;
1791
1792 switch (unitid) {
1793 case BADD_FU_ID_BAOF:
1794 switch (assoc->bFunctionSubClass) {
1795 case PROF_HEADSET:
1796 case PROF_HEADSET_ADAPTER:
1797 hdr->bSourceID = BADD_MU_ID_BAIOF;
1798 break;
1799 default:
1800 hdr->bSourceID = BADD_IN_TERM_ID_BAOF;
1801 break;
1802 }
1803 }
1804 err = parse_audio_unit(state, hdr->bSourceID);
1805 if (err < 0)
1806 return err;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001810 err = check_input_term(state, hdr->bSourceID, &iterm);
1811 if (err < 0)
1812 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Daniel Mack23caaf12010-03-11 21:13:25 +01001814 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001815 /* master configuration quirks */
1816 switch (state->chip->usb_id) {
1817 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001818 usb_audio_info(state->chip,
1819 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001820 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001821 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001822 break;
David Henningssonc1051432012-09-20 10:20:41 +02001823 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001824 usb_audio_info(state->chip,
1825 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001826 /* disable non-functional volume control */
1827 channels = 0;
1828 break;
1829
Javier Kohen0c3cee52009-11-17 15:36:13 +01001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001832 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 else
1834 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001835
1836 if (state->mixer->protocol == UAC_VERSION_1) {
1837 /* check all control types */
1838 for (i = 0; i < 10; i++) {
1839 unsigned int ch_bits = 0;
1840 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001841 unsigned int mask;
1842
1843 mask = snd_usb_combine_bytes(bmaControls +
1844 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001845 if (mask & (1 << i))
1846 ch_bits |= (1 << j);
1847 }
1848 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001849
1850 /*
1851 * The first channel must be set
1852 * (for ease of programming).
1853 */
1854 if (ch_bits & 1)
1855 build_feature_ctl(state, _ftr, ch_bits, i,
1856 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001857 if (master_bits & (1 << i))
Daniel Mack6bc170e2014-05-24 10:58:16 +02001858 build_feature_ctl(state, _ftr, 0, i, &iterm,
1859 unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301861 } else { /* UAC_VERSION_2 or UAC_VERSION_3*/
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001862 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001863 unsigned int ch_bits = 0;
1864 unsigned int ch_read_only = 0;
1865
1866 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001867 unsigned int mask;
1868
1869 mask = snd_usb_combine_bytes(bmaControls +
1870 csize * (j+1), csize);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001871 if (uac2_control_is_readable(mask, i)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001872 ch_bits |= (1 << j);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001873 if (!uac2_control_is_writeable(mask, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001874 ch_read_only |= (1 << j);
1875 }
1876 }
1877
Daniel Mack6bc170e2014-05-24 10:58:16 +02001878 /*
1879 * NOTE: build_feature_ctl() will mark the control
1880 * read-only if all channels are marked read-only in
1881 * the descriptors. Otherwise, the control will be
1882 * reported as writeable, but the driver will not
1883 * actually issue a write command for read-only
1884 * channels.
1885 */
1886
1887 /*
1888 * The first channel must be set
1889 * (for ease of programming).
1890 */
1891 if (ch_bits & 1)
1892 build_feature_ctl(state, _ftr, ch_bits, i,
1893 &iterm, unitid, ch_read_only);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001894 if (uac2_control_is_readable(master_bits, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001895 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001896 !uac2_control_is_writeable(master_bits, i));
Daniel Mack23caaf12010-03-11 21:13:25 +01001897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
1900 return 0;
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
1904 * Mixer Unit
1905 */
1906
1907/*
1908 * build a mixer unit control
1909 *
1910 * the callbacks are identical with feature unit.
1911 * input channel number (zero based) is given in control field instead.
1912 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001913static void build_mixer_unit_ctl(struct mixer_build *state,
1914 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001916 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001918 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001919 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001921 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001922 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001924 map = find_map(state, unitid, 0);
1925 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return;
1927
Takashi Iwai561b2202005-09-09 14:22:34 +02001928 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001929 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return;
1931
Takashi Iwai3360b842014-11-18 11:47:04 +01001932 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 cval->control = in_ch + 1; /* based on 1 */
1934 cval->val_type = USB_MIXER_S16;
1935 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001936 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1937
1938 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 cval->cmask |= (1 << i);
1940 cval->channels++;
1941 }
1942 }
1943
1944 /* get min/max values */
1945 get_min_max(cval, 0);
1946
1947 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001948 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001949 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 kfree(cval);
1951 return;
1952 }
Chris J Argeseef90452014-11-12 12:07:01 -06001953 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001955 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02001956 if (!len)
1957 len = get_term_name(state, iterm, kctl->id.name,
1958 sizeof(kctl->id.name), 0);
1959 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001961 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001963 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001964 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
1965 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * parse a mixer unit
1970 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001971static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
1972 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Daniel Mack99fc8642010-03-11 21:13:24 +01001974 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001975 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int input_pins, num_ins, num_outs;
1977 int pin, ich, err;
1978
Ajay Agarwal045e3742017-02-07 18:41:34 +05301979 if (state->mixer->protocol == UAC_VERSION_3) {
1980 input_pins = badd_baiof_mu_desc.bNrInPins;
1981 num_outs =
1982 (badd_baiof_mu_desc.wClusterDescrID == CLUSTER_ID_MONO) ?
1983 NUM_CHANNELS_MONO : NUM_CHANNELS_STEREO;
1984 } else {
1985 input_pins = desc->bNrInPins;
1986 num_outs = uac_mixer_unit_bNrChannels(desc);
1987 if (desc->bLength < 11 || !input_pins || !num_outs) {
1988 usb_audio_err(state->chip,
1989 "invalid MIXER UNIT descriptor %d\n",
1990 unitid);
1991 return -EINVAL;
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 num_ins = 0;
1996 ich = 0;
1997 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001998 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002000 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002001 /* no bmControls field (e.g. Maya44) -> ignore */
2002 if (desc->bLength <= 10 + input_pins)
2003 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002004 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (err < 0)
2006 return err;
2007 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002008 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 int och, ich_has_controls = 0;
2010
Daniel Mack6bc170e2014-05-24 10:58:16 +02002011 for (och = 0; och < num_outs; och++) {
Ajay Agarwal045e3742017-02-07 18:41:34 +05302012 __u8 *c = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002013
Ajay Agarwal045e3742017-02-07 18:41:34 +05302014 if (state->mixer->protocol == UAC_VERSION_3)
2015 c =
2016 &(badd_baiof_mu_desc.bmMixerControls);
2017 else
2018 c = uac_mixer_unit_bmControls(desc,
2019 state->mixer->protocol);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002020 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 ich_has_controls = 1;
2022 break;
2023 }
2024 }
2025 if (ich_has_controls)
2026 build_mixer_unit_ctl(state, desc, pin, ich,
2027 unitid, &iterm);
2028 }
2029 }
2030 return 0;
2031}
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033/*
2034 * Processing Unit / Extension Unit
2035 */
2036
2037/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002038static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002041 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 int err, val;
2043
2044 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002045 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002047 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 val = get_relative_value(cval, val);
2050 ucontrol->value.integer.value[0] = val;
2051 return 0;
2052}
2053
2054/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002055static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002058 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 int val, oval, err;
2060
2061 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002062 if (err < 0)
2063 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 val = ucontrol->value.integer.value[0];
2065 val = get_abs_value(cval, val);
2066 if (val != oval) {
2067 set_cur_ctl_value(cval, cval->control << 8, val);
2068 return 1;
2069 }
2070 return 0;
2071}
2072
2073/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002074static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .name = "", /* will be filled later */
2077 .info = mixer_ctl_feature_info,
2078 .get = mixer_ctl_procunit_get,
2079 .put = mixer_ctl_procunit_put,
2080};
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082/*
2083 * predefined data for processing units
2084 */
2085struct procunit_value_info {
2086 int control;
2087 char *suffix;
2088 int val_type;
2089 int min_value;
2090};
2091
2092struct procunit_info {
2093 int type;
2094 char *name;
2095 struct procunit_value_info *values;
2096};
2097
2098static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002099 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2100 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 { 0 }
2102};
2103static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002104 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2105 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 { 0 }
2107};
2108static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002109 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2110 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 { 0 }
2112};
2113static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002114 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2115 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2116 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2117 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 { 0 }
2119};
2120static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002121 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2122 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2123 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2124 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 { 0 }
2126};
2127static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002128 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2129 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2130 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2131 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2132 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2133 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 { 0 }
2135};
2136
2137static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002138 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2139 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2140 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2141 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2142 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2143 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 { 0 },
2145};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002146/*
2147 * predefined data for extension units
2148 */
2149static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002150 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2151 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002152};
2153static struct procunit_value_info clock_source_xu_info[] = {
2154 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2155 { 0 }
2156};
2157static struct procunit_value_info spdif_format_xu_info[] = {
2158 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2159 { 0 }
2160};
2161static struct procunit_value_info soft_limit_xu_info[] = {
2162 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2163 { 0 }
2164};
2165static struct procunit_info extunits[] = {
2166 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2167 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2168 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2169 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2170 { 0 }
2171};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173/*
2174 * build a processing/extension unit
2175 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002176static int build_audio_procunit(struct mixer_build *state, int unitid,
2177 void *raw_desc, struct procunit_info *list,
2178 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Daniel Mack99fc8642010-03-11 21:13:24 +01002180 struct uac_processing_unit_descriptor *desc = raw_desc;
Takashi Iwaibeab6f32018-12-19 12:36:27 +01002181 int num_ins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002182 struct usb_mixer_elem_info *cval;
2183 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 int i, err, nameid, type, len;
2185 struct procunit_info *info;
2186 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002187 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 static struct procunit_value_info default_value_info[] = {
2189 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2190 { 0 }
2191 };
2192 static struct procunit_info default_info = {
2193 0, NULL, default_value_info
2194 };
2195
Takashi Iwaibeab6f32018-12-19 12:36:27 +01002196 if (desc->bLength < 13) {
2197 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2198 return -EINVAL;
2199 }
2200
2201 num_ins = desc->bNrInPins;
2202 if (desc->bLength < 13 + num_ins ||
Daniel Mack23caaf12010-03-11 21:13:25 +01002203 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002204 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return -EINVAL;
2206 }
2207
2208 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002209 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return err;
2211 }
2212
Daniel Mack99fc8642010-03-11 21:13:24 +01002213 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 for (info = list; info && info->type; info++)
2215 if (info->type == type)
2216 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002217 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 info = &default_info;
2219
2220 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002221 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002222
Daniel Mack6bc170e2014-05-24 10:58:16 +02002223 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002225 map = find_map(state, unitid, valinfo->control);
2226 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002228 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002229 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002231 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 cval->control = valinfo->control;
2233 cval->val_type = valinfo->val_type;
2234 cval->channels = 1;
2235
2236 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002237 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002238 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* FIXME: hard-coded */
2240 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002241 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 cval->res = 1;
2243 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002244 } else {
2245 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002246 /*
2247 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002248 * samplerate control quirk
2249 */
2250 cval->min = 0;
2251 cval->max = 5;
2252 cval->res = 1;
2253 cval->initialized = 1;
2254 } else
2255 get_min_max(cval, valinfo->min_value);
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002259 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 kfree(cval);
2261 return -ENOMEM;
2262 }
Chris J Argeseef90452014-11-12 12:07:01 -06002263 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Daniel Mack6bc170e2014-05-24 10:58:16 +02002265 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002266 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002267 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002269 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002270 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 len = 0;
2272 if (nameid)
Daniel Mack6bc170e2014-05-24 10:58:16 +02002273 len = snd_usb_copy_string_desc(state, nameid,
2274 kctl->id.name,
2275 sizeof(kctl->id.name));
2276 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2278 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002279 append_ctl_name(kctl, " ");
2280 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002282 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002283 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002284 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002285 cval->min, cval->max);
2286
Takashi Iwai3360b842014-11-18 11:47:04 +01002287 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002288 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return err;
2290 }
2291 return 0;
2292}
2293
Daniel Mack6bc170e2014-05-24 10:58:16 +02002294static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2295 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002297 return build_audio_procunit(state, unitid, raw_desc,
2298 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Daniel Mack6bc170e2014-05-24 10:58:16 +02002301static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2302 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002304 /*
2305 * Note that we parse extension units with processing unit descriptors.
2306 * That's ok as the layout is the same.
2307 */
2308 return build_audio_procunit(state, unitid, raw_desc,
2309 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
2313 * Selector Unit
2314 */
2315
Daniel Mack6bc170e2014-05-24 10:58:16 +02002316/*
2317 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 * use an enumerator type for routing
2319 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002320static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002323 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002324 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Takashi Iwai5e246b82008-08-08 17:12:47 +02002326 if (snd_BUG_ON(!itemlist))
2327 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002328 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
2331/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002332static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2333 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002335 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int val, err;
2337
Daniel Mack09414202010-05-31 13:35:44 +02002338 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002340 ucontrol->value.enumerated.item[0] = 0;
2341 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 val = get_relative_value(cval, val);
2344 ucontrol->value.enumerated.item[0] = val;
2345 return 0;
2346}
2347
2348/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002349static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002352 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int val, oval, err;
2354
Daniel Mack09414202010-05-31 13:35:44 +02002355 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002356 if (err < 0)
2357 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 val = ucontrol->value.enumerated.item[0];
2359 val = get_abs_value(cval, val);
2360 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002361 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return 1;
2363 }
2364 return 0;
2365}
2366
2367/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002368static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2370 .name = "", /* will be filled later */
2371 .info = mixer_ctl_selector_info,
2372 .get = mixer_ctl_selector_get,
2373 .put = mixer_ctl_selector_put,
2374};
2375
Daniel Mack6bc170e2014-05-24 10:58:16 +02002376/*
2377 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 * free both private_data and private_value
2379 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002380static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 int i, num_ins = 0;
2383
2384 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002385 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 num_ins = cval->max;
2387 kfree(cval);
2388 kctl->private_data = NULL;
2389 }
2390 if (kctl->private_value) {
2391 char **itemlist = (char **)kctl->private_value;
2392 for (i = 0; i < num_ins; i++)
2393 kfree(itemlist[i]);
2394 kfree(itemlist);
2395 kctl->private_value = 0;
2396 }
2397}
2398
2399/*
2400 * parse a selector unit
2401 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002402static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2403 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Daniel Mack99fc8642010-03-11 21:13:24 +01002405 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 unsigned int i, nameid, len;
2407 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002408 struct usb_mixer_elem_info *cval;
2409 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002410 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 char **namelist;
2412
Takashi Iwai23353222017-11-21 17:00:32 +01002413 if (desc->bLength < 5 || !desc->bNrInPins ||
2414 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002415 usb_audio_err(state->chip,
2416 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return -EINVAL;
2418 }
2419
Daniel Mack99fc8642010-03-11 21:13:24 +01002420 for (i = 0; i < desc->bNrInPins; i++) {
2421 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return err;
2423 }
2424
Daniel Mack99fc8642010-03-11 21:13:24 +01002425 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002428 map = find_map(state, unitid, 0);
2429 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return 0;
2431
Takashi Iwai561b2202005-09-09 14:22:34 +02002432 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002433 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002435 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 cval->val_type = USB_MIXER_U8;
2437 cval->channels = 1;
2438 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002439 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 cval->res = 1;
2441 cval->initialized = 1;
2442
Johan Rasténa358a0e2014-11-17 08:39:33 +01002443 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002444 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002445 else /* UAC_VERSION_2 */
2446 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2447 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002448
Daniel Mack99fc8642010-03-11 21:13:24 +01002449 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002450 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 kfree(cval);
2452 return -ENOMEM;
2453 }
2454#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002455 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002456 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 len = 0;
2458 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002459 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002460 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 kfree(namelist[i]);
2462 kfree(namelist);
2463 kfree(cval);
2464 return -ENOMEM;
2465 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002466 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2467 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002468 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
2470 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002471 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473
2474 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2475 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002476 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Wenwen Wangf934bf32019-04-27 01:06:46 -05002477 for (i = 0; i < desc->bNrInPins; i++)
2478 kfree(namelist[i]);
Jesper Juhl878b4782006-03-20 11:27:13 +01002479 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 kfree(cval);
2481 return -ENOMEM;
2482 }
2483 kctl->private_value = (unsigned long)namelist;
2484 kctl->private_free = usb_mixer_selector_elem_free;
2485
Takashi Iwai31760652017-12-18 23:36:57 +01002486 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002487 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim173c8c32017-12-04 15:31:49 +09002488 if (!len) {
Takashi Iwai31760652017-12-18 23:36:57 +01002489 /* no mapping ? */
2490 /* if iSelector is given, use it */
2491 nameid = uac_selector_unit_iSelector(desc);
2492 if (nameid)
2493 len = snd_usb_copy_string_desc(state, nameid,
2494 kctl->id.name,
2495 sizeof(kctl->id.name));
2496 /* ... or pick up the terminal name at next */
2497 if (!len)
2498 len = get_term_name(state, &state->oterm,
2499 kctl->id.name, sizeof(kctl->id.name), 0);
2500 /* ... or use the fixed string "USB" as the last resort */
2501 if (!len)
2502 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Takashi Iwai31760652017-12-18 23:36:57 +01002504 /* and add the proper suffix */
Daniel Mack09414202010-05-31 13:35:44 +02002505 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2506 append_ctl_name(kctl, " Clock Source");
2507 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002508 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002510 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002513 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002514 cval->head.id, kctl->id.name, desc->bNrInPins);
2515 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518/*
2519 * parse an audio unit recursively
2520 */
2521
Takashi Iwai86e07d32005-11-17 15:08:02 +01002522static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 unsigned char *p1;
2525
2526 if (test_and_set_bit(unitid, state->unitbitmap))
2527 return 0; /* the unit already visited */
2528
2529 p1 = find_audio_control_unit(state, unitid);
2530 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002531 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return -EINVAL;
2533 }
2534
2535 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01002536 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01002538 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackcddaafb2016-04-09 11:21:46 +02002540 case UAC2_CLOCK_SOURCE:
2541 return parse_clock_source_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002542 case UAC_SELECTOR_UNIT:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302543 /* UAC3_MIXER_UNIT_V3 has the same value */
Daniel Mack09414202010-05-31 13:35:44 +02002544 case UAC2_CLOCK_SELECTOR:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302545 /* UAC3_CLOCK_SOURCE has the same value */
2546 if (state->mixer->protocol == UAC_VERSION_3 &&
2547 p1[2] == UAC3_CLOCK_SOURCE)
2548 return 0; /* NOP */
2549 else if (state->mixer->protocol == UAC_VERSION_3
2550 && p1[2] == UAC3_MIXER_UNIT_V3)
2551 return parse_audio_mixer_unit(state, unitid, p1);
2552 else
2553 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002554 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002556 case UAC1_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002557 /* UAC2_EFFECT_UNIT has the same value */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302558 /* UAC3_FEATURE_UNIT_V3 has the same value */
Daniel Mack23caaf12010-03-11 21:13:25 +01002559 if (state->mixer->protocol == UAC_VERSION_1)
2560 return parse_audio_processing_unit(state, unitid, p1);
Ajay Agarwal045e3742017-02-07 18:41:34 +05302561 else if (state->mixer->protocol == UAC_VERSION_2)
Daniel Mack23caaf12010-03-11 21:13:25 +01002562 return 0; /* FIXME - effect units not implemented yet */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302563 else
2564 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002565 case UAC1_EXTENSION_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002566 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2567 if (state->mixer->protocol == UAC_VERSION_1)
2568 return parse_audio_extension_unit(state, unitid, p1);
2569 else /* UAC_VERSION_2 */
2570 return parse_audio_processing_unit(state, unitid, p1);
Torstein Hegge61ac5132013-03-19 17:12:14 +01002571 case UAC2_EXTENSION_UNIT_V2:
2572 return parse_audio_extension_unit(state, unitid, p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002574 usb_audio_err(state->chip,
2575 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -EINVAL;
2577 }
2578}
2579
Clemens Ladisch84957a82005-04-29 16:23:13 +02002580static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2581{
Takashi Iwaie0c70282017-10-10 14:10:32 +02002582 /* kill pending URBs */
2583 snd_usb_mixer_disconnect(mixer);
2584
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002585 kfree(mixer->id_elems);
2586 if (mixer->urb) {
2587 kfree(mixer->urb->transfer_buffer);
2588 usb_free_urb(mixer->urb);
2589 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002590 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002591 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002592 kfree(mixer);
2593}
2594
Takashi Iwai86e07d32005-11-17 15:08:02 +01002595static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002596{
2597 struct usb_mixer_interface *mixer = device->device_data;
2598 snd_usb_mixer_free(mixer);
2599 return 0;
2600}
2601
Ajay Agarwal045e3742017-02-07 18:41:34 +05302602static int make_out_term(struct mixer_build state, int wTerminalType)
2603{
2604 struct uac3_output_terminal_descriptor *desc = NULL;
2605
2606 if (wTerminalType == UAC_TERMINAL_STREAMING)
2607 desc = &badd_baif_out_term_desc;
2608 else {
2609 desc = &badd_baof_out_term_desc;
2610 desc->wTerminalType = wTerminalType;
2611 }
2612 set_bit(desc->bTerminalID, state.unitbitmap);
2613 state.oterm.id = desc->bTerminalID;
2614 state.oterm.type = desc->wTerminalType;
2615 state.oterm.name = desc->wTerminalDescrStr;
2616 return parse_audio_unit(&state, desc->bSourceID);
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/*
2620 * create mixer controls
2621 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002622 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002624static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002626 struct mixer_build state;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302627 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 const struct usbmix_ctl_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002631 state.chip = mixer->chip;
2632 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002633 state.buffer = mixer->hostif->extra;
2634 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002637 for (map = usbmix_ctl_maps; map->id; map++) {
2638 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002640 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002641 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 break;
2643 }
2644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Ajay Agarwal045e3742017-02-07 18:41:34 +05302646 if (mixer->protocol == UAC_VERSION_3) {
2647 struct usb_interface *usb_iface =
2648 usb_ifnum_to_if(mixer->chip->dev,
2649 get_iface_desc(mixer->hostif)->bInterfaceNumber);
2650 struct usb_interface_assoc_descriptor *assoc =
2651 usb_iface->intf_assoc;
Daniel Mack23caaf12010-03-11 21:13:25 +01002652
Ajay Agarwal045e3742017-02-07 18:41:34 +05302653 switch (assoc->bFunctionSubClass) {
2654 case PROF_GENERIC_IO: {
2655 if (assoc->bInterfaceCount == 0x02) {
2656 if (get_endpoint(mixer->hostif,
2657 0)->bEndpointAddress | USB_DIR_IN)
2658 err = make_out_term(state,
2659 UAC_TERMINAL_STREAMING);
2660 else
2661 err = make_out_term(state,
2662 UAC_OUTPUT_TERMINAL_UNDEFINED);
2663 } else {
2664 err = make_out_term(state,
2665 UAC_OUTPUT_TERMINAL_UNDEFINED);
2666 if (err < 0 && err != -EINVAL)
2667 return err;
2668 err = make_out_term(state,
2669 UAC_TERMINAL_STREAMING);
2670 }
2671 break;
2672 }
2673
2674 case PROF_HEADPHONE:
2675 err = make_out_term(state,
2676 UAC_OUTPUT_TERMINAL_HEADPHONES);
2677 break;
2678 case PROF_SPEAKER:
2679 err = make_out_term(state, UAC_OUTPUT_TERMINAL_SPEAKER);
2680 break;
2681 case PROF_MICROPHONE:
2682 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2683 break;
2684 case PROF_HEADSET:
2685 case PROF_HEADSET_ADAPTER:
2686 err = make_out_term(state, UAC_BIDIR_TERMINAL_HEADSET);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002687 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002688 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302689 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2690 break;
2691 case PROF_SPEAKERPHONE:
2692 err = make_out_term(state,
2693 UAC_BIDIR_TERMINAL_SPEAKERPHONE);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002694 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002695 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302696 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2697 break;
2698 }
2699 if (err < 0 && err != -EINVAL)
2700 return err;
2701 } else {
2702 void *p;
Daniel Mack09414202010-05-31 13:35:44 +02002703
Ajay Agarwal045e3742017-02-07 18:41:34 +05302704 p = NULL;
2705 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2706 mixer->hostif->extralen, p,
2707 UAC_OUTPUT_TERMINAL)) != NULL) {
2708 if (mixer->protocol == UAC_VERSION_1) {
2709 struct uac1_output_terminal_descriptor *desc =
2710 p;
2711
2712 if (desc->bLength < sizeof(*desc))
2713 continue; /* invalid descriptor? */
2714 /* mark terminal ID as visited */
2715 set_bit(desc->bTerminalID, state.unitbitmap);
2716 state.oterm.id = desc->bTerminalID;
2717 state.oterm.type =
2718 le16_to_cpu(desc->wTerminalType);
2719 state.oterm.name = desc->iTerminal;
2720 err = parse_audio_unit(&state, desc->bSourceID);
2721 if (err < 0 && err != -EINVAL)
2722 return err;
2723 } else { /* UAC_VERSION_2 */
2724 struct uac2_output_terminal_descriptor *desc =
2725 p;
2726
2727 if (desc->bLength < sizeof(*desc))
2728 continue; /* invalid descriptor? */
2729 /* mark terminal ID as visited */
2730 set_bit(desc->bTerminalID, state.unitbitmap);
2731 state.oterm.id = desc->bTerminalID;
2732 state.oterm.type =
2733 le16_to_cpu(desc->wTerminalType);
2734 state.oterm.name = desc->iTerminal;
2735 err = parse_audio_unit(&state, desc->bSourceID);
2736 if (err < 0 && err != -EINVAL)
2737 return err;
2738
2739 /*
2740 * For UAC2, use the same approach to also add
2741 * the clock selectors
2742 */
2743 err = parse_audio_unit(&state,
2744 desc->bCSourceID);
2745 if (err < 0 && err != -EINVAL)
2746 return err;
2747 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return 0;
2752}
Clemens Ladisch84957a82005-04-29 16:23:13 +02002753
Daniel Mack7b1eda22010-03-11 21:13:22 +01002754void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002755{
Takashi Iwai3360b842014-11-18 11:47:04 +01002756 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002757
Takashi Iwai3360b842014-11-18 11:47:04 +01002758 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002759 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002760 &list->kctl->id);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002761}
2762
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002763static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01002764 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002765{
Takashi Iwai3360b842014-11-18 11:47:04 +01002766 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002767 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
2768 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002769 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01002770 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002771 cval->control, cval->cmask, cval->channels,
2772 val_types[cval->val_type]);
2773 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
2774 cval->min, cval->max, cval->dBmin, cval->dBmax);
2775}
2776
2777static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
2778 struct snd_info_buffer *buffer)
2779{
2780 struct snd_usb_audio *chip = entry->private_data;
2781 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01002782 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002783 int unitid;
2784
2785 list_for_each_entry(mixer, &chip->mixer_list, list) {
2786 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002787 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02002788 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002789 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002790 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
2791 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01002792 for (list = mixer->id_elems[unitid]; list;
2793 list = list->next_id_elem) {
2794 snd_iprintf(buffer, " Unit: %i\n", list->id);
2795 if (list->kctl)
2796 snd_iprintf(buffer,
2797 " Control: name=\"%s\", index=%i\n",
2798 list->kctl->id.name,
2799 list->kctl->id.index);
2800 if (list->dump)
2801 list->dump(buffer, list);
2802 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002803 }
2804 }
2805}
2806
Daniel Macke213e9c2010-05-11 18:13:50 +02002807static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
2808 int attribute, int value, int index)
2809{
Takashi Iwai3360b842014-11-18 11:47:04 +01002810 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02002811 __u8 unitid = (index >> 8) & 0xff;
2812 __u8 control = (value >> 8) & 0xff;
2813 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02002814 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02002815
2816 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002817 usb_audio_dbg(mixer->chip,
2818 "%s(): bogus channel number %d\n",
2819 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02002820 return;
2821 }
2822
Daniel Mack191227d2016-04-08 19:52:02 +02002823 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
2824 count++;
2825
2826 if (count == 0)
2827 return;
2828
Takashi Iwai3360b842014-11-18 11:47:04 +01002829 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
2830 struct usb_mixer_elem_info *info;
2831
2832 if (!list->kctl)
2833 continue;
2834
2835 info = (struct usb_mixer_elem_info *)list;
Daniel Mack191227d2016-04-08 19:52:02 +02002836 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02002837 continue;
2838
2839 switch (attribute) {
2840 case UAC2_CS_CUR:
2841 /* invalidate cache, so the value is read from the device */
2842 if (channel)
2843 info->cached &= ~(1 << channel);
2844 else /* master channel */
2845 info->cached = 0;
2846
2847 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002848 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02002849 break;
2850
2851 case UAC2_CS_RANGE:
2852 /* TODO */
2853 break;
2854
2855 case UAC2_CS_MEM:
2856 /* TODO */
2857 break;
2858
2859 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002860 usb_audio_dbg(mixer->chip,
2861 "unknown attribute %d in interrupt\n",
2862 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02002863 break;
2864 } /* switch */
2865 }
2866}
2867
2868static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002869{
2870 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02002871 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01002872 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002873
Oliver Neukumedf7de32011-03-11 13:19:43 +01002874 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02002875 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002876
Daniel Macke213e9c2010-05-11 18:13:50 +02002877 if (mixer->protocol == UAC_VERSION_1) {
2878 struct uac1_status_word *status;
2879
2880 for (status = urb->transfer_buffer;
2881 len >= sizeof(*status);
2882 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002883 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02002884 status->bStatusType,
2885 status->bOriginator);
2886
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002887 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02002888 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2889 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002890 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02002891
2892 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
2893 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02002894 else
Daniel Macke213e9c2010-05-11 18:13:50 +02002895 snd_usb_mixer_notify_id(mixer, status->bOriginator);
2896 }
2897 } else { /* UAC_VERSION_2 */
2898 struct uac2_interrupt_data_msg *msg;
2899
2900 for (msg = urb->transfer_buffer;
2901 len >= sizeof(*msg);
2902 len -= sizeof(*msg), msg++) {
2903 /* drop vendor specific and endpoint requests */
2904 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
2905 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
2906 continue;
2907
2908 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
2909 le16_to_cpu(msg->wValue),
2910 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002911 }
2912 }
Daniel Macke213e9c2010-05-11 18:13:50 +02002913
2914requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02002915 if (ustatus != -ENOENT &&
2916 ustatus != -ECONNRESET &&
2917 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002918 urb->dev = mixer->chip->dev;
2919 usb_submit_urb(urb, GFP_ATOMIC);
2920 }
2921}
2922
2923/* create the handler for the optional status interrupt endpoint */
2924static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2925{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002926 struct usb_endpoint_descriptor *ep;
2927 void *transfer_buffer;
2928 int buffer_length;
2929 unsigned int epnum;
2930
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002931 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02002932 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002933 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002934 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002935 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002936 return 0;
2937
Julia Lawall42a6e662008-12-29 11:23:02 +01002938 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002939 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
2940 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
2941 if (!transfer_buffer)
2942 return -ENOMEM;
2943 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2944 if (!mixer->urb) {
2945 kfree(transfer_buffer);
2946 return -ENOMEM;
2947 }
2948 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
2949 usb_rcvintpipe(mixer->chip->dev, epnum),
2950 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02002951 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002952 usb_submit_urb(mixer->urb, GFP_KERNEL);
2953 return 0;
2954}
2955
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002956int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2957 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002958{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002959 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002960 .dev_free = snd_usb_mixer_dev_free
2961 };
2962 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002963 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01002964 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002965
2966 strcpy(chip->card->mixername, "USB Mixer");
2967
Takashi Iwai561b2202005-09-09 14:22:34 +02002968 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002969 if (!mixer)
2970 return -ENOMEM;
2971 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002972 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002973 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2974 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002975 if (!mixer->id_elems) {
2976 kfree(mixer);
2977 return -ENOMEM;
2978 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002979
Daniel Mack1faa5d02011-08-04 15:56:28 +02002980 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2981 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02002982 case UAC_VERSION_1:
2983 default:
2984 mixer->protocol = UAC_VERSION_1;
2985 break;
2986 case UAC_VERSION_2:
2987 mixer->protocol = UAC_VERSION_2;
2988 break;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302989 case UAC_VERSION_3:
2990 mixer->protocol = UAC_VERSION_3;
2991 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02002992 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01002993
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002994 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002995 (err = snd_usb_mixer_status_create(mixer)) < 0)
2996 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002997
Takashi Iwaiebb1de52019-04-24 13:00:03 +02002998 err = snd_usb_mixer_apply_create_quirk(mixer);
2999 if (err < 0)
3000 goto _error;
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003001
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003002 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003003 if (err < 0)
3004 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003005
3006 if (list_empty(&chip->mixer_list) &&
3007 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3008 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3009
Clemens Ladisch84957a82005-04-29 16:23:13 +02003010 list_add(&mixer->list, &chip->mixer_list);
3011 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003012
3013_error:
3014 snd_usb_mixer_free(mixer);
3015 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003016}
3017
Takashi Iwaia6cece92014-10-31 11:24:32 +01003018void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003019{
Takashi Iwaie0c70282017-10-10 14:10:32 +02003020 if (mixer->disconnected)
3021 return;
3022 if (mixer->urb)
3023 usb_kill_urb(mixer->urb);
3024 if (mixer->rc_urb)
3025 usb_kill_urb(mixer->rc_urb);
3026 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003027}
Takashi Iwai400362f2014-01-20 16:51:16 +01003028
3029#ifdef CONFIG_PM
3030/* stop any bus activity of a mixer */
3031static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3032{
3033 usb_kill_urb(mixer->urb);
3034 usb_kill_urb(mixer->rc_urb);
3035}
3036
3037static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3038{
3039 int err;
3040
3041 if (mixer->urb) {
3042 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3043 if (err < 0)
3044 return err;
3045 }
3046
3047 return 0;
3048}
3049
3050int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3051{
3052 snd_usb_mixer_inactivate(mixer);
3053 return 0;
3054}
3055
Takashi Iwai3360b842014-11-18 11:47:04 +01003056static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003057{
Takashi Iwai3360b842014-11-18 11:47:04 +01003058 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003059 int c, err, idx;
3060
3061 if (cval->cmask) {
3062 idx = 0;
3063 for (c = 0; c < MAX_CHANNELS; c++) {
3064 if (!(cval->cmask & (1 << c)))
3065 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003066 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003067 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003068 cval->cache_val[idx]);
3069 if (err < 0)
3070 return err;
3071 }
3072 idx++;
3073 }
3074 } else {
3075 /* master */
3076 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003077 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003078 if (err < 0)
3079 return err;
3080 }
3081 }
3082
3083 return 0;
3084}
3085
3086int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3087{
Takashi Iwai3360b842014-11-18 11:47:04 +01003088 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003089 int id, err;
3090
Takashi Iwai400362f2014-01-20 16:51:16 +01003091 if (reset_resume) {
3092 /* restore cached mixer values */
3093 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003094 for (list = mixer->id_elems[id]; list;
3095 list = list->next_id_elem) {
3096 if (list->resume) {
3097 err = list->resume(list);
3098 if (err < 0)
3099 return err;
3100 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003101 }
3102 }
3103 }
3104
3105 return snd_usb_mixer_activate(mixer);
3106}
3107#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003108
3109void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3110 struct usb_mixer_interface *mixer,
3111 int unitid)
3112{
3113 list->mixer = mixer;
3114 list->id = unitid;
3115 list->dump = snd_usb_mixer_dump_cval;
3116#ifdef CONFIG_PM
3117 list->resume = restore_mixer_value;
3118#endif
3119}