blob: bf74e7edc92b011a054d939b9853f4194b4ef78b [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++;
Daniel Mackef9d5972011-05-25 09:09:00 +0200601 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200602 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
603 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100606 list->kctl = kctl;
607 list->next_id_elem = mixer->id_elems[list->id];
608 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * get a terminal name string
614 */
615
616static struct iterm_name_combo {
617 int type;
618 char *name;
619} iterm_names[] = {
620 { 0x0300, "Output" },
621 { 0x0301, "Speaker" },
622 { 0x0302, "Headphone" },
623 { 0x0303, "HMD Audio" },
624 { 0x0304, "Desktop Speaker" },
625 { 0x0305, "Room Speaker" },
626 { 0x0306, "Com Speaker" },
627 { 0x0307, "LFE" },
628 { 0x0600, "External In" },
629 { 0x0601, "Analog In" },
630 { 0x0602, "Digital In" },
631 { 0x0603, "Line" },
632 { 0x0604, "Legacy In" },
633 { 0x0605, "IEC958 In" },
634 { 0x0606, "1394 DA Stream" },
635 { 0x0607, "1394 DV Stream" },
636 { 0x0700, "Embedded" },
637 { 0x0701, "Noise Source" },
638 { 0x0702, "Equalization Noise" },
639 { 0x0703, "CD" },
640 { 0x0704, "DAT" },
641 { 0x0705, "DCC" },
642 { 0x0706, "MiniDisk" },
643 { 0x0707, "Analog Tape" },
644 { 0x0708, "Phonograph" },
645 { 0x0709, "VCR Audio" },
646 { 0x070a, "Video Disk Audio" },
647 { 0x070b, "DVD Audio" },
648 { 0x070c, "TV Tuner Audio" },
649 { 0x070d, "Satellite Rec Audio" },
650 { 0x070e, "Cable Tuner Audio" },
651 { 0x070f, "DSS Audio" },
652 { 0x0710, "Radio Receiver" },
653 { 0x0711, "Radio Transmitter" },
654 { 0x0712, "Multi-Track Recorder" },
655 { 0x0713, "Synthesizer" },
656 { 0 },
657};
658
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300659static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned char *name, int maxlen, int term_only)
661{
662 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100663 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100665 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300666 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200667 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100668 if (len)
669 return len;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* virtual type - not a real terminal */
673 if (iterm->type >> 16) {
674 if (term_only)
675 return 0;
676 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100677 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200678 strcpy(name, "Selector");
679 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200680 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200681 strcpy(name, "Process Unit");
682 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200683 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200684 strcpy(name, "Ext Unit");
685 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100686 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200687 strcpy(name, "Mixer");
688 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 default:
690 return sprintf(name, "Unit %d", iterm->id);
691 }
692 }
693
694 switch (iterm->type & 0xff00) {
695 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200696 strcpy(name, "PCM");
697 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200699 strcpy(name, "Mic");
700 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200702 strcpy(name, "Headset");
703 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200705 strcpy(name, "Phone");
706 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
Daniel Mack6bc170e2014-05-24 10:58:16 +0200709 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (names->type == iterm->type) {
711 strcpy(name, names->name);
712 return strlen(names->name);
713 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100720 * Get logical cluster information for UAC3 devices.
721 */
722static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
723{
724 struct uac3_cluster_header_descriptor c_header;
725 int err;
726
727 err = snd_usb_ctl_msg(state->chip->dev,
728 usb_rcvctrlpipe(state->chip->dev, 0),
729 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
730 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
731 cluster_id,
732 snd_usb_ctrl_intf(state->chip),
733 &c_header, sizeof(c_header));
734 if (err < 0)
735 goto error;
736 if (err != sizeof(c_header)) {
737 err = -EIO;
738 goto error;
739 }
740
741 return c_header.bNrChannels;
742
743error:
744 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
745 return err;
746}
747
748/*
749 * Get number of channels for a Mixer Unit.
750 */
751static int uac_mixer_unit_get_channels(struct mixer_build *state,
752 struct uac_mixer_unit_descriptor *desc)
753{
754 int mu_channels;
755
756 if (desc->bLength < 11)
757 return -EINVAL;
758 if (!desc->bNrInPins)
759 return -EINVAL;
760
761 switch (state->mixer->protocol) {
762 case UAC_VERSION_1:
763 case UAC_VERSION_2:
764 default:
765 mu_channels = uac_mixer_unit_bNrChannels(desc);
766 break;
767 case UAC_VERSION_3:
768 mu_channels = get_cluster_channels_v3(state,
769 uac3_mixer_unit_wClusterDescrID(desc));
770 break;
771 }
772
773 if (!mu_channels)
774 return -EINVAL;
775
776 return mu_channels;
777}
778
779/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * parse the source unit recursively until it reaches to a terminal
781 * or a branched unit.
782 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200783static int check_input_term(struct mixer_build *state, int id,
784 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200786 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200787 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100788 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 memset(term, 0, sizeof(*term));
791 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100792 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200794
795 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
796 switch (hdr[2]) {
797 case UAC_INPUT_TERMINAL:
798 if (protocol == UAC_VERSION_1) {
799 struct uac_input_terminal_descriptor *d = p1;
800
801 term->type = le16_to_cpu(d->wTerminalType);
802 term->channels = d->bNrChannels;
803 term->chconfig = le16_to_cpu(d->wChannelConfig);
804 term->name = d->iTerminal;
805 } else { /* UAC_VERSION_2 */
806 struct uac2_input_terminal_descriptor *d = p1;
807
808 /* call recursively to verify that the
809 * referenced clock entity is valid */
810 err = check_input_term(state, d->bCSourceID, term);
811 if (err < 0)
812 return err;
813
814 /* save input term properties after recursion,
815 * to ensure they are not overriden by the
816 * recursion calls */
817 term->id = id;
818 term->type = le16_to_cpu(d->wTerminalType);
819 term->channels = d->bNrChannels;
820 term->chconfig = le32_to_cpu(d->bmChannelConfig);
821 term->name = d->iTerminal;
822 }
823 return 0;
824 case UAC_FEATURE_UNIT: {
825 /* the header is the same for v1 and v2 */
826 struct uac_feature_unit_descriptor *d = p1;
827
828 id = d->bSourceID;
829 break; /* continue to parse */
830 }
831 case UAC_MIXER_UNIT: {
832 struct uac_mixer_unit_descriptor *d = p1;
833
834 term->type = d->bDescriptorSubtype << 16; /* virtual type */
835 term->channels = uac_mixer_unit_bNrChannels(d);
836 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
837 term->name = uac_mixer_unit_iMixer(d);
838 return 0;
839 }
840 case UAC_SELECTOR_UNIT:
841 case UAC2_CLOCK_SELECTOR: {
842 struct uac_selector_unit_descriptor *d = p1;
843 /* call recursively to retrieve the channel info */
844 err = check_input_term(state, d->baSourceID[0], term);
845 if (err < 0)
846 return err;
847 term->type = d->bDescriptorSubtype << 16; /* virtual type */
848 term->id = id;
849 term->name = uac_selector_unit_iSelector(d);
850 return 0;
851 }
852 case UAC1_PROCESSING_UNIT:
853 case UAC1_EXTENSION_UNIT:
854 /* UAC2_PROCESSING_UNIT_V2 */
855 /* UAC2_EFFECT_UNIT */
856 case UAC2_EXTENSION_UNIT_V2: {
857 struct uac_processing_unit_descriptor *d = p1;
858
859 if (protocol == UAC_VERSION_2 &&
860 hdr[2] == UAC2_EFFECT_UNIT) {
861 /* UAC2/UAC1 unit IDs overlap here in an
862 * uncompatible way. Ignore this unit for now.
863 */
864 return 0;
865 }
866
867 if (d->bNrInPins) {
868 id = d->baSourceID[0];
869 break; /* continue to parse */
870 }
871 term->type = d->bDescriptorSubtype << 16; /* virtual type */
872 term->channels = uac_processing_unit_bNrChannels(d);
873 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
874 term->name = uac_processing_unit_iProcessing(d, protocol);
875 return 0;
876 }
877 case UAC2_CLOCK_SOURCE: {
878 struct uac_clock_source_descriptor *d = p1;
879
880 term->type = d->bDescriptorSubtype << 16; /* virtual type */
881 term->id = id;
882 term->name = d->iClockSource;
883 return 0;
884 }
885 default:
886 return -ENODEV;
887 }
888 } else { /* UAC_VERSION_3 */
889 switch (hdr[2]) {
890 case UAC_INPUT_TERMINAL: {
891 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200892
893 /* call recursively to verify that the
894 * referenced clock entity is valid */
895 err = check_input_term(state, d->bCSourceID, term);
896 if (err < 0)
897 return err;
898
899 /* save input term properties after recursion,
900 * to ensure they are not overriden by the
901 * recursion calls */
902 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100903 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100904
Jorge Sanjuan71066942018-05-14 12:03:42 +0100905 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
906 if (err < 0)
907 return err;
908 term->channels = err;
909
910 /* REVISIT: UAC3 IT doesn't have channels cfg */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200911 term->chconfig = 0;
912
913 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100914 return 0;
915 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200916 case UAC3_FEATURE_UNIT: {
917 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100918
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200919 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 break; /* continue to parse */
921 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200922 case UAC3_CLOCK_SOURCE: {
923 struct uac3_clock_source_descriptor *d = p1;
924
925 term->type = d->bDescriptorSubtype << 16; /* virtual type */
926 term->id = id;
927 term->name = le16_to_cpu(d->wClockSourceStr);
928 return 0;
929 }
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100930 case UAC3_MIXER_UNIT: {
931 struct uac_mixer_unit_descriptor *d = p1;
932
933 err = uac_mixer_unit_get_channels(state, d);
934 if (err < 0)
935 return err;
936
937 term->channels = err;
938 term->type = d->bDescriptorSubtype << 16; /* virtual type */
939
940 return 0;
941 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200942 default:
943 return -ENODEV;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 }
947 return -ENODEV;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/*
951 * Feature Unit
952 */
953
954/* feature unit control information */
955struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700956 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200958 int type; /* data type for uac1 */
959 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960};
961
962static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700963 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
964 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
965 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
966 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
967 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
968 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
969 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
970 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
971 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
972 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200973 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700974 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
975 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
976 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977};
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600980void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Jesper Juhl4d572772005-05-30 17:30:32 +0200982 kfree(kctl->private_data);
983 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * interface to ALSA control for feature/mixer units
988 */
989
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100990/* volume control quirks */
991static void volume_control_quirks(struct usb_mixer_elem_info *cval,
992 struct snd_kcontrol *kctl)
993{
Takashi Iwai3360b842014-11-18 11:47:04 +0100994 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100995 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +0100996 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500997 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +0100998 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
999 cval->min = 0x0000;
1000 cval->max = 0xffff;
1001 cval->res = 0x00e6;
1002 break;
1003 }
1004 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1005 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1006 cval->min = 0x00;
1007 cval->max = 0xff;
1008 break;
1009 }
1010 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1011 cval->min = 0xb706;
1012 cval->max = 0xff7b;
1013 cval->res = 0x0073;
1014 break;
1015 }
1016 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1017 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1018 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1019 cval->max = 0xfcfe;
1020 cval->res = 0x0073;
1021 }
1022 break;
1023
Felix Homannd34bf142012-04-23 20:24:27 +02001024 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1025 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1026 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001027 usb_audio_info(chip,
1028 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001029 cval->min = 0x0000;
1030 cval->max = 0x7f00;
1031 cval->res = 0x0100;
1032 break;
1033 }
1034 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1035 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001036 usb_audio_info(chip,
1037 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001038 cval->min = 0x00;
1039 cval->max = 0x7f;
1040 break;
1041 }
1042 break;
1043
Federico Cuello21493312018-05-09 00:13:38 +02001044 case USB_ID(0x0d8c, 0x0103):
1045 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1046 usb_audio_info(chip,
1047 "set volume quirk for CM102-A+/102S+\n");
1048 cval->min = -256;
1049 }
1050 break;
1051
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001052 case USB_ID(0x0471, 0x0101):
1053 case USB_ID(0x0471, 0x0104):
1054 case USB_ID(0x0471, 0x0105):
1055 case USB_ID(0x0672, 0x1041):
1056 /* quirk for UDA1321/N101.
1057 * note that detection between firmware 2.1.1.7 (N101)
1058 * and later 2.1.1.21 is not very clear from datasheets.
1059 * I hope that the min value is -15360 for newer firmware --jk
1060 */
1061 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1062 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001063 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001064 "set volume quirk for UDA1321/N101 chip\n");
1065 cval->max = -256;
1066 }
1067 break;
1068
1069 case USB_ID(0x046d, 0x09a4):
1070 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001071 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001072 "set volume quirk for QuickCam E3500\n");
1073 cval->min = 6080;
1074 cval->max = 8768;
1075 cval->res = 192;
1076 }
1077 break;
1078
Takashi Iwaie805ca82014-03-05 12:34:39 +01001079 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001080 case USB_ID(0x046d, 0x0808):
1081 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001082 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001083 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001084 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001085 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001086 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001087 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001088 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001089 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001090 /* Most audio usb devices lie about volume resolution.
1091 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001092 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001093 */
1094 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001095 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001096 "set resolution quirk: cval->res = 384\n");
1097 cval->res = 384;
1098 }
1099 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001100 }
1101}
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/*
1104 * retrieve the minimum and maximum values for the specified control
1105 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001106static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1107 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 /* for failsafe */
1110 cval->min = default_min;
1111 cval->max = cval->min + 1;
1112 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001113 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (cval->val_type == USB_MIXER_BOOLEAN ||
1116 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1117 cval->initialized = 1;
1118 } else {
1119 int minchn = 0;
1120 if (cval->cmask) {
1121 int i;
1122 for (i = 0; i < MAX_CHANNELS; i++)
1123 if (cval->cmask & (1 << i)) {
1124 minchn = i + 1;
1125 break;
1126 }
1127 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001128 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1129 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001130 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001131 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001132 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1133 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -EINVAL;
1135 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001136 if (get_ctl_value(cval, UAC_GET_RES,
1137 (cval->control << 8) | minchn,
1138 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 cval->res = 1;
1140 } else {
1141 int last_valid_res = cval->res;
1142
1143 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001144 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001145 (cval->control << 8) | minchn,
1146 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
1148 cval->res /= 2;
1149 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001150 if (get_ctl_value(cval, UAC_GET_RES,
1151 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 cval->res = last_valid_res;
1153 }
1154 if (cval->res == 0)
1155 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001156
1157 /* Additional checks for the proper resolution
1158 *
1159 * Some devices report smaller resolutions than actually
1160 * reacting. They don't return errors but simply clip
1161 * to the lower aligned value.
1162 */
1163 if (cval->min + cval->res < cval->max) {
1164 int last_valid_res = cval->res;
1165 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001166 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001167 for (;;) {
1168 test = saved;
1169 if (test < cval->max)
1170 test += cval->res;
1171 else
1172 test -= cval->res;
1173 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001174 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001175 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001176 cval->res = last_valid_res;
1177 break;
1178 }
1179 if (test == check)
1180 break;
1181 cval->res *= 2;
1182 }
Chris J Argeseef90452014-11-12 12:07:01 -06001183 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001184 }
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 cval->initialized = 1;
1187 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001188
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001189 if (kctl)
1190 volume_control_quirks(cval, kctl);
1191
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001192 /* USB descriptions contain the dB scale in 1/256 dB unit
1193 * while ALSA TLV contains in 1/100 dB unit
1194 */
1195 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1196 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1197 if (cval->dBmin > cval->dBmax) {
1198 /* something is wrong; assume it's either from/to 0dB */
1199 if (cval->dBmin < 0)
1200 cval->dBmax = 0;
1201 else if (cval->dBmin > 0)
1202 cval->dBmin = 0;
1203 if (cval->dBmin > cval->dBmax) {
1204 /* totally crap, return an error */
1205 return -EINVAL;
1206 }
1207 }
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return 0;
1210}
1211
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001212#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001215static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1216 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001218 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 if (cval->val_type == USB_MIXER_BOOLEAN ||
1221 cval->val_type == USB_MIXER_INV_BOOLEAN)
1222 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1223 else
1224 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1225 uinfo->count = cval->channels;
1226 if (cval->val_type == USB_MIXER_BOOLEAN ||
1227 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1228 uinfo->value.integer.min = 0;
1229 uinfo->value.integer.max = 1;
1230 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001231 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001232 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001233 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1234 kcontrol->vd[0].access &=
1235 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1236 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001237 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001238 SNDRV_CTL_EVENT_MASK_INFO,
1239 &kcontrol->id);
1240 }
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001243 uinfo->value.integer.max =
1244 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 return 0;
1247}
1248
1249/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001250static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1251 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001253 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int c, cnt, val, err;
1255
Takashi Iwai641b4872009-01-15 17:05:24 +01001256 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (cval->cmask) {
1258 cnt = 0;
1259 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001260 if (!(cval->cmask & (1 << c)))
1261 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001262 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001263 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001264 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001265 val = get_relative_value(cval, val);
1266 ucontrol->value.integer.value[cnt] = val;
1267 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 } else {
1271 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001272 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001273 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001274 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 val = get_relative_value(cval, val);
1276 ucontrol->value.integer.value[0] = val;
1277 }
1278 return 0;
1279}
1280
1281/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001282static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1283 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001285 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int c, cnt, val, oval, err;
1287 int changed = 0;
1288
1289 if (cval->cmask) {
1290 cnt = 0;
1291 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001292 if (!(cval->cmask & (1 << c)))
1293 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001294 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001295 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001296 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001297 val = ucontrol->value.integer.value[cnt];
1298 val = get_abs_value(cval, val);
1299 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001300 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001301 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001303 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 } else {
1306 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001307 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001309 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 val = ucontrol->value.integer.value[0];
1311 val = get_abs_value(cval, val);
1312 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001313 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 changed = 1;
1315 }
1316 }
1317 return changed;
1318}
1319
Andrew Chant568fa7e2018-03-23 19:25:24 -07001320/* get the boolean value from the master channel of a UAC control */
1321static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1322 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001323{
1324 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1325 int val, err;
1326
1327 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1328 if (err < 0)
1329 return filter_error(cval, err);
1330 val = (val != 0);
1331 ucontrol->value.integer.value[0] = val;
1332 return 0;
1333}
1334
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001335/* get the connectors status and report it as boolean type */
1336static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1337 struct snd_ctl_elem_value *ucontrol)
1338{
1339 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1340 struct snd_usb_audio *chip = cval->head.mixer->chip;
1341 int idx = 0, validx, ret, val;
1342
1343 validx = cval->control << 8 | 0;
1344
1345 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1346 if (ret)
1347 goto error;
1348
1349 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1350 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1351 struct uac2_connectors_ctl_blk uac2_conn;
1352
1353 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1354 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1355 validx, idx, &uac2_conn, sizeof(uac2_conn));
1356 val = !!uac2_conn.bNrChannels;
1357 } else { /* UAC_VERSION_3 */
1358 struct uac3_insertion_ctl_blk uac3_conn;
1359
1360 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1361 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1362 validx, idx, &uac3_conn, sizeof(uac3_conn));
1363 val = !!uac3_conn.bmConInserted;
1364 }
1365
1366 snd_usb_unlock_shutdown(chip);
1367
1368 if (ret < 0) {
1369error:
1370 usb_audio_err(chip,
1371 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1372 UAC_GET_CUR, validx, idx, cval->val_type);
1373 return ret;
1374 }
1375
1376 ucontrol->value.integer.value[0] = val;
1377 return 0;
1378}
1379
Takashi Iwai86e07d32005-11-17 15:08:02 +01001380static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1382 .name = "", /* will be filled later manually */
1383 .info = mixer_ctl_feature_info,
1384 .get = mixer_ctl_feature_get,
1385 .put = mixer_ctl_feature_put,
1386};
1387
Daniel Mack23caaf12010-03-11 21:13:25 +01001388/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301389static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001390 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1391 .name = "", /* will be filled later manually */
1392 .info = mixer_ctl_feature_info,
1393 .get = mixer_ctl_feature_get,
1394 .put = NULL,
1395};
1396
Andrew Chant568fa7e2018-03-23 19:25:24 -07001397/*
1398 * A control which shows the boolean value from reading a UAC control on
1399 * the master channel.
1400 */
1401static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001402 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1403 .name = "", /* will be filled later manually */
1404 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1405 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001406 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001407 .put = NULL,
1408};
1409
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001410static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1411 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1412 .name = "", /* will be filled later manually */
1413 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1414 .info = snd_ctl_boolean_mono_info,
1415 .get = mixer_ctl_connector_get,
1416 .put = NULL,
1417};
1418
Daniel Mack6bc170e2014-05-24 10:58:16 +02001419/*
1420 * This symbol is exported in order to allow the mixer quirks to
1421 * hook up to the standard feature unit control mechanism
1422 */
Daniel Mack9e386582011-05-25 09:09:01 +02001423struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425/*
1426 * build a feature control
1427 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001428static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1429{
1430 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1431}
1432
Daniel Mack6bc170e2014-05-24 10:58:16 +02001433/*
1434 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1435 * rename it to "Headphone". We determine if something is a headphone
1436 * similar to how udev determines form factor.
1437 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001438static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1439 struct snd_card *card)
1440{
1441 const char *names_to_check[] = {
1442 "Headset", "headset", "Headphone", "headphone", NULL};
1443 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001444 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001445
1446 if (strcmp("Speaker", kctl->id.name))
1447 return;
1448
1449 for (s = names_to_check; *s; s++)
1450 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001451 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001452 break;
1453 }
1454
1455 if (!found)
1456 return;
1457
1458 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1459}
1460
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001461static struct usb_feature_control_info *get_feature_control_info(int control)
1462{
1463 int i;
1464
1465 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1466 if (audio_feature_info[i].control == control)
1467 return &audio_feature_info[i];
1468 }
1469 return NULL;
1470}
1471
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001472static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1473 const struct usbmix_name_map *imap,
1474 unsigned int ctl_mask, int control,
1475 struct usb_audio_term *iterm,
1476 struct usb_audio_term *oterm,
1477 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Julian Scheelbc18e312015-08-14 16:14:45 +02001479 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 unsigned int len = 0;
1481 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001482 struct snd_kcontrol *kctl;
1483 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001484 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001485 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Daniel Mack65f25da2010-05-31 13:35:41 +02001487 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* FIXME: not supported yet */
1489 return;
1490 }
1491
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001492 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001493 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return;
1495
Takashi Iwai561b2202005-09-09 14:22:34 +02001496 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001497 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001499 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 cval->control = control;
1501 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001502
1503 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001504 if (!ctl_info) {
1505 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001506 return;
Colin Ian King62376022018-03-27 15:30:01 +01001507 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001508 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001509 cval->val_type = ctl_info->type;
1510 else /* UAC_VERSION_2 */
1511 cval->val_type = ctl_info->type_uac2 >= 0 ?
1512 ctl_info->type_uac2 : ctl_info->type;
1513
Daniel Macka6a33252010-05-31 13:35:37 +02001514 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001516 cval->master_readonly = readonly_mask;
1517 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int i, c = 0;
1519 for (i = 0; i < 16; i++)
1520 if (ctl_mask & (1 << i))
1521 c++;
1522 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001523 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
Daniel Mack6bc170e2014-05-24 10:58:16 +02001526 /*
1527 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001528 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001529 * issue write commands to read-only channels.
1530 */
Daniel Macka6a33252010-05-31 13:35:37 +02001531 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001532 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1533 else
1534 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1535
Daniel Mack6bc170e2014-05-24 10:58:16 +02001536 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001537 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 kfree(cval);
1539 return;
1540 }
Chris J Argeseef90452014-11-12 12:07:01 -06001541 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001543 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001545 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001546 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001547 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001550 case UAC_FU_MUTE:
1551 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001552 /*
1553 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * - if a name id is given in descriptor, use it.
1555 * - if the connected input can be determined, then use the name
1556 * of terminal type.
1557 * - if the connected output can be determined, use it.
1558 * - otherwise, anonymous name.
1559 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001560 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001561 if (iterm)
1562 len = get_term_name(mixer->chip, iterm,
1563 kctl->id.name,
1564 sizeof(kctl->id.name), 1);
1565 if (!len && oterm)
1566 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001567 kctl->id.name,
1568 sizeof(kctl->id.name), 1);
1569 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001570 snprintf(kctl->id.name, sizeof(kctl->id.name),
1571 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001573
1574 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001575 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001576
Daniel Mack6bc170e2014-05-24 10:58:16 +02001577 /*
1578 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * if the connected output is USB stream, then it's likely a
1580 * capture stream. otherwise it should be playback (hopefully :)
1581 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001582 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1583 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001584 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001585 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001586 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001588 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001589 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001592 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1594 sizeof(kctl->id.name));
1595 break;
1596 }
1597
Takashi Iwaie1825342012-05-14 17:11:06 +02001598 /* get min/max values */
1599 get_min_max_with_quirks(cval, 0, kctl);
1600
1601 if (control == UAC_FU_VOLUME) {
1602 check_mapped_dB(map, cval);
1603 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1604 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1605 kctl->vd[0].access |=
1606 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1607 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1608 }
1609 }
1610
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001611 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001612
Alexey Fisher80aceff2011-03-10 14:53:38 +01001613 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001614 /*
1615 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001616 * to be sure. 384 is a resolution magic number found on Logitech
1617 * devices. It will definitively catch all buggy Logitech devices.
1618 */
1619 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001620 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001621 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001622 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001623 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001624 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001625 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001626 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001627 }
1628
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001629 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001630 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001631 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001632 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001635static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1636 unsigned int ctl_mask, int control,
1637 struct usb_audio_term *iterm, int unitid,
1638 int readonly_mask)
1639{
1640 struct uac_feature_unit_descriptor *desc = raw_desc;
1641 int nameid = uac_feature_unit_iFeature(desc);
1642
1643 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1644 iterm, &state->oterm, unitid, nameid, readonly_mask);
1645}
1646
1647static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1648 unsigned int ctl_mask, int control, int unitid,
1649 const struct usbmix_name_map *badd_map)
1650{
1651 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1652 NULL, NULL, unitid, 0, 0);
1653}
1654
Andrew Chant5a222e82018-03-23 19:25:23 -07001655static void get_connector_control_name(struct mixer_build *state,
1656 struct usb_audio_term *term,
1657 bool is_input, char *name, int name_size)
1658{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001659 int name_len = get_term_name(state->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001660
1661 if (name_len == 0)
1662 strlcpy(name, "Unknown", name_size);
1663
1664 /*
1665 * sound/core/ctljack.c has a convention of naming jack controls
1666 * by ending in " Jack". Make it slightly more useful by
1667 * indicating Input or Output after the terminal name.
1668 */
1669 if (is_input)
1670 strlcat(name, " - Input Jack", name_size);
1671 else
1672 strlcat(name, " - Output Jack", name_size);
1673}
1674
1675/* Build a mixer control for a UAC connector control (jack-detect) */
1676static void build_connector_control(struct mixer_build *state,
1677 struct usb_audio_term *term, bool is_input)
1678{
1679 struct snd_kcontrol *kctl;
1680 struct usb_mixer_elem_info *cval;
1681
1682 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1683 if (!cval)
1684 return;
1685 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001686 /*
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001687 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1688 * number of channels connected.
1689 *
1690 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1691 * following byte(s) specifies which connectors are inserted.
1692 *
1693 * This boolean ctl will simply report if any channels are connected
1694 * or not.
Andrew Chant568fa7e2018-03-23 19:25:24 -07001695 */
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001696 if (state->mixer->protocol == UAC_VERSION_2)
1697 cval->control = UAC2_TE_CONNECTOR;
1698 else /* UAC_VERSION_3 */
1699 cval->control = UAC3_TE_INSERTION;
1700
Andrew Chant5a222e82018-03-23 19:25:23 -07001701 cval->val_type = USB_MIXER_BOOLEAN;
1702 cval->channels = 1; /* report true if any channel is connected */
1703 cval->min = 0;
1704 cval->max = 1;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001705 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001706 if (!kctl) {
1707 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1708 kfree(cval);
1709 return;
1710 }
1711 get_connector_control_name(state, term, is_input, kctl->id.name,
1712 sizeof(kctl->id.name));
1713 kctl->private_free = snd_usb_mixer_elem_free;
1714 snd_usb_mixer_add_control(&cval->head, kctl);
1715}
1716
Daniel Mackcddaafb2016-04-09 11:21:46 +02001717static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1718 void *_ftr)
1719{
1720 struct uac_clock_source_descriptor *hdr = _ftr;
1721 struct usb_mixer_elem_info *cval;
1722 struct snd_kcontrol *kctl;
1723 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1724 int ret;
1725
1726 if (state->mixer->protocol != UAC_VERSION_2)
1727 return -EINVAL;
1728
1729 if (hdr->bLength != sizeof(*hdr)) {
1730 usb_audio_dbg(state->chip,
1731 "Bogus clock source descriptor length of %d, ignoring.\n",
1732 hdr->bLength);
1733 return 0;
1734 }
1735
1736 /*
1737 * The only property of this unit we are interested in is the
1738 * clock source validity. If that isn't readable, just bail out.
1739 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001740 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001741 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001742 return 0;
1743
1744 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1745 if (!cval)
1746 return -ENOMEM;
1747
1748 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1749
1750 cval->min = 0;
1751 cval->max = 1;
1752 cval->channels = 1;
1753 cval->val_type = USB_MIXER_BOOLEAN;
1754 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1755
Andrew Chant568fa7e2018-03-23 19:25:24 -07001756 cval->master_readonly = 1;
1757 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1758 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001759
1760 if (!kctl) {
1761 kfree(cval);
1762 return -ENOMEM;
1763 }
1764
1765 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001766 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001767 name, sizeof(name));
1768 if (ret > 0)
1769 snprintf(kctl->id.name, sizeof(kctl->id.name),
1770 "%s Validity", name);
1771 else
1772 snprintf(kctl->id.name, sizeof(kctl->id.name),
1773 "Clock Source %d Validity", hdr->bClockID);
1774
1775 return snd_usb_mixer_add_control(&cval->head, kctl);
1776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/*
1779 * parse a feature unit
1780 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001781 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001783static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1784 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001787 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 unsigned int master_bits, first_ch_bits;
1789 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001790 struct uac_feature_unit_descriptor *hdr = _ftr;
1791 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Daniel Mack23caaf12010-03-11 21:13:25 +01001793 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001794 if (hdr->bLength < 7) {
1795 usb_audio_err(state->chip,
1796 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1797 unitid);
1798 return -EINVAL;
1799 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001800 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001801 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001802 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001803 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001804 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001805 return -EINVAL;
1806 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001807 channels = (hdr->bLength - 7) / csize - 1;
1808 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001809 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001810 usb_audio_err(state->chip,
1811 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1812 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001813 return -EINVAL;
1814 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001815 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001816 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001817 if (hdr->bLength < 6) {
1818 usb_audio_err(state->chip,
1819 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1820 unitid);
1821 return -EINVAL;
1822 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001823 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001824 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001825 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001826 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001827 usb_audio_err(state->chip,
1828 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1829 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001830 return -EINVAL;
1831 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001832 } else { /* UAC_VERSION_3 */
1833 struct uac3_feature_unit_descriptor *ftr = _ftr;
1834
1835 if (hdr->bLength < 7) {
1836 usb_audio_err(state->chip,
1837 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1838 unitid);
1839 return -EINVAL;
1840 }
1841 csize = 4;
1842 channels = (ftr->bLength - 7) / 4 - 1;
1843 bmaControls = ftr->bmaControls;
1844 if (hdr->bLength < 7 + csize) {
1845 usb_audio_err(state->chip,
1846 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1847 unitid);
1848 return -EINVAL;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
1852 /* parse the source unit */
Daniel Mack23caaf12010-03-11 21:13:25 +01001853 if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return err;
1855
1856 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001857 err = check_input_term(state, hdr->bSourceID, &iterm);
1858 if (err < 0)
1859 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Daniel Mack23caaf12010-03-11 21:13:25 +01001861 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001862 /* master configuration quirks */
1863 switch (state->chip->usb_id) {
1864 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001865 usb_audio_info(state->chip,
1866 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001867 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001868 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001869 break;
David Henningssonc1051432012-09-20 10:20:41 +02001870 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001871 usb_audio_info(state->chip,
1872 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001873 /* disable non-functional volume control */
1874 channels = 0;
1875 break;
1876
Javier Kohen0c3cee52009-11-17 15:36:13 +01001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001879 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 else
1881 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001882
1883 if (state->mixer->protocol == UAC_VERSION_1) {
1884 /* check all control types */
1885 for (i = 0; i < 10; i++) {
1886 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001887 int control = audio_feature_info[i].control;
1888
Daniel Mack23caaf12010-03-11 21:13:25 +01001889 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001890 unsigned int mask;
1891
1892 mask = snd_usb_combine_bytes(bmaControls +
1893 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001894 if (mask & (1 << i))
1895 ch_bits |= (1 << j);
1896 }
1897 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001898
1899 /*
1900 * The first channel must be set
1901 * (for ease of programming).
1902 */
1903 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001904 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001905 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001906 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001907 build_feature_ctl(state, _ftr, 0, control,
1908 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001910 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001911 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001912 unsigned int ch_bits = 0;
1913 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001914 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001915
1916 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001917 unsigned int mask;
1918
1919 mask = snd_usb_combine_bytes(bmaControls +
1920 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001921 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001922 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001923 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001924 ch_read_only |= (1 << j);
1925 }
1926 }
1927
Daniel Mack6bc170e2014-05-24 10:58:16 +02001928 /*
1929 * NOTE: build_feature_ctl() will mark the control
1930 * read-only if all channels are marked read-only in
1931 * the descriptors. Otherwise, the control will be
1932 * reported as writeable, but the driver will not
1933 * actually issue a write command for read-only
1934 * channels.
1935 */
1936
1937 /*
1938 * The first channel must be set
1939 * (for ease of programming).
1940 */
1941 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001942 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001943 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001944 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001945 build_feature_ctl(state, _ftr, 0, control,
1946 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001947 !uac_v2v3_control_is_writeable(master_bits,
1948 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 return 0;
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/*
1956 * Mixer Unit
1957 */
1958
1959/*
1960 * build a mixer unit control
1961 *
1962 * the callbacks are identical with feature unit.
1963 * input channel number (zero based) is given in control field instead.
1964 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001965static void build_mixer_unit_ctl(struct mixer_build *state,
1966 struct uac_mixer_unit_descriptor *desc,
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01001967 int in_pin, int in_ch, int num_outs,
1968 int unitid, struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001970 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001972 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001973 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001975 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001976 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return;
1978
Takashi Iwai561b2202005-09-09 14:22:34 +02001979 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001980 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return;
1982
Takashi Iwai3360b842014-11-18 11:47:04 +01001983 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 cval->control = in_ch + 1; /* based on 1 */
1985 cval->val_type = USB_MIXER_S16;
1986 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001987 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1988
1989 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 cval->cmask |= (1 << i);
1991 cval->channels++;
1992 }
1993 }
1994
1995 /* get min/max values */
1996 get_min_max(cval, 0);
1997
1998 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001999 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002000 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 kfree(cval);
2002 return;
2003 }
Chris J Argeseef90452014-11-12 12:07:01 -06002004 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002006 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002007 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002008 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002009 sizeof(kctl->id.name), 0);
2010 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02002012 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002014 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002015 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2016 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Andrew Chant5a222e82018-03-23 19:25:23 -07002019static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2020 void *raw_desc)
2021{
2022 struct usb_audio_term iterm;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002023 unsigned int control, bmctls, term_id;
Andrew Chant5a222e82018-03-23 19:25:23 -07002024
Andrew Chant5a222e82018-03-23 19:25:23 -07002025 if (state->mixer->protocol == UAC_VERSION_2) {
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002026 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2027 control = UAC2_TE_CONNECTOR;
2028 term_id = d_v2->bTerminalID;
2029 bmctls = le16_to_cpu(d_v2->bmControls);
2030 } else if (state->mixer->protocol == UAC_VERSION_3) {
2031 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2032 control = UAC3_TE_INSERTION;
2033 term_id = d_v3->bTerminalID;
2034 bmctls = le32_to_cpu(d_v3->bmControls);
2035 } else {
2036 return 0; /* UAC1. No Insertion control */
Andrew Chant5a222e82018-03-23 19:25:23 -07002037 }
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002038
2039 check_input_term(state, term_id, &iterm);
2040
2041 /* Check for jack detection. */
2042 if (uac_v2v3_control_is_readable(bmctls, control))
2043 build_connector_control(state, &iterm, true);
2044
Andrew Chant5a222e82018-03-23 19:25:23 -07002045 return 0;
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/*
2049 * parse a mixer unit
2050 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002051static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2052 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Daniel Mack99fc8642010-03-11 21:13:24 +01002054 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002055 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int input_pins, num_ins, num_outs;
2057 int pin, ich, err;
2058
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002059 err = uac_mixer_unit_get_channels(state, desc);
2060 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002061 usb_audio_err(state->chip,
2062 "invalid MIXER UNIT descriptor %d\n",
2063 unitid);
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002064 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002067 num_outs = err;
2068 input_pins = desc->bNrInPins;
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 num_ins = 0;
2071 ich = 0;
2072 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002073 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002075 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002076 /* no bmControls field (e.g. Maya44) -> ignore */
2077 if (desc->bLength <= 10 + input_pins)
2078 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002079 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (err < 0)
2081 return err;
2082 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002083 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int och, ich_has_controls = 0;
2085
Daniel Mack6bc170e2014-05-24 10:58:16 +02002086 for (och = 0; och < num_outs; och++) {
2087 __u8 *c = uac_mixer_unit_bmControls(desc,
2088 state->mixer->protocol);
2089
2090 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 ich_has_controls = 1;
2092 break;
2093 }
2094 }
2095 if (ich_has_controls)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002096 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 unitid, &iterm);
2098 }
2099 }
2100 return 0;
2101}
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/*
2104 * Processing Unit / Extension Unit
2105 */
2106
2107/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002108static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2109 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002111 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int err, val;
2113
2114 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002115 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002117 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 val = get_relative_value(cval, val);
2120 ucontrol->value.integer.value[0] = val;
2121 return 0;
2122}
2123
2124/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002125static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002128 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int val, oval, err;
2130
2131 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002132 if (err < 0)
2133 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 val = ucontrol->value.integer.value[0];
2135 val = get_abs_value(cval, val);
2136 if (val != oval) {
2137 set_cur_ctl_value(cval, cval->control << 8, val);
2138 return 1;
2139 }
2140 return 0;
2141}
2142
2143/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302144static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2146 .name = "", /* will be filled later */
2147 .info = mixer_ctl_feature_info,
2148 .get = mixer_ctl_procunit_get,
2149 .put = mixer_ctl_procunit_put,
2150};
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/*
2153 * predefined data for processing units
2154 */
2155struct procunit_value_info {
2156 int control;
2157 char *suffix;
2158 int val_type;
2159 int min_value;
2160};
2161
2162struct procunit_info {
2163 int type;
2164 char *name;
2165 struct procunit_value_info *values;
2166};
2167
2168static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002169 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2170 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 { 0 }
2172};
2173static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002174 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2175 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 { 0 }
2177};
2178static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002179 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2180 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 { 0 }
2182};
2183static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002184 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2185 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2186 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2187 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 { 0 }
2189};
2190static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002191 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2192 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2193 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2194 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 { 0 }
2196};
2197static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002198 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2199 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2200 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2201 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2202 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2203 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 { 0 }
2205};
2206
2207static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002208 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2209 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2210 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2211 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2212 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2213 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 { 0 },
2215};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002216/*
2217 * predefined data for extension units
2218 */
2219static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002220 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2221 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002222};
2223static struct procunit_value_info clock_source_xu_info[] = {
2224 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2225 { 0 }
2226};
2227static struct procunit_value_info spdif_format_xu_info[] = {
2228 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2229 { 0 }
2230};
2231static struct procunit_value_info soft_limit_xu_info[] = {
2232 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2233 { 0 }
2234};
2235static struct procunit_info extunits[] = {
2236 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2237 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2238 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2239 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2240 { 0 }
2241};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/*
2244 * build a processing/extension unit
2245 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002246static int build_audio_procunit(struct mixer_build *state, int unitid,
2247 void *raw_desc, struct procunit_info *list,
2248 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Daniel Mack99fc8642010-03-11 21:13:24 +01002250 struct uac_processing_unit_descriptor *desc = raw_desc;
2251 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002252 struct usb_mixer_elem_info *cval;
2253 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int i, err, nameid, type, len;
2255 struct procunit_info *info;
2256 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002257 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 static struct procunit_value_info default_value_info[] = {
2259 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2260 { 0 }
2261 };
2262 static struct procunit_info default_info = {
2263 0, NULL, default_value_info
2264 };
2265
Daniel Mack23caaf12010-03-11 21:13:25 +01002266 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2267 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002268 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return -EINVAL;
2270 }
2271
2272 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002273 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return err;
2275 }
2276
Daniel Mack99fc8642010-03-11 21:13:24 +01002277 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 for (info = list; info && info->type; info++)
2279 if (info->type == type)
2280 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002281 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 info = &default_info;
2283
2284 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002285 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002286
Daniel Mack6bc170e2014-05-24 10:58:16 +02002287 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 continue;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002289 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002290 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002292 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002293 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002295 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 cval->control = valinfo->control;
2297 cval->val_type = valinfo->val_type;
2298 cval->channels = 1;
2299
2300 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002301 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002302 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /* FIXME: hard-coded */
2304 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002305 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 cval->res = 1;
2307 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002308 } else {
2309 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002310 /*
2311 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002312 * samplerate control quirk
2313 */
2314 cval->min = 0;
2315 cval->max = 5;
2316 cval->res = 1;
2317 cval->initialized = 1;
2318 } else
2319 get_min_max(cval, valinfo->min_value);
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002323 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 kfree(cval);
2325 return -ENOMEM;
2326 }
Chris J Argeseef90452014-11-12 12:07:01 -06002327 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Daniel Mack6bc170e2014-05-24 10:58:16 +02002329 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002330 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002331 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002333 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002334 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 len = 0;
2336 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002337 len = snd_usb_copy_string_desc(state->chip,
2338 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002339 kctl->id.name,
2340 sizeof(kctl->id.name));
2341 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2343 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002344 append_ctl_name(kctl, " ");
2345 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002347 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002348 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002349 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002350 cval->min, cval->max);
2351
Takashi Iwai3360b842014-11-18 11:47:04 +01002352 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002353 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return err;
2355 }
2356 return 0;
2357}
2358
Daniel Mack6bc170e2014-05-24 10:58:16 +02002359static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2360 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002362 return build_audio_procunit(state, unitid, raw_desc,
2363 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Daniel Mack6bc170e2014-05-24 10:58:16 +02002366static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2367 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002369 /*
2370 * Note that we parse extension units with processing unit descriptors.
2371 * That's ok as the layout is the same.
2372 */
2373 return build_audio_procunit(state, unitid, raw_desc,
2374 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377/*
2378 * Selector Unit
2379 */
2380
Daniel Mack6bc170e2014-05-24 10:58:16 +02002381/*
2382 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 * use an enumerator type for routing
2384 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002385static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002388 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002389 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Takashi Iwai5e246b82008-08-08 17:12:47 +02002391 if (snd_BUG_ON(!itemlist))
2392 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002393 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002397static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2398 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002400 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int val, err;
2402
Daniel Mack09414202010-05-31 13:35:44 +02002403 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002405 ucontrol->value.enumerated.item[0] = 0;
2406 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408 val = get_relative_value(cval, val);
2409 ucontrol->value.enumerated.item[0] = val;
2410 return 0;
2411}
2412
2413/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002414static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002417 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 int val, oval, err;
2419
Daniel Mack09414202010-05-31 13:35:44 +02002420 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002421 if (err < 0)
2422 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 val = ucontrol->value.enumerated.item[0];
2424 val = get_abs_value(cval, val);
2425 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002426 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 1;
2428 }
2429 return 0;
2430}
2431
2432/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302433static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2435 .name = "", /* will be filled later */
2436 .info = mixer_ctl_selector_info,
2437 .get = mixer_ctl_selector_get,
2438 .put = mixer_ctl_selector_put,
2439};
2440
Daniel Mack6bc170e2014-05-24 10:58:16 +02002441/*
2442 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 * free both private_data and private_value
2444 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002445static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 int i, num_ins = 0;
2448
2449 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002450 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 num_ins = cval->max;
2452 kfree(cval);
2453 kctl->private_data = NULL;
2454 }
2455 if (kctl->private_value) {
2456 char **itemlist = (char **)kctl->private_value;
2457 for (i = 0; i < num_ins; i++)
2458 kfree(itemlist[i]);
2459 kfree(itemlist);
2460 kctl->private_value = 0;
2461 }
2462}
2463
2464/*
2465 * parse a selector unit
2466 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002467static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2468 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Daniel Mack99fc8642010-03-11 21:13:24 +01002470 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 unsigned int i, nameid, len;
2472 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002473 struct usb_mixer_elem_info *cval;
2474 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002475 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 char **namelist;
2477
Takashi Iwaif658f172017-11-21 17:00:32 +01002478 if (desc->bLength < 5 || !desc->bNrInPins ||
2479 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002480 usb_audio_err(state->chip,
2481 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return -EINVAL;
2483 }
2484
Daniel Mack99fc8642010-03-11 21:13:24 +01002485 for (i = 0; i < desc->bNrInPins; i++) {
2486 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return err;
2488 }
2489
Daniel Mack99fc8642010-03-11 21:13:24 +01002490 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return 0;
2492
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002493 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002494 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
2496
Takashi Iwai561b2202005-09-09 14:22:34 +02002497 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002498 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002500 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 cval->val_type = USB_MIXER_U8;
2502 cval->channels = 1;
2503 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002504 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 cval->res = 1;
2506 cval->initialized = 1;
2507
Johan Rasténa358a0e2014-11-17 08:39:33 +01002508 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002509 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002510 else /* UAC_VERSION_2 */
2511 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2512 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002513
Daniel Mack99fc8642010-03-11 21:13:24 +01002514 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002515 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 kfree(cval);
2517 return -ENOMEM;
2518 }
2519#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002520 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002521 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 len = 0;
2523 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002524 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002525 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 kfree(namelist[i]);
2527 kfree(namelist);
2528 kfree(cval);
2529 return -ENOMEM;
2530 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002531 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2532 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002533 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002534 len = get_term_name(state->chip, &iterm, namelist[i],
2535 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002537 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539
2540 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2541 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002542 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002543 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 kfree(cval);
2545 return -ENOMEM;
2546 }
2547 kctl->private_value = (unsigned long)namelist;
2548 kctl->private_free = usb_mixer_selector_elem_free;
2549
Takashi Iwai5a15f282017-12-18 23:36:57 +01002550 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002551 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002552 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002553 /* no mapping ? */
2554 /* if iSelector is given, use it */
2555 nameid = uac_selector_unit_iSelector(desc);
2556 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002557 len = snd_usb_copy_string_desc(state->chip, nameid,
Takashi Iwai5a15f282017-12-18 23:36:57 +01002558 kctl->id.name,
2559 sizeof(kctl->id.name));
2560 /* ... or pick up the terminal name at next */
2561 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002562 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002564 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002565 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2567
Takashi Iwai5a15f282017-12-18 23:36:57 +01002568 /* and add the proper suffix */
Daniel Mack09414202010-05-31 13:35:44 +02002569 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2570 append_ctl_name(kctl, " Clock Source");
2571 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002572 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002574 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002577 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002578 cval->head.id, kctl->id.name, desc->bNrInPins);
2579 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582/*
2583 * parse an audio unit recursively
2584 */
2585
Takashi Iwai86e07d32005-11-17 15:08:02 +01002586static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002589 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 if (test_and_set_bit(unitid, state->unitbitmap))
2592 return 0; /* the unit already visited */
2593
2594 p1 = find_audio_control_unit(state, unitid);
2595 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002596 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return -EINVAL;
2598 }
2599
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002600 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2601 switch (p1[2]) {
2602 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002603 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002604 case UAC_MIXER_UNIT:
2605 return parse_audio_mixer_unit(state, unitid, p1);
2606 case UAC2_CLOCK_SOURCE:
2607 return parse_clock_source_unit(state, unitid, p1);
2608 case UAC_SELECTOR_UNIT:
2609 case UAC2_CLOCK_SELECTOR:
2610 return parse_audio_selector_unit(state, unitid, p1);
2611 case UAC_FEATURE_UNIT:
2612 return parse_audio_feature_unit(state, unitid, p1);
2613 case UAC1_PROCESSING_UNIT:
2614 /* UAC2_EFFECT_UNIT has the same value */
2615 if (protocol == UAC_VERSION_1)
2616 return parse_audio_processing_unit(state, unitid, p1);
2617 else
2618 return 0; /* FIXME - effect units not implemented yet */
2619 case UAC1_EXTENSION_UNIT:
2620 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2621 if (protocol == UAC_VERSION_1)
2622 return parse_audio_extension_unit(state, unitid, p1);
2623 else /* UAC_VERSION_2 */
2624 return parse_audio_processing_unit(state, unitid, p1);
2625 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002626 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002627 default:
2628 usb_audio_err(state->chip,
2629 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2630 return -EINVAL;
2631 }
2632 } else { /* UAC_VERSION_3 */
2633 switch (p1[2]) {
2634 case UAC_INPUT_TERMINAL:
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002635 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002636 case UAC3_MIXER_UNIT:
2637 return parse_audio_mixer_unit(state, unitid, p1);
2638 case UAC3_CLOCK_SOURCE:
2639 return parse_clock_source_unit(state, unitid, p1);
2640 case UAC3_CLOCK_SELECTOR:
2641 return parse_audio_selector_unit(state, unitid, p1);
2642 case UAC3_FEATURE_UNIT:
2643 return parse_audio_feature_unit(state, unitid, p1);
2644 case UAC3_EFFECT_UNIT:
2645 return 0; /* FIXME - effect units not implemented yet */
2646 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002647 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002648 case UAC3_EXTENSION_UNIT:
2649 return parse_audio_extension_unit(state, unitid, p1);
2650 default:
2651 usb_audio_err(state->chip,
2652 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2653 return -EINVAL;
2654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656}
2657
Clemens Ladisch84957a82005-04-29 16:23:13 +02002658static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2659{
Takashi Iwai124751d2017-10-10 14:10:32 +02002660 /* kill pending URBs */
2661 snd_usb_mixer_disconnect(mixer);
2662
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002663 kfree(mixer->id_elems);
2664 if (mixer->urb) {
2665 kfree(mixer->urb->transfer_buffer);
2666 usb_free_urb(mixer->urb);
2667 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002668 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002669 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002670 kfree(mixer);
2671}
2672
Takashi Iwai86e07d32005-11-17 15:08:02 +01002673static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002674{
2675 struct usb_mixer_interface *mixer = device->device_data;
2676 snd_usb_mixer_free(mixer);
2677 return 0;
2678}
2679
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002680/* UAC3 predefined channels configuration */
2681struct uac3_badd_profile {
2682 int subclass;
2683 const char *name;
2684 int c_chmask; /* capture channels mask */
2685 int p_chmask; /* playback channels mask */
2686 int st_chmask; /* side tone mixing channel mask */
2687};
2688
2689static struct uac3_badd_profile uac3_badd_profiles[] = {
2690 {
2691 /*
2692 * BAIF, BAOF or combination of both
2693 * IN: Mono or Stereo cfg, Mono alt possible
2694 * OUT: Mono or Stereo cfg, Mono alt possible
2695 */
2696 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2697 .name = "GENERIC IO",
2698 .c_chmask = -1, /* dynamic channels */
2699 .p_chmask = -1, /* dynamic channels */
2700 },
2701 {
2702 /* BAOF; Stereo only cfg, Mono alt possible */
2703 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2704 .name = "HEADPHONE",
2705 .p_chmask = 3,
2706 },
2707 {
2708 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2709 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2710 .name = "SPEAKER",
2711 .p_chmask = -1, /* dynamic channels */
2712 },
2713 {
2714 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2715 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2716 .name = "MICROPHONE",
2717 .c_chmask = -1, /* dynamic channels */
2718 },
2719 {
2720 /*
2721 * BAIOF topology
2722 * IN: Mono only
2723 * OUT: Mono or Stereo cfg, Mono alt possible
2724 */
2725 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2726 .name = "HEADSET",
2727 .c_chmask = 1,
2728 .p_chmask = -1, /* dynamic channels */
2729 .st_chmask = 1,
2730 },
2731 {
2732 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2733 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2734 .name = "HEADSET ADAPTER",
2735 .c_chmask = 1,
2736 .p_chmask = 3,
2737 .st_chmask = 1,
2738 },
2739 {
2740 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2741 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2742 .name = "SPEAKERPHONE",
2743 .c_chmask = 1,
2744 .p_chmask = 1,
2745 },
2746 { 0 } /* terminator */
2747};
2748
2749static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2750 struct uac3_badd_profile *f,
2751 int c_chmask, int p_chmask)
2752{
2753 /*
2754 * If both playback/capture channels are dynamic, make sure
2755 * at least one channel is present
2756 */
2757 if (f->c_chmask < 0 && f->p_chmask < 0) {
2758 if (!c_chmask && !p_chmask) {
2759 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2760 f->name);
2761 return false;
2762 }
2763 return true;
2764 }
2765
2766 if ((f->c_chmask < 0 && !c_chmask) ||
2767 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2768 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2769 f->name);
2770 return false;
2771 }
2772 if ((f->p_chmask < 0 && !p_chmask) ||
2773 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2774 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2775 f->name);
2776 return false;
2777 }
2778 return true;
2779}
2780
2781/*
2782 * create mixer controls for UAC3 BADD profiles
2783 *
2784 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2785 *
2786 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2787 */
2788static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2789 int ctrlif)
2790{
2791 struct usb_device *dev = mixer->chip->dev;
2792 struct usb_interface_assoc_descriptor *assoc;
2793 int badd_profile = mixer->chip->badd_profile;
2794 struct uac3_badd_profile *f;
2795 const struct usbmix_ctl_map *map;
2796 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2797 int i;
2798
2799 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2800
2801 /* Detect BADD capture/playback channels from AS EP descriptors */
2802 for (i = 0; i < assoc->bInterfaceCount; i++) {
2803 int intf = assoc->bFirstInterface + i;
2804
2805 struct usb_interface *iface;
2806 struct usb_host_interface *alts;
2807 struct usb_interface_descriptor *altsd;
2808 unsigned int maxpacksize;
2809 char dir_in;
2810 int chmask, num;
2811
2812 if (intf == ctrlif)
2813 continue;
2814
2815 iface = usb_ifnum_to_if(dev, intf);
2816 num = iface->num_altsetting;
2817
2818 if (num < 2)
2819 return -EINVAL;
2820
2821 /*
2822 * The number of Channels in an AudioStreaming interface
2823 * and the audio sample bit resolution (16 bits or 24
2824 * bits) can be derived from the wMaxPacketSize field in
2825 * the Standard AS Audio Data Endpoint descriptor in
2826 * Alternate Setting 1
2827 */
2828 alts = &iface->altsetting[1];
2829 altsd = get_iface_desc(alts);
2830
2831 if (altsd->bNumEndpoints < 1)
2832 return -EINVAL;
2833
2834 /* check direction */
2835 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2836 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2837
2838 switch (maxpacksize) {
2839 default:
2840 usb_audio_err(mixer->chip,
2841 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2842 maxpacksize);
2843 return -EINVAL;
2844 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2845 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2846 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2847 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2848 chmask = 1;
2849 break;
2850 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2851 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2852 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2853 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2854 chmask = 3;
2855 break;
2856 }
2857
2858 if (dir_in)
2859 c_chmask = chmask;
2860 else
2861 p_chmask = chmask;
2862 }
2863
2864 usb_audio_dbg(mixer->chip,
2865 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
2866 badd_profile, c_chmask, p_chmask);
2867
2868 /* check the mapping table */
2869 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
2870 if (map->id == badd_profile)
2871 break;
2872 }
2873
2874 if (!map->id)
2875 return -EINVAL;
2876
2877 for (f = uac3_badd_profiles; f->name; f++) {
2878 if (badd_profile == f->subclass)
2879 break;
2880 }
2881 if (!f->name)
2882 return -EINVAL;
2883 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
2884 return -EINVAL;
2885 st_chmask = f->st_chmask;
2886
2887 /* Playback */
2888 if (p_chmask) {
2889 /* Master channel, always writable */
2890 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2891 UAC3_BADD_FU_ID2, map->map);
2892 /* Mono/Stereo volume channels, always writable */
2893 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
2894 UAC3_BADD_FU_ID2, map->map);
2895 }
2896
2897 /* Capture */
2898 if (c_chmask) {
2899 /* Master channel, always writable */
2900 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2901 UAC3_BADD_FU_ID5, map->map);
2902 /* Mono/Stereo volume channels, always writable */
2903 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
2904 UAC3_BADD_FU_ID5, map->map);
2905 }
2906
2907 /* Side tone-mixing */
2908 if (st_chmask) {
2909 /* Master channel, always writable */
2910 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2911 UAC3_BADD_FU_ID7, map->map);
2912 /* Mono volume channel, always writable */
2913 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
2914 UAC3_BADD_FU_ID7, map->map);
2915 }
2916
2917 return 0;
2918}
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920/*
2921 * create mixer controls
2922 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002923 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002925static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002927 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 int err;
2929 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01002930 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002933 state.chip = mixer->chip;
2934 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002935 state.buffer = mixer->hostif->extra;
2936 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002939 for (map = usbmix_ctl_maps; map->id; map++) {
2940 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002942 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002943 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 break;
2945 }
2946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Daniel Mack23caaf12010-03-11 21:13:25 +01002948 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002949 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2950 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02002951 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002952 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02002953 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01002954
2955 if (desc->bLength < sizeof(*desc))
2956 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002957 /* mark terminal ID as visited */
2958 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002959 state.oterm.id = desc->bTerminalID;
2960 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2961 state.oterm.name = desc->iTerminal;
2962 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002963 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002964 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002965 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002966 struct uac2_output_terminal_descriptor *desc = p;
2967
2968 if (desc->bLength < sizeof(*desc))
2969 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002970 /* mark terminal ID as visited */
2971 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002972 state.oterm.id = desc->bTerminalID;
2973 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2974 state.oterm.name = desc->iTerminal;
2975 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002976 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002977 return err;
Daniel Mack09414202010-05-31 13:35:44 +02002978
Daniel Mack6bc170e2014-05-24 10:58:16 +02002979 /*
2980 * For UAC2, use the same approach to also add the
2981 * clock selectors
2982 */
Daniel Mack09414202010-05-31 13:35:44 +02002983 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002984 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02002985 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07002986
Takashi Iwai2b54f7852018-04-23 15:19:25 +02002987 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07002988 UAC2_TE_CONNECTOR)) {
2989 build_connector_control(&state, &state.oterm,
2990 false);
2991 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002992 } else { /* UAC_VERSION_3 */
2993 struct uac3_output_terminal_descriptor *desc = p;
2994
2995 if (desc->bLength < sizeof(*desc))
2996 continue; /* invalid descriptor? */
2997 /* mark terminal ID as visited */
2998 set_bit(desc->bTerminalID, state.unitbitmap);
2999 state.oterm.id = desc->bTerminalID;
3000 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3001 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3002 err = parse_audio_unit(&state, desc->bSourceID);
3003 if (err < 0 && err != -EINVAL)
3004 return err;
3005
3006 /*
3007 * For UAC3, use the same approach to also add the
3008 * clock selectors
3009 */
3010 err = parse_audio_unit(&state, desc->bCSourceID);
3011 if (err < 0 && err != -EINVAL)
3012 return err;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003013
3014 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3015 UAC3_TE_INSERTION)) {
3016 build_connector_control(&state, &state.oterm,
3017 false);
3018 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return 0;
3023}
Clemens Ladisch84957a82005-04-29 16:23:13 +02003024
Daniel Mack7b1eda22010-03-11 21:13:22 +01003025void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003026{
Takashi Iwai3360b842014-11-18 11:47:04 +01003027 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003028
Takashi Iwai8c558072018-05-03 12:33:32 +02003029 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01003030 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02003031 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01003032 /* invalidate cache, so the value is read from the device */
3033 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003034 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003035 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01003036 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003037}
3038
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003039static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01003040 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003041{
Takashi Iwai8c558072018-05-03 12:33:32 +02003042 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003043 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3044 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003045 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01003046 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003047 cval->control, cval->cmask, cval->channels,
3048 val_types[cval->val_type]);
3049 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3050 cval->min, cval->max, cval->dBmin, cval->dBmax);
3051}
3052
3053static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3054 struct snd_info_buffer *buffer)
3055{
3056 struct snd_usb_audio *chip = entry->private_data;
3057 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01003058 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003059 int unitid;
3060
3061 list_for_each_entry(mixer, &chip->mixer_list, list) {
3062 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003063 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02003064 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003065 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003066 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3067 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003068 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003069 snd_iprintf(buffer, " Unit: %i\n", list->id);
3070 if (list->kctl)
3071 snd_iprintf(buffer,
3072 " Control: name=\"%s\", index=%i\n",
3073 list->kctl->id.name,
3074 list->kctl->id.index);
3075 if (list->dump)
3076 list->dump(buffer, list);
3077 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003078 }
3079 }
3080}
3081
Daniel Macke213e9c2010-05-11 18:13:50 +02003082static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3083 int attribute, int value, int index)
3084{
Takashi Iwai3360b842014-11-18 11:47:04 +01003085 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02003086 __u8 unitid = (index >> 8) & 0xff;
3087 __u8 control = (value >> 8) & 0xff;
3088 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02003089 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02003090
3091 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003092 usb_audio_dbg(mixer->chip,
3093 "%s(): bogus channel number %d\n",
3094 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02003095 return;
3096 }
3097
Takashi Iwai8c558072018-05-03 12:33:32 +02003098 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02003099 count++;
3100
3101 if (count == 0)
3102 return;
3103
Takashi Iwai8c558072018-05-03 12:33:32 +02003104 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003105 struct usb_mixer_elem_info *info;
3106
3107 if (!list->kctl)
3108 continue;
3109
Takashi Iwai8c558072018-05-03 12:33:32 +02003110 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02003111 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02003112 continue;
3113
3114 switch (attribute) {
3115 case UAC2_CS_CUR:
3116 /* invalidate cache, so the value is read from the device */
3117 if (channel)
3118 info->cached &= ~(1 << channel);
3119 else /* master channel */
3120 info->cached = 0;
3121
3122 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003123 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02003124 break;
3125
3126 case UAC2_CS_RANGE:
3127 /* TODO */
3128 break;
3129
3130 case UAC2_CS_MEM:
3131 /* TODO */
3132 break;
3133
3134 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003135 usb_audio_dbg(mixer->chip,
3136 "unknown attribute %d in interrupt\n",
3137 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02003138 break;
3139 } /* switch */
3140 }
3141}
3142
3143static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003144{
3145 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02003146 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01003147 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003148
Oliver Neukumedf7de32011-03-11 13:19:43 +01003149 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02003150 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003151
Daniel Macke213e9c2010-05-11 18:13:50 +02003152 if (mixer->protocol == UAC_VERSION_1) {
3153 struct uac1_status_word *status;
3154
3155 for (status = urb->transfer_buffer;
3156 len >= sizeof(*status);
3157 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003158 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02003159 status->bStatusType,
3160 status->bOriginator);
3161
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003162 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02003163 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3164 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003165 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003166
3167 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3168 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003169 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003170 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3171 }
3172 } else { /* UAC_VERSION_2 */
3173 struct uac2_interrupt_data_msg *msg;
3174
3175 for (msg = urb->transfer_buffer;
3176 len >= sizeof(*msg);
3177 len -= sizeof(*msg), msg++) {
3178 /* drop vendor specific and endpoint requests */
3179 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3180 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3181 continue;
3182
3183 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3184 le16_to_cpu(msg->wValue),
3185 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003186 }
3187 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003188
3189requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003190 if (ustatus != -ENOENT &&
3191 ustatus != -ECONNRESET &&
3192 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003193 urb->dev = mixer->chip->dev;
3194 usb_submit_urb(urb, GFP_ATOMIC);
3195 }
3196}
3197
3198/* create the handler for the optional status interrupt endpoint */
3199static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3200{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003201 struct usb_endpoint_descriptor *ep;
3202 void *transfer_buffer;
3203 int buffer_length;
3204 unsigned int epnum;
3205
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003206 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003207 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003208 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003209 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003210 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003211 return 0;
3212
Julia Lawall42a6e662008-12-29 11:23:02 +01003213 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003214 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3215 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3216 if (!transfer_buffer)
3217 return -ENOMEM;
3218 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3219 if (!mixer->urb) {
3220 kfree(transfer_buffer);
3221 return -ENOMEM;
3222 }
3223 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3224 usb_rcvintpipe(mixer->chip->dev, epnum),
3225 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003226 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003227 usb_submit_urb(mixer->urb, GFP_KERNEL);
3228 return 0;
3229}
3230
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003231static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
3233{
3234 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3235
3236 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3237 return 0;
3238}
3239
3240static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3241 struct snd_ctl_elem_value *ucontrol)
3242{
3243 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3244 bool keep_iface = !!ucontrol->value.integer.value[0];
3245
3246 if (mixer->chip->keep_iface == keep_iface)
3247 return 0;
3248 mixer->chip->keep_iface = keep_iface;
3249 return 1;
3250}
3251
3252static const struct snd_kcontrol_new keep_iface_ctl = {
3253 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3254 .name = "Keep Interface",
3255 .info = snd_ctl_boolean_mono_info,
3256 .get = keep_iface_ctl_get,
3257 .put = keep_iface_ctl_put,
3258};
3259
3260static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3261{
3262 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3263
3264 /* need only one control per card */
3265 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3266 snd_ctl_free_one(kctl);
3267 return 0;
3268 }
3269
3270 return snd_ctl_add(mixer->chip->card, kctl);
3271}
3272
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003273int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3274 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003275{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003276 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003277 .dev_free = snd_usb_mixer_dev_free
3278 };
3279 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003280 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01003281 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003282
3283 strcpy(chip->card->mixername, "USB Mixer");
3284
Takashi Iwai561b2202005-09-09 14:22:34 +02003285 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003286 if (!mixer)
3287 return -ENOMEM;
3288 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003289 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003290 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3291 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003292 if (!mixer->id_elems) {
3293 kfree(mixer);
3294 return -ENOMEM;
3295 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003296
Daniel Mack1faa5d02011-08-04 15:56:28 +02003297 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3298 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003299 case UAC_VERSION_1:
3300 default:
3301 mixer->protocol = UAC_VERSION_1;
3302 break;
3303 case UAC_VERSION_2:
3304 mixer->protocol = UAC_VERSION_2;
3305 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003306 case UAC_VERSION_3:
3307 mixer->protocol = UAC_VERSION_3;
3308 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003309 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003310
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003311 if (mixer->protocol == UAC_VERSION_3 &&
3312 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
3313 if ((err = snd_usb_mixer_controls_badd(mixer, ctrlif)) < 0)
3314 goto _error;
3315 } else if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
3316 (err = snd_usb_mixer_status_create(mixer)) < 0) {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003317 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003318 }
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003319 err = create_keep_iface_ctl(mixer);
3320 if (err < 0)
3321 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003322
Daniel Mack7b1eda22010-03-11 21:13:22 +01003323 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003324
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003325 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003326 if (err < 0)
3327 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003328
3329 if (list_empty(&chip->mixer_list) &&
3330 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3331 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3332
Clemens Ladisch84957a82005-04-29 16:23:13 +02003333 list_add(&mixer->list, &chip->mixer_list);
3334 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003335
3336_error:
3337 snd_usb_mixer_free(mixer);
3338 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003339}
3340
Takashi Iwaia6cece92014-10-31 11:24:32 +01003341void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003342{
Takashi Iwai124751d2017-10-10 14:10:32 +02003343 if (mixer->disconnected)
3344 return;
3345 if (mixer->urb)
3346 usb_kill_urb(mixer->urb);
3347 if (mixer->rc_urb)
3348 usb_kill_urb(mixer->rc_urb);
3349 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003350}
Takashi Iwai400362f2014-01-20 16:51:16 +01003351
3352#ifdef CONFIG_PM
3353/* stop any bus activity of a mixer */
3354static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3355{
3356 usb_kill_urb(mixer->urb);
3357 usb_kill_urb(mixer->rc_urb);
3358}
3359
3360static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3361{
3362 int err;
3363
3364 if (mixer->urb) {
3365 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3366 if (err < 0)
3367 return err;
3368 }
3369
3370 return 0;
3371}
3372
3373int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3374{
3375 snd_usb_mixer_inactivate(mixer);
3376 return 0;
3377}
3378
Takashi Iwai3360b842014-11-18 11:47:04 +01003379static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003380{
Takashi Iwai8c558072018-05-03 12:33:32 +02003381 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003382 int c, err, idx;
3383
3384 if (cval->cmask) {
3385 idx = 0;
3386 for (c = 0; c < MAX_CHANNELS; c++) {
3387 if (!(cval->cmask & (1 << c)))
3388 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003389 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003390 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003391 cval->cache_val[idx]);
3392 if (err < 0)
3393 return err;
3394 }
3395 idx++;
3396 }
3397 } else {
3398 /* master */
3399 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003400 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003401 if (err < 0)
3402 return err;
3403 }
3404 }
3405
3406 return 0;
3407}
3408
3409int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3410{
Takashi Iwai3360b842014-11-18 11:47:04 +01003411 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003412 int id, err;
3413
Takashi Iwai400362f2014-01-20 16:51:16 +01003414 if (reset_resume) {
3415 /* restore cached mixer values */
3416 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003417 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003418 if (list->resume) {
3419 err = list->resume(list);
3420 if (err < 0)
3421 return err;
3422 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003423 }
3424 }
3425 }
3426
Takashi Iwai964af632018-04-27 14:23:37 +02003427 snd_usb_mixer_resume_quirk(mixer);
3428
Takashi Iwai400362f2014-01-20 16:51:16 +01003429 return snd_usb_mixer_activate(mixer);
3430}
3431#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003432
3433void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3434 struct usb_mixer_interface *mixer,
3435 int unitid)
3436{
3437 list->mixer = mixer;
3438 list->id = unitid;
3439 list->dump = snd_usb_mixer_dump_cval;
3440#ifdef CONFIG_PM
3441 list->resume = restore_mixer_value;
3442#endif
3443}