blob: 4987982250d5210584327258dafece4023da659e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Daniel Mack157a57b2010-06-16 17:57:30 +020029/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
Daniel Mackcddaafb2016-04-09 11:21:46 +020048#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010052#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010053#include <linux/usb/audio-v2.h>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020054#include <linux/usb/audio-v3.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <sound/core.h>
57#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020058#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020059#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020060#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010063#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010064#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010065#include "mixer_quirks.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010066#include "power.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020067
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010068#define MAX_ID_ELEMS 256
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
Takashi Iwai86e07d32005-11-17 15:08:02 +010080struct mixer_build {
81 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020082 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned char *buffer;
84 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010085 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010086 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020088 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -050091/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -080092enum {
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
99};
100enum {
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
111 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100112#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100114static const struct usbmix_name_map *
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300115find_map(const struct usbmix_name_map *p, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100117 if (!p)
118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ruslan Bilovol17156f22018-05-04 04:24:04 +0300120 for (; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
123 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100125 return NULL;
126}
127
128/* get the mapped name if the unit matches */
129static int
130check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
131{
132 if (!p || !p->name)
133 return 0;
134
135 buflen--;
136 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Takashi Iwai5aeee342014-11-18 11:02:14 +0100139/* ignore the error value if ignore_ctl_error flag is set */
140#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100144static inline int
145check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100149 return 1;
150}
151
152/* dB mapping */
153static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
155{
156 if (p && p->dB) {
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200159 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200163/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100164static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200165 int index, char *buf, int buflen)
166{
167 const struct usbmix_selector_map *p;
168
Daniel Mack6bc170e2014-05-24 10:58:16 +0200169 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200170 return 0;
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
174 }
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * find an audio control unit with the given unit id
180 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200181static void *find_audio_control_unit(struct mixer_build *state,
182 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Daniel Mack67e1daa2010-05-31 13:35:43 +0200187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
Daniel Mack67e1daa2010-05-31 13:35:43 +0200192 hdr->bUnitID == unit)
193 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * copy a string with the given id
201 */
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300202static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200203 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
Jaejoong Kim251552a2017-12-04 15:31:48 +0900206
207 if (len < 0)
208 return 0;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 buf[len] = 0;
211 return len;
212}
213
214/*
215 * convert from the byte/word on usb descriptor to the zero-based integer
216 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
221 return !!val;
222 case USB_MIXER_INV_BOOLEAN:
223 return !val;
224 case USB_MIXER_U8:
225 val &= 0xff;
226 break;
227 case USB_MIXER_S8:
228 val &= 0xff;
229 if (val >= 0x80)
230 val -= 0x100;
231 break;
232 case USB_MIXER_U16:
233 val &= 0xffff;
234 break;
235 case USB_MIXER_S16:
236 val &= 0xffff;
237 if (val >= 0x8000)
238 val -= 0x10000;
239 break;
240 }
241 return val;
242}
243
244/*
245 * convert from the zero-based int to the byte/word for usb descriptor
246 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100247static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 switch (cval->val_type) {
250 case USB_MIXER_BOOLEAN:
251 return !!val;
252 case USB_MIXER_INV_BOOLEAN:
253 return !val;
254 case USB_MIXER_S8:
255 case USB_MIXER_U8:
256 return val & 0xff;
257 case USB_MIXER_S16:
258 case USB_MIXER_U16:
259 return val & 0xffff;
260 }
261 return 0; /* not reached */
262}
263
Takashi Iwai86e07d32005-11-17 15:08:02 +0100264static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200266 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cval->res = 1;
268 if (val < cval->min)
269 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 else
273 return (val - cval->min) / cval->res;
274}
275
Takashi Iwai86e07d32005-11-17 15:08:02 +0100276static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (val < 0)
279 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200280 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cval->res = 1;
282 val *= cval->res;
283 val += cval->min;
284 if (val > cval->max)
285 return cval->max;
286 return val;
287}
288
Julian Scheelbc18e312015-08-14 16:14:45 +0200289static int uac2_ctl_value_size(int val_type)
290{
291 switch (val_type) {
292 case USB_MIXER_S32:
293 case USB_MIXER_U32:
294 return 4;
295 case USB_MIXER_S16:
296 case USB_MIXER_U16:
297 return 2;
298 default:
299 return 1;
300 }
301 return 0; /* unreachable */
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/*
306 * retrieve a mixer value
307 */
308
Daniel Mack6bc170e2014-05-24 10:58:16 +0200309static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai3360b842014-11-18 11:47:04 +0100312 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
315 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200316 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Takashi Iwai47ab1542015-08-25 16:09:00 +0200318 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100319 if (err < 0)
320 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200329 err = 0;
330 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700331 } else if (err == -ETIMEDOUT) {
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100335 usb_audio_dbg(chip,
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200338 err = -EINVAL;
339
340 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200341 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Daniel Mack6bc170e2014-05-24 10:58:16 +0200345static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100347{
Takashi Iwai3360b842014-11-18 11:47:04 +0100348 struct snd_usb_audio *chip = cval->head.mixer->chip;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
Daniel Mack23caaf12010-03-11 21:13:25 +0100351 unsigned char *val;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100352 int idx = 0, ret, val_size, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100353 __u8 bRequest;
354
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100355 val_size = uac2_ctl_value_size(cval->val_type);
356
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100359 size = val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200360 } else {
361 bRequest = UAC2_CS_RANGE;
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100362 size = sizeof(__u16) + 3 * val_size;
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200363 }
364
365 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100366
Takashi Iwai47ab1542015-08-25 16:09:00 +0200367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100368 if (ret)
369 goto error;
370
Takashi Iwai47ab1542015-08-25 16:09:00 +0200371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200374 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200375 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100376
377 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100378error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100379 usb_audio_err(chip,
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100382 return ret;
383 }
384
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200385 /* FIXME: how should we handle multiple triplets here? */
386
Daniel Mack23caaf12010-03-11 21:13:25 +0100387 switch (request) {
388 case UAC_GET_CUR:
389 val = buf;
390 break;
391 case UAC_GET_MIN:
392 val = buf + sizeof(__u16);
393 break;
394 case UAC_GET_MAX:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100395 val = buf + sizeof(__u16) + val_size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100396 break;
397 case UAC_GET_RES:
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100398 val = buf + sizeof(__u16) + val_size * 2;
Daniel Mack23caaf12010-03-11 21:13:25 +0100399 break;
400 default:
401 return -EINVAL;
402 }
403
Kirill Marinushkin447cae52018-01-29 06:37:55 +0100404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
Daniel Mack23caaf12010-03-11 21:13:25 +0100406
407 return 0;
408}
409
Daniel Mack6bc170e2014-05-24 10:58:16 +0200410static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100412{
Eldad Zack9f814102012-11-28 23:55:35 +0100413 validx += cval->idx_off;
414
Takashi Iwai3360b842014-11-18 11:47:04 +0100415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
418}
419
Daniel Mack6bc170e2014-05-24 10:58:16 +0200420static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100427static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
432 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Chris J Argeseef90452014-11-12 12:07:01 -0600435int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100436 int channel, int index, int *value)
437{
438 int err;
439
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
442 return 0;
443 }
444 err = get_cur_mix_raw(cval, channel, value);
445 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100448 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200449 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100450 return err;
451 }
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * set a mixer value
459 */
460
Daniel Mack7b1eda22010-03-11 21:13:22 +0100461int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai3360b842014-11-18 11:47:04 +0100464 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200465 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200466 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100467
Eldad Zack9f814102012-11-28 23:55:35 +0100468 validx += cval->idx_off;
469
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200470
Takashi Iwai3360b842014-11-18 11:47:04 +0100471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200473 } else { /* UAC_VERSION_2/3 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200490
491 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 if (err < 0)
493 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200494
Takashi Iwai978520b2012-10-12 15:12:55 +0200495 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
501 if (err >= 0) {
Takashi Iwai978520b2012-10-12 15:12:55 +0200502 err = 0;
503 goto out;
Stephen Barber5a9a8ec2017-08-17 15:17:46 -0700504 } else if (err == -ETIMEDOUT) {
505 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100506 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200507 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100508 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200509 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 err = -EINVAL;
511
512 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200513 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Daniel Mack6bc170e2014-05-24 10:58:16 +0200517static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Chris J Argeseef90452014-11-12 12:07:01 -0600523int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100524 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai641b4872009-01-15 17:05:24 +0100526 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
530
531 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100532 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200534 __func__, channel, cval->control);
535 return 0;
536 }
537
Daniel Mack6bc170e2014-05-24 10:58:16 +0200538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
540 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100541 if (err < 0)
542 return err;
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200548/*
549 * TLV callback for mixer volume controls
550 */
Felix Homann285de9c2012-04-23 20:24:24 +0200551int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200552 unsigned int size, unsigned int __user *_tlv)
553{
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200556
557 if (size < sizeof(scale))
558 return -ENOMEM;
Takashi Iwai0f174b32017-08-16 14:18:37 +0200559 if (cval->min_mute)
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200563 if (copy_to_user(_tlv, scale, sizeof(scale)))
564 return -EFAULT;
565 return 0;
566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * parser routines begin here...
570 */
571
Takashi Iwai86e07d32005-11-17 15:08:02 +0100572static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574
575/*
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
578 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200579static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
589 *
590 * if failed, give up and free the control instance.
591 */
592
Takashi Iwai3360b842014-11-18 11:47:04 +0100593int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200594 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Takashi Iwai3360b842014-11-18 11:47:04 +0100596 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200598
Daniel Mackef9d5972011-05-25 09:09:00 +0200599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kctl->id.index++;
Daniel Mackef9d5972011-05-25 09:09:00 +0200601 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200602 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
603 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100606 list->kctl = kctl;
607 list->next_id_elem = mixer->id_elems[list->id];
608 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * get a terminal name string
614 */
615
616static struct iterm_name_combo {
617 int type;
618 char *name;
619} iterm_names[] = {
620 { 0x0300, "Output" },
621 { 0x0301, "Speaker" },
622 { 0x0302, "Headphone" },
623 { 0x0303, "HMD Audio" },
624 { 0x0304, "Desktop Speaker" },
625 { 0x0305, "Room Speaker" },
626 { 0x0306, "Com Speaker" },
627 { 0x0307, "LFE" },
628 { 0x0600, "External In" },
629 { 0x0601, "Analog In" },
630 { 0x0602, "Digital In" },
631 { 0x0603, "Line" },
632 { 0x0604, "Legacy In" },
633 { 0x0605, "IEC958 In" },
634 { 0x0606, "1394 DA Stream" },
635 { 0x0607, "1394 DV Stream" },
636 { 0x0700, "Embedded" },
637 { 0x0701, "Noise Source" },
638 { 0x0702, "Equalization Noise" },
639 { 0x0703, "CD" },
640 { 0x0704, "DAT" },
641 { 0x0705, "DCC" },
642 { 0x0706, "MiniDisk" },
643 { 0x0707, "Analog Tape" },
644 { 0x0708, "Phonograph" },
645 { 0x0709, "VCR Audio" },
646 { 0x070a, "Video Disk Audio" },
647 { 0x070b, "DVD Audio" },
648 { 0x070c, "TV Tuner Audio" },
649 { 0x070d, "Satellite Rec Audio" },
650 { 0x070e, "Cable Tuner Audio" },
651 { 0x070f, "DSS Audio" },
652 { 0x0710, "Radio Receiver" },
653 { 0x0711, "Radio Transmitter" },
654 { 0x0712, "Multi-Track Recorder" },
655 { 0x0713, "Synthesizer" },
656 { 0 },
657};
658
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300659static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned char *name, int maxlen, int term_only)
661{
662 struct iterm_name_combo *names;
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100663 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100665 if (iterm->name) {
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +0300666 len = snd_usb_copy_string_desc(chip, iterm->name,
Daniel Mack6bc170e2014-05-24 10:58:16 +0200667 name, maxlen);
Takashi Iwai56a23ee2017-12-19 13:38:23 +0100668 if (len)
669 return len;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* virtual type - not a real terminal */
673 if (iterm->type >> 16) {
674 if (term_only)
675 return 0;
676 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100677 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200678 strcpy(name, "Selector");
679 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200680 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200681 strcpy(name, "Process Unit");
682 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200683 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200684 strcpy(name, "Ext Unit");
685 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100686 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200687 strcpy(name, "Mixer");
688 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 default:
690 return sprintf(name, "Unit %d", iterm->id);
691 }
692 }
693
694 switch (iterm->type & 0xff00) {
695 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200696 strcpy(name, "PCM");
697 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200699 strcpy(name, "Mic");
700 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200702 strcpy(name, "Headset");
703 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200705 strcpy(name, "Phone");
706 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
Daniel Mack6bc170e2014-05-24 10:58:16 +0200709 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (names->type == iterm->type) {
711 strcpy(name, names->name);
712 return strlen(names->name);
713 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * parse the source unit recursively until it reaches to a terminal
721 * or a branched unit.
722 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200723static int check_input_term(struct mixer_build *state, int id,
724 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200726 int protocol = state->mixer->protocol;
Daniel Mack09414202010-05-31 13:35:44 +0200727 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100728 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 memset(term, 0, sizeof(*term));
731 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100732 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 term->id = id;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200734
735 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
736 switch (hdr[2]) {
737 case UAC_INPUT_TERMINAL:
738 if (protocol == UAC_VERSION_1) {
739 struct uac_input_terminal_descriptor *d = p1;
740
741 term->type = le16_to_cpu(d->wTerminalType);
742 term->channels = d->bNrChannels;
743 term->chconfig = le16_to_cpu(d->wChannelConfig);
744 term->name = d->iTerminal;
745 } else { /* UAC_VERSION_2 */
746 struct uac2_input_terminal_descriptor *d = p1;
747
748 /* call recursively to verify that the
749 * referenced clock entity is valid */
750 err = check_input_term(state, d->bCSourceID, term);
751 if (err < 0)
752 return err;
753
754 /* save input term properties after recursion,
755 * to ensure they are not overriden by the
756 * recursion calls */
757 term->id = id;
758 term->type = le16_to_cpu(d->wTerminalType);
759 term->channels = d->bNrChannels;
760 term->chconfig = le32_to_cpu(d->bmChannelConfig);
761 term->name = d->iTerminal;
762 }
763 return 0;
764 case UAC_FEATURE_UNIT: {
765 /* the header is the same for v1 and v2 */
766 struct uac_feature_unit_descriptor *d = p1;
767
768 id = d->bSourceID;
769 break; /* continue to parse */
770 }
771 case UAC_MIXER_UNIT: {
772 struct uac_mixer_unit_descriptor *d = p1;
773
774 term->type = d->bDescriptorSubtype << 16; /* virtual type */
775 term->channels = uac_mixer_unit_bNrChannels(d);
776 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
777 term->name = uac_mixer_unit_iMixer(d);
778 return 0;
779 }
780 case UAC_SELECTOR_UNIT:
781 case UAC2_CLOCK_SELECTOR: {
782 struct uac_selector_unit_descriptor *d = p1;
783 /* call recursively to retrieve the channel info */
784 err = check_input_term(state, d->baSourceID[0], term);
785 if (err < 0)
786 return err;
787 term->type = d->bDescriptorSubtype << 16; /* virtual type */
788 term->id = id;
789 term->name = uac_selector_unit_iSelector(d);
790 return 0;
791 }
792 case UAC1_PROCESSING_UNIT:
793 case UAC1_EXTENSION_UNIT:
794 /* UAC2_PROCESSING_UNIT_V2 */
795 /* UAC2_EFFECT_UNIT */
796 case UAC2_EXTENSION_UNIT_V2: {
797 struct uac_processing_unit_descriptor *d = p1;
798
799 if (protocol == UAC_VERSION_2 &&
800 hdr[2] == UAC2_EFFECT_UNIT) {
801 /* UAC2/UAC1 unit IDs overlap here in an
802 * uncompatible way. Ignore this unit for now.
803 */
804 return 0;
805 }
806
807 if (d->bNrInPins) {
808 id = d->baSourceID[0];
809 break; /* continue to parse */
810 }
811 term->type = d->bDescriptorSubtype << 16; /* virtual type */
812 term->channels = uac_processing_unit_bNrChannels(d);
813 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
814 term->name = uac_processing_unit_iProcessing(d, protocol);
815 return 0;
816 }
817 case UAC2_CLOCK_SOURCE: {
818 struct uac_clock_source_descriptor *d = p1;
819
820 term->type = d->bDescriptorSubtype << 16; /* virtual type */
821 term->id = id;
822 term->name = d->iClockSource;
823 return 0;
824 }
825 default:
826 return -ENODEV;
827 }
828 } else { /* UAC_VERSION_3 */
829 switch (hdr[2]) {
830 case UAC_INPUT_TERMINAL: {
831 struct uac3_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200832
833 /* call recursively to verify that the
834 * referenced clock entity is valid */
835 err = check_input_term(state, d->bCSourceID, term);
836 if (err < 0)
837 return err;
838
839 /* save input term properties after recursion,
840 * to ensure they are not overriden by the
841 * recursion calls */
842 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100843 term->type = le16_to_cpu(d->wTerminalType);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100844
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200845 /* REVISIT: UAC3 IT doesn't have channels/cfg */
846 term->channels = 0;
847 term->chconfig = 0;
848
849 term->name = le16_to_cpu(d->wTerminalDescrStr);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100850 return 0;
851 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200852 case UAC3_FEATURE_UNIT: {
853 struct uac3_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100854
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200855 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break; /* continue to parse */
857 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200858 case UAC3_CLOCK_SOURCE: {
859 struct uac3_clock_source_descriptor *d = p1;
860
861 term->type = d->bDescriptorSubtype << 16; /* virtual type */
862 term->id = id;
863 term->name = le16_to_cpu(d->wClockSourceStr);
864 return 0;
865 }
866 default:
867 return -ENODEV;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 }
871 return -ENODEV;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Feature Unit
876 */
877
878/* feature unit control information */
879struct usb_feature_control_info {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700880 int control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200882 int type; /* data type for uac1 */
883 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886static struct usb_feature_control_info audio_feature_info[] = {
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700887 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
888 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
889 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
890 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
891 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
892 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
893 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
894 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
895 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
896 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200897 /* UAC2 specific */
Andrew Chant21e9b3e2018-03-22 14:39:55 -0700898 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
899 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
900 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901};
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600904void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jesper Juhl4d572772005-05-30 17:30:32 +0200906 kfree(kctl->private_data);
907 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
911 * interface to ALSA control for feature/mixer units
912 */
913
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100914/* volume control quirks */
915static void volume_control_quirks(struct usb_mixer_elem_info *cval,
916 struct snd_kcontrol *kctl)
917{
Takashi Iwai3360b842014-11-18 11:47:04 +0100918 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100919 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +0100920 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500921 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +0100922 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
923 cval->min = 0x0000;
924 cval->max = 0xffff;
925 cval->res = 0x00e6;
926 break;
927 }
928 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
929 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
930 cval->min = 0x00;
931 cval->max = 0xff;
932 break;
933 }
934 if (strstr(kctl->id.name, "Effect Return") != NULL) {
935 cval->min = 0xb706;
936 cval->max = 0xff7b;
937 cval->res = 0x0073;
938 break;
939 }
940 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
941 (strstr(kctl->id.name, "Effect Send") != NULL)) {
942 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
943 cval->max = 0xfcfe;
944 cval->res = 0x0073;
945 }
946 break;
947
Felix Homannd34bf142012-04-23 20:24:27 +0200948 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
949 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
950 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100951 usb_audio_info(chip,
952 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +0200953 cval->min = 0x0000;
954 cval->max = 0x7f00;
955 cval->res = 0x0100;
956 break;
957 }
958 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
959 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100960 usb_audio_info(chip,
961 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +0200962 cval->min = 0x00;
963 cval->max = 0x7f;
964 break;
965 }
966 break;
967
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100968 case USB_ID(0x0471, 0x0101):
969 case USB_ID(0x0471, 0x0104):
970 case USB_ID(0x0471, 0x0105):
971 case USB_ID(0x0672, 0x1041):
972 /* quirk for UDA1321/N101.
973 * note that detection between firmware 2.1.1.7 (N101)
974 * and later 2.1.1.21 is not very clear from datasheets.
975 * I hope that the min value is -15360 for newer firmware --jk
976 */
977 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
978 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100979 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100980 "set volume quirk for UDA1321/N101 chip\n");
981 cval->max = -256;
982 }
983 break;
984
985 case USB_ID(0x046d, 0x09a4):
986 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100987 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100988 "set volume quirk for QuickCam E3500\n");
989 cval->min = 6080;
990 cval->max = 8768;
991 cval->res = 192;
992 }
993 break;
994
Takashi Iwaie805ca82014-03-05 12:34:39 +0100995 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100996 case USB_ID(0x046d, 0x0808):
997 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -0500998 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +0200999 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001000 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001001 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001002 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001003 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001004 case USB_ID(0x046d, 0x0991):
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001005 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001006 /* Most audio usb devices lie about volume resolution.
1007 * Most Logitech webcams have res = 384.
Con Kolivas82ffb6f2016-12-09 15:15:57 +11001008 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001009 */
1010 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001011 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001012 "set resolution quirk: cval->res = 384\n");
1013 cval->res = 384;
1014 }
1015 break;
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001016 }
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * retrieve the minimum and maximum values for the specified control
1021 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001022static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1023 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 /* for failsafe */
1026 cval->min = default_min;
1027 cval->max = cval->min + 1;
1028 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001029 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (cval->val_type == USB_MIXER_BOOLEAN ||
1032 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1033 cval->initialized = 1;
1034 } else {
1035 int minchn = 0;
1036 if (cval->cmask) {
1037 int i;
1038 for (i = 0; i < MAX_CHANNELS; i++)
1039 if (cval->cmask & (1 << i)) {
1040 minchn = i + 1;
1041 break;
1042 }
1043 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001044 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1045 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001046 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001047 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001048 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1049 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -EINVAL;
1051 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001052 if (get_ctl_value(cval, UAC_GET_RES,
1053 (cval->control << 8) | minchn,
1054 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 cval->res = 1;
1056 } else {
1057 int last_valid_res = cval->res;
1058
1059 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001060 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001061 (cval->control << 8) | minchn,
1062 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
1064 cval->res /= 2;
1065 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001066 if (get_ctl_value(cval, UAC_GET_RES,
1067 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 cval->res = last_valid_res;
1069 }
1070 if (cval->res == 0)
1071 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001072
1073 /* Additional checks for the proper resolution
1074 *
1075 * Some devices report smaller resolutions than actually
1076 * reacting. They don't return errors but simply clip
1077 * to the lower aligned value.
1078 */
1079 if (cval->min + cval->res < cval->max) {
1080 int last_valid_res = cval->res;
1081 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001082 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001083 for (;;) {
1084 test = saved;
1085 if (test < cval->max)
1086 test += cval->res;
1087 else
1088 test -= cval->res;
1089 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001090 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001091 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001092 cval->res = last_valid_res;
1093 break;
1094 }
1095 if (test == check)
1096 break;
1097 cval->res *= 2;
1098 }
Chris J Argeseef90452014-11-12 12:07:01 -06001099 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 cval->initialized = 1;
1103 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001104
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001105 if (kctl)
1106 volume_control_quirks(cval, kctl);
1107
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001108 /* USB descriptions contain the dB scale in 1/256 dB unit
1109 * while ALSA TLV contains in 1/100 dB unit
1110 */
1111 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1112 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1113 if (cval->dBmin > cval->dBmax) {
1114 /* something is wrong; assume it's either from/to 0dB */
1115 if (cval->dBmin < 0)
1116 cval->dBmax = 0;
1117 else if (cval->dBmin > 0)
1118 cval->dBmin = 0;
1119 if (cval->dBmin > cval->dBmax) {
1120 /* totally crap, return an error */
1121 return -EINVAL;
1122 }
1123 }
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126}
1127
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001128#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001131static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1132 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001134 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (cval->val_type == USB_MIXER_BOOLEAN ||
1137 cval->val_type == USB_MIXER_INV_BOOLEAN)
1138 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1139 else
1140 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1141 uinfo->count = cval->channels;
1142 if (cval->val_type == USB_MIXER_BOOLEAN ||
1143 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1144 uinfo->value.integer.min = 0;
1145 uinfo->value.integer.max = 1;
1146 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001147 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001148 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001149 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1150 kcontrol->vd[0].access &=
1151 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1152 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001153 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001154 SNDRV_CTL_EVENT_MASK_INFO,
1155 &kcontrol->id);
1156 }
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001159 uinfo->value.integer.max =
1160 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 return 0;
1163}
1164
1165/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001166static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1167 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001169 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int c, cnt, val, err;
1171
Takashi Iwai641b4872009-01-15 17:05:24 +01001172 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (cval->cmask) {
1174 cnt = 0;
1175 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001176 if (!(cval->cmask & (1 << c)))
1177 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001178 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001179 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001180 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001181 val = get_relative_value(cval, val);
1182 ucontrol->value.integer.value[cnt] = val;
1183 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else {
1187 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001188 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001189 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001190 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 val = get_relative_value(cval, val);
1192 ucontrol->value.integer.value[0] = val;
1193 }
1194 return 0;
1195}
1196
1197/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001198static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1199 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001201 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int c, cnt, val, oval, err;
1203 int changed = 0;
1204
1205 if (cval->cmask) {
1206 cnt = 0;
1207 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001208 if (!(cval->cmask & (1 << c)))
1209 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001210 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001211 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001212 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001213 val = ucontrol->value.integer.value[cnt];
1214 val = get_abs_value(cval, val);
1215 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001216 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001217 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001219 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 } else {
1222 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001223 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001225 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 val = ucontrol->value.integer.value[0];
1227 val = get_abs_value(cval, val);
1228 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001229 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 changed = 1;
1231 }
1232 }
1233 return changed;
1234}
1235
Andrew Chant568fa7e2018-03-23 19:25:24 -07001236/* get the boolean value from the master channel of a UAC control */
1237static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1238 struct snd_ctl_elem_value *ucontrol)
Andrew Chant5a222e82018-03-23 19:25:23 -07001239{
1240 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1241 int val, err;
1242
1243 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1244 if (err < 0)
1245 return filter_error(cval, err);
1246 val = (val != 0);
1247 ucontrol->value.integer.value[0] = val;
1248 return 0;
1249}
1250
Takashi Iwai86e07d32005-11-17 15:08:02 +01001251static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1253 .name = "", /* will be filled later manually */
1254 .info = mixer_ctl_feature_info,
1255 .get = mixer_ctl_feature_get,
1256 .put = mixer_ctl_feature_put,
1257};
1258
Daniel Mack23caaf12010-03-11 21:13:25 +01001259/* the read-only variant */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301260static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
Daniel Mack23caaf12010-03-11 21:13:25 +01001261 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1262 .name = "", /* will be filled later manually */
1263 .info = mixer_ctl_feature_info,
1264 .get = mixer_ctl_feature_get,
1265 .put = NULL,
1266};
1267
Andrew Chant568fa7e2018-03-23 19:25:24 -07001268/*
1269 * A control which shows the boolean value from reading a UAC control on
1270 * the master channel.
1271 */
1272static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
Andrew Chant5a222e82018-03-23 19:25:23 -07001273 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1274 .name = "", /* will be filled later manually */
1275 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1276 .info = snd_ctl_boolean_mono_info,
Andrew Chant568fa7e2018-03-23 19:25:24 -07001277 .get = mixer_ctl_master_bool_get,
Andrew Chant5a222e82018-03-23 19:25:23 -07001278 .put = NULL,
1279};
1280
Daniel Mack6bc170e2014-05-24 10:58:16 +02001281/*
1282 * This symbol is exported in order to allow the mixer quirks to
1283 * hook up to the standard feature unit control mechanism
1284 */
Daniel Mack9e386582011-05-25 09:09:01 +02001285struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287/*
1288 * build a feature control
1289 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001290static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1291{
1292 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1293}
1294
Daniel Mack6bc170e2014-05-24 10:58:16 +02001295/*
1296 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1297 * rename it to "Headphone". We determine if something is a headphone
1298 * similar to how udev determines form factor.
1299 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001300static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1301 struct snd_card *card)
1302{
1303 const char *names_to_check[] = {
1304 "Headset", "headset", "Headphone", "headphone", NULL};
1305 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001306 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001307
1308 if (strcmp("Speaker", kctl->id.name))
1309 return;
1310
1311 for (s = names_to_check; *s; s++)
1312 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001313 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001314 break;
1315 }
1316
1317 if (!found)
1318 return;
1319
1320 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1321}
1322
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001323static struct usb_feature_control_info *get_feature_control_info(int control)
1324{
1325 int i;
1326
1327 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1328 if (audio_feature_info[i].control == control)
1329 return &audio_feature_info[i];
1330 }
1331 return NULL;
1332}
1333
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001334static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1335 const struct usbmix_name_map *imap,
1336 unsigned int ctl_mask, int control,
1337 struct usb_audio_term *iterm,
1338 struct usb_audio_term *oterm,
1339 int unitid, int nameid, int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Julian Scheelbc18e312015-08-14 16:14:45 +02001341 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 unsigned int len = 0;
1343 int mapped_name = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001344 struct snd_kcontrol *kctl;
1345 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001346 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001347 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Daniel Mack65f25da2010-05-31 13:35:41 +02001349 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* FIXME: not supported yet */
1351 return;
1352 }
1353
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001354 map = find_map(imap, unitid, control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001355 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return;
1357
Takashi Iwai561b2202005-09-09 14:22:34 +02001358 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001359 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001361 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 cval->control = control;
1363 cval->cmask = ctl_mask;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001364
1365 ctl_info = get_feature_control_info(control);
Colin Ian King62376022018-03-27 15:30:01 +01001366 if (!ctl_info) {
1367 kfree(cval);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001368 return;
Colin Ian King62376022018-03-27 15:30:01 +01001369 }
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001370 if (mixer->protocol == UAC_VERSION_1)
Julian Scheelbc18e312015-08-14 16:14:45 +02001371 cval->val_type = ctl_info->type;
1372 else /* UAC_VERSION_2 */
1373 cval->val_type = ctl_info->type_uac2 >= 0 ?
1374 ctl_info->type_uac2 : ctl_info->type;
1375
Daniel Macka6a33252010-05-31 13:35:37 +02001376 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001378 cval->master_readonly = readonly_mask;
1379 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int i, c = 0;
1381 for (i = 0; i < 16; i++)
1382 if (ctl_mask & (1 << i))
1383 c++;
1384 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001385 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Daniel Mack6bc170e2014-05-24 10:58:16 +02001388 /*
1389 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001390 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001391 * issue write commands to read-only channels.
1392 */
Daniel Macka6a33252010-05-31 13:35:37 +02001393 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001394 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1395 else
1396 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1397
Daniel Mack6bc170e2014-05-24 10:58:16 +02001398 if (!kctl) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001399 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 kfree(cval);
1401 return;
1402 }
Chris J Argeseef90452014-11-12 12:07:01 -06001403 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001405 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001407 if (!len && nameid)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001408 len = snd_usb_copy_string_desc(mixer->chip, nameid,
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001409 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001412 case UAC_FU_MUTE:
1413 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001414 /*
1415 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * - if a name id is given in descriptor, use it.
1417 * - if the connected input can be determined, then use the name
1418 * of terminal type.
1419 * - if the connected output can be determined, use it.
1420 * - otherwise, anonymous name.
1421 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001422 if (!len) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001423 if (iterm)
1424 len = get_term_name(mixer->chip, iterm,
1425 kctl->id.name,
1426 sizeof(kctl->id.name), 1);
1427 if (!len && oterm)
1428 len = get_term_name(mixer->chip, oterm,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001429 kctl->id.name,
1430 sizeof(kctl->id.name), 1);
1431 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001432 snprintf(kctl->id.name, sizeof(kctl->id.name),
1433 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001435
1436 if (!mapped_name)
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001437 check_no_speaker_on_headset(kctl, mixer->chip->card);
David Henningsson9b4ef972012-11-23 13:48:55 +01001438
Daniel Mack6bc170e2014-05-24 10:58:16 +02001439 /*
1440 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * if the connected output is USB stream, then it's likely a
1442 * capture stream. otherwise it should be playback (hopefully :)
1443 */
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001444 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1445 if ((oterm->type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001446 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001447 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001448 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001450 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001451 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001454 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1456 sizeof(kctl->id.name));
1457 break;
1458 }
1459
Takashi Iwaie1825342012-05-14 17:11:06 +02001460 /* get min/max values */
1461 get_min_max_with_quirks(cval, 0, kctl);
1462
1463 if (control == UAC_FU_VOLUME) {
1464 check_mapped_dB(map, cval);
1465 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1466 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1467 kctl->vd[0].access |=
1468 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1469 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1470 }
1471 }
1472
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001473 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02001474
Alexey Fisher80aceff2011-03-10 14:53:38 +01001475 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001476 /*
1477 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001478 * to be sure. 384 is a resolution magic number found on Logitech
1479 * devices. It will definitively catch all buggy Logitech devices.
1480 */
1481 if (range > 384) {
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001482 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001483 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001484 range);
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001485 usb_audio_warn(mixer->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001486 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001487 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001488 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001489 }
1490
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001491 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001492 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001493 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001494 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001497static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1498 unsigned int ctl_mask, int control,
1499 struct usb_audio_term *iterm, int unitid,
1500 int readonly_mask)
1501{
1502 struct uac_feature_unit_descriptor *desc = raw_desc;
1503 int nameid = uac_feature_unit_iFeature(desc);
1504
1505 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1506 iterm, &state->oterm, unitid, nameid, readonly_mask);
1507}
1508
1509static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1510 unsigned int ctl_mask, int control, int unitid,
1511 const struct usbmix_name_map *badd_map)
1512{
1513 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1514 NULL, NULL, unitid, 0, 0);
1515}
1516
Andrew Chant5a222e82018-03-23 19:25:23 -07001517static void get_connector_control_name(struct mixer_build *state,
1518 struct usb_audio_term *term,
1519 bool is_input, char *name, int name_size)
1520{
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001521 int name_len = get_term_name(state->chip, term, name, name_size, 0);
Andrew Chant5a222e82018-03-23 19:25:23 -07001522
1523 if (name_len == 0)
1524 strlcpy(name, "Unknown", name_size);
1525
1526 /*
1527 * sound/core/ctljack.c has a convention of naming jack controls
1528 * by ending in " Jack". Make it slightly more useful by
1529 * indicating Input or Output after the terminal name.
1530 */
1531 if (is_input)
1532 strlcat(name, " - Input Jack", name_size);
1533 else
1534 strlcat(name, " - Output Jack", name_size);
1535}
1536
1537/* Build a mixer control for a UAC connector control (jack-detect) */
1538static void build_connector_control(struct mixer_build *state,
1539 struct usb_audio_term *term, bool is_input)
1540{
1541 struct snd_kcontrol *kctl;
1542 struct usb_mixer_elem_info *cval;
1543
1544 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1545 if (!cval)
1546 return;
1547 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, term->id);
Andrew Chant568fa7e2018-03-23 19:25:24 -07001548 /*
1549 * The first byte from reading the UAC2_TE_CONNECTOR control returns the
1550 * number of channels connected. This boolean ctl will simply report
1551 * if any channels are connected or not.
1552 * (Audio20_final.pdf Table 5-10: Connector Control CUR Parameter Block)
1553 */
Andrew Chant5a222e82018-03-23 19:25:23 -07001554 cval->control = UAC2_TE_CONNECTOR;
1555 cval->val_type = USB_MIXER_BOOLEAN;
1556 cval->channels = 1; /* report true if any channel is connected */
1557 cval->min = 0;
1558 cval->max = 1;
Andrew Chant568fa7e2018-03-23 19:25:24 -07001559 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Andrew Chant5a222e82018-03-23 19:25:23 -07001560 if (!kctl) {
1561 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1562 kfree(cval);
1563 return;
1564 }
1565 get_connector_control_name(state, term, is_input, kctl->id.name,
1566 sizeof(kctl->id.name));
1567 kctl->private_free = snd_usb_mixer_elem_free;
1568 snd_usb_mixer_add_control(&cval->head, kctl);
1569}
1570
Daniel Mackcddaafb2016-04-09 11:21:46 +02001571static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1572 void *_ftr)
1573{
1574 struct uac_clock_source_descriptor *hdr = _ftr;
1575 struct usb_mixer_elem_info *cval;
1576 struct snd_kcontrol *kctl;
1577 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1578 int ret;
1579
1580 if (state->mixer->protocol != UAC_VERSION_2)
1581 return -EINVAL;
1582
1583 if (hdr->bLength != sizeof(*hdr)) {
1584 usb_audio_dbg(state->chip,
1585 "Bogus clock source descriptor length of %d, ignoring.\n",
1586 hdr->bLength);
1587 return 0;
1588 }
1589
1590 /*
1591 * The only property of this unit we are interested in is the
1592 * clock source validity. If that isn't readable, just bail out.
1593 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001594 if (!uac_v2v3_control_is_readable(hdr->bmControls,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001595 UAC2_CS_CONTROL_CLOCK_VALID))
Daniel Mackcddaafb2016-04-09 11:21:46 +02001596 return 0;
1597
1598 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1599 if (!cval)
1600 return -ENOMEM;
1601
1602 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1603
1604 cval->min = 0;
1605 cval->max = 1;
1606 cval->channels = 1;
1607 cval->val_type = USB_MIXER_BOOLEAN;
1608 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1609
Andrew Chant568fa7e2018-03-23 19:25:24 -07001610 cval->master_readonly = 1;
1611 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1612 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
Daniel Mackcddaafb2016-04-09 11:21:46 +02001613
1614 if (!kctl) {
1615 kfree(cval);
1616 return -ENOMEM;
1617 }
1618
1619 kctl->private_free = snd_usb_mixer_elem_free;
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001620 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
Daniel Mackcddaafb2016-04-09 11:21:46 +02001621 name, sizeof(name));
1622 if (ret > 0)
1623 snprintf(kctl->id.name, sizeof(kctl->id.name),
1624 "%s Validity", name);
1625 else
1626 snprintf(kctl->id.name, sizeof(kctl->id.name),
1627 "Clock Source %d Validity", hdr->bClockID);
1628
1629 return snd_usb_mixer_add_control(&cval->head, kctl);
1630}
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
1633 * parse a feature unit
1634 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001635 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001637static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1638 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001641 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 unsigned int master_bits, first_ch_bits;
1643 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001644 struct uac_feature_unit_descriptor *hdr = _ftr;
1645 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Daniel Mack23caaf12010-03-11 21:13:25 +01001647 if (state->mixer->protocol == UAC_VERSION_1) {
Takashi Iwaid937cd62017-11-21 16:55:51 +01001648 if (hdr->bLength < 7) {
1649 usb_audio_err(state->chip,
1650 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1651 unitid);
1652 return -EINVAL;
1653 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001654 csize = hdr->bControlSize;
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001655 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001656 usb_audio_dbg(state->chip,
Takashi Iwai3c02a6d2017-11-27 10:59:40 +01001657 "unit %u: invalid bControlSize == 0\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001658 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001659 return -EINVAL;
1660 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001661 channels = (hdr->bLength - 7) / csize - 1;
1662 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001663 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001664 usb_audio_err(state->chip,
1665 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1666 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001667 return -EINVAL;
1668 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001669 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001670 struct uac2_feature_unit_descriptor *ftr = _ftr;
Takashi Iwaid937cd62017-11-21 16:55:51 +01001671 if (hdr->bLength < 6) {
1672 usb_audio_err(state->chip,
1673 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1674 unitid);
1675 return -EINVAL;
1676 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001677 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001678 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001679 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001680 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001681 usb_audio_err(state->chip,
1682 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1683 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001684 return -EINVAL;
1685 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001686 } else { /* UAC_VERSION_3 */
1687 struct uac3_feature_unit_descriptor *ftr = _ftr;
1688
1689 if (hdr->bLength < 7) {
1690 usb_audio_err(state->chip,
1691 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1692 unitid);
1693 return -EINVAL;
1694 }
1695 csize = 4;
1696 channels = (ftr->bLength - 7) / 4 - 1;
1697 bmaControls = ftr->bmaControls;
1698 if (hdr->bLength < 7 + csize) {
1699 usb_audio_err(state->chip,
1700 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1701 unitid);
1702 return -EINVAL;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
1706 /* parse the source unit */
Daniel Mack23caaf12010-03-11 21:13:25 +01001707 if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return err;
1709
1710 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001711 err = check_input_term(state, hdr->bSourceID, &iterm);
1712 if (err < 0)
1713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Daniel Mack23caaf12010-03-11 21:13:25 +01001715 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001716 /* master configuration quirks */
1717 switch (state->chip->usb_id) {
1718 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001719 usb_audio_info(state->chip,
1720 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001721 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001722 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001723 break;
David Henningssonc1051432012-09-20 10:20:41 +02001724 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001725 usb_audio_info(state->chip,
1726 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001727 /* disable non-functional volume control */
1728 channels = 0;
1729 break;
1730
Javier Kohen0c3cee52009-11-17 15:36:13 +01001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001733 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 else
1735 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001736
1737 if (state->mixer->protocol == UAC_VERSION_1) {
1738 /* check all control types */
1739 for (i = 0; i < 10; i++) {
1740 unsigned int ch_bits = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001741 int control = audio_feature_info[i].control;
1742
Daniel Mack23caaf12010-03-11 21:13:25 +01001743 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001744 unsigned int mask;
1745
1746 mask = snd_usb_combine_bytes(bmaControls +
1747 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001748 if (mask & (1 << i))
1749 ch_bits |= (1 << j);
1750 }
1751 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001752
1753 /*
1754 * The first channel must be set
1755 * (for ease of programming).
1756 */
1757 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001758 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001759 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001760 if (master_bits & (1 << i))
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001761 build_feature_ctl(state, _ftr, 0, control,
1762 &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02001764 } else { /* UAC_VERSION_2/3 */
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001765 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001766 unsigned int ch_bits = 0;
1767 unsigned int ch_read_only = 0;
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001768 int control = audio_feature_info[i].control;
Daniel Mack23caaf12010-03-11 21:13:25 +01001769
1770 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001771 unsigned int mask;
1772
1773 mask = snd_usb_combine_bytes(bmaControls +
1774 csize * (j+1), csize);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001775 if (uac_v2v3_control_is_readable(mask, control)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001776 ch_bits |= (1 << j);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001777 if (!uac_v2v3_control_is_writeable(mask, control))
Daniel Mack23caaf12010-03-11 21:13:25 +01001778 ch_read_only |= (1 << j);
1779 }
1780 }
1781
Daniel Mack6bc170e2014-05-24 10:58:16 +02001782 /*
1783 * NOTE: build_feature_ctl() will mark the control
1784 * read-only if all channels are marked read-only in
1785 * the descriptors. Otherwise, the control will be
1786 * reported as writeable, but the driver will not
1787 * actually issue a write command for read-only
1788 * channels.
1789 */
1790
1791 /*
1792 * The first channel must be set
1793 * (for ease of programming).
1794 */
1795 if (ch_bits & 1)
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001796 build_feature_ctl(state, _ftr, ch_bits, control,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001797 &iterm, unitid, ch_read_only);
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001798 if (uac_v2v3_control_is_readable(master_bits, control))
Takashi Iwai2de841e2018-04-23 08:59:36 +02001799 build_feature_ctl(state, _ftr, 0, control,
1800 &iterm, unitid,
Andrew Chant21e9b3e2018-03-22 14:39:55 -07001801 !uac_v2v3_control_is_writeable(master_bits,
1802 control));
Daniel Mack23caaf12010-03-11 21:13:25 +01001803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805
1806 return 0;
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/*
1810 * Mixer Unit
1811 */
1812
1813/*
1814 * build a mixer unit control
1815 *
1816 * the callbacks are identical with feature unit.
1817 * input channel number (zero based) is given in control field instead.
1818 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001819static void build_mixer_unit_ctl(struct mixer_build *state,
1820 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001822 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001824 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001825 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001827 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001828 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Ruslan Bilovol17156f22018-05-04 04:24:04 +03001830 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001831 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return;
1833
Takashi Iwai561b2202005-09-09 14:22:34 +02001834 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001835 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return;
1837
Takashi Iwai3360b842014-11-18 11:47:04 +01001838 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 cval->control = in_ch + 1; /* based on 1 */
1840 cval->val_type = USB_MIXER_S16;
1841 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001842 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1843
1844 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 cval->cmask |= (1 << i);
1846 cval->channels++;
1847 }
1848 }
1849
1850 /* get min/max values */
1851 get_min_max(cval, 0);
1852
1853 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001854 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001855 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 kfree(cval);
1857 return;
1858 }
Chris J Argeseef90452014-11-12 12:07:01 -06001859 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001861 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02001862 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03001863 len = get_term_name(state->chip, iterm, kctl->id.name,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001864 sizeof(kctl->id.name), 0);
1865 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001867 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001869 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001870 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
1871 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Andrew Chant5a222e82018-03-23 19:25:23 -07001874static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
1875 void *raw_desc)
1876{
1877 struct usb_audio_term iterm;
1878 struct uac2_input_terminal_descriptor *d = raw_desc;
1879
1880 check_input_term(state, d->bTerminalID, &iterm);
1881 if (state->mixer->protocol == UAC_VERSION_2) {
1882 /* Check for jack detection. */
Takashi Iwai2b54f7852018-04-23 15:19:25 +02001883 if (uac_v2v3_control_is_readable(le16_to_cpu(d->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07001884 UAC2_TE_CONNECTOR)) {
1885 build_connector_control(state, &iterm, true);
1886 }
1887 }
1888 return 0;
1889}
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891/*
1892 * parse a mixer unit
1893 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001894static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
1895 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Daniel Mack99fc8642010-03-11 21:13:24 +01001897 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001898 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 int input_pins, num_ins, num_outs;
1900 int pin, ich, err;
1901
Daniel Mack6bc170e2014-05-24 10:58:16 +02001902 if (desc->bLength < 11 || !(input_pins = desc->bNrInPins) ||
1903 !(num_outs = uac_mixer_unit_bNrChannels(desc))) {
1904 usb_audio_err(state->chip,
1905 "invalid MIXER UNIT descriptor %d\n",
1906 unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return -EINVAL;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 num_ins = 0;
1911 ich = 0;
1912 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001913 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01001915 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02001916 /* no bmControls field (e.g. Maya44) -> ignore */
1917 if (desc->bLength <= 10 + input_pins)
1918 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01001919 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (err < 0)
1921 return err;
1922 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001923 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int och, ich_has_controls = 0;
1925
Daniel Mack6bc170e2014-05-24 10:58:16 +02001926 for (och = 0; och < num_outs; och++) {
1927 __u8 *c = uac_mixer_unit_bmControls(desc,
1928 state->mixer->protocol);
1929
1930 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ich_has_controls = 1;
1932 break;
1933 }
1934 }
1935 if (ich_has_controls)
1936 build_mixer_unit_ctl(state, desc, pin, ich,
1937 unitid, &iterm);
1938 }
1939 }
1940 return 0;
1941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/*
1944 * Processing Unit / Extension Unit
1945 */
1946
1947/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001948static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001951 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int err, val;
1953
1954 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01001955 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01001957 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 val = get_relative_value(cval, val);
1960 ucontrol->value.integer.value[0] = val;
1961 return 0;
1962}
1963
1964/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001965static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
1966 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001968 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 int val, oval, err;
1970
1971 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01001972 if (err < 0)
1973 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 val = ucontrol->value.integer.value[0];
1975 val = get_abs_value(cval, val);
1976 if (val != oval) {
1977 set_cur_ctl_value(cval, cval->control << 8, val);
1978 return 1;
1979 }
1980 return 0;
1981}
1982
1983/* alsa control interface for processing/extension unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05301984static const struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1986 .name = "", /* will be filled later */
1987 .info = mixer_ctl_feature_info,
1988 .get = mixer_ctl_procunit_get,
1989 .put = mixer_ctl_procunit_put,
1990};
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/*
1993 * predefined data for processing units
1994 */
1995struct procunit_value_info {
1996 int control;
1997 char *suffix;
1998 int val_type;
1999 int min_value;
2000};
2001
2002struct procunit_info {
2003 int type;
2004 char *name;
2005 struct procunit_value_info *values;
2006};
2007
2008static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002009 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2010 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 { 0 }
2012};
2013static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002014 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2015 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 { 0 }
2017};
2018static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002019 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2020 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 { 0 }
2022};
2023static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002024 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2025 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2026 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2027 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 { 0 }
2029};
2030static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002031 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2032 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2033 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2034 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 { 0 }
2036};
2037static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002038 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2039 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2040 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2041 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2042 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2043 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 { 0 }
2045};
2046
2047static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002048 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2049 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2050 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2051 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2052 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2053 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 { 0 },
2055};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002056/*
2057 * predefined data for extension units
2058 */
2059static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002060 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2061 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002062};
2063static struct procunit_value_info clock_source_xu_info[] = {
2064 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2065 { 0 }
2066};
2067static struct procunit_value_info spdif_format_xu_info[] = {
2068 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2069 { 0 }
2070};
2071static struct procunit_value_info soft_limit_xu_info[] = {
2072 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2073 { 0 }
2074};
2075static struct procunit_info extunits[] = {
2076 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2077 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2078 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2079 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2080 { 0 }
2081};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * build a processing/extension unit
2085 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002086static int build_audio_procunit(struct mixer_build *state, int unitid,
2087 void *raw_desc, struct procunit_info *list,
2088 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Daniel Mack99fc8642010-03-11 21:13:24 +01002090 struct uac_processing_unit_descriptor *desc = raw_desc;
2091 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002092 struct usb_mixer_elem_info *cval;
2093 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int i, err, nameid, type, len;
2095 struct procunit_info *info;
2096 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002097 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 static struct procunit_value_info default_value_info[] = {
2099 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2100 { 0 }
2101 };
2102 static struct procunit_info default_info = {
2103 0, NULL, default_value_info
2104 };
2105
Daniel Mack23caaf12010-03-11 21:13:25 +01002106 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2107 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002108 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return -EINVAL;
2110 }
2111
2112 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002113 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return err;
2115 }
2116
Daniel Mack99fc8642010-03-11 21:13:24 +01002117 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 for (info = list; info && info->type; info++)
2119 if (info->type == type)
2120 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002121 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 info = &default_info;
2123
2124 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002125 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002126
Daniel Mack6bc170e2014-05-24 10:58:16 +02002127 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 continue;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002129 map = find_map(state->map, unitid, valinfo->control);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002130 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002132 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002133 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002135 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 cval->control = valinfo->control;
2137 cval->val_type = valinfo->val_type;
2138 cval->channels = 1;
2139
2140 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002141 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002142 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /* FIXME: hard-coded */
2144 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002145 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 cval->res = 1;
2147 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002148 } else {
2149 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002150 /*
2151 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002152 * samplerate control quirk
2153 */
2154 cval->min = 0;
2155 cval->max = 5;
2156 cval->res = 1;
2157 cval->initialized = 1;
2158 } else
2159 get_min_max(cval, valinfo->min_value);
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002163 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 kfree(cval);
2165 return -ENOMEM;
2166 }
Chris J Argeseef90452014-11-12 12:07:01 -06002167 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Daniel Mack6bc170e2014-05-24 10:58:16 +02002169 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002170 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002171 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002173 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002174 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 len = 0;
2176 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002177 len = snd_usb_copy_string_desc(state->chip,
2178 nameid,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002179 kctl->id.name,
2180 sizeof(kctl->id.name));
2181 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2183 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002184 append_ctl_name(kctl, " ");
2185 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002187 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002188 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002189 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002190 cval->min, cval->max);
2191
Takashi Iwai3360b842014-11-18 11:47:04 +01002192 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002193 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return err;
2195 }
2196 return 0;
2197}
2198
Daniel Mack6bc170e2014-05-24 10:58:16 +02002199static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2200 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002202 return build_audio_procunit(state, unitid, raw_desc,
2203 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Daniel Mack6bc170e2014-05-24 10:58:16 +02002206static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2207 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002209 /*
2210 * Note that we parse extension units with processing unit descriptors.
2211 * That's ok as the layout is the same.
2212 */
2213 return build_audio_procunit(state, unitid, raw_desc,
2214 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217/*
2218 * Selector Unit
2219 */
2220
Daniel Mack6bc170e2014-05-24 10:58:16 +02002221/*
2222 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 * use an enumerator type for routing
2224 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002225static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002228 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002229 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Takashi Iwai5e246b82008-08-08 17:12:47 +02002231 if (snd_BUG_ON(!itemlist))
2232 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002233 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
2236/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002237static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2238 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002240 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int val, err;
2242
Daniel Mack09414202010-05-31 13:35:44 +02002243 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002245 ucontrol->value.enumerated.item[0] = 0;
2246 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 val = get_relative_value(cval, val);
2249 ucontrol->value.enumerated.item[0] = val;
2250 return 0;
2251}
2252
2253/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002254static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002257 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 int val, oval, err;
2259
Daniel Mack09414202010-05-31 13:35:44 +02002260 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002261 if (err < 0)
2262 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 val = ucontrol->value.enumerated.item[0];
2264 val = get_abs_value(cval, val);
2265 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002266 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 1;
2268 }
2269 return 0;
2270}
2271
2272/* alsa control interface for selector unit */
Bhumika Goyal8fdaebb2017-04-12 18:38:06 +05302273static const struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2275 .name = "", /* will be filled later */
2276 .info = mixer_ctl_selector_info,
2277 .get = mixer_ctl_selector_get,
2278 .put = mixer_ctl_selector_put,
2279};
2280
Daniel Mack6bc170e2014-05-24 10:58:16 +02002281/*
2282 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * free both private_data and private_value
2284 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002285static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 int i, num_ins = 0;
2288
2289 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002290 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 num_ins = cval->max;
2292 kfree(cval);
2293 kctl->private_data = NULL;
2294 }
2295 if (kctl->private_value) {
2296 char **itemlist = (char **)kctl->private_value;
2297 for (i = 0; i < num_ins; i++)
2298 kfree(itemlist[i]);
2299 kfree(itemlist);
2300 kctl->private_value = 0;
2301 }
2302}
2303
2304/*
2305 * parse a selector unit
2306 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002307static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2308 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Daniel Mack99fc8642010-03-11 21:13:24 +01002310 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 unsigned int i, nameid, len;
2312 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002313 struct usb_mixer_elem_info *cval;
2314 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002315 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 char **namelist;
2317
Takashi Iwaif658f172017-11-21 17:00:32 +01002318 if (desc->bLength < 5 || !desc->bNrInPins ||
2319 desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002320 usb_audio_err(state->chip,
2321 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return -EINVAL;
2323 }
2324
Daniel Mack99fc8642010-03-11 21:13:24 +01002325 for (i = 0; i < desc->bNrInPins; i++) {
2326 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return err;
2328 }
2329
Daniel Mack99fc8642010-03-11 21:13:24 +01002330 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
2332
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002333 map = find_map(state->map, unitid, 0);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002334 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return 0;
2336
Takashi Iwai561b2202005-09-09 14:22:34 +02002337 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002338 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002340 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 cval->val_type = USB_MIXER_U8;
2342 cval->channels = 1;
2343 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002344 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 cval->res = 1;
2346 cval->initialized = 1;
2347
Johan Rasténa358a0e2014-11-17 08:39:33 +01002348 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002349 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002350 else /* UAC_VERSION_2 */
2351 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2352 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002353
Daniel Mack99fc8642010-03-11 21:13:24 +01002354 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002355 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 kfree(cval);
2357 return -ENOMEM;
2358 }
2359#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002360 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002361 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 len = 0;
2363 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002364 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002365 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 kfree(namelist[i]);
2367 kfree(namelist);
2368 kfree(cval);
2369 return -ENOMEM;
2370 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002371 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2372 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002373 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002374 len = get_term_name(state->chip, &iterm, namelist[i],
2375 MAX_ITEM_NAME_LEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002377 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
2380 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2381 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002382 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002383 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 kfree(cval);
2385 return -ENOMEM;
2386 }
2387 kctl->private_value = (unsigned long)namelist;
2388 kctl->private_free = usb_mixer_selector_elem_free;
2389
Takashi Iwai5a15f282017-12-18 23:36:57 +01002390 /* check the static mapping table at first */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002391 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Jaejoong Kim89b89d12017-12-04 15:31:49 +09002392 if (!len) {
Takashi Iwai5a15f282017-12-18 23:36:57 +01002393 /* no mapping ? */
2394 /* if iSelector is given, use it */
2395 nameid = uac_selector_unit_iSelector(desc);
2396 if (nameid)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002397 len = snd_usb_copy_string_desc(state->chip, nameid,
Takashi Iwai5a15f282017-12-18 23:36:57 +01002398 kctl->id.name,
2399 sizeof(kctl->id.name));
2400 /* ... or pick up the terminal name at next */
2401 if (!len)
Ruslan Bilovoleccfc1b2018-05-04 04:24:02 +03002402 len = get_term_name(state->chip, &state->oterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 kctl->id.name, sizeof(kctl->id.name), 0);
Takashi Iwai5a15f282017-12-18 23:36:57 +01002404 /* ... or use the fixed string "USB" as the last resort */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002405 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2407
Takashi Iwai5a15f282017-12-18 23:36:57 +01002408 /* and add the proper suffix */
Daniel Mack09414202010-05-31 13:35:44 +02002409 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2410 append_ctl_name(kctl, " Clock Source");
2411 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002412 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002414 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002417 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002418 cval->head.id, kctl->id.name, desc->bNrInPins);
2419 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422/*
2423 * parse an audio unit recursively
2424 */
2425
Takashi Iwai86e07d32005-11-17 15:08:02 +01002426static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 unsigned char *p1;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002429 int protocol = state->mixer->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 if (test_and_set_bit(unitid, state->unitbitmap))
2432 return 0; /* the unit already visited */
2433
2434 p1 = find_audio_control_unit(state, unitid);
2435 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002436 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return -EINVAL;
2438 }
2439
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002440 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2441 switch (p1[2]) {
2442 case UAC_INPUT_TERMINAL:
Andrew Chant5a222e82018-03-23 19:25:23 -07002443 return parse_audio_input_terminal(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002444 case UAC_MIXER_UNIT:
2445 return parse_audio_mixer_unit(state, unitid, p1);
2446 case UAC2_CLOCK_SOURCE:
2447 return parse_clock_source_unit(state, unitid, p1);
2448 case UAC_SELECTOR_UNIT:
2449 case UAC2_CLOCK_SELECTOR:
2450 return parse_audio_selector_unit(state, unitid, p1);
2451 case UAC_FEATURE_UNIT:
2452 return parse_audio_feature_unit(state, unitid, p1);
2453 case UAC1_PROCESSING_UNIT:
2454 /* UAC2_EFFECT_UNIT has the same value */
2455 if (protocol == UAC_VERSION_1)
2456 return parse_audio_processing_unit(state, unitid, p1);
2457 else
2458 return 0; /* FIXME - effect units not implemented yet */
2459 case UAC1_EXTENSION_UNIT:
2460 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2461 if (protocol == UAC_VERSION_1)
2462 return parse_audio_extension_unit(state, unitid, p1);
2463 else /* UAC_VERSION_2 */
2464 return parse_audio_processing_unit(state, unitid, p1);
2465 case UAC2_EXTENSION_UNIT_V2:
Daniel Mack23caaf12010-03-11 21:13:25 +01002466 return parse_audio_extension_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002467 default:
2468 usb_audio_err(state->chip,
2469 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2470 return -EINVAL;
2471 }
2472 } else { /* UAC_VERSION_3 */
2473 switch (p1[2]) {
2474 case UAC_INPUT_TERMINAL:
2475 return 0; /* NOP */
2476 case UAC3_MIXER_UNIT:
2477 return parse_audio_mixer_unit(state, unitid, p1);
2478 case UAC3_CLOCK_SOURCE:
2479 return parse_clock_source_unit(state, unitid, p1);
2480 case UAC3_CLOCK_SELECTOR:
2481 return parse_audio_selector_unit(state, unitid, p1);
2482 case UAC3_FEATURE_UNIT:
2483 return parse_audio_feature_unit(state, unitid, p1);
2484 case UAC3_EFFECT_UNIT:
2485 return 0; /* FIXME - effect units not implemented yet */
2486 case UAC3_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002487 return parse_audio_processing_unit(state, unitid, p1);
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002488 case UAC3_EXTENSION_UNIT:
2489 return parse_audio_extension_unit(state, unitid, p1);
2490 default:
2491 usb_audio_err(state->chip,
2492 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2493 return -EINVAL;
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496}
2497
Clemens Ladisch84957a82005-04-29 16:23:13 +02002498static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2499{
Takashi Iwai124751d2017-10-10 14:10:32 +02002500 /* kill pending URBs */
2501 snd_usb_mixer_disconnect(mixer);
2502
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002503 kfree(mixer->id_elems);
2504 if (mixer->urb) {
2505 kfree(mixer->urb->transfer_buffer);
2506 usb_free_urb(mixer->urb);
2507 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002508 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002509 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002510 kfree(mixer);
2511}
2512
Takashi Iwai86e07d32005-11-17 15:08:02 +01002513static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002514{
2515 struct usb_mixer_interface *mixer = device->device_data;
2516 snd_usb_mixer_free(mixer);
2517 return 0;
2518}
2519
Ruslan Bilovol17156f22018-05-04 04:24:04 +03002520/* UAC3 predefined channels configuration */
2521struct uac3_badd_profile {
2522 int subclass;
2523 const char *name;
2524 int c_chmask; /* capture channels mask */
2525 int p_chmask; /* playback channels mask */
2526 int st_chmask; /* side tone mixing channel mask */
2527};
2528
2529static struct uac3_badd_profile uac3_badd_profiles[] = {
2530 {
2531 /*
2532 * BAIF, BAOF or combination of both
2533 * IN: Mono or Stereo cfg, Mono alt possible
2534 * OUT: Mono or Stereo cfg, Mono alt possible
2535 */
2536 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2537 .name = "GENERIC IO",
2538 .c_chmask = -1, /* dynamic channels */
2539 .p_chmask = -1, /* dynamic channels */
2540 },
2541 {
2542 /* BAOF; Stereo only cfg, Mono alt possible */
2543 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2544 .name = "HEADPHONE",
2545 .p_chmask = 3,
2546 },
2547 {
2548 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2549 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2550 .name = "SPEAKER",
2551 .p_chmask = -1, /* dynamic channels */
2552 },
2553 {
2554 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2555 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2556 .name = "MICROPHONE",
2557 .c_chmask = -1, /* dynamic channels */
2558 },
2559 {
2560 /*
2561 * BAIOF topology
2562 * IN: Mono only
2563 * OUT: Mono or Stereo cfg, Mono alt possible
2564 */
2565 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2566 .name = "HEADSET",
2567 .c_chmask = 1,
2568 .p_chmask = -1, /* dynamic channels */
2569 .st_chmask = 1,
2570 },
2571 {
2572 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2573 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2574 .name = "HEADSET ADAPTER",
2575 .c_chmask = 1,
2576 .p_chmask = 3,
2577 .st_chmask = 1,
2578 },
2579 {
2580 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2581 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2582 .name = "SPEAKERPHONE",
2583 .c_chmask = 1,
2584 .p_chmask = 1,
2585 },
2586 { 0 } /* terminator */
2587};
2588
2589static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2590 struct uac3_badd_profile *f,
2591 int c_chmask, int p_chmask)
2592{
2593 /*
2594 * If both playback/capture channels are dynamic, make sure
2595 * at least one channel is present
2596 */
2597 if (f->c_chmask < 0 && f->p_chmask < 0) {
2598 if (!c_chmask && !p_chmask) {
2599 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2600 f->name);
2601 return false;
2602 }
2603 return true;
2604 }
2605
2606 if ((f->c_chmask < 0 && !c_chmask) ||
2607 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2608 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2609 f->name);
2610 return false;
2611 }
2612 if ((f->p_chmask < 0 && !p_chmask) ||
2613 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2614 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2615 f->name);
2616 return false;
2617 }
2618 return true;
2619}
2620
2621/*
2622 * create mixer controls for UAC3 BADD profiles
2623 *
2624 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2625 *
2626 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2627 */
2628static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2629 int ctrlif)
2630{
2631 struct usb_device *dev = mixer->chip->dev;
2632 struct usb_interface_assoc_descriptor *assoc;
2633 int badd_profile = mixer->chip->badd_profile;
2634 struct uac3_badd_profile *f;
2635 const struct usbmix_ctl_map *map;
2636 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2637 int i;
2638
2639 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2640
2641 /* Detect BADD capture/playback channels from AS EP descriptors */
2642 for (i = 0; i < assoc->bInterfaceCount; i++) {
2643 int intf = assoc->bFirstInterface + i;
2644
2645 struct usb_interface *iface;
2646 struct usb_host_interface *alts;
2647 struct usb_interface_descriptor *altsd;
2648 unsigned int maxpacksize;
2649 char dir_in;
2650 int chmask, num;
2651
2652 if (intf == ctrlif)
2653 continue;
2654
2655 iface = usb_ifnum_to_if(dev, intf);
2656 num = iface->num_altsetting;
2657
2658 if (num < 2)
2659 return -EINVAL;
2660
2661 /*
2662 * The number of Channels in an AudioStreaming interface
2663 * and the audio sample bit resolution (16 bits or 24
2664 * bits) can be derived from the wMaxPacketSize field in
2665 * the Standard AS Audio Data Endpoint descriptor in
2666 * Alternate Setting 1
2667 */
2668 alts = &iface->altsetting[1];
2669 altsd = get_iface_desc(alts);
2670
2671 if (altsd->bNumEndpoints < 1)
2672 return -EINVAL;
2673
2674 /* check direction */
2675 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2676 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2677
2678 switch (maxpacksize) {
2679 default:
2680 usb_audio_err(mixer->chip,
2681 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2682 maxpacksize);
2683 return -EINVAL;
2684 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2685 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2686 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2687 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2688 chmask = 1;
2689 break;
2690 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2691 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2692 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2693 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
2694 chmask = 3;
2695 break;
2696 }
2697
2698 if (dir_in)
2699 c_chmask = chmask;
2700 else
2701 p_chmask = chmask;
2702 }
2703
2704 usb_audio_dbg(mixer->chip,
2705 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
2706 badd_profile, c_chmask, p_chmask);
2707
2708 /* check the mapping table */
2709 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
2710 if (map->id == badd_profile)
2711 break;
2712 }
2713
2714 if (!map->id)
2715 return -EINVAL;
2716
2717 for (f = uac3_badd_profiles; f->name; f++) {
2718 if (badd_profile == f->subclass)
2719 break;
2720 }
2721 if (!f->name)
2722 return -EINVAL;
2723 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
2724 return -EINVAL;
2725 st_chmask = f->st_chmask;
2726
2727 /* Playback */
2728 if (p_chmask) {
2729 /* Master channel, always writable */
2730 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2731 UAC3_BADD_FU_ID2, map->map);
2732 /* Mono/Stereo volume channels, always writable */
2733 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
2734 UAC3_BADD_FU_ID2, map->map);
2735 }
2736
2737 /* Capture */
2738 if (c_chmask) {
2739 /* Master channel, always writable */
2740 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2741 UAC3_BADD_FU_ID5, map->map);
2742 /* Mono/Stereo volume channels, always writable */
2743 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
2744 UAC3_BADD_FU_ID5, map->map);
2745 }
2746
2747 /* Side tone-mixing */
2748 if (st_chmask) {
2749 /* Master channel, always writable */
2750 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
2751 UAC3_BADD_FU_ID7, map->map);
2752 /* Mono volume channel, always writable */
2753 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
2754 UAC3_BADD_FU_ID7, map->map);
2755 }
2756
2757 return 0;
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760/*
2761 * create mixer controls
2762 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002763 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002765static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002767 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 int err;
2769 const struct usbmix_ctl_map *map;
Daniel Mack23caaf12010-03-11 21:13:25 +01002770 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002773 state.chip = mixer->chip;
2774 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002775 state.buffer = mixer->hostif->extra;
2776 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002779 for (map = usbmix_ctl_maps; map->id; map++) {
2780 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002782 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002783 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 break;
2785 }
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Daniel Mack23caaf12010-03-11 21:13:25 +01002788 p = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002789 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2790 mixer->hostif->extralen,
Daniel Mack1faa5d02011-08-04 15:56:28 +02002791 p, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002792 if (mixer->protocol == UAC_VERSION_1) {
Daniel Mack69da9bc2010-06-16 17:57:28 +02002793 struct uac1_output_terminal_descriptor *desc = p;
Daniel Mack23caaf12010-03-11 21:13:25 +01002794
2795 if (desc->bLength < sizeof(*desc))
2796 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002797 /* mark terminal ID as visited */
2798 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002799 state.oterm.id = desc->bTerminalID;
2800 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2801 state.oterm.name = desc->iTerminal;
2802 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002803 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002804 return err;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002805 } else if (mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002806 struct uac2_output_terminal_descriptor *desc = p;
2807
2808 if (desc->bLength < sizeof(*desc))
2809 continue; /* invalid descriptor? */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002810 /* mark terminal ID as visited */
2811 set_bit(desc->bTerminalID, state.unitbitmap);
Daniel Mack23caaf12010-03-11 21:13:25 +01002812 state.oterm.id = desc->bTerminalID;
2813 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2814 state.oterm.name = desc->iTerminal;
2815 err = parse_audio_unit(&state, desc->bSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002816 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002817 return err;
Daniel Mack09414202010-05-31 13:35:44 +02002818
Daniel Mack6bc170e2014-05-24 10:58:16 +02002819 /*
2820 * For UAC2, use the same approach to also add the
2821 * clock selectors
2822 */
Daniel Mack09414202010-05-31 13:35:44 +02002823 err = parse_audio_unit(&state, desc->bCSourceID);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002824 if (err < 0 && err != -EINVAL)
Daniel Mack09414202010-05-31 13:35:44 +02002825 return err;
Andrew Chant5a222e82018-03-23 19:25:23 -07002826
Takashi Iwai2b54f7852018-04-23 15:19:25 +02002827 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
Andrew Chant5a222e82018-03-23 19:25:23 -07002828 UAC2_TE_CONNECTOR)) {
2829 build_connector_control(&state, &state.oterm,
2830 false);
2831 }
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02002832 } else { /* UAC_VERSION_3 */
2833 struct uac3_output_terminal_descriptor *desc = p;
2834
2835 if (desc->bLength < sizeof(*desc))
2836 continue; /* invalid descriptor? */
2837 /* mark terminal ID as visited */
2838 set_bit(desc->bTerminalID, state.unitbitmap);
2839 state.oterm.id = desc->bTerminalID;
2840 state.oterm.type = le16_to_cpu(desc->wTerminalType);
2841 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
2842 err = parse_audio_unit(&state, desc->bSourceID);
2843 if (err < 0 && err != -EINVAL)
2844 return err;
2845
2846 /*
2847 * For UAC3, use the same approach to also add the
2848 * clock selectors
2849 */
2850 err = parse_audio_unit(&state, desc->bCSourceID);
2851 if (err < 0 && err != -EINVAL)
2852 return err;
Daniel Mack23caaf12010-03-11 21:13:25 +01002853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
2857}
Clemens Ladisch84957a82005-04-29 16:23:13 +02002858
Daniel Mack7b1eda22010-03-11 21:13:22 +01002859void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002860{
Takashi Iwai3360b842014-11-18 11:47:04 +01002861 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002862
Takashi Iwai8c558072018-05-03 12:33:32 +02002863 for_each_mixer_elem(list, mixer, unitid) {
Julian Scheelb2500b52017-11-16 17:35:17 +01002864 struct usb_mixer_elem_info *info =
Takashi Iwai8c558072018-05-03 12:33:32 +02002865 mixer_elem_list_to_info(list);
Julian Scheelb2500b52017-11-16 17:35:17 +01002866 /* invalidate cache, so the value is read from the device */
2867 info->cached = 0;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002868 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002869 &list->kctl->id);
Julian Scheelb2500b52017-11-16 17:35:17 +01002870 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002871}
2872
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002873static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01002874 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002875{
Takashi Iwai8c558072018-05-03 12:33:32 +02002876 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002877 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
2878 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002879 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01002880 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002881 cval->control, cval->cmask, cval->channels,
2882 val_types[cval->val_type]);
2883 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
2884 cval->min, cval->max, cval->dBmin, cval->dBmax);
2885}
2886
2887static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
2888 struct snd_info_buffer *buffer)
2889{
2890 struct snd_usb_audio *chip = entry->private_data;
2891 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01002892 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002893 int unitid;
2894
2895 list_for_each_entry(mixer, &chip->mixer_list, list) {
2896 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002897 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02002898 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002899 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002900 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
2901 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02002902 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01002903 snd_iprintf(buffer, " Unit: %i\n", list->id);
2904 if (list->kctl)
2905 snd_iprintf(buffer,
2906 " Control: name=\"%s\", index=%i\n",
2907 list->kctl->id.name,
2908 list->kctl->id.index);
2909 if (list->dump)
2910 list->dump(buffer, list);
2911 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002912 }
2913 }
2914}
2915
Daniel Macke213e9c2010-05-11 18:13:50 +02002916static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
2917 int attribute, int value, int index)
2918{
Takashi Iwai3360b842014-11-18 11:47:04 +01002919 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02002920 __u8 unitid = (index >> 8) & 0xff;
2921 __u8 control = (value >> 8) & 0xff;
2922 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02002923 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02002924
2925 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002926 usb_audio_dbg(mixer->chip,
2927 "%s(): bogus channel number %d\n",
2928 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02002929 return;
2930 }
2931
Takashi Iwai8c558072018-05-03 12:33:32 +02002932 for_each_mixer_elem(list, mixer, unitid)
Daniel Mack191227d2016-04-08 19:52:02 +02002933 count++;
2934
2935 if (count == 0)
2936 return;
2937
Takashi Iwai8c558072018-05-03 12:33:32 +02002938 for_each_mixer_elem(list, mixer, unitid) {
Takashi Iwai3360b842014-11-18 11:47:04 +01002939 struct usb_mixer_elem_info *info;
2940
2941 if (!list->kctl)
2942 continue;
2943
Takashi Iwai8c558072018-05-03 12:33:32 +02002944 info = mixer_elem_list_to_info(list);
Daniel Mack191227d2016-04-08 19:52:02 +02002945 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02002946 continue;
2947
2948 switch (attribute) {
2949 case UAC2_CS_CUR:
2950 /* invalidate cache, so the value is read from the device */
2951 if (channel)
2952 info->cached &= ~(1 << channel);
2953 else /* master channel */
2954 info->cached = 0;
2955
2956 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002957 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02002958 break;
2959
2960 case UAC2_CS_RANGE:
2961 /* TODO */
2962 break;
2963
2964 case UAC2_CS_MEM:
2965 /* TODO */
2966 break;
2967
2968 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002969 usb_audio_dbg(mixer->chip,
2970 "unknown attribute %d in interrupt\n",
2971 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02002972 break;
2973 } /* switch */
2974 }
2975}
2976
2977static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002978{
2979 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02002980 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01002981 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002982
Oliver Neukumedf7de32011-03-11 13:19:43 +01002983 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02002984 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002985
Daniel Macke213e9c2010-05-11 18:13:50 +02002986 if (mixer->protocol == UAC_VERSION_1) {
2987 struct uac1_status_word *status;
2988
2989 for (status = urb->transfer_buffer;
2990 len >= sizeof(*status);
2991 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002992 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02002993 status->bStatusType,
2994 status->bOriginator);
2995
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002996 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02002997 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2998 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002999 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02003000
3001 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3002 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02003003 else
Daniel Macke213e9c2010-05-11 18:13:50 +02003004 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3005 }
3006 } else { /* UAC_VERSION_2 */
3007 struct uac2_interrupt_data_msg *msg;
3008
3009 for (msg = urb->transfer_buffer;
3010 len >= sizeof(*msg);
3011 len -= sizeof(*msg), msg++) {
3012 /* drop vendor specific and endpoint requests */
3013 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3014 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3015 continue;
3016
3017 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3018 le16_to_cpu(msg->wValue),
3019 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003020 }
3021 }
Daniel Macke213e9c2010-05-11 18:13:50 +02003022
3023requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02003024 if (ustatus != -ENOENT &&
3025 ustatus != -ECONNRESET &&
3026 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003027 urb->dev = mixer->chip->dev;
3028 usb_submit_urb(urb, GFP_ATOMIC);
3029 }
3030}
3031
3032/* create the handler for the optional status interrupt endpoint */
3033static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3034{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003035 struct usb_endpoint_descriptor *ep;
3036 void *transfer_buffer;
3037 int buffer_length;
3038 unsigned int epnum;
3039
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003040 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02003041 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003042 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02003043 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01003044 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003045 return 0;
3046
Julia Lawall42a6e662008-12-29 11:23:02 +01003047 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003048 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3049 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3050 if (!transfer_buffer)
3051 return -ENOMEM;
3052 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3053 if (!mixer->urb) {
3054 kfree(transfer_buffer);
3055 return -ENOMEM;
3056 }
3057 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3058 usb_rcvintpipe(mixer->chip->dev, epnum),
3059 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02003060 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003061 usb_submit_urb(mixer->urb, GFP_KERNEL);
3062 return 0;
3063}
3064
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003065static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
3067{
3068 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3069
3070 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3071 return 0;
3072}
3073
3074static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_value *ucontrol)
3076{
3077 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3078 bool keep_iface = !!ucontrol->value.integer.value[0];
3079
3080 if (mixer->chip->keep_iface == keep_iface)
3081 return 0;
3082 mixer->chip->keep_iface = keep_iface;
3083 return 1;
3084}
3085
3086static const struct snd_kcontrol_new keep_iface_ctl = {
3087 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3088 .name = "Keep Interface",
3089 .info = snd_ctl_boolean_mono_info,
3090 .get = keep_iface_ctl_get,
3091 .put = keep_iface_ctl_put,
3092};
3093
3094static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3095{
3096 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3097
3098 /* need only one control per card */
3099 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3100 snd_ctl_free_one(kctl);
3101 return 0;
3102 }
3103
3104 return snd_ctl_add(mixer->chip->card, kctl);
3105}
3106
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003107int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3108 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003109{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003110 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02003111 .dev_free = snd_usb_mixer_dev_free
3112 };
3113 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003114 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01003115 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003116
3117 strcpy(chip->card->mixername, "USB Mixer");
3118
Takashi Iwai561b2202005-09-09 14:22:34 +02003119 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003120 if (!mixer)
3121 return -ENOMEM;
3122 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003123 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01003124 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3125 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02003126 if (!mixer->id_elems) {
3127 kfree(mixer);
3128 return -ENOMEM;
3129 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003130
Daniel Mack1faa5d02011-08-04 15:56:28 +02003131 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3132 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02003133 case UAC_VERSION_1:
3134 default:
3135 mixer->protocol = UAC_VERSION_1;
3136 break;
3137 case UAC_VERSION_2:
3138 mixer->protocol = UAC_VERSION_2;
3139 break;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +02003140 case UAC_VERSION_3:
3141 mixer->protocol = UAC_VERSION_3;
3142 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02003143 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01003144
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003145 if (mixer->protocol == UAC_VERSION_3 &&
3146 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
3147 if ((err = snd_usb_mixer_controls_badd(mixer, ctrlif)) < 0)
3148 goto _error;
3149 } else if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
3150 (err = snd_usb_mixer_status_create(mixer)) < 0) {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003151 goto _error;
Ruslan Bilovol17156f22018-05-04 04:24:04 +03003152 }
Takashi Iwai4120fbe2018-05-02 11:52:55 +02003153 err = create_keep_iface_ctl(mixer);
3154 if (err < 0)
3155 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003156
Daniel Mack7b1eda22010-03-11 21:13:22 +01003157 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02003158
Takashi Iwai9cbb2802014-02-04 11:15:31 +01003159 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003160 if (err < 0)
3161 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01003162
3163 if (list_empty(&chip->mixer_list) &&
3164 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3165 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3166
Clemens Ladisch84957a82005-04-29 16:23:13 +02003167 list_add(&mixer->list, &chip->mixer_list);
3168 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02003169
3170_error:
3171 snd_usb_mixer_free(mixer);
3172 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003173}
3174
Takashi Iwaia6cece92014-10-31 11:24:32 +01003175void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02003176{
Takashi Iwai124751d2017-10-10 14:10:32 +02003177 if (mixer->disconnected)
3178 return;
3179 if (mixer->urb)
3180 usb_kill_urb(mixer->urb);
3181 if (mixer->rc_urb)
3182 usb_kill_urb(mixer->rc_urb);
3183 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02003184}
Takashi Iwai400362f2014-01-20 16:51:16 +01003185
3186#ifdef CONFIG_PM
3187/* stop any bus activity of a mixer */
3188static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3189{
3190 usb_kill_urb(mixer->urb);
3191 usb_kill_urb(mixer->rc_urb);
3192}
3193
3194static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3195{
3196 int err;
3197
3198 if (mixer->urb) {
3199 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3200 if (err < 0)
3201 return err;
3202 }
3203
3204 return 0;
3205}
3206
3207int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3208{
3209 snd_usb_mixer_inactivate(mixer);
3210 return 0;
3211}
3212
Takashi Iwai3360b842014-11-18 11:47:04 +01003213static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01003214{
Takashi Iwai8c558072018-05-03 12:33:32 +02003215 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
Takashi Iwai400362f2014-01-20 16:51:16 +01003216 int c, err, idx;
3217
3218 if (cval->cmask) {
3219 idx = 0;
3220 for (c = 0; c < MAX_CHANNELS; c++) {
3221 if (!(cval->cmask & (1 << c)))
3222 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08003223 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06003224 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01003225 cval->cache_val[idx]);
3226 if (err < 0)
3227 return err;
3228 }
3229 idx++;
3230 }
3231 } else {
3232 /* master */
3233 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003234 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003235 if (err < 0)
3236 return err;
3237 }
3238 }
3239
3240 return 0;
3241}
3242
3243int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3244{
Takashi Iwai3360b842014-11-18 11:47:04 +01003245 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003246 int id, err;
3247
Takashi Iwai400362f2014-01-20 16:51:16 +01003248 if (reset_resume) {
3249 /* restore cached mixer values */
3250 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai8c558072018-05-03 12:33:32 +02003251 for_each_mixer_elem(list, mixer, id) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003252 if (list->resume) {
3253 err = list->resume(list);
3254 if (err < 0)
3255 return err;
3256 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003257 }
3258 }
3259 }
3260
Takashi Iwai964af632018-04-27 14:23:37 +02003261 snd_usb_mixer_resume_quirk(mixer);
3262
Takashi Iwai400362f2014-01-20 16:51:16 +01003263 return snd_usb_mixer_activate(mixer);
3264}
3265#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003266
3267void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3268 struct usb_mixer_interface *mixer,
3269 int unitid)
3270{
3271 list->mixer = mixer;
3272 list->id = unitid;
3273 list->dump = snd_usb_mixer_dump_cval;
3274#ifdef CONFIG_PM
3275 list->resume = restore_mixer_value;
3276#endif
3277}