blob: 63ed4793885e9d5998bb10f059047cb0afe3ddac [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 Iwai641b4872009-01-15 17:05:24 +01001140 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001141 for (;;) {
1142 test = saved;
1143 if (test < cval->max)
1144 test += cval->res;
1145 else
1146 test -= cval->res;
1147 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001148 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001149 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001150 cval->res = last_valid_res;
1151 break;
1152 }
1153 if (test == check)
1154 break;
1155 cval->res *= 2;
1156 }
Chris J Argeseef90452014-11-12 12:07:01 -06001157 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 cval->initialized = 1;
1161 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001162
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001163 if (kctl)
1164 volume_control_quirks(cval, kctl);
1165
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001166 /* USB descriptions contain the dB scale in 1/256 dB unit
1167 * while ALSA TLV contains in 1/100 dB unit
1168 */
Deepa Madiregama8b7f7fe2013-08-21 17:54:08 +05301169 cval->dBmin =
1170 (convert_signed_value(cval, cval->min) * 100) / (cval->res);
1171 cval->dBmax =
1172 (convert_signed_value(cval, cval->max) * 100) / (cval->res);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001173 if (cval->dBmin > cval->dBmax) {
1174 /* something is wrong; assume it's either from/to 0dB */
1175 if (cval->dBmin < 0)
1176 cval->dBmax = 0;
1177 else if (cval->dBmin > 0)
1178 cval->dBmin = 0;
1179 if (cval->dBmin > cval->dBmax) {
1180 /* totally crap, return an error */
1181 return -EINVAL;
1182 }
1183 }
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
1186}
1187
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001188#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001191static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1192 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001194 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (cval->val_type == USB_MIXER_BOOLEAN ||
1197 cval->val_type == USB_MIXER_INV_BOOLEAN)
1198 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1199 else
1200 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1201 uinfo->count = cval->channels;
1202 if (cval->val_type == USB_MIXER_BOOLEAN ||
1203 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1204 uinfo->value.integer.min = 0;
1205 uinfo->value.integer.max = 1;
1206 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001207 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001208 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001209 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1210 kcontrol->vd[0].access &=
1211 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1212 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001213 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001214 SNDRV_CTL_EVENT_MASK_INFO,
1215 &kcontrol->id);
1216 }
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001219 uinfo->value.integer.max =
1220 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 return 0;
1223}
1224
1225/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001226static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1227 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001229 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int c, cnt, val, err;
1231
Takashi Iwai641b4872009-01-15 17:05:24 +01001232 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (cval->cmask) {
1234 cnt = 0;
1235 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001236 if (!(cval->cmask & (1 << c)))
1237 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001238 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001239 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001240 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001241 val = get_relative_value(cval, val);
1242 ucontrol->value.integer.value[cnt] = val;
1243 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 } else {
1247 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001248 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001249 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001250 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 val = get_relative_value(cval, val);
1252 ucontrol->value.integer.value[0] = val;
1253 }
1254 return 0;
1255}
1256
1257/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001258static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1259 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001261 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int c, cnt, val, oval, err;
1263 int changed = 0;
1264
1265 if (cval->cmask) {
1266 cnt = 0;
1267 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001268 if (!(cval->cmask & (1 << c)))
1269 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001270 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001271 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001272 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001273 val = ucontrol->value.integer.value[cnt];
1274 val = get_abs_value(cval, val);
1275 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001276 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001277 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001279 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 } else {
1282 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001283 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001285 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 val = ucontrol->value.integer.value[0];
1287 val = get_abs_value(cval, val);
1288 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001289 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 changed = 1;
1291 }
1292 }
1293 return changed;
1294}
1295
Takashi Iwai86e07d32005-11-17 15:08:02 +01001296static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1298 .name = "", /* will be filled later manually */
1299 .info = mixer_ctl_feature_info,
1300 .get = mixer_ctl_feature_get,
1301 .put = mixer_ctl_feature_put,
1302};
1303
Daniel Mack23caaf12010-03-11 21:13:25 +01001304/* the read-only variant */
1305static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1306 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1307 .name = "", /* will be filled later manually */
1308 .info = mixer_ctl_feature_info,
1309 .get = mixer_ctl_feature_get,
1310 .put = NULL,
1311};
1312
Daniel Mack6bc170e2014-05-24 10:58:16 +02001313/*
1314 * This symbol is exported in order to allow the mixer quirks to
1315 * hook up to the standard feature unit control mechanism
1316 */
Daniel Mack9e386582011-05-25 09:09:01 +02001317struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319/*
1320 * build a feature control
1321 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001322static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1323{
1324 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1325}
1326
Daniel Mack6bc170e2014-05-24 10:58:16 +02001327/*
1328 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1329 * rename it to "Headphone". We determine if something is a headphone
1330 * similar to how udev determines form factor.
1331 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001332static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1333 struct snd_card *card)
1334{
1335 const char *names_to_check[] = {
1336 "Headset", "headset", "Headphone", "headphone", NULL};
1337 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001338 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001339
1340 if (strcmp("Speaker", kctl->id.name))
1341 return;
1342
1343 for (s = names_to_check; *s; s++)
1344 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001345 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001346 break;
1347 }
1348
1349 if (!found)
1350 return;
1351
1352 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1353}
1354
Daniel Mack99fc8642010-03-11 21:13:24 +01001355static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 unsigned int ctl_mask, int control,
Daniel Mack23caaf12010-03-11 21:13:25 +01001357 struct usb_audio_term *iterm, int unitid,
Daniel Macka6a33252010-05-31 13:35:37 +02001358 int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Daniel Mack99fc8642010-03-11 21:13:24 +01001360 struct uac_feature_unit_descriptor *desc = raw_desc;
Julian Scheelbc18e312015-08-14 16:14:45 +02001361 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 unsigned int len = 0;
1363 int mapped_name = 0;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301364 int nameid;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001365 struct snd_kcontrol *kctl;
1366 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001367 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001368 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Ajay Agarwal045e3742017-02-07 18:41:34 +05301370 if (state->mixer->protocol == UAC_VERSION_3)
1371 nameid = ((struct uac3_feature_unit_descriptor *)
1372 raw_desc)->wFeatureDescrStr;
1373 else
1374 nameid = uac_feature_unit_iFeature(desc);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 control++; /* change from zero-based to 1-based value */
1377
Daniel Mack65f25da2010-05-31 13:35:41 +02001378 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* FIXME: not supported yet */
1380 return;
1381 }
1382
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001383 map = find_map(state, unitid, control);
1384 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return;
1386
Takashi Iwai561b2202005-09-09 14:22:34 +02001387 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001388 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return;
Takashi Iwai3360b842014-11-18 11:47:04 +01001390 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 cval->control = control;
1392 cval->cmask = ctl_mask;
Julian Scheelbc18e312015-08-14 16:14:45 +02001393 ctl_info = &audio_feature_info[control-1];
1394 if (state->mixer->protocol == UAC_VERSION_1)
1395 cval->val_type = ctl_info->type;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301396 else /* UAC_VERSION_2 or UAC_VERSION_3*/
Julian Scheelbc18e312015-08-14 16:14:45 +02001397 cval->val_type = ctl_info->type_uac2 >= 0 ?
1398 ctl_info->type_uac2 : ctl_info->type;
1399
Daniel Macka6a33252010-05-31 13:35:37 +02001400 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001402 cval->master_readonly = readonly_mask;
1403 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int i, c = 0;
1405 for (i = 0; i < 16; i++)
1406 if (ctl_mask & (1 << i))
1407 c++;
1408 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001409 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
Daniel Mack6bc170e2014-05-24 10:58:16 +02001412 /*
1413 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001414 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001415 * issue write commands to read-only channels.
1416 */
Daniel Macka6a33252010-05-31 13:35:37 +02001417 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001418 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1419 else
1420 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1421
Daniel Mack6bc170e2014-05-24 10:58:16 +02001422 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001423 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 kfree(cval);
1425 return;
1426 }
Chris J Argeseef90452014-11-12 12:07:01 -06001427 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001429 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001431 if (!len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001432 len = snd_usb_copy_string_desc(state, nameid,
1433 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001436 case UAC_FU_MUTE:
1437 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001438 /*
1439 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * - if a name id is given in descriptor, use it.
1441 * - if the connected input can be determined, then use the name
1442 * of terminal type.
1443 * - if the connected output can be determined, use it.
1444 * - otherwise, anonymous name.
1445 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001446 if (!len) {
1447 len = get_term_name(state, iterm, kctl->id.name,
1448 sizeof(kctl->id.name), 1);
1449 if (!len)
1450 len = get_term_name(state, &state->oterm,
1451 kctl->id.name,
1452 sizeof(kctl->id.name), 1);
1453 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001454 snprintf(kctl->id.name, sizeof(kctl->id.name),
1455 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001457
1458 if (!mapped_name)
1459 check_no_speaker_on_headset(kctl, state->mixer->chip->card);
1460
Daniel Mack6bc170e2014-05-24 10:58:16 +02001461 /*
1462 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * if the connected output is USB stream, then it's likely a
1464 * capture stream. otherwise it should be playback (hopefully :)
1465 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001466 if (!mapped_name && !(state->oterm.type >> 16)) {
1467 if ((state->oterm.type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001468 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001469 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001470 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001472 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001473 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001476 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1478 sizeof(kctl->id.name));
1479 break;
1480 }
1481
Takashi Iwaie1825342012-05-14 17:11:06 +02001482 /* get min/max values */
1483 get_min_max_with_quirks(cval, 0, kctl);
1484
1485 if (control == UAC_FU_VOLUME) {
1486 check_mapped_dB(map, cval);
1487 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1488 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1489 kctl->vd[0].access |=
1490 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1491 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1492 }
1493 }
1494
Anssi Hannula42e31212015-12-13 20:49:58 +02001495 snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
1496
Alexey Fisher80aceff2011-03-10 14:53:38 +01001497 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001498 /*
1499 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001500 * to be sure. 384 is a resolution magic number found on Logitech
1501 * devices. It will definitively catch all buggy Logitech devices.
1502 */
1503 if (range > 384) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001504 usb_audio_warn(state->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001505 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001506 range);
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001507 usb_audio_warn(state->chip,
1508 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001509 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001510 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001511 }
1512
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001513 usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001514 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001515 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001516 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Daniel Mackcddaafb2016-04-09 11:21:46 +02001519static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1520 void *_ftr)
1521{
1522 struct uac_clock_source_descriptor *hdr = _ftr;
1523 struct usb_mixer_elem_info *cval;
1524 struct snd_kcontrol *kctl;
1525 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1526 int ret;
1527
1528 if (state->mixer->protocol != UAC_VERSION_2)
1529 return -EINVAL;
1530
1531 if (hdr->bLength != sizeof(*hdr)) {
1532 usb_audio_dbg(state->chip,
1533 "Bogus clock source descriptor length of %d, ignoring.\n",
1534 hdr->bLength);
1535 return 0;
1536 }
1537
1538 /*
1539 * The only property of this unit we are interested in is the
1540 * clock source validity. If that isn't readable, just bail out.
1541 */
1542 if (!uac2_control_is_readable(hdr->bmControls,
1543 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1544 return 0;
1545
1546 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1547 if (!cval)
1548 return -ENOMEM;
1549
1550 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1551
1552 cval->min = 0;
1553 cval->max = 1;
1554 cval->channels = 1;
1555 cval->val_type = USB_MIXER_BOOLEAN;
1556 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1557
1558 if (uac2_control_is_writeable(hdr->bmControls,
1559 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1560 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1561 else {
1562 cval->master_readonly = 1;
1563 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1564 }
1565
1566 if (!kctl) {
1567 kfree(cval);
1568 return -ENOMEM;
1569 }
1570
1571 kctl->private_free = snd_usb_mixer_elem_free;
1572 ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
1573 name, sizeof(name));
1574 if (ret > 0)
1575 snprintf(kctl->id.name, sizeof(kctl->id.name),
1576 "%s Validity", name);
1577 else
1578 snprintf(kctl->id.name, sizeof(kctl->id.name),
1579 "Clock Source %d Validity", hdr->bClockID);
1580
1581 return snd_usb_mixer_add_control(&cval->head, kctl);
1582}
1583
Ajay Agarwal045e3742017-02-07 18:41:34 +05301584static int find_num_channels(struct mixer_build *state, int dir)
1585{
1586 int num_ch = -EINVAL, num, i, j, wMaxPacketSize;
1587 int ctrlif = get_iface_desc(state->mixer->hostif)->bInterfaceNumber;
1588 struct usb_interface *usb_iface =
1589 usb_ifnum_to_if(state->mixer->chip->dev, ctrlif);
1590 struct usb_interface_assoc_descriptor *assoc = usb_iface->intf_assoc;
1591 struct usb_host_interface *alts;
1592
1593 for (i = 0; i < assoc->bInterfaceCount; i++) {
1594 int intf = assoc->bFirstInterface + i;
1595
1596 if (intf != ctrlif) {
1597 struct usb_interface *iface =
1598 usb_ifnum_to_if(state->mixer->chip->dev, intf);
1599
1600 alts = &iface->altsetting[1];
1601 if (dir == USB_DIR_OUT &&
1602 get_endpoint(alts, 0)->bEndpointAddress &
1603 USB_DIR_IN)
1604 continue;
1605 if (dir == USB_DIR_IN &&
1606 !(get_endpoint(alts, 0)->bEndpointAddress &
1607 USB_DIR_IN))
1608 continue;
1609 num = iface->num_altsetting;
1610 for (j = 1; j < num; j++) {
1611 num_ch = NUM_CHANNELS_MONO;
1612 alts = &iface->altsetting[j];
1613 wMaxPacketSize = le16_to_cpu(
1614 get_endpoint(alts, 0)->
1615 wMaxPacketSize);
1616 switch (wMaxPacketSize) {
1617 case BADD_MAXPSIZE_SYNC_MONO_16:
1618 case BADD_MAXPSIZE_SYNC_MONO_24:
1619 case BADD_MAXPSIZE_ASYNC_MONO_16:
1620 case BADD_MAXPSIZE_ASYNC_MONO_24:
1621 break;
1622 case BADD_MAXPSIZE_SYNC_STEREO_16:
1623 case BADD_MAXPSIZE_SYNC_STEREO_24:
1624 case BADD_MAXPSIZE_ASYNC_STEREO_16:
1625 case BADD_MAXPSIZE_ASYNC_STEREO_24:
1626 num_ch = NUM_CHANNELS_STEREO;
1627 break;
1628 }
1629 if (num_ch == NUM_CHANNELS_MONO)
1630 continue;
1631 else
1632 break;
1633 }
1634 }
1635 }
1636
1637 return num_ch;
1638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640/*
1641 * parse a feature unit
1642 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001643 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001645static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1646 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001649 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 unsigned int master_bits, first_ch_bits;
1651 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001652 struct uac_feature_unit_descriptor *hdr = _ftr;
1653 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Daniel Mack23caaf12010-03-11 21:13:25 +01001655 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwai85cc0202017-11-21 16:55:51 +01001656 if (hdr->bLength < 7) {
1657 usb_audio_err(state->chip,
1658 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1659 unitid);
1660 return -EINVAL;
1661 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001662 csize = hdr->bControlSize;
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001663 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001664 usb_audio_dbg(state->chip,
1665 "unit %u: invalid bControlSize == 0\n",
1666 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001667 return -EINVAL;
1668 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001669 channels = (hdr->bLength - 7) / csize - 1;
1670 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001671 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001672 usb_audio_err(state->chip,
1673 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1674 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001675 return -EINVAL;
1676 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301677 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001678 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwai85cc0202017-11-21 16:55:51 +01001679 if (hdr->bLength < 6) {
1680 usb_audio_err(state->chip,
1681 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1682 unitid);
1683 return -EINVAL;
1684 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001685 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001686 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001687 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001688 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001689 usb_audio_err(state->chip,
1690 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1691 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001692 return -EINVAL;
1693 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301694 } else {
1695 struct usb_interface *usb_iface =
1696 usb_ifnum_to_if(state->mixer->chip->dev,
1697 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1698 struct usb_interface_assoc_descriptor *assoc =
1699 usb_iface->intf_assoc;
1700
1701 csize = 4;
1702 switch (unitid) {
1703 case BADD_FU_ID_BAIOF:
1704 channels = NUM_CHANNELS_MONO;
1705 bmaControls = monoControls;
1706 badd_baif_in_term_desc.wClusterDescrID =
1707 CLUSTER_ID_MONO;
1708 break;
1709
1710 case BADD_FU_ID_BAOF:
1711 switch (assoc->bFunctionSubClass) {
1712 case PROF_HEADPHONE:
1713 case PROF_HEADSET_ADAPTER:
1714 channels = NUM_CHANNELS_STEREO;
1715 bmaControls = stereoControls;
1716 badd_baiof_mu_desc.wClusterDescrID =
1717 CLUSTER_ID_MONO;
1718 break;
1719 case PROF_SPEAKERPHONE:
1720 channels = NUM_CHANNELS_MONO;
1721 bmaControls = monoControls;
1722 badd_baof_in_term_desc.wClusterDescrID =
1723 CLUSTER_ID_MONO;
1724 break;
1725 default:
1726 channels = find_num_channels(state,
1727 USB_DIR_OUT);
1728 if (channels < 0) {
1729 usb_audio_err(state->chip,
1730 "unit %u: Cant find num of channels\n",
1731 unitid);
1732 return channels;
1733 }
1734
1735 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1736 monoControls : stereoControls;
1737 badd_baof_in_term_desc.wClusterDescrID =
1738 (channels == NUM_CHANNELS_MONO) ?
1739 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1740 break;
1741 }
1742 break;
1743
1744 case BADD_FU_ID_BAIF:
1745 switch (assoc->bFunctionSubClass) {
1746 case PROF_HEADSET:
1747 case PROF_HEADSET_ADAPTER:
1748 case PROF_SPEAKERPHONE:
1749 channels = NUM_CHANNELS_MONO;
1750 bmaControls = monoControls;
1751 badd_baif_in_term_desc.wClusterDescrID =
1752 CLUSTER_ID_MONO;
1753 break;
1754 default:
1755 channels = find_num_channels(state, USB_DIR_IN);
1756 if (channels < 0) {
1757 usb_audio_err(state->chip,
1758 "unit %u: Cant find num of channels\n",
1759 unitid);
1760 return channels;
1761 }
1762
1763 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1764 monoControls : stereoControls;
1765 badd_baif_in_term_desc.wClusterDescrID =
1766 (channels == NUM_CHANNELS_MONO) ?
1767 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1768 break;
1769 }
1770 break;
1771
1772 default:
1773 usb_audio_err(state->chip, "Invalid unit %u\n", unitid);
1774 return -EINVAL;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
1778 /* parse the source unit */
Ajay Agarwal045e3742017-02-07 18:41:34 +05301779 if (state->mixer->protocol != UAC_VERSION_3) {
1780 err = parse_audio_unit(state, hdr->bSourceID);
1781 if (err < 0)
1782 return err;
1783 } else {
1784 struct usb_interface *usb_iface =
1785 usb_ifnum_to_if(state->mixer->chip->dev,
1786 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1787 struct usb_interface_assoc_descriptor *assoc =
1788 usb_iface->intf_assoc;
1789
1790 switch (unitid) {
1791 case BADD_FU_ID_BAOF:
1792 switch (assoc->bFunctionSubClass) {
1793 case PROF_HEADSET:
1794 case PROF_HEADSET_ADAPTER:
1795 hdr->bSourceID = BADD_MU_ID_BAIOF;
1796 break;
1797 default:
1798 hdr->bSourceID = BADD_IN_TERM_ID_BAOF;
1799 break;
1800 }
1801 }
1802 err = parse_audio_unit(state, hdr->bSourceID);
1803 if (err < 0)
1804 return err;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001808 err = check_input_term(state, hdr->bSourceID, &iterm);
1809 if (err < 0)
1810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Daniel Mack23caaf12010-03-11 21:13:25 +01001812 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001813 /* master configuration quirks */
1814 switch (state->chip->usb_id) {
1815 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001816 usb_audio_info(state->chip,
1817 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001818 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001819 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001820 break;
David Henningssonc1051432012-09-20 10:20:41 +02001821 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001822 usb_audio_info(state->chip,
1823 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001824 /* disable non-functional volume control */
1825 channels = 0;
1826 break;
1827
Javier Kohen0c3cee52009-11-17 15:36:13 +01001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001830 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 else
1832 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001833
1834 if (state->mixer->protocol == UAC_VERSION_1) {
1835 /* check all control types */
1836 for (i = 0; i < 10; i++) {
1837 unsigned int ch_bits = 0;
1838 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001839 unsigned int mask;
1840
1841 mask = snd_usb_combine_bytes(bmaControls +
1842 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001843 if (mask & (1 << i))
1844 ch_bits |= (1 << j);
1845 }
1846 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001847
1848 /*
1849 * The first channel must be set
1850 * (for ease of programming).
1851 */
1852 if (ch_bits & 1)
1853 build_feature_ctl(state, _ftr, ch_bits, i,
1854 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001855 if (master_bits & (1 << i))
Daniel Mack6bc170e2014-05-24 10:58:16 +02001856 build_feature_ctl(state, _ftr, 0, i, &iterm,
1857 unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301859 } else { /* UAC_VERSION_2 or UAC_VERSION_3*/
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001860 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001861 unsigned int ch_bits = 0;
1862 unsigned int ch_read_only = 0;
1863
1864 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001865 unsigned int mask;
1866
1867 mask = snd_usb_combine_bytes(bmaControls +
1868 csize * (j+1), csize);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001869 if (uac2_control_is_readable(mask, i)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001870 ch_bits |= (1 << j);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001871 if (!uac2_control_is_writeable(mask, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001872 ch_read_only |= (1 << j);
1873 }
1874 }
1875
Daniel Mack6bc170e2014-05-24 10:58:16 +02001876 /*
1877 * NOTE: build_feature_ctl() will mark the control
1878 * read-only if all channels are marked read-only in
1879 * the descriptors. Otherwise, the control will be
1880 * reported as writeable, but the driver will not
1881 * actually issue a write command for read-only
1882 * channels.
1883 */
1884
1885 /*
1886 * The first channel must be set
1887 * (for ease of programming).
1888 */
1889 if (ch_bits & 1)
1890 build_feature_ctl(state, _ftr, ch_bits, i,
1891 &iterm, unitid, ch_read_only);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001892 if (uac2_control_is_readable(master_bits, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001893 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001894 !uac2_control_is_writeable(master_bits, i));
Daniel Mack23caaf12010-03-11 21:13:25 +01001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
1898 return 0;
1899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/*
1902 * Mixer Unit
1903 */
1904
1905/*
1906 * build a mixer unit control
1907 *
1908 * the callbacks are identical with feature unit.
1909 * input channel number (zero based) is given in control field instead.
1910 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001911static void build_mixer_unit_ctl(struct mixer_build *state,
1912 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001914 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001916 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001917 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001919 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001920 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001922 map = find_map(state, unitid, 0);
1923 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return;
1925
Takashi Iwai561b2202005-09-09 14:22:34 +02001926 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001927 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return;
1929
Takashi Iwai3360b842014-11-18 11:47:04 +01001930 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 cval->control = in_ch + 1; /* based on 1 */
1932 cval->val_type = USB_MIXER_S16;
1933 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001934 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1935
1936 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 cval->cmask |= (1 << i);
1938 cval->channels++;
1939 }
1940 }
1941
1942 /* get min/max values */
1943 get_min_max(cval, 0);
1944
1945 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001946 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001947 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 kfree(cval);
1949 return;
1950 }
Chris J Argeseef90452014-11-12 12:07:01 -06001951 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001953 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02001954 if (!len)
1955 len = get_term_name(state, iterm, kctl->id.name,
1956 sizeof(kctl->id.name), 0);
1957 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001959 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001961 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001962 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
1963 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * parse a mixer unit
1968 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001969static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
1970 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Daniel Mack99fc8642010-03-11 21:13:24 +01001972 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001973 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int input_pins, num_ins, num_outs;
1975 int pin, ich, err;
1976
Ajay Agarwal045e3742017-02-07 18:41:34 +05301977 if (state->mixer->protocol == UAC_VERSION_3) {
1978 input_pins = badd_baiof_mu_desc.bNrInPins;
1979 num_outs =
1980 (badd_baiof_mu_desc.wClusterDescrID == CLUSTER_ID_MONO) ?
1981 NUM_CHANNELS_MONO : NUM_CHANNELS_STEREO;
1982 } else {
1983 input_pins = desc->bNrInPins;
1984 num_outs = uac_mixer_unit_bNrChannels(desc);
1985 if (desc->bLength < 11 || !input_pins || !num_outs) {
1986 usb_audio_err(state->chip,
1987 "invalid MIXER UNIT descriptor %d\n",
1988 unitid);
1989 return -EINVAL;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 num_ins = 0;
1994 ich = 0;
1995 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001996 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01001998 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02001999 /* no bmControls field (e.g. Maya44) -> ignore */
2000 if (desc->bLength <= 10 + input_pins)
2001 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002002 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (err < 0)
2004 return err;
2005 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002006 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int och, ich_has_controls = 0;
2008
Daniel Mack6bc170e2014-05-24 10:58:16 +02002009 for (och = 0; och < num_outs; och++) {
Ajay Agarwal045e3742017-02-07 18:41:34 +05302010 __u8 *c = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002011
Ajay Agarwal045e3742017-02-07 18:41:34 +05302012 if (state->mixer->protocol == UAC_VERSION_3)
2013 c =
2014 &(badd_baiof_mu_desc.bmMixerControls);
2015 else
2016 c = uac_mixer_unit_bmControls(desc,
2017 state->mixer->protocol);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002018 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 ich_has_controls = 1;
2020 break;
2021 }
2022 }
2023 if (ich_has_controls)
2024 build_mixer_unit_ctl(state, desc, pin, ich,
2025 unitid, &iterm);
2026 }
2027 }
2028 return 0;
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/*
2032 * Processing Unit / Extension Unit
2033 */
2034
2035/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002036static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002039 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 int err, val;
2041
2042 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002043 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002045 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 val = get_relative_value(cval, val);
2048 ucontrol->value.integer.value[0] = val;
2049 return 0;
2050}
2051
2052/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002053static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2054 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002056 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int val, oval, err;
2058
2059 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002060 if (err < 0)
2061 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 val = ucontrol->value.integer.value[0];
2063 val = get_abs_value(cval, val);
2064 if (val != oval) {
2065 set_cur_ctl_value(cval, cval->control << 8, val);
2066 return 1;
2067 }
2068 return 0;
2069}
2070
2071/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002072static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2074 .name = "", /* will be filled later */
2075 .info = mixer_ctl_feature_info,
2076 .get = mixer_ctl_procunit_get,
2077 .put = mixer_ctl_procunit_put,
2078};
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080/*
2081 * predefined data for processing units
2082 */
2083struct procunit_value_info {
2084 int control;
2085 char *suffix;
2086 int val_type;
2087 int min_value;
2088};
2089
2090struct procunit_info {
2091 int type;
2092 char *name;
2093 struct procunit_value_info *values;
2094};
2095
2096static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002097 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2098 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 { 0 }
2100};
2101static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002102 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2103 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 { 0 }
2105};
2106static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002107 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2108 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 { 0 }
2110};
2111static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002112 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2113 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2114 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2115 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 { 0 }
2117};
2118static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002119 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2120 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2121 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2122 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 { 0 }
2124};
2125static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002126 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2127 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2128 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2129 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2130 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2131 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 { 0 }
2133};
2134
2135static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002136 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2137 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2138 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2139 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2140 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2141 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 { 0 },
2143};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002144/*
2145 * predefined data for extension units
2146 */
2147static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002148 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2149 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002150};
2151static struct procunit_value_info clock_source_xu_info[] = {
2152 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2153 { 0 }
2154};
2155static struct procunit_value_info spdif_format_xu_info[] = {
2156 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2157 { 0 }
2158};
2159static struct procunit_value_info soft_limit_xu_info[] = {
2160 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2161 { 0 }
2162};
2163static struct procunit_info extunits[] = {
2164 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2165 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2166 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2167 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2168 { 0 }
2169};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171/*
2172 * build a processing/extension unit
2173 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002174static int build_audio_procunit(struct mixer_build *state, int unitid,
2175 void *raw_desc, struct procunit_info *list,
2176 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Daniel Mack99fc8642010-03-11 21:13:24 +01002178 struct uac_processing_unit_descriptor *desc = raw_desc;
Takashi Iwaibeab6f32018-12-19 12:36:27 +01002179 int num_ins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002180 struct usb_mixer_elem_info *cval;
2181 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int i, err, nameid, type, len;
2183 struct procunit_info *info;
2184 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002185 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 static struct procunit_value_info default_value_info[] = {
2187 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2188 { 0 }
2189 };
2190 static struct procunit_info default_info = {
2191 0, NULL, default_value_info
2192 };
2193
Takashi Iwaibeab6f32018-12-19 12:36:27 +01002194 if (desc->bLength < 13) {
2195 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2196 return -EINVAL;
2197 }
2198
2199 num_ins = desc->bNrInPins;
2200 if (desc->bLength < 13 + num_ins ||
Daniel Mack23caaf12010-03-11 21:13:25 +01002201 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002202 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return -EINVAL;
2204 }
2205
2206 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002207 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return err;
2209 }
2210
Daniel Mack99fc8642010-03-11 21:13:24 +01002211 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 for (info = list; info && info->type; info++)
2213 if (info->type == type)
2214 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002215 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 info = &default_info;
2217
2218 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002219 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002220
Daniel Mack6bc170e2014-05-24 10:58:16 +02002221 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002223 map = find_map(state, unitid, valinfo->control);
2224 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002226 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002227 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002229 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 cval->control = valinfo->control;
2231 cval->val_type = valinfo->val_type;
2232 cval->channels = 1;
2233
2234 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002235 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002236 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* FIXME: hard-coded */
2238 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002239 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 cval->res = 1;
2241 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002242 } else {
2243 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002244 /*
2245 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002246 * samplerate control quirk
2247 */
2248 cval->min = 0;
2249 cval->max = 5;
2250 cval->res = 1;
2251 cval->initialized = 1;
2252 } else
2253 get_min_max(cval, valinfo->min_value);
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002257 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 kfree(cval);
2259 return -ENOMEM;
2260 }
Chris J Argeseef90452014-11-12 12:07:01 -06002261 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Daniel Mack6bc170e2014-05-24 10:58:16 +02002263 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002264 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002265 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002267 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002268 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 len = 0;
2270 if (nameid)
Daniel Mack6bc170e2014-05-24 10:58:16 +02002271 len = snd_usb_copy_string_desc(state, nameid,
2272 kctl->id.name,
2273 sizeof(kctl->id.name));
2274 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2276 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002277 append_ctl_name(kctl, " ");
2278 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002280 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002281 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002282 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002283 cval->min, cval->max);
2284
Takashi Iwai3360b842014-11-18 11:47:04 +01002285 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002286 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return err;
2288 }
2289 return 0;
2290}
2291
Daniel Mack6bc170e2014-05-24 10:58:16 +02002292static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2293 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002295 return build_audio_procunit(state, unitid, raw_desc,
2296 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Daniel Mack6bc170e2014-05-24 10:58:16 +02002299static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2300 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002302 /*
2303 * Note that we parse extension units with processing unit descriptors.
2304 * That's ok as the layout is the same.
2305 */
2306 return build_audio_procunit(state, unitid, raw_desc,
2307 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/*
2311 * Selector Unit
2312 */
2313
Daniel Mack6bc170e2014-05-24 10:58:16 +02002314/*
2315 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * use an enumerator type for routing
2317 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002318static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2319 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002321 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002322 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Takashi Iwai5e246b82008-08-08 17:12:47 +02002324 if (snd_BUG_ON(!itemlist))
2325 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002326 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
2329/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002330static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002333 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int val, err;
2335
Daniel Mack09414202010-05-31 13:35:44 +02002336 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002338 ucontrol->value.enumerated.item[0] = 0;
2339 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 val = get_relative_value(cval, val);
2342 ucontrol->value.enumerated.item[0] = val;
2343 return 0;
2344}
2345
2346/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002347static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2348 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002350 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int val, oval, err;
2352
Daniel Mack09414202010-05-31 13:35:44 +02002353 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002354 if (err < 0)
2355 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 val = ucontrol->value.enumerated.item[0];
2357 val = get_abs_value(cval, val);
2358 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002359 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return 1;
2361 }
2362 return 0;
2363}
2364
2365/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002366static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2368 .name = "", /* will be filled later */
2369 .info = mixer_ctl_selector_info,
2370 .get = mixer_ctl_selector_get,
2371 .put = mixer_ctl_selector_put,
2372};
2373
Daniel Mack6bc170e2014-05-24 10:58:16 +02002374/*
2375 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 * free both private_data and private_value
2377 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002378static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 int i, num_ins = 0;
2381
2382 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002383 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 num_ins = cval->max;
2385 kfree(cval);
2386 kctl->private_data = NULL;
2387 }
2388 if (kctl->private_value) {
2389 char **itemlist = (char **)kctl->private_value;
2390 for (i = 0; i < num_ins; i++)
2391 kfree(itemlist[i]);
2392 kfree(itemlist);
2393 kctl->private_value = 0;
2394 }
2395}
2396
2397/*
2398 * parse a selector unit
2399 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002400static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2401 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Daniel Mack99fc8642010-03-11 21:13:24 +01002403 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 unsigned int i, nameid, len;
2405 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002406 struct usb_mixer_elem_info *cval;
2407 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002408 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 char **namelist;
2410
Takashi Iwai23353222017-11-21 17:00:32 +01002411 if (desc->bLength < 5 || !desc->bNrInPins ||
2412 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002413 usb_audio_err(state->chip,
2414 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return -EINVAL;
2416 }
2417
Daniel Mack99fc8642010-03-11 21:13:24 +01002418 for (i = 0; i < desc->bNrInPins; i++) {
2419 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return err;
2421 }
2422
Daniel Mack99fc8642010-03-11 21:13:24 +01002423 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return 0;
2425
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002426 map = find_map(state, unitid, 0);
2427 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
2429
Takashi Iwai561b2202005-09-09 14:22:34 +02002430 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002431 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002433 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 cval->val_type = USB_MIXER_U8;
2435 cval->channels = 1;
2436 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002437 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 cval->res = 1;
2439 cval->initialized = 1;
2440
Johan Rasténa358a0e2014-11-17 08:39:33 +01002441 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002442 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002443 else /* UAC_VERSION_2 */
2444 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2445 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002446
Daniel Mack99fc8642010-03-11 21:13:24 +01002447 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002448 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 kfree(cval);
2450 return -ENOMEM;
2451 }
2452#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002453 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002454 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 len = 0;
2456 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002457 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002458 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 kfree(namelist[i]);
2460 kfree(namelist);
2461 kfree(cval);
2462 return -ENOMEM;
2463 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002464 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2465 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002466 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
2468 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002469 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471
2472 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2473 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002474 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Wenwen Wangf934bf32019-04-27 01:06:46 -05002475 for (i = 0; i < desc->bNrInPins; i++)
2476 kfree(namelist[i]);
Jesper Juhl878b4782006-03-20 11:27:13 +01002477 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 kfree(cval);
2479 return -ENOMEM;
2480 }
2481 kctl->private_value = (unsigned long)namelist;
2482 kctl->private_free = usb_mixer_selector_elem_free;
2483
Takashi Iwai31760652017-12-18 23:36:57 +01002484 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002485 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim173c8c32017-12-04 15:31:49 +09002486 if (!len) {
Takashi Iwai31760652017-12-18 23:36:57 +01002487 /* no mapping ? */
2488 /* if iSelector is given, use it */
2489 nameid = uac_selector_unit_iSelector(desc);
2490 if (nameid)
2491 len = snd_usb_copy_string_desc(state, nameid,
2492 kctl->id.name,
2493 sizeof(kctl->id.name));
2494 /* ... or pick up the terminal name at next */
2495 if (!len)
2496 len = get_term_name(state, &state->oterm,
2497 kctl->id.name, sizeof(kctl->id.name), 0);
2498 /* ... or use the fixed string "USB" as the last resort */
2499 if (!len)
2500 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Takashi Iwai31760652017-12-18 23:36:57 +01002502 /* and add the proper suffix */
Daniel Mack09414202010-05-31 13:35:44 +02002503 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2504 append_ctl_name(kctl, " Clock Source");
2505 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002506 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002508 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002511 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002512 cval->head.id, kctl->id.name, desc->bNrInPins);
2513 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516/*
2517 * parse an audio unit recursively
2518 */
2519
Takashi Iwai86e07d32005-11-17 15:08:02 +01002520static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 unsigned char *p1;
2523
2524 if (test_and_set_bit(unitid, state->unitbitmap))
2525 return 0; /* the unit already visited */
2526
2527 p1 = find_audio_control_unit(state, unitid);
2528 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002529 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return -EINVAL;
2531 }
2532
2533 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01002534 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01002536 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackcddaafb2016-04-09 11:21:46 +02002538 case UAC2_CLOCK_SOURCE:
2539 return parse_clock_source_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002540 case UAC_SELECTOR_UNIT:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302541 /* UAC3_MIXER_UNIT_V3 has the same value */
Daniel Mack09414202010-05-31 13:35:44 +02002542 case UAC2_CLOCK_SELECTOR:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302543 /* UAC3_CLOCK_SOURCE has the same value */
2544 if (state->mixer->protocol == UAC_VERSION_3 &&
2545 p1[2] == UAC3_CLOCK_SOURCE)
2546 return 0; /* NOP */
2547 else if (state->mixer->protocol == UAC_VERSION_3
2548 && p1[2] == UAC3_MIXER_UNIT_V3)
2549 return parse_audio_mixer_unit(state, unitid, p1);
2550 else
2551 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002552 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002554 case UAC1_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002555 /* UAC2_EFFECT_UNIT has the same value */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302556 /* UAC3_FEATURE_UNIT_V3 has the same value */
Daniel Mack23caaf12010-03-11 21:13:25 +01002557 if (state->mixer->protocol == UAC_VERSION_1)
2558 return parse_audio_processing_unit(state, unitid, p1);
Ajay Agarwal045e3742017-02-07 18:41:34 +05302559 else if (state->mixer->protocol == UAC_VERSION_2)
Daniel Mack23caaf12010-03-11 21:13:25 +01002560 return 0; /* FIXME - effect units not implemented yet */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302561 else
2562 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002563 case UAC1_EXTENSION_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002564 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2565 if (state->mixer->protocol == UAC_VERSION_1)
2566 return parse_audio_extension_unit(state, unitid, p1);
2567 else /* UAC_VERSION_2 */
2568 return parse_audio_processing_unit(state, unitid, p1);
Torstein Hegge61ac5132013-03-19 17:12:14 +01002569 case UAC2_EXTENSION_UNIT_V2:
2570 return parse_audio_extension_unit(state, unitid, p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002572 usb_audio_err(state->chip,
2573 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return -EINVAL;
2575 }
2576}
2577
Clemens Ladisch84957a82005-04-29 16:23:13 +02002578static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2579{
Takashi Iwaie0c70282017-10-10 14:10:32 +02002580 /* kill pending URBs */
2581 snd_usb_mixer_disconnect(mixer);
2582
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002583 kfree(mixer->id_elems);
2584 if (mixer->urb) {
2585 kfree(mixer->urb->transfer_buffer);
2586 usb_free_urb(mixer->urb);
2587 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002588 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002589 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002590 kfree(mixer);
2591}
2592
Takashi Iwai86e07d32005-11-17 15:08:02 +01002593static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002594{
2595 struct usb_mixer_interface *mixer = device->device_data;
2596 snd_usb_mixer_free(mixer);
2597 return 0;
2598}
2599
Ajay Agarwal045e3742017-02-07 18:41:34 +05302600static int make_out_term(struct mixer_build state, int wTerminalType)
2601{
2602 struct uac3_output_terminal_descriptor *desc = NULL;
2603
2604 if (wTerminalType == UAC_TERMINAL_STREAMING)
2605 desc = &badd_baif_out_term_desc;
2606 else {
2607 desc = &badd_baof_out_term_desc;
2608 desc->wTerminalType = wTerminalType;
2609 }
2610 set_bit(desc->bTerminalID, state.unitbitmap);
2611 state.oterm.id = desc->bTerminalID;
2612 state.oterm.type = desc->wTerminalType;
2613 state.oterm.name = desc->wTerminalDescrStr;
2614 return parse_audio_unit(&state, desc->bSourceID);
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617/*
2618 * create mixer controls
2619 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002620 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002622static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002624 struct mixer_build state;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302625 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 const struct usbmix_ctl_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002629 state.chip = mixer->chip;
2630 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002631 state.buffer = mixer->hostif->extra;
2632 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002635 for (map = usbmix_ctl_maps; map->id; map++) {
2636 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002638 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002639 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 break;
2641 }
2642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Ajay Agarwal045e3742017-02-07 18:41:34 +05302644 if (mixer->protocol == UAC_VERSION_3) {
2645 struct usb_interface *usb_iface =
2646 usb_ifnum_to_if(mixer->chip->dev,
2647 get_iface_desc(mixer->hostif)->bInterfaceNumber);
2648 struct usb_interface_assoc_descriptor *assoc =
2649 usb_iface->intf_assoc;
Daniel Mack23caaf12010-03-11 21:13:25 +01002650
Ajay Agarwal045e3742017-02-07 18:41:34 +05302651 switch (assoc->bFunctionSubClass) {
2652 case PROF_GENERIC_IO: {
2653 if (assoc->bInterfaceCount == 0x02) {
2654 if (get_endpoint(mixer->hostif,
2655 0)->bEndpointAddress | USB_DIR_IN)
2656 err = make_out_term(state,
2657 UAC_TERMINAL_STREAMING);
2658 else
2659 err = make_out_term(state,
2660 UAC_OUTPUT_TERMINAL_UNDEFINED);
2661 } else {
2662 err = make_out_term(state,
2663 UAC_OUTPUT_TERMINAL_UNDEFINED);
2664 if (err < 0 && err != -EINVAL)
2665 return err;
2666 err = make_out_term(state,
2667 UAC_TERMINAL_STREAMING);
2668 }
2669 break;
2670 }
2671
2672 case PROF_HEADPHONE:
2673 err = make_out_term(state,
2674 UAC_OUTPUT_TERMINAL_HEADPHONES);
2675 break;
2676 case PROF_SPEAKER:
2677 err = make_out_term(state, UAC_OUTPUT_TERMINAL_SPEAKER);
2678 break;
2679 case PROF_MICROPHONE:
2680 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2681 break;
2682 case PROF_HEADSET:
2683 case PROF_HEADSET_ADAPTER:
2684 err = make_out_term(state, UAC_BIDIR_TERMINAL_HEADSET);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002685 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002686 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302687 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2688 break;
2689 case PROF_SPEAKERPHONE:
2690 err = make_out_term(state,
2691 UAC_BIDIR_TERMINAL_SPEAKERPHONE);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002692 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002693 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302694 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2695 break;
2696 }
2697 if (err < 0 && err != -EINVAL)
2698 return err;
2699 } else {
2700 void *p;
Daniel Mack09414202010-05-31 13:35:44 +02002701
Ajay Agarwal045e3742017-02-07 18:41:34 +05302702 p = NULL;
2703 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2704 mixer->hostif->extralen, p,
2705 UAC_OUTPUT_TERMINAL)) != NULL) {
2706 if (mixer->protocol == UAC_VERSION_1) {
2707 struct uac1_output_terminal_descriptor *desc =
2708 p;
2709
2710 if (desc->bLength < sizeof(*desc))
2711 continue; /* invalid descriptor? */
2712 /* mark terminal ID as visited */
2713 set_bit(desc->bTerminalID, state.unitbitmap);
2714 state.oterm.id = desc->bTerminalID;
2715 state.oterm.type =
2716 le16_to_cpu(desc->wTerminalType);
2717 state.oterm.name = desc->iTerminal;
2718 err = parse_audio_unit(&state, desc->bSourceID);
2719 if (err < 0 && err != -EINVAL)
2720 return err;
2721 } else { /* UAC_VERSION_2 */
2722 struct uac2_output_terminal_descriptor *desc =
2723 p;
2724
2725 if (desc->bLength < sizeof(*desc))
2726 continue; /* invalid descriptor? */
2727 /* mark terminal ID as visited */
2728 set_bit(desc->bTerminalID, state.unitbitmap);
2729 state.oterm.id = desc->bTerminalID;
2730 state.oterm.type =
2731 le16_to_cpu(desc->wTerminalType);
2732 state.oterm.name = desc->iTerminal;
2733 err = parse_audio_unit(&state, desc->bSourceID);
2734 if (err < 0 && err != -EINVAL)
2735 return err;
2736
2737 /*
2738 * For UAC2, use the same approach to also add
2739 * the clock selectors
2740 */
2741 err = parse_audio_unit(&state,
2742 desc->bCSourceID);
2743 if (err < 0 && err != -EINVAL)
2744 return err;
2745 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return 0;
2750}
Clemens Ladisch84957a82005-04-29 16:23:13 +02002751
Daniel Mack7b1eda22010-03-11 21:13:22 +01002752void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002753{
Takashi Iwai3360b842014-11-18 11:47:04 +01002754 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002755
Takashi Iwai3360b842014-11-18 11:47:04 +01002756 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002757 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002758 &list->kctl->id);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002759}
2760
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002761static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01002762 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002763{
Takashi Iwai3360b842014-11-18 11:47:04 +01002764 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002765 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
2766 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002767 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01002768 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002769 cval->control, cval->cmask, cval->channels,
2770 val_types[cval->val_type]);
2771 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
2772 cval->min, cval->max, cval->dBmin, cval->dBmax);
2773}
2774
2775static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
2776 struct snd_info_buffer *buffer)
2777{
2778 struct snd_usb_audio *chip = entry->private_data;
2779 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01002780 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002781 int unitid;
2782
2783 list_for_each_entry(mixer, &chip->mixer_list, list) {
2784 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002785 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02002786 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002787 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002788 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
2789 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01002790 for (list = mixer->id_elems[unitid]; list;
2791 list = list->next_id_elem) {
2792 snd_iprintf(buffer, " Unit: %i\n", list->id);
2793 if (list->kctl)
2794 snd_iprintf(buffer,
2795 " Control: name=\"%s\", index=%i\n",
2796 list->kctl->id.name,
2797 list->kctl->id.index);
2798 if (list->dump)
2799 list->dump(buffer, list);
2800 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002801 }
2802 }
2803}
2804
Daniel Macke213e9c2010-05-11 18:13:50 +02002805static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
2806 int attribute, int value, int index)
2807{
Takashi Iwai3360b842014-11-18 11:47:04 +01002808 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02002809 __u8 unitid = (index >> 8) & 0xff;
2810 __u8 control = (value >> 8) & 0xff;
2811 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02002812 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02002813
2814 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002815 usb_audio_dbg(mixer->chip,
2816 "%s(): bogus channel number %d\n",
2817 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02002818 return;
2819 }
2820
Daniel Mack191227d2016-04-08 19:52:02 +02002821 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
2822 count++;
2823
2824 if (count == 0)
2825 return;
2826
Takashi Iwai3360b842014-11-18 11:47:04 +01002827 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
2828 struct usb_mixer_elem_info *info;
2829
2830 if (!list->kctl)
2831 continue;
2832
2833 info = (struct usb_mixer_elem_info *)list;
Daniel Mack191227d2016-04-08 19:52:02 +02002834 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02002835 continue;
2836
2837 switch (attribute) {
2838 case UAC2_CS_CUR:
2839 /* invalidate cache, so the value is read from the device */
2840 if (channel)
2841 info->cached &= ~(1 << channel);
2842 else /* master channel */
2843 info->cached = 0;
2844
2845 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002846 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02002847 break;
2848
2849 case UAC2_CS_RANGE:
2850 /* TODO */
2851 break;
2852
2853 case UAC2_CS_MEM:
2854 /* TODO */
2855 break;
2856
2857 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002858 usb_audio_dbg(mixer->chip,
2859 "unknown attribute %d in interrupt\n",
2860 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02002861 break;
2862 } /* switch */
2863 }
2864}
2865
2866static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002867{
2868 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02002869 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01002870 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002871
Oliver Neukumedf7de32011-03-11 13:19:43 +01002872 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02002873 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002874
Daniel Macke213e9c2010-05-11 18:13:50 +02002875 if (mixer->protocol == UAC_VERSION_1) {
2876 struct uac1_status_word *status;
2877
2878 for (status = urb->transfer_buffer;
2879 len >= sizeof(*status);
2880 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002881 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02002882 status->bStatusType,
2883 status->bOriginator);
2884
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002885 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02002886 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2887 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002888 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02002889
2890 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
2891 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02002892 else
Daniel Macke213e9c2010-05-11 18:13:50 +02002893 snd_usb_mixer_notify_id(mixer, status->bOriginator);
2894 }
2895 } else { /* UAC_VERSION_2 */
2896 struct uac2_interrupt_data_msg *msg;
2897
2898 for (msg = urb->transfer_buffer;
2899 len >= sizeof(*msg);
2900 len -= sizeof(*msg), msg++) {
2901 /* drop vendor specific and endpoint requests */
2902 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
2903 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
2904 continue;
2905
2906 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
2907 le16_to_cpu(msg->wValue),
2908 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002909 }
2910 }
Daniel Macke213e9c2010-05-11 18:13:50 +02002911
2912requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02002913 if (ustatus != -ENOENT &&
2914 ustatus != -ECONNRESET &&
2915 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002916 urb->dev = mixer->chip->dev;
2917 usb_submit_urb(urb, GFP_ATOMIC);
2918 }
2919}
2920
2921/* create the handler for the optional status interrupt endpoint */
2922static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2923{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002924 struct usb_endpoint_descriptor *ep;
2925 void *transfer_buffer;
2926 int buffer_length;
2927 unsigned int epnum;
2928
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002929 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02002930 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002931 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002932 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002933 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002934 return 0;
2935
Julia Lawall42a6e662008-12-29 11:23:02 +01002936 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002937 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
2938 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
2939 if (!transfer_buffer)
2940 return -ENOMEM;
2941 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2942 if (!mixer->urb) {
2943 kfree(transfer_buffer);
2944 return -ENOMEM;
2945 }
2946 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
2947 usb_rcvintpipe(mixer->chip->dev, epnum),
2948 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02002949 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002950 usb_submit_urb(mixer->urb, GFP_KERNEL);
2951 return 0;
2952}
2953
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002954int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2955 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002956{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002957 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002958 .dev_free = snd_usb_mixer_dev_free
2959 };
2960 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002961 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01002962 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002963
2964 strcpy(chip->card->mixername, "USB Mixer");
2965
Takashi Iwai561b2202005-09-09 14:22:34 +02002966 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002967 if (!mixer)
2968 return -ENOMEM;
2969 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002970 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002971 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2972 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002973 if (!mixer->id_elems) {
2974 kfree(mixer);
2975 return -ENOMEM;
2976 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002977
Daniel Mack1faa5d02011-08-04 15:56:28 +02002978 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2979 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02002980 case UAC_VERSION_1:
2981 default:
2982 mixer->protocol = UAC_VERSION_1;
2983 break;
2984 case UAC_VERSION_2:
2985 mixer->protocol = UAC_VERSION_2;
2986 break;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302987 case UAC_VERSION_3:
2988 mixer->protocol = UAC_VERSION_3;
2989 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02002990 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01002991
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002992 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002993 (err = snd_usb_mixer_status_create(mixer)) < 0)
2994 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002995
Daniel Mack7b1eda22010-03-11 21:13:22 +01002996 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002997
Takashi Iwai9cbb2802014-02-04 11:15:31 +01002998 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002999 if (err < 0)
3000 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003001
3002 if (list_empty(&chip->mixer_list) &&
3003 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3004 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3005
Clemens Ladisch84957a82005-04-29 16:23:13 +02003006 list_add(&mixer->list, &chip->mixer_list);
3007 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003008
3009_error:
3010 snd_usb_mixer_free(mixer);
3011 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003012}
3013
Takashi Iwaia6cece92014-10-31 11:24:32 +01003014void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003015{
Takashi Iwaie0c70282017-10-10 14:10:32 +02003016 if (mixer->disconnected)
3017 return;
3018 if (mixer->urb)
3019 usb_kill_urb(mixer->urb);
3020 if (mixer->rc_urb)
3021 usb_kill_urb(mixer->rc_urb);
3022 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003023}
Takashi Iwai400362f2014-01-20 16:51:16 +01003024
3025#ifdef CONFIG_PM
3026/* stop any bus activity of a mixer */
3027static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3028{
3029 usb_kill_urb(mixer->urb);
3030 usb_kill_urb(mixer->rc_urb);
3031}
3032
3033static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3034{
3035 int err;
3036
3037 if (mixer->urb) {
3038 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3039 if (err < 0)
3040 return err;
3041 }
3042
3043 return 0;
3044}
3045
3046int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3047{
3048 snd_usb_mixer_inactivate(mixer);
3049 return 0;
3050}
3051
Takashi Iwai3360b842014-11-18 11:47:04 +01003052static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003053{
Takashi Iwai3360b842014-11-18 11:47:04 +01003054 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003055 int c, err, idx;
3056
3057 if (cval->cmask) {
3058 idx = 0;
3059 for (c = 0; c < MAX_CHANNELS; c++) {
3060 if (!(cval->cmask & (1 << c)))
3061 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003062 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003063 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003064 cval->cache_val[idx]);
3065 if (err < 0)
3066 return err;
3067 }
3068 idx++;
3069 }
3070 } else {
3071 /* master */
3072 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003073 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003074 if (err < 0)
3075 return err;
3076 }
3077 }
3078
3079 return 0;
3080}
3081
3082int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3083{
Takashi Iwai3360b842014-11-18 11:47:04 +01003084 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003085 int id, err;
3086
Takashi Iwai400362f2014-01-20 16:51:16 +01003087 if (reset_resume) {
3088 /* restore cached mixer values */
3089 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003090 for (list = mixer->id_elems[id]; list;
3091 list = list->next_id_elem) {
3092 if (list->resume) {
3093 err = list->resume(list);
3094 if (err < 0)
3095 return err;
3096 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003097 }
3098 }
3099 }
3100
3101 return snd_usb_mixer_activate(mixer);
3102}
3103#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003104
3105void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3106 struct usb_mixer_interface *mixer,
3107 int unitid)
3108{
3109 list->mixer = mixer;
3110 list->id = unitid;
3111 list->dump = snd_usb_mixer_dump_cval;
3112#ifdef CONFIG_PM
3113 list->resume = restore_mixer_value;
3114#endif
3115}