blob: 3501ff9053ee31fa3603b86447722cc3ca86915a [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return len;
220}
221
222/*
223 * convert from the byte/word on usb descriptor to the zero-based integer
224 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100225static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 switch (cval->val_type) {
228 case USB_MIXER_BOOLEAN:
229 return !!val;
230 case USB_MIXER_INV_BOOLEAN:
231 return !val;
232 case USB_MIXER_U8:
233 val &= 0xff;
234 break;
235 case USB_MIXER_S8:
236 val &= 0xff;
237 if (val >= 0x80)
238 val -= 0x100;
239 break;
240 case USB_MIXER_U16:
241 val &= 0xffff;
242 break;
243 case USB_MIXER_S16:
244 val &= 0xffff;
245 if (val >= 0x8000)
246 val -= 0x10000;
247 break;
248 }
249 return val;
250}
251
252/*
253 * convert from the zero-based int to the byte/word for usb descriptor
254 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100255static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 switch (cval->val_type) {
258 case USB_MIXER_BOOLEAN:
259 return !!val;
260 case USB_MIXER_INV_BOOLEAN:
261 return !val;
262 case USB_MIXER_S8:
263 case USB_MIXER_U8:
264 return val & 0xff;
265 case USB_MIXER_S16:
266 case USB_MIXER_U16:
267 return val & 0xffff;
268 }
269 return 0; /* not reached */
270}
271
Takashi Iwai86e07d32005-11-17 15:08:02 +0100272static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200274 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 cval->res = 1;
276 if (val < cval->min)
277 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200278 else if (val >= cval->max)
279 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 else
281 return (val - cval->min) / cval->res;
282}
283
Takashi Iwai86e07d32005-11-17 15:08:02 +0100284static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 if (val < 0)
287 return cval->min;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200288 if (!cval->res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 cval->res = 1;
290 val *= cval->res;
291 val += cval->min;
292 if (val > cval->max)
293 return cval->max;
294 return val;
295}
296
Julian Scheelbc18e312015-08-14 16:14:45 +0200297static int uac2_ctl_value_size(int val_type)
298{
299 switch (val_type) {
300 case USB_MIXER_S32:
301 case USB_MIXER_U32:
302 return 4;
303 case USB_MIXER_S16:
304 case USB_MIXER_U16:
305 return 2;
306 default:
307 return 1;
308 }
309 return 0; /* unreachable */
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313/*
314 * retrieve a mixer value
315 */
316
Daniel Mack6bc170e2014-05-24 10:58:16 +0200317static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
318 int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Takashi Iwai3360b842014-11-18 11:47:04 +0100320 struct snd_usb_audio *chip = cval->head.mixer->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned char buf[2];
322 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
323 int timeout = 10;
Takashi Iwai978520b2012-10-12 15:12:55 +0200324 int idx = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Takashi Iwai47ab1542015-08-25 16:09:00 +0200326 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100327 if (err < 0)
328 return -EIO;
Daniel Mack6bc170e2014-05-24 10:58:16 +0200329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100331 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200332 if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200334 validx, idx, buf, val_len) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
Takashi Iwai978520b2012-10-12 15:12:55 +0200336 err = 0;
337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100340 usb_audio_dbg(chip,
341 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
342 request, validx, idx, cval->val_type);
Takashi Iwai978520b2012-10-12 15:12:55 +0200343 err = -EINVAL;
344
345 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200346 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Daniel Mack6bc170e2014-05-24 10:58:16 +0200350static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
351 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100352{
Takashi Iwai3360b842014-11-18 11:47:04 +0100353 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200354 unsigned char buf[4 + 3 * sizeof(__u32)]; /* enough space for one range */
Daniel Mack23caaf12010-03-11 21:13:25 +0100355 unsigned char *val;
Takashi Iwai978520b2012-10-12 15:12:55 +0200356 int idx = 0, ret, size;
Daniel Mack23caaf12010-03-11 21:13:25 +0100357 __u8 bRequest;
358
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200359 if (request == UAC_GET_CUR) {
360 bRequest = UAC2_CS_CUR;
Julian Scheelbc18e312015-08-14 16:14:45 +0200361 size = uac2_ctl_value_size(cval->val_type);
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200362 } else {
363 bRequest = UAC2_CS_RANGE;
364 size = sizeof(buf);
365 }
366
367 memset(buf, 0, sizeof(buf));
Daniel Mack23caaf12010-03-11 21:13:25 +0100368
Takashi Iwai47ab1542015-08-25 16:09:00 +0200369 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100370 if (ret)
371 goto error;
372
Takashi Iwai47ab1542015-08-25 16:09:00 +0200373 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
374 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
Daniel Mack23caaf12010-03-11 21:13:25 +0100375 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Takashi Iwai978520b2012-10-12 15:12:55 +0200376 validx, idx, buf, size);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200377 snd_usb_unlock_shutdown(chip);
Daniel Mack23caaf12010-03-11 21:13:25 +0100378
379 if (ret < 0) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100380error:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100381 usb_audio_err(chip,
382 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
383 request, validx, idx, cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100384 return ret;
385 }
386
Daniel Macke8bdb6b2010-06-11 17:34:22 +0200387 /* FIXME: how should we handle multiple triplets here? */
388
Daniel Mack23caaf12010-03-11 21:13:25 +0100389 switch (request) {
390 case UAC_GET_CUR:
391 val = buf;
392 break;
393 case UAC_GET_MIN:
394 val = buf + sizeof(__u16);
395 break;
396 case UAC_GET_MAX:
397 val = buf + sizeof(__u16) * 2;
398 break;
399 case UAC_GET_RES:
400 val = buf + sizeof(__u16) * 3;
401 break;
402 default:
403 return -EINVAL;
404 }
405
406 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16)));
407
408 return 0;
409}
410
Daniel Mack6bc170e2014-05-24 10:58:16 +0200411static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
412 int validx, int *value_ret)
Daniel Mack23caaf12010-03-11 21:13:25 +0100413{
Eldad Zack9f814102012-11-28 23:55:35 +0100414 validx += cval->idx_off;
415
Takashi Iwai3360b842014-11-18 11:47:04 +0100416 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
Daniel Mack23caaf12010-03-11 21:13:25 +0100417 get_ctl_value_v1(cval, request, validx, value_ret) :
418 get_ctl_value_v2(cval, request, validx, value_ret);
419}
420
Daniel Mack6bc170e2014-05-24 10:58:16 +0200421static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
422 int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100424 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100428static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
429 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Daniel Mack6bc170e2014-05-24 10:58:16 +0200431 return get_ctl_value(cval, UAC_GET_CUR,
432 (cval->control << 8) | channel,
433 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Chris J Argeseef90452014-11-12 12:07:01 -0600436int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
Takashi Iwai641b4872009-01-15 17:05:24 +0100437 int channel, int index, int *value)
438{
439 int err;
440
441 if (cval->cached & (1 << channel)) {
442 *value = cval->cache_val[index];
443 return 0;
444 }
445 err = get_cur_mix_raw(cval, channel, value);
446 if (err < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100447 if (!cval->head.mixer->ignore_ctl_error)
448 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100449 "cannot get current value for control %d ch %d: err = %d\n",
Daniel Mack6bc170e2014-05-24 10:58:16 +0200450 cval->control, channel, err);
Takashi Iwai641b4872009-01-15 17:05:24 +0100451 return err;
452 }
453 cval->cached |= 1 << channel;
454 cval->cache_val[index] = *value;
455 return 0;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * set a mixer value
460 */
461
Daniel Mack7b1eda22010-03-11 21:13:22 +0100462int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
463 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Takashi Iwai3360b842014-11-18 11:47:04 +0100465 struct snd_usb_audio *chip = cval->head.mixer->chip;
Julian Scheelbc18e312015-08-14 16:14:45 +0200466 unsigned char buf[4];
Takashi Iwai978520b2012-10-12 15:12:55 +0200467 int idx = 0, val_len, err, timeout = 10;
Daniel Mack23caaf12010-03-11 21:13:25 +0100468
Eldad Zack9f814102012-11-28 23:55:35 +0100469 validx += cval->idx_off;
470
Takashi Iwai3360b842014-11-18 11:47:04 +0100471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
473 } else { /* UAC_VERSION_2 */
Julian Scheelbc18e312015-08-14 16:14:45 +0200474 val_len = uac2_ctl_value_size(cval->val_type);
Daniel Mack23caaf12010-03-11 21:13:25 +0100475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
Daniel Mack23caaf12010-03-11 21:13:25 +0100479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
Julian Scheelbc18e312015-08-14 16:14:45 +0200488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200490
491 err = snd_usb_lock_shutdown(chip);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 if (err < 0)
493 return -EIO;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200494
Takashi Iwai978520b2012-10-12 15:12:55 +0200495 while (timeout-- > 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200497 if (snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Takashi Iwai978520b2012-10-12 15:12:55 +0200500 validx, idx, buf, val_len) >= 0) {
501 err = 0;
502 goto out;
Oliver Neukum88a85162011-03-11 14:51:12 +0100503 }
Takashi Iwai978520b2012-10-12 15:12:55 +0200504 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100505 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 +0200506 request, validx, idx, cval->val_type, buf[0], buf[1]);
Takashi Iwai978520b2012-10-12 15:12:55 +0200507 err = -EINVAL;
508
509 out:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200510 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Daniel Mack6bc170e2014-05-24 10:58:16 +0200514static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
515 int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100517 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Chris J Argeseef90452014-11-12 12:07:01 -0600520int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100521 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai641b4872009-01-15 17:05:24 +0100523 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200524 unsigned int read_only = (channel == 0) ?
525 cval->master_readonly :
526 cval->ch_readonly & (1 << (channel - 1));
527
528 if (read_only) {
Takashi Iwai3360b842014-11-18 11:47:04 +0100529 usb_audio_dbg(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100530 "%s(): channel %d of control %d is read_only\n",
Daniel Macka6a33252010-05-31 13:35:37 +0200531 __func__, channel, cval->control);
532 return 0;
533 }
534
Daniel Mack6bc170e2014-05-24 10:58:16 +0200535 err = snd_usb_mixer_set_ctl_value(cval,
536 UAC_SET_CUR, (cval->control << 8) | channel,
537 value);
Takashi Iwai641b4872009-01-15 17:05:24 +0100538 if (err < 0)
539 return err;
540 cval->cached |= 1 << channel;
541 cval->cache_val[index] = value;
542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200545/*
546 * TLV callback for mixer volume controls
547 */
Felix Homann285de9c2012-04-23 20:24:24 +0200548int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200549 unsigned int size, unsigned int __user *_tlv)
550{
551 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200552 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200553
554 if (size < sizeof(scale))
555 return -ENOMEM;
Takashi Iwaid3e6e592017-08-16 14:18:37 +0200556 if (cval->min_mute)
557 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100558 scale[2] = cval->dBmin;
559 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200560 if (copy_to_user(_tlv, scale, sizeof(scale)))
561 return -EFAULT;
562 return 0;
563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565/*
566 * parser routines begin here...
567 */
568
Takashi Iwai86e07d32005-11-17 15:08:02 +0100569static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571
572/*
573 * check if the input/output channel routing is enabled on the given bitmap.
574 * used for mixer unit parser
575 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200576static int check_matrix_bitmap(unsigned char *bmap,
577 int ich, int och, int num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 int idx = ich * num_outs + och;
580 return bmap[idx >> 3] & (0x80 >> (idx & 7));
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/*
584 * add an alsa control element
585 * search and increment the index until an empty slot is found.
586 *
587 * if failed, give up and free the control instance.
588 */
589
Takashi Iwai3360b842014-11-18 11:47:04 +0100590int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
Daniel Mackef9d5972011-05-25 09:09:00 +0200591 struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai3360b842014-11-18 11:47:04 +0100593 struct usb_mixer_interface *mixer = list->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200595
Daniel Mackef9d5972011-05-25 09:09:00 +0200596 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 kctl->id.index++;
Daniel Mackef9d5972011-05-25 09:09:00 +0200598 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
Daniel Mack6bc170e2014-05-24 10:58:16 +0200599 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
600 err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200601 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Takashi Iwai3360b842014-11-18 11:47:04 +0100603 list->kctl = kctl;
604 list->next_id_elem = mixer->id_elems[list->id];
605 mixer->id_elems[list->id] = list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
610 * get a terminal name string
611 */
612
613static struct iterm_name_combo {
614 int type;
615 char *name;
616} iterm_names[] = {
617 { 0x0300, "Output" },
618 { 0x0301, "Speaker" },
619 { 0x0302, "Headphone" },
620 { 0x0303, "HMD Audio" },
621 { 0x0304, "Desktop Speaker" },
622 { 0x0305, "Room Speaker" },
623 { 0x0306, "Com Speaker" },
624 { 0x0307, "LFE" },
625 { 0x0600, "External In" },
626 { 0x0601, "Analog In" },
627 { 0x0602, "Digital In" },
628 { 0x0603, "Line" },
629 { 0x0604, "Legacy In" },
630 { 0x0605, "IEC958 In" },
631 { 0x0606, "1394 DA Stream" },
632 { 0x0607, "1394 DV Stream" },
633 { 0x0700, "Embedded" },
634 { 0x0701, "Noise Source" },
635 { 0x0702, "Equalization Noise" },
636 { 0x0703, "CD" },
637 { 0x0704, "DAT" },
638 { 0x0705, "DCC" },
639 { 0x0706, "MiniDisk" },
640 { 0x0707, "Analog Tape" },
641 { 0x0708, "Phonograph" },
642 { 0x0709, "VCR Audio" },
643 { 0x070a, "Video Disk Audio" },
644 { 0x070b, "DVD Audio" },
645 { 0x070c, "TV Tuner Audio" },
646 { 0x070d, "Satellite Rec Audio" },
647 { 0x070e, "Cable Tuner Audio" },
648 { 0x070f, "DSS Audio" },
649 { 0x0710, "Radio Receiver" },
650 { 0x0711, "Radio Transmitter" },
651 { 0x0712, "Multi-Track Recorder" },
652 { 0x0713, "Synthesizer" },
653 { 0 },
654};
655
Takashi Iwai86e07d32005-11-17 15:08:02 +0100656static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned char *name, int maxlen, int term_only)
658{
659 struct iterm_name_combo *names;
660
661 if (iterm->name)
Daniel Mack6bc170e2014-05-24 10:58:16 +0200662 return snd_usb_copy_string_desc(state, iterm->name,
663 name, maxlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* virtual type - not a real terminal */
666 if (iterm->type >> 16) {
667 if (term_only)
668 return 0;
669 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100670 case UAC_SELECTOR_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200671 strcpy(name, "Selector");
672 return 8;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200673 case UAC1_PROCESSING_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200674 strcpy(name, "Process Unit");
675 return 12;
Daniel Mack69da9bc2010-06-16 17:57:28 +0200676 case UAC1_EXTENSION_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200677 strcpy(name, "Ext Unit");
678 return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100679 case UAC_MIXER_UNIT:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200680 strcpy(name, "Mixer");
681 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 default:
683 return sprintf(name, "Unit %d", iterm->id);
684 }
685 }
686
687 switch (iterm->type & 0xff00) {
688 case 0x0100:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200689 strcpy(name, "PCM");
690 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 case 0x0200:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200692 strcpy(name, "Mic");
693 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case 0x0400:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200695 strcpy(name, "Headset");
696 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 case 0x0500:
Daniel Mack6bc170e2014-05-24 10:58:16 +0200698 strcpy(name, "Phone");
699 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
Daniel Mack6bc170e2014-05-24 10:58:16 +0200702 for (names = iterm_names; names->type; names++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (names->type == iterm->type) {
704 strcpy(name, names->name);
705 return strlen(names->name);
706 }
Daniel Mack6bc170e2014-05-24 10:58:16 +0200707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/*
713 * parse the source unit recursively until it reaches to a terminal
714 * or a branched unit.
715 */
Daniel Mack6bc170e2014-05-24 10:58:16 +0200716static int check_input_term(struct mixer_build *state, int id,
717 struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Daniel Mack09414202010-05-31 13:35:44 +0200719 int err;
Daniel Mack23caaf12010-03-11 21:13:25 +0100720 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 memset(term, 0, sizeof(*term));
723 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100724 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100726 switch (hdr[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100727 case UAC_INPUT_TERMINAL:
Daniel Mack23caaf12010-03-11 21:13:25 +0100728 if (state->mixer->protocol == UAC_VERSION_1) {
729 struct uac_input_terminal_descriptor *d = p1;
730 term->type = le16_to_cpu(d->wTerminalType);
731 term->channels = d->bNrChannels;
732 term->chconfig = le16_to_cpu(d->wChannelConfig);
733 term->name = d->iTerminal;
Ajay Agarwal045e3742017-02-07 18:41:34 +0530734 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100735 struct uac2_input_terminal_descriptor *d = p1;
Julian Scheel9430e542015-08-19 09:28:09 +0200736
737 /* call recursively to verify that the
738 * referenced clock entity is valid */
739 err = check_input_term(state, d->bCSourceID, term);
740 if (err < 0)
741 return err;
742
743 /* save input term properties after recursion,
744 * to ensure they are not overriden by the
745 * recursion calls */
746 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100747 term->type = le16_to_cpu(d->wTerminalType);
748 term->channels = d->bNrChannels;
749 term->chconfig = le32_to_cpu(d->bmChannelConfig);
750 term->name = d->iTerminal;
Ajay Agarwal045e3742017-02-07 18:41:34 +0530751 } else { /* UAC_VERSION_3 */
752 struct uac3_input_terminal_descriptor *d = p1;
753
754 err = check_input_term(state,
755 d->bCSourceID, term);
756 if (err < 0)
757 return err;
758
759 term->id = id;
760 term->type = d->wTerminalType;
761 if (d->wClusterDescrID == CLUSTER_ID_MONO) {
762 term->channels = NUM_CHANNELS_MONO;
763 term->chconfig = BADD_CH_CONFIG_MONO;
764 } else {
765 term->channels = NUM_CHANNELS_STEREO;
766 term->chconfig = BADD_CH_CONFIG_STEREO;
767 }
768 term->name = d->wTerminalDescrStr;
Daniel Mack23caaf12010-03-11 21:13:25 +0100769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100771 case UAC_FEATURE_UNIT: {
772 /* the header is the same for v1 and v2 */
773 struct uac_feature_unit_descriptor *d = p1;
Daniel Mack5e688882010-05-08 11:24:56 +0200774 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break; /* continue to parse */
Daniel Mack23caaf12010-03-11 21:13:25 +0100776 }
777 case UAC_MIXER_UNIT: {
778 struct uac_mixer_unit_descriptor *d = p1;
779 term->type = d->bDescriptorSubtype << 16; /* virtual type */
780 term->channels = uac_mixer_unit_bNrChannels(d);
781 term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
782 term->name = uac_mixer_unit_iMixer(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100784 }
Daniel Mack09414202010-05-31 13:35:44 +0200785 case UAC_SELECTOR_UNIT:
Ajay Agarwal045e3742017-02-07 18:41:34 +0530786 /* UAC3_MIXER_UNIT_V3 */
787 case UAC2_CLOCK_SELECTOR:
788 /* UAC3_CLOCK_SOURCE */ {
789 if (state->mixer->protocol == UAC_VERSION_3
790 && hdr[2] == UAC3_CLOCK_SOURCE) {
791 struct uac3_clock_source_descriptor *d = p1;
792
793 term->type = d->bDescriptorSubtype << 16;
794 term->id = id;
795 term->name = d->wClockSourceStr;
796 } else if (state->mixer->protocol == UAC_VERSION_3
797 && hdr[2] == UAC3_MIXER_UNIT_V3) {
798 struct uac3_mixer_unit_descriptor *d = p1;
799
800 term->type = d->bDescriptorSubtype << 16;
801 if (d->wClusterDescrID == CLUSTER_ID_MONO) {
802 term->channels = NUM_CHANNELS_MONO;
803 term->chconfig = BADD_CH_CONFIG_MONO;
804 } else {
805 term->channels = NUM_CHANNELS_STEREO;
806 term->chconfig = BADD_CH_CONFIG_STEREO;
807 }
808 term->name = d->wMixerDescrStr;
809 } else {
810 struct uac_selector_unit_descriptor *d = p1;
811 /* call recursively to retrieve channel info */
812 err = check_input_term(state,
813 d->baSourceID[0], term);
814 if (err < 0)
815 return err;
816 /* virtual type */
817 term->type = d->bDescriptorSubtype << 16;
818 term->id = id;
819 term->name = uac_selector_unit_iSelector(d);
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100822 }
Daniel Mack69da9bc2010-06-16 17:57:28 +0200823 case UAC1_PROCESSING_UNIT:
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100824 case UAC1_EXTENSION_UNIT:
825 /* UAC2_PROCESSING_UNIT_V2 */
Torstein Hegge61ac5132013-03-19 17:12:14 +0100826 /* UAC2_EFFECT_UNIT */
Ajay Agarwal045e3742017-02-07 18:41:34 +0530827 /* UAC3_FEATURE_UNIT_V3 */
Torstein Hegge61ac5132013-03-19 17:12:14 +0100828 case UAC2_EXTENSION_UNIT_V2: {
Ajay Agarwal045e3742017-02-07 18:41:34 +0530829 if (state->mixer->protocol == UAC_VERSION_3) {
830 struct uac_feature_unit_descriptor *d = p1;
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100831
Ajay Agarwal045e3742017-02-07 18:41:34 +0530832 id = d->bSourceID;
833 } else {
834 struct uac_processing_unit_descriptor *d = p1;
835
836 if (state->mixer->protocol == UAC_VERSION_2 &&
837 hdr[2] == UAC2_EFFECT_UNIT) {
838 /* UAC2/UAC1 unit IDs overlap here in an
839 * uncompatible way. Ignore this unit
840 * for now.
841 */
842 return 0;
843 }
844
845 if (d->bNrInPins) {
846 id = d->baSourceID[0];
847 break; /* continue to parse */
848 }
849 /* virtual type */
850 term->type = d->bDescriptorSubtype << 16;
851 term->channels =
852 uac_processing_unit_bNrChannels(d);
853 term->chconfig =
854 uac_processing_unit_wChannelConfig(
855 d, state->mixer->protocol);
856 term->name = uac_processing_unit_iProcessing(
857 d, state->mixer->protocol);
Eldad Zack5dae5fd2012-11-28 23:55:36 +0100858 return 0;
859 }
Ajay Agarwal045e3742017-02-07 18:41:34 +0530860 break;
Daniel Mack23caaf12010-03-11 21:13:25 +0100861 }
Daniel Mack09414202010-05-31 13:35:44 +0200862 case UAC2_CLOCK_SOURCE: {
863 struct uac_clock_source_descriptor *d = p1;
864 term->type = d->bDescriptorSubtype << 16; /* virtual type */
865 term->id = id;
866 term->name = d->iClockSource;
867 return 0;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 default:
870 return -ENODEV;
871 }
872 }
873 return -ENODEV;
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/*
877 * Feature Unit
878 */
879
880/* feature unit control information */
881struct usb_feature_control_info {
882 const char *name;
Julian Scheelbc18e312015-08-14 16:14:45 +0200883 int type; /* data type for uac1 */
884 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885};
886
887static struct usb_feature_control_info audio_feature_info[] = {
Julian Scheelbc18e312015-08-14 16:14:45 +0200888 { "Mute", USB_MIXER_INV_BOOLEAN, -1 },
889 { "Volume", USB_MIXER_S16, -1 },
890 { "Tone Control - Bass", USB_MIXER_S8, -1 },
891 { "Tone Control - Mid", USB_MIXER_S8, -1 },
892 { "Tone Control - Treble", USB_MIXER_S8, -1 },
893 { "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */
894 { "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
895 { "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
896 { "Bass Boost", USB_MIXER_BOOLEAN, -1 },
897 { "Loudness", USB_MIXER_BOOLEAN, -1 },
Daniel Mack2e0281d2010-05-31 13:35:42 +0200898 /* UAC2 specific */
Julian Scheelbc18e312015-08-14 16:14:45 +0200899 { "Input Gain Control", USB_MIXER_S16, -1 },
900 { "Input Gain Pad Control", USB_MIXER_S16, -1 },
901 { "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902};
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/* private_free callback */
Chris J Argeseef90452014-11-12 12:07:01 -0600905void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jesper Juhl4d572772005-05-30 17:30:32 +0200907 kfree(kctl->private_data);
908 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/*
912 * interface to ALSA control for feature/mixer units
913 */
914
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100915/* volume control quirks */
916static void volume_control_quirks(struct usb_mixer_elem_info *cval,
917 struct snd_kcontrol *kctl)
918{
Takashi Iwai3360b842014-11-18 11:47:04 +0100919 struct snd_usb_audio *chip = cval->head.mixer->chip;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100920 switch (chip->usb_id) {
Eldad Zackd50ed622012-11-28 23:55:39 +0100921 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500922 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackd50ed622012-11-28 23:55:39 +0100923 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
924 cval->min = 0x0000;
925 cval->max = 0xffff;
926 cval->res = 0x00e6;
927 break;
928 }
929 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
930 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
931 cval->min = 0x00;
932 cval->max = 0xff;
933 break;
934 }
935 if (strstr(kctl->id.name, "Effect Return") != NULL) {
936 cval->min = 0xb706;
937 cval->max = 0xff7b;
938 cval->res = 0x0073;
939 break;
940 }
941 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
942 (strstr(kctl->id.name, "Effect Send") != NULL)) {
943 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
944 cval->max = 0xfcfe;
945 cval->res = 0x0073;
946 }
947 break;
948
Felix Homannd34bf142012-04-23 20:24:27 +0200949 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
950 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
951 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100952 usb_audio_info(chip,
953 "set quirk for FTU Effect Duration\n");
Felix Homannd34bf142012-04-23 20:24:27 +0200954 cval->min = 0x0000;
955 cval->max = 0x7f00;
956 cval->res = 0x0100;
957 break;
958 }
959 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
960 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100961 usb_audio_info(chip,
962 "set quirks for FTU Effect Feedback/Volume\n");
Felix Homannd34bf142012-04-23 20:24:27 +0200963 cval->min = 0x00;
964 cval->max = 0x7f;
965 break;
966 }
967 break;
968
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100969 case USB_ID(0x0471, 0x0101):
970 case USB_ID(0x0471, 0x0104):
971 case USB_ID(0x0471, 0x0105):
972 case USB_ID(0x0672, 0x1041):
973 /* quirk for UDA1321/N101.
974 * note that detection between firmware 2.1.1.7 (N101)
975 * and later 2.1.1.21 is not very clear from datasheets.
976 * I hope that the min value is -15360 for newer firmware --jk
977 */
978 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
979 cval->min == -15616) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100980 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100981 "set volume quirk for UDA1321/N101 chip\n");
982 cval->max = -256;
983 }
984 break;
985
986 case USB_ID(0x046d, 0x09a4):
987 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100988 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100989 "set volume quirk for QuickCam E3500\n");
990 cval->min = 6080;
991 cval->max = 8768;
992 cval->res = 192;
993 }
994 break;
995
Takashi Iwaie805ca82014-03-05 12:34:39 +0100996 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +0100997 case USB_ID(0x046d, 0x0808):
998 case USB_ID(0x046d, 0x0809):
Jason Lee Cragg64559312015-01-17 12:28:29 -0500999 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
Takashi Iwai36691e12013-06-17 10:25:02 +02001000 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
Alexey Fisher55c00082011-11-09 11:39:24 +01001001 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
Takashi Iwai11e70642013-06-05 08:35:26 +02001002 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
Maksim A. Boyko140d37d2013-08-10 12:20:02 +04001003 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
Wolfram Sang1ef9f052015-05-29 19:50:56 +09001004 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001005 case USB_ID(0x046d, 0x0991):
Con Kolivas205d3de2016-12-09 15:15:57 +11001006 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001007 /* Most audio usb devices lie about volume resolution.
1008 * Most Logitech webcams have res = 384.
Con Kolivas205d3de2016-12-09 15:15:57 +11001009 * Probably there is some logitech magic behind this number --fishor
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001010 */
1011 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001012 usb_audio_info(chip,
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001013 "set resolution quirk: cval->res = 384\n");
1014 cval->res = 384;
1015 }
1016 break;
Ajay Agarwal5a19df02017-06-22 11:09:33 +05301017
1018 case USB_ID(0x1130, 0x1620): /* Logitech Speakers S150 */
1019 /* This audio device has 2 channels and it explicitly requires the
1020 * host to send SET_CUR command on the volume control of both the
1021 * channels. 7936 = 0x1F00 is the default value.
1022 */
1023 if (cval->channels == 2)
1024 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1025 (cval->control << 8) | 2, 7936);
1026 break;
1027
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001028 }
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/*
1032 * retrieve the minimum and maximum values for the specified control
1033 */
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001034static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1035 int default_min, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 /* for failsafe */
1038 cval->min = default_min;
1039 cval->max = cval->min + 1;
1040 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001041 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (cval->val_type == USB_MIXER_BOOLEAN ||
1044 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1045 cval->initialized = 1;
1046 } else {
1047 int minchn = 0;
1048 if (cval->cmask) {
1049 int i;
1050 for (i = 0; i < MAX_CHANNELS; i++)
1051 if (cval->cmask & (1 << i)) {
1052 minchn = i + 1;
1053 break;
1054 }
1055 }
Daniel Mackde48c7b2010-02-22 23:49:13 +01001056 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1057 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Takashi Iwai3360b842014-11-18 11:47:04 +01001058 usb_audio_err(cval->head.mixer->chip,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001059 "%d:%d: cannot get min/max values for control %d (id %d)\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001060 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1061 cval->control, cval->head.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -EINVAL;
1063 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001064 if (get_ctl_value(cval, UAC_GET_RES,
1065 (cval->control << 8) | minchn,
1066 &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 cval->res = 1;
1068 } else {
1069 int last_valid_res = cval->res;
1070
1071 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +01001072 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001073 (cval->control << 8) | minchn,
1074 cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 cval->res /= 2;
1077 }
Daniel Mack6bc170e2014-05-24 10:58:16 +02001078 if (get_ctl_value(cval, UAC_GET_RES,
1079 (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 cval->res = last_valid_res;
1081 }
1082 if (cval->res == 0)
1083 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +02001084
1085 /* Additional checks for the proper resolution
1086 *
1087 * Some devices report smaller resolutions than actually
1088 * reacting. They don't return errors but simply clip
1089 * to the lower aligned value.
1090 */
1091 if (cval->min + cval->res < cval->max) {
1092 int last_valid_res = cval->res;
1093 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +01001094 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001095 for (;;) {
1096 test = saved;
1097 if (test < cval->max)
1098 test += cval->res;
1099 else
1100 test -= cval->res;
1101 if (test < cval->min || test > cval->max ||
Chris J Argeseef90452014-11-12 12:07:01 -06001102 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
Takashi Iwai641b4872009-01-15 17:05:24 +01001103 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +02001104 cval->res = last_valid_res;
1105 break;
1106 }
1107 if (test == check)
1108 break;
1109 cval->res *= 2;
1110 }
Chris J Argeseef90452014-11-12 12:07:01 -06001111 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +02001112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 cval->initialized = 1;
1115 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001116
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001117 if (kctl)
1118 volume_control_quirks(cval, kctl);
1119
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001120 /* USB descriptions contain the dB scale in 1/256 dB unit
1121 * while ALSA TLV contains in 1/100 dB unit
1122 */
Deepa Madiregama8b7f7fe2013-08-21 17:54:08 +05301123 cval->dBmin =
1124 (convert_signed_value(cval, cval->min) * 100) / (cval->res);
1125 cval->dBmax =
1126 (convert_signed_value(cval, cval->max) * 100) / (cval->res);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001127 if (cval->dBmin > cval->dBmax) {
1128 /* something is wrong; assume it's either from/to 0dB */
1129 if (cval->dBmin < 0)
1130 cval->dBmax = 0;
1131 else if (cval->dBmin > 0)
1132 cval->dBmin = 0;
1133 if (cval->dBmin > cval->dBmax) {
1134 /* totally crap, return an error */
1135 return -EINVAL;
1136 }
1137 }
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001142#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144/* get a feature/mixer unit info */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001145static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1146 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001148 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (cval->val_type == USB_MIXER_BOOLEAN ||
1151 cval->val_type == USB_MIXER_INV_BOOLEAN)
1152 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1153 else
1154 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1155 uinfo->count = cval->channels;
1156 if (cval->val_type == USB_MIXER_BOOLEAN ||
1157 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1158 uinfo->value.integer.min = 0;
1159 uinfo->value.integer.max = 1;
1160 } else {
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001161 if (!cval->initialized) {
Takashi Iwaidcaaf9f2011-11-08 17:50:27 +01001162 get_min_max_with_quirks(cval, 0, kcontrol);
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001163 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1164 kcontrol->vd[0].access &=
1165 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1166 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
Takashi Iwai3360b842014-11-18 11:47:04 +01001167 snd_ctl_notify(cval->head.mixer->chip->card,
Takashi Iwai9fcd0ab2011-08-19 08:30:53 +02001168 SNDRV_CTL_EVENT_MASK_INFO,
1169 &kcontrol->id);
1170 }
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +02001173 uinfo->value.integer.max =
1174 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 return 0;
1177}
1178
1179/* get the current value from feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001180static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1181 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001183 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int c, cnt, val, err;
1185
Takashi Iwai641b4872009-01-15 17:05:24 +01001186 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (cval->cmask) {
1188 cnt = 0;
1189 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001190 if (!(cval->cmask & (1 << c)))
1191 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001192 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001193 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001194 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001195 val = get_relative_value(cval, val);
1196 ucontrol->value.integer.value[cnt] = val;
1197 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 } else {
1201 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001202 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001203 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001204 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 val = get_relative_value(cval, val);
1206 ucontrol->value.integer.value[0] = val;
1207 }
1208 return 0;
1209}
1210
1211/* put the current value to feature/mixer unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001212static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1213 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001215 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int c, cnt, val, oval, err;
1217 int changed = 0;
1218
1219 if (cval->cmask) {
1220 cnt = 0;
1221 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +01001222 if (!(cval->cmask & (1 << c)))
1223 continue;
Chris J Argeseef90452014-11-12 12:07:01 -06001224 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
Takashi Iwai641b4872009-01-15 17:05:24 +01001225 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001226 return filter_error(cval, err);
Takashi Iwai641b4872009-01-15 17:05:24 +01001227 val = ucontrol->value.integer.value[cnt];
1228 val = get_abs_value(cval, val);
1229 if (oval != val) {
Chris J Argeseef90452014-11-12 12:07:01 -06001230 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
Takashi Iwai641b4872009-01-15 17:05:24 +01001231 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Takashi Iwai641b4872009-01-15 17:05:24 +01001233 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 } else {
1236 /* master channel */
Chris J Argeseef90452014-11-12 12:07:01 -06001237 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (err < 0)
Takashi Iwai5aeee342014-11-18 11:02:14 +01001239 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 val = ucontrol->value.integer.value[0];
1241 val = get_abs_value(cval, val);
1242 if (val != oval) {
Chris J Argeseef90452014-11-12 12:07:01 -06001243 snd_usb_set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 changed = 1;
1245 }
1246 }
1247 return changed;
1248}
1249
Takashi Iwai86e07d32005-11-17 15:08:02 +01001250static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1252 .name = "", /* will be filled later manually */
1253 .info = mixer_ctl_feature_info,
1254 .get = mixer_ctl_feature_get,
1255 .put = mixer_ctl_feature_put,
1256};
1257
Daniel Mack23caaf12010-03-11 21:13:25 +01001258/* the read-only variant */
1259static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1260 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1261 .name = "", /* will be filled later manually */
1262 .info = mixer_ctl_feature_info,
1263 .get = mixer_ctl_feature_get,
1264 .put = NULL,
1265};
1266
Daniel Mack6bc170e2014-05-24 10:58:16 +02001267/*
1268 * This symbol is exported in order to allow the mixer quirks to
1269 * hook up to the standard feature unit control mechanism
1270 */
Daniel Mack9e386582011-05-25 09:09:01 +02001271struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273/*
1274 * build a feature control
1275 */
Takashi Iwai08d1e632009-10-02 14:06:08 +02001276static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1277{
1278 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1279}
1280
Daniel Mack6bc170e2014-05-24 10:58:16 +02001281/*
1282 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1283 * rename it to "Headphone". We determine if something is a headphone
1284 * similar to how udev determines form factor.
1285 */
David Henningsson9b4ef972012-11-23 13:48:55 +01001286static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1287 struct snd_card *card)
1288{
1289 const char *names_to_check[] = {
1290 "Headset", "headset", "Headphone", "headphone", NULL};
1291 const char **s;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001292 bool found = false;
David Henningsson9b4ef972012-11-23 13:48:55 +01001293
1294 if (strcmp("Speaker", kctl->id.name))
1295 return;
1296
1297 for (s = names_to_check; *s; s++)
1298 if (strstr(card->shortname, *s)) {
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001299 found = true;
David Henningsson9b4ef972012-11-23 13:48:55 +01001300 break;
1301 }
1302
1303 if (!found)
1304 return;
1305
1306 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1307}
1308
Daniel Mack99fc8642010-03-11 21:13:24 +01001309static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned int ctl_mask, int control,
Daniel Mack23caaf12010-03-11 21:13:25 +01001311 struct usb_audio_term *iterm, int unitid,
Daniel Macka6a33252010-05-31 13:35:37 +02001312 int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Daniel Mack99fc8642010-03-11 21:13:24 +01001314 struct uac_feature_unit_descriptor *desc = raw_desc;
Julian Scheelbc18e312015-08-14 16:14:45 +02001315 struct usb_feature_control_info *ctl_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 unsigned int len = 0;
1317 int mapped_name = 0;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301318 int nameid;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001319 struct snd_kcontrol *kctl;
1320 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001321 const struct usbmix_name_map *map;
Alexey Fisher80aceff2011-03-10 14:53:38 +01001322 unsigned int range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Ajay Agarwal045e3742017-02-07 18:41:34 +05301324 if (state->mixer->protocol == UAC_VERSION_3)
1325 nameid = ((struct uac3_feature_unit_descriptor *)
1326 raw_desc)->wFeatureDescrStr;
1327 else
1328 nameid = uac_feature_unit_iFeature(desc);
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 control++; /* change from zero-based to 1-based value */
1331
Daniel Mack65f25da2010-05-31 13:35:41 +02001332 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* FIXME: not supported yet */
1334 return;
1335 }
1336
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001337 map = find_map(state, unitid, control);
1338 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return;
1340
Takashi Iwai561b2202005-09-09 14:22:34 +02001341 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02001342 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return;
Takashi Iwai3360b842014-11-18 11:47:04 +01001344 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 cval->control = control;
1346 cval->cmask = ctl_mask;
Julian Scheelbc18e312015-08-14 16:14:45 +02001347 ctl_info = &audio_feature_info[control-1];
1348 if (state->mixer->protocol == UAC_VERSION_1)
1349 cval->val_type = ctl_info->type;
Ajay Agarwal045e3742017-02-07 18:41:34 +05301350 else /* UAC_VERSION_2 or UAC_VERSION_3*/
Julian Scheelbc18e312015-08-14 16:14:45 +02001351 cval->val_type = ctl_info->type_uac2 >= 0 ?
1352 ctl_info->type_uac2 : ctl_info->type;
1353
Daniel Macka6a33252010-05-31 13:35:37 +02001354 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001356 cval->master_readonly = readonly_mask;
1357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int i, c = 0;
1359 for (i = 0; i < 16; i++)
1360 if (ctl_mask & (1 << i))
1361 c++;
1362 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001363 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Daniel Mack6bc170e2014-05-24 10:58:16 +02001366 /*
1367 * If all channels in the mask are marked read-only, make the control
Chris J Argeseef90452014-11-12 12:07:01 -06001368 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
Daniel Mack6bc170e2014-05-24 10:58:16 +02001369 * issue write commands to read-only channels.
1370 */
Daniel Macka6a33252010-05-31 13:35:37 +02001371 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001372 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1373 else
1374 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1375
Daniel Mack6bc170e2014-05-24 10:58:16 +02001376 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001377 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 kfree(cval);
1379 return;
1380 }
Chris J Argeseef90452014-11-12 12:07:01 -06001381 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001383 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 mapped_name = len != 0;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001385 if (!len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001386 len = snd_usb_copy_string_desc(state, nameid,
1387 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 switch (control) {
Daniel Mack65f25da2010-05-31 13:35:41 +02001390 case UAC_FU_MUTE:
1391 case UAC_FU_VOLUME:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001392 /*
1393 * determine the control name. the rule is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * - if a name id is given in descriptor, use it.
1395 * - if the connected input can be determined, then use the name
1396 * of terminal type.
1397 * - if the connected output can be determined, use it.
1398 * - otherwise, anonymous name.
1399 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001400 if (!len) {
1401 len = get_term_name(state, iterm, kctl->id.name,
1402 sizeof(kctl->id.name), 1);
1403 if (!len)
1404 len = get_term_name(state, &state->oterm,
1405 kctl->id.name,
1406 sizeof(kctl->id.name), 1);
1407 if (!len)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001408 snprintf(kctl->id.name, sizeof(kctl->id.name),
1409 "Feature %d", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
David Henningsson9b4ef972012-11-23 13:48:55 +01001411
1412 if (!mapped_name)
1413 check_no_speaker_on_headset(kctl, state->mixer->chip->card);
1414
Daniel Mack6bc170e2014-05-24 10:58:16 +02001415 /*
1416 * determine the stream direction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * if the connected output is USB stream, then it's likely a
1418 * capture stream. otherwise it should be playback (hopefully :)
1419 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001420 if (!mapped_name && !(state->oterm.type >> 16)) {
1421 if ((state->oterm.type & 0xff00) == 0x0100)
Daniel Mack49fd46d2014-10-19 09:11:25 +02001422 append_ctl_name(kctl, " Capture");
Daniel Mack6bc170e2014-05-24 10:58:16 +02001423 else
Daniel Mack49fd46d2014-10-19 09:11:25 +02001424 append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Daniel Mack65f25da2010-05-31 13:35:41 +02001426 append_ctl_name(kctl, control == UAC_FU_MUTE ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001427 " Switch" : " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 default:
Daniel Mack6bc170e2014-05-24 10:58:16 +02001430 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1432 sizeof(kctl->id.name));
1433 break;
1434 }
1435
Takashi Iwaie1825342012-05-14 17:11:06 +02001436 /* get min/max values */
1437 get_min_max_with_quirks(cval, 0, kctl);
1438
1439 if (control == UAC_FU_VOLUME) {
1440 check_mapped_dB(map, cval);
1441 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1442 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1443 kctl->vd[0].access |=
1444 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1445 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1446 }
1447 }
1448
Anssi Hannula42e31212015-12-13 20:49:58 +02001449 snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
1450
Alexey Fisher80aceff2011-03-10 14:53:38 +01001451 range = (cval->max - cval->min) / cval->res;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001452 /*
1453 * Are there devices with volume range more than 255? I use a bit more
Alexey Fisher80aceff2011-03-10 14:53:38 +01001454 * to be sure. 384 is a resolution magic number found on Logitech
1455 * devices. It will definitively catch all buggy Logitech devices.
1456 */
1457 if (range > 384) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001458 usb_audio_warn(state->chip,
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001459 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
Daniel Mack6bc170e2014-05-24 10:58:16 +02001460 range);
Michał Mirosław82c1cf02014-08-03 15:09:57 +02001461 usb_audio_warn(state->chip,
1462 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
Takashi Iwai3360b842014-11-18 11:47:04 +01001463 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001464 cval->min, cval->max, cval->res);
Alexey Fisher80aceff2011-03-10 14:53:38 +01001465 }
1466
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001467 usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001468 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02001469 cval->min, cval->max, cval->res);
Takashi Iwai3360b842014-11-18 11:47:04 +01001470 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Daniel Mackcddaafb2016-04-09 11:21:46 +02001473static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1474 void *_ftr)
1475{
1476 struct uac_clock_source_descriptor *hdr = _ftr;
1477 struct usb_mixer_elem_info *cval;
1478 struct snd_kcontrol *kctl;
1479 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1480 int ret;
1481
1482 if (state->mixer->protocol != UAC_VERSION_2)
1483 return -EINVAL;
1484
1485 if (hdr->bLength != sizeof(*hdr)) {
1486 usb_audio_dbg(state->chip,
1487 "Bogus clock source descriptor length of %d, ignoring.\n",
1488 hdr->bLength);
1489 return 0;
1490 }
1491
1492 /*
1493 * The only property of this unit we are interested in is the
1494 * clock source validity. If that isn't readable, just bail out.
1495 */
1496 if (!uac2_control_is_readable(hdr->bmControls,
1497 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1498 return 0;
1499
1500 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1501 if (!cval)
1502 return -ENOMEM;
1503
1504 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1505
1506 cval->min = 0;
1507 cval->max = 1;
1508 cval->channels = 1;
1509 cval->val_type = USB_MIXER_BOOLEAN;
1510 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1511
1512 if (uac2_control_is_writeable(hdr->bmControls,
1513 ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1514 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1515 else {
1516 cval->master_readonly = 1;
1517 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1518 }
1519
1520 if (!kctl) {
1521 kfree(cval);
1522 return -ENOMEM;
1523 }
1524
1525 kctl->private_free = snd_usb_mixer_elem_free;
1526 ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
1527 name, sizeof(name));
1528 if (ret > 0)
1529 snprintf(kctl->id.name, sizeof(kctl->id.name),
1530 "%s Validity", name);
1531 else
1532 snprintf(kctl->id.name, sizeof(kctl->id.name),
1533 "Clock Source %d Validity", hdr->bClockID);
1534
1535 return snd_usb_mixer_add_control(&cval->head, kctl);
1536}
1537
Ajay Agarwal045e3742017-02-07 18:41:34 +05301538static int find_num_channels(struct mixer_build *state, int dir)
1539{
1540 int num_ch = -EINVAL, num, i, j, wMaxPacketSize;
1541 int ctrlif = get_iface_desc(state->mixer->hostif)->bInterfaceNumber;
1542 struct usb_interface *usb_iface =
1543 usb_ifnum_to_if(state->mixer->chip->dev, ctrlif);
1544 struct usb_interface_assoc_descriptor *assoc = usb_iface->intf_assoc;
1545 struct usb_host_interface *alts;
1546
1547 for (i = 0; i < assoc->bInterfaceCount; i++) {
1548 int intf = assoc->bFirstInterface + i;
1549
1550 if (intf != ctrlif) {
1551 struct usb_interface *iface =
1552 usb_ifnum_to_if(state->mixer->chip->dev, intf);
1553
1554 alts = &iface->altsetting[1];
1555 if (dir == USB_DIR_OUT &&
1556 get_endpoint(alts, 0)->bEndpointAddress &
1557 USB_DIR_IN)
1558 continue;
1559 if (dir == USB_DIR_IN &&
1560 !(get_endpoint(alts, 0)->bEndpointAddress &
1561 USB_DIR_IN))
1562 continue;
1563 num = iface->num_altsetting;
1564 for (j = 1; j < num; j++) {
1565 num_ch = NUM_CHANNELS_MONO;
1566 alts = &iface->altsetting[j];
1567 wMaxPacketSize = le16_to_cpu(
1568 get_endpoint(alts, 0)->
1569 wMaxPacketSize);
1570 switch (wMaxPacketSize) {
1571 case BADD_MAXPSIZE_SYNC_MONO_16:
1572 case BADD_MAXPSIZE_SYNC_MONO_24:
1573 case BADD_MAXPSIZE_ASYNC_MONO_16:
1574 case BADD_MAXPSIZE_ASYNC_MONO_24:
1575 break;
1576 case BADD_MAXPSIZE_SYNC_STEREO_16:
1577 case BADD_MAXPSIZE_SYNC_STEREO_24:
1578 case BADD_MAXPSIZE_ASYNC_STEREO_16:
1579 case BADD_MAXPSIZE_ASYNC_STEREO_24:
1580 num_ch = NUM_CHANNELS_STEREO;
1581 break;
1582 }
1583 if (num_ch == NUM_CHANNELS_MONO)
1584 continue;
1585 else
1586 break;
1587 }
1588 }
1589 }
1590
1591 return num_ch;
1592}
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594/*
1595 * parse a feature unit
1596 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001597 * most of controls are defined here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001599static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1600 void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001603 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 unsigned int master_bits, first_ch_bits;
1605 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001606 struct uac_feature_unit_descriptor *hdr = _ftr;
1607 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Daniel Mack23caaf12010-03-11 21:13:25 +01001609 if (state->mixer->protocol == UAC_VERSION_1) {
1610 csize = hdr->bControlSize;
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001611 if (!csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001612 usb_audio_dbg(state->chip,
1613 "unit %u: invalid bControlSize == 0\n",
1614 unitid);
Nicolai Krakowiak60c961a92011-08-04 15:56:27 +02001615 return -EINVAL;
1616 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001617 channels = (hdr->bLength - 7) / csize - 1;
1618 bmaControls = hdr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001619 if (hdr->bLength < 7 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001620 usb_audio_err(state->chip,
1621 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1622 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001623 return -EINVAL;
1624 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301625 } else if (state->mixer->protocol == UAC_VERSION_2) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001626 struct uac2_feature_unit_descriptor *ftr = _ftr;
1627 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001628 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001629 bmaControls = ftr->bmaControls;
Clemens Ladischd56268f2012-11-29 17:04:23 +01001630 if (hdr->bLength < 6 + csize) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001631 usb_audio_err(state->chip,
1632 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1633 unitid);
Clemens Ladischd56268f2012-11-29 17:04:23 +01001634 return -EINVAL;
1635 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301636 } else {
1637 struct usb_interface *usb_iface =
1638 usb_ifnum_to_if(state->mixer->chip->dev,
1639 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1640 struct usb_interface_assoc_descriptor *assoc =
1641 usb_iface->intf_assoc;
1642
1643 csize = 4;
1644 switch (unitid) {
1645 case BADD_FU_ID_BAIOF:
1646 channels = NUM_CHANNELS_MONO;
1647 bmaControls = monoControls;
1648 badd_baif_in_term_desc.wClusterDescrID =
1649 CLUSTER_ID_MONO;
1650 break;
1651
1652 case BADD_FU_ID_BAOF:
1653 switch (assoc->bFunctionSubClass) {
1654 case PROF_HEADPHONE:
1655 case PROF_HEADSET_ADAPTER:
1656 channels = NUM_CHANNELS_STEREO;
1657 bmaControls = stereoControls;
1658 badd_baiof_mu_desc.wClusterDescrID =
1659 CLUSTER_ID_MONO;
1660 break;
1661 case PROF_SPEAKERPHONE:
1662 channels = NUM_CHANNELS_MONO;
1663 bmaControls = monoControls;
1664 badd_baof_in_term_desc.wClusterDescrID =
1665 CLUSTER_ID_MONO;
1666 break;
1667 default:
1668 channels = find_num_channels(state,
1669 USB_DIR_OUT);
1670 if (channels < 0) {
1671 usb_audio_err(state->chip,
1672 "unit %u: Cant find num of channels\n",
1673 unitid);
1674 return channels;
1675 }
1676
1677 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1678 monoControls : stereoControls;
1679 badd_baof_in_term_desc.wClusterDescrID =
1680 (channels == NUM_CHANNELS_MONO) ?
1681 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1682 break;
1683 }
1684 break;
1685
1686 case BADD_FU_ID_BAIF:
1687 switch (assoc->bFunctionSubClass) {
1688 case PROF_HEADSET:
1689 case PROF_HEADSET_ADAPTER:
1690 case PROF_SPEAKERPHONE:
1691 channels = NUM_CHANNELS_MONO;
1692 bmaControls = monoControls;
1693 badd_baif_in_term_desc.wClusterDescrID =
1694 CLUSTER_ID_MONO;
1695 break;
1696 default:
1697 channels = find_num_channels(state, USB_DIR_IN);
1698 if (channels < 0) {
1699 usb_audio_err(state->chip,
1700 "unit %u: Cant find num of channels\n",
1701 unitid);
1702 return channels;
1703 }
1704
1705 bmaControls = (channels == NUM_CHANNELS_MONO) ?
1706 monoControls : stereoControls;
1707 badd_baif_in_term_desc.wClusterDescrID =
1708 (channels == NUM_CHANNELS_MONO) ?
1709 CLUSTER_ID_MONO : CLUSTER_ID_STEREO;
1710 break;
1711 }
1712 break;
1713
1714 default:
1715 usb_audio_err(state->chip, "Invalid unit %u\n", unitid);
1716 return -EINVAL;
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 /* parse the source unit */
Ajay Agarwal045e3742017-02-07 18:41:34 +05301721 if (state->mixer->protocol != UAC_VERSION_3) {
1722 err = parse_audio_unit(state, hdr->bSourceID);
1723 if (err < 0)
1724 return err;
1725 } else {
1726 struct usb_interface *usb_iface =
1727 usb_ifnum_to_if(state->mixer->chip->dev,
1728 get_iface_desc(state->mixer->hostif)->bInterfaceNumber);
1729 struct usb_interface_assoc_descriptor *assoc =
1730 usb_iface->intf_assoc;
1731
1732 switch (unitid) {
1733 case BADD_FU_ID_BAOF:
1734 switch (assoc->bFunctionSubClass) {
1735 case PROF_HEADSET:
1736 case PROF_HEADSET_ADAPTER:
1737 hdr->bSourceID = BADD_MU_ID_BAIOF;
1738 break;
1739 default:
1740 hdr->bSourceID = BADD_IN_TERM_ID_BAOF;
1741 break;
1742 }
1743 }
1744 err = parse_audio_unit(state, hdr->bSourceID);
1745 if (err < 0)
1746 return err;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 /* determine the input source type and name */
Daniel Mack4d7b86c2013-03-19 21:09:24 +01001750 err = check_input_term(state, hdr->bSourceID, &iterm);
1751 if (err < 0)
1752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Daniel Mack23caaf12010-03-11 21:13:25 +01001754 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001755 /* master configuration quirks */
1756 switch (state->chip->usb_id) {
1757 case USB_ID(0x08bb, 0x2702):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001758 usb_audio_info(state->chip,
1759 "usbmixer: master volume quirk for PCM2702 chip\n");
Javier Kohen0c3cee52009-11-17 15:36:13 +01001760 /* disable non-functional volume control */
Daniel Mack65f25da2010-05-31 13:35:41 +02001761 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001762 break;
David Henningssonc1051432012-09-20 10:20:41 +02001763 case USB_ID(0x1130, 0xf211):
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001764 usb_audio_info(state->chip,
1765 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
David Henningssonc1051432012-09-20 10:20:41 +02001766 /* disable non-functional volume control */
1767 channels = 0;
1768 break;
1769
Javier Kohen0c3cee52009-11-17 15:36:13 +01001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001772 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 else
1774 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001775
1776 if (state->mixer->protocol == UAC_VERSION_1) {
1777 /* check all control types */
1778 for (i = 0; i < 10; i++) {
1779 unsigned int ch_bits = 0;
1780 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001781 unsigned int mask;
1782
1783 mask = snd_usb_combine_bytes(bmaControls +
1784 csize * (j+1), csize);
Daniel Mack23caaf12010-03-11 21:13:25 +01001785 if (mask & (1 << i))
1786 ch_bits |= (1 << j);
1787 }
1788 /* audio class v1 controls are never read-only */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001789
1790 /*
1791 * The first channel must be set
1792 * (for ease of programming).
1793 */
1794 if (ch_bits & 1)
1795 build_feature_ctl(state, _ftr, ch_bits, i,
1796 &iterm, unitid, 0);
Daniel Mack23caaf12010-03-11 21:13:25 +01001797 if (master_bits & (1 << i))
Daniel Mack6bc170e2014-05-24 10:58:16 +02001798 build_feature_ctl(state, _ftr, 0, i, &iterm,
1799 unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Ajay Agarwal045e3742017-02-07 18:41:34 +05301801 } else { /* UAC_VERSION_2 or UAC_VERSION_3*/
Takashi Iwaid09c06c2011-10-13 08:19:09 +02001802 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001803 unsigned int ch_bits = 0;
1804 unsigned int ch_read_only = 0;
1805
1806 for (j = 0; j < channels; j++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001807 unsigned int mask;
1808
1809 mask = snd_usb_combine_bytes(bmaControls +
1810 csize * (j+1), csize);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001811 if (uac2_control_is_readable(mask, i)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001812 ch_bits |= (1 << j);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001813 if (!uac2_control_is_writeable(mask, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001814 ch_read_only |= (1 << j);
1815 }
1816 }
1817
Daniel Mack6bc170e2014-05-24 10:58:16 +02001818 /*
1819 * NOTE: build_feature_ctl() will mark the control
1820 * read-only if all channels are marked read-only in
1821 * the descriptors. Otherwise, the control will be
1822 * reported as writeable, but the driver will not
1823 * actually issue a write command for read-only
1824 * channels.
1825 */
1826
1827 /*
1828 * The first channel must be set
1829 * (for ease of programming).
1830 */
1831 if (ch_bits & 1)
1832 build_feature_ctl(state, _ftr, ch_bits, i,
1833 &iterm, unitid, ch_read_only);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001834 if (uac2_control_is_readable(master_bits, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001835 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001836 !uac2_control_is_writeable(master_bits, i));
Daniel Mack23caaf12010-03-11 21:13:25 +01001837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
1840 return 0;
1841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843/*
1844 * Mixer Unit
1845 */
1846
1847/*
1848 * build a mixer unit control
1849 *
1850 * the callbacks are identical with feature unit.
1851 * input channel number (zero based) is given in control field instead.
1852 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001853static void build_mixer_unit_ctl(struct mixer_build *state,
1854 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001856 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001858 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001859 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001861 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001862 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001864 map = find_map(state, unitid, 0);
1865 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return;
1867
Takashi Iwai561b2202005-09-09 14:22:34 +02001868 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001869 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return;
1871
Takashi Iwai3360b842014-11-18 11:47:04 +01001872 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 cval->control = in_ch + 1; /* based on 1 */
1874 cval->val_type = USB_MIXER_S16;
1875 for (i = 0; i < num_outs; i++) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02001876 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1877
1878 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 cval->cmask |= (1 << i);
1880 cval->channels++;
1881 }
1882 }
1883
1884 /* get min/max values */
1885 get_min_max(cval, 0);
1886
1887 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001888 if (!kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001889 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 kfree(cval);
1891 return;
1892 }
Chris J Argeseef90452014-11-12 12:07:01 -06001893 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001895 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02001896 if (!len)
1897 len = get_term_name(state, iterm, kctl->id.name,
1898 sizeof(kctl->id.name), 0);
1899 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001901 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001903 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01001904 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
1905 snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/*
1909 * parse a mixer unit
1910 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001911static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
1912 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Daniel Mack99fc8642010-03-11 21:13:24 +01001914 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001915 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 int input_pins, num_ins, num_outs;
1917 int pin, ich, err;
1918
Ajay Agarwal045e3742017-02-07 18:41:34 +05301919 if (state->mixer->protocol == UAC_VERSION_3) {
1920 input_pins = badd_baiof_mu_desc.bNrInPins;
1921 num_outs =
1922 (badd_baiof_mu_desc.wClusterDescrID == CLUSTER_ID_MONO) ?
1923 NUM_CHANNELS_MONO : NUM_CHANNELS_STEREO;
1924 } else {
1925 input_pins = desc->bNrInPins;
1926 num_outs = uac_mixer_unit_bNrChannels(desc);
1927 if (desc->bLength < 11 || !input_pins || !num_outs) {
1928 usb_audio_err(state->chip,
1929 "invalid MIXER UNIT descriptor %d\n",
1930 unitid);
1931 return -EINVAL;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 num_ins = 0;
1936 ich = 0;
1937 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001938 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (err < 0)
Mark Hills284a8dd2012-04-14 17:19:23 +01001940 continue;
Clemens Ladischea114fc2015-06-03 11:36:51 +02001941 /* no bmControls field (e.g. Maya44) -> ignore */
1942 if (desc->bLength <= 10 + input_pins)
1943 continue;
Daniel Mack99fc8642010-03-11 21:13:24 +01001944 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (err < 0)
1946 return err;
1947 num_ins += iterm.channels;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001948 for (; ich < num_ins; ich++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int och, ich_has_controls = 0;
1950
Daniel Mack6bc170e2014-05-24 10:58:16 +02001951 for (och = 0; och < num_outs; och++) {
Ajay Agarwal045e3742017-02-07 18:41:34 +05301952 __u8 *c = NULL;
Daniel Mack6bc170e2014-05-24 10:58:16 +02001953
Ajay Agarwal045e3742017-02-07 18:41:34 +05301954 if (state->mixer->protocol == UAC_VERSION_3)
1955 c =
1956 &(badd_baiof_mu_desc.bmMixerControls);
1957 else
1958 c = uac_mixer_unit_bmControls(desc,
1959 state->mixer->protocol);
Daniel Mack6bc170e2014-05-24 10:58:16 +02001960 if (check_matrix_bitmap(c, ich, och, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 ich_has_controls = 1;
1962 break;
1963 }
1964 }
1965 if (ich_has_controls)
1966 build_mixer_unit_ctl(state, desc, pin, ich,
1967 unitid, &iterm);
1968 }
1969 }
1970 return 0;
1971}
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/*
1974 * Processing Unit / Extension Unit
1975 */
1976
1977/* get callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001978static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001981 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 int err, val;
1983
1984 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Takashi Iwai5aeee342014-11-18 11:02:14 +01001985 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 ucontrol->value.integer.value[0] = cval->min;
Takashi Iwai5aeee342014-11-18 11:02:14 +01001987 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 val = get_relative_value(cval, val);
1990 ucontrol->value.integer.value[0] = val;
1991 return 0;
1992}
1993
1994/* put callback for processing/extension unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02001995static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
1996 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001998 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int val, oval, err;
2000
2001 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002002 if (err < 0)
2003 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 val = ucontrol->value.integer.value[0];
2005 val = get_abs_value(cval, val);
2006 if (val != oval) {
2007 set_cur_ctl_value(cval, cval->control << 8, val);
2008 return 1;
2009 }
2010 return 0;
2011}
2012
2013/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002014static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2016 .name = "", /* will be filled later */
2017 .info = mixer_ctl_feature_info,
2018 .get = mixer_ctl_procunit_get,
2019 .put = mixer_ctl_procunit_put,
2020};
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022/*
2023 * predefined data for processing units
2024 */
2025struct procunit_value_info {
2026 int control;
2027 char *suffix;
2028 int val_type;
2029 int min_value;
2030};
2031
2032struct procunit_info {
2033 int type;
2034 char *name;
2035 struct procunit_value_info *values;
2036};
2037
2038static struct procunit_value_info updown_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002039 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2040 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 { 0 }
2042};
2043static struct procunit_value_info prologic_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002044 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2045 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 { 0 }
2047};
2048static struct procunit_value_info threed_enh_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002049 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2050 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 { 0 }
2052};
2053static struct procunit_value_info reverb_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002054 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2055 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2056 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2057 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 { 0 }
2059};
2060static struct procunit_value_info chorus_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002061 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2062 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2063 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2064 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 { 0 }
2066};
2067static struct procunit_value_info dcr_proc_info[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002068 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2069 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2070 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2071 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2072 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2073 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 { 0 }
2075};
2076
2077static struct procunit_info procunits[] = {
Daniel Mack65f25da2010-05-31 13:35:41 +02002078 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2079 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2080 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2081 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2082 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2083 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 { 0 },
2085};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002086/*
2087 * predefined data for extension units
2088 */
2089static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02002090 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2091 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002092};
2093static struct procunit_value_info clock_source_xu_info[] = {
2094 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2095 { 0 }
2096};
2097static struct procunit_value_info spdif_format_xu_info[] = {
2098 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2099 { 0 }
2100};
2101static struct procunit_value_info soft_limit_xu_info[] = {
2102 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2103 { 0 }
2104};
2105static struct procunit_info extunits[] = {
2106 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2107 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2108 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2109 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2110 { 0 }
2111};
Daniel Mack6bc170e2014-05-24 10:58:16 +02002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113/*
2114 * build a processing/extension unit
2115 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002116static int build_audio_procunit(struct mixer_build *state, int unitid,
2117 void *raw_desc, struct procunit_info *list,
2118 char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Daniel Mack99fc8642010-03-11 21:13:24 +01002120 struct uac_processing_unit_descriptor *desc = raw_desc;
2121 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002122 struct usb_mixer_elem_info *cval;
2123 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 int i, err, nameid, type, len;
2125 struct procunit_info *info;
2126 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002127 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 static struct procunit_value_info default_value_info[] = {
2129 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2130 { 0 }
2131 };
2132 static struct procunit_info default_info = {
2133 0, NULL, default_value_info
2134 };
2135
Daniel Mack23caaf12010-03-11 21:13:25 +01002136 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
2137 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002138 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return -EINVAL;
2140 }
2141
2142 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01002143 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return err;
2145 }
2146
Daniel Mack99fc8642010-03-11 21:13:24 +01002147 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 for (info = list; info && info->type; info++)
2149 if (info->type == type)
2150 break;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002151 if (!info || !info->type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 info = &default_info;
2153
2154 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002155 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01002156
Daniel Mack6bc170e2014-05-24 10:58:16 +02002157 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002159 map = find_map(state, unitid, valinfo->control);
2160 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02002162 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002163 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002165 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 cval->control = valinfo->control;
2167 cval->val_type = valinfo->val_type;
2168 cval->channels = 1;
2169
2170 /* get min/max values */
Daniel Mack65f25da2010-05-31 13:35:41 +02002171 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
Daniel Mack23caaf12010-03-11 21:13:25 +01002172 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* FIXME: hard-coded */
2174 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002175 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 cval->res = 1;
2177 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002178 } else {
2179 if (type == USB_XU_CLOCK_RATE) {
Daniel Mack6bc170e2014-05-24 10:58:16 +02002180 /*
2181 * E-Mu USB 0404/0202/TrackerPre/0204
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002182 * samplerate control quirk
2183 */
2184 cval->min = 0;
2185 cval->max = 5;
2186 cval->res = 1;
2187 cval->initialized = 1;
2188 } else
2189 get_min_max(cval, valinfo->min_value);
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002193 if (!kctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 kfree(cval);
2195 return -ENOMEM;
2196 }
Chris J Argeseef90452014-11-12 12:07:01 -06002197 kctl->private_free = snd_usb_mixer_elem_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Daniel Mack6bc170e2014-05-24 10:58:16 +02002199 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002200 /* nothing */ ;
Daniel Mack6bc170e2014-05-24 10:58:16 +02002201 } else if (info->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
Daniel Mack6bc170e2014-05-24 10:58:16 +02002203 } else {
Daniel Mack23caaf12010-03-11 21:13:25 +01002204 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 len = 0;
2206 if (nameid)
Daniel Mack6bc170e2014-05-24 10:58:16 +02002207 len = snd_usb_copy_string_desc(state, nameid,
2208 kctl->id.name,
2209 sizeof(kctl->id.name));
2210 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2212 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02002213 append_ctl_name(kctl, " ");
2214 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002216 usb_audio_dbg(state->chip,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002217 "[%d] PU [%s] ch = %d, val = %d/%d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002218 cval->head.id, kctl->id.name, cval->channels,
Daniel Mack6bc170e2014-05-24 10:58:16 +02002219 cval->min, cval->max);
2220
Takashi Iwai3360b842014-11-18 11:47:04 +01002221 err = snd_usb_mixer_add_control(&cval->head, kctl);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002222 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return err;
2224 }
2225 return 0;
2226}
2227
Daniel Mack6bc170e2014-05-24 10:58:16 +02002228static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2229 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002231 return build_audio_procunit(state, unitid, raw_desc,
2232 procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Daniel Mack6bc170e2014-05-24 10:58:16 +02002235static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2236 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Daniel Mack6bc170e2014-05-24 10:58:16 +02002238 /*
2239 * Note that we parse extension units with processing unit descriptors.
2240 * That's ok as the layout is the same.
2241 */
2242 return build_audio_procunit(state, unitid, raw_desc,
2243 extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246/*
2247 * Selector Unit
2248 */
2249
Daniel Mack6bc170e2014-05-24 10:58:16 +02002250/*
2251 * info callback for selector unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * use an enumerator type for routing
2253 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002254static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002257 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002258 const char **itemlist = (const char **)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Takashi Iwai5e246b82008-08-08 17:12:47 +02002260 if (snd_BUG_ON(!itemlist))
2261 return -EINVAL;
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01002262 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265/* get callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002266static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002269 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int val, err;
2271
Daniel Mack09414202010-05-31 13:35:44 +02002272 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (err < 0) {
Takashi Iwai5aeee342014-11-18 11:02:14 +01002274 ucontrol->value.enumerated.item[0] = 0;
2275 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277 val = get_relative_value(cval, val);
2278 ucontrol->value.enumerated.item[0] = val;
2279 return 0;
2280}
2281
2282/* put callback for selector unit */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002283static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2284 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002286 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 int val, oval, err;
2288
Daniel Mack09414202010-05-31 13:35:44 +02002289 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
Takashi Iwai5aeee342014-11-18 11:02:14 +01002290 if (err < 0)
2291 return filter_error(cval, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 val = ucontrol->value.enumerated.item[0];
2293 val = get_abs_value(cval, val);
2294 if (val != oval) {
Daniel Mack09414202010-05-31 13:35:44 +02002295 set_cur_ctl_value(cval, cval->control << 8, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return 1;
2297 }
2298 return 0;
2299}
2300
2301/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002302static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2304 .name = "", /* will be filled later */
2305 .info = mixer_ctl_selector_info,
2306 .get = mixer_ctl_selector_get,
2307 .put = mixer_ctl_selector_put,
2308};
2309
Daniel Mack6bc170e2014-05-24 10:58:16 +02002310/*
2311 * private free callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 * free both private_data and private_value
2313 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002314static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 int i, num_ins = 0;
2317
2318 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002319 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 num_ins = cval->max;
2321 kfree(cval);
2322 kctl->private_data = NULL;
2323 }
2324 if (kctl->private_value) {
2325 char **itemlist = (char **)kctl->private_value;
2326 for (i = 0; i < num_ins; i++)
2327 kfree(itemlist[i]);
2328 kfree(itemlist);
2329 kctl->private_value = 0;
2330 }
2331}
2332
2333/*
2334 * parse a selector unit
2335 */
Daniel Mack6bc170e2014-05-24 10:58:16 +02002336static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2337 void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Daniel Mack99fc8642010-03-11 21:13:24 +01002339 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 unsigned int i, nameid, len;
2341 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002342 struct usb_mixer_elem_info *cval;
2343 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002344 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 char **namelist;
2346
Daniel Mack99fc8642010-03-11 21:13:24 +01002347 if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002348 usb_audio_err(state->chip,
2349 "invalid SELECTOR UNIT descriptor %d\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return -EINVAL;
2351 }
2352
Daniel Mack99fc8642010-03-11 21:13:24 +01002353 for (i = 0; i < desc->bNrInPins; i++) {
2354 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return err;
2356 }
2357
Daniel Mack99fc8642010-03-11 21:13:24 +01002358 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return 0;
2360
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002361 map = find_map(state, unitid, 0);
2362 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return 0;
2364
Takashi Iwai561b2202005-09-09 14:22:34 +02002365 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Daniel Macka860d952014-05-24 10:58:17 +02002366 if (!cval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return -ENOMEM;
Takashi Iwai3360b842014-11-18 11:47:04 +01002368 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 cval->val_type = USB_MIXER_U8;
2370 cval->channels = 1;
2371 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01002372 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 cval->res = 1;
2374 cval->initialized = 1;
2375
Johan Rasténa358a0e2014-11-17 08:39:33 +01002376 if (state->mixer->protocol == UAC_VERSION_1)
Daniel Mack09414202010-05-31 13:35:44 +02002377 cval->control = 0;
Johan Rasténa358a0e2014-11-17 08:39:33 +01002378 else /* UAC_VERSION_2 */
2379 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2380 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
Daniel Mack09414202010-05-31 13:35:44 +02002381
Daniel Mack99fc8642010-03-11 21:13:24 +01002382 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002383 if (!namelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 kfree(cval);
2385 return -ENOMEM;
2386 }
2387#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01002388 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002389 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 len = 0;
2391 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002392 if (!namelist[i]) {
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01002393 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 kfree(namelist[i]);
2395 kfree(namelist);
2396 kfree(cval);
2397 return -ENOMEM;
2398 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002399 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2400 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01002401 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
2403 if (! len)
Masanari Iidaaf831ee2014-04-28 13:08:55 +09002404 sprintf(namelist[i], "Input %u", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406
2407 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2408 if (! kctl) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002409 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01002410 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 kfree(cval);
2412 return -ENOMEM;
2413 }
2414 kctl->private_value = (unsigned long)namelist;
2415 kctl->private_free = usb_mixer_selector_elem_free;
2416
Daniel Mack99fc8642010-03-11 21:13:24 +01002417 nameid = uac_selector_unit_iSelector(desc);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01002418 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (len)
2420 ;
2421 else if (nameid)
Daniel Mack6bc170e2014-05-24 10:58:16 +02002422 snd_usb_copy_string_desc(state, nameid, kctl->id.name,
2423 sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 else {
2425 len = get_term_name(state, &state->oterm,
2426 kctl->id.name, sizeof(kctl->id.name), 0);
Daniel Mack6bc170e2014-05-24 10:58:16 +02002427 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2429
Daniel Mack09414202010-05-31 13:35:44 +02002430 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2431 append_ctl_name(kctl, " Clock Source");
2432 else if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02002433 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02002435 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002438 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
Takashi Iwai3360b842014-11-18 11:47:04 +01002439 cval->head.id, kctl->id.name, desc->bNrInPins);
2440 return snd_usb_mixer_add_control(&cval->head, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443/*
2444 * parse an audio unit recursively
2445 */
2446
Takashi Iwai86e07d32005-11-17 15:08:02 +01002447static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 unsigned char *p1;
2450
2451 if (test_and_set_bit(unitid, state->unitbitmap))
2452 return 0; /* the unit already visited */
2453
2454 p1 = find_audio_control_unit(state, unitid);
2455 if (!p1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002456 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return -EINVAL;
2458 }
2459
2460 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01002461 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01002463 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackcddaafb2016-04-09 11:21:46 +02002465 case UAC2_CLOCK_SOURCE:
2466 return parse_clock_source_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002467 case UAC_SELECTOR_UNIT:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302468 /* UAC3_MIXER_UNIT_V3 has the same value */
Daniel Mack09414202010-05-31 13:35:44 +02002469 case UAC2_CLOCK_SELECTOR:
Ajay Agarwal045e3742017-02-07 18:41:34 +05302470 /* UAC3_CLOCK_SOURCE has the same value */
2471 if (state->mixer->protocol == UAC_VERSION_3 &&
2472 p1[2] == UAC3_CLOCK_SOURCE)
2473 return 0; /* NOP */
2474 else if (state->mixer->protocol == UAC_VERSION_3
2475 && p1[2] == UAC3_MIXER_UNIT_V3)
2476 return parse_audio_mixer_unit(state, unitid, p1);
2477 else
2478 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01002479 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002481 case UAC1_PROCESSING_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002482 /* UAC2_EFFECT_UNIT has the same value */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302483 /* UAC3_FEATURE_UNIT_V3 has the same value */
Daniel Mack23caaf12010-03-11 21:13:25 +01002484 if (state->mixer->protocol == UAC_VERSION_1)
2485 return parse_audio_processing_unit(state, unitid, p1);
Ajay Agarwal045e3742017-02-07 18:41:34 +05302486 else if (state->mixer->protocol == UAC_VERSION_2)
Daniel Mack23caaf12010-03-11 21:13:25 +01002487 return 0; /* FIXME - effect units not implemented yet */
Ajay Agarwal045e3742017-02-07 18:41:34 +05302488 else
2489 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mack69da9bc2010-06-16 17:57:28 +02002490 case UAC1_EXTENSION_UNIT:
Daniel Mack23caaf12010-03-11 21:13:25 +01002491 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2492 if (state->mixer->protocol == UAC_VERSION_1)
2493 return parse_audio_extension_unit(state, unitid, p1);
2494 else /* UAC_VERSION_2 */
2495 return parse_audio_processing_unit(state, unitid, p1);
Torstein Hegge61ac5132013-03-19 17:12:14 +01002496 case UAC2_EXTENSION_UNIT_V2:
2497 return parse_audio_extension_unit(state, unitid, p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002499 usb_audio_err(state->chip,
2500 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return -EINVAL;
2502 }
2503}
2504
Clemens Ladisch84957a82005-04-29 16:23:13 +02002505static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2506{
Takashi Iwaie0c70282017-10-10 14:10:32 +02002507 /* kill pending URBs */
2508 snd_usb_mixer_disconnect(mixer);
2509
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002510 kfree(mixer->id_elems);
2511 if (mixer->urb) {
2512 kfree(mixer->urb->transfer_buffer);
2513 usb_free_urb(mixer->urb);
2514 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002515 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02002516 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002517 kfree(mixer);
2518}
2519
Takashi Iwai86e07d32005-11-17 15:08:02 +01002520static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002521{
2522 struct usb_mixer_interface *mixer = device->device_data;
2523 snd_usb_mixer_free(mixer);
2524 return 0;
2525}
2526
Ajay Agarwal045e3742017-02-07 18:41:34 +05302527static int make_out_term(struct mixer_build state, int wTerminalType)
2528{
2529 struct uac3_output_terminal_descriptor *desc = NULL;
2530
2531 if (wTerminalType == UAC_TERMINAL_STREAMING)
2532 desc = &badd_baif_out_term_desc;
2533 else {
2534 desc = &badd_baof_out_term_desc;
2535 desc->wTerminalType = wTerminalType;
2536 }
2537 set_bit(desc->bTerminalID, state.unitbitmap);
2538 state.oterm.id = desc->bTerminalID;
2539 state.oterm.type = desc->wTerminalType;
2540 state.oterm.name = desc->wTerminalDescrStr;
2541 return parse_audio_unit(&state, desc->bSourceID);
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544/*
2545 * create mixer controls
2546 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01002547 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02002549static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002551 struct mixer_build state;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302552 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 const struct usbmix_ctl_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02002556 state.chip = mixer->chip;
2557 state.mixer = mixer;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002558 state.buffer = mixer->hostif->extra;
2559 state.buflen = mixer->hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002562 for (map = usbmix_ctl_maps; map->id; map++) {
2563 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02002565 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002566 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 break;
2568 }
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Ajay Agarwal045e3742017-02-07 18:41:34 +05302571 if (mixer->protocol == UAC_VERSION_3) {
2572 struct usb_interface *usb_iface =
2573 usb_ifnum_to_if(mixer->chip->dev,
2574 get_iface_desc(mixer->hostif)->bInterfaceNumber);
2575 struct usb_interface_assoc_descriptor *assoc =
2576 usb_iface->intf_assoc;
Daniel Mack23caaf12010-03-11 21:13:25 +01002577
Ajay Agarwal045e3742017-02-07 18:41:34 +05302578 switch (assoc->bFunctionSubClass) {
2579 case PROF_GENERIC_IO: {
2580 if (assoc->bInterfaceCount == 0x02) {
2581 if (get_endpoint(mixer->hostif,
2582 0)->bEndpointAddress | USB_DIR_IN)
2583 err = make_out_term(state,
2584 UAC_TERMINAL_STREAMING);
2585 else
2586 err = make_out_term(state,
2587 UAC_OUTPUT_TERMINAL_UNDEFINED);
2588 } else {
2589 err = make_out_term(state,
2590 UAC_OUTPUT_TERMINAL_UNDEFINED);
2591 if (err < 0 && err != -EINVAL)
2592 return err;
2593 err = make_out_term(state,
2594 UAC_TERMINAL_STREAMING);
2595 }
2596 break;
2597 }
2598
2599 case PROF_HEADPHONE:
2600 err = make_out_term(state,
2601 UAC_OUTPUT_TERMINAL_HEADPHONES);
2602 break;
2603 case PROF_SPEAKER:
2604 err = make_out_term(state, UAC_OUTPUT_TERMINAL_SPEAKER);
2605 break;
2606 case PROF_MICROPHONE:
2607 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2608 break;
2609 case PROF_HEADSET:
2610 case PROF_HEADSET_ADAPTER:
2611 err = make_out_term(state, UAC_BIDIR_TERMINAL_HEADSET);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002612 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002613 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302614 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2615 break;
2616 case PROF_SPEAKERPHONE:
2617 err = make_out_term(state,
2618 UAC_BIDIR_TERMINAL_SPEAKERPHONE);
Daniel Mack83ea5d12013-03-19 21:09:25 +01002619 if (err < 0 && err != -EINVAL)
Daniel Mack23caaf12010-03-11 21:13:25 +01002620 return err;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302621 err = make_out_term(state, UAC_TERMINAL_STREAMING);
2622 break;
2623 }
2624 if (err < 0 && err != -EINVAL)
2625 return err;
2626 } else {
2627 void *p;
Daniel Mack09414202010-05-31 13:35:44 +02002628
Ajay Agarwal045e3742017-02-07 18:41:34 +05302629 p = NULL;
2630 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2631 mixer->hostif->extralen, p,
2632 UAC_OUTPUT_TERMINAL)) != NULL) {
2633 if (mixer->protocol == UAC_VERSION_1) {
2634 struct uac1_output_terminal_descriptor *desc =
2635 p;
2636
2637 if (desc->bLength < sizeof(*desc))
2638 continue; /* invalid descriptor? */
2639 /* mark terminal ID as visited */
2640 set_bit(desc->bTerminalID, state.unitbitmap);
2641 state.oterm.id = desc->bTerminalID;
2642 state.oterm.type =
2643 le16_to_cpu(desc->wTerminalType);
2644 state.oterm.name = desc->iTerminal;
2645 err = parse_audio_unit(&state, desc->bSourceID);
2646 if (err < 0 && err != -EINVAL)
2647 return err;
2648 } else { /* UAC_VERSION_2 */
2649 struct uac2_output_terminal_descriptor *desc =
2650 p;
2651
2652 if (desc->bLength < sizeof(*desc))
2653 continue; /* invalid descriptor? */
2654 /* mark terminal ID as visited */
2655 set_bit(desc->bTerminalID, state.unitbitmap);
2656 state.oterm.id = desc->bTerminalID;
2657 state.oterm.type =
2658 le16_to_cpu(desc->wTerminalType);
2659 state.oterm.name = desc->iTerminal;
2660 err = parse_audio_unit(&state, desc->bSourceID);
2661 if (err < 0 && err != -EINVAL)
2662 return err;
2663
2664 /*
2665 * For UAC2, use the same approach to also add
2666 * the clock selectors
2667 */
2668 err = parse_audio_unit(&state,
2669 desc->bCSourceID);
2670 if (err < 0 && err != -EINVAL)
2671 return err;
2672 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
Daniel Mack23caaf12010-03-11 21:13:25 +01002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return 0;
2677}
Clemens Ladisch84957a82005-04-29 16:23:13 +02002678
Daniel Mack7b1eda22010-03-11 21:13:22 +01002679void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002680{
Takashi Iwai3360b842014-11-18 11:47:04 +01002681 struct usb_mixer_elem_list *list;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002682
Takashi Iwai3360b842014-11-18 11:47:04 +01002683 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002684 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002685 &list->kctl->id);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002686}
2687
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002688static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
Takashi Iwai3360b842014-11-18 11:47:04 +01002689 struct usb_mixer_elem_list *list)
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002690{
Takashi Iwai3360b842014-11-18 11:47:04 +01002691 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002692 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
2693 "S8", "U8", "S16", "U16"};
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002694 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
Takashi Iwai3360b842014-11-18 11:47:04 +01002695 "channels=%i, type=\"%s\"\n", cval->head.id,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002696 cval->control, cval->cmask, cval->channels,
2697 val_types[cval->val_type]);
2698 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
2699 cval->min, cval->max, cval->dBmin, cval->dBmax);
2700}
2701
2702static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
2703 struct snd_info_buffer *buffer)
2704{
2705 struct snd_usb_audio *chip = entry->private_data;
2706 struct usb_mixer_interface *mixer;
Takashi Iwai3360b842014-11-18 11:47:04 +01002707 struct usb_mixer_elem_list *list;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002708 int unitid;
2709
2710 list_for_each_entry(mixer, &chip->mixer_list, list) {
2711 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002712 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
Daniel Mack3d8d4dc2010-06-16 17:57:31 +02002713 chip->usb_id, snd_usb_ctrl_intf(chip),
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01002714 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002715 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
2716 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01002717 for (list = mixer->id_elems[unitid]; list;
2718 list = list->next_id_elem) {
2719 snd_iprintf(buffer, " Unit: %i\n", list->id);
2720 if (list->kctl)
2721 snd_iprintf(buffer,
2722 " Control: name=\"%s\", index=%i\n",
2723 list->kctl->id.name,
2724 list->kctl->id.index);
2725 if (list->dump)
2726 list->dump(buffer, list);
2727 }
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002728 }
2729 }
2730}
2731
Daniel Macke213e9c2010-05-11 18:13:50 +02002732static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
2733 int attribute, int value, int index)
2734{
Takashi Iwai3360b842014-11-18 11:47:04 +01002735 struct usb_mixer_elem_list *list;
Daniel Macke213e9c2010-05-11 18:13:50 +02002736 __u8 unitid = (index >> 8) & 0xff;
2737 __u8 control = (value >> 8) & 0xff;
2738 __u8 channel = value & 0xff;
Daniel Mack191227d2016-04-08 19:52:02 +02002739 unsigned int count = 0;
Daniel Macke213e9c2010-05-11 18:13:50 +02002740
2741 if (channel >= MAX_CHANNELS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002742 usb_audio_dbg(mixer->chip,
2743 "%s(): bogus channel number %d\n",
2744 __func__, channel);
Daniel Macke213e9c2010-05-11 18:13:50 +02002745 return;
2746 }
2747
Daniel Mack191227d2016-04-08 19:52:02 +02002748 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
2749 count++;
2750
2751 if (count == 0)
2752 return;
2753
Takashi Iwai3360b842014-11-18 11:47:04 +01002754 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
2755 struct usb_mixer_elem_info *info;
2756
2757 if (!list->kctl)
2758 continue;
2759
2760 info = (struct usb_mixer_elem_info *)list;
Daniel Mack191227d2016-04-08 19:52:02 +02002761 if (count > 1 && info->control != control)
Daniel Macke213e9c2010-05-11 18:13:50 +02002762 continue;
2763
2764 switch (attribute) {
2765 case UAC2_CS_CUR:
2766 /* invalidate cache, so the value is read from the device */
2767 if (channel)
2768 info->cached &= ~(1 << channel);
2769 else /* master channel */
2770 info->cached = 0;
2771
2772 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Takashi Iwai3360b842014-11-18 11:47:04 +01002773 &info->head.kctl->id);
Daniel Macke213e9c2010-05-11 18:13:50 +02002774 break;
2775
2776 case UAC2_CS_RANGE:
2777 /* TODO */
2778 break;
2779
2780 case UAC2_CS_MEM:
2781 /* TODO */
2782 break;
2783
2784 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002785 usb_audio_dbg(mixer->chip,
2786 "unknown attribute %d in interrupt\n",
2787 attribute);
Daniel Macke213e9c2010-05-11 18:13:50 +02002788 break;
2789 } /* switch */
2790 }
2791}
2792
2793static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002794{
2795 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02002796 int len = urb->actual_length;
Oliver Neukumedf7de32011-03-11 13:19:43 +01002797 int ustatus = urb->status;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002798
Oliver Neukumedf7de32011-03-11 13:19:43 +01002799 if (ustatus != 0)
Daniel Macke213e9c2010-05-11 18:13:50 +02002800 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002801
Daniel Macke213e9c2010-05-11 18:13:50 +02002802 if (mixer->protocol == UAC_VERSION_1) {
2803 struct uac1_status_word *status;
2804
2805 for (status = urb->transfer_buffer;
2806 len >= sizeof(*status);
2807 len -= sizeof(*status), status++) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002808 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02002809 status->bStatusType,
2810 status->bOriginator);
2811
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002812 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02002813 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2814 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002815 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02002816
2817 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
2818 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02002819 else
Daniel Macke213e9c2010-05-11 18:13:50 +02002820 snd_usb_mixer_notify_id(mixer, status->bOriginator);
2821 }
2822 } else { /* UAC_VERSION_2 */
2823 struct uac2_interrupt_data_msg *msg;
2824
2825 for (msg = urb->transfer_buffer;
2826 len >= sizeof(*msg);
2827 len -= sizeof(*msg), msg++) {
2828 /* drop vendor specific and endpoint requests */
2829 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
2830 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
2831 continue;
2832
2833 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
2834 le16_to_cpu(msg->wValue),
2835 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002836 }
2837 }
Daniel Macke213e9c2010-05-11 18:13:50 +02002838
2839requeue:
Daniel Mack6bc170e2014-05-24 10:58:16 +02002840 if (ustatus != -ENOENT &&
2841 ustatus != -ECONNRESET &&
2842 ustatus != -ESHUTDOWN) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002843 urb->dev = mixer->chip->dev;
2844 usb_submit_urb(urb, GFP_ATOMIC);
2845 }
2846}
2847
2848/* create the handler for the optional status interrupt endpoint */
2849static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2850{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002851 struct usb_endpoint_descriptor *ep;
2852 void *transfer_buffer;
2853 int buffer_length;
2854 unsigned int epnum;
2855
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002856 /* we need one interrupt input endpoint */
Daniel Mack1faa5d02011-08-04 15:56:28 +02002857 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002858 return 0;
Daniel Mack1faa5d02011-08-04 15:56:28 +02002859 ep = get_endpoint(mixer->hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002860 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002861 return 0;
2862
Julia Lawall42a6e662008-12-29 11:23:02 +01002863 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002864 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
2865 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
2866 if (!transfer_buffer)
2867 return -ENOMEM;
2868 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2869 if (!mixer->urb) {
2870 kfree(transfer_buffer);
2871 return -ENOMEM;
2872 }
2873 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
2874 usb_rcvintpipe(mixer->chip->dev, epnum),
2875 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02002876 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002877 usb_submit_urb(mixer->urb, GFP_KERNEL);
2878 return 0;
2879}
2880
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002881int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2882 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002883{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002884 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002885 .dev_free = snd_usb_mixer_dev_free
2886 };
2887 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002888 struct snd_info_entry *entry;
Daniel Mack23caaf12010-03-11 21:13:25 +01002889 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002890
2891 strcpy(chip->card->mixername, "USB Mixer");
2892
Takashi Iwai561b2202005-09-09 14:22:34 +02002893 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002894 if (!mixer)
2895 return -ENOMEM;
2896 mixer->chip = chip;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002897 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002898 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2899 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002900 if (!mixer->id_elems) {
2901 kfree(mixer);
2902 return -ENOMEM;
2903 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002904
Daniel Mack1faa5d02011-08-04 15:56:28 +02002905 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2906 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +02002907 case UAC_VERSION_1:
2908 default:
2909 mixer->protocol = UAC_VERSION_1;
2910 break;
2911 case UAC_VERSION_2:
2912 mixer->protocol = UAC_VERSION_2;
2913 break;
Ajay Agarwal045e3742017-02-07 18:41:34 +05302914 case UAC_VERSION_3:
2915 mixer->protocol = UAC_VERSION_3;
2916 break;
Clemens Ladischa2acad82010-09-03 10:53:11 +02002917 }
Daniel Mack7b8a0432010-02-22 23:49:12 +01002918
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002919 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002920 (err = snd_usb_mixer_status_create(mixer)) < 0)
2921 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002922
Daniel Mack7b1eda22010-03-11 21:13:22 +01002923 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002924
Takashi Iwai9cbb2802014-02-04 11:15:31 +01002925 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002926 if (err < 0)
2927 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002928
2929 if (list_empty(&chip->mixer_list) &&
2930 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2931 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2932
Clemens Ladisch84957a82005-04-29 16:23:13 +02002933 list_add(&mixer->list, &chip->mixer_list);
2934 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002935
2936_error:
2937 snd_usb_mixer_free(mixer);
2938 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002939}
2940
Takashi Iwaia6cece92014-10-31 11:24:32 +01002941void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002942{
Takashi Iwaie0c70282017-10-10 14:10:32 +02002943 if (mixer->disconnected)
2944 return;
2945 if (mixer->urb)
2946 usb_kill_urb(mixer->urb);
2947 if (mixer->rc_urb)
2948 usb_kill_urb(mixer->rc_urb);
2949 mixer->disconnected = true;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002950}
Takashi Iwai400362f2014-01-20 16:51:16 +01002951
2952#ifdef CONFIG_PM
2953/* stop any bus activity of a mixer */
2954static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
2955{
2956 usb_kill_urb(mixer->urb);
2957 usb_kill_urb(mixer->rc_urb);
2958}
2959
2960static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
2961{
2962 int err;
2963
2964 if (mixer->urb) {
2965 err = usb_submit_urb(mixer->urb, GFP_NOIO);
2966 if (err < 0)
2967 return err;
2968 }
2969
2970 return 0;
2971}
2972
2973int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
2974{
2975 snd_usb_mixer_inactivate(mixer);
2976 return 0;
2977}
2978
Takashi Iwai3360b842014-11-18 11:47:04 +01002979static int restore_mixer_value(struct usb_mixer_elem_list *list)
Takashi Iwai400362f2014-01-20 16:51:16 +01002980{
Takashi Iwai3360b842014-11-18 11:47:04 +01002981 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
Takashi Iwai400362f2014-01-20 16:51:16 +01002982 int c, err, idx;
2983
2984 if (cval->cmask) {
2985 idx = 0;
2986 for (c = 0; c < MAX_CHANNELS; c++) {
2987 if (!(cval->cmask & (1 << c)))
2988 continue;
Yao-Wen Mao6aa69252015-08-28 16:33:25 +08002989 if (cval->cached & (1 << (c + 1))) {
Chris J Argeseef90452014-11-12 12:07:01 -06002990 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
Takashi Iwai400362f2014-01-20 16:51:16 +01002991 cval->cache_val[idx]);
2992 if (err < 0)
2993 return err;
2994 }
2995 idx++;
2996 }
2997 } else {
2998 /* master */
2999 if (cval->cached) {
Chris J Argeseef90452014-11-12 12:07:01 -06003000 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
Takashi Iwai400362f2014-01-20 16:51:16 +01003001 if (err < 0)
3002 return err;
3003 }
3004 }
3005
3006 return 0;
3007}
3008
3009int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3010{
Takashi Iwai3360b842014-11-18 11:47:04 +01003011 struct usb_mixer_elem_list *list;
Takashi Iwai400362f2014-01-20 16:51:16 +01003012 int id, err;
3013
Takashi Iwai400362f2014-01-20 16:51:16 +01003014 if (reset_resume) {
3015 /* restore cached mixer values */
3016 for (id = 0; id < MAX_ID_ELEMS; id++) {
Takashi Iwai3360b842014-11-18 11:47:04 +01003017 for (list = mixer->id_elems[id]; list;
3018 list = list->next_id_elem) {
3019 if (list->resume) {
3020 err = list->resume(list);
3021 if (err < 0)
3022 return err;
3023 }
Takashi Iwai400362f2014-01-20 16:51:16 +01003024 }
3025 }
3026 }
3027
3028 return snd_usb_mixer_activate(mixer);
3029}
3030#endif
Takashi Iwai3360b842014-11-18 11:47:04 +01003031
3032void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3033 struct usb_mixer_interface *mixer,
3034 int unitid)
3035{
3036 list->mixer = mixer;
3037 list->id = unitid;
3038 list->dump = snd_usb_mixer_dump_cval;
3039#ifdef CONFIG_PM
3040 list->resume = restore_mixer_value;
3041#endif
3042}