blob: fb77847cbffc095a593ba805fdb7a4cb3d124774 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Daniel Mack157a57b2010-06-16 17:57:30 +020029/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
Daniel Mackcddaafb2016-04-09 11:21:46 +020048#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010052#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010053#include <linux/usb/audio-v2.h>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020054#include <linux/usb/audio-v3.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <sound/core.h>
57#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020058#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020059#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020060#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010063#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010064#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010065#include "mixer_quirks.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010066#include "power.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020067
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010068#define MAX_ID_ELEMS 256
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
Takashi Iwai86e07d32005-11-17 15:08:02 +010080struct mixer_build {
81 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020082 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned char *buffer;
84 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010085 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010086 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020088 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050091/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080092enum {
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
99};
100enum {
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
111 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100112#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100114static const struct usbmix_name_map *
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300115find_map(const struct usbmix_name_map *p, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100117 if (!p)
118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300120 for (; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
123 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100125 return NULL;
126}
127
128/* get the mapped name if the unit matches */
129static int
130check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
131{
132 if (!p || !p->name)
133 return 0;
134
135 buflen--;
136 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Takashi Iwai5aeee342014-11-18 11:02:14 +0100139/* ignore the error value if ignore_ctl_error flag is set */
140#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100144static inline int
145check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100149 return 1;
150}
151
152/* dB mapping */
153static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
155{
156 if (p && p->dB) {
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200159 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200163/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100164static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200165 int index, char *buf, int buflen)
166{
167 const struct usbmix_selector_map *p;
168
Daniel Mack6bc170e2014-05-24 10:58:16 +0200169 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200170 return 0;
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
174 }
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * find an audio control unit with the given unit id
180 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200181static void *find_audio_control_unit(struct mixer_build *state,
182 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Daniel Mack67e1daa2010-05-31 13:35:43 +0200187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
Daniel Mack67e1daa2010-05-31 13:35:43 +0200192 hdr->bUnitID == unit)
193 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * copy a string with the given id
201 */
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300202static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200203 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
Jaejoong Kim251552a2017-12-04 15:31:48 +0900206
207 if (len < 0)
208 return 0;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 buf[len] = 0;
211 return len;
212}
213
214/*
215 * convert from the byte/word on usb descriptor to the zero-based integer
216 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
221 return !!val;
222 case USB_MIXER_INV_BOOLEAN:
223 return !val;
224 case USB_MIXER_U8:
225 val &= 0xff;
226 break;
227 case USB_MIXER_S8:
228 val &= 0xff;
229 if (val >= 0x80)
230 val -= 0x100;
231 break;
232 case USB_MIXER_U16:
233 val &= 0xffff;
234 break;
235 case USB_MIXER_S16:
236 val &= 0xffff;
237 if (val >= 0x8000)
238 val -= 0x10000;
239 break;
240 }
241 return val;
242}
243
244/*
245 * convert from the zero-based int to the byte/word for usb descriptor
246 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100247static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 switch (cval->val_type) {
250 case USB_MIXER_BOOLEAN:
251 return !!val;
252 case USB_MIXER_INV_BOOLEAN:
253 return !val;
254 case USB_MIXER_S8:
255 case USB_MIXER_U8:
256 return val & 0xff;
257 case USB_MIXER_S16:
258 case USB_MIXER_U16:
259 return val & 0xffff;
260 }
261 return 0; /* not reached */
262}
263
Takashi Iwai86e07d32005-11-17 15:08:02 +0100264static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200266 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cval->res = 1;
268 if (val < cval->min)
269 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 else
273 return (val - cval->min) / cval->res;
274}
275
Takashi Iwai86e07d32005-11-17 15:08:02 +0100276static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (val < 0)
279 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200280 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cval->res = 1;
282 val *= cval->res;
283 val += cval->min;
284 if (val > cval->max)
285 return cval->max;
286 return val;
287}
288
Julian Scheelbc18e312015-08-14 16:14:45 +0200289static int uac2_ctl_value_size(int val_type)
290{
291 switch (val_type) {
292 case USB_MIXER_S32:
293 case USB_MIXER_U32:
294 return 4;
295 case USB_MIXER_S16:
296 case USB_MIXER_U16:
297 return 2;
298 default:
299 return 1;
300 }
301 return 0; /* unreachable */
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/*
306 * retrieve a mixer value
307 */
308
Daniel Mack6bc170e2014-05-24 10:58:16 +0200309static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai3360b842014-11-18 11:47:04 +0100312 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
315 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200316 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Takashi Iwai47ab1542015-08-25 16:09:00 +0200318 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100319 if (err < 0)
320 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200329 err = 0;
330 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700331 } else if (err == -ETIMEDOUT) {
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100335 usb_audio_dbg(chip,
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200338 err = -EINVAL;
339
340 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200341 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Daniel Mack6bc170e2014-05-24 10:58:16 +0200345static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100347{
Takashi Iwai3360b842014-11-18 11:47:04 +0100348 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100351 unsigned char *val;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100352 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100353 __u8 bRequest;
354
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100355 val_size = uac2_ctl_value_size(cval->val_type);
356
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100359 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200360 } else {
361 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100362 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200363 }
364
365 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100366
Takashi Iwai47ab1542015-08-25 16:09:00 +0200367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100368 if (ret)
369 goto error;
370
Takashi Iwai47ab1542015-08-25 16:09:00 +0200371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200374 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200375 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100376
377 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100378error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100379 usb_audio_err(chip,
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100382 return ret;
383 }
384
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200385 /* FIXME: how should we handle multiple triplets here? */
386
Daniel Mack23caaf12010-03-11 21:13:25 +0100387 switch (request) {
388 case UAC_GET_CUR:
389 val = buf;
390 break;
391 case UAC_GET_MIN:
392 val = buf + sizeof(__u16);
393 break;
394 case UAC_GET_MAX:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100395 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100396 break;
397 case UAC_GET_RES:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100398 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100399 break;
400 default:
401 return -EINVAL;
402 }
403
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100406
407 return 0;
408}
409
Daniel Mack6bc170e2014-05-24 10:58:16 +0200410static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100412{
Eldad Zack9f814102012-11-28 23:55:35 +0100413 validx += cval->idx_off;
414
Takashi Iwai3360b842014-11-18 11:47:04 +0100415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
418}
419
Daniel Mack6bc170e2014-05-24 10:58:16 +0200420static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100427static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
432 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Chris J Argeseef90452014-11-12 12:07:01 -0600435int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100436 int channel, int index, int *value)
437{
438 int err;
439
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
442 return 0;
443 }
444 err = get_cur_mix_raw(cval, channel, value);
445 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100448 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200449 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100450 return err;
451 }
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * set a mixer value
459 */
460
Daniel Mack7b1eda22010-03-11 21:13:22 +0100461int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai3360b842014-11-18 11:47:04 +0100464 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200465 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200466 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100467
Eldad Zack9f814102012-11-28 23:55:35 +0100468 validx += cval->idx_off;
469
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200470
Takashi Iwai3360b842014-11-18 11:47:04 +0100471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200473 } else { /* UAC_VERSION_2/3 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200490
491 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 if (err < 0)
493 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200494
Takashi Iwai978520b2012-10-12 15:12:55 +0200495 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
501 if (err >= 0) {
Takashi Iwai978520b2012-10-12 15:12:55 +0200502 err = 0;
503 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700504 } else if (err == -ETIMEDOUT) {
505 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100506 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200507 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100508 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200509 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 err = -EINVAL;
511
512 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200513 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Daniel Mack6bc170e2014-05-24 10:58:16 +0200517static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Chris J Argeseef90452014-11-12 12:07:01 -0600523int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100524 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai641b4872009-01-15 17:05:24 +0100526 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
530
531 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100532 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200534 __func__, channel, cval->control);
535 return 0;
536 }
537
Daniel Mack6bc170e2014-05-24 10:58:16 +0200538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
540 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100541 if (err < 0)
542 return err;
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200548/*
549 * TLV callback for mixer volume controls
550 */
Felix Homann285de9c2012-04-23 20:24:24 +0200551int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200552 unsigned int size, unsigned int __user *_tlv)
553{
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200556
557 if (size < sizeof(scale))
558 return -ENOMEM;
Takashi Iwai0f174b32017-08-16 14:18:37 +0200559 if (cval->min_mute)
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200563 if (copy_to_user(_tlv, scale, sizeof(scale)))
564 return -EFAULT;
565 return 0;
566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * parser routines begin here...
570 */
571
Takashi Iwai86e07d32005-11-17 15:08:02 +0100572static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574
575/*
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
578 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200579static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
589 *
590 * if failed, give up and free the control instance.
591 */
592
Takashi Iwai3360b842014-11-18 11:47:04 +0100593int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200594 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Takashi Iwai3360b842014-11-18 11:47:04 +0100596 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200598
Daniel Mackef9d5972011-05-25 09:09:00 +0200599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kctl->id.index++;
Daniel Mackef9d5972011-05-25 09:09:00 +0200601 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200602 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
603 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100606 list->kctl = kctl;
607 list->next_id_elem = mixer->id_elems[list->id];
608 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * get a terminal name string
614 */
615
616static struct iterm_name_combo {
617 int type;
618 char *name;
619} iterm_names[] = {
620 { 0x0300, "Output" },
621 { 0x0301, "Speaker" },
622 { 0x0302, "Headphone" },
623 { 0x0303, "HMD Audio" },
624 { 0x0304, "Desktop Speaker" },
625 { 0x0305, "Room Speaker" },
626 { 0x0306, "Com Speaker" },
627 { 0x0307, "LFE" },
628 { 0x0600, "External In" },
629 { 0x0601, "Analog In" },
630 { 0x0602, "Digital In" },
631 { 0x0603, "Line" },
632 { 0x0604, "Legacy In" },
633 { 0x0605, "IEC958 In" },
634 { 0x0606, "1394 DA Stream" },
635 { 0x0607, "1394 DV Stream" },
636 { 0x0700, "Embedded" },
637 { 0x0701, "Noise Source" },
638 { 0x0702, "Equalization Noise" },
639 { 0x0703, "CD" },
640 { 0x0704, "DAT" },
641 { 0x0705, "DCC" },
642 { 0x0706, "MiniDisk" },
643 { 0x0707, "Analog Tape" },
644 { 0x0708, "Phonograph" },
645 { 0x0709, "VCR Audio" },
646 { 0x070a, "Video Disk Audio" },
647 { 0x070b, "DVD Audio" },
648 { 0x070c, "TV Tuner Audio" },
649 { 0x070d, "Satellite Rec Audio" },
650 { 0x070e, "Cable Tuner Audio" },
651 { 0x070f, "DSS Audio" },
652 { 0x0710, "Radio Receiver" },
653 { 0x0711, "Radio Transmitter" },
654 { 0x0712, "Multi-Track Recorder" },
655 { 0x0713, "Synthesizer" },
656 { 0 },
657};
658
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300659static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned char *name, int maxlen, int term_only)
661{
662 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100663 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100665 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300666 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200667 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100668 if (len)
669 return len;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* virtual type - not a real terminal */
673 if (iterm->type >> 16) {
674 if (term_only)
675 return 0;
676 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100677 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200678 strcpy(name, "Selector");
679 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200680 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200681 strcpy(name, "Process Unit");
682 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200683 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200684 strcpy(name, "Ext Unit");
685 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100686 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200687 strcpy(name, "Mixer");
688 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 default:
690 return sprintf(name, "Unit %d", iterm->id);
691 }
692 }
693
694 switch (iterm->type & 0xff00) {
695 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200696 strcpy(name, "PCM");
697 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200699 strcpy(name, "Mic");
700 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200702 strcpy(name, "Headset");
703 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200705 strcpy(name, "Phone");
706 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
Daniel Mack6bc170e2014-05-24 10:58:16 +0200709 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (names->type == iterm->type) {
711 strcpy(name, names->name);
712 return strlen(names->name);
713 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100720 * Get logical cluster information for UAC3 devices.
721 */
722static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
723{
724 struct uac3_cluster_header_descriptor c_header;
725 int err;
726
727 err = snd_usb_ctl_msg(state->chip->dev,
728 usb_rcvctrlpipe(state->chip->dev, 0),
729 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
730 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
731 cluster_id,
732 snd_usb_ctrl_intf(state->chip),
733 &c_header, sizeof(c_header));
734 if (err < 0)
735 goto error;
736 if (err != sizeof(c_header)) {
737 err = -EIO;
738 goto error;
739 }
740
741 return c_header.bNrChannels;
742
743error:
744 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
745 return err;
746}
747
748/*
749 * Get number of channels for a Mixer Unit.
750 */
751static int uac_mixer_unit_get_channels(struct mixer_build *state,
752 struct uac_mixer_unit_descriptor *desc)
753{
754 int mu_channels;
755
756 if (desc->bLength < 11)
757 return -EINVAL;
758 if (!desc->bNrInPins)
759 return -EINVAL;
760
761 switch (state->mixer->protocol) {
762 case UAC_VERSION_1:
763 case UAC_VERSION_2:
764 default:
765 mu_channels = uac_mixer_unit_bNrChannels(desc);
766 break;
767 case UAC_VERSION_3:
768 mu_channels = get_cluster_channels_v3(state,
769 uac3_mixer_unit_wClusterDescrID(desc));
770 break;
771 }
772
773 if (!mu_channels)
774 return -EINVAL;
775
776 return mu_channels;
777}
778
779/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * parse the source unit recursively until it reaches to a terminal
781 * or a branched unit.
782 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200783static int check_input_term(struct mixer_build *state, int id,
784 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200786 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200787 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100788 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 memset(term, 0, sizeof(*term));
791 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100792 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200794
795 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
796 switch (hdr[2]) {
797 case UAC_INPUT_TERMINAL:
798 if (protocol == UAC_VERSION_1) {
799 struct uac_input_terminal_descriptor *d = p1;
800
801 term->type = le16_to_cpu(d->wTerminalType);
802 term->channels = d->bNrChannels;
803 term->chconfig = le16_to_cpu(d->wChannelConfig);
804 term->name = d->iTerminal;
805 } else { /* UAC_VERSION_2 */
806 struct uac2_input_terminal_descriptor *d = p1;
807
808 /* call recursively to verify that the
809 * referenced clock entity is valid */
810 err = check_input_term(state, d->bCSourceID, term);
811 if (err < 0)
812 return err;
813
814 /* save input term properties after recursion,
815 * to ensure they are not overriden by the
816 * recursion calls */
817 term->id = id;
818 term->type = le16_to_cpu(d->wTerminalType);
819 term->channels = d->bNrChannels;
820 term->chconfig = le32_to_cpu(d->bmChannelConfig);
821 term->name = d->iTerminal;
822 }
823 return 0;
824 case UAC_FEATURE_UNIT: {
825 /* the header is the same for v1 and v2 */
826 struct uac_feature_unit_descriptor *d = p1;
827
828 id = d->bSourceID;
829 break; /* continue to parse */
830 }
831 case UAC_MIXER_UNIT: {
832 struct uac_mixer_unit_descriptor *d = p1;
833
834 term->type = d->bDescriptorSubtype << 16; /* virtual type */
835 term->channels = uac_mixer_unit_bNrChannels(d);
836 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
837 term->name = uac_mixer_unit_iMixer(d);
838 return 0;
839 }
840 case UAC_SELECTOR_UNIT:
841 case UAC2_CLOCK_SELECTOR: {
842 struct uac_selector_unit_descriptor *d = p1;
843 /* call recursively to retrieve the channel info */
844 err = check_input_term(state, d->baSourceID[0], term);
845 if (err < 0)
846 return err;
847 term->type = d->bDescriptorSubtype << 16; /* virtual type */
848 term->id = id;
849 term->name = uac_selector_unit_iSelector(d);
850 return 0;
851 }
852 case UAC1_PROCESSING_UNIT:
853 case UAC1_EXTENSION_UNIT:
854 /* UAC2_PROCESSING_UNIT_V2 */
855 /* UAC2_EFFECT_UNIT */
856 case UAC2_EXTENSION_UNIT_V2: {
857 struct uac_processing_unit_descriptor *d = p1;
858
859 if (protocol == UAC_VERSION_2 &&
860 hdr[2] == UAC2_EFFECT_UNIT) {
861 /* UAC2/UAC1 unit IDs overlap here in an
862 * uncompatible way. Ignore this unit for now.
863 */
864 return 0;
865 }
866
867 if (d->bNrInPins) {
868 id = d->baSourceID[0];
869 break; /* continue to parse */
870 }
871 term->type = d->bDescriptorSubtype << 16; /* virtual type */
872 term->channels = uac_processing_unit_bNrChannels(d);
873 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
874 term->name = uac_processing_unit_iProcessing(d, protocol);
875 return 0;
876 }
877 case UAC2_CLOCK_SOURCE: {
878 struct uac_clock_source_descriptor *d = p1;
879
880 term->type = d->bDescriptorSubtype << 16; /* virtual type */
881 term->id = id;
882 term->name = d->iClockSource;
883 return 0;
884 }
885 default:
886 return -ENODEV;
887 }
888 } else { /* UAC_VERSION_3 */
889 switch (hdr[2]) {
890 case UAC_INPUT_TERMINAL: {
891 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200892
893 /* call recursively to verify that the
894 * referenced clock entity is valid */
895 err = check_input_term(state, d->bCSourceID, term);
896 if (err < 0)
897 return err;
898
899 /* save input term properties after recursion,
900 * to ensure they are not overriden by the
901 * recursion calls */
902 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100903 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100904
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200905 /* REVISIT: UAC3 IT doesn't have channels/cfg */
906 term->channels = 0;
907 term->chconfig = 0;
908
909 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100910 return 0;
911 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200912 case UAC3_FEATURE_UNIT: {
913 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100914
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200915 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break; /* continue to parse */
917 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200918 case UAC3_CLOCK_SOURCE: {
919 struct uac3_clock_source_descriptor *d = p1;
920
921 term->type = d->bDescriptorSubtype << 16; /* virtual type */
922 term->id = id;
923 term->name = le16_to_cpu(d->wClockSourceStr);
924 return 0;
925 }
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100926 case UAC3_MIXER_UNIT: {
927 struct uac_mixer_unit_descriptor *d = p1;
928
929 err = uac_mixer_unit_get_channels(state, d);
930 if (err < 0)
931 return err;
932
933 term->channels = err;
934 term->type = d->bDescriptorSubtype << 16; /* virtual type */
935
936 return 0;
937 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200938 default:
939 return -ENODEV;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942 }
943 return -ENODEV;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * Feature Unit
948 */
949
950/* feature unit control information */
951struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700952 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200954 int type; /* data type for uac1 */
955 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956};
957
958static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700959 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
960 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
961 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
962 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
963 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
964 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
965 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
966 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
967 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
968 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200969 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700970 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
971 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
972 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973};
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600976void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Jesper Juhl4d572772005-05-30 17:30:32 +0200978 kfree(kctl->private_data);
979 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * interface to ALSA control for feature/mixer units
984 */
985
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100986/* volume control quirks */
987static void volume_control_quirks(struct usb_mixer_elem_info *cval,
988 struct snd_kcontrol *kctl)
989{
Takashi Iwai3360b842014-11-18 11:47:04 +0100990 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100991 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +0100992 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500993 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +0100994 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
995 cval->min = 0x0000;
996 cval->max = 0xffff;
997 cval->res = 0x00e6;
998 break;
999 }
1000 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1001 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1002 cval->min = 0x00;
1003 cval->max = 0xff;
1004 break;
1005 }
1006 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1007 cval->min = 0xb706;
1008 cval->max = 0xff7b;
1009 cval->res = 0x0073;
1010 break;
1011 }
1012 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1013 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1014 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1015 cval->max = 0xfcfe;
1016 cval->res = 0x0073;
1017 }
1018 break;
1019
Felix Homannd34bf142012-04-23 20:24:27 +02001020 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1021 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1022 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001023 usb_audio_info(chip,
1024 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001025 cval->min = 0x0000;
1026 cval->max = 0x7f00;
1027 cval->res = 0x0100;
1028 break;
1029 }
1030 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1031 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001032 usb_audio_info(chip,
1033 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001034 cval->min = 0x00;
1035 cval->max = 0x7f;
1036 break;
1037 }
1038 break;
1039
Federico Cuello21493312018-05-09 00:13:38 +02001040 case USB_ID(0x0d8c, 0x0103):
1041 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1042 usb_audio_info(chip,
1043 "set volume quirk for CM102-A+/102S+\n");
1044 cval->min = -256;
1045 }
1046 break;
1047
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001048 case USB_ID(0x0471, 0x0101):
1049 case USB_ID(0x0471, 0x0104):
1050 case USB_ID(0x0471, 0x0105):
1051 case USB_ID(0x0672, 0x1041):
1052 /* quirk for UDA1321/N101.
1053 * note that detection between firmware 2.1.1.7 (N101)
1054 * and later 2.1.1.21 is not very clear from datasheets.
1055 * I hope that the min value is -15360 for newer firmware --jk
1056 */
1057 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1058 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001059 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001060 "set volume quirk for UDA1321/N101 chip\n");
1061 cval->max = -256;
1062 }
1063 break;
1064
1065 case USB_ID(0x046d, 0x09a4):
1066 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001067 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001068 "set volume quirk for QuickCam E3500\n");
1069 cval->min = 6080;
1070 cval->max = 8768;
1071 cval->res = 192;
1072 }
1073 break;
1074
Takashi Iwaie805ca82014-03-05 12:34:39 +01001075 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001076 case USB_ID(0x046d, 0x0808):
1077 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001078 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001079 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001080 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001081 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001082 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001083 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001084 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001085 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001086 /* Most audio usb devices lie about volume resolution.
1087 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001088 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001089 */
1090 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001091 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001092 "set resolution quirk: cval->res = 384\n");
1093 cval->res = 384;
1094 }
1095 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001096 }
1097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/*
1100 * retrieve the minimum and maximum values for the specified control
1101 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001102static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1103 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 /* for failsafe */
1106 cval->min = default_min;
1107 cval->max = cval->min + 1;
1108 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001109 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (cval->val_type == USB_MIXER_BOOLEAN ||
1112 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1113 cval->initialized = 1;
1114 } else {
1115 int minchn = 0;
1116 if (cval->cmask) {
1117 int i;
1118 for (i = 0; i < MAX_CHANNELS; i++)
1119 if (cval->cmask & (1 << i)) {
1120 minchn = i + 1;
1121 break;
1122 }
1123 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001124 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1125 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001126 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001127 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001128 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1129 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return -EINVAL;
1131 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001132 if (get_ctl_value(cval, UAC_GET_RES,
1133 (cval->control << 8) | minchn,
1134 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 cval->res = 1;
1136 } else {
1137 int last_valid_res = cval->res;
1138
1139 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001140 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001141 (cval->control << 8) | minchn,
1142 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 break;
1144 cval->res /= 2;
1145 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001146 if (get_ctl_value(cval, UAC_GET_RES,
1147 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 cval->res = last_valid_res;
1149 }
1150 if (cval->res == 0)
1151 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001152
1153 /* Additional checks for the proper resolution
1154 *
1155 * Some devices report smaller resolutions than actually
1156 * reacting. They don't return errors but simply clip
1157 * to the lower aligned value.
1158 */
1159 if (cval->min + cval->res < cval->max) {
1160 int last_valid_res = cval->res;
1161 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001162 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001163 for (;;) {
1164 test = saved;
1165 if (test < cval->max)
1166 test += cval->res;
1167 else
1168 test -= cval->res;
1169 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001170 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001171 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001172 cval->res = last_valid_res;
1173 break;
1174 }
1175 if (test == check)
1176 break;
1177 cval->res *= 2;
1178 }
Chris J Argeseef90452014-11-12 12:07:01 -06001179 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001180 }
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 cval->initialized = 1;
1183 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001184
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001185 if (kctl)
1186 volume_control_quirks(cval, kctl);
1187
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001188 /* USB descriptions contain the dB scale in 1/256 dB unit
1189 * while ALSA TLV contains in 1/100 dB unit
1190 */
1191 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1192 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1193 if (cval->dBmin > cval->dBmax) {
1194 /* something is wrong; assume it's either from/to 0dB */
1195 if (cval->dBmin < 0)
1196 cval->dBmax = 0;
1197 else if (cval->dBmin > 0)
1198 cval->dBmin = 0;
1199 if (cval->dBmin > cval->dBmax) {
1200 /* totally crap, return an error */
1201 return -EINVAL;
1202 }
1203 }
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001208#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001211static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1212 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001214 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (cval->val_type == USB_MIXER_BOOLEAN ||
1217 cval->val_type == USB_MIXER_INV_BOOLEAN)
1218 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1219 else
1220 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1221 uinfo->count = cval->channels;
1222 if (cval->val_type == USB_MIXER_BOOLEAN ||
1223 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1224 uinfo->value.integer.min = 0;
1225 uinfo->value.integer.max = 1;
1226 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001227 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001228 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001229 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1230 kcontrol->vd[0].access &=
1231 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1232 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001233 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001234 SNDRV_CTL_EVENT_MASK_INFO,
1235 &kcontrol->id);
1236 }
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001239 uinfo->value.integer.max =
1240 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 return 0;
1243}
1244
1245/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001246static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1247 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001249 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int c, cnt, val, err;
1251
Takashi Iwai641b4872009-01-15 17:05:24 +01001252 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (cval->cmask) {
1254 cnt = 0;
1255 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001256 if (!(cval->cmask & (1 << c)))
1257 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001258 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001259 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001260 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001261 val = get_relative_value(cval, val);
1262 ucontrol->value.integer.value[cnt] = val;
1263 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } else {
1267 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001268 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001269 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001270 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 val = get_relative_value(cval, val);
1272 ucontrol->value.integer.value[0] = val;
1273 }
1274 return 0;
1275}
1276
1277/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001278static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1279 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001281 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int c, cnt, val, oval, err;
1283 int changed = 0;
1284
1285 if (cval->cmask) {
1286 cnt = 0;
1287 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001288 if (!(cval->cmask & (1 << c)))
1289 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001290 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001291 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001292 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001293 val = ucontrol->value.integer.value[cnt];
1294 val = get_abs_value(cval, val);
1295 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001296 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001297 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001299 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 } else {
1302 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001303 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001305 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 val = ucontrol->value.integer.value[0];
1307 val = get_abs_value(cval, val);
1308 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001309 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 changed = 1;
1311 }
1312 }
1313 return changed;
1314}
1315
Andrew Chant568fa7e2018-03-23 19:25:24 -07001316/* get the boolean value from the master channel of a UAC control */
1317static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1318 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001319{
1320 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1321 int val, err;
1322
1323 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1324 if (err < 0)
1325 return filter_error(cval, err);
1326 val = (val != 0);
1327 ucontrol->value.integer.value[0] = val;
1328 return 0;
1329}
1330
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001331/* get the connectors status and report it as boolean type */
1332static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1333 struct snd_ctl_elem_value *ucontrol)
1334{
1335 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1336 struct snd_usb_audio *chip = cval->head.mixer->chip;
1337 int idx = 0, validx, ret, val;
1338
1339 validx = cval->control << 8 | 0;
1340
1341 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1342 if (ret)
1343 goto error;
1344
1345 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1346 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1347 struct uac2_connectors_ctl_blk uac2_conn;
1348
1349 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1350 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1351 validx, idx, &uac2_conn, sizeof(uac2_conn));
1352 val = !!uac2_conn.bNrChannels;
1353 } else { /* UAC_VERSION_3 */
1354 struct uac3_insertion_ctl_blk uac3_conn;
1355
1356 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1357 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1358 validx, idx, &uac3_conn, sizeof(uac3_conn));
1359 val = !!uac3_conn.bmConInserted;
1360 }
1361
1362 snd_usb_unlock_shutdown(chip);
1363
1364 if (ret < 0) {
1365error:
1366 usb_audio_err(chip,
1367 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1368 UAC_GET_CUR, validx, idx, cval->val_type);
1369 return ret;
1370 }
1371
1372 ucontrol->value.integer.value[0] = val;
1373 return 0;
1374}
1375
Takashi Iwai86e07d32005-11-17 15:08:02 +01001376static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1378 .name = "", /* will be filled later manually */
1379 .info = mixer_ctl_feature_info,
1380 .get = mixer_ctl_feature_get,
1381 .put = mixer_ctl_feature_put,
1382};
1383
Daniel Mack23caaf12010-03-11 21:13:25 +01001384/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301385static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1387 .name = "", /* will be filled later manually */
1388 .info = mixer_ctl_feature_info,
1389 .get = mixer_ctl_feature_get,
1390 .put = NULL,
1391};
1392
Andrew Chant568fa7e2018-03-23 19:25:24 -07001393/*
1394 * A control which shows the boolean value from reading a UAC control on
1395 * the master channel.
1396 */
1397static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001398 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1399 .name = "", /* will be filled later manually */
1400 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1401 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001402 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001403 .put = NULL,
1404};
1405
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001406static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1407 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1408 .name = "", /* will be filled later manually */
1409 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1410 .info = snd_ctl_boolean_mono_info,
1411 .get = mixer_ctl_connector_get,
1412 .put = NULL,
1413};
1414
Daniel Mack6bc170e2014-05-24 10:58:16 +02001415/*
1416 * This symbol is exported in order to allow the mixer quirks to
1417 * hook up to the standard feature unit control mechanism
1418 */
Daniel Mack9e386582011-05-25 09:09:01 +02001419struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/*
1422 * build a feature control
1423 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001424static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1425{
1426 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1427}
1428
Daniel Mack6bc170e2014-05-24 10:58:16 +02001429/*
1430 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1431 * rename it to "Headphone". We determine if something is a headphone
1432 * similar to how udev determines form factor.
1433 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001434static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1435 struct snd_card *card)
1436{
1437 const char *names_to_check[] = {
1438 "Headset", "headset", "Headphone", "headphone", NULL};
1439 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001440 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001441
1442 if (strcmp("Speaker", kctl->id.name))
1443 return;
1444
1445 for (s = names_to_check; *s; s++)
1446 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001447 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001448 break;
1449 }
1450
1451 if (!found)
1452 return;
1453
1454 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1455}
1456
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001457static struct usb_feature_control_info *get_feature_control_info(int control)
1458{
1459 int i;
1460
1461 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1462 if (audio_feature_info[i].control == control)
1463 return &audio_feature_info[i];
1464 }
1465 return NULL;
1466}
1467
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001468static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1469 const struct usbmix_name_map *imap,
1470 unsigned int ctl_mask, int control,
1471 struct usb_audio_term *iterm,
1472 struct usb_audio_term *oterm,
1473 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Julian Scheelbc18e312015-08-14 16:14:45 +02001475 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 unsigned int len = 0;
1477 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001478 struct snd_kcontrol *kctl;
1479 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001480 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001481 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Daniel Mack65f25da2010-05-31 13:35:41 +02001483 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* FIXME: not supported yet */
1485 return;
1486 }
1487
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001488 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001489 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return;
1491
Takashi Iwai561b2202005-09-09 14:22:34 +02001492 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001493 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001495 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 cval->control = control;
1497 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001498
1499 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001500 if (!ctl_info) {
1501 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001502 return;
Colin Ian King62376022018-03-27 15:30:01 +01001503 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001504 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001505 cval->val_type = ctl_info->type;
1506 else /* UAC_VERSION_2 */
1507 cval->val_type = ctl_info->type_uac2 >= 0 ?
1508 ctl_info->type_uac2 : ctl_info->type;
1509
Daniel Macka6a33252010-05-31 13:35:37 +02001510 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001512 cval->master_readonly = readonly_mask;
1513 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 int i, c = 0;
1515 for (i = 0; i < 16; i++)
1516 if (ctl_mask & (1 << i))
1517 c++;
1518 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001519 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Daniel Mack6bc170e2014-05-24 10:58:16 +02001522 /*
1523 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001524 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001525 * issue write commands to read-only channels.
1526 */
Daniel Macka6a33252010-05-31 13:35:37 +02001527 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001528 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1529 else
1530 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1531
Daniel Mack6bc170e2014-05-24 10:58:16 +02001532 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001533 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 kfree(cval);
1535 return;
1536 }
Chris J Argeseef90452014-11-12 12:07:01 -06001537 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001539 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001541 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001542 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001543 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001546 case UAC_FU_MUTE:
1547 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001548 /*
1549 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * - if a name id is given in descriptor, use it.
1551 * - if the connected input can be determined, then use the name
1552 * of terminal type.
1553 * - if the connected output can be determined, use it.
1554 * - otherwise, anonymous name.
1555 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001556 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001557 if (iterm)
1558 len = get_term_name(mixer->chip, iterm,
1559 kctl->id.name,
1560 sizeof(kctl->id.name), 1);
1561 if (!len && oterm)
1562 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001563 kctl->id.name,
1564 sizeof(kctl->id.name), 1);
1565 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001566 snprintf(kctl->id.name, sizeof(kctl->id.name),
1567 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001569
1570 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001571 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001572
Daniel Mack6bc170e2014-05-24 10:58:16 +02001573 /*
1574 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 * if the connected output is USB stream, then it's likely a
1576 * capture stream. otherwise it should be playback (hopefully :)
1577 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001578 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1579 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001580 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001581 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001582 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001584 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001585 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001588 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1590 sizeof(kctl->id.name));
1591 break;
1592 }
1593
Takashi Iwaie1825342012-05-14 17:11:06 +02001594 /* get min/max values */
1595 get_min_max_with_quirks(cval, 0, kctl);
1596
1597 if (control == UAC_FU_VOLUME) {
1598 check_mapped_dB(map, cval);
1599 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1600 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1601 kctl->vd[0].access |=
1602 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1603 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1604 }
1605 }
1606
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001607 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001608
Alexey Fisher80aceff2011-03-10 14:53:38 +01001609 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001610 /*
1611 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001612 * to be sure. 384 is a resolution magic number found on Logitech
1613 * devices. It will definitively catch all buggy Logitech devices.
1614 */
1615 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001616 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001617 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001618 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001619 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001620 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001621 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001622 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001623 }
1624
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001625 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
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);
Takashi Iwai3360b842014-11-18 11:47:04 +01001628 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001631static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1632 unsigned int ctl_mask, int control,
1633 struct usb_audio_term *iterm, int unitid,
1634 int readonly_mask)
1635{
1636 struct uac_feature_unit_descriptor *desc = raw_desc;
1637 int nameid = uac_feature_unit_iFeature(desc);
1638
1639 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1640 iterm, &state->oterm, unitid, nameid, readonly_mask);
1641}
1642
1643static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1644 unsigned int ctl_mask, int control, int unitid,
1645 const struct usbmix_name_map *badd_map)
1646{
1647 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1648 NULL, NULL, unitid, 0, 0);
1649}
1650
Andrew Chant5a222e82018-03-23 19:25:23 -07001651static void get_connector_control_name(struct mixer_build *state,
1652 struct usb_audio_term *term,
1653 bool is_input, char *name, int name_size)
1654{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001655 int name_len = get_term_name(state->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001656
1657 if (name_len == 0)
1658 strlcpy(name, "Unknown", name_size);
1659
1660 /*
1661 * sound/core/ctljack.c has a convention of naming jack controls
1662 * by ending in " Jack". Make it slightly more useful by
1663 * indicating Input or Output after the terminal name.
1664 */
1665 if (is_input)
1666 strlcat(name, " - Input Jack", name_size);
1667 else
1668 strlcat(name, " - Output Jack", name_size);
1669}
1670
1671/* Build a mixer control for a UAC connector control (jack-detect) */
1672static void build_connector_control(struct mixer_build *state,
1673 struct usb_audio_term *term, bool is_input)
1674{
1675 struct snd_kcontrol *kctl;
1676 struct usb_mixer_elem_info *cval;
1677
1678 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1679 if (!cval)
1680 return;
1681 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001682 /*
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001683 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1684 * number of channels connected.
1685 *
1686 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1687 * following byte(s) specifies which connectors are inserted.
1688 *
1689 * This boolean ctl will simply report if any channels are connected
1690 * or not.
Andrew Chant568fa7e2018-03-23 19:25:24 -07001691 */
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001692 if (state->mixer->protocol == UAC_VERSION_2)
1693 cval->control = UAC2_TE_CONNECTOR;
1694 else /* UAC_VERSION_3 */
1695 cval->control = UAC3_TE_INSERTION;
1696
Andrew Chant5a222e82018-03-23 19:25:23 -07001697 cval->val_type = USB_MIXER_BOOLEAN;
1698 cval->channels = 1; /* report true if any channel is connected */
1699 cval->min = 0;
1700 cval->max = 1;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001701 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001702 if (!kctl) {
1703 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1704 kfree(cval);
1705 return;
1706 }
1707 get_connector_control_name(state, term, is_input, kctl->id.name,
1708 sizeof(kctl->id.name));
1709 kctl->private_free = snd_usb_mixer_elem_free;
1710 snd_usb_mixer_add_control(&cval->head, kctl);
1711}
1712
Daniel Mackcddaafb2016-04-09 11:21:46 +02001713static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1714 void *_ftr)
1715{
1716 struct uac_clock_source_descriptor *hdr = _ftr;
1717 struct usb_mixer_elem_info *cval;
1718 struct snd_kcontrol *kctl;
1719 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1720 int ret;
1721
1722 if (state->mixer->protocol != UAC_VERSION_2)
1723 return -EINVAL;
1724
1725 if (hdr->bLength != sizeof(*hdr)) {
1726 usb_audio_dbg(state->chip,
1727 "Bogus clock source descriptor length of %d, ignoring.\n",
1728 hdr->bLength);
1729 return 0;
1730 }
1731
1732 /*
1733 * The only property of this unit we are interested in is the
1734 * clock source validity. If that isn't readable, just bail out.
1735 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001736 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001737 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001738 return 0;
1739
1740 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1741 if (!cval)
1742 return -ENOMEM;
1743
1744 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1745
1746 cval->min = 0;
1747 cval->max = 1;
1748 cval->channels = 1;
1749 cval->val_type = USB_MIXER_BOOLEAN;
1750 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1751
Andrew Chant568fa7e2018-03-23 19:25:24 -07001752 cval->master_readonly = 1;
1753 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1754 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001755
1756 if (!kctl) {
1757 kfree(cval);
1758 return -ENOMEM;
1759 }
1760
1761 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001762 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001763 name, sizeof(name));
1764 if (ret > 0)
1765 snprintf(kctl->id.name, sizeof(kctl->id.name),
1766 "%s Validity", name);
1767 else
1768 snprintf(kctl->id.name, sizeof(kctl->id.name),
1769 "Clock Source %d Validity", hdr->bClockID);
1770
1771 return snd_usb_mixer_add_control(&cval->head, kctl);
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/*
1775 * parse a feature unit
1776 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001777 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001779static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1780 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001783 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 unsigned int master_bits, first_ch_bits;
1785 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001786 struct uac_feature_unit_descriptor *hdr = _ftr;
1787 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Daniel Mack23caaf12010-03-11 21:13:25 +01001789 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001790 if (hdr->bLength < 7) {
1791 usb_audio_err(state->chip,
1792 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1793 unitid);
1794 return -EINVAL;
1795 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001796 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001797 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001798 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001799 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001800 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001801 return -EINVAL;
1802 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001803 channels = (hdr->bLength - 7) / csize - 1;
1804 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001805 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001806 usb_audio_err(state->chip,
1807 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1808 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001809 return -EINVAL;
1810 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001811 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001812 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001813 if (hdr->bLength < 6) {
1814 usb_audio_err(state->chip,
1815 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1816 unitid);
1817 return -EINVAL;
1818 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001819 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001820 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001821 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001822 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001823 usb_audio_err(state->chip,
1824 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1825 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001826 return -EINVAL;
1827 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001828 } else { /* UAC_VERSION_3 */
1829 struct uac3_feature_unit_descriptor *ftr = _ftr;
1830
1831 if (hdr->bLength < 7) {
1832 usb_audio_err(state->chip,
1833 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1834 unitid);
1835 return -EINVAL;
1836 }
1837 csize = 4;
1838 channels = (ftr->bLength - 7) / 4 - 1;
1839 bmaControls = ftr->bmaControls;
1840 if (hdr->bLength < 7 + csize) {
1841 usb_audio_err(state->chip,
1842 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1843 unitid);
1844 return -EINVAL;
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847
1848 /* parse the source unit */
Daniel Mack23caaf12010-03-11 21:13:25 +01001849 if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return err;
1851
1852 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001853 err = check_input_term(state, hdr->bSourceID, &iterm);
1854 if (err < 0)
1855 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Daniel Mack23caaf12010-03-11 21:13:25 +01001857 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001858 /* master configuration quirks */
1859 switch (state->chip->usb_id) {
1860 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001861 usb_audio_info(state->chip,
1862 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001863 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001864 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001865 break;
David Henningssonc1051432012-09-20 10:20:41 +02001866 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001867 usb_audio_info(state->chip,
1868 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001869 /* disable non-functional volume control */
1870 channels = 0;
1871 break;
1872
Javier Kohen0c3cee52009-11-17 15:36:13 +01001873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001875 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
1877 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001878
1879 if (state->mixer->protocol == UAC_VERSION_1) {
1880 /* check all control types */
1881 for (i = 0; i < 10; i++) {
1882 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001883 int control = audio_feature_info[i].control;
1884
Daniel Mack23caaf12010-03-11 21:13:25 +01001885 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001886 unsigned int mask;
1887
1888 mask = snd_usb_combine_bytes(bmaControls +
1889 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001890 if (mask & (1 << i))
1891 ch_bits |= (1 << j);
1892 }
1893 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001894
1895 /*
1896 * The first channel must be set
1897 * (for ease of programming).
1898 */
1899 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001900 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001901 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001902 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001903 build_feature_ctl(state, _ftr, 0, control,
1904 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001906 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001907 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001908 unsigned int ch_bits = 0;
1909 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001910 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001911
1912 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001913 unsigned int mask;
1914
1915 mask = snd_usb_combine_bytes(bmaControls +
1916 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001917 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001918 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001919 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001920 ch_read_only |= (1 << j);
1921 }
1922 }
1923
Daniel Mack6bc170e2014-05-24 10:58:16 +02001924 /*
1925 * NOTE: build_feature_ctl() will mark the control
1926 * read-only if all channels are marked read-only in
1927 * the descriptors. Otherwise, the control will be
1928 * reported as writeable, but the driver will not
1929 * actually issue a write command for read-only
1930 * channels.
1931 */
1932
1933 /*
1934 * The first channel must be set
1935 * (for ease of programming).
1936 */
1937 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001938 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001939 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001940 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001941 build_feature_ctl(state, _ftr, 0, control,
1942 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001943 !uac_v2v3_control_is_writeable(master_bits,
1944 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
1948 return 0;
1949}
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/*
1952 * Mixer Unit
1953 */
1954
1955/*
1956 * build a mixer unit control
1957 *
1958 * the callbacks are identical with feature unit.
1959 * input channel number (zero based) is given in control field instead.
1960 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001961static void build_mixer_unit_ctl(struct mixer_build *state,
1962 struct uac_mixer_unit_descriptor *desc,
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01001963 int in_pin, int in_ch, int num_outs,
1964 int unitid, struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001966 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001968 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001969 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001971 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001972 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return;
1974
Takashi Iwai561b2202005-09-09 14:22:34 +02001975 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001976 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return;
1978
Takashi Iwai3360b842014-11-18 11:47:04 +01001979 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 cval->control = in_ch + 1; /* based on 1 */
1981 cval->val_type = USB_MIXER_S16;
1982 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001983 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1984
1985 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 cval->cmask |= (1 << i);
1987 cval->channels++;
1988 }
1989 }
1990
1991 /* get min/max values */
1992 get_min_max(cval, 0);
1993
1994 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001995 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001996 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 kfree(cval);
1998 return;
1999 }
Chris J Argeseef90452014-11-12 12:07:01 -06002000 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002002 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002003 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002004 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002005 sizeof(kctl->id.name), 0);
2006 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02002008 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002010 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002011 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2012 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Andrew Chant5a222e82018-03-23 19:25:23 -07002015static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2016 void *raw_desc)
2017{
2018 struct usb_audio_term iterm;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002019 unsigned int control, bmctls, term_id;
Andrew Chant5a222e82018-03-23 19:25:23 -07002020
Andrew Chant5a222e82018-03-23 19:25:23 -07002021 if (state->mixer->protocol == UAC_VERSION_2) {
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002022 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2023 control = UAC2_TE_CONNECTOR;
2024 term_id = d_v2->bTerminalID;
2025 bmctls = le16_to_cpu(d_v2->bmControls);
2026 } else if (state->mixer->protocol == UAC_VERSION_3) {
2027 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2028 control = UAC3_TE_INSERTION;
2029 term_id = d_v3->bTerminalID;
2030 bmctls = le32_to_cpu(d_v3->bmControls);
2031 } else {
2032 return 0; /* UAC1. No Insertion control */
Andrew Chant5a222e82018-03-23 19:25:23 -07002033 }
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002034
2035 check_input_term(state, term_id, &iterm);
2036
2037 /* Check for jack detection. */
2038 if (uac_v2v3_control_is_readable(bmctls, control))
2039 build_connector_control(state, &iterm, true);
2040
Andrew Chant5a222e82018-03-23 19:25:23 -07002041 return 0;
2042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044/*
2045 * parse a mixer unit
2046 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002047static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2048 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Daniel Mack99fc8642010-03-11 21:13:24 +01002050 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002051 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int input_pins, num_ins, num_outs;
2053 int pin, ich, err;
2054
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002055 err = uac_mixer_unit_get_channels(state, desc);
2056 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002057 usb_audio_err(state->chip,
2058 "invalid MIXER UNIT descriptor %d\n",
2059 unitid);
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002063 num_outs = err;
2064 input_pins = desc->bNrInPins;
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 num_ins = 0;
2067 ich = 0;
2068 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002069 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002071 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002072 /* no bmControls field (e.g. Maya44) -> ignore */
2073 if (desc->bLength <= 10 + input_pins)
2074 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002075 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (err < 0)
2077 return err;
2078 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002079 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 int och, ich_has_controls = 0;
2081
Daniel Mack6bc170e2014-05-24 10:58:16 +02002082 for (och = 0; och < num_outs; och++) {
2083 __u8 *c = uac_mixer_unit_bmControls(desc,
2084 state->mixer->protocol);
2085
2086 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 ich_has_controls = 1;
2088 break;
2089 }
2090 }
2091 if (ich_has_controls)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002092 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 unitid, &iterm);
2094 }
2095 }
2096 return 0;
2097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099/*
2100 * Processing Unit / Extension Unit
2101 */
2102
2103/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002104static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2105 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002107 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 int err, val;
2109
2110 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002111 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002113 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 val = get_relative_value(cval, val);
2116 ucontrol->value.integer.value[0] = val;
2117 return 0;
2118}
2119
2120/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002121static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2122 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002124 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int val, oval, err;
2126
2127 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002128 if (err < 0)
2129 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 val = ucontrol->value.integer.value[0];
2131 val = get_abs_value(cval, val);
2132 if (val != oval) {
2133 set_cur_ctl_value(cval, cval->control << 8, val);
2134 return 1;
2135 }
2136 return 0;
2137}
2138
2139/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302140static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2142 .name = "", /* will be filled later */
2143 .info = mixer_ctl_feature_info,
2144 .get = mixer_ctl_procunit_get,
2145 .put = mixer_ctl_procunit_put,
2146};
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/*
2149 * predefined data for processing units
2150 */
2151struct procunit_value_info {
2152 int control;
2153 char *suffix;
2154 int val_type;
2155 int min_value;
2156};
2157
2158struct procunit_info {
2159 int type;
2160 char *name;
2161 struct procunit_value_info *values;
2162};
2163
2164static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002165 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2166 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 { 0 }
2168};
2169static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002170 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2171 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 { 0 }
2173};
2174static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002175 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2176 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 { 0 }
2178};
2179static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002180 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2181 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2182 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2183 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 { 0 }
2185};
2186static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002187 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2188 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2189 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2190 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 { 0 }
2192};
2193static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002194 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2195 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2196 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2197 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2198 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2199 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 { 0 }
2201};
2202
2203static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002204 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2205 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2206 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2207 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2208 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2209 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 { 0 },
2211};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002212/*
2213 * predefined data for extension units
2214 */
2215static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002216 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2217 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002218};
2219static struct procunit_value_info clock_source_xu_info[] = {
2220 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2221 { 0 }
2222};
2223static struct procunit_value_info spdif_format_xu_info[] = {
2224 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2225 { 0 }
2226};
2227static struct procunit_value_info soft_limit_xu_info[] = {
2228 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2229 { 0 }
2230};
2231static struct procunit_info extunits[] = {
2232 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2233 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2234 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2235 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2236 { 0 }
2237};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239/*
2240 * build a processing/extension unit
2241 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002242static int build_audio_procunit(struct mixer_build *state, int unitid,
2243 void *raw_desc, struct procunit_info *list,
2244 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Daniel Mack99fc8642010-03-11 21:13:24 +01002246 struct uac_processing_unit_descriptor *desc = raw_desc;
2247 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002248 struct usb_mixer_elem_info *cval;
2249 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int i, err, nameid, type, len;
2251 struct procunit_info *info;
2252 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002253 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 static struct procunit_value_info default_value_info[] = {
2255 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2256 { 0 }
2257 };
2258 static struct procunit_info default_info = {
2259 0, NULL, default_value_info
2260 };
2261
Daniel Mack23caaf12010-03-11 21:13:25 +01002262 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2263 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002264 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return -EINVAL;
2266 }
2267
2268 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002269 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return err;
2271 }
2272
Daniel Mack99fc8642010-03-11 21:13:24 +01002273 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 for (info = list; info && info->type; info++)
2275 if (info->type == type)
2276 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002277 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 info = &default_info;
2279
2280 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002281 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002282
Daniel Mack6bc170e2014-05-24 10:58:16 +02002283 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 continue;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002285 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002286 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002288 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002289 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002291 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 cval->control = valinfo->control;
2293 cval->val_type = valinfo->val_type;
2294 cval->channels = 1;
2295
2296 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002297 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002298 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* FIXME: hard-coded */
2300 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002301 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 cval->res = 1;
2303 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002304 } else {
2305 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002306 /*
2307 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002308 * samplerate control quirk
2309 */
2310 cval->min = 0;
2311 cval->max = 5;
2312 cval->res = 1;
2313 cval->initialized = 1;
2314 } else
2315 get_min_max(cval, valinfo->min_value);
2316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002319 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 kfree(cval);
2321 return -ENOMEM;
2322 }
Chris J Argeseef90452014-11-12 12:07:01 -06002323 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Daniel Mack6bc170e2014-05-24 10:58:16 +02002325 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002326 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002327 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002329 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002330 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 len = 0;
2332 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002333 len = snd_usb_copy_string_desc(state->chip,
2334 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002335 kctl->id.name,
2336 sizeof(kctl->id.name));
2337 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2339 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002340 append_ctl_name(kctl, " ");
2341 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002343 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002344 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002345 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002346 cval->min, cval->max);
2347
Takashi Iwai3360b842014-11-18 11:47:04 +01002348 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002349 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return err;
2351 }
2352 return 0;
2353}
2354
Daniel Mack6bc170e2014-05-24 10:58:16 +02002355static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2356 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002358 return build_audio_procunit(state, unitid, raw_desc,
2359 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Daniel Mack6bc170e2014-05-24 10:58:16 +02002362static int parse_audio_extension_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 /*
2366 * Note that we parse extension units with processing unit descriptors.
2367 * That's ok as the layout is the same.
2368 */
2369 return build_audio_procunit(state, unitid, raw_desc,
2370 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373/*
2374 * Selector Unit
2375 */
2376
Daniel Mack6bc170e2014-05-24 10:58:16 +02002377/*
2378 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 * use an enumerator type for routing
2380 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002381static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002384 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002385 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Takashi Iwai5e246b82008-08-08 17:12:47 +02002387 if (snd_BUG_ON(!itemlist))
2388 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002389 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
2392/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002393static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002396 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 int val, err;
2398
Daniel Mack09414202010-05-31 13:35:44 +02002399 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002401 ucontrol->value.enumerated.item[0] = 0;
2402 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404 val = get_relative_value(cval, val);
2405 ucontrol->value.enumerated.item[0] = val;
2406 return 0;
2407}
2408
2409/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002410static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002413 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int val, oval, err;
2415
Daniel Mack09414202010-05-31 13:35:44 +02002416 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002417 if (err < 0)
2418 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 val = ucontrol->value.enumerated.item[0];
2420 val = get_abs_value(cval, val);
2421 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002422 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return 1;
2424 }
2425 return 0;
2426}
2427
2428/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302429static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2431 .name = "", /* will be filled later */
2432 .info = mixer_ctl_selector_info,
2433 .get = mixer_ctl_selector_get,
2434 .put = mixer_ctl_selector_put,
2435};
2436
Daniel Mack6bc170e2014-05-24 10:58:16 +02002437/*
2438 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 * free both private_data and private_value
2440 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002441static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 int i, num_ins = 0;
2444
2445 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002446 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 num_ins = cval->max;
2448 kfree(cval);
2449 kctl->private_data = NULL;
2450 }
2451 if (kctl->private_value) {
2452 char **itemlist = (char **)kctl->private_value;
2453 for (i = 0; i < num_ins; i++)
2454 kfree(itemlist[i]);
2455 kfree(itemlist);
2456 kctl->private_value = 0;
2457 }
2458}
2459
2460/*
2461 * parse a selector unit
2462 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002463static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2464 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Daniel Mack99fc8642010-03-11 21:13:24 +01002466 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 unsigned int i, nameid, len;
2468 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002469 struct usb_mixer_elem_info *cval;
2470 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002471 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 char **namelist;
2473
Takashi Iwaif658f172017-11-21 17:00:32 +01002474 if (desc->bLength < 5 || !desc->bNrInPins ||
2475 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002476 usb_audio_err(state->chip,
2477 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return -EINVAL;
2479 }
2480
Daniel Mack99fc8642010-03-11 21:13:24 +01002481 for (i = 0; i < desc->bNrInPins; i++) {
2482 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return err;
2484 }
2485
Daniel Mack99fc8642010-03-11 21:13:24 +01002486 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return 0;
2488
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002489 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002490 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return 0;
2492
Takashi Iwai561b2202005-09-09 14:22:34 +02002493 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002494 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002496 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 cval->val_type = USB_MIXER_U8;
2498 cval->channels = 1;
2499 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002500 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 cval->res = 1;
2502 cval->initialized = 1;
2503
Johan Rasténa358a0e2014-11-17 08:39:33 +01002504 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002505 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002506 else /* UAC_VERSION_2 */
2507 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2508 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002509
Daniel Mack99fc8642010-03-11 21:13:24 +01002510 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002511 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 kfree(cval);
2513 return -ENOMEM;
2514 }
2515#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002516 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002517 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 len = 0;
2519 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002520 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002521 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 kfree(namelist[i]);
2523 kfree(namelist);
2524 kfree(cval);
2525 return -ENOMEM;
2526 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002527 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2528 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002529 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002530 len = get_term_name(state->chip, &iterm, namelist[i],
2531 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002533 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
2536 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2537 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002538 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002539 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 kfree(cval);
2541 return -ENOMEM;
2542 }
2543 kctl->private_value = (unsigned long)namelist;
2544 kctl->private_free = usb_mixer_selector_elem_free;
2545
Takashi Iwai5a15f282017-12-18 23:36:57 +01002546 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002547 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002548 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002549 /* no mapping ? */
2550 /* if iSelector is given, use it */
2551 nameid = uac_selector_unit_iSelector(desc);
2552 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002553 len = snd_usb_copy_string_desc(state->chip, nameid,
Takashi Iwai5a15f282017-12-18 23:36:57 +01002554 kctl->id.name,
2555 sizeof(kctl->id.name));
2556 /* ... or pick up the terminal name at next */
2557 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002558 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002560 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002561 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2563
Takashi Iwai5a15f282017-12-18 23:36:57 +01002564 /* and add the proper suffix */
Daniel Mack09414202010-05-31 13:35:44 +02002565 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2566 append_ctl_name(kctl, " Clock Source");
2567 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002568 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002570 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
2572
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002573 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002574 cval->head.id, kctl->id.name, desc->bNrInPins);
2575 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578/*
2579 * parse an audio unit recursively
2580 */
2581
Takashi Iwai86e07d32005-11-17 15:08:02 +01002582static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
2584 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002585 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 if (test_and_set_bit(unitid, state->unitbitmap))
2588 return 0; /* the unit already visited */
2589
2590 p1 = find_audio_control_unit(state, unitid);
2591 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002592 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return -EINVAL;
2594 }
2595
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002596 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2597 switch (p1[2]) {
2598 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002599 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002600 case UAC_MIXER_UNIT:
2601 return parse_audio_mixer_unit(state, unitid, p1);
2602 case UAC2_CLOCK_SOURCE:
2603 return parse_clock_source_unit(state, unitid, p1);
2604 case UAC_SELECTOR_UNIT:
2605 case UAC2_CLOCK_SELECTOR:
2606 return parse_audio_selector_unit(state, unitid, p1);
2607 case UAC_FEATURE_UNIT:
2608 return parse_audio_feature_unit(state, unitid, p1);
2609 case UAC1_PROCESSING_UNIT:
2610 /* UAC2_EFFECT_UNIT has the same value */
2611 if (protocol == UAC_VERSION_1)
2612 return parse_audio_processing_unit(state, unitid, p1);
2613 else
2614 return 0; /* FIXME - effect units not implemented yet */
2615 case UAC1_EXTENSION_UNIT:
2616 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2617 if (protocol == UAC_VERSION_1)
2618 return parse_audio_extension_unit(state, unitid, p1);
2619 else /* UAC_VERSION_2 */
2620 return parse_audio_processing_unit(state, unitid, p1);
2621 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002622 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002623 default:
2624 usb_audio_err(state->chip,
2625 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2626 return -EINVAL;
2627 }
2628 } else { /* UAC_VERSION_3 */
2629 switch (p1[2]) {
2630 case UAC_INPUT_TERMINAL:
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002631 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002632 case UAC3_MIXER_UNIT:
2633 return parse_audio_mixer_unit(state, unitid, p1);
2634 case UAC3_CLOCK_SOURCE:
2635 return parse_clock_source_unit(state, unitid, p1);
2636 case UAC3_CLOCK_SELECTOR:
2637 return parse_audio_selector_unit(state, unitid, p1);
2638 case UAC3_FEATURE_UNIT:
2639 return parse_audio_feature_unit(state, unitid, p1);
2640 case UAC3_EFFECT_UNIT:
2641 return 0; /* FIXME - effect units not implemented yet */
2642 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002643 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002644 case UAC3_EXTENSION_UNIT:
2645 return parse_audio_extension_unit(state, unitid, p1);
2646 default:
2647 usb_audio_err(state->chip,
2648 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2649 return -EINVAL;
2650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652}
2653
Clemens Ladisch84957a82005-04-29 16:23:13 +02002654static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2655{
Takashi Iwai124751d2017-10-10 14:10:32 +02002656 /* kill pending URBs */
2657 snd_usb_mixer_disconnect(mixer);
2658
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002659 kfree(mixer->id_elems);
2660 if (mixer->urb) {
2661 kfree(mixer->urb->transfer_buffer);
2662 usb_free_urb(mixer->urb);
2663 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002664 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002665 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002666 kfree(mixer);
2667}
2668
Takashi Iwai86e07d32005-11-17 15:08:02 +01002669static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002670{
2671 struct usb_mixer_interface *mixer = device->device_data;
2672 snd_usb_mixer_free(mixer);
2673 return 0;
2674}
2675
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002676/* UAC3 predefined channels configuration */
2677struct uac3_badd_profile {
2678 int subclass;
2679 const char *name;
2680 int c_chmask; /* capture channels mask */
2681 int p_chmask; /* playback channels mask */
2682 int st_chmask; /* side tone mixing channel mask */
2683};
2684
2685static struct uac3_badd_profile uac3_badd_profiles[] = {
2686 {
2687 /*
2688 * BAIF, BAOF or combination of both
2689 * IN: Mono or Stereo cfg, Mono alt possible
2690 * OUT: Mono or Stereo cfg, Mono alt possible
2691 */
2692 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2693 .name = "GENERIC IO",
2694 .c_chmask = -1, /* dynamic channels */
2695 .p_chmask = -1, /* dynamic channels */
2696 },
2697 {
2698 /* BAOF; Stereo only cfg, Mono alt possible */
2699 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2700 .name = "HEADPHONE",
2701 .p_chmask = 3,
2702 },
2703 {
2704 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2705 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2706 .name = "SPEAKER",
2707 .p_chmask = -1, /* dynamic channels */
2708 },
2709 {
2710 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2711 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2712 .name = "MICROPHONE",
2713 .c_chmask = -1, /* dynamic channels */
2714 },
2715 {
2716 /*
2717 * BAIOF topology
2718 * IN: Mono only
2719 * OUT: Mono or Stereo cfg, Mono alt possible
2720 */
2721 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2722 .name = "HEADSET",
2723 .c_chmask = 1,
2724 .p_chmask = -1, /* dynamic channels */
2725 .st_chmask = 1,
2726 },
2727 {
2728 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2729 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2730 .name = "HEADSET ADAPTER",
2731 .c_chmask = 1,
2732 .p_chmask = 3,
2733 .st_chmask = 1,
2734 },
2735 {
2736 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2737 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2738 .name = "SPEAKERPHONE",
2739 .c_chmask = 1,
2740 .p_chmask = 1,
2741 },
2742 { 0 } /* terminator */
2743};
2744
2745static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2746 struct uac3_badd_profile *f,
2747 int c_chmask, int p_chmask)
2748{
2749 /*
2750 * If both playback/capture channels are dynamic, make sure
2751 * at least one channel is present
2752 */
2753 if (f->c_chmask < 0 && f->p_chmask < 0) {
2754 if (!c_chmask && !p_chmask) {
2755 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2756 f->name);
2757 return false;
2758 }
2759 return true;
2760 }
2761
2762 if ((f->c_chmask < 0 && !c_chmask) ||
2763 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2764 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2765 f->name);
2766 return false;
2767 }
2768 if ((f->p_chmask < 0 && !p_chmask) ||
2769 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2770 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2771 f->name);
2772 return false;
2773 }
2774 return true;
2775}
2776
2777/*
2778 * create mixer controls for UAC3 BADD profiles
2779 *
2780 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2781 *
2782 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2783 */
2784static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2785 int ctrlif)
2786{
2787 struct usb_device *dev = mixer->chip->dev;
2788 struct usb_interface_assoc_descriptor *assoc;
2789 int badd_profile = mixer->chip->badd_profile;
2790 struct uac3_badd_profile *f;
2791 const struct usbmix_ctl_map *map;
2792 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2793 int i;
2794
2795 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2796
2797 /* Detect BADD capture/playback channels from AS EP descriptors */
2798 for (i = 0; i < assoc->bInterfaceCount; i++) {
2799 int intf = assoc->bFirstInterface + i;
2800
2801 struct usb_interface *iface;
2802 struct usb_host_interface *alts;
2803 struct usb_interface_descriptor *altsd;
2804 unsigned int maxpacksize;
2805 char dir_in;
2806 int chmask, num;
2807
2808 if (intf == ctrlif)
2809 continue;
2810
2811 iface = usb_ifnum_to_if(dev, intf);
2812 num = iface->num_altsetting;
2813
2814 if (num < 2)
2815 return -EINVAL;
2816
2817 /*
2818 * The number of Channels in an AudioStreaming interface
2819 * and the audio sample bit resolution (16 bits or 24
2820 * bits) can be derived from the wMaxPacketSize field in
2821 * the Standard AS Audio Data Endpoint descriptor in
2822 * Alternate Setting 1
2823 */
2824 alts = &iface->altsetting[1];
2825 altsd = get_iface_desc(alts);
2826
2827 if (altsd->bNumEndpoints < 1)
2828 return -EINVAL;
2829
2830 /* check direction */
2831 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2832 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2833
2834 switch (maxpacksize) {
2835 default:
2836 usb_audio_err(mixer->chip,
2837 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2838 maxpacksize);
2839 return -EINVAL;
2840 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2841 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2842 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2843 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2844 chmask = 1;
2845 break;
2846 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2847 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2848 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2849 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2850 chmask = 3;
2851 break;
2852 }
2853
2854 if (dir_in)
2855 c_chmask = chmask;
2856 else
2857 p_chmask = chmask;
2858 }
2859
2860 usb_audio_dbg(mixer->chip,
2861 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
2862 badd_profile, c_chmask, p_chmask);
2863
2864 /* check the mapping table */
2865 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
2866 if (map->id == badd_profile)
2867 break;
2868 }
2869
2870 if (!map->id)
2871 return -EINVAL;
2872
2873 for (f = uac3_badd_profiles; f->name; f++) {
2874 if (badd_profile == f->subclass)
2875 break;
2876 }
2877 if (!f->name)
2878 return -EINVAL;
2879 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
2880 return -EINVAL;
2881 st_chmask = f->st_chmask;
2882
2883 /* Playback */
2884 if (p_chmask) {
2885 /* Master channel, always writable */
2886 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2887 UAC3_BADD_FU_ID2, map->map);
2888 /* Mono/Stereo volume channels, always writable */
2889 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
2890 UAC3_BADD_FU_ID2, map->map);
2891 }
2892
2893 /* Capture */
2894 if (c_chmask) {
2895 /* Master channel, always writable */
2896 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2897 UAC3_BADD_FU_ID5, map->map);
2898 /* Mono/Stereo volume channels, always writable */
2899 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
2900 UAC3_BADD_FU_ID5, map->map);
2901 }
2902
2903 /* Side tone-mixing */
2904 if (st_chmask) {
2905 /* Master channel, always writable */
2906 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2907 UAC3_BADD_FU_ID7, map->map);
2908 /* Mono volume channel, always writable */
2909 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
2910 UAC3_BADD_FU_ID7, map->map);
2911 }
2912
2913 return 0;
2914}
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916/*
2917 * create mixer controls
2918 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002919 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002921static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002923 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 int err;
2925 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01002926 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002929 state.chip = mixer->chip;
2930 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002931 state.buffer = mixer->hostif->extra;
2932 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002935 for (map = usbmix_ctl_maps; map->id; map++) {
2936 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002938 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002939 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 break;
2941 }
2942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Daniel Mack23caaf12010-03-11 21:13:25 +01002944 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002945 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2946 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02002947 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002948 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02002949 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01002950
2951 if (desc->bLength < sizeof(*desc))
2952 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002953 /* mark terminal ID as visited */
2954 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002955 state.oterm.id = desc->bTerminalID;
2956 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2957 state.oterm.name = desc->iTerminal;
2958 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002959 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002960 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002961 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002962 struct uac2_output_terminal_descriptor *desc = p;
2963
2964 if (desc->bLength < sizeof(*desc))
2965 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002966 /* mark terminal ID as visited */
2967 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002968 state.oterm.id = desc->bTerminalID;
2969 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2970 state.oterm.name = desc->iTerminal;
2971 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002972 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002973 return err;
Daniel Mack09414202010-05-31 13:35:44 +02002974
Daniel Mack6bc170e2014-05-24 10:58:16 +02002975 /*
2976 * For UAC2, use the same approach to also add the
2977 * clock selectors
2978 */
Daniel Mack09414202010-05-31 13:35:44 +02002979 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002980 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02002981 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07002982
Takashi Iwai2b54f7852018-04-23 15:19:25 +02002983 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07002984 UAC2_TE_CONNECTOR)) {
2985 build_connector_control(&state, &state.oterm,
2986 false);
2987 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002988 } else { /* UAC_VERSION_3 */
2989 struct uac3_output_terminal_descriptor *desc = p;
2990
2991 if (desc->bLength < sizeof(*desc))
2992 continue; /* invalid descriptor? */
2993 /* mark terminal ID as visited */
2994 set_bit(desc->bTerminalID, state.unitbitmap);
2995 state.oterm.id = desc->bTerminalID;
2996 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2997 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
2998 err = parse_audio_unit(&state, desc->bSourceID);
2999 if (err < 0 && err != -EINVAL)
3000 return err;
3001
3002 /*
3003 * For UAC3, use the same approach to also add the
3004 * clock selectors
3005 */
3006 err = parse_audio_unit(&state, desc->bCSourceID);
3007 if (err < 0 && err != -EINVAL)
3008 return err;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003009
3010 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3011 UAC3_TE_INSERTION)) {
3012 build_connector_control(&state, &state.oterm,
3013 false);
3014 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return 0;
3019}
Clemens Ladisch84957a82005-04-29 16:23:13 +02003020
Daniel Mack7b1eda22010-03-11 21:13:22 +01003021void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003022{
Takashi Iwai3360b842014-11-18 11:47:04 +01003023 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003024
Takashi Iwai8c558072018-05-03 12:33:32 +02003025 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01003026 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02003027 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01003028 /* invalidate cache, so the value is read from the device */
3029 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003030 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003031 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01003032 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003033}
3034
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003035static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01003036 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003037{
Takashi Iwai8c558072018-05-03 12:33:32 +02003038 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003039 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3040 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003041 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01003042 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003043 cval->control, cval->cmask, cval->channels,
3044 val_types[cval->val_type]);
3045 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3046 cval->min, cval->max, cval->dBmin, cval->dBmax);
3047}
3048
3049static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3050 struct snd_info_buffer *buffer)
3051{
3052 struct snd_usb_audio *chip = entry->private_data;
3053 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01003054 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003055 int unitid;
3056
3057 list_for_each_entry(mixer, &chip->mixer_list, list) {
3058 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003059 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02003060 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003061 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003062 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3063 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003064 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003065 snd_iprintf(buffer, " Unit: %i\n", list->id);
3066 if (list->kctl)
3067 snd_iprintf(buffer,
3068 " Control: name=\"%s\", index=%i\n",
3069 list->kctl->id.name,
3070 list->kctl->id.index);
3071 if (list->dump)
3072 list->dump(buffer, list);
3073 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003074 }
3075 }
3076}
3077
Daniel Macke213e9c2010-05-11 18:13:50 +02003078static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3079 int attribute, int value, int index)
3080{
Takashi Iwai3360b842014-11-18 11:47:04 +01003081 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02003082 __u8 unitid = (index >> 8) & 0xff;
3083 __u8 control = (value >> 8) & 0xff;
3084 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02003085 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02003086
3087 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003088 usb_audio_dbg(mixer->chip,
3089 "%s(): bogus channel number %d\n",
3090 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02003091 return;
3092 }
3093
Takashi Iwai8c558072018-05-03 12:33:32 +02003094 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02003095 count++;
3096
3097 if (count == 0)
3098 return;
3099
Takashi Iwai8c558072018-05-03 12:33:32 +02003100 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003101 struct usb_mixer_elem_info *info;
3102
3103 if (!list->kctl)
3104 continue;
3105
Takashi Iwai8c558072018-05-03 12:33:32 +02003106 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02003107 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02003108 continue;
3109
3110 switch (attribute) {
3111 case UAC2_CS_CUR:
3112 /* invalidate cache, so the value is read from the device */
3113 if (channel)
3114 info->cached &= ~(1 << channel);
3115 else /* master channel */
3116 info->cached = 0;
3117
3118 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003119 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02003120 break;
3121
3122 case UAC2_CS_RANGE:
3123 /* TODO */
3124 break;
3125
3126 case UAC2_CS_MEM:
3127 /* TODO */
3128 break;
3129
3130 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003131 usb_audio_dbg(mixer->chip,
3132 "unknown attribute %d in interrupt\n",
3133 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02003134 break;
3135 } /* switch */
3136 }
3137}
3138
3139static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003140{
3141 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02003142 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01003143 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003144
Oliver Neukumedf7de32011-03-11 13:19:43 +01003145 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02003146 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003147
Daniel Macke213e9c2010-05-11 18:13:50 +02003148 if (mixer->protocol == UAC_VERSION_1) {
3149 struct uac1_status_word *status;
3150
3151 for (status = urb->transfer_buffer;
3152 len >= sizeof(*status);
3153 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003154 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02003155 status->bStatusType,
3156 status->bOriginator);
3157
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003158 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02003159 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3160 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003161 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003162
3163 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3164 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003165 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003166 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3167 }
3168 } else { /* UAC_VERSION_2 */
3169 struct uac2_interrupt_data_msg *msg;
3170
3171 for (msg = urb->transfer_buffer;
3172 len >= sizeof(*msg);
3173 len -= sizeof(*msg), msg++) {
3174 /* drop vendor specific and endpoint requests */
3175 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3176 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3177 continue;
3178
3179 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3180 le16_to_cpu(msg->wValue),
3181 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003182 }
3183 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003184
3185requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003186 if (ustatus != -ENOENT &&
3187 ustatus != -ECONNRESET &&
3188 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003189 urb->dev = mixer->chip->dev;
3190 usb_submit_urb(urb, GFP_ATOMIC);
3191 }
3192}
3193
3194/* create the handler for the optional status interrupt endpoint */
3195static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3196{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003197 struct usb_endpoint_descriptor *ep;
3198 void *transfer_buffer;
3199 int buffer_length;
3200 unsigned int epnum;
3201
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003202 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003203 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003204 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003205 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003206 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003207 return 0;
3208
Julia Lawall42a6e662008-12-29 11:23:02 +01003209 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003210 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3211 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3212 if (!transfer_buffer)
3213 return -ENOMEM;
3214 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3215 if (!mixer->urb) {
3216 kfree(transfer_buffer);
3217 return -ENOMEM;
3218 }
3219 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3220 usb_rcvintpipe(mixer->chip->dev, epnum),
3221 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003222 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003223 usb_submit_urb(mixer->urb, GFP_KERNEL);
3224 return 0;
3225}
3226
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003227static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
3229{
3230 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3231
3232 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3233 return 0;
3234}
3235
3236static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3237 struct snd_ctl_elem_value *ucontrol)
3238{
3239 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3240 bool keep_iface = !!ucontrol->value.integer.value[0];
3241
3242 if (mixer->chip->keep_iface == keep_iface)
3243 return 0;
3244 mixer->chip->keep_iface = keep_iface;
3245 return 1;
3246}
3247
3248static const struct snd_kcontrol_new keep_iface_ctl = {
3249 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3250 .name = "Keep Interface",
3251 .info = snd_ctl_boolean_mono_info,
3252 .get = keep_iface_ctl_get,
3253 .put = keep_iface_ctl_put,
3254};
3255
3256static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3257{
3258 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3259
3260 /* need only one control per card */
3261 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3262 snd_ctl_free_one(kctl);
3263 return 0;
3264 }
3265
3266 return snd_ctl_add(mixer->chip->card, kctl);
3267}
3268
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003269int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3270 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003271{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003272 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003273 .dev_free = snd_usb_mixer_dev_free
3274 };
3275 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003276 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01003277 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003278
3279 strcpy(chip->card->mixername, "USB Mixer");
3280
Takashi Iwai561b2202005-09-09 14:22:34 +02003281 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003282 if (!mixer)
3283 return -ENOMEM;
3284 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003285 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003286 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3287 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003288 if (!mixer->id_elems) {
3289 kfree(mixer);
3290 return -ENOMEM;
3291 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003292
Daniel Mack1faa5d02011-08-04 15:56:28 +02003293 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3294 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003295 case UAC_VERSION_1:
3296 default:
3297 mixer->protocol = UAC_VERSION_1;
3298 break;
3299 case UAC_VERSION_2:
3300 mixer->protocol = UAC_VERSION_2;
3301 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003302 case UAC_VERSION_3:
3303 mixer->protocol = UAC_VERSION_3;
3304 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003305 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003306
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003307 if (mixer->protocol == UAC_VERSION_3 &&
3308 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
3309 if ((err = snd_usb_mixer_controls_badd(mixer, ctrlif)) < 0)
3310 goto _error;
3311 } else if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
3312 (err = snd_usb_mixer_status_create(mixer)) < 0) {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003313 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003314 }
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003315 err = create_keep_iface_ctl(mixer);
3316 if (err < 0)
3317 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003318
Daniel Mack7b1eda22010-03-11 21:13:22 +01003319 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003320
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003321 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003322 if (err < 0)
3323 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003324
3325 if (list_empty(&chip->mixer_list) &&
3326 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3327 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3328
Clemens Ladisch84957a82005-04-29 16:23:13 +02003329 list_add(&mixer->list, &chip->mixer_list);
3330 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003331
3332_error:
3333 snd_usb_mixer_free(mixer);
3334 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003335}
3336
Takashi Iwaia6cece92014-10-31 11:24:32 +01003337void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003338{
Takashi Iwai124751d2017-10-10 14:10:32 +02003339 if (mixer->disconnected)
3340 return;
3341 if (mixer->urb)
3342 usb_kill_urb(mixer->urb);
3343 if (mixer->rc_urb)
3344 usb_kill_urb(mixer->rc_urb);
3345 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003346}
Takashi Iwai400362f2014-01-20 16:51:16 +01003347
3348#ifdef CONFIG_PM
3349/* stop any bus activity of a mixer */
3350static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3351{
3352 usb_kill_urb(mixer->urb);
3353 usb_kill_urb(mixer->rc_urb);
3354}
3355
3356static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3357{
3358 int err;
3359
3360 if (mixer->urb) {
3361 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3362 if (err < 0)
3363 return err;
3364 }
3365
3366 return 0;
3367}
3368
3369int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3370{
3371 snd_usb_mixer_inactivate(mixer);
3372 return 0;
3373}
3374
Takashi Iwai3360b842014-11-18 11:47:04 +01003375static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003376{
Takashi Iwai8c558072018-05-03 12:33:32 +02003377 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003378 int c, err, idx;
3379
3380 if (cval->cmask) {
3381 idx = 0;
3382 for (c = 0; c < MAX_CHANNELS; c++) {
3383 if (!(cval->cmask & (1 << c)))
3384 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003385 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003386 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003387 cval->cache_val[idx]);
3388 if (err < 0)
3389 return err;
3390 }
3391 idx++;
3392 }
3393 } else {
3394 /* master */
3395 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003396 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003397 if (err < 0)
3398 return err;
3399 }
3400 }
3401
3402 return 0;
3403}
3404
3405int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3406{
Takashi Iwai3360b842014-11-18 11:47:04 +01003407 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003408 int id, err;
3409
Takashi Iwai400362f2014-01-20 16:51:16 +01003410 if (reset_resume) {
3411 /* restore cached mixer values */
3412 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003413 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003414 if (list->resume) {
3415 err = list->resume(list);
3416 if (err < 0)
3417 return err;
3418 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003419 }
3420 }
3421 }
3422
Takashi Iwai964af632018-04-27 14:23:37 +02003423 snd_usb_mixer_resume_quirk(mixer);
3424
Takashi Iwai400362f2014-01-20 16:51:16 +01003425 return snd_usb_mixer_activate(mixer);
3426}
3427#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003428
3429void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3430 struct usb_mixer_interface *mixer,
3431 int unitid)
3432{
3433 list->mixer = mixer;
3434 list->id = unitid;
3435 list->dump = snd_usb_mixer_dump_cval;
3436#ifdef CONFIG_PM
3437 list->resume = restore_mixer_value;
3438#endif
3439}