blob: c63c84b54969b9b2e51a6321f98695534615b1b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Daniel Mack157a57b2010-06-16 17:57:30 +020029/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
Daniel Mackcddaafb2016-04-09 11:21:46 +020048#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010052#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010053#include <linux/usb/audio-v2.h>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020054#include <linux/usb/audio-v3.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <sound/core.h>
57#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020058#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020059#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020060#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010063#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010064#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010065#include "mixer_quirks.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010066#include "power.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020067
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010068#define MAX_ID_ELEMS 256
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
Takashi Iwai86e07d32005-11-17 15:08:02 +010080struct mixer_build {
81 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020082 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned char *buffer;
84 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010085 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010086 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020088 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050091/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080092enum {
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
99};
100enum {
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
111 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100112#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100114static const struct usbmix_name_map *
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300115find_map(const struct usbmix_name_map *p, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100117 if (!p)
118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300120 for (; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
123 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100125 return NULL;
126}
127
128/* get the mapped name if the unit matches */
129static int
130check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
131{
132 if (!p || !p->name)
133 return 0;
134
135 buflen--;
136 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Takashi Iwai5aeee342014-11-18 11:02:14 +0100139/* ignore the error value if ignore_ctl_error flag is set */
140#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100144static inline int
145check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100149 return 1;
150}
151
152/* dB mapping */
153static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
155{
156 if (p && p->dB) {
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200159 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200163/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100164static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200165 int index, char *buf, int buflen)
166{
167 const struct usbmix_selector_map *p;
168
Daniel Mack6bc170e2014-05-24 10:58:16 +0200169 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200170 return 0;
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
174 }
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * find an audio control unit with the given unit id
180 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200181static void *find_audio_control_unit(struct mixer_build *state,
182 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Daniel Mack67e1daa2010-05-31 13:35:43 +0200187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
Daniel Mack67e1daa2010-05-31 13:35:43 +0200192 hdr->bUnitID == unit)
193 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * copy a string with the given id
201 */
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300202static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200203 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
Jaejoong Kim251552a2017-12-04 15:31:48 +0900206
207 if (len < 0)
208 return 0;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 buf[len] = 0;
211 return len;
212}
213
214/*
215 * convert from the byte/word on usb descriptor to the zero-based integer
216 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
221 return !!val;
222 case USB_MIXER_INV_BOOLEAN:
223 return !val;
224 case USB_MIXER_U8:
225 val &= 0xff;
226 break;
227 case USB_MIXER_S8:
228 val &= 0xff;
229 if (val >= 0x80)
230 val -= 0x100;
231 break;
232 case USB_MIXER_U16:
233 val &= 0xffff;
234 break;
235 case USB_MIXER_S16:
236 val &= 0xffff;
237 if (val >= 0x8000)
238 val -= 0x10000;
239 break;
240 }
241 return val;
242}
243
244/*
245 * convert from the zero-based int to the byte/word for usb descriptor
246 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100247static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 switch (cval->val_type) {
250 case USB_MIXER_BOOLEAN:
251 return !!val;
252 case USB_MIXER_INV_BOOLEAN:
253 return !val;
254 case USB_MIXER_S8:
255 case USB_MIXER_U8:
256 return val & 0xff;
257 case USB_MIXER_S16:
258 case USB_MIXER_U16:
259 return val & 0xffff;
260 }
261 return 0; /* not reached */
262}
263
Takashi Iwai86e07d32005-11-17 15:08:02 +0100264static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200266 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cval->res = 1;
268 if (val < cval->min)
269 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 else
273 return (val - cval->min) / cval->res;
274}
275
Takashi Iwai86e07d32005-11-17 15:08:02 +0100276static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (val < 0)
279 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200280 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cval->res = 1;
282 val *= cval->res;
283 val += cval->min;
284 if (val > cval->max)
285 return cval->max;
286 return val;
287}
288
Julian Scheelbc18e312015-08-14 16:14:45 +0200289static int uac2_ctl_value_size(int val_type)
290{
291 switch (val_type) {
292 case USB_MIXER_S32:
293 case USB_MIXER_U32:
294 return 4;
295 case USB_MIXER_S16:
296 case USB_MIXER_U16:
297 return 2;
298 default:
299 return 1;
300 }
301 return 0; /* unreachable */
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/*
306 * retrieve a mixer value
307 */
308
Daniel Mack6bc170e2014-05-24 10:58:16 +0200309static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai3360b842014-11-18 11:47:04 +0100312 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
315 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200316 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Takashi Iwai47ab1542015-08-25 16:09:00 +0200318 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100319 if (err < 0)
320 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200329 err = 0;
330 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700331 } else if (err == -ETIMEDOUT) {
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100335 usb_audio_dbg(chip,
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200338 err = -EINVAL;
339
340 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200341 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Daniel Mack6bc170e2014-05-24 10:58:16 +0200345static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100347{
Takashi Iwai3360b842014-11-18 11:47:04 +0100348 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100351 unsigned char *val;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100352 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100353 __u8 bRequest;
354
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100355 val_size = uac2_ctl_value_size(cval->val_type);
356
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100359 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200360 } else {
361 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100362 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200363 }
364
365 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100366
Takashi Iwai47ab1542015-08-25 16:09:00 +0200367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100368 if (ret)
369 goto error;
370
Takashi Iwai47ab1542015-08-25 16:09:00 +0200371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200374 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200375 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100376
377 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100378error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100379 usb_audio_err(chip,
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100382 return ret;
383 }
384
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200385 /* FIXME: how should we handle multiple triplets here? */
386
Daniel Mack23caaf12010-03-11 21:13:25 +0100387 switch (request) {
388 case UAC_GET_CUR:
389 val = buf;
390 break;
391 case UAC_GET_MIN:
392 val = buf + sizeof(__u16);
393 break;
394 case UAC_GET_MAX:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100395 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100396 break;
397 case UAC_GET_RES:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100398 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100399 break;
400 default:
401 return -EINVAL;
402 }
403
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100406
407 return 0;
408}
409
Daniel Mack6bc170e2014-05-24 10:58:16 +0200410static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100412{
Eldad Zack9f814102012-11-28 23:55:35 +0100413 validx += cval->idx_off;
414
Takashi Iwai3360b842014-11-18 11:47:04 +0100415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
418}
419
Daniel Mack6bc170e2014-05-24 10:58:16 +0200420static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100427static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
432 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Chris J Argeseef90452014-11-12 12:07:01 -0600435int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100436 int channel, int index, int *value)
437{
438 int err;
439
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
442 return 0;
443 }
444 err = get_cur_mix_raw(cval, channel, value);
445 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100448 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200449 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100450 return err;
451 }
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * set a mixer value
459 */
460
Daniel Mack7b1eda22010-03-11 21:13:22 +0100461int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai3360b842014-11-18 11:47:04 +0100464 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200465 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200466 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100467
Eldad Zack9f814102012-11-28 23:55:35 +0100468 validx += cval->idx_off;
469
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200470
Takashi Iwai3360b842014-11-18 11:47:04 +0100471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200473 } else { /* UAC_VERSION_2/3 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200490
491 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 if (err < 0)
493 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200494
Takashi Iwai978520b2012-10-12 15:12:55 +0200495 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
501 if (err >= 0) {
Takashi Iwai978520b2012-10-12 15:12:55 +0200502 err = 0;
503 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700504 } else if (err == -ETIMEDOUT) {
505 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100506 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200507 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100508 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200509 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 err = -EINVAL;
511
512 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200513 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Daniel Mack6bc170e2014-05-24 10:58:16 +0200517static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Chris J Argeseef90452014-11-12 12:07:01 -0600523int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100524 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai641b4872009-01-15 17:05:24 +0100526 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
530
531 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100532 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200534 __func__, channel, cval->control);
535 return 0;
536 }
537
Daniel Mack6bc170e2014-05-24 10:58:16 +0200538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
540 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100541 if (err < 0)
542 return err;
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200548/*
549 * TLV callback for mixer volume controls
550 */
Felix Homann285de9c2012-04-23 20:24:24 +0200551int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200552 unsigned int size, unsigned int __user *_tlv)
553{
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200556
557 if (size < sizeof(scale))
558 return -ENOMEM;
Takashi Iwai0f174b32017-08-16 14:18:37 +0200559 if (cval->min_mute)
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200563 if (copy_to_user(_tlv, scale, sizeof(scale)))
564 return -EFAULT;
565 return 0;
566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * parser routines begin here...
570 */
571
Takashi Iwai86e07d32005-11-17 15:08:02 +0100572static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574
575/*
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
578 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200579static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
589 *
590 * if failed, give up and free the control instance.
591 */
592
Takashi Iwai3360b842014-11-18 11:47:04 +0100593int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200594 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Takashi Iwai3360b842014-11-18 11:47:04 +0100596 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200598
Daniel Mackef9d5972011-05-25 09:09:00 +0200599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kctl->id.index++;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200601 err = snd_ctl_add(mixer->chip->card, kctl);
602 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200603 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
604 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100607 list->kctl = kctl;
608 list->next_id_elem = mixer->id_elems[list->id];
609 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
614 * get a terminal name string
615 */
616
617static struct iterm_name_combo {
618 int type;
619 char *name;
620} iterm_names[] = {
621 { 0x0300, "Output" },
622 { 0x0301, "Speaker" },
623 { 0x0302, "Headphone" },
624 { 0x0303, "HMD Audio" },
625 { 0x0304, "Desktop Speaker" },
626 { 0x0305, "Room Speaker" },
627 { 0x0306, "Com Speaker" },
628 { 0x0307, "LFE" },
629 { 0x0600, "External In" },
630 { 0x0601, "Analog In" },
631 { 0x0602, "Digital In" },
632 { 0x0603, "Line" },
633 { 0x0604, "Legacy In" },
634 { 0x0605, "IEC958 In" },
635 { 0x0606, "1394 DA Stream" },
636 { 0x0607, "1394 DV Stream" },
637 { 0x0700, "Embedded" },
638 { 0x0701, "Noise Source" },
639 { 0x0702, "Equalization Noise" },
640 { 0x0703, "CD" },
641 { 0x0704, "DAT" },
642 { 0x0705, "DCC" },
643 { 0x0706, "MiniDisk" },
644 { 0x0707, "Analog Tape" },
645 { 0x0708, "Phonograph" },
646 { 0x0709, "VCR Audio" },
647 { 0x070a, "Video Disk Audio" },
648 { 0x070b, "DVD Audio" },
649 { 0x070c, "TV Tuner Audio" },
650 { 0x070d, "Satellite Rec Audio" },
651 { 0x070e, "Cable Tuner Audio" },
652 { 0x070f, "DSS Audio" },
653 { 0x0710, "Radio Receiver" },
654 { 0x0711, "Radio Transmitter" },
655 { 0x0712, "Multi-Track Recorder" },
656 { 0x0713, "Synthesizer" },
657 { 0 },
658};
659
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300660static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 unsigned char *name, int maxlen, int term_only)
662{
663 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100664 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100666 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300667 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200668 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100669 if (len)
670 return len;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* virtual type - not a real terminal */
674 if (iterm->type >> 16) {
675 if (term_only)
676 return 0;
677 switch (iterm->type >> 16) {
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100678 case UAC3_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200679 strcpy(name, "Selector");
680 return 8;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100681 case UAC3_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200682 strcpy(name, "Process Unit");
683 return 12;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100684 case UAC3_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200685 strcpy(name, "Ext Unit");
686 return 8;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100687 case UAC3_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200688 strcpy(name, "Mixer");
689 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 default:
691 return sprintf(name, "Unit %d", iterm->id);
692 }
693 }
694
695 switch (iterm->type & 0xff00) {
696 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200697 strcpy(name, "PCM");
698 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200700 strcpy(name, "Mic");
701 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200703 strcpy(name, "Headset");
704 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200706 strcpy(name, "Phone");
707 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Daniel Mack6bc170e2014-05-24 10:58:16 +0200710 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (names->type == iterm->type) {
712 strcpy(name, names->name);
713 return strlen(names->name);
714 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100721 * Get logical cluster information for UAC3 devices.
722 */
723static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
724{
725 struct uac3_cluster_header_descriptor c_header;
726 int err;
727
728 err = snd_usb_ctl_msg(state->chip->dev,
729 usb_rcvctrlpipe(state->chip->dev, 0),
730 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
731 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
732 cluster_id,
733 snd_usb_ctrl_intf(state->chip),
734 &c_header, sizeof(c_header));
735 if (err < 0)
736 goto error;
737 if (err != sizeof(c_header)) {
738 err = -EIO;
739 goto error;
740 }
741
742 return c_header.bNrChannels;
743
744error:
745 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
746 return err;
747}
748
749/*
750 * Get number of channels for a Mixer Unit.
751 */
752static int uac_mixer_unit_get_channels(struct mixer_build *state,
753 struct uac_mixer_unit_descriptor *desc)
754{
755 int mu_channels;
756
757 if (desc->bLength < 11)
758 return -EINVAL;
759 if (!desc->bNrInPins)
760 return -EINVAL;
761
762 switch (state->mixer->protocol) {
763 case UAC_VERSION_1:
764 case UAC_VERSION_2:
765 default:
766 mu_channels = uac_mixer_unit_bNrChannels(desc);
767 break;
768 case UAC_VERSION_3:
769 mu_channels = get_cluster_channels_v3(state,
770 uac3_mixer_unit_wClusterDescrID(desc));
771 break;
772 }
773
774 if (!mu_channels)
775 return -EINVAL;
776
777 return mu_channels;
778}
779
780/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * parse the source unit recursively until it reaches to a terminal
782 * or a branched unit.
783 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200784static int check_input_term(struct mixer_build *state, int id,
785 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200787 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200788 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100789 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 memset(term, 0, sizeof(*term));
792 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100793 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200795
796 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
797 switch (hdr[2]) {
798 case UAC_INPUT_TERMINAL:
799 if (protocol == UAC_VERSION_1) {
800 struct uac_input_terminal_descriptor *d = p1;
801
802 term->type = le16_to_cpu(d->wTerminalType);
803 term->channels = d->bNrChannels;
804 term->chconfig = le16_to_cpu(d->wChannelConfig);
805 term->name = d->iTerminal;
806 } else { /* UAC_VERSION_2 */
807 struct uac2_input_terminal_descriptor *d = p1;
808
809 /* call recursively to verify that the
810 * referenced clock entity is valid */
811 err = check_input_term(state, d->bCSourceID, term);
812 if (err < 0)
813 return err;
814
815 /* save input term properties after recursion,
816 * to ensure they are not overriden by the
817 * recursion calls */
818 term->id = id;
819 term->type = le16_to_cpu(d->wTerminalType);
820 term->channels = d->bNrChannels;
821 term->chconfig = le32_to_cpu(d->bmChannelConfig);
822 term->name = d->iTerminal;
823 }
824 return 0;
825 case UAC_FEATURE_UNIT: {
826 /* the header is the same for v1 and v2 */
827 struct uac_feature_unit_descriptor *d = p1;
828
829 id = d->bSourceID;
830 break; /* continue to parse */
831 }
832 case UAC_MIXER_UNIT: {
833 struct uac_mixer_unit_descriptor *d = p1;
834
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100835 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200836 term->channels = uac_mixer_unit_bNrChannels(d);
837 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
838 term->name = uac_mixer_unit_iMixer(d);
839 return 0;
840 }
841 case UAC_SELECTOR_UNIT:
842 case UAC2_CLOCK_SELECTOR: {
843 struct uac_selector_unit_descriptor *d = p1;
844 /* call recursively to retrieve the channel info */
845 err = check_input_term(state, d->baSourceID[0], term);
846 if (err < 0)
847 return err;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100848 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200849 term->id = id;
850 term->name = uac_selector_unit_iSelector(d);
851 return 0;
852 }
853 case UAC1_PROCESSING_UNIT:
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100854 /* UAC2_EFFECT_UNIT */
855 if (protocol == UAC_VERSION_1)
856 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
857 else /* UAC_VERSION_2 */
858 term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */
Gustavo A. R. Silva91c6e152018-08-08 17:23:44 -0500859 /* fall through */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200860 case UAC1_EXTENSION_UNIT:
861 /* UAC2_PROCESSING_UNIT_V2 */
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100862 if (protocol == UAC_VERSION_1 && !term->type)
863 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
864 else if (protocol == UAC_VERSION_2 && !term->type)
865 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
Gustavo A. R. Silva91c6e152018-08-08 17:23:44 -0500866 /* fall through */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200867 case UAC2_EXTENSION_UNIT_V2: {
868 struct uac_processing_unit_descriptor *d = p1;
869
870 if (protocol == UAC_VERSION_2 &&
871 hdr[2] == UAC2_EFFECT_UNIT) {
872 /* UAC2/UAC1 unit IDs overlap here in an
873 * uncompatible way. Ignore this unit for now.
874 */
875 return 0;
876 }
877
878 if (d->bNrInPins) {
879 id = d->baSourceID[0];
880 break; /* continue to parse */
881 }
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100882 if (!term->type)
883 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
884
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200885 term->channels = uac_processing_unit_bNrChannels(d);
886 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
887 term->name = uac_processing_unit_iProcessing(d, protocol);
888 return 0;
889 }
890 case UAC2_CLOCK_SOURCE: {
891 struct uac_clock_source_descriptor *d = p1;
892
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100893 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200894 term->id = id;
895 term->name = d->iClockSource;
896 return 0;
897 }
898 default:
899 return -ENODEV;
900 }
901 } else { /* UAC_VERSION_3 */
902 switch (hdr[2]) {
903 case UAC_INPUT_TERMINAL: {
904 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200905
906 /* call recursively to verify that the
907 * referenced clock entity is valid */
908 err = check_input_term(state, d->bCSourceID, term);
909 if (err < 0)
910 return err;
911
912 /* save input term properties after recursion,
913 * to ensure they are not overriden by the
914 * recursion calls */
915 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100916 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100917
Jorge Sanjuan71066942018-05-14 12:03:42 +0100918 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
919 if (err < 0)
920 return err;
921 term->channels = err;
922
923 /* REVISIT: UAC3 IT doesn't have channels cfg */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200924 term->chconfig = 0;
925
926 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100927 return 0;
928 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200929 case UAC3_FEATURE_UNIT: {
930 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100931
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200932 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break; /* continue to parse */
934 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200935 case UAC3_CLOCK_SOURCE: {
936 struct uac3_clock_source_descriptor *d = p1;
937
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100938 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200939 term->id = id;
940 term->name = le16_to_cpu(d->wClockSourceStr);
941 return 0;
942 }
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100943 case UAC3_MIXER_UNIT: {
944 struct uac_mixer_unit_descriptor *d = p1;
945
946 err = uac_mixer_unit_get_channels(state, d);
947 if (err < 0)
948 return err;
949
950 term->channels = err;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100951 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +0100952
953 return 0;
954 }
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +0100955 case UAC3_SELECTOR_UNIT:
956 case UAC3_CLOCK_SELECTOR: {
957 struct uac_selector_unit_descriptor *d = p1;
958 /* call recursively to retrieve the channel info */
959 err = check_input_term(state, d->baSourceID[0], term);
960 if (err < 0)
961 return err;
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100962 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +0100963 term->id = id;
964 term->name = 0; /* TODO: UAC3 Class-specific strings */
965
966 return 0;
967 }
Jorge Sanjuan0f292f02018-07-11 13:37:53 +0100968 case UAC3_PROCESSING_UNIT: {
969 struct uac_processing_unit_descriptor *d = p1;
970
971 if (!d->bNrInPins)
972 return -EINVAL;
973
974 /* call recursively to retrieve the channel info */
975 err = check_input_term(state, d->baSourceID[0], term);
976 if (err < 0)
977 return err;
978
Jorge Sanjuan8b3a0872018-07-11 13:37:54 +0100979 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
Jorge Sanjuan0f292f02018-07-11 13:37:53 +0100980 term->id = id;
981 term->name = 0; /* TODO: UAC3 Class-specific strings */
982
983 return 0;
984 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200985 default:
986 return -ENODEV;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 }
990 return -ENODEV;
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/*
994 * Feature Unit
995 */
996
997/* feature unit control information */
998struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700999 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +02001001 int type; /* data type for uac1 */
1002 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003};
1004
1005static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001006 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
1007 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
1008 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
1009 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
1010 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
1011 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
1012 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
1013 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
1014 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
1015 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +02001016 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001017 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
1018 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
1019 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020};
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -06001023void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Jesper Juhl4d572772005-05-30 17:30:32 +02001025 kfree(kctl->private_data);
1026 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * interface to ALSA control for feature/mixer units
1031 */
1032
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001033/* volume control quirks */
1034static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1035 struct snd_kcontrol *kctl)
1036{
Takashi Iwai3360b842014-11-18 11:47:04 +01001037 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001038 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +01001039 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001040 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +01001041 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1042 cval->min = 0x0000;
1043 cval->max = 0xffff;
1044 cval->res = 0x00e6;
1045 break;
1046 }
1047 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1048 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1049 cval->min = 0x00;
1050 cval->max = 0xff;
1051 break;
1052 }
1053 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1054 cval->min = 0xb706;
1055 cval->max = 0xff7b;
1056 cval->res = 0x0073;
1057 break;
1058 }
1059 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1060 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1061 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1062 cval->max = 0xfcfe;
1063 cval->res = 0x0073;
1064 }
1065 break;
1066
Felix Homannd34bf142012-04-23 20:24:27 +02001067 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1068 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1069 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001070 usb_audio_info(chip,
1071 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001072 cval->min = 0x0000;
1073 cval->max = 0x7f00;
1074 cval->res = 0x0100;
1075 break;
1076 }
1077 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1078 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001079 usb_audio_info(chip,
1080 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +02001081 cval->min = 0x00;
1082 cval->max = 0x7f;
1083 break;
1084 }
1085 break;
1086
Federico Cuello21493312018-05-09 00:13:38 +02001087 case USB_ID(0x0d8c, 0x0103):
1088 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1089 usb_audio_info(chip,
1090 "set volume quirk for CM102-A+/102S+\n");
1091 cval->min = -256;
1092 }
1093 break;
1094
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001095 case USB_ID(0x0471, 0x0101):
1096 case USB_ID(0x0471, 0x0104):
1097 case USB_ID(0x0471, 0x0105):
1098 case USB_ID(0x0672, 0x1041):
1099 /* quirk for UDA1321/N101.
1100 * note that detection between firmware 2.1.1.7 (N101)
1101 * and later 2.1.1.21 is not very clear from datasheets.
1102 * I hope that the min value is -15360 for newer firmware --jk
1103 */
1104 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1105 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001106 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001107 "set volume quirk for UDA1321/N101 chip\n");
1108 cval->max = -256;
1109 }
1110 break;
1111
1112 case USB_ID(0x046d, 0x09a4):
1113 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001114 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001115 "set volume quirk for QuickCam E3500\n");
1116 cval->min = 6080;
1117 cval->max = 8768;
1118 cval->res = 192;
1119 }
1120 break;
1121
Takashi Iwaie805ca82014-03-05 12:34:39 +01001122 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001123 case USB_ID(0x046d, 0x0808):
1124 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -05001125 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001126 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001127 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001128 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001129 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001130 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001131 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001132 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001133 /* Most audio usb devices lie about volume resolution.
1134 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001135 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001136 */
1137 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001138 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001139 "set resolution quirk: cval->res = 384\n");
1140 cval->res = 384;
1141 }
1142 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001143 }
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/*
1147 * retrieve the minimum and maximum values for the specified control
1148 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001149static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1150 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 /* for failsafe */
1153 cval->min = default_min;
1154 cval->max = cval->min + 1;
1155 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001156 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (cval->val_type == USB_MIXER_BOOLEAN ||
1159 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1160 cval->initialized = 1;
1161 } else {
1162 int minchn = 0;
1163 if (cval->cmask) {
1164 int i;
1165 for (i = 0; i < MAX_CHANNELS; i++)
1166 if (cval->cmask & (1 << i)) {
1167 minchn = i + 1;
1168 break;
1169 }
1170 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001171 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1172 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001173 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001174 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001175 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1176 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return -EINVAL;
1178 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001179 if (get_ctl_value(cval, UAC_GET_RES,
1180 (cval->control << 8) | minchn,
1181 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 cval->res = 1;
1183 } else {
1184 int last_valid_res = cval->res;
1185
1186 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001187 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001188 (cval->control << 8) | minchn,
1189 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191 cval->res /= 2;
1192 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001193 if (get_ctl_value(cval, UAC_GET_RES,
1194 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 cval->res = last_valid_res;
1196 }
1197 if (cval->res == 0)
1198 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001199
1200 /* Additional checks for the proper resolution
1201 *
1202 * Some devices report smaller resolutions than actually
1203 * reacting. They don't return errors but simply clip
1204 * to the lower aligned value.
1205 */
1206 if (cval->min + cval->res < cval->max) {
1207 int last_valid_res = cval->res;
1208 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001209 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001210 for (;;) {
1211 test = saved;
1212 if (test < cval->max)
1213 test += cval->res;
1214 else
1215 test -= cval->res;
1216 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001217 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001218 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001219 cval->res = last_valid_res;
1220 break;
1221 }
1222 if (test == check)
1223 break;
1224 cval->res *= 2;
1225 }
Chris J Argeseef90452014-11-12 12:07:01 -06001226 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001227 }
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 cval->initialized = 1;
1230 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001231
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001232 if (kctl)
1233 volume_control_quirks(cval, kctl);
1234
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001235 /* USB descriptions contain the dB scale in 1/256 dB unit
1236 * while ALSA TLV contains in 1/100 dB unit
1237 */
1238 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1239 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1240 if (cval->dBmin > cval->dBmax) {
1241 /* something is wrong; assume it's either from/to 0dB */
1242 if (cval->dBmin < 0)
1243 cval->dBmax = 0;
1244 else if (cval->dBmin > 0)
1245 cval->dBmin = 0;
1246 if (cval->dBmin > cval->dBmax) {
1247 /* totally crap, return an error */
1248 return -EINVAL;
1249 }
1250 }
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001255#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001258static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1259 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001261 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (cval->val_type == USB_MIXER_BOOLEAN ||
1264 cval->val_type == USB_MIXER_INV_BOOLEAN)
1265 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1266 else
1267 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1268 uinfo->count = cval->channels;
1269 if (cval->val_type == USB_MIXER_BOOLEAN ||
1270 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1271 uinfo->value.integer.min = 0;
1272 uinfo->value.integer.max = 1;
1273 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001274 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001275 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001276 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1277 kcontrol->vd[0].access &=
1278 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1279 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001280 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001281 SNDRV_CTL_EVENT_MASK_INFO,
1282 &kcontrol->id);
1283 }
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001286 uinfo->value.integer.max =
1287 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 return 0;
1290}
1291
1292/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001293static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1294 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001296 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int c, cnt, val, err;
1298
Takashi Iwai641b4872009-01-15 17:05:24 +01001299 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (cval->cmask) {
1301 cnt = 0;
1302 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001303 if (!(cval->cmask & (1 << c)))
1304 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001305 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001306 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001307 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001308 val = get_relative_value(cval, val);
1309 ucontrol->value.integer.value[cnt] = val;
1310 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 } else {
1314 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001315 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001316 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001317 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 val = get_relative_value(cval, val);
1319 ucontrol->value.integer.value[0] = val;
1320 }
1321 return 0;
1322}
1323
1324/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001325static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1326 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001328 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int c, cnt, val, oval, err;
1330 int changed = 0;
1331
1332 if (cval->cmask) {
1333 cnt = 0;
1334 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001335 if (!(cval->cmask & (1 << c)))
1336 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001337 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001338 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001339 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001340 val = ucontrol->value.integer.value[cnt];
1341 val = get_abs_value(cval, val);
1342 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001343 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001344 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001346 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 } else {
1349 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001350 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001352 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 val = ucontrol->value.integer.value[0];
1354 val = get_abs_value(cval, val);
1355 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001356 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 changed = 1;
1358 }
1359 }
1360 return changed;
1361}
1362
Andrew Chant568fa7e2018-03-23 19:25:24 -07001363/* get the boolean value from the master channel of a UAC control */
1364static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1365 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001366{
1367 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1368 int val, err;
1369
1370 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1371 if (err < 0)
1372 return filter_error(cval, err);
1373 val = (val != 0);
1374 ucontrol->value.integer.value[0] = val;
1375 return 0;
1376}
1377
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001378/* get the connectors status and report it as boolean type */
1379static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1380 struct snd_ctl_elem_value *ucontrol)
1381{
1382 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1383 struct snd_usb_audio *chip = cval->head.mixer->chip;
1384 int idx = 0, validx, ret, val;
1385
1386 validx = cval->control << 8 | 0;
1387
1388 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1389 if (ret)
1390 goto error;
1391
1392 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1393 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1394 struct uac2_connectors_ctl_blk uac2_conn;
1395
1396 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1397 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1398 validx, idx, &uac2_conn, sizeof(uac2_conn));
1399 val = !!uac2_conn.bNrChannels;
1400 } else { /* UAC_VERSION_3 */
1401 struct uac3_insertion_ctl_blk uac3_conn;
1402
1403 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1404 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1405 validx, idx, &uac3_conn, sizeof(uac3_conn));
1406 val = !!uac3_conn.bmConInserted;
1407 }
1408
1409 snd_usb_unlock_shutdown(chip);
1410
1411 if (ret < 0) {
1412error:
1413 usb_audio_err(chip,
1414 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1415 UAC_GET_CUR, validx, idx, cval->val_type);
1416 return ret;
1417 }
1418
1419 ucontrol->value.integer.value[0] = val;
1420 return 0;
1421}
1422
Takashi Iwai86e07d32005-11-17 15:08:02 +01001423static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1425 .name = "", /* will be filled later manually */
1426 .info = mixer_ctl_feature_info,
1427 .get = mixer_ctl_feature_get,
1428 .put = mixer_ctl_feature_put,
1429};
1430
Daniel Mack23caaf12010-03-11 21:13:25 +01001431/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301432static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001433 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1434 .name = "", /* will be filled later manually */
1435 .info = mixer_ctl_feature_info,
1436 .get = mixer_ctl_feature_get,
1437 .put = NULL,
1438};
1439
Andrew Chant568fa7e2018-03-23 19:25:24 -07001440/*
1441 * A control which shows the boolean value from reading a UAC control on
1442 * the master channel.
1443 */
1444static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001445 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1446 .name = "", /* will be filled later manually */
1447 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1448 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001449 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001450 .put = NULL,
1451};
1452
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001453static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1454 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1455 .name = "", /* will be filled later manually */
1456 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1457 .info = snd_ctl_boolean_mono_info,
1458 .get = mixer_ctl_connector_get,
1459 .put = NULL,
1460};
1461
Daniel Mack6bc170e2014-05-24 10:58:16 +02001462/*
1463 * This symbol is exported in order to allow the mixer quirks to
1464 * hook up to the standard feature unit control mechanism
1465 */
Daniel Mack9e386582011-05-25 09:09:01 +02001466struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468/*
1469 * build a feature control
1470 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001471static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1472{
1473 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1474}
1475
Daniel Mack6bc170e2014-05-24 10:58:16 +02001476/*
1477 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1478 * rename it to "Headphone". We determine if something is a headphone
1479 * similar to how udev determines form factor.
1480 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001481static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1482 struct snd_card *card)
1483{
1484 const char *names_to_check[] = {
1485 "Headset", "headset", "Headphone", "headphone", NULL};
1486 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001487 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001488
1489 if (strcmp("Speaker", kctl->id.name))
1490 return;
1491
1492 for (s = names_to_check; *s; s++)
1493 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001494 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001495 break;
1496 }
1497
1498 if (!found)
1499 return;
1500
1501 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1502}
1503
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001504static struct usb_feature_control_info *get_feature_control_info(int control)
1505{
1506 int i;
1507
1508 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1509 if (audio_feature_info[i].control == control)
1510 return &audio_feature_info[i];
1511 }
1512 return NULL;
1513}
1514
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001515static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1516 const struct usbmix_name_map *imap,
1517 unsigned int ctl_mask, int control,
1518 struct usb_audio_term *iterm,
1519 struct usb_audio_term *oterm,
1520 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Julian Scheelbc18e312015-08-14 16:14:45 +02001522 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 unsigned int len = 0;
1524 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001525 struct snd_kcontrol *kctl;
1526 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001527 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001528 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Daniel Mack65f25da2010-05-31 13:35:41 +02001530 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* FIXME: not supported yet */
1532 return;
1533 }
1534
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001535 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001536 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return;
1538
Takashi Iwai561b2202005-09-09 14:22:34 +02001539 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001540 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001542 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 cval->control = control;
1544 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001545
1546 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001547 if (!ctl_info) {
1548 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001549 return;
Colin Ian King62376022018-03-27 15:30:01 +01001550 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001551 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001552 cval->val_type = ctl_info->type;
1553 else /* UAC_VERSION_2 */
1554 cval->val_type = ctl_info->type_uac2 >= 0 ?
1555 ctl_info->type_uac2 : ctl_info->type;
1556
Daniel Macka6a33252010-05-31 13:35:37 +02001557 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001559 cval->master_readonly = readonly_mask;
1560 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int i, c = 0;
1562 for (i = 0; i < 16; i++)
1563 if (ctl_mask & (1 << i))
1564 c++;
1565 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001566 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
Daniel Mack6bc170e2014-05-24 10:58:16 +02001569 /*
1570 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001571 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001572 * issue write commands to read-only channels.
1573 */
Daniel Macka6a33252010-05-31 13:35:37 +02001574 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001575 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1576 else
1577 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1578
Daniel Mack6bc170e2014-05-24 10:58:16 +02001579 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001580 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 kfree(cval);
1582 return;
1583 }
Chris J Argeseef90452014-11-12 12:07:01 -06001584 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001586 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001588 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001589 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001590 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001593 case UAC_FU_MUTE:
1594 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001595 /*
1596 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 * - if a name id is given in descriptor, use it.
1598 * - if the connected input can be determined, then use the name
1599 * of terminal type.
1600 * - if the connected output can be determined, use it.
1601 * - otherwise, anonymous name.
1602 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001603 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001604 if (iterm)
1605 len = get_term_name(mixer->chip, iterm,
1606 kctl->id.name,
1607 sizeof(kctl->id.name), 1);
1608 if (!len && oterm)
1609 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001610 kctl->id.name,
1611 sizeof(kctl->id.name), 1);
1612 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001613 snprintf(kctl->id.name, sizeof(kctl->id.name),
1614 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001616
1617 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001618 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001619
Daniel Mack6bc170e2014-05-24 10:58:16 +02001620 /*
1621 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * if the connected output is USB stream, then it's likely a
1623 * capture stream. otherwise it should be playback (hopefully :)
1624 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001625 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1626 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001627 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001628 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001629 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001631 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001632 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001635 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1637 sizeof(kctl->id.name));
1638 break;
1639 }
1640
Takashi Iwaie1825342012-05-14 17:11:06 +02001641 /* get min/max values */
1642 get_min_max_with_quirks(cval, 0, kctl);
1643
1644 if (control == UAC_FU_VOLUME) {
1645 check_mapped_dB(map, cval);
1646 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1647 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1648 kctl->vd[0].access |=
1649 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1650 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1651 }
1652 }
1653
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001654 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001655
Alexey Fisher80aceff2011-03-10 14:53:38 +01001656 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001657 /*
1658 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001659 * to be sure. 384 is a resolution magic number found on Logitech
1660 * devices. It will definitively catch all buggy Logitech devices.
1661 */
1662 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001663 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001664 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001665 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001666 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001667 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001668 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001669 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001670 }
1671
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001672 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001673 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001674 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001675 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001678static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1679 unsigned int ctl_mask, int control,
1680 struct usb_audio_term *iterm, int unitid,
1681 int readonly_mask)
1682{
1683 struct uac_feature_unit_descriptor *desc = raw_desc;
1684 int nameid = uac_feature_unit_iFeature(desc);
1685
1686 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1687 iterm, &state->oterm, unitid, nameid, readonly_mask);
1688}
1689
1690static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1691 unsigned int ctl_mask, int control, int unitid,
1692 const struct usbmix_name_map *badd_map)
1693{
1694 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1695 NULL, NULL, unitid, 0, 0);
1696}
1697
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001698static void get_connector_control_name(struct usb_mixer_interface *mixer,
Andrew Chant5a222e82018-03-23 19:25:23 -07001699 struct usb_audio_term *term,
1700 bool is_input, char *name, int name_size)
1701{
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001702 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001703
1704 if (name_len == 0)
1705 strlcpy(name, "Unknown", name_size);
1706
1707 /*
1708 * sound/core/ctljack.c has a convention of naming jack controls
1709 * by ending in " Jack". Make it slightly more useful by
1710 * indicating Input or Output after the terminal name.
1711 */
1712 if (is_input)
1713 strlcat(name, " - Input Jack", name_size);
1714 else
1715 strlcat(name, " - Output Jack", name_size);
1716}
1717
1718/* Build a mixer control for a UAC connector control (jack-detect) */
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001719static void build_connector_control(struct usb_mixer_interface *mixer,
Andrew Chant5a222e82018-03-23 19:25:23 -07001720 struct usb_audio_term *term, bool is_input)
1721{
1722 struct snd_kcontrol *kctl;
1723 struct usb_mixer_elem_info *cval;
1724
1725 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1726 if (!cval)
1727 return;
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001728 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001729 /*
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001730 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1731 * number of channels connected.
1732 *
1733 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1734 * following byte(s) specifies which connectors are inserted.
1735 *
1736 * This boolean ctl will simply report if any channels are connected
1737 * or not.
Andrew Chant568fa7e2018-03-23 19:25:24 -07001738 */
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001739 if (mixer->protocol == UAC_VERSION_2)
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001740 cval->control = UAC2_TE_CONNECTOR;
1741 else /* UAC_VERSION_3 */
1742 cval->control = UAC3_TE_INSERTION;
1743
Andrew Chant5a222e82018-03-23 19:25:23 -07001744 cval->val_type = USB_MIXER_BOOLEAN;
1745 cval->channels = 1; /* report true if any channel is connected */
1746 cval->min = 0;
1747 cval->max = 1;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01001748 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001749 if (!kctl) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001750 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Andrew Chant5a222e82018-03-23 19:25:23 -07001751 kfree(cval);
1752 return;
1753 }
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01001754 get_connector_control_name(mixer, term, is_input, kctl->id.name,
Andrew Chant5a222e82018-03-23 19:25:23 -07001755 sizeof(kctl->id.name));
1756 kctl->private_free = snd_usb_mixer_elem_free;
1757 snd_usb_mixer_add_control(&cval->head, kctl);
1758}
1759
Daniel Mackcddaafb2016-04-09 11:21:46 +02001760static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1761 void *_ftr)
1762{
1763 struct uac_clock_source_descriptor *hdr = _ftr;
1764 struct usb_mixer_elem_info *cval;
1765 struct snd_kcontrol *kctl;
1766 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1767 int ret;
1768
1769 if (state->mixer->protocol != UAC_VERSION_2)
1770 return -EINVAL;
1771
1772 if (hdr->bLength != sizeof(*hdr)) {
1773 usb_audio_dbg(state->chip,
1774 "Bogus clock source descriptor length of %d, ignoring.\n",
1775 hdr->bLength);
1776 return 0;
1777 }
1778
1779 /*
1780 * The only property of this unit we are interested in is the
1781 * clock source validity. If that isn't readable, just bail out.
1782 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001783 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001784 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001785 return 0;
1786
1787 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1788 if (!cval)
1789 return -ENOMEM;
1790
1791 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1792
1793 cval->min = 0;
1794 cval->max = 1;
1795 cval->channels = 1;
1796 cval->val_type = USB_MIXER_BOOLEAN;
1797 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1798
Andrew Chant568fa7e2018-03-23 19:25:24 -07001799 cval->master_readonly = 1;
1800 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1801 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001802
1803 if (!kctl) {
1804 kfree(cval);
1805 return -ENOMEM;
1806 }
1807
1808 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001809 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001810 name, sizeof(name));
1811 if (ret > 0)
1812 snprintf(kctl->id.name, sizeof(kctl->id.name),
1813 "%s Validity", name);
1814 else
1815 snprintf(kctl->id.name, sizeof(kctl->id.name),
1816 "Clock Source %d Validity", hdr->bClockID);
1817
1818 return snd_usb_mixer_add_control(&cval->head, kctl);
1819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821/*
1822 * parse a feature unit
1823 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001824 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001826static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1827 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001830 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 unsigned int master_bits, first_ch_bits;
1832 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001833 struct uac_feature_unit_descriptor *hdr = _ftr;
1834 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Daniel Mack23caaf12010-03-11 21:13:25 +01001836 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001837 if (hdr->bLength < 7) {
1838 usb_audio_err(state->chip,
1839 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1840 unitid);
1841 return -EINVAL;
1842 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001843 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001844 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001845 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001846 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001847 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001848 return -EINVAL;
1849 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001850 channels = (hdr->bLength - 7) / csize - 1;
1851 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001852 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001853 usb_audio_err(state->chip,
1854 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1855 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001856 return -EINVAL;
1857 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001858 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001859 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001860 if (hdr->bLength < 6) {
1861 usb_audio_err(state->chip,
1862 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1863 unitid);
1864 return -EINVAL;
1865 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001866 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001867 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001868 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001869 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001870 usb_audio_err(state->chip,
1871 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1872 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001873 return -EINVAL;
1874 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001875 } else { /* UAC_VERSION_3 */
1876 struct uac3_feature_unit_descriptor *ftr = _ftr;
1877
1878 if (hdr->bLength < 7) {
1879 usb_audio_err(state->chip,
1880 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1881 unitid);
1882 return -EINVAL;
1883 }
1884 csize = 4;
1885 channels = (ftr->bLength - 7) / 4 - 1;
1886 bmaControls = ftr->bmaControls;
1887 if (hdr->bLength < 7 + csize) {
1888 usb_audio_err(state->chip,
1889 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1890 unitid);
1891 return -EINVAL;
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
1895 /* parse the source unit */
Takashi Iwaif25ecf82018-05-27 15:18:22 +02001896 err = parse_audio_unit(state, hdr->bSourceID);
1897 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return err;
1899
1900 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001901 err = check_input_term(state, hdr->bSourceID, &iterm);
1902 if (err < 0)
1903 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Daniel Mack23caaf12010-03-11 21:13:25 +01001905 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001906 /* master configuration quirks */
1907 switch (state->chip->usb_id) {
1908 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001909 usb_audio_info(state->chip,
1910 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001911 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001912 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001913 break;
David Henningssonc1051432012-09-20 10:20:41 +02001914 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001915 usb_audio_info(state->chip,
1916 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001917 /* disable non-functional volume control */
1918 channels = 0;
1919 break;
1920
Javier Kohen0c3cee52009-11-17 15:36:13 +01001921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001923 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 else
1925 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001926
1927 if (state->mixer->protocol == UAC_VERSION_1) {
1928 /* check all control types */
1929 for (i = 0; i < 10; i++) {
1930 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001931 int control = audio_feature_info[i].control;
1932
Daniel Mack23caaf12010-03-11 21:13:25 +01001933 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001934 unsigned int mask;
1935
1936 mask = snd_usb_combine_bytes(bmaControls +
1937 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001938 if (mask & (1 << i))
1939 ch_bits |= (1 << j);
1940 }
1941 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001942
1943 /*
1944 * The first channel must be set
1945 * (for ease of programming).
1946 */
1947 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001948 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001949 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001950 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001951 build_feature_ctl(state, _ftr, 0, control,
1952 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001954 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001955 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001956 unsigned int ch_bits = 0;
1957 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001958 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001959
1960 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001961 unsigned int mask;
1962
1963 mask = snd_usb_combine_bytes(bmaControls +
1964 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001965 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001966 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001967 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001968 ch_read_only |= (1 << j);
1969 }
1970 }
1971
Daniel Mack6bc170e2014-05-24 10:58:16 +02001972 /*
1973 * NOTE: build_feature_ctl() will mark the control
1974 * read-only if all channels are marked read-only in
1975 * the descriptors. Otherwise, the control will be
1976 * reported as writeable, but the driver will not
1977 * actually issue a write command for read-only
1978 * channels.
1979 */
1980
1981 /*
1982 * The first channel must be set
1983 * (for ease of programming).
1984 */
1985 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001986 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001987 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001988 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001989 build_feature_ctl(state, _ftr, 0, control,
1990 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001991 !uac_v2v3_control_is_writeable(master_bits,
1992 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
1996 return 0;
1997}
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
2000 * Mixer Unit
2001 */
2002
2003/*
2004 * build a mixer unit control
2005 *
2006 * the callbacks are identical with feature unit.
2007 * input channel number (zero based) is given in control field instead.
2008 */
Daniel Mack99fc8642010-03-11 21:13:24 +01002009static void build_mixer_unit_ctl(struct mixer_build *state,
2010 struct uac_mixer_unit_descriptor *desc,
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002011 int in_pin, int in_ch, int num_outs,
2012 int unitid, struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002014 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002016 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002017 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002019 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002020 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return;
2022
Takashi Iwai561b2202005-09-09 14:22:34 +02002023 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002024 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return;
2026
Takashi Iwai3360b842014-11-18 11:47:04 +01002027 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 cval->control = in_ch + 1; /* based on 1 */
2029 cval->val_type = USB_MIXER_S16;
2030 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002031 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2032
2033 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 cval->cmask |= (1 << i);
2035 cval->channels++;
2036 }
2037 }
2038
2039 /* get min/max values */
2040 get_min_max(cval, 0);
2041
2042 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002043 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002044 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 kfree(cval);
2046 return;
2047 }
Chris J Argeseef90452014-11-12 12:07:01 -06002048 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002050 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002051 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002052 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002053 sizeof(kctl->id.name), 0);
2054 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02002056 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002058 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002059 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2060 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Andrew Chant5a222e82018-03-23 19:25:23 -07002063static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2064 void *raw_desc)
2065{
2066 struct usb_audio_term iterm;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002067 unsigned int control, bmctls, term_id;
Andrew Chant5a222e82018-03-23 19:25:23 -07002068
Andrew Chant5a222e82018-03-23 19:25:23 -07002069 if (state->mixer->protocol == UAC_VERSION_2) {
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002070 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2071 control = UAC2_TE_CONNECTOR;
2072 term_id = d_v2->bTerminalID;
2073 bmctls = le16_to_cpu(d_v2->bmControls);
2074 } else if (state->mixer->protocol == UAC_VERSION_3) {
2075 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2076 control = UAC3_TE_INSERTION;
2077 term_id = d_v3->bTerminalID;
2078 bmctls = le32_to_cpu(d_v3->bmControls);
2079 } else {
2080 return 0; /* UAC1. No Insertion control */
Andrew Chant5a222e82018-03-23 19:25:23 -07002081 }
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002082
2083 check_input_term(state, term_id, &iterm);
2084
2085 /* Check for jack detection. */
2086 if (uac_v2v3_control_is_readable(bmctls, control))
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01002087 build_connector_control(state->mixer, &iterm, true);
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002088
Andrew Chant5a222e82018-03-23 19:25:23 -07002089 return 0;
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/*
2093 * parse a mixer unit
2094 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002095static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2096 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Daniel Mack99fc8642010-03-11 21:13:24 +01002098 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002099 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 int input_pins, num_ins, num_outs;
2101 int pin, ich, err;
2102
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002103 err = uac_mixer_unit_get_channels(state, desc);
2104 if (err < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002105 usb_audio_err(state->chip,
2106 "invalid MIXER UNIT descriptor %d\n",
2107 unitid);
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002108 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002111 num_outs = err;
2112 input_pins = desc->bNrInPins;
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 num_ins = 0;
2115 ich = 0;
2116 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002117 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01002119 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02002120 /* no bmControls field (e.g. Maya44) -> ignore */
2121 if (desc->bLength <= 10 + input_pins)
2122 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01002123 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (err < 0)
2125 return err;
2126 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002127 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 int och, ich_has_controls = 0;
2129
Daniel Mack6bc170e2014-05-24 10:58:16 +02002130 for (och = 0; och < num_outs; och++) {
2131 __u8 *c = uac_mixer_unit_bmControls(desc,
2132 state->mixer->protocol);
2133
2134 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 ich_has_controls = 1;
2136 break;
2137 }
2138 }
2139 if (ich_has_controls)
Jorge Sanjuan6cfd8392018-05-11 16:25:34 +01002140 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 unitid, &iterm);
2142 }
2143 }
2144 return 0;
2145}
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/*
2148 * Processing Unit / Extension Unit
2149 */
2150
2151/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002152static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002155 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int err, val;
2157
2158 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002159 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01002161 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 val = get_relative_value(cval, val);
2164 ucontrol->value.integer.value[0] = val;
2165 return 0;
2166}
2167
2168/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002169static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002172 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 int val, oval, err;
2174
2175 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002176 if (err < 0)
2177 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 val = ucontrol->value.integer.value[0];
2179 val = get_abs_value(cval, val);
2180 if (val != oval) {
2181 set_cur_ctl_value(cval, cval->control << 8, val);
2182 return 1;
2183 }
2184 return 0;
2185}
2186
2187/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302188static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2190 .name = "", /* will be filled later */
2191 .info = mixer_ctl_feature_info,
2192 .get = mixer_ctl_procunit_get,
2193 .put = mixer_ctl_procunit_put,
2194};
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196/*
2197 * predefined data for processing units
2198 */
2199struct procunit_value_info {
2200 int control;
2201 char *suffix;
2202 int val_type;
2203 int min_value;
2204};
2205
2206struct procunit_info {
2207 int type;
2208 char *name;
2209 struct procunit_value_info *values;
2210};
2211
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002212static struct procunit_value_info undefined_proc_info[] = {
2213 { 0x00, "Control Undefined", 0 },
2214 { 0 }
2215};
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002218 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2219 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 { 0 }
2221};
2222static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002223 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2224 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 { 0 }
2226};
2227static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002228 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2229 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 { 0 }
2231};
2232static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002233 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2234 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2235 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2236 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 { 0 }
2238};
2239static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002240 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2241 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2242 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2243 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 { 0 }
2245};
2246static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002247 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2248 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2249 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2250 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2251 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2252 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 { 0 }
2254};
2255
2256static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002257 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2258 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2259 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2260 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2261 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2262 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 { 0 },
2264};
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002265
2266static struct procunit_value_info uac3_updown_proc_info[] = {
2267 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2268 { 0 }
2269};
2270static struct procunit_value_info uac3_stereo_ext_proc_info[] = {
2271 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 },
2272 { 0 }
2273};
2274
2275static struct procunit_info uac3_procunits[] = {
2276 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info },
2277 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info },
2278 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info },
2279 { 0 },
2280};
2281
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002282/*
2283 * predefined data for extension units
2284 */
2285static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002286 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2287 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002288};
2289static struct procunit_value_info clock_source_xu_info[] = {
2290 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2291 { 0 }
2292};
2293static struct procunit_value_info spdif_format_xu_info[] = {
2294 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2295 { 0 }
2296};
2297static struct procunit_value_info soft_limit_xu_info[] = {
2298 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2299 { 0 }
2300};
2301static struct procunit_info extunits[] = {
2302 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2303 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2304 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2305 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2306 { 0 }
2307};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309/*
2310 * build a processing/extension unit
2311 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002312static int build_audio_procunit(struct mixer_build *state, int unitid,
2313 void *raw_desc, struct procunit_info *list,
2314 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Daniel Mack99fc8642010-03-11 21:13:24 +01002316 struct uac_processing_unit_descriptor *desc = raw_desc;
2317 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002318 struct usb_mixer_elem_info *cval;
2319 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 int i, err, nameid, type, len;
2321 struct procunit_info *info;
2322 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002323 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 static struct procunit_value_info default_value_info[] = {
2325 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2326 { 0 }
2327 };
2328 static struct procunit_info default_info = {
2329 0, NULL, default_value_info
2330 };
2331
Daniel Mack23caaf12010-03-11 21:13:25 +01002332 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2333 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002334 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return -EINVAL;
2336 }
2337
2338 for (i = 0; i < num_ins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002339 err = parse_audio_unit(state, desc->baSourceID[i]);
2340 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return err;
2342 }
2343
Daniel Mack99fc8642010-03-11 21:13:24 +01002344 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 for (info = list; info && info->type; info++)
2346 if (info->type == type)
2347 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002348 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 info = &default_info;
2350
2351 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002352 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002353
Jorge Sanjuan4e887af2018-07-11 13:37:52 +01002354 if (state->mixer->protocol == UAC_VERSION_1) {
2355 if (!(controls[valinfo->control / 8] &
2356 (1 << ((valinfo->control % 8) - 1))))
2357 continue;
2358 } else { /* UAC_VERSION_2/3 */
2359 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8],
2360 valinfo->control))
2361 continue;
2362 }
2363
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002364 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002365 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002367 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002368 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002370 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 cval->control = valinfo->control;
2372 cval->val_type = valinfo->val_type;
2373 cval->channels = 1;
2374
Jorge Sanjuan4e887af2018-07-11 13:37:52 +01002375 if (state->mixer->protocol > UAC_VERSION_1 &&
2376 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8],
2377 valinfo->control))
2378 cval->master_readonly = 1;
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* get min/max values */
Jorge Sanjuan55b8cb42018-07-11 13:37:55 +01002381 switch (type) {
2382 case UAC_PROCESS_UP_DOWNMIX: {
2383 bool mode_sel = false;
2384
2385 switch (state->mixer->protocol) {
2386 case UAC_VERSION_1:
2387 case UAC_VERSION_2:
2388 default:
2389 if (cval->control == UAC_UD_MODE_SELECT)
2390 mode_sel = true;
2391 break;
2392 case UAC_VERSION_3:
2393 if (cval->control == UAC3_UD_MODE_SELECT)
2394 mode_sel = true;
2395 break;
2396 }
2397
2398 if (mode_sel) {
2399 __u8 *control_spec = uac_processing_unit_specific(desc,
2400 state->mixer->protocol);
2401 cval->min = 1;
2402 cval->max = control_spec[0];
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002403 cval->res = 1;
2404 cval->initialized = 1;
Jorge Sanjuan55b8cb42018-07-11 13:37:55 +01002405 break;
2406 }
2407
2408 get_min_max(cval, valinfo->min_value);
2409 break;
2410 }
2411 case USB_XU_CLOCK_RATE:
2412 /*
2413 * E-Mu USB 0404/0202/TrackerPre/0204
2414 * samplerate control quirk
2415 */
2416 cval->min = 0;
2417 cval->max = 5;
2418 cval->res = 1;
2419 cval->initialized = 1;
2420 break;
2421 default:
2422 get_min_max(cval, valinfo->min_value);
2423 break;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002427 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 kfree(cval);
2429 return -ENOMEM;
2430 }
Chris J Argeseef90452014-11-12 12:07:01 -06002431 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Daniel Mack6bc170e2014-05-24 10:58:16 +02002433 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002434 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002435 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002437 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002438 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 len = 0;
2440 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002441 len = snd_usb_copy_string_desc(state->chip,
2442 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002443 kctl->id.name,
2444 sizeof(kctl->id.name));
2445 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2447 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002448 append_ctl_name(kctl, " ");
2449 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002451 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002452 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002453 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002454 cval->min, cval->max);
2455
Takashi Iwai3360b842014-11-18 11:47:04 +01002456 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002457 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return err;
2459 }
2460 return 0;
2461}
2462
Daniel Mack6bc170e2014-05-24 10:58:16 +02002463static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2464 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Jorge Sanjuan0f292f02018-07-11 13:37:53 +01002466 switch (state->mixer->protocol) {
2467 case UAC_VERSION_1:
2468 case UAC_VERSION_2:
2469 default:
2470 return build_audio_procunit(state, unitid, raw_desc,
2471 procunits, "Processing Unit");
2472 case UAC_VERSION_3:
2473 return build_audio_procunit(state, unitid, raw_desc,
2474 uac3_procunits, "Processing Unit");
2475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Daniel Mack6bc170e2014-05-24 10:58:16 +02002478static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2479 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002481 /*
2482 * Note that we parse extension units with processing unit descriptors.
2483 * That's ok as the layout is the same.
2484 */
2485 return build_audio_procunit(state, unitid, raw_desc,
2486 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489/*
2490 * Selector Unit
2491 */
2492
Daniel Mack6bc170e2014-05-24 10:58:16 +02002493/*
2494 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 * use an enumerator type for routing
2496 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002497static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002500 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002501 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Takashi Iwai5e246b82008-08-08 17:12:47 +02002503 if (snd_BUG_ON(!itemlist))
2504 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002505 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002509static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2510 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002512 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 int val, err;
2514
Daniel Mack09414202010-05-31 13:35:44 +02002515 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002517 ucontrol->value.enumerated.item[0] = 0;
2518 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 val = get_relative_value(cval, val);
2521 ucontrol->value.enumerated.item[0] = val;
2522 return 0;
2523}
2524
2525/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002526static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002529 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int val, oval, err;
2531
Daniel Mack09414202010-05-31 13:35:44 +02002532 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002533 if (err < 0)
2534 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 val = ucontrol->value.enumerated.item[0];
2536 val = get_abs_value(cval, val);
2537 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002538 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 1;
2540 }
2541 return 0;
2542}
2543
2544/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302545static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2547 .name = "", /* will be filled later */
2548 .info = mixer_ctl_selector_info,
2549 .get = mixer_ctl_selector_get,
2550 .put = mixer_ctl_selector_put,
2551};
2552
Daniel Mack6bc170e2014-05-24 10:58:16 +02002553/*
2554 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 * free both private_data and private_value
2556 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002557static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 int i, num_ins = 0;
2560
2561 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002562 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 num_ins = cval->max;
2564 kfree(cval);
2565 kctl->private_data = NULL;
2566 }
2567 if (kctl->private_value) {
2568 char **itemlist = (char **)kctl->private_value;
2569 for (i = 0; i < num_ins; i++)
2570 kfree(itemlist[i]);
2571 kfree(itemlist);
2572 kctl->private_value = 0;
2573 }
2574}
2575
2576/*
2577 * parse a selector unit
2578 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002579static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2580 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Daniel Mack99fc8642010-03-11 21:13:24 +01002582 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 unsigned int i, nameid, len;
2584 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002585 struct usb_mixer_elem_info *cval;
2586 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002587 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 char **namelist;
2589
Takashi Iwaif658f172017-11-21 17:00:32 +01002590 if (desc->bLength < 5 || !desc->bNrInPins ||
2591 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002592 usb_audio_err(state->chip,
2593 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return -EINVAL;
2595 }
2596
Daniel Mack99fc8642010-03-11 21:13:24 +01002597 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002598 err = parse_audio_unit(state, desc->baSourceID[i]);
2599 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return err;
2601 }
2602
Daniel Mack99fc8642010-03-11 21:13:24 +01002603 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return 0;
2605
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002606 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002607 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return 0;
2609
Takashi Iwai561b2202005-09-09 14:22:34 +02002610 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002611 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002613 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 cval->val_type = USB_MIXER_U8;
2615 cval->channels = 1;
2616 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002617 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 cval->res = 1;
2619 cval->initialized = 1;
2620
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002621 switch (state->mixer->protocol) {
2622 case UAC_VERSION_1:
2623 default:
Daniel Mack09414202010-05-31 13:35:44 +02002624 cval->control = 0;
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002625 break;
2626 case UAC_VERSION_2:
2627 case UAC_VERSION_3:
2628 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2629 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2630 cval->control = UAC2_CX_CLOCK_SELECTOR;
2631 else /* UAC2/3_SELECTOR_UNIT */
2632 cval->control = UAC2_SU_SELECTOR;
2633 break;
2634 }
Daniel Mack09414202010-05-31 13:35:44 +02002635
Kees Cook6da2ec52018-06-12 13:55:00 -07002636 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002637 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 kfree(cval);
2639 return -ENOMEM;
2640 }
2641#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002642 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002643 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 len = 0;
2645 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002646 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002647 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 kfree(namelist[i]);
2649 kfree(namelist);
2650 kfree(cval);
2651 return -ENOMEM;
2652 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002653 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2654 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002655 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002656 len = get_term_name(state->chip, &iterm, namelist[i],
2657 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002659 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
2661
2662 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2663 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002664 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002665 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 kfree(cval);
2667 return -ENOMEM;
2668 }
2669 kctl->private_value = (unsigned long)namelist;
2670 kctl->private_free = usb_mixer_selector_elem_free;
2671
Takashi Iwai5a15f282017-12-18 23:36:57 +01002672 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002673 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002674 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002675 /* no mapping ? */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002676 switch (state->mixer->protocol) {
2677 case UAC_VERSION_1:
2678 case UAC_VERSION_2:
2679 default:
Takashi Iwai5a15f282017-12-18 23:36:57 +01002680 /* if iSelector is given, use it */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002681 nameid = uac_selector_unit_iSelector(desc);
2682 if (nameid)
2683 len = snd_usb_copy_string_desc(state->chip,
2684 nameid, kctl->id.name,
2685 sizeof(kctl->id.name));
2686 break;
2687 case UAC_VERSION_3:
2688 /* TODO: Class-Specific strings not yet supported */
2689 break;
2690 }
2691
Takashi Iwai5a15f282017-12-18 23:36:57 +01002692 /* ... or pick up the terminal name at next */
2693 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002694 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002696 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002697 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2699
Takashi Iwai5a15f282017-12-18 23:36:57 +01002700 /* and add the proper suffix */
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002701 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2702 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
Daniel Mack09414202010-05-31 13:35:44 +02002703 append_ctl_name(kctl, " Clock Source");
2704 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002705 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002707 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002710 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002711 cval->head.id, kctl->id.name, desc->bNrInPins);
2712 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715/*
2716 * parse an audio unit recursively
2717 */
2718
Takashi Iwai86e07d32005-11-17 15:08:02 +01002719static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002722 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 if (test_and_set_bit(unitid, state->unitbitmap))
2725 return 0; /* the unit already visited */
2726
2727 p1 = find_audio_control_unit(state, unitid);
2728 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002729 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return -EINVAL;
2731 }
2732
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002733 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2734 switch (p1[2]) {
2735 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002736 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002737 case UAC_MIXER_UNIT:
2738 return parse_audio_mixer_unit(state, unitid, p1);
2739 case UAC2_CLOCK_SOURCE:
2740 return parse_clock_source_unit(state, unitid, p1);
2741 case UAC_SELECTOR_UNIT:
2742 case UAC2_CLOCK_SELECTOR:
2743 return parse_audio_selector_unit(state, unitid, p1);
2744 case UAC_FEATURE_UNIT:
2745 return parse_audio_feature_unit(state, unitid, p1);
2746 case UAC1_PROCESSING_UNIT:
2747 /* UAC2_EFFECT_UNIT has the same value */
2748 if (protocol == UAC_VERSION_1)
2749 return parse_audio_processing_unit(state, unitid, p1);
2750 else
2751 return 0; /* FIXME - effect units not implemented yet */
2752 case UAC1_EXTENSION_UNIT:
2753 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2754 if (protocol == UAC_VERSION_1)
2755 return parse_audio_extension_unit(state, unitid, p1);
2756 else /* UAC_VERSION_2 */
2757 return parse_audio_processing_unit(state, unitid, p1);
2758 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002759 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002760 default:
2761 usb_audio_err(state->chip,
2762 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2763 return -EINVAL;
2764 }
2765 } else { /* UAC_VERSION_3 */
2766 switch (p1[2]) {
2767 case UAC_INPUT_TERMINAL:
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01002768 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002769 case UAC3_MIXER_UNIT:
2770 return parse_audio_mixer_unit(state, unitid, p1);
2771 case UAC3_CLOCK_SOURCE:
2772 return parse_clock_source_unit(state, unitid, p1);
Jorge Sanjuanc77e1ef2018-07-11 13:37:51 +01002773 case UAC3_SELECTOR_UNIT:
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002774 case UAC3_CLOCK_SELECTOR:
2775 return parse_audio_selector_unit(state, unitid, p1);
2776 case UAC3_FEATURE_UNIT:
2777 return parse_audio_feature_unit(state, unitid, p1);
2778 case UAC3_EFFECT_UNIT:
2779 return 0; /* FIXME - effect units not implemented yet */
2780 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002781 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002782 case UAC3_EXTENSION_UNIT:
2783 return parse_audio_extension_unit(state, unitid, p1);
2784 default:
2785 usb_audio_err(state->chip,
2786 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2787 return -EINVAL;
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790}
2791
Clemens Ladisch84957a82005-04-29 16:23:13 +02002792static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2793{
Takashi Iwai124751d2017-10-10 14:10:32 +02002794 /* kill pending URBs */
2795 snd_usb_mixer_disconnect(mixer);
2796
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002797 kfree(mixer->id_elems);
2798 if (mixer->urb) {
2799 kfree(mixer->urb->transfer_buffer);
2800 usb_free_urb(mixer->urb);
2801 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002802 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002803 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002804 kfree(mixer);
2805}
2806
Takashi Iwai86e07d32005-11-17 15:08:02 +01002807static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002808{
2809 struct usb_mixer_interface *mixer = device->device_data;
2810 snd_usb_mixer_free(mixer);
2811 return 0;
2812}
2813
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002814/* UAC3 predefined channels configuration */
2815struct uac3_badd_profile {
2816 int subclass;
2817 const char *name;
2818 int c_chmask; /* capture channels mask */
2819 int p_chmask; /* playback channels mask */
2820 int st_chmask; /* side tone mixing channel mask */
2821};
2822
2823static struct uac3_badd_profile uac3_badd_profiles[] = {
2824 {
2825 /*
2826 * BAIF, BAOF or combination of both
2827 * IN: Mono or Stereo cfg, Mono alt possible
2828 * OUT: Mono or Stereo cfg, Mono alt possible
2829 */
2830 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2831 .name = "GENERIC IO",
2832 .c_chmask = -1, /* dynamic channels */
2833 .p_chmask = -1, /* dynamic channels */
2834 },
2835 {
2836 /* BAOF; Stereo only cfg, Mono alt possible */
2837 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2838 .name = "HEADPHONE",
2839 .p_chmask = 3,
2840 },
2841 {
2842 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2843 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2844 .name = "SPEAKER",
2845 .p_chmask = -1, /* dynamic channels */
2846 },
2847 {
2848 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2849 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2850 .name = "MICROPHONE",
2851 .c_chmask = -1, /* dynamic channels */
2852 },
2853 {
2854 /*
2855 * BAIOF topology
2856 * IN: Mono only
2857 * OUT: Mono or Stereo cfg, Mono alt possible
2858 */
2859 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2860 .name = "HEADSET",
2861 .c_chmask = 1,
2862 .p_chmask = -1, /* dynamic channels */
2863 .st_chmask = 1,
2864 },
2865 {
2866 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2867 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2868 .name = "HEADSET ADAPTER",
2869 .c_chmask = 1,
2870 .p_chmask = 3,
2871 .st_chmask = 1,
2872 },
2873 {
2874 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2875 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2876 .name = "SPEAKERPHONE",
2877 .c_chmask = 1,
2878 .p_chmask = 1,
2879 },
2880 { 0 } /* terminator */
2881};
2882
2883static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2884 struct uac3_badd_profile *f,
2885 int c_chmask, int p_chmask)
2886{
2887 /*
2888 * If both playback/capture channels are dynamic, make sure
2889 * at least one channel is present
2890 */
2891 if (f->c_chmask < 0 && f->p_chmask < 0) {
2892 if (!c_chmask && !p_chmask) {
2893 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2894 f->name);
2895 return false;
2896 }
2897 return true;
2898 }
2899
2900 if ((f->c_chmask < 0 && !c_chmask) ||
2901 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2902 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2903 f->name);
2904 return false;
2905 }
2906 if ((f->p_chmask < 0 && !p_chmask) ||
2907 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2908 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2909 f->name);
2910 return false;
2911 }
2912 return true;
2913}
2914
2915/*
2916 * create mixer controls for UAC3 BADD profiles
2917 *
2918 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2919 *
2920 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2921 */
2922static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2923 int ctrlif)
2924{
2925 struct usb_device *dev = mixer->chip->dev;
2926 struct usb_interface_assoc_descriptor *assoc;
2927 int badd_profile = mixer->chip->badd_profile;
2928 struct uac3_badd_profile *f;
2929 const struct usbmix_ctl_map *map;
2930 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2931 int i;
2932
2933 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2934
2935 /* Detect BADD capture/playback channels from AS EP descriptors */
2936 for (i = 0; i < assoc->bInterfaceCount; i++) {
2937 int intf = assoc->bFirstInterface + i;
2938
2939 struct usb_interface *iface;
2940 struct usb_host_interface *alts;
2941 struct usb_interface_descriptor *altsd;
2942 unsigned int maxpacksize;
2943 char dir_in;
2944 int chmask, num;
2945
2946 if (intf == ctrlif)
2947 continue;
2948
2949 iface = usb_ifnum_to_if(dev, intf);
2950 num = iface->num_altsetting;
2951
2952 if (num < 2)
2953 return -EINVAL;
2954
2955 /*
2956 * The number of Channels in an AudioStreaming interface
2957 * and the audio sample bit resolution (16 bits or 24
2958 * bits) can be derived from the wMaxPacketSize field in
2959 * the Standard AS Audio Data Endpoint descriptor in
2960 * Alternate Setting 1
2961 */
2962 alts = &iface->altsetting[1];
2963 altsd = get_iface_desc(alts);
2964
2965 if (altsd->bNumEndpoints < 1)
2966 return -EINVAL;
2967
2968 /* check direction */
2969 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2970 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2971
2972 switch (maxpacksize) {
2973 default:
2974 usb_audio_err(mixer->chip,
2975 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2976 maxpacksize);
2977 return -EINVAL;
2978 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2979 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2980 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2981 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2982 chmask = 1;
2983 break;
2984 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2985 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2986 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2987 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2988 chmask = 3;
2989 break;
2990 }
2991
2992 if (dir_in)
2993 c_chmask = chmask;
2994 else
2995 p_chmask = chmask;
2996 }
2997
2998 usb_audio_dbg(mixer->chip,
2999 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
3000 badd_profile, c_chmask, p_chmask);
3001
3002 /* check the mapping table */
3003 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
3004 if (map->id == badd_profile)
3005 break;
3006 }
3007
3008 if (!map->id)
3009 return -EINVAL;
3010
3011 for (f = uac3_badd_profiles; f->name; f++) {
3012 if (badd_profile == f->subclass)
3013 break;
3014 }
3015 if (!f->name)
3016 return -EINVAL;
3017 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
3018 return -EINVAL;
3019 st_chmask = f->st_chmask;
3020
3021 /* Playback */
3022 if (p_chmask) {
3023 /* Master channel, always writable */
3024 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3025 UAC3_BADD_FU_ID2, map->map);
3026 /* Mono/Stereo volume channels, always writable */
3027 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
3028 UAC3_BADD_FU_ID2, map->map);
3029 }
3030
3031 /* Capture */
3032 if (c_chmask) {
3033 /* Master channel, always writable */
3034 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3035 UAC3_BADD_FU_ID5, map->map);
3036 /* Mono/Stereo volume channels, always writable */
3037 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
3038 UAC3_BADD_FU_ID5, map->map);
3039 }
3040
3041 /* Side tone-mixing */
3042 if (st_chmask) {
3043 /* Master channel, always writable */
3044 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3045 UAC3_BADD_FU_ID7, map->map);
3046 /* Mono volume channel, always writable */
3047 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
3048 UAC3_BADD_FU_ID7, map->map);
3049 }
3050
Jorge Sanjuan3528cd82018-06-14 15:05:57 +01003051 /* Insertion Control */
3052 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
3053 struct usb_audio_term iterm, oterm;
3054
3055 /* Input Term - Insertion control */
3056 memset(&iterm, 0, sizeof(iterm));
3057 iterm.id = UAC3_BADD_IT_ID4;
3058 iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3059 build_connector_control(mixer, &iterm, true);
3060
3061 /* Output Term - Insertion control */
3062 memset(&oterm, 0, sizeof(oterm));
3063 oterm.id = UAC3_BADD_OT_ID3;
3064 oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3065 build_connector_control(mixer, &oterm, false);
3066 }
3067
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003068 return 0;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071/*
3072 * create mixer controls
3073 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01003074 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02003076static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003078 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 int err;
3080 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01003081 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02003084 state.chip = mixer->chip;
3085 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003086 state.buffer = mixer->hostif->extra;
3087 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003090 for (map = usbmix_ctl_maps; map->id; map++) {
3091 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02003093 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003094 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 break;
3096 }
3097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Daniel Mack23caaf12010-03-11 21:13:25 +01003099 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02003100 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3101 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02003102 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01003103 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02003104 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01003105
3106 if (desc->bLength < sizeof(*desc))
3107 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02003108 /* mark terminal ID as visited */
3109 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01003110 state.oterm.id = desc->bTerminalID;
3111 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3112 state.oterm.name = desc->iTerminal;
3113 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003114 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01003115 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003116 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01003117 struct uac2_output_terminal_descriptor *desc = p;
3118
3119 if (desc->bLength < sizeof(*desc))
3120 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02003121 /* mark terminal ID as visited */
3122 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01003123 state.oterm.id = desc->bTerminalID;
3124 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3125 state.oterm.name = desc->iTerminal;
3126 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003127 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01003128 return err;
Daniel Mack09414202010-05-31 13:35:44 +02003129
Daniel Mack6bc170e2014-05-24 10:58:16 +02003130 /*
3131 * For UAC2, use the same approach to also add the
3132 * clock selectors
3133 */
Daniel Mack09414202010-05-31 13:35:44 +02003134 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01003135 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02003136 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07003137
Takashi Iwai2b54f7852018-04-23 15:19:25 +02003138 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07003139 UAC2_TE_CONNECTOR)) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01003140 build_connector_control(state.mixer, &state.oterm,
Andrew Chant5a222e82018-03-23 19:25:23 -07003141 false);
3142 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003143 } else { /* UAC_VERSION_3 */
3144 struct uac3_output_terminal_descriptor *desc = p;
3145
3146 if (desc->bLength < sizeof(*desc))
3147 continue; /* invalid descriptor? */
3148 /* mark terminal ID as visited */
3149 set_bit(desc->bTerminalID, state.unitbitmap);
3150 state.oterm.id = desc->bTerminalID;
3151 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3152 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3153 err = parse_audio_unit(&state, desc->bSourceID);
3154 if (err < 0 && err != -EINVAL)
3155 return err;
3156
3157 /*
3158 * For UAC3, use the same approach to also add the
3159 * clock selectors
3160 */
3161 err = parse_audio_unit(&state, desc->bCSourceID);
3162 if (err < 0 && err != -EINVAL)
3163 return err;
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003164
3165 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3166 UAC3_TE_INSERTION)) {
Jorge Sanjuan167e1fb2018-06-14 15:05:56 +01003167 build_connector_control(state.mixer, &state.oterm,
Jorge Sanjuan1d38f5d2018-05-11 16:25:36 +01003168 false);
3169 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Daniel Mack23caaf12010-03-11 21:13:25 +01003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return 0;
3174}
Clemens Ladisch84957a82005-04-29 16:23:13 +02003175
Daniel Mack7b1eda22010-03-11 21:13:22 +01003176void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003177{
Takashi Iwai3360b842014-11-18 11:47:04 +01003178 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003179
Takashi Iwai8c558072018-05-03 12:33:32 +02003180 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01003181 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02003182 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01003183 /* invalidate cache, so the value is read from the device */
3184 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003185 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003186 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01003187 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003188}
3189
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003190static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01003191 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003192{
Takashi Iwai8c558072018-05-03 12:33:32 +02003193 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003194 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3195 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003196 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01003197 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003198 cval->control, cval->cmask, cval->channels,
3199 val_types[cval->val_type]);
3200 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3201 cval->min, cval->max, cval->dBmin, cval->dBmax);
3202}
3203
3204static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3205 struct snd_info_buffer *buffer)
3206{
3207 struct snd_usb_audio *chip = entry->private_data;
3208 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01003209 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003210 int unitid;
3211
3212 list_for_each_entry(mixer, &chip->mixer_list, list) {
3213 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003214 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02003215 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01003216 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003217 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3218 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003219 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003220 snd_iprintf(buffer, " Unit: %i\n", list->id);
3221 if (list->kctl)
3222 snd_iprintf(buffer,
3223 " Control: name=\"%s\", index=%i\n",
3224 list->kctl->id.name,
3225 list->kctl->id.index);
3226 if (list->dump)
3227 list->dump(buffer, list);
3228 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003229 }
3230 }
3231}
3232
Daniel Macke213e9c2010-05-11 18:13:50 +02003233static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3234 int attribute, int value, int index)
3235{
Takashi Iwai3360b842014-11-18 11:47:04 +01003236 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02003237 __u8 unitid = (index >> 8) & 0xff;
3238 __u8 control = (value >> 8) & 0xff;
3239 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02003240 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02003241
3242 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003243 usb_audio_dbg(mixer->chip,
3244 "%s(): bogus channel number %d\n",
3245 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02003246 return;
3247 }
3248
Takashi Iwai8c558072018-05-03 12:33:32 +02003249 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02003250 count++;
3251
3252 if (count == 0)
3253 return;
3254
Takashi Iwai8c558072018-05-03 12:33:32 +02003255 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003256 struct usb_mixer_elem_info *info;
3257
3258 if (!list->kctl)
3259 continue;
3260
Takashi Iwai8c558072018-05-03 12:33:32 +02003261 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02003262 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02003263 continue;
3264
3265 switch (attribute) {
3266 case UAC2_CS_CUR:
3267 /* invalidate cache, so the value is read from the device */
3268 if (channel)
3269 info->cached &= ~(1 << channel);
3270 else /* master channel */
3271 info->cached = 0;
3272
3273 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01003274 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02003275 break;
3276
3277 case UAC2_CS_RANGE:
3278 /* TODO */
3279 break;
3280
3281 case UAC2_CS_MEM:
3282 /* TODO */
3283 break;
3284
3285 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003286 usb_audio_dbg(mixer->chip,
3287 "unknown attribute %d in interrupt\n",
3288 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02003289 break;
3290 } /* switch */
3291 }
3292}
3293
3294static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003295{
3296 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02003297 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01003298 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003299
Oliver Neukumedf7de32011-03-11 13:19:43 +01003300 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02003301 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003302
Daniel Macke213e9c2010-05-11 18:13:50 +02003303 if (mixer->protocol == UAC_VERSION_1) {
3304 struct uac1_status_word *status;
3305
3306 for (status = urb->transfer_buffer;
3307 len >= sizeof(*status);
3308 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01003309 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02003310 status->bStatusType,
3311 status->bOriginator);
3312
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003313 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02003314 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3315 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003316 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003317
3318 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3319 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003320 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003321 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3322 }
3323 } else { /* UAC_VERSION_2 */
3324 struct uac2_interrupt_data_msg *msg;
3325
3326 for (msg = urb->transfer_buffer;
3327 len >= sizeof(*msg);
3328 len -= sizeof(*msg), msg++) {
3329 /* drop vendor specific and endpoint requests */
3330 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3331 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3332 continue;
3333
3334 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3335 le16_to_cpu(msg->wValue),
3336 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003337 }
3338 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003339
3340requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003341 if (ustatus != -ENOENT &&
3342 ustatus != -ECONNRESET &&
3343 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003344 urb->dev = mixer->chip->dev;
3345 usb_submit_urb(urb, GFP_ATOMIC);
3346 }
3347}
3348
3349/* create the handler for the optional status interrupt endpoint */
3350static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3351{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003352 struct usb_endpoint_descriptor *ep;
3353 void *transfer_buffer;
3354 int buffer_length;
3355 unsigned int epnum;
3356
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003357 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003358 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003359 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003360 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003361 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003362 return 0;
3363
Julia Lawall42a6e662008-12-29 11:23:02 +01003364 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003365 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3366 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3367 if (!transfer_buffer)
3368 return -ENOMEM;
3369 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3370 if (!mixer->urb) {
3371 kfree(transfer_buffer);
3372 return -ENOMEM;
3373 }
3374 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3375 usb_rcvintpipe(mixer->chip->dev, epnum),
3376 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003377 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003378 usb_submit_urb(mixer->urb, GFP_KERNEL);
3379 return 0;
3380}
3381
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003382static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3383 struct snd_ctl_elem_value *ucontrol)
3384{
3385 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3386
3387 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3388 return 0;
3389}
3390
3391static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3392 struct snd_ctl_elem_value *ucontrol)
3393{
3394 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3395 bool keep_iface = !!ucontrol->value.integer.value[0];
3396
3397 if (mixer->chip->keep_iface == keep_iface)
3398 return 0;
3399 mixer->chip->keep_iface = keep_iface;
3400 return 1;
3401}
3402
3403static const struct snd_kcontrol_new keep_iface_ctl = {
3404 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3405 .name = "Keep Interface",
3406 .info = snd_ctl_boolean_mono_info,
3407 .get = keep_iface_ctl_get,
3408 .put = keep_iface_ctl_put,
3409};
3410
3411static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3412{
3413 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3414
3415 /* need only one control per card */
3416 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3417 snd_ctl_free_one(kctl);
3418 return 0;
3419 }
3420
3421 return snd_ctl_add(mixer->chip->card, kctl);
3422}
3423
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003424int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3425 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003426{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003427 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003428 .dev_free = snd_usb_mixer_dev_free
3429 };
3430 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003431 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01003432 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003433
3434 strcpy(chip->card->mixername, "USB Mixer");
3435
Takashi Iwai561b2202005-09-09 14:22:34 +02003436 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003437 if (!mixer)
3438 return -ENOMEM;
3439 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003440 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003441 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3442 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003443 if (!mixer->id_elems) {
3444 kfree(mixer);
3445 return -ENOMEM;
3446 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003447
Daniel Mack1faa5d02011-08-04 15:56:28 +02003448 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3449 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003450 case UAC_VERSION_1:
3451 default:
3452 mixer->protocol = UAC_VERSION_1;
3453 break;
3454 case UAC_VERSION_2:
3455 mixer->protocol = UAC_VERSION_2;
3456 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003457 case UAC_VERSION_3:
3458 mixer->protocol = UAC_VERSION_3;
3459 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003460 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003461
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003462 if (mixer->protocol == UAC_VERSION_3 &&
3463 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003464 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3465 if (err < 0)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003466 goto _error;
Takashi Iwaif25ecf82018-05-27 15:18:22 +02003467 } else {
3468 err = snd_usb_mixer_controls(mixer);
3469 if (err < 0)
3470 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003471 }
Jorge Sanjuanad6baae2018-06-14 15:05:58 +01003472
3473 err = snd_usb_mixer_status_create(mixer);
3474 if (err < 0)
3475 goto _error;
3476
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003477 err = create_keep_iface_ctl(mixer);
3478 if (err < 0)
3479 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003480
Daniel Mack7b1eda22010-03-11 21:13:22 +01003481 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003482
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003483 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003484 if (err < 0)
3485 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003486
3487 if (list_empty(&chip->mixer_list) &&
3488 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3489 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3490
Clemens Ladisch84957a82005-04-29 16:23:13 +02003491 list_add(&mixer->list, &chip->mixer_list);
3492 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003493
3494_error:
3495 snd_usb_mixer_free(mixer);
3496 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003497}
3498
Takashi Iwaia6cece92014-10-31 11:24:32 +01003499void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003500{
Takashi Iwai124751d2017-10-10 14:10:32 +02003501 if (mixer->disconnected)
3502 return;
3503 if (mixer->urb)
3504 usb_kill_urb(mixer->urb);
3505 if (mixer->rc_urb)
3506 usb_kill_urb(mixer->rc_urb);
3507 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003508}
Takashi Iwai400362f2014-01-20 16:51:16 +01003509
3510#ifdef CONFIG_PM
3511/* stop any bus activity of a mixer */
3512static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3513{
3514 usb_kill_urb(mixer->urb);
3515 usb_kill_urb(mixer->rc_urb);
3516}
3517
3518static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3519{
3520 int err;
3521
3522 if (mixer->urb) {
3523 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3524 if (err < 0)
3525 return err;
3526 }
3527
3528 return 0;
3529}
3530
3531int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3532{
3533 snd_usb_mixer_inactivate(mixer);
3534 return 0;
3535}
3536
Takashi Iwai3360b842014-11-18 11:47:04 +01003537static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003538{
Takashi Iwai8c558072018-05-03 12:33:32 +02003539 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003540 int c, err, idx;
3541
3542 if (cval->cmask) {
3543 idx = 0;
3544 for (c = 0; c < MAX_CHANNELS; c++) {
3545 if (!(cval->cmask & (1 << c)))
3546 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003547 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003548 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003549 cval->cache_val[idx]);
3550 if (err < 0)
3551 return err;
3552 }
3553 idx++;
3554 }
3555 } else {
3556 /* master */
3557 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003558 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003559 if (err < 0)
3560 return err;
3561 }
3562 }
3563
3564 return 0;
3565}
3566
3567int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3568{
Takashi Iwai3360b842014-11-18 11:47:04 +01003569 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003570 int id, err;
3571
Takashi Iwai400362f2014-01-20 16:51:16 +01003572 if (reset_resume) {
3573 /* restore cached mixer values */
3574 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003575 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003576 if (list->resume) {
3577 err = list->resume(list);
3578 if (err < 0)
3579 return err;
3580 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003581 }
3582 }
3583 }
3584
Takashi Iwai964af632018-04-27 14:23:37 +02003585 snd_usb_mixer_resume_quirk(mixer);
3586
Takashi Iwai400362f2014-01-20 16:51:16 +01003587 return snd_usb_mixer_activate(mixer);
3588}
3589#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003590
3591void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3592 struct usb_mixer_interface *mixer,
3593 int unitid)
3594{
3595 list->mixer = mixer;
3596 list->id = unitid;
3597 list->dump = snd_usb_mixer_dump_cval;
3598#ifdef CONFIG_PM
3599 list->resume = restore_mixer_value;
3600#endif
3601}