blob: 9e7861af23a2dddf87db92f8a9646f2e59c75a7c [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>
Ajay Agarwal045e3742017-02-07 18:41:34 +053054#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 *
115find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100117 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100119 if (!p)
120 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100123 if (p->id == unitid &&
124 (!control || !p->control || control == p->control))
125 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100127 return NULL;
128}
129
130/* get the mapped name if the unit matches */
131static int
132check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
133{
134 if (!p || !p->name)
135 return 0;
136
137 buflen--;
138 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Takashi Iwai5aeee342014-11-18 11:02:14 +0100141/* ignore the error value if ignore_ctl_error flag is set */
142#define filter_error(cval, err) \
Takashi Iwai3360b842014-11-18 11:47:04 +0100143 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
Takashi Iwai5aeee342014-11-18 11:02:14 +0100144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100146static inline int
147check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100149 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100151 return 1;
152}
153
154/* dB mapping */
155static inline void check_mapped_dB(const struct usbmix_name_map *p,
156 struct usb_mixer_elem_info *cval)
157{
158 if (p && p->dB) {
159 cval->dBmin = p->dB->min;
160 cval->dBmax = p->dB->max;
Takashi Iwai38b65192011-08-19 07:55:10 +0200161 cval->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200165/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100166static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200167 int index, char *buf, int buflen)
168{
169 const struct usbmix_selector_map *p;
170
Daniel Mack6bc170e2014-05-24 10:58:16 +0200171 if (!state->selector_map)
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200172 return 0;
173 for (p = state->selector_map; p->id; p++) {
174 if (p->id == unitid && index < p->count)
175 return strlcpy(buf, p->names[index], buflen);
176 }
177 return 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * find an audio control unit with the given unit id
182 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200183static void *find_audio_control_unit(struct mixer_build *state,
184 unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Daniel Mack67e1daa2010-05-31 13:35:43 +0200186 /* we just parse the header */
187 struct uac_feature_unit_descriptor *hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Ajay Agarwal045e3742017-02-07 18:41:34 +0530189 if (state->mixer->protocol == UAC_VERSION_3) {
190 int i;
191
192 for (i = 0; i < NUM_BADD_DESCS; i++) {
193 hdr = (void *)badd_desc_list[i];
194 if (hdr->bUnitID == unit)
195 return hdr;
196 }
197
198 return NULL;
199 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200200 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
201 USB_DT_CS_INTERFACE)) != NULL) {
202 if (hdr->bLength >= 4 &&
203 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
204 hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER &&
205 hdr->bUnitID == unit)
206 return hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Daniel Mack67e1daa2010-05-31 13:35:43 +0200208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return NULL;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * copy a string with the given id
214 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200215static int snd_usb_copy_string_desc(struct mixer_build *state,
216 int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
219 buf[len] = 0;
220 return len;
221}
222
223/*
224 * convert from the byte/word on usb descriptor to the zero-based integer
225 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100226static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 switch (cval->val_type) {
229 case USB_MIXER_BOOLEAN:
230 return !!val;
231 case USB_MIXER_INV_BOOLEAN:
232 return !val;
233 case USB_MIXER_U8:
234 val &= 0xff;
235 break;
236 case USB_MIXER_S8:
237 val &= 0xff;
238 if (val >= 0x80)
239 val -= 0x100;
240 break;
241 case USB_MIXER_U16:
242 val &= 0xffff;
243 break;
244 case USB_MIXER_S16:
245 val &= 0xffff;
246 if (val >= 0x8000)
247 val -= 0x10000;
248 break;
249 }
250 return val;
251}
252
253/*
254 * convert from the zero-based int to the byte/word for usb descriptor
255 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100256static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 switch (cval->val_type) {
259 case USB_MIXER_BOOLEAN:
260 return !!val;
261 case USB_MIXER_INV_BOOLEAN:
262 return !val;
263 case USB_MIXER_S8:
264 case USB_MIXER_U8:
265 return val & 0xff;
266 case USB_MIXER_S16:
267 case USB_MIXER_U16:
268 return val & 0xffff;
269 }
270 return 0; /* not reached */
271}
272
Takashi Iwai86e07d32005-11-17 15:08:02 +0100273static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200275 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 cval->res = 1;
277 if (val < cval->min)
278 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200279 else if (val >= cval->max)
280 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 else
282 return (val - cval->min) / cval->res;
283}
284
Takashi Iwai86e07d32005-11-17 15:08:02 +0100285static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 if (val < 0)
288 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200289 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 cval->res = 1;
291 val *= cval->res;
292 val += cval->min;
293 if (val > cval->max)
294 return cval->max;
295 return val;
296}
297
Julian Scheelbc18e312015-08-14 16:14:45 +0200298static int uac2_ctl_value_size(int val_type)
299{
300 switch (val_type) {
301 case USB_MIXER_S32:
302 case USB_MIXER_U32:
303 return 4;
304 case USB_MIXER_S16:
305 case USB_MIXER_U16:
306 return 2;
307 default:
308 return 1;
309 }
310 return 0; /* unreachable */
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314/*
315 * retrieve a mixer value
316 */
317
Daniel Mack6bc170e2014-05-24 10:58:16 +0200318static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
319 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Takashi Iwai3360b842014-11-18 11:47:04 +0100321 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned char buf[2];
323 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
324 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200325 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Takashi Iwai47ab1542015-08-25 16:09:00 +0200327 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100328 if (err < 0)
329 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100332 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200333 if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200335 validx, idx, buf, val_len) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200337 err = 0;
338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100341 usb_audio_dbg(chip,
342 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
343 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200344 err = -EINVAL;
345
346 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200347 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Daniel Mack6bc170e2014-05-24 10:58:16 +0200351static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
352 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100353{
Takashi Iwai3360b842014-11-18 11:47:04 +0100354 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200355 unsigned char buf[4 + 3 * sizeof(__u32)]; /* enough space for one range */
Daniel Mack23caaf12010-03-11 21:13:25 +0100356 unsigned char *val;
Takashi Iwai978520b2012-10-12 15:12:55 +0200357 int idx = 0, ret, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100358 __u8 bRequest;
359
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200360 if (request == UAC_GET_CUR) {
361 bRequest = UAC2_CS_CUR;
Julian Scheelbc18e312015-08-14 16:14:45 +0200362 size = uac2_ctl_value_size(cval->val_type);
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200363 } else {
364 bRequest = UAC2_CS_RANGE;
365 size = sizeof(buf);
366 }
367
368 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100369
Takashi Iwai47ab1542015-08-25 16:09:00 +0200370 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100371 if (ret)
372 goto error;
373
Takashi Iwai47ab1542015-08-25 16:09:00 +0200374 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
375 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100376 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200377 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200378 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100379
380 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100381error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100382 usb_audio_err(chip,
383 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
384 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100385 return ret;
386 }
387
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200388 /* FIXME: how should we handle multiple triplets here? */
389
Daniel Mack23caaf12010-03-11 21:13:25 +0100390 switch (request) {
391 case UAC_GET_CUR:
392 val = buf;
393 break;
394 case UAC_GET_MIN:
395 val = buf + sizeof(__u16);
396 break;
397 case UAC_GET_MAX:
398 val = buf + sizeof(__u16) * 2;
399 break;
400 case UAC_GET_RES:
401 val = buf + sizeof(__u16) * 3;
402 break;
403 default:
404 return -EINVAL;
405 }
406
407 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16)));
408
409 return 0;
410}
411
Daniel Mack6bc170e2014-05-24 10:58:16 +0200412static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
413 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100414{
Eldad Zack9f814102012-11-28 23:55:35 +0100415 validx += cval->idx_off;
416
Takashi Iwai3360b842014-11-18 11:47:04 +0100417 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100418 get_ctl_value_v1(cval, request, validx, value_ret) :
419 get_ctl_value_v2(cval, request, validx, value_ret);
420}
421
Daniel Mack6bc170e2014-05-24 10:58:16 +0200422static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
423 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100425 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100429static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
430 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200432 return get_ctl_value(cval, UAC_GET_CUR,
433 (cval->control << 8) | channel,
434 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Chris J Argeseef90452014-11-12 12:07:01 -0600437int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100438 int channel, int index, int *value)
439{
440 int err;
441
442 if (cval->cached & (1 << channel)) {
443 *value = cval->cache_val[index];
444 return 0;
445 }
446 err = get_cur_mix_raw(cval, channel, value);
447 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100448 if (!cval->head.mixer->ignore_ctl_error)
449 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100450 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200451 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100452 return err;
453 }
454 cval->cached |= 1 << channel;
455 cval->cache_val[index] = *value;
456 return 0;
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
460 * set a mixer value
461 */
462
Daniel Mack7b1eda22010-03-11 21:13:22 +0100463int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
464 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Takashi Iwai3360b842014-11-18 11:47:04 +0100466 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200467 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200468 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100469
Eldad Zack9f814102012-11-28 23:55:35 +0100470 validx += cval->idx_off;
471
Takashi Iwai3360b842014-11-18 11:47:04 +0100472 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100473 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
474 } else { /* UAC_VERSION_2 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200475 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100476
477 /* FIXME */
478 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100479 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100480 return -EINVAL;
481 }
482
483 request = UAC2_CS_CUR;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 value_set = convert_bytes_value(cval, value_set);
487 buf[0] = value_set & 0xff;
488 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200489 buf[2] = (value_set >> 16) & 0xff;
490 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200491
492 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100493 if (err < 0)
494 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200495
Takashi Iwai978520b2012-10-12 15:12:55 +0200496 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100497 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200498 if (snd_usb_ctl_msg(chip->dev,
499 usb_sndctrlpipe(chip->dev, 0), request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Takashi Iwai978520b2012-10-12 15:12:55 +0200501 validx, idx, buf, val_len) >= 0) {
502 err = 0;
503 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100504 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200505 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100506 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 +0200507 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200508 err = -EINVAL;
509
510 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200511 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Daniel Mack6bc170e2014-05-24 10:58:16 +0200515static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
516 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100518 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Chris J Argeseef90452014-11-12 12:07:01 -0600521int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100522 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Takashi Iwai641b4872009-01-15 17:05:24 +0100524 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200525 unsigned int read_only = (channel == 0) ?
526 cval->master_readonly :
527 cval->ch_readonly & (1 << (channel - 1));
528
529 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100530 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100531 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200532 __func__, channel, cval->control);
533 return 0;
534 }
535
Daniel Mack6bc170e2014-05-24 10:58:16 +0200536 err = snd_usb_mixer_set_ctl_value(cval,
537 UAC_SET_CUR, (cval->control << 8) | channel,
538 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100539 if (err < 0)
540 return err;
541 cval->cached |= 1 << channel;
542 cval->cache_val[index] = value;
543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200546/*
547 * TLV callback for mixer volume controls
548 */
Felix Homann285de9c2012-04-23 20:24:24 +0200549int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200550 unsigned int size, unsigned int __user *_tlv)
551{
552 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200553 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200554
555 if (size < sizeof(scale))
556 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100557 scale[2] = cval->dBmin;
558 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200559 if (copy_to_user(_tlv, scale, sizeof(scale)))
560 return -EFAULT;
561 return 0;
562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564/*
565 * parser routines begin here...
566 */
567
Takashi Iwai86e07d32005-11-17 15:08:02 +0100568static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570
571/*
572 * check if the input/output channel routing is enabled on the given bitmap.
573 * used for mixer unit parser
574 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200575static int check_matrix_bitmap(unsigned char *bmap,
576 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int idx = ich * num_outs + och;
579 return bmap[idx >> 3] & (0x80 >> (idx & 7));
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * add an alsa control element
584 * search and increment the index until an empty slot is found.
585 *
586 * if failed, give up and free the control instance.
587 */
588
Takashi Iwai3360b842014-11-18 11:47:04 +0100589int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200590 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwai3360b842014-11-18 11:47:04 +0100592 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200594
Daniel Mackef9d5972011-05-25 09:09:00 +0200595 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 kctl->id.index++;
Daniel Mackef9d5972011-05-25 09:09:00 +0200597 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200598 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
599 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100602 list->kctl = kctl;
603 list->next_id_elem = mixer->id_elems[list->id];
604 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * get a terminal name string
610 */
611
612static struct iterm_name_combo {
613 int type;
614 char *name;
615} iterm_names[] = {
616 { 0x0300, "Output" },
617 { 0x0301, "Speaker" },
618 { 0x0302, "Headphone" },
619 { 0x0303, "HMD Audio" },
620 { 0x0304, "Desktop Speaker" },
621 { 0x0305, "Room Speaker" },
622 { 0x0306, "Com Speaker" },
623 { 0x0307, "LFE" },
624 { 0x0600, "External In" },
625 { 0x0601, "Analog In" },
626 { 0x0602, "Digital In" },
627 { 0x0603, "Line" },
628 { 0x0604, "Legacy In" },
629 { 0x0605, "IEC958 In" },
630 { 0x0606, "1394 DA Stream" },
631 { 0x0607, "1394 DV Stream" },
632 { 0x0700, "Embedded" },
633 { 0x0701, "Noise Source" },
634 { 0x0702, "Equalization Noise" },
635 { 0x0703, "CD" },
636 { 0x0704, "DAT" },
637 { 0x0705, "DCC" },
638 { 0x0706, "MiniDisk" },
639 { 0x0707, "Analog Tape" },
640 { 0x0708, "Phonograph" },
641 { 0x0709, "VCR Audio" },
642 { 0x070a, "Video Disk Audio" },
643 { 0x070b, "DVD Audio" },
644 { 0x070c, "TV Tuner Audio" },
645 { 0x070d, "Satellite Rec Audio" },
646 { 0x070e, "Cable Tuner Audio" },
647 { 0x070f, "DSS Audio" },
648 { 0x0710, "Radio Receiver" },
649 { 0x0711, "Radio Transmitter" },
650 { 0x0712, "Multi-Track Recorder" },
651 { 0x0713, "Synthesizer" },
652 { 0 },
653};
654
Takashi Iwai86e07d32005-11-17 15:08:02 +0100655static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned char *name, int maxlen, int term_only)
657{
658 struct iterm_name_combo *names;
659
660 if (iterm->name)
Daniel Mack6bc170e2014-05-24 10:58:16 +0200661 return snd_usb_copy_string_desc(state, iterm->name,
662 name, maxlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 /* virtual type - not a real terminal */
665 if (iterm->type >> 16) {
666 if (term_only)
667 return 0;
668 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100669 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200670 strcpy(name, "Selector");
671 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200672 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200673 strcpy(name, "Process Unit");
674 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200675 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200676 strcpy(name, "Ext Unit");
677 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100678 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200679 strcpy(name, "Mixer");
680 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 default:
682 return sprintf(name, "Unit %d", iterm->id);
683 }
684 }
685
686 switch (iterm->type & 0xff00) {
687 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200688 strcpy(name, "PCM");
689 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200691 strcpy(name, "Mic");
692 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200694 strcpy(name, "Headset");
695 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200697 strcpy(name, "Phone");
698 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
Daniel Mack6bc170e2014-05-24 10:58:16 +0200701 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (names->type == iterm->type) {
703 strcpy(name, names->name);
704 return strlen(names->name);
705 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * parse the source unit recursively until it reaches to a terminal
713 * or a branched unit.
714 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200715static int check_input_term(struct mixer_build *state, int id,
716 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Daniel Mack09414202010-05-31 13:35:44 +0200718 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100719 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 memset(term, 0, sizeof(*term));
722 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100723 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100725 switch (hdr[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100726 case UAC_INPUT_TERMINAL:
Daniel Mack23caaf12010-03-11 21:13:25 +0100727 if (state->mixer->protocol == UAC_VERSION_1) {
728 struct uac_input_terminal_descriptor *d = p1;
729 term->type = le16_to_cpu(d->wTerminalType);
730 term->channels = d->bNrChannels;
731 term->chconfig = le16_to_cpu(d->wChannelConfig);
732 term->name = d->iTerminal;
Ajay Agarwal045e3742017-02-07 18:41:34 +0530733 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100734 struct uac2_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200735
736 /* call recursively to verify that the
737 * referenced clock entity is valid */
738 err = check_input_term(state, d->bCSourceID, term);
739 if (err < 0)
740 return err;
741
742 /* save input term properties after recursion,
743 * to ensure they are not overriden by the
744 * recursion calls */
745 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100746 term->type = le16_to_cpu(d->wTerminalType);
747 term->channels = d->bNrChannels;
748 term->chconfig = le32_to_cpu(d->bmChannelConfig);
749 term->name = d->iTerminal;
Ajay Agarwal045e3742017-02-07 18:41:34 +0530750 } else { /* UAC_VERSION_3 */
751 struct uac3_input_terminal_descriptor *d = p1;
752
753 err = check_input_term(state,
754 d->bCSourceID, term);
755 if (err < 0)
756 return err;
757
758 term->id = id;
759 term->type = d->wTerminalType;
760 if (d->wClusterDescrID == CLUSTER_ID_MONO) {
761 term->channels = NUM_CHANNELS_MONO;
762 term->chconfig = BADD_CH_CONFIG_MONO;
763 } else {
764 term->channels = NUM_CHANNELS_STEREO;
765 term->chconfig = BADD_CH_CONFIG_STEREO;
766 }
767 term->name = d->wTerminalDescrStr;
Daniel Mack23caaf12010-03-11 21:13:25 +0100768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100770 case UAC_FEATURE_UNIT: {
771 /* the header is the same for v1 and v2 */
772 struct uac_feature_unit_descriptor *d = p1;
Daniel Mack5e688882010-05-08 11:24:56 +0200773 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break; /* continue to parse */
Daniel Mack23caaf12010-03-11 21:13:25 +0100775 }
776 case UAC_MIXER_UNIT: {
777 struct uac_mixer_unit_descriptor *d = p1;
778 term->type = d->bDescriptorSubtype << 16; /* virtual type */
779 term->channels = uac_mixer_unit_bNrChannels(d);
780 term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
781 term->name = uac_mixer_unit_iMixer(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100783 }
Daniel Mack09414202010-05-31 13:35:44 +0200784 case UAC_SELECTOR_UNIT:
Ajay Agarwal045e3742017-02-07 18:41:34 +0530785 /* UAC3_MIXER_UNIT_V3 */
786 case UAC2_CLOCK_SELECTOR:
787 /* UAC3_CLOCK_SOURCE */ {
788 if (state->mixer->protocol == UAC_VERSION_3
789 && hdr[2] == UAC3_CLOCK_SOURCE) {
790 struct uac3_clock_source_descriptor *d = p1;
791
792 term->type = d->bDescriptorSubtype << 16;
793 term->id = id;
794 term->name = d->wClockSourceStr;
795 } else if (state->mixer->protocol == UAC_VERSION_3
796 && hdr[2] == UAC3_MIXER_UNIT_V3) {
797 struct uac3_mixer_unit_descriptor *d = p1;
798
799 term->type = d->bDescriptorSubtype << 16;
800 if (d->wClusterDescrID == CLUSTER_ID_MONO) {
801 term->channels = NUM_CHANNELS_MONO;
802 term->chconfig = BADD_CH_CONFIG_MONO;
803 } else {
804 term->channels = NUM_CHANNELS_STEREO;
805 term->chconfig = BADD_CH_CONFIG_STEREO;
806 }
807 term->name = d->wMixerDescrStr;
808 } else {
809 struct uac_selector_unit_descriptor *d = p1;
810 /* call recursively to retrieve channel info */
811 err = check_input_term(state,
812 d->baSourceID[0], term);
813 if (err < 0)
814 return err;
815 /* virtual type */
816 term->type = d->bDescriptorSubtype << 16;
817 term->id = id;
818 term->name = uac_selector_unit_iSelector(d);
819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100821 }
Daniel Mack69da9bc2010-06-16 17:57:28 +0200822 case UAC1_PROCESSING_UNIT:
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100823 case UAC1_EXTENSION_UNIT:
824 /* UAC2_PROCESSING_UNIT_V2 */
Torstein Hegge61ac5132013-03-19 17:12:14 +0100825 /* UAC2_EFFECT_UNIT */
Ajay Agarwal045e3742017-02-07 18:41:34 +0530826 /* UAC3_FEATURE_UNIT_V3 */
Torstein Hegge61ac5132013-03-19 17:12:14 +0100827 case UAC2_EXTENSION_UNIT_V2: {
Ajay Agarwal045e3742017-02-07 18:41:34 +0530828 if (state->mixer->protocol == UAC_VERSION_3) {
829 struct uac_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100830
Ajay Agarwal045e3742017-02-07 18:41:34 +0530831 id = d->bSourceID;
832 } else {
833 struct uac_processing_unit_descriptor *d = p1;
834
835 if (state->mixer->protocol == UAC_VERSION_2 &&
836 hdr[2] == UAC2_EFFECT_UNIT) {
837 /* UAC2/UAC1 unit IDs overlap here in an
838 * uncompatible way. Ignore this unit
839 * for now.
840 */
841 return 0;
842 }
843
844 if (d->bNrInPins) {
845 id = d->baSourceID[0];
846 break; /* continue to parse */
847 }
848 /* virtual type */
849 term->type = d->bDescriptorSubtype << 16;
850 term->channels =
851 uac_processing_unit_bNrChannels(d);
852 term->chconfig =
853 uac_processing_unit_wChannelConfig(
854 d, state->mixer->protocol);
855 term->name = uac_processing_unit_iProcessing(
856 d, state->mixer->protocol);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100857 return 0;
858 }
Ajay Agarwal045e3742017-02-07 18:41:34 +0530859 break;
Daniel Mack23caaf12010-03-11 21:13:25 +0100860 }
Daniel Mack09414202010-05-31 13:35:44 +0200861 case UAC2_CLOCK_SOURCE: {
862 struct uac_clock_source_descriptor *d = p1;
863 term->type = d->bDescriptorSubtype << 16; /* virtual type */
864 term->id = id;
865 term->name = d->iClockSource;
866 return 0;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 default:
869 return -ENODEV;
870 }
871 }
872 return -ENODEV;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * Feature Unit
877 */
878
879/* feature unit control information */
880struct usb_feature_control_info {
881 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[] = {
Julian Scheelbc18e312015-08-14 16:14:45 +0200887 { "Mute", USB_MIXER_INV_BOOLEAN, -1 },
888 { "Volume", USB_MIXER_S16, -1 },
889 { "Tone Control - Bass", USB_MIXER_S8, -1 },
890 { "Tone Control - Mid", USB_MIXER_S8, -1 },
891 { "Tone Control - Treble", USB_MIXER_S8, -1 },
892 { "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */
893 { "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
894 { "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
895 { "Bass Boost", USB_MIXER_BOOLEAN, -1 },
896 { "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200897 /* UAC2 specific */
Julian Scheelbc18e312015-08-14 16:14:45 +0200898 { "Input Gain Control", USB_MIXER_S16, -1 },
899 { "Input Gain Pad Control", USB_MIXER_S16, -1 },
900 { "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 Kolivas205d3de2016-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 Kolivas205d3de2016-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;
Ajay Agarwal5a19df02017-06-22 11:09:33 +05301016
1017 case USB_ID(0x1130, 0x1620): /* Logitech Speakers S150 */
1018 /* This audio device has 2 channels and it explicitly requires the
1019 * host to send SET_CUR command on the volume control of both the
1020 * channels. 7936 = 0x1F00 is the default value.
1021 */
1022 if (cval->channels == 2)
1023 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1024 (cval->control << 8) | 2, 7936);
1025 break;
1026
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001027 }
1028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/*
1031 * retrieve the minimum and maximum values for the specified control
1032 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001033static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1034 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 /* for failsafe */
1037 cval->min = default_min;
1038 cval->max = cval->min + 1;
1039 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001040 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (cval->val_type == USB_MIXER_BOOLEAN ||
1043 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1044 cval->initialized = 1;
1045 } else {
1046 int minchn = 0;
1047 if (cval->cmask) {
1048 int i;
1049 for (i = 0; i < MAX_CHANNELS; i++)
1050 if (cval->cmask & (1 << i)) {
1051 minchn = i + 1;
1052 break;
1053 }
1054 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001055 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1056 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001057 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001058 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001059 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1060 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -EINVAL;
1062 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001063 if (get_ctl_value(cval, UAC_GET_RES,
1064 (cval->control << 8) | minchn,
1065 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 cval->res = 1;
1067 } else {
1068 int last_valid_res = cval->res;
1069
1070 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001071 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001072 (cval->control << 8) | minchn,
1073 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 break;
1075 cval->res /= 2;
1076 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001077 if (get_ctl_value(cval, UAC_GET_RES,
1078 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 cval->res = last_valid_res;
1080 }
1081 if (cval->res == 0)
1082 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001083
1084 /* Additional checks for the proper resolution
1085 *
1086 * Some devices report smaller resolutions than actually
1087 * reacting. They don't return errors but simply clip
1088 * to the lower aligned value.
1089 */
1090 if (cval->min + cval->res < cval->max) {
1091 int last_valid_res = cval->res;
1092 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001093 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001094 for (;;) {
1095 test = saved;
1096 if (test < cval->max)
1097 test += cval->res;
1098 else
1099 test -= cval->res;
1100 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001101 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001102 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001103 cval->res = last_valid_res;
1104 break;
1105 }
1106 if (test == check)
1107 break;
1108 cval->res *= 2;
1109 }
Chris J Argeseef90452014-11-12 12:07:01 -06001110 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 cval->initialized = 1;
1114 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001115
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001116 if (kctl)
1117 volume_control_quirks(cval, kctl);
1118
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001119 /* USB descriptions contain the dB scale in 1/256 dB unit
1120 * while ALSA TLV contains in 1/100 dB unit
1121 */
Deepa Madiregama8b7f7fe2013-08-21 17:54:08 +05301122 cval->dBmin =
1123 (convert_signed_value(cval, cval->min) * 100) / (cval->res);
1124 cval->dBmax =
1125 (convert_signed_value(cval, cval->max) * 100) / (cval->res);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001126 if (cval->dBmin > cval->dBmax) {
1127 /* something is wrong; assume it's either from/to 0dB */
1128 if (cval->dBmin < 0)
1129 cval->dBmax = 0;
1130 else if (cval->dBmin > 0)
1131 cval->dBmin = 0;
1132 if (cval->dBmin > cval->dBmax) {
1133 /* totally crap, return an error */
1134 return -EINVAL;
1135 }
1136 }
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return 0;
1139}
1140
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001141#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001144static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1145 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001147 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 if (cval->val_type == USB_MIXER_BOOLEAN ||
1150 cval->val_type == USB_MIXER_INV_BOOLEAN)
1151 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1152 else
1153 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1154 uinfo->count = cval->channels;
1155 if (cval->val_type == USB_MIXER_BOOLEAN ||
1156 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1157 uinfo->value.integer.min = 0;
1158 uinfo->value.integer.max = 1;
1159 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001160 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001161 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001162 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1163 kcontrol->vd[0].access &=
1164 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1165 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001166 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001167 SNDRV_CTL_EVENT_MASK_INFO,
1168 &kcontrol->id);
1169 }
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001172 uinfo->value.integer.max =
1173 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 return 0;
1176}
1177
1178/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001179static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1180 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001182 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int c, cnt, val, err;
1184
Takashi Iwai641b4872009-01-15 17:05:24 +01001185 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (cval->cmask) {
1187 cnt = 0;
1188 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001189 if (!(cval->cmask & (1 << c)))
1190 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001191 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001192 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001193 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001194 val = get_relative_value(cval, val);
1195 ucontrol->value.integer.value[cnt] = val;
1196 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
1200 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001201 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001202 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001203 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 val = get_relative_value(cval, val);
1205 ucontrol->value.integer.value[0] = val;
1206 }
1207 return 0;
1208}
1209
1210/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001211static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1212 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001214 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int c, cnt, val, oval, err;
1216 int changed = 0;
1217
1218 if (cval->cmask) {
1219 cnt = 0;
1220 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001221 if (!(cval->cmask & (1 << c)))
1222 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001223 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001224 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001225 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001226 val = ucontrol->value.integer.value[cnt];
1227 val = get_abs_value(cval, val);
1228 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001229 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001230 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001232 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 } else {
1235 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001236 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001238 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 val = ucontrol->value.integer.value[0];
1240 val = get_abs_value(cval, val);
1241 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001242 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 changed = 1;
1244 }
1245 }
1246 return changed;
1247}
1248
Takashi Iwai86e07d32005-11-17 15:08:02 +01001249static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1251 .name = "", /* will be filled later manually */
1252 .info = mixer_ctl_feature_info,
1253 .get = mixer_ctl_feature_get,
1254 .put = mixer_ctl_feature_put,
1255};
1256
Daniel Mack23caaf12010-03-11 21:13:25 +01001257/* the read-only variant */
1258static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1259 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1260 .name = "", /* will be filled later manually */
1261 .info = mixer_ctl_feature_info,
1262 .get = mixer_ctl_feature_get,
1263 .put = NULL,
1264};
1265
Daniel Mack6bc170e2014-05-24 10:58:16 +02001266/*
1267 * This symbol is exported in order to allow the mixer quirks to
1268 * hook up to the standard feature unit control mechanism
1269 */
Daniel Mack9e386582011-05-25 09:09:01 +02001270struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272/*
1273 * build a feature control
1274 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001275static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1276{
1277 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1278}
1279
Daniel Mack6bc170e2014-05-24 10:58:16 +02001280/*
1281 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1282 * rename it to "Headphone". We determine if something is a headphone
1283 * similar to how udev determines form factor.
1284 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001285static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1286 struct snd_card *card)
1287{
1288 const char *names_to_check[] = {
1289 "Headset", "headset", "Headphone", "headphone", NULL};
1290 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001291 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001292
1293 if (strcmp("Speaker", kctl->id.name))
1294 return;
1295
1296 for (s = names_to_check; *s; s++)
1297 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001298 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001299 break;
1300 }
1301
1302 if (!found)
1303 return;
1304
1305 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1306}
1307
Daniel Mack99fc8642010-03-11 21:13:24 +01001308static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 unsigned int ctl_mask, int control,
Daniel Mack23caaf12010-03-11 21:13:25 +01001310 struct usb_audio_term *iterm, int unitid,
Daniel Macka6a33252010-05-31 13:35:37 +02001311 int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Daniel Mack99fc8642010-03-11 21:13:24 +01001313 struct uac_feature_unit_descriptor *desc = raw_desc;
Julian Scheelbc18e312015-08-14 16:14:45 +02001314 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 unsigned int len = 0;
1316 int mapped_name = 0;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301317 int nameid;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001318 struct snd_kcontrol *kctl;
1319 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001320 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001321 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Ajay Agarwal045e3742017-02-07 18:41:34 +05301323 if (state->mixer->protocol == UAC_VERSION_3)
1324 nameid = ((struct uac3_feature_unit_descriptor *)
1325 raw_desc)->wFeatureDescrStr;
1326 else
1327 nameid = uac_feature_unit_iFeature(desc);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 control++; /* change from zero-based to 1-based value */
1330
Daniel Mack65f25da2010-05-31 13:35:41 +02001331 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* FIXME: not supported yet */
1333 return;
1334 }
1335
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001336 map = find_map(state, unitid, control);
1337 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return;
1339
Takashi Iwai561b2202005-09-09 14:22:34 +02001340 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001341 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
Takashi Iwai3360b842014-11-18 11:47:04 +01001343 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 cval->control = control;
1345 cval->cmask = ctl_mask;
Julian Scheelbc18e312015-08-14 16:14:45 +02001346 ctl_info = &audio_feature_info[control-1];
1347 if (state->mixer->protocol == UAC_VERSION_1)
1348 cval->val_type = ctl_info->type;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301349 else /* UAC_VERSION_2 or UAC_VERSION_3*/
Julian Scheelbc18e312015-08-14 16:14:45 +02001350 cval->val_type = ctl_info->type_uac2 >= 0 ?
1351 ctl_info->type_uac2 : ctl_info->type;
1352
Daniel Macka6a33252010-05-31 13:35:37 +02001353 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001355 cval->master_readonly = readonly_mask;
1356 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int i, c = 0;
1358 for (i = 0; i < 16; i++)
1359 if (ctl_mask & (1 << i))
1360 c++;
1361 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001362 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Daniel Mack6bc170e2014-05-24 10:58:16 +02001365 /*
1366 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001367 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001368 * issue write commands to read-only channels.
1369 */
Daniel Macka6a33252010-05-31 13:35:37 +02001370 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001371 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1372 else
1373 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1374
Daniel Mack6bc170e2014-05-24 10:58:16 +02001375 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001376 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 kfree(cval);
1378 return;
1379 }
Chris J Argeseef90452014-11-12 12:07:01 -06001380 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001382 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001384 if (!len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001385 len = snd_usb_copy_string_desc(state, nameid,
1386 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001389 case UAC_FU_MUTE:
1390 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001391 /*
1392 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * - if a name id is given in descriptor, use it.
1394 * - if the connected input can be determined, then use the name
1395 * of terminal type.
1396 * - if the connected output can be determined, use it.
1397 * - otherwise, anonymous name.
1398 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001399 if (!len) {
1400 len = get_term_name(state, iterm, kctl->id.name,
1401 sizeof(kctl->id.name), 1);
1402 if (!len)
1403 len = get_term_name(state, &state->oterm,
1404 kctl->id.name,
1405 sizeof(kctl->id.name), 1);
1406 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001407 snprintf(kctl->id.name, sizeof(kctl->id.name),
1408 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001410
1411 if (!mapped_name)
1412 check_no_speaker_on_headset(kctl, state->mixer->chip->card);
1413
Daniel Mack6bc170e2014-05-24 10:58:16 +02001414 /*
1415 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * if the connected output is USB stream, then it's likely a
1417 * capture stream. otherwise it should be playback (hopefully :)
1418 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001419 if (!mapped_name && !(state->oterm.type >> 16)) {
1420 if ((state->oterm.type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001421 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001422 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001423 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001425 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001426 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001429 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1431 sizeof(kctl->id.name));
1432 break;
1433 }
1434
Takashi Iwaie1825342012-05-14 17:11:06 +02001435 /* get min/max values */
1436 get_min_max_with_quirks(cval, 0, kctl);
1437
1438 if (control == UAC_FU_VOLUME) {
1439 check_mapped_dB(map, cval);
1440 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1441 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1442 kctl->vd[0].access |=
1443 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1444 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1445 }
1446 }
1447
Anssi Hannula42e31212015-12-13 20:49:58 +02001448 snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
1449
Alexey Fisher80aceff2011-03-10 14:53:38 +01001450 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001451 /*
1452 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001453 * to be sure. 384 is a resolution magic number found on Logitech
1454 * devices. It will definitively catch all buggy Logitech devices.
1455 */
1456 if (range > 384) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001457 usb_audio_warn(state->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001458 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001459 range);
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001460 usb_audio_warn(state->chip,
1461 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001462 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001463 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001464 }
1465
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001466 usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001467 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001468 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001469 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Daniel Mackcddaafb2016-04-09 11:21:46 +02001472static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1473 void *_ftr)
1474{
1475 struct uac_clock_source_descriptor *hdr = _ftr;
1476 struct usb_mixer_elem_info *cval;
1477 struct snd_kcontrol *kctl;
1478 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1479 int ret;
1480
1481 if (state->mixer->protocol != UAC_VERSION_2)
1482 return -EINVAL;
1483
1484 if (hdr->bLength != sizeof(*hdr)) {
1485 usb_audio_dbg(state->chip,
1486 "Bogus clock source descriptor length of %d, ignoring.\n",
1487 hdr->bLength);
1488 return 0;
1489 }
1490
1491 /*
1492 * The only property of this unit we are interested in is the
1493 * clock source validity. If that isn't readable, just bail out.
1494 */
1495 if (!uac2_control_is_readable(hdr->bmControls,
1496 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1497 return 0;
1498
1499 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1500 if (!cval)
1501 return -ENOMEM;
1502
1503 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1504
1505 cval->min = 0;
1506 cval->max = 1;
1507 cval->channels = 1;
1508 cval->val_type = USB_MIXER_BOOLEAN;
1509 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1510
1511 if (uac2_control_is_writeable(hdr->bmControls,
1512 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1513 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1514 else {
1515 cval->master_readonly = 1;
1516 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1517 }
1518
1519 if (!kctl) {
1520 kfree(cval);
1521 return -ENOMEM;
1522 }
1523
1524 kctl->private_free = snd_usb_mixer_elem_free;
1525 ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
1526 name, sizeof(name));
1527 if (ret > 0)
1528 snprintf(kctl->id.name, sizeof(kctl->id.name),
1529 "%s Validity", name);
1530 else
1531 snprintf(kctl->id.name, sizeof(kctl->id.name),
1532 "Clock Source %d Validity", hdr->bClockID);
1533
1534 return snd_usb_mixer_add_control(&cval->head, kctl);
1535}
1536
Ajay Agarwal045e3742017-02-07 18:41:34 +05301537static int find_num_channels(struct mixer_build *state, int dir)
1538{
1539 int num_ch = -EINVAL, num, i, j, wMaxPacketSize;
1540 int ctrlif = get_iface_desc(state->mixer->hostif)->bInterfaceNumber;
1541 struct usb_interface *usb_iface =
1542 usb_ifnum_to_if(state->mixer->chip->dev, ctrlif);
1543 struct usb_interface_assoc_descriptor *assoc = usb_iface->intf_assoc;
1544 struct usb_host_interface *alts;
1545
1546 for (i = 0; i < assoc->bInterfaceCount; i++) {
1547 int intf = assoc->bFirstInterface + i;
1548
1549 if (intf != ctrlif) {
1550 struct usb_interface *iface =
1551 usb_ifnum_to_if(state->mixer->chip->dev, intf);
1552
1553 alts = &iface->altsetting[1];
1554 if (dir == USB_DIR_OUT &&
1555 get_endpoint(alts, 0)->bEndpointAddress &
1556 USB_DIR_IN)
1557 continue;
1558 if (dir == USB_DIR_IN &&
1559 !(get_endpoint(alts, 0)->bEndpointAddress &
1560 USB_DIR_IN))
1561 continue;
1562 num = iface->num_altsetting;
1563 for (j = 1; j < num; j++) {
1564 num_ch = NUM_CHANNELS_MONO;
1565 alts = &iface->altsetting[j];
1566 wMaxPacketSize = le16_to_cpu(
1567 get_endpoint(alts, 0)->
1568 wMaxPacketSize);
1569 switch (wMaxPacketSize) {
1570 case BADD_MAXPSIZE_SYNC_MONO_16:
1571 case BADD_MAXPSIZE_SYNC_MONO_24:
1572 case BADD_MAXPSIZE_ASYNC_MONO_16:
1573 case BADD_MAXPSIZE_ASYNC_MONO_24:
1574 break;
1575 case BADD_MAXPSIZE_SYNC_STEREO_16:
1576 case BADD_MAXPSIZE_SYNC_STEREO_24:
1577 case BADD_MAXPSIZE_ASYNC_STEREO_16:
1578 case BADD_MAXPSIZE_ASYNC_STEREO_24:
1579 num_ch = NUM_CHANNELS_STEREO;
1580 break;
1581 }
1582 if (num_ch == NUM_CHANNELS_MONO)
1583 continue;
1584 else
1585 break;
1586 }
1587 }
1588 }
1589
1590 return num_ch;
1591}
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593/*
1594 * parse a feature unit
1595 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001596 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001598static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1599 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001602 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 unsigned int master_bits, first_ch_bits;
1604 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001605 struct uac_feature_unit_descriptor *hdr = _ftr;
1606 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Daniel Mack23caaf12010-03-11 21:13:25 +01001608 if (state->mixer->protocol == UAC_VERSION_1) {
1609 csize = hdr->bControlSize;
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001610 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001611 usb_audio_dbg(state->chip,
1612 "unit %u: invalid bControlSize == 0\n",
1613 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001614 return -EINVAL;
1615 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001616 channels = (hdr->bLength - 7) / csize - 1;
1617 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001618 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001619 usb_audio_err(state->chip,
1620 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1621 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001622 return -EINVAL;
1623 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301624 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001625 struct uac2_feature_unit_descriptor *ftr = _ftr;
1626 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001627 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001628 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001629 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001630 usb_audio_err(state->chip,
1631 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1632 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001633 return -EINVAL;
1634 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301635 } else {
1636 struct usb_interface *usb_iface =
1637 usb_ifnum_to_if(state->mixer->chip->dev,
1638 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1639 struct usb_interface_assoc_descriptor *assoc =
1640 usb_iface->intf_assoc;
1641
1642 csize = 4;
1643 switch (unitid) {
1644 case BADD_FU_ID_BAIOF:
1645 channels = NUM_CHANNELS_MONO;
1646 bmaControls = monoControls;
1647 badd_baif_in_term_desc.wClusterDescrID =
1648 CLUSTER_ID_MONO;
1649 break;
1650
1651 case BADD_FU_ID_BAOF:
1652 switch (assoc->bFunctionSubClass) {
1653 case PROF_HEADPHONE:
1654 case PROF_HEADSET_ADAPTER:
1655 channels = NUM_CHANNELS_STEREO;
1656 bmaControls = stereoControls;
1657 badd_baiof_mu_desc.wClusterDescrID =
1658 CLUSTER_ID_MONO;
1659 break;
1660 case PROF_SPEAKERPHONE:
1661 channels = NUM_CHANNELS_MONO;
1662 bmaControls = monoControls;
1663 badd_baof_in_term_desc.wClusterDescrID =
1664 CLUSTER_ID_MONO;
1665 break;
1666 default:
1667 channels = find_num_channels(state,
1668 USB_DIR_OUT);
1669 if (channels < 0) {
1670 usb_audio_err(state->chip,
1671 "unit %u: Cant find num of channels\n",
1672 unitid);
1673 return channels;
1674 }
1675
1676 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1677 monoControls : stereoControls;
1678 badd_baof_in_term_desc.wClusterDescrID =
1679 (channels == NUM_CHANNELS_MONO) ?
1680 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1681 break;
1682 }
1683 break;
1684
1685 case BADD_FU_ID_BAIF:
1686 switch (assoc->bFunctionSubClass) {
1687 case PROF_HEADSET:
1688 case PROF_HEADSET_ADAPTER:
1689 case PROF_SPEAKERPHONE:
1690 channels = NUM_CHANNELS_MONO;
1691 bmaControls = monoControls;
1692 badd_baif_in_term_desc.wClusterDescrID =
1693 CLUSTER_ID_MONO;
1694 break;
1695 default:
1696 channels = find_num_channels(state, USB_DIR_IN);
1697 if (channels < 0) {
1698 usb_audio_err(state->chip,
1699 "unit %u: Cant find num of channels\n",
1700 unitid);
1701 return channels;
1702 }
1703
1704 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1705 monoControls : stereoControls;
1706 badd_baif_in_term_desc.wClusterDescrID =
1707 (channels == NUM_CHANNELS_MONO) ?
1708 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1709 break;
1710 }
1711 break;
1712
1713 default:
1714 usb_audio_err(state->chip, "Invalid unit %u\n", unitid);
1715 return -EINVAL;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
1719 /* parse the source unit */
Ajay Agarwal045e3742017-02-07 18:41:34 +05301720 if (state->mixer->protocol != UAC_VERSION_3) {
1721 err = parse_audio_unit(state, hdr->bSourceID);
1722 if (err < 0)
1723 return err;
1724 } else {
1725 struct usb_interface *usb_iface =
1726 usb_ifnum_to_if(state->mixer->chip->dev,
1727 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1728 struct usb_interface_assoc_descriptor *assoc =
1729 usb_iface->intf_assoc;
1730
1731 switch (unitid) {
1732 case BADD_FU_ID_BAOF:
1733 switch (assoc->bFunctionSubClass) {
1734 case PROF_HEADSET:
1735 case PROF_HEADSET_ADAPTER:
1736 hdr->bSourceID = BADD_MU_ID_BAIOF;
1737 break;
1738 default:
1739 hdr->bSourceID = BADD_IN_TERM_ID_BAOF;
1740 break;
1741 }
1742 }
1743 err = parse_audio_unit(state, hdr->bSourceID);
1744 if (err < 0)
1745 return err;
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001749 err = check_input_term(state, hdr->bSourceID, &iterm);
1750 if (err < 0)
1751 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Daniel Mack23caaf12010-03-11 21:13:25 +01001753 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001754 /* master configuration quirks */
1755 switch (state->chip->usb_id) {
1756 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001757 usb_audio_info(state->chip,
1758 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001759 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001760 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001761 break;
David Henningssonc1051432012-09-20 10:20:41 +02001762 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001763 usb_audio_info(state->chip,
1764 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001765 /* disable non-functional volume control */
1766 channels = 0;
1767 break;
1768
Javier Kohen0c3cee52009-11-17 15:36:13 +01001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001771 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 else
1773 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001774
1775 if (state->mixer->protocol == UAC_VERSION_1) {
1776 /* check all control types */
1777 for (i = 0; i < 10; i++) {
1778 unsigned int ch_bits = 0;
1779 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001780 unsigned int mask;
1781
1782 mask = snd_usb_combine_bytes(bmaControls +
1783 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001784 if (mask & (1 << i))
1785 ch_bits |= (1 << j);
1786 }
1787 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001788
1789 /*
1790 * The first channel must be set
1791 * (for ease of programming).
1792 */
1793 if (ch_bits & 1)
1794 build_feature_ctl(state, _ftr, ch_bits, i,
1795 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001796 if (master_bits & (1 << i))
Daniel Mack6bc170e2014-05-24 10:58:16 +02001797 build_feature_ctl(state, _ftr, 0, i, &iterm,
1798 unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301800 } else { /* UAC_VERSION_2 or UAC_VERSION_3*/
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001801 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001802 unsigned int ch_bits = 0;
1803 unsigned int ch_read_only = 0;
1804
1805 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001806 unsigned int mask;
1807
1808 mask = snd_usb_combine_bytes(bmaControls +
1809 csize * (j+1), csize);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001810 if (uac2_control_is_readable(mask, i)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001811 ch_bits |= (1 << j);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001812 if (!uac2_control_is_writeable(mask, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001813 ch_read_only |= (1 << j);
1814 }
1815 }
1816
Daniel Mack6bc170e2014-05-24 10:58:16 +02001817 /*
1818 * NOTE: build_feature_ctl() will mark the control
1819 * read-only if all channels are marked read-only in
1820 * the descriptors. Otherwise, the control will be
1821 * reported as writeable, but the driver will not
1822 * actually issue a write command for read-only
1823 * channels.
1824 */
1825
1826 /*
1827 * The first channel must be set
1828 * (for ease of programming).
1829 */
1830 if (ch_bits & 1)
1831 build_feature_ctl(state, _ftr, ch_bits, i,
1832 &iterm, unitid, ch_read_only);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001833 if (uac2_control_is_readable(master_bits, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001834 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001835 !uac2_control_is_writeable(master_bits, i));
Daniel Mack23caaf12010-03-11 21:13:25 +01001836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
1839 return 0;
1840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842/*
1843 * Mixer Unit
1844 */
1845
1846/*
1847 * build a mixer unit control
1848 *
1849 * the callbacks are identical with feature unit.
1850 * input channel number (zero based) is given in control field instead.
1851 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001852static void build_mixer_unit_ctl(struct mixer_build *state,
1853 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001855 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001857 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001858 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001860 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001861 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001863 map = find_map(state, unitid, 0);
1864 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return;
1866
Takashi Iwai561b2202005-09-09 14:22:34 +02001867 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001868 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return;
1870
Takashi Iwai3360b842014-11-18 11:47:04 +01001871 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 cval->control = in_ch + 1; /* based on 1 */
1873 cval->val_type = USB_MIXER_S16;
1874 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001875 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1876
1877 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 cval->cmask |= (1 << i);
1879 cval->channels++;
1880 }
1881 }
1882
1883 /* get min/max values */
1884 get_min_max(cval, 0);
1885
1886 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001887 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001888 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 kfree(cval);
1890 return;
1891 }
Chris J Argeseef90452014-11-12 12:07:01 -06001892 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001894 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02001895 if (!len)
1896 len = get_term_name(state, iterm, kctl->id.name,
1897 sizeof(kctl->id.name), 0);
1898 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001900 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001902 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001903 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
1904 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907/*
1908 * parse a mixer unit
1909 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001910static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
1911 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Daniel Mack99fc8642010-03-11 21:13:24 +01001913 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001914 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 int input_pins, num_ins, num_outs;
1916 int pin, ich, err;
1917
Ajay Agarwal045e3742017-02-07 18:41:34 +05301918 if (state->mixer->protocol == UAC_VERSION_3) {
1919 input_pins = badd_baiof_mu_desc.bNrInPins;
1920 num_outs =
1921 (badd_baiof_mu_desc.wClusterDescrID == CLUSTER_ID_MONO) ?
1922 NUM_CHANNELS_MONO : NUM_CHANNELS_STEREO;
1923 } else {
1924 input_pins = desc->bNrInPins;
1925 num_outs = uac_mixer_unit_bNrChannels(desc);
1926 if (desc->bLength < 11 || !input_pins || !num_outs) {
1927 usb_audio_err(state->chip,
1928 "invalid MIXER UNIT descriptor %d\n",
1929 unitid);
1930 return -EINVAL;
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 num_ins = 0;
1935 ich = 0;
1936 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001937 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01001939 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02001940 /* no bmControls field (e.g. Maya44) -> ignore */
1941 if (desc->bLength <= 10 + input_pins)
1942 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01001943 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (err < 0)
1945 return err;
1946 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001947 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int och, ich_has_controls = 0;
1949
Daniel Mack6bc170e2014-05-24 10:58:16 +02001950 for (och = 0; och < num_outs; och++) {
Ajay Agarwal045e3742017-02-07 18:41:34 +05301951 __u8 *c = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001952
Ajay Agarwal045e3742017-02-07 18:41:34 +05301953 if (state->mixer->protocol == UAC_VERSION_3)
1954 c =
1955 &(badd_baiof_mu_desc.bmMixerControls);
1956 else
1957 c = uac_mixer_unit_bmControls(desc,
1958 state->mixer->protocol);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001959 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 ich_has_controls = 1;
1961 break;
1962 }
1963 }
1964 if (ich_has_controls)
1965 build_mixer_unit_ctl(state, desc, pin, ich,
1966 unitid, &iterm);
1967 }
1968 }
1969 return 0;
1970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/*
1973 * Processing Unit / Extension Unit
1974 */
1975
1976/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001977static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001980 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int err, val;
1982
1983 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01001984 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01001986 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 val = get_relative_value(cval, val);
1989 ucontrol->value.integer.value[0] = val;
1990 return 0;
1991}
1992
1993/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001994static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
1995 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001997 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int val, oval, err;
1999
2000 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002001 if (err < 0)
2002 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 val = ucontrol->value.integer.value[0];
2004 val = get_abs_value(cval, val);
2005 if (val != oval) {
2006 set_cur_ctl_value(cval, cval->control << 8, val);
2007 return 1;
2008 }
2009 return 0;
2010}
2011
2012/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002013static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2015 .name = "", /* will be filled later */
2016 .info = mixer_ctl_feature_info,
2017 .get = mixer_ctl_procunit_get,
2018 .put = mixer_ctl_procunit_put,
2019};
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/*
2022 * predefined data for processing units
2023 */
2024struct procunit_value_info {
2025 int control;
2026 char *suffix;
2027 int val_type;
2028 int min_value;
2029};
2030
2031struct procunit_info {
2032 int type;
2033 char *name;
2034 struct procunit_value_info *values;
2035};
2036
2037static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002038 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2039 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 { 0 }
2041};
2042static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002043 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2044 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 { 0 }
2046};
2047static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002048 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2049 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 { 0 }
2051};
2052static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002053 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2054 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2055 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2056 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 { 0 }
2058};
2059static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002060 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2061 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2062 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2063 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 { 0 }
2065};
2066static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002067 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2068 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2069 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2070 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2071 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2072 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 { 0 }
2074};
2075
2076static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002077 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2078 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2079 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2080 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2081 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2082 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 { 0 },
2084};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002085/*
2086 * predefined data for extension units
2087 */
2088static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002089 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2090 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002091};
2092static struct procunit_value_info clock_source_xu_info[] = {
2093 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2094 { 0 }
2095};
2096static struct procunit_value_info spdif_format_xu_info[] = {
2097 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2098 { 0 }
2099};
2100static struct procunit_value_info soft_limit_xu_info[] = {
2101 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2102 { 0 }
2103};
2104static struct procunit_info extunits[] = {
2105 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2106 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2107 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2108 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2109 { 0 }
2110};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/*
2113 * build a processing/extension unit
2114 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002115static int build_audio_procunit(struct mixer_build *state, int unitid,
2116 void *raw_desc, struct procunit_info *list,
2117 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Daniel Mack99fc8642010-03-11 21:13:24 +01002119 struct uac_processing_unit_descriptor *desc = raw_desc;
2120 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002121 struct usb_mixer_elem_info *cval;
2122 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int i, err, nameid, type, len;
2124 struct procunit_info *info;
2125 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002126 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 static struct procunit_value_info default_value_info[] = {
2128 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2129 { 0 }
2130 };
2131 static struct procunit_info default_info = {
2132 0, NULL, default_value_info
2133 };
2134
Daniel Mack23caaf12010-03-11 21:13:25 +01002135 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2136 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002137 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -EINVAL;
2139 }
2140
2141 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002142 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return err;
2144 }
2145
Daniel Mack99fc8642010-03-11 21:13:24 +01002146 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 for (info = list; info && info->type; info++)
2148 if (info->type == type)
2149 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002150 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 info = &default_info;
2152
2153 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002154 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002155
Daniel Mack6bc170e2014-05-24 10:58:16 +02002156 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002158 map = find_map(state, unitid, valinfo->control);
2159 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002161 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002162 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002164 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 cval->control = valinfo->control;
2166 cval->val_type = valinfo->val_type;
2167 cval->channels = 1;
2168
2169 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002170 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002171 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* FIXME: hard-coded */
2173 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002174 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 cval->res = 1;
2176 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002177 } else {
2178 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002179 /*
2180 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002181 * samplerate control quirk
2182 */
2183 cval->min = 0;
2184 cval->max = 5;
2185 cval->res = 1;
2186 cval->initialized = 1;
2187 } else
2188 get_min_max(cval, valinfo->min_value);
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002192 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 kfree(cval);
2194 return -ENOMEM;
2195 }
Chris J Argeseef90452014-11-12 12:07:01 -06002196 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Daniel Mack6bc170e2014-05-24 10:58:16 +02002198 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002199 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002200 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002202 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002203 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 len = 0;
2205 if (nameid)
Daniel Mack6bc170e2014-05-24 10:58:16 +02002206 len = snd_usb_copy_string_desc(state, nameid,
2207 kctl->id.name,
2208 sizeof(kctl->id.name));
2209 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2211 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002212 append_ctl_name(kctl, " ");
2213 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002215 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002216 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002217 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002218 cval->min, cval->max);
2219
Takashi Iwai3360b842014-11-18 11:47:04 +01002220 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002221 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return err;
2223 }
2224 return 0;
2225}
2226
Daniel Mack6bc170e2014-05-24 10:58:16 +02002227static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2228 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002230 return build_audio_procunit(state, unitid, raw_desc,
2231 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Daniel Mack6bc170e2014-05-24 10:58:16 +02002234static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2235 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002237 /*
2238 * Note that we parse extension units with processing unit descriptors.
2239 * That's ok as the layout is the same.
2240 */
2241 return build_audio_procunit(state, unitid, raw_desc,
2242 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245/*
2246 * Selector Unit
2247 */
2248
Daniel Mack6bc170e2014-05-24 10:58:16 +02002249/*
2250 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 * use an enumerator type for routing
2252 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002253static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002256 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002257 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Takashi Iwai5e246b82008-08-08 17:12:47 +02002259 if (snd_BUG_ON(!itemlist))
2260 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002261 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002265static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002268 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 int val, err;
2270
Daniel Mack09414202010-05-31 13:35:44 +02002271 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002273 ucontrol->value.enumerated.item[0] = 0;
2274 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 val = get_relative_value(cval, val);
2277 ucontrol->value.enumerated.item[0] = val;
2278 return 0;
2279}
2280
2281/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002282static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2283 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002285 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 int val, oval, err;
2287
Daniel Mack09414202010-05-31 13:35:44 +02002288 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002289 if (err < 0)
2290 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 val = ucontrol->value.enumerated.item[0];
2292 val = get_abs_value(cval, val);
2293 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002294 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 1;
2296 }
2297 return 0;
2298}
2299
2300/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002301static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2303 .name = "", /* will be filled later */
2304 .info = mixer_ctl_selector_info,
2305 .get = mixer_ctl_selector_get,
2306 .put = mixer_ctl_selector_put,
2307};
2308
Daniel Mack6bc170e2014-05-24 10:58:16 +02002309/*
2310 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * free both private_data and private_value
2312 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002313static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 int i, num_ins = 0;
2316
2317 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002318 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 num_ins = cval->max;
2320 kfree(cval);
2321 kctl->private_data = NULL;
2322 }
2323 if (kctl->private_value) {
2324 char **itemlist = (char **)kctl->private_value;
2325 for (i = 0; i < num_ins; i++)
2326 kfree(itemlist[i]);
2327 kfree(itemlist);
2328 kctl->private_value = 0;
2329 }
2330}
2331
2332/*
2333 * parse a selector unit
2334 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002335static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2336 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Daniel Mack99fc8642010-03-11 21:13:24 +01002338 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 unsigned int i, nameid, len;
2340 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002341 struct usb_mixer_elem_info *cval;
2342 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002343 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 char **namelist;
2345
Daniel Mack99fc8642010-03-11 21:13:24 +01002346 if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002347 usb_audio_err(state->chip,
2348 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return -EINVAL;
2350 }
2351
Daniel Mack99fc8642010-03-11 21:13:24 +01002352 for (i = 0; i < desc->bNrInPins; i++) {
2353 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return err;
2355 }
2356
Daniel Mack99fc8642010-03-11 21:13:24 +01002357 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return 0;
2359
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002360 map = find_map(state, unitid, 0);
2361 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return 0;
2363
Takashi Iwai561b2202005-09-09 14:22:34 +02002364 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002365 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002367 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 cval->val_type = USB_MIXER_U8;
2369 cval->channels = 1;
2370 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002371 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 cval->res = 1;
2373 cval->initialized = 1;
2374
Johan Rasténa358a0e2014-11-17 08:39:33 +01002375 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002376 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002377 else /* UAC_VERSION_2 */
2378 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2379 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002380
Daniel Mack99fc8642010-03-11 21:13:24 +01002381 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002382 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 kfree(cval);
2384 return -ENOMEM;
2385 }
2386#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002387 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002388 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 len = 0;
2390 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002391 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002392 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 kfree(namelist[i]);
2394 kfree(namelist);
2395 kfree(cval);
2396 return -ENOMEM;
2397 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002398 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2399 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002400 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
2402 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002403 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405
2406 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2407 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002408 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002409 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 kfree(cval);
2411 return -ENOMEM;
2412 }
2413 kctl->private_value = (unsigned long)namelist;
2414 kctl->private_free = usb_mixer_selector_elem_free;
2415
Daniel Mack99fc8642010-03-11 21:13:24 +01002416 nameid = uac_selector_unit_iSelector(desc);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002417 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (len)
2419 ;
2420 else if (nameid)
Daniel Mack6bc170e2014-05-24 10:58:16 +02002421 snd_usb_copy_string_desc(state, nameid, kctl->id.name,
2422 sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 else {
2424 len = get_term_name(state, &state->oterm,
2425 kctl->id.name, sizeof(kctl->id.name), 0);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002426 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2428
Daniel Mack09414202010-05-31 13:35:44 +02002429 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2430 append_ctl_name(kctl, " Clock Source");
2431 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002432 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002434 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002437 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002438 cval->head.id, kctl->id.name, desc->bNrInPins);
2439 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/*
2443 * parse an audio unit recursively
2444 */
2445
Takashi Iwai86e07d32005-11-17 15:08:02 +01002446static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448 unsigned char *p1;
2449
2450 if (test_and_set_bit(unitid, state->unitbitmap))
2451 return 0; /* the unit already visited */
2452
2453 p1 = find_audio_control_unit(state, unitid);
2454 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002455 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return -EINVAL;
2457 }
2458
2459 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01002460 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01002462 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackcddaafb2016-04-09 11:21:46 +02002464 case UAC2_CLOCK_SOURCE:
2465 return parse_clock_source_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002466 case UAC_SELECTOR_UNIT:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302467 /* UAC3_MIXER_UNIT_V3 has the same value */
Daniel Mack09414202010-05-31 13:35:44 +02002468 case UAC2_CLOCK_SELECTOR:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302469 /* UAC3_CLOCK_SOURCE has the same value */
2470 if (state->mixer->protocol == UAC_VERSION_3 &&
2471 p1[2] == UAC3_CLOCK_SOURCE)
2472 return 0; /* NOP */
2473 else if (state->mixer->protocol == UAC_VERSION_3
2474 && p1[2] == UAC3_MIXER_UNIT_V3)
2475 return parse_audio_mixer_unit(state, unitid, p1);
2476 else
2477 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002478 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002480 case UAC1_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002481 /* UAC2_EFFECT_UNIT has the same value */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302482 /* UAC3_FEATURE_UNIT_V3 has the same value */
Daniel Mack23caaf12010-03-11 21:13:25 +01002483 if (state->mixer->protocol == UAC_VERSION_1)
2484 return parse_audio_processing_unit(state, unitid, p1);
Ajay Agarwal045e3742017-02-07 18:41:34 +05302485 else if (state->mixer->protocol == UAC_VERSION_2)
Daniel Mack23caaf12010-03-11 21:13:25 +01002486 return 0; /* FIXME - effect units not implemented yet */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302487 else
2488 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002489 case UAC1_EXTENSION_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002490 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2491 if (state->mixer->protocol == UAC_VERSION_1)
2492 return parse_audio_extension_unit(state, unitid, p1);
2493 else /* UAC_VERSION_2 */
2494 return parse_audio_processing_unit(state, unitid, p1);
Torstein Hegge61ac5132013-03-19 17:12:14 +01002495 case UAC2_EXTENSION_UNIT_V2:
2496 return parse_audio_extension_unit(state, unitid, p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002498 usb_audio_err(state->chip,
2499 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return -EINVAL;
2501 }
2502}
2503
Clemens Ladisch84957a82005-04-29 16:23:13 +02002504static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2505{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002506 kfree(mixer->id_elems);
2507 if (mixer->urb) {
2508 kfree(mixer->urb->transfer_buffer);
2509 usb_free_urb(mixer->urb);
2510 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002511 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002512 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002513 kfree(mixer);
2514}
2515
Takashi Iwai86e07d32005-11-17 15:08:02 +01002516static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002517{
2518 struct usb_mixer_interface *mixer = device->device_data;
2519 snd_usb_mixer_free(mixer);
2520 return 0;
2521}
2522
Ajay Agarwal045e3742017-02-07 18:41:34 +05302523static int make_out_term(struct mixer_build state, int wTerminalType)
2524{
2525 struct uac3_output_terminal_descriptor *desc = NULL;
2526
2527 if (wTerminalType == UAC_TERMINAL_STREAMING)
2528 desc = &badd_baif_out_term_desc;
2529 else {
2530 desc = &badd_baof_out_term_desc;
2531 desc->wTerminalType = wTerminalType;
2532 }
2533 set_bit(desc->bTerminalID, state.unitbitmap);
2534 state.oterm.id = desc->bTerminalID;
2535 state.oterm.type = desc->wTerminalType;
2536 state.oterm.name = desc->wTerminalDescrStr;
2537 return parse_audio_unit(&state, desc->bSourceID);
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540/*
2541 * create mixer controls
2542 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002543 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002545static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002547 struct mixer_build state;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302548 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 const struct usbmix_ctl_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002552 state.chip = mixer->chip;
2553 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002554 state.buffer = mixer->hostif->extra;
2555 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002558 for (map = usbmix_ctl_maps; map->id; map++) {
2559 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002561 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002562 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 break;
2564 }
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Ajay Agarwal045e3742017-02-07 18:41:34 +05302567 if (mixer->protocol == UAC_VERSION_3) {
2568 struct usb_interface *usb_iface =
2569 usb_ifnum_to_if(mixer->chip->dev,
2570 get_iface_desc(mixer->hostif)->bInterfaceNumber);
2571 struct usb_interface_assoc_descriptor *assoc =
2572 usb_iface->intf_assoc;
Daniel Mack23caaf12010-03-11 21:13:25 +01002573
Ajay Agarwal045e3742017-02-07 18:41:34 +05302574 switch (assoc->bFunctionSubClass) {
2575 case PROF_GENERIC_IO: {
2576 if (assoc->bInterfaceCount == 0x02) {
2577 if (get_endpoint(mixer->hostif,
2578 0)->bEndpointAddress | USB_DIR_IN)
2579 err = make_out_term(state,
2580 UAC_TERMINAL_STREAMING);
2581 else
2582 err = make_out_term(state,
2583 UAC_OUTPUT_TERMINAL_UNDEFINED);
2584 } else {
2585 err = make_out_term(state,
2586 UAC_OUTPUT_TERMINAL_UNDEFINED);
2587 if (err < 0 && err != -EINVAL)
2588 return err;
2589 err = make_out_term(state,
2590 UAC_TERMINAL_STREAMING);
2591 }
2592 break;
2593 }
2594
2595 case PROF_HEADPHONE:
2596 err = make_out_term(state,
2597 UAC_OUTPUT_TERMINAL_HEADPHONES);
2598 break;
2599 case PROF_SPEAKER:
2600 err = make_out_term(state, UAC_OUTPUT_TERMINAL_SPEAKER);
2601 break;
2602 case PROF_MICROPHONE:
2603 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2604 break;
2605 case PROF_HEADSET:
2606 case PROF_HEADSET_ADAPTER:
2607 err = make_out_term(state, UAC_BIDIR_TERMINAL_HEADSET);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002608 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002609 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302610 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2611 break;
2612 case PROF_SPEAKERPHONE:
2613 err = make_out_term(state,
2614 UAC_BIDIR_TERMINAL_SPEAKERPHONE);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002615 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002616 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302617 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2618 break;
2619 }
2620 if (err < 0 && err != -EINVAL)
2621 return err;
2622 } else {
2623 void *p;
Daniel Mack09414202010-05-31 13:35:44 +02002624
Ajay Agarwal045e3742017-02-07 18:41:34 +05302625 p = NULL;
2626 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2627 mixer->hostif->extralen, p,
2628 UAC_OUTPUT_TERMINAL)) != NULL) {
2629 if (mixer->protocol == UAC_VERSION_1) {
2630 struct uac1_output_terminal_descriptor *desc =
2631 p;
2632
2633 if (desc->bLength < sizeof(*desc))
2634 continue; /* invalid descriptor? */
2635 /* mark terminal ID as visited */
2636 set_bit(desc->bTerminalID, state.unitbitmap);
2637 state.oterm.id = desc->bTerminalID;
2638 state.oterm.type =
2639 le16_to_cpu(desc->wTerminalType);
2640 state.oterm.name = desc->iTerminal;
2641 err = parse_audio_unit(&state, desc->bSourceID);
2642 if (err < 0 && err != -EINVAL)
2643 return err;
2644 } else { /* UAC_VERSION_2 */
2645 struct uac2_output_terminal_descriptor *desc =
2646 p;
2647
2648 if (desc->bLength < sizeof(*desc))
2649 continue; /* invalid descriptor? */
2650 /* mark terminal ID as visited */
2651 set_bit(desc->bTerminalID, state.unitbitmap);
2652 state.oterm.id = desc->bTerminalID;
2653 state.oterm.type =
2654 le16_to_cpu(desc->wTerminalType);
2655 state.oterm.name = desc->iTerminal;
2656 err = parse_audio_unit(&state, desc->bSourceID);
2657 if (err < 0 && err != -EINVAL)
2658 return err;
2659
2660 /*
2661 * For UAC2, use the same approach to also add
2662 * the clock selectors
2663 */
2664 err = parse_audio_unit(&state,
2665 desc->bCSourceID);
2666 if (err < 0 && err != -EINVAL)
2667 return err;
2668 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return 0;
2673}
Clemens Ladisch84957a82005-04-29 16:23:13 +02002674
Daniel Mack7b1eda22010-03-11 21:13:22 +01002675void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002676{
Takashi Iwai3360b842014-11-18 11:47:04 +01002677 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002678
Takashi Iwai3360b842014-11-18 11:47:04 +01002679 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002680 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002681 &list->kctl->id);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002682}
2683
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002684static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01002685 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002686{
Takashi Iwai3360b842014-11-18 11:47:04 +01002687 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002688 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
2689 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002690 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01002691 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002692 cval->control, cval->cmask, cval->channels,
2693 val_types[cval->val_type]);
2694 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
2695 cval->min, cval->max, cval->dBmin, cval->dBmax);
2696}
2697
2698static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
2699 struct snd_info_buffer *buffer)
2700{
2701 struct snd_usb_audio *chip = entry->private_data;
2702 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01002703 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002704 int unitid;
2705
2706 list_for_each_entry(mixer, &chip->mixer_list, list) {
2707 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002708 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02002709 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002710 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002711 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
2712 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01002713 for (list = mixer->id_elems[unitid]; list;
2714 list = list->next_id_elem) {
2715 snd_iprintf(buffer, " Unit: %i\n", list->id);
2716 if (list->kctl)
2717 snd_iprintf(buffer,
2718 " Control: name=\"%s\", index=%i\n",
2719 list->kctl->id.name,
2720 list->kctl->id.index);
2721 if (list->dump)
2722 list->dump(buffer, list);
2723 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002724 }
2725 }
2726}
2727
Daniel Macke213e9c2010-05-11 18:13:50 +02002728static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
2729 int attribute, int value, int index)
2730{
Takashi Iwai3360b842014-11-18 11:47:04 +01002731 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02002732 __u8 unitid = (index >> 8) & 0xff;
2733 __u8 control = (value >> 8) & 0xff;
2734 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02002735 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02002736
2737 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002738 usb_audio_dbg(mixer->chip,
2739 "%s(): bogus channel number %d\n",
2740 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02002741 return;
2742 }
2743
Daniel Mack191227d2016-04-08 19:52:02 +02002744 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
2745 count++;
2746
2747 if (count == 0)
2748 return;
2749
Takashi Iwai3360b842014-11-18 11:47:04 +01002750 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
2751 struct usb_mixer_elem_info *info;
2752
2753 if (!list->kctl)
2754 continue;
2755
2756 info = (struct usb_mixer_elem_info *)list;
Daniel Mack191227d2016-04-08 19:52:02 +02002757 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02002758 continue;
2759
2760 switch (attribute) {
2761 case UAC2_CS_CUR:
2762 /* invalidate cache, so the value is read from the device */
2763 if (channel)
2764 info->cached &= ~(1 << channel);
2765 else /* master channel */
2766 info->cached = 0;
2767
2768 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002769 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02002770 break;
2771
2772 case UAC2_CS_RANGE:
2773 /* TODO */
2774 break;
2775
2776 case UAC2_CS_MEM:
2777 /* TODO */
2778 break;
2779
2780 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002781 usb_audio_dbg(mixer->chip,
2782 "unknown attribute %d in interrupt\n",
2783 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02002784 break;
2785 } /* switch */
2786 }
2787}
2788
2789static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002790{
2791 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02002792 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01002793 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002794
Oliver Neukumedf7de32011-03-11 13:19:43 +01002795 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02002796 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002797
Daniel Macke213e9c2010-05-11 18:13:50 +02002798 if (mixer->protocol == UAC_VERSION_1) {
2799 struct uac1_status_word *status;
2800
2801 for (status = urb->transfer_buffer;
2802 len >= sizeof(*status);
2803 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002804 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02002805 status->bStatusType,
2806 status->bOriginator);
2807
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002808 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02002809 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2810 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002811 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02002812
2813 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
2814 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02002815 else
Daniel Macke213e9c2010-05-11 18:13:50 +02002816 snd_usb_mixer_notify_id(mixer, status->bOriginator);
2817 }
2818 } else { /* UAC_VERSION_2 */
2819 struct uac2_interrupt_data_msg *msg;
2820
2821 for (msg = urb->transfer_buffer;
2822 len >= sizeof(*msg);
2823 len -= sizeof(*msg), msg++) {
2824 /* drop vendor specific and endpoint requests */
2825 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
2826 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
2827 continue;
2828
2829 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
2830 le16_to_cpu(msg->wValue),
2831 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002832 }
2833 }
Daniel Macke213e9c2010-05-11 18:13:50 +02002834
2835requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02002836 if (ustatus != -ENOENT &&
2837 ustatus != -ECONNRESET &&
2838 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002839 urb->dev = mixer->chip->dev;
2840 usb_submit_urb(urb, GFP_ATOMIC);
2841 }
2842}
2843
2844/* create the handler for the optional status interrupt endpoint */
2845static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2846{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002847 struct usb_endpoint_descriptor *ep;
2848 void *transfer_buffer;
2849 int buffer_length;
2850 unsigned int epnum;
2851
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002852 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02002853 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002854 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002855 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002856 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002857 return 0;
2858
Julia Lawall42a6e662008-12-29 11:23:02 +01002859 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002860 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
2861 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
2862 if (!transfer_buffer)
2863 return -ENOMEM;
2864 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2865 if (!mixer->urb) {
2866 kfree(transfer_buffer);
2867 return -ENOMEM;
2868 }
2869 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
2870 usb_rcvintpipe(mixer->chip->dev, epnum),
2871 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02002872 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002873 usb_submit_urb(mixer->urb, GFP_KERNEL);
2874 return 0;
2875}
2876
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002877int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2878 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002879{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002880 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002881 .dev_free = snd_usb_mixer_dev_free
2882 };
2883 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002884 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01002885 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002886
2887 strcpy(chip->card->mixername, "USB Mixer");
2888
Takashi Iwai561b2202005-09-09 14:22:34 +02002889 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002890 if (!mixer)
2891 return -ENOMEM;
2892 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002893 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002894 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2895 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002896 if (!mixer->id_elems) {
2897 kfree(mixer);
2898 return -ENOMEM;
2899 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002900
Daniel Mack1faa5d02011-08-04 15:56:28 +02002901 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2902 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02002903 case UAC_VERSION_1:
2904 default:
2905 mixer->protocol = UAC_VERSION_1;
2906 break;
2907 case UAC_VERSION_2:
2908 mixer->protocol = UAC_VERSION_2;
2909 break;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302910 case UAC_VERSION_3:
2911 mixer->protocol = UAC_VERSION_3;
2912 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02002913 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01002914
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002915 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002916 (err = snd_usb_mixer_status_create(mixer)) < 0)
2917 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002918
Daniel Mack7b1eda22010-03-11 21:13:22 +01002919 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002920
Takashi Iwai9cbb2802014-02-04 11:15:31 +01002921 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002922 if (err < 0)
2923 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002924
2925 if (list_empty(&chip->mixer_list) &&
2926 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2927 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2928
Clemens Ladisch84957a82005-04-29 16:23:13 +02002929 list_add(&mixer->list, &chip->mixer_list);
2930 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002931
2932_error:
2933 snd_usb_mixer_free(mixer);
2934 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002935}
2936
Takashi Iwaia6cece92014-10-31 11:24:32 +01002937void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002938{
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002939 usb_kill_urb(mixer->urb);
2940 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002941}
Takashi Iwai400362f2014-01-20 16:51:16 +01002942
2943#ifdef CONFIG_PM
2944/* stop any bus activity of a mixer */
2945static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
2946{
2947 usb_kill_urb(mixer->urb);
2948 usb_kill_urb(mixer->rc_urb);
2949}
2950
2951static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
2952{
2953 int err;
2954
2955 if (mixer->urb) {
2956 err = usb_submit_urb(mixer->urb, GFP_NOIO);
2957 if (err < 0)
2958 return err;
2959 }
2960
2961 return 0;
2962}
2963
2964int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
2965{
2966 snd_usb_mixer_inactivate(mixer);
2967 return 0;
2968}
2969
Takashi Iwai3360b842014-11-18 11:47:04 +01002970static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01002971{
Takashi Iwai3360b842014-11-18 11:47:04 +01002972 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Takashi Iwai400362f2014-01-20 16:51:16 +01002973 int c, err, idx;
2974
2975 if (cval->cmask) {
2976 idx = 0;
2977 for (c = 0; c < MAX_CHANNELS; c++) {
2978 if (!(cval->cmask & (1 << c)))
2979 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08002980 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06002981 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01002982 cval->cache_val[idx]);
2983 if (err < 0)
2984 return err;
2985 }
2986 idx++;
2987 }
2988 } else {
2989 /* master */
2990 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06002991 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01002992 if (err < 0)
2993 return err;
2994 }
2995 }
2996
2997 return 0;
2998}
2999
3000int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3001{
Takashi Iwai3360b842014-11-18 11:47:04 +01003002 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003003 int id, err;
3004
Takashi Iwai400362f2014-01-20 16:51:16 +01003005 if (reset_resume) {
3006 /* restore cached mixer values */
3007 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003008 for (list = mixer->id_elems[id]; list;
3009 list = list->next_id_elem) {
3010 if (list->resume) {
3011 err = list->resume(list);
3012 if (err < 0)
3013 return err;
3014 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003015 }
3016 }
3017 }
3018
3019 return snd_usb_mixer_activate(mixer);
3020}
3021#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003022
3023void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3024 struct usb_mixer_interface *mixer,
3025 int unitid)
3026{
3027 list->mixer = mixer;
3028 list->id = unitid;
3029 list->dump = snd_usb_mixer_dump_cval;
3030#ifdef CONFIG_PM
3031 list->resume = restore_mixer_value;
3032#endif
3033}