blob: a51f2320a3dd24eabae63f0bc44f2e2e8f11a76f [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>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020054#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);
Takashi Iwai86e07d32005-11-17 15:08:02 +010086 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020088 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050091/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080092enum {
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
99};
100enum {
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
111 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100112#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100114static const struct usbmix_name_map *
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300115find_map(const struct usbmix_name_map *p, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100117 if (!p)
118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300120 for (; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
123 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100125 return NULL;
126}
127
128/* get the mapped name if the unit matches */
129static int
130check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
131{
132 if (!p || !p->name)
133 return 0;
134
135 buflen--;
136 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Takashi Iwai5aeee342014-11-18 11:02:14 +0100139/* ignore the error value if ignore_ctl_error flag is set */
140#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100144static inline int
145check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100149 return 1;
150}
151
152/* dB mapping */
153static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
155{
156 if (p && p->dB) {
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200159 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200163/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100164static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200165 int index, char *buf, int buflen)
166{
167 const struct usbmix_selector_map *p;
168
Daniel Mack6bc170e2014-05-24 10:58:16 +0200169 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200170 return 0;
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
174 }
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * find an audio control unit with the given unit id
180 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200181static void *find_audio_control_unit(struct mixer_build *state,
182 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Daniel Mack67e1daa2010-05-31 13:35:43 +0200187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
Daniel Mack67e1daa2010-05-31 13:35:43 +0200192 hdr->bUnitID == unit)
193 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * copy a string with the given id
201 */
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300202static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200203 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
Jaejoong Kim251552a2017-12-04 15:31:48 +0900206
207 if (len < 0)
208 return 0;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 buf[len] = 0;
211 return len;
212}
213
214/*
215 * convert from the byte/word on usb descriptor to the zero-based integer
216 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
221 return !!val;
222 case USB_MIXER_INV_BOOLEAN:
223 return !val;
224 case USB_MIXER_U8:
225 val &= 0xff;
226 break;
227 case USB_MIXER_S8:
228 val &= 0xff;
229 if (val >= 0x80)
230 val -= 0x100;
231 break;
232 case USB_MIXER_U16:
233 val &= 0xffff;
234 break;
235 case USB_MIXER_S16:
236 val &= 0xffff;
237 if (val >= 0x8000)
238 val -= 0x10000;
239 break;
240 }
241 return val;
242}
243
244/*
245 * convert from the zero-based int to the byte/word for usb descriptor
246 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100247static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 switch (cval->val_type) {
250 case USB_MIXER_BOOLEAN:
251 return !!val;
252 case USB_MIXER_INV_BOOLEAN:
253 return !val;
254 case USB_MIXER_S8:
255 case USB_MIXER_U8:
256 return val & 0xff;
257 case USB_MIXER_S16:
258 case USB_MIXER_U16:
259 return val & 0xffff;
260 }
261 return 0; /* not reached */
262}
263
Takashi Iwai86e07d32005-11-17 15:08:02 +0100264static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200266 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cval->res = 1;
268 if (val < cval->min)
269 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 else
273 return (val - cval->min) / cval->res;
274}
275
Takashi Iwai86e07d32005-11-17 15:08:02 +0100276static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (val < 0)
279 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200280 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cval->res = 1;
282 val *= cval->res;
283 val += cval->min;
284 if (val > cval->max)
285 return cval->max;
286 return val;
287}
288
Julian Scheelbc18e312015-08-14 16:14:45 +0200289static int uac2_ctl_value_size(int val_type)
290{
291 switch (val_type) {
292 case USB_MIXER_S32:
293 case USB_MIXER_U32:
294 return 4;
295 case USB_MIXER_S16:
296 case USB_MIXER_U16:
297 return 2;
298 default:
299 return 1;
300 }
301 return 0; /* unreachable */
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/*
306 * retrieve a mixer value
307 */
308
Daniel Mack6bc170e2014-05-24 10:58:16 +0200309static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai3360b842014-11-18 11:47:04 +0100312 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
315 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200316 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Takashi Iwai47ab1542015-08-25 16:09:00 +0200318 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100319 if (err < 0)
320 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200329 err = 0;
330 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700331 } else if (err == -ETIMEDOUT) {
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100335 usb_audio_dbg(chip,
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200338 err = -EINVAL;
339
340 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200341 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Daniel Mack6bc170e2014-05-24 10:58:16 +0200345static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100347{
Takashi Iwai3360b842014-11-18 11:47:04 +0100348 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100351 unsigned char *val;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100352 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100353 __u8 bRequest;
354
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100355 val_size = uac2_ctl_value_size(cval->val_type);
356
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100359 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200360 } else {
361 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100362 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200363 }
364
365 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100366
Takashi Iwai47ab1542015-08-25 16:09:00 +0200367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100368 if (ret)
369 goto error;
370
Takashi Iwai47ab1542015-08-25 16:09:00 +0200371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200374 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200375 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100376
377 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100378error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100379 usb_audio_err(chip,
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100382 return ret;
383 }
384
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200385 /* FIXME: how should we handle multiple triplets here? */
386
Daniel Mack23caaf12010-03-11 21:13:25 +0100387 switch (request) {
388 case UAC_GET_CUR:
389 val = buf;
390 break;
391 case UAC_GET_MIN:
392 val = buf + sizeof(__u16);
393 break;
394 case UAC_GET_MAX:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100395 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100396 break;
397 case UAC_GET_RES:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100398 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100399 break;
400 default:
401 return -EINVAL;
402 }
403
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100406
407 return 0;
408}
409
Daniel Mack6bc170e2014-05-24 10:58:16 +0200410static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100412{
Eldad Zack9f814102012-11-28 23:55:35 +0100413 validx += cval->idx_off;
414
Takashi Iwai3360b842014-11-18 11:47:04 +0100415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
418}
419
Daniel Mack6bc170e2014-05-24 10:58:16 +0200420static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100427static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
432 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Chris J Argeseef90452014-11-12 12:07:01 -0600435int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100436 int channel, int index, int *value)
437{
438 int err;
439
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
442 return 0;
443 }
444 err = get_cur_mix_raw(cval, channel, value);
445 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100448 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200449 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100450 return err;
451 }
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * set a mixer value
459 */
460
Daniel Mack7b1eda22010-03-11 21:13:22 +0100461int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai3360b842014-11-18 11:47:04 +0100464 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200465 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200466 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100467
Eldad Zack9f814102012-11-28 23:55:35 +0100468 validx += cval->idx_off;
469
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200470
Takashi Iwai3360b842014-11-18 11:47:04 +0100471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200473 } else { /* UAC_VERSION_2/3 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200490
491 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 if (err < 0)
493 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200494
Takashi Iwai978520b2012-10-12 15:12:55 +0200495 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
501 if (err >= 0) {
Takashi Iwai978520b2012-10-12 15:12:55 +0200502 err = 0;
503 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700504 } else if (err == -ETIMEDOUT) {
505 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100506 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200507 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100508 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 +0200509 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 err = -EINVAL;
511
512 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200513 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Daniel Mack6bc170e2014-05-24 10:58:16 +0200517static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Chris J Argeseef90452014-11-12 12:07:01 -0600523int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100524 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai641b4872009-01-15 17:05:24 +0100526 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
530
531 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100532 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200534 __func__, channel, cval->control);
535 return 0;
536 }
537
Daniel Mack6bc170e2014-05-24 10:58:16 +0200538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
540 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100541 if (err < 0)
542 return err;
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200548/*
549 * TLV callback for mixer volume controls
550 */
Felix Homann285de9c2012-04-23 20:24:24 +0200551int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200552 unsigned int size, unsigned int __user *_tlv)
553{
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200556
557 if (size < sizeof(scale))
558 return -ENOMEM;
Takashi Iwai0f174b32017-08-16 14:18:37 +0200559 if (cval->min_mute)
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200563 if (copy_to_user(_tlv, scale, sizeof(scale)))
564 return -EFAULT;
565 return 0;
566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * parser routines begin here...
570 */
571
Takashi Iwai86e07d32005-11-17 15:08:02 +0100572static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574
575/*
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
578 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200579static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
589 *
590 * if failed, give up and free the control instance.
591 */
592
Takashi Iwai3360b842014-11-18 11:47:04 +0100593int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200594 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Takashi Iwai3360b842014-11-18 11:47:04 +0100596 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200598
Daniel Mackef9d5972011-05-25 09:09:00 +0200599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kctl->id.index++;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200601 err = snd_ctl_add(mixer->chip->card, kctl);
602 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200603 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
604 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100607 list->kctl = kctl;
608 list->next_id_elem = mixer->id_elems[list->id];
609 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
614 * get a terminal name string
615 */
616
617static struct iterm_name_combo {
618 int type;
619 char *name;
620} iterm_names[] = {
621 { 0x0300, "Output" },
622 { 0x0301, "Speaker" },
623 { 0x0302, "Headphone" },
624 { 0x0303, "HMD Audio" },
625 { 0x0304, "Desktop Speaker" },
626 { 0x0305, "Room Speaker" },
627 { 0x0306, "Com Speaker" },
628 { 0x0307, "LFE" },
629 { 0x0600, "External In" },
630 { 0x0601, "Analog In" },
631 { 0x0602, "Digital In" },
632 { 0x0603, "Line" },
633 { 0x0604, "Legacy In" },
634 { 0x0605, "IEC958 In" },
635 { 0x0606, "1394 DA Stream" },
636 { 0x0607, "1394 DV Stream" },
637 { 0x0700, "Embedded" },
638 { 0x0701, "Noise Source" },
639 { 0x0702, "Equalization Noise" },
640 { 0x0703, "CD" },
641 { 0x0704, "DAT" },
642 { 0x0705, "DCC" },
643 { 0x0706, "MiniDisk" },
644 { 0x0707, "Analog Tape" },
645 { 0x0708, "Phonograph" },
646 { 0x0709, "VCR Audio" },
647 { 0x070a, "Video Disk Audio" },
648 { 0x070b, "DVD Audio" },
649 { 0x070c, "TV Tuner Audio" },
650 { 0x070d, "Satellite Rec Audio" },
651 { 0x070e, "Cable Tuner Audio" },
652 { 0x070f, "DSS Audio" },
653 { 0x0710, "Radio Receiver" },
654 { 0x0711, "Radio Transmitter" },
655 { 0x0712, "Multi-Track Recorder" },
656 { 0x0713, "Synthesizer" },
657 { 0 },
658};
659
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300660static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 unsigned char *name, int maxlen, int term_only)
662{
663 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100664 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100666 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300667 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200668 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100669 if (len)
670 return len;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* virtual type - not a real terminal */
674 if (iterm->type >> 16) {
675 if (term_only)
676 return 0;
677 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100678 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200679 strcpy(name, "Selector");
680 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200681 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200682 strcpy(name, "Process Unit");
683 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200684 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200685 strcpy(name, "Ext Unit");
686 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100687 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200688 strcpy(name, "Mixer");
689 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 default:
691 return sprintf(name, "Unit %d", iterm->id);
692 }
693 }
694
695 switch (iterm->type & 0xff00) {
696 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200697 strcpy(name, "PCM");
698 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200700 strcpy(name, "Mic");
701 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200703 strcpy(name, "Headset");
704 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200706 strcpy(name, "Phone");
707 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Daniel Mack6bc170e2014-05-24 10:58:16 +0200710 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (names->type == iterm->type) {
712 strcpy(name, names->name);
713 return strlen(names->name);
714 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100721 * Get logical cluster information for UAC3 devices.
722 */
723static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
724{
725 struct uac3_cluster_header_descriptor c_header;
726 int err;
727
728 err = snd_usb_ctl_msg(state->chip->dev,
729 usb_rcvctrlpipe(state->chip->dev, 0),
730 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
731 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
732 cluster_id,
733 snd_usb_ctrl_intf(state->chip),
734 &c_header, sizeof(c_header));
735 if (err < 0)
736 goto error;
737 if (err != sizeof(c_header)) {
738 err = -EIO;
739 goto error;
740 }
741
742 return c_header.bNrChannels;
743
744error:
745 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
746 return err;
747}
748
749/*
750 * Get number of channels for a Mixer Unit.
751 */
752static int uac_mixer_unit_get_channels(struct mixer_build *state,
753 struct uac_mixer_unit_descriptor *desc)
754{
755 int mu_channels;
756
757 if (desc->bLength < 11)
758 return -EINVAL;
759 if (!desc->bNrInPins)
760 return -EINVAL;
761
762 switch (state->mixer->protocol) {
763 case UAC_VERSION_1:
764 case UAC_VERSION_2:
765 default:
766 mu_channels = uac_mixer_unit_bNrChannels(desc);
767 break;
768 case UAC_VERSION_3:
769 mu_channels = get_cluster_channels_v3(state,
770 uac3_mixer_unit_wClusterDescrID(desc));
771 break;
772 }
773
774 if (!mu_channels)
775 return -EINVAL;
776
777 return mu_channels;
778}
779
780/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * parse the source unit recursively until it reaches to a terminal
782 * or a branched unit.
783 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200784static int check_input_term(struct mixer_build *state, int id,
785 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200787 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200788 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100789 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 memset(term, 0, sizeof(*term));
792 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100793 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200795
796 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
797 switch (hdr[2]) {
798 case UAC_INPUT_TERMINAL:
799 if (protocol == UAC_VERSION_1) {
800 struct uac_input_terminal_descriptor *d = p1;
801
802 term->type = le16_to_cpu(d->wTerminalType);
803 term->channels = d->bNrChannels;
804 term->chconfig = le16_to_cpu(d->wChannelConfig);
805 term->name = d->iTerminal;
806 } else { /* UAC_VERSION_2 */
807 struct uac2_input_terminal_descriptor *d = p1;
808
809 /* call recursively to verify that the
810 * referenced clock entity is valid */
811 err = check_input_term(state, d->bCSourceID, term);
812 if (err < 0)
813 return err;
814
815 /* save input term properties after recursion,
816 * to ensure they are not overriden by the
817 * recursion calls */
818 term->id = id;
819 term->type = le16_to_cpu(d->wTerminalType);
820 term->channels = d->bNrChannels;
821 term->chconfig = le32_to_cpu(d->bmChannelConfig);
822 term->name = d->iTerminal;
823 }
824 return 0;
825 case UAC_FEATURE_UNIT: {
826 /* the header is the same for v1 and v2 */
827 struct uac_feature_unit_descriptor *d = p1;
828
829 id = d->bSourceID;
830 break; /* continue to parse */
831 }
832 case UAC_MIXER_UNIT: {
833 struct uac_mixer_unit_descriptor *d = p1;
834
835 term->type = d->bDescriptorSubtype << 16; /* virtual type */
836 term->channels = uac_mixer_unit_bNrChannels(d);
837 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
838 term->name = uac_mixer_unit_iMixer(d);
839 return 0;
840 }
841 case UAC_SELECTOR_UNIT:
842 case UAC2_CLOCK_SELECTOR: {
843 struct uac_selector_unit_descriptor *d = p1;
844 /* call recursively to retrieve the channel info */
845 err = check_input_term(state, d->baSourceID[0], term);
846 if (err < 0)
847 return err;
848 term->type = d->bDescriptorSubtype << 16; /* virtual type */
849 term->id = id;
850 term->name = uac_selector_unit_iSelector(d);
851 return 0;
852 }
853 case UAC1_PROCESSING_UNIT:
854 case UAC1_EXTENSION_UNIT:
855 /* UAC2_PROCESSING_UNIT_V2 */
856 /* UAC2_EFFECT_UNIT */
857 case UAC2_EXTENSION_UNIT_V2: {
858 struct uac_processing_unit_descriptor *d = p1;
859
860 if (protocol == UAC_VERSION_2 &&
861 hdr[2] == UAC2_EFFECT_UNIT) {
862 /* UAC2/UAC1 unit IDs overlap here in an
863 * uncompatible way. Ignore this unit for now.
864 */
865 return 0;
866 }
867
868 if (d->bNrInPins) {
869 id = d->baSourceID[0];
870 break; /* continue to parse */
871 }
872 term->type = d->bDescriptorSubtype << 16; /* virtual type */
873 term->channels = uac_processing_unit_bNrChannels(d);
874 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
875 term->name = uac_processing_unit_iProcessing(d, protocol);
876 return 0;
877 }
878 case UAC2_CLOCK_SOURCE: {
879 struct uac_clock_source_descriptor *d = p1;
880
881 term->type = d->bDescriptorSubtype << 16; /* virtual type */
882 term->id = id;
883 term->name = d->iClockSource;
884 return 0;
885 }
886 default:
887 return -ENODEV;
888 }
889 } else { /* UAC_VERSION_3 */
890 switch (hdr[2]) {
891 case UAC_INPUT_TERMINAL: {
892 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200893
894 /* call recursively to verify that the
895 * referenced clock entity is valid */
896 err = check_input_term(state, d->bCSourceID, term);
897 if (err < 0)
898 return err;
899
900 /* save input term properties after recursion,
901 * to ensure they are not overriden by the
902 * recursion calls */
903 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100904 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100905
Jorge Sanjuan71066942018-05-14 12:03:42 +0100906 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
907 if (err < 0)
908 return err;
909 term->channels = err;
910
911 /* REVISIT: UAC3 IT doesn't have channels cfg */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200912 term->chconfig = 0;
913
914 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100915 return 0;
916 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200917 case UAC3_FEATURE_UNIT: {
918 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100919
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200920 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break; /* continue to parse */
922 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200923 case UAC3_CLOCK_SOURCE: {
924 struct uac3_clock_source_descriptor *d = p1;
925
926 term->type = d->bDescriptorSubtype << 16; /* virtual type */
927 term->id = id;
928 term->name = le16_to_cpu(d->wClockSourceStr);
929 return 0;
930 }
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100931 case UAC3_MIXER_UNIT: {
932 struct uac_mixer_unit_descriptor *d = p1;
933
934 err = uac_mixer_unit_get_channels(state, d);
935 if (err < 0)
936 return err;
937
938 term->channels = err;
939 term->type = d->bDescriptorSubtype << 16; /* virtual type */
940
941 return 0;
942 }
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +0100943 case UAC3_SELECTOR_UNIT:
944 case UAC3_CLOCK_SELECTOR: {
945 struct uac_selector_unit_descriptor *d = p1;
946 /* call recursively to retrieve the channel info */
947 err = check_input_term(state, d->baSourceID[0], term);
948 if (err < 0)
949 return err;
950 term->type = d->bDescriptorSubtype << 16; /* virtual type */
951 term->id = id;
952 term->name = 0; /* TODO: UAC3 Class-specific strings */
953
954 return 0;
955 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200956 default:
957 return -ENODEV;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 }
961 return -ENODEV;
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * Feature Unit
966 */
967
968/* feature unit control information */
969struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700970 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200972 int type; /* data type for uac1 */
973 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974};
975
976static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700977 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
978 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
979 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
980 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
981 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
982 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
983 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
984 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
985 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
986 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200987 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700988 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
989 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
990 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991};
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600994void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Jesper Juhl4d572772005-05-30 17:30:32 +0200996 kfree(kctl->private_data);
997 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/*
1001 * interface to ALSA control for feature/mixer units
1002 */
1003
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001004/* volume control quirks */
1005static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1006 struct snd_kcontrol *kctl)
1007{
Takashi Iwai3360b842014-11-18 11:47:04 +01001008 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001009 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +01001010 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001011 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +01001012 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1013 cval->min = 0x0000;
1014 cval->max = 0xffff;
1015 cval->res = 0x00e6;
1016 break;
1017 }
1018 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1019 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1020 cval->min = 0x00;
1021 cval->max = 0xff;
1022 break;
1023 }
1024 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1025 cval->min = 0xb706;
1026 cval->max = 0xff7b;
1027 cval->res = 0x0073;
1028 break;
1029 }
1030 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1031 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1032 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1033 cval->max = 0xfcfe;
1034 cval->res = 0x0073;
1035 }
1036 break;
1037
Felix Homannd34bf142012-04-23 20:24:27 +02001038 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1039 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1040 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001041 usb_audio_info(chip,
1042 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001043 cval->min = 0x0000;
1044 cval->max = 0x7f00;
1045 cval->res = 0x0100;
1046 break;
1047 }
1048 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1049 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001050 usb_audio_info(chip,
1051 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001052 cval->min = 0x00;
1053 cval->max = 0x7f;
1054 break;
1055 }
1056 break;
1057
Federico Cuello21493312018-05-09 00:13:38 +02001058 case USB_ID(0x0d8c, 0x0103):
1059 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1060 usb_audio_info(chip,
1061 "set volume quirk for CM102-A+/102S+\n");
1062 cval->min = -256;
1063 }
1064 break;
1065
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001066 case USB_ID(0x0471, 0x0101):
1067 case USB_ID(0x0471, 0x0104):
1068 case USB_ID(0x0471, 0x0105):
1069 case USB_ID(0x0672, 0x1041):
1070 /* quirk for UDA1321/N101.
1071 * note that detection between firmware 2.1.1.7 (N101)
1072 * and later 2.1.1.21 is not very clear from datasheets.
1073 * I hope that the min value is -15360 for newer firmware --jk
1074 */
1075 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1076 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001077 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001078 "set volume quirk for UDA1321/N101 chip\n");
1079 cval->max = -256;
1080 }
1081 break;
1082
1083 case USB_ID(0x046d, 0x09a4):
1084 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001085 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001086 "set volume quirk for QuickCam E3500\n");
1087 cval->min = 6080;
1088 cval->max = 8768;
1089 cval->res = 192;
1090 }
1091 break;
1092
Takashi Iwaie805ca82014-03-05 12:34:39 +01001093 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001094 case USB_ID(0x046d, 0x0808):
1095 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001096 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001097 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001098 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001099 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001100 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001101 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001102 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001103 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001104 /* Most audio usb devices lie about volume resolution.
1105 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001106 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001107 */
1108 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001109 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001110 "set resolution quirk: cval->res = 384\n");
1111 cval->res = 384;
1112 }
1113 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001114 }
1115}
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117/*
1118 * retrieve the minimum and maximum values for the specified control
1119 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001120static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1121 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 /* for failsafe */
1124 cval->min = default_min;
1125 cval->max = cval->min + 1;
1126 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001127 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (cval->val_type == USB_MIXER_BOOLEAN ||
1130 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1131 cval->initialized = 1;
1132 } else {
1133 int minchn = 0;
1134 if (cval->cmask) {
1135 int i;
1136 for (i = 0; i < MAX_CHANNELS; i++)
1137 if (cval->cmask & (1 << i)) {
1138 minchn = i + 1;
1139 break;
1140 }
1141 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001142 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1143 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001144 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001145 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001146 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1147 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return -EINVAL;
1149 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001150 if (get_ctl_value(cval, UAC_GET_RES,
1151 (cval->control << 8) | minchn,
1152 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 cval->res = 1;
1154 } else {
1155 int last_valid_res = cval->res;
1156
1157 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001158 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001159 (cval->control << 8) | minchn,
1160 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 cval->res /= 2;
1163 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001164 if (get_ctl_value(cval, UAC_GET_RES,
1165 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 cval->res = last_valid_res;
1167 }
1168 if (cval->res == 0)
1169 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001170
1171 /* Additional checks for the proper resolution
1172 *
1173 * Some devices report smaller resolutions than actually
1174 * reacting. They don't return errors but simply clip
1175 * to the lower aligned value.
1176 */
1177 if (cval->min + cval->res < cval->max) {
1178 int last_valid_res = cval->res;
1179 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001180 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001181 for (;;) {
1182 test = saved;
1183 if (test < cval->max)
1184 test += cval->res;
1185 else
1186 test -= cval->res;
1187 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001188 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001189 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001190 cval->res = last_valid_res;
1191 break;
1192 }
1193 if (test == check)
1194 break;
1195 cval->res *= 2;
1196 }
Chris J Argeseef90452014-11-12 12:07:01 -06001197 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001198 }
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 cval->initialized = 1;
1201 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001202
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001203 if (kctl)
1204 volume_control_quirks(cval, kctl);
1205
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001206 /* USB descriptions contain the dB scale in 1/256 dB unit
1207 * while ALSA TLV contains in 1/100 dB unit
1208 */
1209 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1210 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1211 if (cval->dBmin > cval->dBmax) {
1212 /* something is wrong; assume it's either from/to 0dB */
1213 if (cval->dBmin < 0)
1214 cval->dBmax = 0;
1215 else if (cval->dBmin > 0)
1216 cval->dBmin = 0;
1217 if (cval->dBmin > cval->dBmax) {
1218 /* totally crap, return an error */
1219 return -EINVAL;
1220 }
1221 }
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
1224}
1225
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001226#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001229static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001232 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (cval->val_type == USB_MIXER_BOOLEAN ||
1235 cval->val_type == USB_MIXER_INV_BOOLEAN)
1236 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1237 else
1238 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1239 uinfo->count = cval->channels;
1240 if (cval->val_type == USB_MIXER_BOOLEAN ||
1241 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1242 uinfo->value.integer.min = 0;
1243 uinfo->value.integer.max = 1;
1244 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001245 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001246 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001247 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1248 kcontrol->vd[0].access &=
1249 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1250 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001251 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001252 SNDRV_CTL_EVENT_MASK_INFO,
1253 &kcontrol->id);
1254 }
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001257 uinfo->value.integer.max =
1258 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 return 0;
1261}
1262
1263/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001264static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1265 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001267 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int c, cnt, val, err;
1269
Takashi Iwai641b4872009-01-15 17:05:24 +01001270 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (cval->cmask) {
1272 cnt = 0;
1273 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001274 if (!(cval->cmask & (1 << c)))
1275 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001276 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001277 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001278 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001279 val = get_relative_value(cval, val);
1280 ucontrol->value.integer.value[cnt] = val;
1281 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } else {
1285 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001286 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001287 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001288 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 val = get_relative_value(cval, val);
1290 ucontrol->value.integer.value[0] = val;
1291 }
1292 return 0;
1293}
1294
1295/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001296static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1297 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001299 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int c, cnt, val, oval, err;
1301 int changed = 0;
1302
1303 if (cval->cmask) {
1304 cnt = 0;
1305 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001306 if (!(cval->cmask & (1 << c)))
1307 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001308 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001309 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001310 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001311 val = ucontrol->value.integer.value[cnt];
1312 val = get_abs_value(cval, val);
1313 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001314 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001315 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001317 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 } else {
1320 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001321 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001323 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 val = ucontrol->value.integer.value[0];
1325 val = get_abs_value(cval, val);
1326 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001327 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 changed = 1;
1329 }
1330 }
1331 return changed;
1332}
1333
Andrew Chant568fa7e2018-03-23 19:25:24 -07001334/* get the boolean value from the master channel of a UAC control */
1335static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1336 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001337{
1338 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1339 int val, err;
1340
1341 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1342 if (err < 0)
1343 return filter_error(cval, err);
1344 val = (val != 0);
1345 ucontrol->value.integer.value[0] = val;
1346 return 0;
1347}
1348
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001349/* get the connectors status and report it as boolean type */
1350static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1351 struct snd_ctl_elem_value *ucontrol)
1352{
1353 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1354 struct snd_usb_audio *chip = cval->head.mixer->chip;
1355 int idx = 0, validx, ret, val;
1356
1357 validx = cval->control << 8 | 0;
1358
1359 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1360 if (ret)
1361 goto error;
1362
1363 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1364 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1365 struct uac2_connectors_ctl_blk uac2_conn;
1366
1367 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1368 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1369 validx, idx, &uac2_conn, sizeof(uac2_conn));
1370 val = !!uac2_conn.bNrChannels;
1371 } else { /* UAC_VERSION_3 */
1372 struct uac3_insertion_ctl_blk uac3_conn;
1373
1374 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1375 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1376 validx, idx, &uac3_conn, sizeof(uac3_conn));
1377 val = !!uac3_conn.bmConInserted;
1378 }
1379
1380 snd_usb_unlock_shutdown(chip);
1381
1382 if (ret < 0) {
1383error:
1384 usb_audio_err(chip,
1385 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1386 UAC_GET_CUR, validx, idx, cval->val_type);
1387 return ret;
1388 }
1389
1390 ucontrol->value.integer.value[0] = val;
1391 return 0;
1392}
1393
Takashi Iwai86e07d32005-11-17 15:08:02 +01001394static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1396 .name = "", /* will be filled later manually */
1397 .info = mixer_ctl_feature_info,
1398 .get = mixer_ctl_feature_get,
1399 .put = mixer_ctl_feature_put,
1400};
1401
Daniel Mack23caaf12010-03-11 21:13:25 +01001402/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301403static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001404 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1405 .name = "", /* will be filled later manually */
1406 .info = mixer_ctl_feature_info,
1407 .get = mixer_ctl_feature_get,
1408 .put = NULL,
1409};
1410
Andrew Chant568fa7e2018-03-23 19:25:24 -07001411/*
1412 * A control which shows the boolean value from reading a UAC control on
1413 * the master channel.
1414 */
1415static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001416 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1417 .name = "", /* will be filled later manually */
1418 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1419 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001420 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001421 .put = NULL,
1422};
1423
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001424static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1425 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1426 .name = "", /* will be filled later manually */
1427 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1428 .info = snd_ctl_boolean_mono_info,
1429 .get = mixer_ctl_connector_get,
1430 .put = NULL,
1431};
1432
Daniel Mack6bc170e2014-05-24 10:58:16 +02001433/*
1434 * This symbol is exported in order to allow the mixer quirks to
1435 * hook up to the standard feature unit control mechanism
1436 */
Daniel Mack9e386582011-05-25 09:09:01 +02001437struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439/*
1440 * build a feature control
1441 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001442static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1443{
1444 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1445}
1446
Daniel Mack6bc170e2014-05-24 10:58:16 +02001447/*
1448 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1449 * rename it to "Headphone". We determine if something is a headphone
1450 * similar to how udev determines form factor.
1451 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001452static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1453 struct snd_card *card)
1454{
1455 const char *names_to_check[] = {
1456 "Headset", "headset", "Headphone", "headphone", NULL};
1457 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001458 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001459
1460 if (strcmp("Speaker", kctl->id.name))
1461 return;
1462
1463 for (s = names_to_check; *s; s++)
1464 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001465 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001466 break;
1467 }
1468
1469 if (!found)
1470 return;
1471
1472 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1473}
1474
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001475static struct usb_feature_control_info *get_feature_control_info(int control)
1476{
1477 int i;
1478
1479 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1480 if (audio_feature_info[i].control == control)
1481 return &audio_feature_info[i];
1482 }
1483 return NULL;
1484}
1485
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001486static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1487 const struct usbmix_name_map *imap,
1488 unsigned int ctl_mask, int control,
1489 struct usb_audio_term *iterm,
1490 struct usb_audio_term *oterm,
1491 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Julian Scheelbc18e312015-08-14 16:14:45 +02001493 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned int len = 0;
1495 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001496 struct snd_kcontrol *kctl;
1497 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001498 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001499 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Daniel Mack65f25da2010-05-31 13:35:41 +02001501 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* FIXME: not supported yet */
1503 return;
1504 }
1505
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001506 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001507 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return;
1509
Takashi Iwai561b2202005-09-09 14:22:34 +02001510 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001511 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001513 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 cval->control = control;
1515 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001516
1517 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001518 if (!ctl_info) {
1519 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001520 return;
Colin Ian King62376022018-03-27 15:30:01 +01001521 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001522 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001523 cval->val_type = ctl_info->type;
1524 else /* UAC_VERSION_2 */
1525 cval->val_type = ctl_info->type_uac2 >= 0 ?
1526 ctl_info->type_uac2 : ctl_info->type;
1527
Daniel Macka6a33252010-05-31 13:35:37 +02001528 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001530 cval->master_readonly = readonly_mask;
1531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int i, c = 0;
1533 for (i = 0; i < 16; i++)
1534 if (ctl_mask & (1 << i))
1535 c++;
1536 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001537 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Daniel Mack6bc170e2014-05-24 10:58:16 +02001540 /*
1541 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001542 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001543 * issue write commands to read-only channels.
1544 */
Daniel Macka6a33252010-05-31 13:35:37 +02001545 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001546 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1547 else
1548 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1549
Daniel Mack6bc170e2014-05-24 10:58:16 +02001550 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001551 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 kfree(cval);
1553 return;
1554 }
Chris J Argeseef90452014-11-12 12:07:01 -06001555 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001557 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001559 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001560 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001561 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001564 case UAC_FU_MUTE:
1565 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001566 /*
1567 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 * - if a name id is given in descriptor, use it.
1569 * - if the connected input can be determined, then use the name
1570 * of terminal type.
1571 * - if the connected output can be determined, use it.
1572 * - otherwise, anonymous name.
1573 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001574 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001575 if (iterm)
1576 len = get_term_name(mixer->chip, iterm,
1577 kctl->id.name,
1578 sizeof(kctl->id.name), 1);
1579 if (!len && oterm)
1580 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001581 kctl->id.name,
1582 sizeof(kctl->id.name), 1);
1583 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001584 snprintf(kctl->id.name, sizeof(kctl->id.name),
1585 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001587
1588 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001589 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001590
Daniel Mack6bc170e2014-05-24 10:58:16 +02001591 /*
1592 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 * if the connected output is USB stream, then it's likely a
1594 * capture stream. otherwise it should be playback (hopefully :)
1595 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001596 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1597 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001598 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001599 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001600 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001602 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001603 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001606 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1608 sizeof(kctl->id.name));
1609 break;
1610 }
1611
Takashi Iwaie1825342012-05-14 17:11:06 +02001612 /* get min/max values */
1613 get_min_max_with_quirks(cval, 0, kctl);
1614
1615 if (control == UAC_FU_VOLUME) {
1616 check_mapped_dB(map, cval);
1617 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1618 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1619 kctl->vd[0].access |=
1620 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1621 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1622 }
1623 }
1624
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001625 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001626
Alexey Fisher80aceff2011-03-10 14:53:38 +01001627 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001628 /*
1629 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001630 * to be sure. 384 is a resolution magic number found on Logitech
1631 * devices. It will definitively catch all buggy Logitech devices.
1632 */
1633 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001634 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001635 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001636 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001637 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001638 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001639 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001640 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001641 }
1642
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001643 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001644 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001645 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001646 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001649static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1650 unsigned int ctl_mask, int control,
1651 struct usb_audio_term *iterm, int unitid,
1652 int readonly_mask)
1653{
1654 struct uac_feature_unit_descriptor *desc = raw_desc;
1655 int nameid = uac_feature_unit_iFeature(desc);
1656
1657 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1658 iterm, &state->oterm, unitid, nameid, readonly_mask);
1659}
1660
1661static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1662 unsigned int ctl_mask, int control, int unitid,
1663 const struct usbmix_name_map *badd_map)
1664{
1665 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1666 NULL, NULL, unitid, 0, 0);
1667}
1668
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001669static void get_connector_control_name(struct usb_mixer_interface *mixer,
Andrew Chant5a222e82018-03-23 19:25:23 -07001670 struct usb_audio_term *term,
1671 bool is_input, char *name, int name_size)
1672{
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001673 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001674
1675 if (name_len == 0)
1676 strlcpy(name, "Unknown", name_size);
1677
1678 /*
1679 * sound/core/ctljack.c has a convention of naming jack controls
1680 * by ending in " Jack". Make it slightly more useful by
1681 * indicating Input or Output after the terminal name.
1682 */
1683 if (is_input)
1684 strlcat(name, " - Input Jack", name_size);
1685 else
1686 strlcat(name, " - Output Jack", name_size);
1687}
1688
1689/* Build a mixer control for a UAC connector control (jack-detect) */
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001690static void build_connector_control(struct usb_mixer_interface *mixer,
Andrew Chant5a222e82018-03-23 19:25:23 -07001691 struct usb_audio_term *term, bool is_input)
1692{
1693 struct snd_kcontrol *kctl;
1694 struct usb_mixer_elem_info *cval;
1695
1696 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1697 if (!cval)
1698 return;
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001699 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001700 /*
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001701 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1702 * number of channels connected.
1703 *
1704 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1705 * following byte(s) specifies which connectors are inserted.
1706 *
1707 * This boolean ctl will simply report if any channels are connected
1708 * or not.
Andrew Chant568fa7e2018-03-23 19:25:24 -07001709 */
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001710 if (mixer->protocol == UAC_VERSION_2)
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001711 cval->control = UAC2_TE_CONNECTOR;
1712 else /* UAC_VERSION_3 */
1713 cval->control = UAC3_TE_INSERTION;
1714
Andrew Chant5a222e82018-03-23 19:25:23 -07001715 cval->val_type = USB_MIXER_BOOLEAN;
1716 cval->channels = 1; /* report true if any channel is connected */
1717 cval->min = 0;
1718 cval->max = 1;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001719 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001720 if (!kctl) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001721 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Andrew Chant5a222e82018-03-23 19:25:23 -07001722 kfree(cval);
1723 return;
1724 }
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001725 get_connector_control_name(mixer, term, is_input, kctl->id.name,
Andrew Chant5a222e82018-03-23 19:25:23 -07001726 sizeof(kctl->id.name));
1727 kctl->private_free = snd_usb_mixer_elem_free;
1728 snd_usb_mixer_add_control(&cval->head, kctl);
1729}
1730
Daniel Mackcddaafb2016-04-09 11:21:46 +02001731static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1732 void *_ftr)
1733{
1734 struct uac_clock_source_descriptor *hdr = _ftr;
1735 struct usb_mixer_elem_info *cval;
1736 struct snd_kcontrol *kctl;
1737 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1738 int ret;
1739
1740 if (state->mixer->protocol != UAC_VERSION_2)
1741 return -EINVAL;
1742
1743 if (hdr->bLength != sizeof(*hdr)) {
1744 usb_audio_dbg(state->chip,
1745 "Bogus clock source descriptor length of %d, ignoring.\n",
1746 hdr->bLength);
1747 return 0;
1748 }
1749
1750 /*
1751 * The only property of this unit we are interested in is the
1752 * clock source validity. If that isn't readable, just bail out.
1753 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001754 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001755 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001756 return 0;
1757
1758 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1759 if (!cval)
1760 return -ENOMEM;
1761
1762 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1763
1764 cval->min = 0;
1765 cval->max = 1;
1766 cval->channels = 1;
1767 cval->val_type = USB_MIXER_BOOLEAN;
1768 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1769
Andrew Chant568fa7e2018-03-23 19:25:24 -07001770 cval->master_readonly = 1;
1771 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1772 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001773
1774 if (!kctl) {
1775 kfree(cval);
1776 return -ENOMEM;
1777 }
1778
1779 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001780 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001781 name, sizeof(name));
1782 if (ret > 0)
1783 snprintf(kctl->id.name, sizeof(kctl->id.name),
1784 "%s Validity", name);
1785 else
1786 snprintf(kctl->id.name, sizeof(kctl->id.name),
1787 "Clock Source %d Validity", hdr->bClockID);
1788
1789 return snd_usb_mixer_add_control(&cval->head, kctl);
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/*
1793 * parse a feature unit
1794 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001795 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001797static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1798 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001801 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 unsigned int master_bits, first_ch_bits;
1803 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001804 struct uac_feature_unit_descriptor *hdr = _ftr;
1805 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Daniel Mack23caaf12010-03-11 21:13:25 +01001807 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001808 if (hdr->bLength < 7) {
1809 usb_audio_err(state->chip,
1810 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1811 unitid);
1812 return -EINVAL;
1813 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001814 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001815 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001816 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001817 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001818 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001819 return -EINVAL;
1820 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001821 channels = (hdr->bLength - 7) / csize - 1;
1822 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001823 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001824 usb_audio_err(state->chip,
1825 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1826 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001827 return -EINVAL;
1828 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001829 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001830 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001831 if (hdr->bLength < 6) {
1832 usb_audio_err(state->chip,
1833 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1834 unitid);
1835 return -EINVAL;
1836 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001837 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001838 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001839 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001840 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001841 usb_audio_err(state->chip,
1842 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1843 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001844 return -EINVAL;
1845 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001846 } else { /* UAC_VERSION_3 */
1847 struct uac3_feature_unit_descriptor *ftr = _ftr;
1848
1849 if (hdr->bLength < 7) {
1850 usb_audio_err(state->chip,
1851 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1852 unitid);
1853 return -EINVAL;
1854 }
1855 csize = 4;
1856 channels = (ftr->bLength - 7) / 4 - 1;
1857 bmaControls = ftr->bmaControls;
1858 if (hdr->bLength < 7 + csize) {
1859 usb_audio_err(state->chip,
1860 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1861 unitid);
1862 return -EINVAL;
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
1866 /* parse the source unit */
Takashi Iwaif25ecf82018-05-27 15:18:22 +02001867 err = parse_audio_unit(state, hdr->bSourceID);
1868 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return err;
1870
1871 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001872 err = check_input_term(state, hdr->bSourceID, &iterm);
1873 if (err < 0)
1874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Daniel Mack23caaf12010-03-11 21:13:25 +01001876 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001877 /* master configuration quirks */
1878 switch (state->chip->usb_id) {
1879 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001880 usb_audio_info(state->chip,
1881 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001882 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001883 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001884 break;
David Henningssonc1051432012-09-20 10:20:41 +02001885 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001886 usb_audio_info(state->chip,
1887 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001888 /* disable non-functional volume control */
1889 channels = 0;
1890 break;
1891
Javier Kohen0c3cee52009-11-17 15:36:13 +01001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001894 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 else
1896 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001897
1898 if (state->mixer->protocol == UAC_VERSION_1) {
1899 /* check all control types */
1900 for (i = 0; i < 10; i++) {
1901 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001902 int control = audio_feature_info[i].control;
1903
Daniel Mack23caaf12010-03-11 21:13:25 +01001904 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001905 unsigned int mask;
1906
1907 mask = snd_usb_combine_bytes(bmaControls +
1908 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001909 if (mask & (1 << i))
1910 ch_bits |= (1 << j);
1911 }
1912 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001913
1914 /*
1915 * The first channel must be set
1916 * (for ease of programming).
1917 */
1918 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001919 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001920 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001921 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001922 build_feature_ctl(state, _ftr, 0, control,
1923 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001925 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001926 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001927 unsigned int ch_bits = 0;
1928 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001929 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001930
1931 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001932 unsigned int mask;
1933
1934 mask = snd_usb_combine_bytes(bmaControls +
1935 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001936 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001937 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001938 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001939 ch_read_only |= (1 << j);
1940 }
1941 }
1942
Daniel Mack6bc170e2014-05-24 10:58:16 +02001943 /*
1944 * NOTE: build_feature_ctl() will mark the control
1945 * read-only if all channels are marked read-only in
1946 * the descriptors. Otherwise, the control will be
1947 * reported as writeable, but the driver will not
1948 * actually issue a write command for read-only
1949 * channels.
1950 */
1951
1952 /*
1953 * The first channel must be set
1954 * (for ease of programming).
1955 */
1956 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001957 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001958 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001959 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001960 build_feature_ctl(state, _ftr, 0, control,
1961 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001962 !uac_v2v3_control_is_writeable(master_bits,
1963 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01001964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
1967 return 0;
1968}
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/*
1971 * Mixer Unit
1972 */
1973
1974/*
1975 * build a mixer unit control
1976 *
1977 * the callbacks are identical with feature unit.
1978 * input channel number (zero based) is given in control field instead.
1979 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001980static void build_mixer_unit_ctl(struct mixer_build *state,
1981 struct uac_mixer_unit_descriptor *desc,
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01001982 int in_pin, int in_ch, int num_outs,
1983 int unitid, struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001985 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001987 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001988 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001990 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001991 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return;
1993
Takashi Iwai561b2202005-09-09 14:22:34 +02001994 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001995 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return;
1997
Takashi Iwai3360b842014-11-18 11:47:04 +01001998 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 cval->control = in_ch + 1; /* based on 1 */
2000 cval->val_type = USB_MIXER_S16;
2001 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002002 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2003
2004 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 cval->cmask |= (1 << i);
2006 cval->channels++;
2007 }
2008 }
2009
2010 /* get min/max values */
2011 get_min_max(cval, 0);
2012
2013 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002014 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002015 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 kfree(cval);
2017 return;
2018 }
Chris J Argeseef90452014-11-12 12:07:01 -06002019 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002021 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002022 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002023 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002024 sizeof(kctl->id.name), 0);
2025 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02002027 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002029 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002030 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2031 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Andrew Chant5a222e82018-03-23 19:25:23 -07002034static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2035 void *raw_desc)
2036{
2037 struct usb_audio_term iterm;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002038 unsigned int control, bmctls, term_id;
Andrew Chant5a222e82018-03-23 19:25:23 -07002039
Andrew Chant5a222e82018-03-23 19:25:23 -07002040 if (state->mixer->protocol == UAC_VERSION_2) {
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002041 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2042 control = UAC2_TE_CONNECTOR;
2043 term_id = d_v2->bTerminalID;
2044 bmctls = le16_to_cpu(d_v2->bmControls);
2045 } else if (state->mixer->protocol == UAC_VERSION_3) {
2046 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2047 control = UAC3_TE_INSERTION;
2048 term_id = d_v3->bTerminalID;
2049 bmctls = le32_to_cpu(d_v3->bmControls);
2050 } else {
2051 return 0; /* UAC1. No Insertion control */
Andrew Chant5a222e82018-03-23 19:25:23 -07002052 }
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002053
2054 check_input_term(state, term_id, &iterm);
2055
2056 /* Check for jack detection. */
2057 if (uac_v2v3_control_is_readable(bmctls, control))
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01002058 build_connector_control(state->mixer, &iterm, true);
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002059
Andrew Chant5a222e82018-03-23 19:25:23 -07002060 return 0;
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063/*
2064 * parse a mixer unit
2065 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002066static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2067 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Daniel Mack99fc8642010-03-11 21:13:24 +01002069 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002070 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int input_pins, num_ins, num_outs;
2072 int pin, ich, err;
2073
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002074 err = uac_mixer_unit_get_channels(state, desc);
2075 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002076 usb_audio_err(state->chip,
2077 "invalid MIXER UNIT descriptor %d\n",
2078 unitid);
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002082 num_outs = err;
2083 input_pins = desc->bNrInPins;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 num_ins = 0;
2086 ich = 0;
2087 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002088 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002090 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002091 /* no bmControls field (e.g. Maya44) -> ignore */
2092 if (desc->bLength <= 10 + input_pins)
2093 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002094 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (err < 0)
2096 return err;
2097 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002098 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 int och, ich_has_controls = 0;
2100
Daniel Mack6bc170e2014-05-24 10:58:16 +02002101 for (och = 0; och < num_outs; och++) {
2102 __u8 *c = uac_mixer_unit_bmControls(desc,
2103 state->mixer->protocol);
2104
2105 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 ich_has_controls = 1;
2107 break;
2108 }
2109 }
2110 if (ich_has_controls)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002111 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 unitid, &iterm);
2113 }
2114 }
2115 return 0;
2116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/*
2119 * Processing Unit / Extension Unit
2120 */
2121
2122/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002123static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002126 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int err, val;
2128
2129 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002130 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002132 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 val = get_relative_value(cval, val);
2135 ucontrol->value.integer.value[0] = val;
2136 return 0;
2137}
2138
2139/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002140static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002143 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 int val, oval, err;
2145
2146 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002147 if (err < 0)
2148 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 val = ucontrol->value.integer.value[0];
2150 val = get_abs_value(cval, val);
2151 if (val != oval) {
2152 set_cur_ctl_value(cval, cval->control << 8, val);
2153 return 1;
2154 }
2155 return 0;
2156}
2157
2158/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302159static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2161 .name = "", /* will be filled later */
2162 .info = mixer_ctl_feature_info,
2163 .get = mixer_ctl_procunit_get,
2164 .put = mixer_ctl_procunit_put,
2165};
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167/*
2168 * predefined data for processing units
2169 */
2170struct procunit_value_info {
2171 int control;
2172 char *suffix;
2173 int val_type;
2174 int min_value;
2175};
2176
2177struct procunit_info {
2178 int type;
2179 char *name;
2180 struct procunit_value_info *values;
2181};
2182
2183static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002184 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2185 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 { 0 }
2187};
2188static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002189 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2190 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 { 0 }
2192};
2193static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002194 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2195 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 { 0 }
2197};
2198static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002199 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2200 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2201 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2202 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 { 0 }
2204};
2205static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002206 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2207 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2208 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2209 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 { 0 }
2211};
2212static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002213 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2214 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2215 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2216 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2217 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2218 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 { 0 }
2220};
2221
2222static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002223 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2224 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2225 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2226 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2227 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2228 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 { 0 },
2230};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002231/*
2232 * predefined data for extension units
2233 */
2234static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002235 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2236 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002237};
2238static struct procunit_value_info clock_source_xu_info[] = {
2239 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2240 { 0 }
2241};
2242static struct procunit_value_info spdif_format_xu_info[] = {
2243 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2244 { 0 }
2245};
2246static struct procunit_value_info soft_limit_xu_info[] = {
2247 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2248 { 0 }
2249};
2250static struct procunit_info extunits[] = {
2251 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2252 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2253 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2254 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2255 { 0 }
2256};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/*
2259 * build a processing/extension unit
2260 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002261static int build_audio_procunit(struct mixer_build *state, int unitid,
2262 void *raw_desc, struct procunit_info *list,
2263 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Daniel Mack99fc8642010-03-11 21:13:24 +01002265 struct uac_processing_unit_descriptor *desc = raw_desc;
2266 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002267 struct usb_mixer_elem_info *cval;
2268 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 int i, err, nameid, type, len;
2270 struct procunit_info *info;
2271 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002272 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 static struct procunit_value_info default_value_info[] = {
2274 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2275 { 0 }
2276 };
2277 static struct procunit_info default_info = {
2278 0, NULL, default_value_info
2279 };
2280
Daniel Mack23caaf12010-03-11 21:13:25 +01002281 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2282 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002283 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return -EINVAL;
2285 }
2286
2287 for (i = 0; i < num_ins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002288 err = parse_audio_unit(state, desc->baSourceID[i]);
2289 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return err;
2291 }
2292
Daniel Mack99fc8642010-03-11 21:13:24 +01002293 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 for (info = list; info && info->type; info++)
2295 if (info->type == type)
2296 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002297 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 info = &default_info;
2299
2300 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002301 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002302
Daniel Mack6bc170e2014-05-24 10:58:16 +02002303 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 continue;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002305 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002306 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002308 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002309 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002311 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 cval->control = valinfo->control;
2313 cval->val_type = valinfo->val_type;
2314 cval->channels = 1;
2315
2316 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002317 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002318 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /* FIXME: hard-coded */
2320 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002321 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 cval->res = 1;
2323 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002324 } else {
2325 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002326 /*
2327 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002328 * samplerate control quirk
2329 */
2330 cval->min = 0;
2331 cval->max = 5;
2332 cval->res = 1;
2333 cval->initialized = 1;
2334 } else
2335 get_min_max(cval, valinfo->min_value);
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002339 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 kfree(cval);
2341 return -ENOMEM;
2342 }
Chris J Argeseef90452014-11-12 12:07:01 -06002343 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Daniel Mack6bc170e2014-05-24 10:58:16 +02002345 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002346 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002347 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002349 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002350 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 len = 0;
2352 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002353 len = snd_usb_copy_string_desc(state->chip,
2354 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002355 kctl->id.name,
2356 sizeof(kctl->id.name));
2357 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2359 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002360 append_ctl_name(kctl, " ");
2361 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002363 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002364 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002365 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002366 cval->min, cval->max);
2367
Takashi Iwai3360b842014-11-18 11:47:04 +01002368 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002369 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return err;
2371 }
2372 return 0;
2373}
2374
Daniel Mack6bc170e2014-05-24 10:58:16 +02002375static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2376 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002378 return build_audio_procunit(state, unitid, raw_desc,
2379 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Daniel Mack6bc170e2014-05-24 10:58:16 +02002382static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2383 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002385 /*
2386 * Note that we parse extension units with processing unit descriptors.
2387 * That's ok as the layout is the same.
2388 */
2389 return build_audio_procunit(state, unitid, raw_desc,
2390 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393/*
2394 * Selector Unit
2395 */
2396
Daniel Mack6bc170e2014-05-24 10:58:16 +02002397/*
2398 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 * use an enumerator type for routing
2400 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002401static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002404 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002405 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Takashi Iwai5e246b82008-08-08 17:12:47 +02002407 if (snd_BUG_ON(!itemlist))
2408 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002409 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
2412/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002413static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002416 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 int val, err;
2418
Daniel Mack09414202010-05-31 13:35:44 +02002419 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002421 ucontrol->value.enumerated.item[0] = 0;
2422 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
2424 val = get_relative_value(cval, val);
2425 ucontrol->value.enumerated.item[0] = val;
2426 return 0;
2427}
2428
2429/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002430static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002433 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 int val, oval, err;
2435
Daniel Mack09414202010-05-31 13:35:44 +02002436 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002437 if (err < 0)
2438 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 val = ucontrol->value.enumerated.item[0];
2440 val = get_abs_value(cval, val);
2441 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002442 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return 1;
2444 }
2445 return 0;
2446}
2447
2448/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302449static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2451 .name = "", /* will be filled later */
2452 .info = mixer_ctl_selector_info,
2453 .get = mixer_ctl_selector_get,
2454 .put = mixer_ctl_selector_put,
2455};
2456
Daniel Mack6bc170e2014-05-24 10:58:16 +02002457/*
2458 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * free both private_data and private_value
2460 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002461static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 int i, num_ins = 0;
2464
2465 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002466 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 num_ins = cval->max;
2468 kfree(cval);
2469 kctl->private_data = NULL;
2470 }
2471 if (kctl->private_value) {
2472 char **itemlist = (char **)kctl->private_value;
2473 for (i = 0; i < num_ins; i++)
2474 kfree(itemlist[i]);
2475 kfree(itemlist);
2476 kctl->private_value = 0;
2477 }
2478}
2479
2480/*
2481 * parse a selector unit
2482 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002483static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2484 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Daniel Mack99fc8642010-03-11 21:13:24 +01002486 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 unsigned int i, nameid, len;
2488 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002489 struct usb_mixer_elem_info *cval;
2490 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002491 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 char **namelist;
2493
Takashi Iwaif658f172017-11-21 17:00:32 +01002494 if (desc->bLength < 5 || !desc->bNrInPins ||
2495 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002496 usb_audio_err(state->chip,
2497 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return -EINVAL;
2499 }
2500
Daniel Mack99fc8642010-03-11 21:13:24 +01002501 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002502 err = parse_audio_unit(state, desc->baSourceID[i]);
2503 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return err;
2505 }
2506
Daniel Mack99fc8642010-03-11 21:13:24 +01002507 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return 0;
2509
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002510 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002511 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return 0;
2513
Takashi Iwai561b2202005-09-09 14:22:34 +02002514 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002515 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002517 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 cval->val_type = USB_MIXER_U8;
2519 cval->channels = 1;
2520 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002521 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 cval->res = 1;
2523 cval->initialized = 1;
2524
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002525 switch (state->mixer->protocol) {
2526 case UAC_VERSION_1:
2527 default:
Daniel Mack09414202010-05-31 13:35:44 +02002528 cval->control = 0;
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002529 break;
2530 case UAC_VERSION_2:
2531 case UAC_VERSION_3:
2532 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2533 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2534 cval->control = UAC2_CX_CLOCK_SELECTOR;
2535 else /* UAC2/3_SELECTOR_UNIT */
2536 cval->control = UAC2_SU_SELECTOR;
2537 break;
2538 }
Daniel Mack09414202010-05-31 13:35:44 +02002539
Kees Cook6da2ec52018-06-12 13:55:00 -07002540 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002541 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 kfree(cval);
2543 return -ENOMEM;
2544 }
2545#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002546 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002547 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 len = 0;
2549 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002550 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002551 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 kfree(namelist[i]);
2553 kfree(namelist);
2554 kfree(cval);
2555 return -ENOMEM;
2556 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002557 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2558 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002559 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002560 len = get_term_name(state->chip, &iterm, namelist[i],
2561 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002563 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
2565
2566 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2567 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002568 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002569 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 kfree(cval);
2571 return -ENOMEM;
2572 }
2573 kctl->private_value = (unsigned long)namelist;
2574 kctl->private_free = usb_mixer_selector_elem_free;
2575
Takashi Iwai5a15f282017-12-18 23:36:57 +01002576 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002577 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002578 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002579 /* no mapping ? */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002580 switch (state->mixer->protocol) {
2581 case UAC_VERSION_1:
2582 case UAC_VERSION_2:
2583 default:
Takashi Iwai5a15f282017-12-18 23:36:57 +01002584 /* if iSelector is given, use it */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002585 nameid = uac_selector_unit_iSelector(desc);
2586 if (nameid)
2587 len = snd_usb_copy_string_desc(state->chip,
2588 nameid, kctl->id.name,
2589 sizeof(kctl->id.name));
2590 break;
2591 case UAC_VERSION_3:
2592 /* TODO: Class-Specific strings not yet supported */
2593 break;
2594 }
2595
Takashi Iwai5a15f282017-12-18 23:36:57 +01002596 /* ... or pick up the terminal name at next */
2597 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002598 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002600 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002601 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2603
Takashi Iwai5a15f282017-12-18 23:36:57 +01002604 /* and add the proper suffix */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002605 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2606 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
Daniel Mack09414202010-05-31 13:35:44 +02002607 append_ctl_name(kctl, " Clock Source");
2608 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002609 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002611 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002614 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002615 cval->head.id, kctl->id.name, desc->bNrInPins);
2616 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/*
2620 * parse an audio unit recursively
2621 */
2622
Takashi Iwai86e07d32005-11-17 15:08:02 +01002623static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002626 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 if (test_and_set_bit(unitid, state->unitbitmap))
2629 return 0; /* the unit already visited */
2630
2631 p1 = find_audio_control_unit(state, unitid);
2632 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002633 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return -EINVAL;
2635 }
2636
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002637 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2638 switch (p1[2]) {
2639 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002640 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002641 case UAC_MIXER_UNIT:
2642 return parse_audio_mixer_unit(state, unitid, p1);
2643 case UAC2_CLOCK_SOURCE:
2644 return parse_clock_source_unit(state, unitid, p1);
2645 case UAC_SELECTOR_UNIT:
2646 case UAC2_CLOCK_SELECTOR:
2647 return parse_audio_selector_unit(state, unitid, p1);
2648 case UAC_FEATURE_UNIT:
2649 return parse_audio_feature_unit(state, unitid, p1);
2650 case UAC1_PROCESSING_UNIT:
2651 /* UAC2_EFFECT_UNIT has the same value */
2652 if (protocol == UAC_VERSION_1)
2653 return parse_audio_processing_unit(state, unitid, p1);
2654 else
2655 return 0; /* FIXME - effect units not implemented yet */
2656 case UAC1_EXTENSION_UNIT:
2657 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2658 if (protocol == UAC_VERSION_1)
2659 return parse_audio_extension_unit(state, unitid, p1);
2660 else /* UAC_VERSION_2 */
2661 return parse_audio_processing_unit(state, unitid, p1);
2662 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002663 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002664 default:
2665 usb_audio_err(state->chip,
2666 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2667 return -EINVAL;
2668 }
2669 } else { /* UAC_VERSION_3 */
2670 switch (p1[2]) {
2671 case UAC_INPUT_TERMINAL:
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002672 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002673 case UAC3_MIXER_UNIT:
2674 return parse_audio_mixer_unit(state, unitid, p1);
2675 case UAC3_CLOCK_SOURCE:
2676 return parse_clock_source_unit(state, unitid, p1);
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002677 case UAC3_SELECTOR_UNIT:
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002678 case UAC3_CLOCK_SELECTOR:
2679 return parse_audio_selector_unit(state, unitid, p1);
2680 case UAC3_FEATURE_UNIT:
2681 return parse_audio_feature_unit(state, unitid, p1);
2682 case UAC3_EFFECT_UNIT:
2683 return 0; /* FIXME - effect units not implemented yet */
2684 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002685 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002686 case UAC3_EXTENSION_UNIT:
2687 return parse_audio_extension_unit(state, unitid, p1);
2688 default:
2689 usb_audio_err(state->chip,
2690 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2691 return -EINVAL;
2692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694}
2695
Clemens Ladisch84957a82005-04-29 16:23:13 +02002696static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2697{
Takashi Iwai124751d2017-10-10 14:10:32 +02002698 /* kill pending URBs */
2699 snd_usb_mixer_disconnect(mixer);
2700
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002701 kfree(mixer->id_elems);
2702 if (mixer->urb) {
2703 kfree(mixer->urb->transfer_buffer);
2704 usb_free_urb(mixer->urb);
2705 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002706 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002707 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002708 kfree(mixer);
2709}
2710
Takashi Iwai86e07d32005-11-17 15:08:02 +01002711static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002712{
2713 struct usb_mixer_interface *mixer = device->device_data;
2714 snd_usb_mixer_free(mixer);
2715 return 0;
2716}
2717
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002718/* UAC3 predefined channels configuration */
2719struct uac3_badd_profile {
2720 int subclass;
2721 const char *name;
2722 int c_chmask; /* capture channels mask */
2723 int p_chmask; /* playback channels mask */
2724 int st_chmask; /* side tone mixing channel mask */
2725};
2726
2727static struct uac3_badd_profile uac3_badd_profiles[] = {
2728 {
2729 /*
2730 * BAIF, BAOF or combination of both
2731 * IN: Mono or Stereo cfg, Mono alt possible
2732 * OUT: Mono or Stereo cfg, Mono alt possible
2733 */
2734 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2735 .name = "GENERIC IO",
2736 .c_chmask = -1, /* dynamic channels */
2737 .p_chmask = -1, /* dynamic channels */
2738 },
2739 {
2740 /* BAOF; Stereo only cfg, Mono alt possible */
2741 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2742 .name = "HEADPHONE",
2743 .p_chmask = 3,
2744 },
2745 {
2746 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2747 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2748 .name = "SPEAKER",
2749 .p_chmask = -1, /* dynamic channels */
2750 },
2751 {
2752 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2753 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2754 .name = "MICROPHONE",
2755 .c_chmask = -1, /* dynamic channels */
2756 },
2757 {
2758 /*
2759 * BAIOF topology
2760 * IN: Mono only
2761 * OUT: Mono or Stereo cfg, Mono alt possible
2762 */
2763 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2764 .name = "HEADSET",
2765 .c_chmask = 1,
2766 .p_chmask = -1, /* dynamic channels */
2767 .st_chmask = 1,
2768 },
2769 {
2770 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2771 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2772 .name = "HEADSET ADAPTER",
2773 .c_chmask = 1,
2774 .p_chmask = 3,
2775 .st_chmask = 1,
2776 },
2777 {
2778 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2779 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2780 .name = "SPEAKERPHONE",
2781 .c_chmask = 1,
2782 .p_chmask = 1,
2783 },
2784 { 0 } /* terminator */
2785};
2786
2787static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2788 struct uac3_badd_profile *f,
2789 int c_chmask, int p_chmask)
2790{
2791 /*
2792 * If both playback/capture channels are dynamic, make sure
2793 * at least one channel is present
2794 */
2795 if (f->c_chmask < 0 && f->p_chmask < 0) {
2796 if (!c_chmask && !p_chmask) {
2797 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2798 f->name);
2799 return false;
2800 }
2801 return true;
2802 }
2803
2804 if ((f->c_chmask < 0 && !c_chmask) ||
2805 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2806 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2807 f->name);
2808 return false;
2809 }
2810 if ((f->p_chmask < 0 && !p_chmask) ||
2811 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2812 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2813 f->name);
2814 return false;
2815 }
2816 return true;
2817}
2818
2819/*
2820 * create mixer controls for UAC3 BADD profiles
2821 *
2822 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2823 *
2824 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2825 */
2826static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2827 int ctrlif)
2828{
2829 struct usb_device *dev = mixer->chip->dev;
2830 struct usb_interface_assoc_descriptor *assoc;
2831 int badd_profile = mixer->chip->badd_profile;
2832 struct uac3_badd_profile *f;
2833 const struct usbmix_ctl_map *map;
2834 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2835 int i;
2836
2837 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2838
2839 /* Detect BADD capture/playback channels from AS EP descriptors */
2840 for (i = 0; i < assoc->bInterfaceCount; i++) {
2841 int intf = assoc->bFirstInterface + i;
2842
2843 struct usb_interface *iface;
2844 struct usb_host_interface *alts;
2845 struct usb_interface_descriptor *altsd;
2846 unsigned int maxpacksize;
2847 char dir_in;
2848 int chmask, num;
2849
2850 if (intf == ctrlif)
2851 continue;
2852
2853 iface = usb_ifnum_to_if(dev, intf);
2854 num = iface->num_altsetting;
2855
2856 if (num < 2)
2857 return -EINVAL;
2858
2859 /*
2860 * The number of Channels in an AudioStreaming interface
2861 * and the audio sample bit resolution (16 bits or 24
2862 * bits) can be derived from the wMaxPacketSize field in
2863 * the Standard AS Audio Data Endpoint descriptor in
2864 * Alternate Setting 1
2865 */
2866 alts = &iface->altsetting[1];
2867 altsd = get_iface_desc(alts);
2868
2869 if (altsd->bNumEndpoints < 1)
2870 return -EINVAL;
2871
2872 /* check direction */
2873 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2874 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2875
2876 switch (maxpacksize) {
2877 default:
2878 usb_audio_err(mixer->chip,
2879 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2880 maxpacksize);
2881 return -EINVAL;
2882 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2883 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2884 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2885 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2886 chmask = 1;
2887 break;
2888 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2889 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2890 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2891 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2892 chmask = 3;
2893 break;
2894 }
2895
2896 if (dir_in)
2897 c_chmask = chmask;
2898 else
2899 p_chmask = chmask;
2900 }
2901
2902 usb_audio_dbg(mixer->chip,
2903 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
2904 badd_profile, c_chmask, p_chmask);
2905
2906 /* check the mapping table */
2907 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
2908 if (map->id == badd_profile)
2909 break;
2910 }
2911
2912 if (!map->id)
2913 return -EINVAL;
2914
2915 for (f = uac3_badd_profiles; f->name; f++) {
2916 if (badd_profile == f->subclass)
2917 break;
2918 }
2919 if (!f->name)
2920 return -EINVAL;
2921 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
2922 return -EINVAL;
2923 st_chmask = f->st_chmask;
2924
2925 /* Playback */
2926 if (p_chmask) {
2927 /* Master channel, always writable */
2928 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2929 UAC3_BADD_FU_ID2, map->map);
2930 /* Mono/Stereo volume channels, always writable */
2931 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
2932 UAC3_BADD_FU_ID2, map->map);
2933 }
2934
2935 /* Capture */
2936 if (c_chmask) {
2937 /* Master channel, always writable */
2938 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2939 UAC3_BADD_FU_ID5, map->map);
2940 /* Mono/Stereo volume channels, always writable */
2941 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
2942 UAC3_BADD_FU_ID5, map->map);
2943 }
2944
2945 /* Side tone-mixing */
2946 if (st_chmask) {
2947 /* Master channel, always writable */
2948 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2949 UAC3_BADD_FU_ID7, map->map);
2950 /* Mono volume channel, always writable */
2951 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
2952 UAC3_BADD_FU_ID7, map->map);
2953 }
2954
Jorge Sanjuan3528cd82018-06-14 15:05:57 +01002955 /* Insertion Control */
2956 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
2957 struct usb_audio_term iterm, oterm;
2958
2959 /* Input Term - Insertion control */
2960 memset(&iterm, 0, sizeof(iterm));
2961 iterm.id = UAC3_BADD_IT_ID4;
2962 iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
2963 build_connector_control(mixer, &iterm, true);
2964
2965 /* Output Term - Insertion control */
2966 memset(&oterm, 0, sizeof(oterm));
2967 oterm.id = UAC3_BADD_OT_ID3;
2968 oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
2969 build_connector_control(mixer, &oterm, false);
2970 }
2971
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002972 return 0;
2973}
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975/*
2976 * create mixer controls
2977 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002978 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002980static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002982 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 int err;
2984 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01002985 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002988 state.chip = mixer->chip;
2989 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002990 state.buffer = mixer->hostif->extra;
2991 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002994 for (map = usbmix_ctl_maps; map->id; map++) {
2995 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002997 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002998 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 break;
3000 }
3001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Daniel Mack23caaf12010-03-11 21:13:25 +01003003 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02003004 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3005 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02003006 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01003007 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02003008 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01003009
3010 if (desc->bLength < sizeof(*desc))
3011 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02003012 /* mark terminal ID as visited */
3013 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01003014 state.oterm.id = desc->bTerminalID;
3015 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3016 state.oterm.name = desc->iTerminal;
3017 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003018 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01003019 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003020 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01003021 struct uac2_output_terminal_descriptor *desc = p;
3022
3023 if (desc->bLength < sizeof(*desc))
3024 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02003025 /* mark terminal ID as visited */
3026 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01003027 state.oterm.id = desc->bTerminalID;
3028 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3029 state.oterm.name = desc->iTerminal;
3030 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003031 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01003032 return err;
Daniel Mack09414202010-05-31 13:35:44 +02003033
Daniel Mack6bc170e2014-05-24 10:58:16 +02003034 /*
3035 * For UAC2, use the same approach to also add the
3036 * clock selectors
3037 */
Daniel Mack09414202010-05-31 13:35:44 +02003038 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003039 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02003040 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07003041
Takashi Iwai2b54f7852018-04-23 15:19:25 +02003042 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07003043 UAC2_TE_CONNECTOR)) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01003044 build_connector_control(state.mixer, &state.oterm,
Andrew Chant5a222e82018-03-23 19:25:23 -07003045 false);
3046 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003047 } else { /* UAC_VERSION_3 */
3048 struct uac3_output_terminal_descriptor *desc = p;
3049
3050 if (desc->bLength < sizeof(*desc))
3051 continue; /* invalid descriptor? */
3052 /* mark terminal ID as visited */
3053 set_bit(desc->bTerminalID, state.unitbitmap);
3054 state.oterm.id = desc->bTerminalID;
3055 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3056 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3057 err = parse_audio_unit(&state, desc->bSourceID);
3058 if (err < 0 && err != -EINVAL)
3059 return err;
3060
3061 /*
3062 * For UAC3, use the same approach to also add the
3063 * clock selectors
3064 */
3065 err = parse_audio_unit(&state, desc->bCSourceID);
3066 if (err < 0 && err != -EINVAL)
3067 return err;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003068
3069 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3070 UAC3_TE_INSERTION)) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01003071 build_connector_control(state.mixer, &state.oterm,
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003072 false);
3073 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return 0;
3078}
Clemens Ladisch84957a82005-04-29 16:23:13 +02003079
Daniel Mack7b1eda22010-03-11 21:13:22 +01003080void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003081{
Takashi Iwai3360b842014-11-18 11:47:04 +01003082 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003083
Takashi Iwai8c558072018-05-03 12:33:32 +02003084 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01003085 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02003086 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01003087 /* invalidate cache, so the value is read from the device */
3088 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003089 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003090 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01003091 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003092}
3093
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003094static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01003095 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003096{
Takashi Iwai8c558072018-05-03 12:33:32 +02003097 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003098 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3099 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003100 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01003101 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003102 cval->control, cval->cmask, cval->channels,
3103 val_types[cval->val_type]);
3104 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3105 cval->min, cval->max, cval->dBmin, cval->dBmax);
3106}
3107
3108static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3109 struct snd_info_buffer *buffer)
3110{
3111 struct snd_usb_audio *chip = entry->private_data;
3112 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01003113 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003114 int unitid;
3115
3116 list_for_each_entry(mixer, &chip->mixer_list, list) {
3117 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003118 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02003119 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003120 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003121 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3122 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003123 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003124 snd_iprintf(buffer, " Unit: %i\n", list->id);
3125 if (list->kctl)
3126 snd_iprintf(buffer,
3127 " Control: name=\"%s\", index=%i\n",
3128 list->kctl->id.name,
3129 list->kctl->id.index);
3130 if (list->dump)
3131 list->dump(buffer, list);
3132 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003133 }
3134 }
3135}
3136
Daniel Macke213e9c2010-05-11 18:13:50 +02003137static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3138 int attribute, int value, int index)
3139{
Takashi Iwai3360b842014-11-18 11:47:04 +01003140 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02003141 __u8 unitid = (index >> 8) & 0xff;
3142 __u8 control = (value >> 8) & 0xff;
3143 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02003144 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02003145
3146 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003147 usb_audio_dbg(mixer->chip,
3148 "%s(): bogus channel number %d\n",
3149 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02003150 return;
3151 }
3152
Takashi Iwai8c558072018-05-03 12:33:32 +02003153 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02003154 count++;
3155
3156 if (count == 0)
3157 return;
3158
Takashi Iwai8c558072018-05-03 12:33:32 +02003159 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003160 struct usb_mixer_elem_info *info;
3161
3162 if (!list->kctl)
3163 continue;
3164
Takashi Iwai8c558072018-05-03 12:33:32 +02003165 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02003166 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02003167 continue;
3168
3169 switch (attribute) {
3170 case UAC2_CS_CUR:
3171 /* invalidate cache, so the value is read from the device */
3172 if (channel)
3173 info->cached &= ~(1 << channel);
3174 else /* master channel */
3175 info->cached = 0;
3176
3177 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003178 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02003179 break;
3180
3181 case UAC2_CS_RANGE:
3182 /* TODO */
3183 break;
3184
3185 case UAC2_CS_MEM:
3186 /* TODO */
3187 break;
3188
3189 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003190 usb_audio_dbg(mixer->chip,
3191 "unknown attribute %d in interrupt\n",
3192 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02003193 break;
3194 } /* switch */
3195 }
3196}
3197
3198static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003199{
3200 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02003201 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01003202 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003203
Oliver Neukumedf7de32011-03-11 13:19:43 +01003204 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02003205 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003206
Daniel Macke213e9c2010-05-11 18:13:50 +02003207 if (mixer->protocol == UAC_VERSION_1) {
3208 struct uac1_status_word *status;
3209
3210 for (status = urb->transfer_buffer;
3211 len >= sizeof(*status);
3212 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003213 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02003214 status->bStatusType,
3215 status->bOriginator);
3216
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003217 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02003218 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3219 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003220 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003221
3222 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3223 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003224 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003225 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3226 }
3227 } else { /* UAC_VERSION_2 */
3228 struct uac2_interrupt_data_msg *msg;
3229
3230 for (msg = urb->transfer_buffer;
3231 len >= sizeof(*msg);
3232 len -= sizeof(*msg), msg++) {
3233 /* drop vendor specific and endpoint requests */
3234 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3235 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3236 continue;
3237
3238 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3239 le16_to_cpu(msg->wValue),
3240 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003241 }
3242 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003243
3244requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003245 if (ustatus != -ENOENT &&
3246 ustatus != -ECONNRESET &&
3247 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003248 urb->dev = mixer->chip->dev;
3249 usb_submit_urb(urb, GFP_ATOMIC);
3250 }
3251}
3252
3253/* create the handler for the optional status interrupt endpoint */
3254static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3255{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003256 struct usb_endpoint_descriptor *ep;
3257 void *transfer_buffer;
3258 int buffer_length;
3259 unsigned int epnum;
3260
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003261 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003262 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003263 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003264 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003265 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003266 return 0;
3267
Julia Lawall42a6e662008-12-29 11:23:02 +01003268 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003269 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3270 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3271 if (!transfer_buffer)
3272 return -ENOMEM;
3273 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3274 if (!mixer->urb) {
3275 kfree(transfer_buffer);
3276 return -ENOMEM;
3277 }
3278 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3279 usb_rcvintpipe(mixer->chip->dev, epnum),
3280 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003281 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003282 usb_submit_urb(mixer->urb, GFP_KERNEL);
3283 return 0;
3284}
3285
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003286static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3287 struct snd_ctl_elem_value *ucontrol)
3288{
3289 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3290
3291 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3292 return 0;
3293}
3294
3295static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3296 struct snd_ctl_elem_value *ucontrol)
3297{
3298 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3299 bool keep_iface = !!ucontrol->value.integer.value[0];
3300
3301 if (mixer->chip->keep_iface == keep_iface)
3302 return 0;
3303 mixer->chip->keep_iface = keep_iface;
3304 return 1;
3305}
3306
3307static const struct snd_kcontrol_new keep_iface_ctl = {
3308 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3309 .name = "Keep Interface",
3310 .info = snd_ctl_boolean_mono_info,
3311 .get = keep_iface_ctl_get,
3312 .put = keep_iface_ctl_put,
3313};
3314
3315static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3316{
3317 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3318
3319 /* need only one control per card */
3320 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3321 snd_ctl_free_one(kctl);
3322 return 0;
3323 }
3324
3325 return snd_ctl_add(mixer->chip->card, kctl);
3326}
3327
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003328int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3329 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003330{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003331 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003332 .dev_free = snd_usb_mixer_dev_free
3333 };
3334 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003335 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01003336 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003337
3338 strcpy(chip->card->mixername, "USB Mixer");
3339
Takashi Iwai561b2202005-09-09 14:22:34 +02003340 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003341 if (!mixer)
3342 return -ENOMEM;
3343 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003344 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003345 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3346 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003347 if (!mixer->id_elems) {
3348 kfree(mixer);
3349 return -ENOMEM;
3350 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003351
Daniel Mack1faa5d02011-08-04 15:56:28 +02003352 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3353 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003354 case UAC_VERSION_1:
3355 default:
3356 mixer->protocol = UAC_VERSION_1;
3357 break;
3358 case UAC_VERSION_2:
3359 mixer->protocol = UAC_VERSION_2;
3360 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003361 case UAC_VERSION_3:
3362 mixer->protocol = UAC_VERSION_3;
3363 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003364 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003365
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003366 if (mixer->protocol == UAC_VERSION_3 &&
3367 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003368 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3369 if (err < 0)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003370 goto _error;
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003371 } else {
3372 err = snd_usb_mixer_controls(mixer);
3373 if (err < 0)
3374 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003375 }
Jorge Sanjuanad6baae2018-06-14 15:05:58 +01003376
3377 err = snd_usb_mixer_status_create(mixer);
3378 if (err < 0)
3379 goto _error;
3380
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003381 err = create_keep_iface_ctl(mixer);
3382 if (err < 0)
3383 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003384
Daniel Mack7b1eda22010-03-11 21:13:22 +01003385 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003386
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003387 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003388 if (err < 0)
3389 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003390
3391 if (list_empty(&chip->mixer_list) &&
3392 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3393 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3394
Clemens Ladisch84957a82005-04-29 16:23:13 +02003395 list_add(&mixer->list, &chip->mixer_list);
3396 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003397
3398_error:
3399 snd_usb_mixer_free(mixer);
3400 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003401}
3402
Takashi Iwaia6cece92014-10-31 11:24:32 +01003403void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003404{
Takashi Iwai124751d2017-10-10 14:10:32 +02003405 if (mixer->disconnected)
3406 return;
3407 if (mixer->urb)
3408 usb_kill_urb(mixer->urb);
3409 if (mixer->rc_urb)
3410 usb_kill_urb(mixer->rc_urb);
3411 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003412}
Takashi Iwai400362f2014-01-20 16:51:16 +01003413
3414#ifdef CONFIG_PM
3415/* stop any bus activity of a mixer */
3416static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3417{
3418 usb_kill_urb(mixer->urb);
3419 usb_kill_urb(mixer->rc_urb);
3420}
3421
3422static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3423{
3424 int err;
3425
3426 if (mixer->urb) {
3427 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3428 if (err < 0)
3429 return err;
3430 }
3431
3432 return 0;
3433}
3434
3435int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3436{
3437 snd_usb_mixer_inactivate(mixer);
3438 return 0;
3439}
3440
Takashi Iwai3360b842014-11-18 11:47:04 +01003441static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003442{
Takashi Iwai8c558072018-05-03 12:33:32 +02003443 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003444 int c, err, idx;
3445
3446 if (cval->cmask) {
3447 idx = 0;
3448 for (c = 0; c < MAX_CHANNELS; c++) {
3449 if (!(cval->cmask & (1 << c)))
3450 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003451 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003452 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003453 cval->cache_val[idx]);
3454 if (err < 0)
3455 return err;
3456 }
3457 idx++;
3458 }
3459 } else {
3460 /* master */
3461 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003462 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003463 if (err < 0)
3464 return err;
3465 }
3466 }
3467
3468 return 0;
3469}
3470
3471int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3472{
Takashi Iwai3360b842014-11-18 11:47:04 +01003473 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003474 int id, err;
3475
Takashi Iwai400362f2014-01-20 16:51:16 +01003476 if (reset_resume) {
3477 /* restore cached mixer values */
3478 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003479 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003480 if (list->resume) {
3481 err = list->resume(list);
3482 if (err < 0)
3483 return err;
3484 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003485 }
3486 }
3487 }
3488
Takashi Iwai964af632018-04-27 14:23:37 +02003489 snd_usb_mixer_resume_quirk(mixer);
3490
Takashi Iwai400362f2014-01-20 16:51:16 +01003491 return snd_usb_mixer_activate(mixer);
3492}
3493#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003494
3495void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3496 struct usb_mixer_interface *mixer,
3497 int unitid)
3498{
3499 list->mixer = mixer;
3500 list->id = unitid;
3501 list->dump = snd_usb_mixer_dump_cval;
3502#ifdef CONFIG_PM
3503 list->resume = restore_mixer_value;
3504#endif
3505}