blob: 8c3568d8d03bab132e5399fccd7cba184c81ed2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Daniel Mack157a57b2010-06-16 17:57:30 +020029/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
Daniel Mackcddaafb2016-04-09 11:21:46 +020048#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010052#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010053#include <linux/usb/audio-v2.h>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020054#include <linux/usb/audio-v3.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <sound/core.h>
57#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020058#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020059#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020060#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010063#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010064#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010065#include "mixer_quirks.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010066#include "power.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020067
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010068#define MAX_ID_ELEMS 256
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
Takashi Iwai86e07d32005-11-17 15:08:02 +010080struct mixer_build {
81 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020082 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned char *buffer;
84 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010085 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010086 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020088 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050091/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080092enum {
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
99};
100enum {
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
111 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100112#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100114static const struct usbmix_name_map *
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300115find_map(const struct usbmix_name_map *p, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100117 if (!p)
118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300120 for (; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
123 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100125 return NULL;
126}
127
128/* get the mapped name if the unit matches */
129static int
130check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
131{
132 if (!p || !p->name)
133 return 0;
134
135 buflen--;
136 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Takashi Iwai5aeee342014-11-18 11:02:14 +0100139/* ignore the error value if ignore_ctl_error flag is set */
140#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100144static inline int
145check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100149 return 1;
150}
151
152/* dB mapping */
153static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
155{
156 if (p && p->dB) {
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200159 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200163/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100164static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200165 int index, char *buf, int buflen)
166{
167 const struct usbmix_selector_map *p;
168
Daniel Mack6bc170e2014-05-24 10:58:16 +0200169 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200170 return 0;
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
174 }
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * find an audio control unit with the given unit id
180 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200181static void *find_audio_control_unit(struct mixer_build *state,
182 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Daniel Mack67e1daa2010-05-31 13:35:43 +0200187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
Daniel Mack67e1daa2010-05-31 13:35:43 +0200192 hdr->bUnitID == unit)
193 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * copy a string with the given id
201 */
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300202static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200203 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
Jaejoong Kim251552a2017-12-04 15:31:48 +0900206
207 if (len < 0)
208 return 0;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 buf[len] = 0;
211 return len;
212}
213
214/*
215 * convert from the byte/word on usb descriptor to the zero-based integer
216 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
221 return !!val;
222 case USB_MIXER_INV_BOOLEAN:
223 return !val;
224 case USB_MIXER_U8:
225 val &= 0xff;
226 break;
227 case USB_MIXER_S8:
228 val &= 0xff;
229 if (val >= 0x80)
230 val -= 0x100;
231 break;
232 case USB_MIXER_U16:
233 val &= 0xffff;
234 break;
235 case USB_MIXER_S16:
236 val &= 0xffff;
237 if (val >= 0x8000)
238 val -= 0x10000;
239 break;
240 }
241 return val;
242}
243
244/*
245 * convert from the zero-based int to the byte/word for usb descriptor
246 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100247static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 switch (cval->val_type) {
250 case USB_MIXER_BOOLEAN:
251 return !!val;
252 case USB_MIXER_INV_BOOLEAN:
253 return !val;
254 case USB_MIXER_S8:
255 case USB_MIXER_U8:
256 return val & 0xff;
257 case USB_MIXER_S16:
258 case USB_MIXER_U16:
259 return val & 0xffff;
260 }
261 return 0; /* not reached */
262}
263
Takashi Iwai86e07d32005-11-17 15:08:02 +0100264static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200266 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cval->res = 1;
268 if (val < cval->min)
269 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 else
273 return (val - cval->min) / cval->res;
274}
275
Takashi Iwai86e07d32005-11-17 15:08:02 +0100276static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (val < 0)
279 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200280 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cval->res = 1;
282 val *= cval->res;
283 val += cval->min;
284 if (val > cval->max)
285 return cval->max;
286 return val;
287}
288
Julian Scheelbc18e312015-08-14 16:14:45 +0200289static int uac2_ctl_value_size(int val_type)
290{
291 switch (val_type) {
292 case USB_MIXER_S32:
293 case USB_MIXER_U32:
294 return 4;
295 case USB_MIXER_S16:
296 case USB_MIXER_U16:
297 return 2;
298 default:
299 return 1;
300 }
301 return 0; /* unreachable */
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/*
306 * retrieve a mixer value
307 */
308
Daniel Mack6bc170e2014-05-24 10:58:16 +0200309static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai3360b842014-11-18 11:47:04 +0100312 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
315 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200316 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Takashi Iwai47ab1542015-08-25 16:09:00 +0200318 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100319 if (err < 0)
320 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200329 err = 0;
330 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700331 } else if (err == -ETIMEDOUT) {
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100335 usb_audio_dbg(chip,
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200338 err = -EINVAL;
339
340 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200341 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Daniel Mack6bc170e2014-05-24 10:58:16 +0200345static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100347{
Takashi Iwai3360b842014-11-18 11:47:04 +0100348 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100351 unsigned char *val;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100352 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100353 __u8 bRequest;
354
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100355 val_size = uac2_ctl_value_size(cval->val_type);
356
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100359 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200360 } else {
361 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100362 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200363 }
364
365 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100366
Takashi Iwai47ab1542015-08-25 16:09:00 +0200367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100368 if (ret)
369 goto error;
370
Takashi Iwai47ab1542015-08-25 16:09:00 +0200371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200374 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200375 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100376
377 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100378error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100379 usb_audio_err(chip,
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100382 return ret;
383 }
384
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200385 /* FIXME: how should we handle multiple triplets here? */
386
Daniel Mack23caaf12010-03-11 21:13:25 +0100387 switch (request) {
388 case UAC_GET_CUR:
389 val = buf;
390 break;
391 case UAC_GET_MIN:
392 val = buf + sizeof(__u16);
393 break;
394 case UAC_GET_MAX:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100395 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100396 break;
397 case UAC_GET_RES:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100398 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100399 break;
400 default:
401 return -EINVAL;
402 }
403
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100406
407 return 0;
408}
409
Daniel Mack6bc170e2014-05-24 10:58:16 +0200410static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100412{
Eldad Zack9f814102012-11-28 23:55:35 +0100413 validx += cval->idx_off;
414
Takashi Iwai3360b842014-11-18 11:47:04 +0100415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
418}
419
Daniel Mack6bc170e2014-05-24 10:58:16 +0200420static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100427static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
432 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Chris J Argeseef90452014-11-12 12:07:01 -0600435int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100436 int channel, int index, int *value)
437{
438 int err;
439
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
442 return 0;
443 }
444 err = get_cur_mix_raw(cval, channel, value);
445 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100448 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200449 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100450 return err;
451 }
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * set a mixer value
459 */
460
Daniel Mack7b1eda22010-03-11 21:13:22 +0100461int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai3360b842014-11-18 11:47:04 +0100464 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200465 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200466 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100467
Eldad Zack9f814102012-11-28 23:55:35 +0100468 validx += cval->idx_off;
469
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200470
Takashi Iwai3360b842014-11-18 11:47:04 +0100471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200473 } else { /* UAC_VERSION_2/3 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200490
491 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 if (err < 0)
493 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200494
Takashi Iwai978520b2012-10-12 15:12:55 +0200495 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
501 if (err >= 0) {
Takashi Iwai978520b2012-10-12 15:12:55 +0200502 err = 0;
503 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700504 } else if (err == -ETIMEDOUT) {
505 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100506 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200507 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100508 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200509 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 err = -EINVAL;
511
512 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200513 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Daniel Mack6bc170e2014-05-24 10:58:16 +0200517static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Chris J Argeseef90452014-11-12 12:07:01 -0600523int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100524 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai641b4872009-01-15 17:05:24 +0100526 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
530
531 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100532 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200534 __func__, channel, cval->control);
535 return 0;
536 }
537
Daniel Mack6bc170e2014-05-24 10:58:16 +0200538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
540 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100541 if (err < 0)
542 return err;
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200548/*
549 * TLV callback for mixer volume controls
550 */
Felix Homann285de9c2012-04-23 20:24:24 +0200551int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200552 unsigned int size, unsigned int __user *_tlv)
553{
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200556
557 if (size < sizeof(scale))
558 return -ENOMEM;
Takashi Iwai0f174b32017-08-16 14:18:37 +0200559 if (cval->min_mute)
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200563 if (copy_to_user(_tlv, scale, sizeof(scale)))
564 return -EFAULT;
565 return 0;
566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * parser routines begin here...
570 */
571
Takashi Iwai86e07d32005-11-17 15:08:02 +0100572static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574
575/*
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
578 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200579static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
589 *
590 * if failed, give up and free the control instance.
591 */
592
Takashi Iwai3360b842014-11-18 11:47:04 +0100593int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200594 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Takashi Iwai3360b842014-11-18 11:47:04 +0100596 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200598
Daniel Mackef9d5972011-05-25 09:09:00 +0200599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kctl->id.index++;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200601 err = snd_ctl_add(mixer->chip->card, kctl);
602 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200603 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
604 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100607 list->kctl = kctl;
608 list->next_id_elem = mixer->id_elems[list->id];
609 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
614 * get a terminal name string
615 */
616
617static struct iterm_name_combo {
618 int type;
619 char *name;
620} iterm_names[] = {
621 { 0x0300, "Output" },
622 { 0x0301, "Speaker" },
623 { 0x0302, "Headphone" },
624 { 0x0303, "HMD Audio" },
625 { 0x0304, "Desktop Speaker" },
626 { 0x0305, "Room Speaker" },
627 { 0x0306, "Com Speaker" },
628 { 0x0307, "LFE" },
629 { 0x0600, "External In" },
630 { 0x0601, "Analog In" },
631 { 0x0602, "Digital In" },
632 { 0x0603, "Line" },
633 { 0x0604, "Legacy In" },
634 { 0x0605, "IEC958 In" },
635 { 0x0606, "1394 DA Stream" },
636 { 0x0607, "1394 DV Stream" },
637 { 0x0700, "Embedded" },
638 { 0x0701, "Noise Source" },
639 { 0x0702, "Equalization Noise" },
640 { 0x0703, "CD" },
641 { 0x0704, "DAT" },
642 { 0x0705, "DCC" },
643 { 0x0706, "MiniDisk" },
644 { 0x0707, "Analog Tape" },
645 { 0x0708, "Phonograph" },
646 { 0x0709, "VCR Audio" },
647 { 0x070a, "Video Disk Audio" },
648 { 0x070b, "DVD Audio" },
649 { 0x070c, "TV Tuner Audio" },
650 { 0x070d, "Satellite Rec Audio" },
651 { 0x070e, "Cable Tuner Audio" },
652 { 0x070f, "DSS Audio" },
653 { 0x0710, "Radio Receiver" },
654 { 0x0711, "Radio Transmitter" },
655 { 0x0712, "Multi-Track Recorder" },
656 { 0x0713, "Synthesizer" },
657 { 0 },
658};
659
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300660static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 unsigned char *name, int maxlen, int term_only)
662{
663 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100664 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100666 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300667 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200668 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100669 if (len)
670 return len;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* virtual type - not a real terminal */
674 if (iterm->type >> 16) {
675 if (term_only)
676 return 0;
677 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100678 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200679 strcpy(name, "Selector");
680 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200681 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200682 strcpy(name, "Process Unit");
683 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200684 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200685 strcpy(name, "Ext Unit");
686 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100687 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200688 strcpy(name, "Mixer");
689 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 default:
691 return sprintf(name, "Unit %d", iterm->id);
692 }
693 }
694
695 switch (iterm->type & 0xff00) {
696 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200697 strcpy(name, "PCM");
698 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200700 strcpy(name, "Mic");
701 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200703 strcpy(name, "Headset");
704 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200706 strcpy(name, "Phone");
707 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Daniel Mack6bc170e2014-05-24 10:58:16 +0200710 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (names->type == iterm->type) {
712 strcpy(name, names->name);
713 return strlen(names->name);
714 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100721 * Get logical cluster information for UAC3 devices.
722 */
723static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
724{
725 struct uac3_cluster_header_descriptor c_header;
726 int err;
727
728 err = snd_usb_ctl_msg(state->chip->dev,
729 usb_rcvctrlpipe(state->chip->dev, 0),
730 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
731 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
732 cluster_id,
733 snd_usb_ctrl_intf(state->chip),
734 &c_header, sizeof(c_header));
735 if (err < 0)
736 goto error;
737 if (err != sizeof(c_header)) {
738 err = -EIO;
739 goto error;
740 }
741
742 return c_header.bNrChannels;
743
744error:
745 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
746 return err;
747}
748
749/*
750 * Get number of channels for a Mixer Unit.
751 */
752static int uac_mixer_unit_get_channels(struct mixer_build *state,
753 struct uac_mixer_unit_descriptor *desc)
754{
755 int mu_channels;
756
757 if (desc->bLength < 11)
758 return -EINVAL;
759 if (!desc->bNrInPins)
760 return -EINVAL;
761
762 switch (state->mixer->protocol) {
763 case UAC_VERSION_1:
764 case UAC_VERSION_2:
765 default:
766 mu_channels = uac_mixer_unit_bNrChannels(desc);
767 break;
768 case UAC_VERSION_3:
769 mu_channels = get_cluster_channels_v3(state,
770 uac3_mixer_unit_wClusterDescrID(desc));
771 break;
772 }
773
774 if (!mu_channels)
775 return -EINVAL;
776
777 return mu_channels;
778}
779
780/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * parse the source unit recursively until it reaches to a terminal
782 * or a branched unit.
783 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200784static int check_input_term(struct mixer_build *state, int id,
785 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200787 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200788 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100789 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 memset(term, 0, sizeof(*term));
792 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100793 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200795
796 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
797 switch (hdr[2]) {
798 case UAC_INPUT_TERMINAL:
799 if (protocol == UAC_VERSION_1) {
800 struct uac_input_terminal_descriptor *d = p1;
801
802 term->type = le16_to_cpu(d->wTerminalType);
803 term->channels = d->bNrChannels;
804 term->chconfig = le16_to_cpu(d->wChannelConfig);
805 term->name = d->iTerminal;
806 } else { /* UAC_VERSION_2 */
807 struct uac2_input_terminal_descriptor *d = p1;
808
809 /* call recursively to verify that the
810 * referenced clock entity is valid */
811 err = check_input_term(state, d->bCSourceID, term);
812 if (err < 0)
813 return err;
814
815 /* save input term properties after recursion,
816 * to ensure they are not overriden by the
817 * recursion calls */
818 term->id = id;
819 term->type = le16_to_cpu(d->wTerminalType);
820 term->channels = d->bNrChannels;
821 term->chconfig = le32_to_cpu(d->bmChannelConfig);
822 term->name = d->iTerminal;
823 }
824 return 0;
825 case UAC_FEATURE_UNIT: {
826 /* the header is the same for v1 and v2 */
827 struct uac_feature_unit_descriptor *d = p1;
828
829 id = d->bSourceID;
830 break; /* continue to parse */
831 }
832 case UAC_MIXER_UNIT: {
833 struct uac_mixer_unit_descriptor *d = p1;
834
835 term->type = d->bDescriptorSubtype << 16; /* virtual type */
836 term->channels = uac_mixer_unit_bNrChannels(d);
837 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
838 term->name = uac_mixer_unit_iMixer(d);
839 return 0;
840 }
841 case UAC_SELECTOR_UNIT:
842 case UAC2_CLOCK_SELECTOR: {
843 struct uac_selector_unit_descriptor *d = p1;
844 /* call recursively to retrieve the channel info */
845 err = check_input_term(state, d->baSourceID[0], term);
846 if (err < 0)
847 return err;
848 term->type = d->bDescriptorSubtype << 16; /* virtual type */
849 term->id = id;
850 term->name = uac_selector_unit_iSelector(d);
851 return 0;
852 }
853 case UAC1_PROCESSING_UNIT:
854 case UAC1_EXTENSION_UNIT:
855 /* UAC2_PROCESSING_UNIT_V2 */
856 /* UAC2_EFFECT_UNIT */
857 case UAC2_EXTENSION_UNIT_V2: {
858 struct uac_processing_unit_descriptor *d = p1;
859
860 if (protocol == UAC_VERSION_2 &&
861 hdr[2] == UAC2_EFFECT_UNIT) {
862 /* UAC2/UAC1 unit IDs overlap here in an
863 * uncompatible way. Ignore this unit for now.
864 */
865 return 0;
866 }
867
868 if (d->bNrInPins) {
869 id = d->baSourceID[0];
870 break; /* continue to parse */
871 }
872 term->type = d->bDescriptorSubtype << 16; /* virtual type */
873 term->channels = uac_processing_unit_bNrChannels(d);
874 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
875 term->name = uac_processing_unit_iProcessing(d, protocol);
876 return 0;
877 }
878 case UAC2_CLOCK_SOURCE: {
879 struct uac_clock_source_descriptor *d = p1;
880
881 term->type = d->bDescriptorSubtype << 16; /* virtual type */
882 term->id = id;
883 term->name = d->iClockSource;
884 return 0;
885 }
886 default:
887 return -ENODEV;
888 }
889 } else { /* UAC_VERSION_3 */
890 switch (hdr[2]) {
891 case UAC_INPUT_TERMINAL: {
892 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200893
894 /* call recursively to verify that the
895 * referenced clock entity is valid */
896 err = check_input_term(state, d->bCSourceID, term);
897 if (err < 0)
898 return err;
899
900 /* save input term properties after recursion,
901 * to ensure they are not overriden by the
902 * recursion calls */
903 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100904 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100905
Jorge Sanjuan71066942018-05-14 12:03:42 +0100906 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
907 if (err < 0)
908 return err;
909 term->channels = err;
910
911 /* REVISIT: UAC3 IT doesn't have channels cfg */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200912 term->chconfig = 0;
913
914 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100915 return 0;
916 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200917 case UAC3_FEATURE_UNIT: {
918 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100919
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200920 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break; /* continue to parse */
922 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200923 case UAC3_CLOCK_SOURCE: {
924 struct uac3_clock_source_descriptor *d = p1;
925
926 term->type = d->bDescriptorSubtype << 16; /* virtual type */
927 term->id = id;
928 term->name = le16_to_cpu(d->wClockSourceStr);
929 return 0;
930 }
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100931 case UAC3_MIXER_UNIT: {
932 struct uac_mixer_unit_descriptor *d = p1;
933
934 err = uac_mixer_unit_get_channels(state, d);
935 if (err < 0)
936 return err;
937
938 term->channels = err;
939 term->type = d->bDescriptorSubtype << 16; /* virtual type */
940
941 return 0;
942 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200943 default:
944 return -ENODEV;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 }
948 return -ENODEV;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * Feature Unit
953 */
954
955/* feature unit control information */
956struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700957 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200959 int type; /* data type for uac1 */
960 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961};
962
963static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700964 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
965 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
966 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
967 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
968 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
969 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
970 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
971 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
972 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
973 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200974 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700975 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
976 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
977 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978};
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600981void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jesper Juhl4d572772005-05-30 17:30:32 +0200983 kfree(kctl->private_data);
984 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/*
988 * interface to ALSA control for feature/mixer units
989 */
990
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100991/* volume control quirks */
992static void volume_control_quirks(struct usb_mixer_elem_info *cval,
993 struct snd_kcontrol *kctl)
994{
Takashi Iwai3360b842014-11-18 11:47:04 +0100995 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100996 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +0100997 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500998 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +0100999 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1000 cval->min = 0x0000;
1001 cval->max = 0xffff;
1002 cval->res = 0x00e6;
1003 break;
1004 }
1005 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1006 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1007 cval->min = 0x00;
1008 cval->max = 0xff;
1009 break;
1010 }
1011 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1012 cval->min = 0xb706;
1013 cval->max = 0xff7b;
1014 cval->res = 0x0073;
1015 break;
1016 }
1017 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1018 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1019 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1020 cval->max = 0xfcfe;
1021 cval->res = 0x0073;
1022 }
1023 break;
1024
Felix Homannd34bf142012-04-23 20:24:27 +02001025 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1026 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1027 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001028 usb_audio_info(chip,
1029 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001030 cval->min = 0x0000;
1031 cval->max = 0x7f00;
1032 cval->res = 0x0100;
1033 break;
1034 }
1035 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1036 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001037 usb_audio_info(chip,
1038 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001039 cval->min = 0x00;
1040 cval->max = 0x7f;
1041 break;
1042 }
1043 break;
1044
Federico Cuello21493312018-05-09 00:13:38 +02001045 case USB_ID(0x0d8c, 0x0103):
1046 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1047 usb_audio_info(chip,
1048 "set volume quirk for CM102-A+/102S+\n");
1049 cval->min = -256;
1050 }
1051 break;
1052
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001053 case USB_ID(0x0471, 0x0101):
1054 case USB_ID(0x0471, 0x0104):
1055 case USB_ID(0x0471, 0x0105):
1056 case USB_ID(0x0672, 0x1041):
1057 /* quirk for UDA1321/N101.
1058 * note that detection between firmware 2.1.1.7 (N101)
1059 * and later 2.1.1.21 is not very clear from datasheets.
1060 * I hope that the min value is -15360 for newer firmware --jk
1061 */
1062 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1063 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001064 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001065 "set volume quirk for UDA1321/N101 chip\n");
1066 cval->max = -256;
1067 }
1068 break;
1069
1070 case USB_ID(0x046d, 0x09a4):
1071 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001072 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001073 "set volume quirk for QuickCam E3500\n");
1074 cval->min = 6080;
1075 cval->max = 8768;
1076 cval->res = 192;
1077 }
1078 break;
1079
Takashi Iwaie805ca82014-03-05 12:34:39 +01001080 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001081 case USB_ID(0x046d, 0x0808):
1082 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001083 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001084 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001085 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001086 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001087 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001088 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001089 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001090 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001091 /* Most audio usb devices lie about volume resolution.
1092 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001093 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001094 */
1095 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001096 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001097 "set resolution quirk: cval->res = 384\n");
1098 cval->res = 384;
1099 }
1100 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001101 }
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
1105 * retrieve the minimum and maximum values for the specified control
1106 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001107static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1108 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 /* for failsafe */
1111 cval->min = default_min;
1112 cval->max = cval->min + 1;
1113 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001114 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (cval->val_type == USB_MIXER_BOOLEAN ||
1117 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1118 cval->initialized = 1;
1119 } else {
1120 int minchn = 0;
1121 if (cval->cmask) {
1122 int i;
1123 for (i = 0; i < MAX_CHANNELS; i++)
1124 if (cval->cmask & (1 << i)) {
1125 minchn = i + 1;
1126 break;
1127 }
1128 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001129 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1130 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001131 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001132 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001133 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1134 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -EINVAL;
1136 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001137 if (get_ctl_value(cval, UAC_GET_RES,
1138 (cval->control << 8) | minchn,
1139 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 cval->res = 1;
1141 } else {
1142 int last_valid_res = cval->res;
1143
1144 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001145 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001146 (cval->control << 8) | minchn,
1147 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 cval->res /= 2;
1150 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001151 if (get_ctl_value(cval, UAC_GET_RES,
1152 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 cval->res = last_valid_res;
1154 }
1155 if (cval->res == 0)
1156 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001157
1158 /* Additional checks for the proper resolution
1159 *
1160 * Some devices report smaller resolutions than actually
1161 * reacting. They don't return errors but simply clip
1162 * to the lower aligned value.
1163 */
1164 if (cval->min + cval->res < cval->max) {
1165 int last_valid_res = cval->res;
1166 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001167 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001168 for (;;) {
1169 test = saved;
1170 if (test < cval->max)
1171 test += cval->res;
1172 else
1173 test -= cval->res;
1174 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001175 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001176 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001177 cval->res = last_valid_res;
1178 break;
1179 }
1180 if (test == check)
1181 break;
1182 cval->res *= 2;
1183 }
Chris J Argeseef90452014-11-12 12:07:01 -06001184 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001185 }
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 cval->initialized = 1;
1188 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001189
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001190 if (kctl)
1191 volume_control_quirks(cval, kctl);
1192
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001193 /* USB descriptions contain the dB scale in 1/256 dB unit
1194 * while ALSA TLV contains in 1/100 dB unit
1195 */
1196 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1197 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1198 if (cval->dBmin > cval->dBmax) {
1199 /* something is wrong; assume it's either from/to 0dB */
1200 if (cval->dBmin < 0)
1201 cval->dBmax = 0;
1202 else if (cval->dBmin > 0)
1203 cval->dBmin = 0;
1204 if (cval->dBmin > cval->dBmax) {
1205 /* totally crap, return an error */
1206 return -EINVAL;
1207 }
1208 }
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return 0;
1211}
1212
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001213#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001216static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1217 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001219 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 if (cval->val_type == USB_MIXER_BOOLEAN ||
1222 cval->val_type == USB_MIXER_INV_BOOLEAN)
1223 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1224 else
1225 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1226 uinfo->count = cval->channels;
1227 if (cval->val_type == USB_MIXER_BOOLEAN ||
1228 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1229 uinfo->value.integer.min = 0;
1230 uinfo->value.integer.max = 1;
1231 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001232 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001233 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001234 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1235 kcontrol->vd[0].access &=
1236 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1237 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001238 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001239 SNDRV_CTL_EVENT_MASK_INFO,
1240 &kcontrol->id);
1241 }
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001244 uinfo->value.integer.max =
1245 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 return 0;
1248}
1249
1250/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001251static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1252 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001254 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 int c, cnt, val, err;
1256
Takashi Iwai641b4872009-01-15 17:05:24 +01001257 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (cval->cmask) {
1259 cnt = 0;
1260 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001261 if (!(cval->cmask & (1 << c)))
1262 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001263 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001264 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001265 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001266 val = get_relative_value(cval, val);
1267 ucontrol->value.integer.value[cnt] = val;
1268 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else {
1272 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001273 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001274 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001275 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 val = get_relative_value(cval, val);
1277 ucontrol->value.integer.value[0] = val;
1278 }
1279 return 0;
1280}
1281
1282/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001283static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1284 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001286 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int c, cnt, val, oval, err;
1288 int changed = 0;
1289
1290 if (cval->cmask) {
1291 cnt = 0;
1292 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001293 if (!(cval->cmask & (1 << c)))
1294 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001295 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001296 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001297 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001298 val = ucontrol->value.integer.value[cnt];
1299 val = get_abs_value(cval, val);
1300 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001301 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001302 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001304 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306 } else {
1307 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001308 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001310 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 val = ucontrol->value.integer.value[0];
1312 val = get_abs_value(cval, val);
1313 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001314 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 changed = 1;
1316 }
1317 }
1318 return changed;
1319}
1320
Andrew Chant568fa7e2018-03-23 19:25:24 -07001321/* get the boolean value from the master channel of a UAC control */
1322static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1323 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001324{
1325 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1326 int val, err;
1327
1328 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1329 if (err < 0)
1330 return filter_error(cval, err);
1331 val = (val != 0);
1332 ucontrol->value.integer.value[0] = val;
1333 return 0;
1334}
1335
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001336/* get the connectors status and report it as boolean type */
1337static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1338 struct snd_ctl_elem_value *ucontrol)
1339{
1340 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1341 struct snd_usb_audio *chip = cval->head.mixer->chip;
1342 int idx = 0, validx, ret, val;
1343
1344 validx = cval->control << 8 | 0;
1345
1346 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1347 if (ret)
1348 goto error;
1349
1350 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1351 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1352 struct uac2_connectors_ctl_blk uac2_conn;
1353
1354 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1355 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1356 validx, idx, &uac2_conn, sizeof(uac2_conn));
1357 val = !!uac2_conn.bNrChannels;
1358 } else { /* UAC_VERSION_3 */
1359 struct uac3_insertion_ctl_blk uac3_conn;
1360
1361 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1362 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1363 validx, idx, &uac3_conn, sizeof(uac3_conn));
1364 val = !!uac3_conn.bmConInserted;
1365 }
1366
1367 snd_usb_unlock_shutdown(chip);
1368
1369 if (ret < 0) {
1370error:
1371 usb_audio_err(chip,
1372 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1373 UAC_GET_CUR, validx, idx, cval->val_type);
1374 return ret;
1375 }
1376
1377 ucontrol->value.integer.value[0] = val;
1378 return 0;
1379}
1380
Takashi Iwai86e07d32005-11-17 15:08:02 +01001381static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1383 .name = "", /* will be filled later manually */
1384 .info = mixer_ctl_feature_info,
1385 .get = mixer_ctl_feature_get,
1386 .put = mixer_ctl_feature_put,
1387};
1388
Daniel Mack23caaf12010-03-11 21:13:25 +01001389/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301390static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001391 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1392 .name = "", /* will be filled later manually */
1393 .info = mixer_ctl_feature_info,
1394 .get = mixer_ctl_feature_get,
1395 .put = NULL,
1396};
1397
Andrew Chant568fa7e2018-03-23 19:25:24 -07001398/*
1399 * A control which shows the boolean value from reading a UAC control on
1400 * the master channel.
1401 */
1402static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001403 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1404 .name = "", /* will be filled later manually */
1405 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1406 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001407 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001408 .put = NULL,
1409};
1410
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001411static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1412 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1413 .name = "", /* will be filled later manually */
1414 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1415 .info = snd_ctl_boolean_mono_info,
1416 .get = mixer_ctl_connector_get,
1417 .put = NULL,
1418};
1419
Daniel Mack6bc170e2014-05-24 10:58:16 +02001420/*
1421 * This symbol is exported in order to allow the mixer quirks to
1422 * hook up to the standard feature unit control mechanism
1423 */
Daniel Mack9e386582011-05-25 09:09:01 +02001424struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426/*
1427 * build a feature control
1428 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001429static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1430{
1431 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1432}
1433
Daniel Mack6bc170e2014-05-24 10:58:16 +02001434/*
1435 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1436 * rename it to "Headphone". We determine if something is a headphone
1437 * similar to how udev determines form factor.
1438 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001439static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1440 struct snd_card *card)
1441{
1442 const char *names_to_check[] = {
1443 "Headset", "headset", "Headphone", "headphone", NULL};
1444 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001445 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001446
1447 if (strcmp("Speaker", kctl->id.name))
1448 return;
1449
1450 for (s = names_to_check; *s; s++)
1451 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001452 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001453 break;
1454 }
1455
1456 if (!found)
1457 return;
1458
1459 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1460}
1461
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001462static struct usb_feature_control_info *get_feature_control_info(int control)
1463{
1464 int i;
1465
1466 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1467 if (audio_feature_info[i].control == control)
1468 return &audio_feature_info[i];
1469 }
1470 return NULL;
1471}
1472
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001473static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1474 const struct usbmix_name_map *imap,
1475 unsigned int ctl_mask, int control,
1476 struct usb_audio_term *iterm,
1477 struct usb_audio_term *oterm,
1478 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Julian Scheelbc18e312015-08-14 16:14:45 +02001480 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 unsigned int len = 0;
1482 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001483 struct snd_kcontrol *kctl;
1484 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001485 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001486 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Daniel Mack65f25da2010-05-31 13:35:41 +02001488 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /* FIXME: not supported yet */
1490 return;
1491 }
1492
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001493 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001494 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return;
1496
Takashi Iwai561b2202005-09-09 14:22:34 +02001497 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001498 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001500 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 cval->control = control;
1502 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001503
1504 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001505 if (!ctl_info) {
1506 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001507 return;
Colin Ian King62376022018-03-27 15:30:01 +01001508 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001509 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001510 cval->val_type = ctl_info->type;
1511 else /* UAC_VERSION_2 */
1512 cval->val_type = ctl_info->type_uac2 >= 0 ?
1513 ctl_info->type_uac2 : ctl_info->type;
1514
Daniel Macka6a33252010-05-31 13:35:37 +02001515 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001517 cval->master_readonly = readonly_mask;
1518 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 int i, c = 0;
1520 for (i = 0; i < 16; i++)
1521 if (ctl_mask & (1 << i))
1522 c++;
1523 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001524 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
Daniel Mack6bc170e2014-05-24 10:58:16 +02001527 /*
1528 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001529 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001530 * issue write commands to read-only channels.
1531 */
Daniel Macka6a33252010-05-31 13:35:37 +02001532 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001533 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1534 else
1535 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1536
Daniel Mack6bc170e2014-05-24 10:58:16 +02001537 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001538 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 kfree(cval);
1540 return;
1541 }
Chris J Argeseef90452014-11-12 12:07:01 -06001542 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001544 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001546 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001547 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001548 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001551 case UAC_FU_MUTE:
1552 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001553 /*
1554 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 * - if a name id is given in descriptor, use it.
1556 * - if the connected input can be determined, then use the name
1557 * of terminal type.
1558 * - if the connected output can be determined, use it.
1559 * - otherwise, anonymous name.
1560 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001561 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001562 if (iterm)
1563 len = get_term_name(mixer->chip, iterm,
1564 kctl->id.name,
1565 sizeof(kctl->id.name), 1);
1566 if (!len && oterm)
1567 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001568 kctl->id.name,
1569 sizeof(kctl->id.name), 1);
1570 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001571 snprintf(kctl->id.name, sizeof(kctl->id.name),
1572 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001574
1575 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001576 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001577
Daniel Mack6bc170e2014-05-24 10:58:16 +02001578 /*
1579 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * if the connected output is USB stream, then it's likely a
1581 * capture stream. otherwise it should be playback (hopefully :)
1582 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001583 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1584 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001585 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001586 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001587 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001589 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001590 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001593 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1595 sizeof(kctl->id.name));
1596 break;
1597 }
1598
Takashi Iwaie1825342012-05-14 17:11:06 +02001599 /* get min/max values */
1600 get_min_max_with_quirks(cval, 0, kctl);
1601
1602 if (control == UAC_FU_VOLUME) {
1603 check_mapped_dB(map, cval);
1604 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1605 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1606 kctl->vd[0].access |=
1607 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1608 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1609 }
1610 }
1611
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001612 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001613
Alexey Fisher80aceff2011-03-10 14:53:38 +01001614 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001615 /*
1616 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001617 * to be sure. 384 is a resolution magic number found on Logitech
1618 * devices. It will definitively catch all buggy Logitech devices.
1619 */
1620 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001621 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001622 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001623 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001624 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001625 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001626 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001627 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001628 }
1629
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001630 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001631 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001632 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001633 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001636static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1637 unsigned int ctl_mask, int control,
1638 struct usb_audio_term *iterm, int unitid,
1639 int readonly_mask)
1640{
1641 struct uac_feature_unit_descriptor *desc = raw_desc;
1642 int nameid = uac_feature_unit_iFeature(desc);
1643
1644 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1645 iterm, &state->oterm, unitid, nameid, readonly_mask);
1646}
1647
1648static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1649 unsigned int ctl_mask, int control, int unitid,
1650 const struct usbmix_name_map *badd_map)
1651{
1652 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1653 NULL, NULL, unitid, 0, 0);
1654}
1655
Andrew Chant5a222e82018-03-23 19:25:23 -07001656static void get_connector_control_name(struct mixer_build *state,
1657 struct usb_audio_term *term,
1658 bool is_input, char *name, int name_size)
1659{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001660 int name_len = get_term_name(state->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001661
1662 if (name_len == 0)
1663 strlcpy(name, "Unknown", name_size);
1664
1665 /*
1666 * sound/core/ctljack.c has a convention of naming jack controls
1667 * by ending in " Jack". Make it slightly more useful by
1668 * indicating Input or Output after the terminal name.
1669 */
1670 if (is_input)
1671 strlcat(name, " - Input Jack", name_size);
1672 else
1673 strlcat(name, " - Output Jack", name_size);
1674}
1675
1676/* Build a mixer control for a UAC connector control (jack-detect) */
1677static void build_connector_control(struct mixer_build *state,
1678 struct usb_audio_term *term, bool is_input)
1679{
1680 struct snd_kcontrol *kctl;
1681 struct usb_mixer_elem_info *cval;
1682
1683 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1684 if (!cval)
1685 return;
1686 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001687 /*
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001688 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1689 * number of channels connected.
1690 *
1691 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1692 * following byte(s) specifies which connectors are inserted.
1693 *
1694 * This boolean ctl will simply report if any channels are connected
1695 * or not.
Andrew Chant568fa7e2018-03-23 19:25:24 -07001696 */
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001697 if (state->mixer->protocol == UAC_VERSION_2)
1698 cval->control = UAC2_TE_CONNECTOR;
1699 else /* UAC_VERSION_3 */
1700 cval->control = UAC3_TE_INSERTION;
1701
Andrew Chant5a222e82018-03-23 19:25:23 -07001702 cval->val_type = USB_MIXER_BOOLEAN;
1703 cval->channels = 1; /* report true if any channel is connected */
1704 cval->min = 0;
1705 cval->max = 1;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001706 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001707 if (!kctl) {
1708 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1709 kfree(cval);
1710 return;
1711 }
1712 get_connector_control_name(state, term, is_input, kctl->id.name,
1713 sizeof(kctl->id.name));
1714 kctl->private_free = snd_usb_mixer_elem_free;
1715 snd_usb_mixer_add_control(&cval->head, kctl);
1716}
1717
Daniel Mackcddaafb2016-04-09 11:21:46 +02001718static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1719 void *_ftr)
1720{
1721 struct uac_clock_source_descriptor *hdr = _ftr;
1722 struct usb_mixer_elem_info *cval;
1723 struct snd_kcontrol *kctl;
1724 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1725 int ret;
1726
1727 if (state->mixer->protocol != UAC_VERSION_2)
1728 return -EINVAL;
1729
1730 if (hdr->bLength != sizeof(*hdr)) {
1731 usb_audio_dbg(state->chip,
1732 "Bogus clock source descriptor length of %d, ignoring.\n",
1733 hdr->bLength);
1734 return 0;
1735 }
1736
1737 /*
1738 * The only property of this unit we are interested in is the
1739 * clock source validity. If that isn't readable, just bail out.
1740 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001741 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001742 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001743 return 0;
1744
1745 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1746 if (!cval)
1747 return -ENOMEM;
1748
1749 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1750
1751 cval->min = 0;
1752 cval->max = 1;
1753 cval->channels = 1;
1754 cval->val_type = USB_MIXER_BOOLEAN;
1755 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1756
Andrew Chant568fa7e2018-03-23 19:25:24 -07001757 cval->master_readonly = 1;
1758 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1759 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001760
1761 if (!kctl) {
1762 kfree(cval);
1763 return -ENOMEM;
1764 }
1765
1766 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001767 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001768 name, sizeof(name));
1769 if (ret > 0)
1770 snprintf(kctl->id.name, sizeof(kctl->id.name),
1771 "%s Validity", name);
1772 else
1773 snprintf(kctl->id.name, sizeof(kctl->id.name),
1774 "Clock Source %d Validity", hdr->bClockID);
1775
1776 return snd_usb_mixer_add_control(&cval->head, kctl);
1777}
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779/*
1780 * parse a feature unit
1781 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001782 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001784static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1785 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001788 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 unsigned int master_bits, first_ch_bits;
1790 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001791 struct uac_feature_unit_descriptor *hdr = _ftr;
1792 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Daniel Mack23caaf12010-03-11 21:13:25 +01001794 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001795 if (hdr->bLength < 7) {
1796 usb_audio_err(state->chip,
1797 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1798 unitid);
1799 return -EINVAL;
1800 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001801 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001802 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001803 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001804 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001805 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001806 return -EINVAL;
1807 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001808 channels = (hdr->bLength - 7) / csize - 1;
1809 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001810 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001811 usb_audio_err(state->chip,
1812 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1813 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001814 return -EINVAL;
1815 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001816 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001817 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001818 if (hdr->bLength < 6) {
1819 usb_audio_err(state->chip,
1820 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1821 unitid);
1822 return -EINVAL;
1823 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001824 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001825 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001826 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001827 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001828 usb_audio_err(state->chip,
1829 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1830 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001831 return -EINVAL;
1832 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001833 } else { /* UAC_VERSION_3 */
1834 struct uac3_feature_unit_descriptor *ftr = _ftr;
1835
1836 if (hdr->bLength < 7) {
1837 usb_audio_err(state->chip,
1838 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1839 unitid);
1840 return -EINVAL;
1841 }
1842 csize = 4;
1843 channels = (ftr->bLength - 7) / 4 - 1;
1844 bmaControls = ftr->bmaControls;
1845 if (hdr->bLength < 7 + csize) {
1846 usb_audio_err(state->chip,
1847 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1848 unitid);
1849 return -EINVAL;
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
1853 /* parse the source unit */
Takashi Iwaif25ecf82018-05-27 15:18:22 +02001854 err = parse_audio_unit(state, hdr->bSourceID);
1855 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return err;
1857
1858 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001859 err = check_input_term(state, hdr->bSourceID, &iterm);
1860 if (err < 0)
1861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Daniel Mack23caaf12010-03-11 21:13:25 +01001863 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001864 /* master configuration quirks */
1865 switch (state->chip->usb_id) {
1866 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001867 usb_audio_info(state->chip,
1868 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001869 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001870 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001871 break;
David Henningssonc1051432012-09-20 10:20:41 +02001872 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001873 usb_audio_info(state->chip,
1874 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001875 /* disable non-functional volume control */
1876 channels = 0;
1877 break;
1878
Javier Kohen0c3cee52009-11-17 15:36:13 +01001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001881 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 else
1883 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001884
1885 if (state->mixer->protocol == UAC_VERSION_1) {
1886 /* check all control types */
1887 for (i = 0; i < 10; i++) {
1888 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001889 int control = audio_feature_info[i].control;
1890
Daniel Mack23caaf12010-03-11 21:13:25 +01001891 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001892 unsigned int mask;
1893
1894 mask = snd_usb_combine_bytes(bmaControls +
1895 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001896 if (mask & (1 << i))
1897 ch_bits |= (1 << j);
1898 }
1899 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001900
1901 /*
1902 * The first channel must be set
1903 * (for ease of programming).
1904 */
1905 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001906 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001907 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001908 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001909 build_feature_ctl(state, _ftr, 0, control,
1910 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001912 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001913 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001914 unsigned int ch_bits = 0;
1915 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001916 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001917
1918 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001919 unsigned int mask;
1920
1921 mask = snd_usb_combine_bytes(bmaControls +
1922 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001923 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001924 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001925 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001926 ch_read_only |= (1 << j);
1927 }
1928 }
1929
Daniel Mack6bc170e2014-05-24 10:58:16 +02001930 /*
1931 * NOTE: build_feature_ctl() will mark the control
1932 * read-only if all channels are marked read-only in
1933 * the descriptors. Otherwise, the control will be
1934 * reported as writeable, but the driver will not
1935 * actually issue a write command for read-only
1936 * channels.
1937 */
1938
1939 /*
1940 * The first channel must be set
1941 * (for ease of programming).
1942 */
1943 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001944 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001945 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001946 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001947 build_feature_ctl(state, _ftr, 0, control,
1948 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001949 !uac_v2v3_control_is_writeable(master_bits,
1950 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
1954 return 0;
1955}
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/*
1958 * Mixer Unit
1959 */
1960
1961/*
1962 * build a mixer unit control
1963 *
1964 * the callbacks are identical with feature unit.
1965 * input channel number (zero based) is given in control field instead.
1966 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001967static void build_mixer_unit_ctl(struct mixer_build *state,
1968 struct uac_mixer_unit_descriptor *desc,
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01001969 int in_pin, int in_ch, int num_outs,
1970 int unitid, struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001972 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001974 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001975 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001977 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001978 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return;
1980
Takashi Iwai561b2202005-09-09 14:22:34 +02001981 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001982 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return;
1984
Takashi Iwai3360b842014-11-18 11:47:04 +01001985 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 cval->control = in_ch + 1; /* based on 1 */
1987 cval->val_type = USB_MIXER_S16;
1988 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001989 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1990
1991 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 cval->cmask |= (1 << i);
1993 cval->channels++;
1994 }
1995 }
1996
1997 /* get min/max values */
1998 get_min_max(cval, 0);
1999
2000 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002001 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002002 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 kfree(cval);
2004 return;
2005 }
Chris J Argeseef90452014-11-12 12:07:01 -06002006 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002008 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002009 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002010 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002011 sizeof(kctl->id.name), 0);
2012 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02002014 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002016 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002017 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2018 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Andrew Chant5a222e82018-03-23 19:25:23 -07002021static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2022 void *raw_desc)
2023{
2024 struct usb_audio_term iterm;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002025 unsigned int control, bmctls, term_id;
Andrew Chant5a222e82018-03-23 19:25:23 -07002026
Andrew Chant5a222e82018-03-23 19:25:23 -07002027 if (state->mixer->protocol == UAC_VERSION_2) {
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002028 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2029 control = UAC2_TE_CONNECTOR;
2030 term_id = d_v2->bTerminalID;
2031 bmctls = le16_to_cpu(d_v2->bmControls);
2032 } else if (state->mixer->protocol == UAC_VERSION_3) {
2033 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2034 control = UAC3_TE_INSERTION;
2035 term_id = d_v3->bTerminalID;
2036 bmctls = le32_to_cpu(d_v3->bmControls);
2037 } else {
2038 return 0; /* UAC1. No Insertion control */
Andrew Chant5a222e82018-03-23 19:25:23 -07002039 }
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002040
2041 check_input_term(state, term_id, &iterm);
2042
2043 /* Check for jack detection. */
2044 if (uac_v2v3_control_is_readable(bmctls, control))
2045 build_connector_control(state, &iterm, true);
2046
Andrew Chant5a222e82018-03-23 19:25:23 -07002047 return 0;
2048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050/*
2051 * parse a mixer unit
2052 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002053static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2054 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Daniel Mack99fc8642010-03-11 21:13:24 +01002056 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002057 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int input_pins, num_ins, num_outs;
2059 int pin, ich, err;
2060
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002061 err = uac_mixer_unit_get_channels(state, desc);
2062 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002063 usb_audio_err(state->chip,
2064 "invalid MIXER UNIT descriptor %d\n",
2065 unitid);
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002066 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002069 num_outs = err;
2070 input_pins = desc->bNrInPins;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 num_ins = 0;
2073 ich = 0;
2074 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002075 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002077 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002078 /* no bmControls field (e.g. Maya44) -> ignore */
2079 if (desc->bLength <= 10 + input_pins)
2080 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002081 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (err < 0)
2083 return err;
2084 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002085 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 int och, ich_has_controls = 0;
2087
Daniel Mack6bc170e2014-05-24 10:58:16 +02002088 for (och = 0; och < num_outs; och++) {
2089 __u8 *c = uac_mixer_unit_bmControls(desc,
2090 state->mixer->protocol);
2091
2092 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 ich_has_controls = 1;
2094 break;
2095 }
2096 }
2097 if (ich_has_controls)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002098 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 unitid, &iterm);
2100 }
2101 }
2102 return 0;
2103}
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/*
2106 * Processing Unit / Extension Unit
2107 */
2108
2109/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002110static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002113 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 int err, val;
2115
2116 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002117 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002119 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 val = get_relative_value(cval, val);
2122 ucontrol->value.integer.value[0] = val;
2123 return 0;
2124}
2125
2126/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002127static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2128 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002130 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 int val, oval, err;
2132
2133 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002134 if (err < 0)
2135 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 val = ucontrol->value.integer.value[0];
2137 val = get_abs_value(cval, val);
2138 if (val != oval) {
2139 set_cur_ctl_value(cval, cval->control << 8, val);
2140 return 1;
2141 }
2142 return 0;
2143}
2144
2145/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302146static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2148 .name = "", /* will be filled later */
2149 .info = mixer_ctl_feature_info,
2150 .get = mixer_ctl_procunit_get,
2151 .put = mixer_ctl_procunit_put,
2152};
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/*
2155 * predefined data for processing units
2156 */
2157struct procunit_value_info {
2158 int control;
2159 char *suffix;
2160 int val_type;
2161 int min_value;
2162};
2163
2164struct procunit_info {
2165 int type;
2166 char *name;
2167 struct procunit_value_info *values;
2168};
2169
2170static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002171 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2172 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 { 0 }
2174};
2175static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002176 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2177 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 { 0 }
2179};
2180static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002181 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2182 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 { 0 }
2184};
2185static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002186 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2187 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2188 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2189 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 { 0 }
2191};
2192static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002193 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2194 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2195 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2196 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 { 0 }
2198};
2199static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002200 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2201 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2202 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2203 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2204 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2205 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 { 0 }
2207};
2208
2209static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002210 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2211 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2212 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2213 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2214 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2215 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 { 0 },
2217};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002218/*
2219 * predefined data for extension units
2220 */
2221static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002222 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2223 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002224};
2225static struct procunit_value_info clock_source_xu_info[] = {
2226 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2227 { 0 }
2228};
2229static struct procunit_value_info spdif_format_xu_info[] = {
2230 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2231 { 0 }
2232};
2233static struct procunit_value_info soft_limit_xu_info[] = {
2234 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2235 { 0 }
2236};
2237static struct procunit_info extunits[] = {
2238 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2239 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2240 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2241 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2242 { 0 }
2243};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245/*
2246 * build a processing/extension unit
2247 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002248static int build_audio_procunit(struct mixer_build *state, int unitid,
2249 void *raw_desc, struct procunit_info *list,
2250 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Daniel Mack99fc8642010-03-11 21:13:24 +01002252 struct uac_processing_unit_descriptor *desc = raw_desc;
2253 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002254 struct usb_mixer_elem_info *cval;
2255 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int i, err, nameid, type, len;
2257 struct procunit_info *info;
2258 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002259 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 static struct procunit_value_info default_value_info[] = {
2261 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2262 { 0 }
2263 };
2264 static struct procunit_info default_info = {
2265 0, NULL, default_value_info
2266 };
2267
Daniel Mack23caaf12010-03-11 21:13:25 +01002268 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2269 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002270 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return -EINVAL;
2272 }
2273
2274 for (i = 0; i < num_ins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002275 err = parse_audio_unit(state, desc->baSourceID[i]);
2276 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return err;
2278 }
2279
Daniel Mack99fc8642010-03-11 21:13:24 +01002280 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 for (info = list; info && info->type; info++)
2282 if (info->type == type)
2283 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002284 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 info = &default_info;
2286
2287 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002288 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002289
Daniel Mack6bc170e2014-05-24 10:58:16 +02002290 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 continue;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002292 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002293 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002295 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002296 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002298 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 cval->control = valinfo->control;
2300 cval->val_type = valinfo->val_type;
2301 cval->channels = 1;
2302
2303 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002304 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002305 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /* FIXME: hard-coded */
2307 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002308 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 cval->res = 1;
2310 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002311 } else {
2312 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002313 /*
2314 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002315 * samplerate control quirk
2316 */
2317 cval->min = 0;
2318 cval->max = 5;
2319 cval->res = 1;
2320 cval->initialized = 1;
2321 } else
2322 get_min_max(cval, valinfo->min_value);
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002326 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 kfree(cval);
2328 return -ENOMEM;
2329 }
Chris J Argeseef90452014-11-12 12:07:01 -06002330 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Daniel Mack6bc170e2014-05-24 10:58:16 +02002332 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002333 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002334 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002336 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002337 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 len = 0;
2339 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002340 len = snd_usb_copy_string_desc(state->chip,
2341 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002342 kctl->id.name,
2343 sizeof(kctl->id.name));
2344 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2346 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002347 append_ctl_name(kctl, " ");
2348 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002350 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002351 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002352 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002353 cval->min, cval->max);
2354
Takashi Iwai3360b842014-11-18 11:47:04 +01002355 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002356 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return err;
2358 }
2359 return 0;
2360}
2361
Daniel Mack6bc170e2014-05-24 10:58:16 +02002362static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2363 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002365 return build_audio_procunit(state, unitid, raw_desc,
2366 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Daniel Mack6bc170e2014-05-24 10:58:16 +02002369static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2370 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002372 /*
2373 * Note that we parse extension units with processing unit descriptors.
2374 * That's ok as the layout is the same.
2375 */
2376 return build_audio_procunit(state, unitid, raw_desc,
2377 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380/*
2381 * Selector Unit
2382 */
2383
Daniel Mack6bc170e2014-05-24 10:58:16 +02002384/*
2385 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * use an enumerator type for routing
2387 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002388static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002391 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002392 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Takashi Iwai5e246b82008-08-08 17:12:47 +02002394 if (snd_BUG_ON(!itemlist))
2395 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002396 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002400static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2401 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002403 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 int val, err;
2405
Daniel Mack09414202010-05-31 13:35:44 +02002406 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002408 ucontrol->value.enumerated.item[0] = 0;
2409 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411 val = get_relative_value(cval, val);
2412 ucontrol->value.enumerated.item[0] = val;
2413 return 0;
2414}
2415
2416/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002417static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2418 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002420 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 int val, oval, err;
2422
Daniel Mack09414202010-05-31 13:35:44 +02002423 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002424 if (err < 0)
2425 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 val = ucontrol->value.enumerated.item[0];
2427 val = get_abs_value(cval, val);
2428 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002429 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return 1;
2431 }
2432 return 0;
2433}
2434
2435/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302436static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2438 .name = "", /* will be filled later */
2439 .info = mixer_ctl_selector_info,
2440 .get = mixer_ctl_selector_get,
2441 .put = mixer_ctl_selector_put,
2442};
2443
Daniel Mack6bc170e2014-05-24 10:58:16 +02002444/*
2445 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 * free both private_data and private_value
2447 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002448static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
2450 int i, num_ins = 0;
2451
2452 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002453 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 num_ins = cval->max;
2455 kfree(cval);
2456 kctl->private_data = NULL;
2457 }
2458 if (kctl->private_value) {
2459 char **itemlist = (char **)kctl->private_value;
2460 for (i = 0; i < num_ins; i++)
2461 kfree(itemlist[i]);
2462 kfree(itemlist);
2463 kctl->private_value = 0;
2464 }
2465}
2466
2467/*
2468 * parse a selector unit
2469 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002470static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2471 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Daniel Mack99fc8642010-03-11 21:13:24 +01002473 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 unsigned int i, nameid, len;
2475 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002476 struct usb_mixer_elem_info *cval;
2477 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002478 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 char **namelist;
2480
Takashi Iwaif658f172017-11-21 17:00:32 +01002481 if (desc->bLength < 5 || !desc->bNrInPins ||
2482 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002483 usb_audio_err(state->chip,
2484 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return -EINVAL;
2486 }
2487
Daniel Mack99fc8642010-03-11 21:13:24 +01002488 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002489 err = parse_audio_unit(state, desc->baSourceID[i]);
2490 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return err;
2492 }
2493
Daniel Mack99fc8642010-03-11 21:13:24 +01002494 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
2496
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002497 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002498 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return 0;
2500
Takashi Iwai561b2202005-09-09 14:22:34 +02002501 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002502 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002504 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 cval->val_type = USB_MIXER_U8;
2506 cval->channels = 1;
2507 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002508 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 cval->res = 1;
2510 cval->initialized = 1;
2511
Johan Rasténa358a0e2014-11-17 08:39:33 +01002512 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002513 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002514 else /* UAC_VERSION_2 */
2515 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2516 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002517
Kees Cook6da2ec52018-06-12 13:55:00 -07002518 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002519 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 kfree(cval);
2521 return -ENOMEM;
2522 }
2523#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002524 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002525 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 len = 0;
2527 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002528 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002529 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 kfree(namelist[i]);
2531 kfree(namelist);
2532 kfree(cval);
2533 return -ENOMEM;
2534 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002535 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2536 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002537 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002538 len = get_term_name(state->chip, &iterm, namelist[i],
2539 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002541 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543
2544 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2545 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002546 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002547 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 kfree(cval);
2549 return -ENOMEM;
2550 }
2551 kctl->private_value = (unsigned long)namelist;
2552 kctl->private_free = usb_mixer_selector_elem_free;
2553
Takashi Iwai5a15f282017-12-18 23:36:57 +01002554 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002555 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002556 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002557 /* no mapping ? */
2558 /* if iSelector is given, use it */
2559 nameid = uac_selector_unit_iSelector(desc);
2560 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002561 len = snd_usb_copy_string_desc(state->chip, nameid,
Takashi Iwai5a15f282017-12-18 23:36:57 +01002562 kctl->id.name,
2563 sizeof(kctl->id.name));
2564 /* ... or pick up the terminal name at next */
2565 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002566 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002568 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002569 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2571
Takashi Iwai5a15f282017-12-18 23:36:57 +01002572 /* and add the proper suffix */
Daniel Mack09414202010-05-31 13:35:44 +02002573 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2574 append_ctl_name(kctl, " Clock Source");
2575 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002576 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002578 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002581 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002582 cval->head.id, kctl->id.name, desc->bNrInPins);
2583 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586/*
2587 * parse an audio unit recursively
2588 */
2589
Takashi Iwai86e07d32005-11-17 15:08:02 +01002590static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002593 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 if (test_and_set_bit(unitid, state->unitbitmap))
2596 return 0; /* the unit already visited */
2597
2598 p1 = find_audio_control_unit(state, unitid);
2599 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002600 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return -EINVAL;
2602 }
2603
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002604 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2605 switch (p1[2]) {
2606 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002607 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002608 case UAC_MIXER_UNIT:
2609 return parse_audio_mixer_unit(state, unitid, p1);
2610 case UAC2_CLOCK_SOURCE:
2611 return parse_clock_source_unit(state, unitid, p1);
2612 case UAC_SELECTOR_UNIT:
2613 case UAC2_CLOCK_SELECTOR:
2614 return parse_audio_selector_unit(state, unitid, p1);
2615 case UAC_FEATURE_UNIT:
2616 return parse_audio_feature_unit(state, unitid, p1);
2617 case UAC1_PROCESSING_UNIT:
2618 /* UAC2_EFFECT_UNIT has the same value */
2619 if (protocol == UAC_VERSION_1)
2620 return parse_audio_processing_unit(state, unitid, p1);
2621 else
2622 return 0; /* FIXME - effect units not implemented yet */
2623 case UAC1_EXTENSION_UNIT:
2624 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2625 if (protocol == UAC_VERSION_1)
2626 return parse_audio_extension_unit(state, unitid, p1);
2627 else /* UAC_VERSION_2 */
2628 return parse_audio_processing_unit(state, unitid, p1);
2629 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002630 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002631 default:
2632 usb_audio_err(state->chip,
2633 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2634 return -EINVAL;
2635 }
2636 } else { /* UAC_VERSION_3 */
2637 switch (p1[2]) {
2638 case UAC_INPUT_TERMINAL:
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002639 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002640 case UAC3_MIXER_UNIT:
2641 return parse_audio_mixer_unit(state, unitid, p1);
2642 case UAC3_CLOCK_SOURCE:
2643 return parse_clock_source_unit(state, unitid, p1);
2644 case UAC3_CLOCK_SELECTOR:
2645 return parse_audio_selector_unit(state, unitid, p1);
2646 case UAC3_FEATURE_UNIT:
2647 return parse_audio_feature_unit(state, unitid, p1);
2648 case UAC3_EFFECT_UNIT:
2649 return 0; /* FIXME - effect units not implemented yet */
2650 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002651 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002652 case UAC3_EXTENSION_UNIT:
2653 return parse_audio_extension_unit(state, unitid, p1);
2654 default:
2655 usb_audio_err(state->chip,
2656 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2657 return -EINVAL;
2658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660}
2661
Clemens Ladisch84957a82005-04-29 16:23:13 +02002662static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2663{
Takashi Iwai124751d2017-10-10 14:10:32 +02002664 /* kill pending URBs */
2665 snd_usb_mixer_disconnect(mixer);
2666
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002667 kfree(mixer->id_elems);
2668 if (mixer->urb) {
2669 kfree(mixer->urb->transfer_buffer);
2670 usb_free_urb(mixer->urb);
2671 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002672 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002673 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002674 kfree(mixer);
2675}
2676
Takashi Iwai86e07d32005-11-17 15:08:02 +01002677static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002678{
2679 struct usb_mixer_interface *mixer = device->device_data;
2680 snd_usb_mixer_free(mixer);
2681 return 0;
2682}
2683
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002684/* UAC3 predefined channels configuration */
2685struct uac3_badd_profile {
2686 int subclass;
2687 const char *name;
2688 int c_chmask; /* capture channels mask */
2689 int p_chmask; /* playback channels mask */
2690 int st_chmask; /* side tone mixing channel mask */
2691};
2692
2693static struct uac3_badd_profile uac3_badd_profiles[] = {
2694 {
2695 /*
2696 * BAIF, BAOF or combination of both
2697 * IN: Mono or Stereo cfg, Mono alt possible
2698 * OUT: Mono or Stereo cfg, Mono alt possible
2699 */
2700 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2701 .name = "GENERIC IO",
2702 .c_chmask = -1, /* dynamic channels */
2703 .p_chmask = -1, /* dynamic channels */
2704 },
2705 {
2706 /* BAOF; Stereo only cfg, Mono alt possible */
2707 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2708 .name = "HEADPHONE",
2709 .p_chmask = 3,
2710 },
2711 {
2712 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2713 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2714 .name = "SPEAKER",
2715 .p_chmask = -1, /* dynamic channels */
2716 },
2717 {
2718 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2719 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2720 .name = "MICROPHONE",
2721 .c_chmask = -1, /* dynamic channels */
2722 },
2723 {
2724 /*
2725 * BAIOF topology
2726 * IN: Mono only
2727 * OUT: Mono or Stereo cfg, Mono alt possible
2728 */
2729 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2730 .name = "HEADSET",
2731 .c_chmask = 1,
2732 .p_chmask = -1, /* dynamic channels */
2733 .st_chmask = 1,
2734 },
2735 {
2736 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2737 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2738 .name = "HEADSET ADAPTER",
2739 .c_chmask = 1,
2740 .p_chmask = 3,
2741 .st_chmask = 1,
2742 },
2743 {
2744 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2745 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2746 .name = "SPEAKERPHONE",
2747 .c_chmask = 1,
2748 .p_chmask = 1,
2749 },
2750 { 0 } /* terminator */
2751};
2752
2753static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2754 struct uac3_badd_profile *f,
2755 int c_chmask, int p_chmask)
2756{
2757 /*
2758 * If both playback/capture channels are dynamic, make sure
2759 * at least one channel is present
2760 */
2761 if (f->c_chmask < 0 && f->p_chmask < 0) {
2762 if (!c_chmask && !p_chmask) {
2763 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2764 f->name);
2765 return false;
2766 }
2767 return true;
2768 }
2769
2770 if ((f->c_chmask < 0 && !c_chmask) ||
2771 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2772 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2773 f->name);
2774 return false;
2775 }
2776 if ((f->p_chmask < 0 && !p_chmask) ||
2777 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2778 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2779 f->name);
2780 return false;
2781 }
2782 return true;
2783}
2784
2785/*
2786 * create mixer controls for UAC3 BADD profiles
2787 *
2788 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2789 *
2790 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2791 */
2792static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2793 int ctrlif)
2794{
2795 struct usb_device *dev = mixer->chip->dev;
2796 struct usb_interface_assoc_descriptor *assoc;
2797 int badd_profile = mixer->chip->badd_profile;
2798 struct uac3_badd_profile *f;
2799 const struct usbmix_ctl_map *map;
2800 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2801 int i;
2802
2803 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2804
2805 /* Detect BADD capture/playback channels from AS EP descriptors */
2806 for (i = 0; i < assoc->bInterfaceCount; i++) {
2807 int intf = assoc->bFirstInterface + i;
2808
2809 struct usb_interface *iface;
2810 struct usb_host_interface *alts;
2811 struct usb_interface_descriptor *altsd;
2812 unsigned int maxpacksize;
2813 char dir_in;
2814 int chmask, num;
2815
2816 if (intf == ctrlif)
2817 continue;
2818
2819 iface = usb_ifnum_to_if(dev, intf);
2820 num = iface->num_altsetting;
2821
2822 if (num < 2)
2823 return -EINVAL;
2824
2825 /*
2826 * The number of Channels in an AudioStreaming interface
2827 * and the audio sample bit resolution (16 bits or 24
2828 * bits) can be derived from the wMaxPacketSize field in
2829 * the Standard AS Audio Data Endpoint descriptor in
2830 * Alternate Setting 1
2831 */
2832 alts = &iface->altsetting[1];
2833 altsd = get_iface_desc(alts);
2834
2835 if (altsd->bNumEndpoints < 1)
2836 return -EINVAL;
2837
2838 /* check direction */
2839 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2840 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2841
2842 switch (maxpacksize) {
2843 default:
2844 usb_audio_err(mixer->chip,
2845 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2846 maxpacksize);
2847 return -EINVAL;
2848 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2849 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2850 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2851 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2852 chmask = 1;
2853 break;
2854 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2855 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2856 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2857 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2858 chmask = 3;
2859 break;
2860 }
2861
2862 if (dir_in)
2863 c_chmask = chmask;
2864 else
2865 p_chmask = chmask;
2866 }
2867
2868 usb_audio_dbg(mixer->chip,
2869 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
2870 badd_profile, c_chmask, p_chmask);
2871
2872 /* check the mapping table */
2873 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
2874 if (map->id == badd_profile)
2875 break;
2876 }
2877
2878 if (!map->id)
2879 return -EINVAL;
2880
2881 for (f = uac3_badd_profiles; f->name; f++) {
2882 if (badd_profile == f->subclass)
2883 break;
2884 }
2885 if (!f->name)
2886 return -EINVAL;
2887 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
2888 return -EINVAL;
2889 st_chmask = f->st_chmask;
2890
2891 /* Playback */
2892 if (p_chmask) {
2893 /* Master channel, always writable */
2894 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2895 UAC3_BADD_FU_ID2, map->map);
2896 /* Mono/Stereo volume channels, always writable */
2897 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
2898 UAC3_BADD_FU_ID2, map->map);
2899 }
2900
2901 /* Capture */
2902 if (c_chmask) {
2903 /* Master channel, always writable */
2904 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2905 UAC3_BADD_FU_ID5, map->map);
2906 /* Mono/Stereo volume channels, always writable */
2907 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
2908 UAC3_BADD_FU_ID5, map->map);
2909 }
2910
2911 /* Side tone-mixing */
2912 if (st_chmask) {
2913 /* Master channel, always writable */
2914 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2915 UAC3_BADD_FU_ID7, map->map);
2916 /* Mono volume channel, always writable */
2917 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
2918 UAC3_BADD_FU_ID7, map->map);
2919 }
2920
2921 return 0;
2922}
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924/*
2925 * create mixer controls
2926 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002927 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002929static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002931 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 int err;
2933 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01002934 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002937 state.chip = mixer->chip;
2938 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002939 state.buffer = mixer->hostif->extra;
2940 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002943 for (map = usbmix_ctl_maps; map->id; map++) {
2944 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002946 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002947 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 break;
2949 }
2950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Daniel Mack23caaf12010-03-11 21:13:25 +01002952 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002953 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2954 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02002955 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002956 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02002957 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01002958
2959 if (desc->bLength < sizeof(*desc))
2960 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002961 /* mark terminal ID as visited */
2962 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002963 state.oterm.id = desc->bTerminalID;
2964 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2965 state.oterm.name = desc->iTerminal;
2966 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002967 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002968 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002969 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002970 struct uac2_output_terminal_descriptor *desc = p;
2971
2972 if (desc->bLength < sizeof(*desc))
2973 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002974 /* mark terminal ID as visited */
2975 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002976 state.oterm.id = desc->bTerminalID;
2977 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2978 state.oterm.name = desc->iTerminal;
2979 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002980 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002981 return err;
Daniel Mack09414202010-05-31 13:35:44 +02002982
Daniel Mack6bc170e2014-05-24 10:58:16 +02002983 /*
2984 * For UAC2, use the same approach to also add the
2985 * clock selectors
2986 */
Daniel Mack09414202010-05-31 13:35:44 +02002987 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002988 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02002989 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07002990
Takashi Iwai2b54f7852018-04-23 15:19:25 +02002991 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07002992 UAC2_TE_CONNECTOR)) {
2993 build_connector_control(&state, &state.oterm,
2994 false);
2995 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002996 } else { /* UAC_VERSION_3 */
2997 struct uac3_output_terminal_descriptor *desc = p;
2998
2999 if (desc->bLength < sizeof(*desc))
3000 continue; /* invalid descriptor? */
3001 /* mark terminal ID as visited */
3002 set_bit(desc->bTerminalID, state.unitbitmap);
3003 state.oterm.id = desc->bTerminalID;
3004 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3005 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3006 err = parse_audio_unit(&state, desc->bSourceID);
3007 if (err < 0 && err != -EINVAL)
3008 return err;
3009
3010 /*
3011 * For UAC3, use the same approach to also add the
3012 * clock selectors
3013 */
3014 err = parse_audio_unit(&state, desc->bCSourceID);
3015 if (err < 0 && err != -EINVAL)
3016 return err;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003017
3018 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3019 UAC3_TE_INSERTION)) {
3020 build_connector_control(&state, &state.oterm,
3021 false);
3022 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 0;
3027}
Clemens Ladisch84957a82005-04-29 16:23:13 +02003028
Daniel Mack7b1eda22010-03-11 21:13:22 +01003029void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003030{
Takashi Iwai3360b842014-11-18 11:47:04 +01003031 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003032
Takashi Iwai8c558072018-05-03 12:33:32 +02003033 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01003034 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02003035 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01003036 /* invalidate cache, so the value is read from the device */
3037 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003038 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003039 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01003040 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003041}
3042
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003043static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01003044 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003045{
Takashi Iwai8c558072018-05-03 12:33:32 +02003046 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003047 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3048 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003049 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01003050 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003051 cval->control, cval->cmask, cval->channels,
3052 val_types[cval->val_type]);
3053 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3054 cval->min, cval->max, cval->dBmin, cval->dBmax);
3055}
3056
3057static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3058 struct snd_info_buffer *buffer)
3059{
3060 struct snd_usb_audio *chip = entry->private_data;
3061 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01003062 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003063 int unitid;
3064
3065 list_for_each_entry(mixer, &chip->mixer_list, list) {
3066 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003067 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02003068 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003069 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003070 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3071 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003072 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003073 snd_iprintf(buffer, " Unit: %i\n", list->id);
3074 if (list->kctl)
3075 snd_iprintf(buffer,
3076 " Control: name=\"%s\", index=%i\n",
3077 list->kctl->id.name,
3078 list->kctl->id.index);
3079 if (list->dump)
3080 list->dump(buffer, list);
3081 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003082 }
3083 }
3084}
3085
Daniel Macke213e9c2010-05-11 18:13:50 +02003086static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3087 int attribute, int value, int index)
3088{
Takashi Iwai3360b842014-11-18 11:47:04 +01003089 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02003090 __u8 unitid = (index >> 8) & 0xff;
3091 __u8 control = (value >> 8) & 0xff;
3092 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02003093 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02003094
3095 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003096 usb_audio_dbg(mixer->chip,
3097 "%s(): bogus channel number %d\n",
3098 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02003099 return;
3100 }
3101
Takashi Iwai8c558072018-05-03 12:33:32 +02003102 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02003103 count++;
3104
3105 if (count == 0)
3106 return;
3107
Takashi Iwai8c558072018-05-03 12:33:32 +02003108 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003109 struct usb_mixer_elem_info *info;
3110
3111 if (!list->kctl)
3112 continue;
3113
Takashi Iwai8c558072018-05-03 12:33:32 +02003114 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02003115 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02003116 continue;
3117
3118 switch (attribute) {
3119 case UAC2_CS_CUR:
3120 /* invalidate cache, so the value is read from the device */
3121 if (channel)
3122 info->cached &= ~(1 << channel);
3123 else /* master channel */
3124 info->cached = 0;
3125
3126 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003127 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02003128 break;
3129
3130 case UAC2_CS_RANGE:
3131 /* TODO */
3132 break;
3133
3134 case UAC2_CS_MEM:
3135 /* TODO */
3136 break;
3137
3138 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003139 usb_audio_dbg(mixer->chip,
3140 "unknown attribute %d in interrupt\n",
3141 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02003142 break;
3143 } /* switch */
3144 }
3145}
3146
3147static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003148{
3149 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02003150 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01003151 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003152
Oliver Neukumedf7de32011-03-11 13:19:43 +01003153 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02003154 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003155
Daniel Macke213e9c2010-05-11 18:13:50 +02003156 if (mixer->protocol == UAC_VERSION_1) {
3157 struct uac1_status_word *status;
3158
3159 for (status = urb->transfer_buffer;
3160 len >= sizeof(*status);
3161 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003162 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02003163 status->bStatusType,
3164 status->bOriginator);
3165
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003166 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02003167 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3168 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003169 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003170
3171 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3172 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003173 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003174 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3175 }
3176 } else { /* UAC_VERSION_2 */
3177 struct uac2_interrupt_data_msg *msg;
3178
3179 for (msg = urb->transfer_buffer;
3180 len >= sizeof(*msg);
3181 len -= sizeof(*msg), msg++) {
3182 /* drop vendor specific and endpoint requests */
3183 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3184 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3185 continue;
3186
3187 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3188 le16_to_cpu(msg->wValue),
3189 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003190 }
3191 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003192
3193requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003194 if (ustatus != -ENOENT &&
3195 ustatus != -ECONNRESET &&
3196 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003197 urb->dev = mixer->chip->dev;
3198 usb_submit_urb(urb, GFP_ATOMIC);
3199 }
3200}
3201
3202/* create the handler for the optional status interrupt endpoint */
3203static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3204{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003205 struct usb_endpoint_descriptor *ep;
3206 void *transfer_buffer;
3207 int buffer_length;
3208 unsigned int epnum;
3209
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003210 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003211 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003212 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003213 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003214 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003215 return 0;
3216
Julia Lawall42a6e662008-12-29 11:23:02 +01003217 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003218 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3219 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3220 if (!transfer_buffer)
3221 return -ENOMEM;
3222 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3223 if (!mixer->urb) {
3224 kfree(transfer_buffer);
3225 return -ENOMEM;
3226 }
3227 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3228 usb_rcvintpipe(mixer->chip->dev, epnum),
3229 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003230 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003231 usb_submit_urb(mixer->urb, GFP_KERNEL);
3232 return 0;
3233}
3234
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003235static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_value *ucontrol)
3237{
3238 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3239
3240 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3241 return 0;
3242}
3243
3244static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3245 struct snd_ctl_elem_value *ucontrol)
3246{
3247 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3248 bool keep_iface = !!ucontrol->value.integer.value[0];
3249
3250 if (mixer->chip->keep_iface == keep_iface)
3251 return 0;
3252 mixer->chip->keep_iface = keep_iface;
3253 return 1;
3254}
3255
3256static const struct snd_kcontrol_new keep_iface_ctl = {
3257 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3258 .name = "Keep Interface",
3259 .info = snd_ctl_boolean_mono_info,
3260 .get = keep_iface_ctl_get,
3261 .put = keep_iface_ctl_put,
3262};
3263
3264static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3265{
3266 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3267
3268 /* need only one control per card */
3269 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3270 snd_ctl_free_one(kctl);
3271 return 0;
3272 }
3273
3274 return snd_ctl_add(mixer->chip->card, kctl);
3275}
3276
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003277int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3278 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003279{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003280 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003281 .dev_free = snd_usb_mixer_dev_free
3282 };
3283 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003284 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01003285 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003286
3287 strcpy(chip->card->mixername, "USB Mixer");
3288
Takashi Iwai561b2202005-09-09 14:22:34 +02003289 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003290 if (!mixer)
3291 return -ENOMEM;
3292 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003293 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003294 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3295 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003296 if (!mixer->id_elems) {
3297 kfree(mixer);
3298 return -ENOMEM;
3299 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003300
Daniel Mack1faa5d02011-08-04 15:56:28 +02003301 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3302 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003303 case UAC_VERSION_1:
3304 default:
3305 mixer->protocol = UAC_VERSION_1;
3306 break;
3307 case UAC_VERSION_2:
3308 mixer->protocol = UAC_VERSION_2;
3309 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003310 case UAC_VERSION_3:
3311 mixer->protocol = UAC_VERSION_3;
3312 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003313 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003314
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003315 if (mixer->protocol == UAC_VERSION_3 &&
3316 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003317 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3318 if (err < 0)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003319 goto _error;
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003320 } else {
3321 err = snd_usb_mixer_controls(mixer);
3322 if (err < 0)
3323 goto _error;
3324 err = snd_usb_mixer_status_create(mixer);
3325 if (err < 0)
3326 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003327 }
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003328 err = create_keep_iface_ctl(mixer);
3329 if (err < 0)
3330 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003331
Daniel Mack7b1eda22010-03-11 21:13:22 +01003332 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003333
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003334 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003335 if (err < 0)
3336 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003337
3338 if (list_empty(&chip->mixer_list) &&
3339 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3340 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3341
Clemens Ladisch84957a82005-04-29 16:23:13 +02003342 list_add(&mixer->list, &chip->mixer_list);
3343 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003344
3345_error:
3346 snd_usb_mixer_free(mixer);
3347 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003348}
3349
Takashi Iwaia6cece92014-10-31 11:24:32 +01003350void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003351{
Takashi Iwai124751d2017-10-10 14:10:32 +02003352 if (mixer->disconnected)
3353 return;
3354 if (mixer->urb)
3355 usb_kill_urb(mixer->urb);
3356 if (mixer->rc_urb)
3357 usb_kill_urb(mixer->rc_urb);
3358 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003359}
Takashi Iwai400362f2014-01-20 16:51:16 +01003360
3361#ifdef CONFIG_PM
3362/* stop any bus activity of a mixer */
3363static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3364{
3365 usb_kill_urb(mixer->urb);
3366 usb_kill_urb(mixer->rc_urb);
3367}
3368
3369static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3370{
3371 int err;
3372
3373 if (mixer->urb) {
3374 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3375 if (err < 0)
3376 return err;
3377 }
3378
3379 return 0;
3380}
3381
3382int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3383{
3384 snd_usb_mixer_inactivate(mixer);
3385 return 0;
3386}
3387
Takashi Iwai3360b842014-11-18 11:47:04 +01003388static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003389{
Takashi Iwai8c558072018-05-03 12:33:32 +02003390 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003391 int c, err, idx;
3392
3393 if (cval->cmask) {
3394 idx = 0;
3395 for (c = 0; c < MAX_CHANNELS; c++) {
3396 if (!(cval->cmask & (1 << c)))
3397 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003398 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003399 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003400 cval->cache_val[idx]);
3401 if (err < 0)
3402 return err;
3403 }
3404 idx++;
3405 }
3406 } else {
3407 /* master */
3408 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003409 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003410 if (err < 0)
3411 return err;
3412 }
3413 }
3414
3415 return 0;
3416}
3417
3418int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3419{
Takashi Iwai3360b842014-11-18 11:47:04 +01003420 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003421 int id, err;
3422
Takashi Iwai400362f2014-01-20 16:51:16 +01003423 if (reset_resume) {
3424 /* restore cached mixer values */
3425 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003426 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003427 if (list->resume) {
3428 err = list->resume(list);
3429 if (err < 0)
3430 return err;
3431 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003432 }
3433 }
3434 }
3435
Takashi Iwai964af632018-04-27 14:23:37 +02003436 snd_usb_mixer_resume_quirk(mixer);
3437
Takashi Iwai400362f2014-01-20 16:51:16 +01003438 return snd_usb_mixer_activate(mixer);
3439}
3440#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003441
3442void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3443 struct usb_mixer_interface *mixer,
3444 int unitid)
3445{
3446 list->mixer = mixer;
3447 list->id = unitid;
3448 list->dump = snd_usb_mixer_dump_cval;
3449#ifdef CONFIG_PM
3450 list->resume = restore_mixer_value;
3451#endif
3452}