blob: 9149a84c716f823ab366b25a4efdba5e3d694c63 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/bitops.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/slab.h>
33#include <linux/string.h>
34#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010035#include <linux/usb/audio.h>
Daniel Mack23caaf12010-03-11 21:13:25 +010036#include <linux/usb/audio-v2.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <sound/core.h>
39#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020040#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020041#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020042#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010045#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010046#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010047#include "mixer_quirks.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020048
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010049#define MAX_ID_ELEMS 256
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051struct usb_audio_term {
52 int id;
53 int type;
54 int channels;
55 unsigned int chconfig;
56 int name;
57};
58
59struct usbmix_name_map;
60
Takashi Iwai86e07d32005-11-17 15:08:02 +010061struct mixer_build {
62 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020063 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 unsigned char *buffer;
65 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010066 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010067 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020069 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072enum {
73 USB_MIXER_BOOLEAN,
74 USB_MIXER_INV_BOOLEAN,
75 USB_MIXER_S8,
76 USB_MIXER_U8,
77 USB_MIXER_S16,
78 USB_MIXER_U16,
79};
80
81enum {
82 USB_PROC_UPDOWN = 1,
83 USB_PROC_UPDOWN_SWITCH = 1,
84 USB_PROC_UPDOWN_MODE_SEL = 2,
85
86 USB_PROC_PROLOGIC = 2,
87 USB_PROC_PROLOGIC_SWITCH = 1,
88 USB_PROC_PROLOGIC_MODE_SEL = 2,
89
90 USB_PROC_3DENH = 3,
91 USB_PROC_3DENH_SWITCH = 1,
92 USB_PROC_3DENH_SPACE = 2,
93
94 USB_PROC_REVERB = 4,
95 USB_PROC_REVERB_SWITCH = 1,
96 USB_PROC_REVERB_LEVEL = 2,
97 USB_PROC_REVERB_TIME = 3,
98 USB_PROC_REVERB_DELAY = 4,
99
100 USB_PROC_CHORUS = 5,
101 USB_PROC_CHORUS_SWITCH = 1,
102 USB_PROC_CHORUS_LEVEL = 2,
103 USB_PROC_CHORUS_RATE = 3,
104 USB_PROC_CHORUS_DEPTH = 4,
105
106 USB_PROC_DCR = 6,
107 USB_PROC_DCR_SWITCH = 1,
108 USB_PROC_DCR_RATIO = 2,
109 USB_PROC_DCR_MAX_AMP = 3,
110 USB_PROC_DCR_THRESHOLD = 4,
111 USB_PROC_DCR_ATTACK = 5,
112 USB_PROC_DCR_RELEASE = 6,
113};
114
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -0800115/*E-mu 0202(0404) eXtension Unit(XU) control*/
116enum {
117 USB_XU_CLOCK_RATE = 0xe301,
118 USB_XU_CLOCK_SOURCE = 0xe302,
119 USB_XU_DIGITAL_IO_STATUS = 0xe303,
120 USB_XU_DEVICE_OPTIONS = 0xe304,
121 USB_XU_DIRECT_MONITORING = 0xe305,
122 USB_XU_METERING = 0xe306
123};
124enum {
125 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
126 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
127 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
128 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
132 * manual mapping of mixer names
133 * if the mixer topology is too complicated and the parsed names are
134 * ambiguous, add the entries in usbmixer_maps.c.
135 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100136#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100138static const struct usbmix_name_map *
139find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100141 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100143 if (!p)
144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100147 if (p->id == unitid &&
148 (!control || !p->control || control == p->control))
149 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100151 return NULL;
152}
153
154/* get the mapped name if the unit matches */
155static int
156check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
157{
158 if (!p || !p->name)
159 return 0;
160
161 buflen--;
162 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100166static inline int
167check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100169 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100171 return 1;
172}
173
174/* dB mapping */
175static inline void check_mapped_dB(const struct usbmix_name_map *p,
176 struct usb_mixer_elem_info *cval)
177{
178 if (p && p->dB) {
179 cval->dBmin = p->dB->min;
180 cval->dBmax = p->dB->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200184/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100185static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200186 int index, char *buf, int buflen)
187{
188 const struct usbmix_selector_map *p;
189
190 if (! state->selector_map)
191 return 0;
192 for (p = state->selector_map; p->id; p++) {
193 if (p->id == unitid && index < p->count)
194 return strlcpy(buf, p->names[index], buflen);
195 }
196 return 0;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * find an audio control unit with the given unit id
Daniel Mack23caaf12010-03-11 21:13:25 +0100201 * this doesn't return any clock related units, so they need to be handled elsewhere
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100203static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned char *p;
206
207 p = NULL;
208 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
209 USB_DT_CS_INTERFACE)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100210 if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC2_EXTENSION_UNIT_V2 && p[3] == unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return p;
212 }
213 return NULL;
214}
215
216
217/*
218 * copy a string with the given id
219 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100220static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
223 buf[len] = 0;
224 return len;
225}
226
227/*
228 * convert from the byte/word on usb descriptor to the zero-based integer
229 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100230static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 switch (cval->val_type) {
233 case USB_MIXER_BOOLEAN:
234 return !!val;
235 case USB_MIXER_INV_BOOLEAN:
236 return !val;
237 case USB_MIXER_U8:
238 val &= 0xff;
239 break;
240 case USB_MIXER_S8:
241 val &= 0xff;
242 if (val >= 0x80)
243 val -= 0x100;
244 break;
245 case USB_MIXER_U16:
246 val &= 0xffff;
247 break;
248 case USB_MIXER_S16:
249 val &= 0xffff;
250 if (val >= 0x8000)
251 val -= 0x10000;
252 break;
253 }
254 return val;
255}
256
257/*
258 * convert from the zero-based int to the byte/word for usb descriptor
259 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100260static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 switch (cval->val_type) {
263 case USB_MIXER_BOOLEAN:
264 return !!val;
265 case USB_MIXER_INV_BOOLEAN:
266 return !val;
267 case USB_MIXER_S8:
268 case USB_MIXER_U8:
269 return val & 0xff;
270 case USB_MIXER_S16:
271 case USB_MIXER_U16:
272 return val & 0xffff;
273 }
274 return 0; /* not reached */
275}
276
Takashi Iwai86e07d32005-11-17 15:08:02 +0100277static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 if (! cval->res)
280 cval->res = 1;
281 if (val < cval->min)
282 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200283 else if (val >= cval->max)
284 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 else
286 return (val - cval->min) / cval->res;
287}
288
Takashi Iwai86e07d32005-11-17 15:08:02 +0100289static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 if (val < 0)
292 return cval->min;
293 if (! cval->res)
294 cval->res = 1;
295 val *= cval->res;
296 val += cval->min;
297 if (val > cval->max)
298 return cval->max;
299 return val;
300}
301
302
303/*
304 * retrieve a mixer value
305 */
306
Daniel Mack23caaf12010-03-11 21:13:25 +0100307static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned char buf[2];
310 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
311 int timeout = 10;
312
313 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200314 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
315 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 request,
317 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200318 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200319 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
321 return 0;
322 }
323 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200324 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
325 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EINVAL;
327}
328
Daniel Mack23caaf12010-03-11 21:13:25 +0100329static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
330{
331 unsigned char buf[14]; /* enough space for one range of 4 bytes */
332 unsigned char *val;
333 int ret;
334 __u8 bRequest;
335
336 bRequest = (request == UAC_GET_CUR) ?
337 UAC2_CS_CUR : UAC2_CS_RANGE;
338
339 ret = snd_usb_ctl_msg(cval->mixer->chip->dev,
340 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
341 bRequest,
342 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
343 validx, cval->mixer->ctrlif | (cval->id << 8),
344 buf, sizeof(buf), 1000);
345
346 if (ret < 0) {
347 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
348 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
349 return ret;
350 }
351
352 switch (request) {
353 case UAC_GET_CUR:
354 val = buf;
355 break;
356 case UAC_GET_MIN:
357 val = buf + sizeof(__u16);
358 break;
359 case UAC_GET_MAX:
360 val = buf + sizeof(__u16) * 2;
361 break;
362 case UAC_GET_RES:
363 val = buf + sizeof(__u16) * 3;
364 break;
365 default:
366 return -EINVAL;
367 }
368
369 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16)));
370
371 return 0;
372}
373
374static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
375{
376 return (cval->mixer->protocol == UAC_VERSION_1) ?
377 get_ctl_value_v1(cval, request, validx, value_ret) :
378 get_ctl_value_v2(cval, request, validx, value_ret);
379}
380
Takashi Iwai86e07d32005-11-17 15:08:02 +0100381static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100383 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100387static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
388 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100390 return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Takashi Iwai641b4872009-01-15 17:05:24 +0100393static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
394 int channel, int index, int *value)
395{
396 int err;
397
398 if (cval->cached & (1 << channel)) {
399 *value = cval->cache_val[index];
400 return 0;
401 }
402 err = get_cur_mix_raw(cval, channel, value);
403 if (err < 0) {
404 if (!cval->mixer->ignore_ctl_error)
Daniel Mack23caaf12010-03-11 21:13:25 +0100405 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n",
Takashi Iwai641b4872009-01-15 17:05:24 +0100406 cval->control, channel, err);
407 return err;
408 }
409 cval->cached |= 1 << channel;
410 cval->cache_val[index] = *value;
411 return 0;
412}
413
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
416 * set a mixer value
417 */
418
Daniel Mack7b1eda22010-03-11 21:13:22 +0100419int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
420 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned char buf[2];
Daniel Mack23caaf12010-03-11 21:13:25 +0100423 int val_len, timeout = 10;
424
425 if (cval->mixer->protocol == UAC_VERSION_1) {
426 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
427 } else { /* UAC_VERSION_2 */
428 /* audio class v2 controls are always 2 bytes in size */
429 val_len = sizeof(__u16);
430
431 /* FIXME */
432 if (request != UAC_SET_CUR) {
433 snd_printdd(KERN_WARNING "RANGE setting not yet supported\n");
434 return -EINVAL;
435 }
436
437 request = UAC2_CS_CUR;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 value_set = convert_bytes_value(cval, value_set);
441 buf[0] = value_set & 0xff;
442 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530443 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200444 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
445 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 request,
447 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200448 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 buf, val_len, 100) >= 0)
450 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200451 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
452 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
454}
455
Takashi Iwai86e07d32005-11-17 15:08:02 +0100456static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100458 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Takashi Iwai641b4872009-01-15 17:05:24 +0100461static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
462 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai641b4872009-01-15 17:05:24 +0100464 int err;
Daniel Macka6a33252010-05-31 13:35:37 +0200465 unsigned int read_only = (channel == 0) ?
466 cval->master_readonly :
467 cval->ch_readonly & (1 << (channel - 1));
468
469 if (read_only) {
470 snd_printdd(KERN_INFO "%s(): channel %d of control %d is read_only\n",
471 __func__, channel, cval->control);
472 return 0;
473 }
474
Daniel Mack7b1eda22010-03-11 21:13:22 +0100475 err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100476 value);
477 if (err < 0)
478 return err;
479 cval->cached |= 1 << channel;
480 cval->cache_val[index] = value;
481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200484/*
485 * TLV callback for mixer volume controls
486 */
487static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
488 unsigned int size, unsigned int __user *_tlv)
489{
490 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200491 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200492
493 if (size < sizeof(scale))
494 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100495 scale[2] = cval->dBmin;
496 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200497 if (copy_to_user(_tlv, scale, sizeof(scale)))
498 return -EFAULT;
499 return 0;
500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502/*
503 * parser routines begin here...
504 */
505
Takashi Iwai86e07d32005-11-17 15:08:02 +0100506static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508
509/*
510 * check if the input/output channel routing is enabled on the given bitmap.
511 * used for mixer unit parser
512 */
513static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
514{
515 int idx = ich * num_outs + och;
516 return bmap[idx >> 3] & (0x80 >> (idx & 7));
517}
518
519
520/*
521 * add an alsa control element
522 * search and increment the index until an empty slot is found.
523 *
524 * if failed, give up and free the control instance.
525 */
526
Takashi Iwai86e07d32005-11-17 15:08:02 +0100527static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100529 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200531
532 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200534 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200538 cval->elem_id = &kctl->id;
539 cval->next_id_elem = state->mixer->id_elems[cval->id];
540 state->mixer->id_elems[cval->id] = cval;
541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544
545/*
546 * get a terminal name string
547 */
548
549static struct iterm_name_combo {
550 int type;
551 char *name;
552} iterm_names[] = {
553 { 0x0300, "Output" },
554 { 0x0301, "Speaker" },
555 { 0x0302, "Headphone" },
556 { 0x0303, "HMD Audio" },
557 { 0x0304, "Desktop Speaker" },
558 { 0x0305, "Room Speaker" },
559 { 0x0306, "Com Speaker" },
560 { 0x0307, "LFE" },
561 { 0x0600, "External In" },
562 { 0x0601, "Analog In" },
563 { 0x0602, "Digital In" },
564 { 0x0603, "Line" },
565 { 0x0604, "Legacy In" },
566 { 0x0605, "IEC958 In" },
567 { 0x0606, "1394 DA Stream" },
568 { 0x0607, "1394 DV Stream" },
569 { 0x0700, "Embedded" },
570 { 0x0701, "Noise Source" },
571 { 0x0702, "Equalization Noise" },
572 { 0x0703, "CD" },
573 { 0x0704, "DAT" },
574 { 0x0705, "DCC" },
575 { 0x0706, "MiniDisk" },
576 { 0x0707, "Analog Tape" },
577 { 0x0708, "Phonograph" },
578 { 0x0709, "VCR Audio" },
579 { 0x070a, "Video Disk Audio" },
580 { 0x070b, "DVD Audio" },
581 { 0x070c, "TV Tuner Audio" },
582 { 0x070d, "Satellite Rec Audio" },
583 { 0x070e, "Cable Tuner Audio" },
584 { 0x070f, "DSS Audio" },
585 { 0x0710, "Radio Receiver" },
586 { 0x0711, "Radio Transmitter" },
587 { 0x0712, "Multi-Track Recorder" },
588 { 0x0713, "Synthesizer" },
589 { 0 },
590};
591
Takashi Iwai86e07d32005-11-17 15:08:02 +0100592static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned char *name, int maxlen, int term_only)
594{
595 struct iterm_name_combo *names;
596
597 if (iterm->name)
598 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
599
600 /* virtual type - not a real terminal */
601 if (iterm->type >> 16) {
602 if (term_only)
603 return 0;
604 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100605 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 strcpy(name, "Selector"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100607 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 strcpy(name, "Process Unit"); return 12;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100609 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 strcpy(name, "Ext Unit"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100611 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 strcpy(name, "Mixer"); return 5;
613 default:
614 return sprintf(name, "Unit %d", iterm->id);
615 }
616 }
617
618 switch (iterm->type & 0xff00) {
619 case 0x0100:
620 strcpy(name, "PCM"); return 3;
621 case 0x0200:
622 strcpy(name, "Mic"); return 3;
623 case 0x0400:
624 strcpy(name, "Headset"); return 7;
625 case 0x0500:
626 strcpy(name, "Phone"); return 5;
627 }
628
629 for (names = iterm_names; names->type; names++)
630 if (names->type == iterm->type) {
631 strcpy(name, names->name);
632 return strlen(names->name);
633 }
634 return 0;
635}
636
637
638/*
639 * parse the source unit recursively until it reaches to a terminal
640 * or a branched unit.
641 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100642static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Daniel Mack23caaf12010-03-11 21:13:25 +0100644 void *p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 memset(term, 0, sizeof(*term));
647 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
Daniel Mack23caaf12010-03-11 21:13:25 +0100648 unsigned char *hdr = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100650 switch (hdr[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100651 case UAC_INPUT_TERMINAL:
Daniel Mack23caaf12010-03-11 21:13:25 +0100652 if (state->mixer->protocol == UAC_VERSION_1) {
653 struct uac_input_terminal_descriptor *d = p1;
654 term->type = le16_to_cpu(d->wTerminalType);
655 term->channels = d->bNrChannels;
656 term->chconfig = le16_to_cpu(d->wChannelConfig);
657 term->name = d->iTerminal;
658 } else { /* UAC_VERSION_2 */
659 struct uac2_input_terminal_descriptor *d = p1;
660 term->type = le16_to_cpu(d->wTerminalType);
661 term->channels = d->bNrChannels;
662 term->chconfig = le32_to_cpu(d->bmChannelConfig);
663 term->name = d->iTerminal;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100666 case UAC_FEATURE_UNIT: {
667 /* the header is the same for v1 and v2 */
668 struct uac_feature_unit_descriptor *d = p1;
Daniel Mack5e688882010-05-08 11:24:56 +0200669 id = d->bSourceID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break; /* continue to parse */
Daniel Mack23caaf12010-03-11 21:13:25 +0100671 }
672 case UAC_MIXER_UNIT: {
673 struct uac_mixer_unit_descriptor *d = p1;
674 term->type = d->bDescriptorSubtype << 16; /* virtual type */
675 term->channels = uac_mixer_unit_bNrChannels(d);
676 term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
677 term->name = uac_mixer_unit_iMixer(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100679 }
680 case UAC_SELECTOR_UNIT: {
681 struct uac_selector_unit_descriptor *d = p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* call recursively to retrieve the channel info */
Daniel Mack23caaf12010-03-11 21:13:25 +0100683 if (check_input_term(state, d->baSourceID[0], term) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -ENODEV;
Daniel Mack23caaf12010-03-11 21:13:25 +0100685 term->type = d->bDescriptorSubtype << 16; /* virtual type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 term->id = id;
Daniel Mack23caaf12010-03-11 21:13:25 +0100687 term->name = uac_selector_unit_iSelector(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100689 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100690 case UAC_PROCESSING_UNIT_V1:
Daniel Mack23caaf12010-03-11 21:13:25 +0100691 case UAC_EXTENSION_UNIT_V1: {
692 struct uac_processing_unit_descriptor *d = p1;
693 if (d->bNrInPins) {
694 id = d->baSourceID[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break; /* continue to parse */
696 }
Daniel Mack23caaf12010-03-11 21:13:25 +0100697 term->type = d->bDescriptorSubtype << 16; /* virtual type */
698 term->channels = uac_processing_unit_bNrChannels(d);
699 term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);
700 term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
Daniel Mack23caaf12010-03-11 21:13:25 +0100702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 default:
704 return -ENODEV;
705 }
706 }
707 return -ENODEV;
708}
709
710
711/*
712 * Feature Unit
713 */
714
715/* feature unit control information */
716struct usb_feature_control_info {
717 const char *name;
718 unsigned int type; /* control type (mute, volume, etc.) */
719};
720
721static struct usb_feature_control_info audio_feature_info[] = {
722 { "Mute", USB_MIXER_INV_BOOLEAN },
723 { "Volume", USB_MIXER_S16 },
724 { "Tone Control - Bass", USB_MIXER_S8 },
725 { "Tone Control - Mid", USB_MIXER_S8 },
726 { "Tone Control - Treble", USB_MIXER_S8 },
727 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
728 { "Auto Gain Control", USB_MIXER_BOOLEAN },
729 { "Delay Control", USB_MIXER_U16 },
730 { "Bass Boost", USB_MIXER_BOOLEAN },
731 { "Loudness", USB_MIXER_BOOLEAN },
732};
733
734
735/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100736static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Jesper Juhl4d572772005-05-30 17:30:32 +0200738 kfree(kctl->private_data);
739 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742
743/*
744 * interface to ALSA control for feature/mixer units
745 */
746
747/*
748 * retrieve the minimum and maximum values for the specified control
749 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100750static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 /* for failsafe */
753 cval->min = default_min;
754 cval->max = cval->min + 1;
755 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100756 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (cval->val_type == USB_MIXER_BOOLEAN ||
759 cval->val_type == USB_MIXER_INV_BOOLEAN) {
760 cval->initialized = 1;
761 } else {
762 int minchn = 0;
763 if (cval->cmask) {
764 int i;
765 for (i = 0; i < MAX_CHANNELS; i++)
766 if (cval->cmask & (1 << i)) {
767 minchn = i + 1;
768 break;
769 }
770 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100771 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
772 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200773 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
774 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EINVAL;
776 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100777 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cval->res = 1;
779 } else {
780 int last_valid_res = cval->res;
781
782 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +0100783 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
784 (cval->control << 8) | minchn, cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786 cval->res /= 2;
787 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100788 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 cval->res = last_valid_res;
790 }
791 if (cval->res == 0)
792 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200793
794 /* Additional checks for the proper resolution
795 *
796 * Some devices report smaller resolutions than actually
797 * reacting. They don't return errors but simply clip
798 * to the lower aligned value.
799 */
800 if (cval->min + cval->res < cval->max) {
801 int last_valid_res = cval->res;
802 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100803 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200804 for (;;) {
805 test = saved;
806 if (test < cval->max)
807 test += cval->res;
808 else
809 test -= cval->res;
810 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100811 set_cur_mix_value(cval, minchn, 0, test) ||
812 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200813 cval->res = last_valid_res;
814 break;
815 }
816 if (test == check)
817 break;
818 cval->res *= 2;
819 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100820 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200821 }
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 cval->initialized = 1;
824 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100825
826 /* USB descriptions contain the dB scale in 1/256 dB unit
827 * while ALSA TLV contains in 1/100 dB unit
828 */
829 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
830 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
831 if (cval->dBmin > cval->dBmax) {
832 /* something is wrong; assume it's either from/to 0dB */
833 if (cval->dBmin < 0)
834 cval->dBmax = 0;
835 else if (cval->dBmin > 0)
836 cval->dBmin = 0;
837 if (cval->dBmin > cval->dBmax) {
838 /* totally crap, return an error */
839 return -EINVAL;
840 }
841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844}
845
846
847/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100848static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100850 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (cval->val_type == USB_MIXER_BOOLEAN ||
853 cval->val_type == USB_MIXER_INV_BOOLEAN)
854 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
855 else
856 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
857 uinfo->count = cval->channels;
858 if (cval->val_type == USB_MIXER_BOOLEAN ||
859 cval->val_type == USB_MIXER_INV_BOOLEAN) {
860 uinfo->value.integer.min = 0;
861 uinfo->value.integer.max = 1;
862 } else {
863 if (! cval->initialized)
864 get_min_max(cval, 0);
865 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200866 uinfo->value.integer.max =
867 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 return 0;
870}
871
872/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100873static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100875 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int c, cnt, val, err;
877
Takashi Iwai641b4872009-01-15 17:05:24 +0100878 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (cval->cmask) {
880 cnt = 0;
881 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100882 if (!(cval->cmask & (1 << c)))
883 continue;
884 err = get_cur_mix_value(cval, c + 1, cnt, &val);
885 if (err < 0)
886 return cval->mixer->ignore_ctl_error ? 0 : err;
887 val = get_relative_value(cval, val);
888 ucontrol->value.integer.value[cnt] = val;
889 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 } else {
893 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100894 err = get_cur_mix_value(cval, 0, 0, &val);
895 if (err < 0)
896 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 val = get_relative_value(cval, val);
898 ucontrol->value.integer.value[0] = val;
899 }
900 return 0;
901}
902
903/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100904static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100906 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int c, cnt, val, oval, err;
908 int changed = 0;
909
910 if (cval->cmask) {
911 cnt = 0;
912 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100913 if (!(cval->cmask & (1 << c)))
914 continue;
915 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
916 if (err < 0)
917 return cval->mixer->ignore_ctl_error ? 0 : err;
918 val = ucontrol->value.integer.value[cnt];
919 val = get_abs_value(cval, val);
920 if (oval != val) {
921 set_cur_mix_value(cval, c + 1, cnt, val);
922 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100924 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 } else {
927 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100928 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100930 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 val = ucontrol->value.integer.value[0];
932 val = get_abs_value(cval, val);
933 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100934 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 changed = 1;
936 }
937 }
938 return changed;
939}
940
Takashi Iwai86e07d32005-11-17 15:08:02 +0100941static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
943 .name = "", /* will be filled later manually */
944 .info = mixer_ctl_feature_info,
945 .get = mixer_ctl_feature_get,
946 .put = mixer_ctl_feature_put,
947};
948
Daniel Mack23caaf12010-03-11 21:13:25 +0100949/* the read-only variant */
950static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
951 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
952 .name = "", /* will be filled later manually */
953 .info = mixer_ctl_feature_info,
954 .get = mixer_ctl_feature_get,
955 .put = NULL,
956};
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959/*
960 * build a feature control
961 */
962
Takashi Iwai08d1e632009-10-02 14:06:08 +0200963static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
964{
965 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
966}
967
Daniel Mack99fc8642010-03-11 21:13:24 +0100968static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned int ctl_mask, int control,
Daniel Mack23caaf12010-03-11 21:13:25 +0100970 struct usb_audio_term *iterm, int unitid,
Daniel Macka6a33252010-05-31 13:35:37 +0200971 int readonly_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Daniel Mack99fc8642010-03-11 21:13:24 +0100973 struct uac_feature_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned int len = 0;
975 int mapped_name = 0;
Daniel Mack99fc8642010-03-11 21:13:24 +0100976 int nameid = uac_feature_unit_iFeature(desc);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100977 struct snd_kcontrol *kctl;
978 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100979 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 control++; /* change from zero-based to 1-based value */
982
Daniel Mack45d76052010-03-11 21:13:21 +0100983 if (control == UAC_GRAPHIC_EQUALIZER_CONTROL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* FIXME: not supported yet */
985 return;
986 }
987
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100988 map = find_map(state, unitid, control);
989 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return;
991
Takashi Iwai561b2202005-09-09 14:22:34 +0200992 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (! cval) {
994 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
995 return;
996 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200997 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 cval->id = unitid;
999 cval->control = control;
1000 cval->cmask = ctl_mask;
1001 cval->val_type = audio_feature_info[control-1].type;
Daniel Macka6a33252010-05-31 13:35:37 +02001002 if (ctl_mask == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 cval->channels = 1; /* master channel */
Daniel Macka6a33252010-05-31 13:35:37 +02001004 cval->master_readonly = readonly_mask;
1005 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int i, c = 0;
1007 for (i = 0; i < 16; i++)
1008 if (ctl_mask & (1 << i))
1009 c++;
1010 cval->channels = c;
Daniel Macka6a33252010-05-31 13:35:37 +02001011 cval->ch_readonly = readonly_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
1014 /* get min/max values */
1015 get_min_max(cval, 0);
1016
Daniel Macka6a33252010-05-31 13:35:37 +02001017 /* if all channels in the mask are marked read-only, make the control
1018 * read-only. set_cur_mix_value() will check the mask again and won't
1019 * issue write commands to read-only channels. */
1020 if (cval->channels == readonly_mask)
Daniel Mack23caaf12010-03-11 21:13:25 +01001021 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1022 else
1023 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (! kctl) {
1026 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1027 kfree(cval);
1028 return;
1029 }
1030 kctl->private_free = usb_mixer_elem_free;
1031
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001032 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 mapped_name = len != 0;
1034 if (! len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001035 len = snd_usb_copy_string_desc(state, nameid,
1036 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 switch (control) {
Daniel Mack45d76052010-03-11 21:13:21 +01001039 case UAC_MUTE_CONTROL:
1040 case UAC_VOLUME_CONTROL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* determine the control name. the rule is:
1042 * - if a name id is given in descriptor, use it.
1043 * - if the connected input can be determined, then use the name
1044 * of terminal type.
1045 * - if the connected output can be determined, use it.
1046 * - otherwise, anonymous name.
1047 */
1048 if (! len) {
1049 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1050 if (! len)
1051 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1052 if (! len)
1053 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1054 "Feature %d", unitid);
1055 }
1056 /* determine the stream direction:
1057 * if the connected output is USB stream, then it's likely a
1058 * capture stream. otherwise it should be playback (hopefully :)
1059 */
1060 if (! mapped_name && ! (state->oterm.type >> 16)) {
1061 if ((state->oterm.type & 0xff00) == 0x0100) {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001062 len = append_ctl_name(kctl, " Capture");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 } else {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001064 len = append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 }
Daniel Mack45d76052010-03-11 21:13:21 +01001067 append_ctl_name(kctl, control == UAC_MUTE_CONTROL ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001068 " Switch" : " Volume");
Daniel Mack45d76052010-03-11 21:13:21 +01001069 if (control == UAC_VOLUME_CONTROL) {
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +02001070 kctl->tlv.c = mixer_vol_tlv;
1071 kctl->vd[0].access |=
1072 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1073 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001074 check_mapped_dB(map, cval);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +02001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 break;
1077
1078 default:
1079 if (! len)
1080 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1081 sizeof(kctl->id.name));
1082 break;
1083 }
1084
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001085 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001086 switch (state->chip->usb_id) {
1087 case USB_ID(0x0471, 0x0101):
1088 case USB_ID(0x0471, 0x0104):
1089 case USB_ID(0x0471, 0x0105):
1090 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001091 /* quirk for UDA1321/N101.
1092 * note that detection between firmware 2.1.1.7 (N101)
1093 * and later 2.1.1.21 is not very clear from datasheets.
1094 * I hope that the min value is -15360 for newer firmware --jk
1095 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001096 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1097 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001098 snd_printk(KERN_INFO
1099 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001100 cval->max = -256;
1101 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001102 break;
1103
1104 case USB_ID(0x046d, 0x09a4):
1105 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1106 snd_printk(KERN_INFO
1107 "set volume quirk for QuickCam E3500\n");
1108 cval->min = 6080;
1109 cval->max = 8768;
1110 cval->res = 192;
1111 }
1112 break;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115
1116 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1117 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001118 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121
1122
1123/*
1124 * parse a feature unit
1125 *
1126 * most of controlls are defined here.
1127 */
Daniel Mack28e1b772010-02-22 23:49:09 +01001128static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001131 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 unsigned int master_bits, first_ch_bits;
1133 int err, csize;
Daniel Mack23caaf12010-03-11 21:13:25 +01001134 struct uac_feature_unit_descriptor *hdr = _ftr;
1135 __u8 *bmaControls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Daniel Mack23caaf12010-03-11 21:13:25 +01001137 if (state->mixer->protocol == UAC_VERSION_1) {
1138 csize = hdr->bControlSize;
1139 channels = (hdr->bLength - 7) / csize - 1;
1140 bmaControls = hdr->bmaControls;
1141 } else {
1142 struct uac2_feature_unit_descriptor *ftr = _ftr;
1143 csize = 4;
Daniel Macke8d0fee2010-05-27 20:15:14 +02001144 channels = (hdr->bLength - 6) / 4 - 1;
Daniel Mack23caaf12010-03-11 21:13:25 +01001145 bmaControls = ftr->bmaControls;
1146 }
1147
1148 if (hdr->bLength < 7 || !csize || hdr->bLength < 7 + csize) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001149 snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EINVAL;
1151 }
1152
1153 /* parse the source unit */
Daniel Mack23caaf12010-03-11 21:13:25 +01001154 if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return err;
1156
1157 /* determine the input source type and name */
Daniel Mack23caaf12010-03-11 21:13:25 +01001158 if (check_input_term(state, hdr->bSourceID, &iterm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -EINVAL;
1160
Daniel Mack23caaf12010-03-11 21:13:25 +01001161 master_bits = snd_usb_combine_bytes(bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001162 /* master configuration quirks */
1163 switch (state->chip->usb_id) {
1164 case USB_ID(0x08bb, 0x2702):
1165 snd_printk(KERN_INFO
1166 "usbmixer: master volume quirk for PCM2702 chip\n");
1167 /* disable non-functional volume control */
Daniel Mack45d76052010-03-11 21:13:21 +01001168 master_bits &= ~UAC_FU_VOLUME;
Javier Kohen0c3cee52009-11-17 15:36:13 +01001169 break;
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (channels > 0)
Daniel Mack23caaf12010-03-11 21:13:25 +01001172 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 else
1174 first_ch_bits = 0;
Daniel Mack23caaf12010-03-11 21:13:25 +01001175
1176 if (state->mixer->protocol == UAC_VERSION_1) {
1177 /* check all control types */
1178 for (i = 0; i < 10; i++) {
1179 unsigned int ch_bits = 0;
1180 for (j = 0; j < channels; j++) {
1181 unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
1182 if (mask & (1 << i))
1183 ch_bits |= (1 << j);
1184 }
1185 /* audio class v1 controls are never read-only */
1186 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1187 build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0);
1188 if (master_bits & (1 << i))
1189 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001191 } else { /* UAC_VERSION_2 */
1192 for (i = 0; i < 30/2; i++) {
1193 /* From the USB Audio spec v2.0:
1194 bmaControls() is a (ch+1)-element array of 4-byte bitmaps,
1195 each containing a set of bit pairs. If a Control is present,
1196 it must be Host readable. If a certain Control is not
1197 present then the bit pair must be set to 0b00.
1198 If a Control is present but read-only, the bit pair must be
1199 set to 0b01. If a Control is also Host programmable, the bit
1200 pair must be set to 0b11. The value 0b10 is not allowed. */
1201 unsigned int ch_bits = 0;
1202 unsigned int ch_read_only = 0;
1203
1204 for (j = 0; j < channels; j++) {
1205 unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001206 if (uac2_control_is_readable(mask, i)) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001207 ch_bits |= (1 << j);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001208 if (!uac2_control_is_writeable(mask, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001209 ch_read_only |= (1 << j);
1210 }
1211 }
1212
Daniel Macka6a33252010-05-31 13:35:37 +02001213 /* NOTE: build_feature_ctl() will mark the control read-only if all channels
1214 * are marked read-only in the descriptors. Otherwise, the control will be
1215 * reported as writeable, but the driver will not actually issue a write
1216 * command for read-only channels */
Daniel Mack23caaf12010-03-11 21:13:25 +01001217 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
Daniel Macka6a33252010-05-31 13:35:37 +02001218 build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, ch_read_only);
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001219 if (uac2_control_is_readable(master_bits, i))
Daniel Mack23caaf12010-03-11 21:13:25 +01001220 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
Daniel Mackdcbe7bc2010-05-31 13:35:36 +02001221 !uac2_control_is_writeable(master_bits, i));
Daniel Mack23caaf12010-03-11 21:13:25 +01001222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 return 0;
1226}
1227
1228
1229/*
1230 * Mixer Unit
1231 */
1232
1233/*
1234 * build a mixer unit control
1235 *
1236 * the callbacks are identical with feature unit.
1237 * input channel number (zero based) is given in control field instead.
1238 */
1239
Daniel Mack99fc8642010-03-11 21:13:24 +01001240static void build_mixer_unit_ctl(struct mixer_build *state,
1241 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001243 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001245 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001246 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001248 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001249 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001251 map = find_map(state, unitid, 0);
1252 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return;
1254
Takashi Iwai561b2202005-09-09 14:22:34 +02001255 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (! cval)
1257 return;
1258
Clemens Ladisch84957a82005-04-29 16:23:13 +02001259 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 cval->id = unitid;
1261 cval->control = in_ch + 1; /* based on 1 */
1262 cval->val_type = USB_MIXER_S16;
1263 for (i = 0; i < num_outs; i++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001264 if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 cval->cmask |= (1 << i);
1266 cval->channels++;
1267 }
1268 }
1269
1270 /* get min/max values */
1271 get_min_max(cval, 0);
1272
1273 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1274 if (! kctl) {
1275 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1276 kfree(cval);
1277 return;
1278 }
1279 kctl->private_free = usb_mixer_elem_free;
1280
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001281 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (! len)
1283 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1284 if (! len)
1285 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001286 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1289 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001290 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293
1294/*
1295 * parse a mixer unit
1296 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001297static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Daniel Mack99fc8642010-03-11 21:13:24 +01001299 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001300 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int input_pins, num_ins, num_outs;
1302 int pin, ich, err;
1303
Daniel Mack99fc8642010-03-11 21:13:24 +01001304 if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1306 return -EINVAL;
1307 }
1308 /* no bmControls field (e.g. Maya44) -> ignore */
Daniel Mack99fc8642010-03-11 21:13:24 +01001309 if (desc->bLength <= 10 + input_pins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1311 return 0;
1312 }
1313
1314 num_ins = 0;
1315 ich = 0;
1316 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001317 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (err < 0)
1319 return err;
Daniel Mack99fc8642010-03-11 21:13:24 +01001320 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (err < 0)
1322 return err;
1323 num_ins += iterm.channels;
1324 for (; ich < num_ins; ++ich) {
1325 int och, ich_has_controls = 0;
1326
1327 for (och = 0; och < num_outs; ++och) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001328 if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ich, och, num_outs)) {
1330 ich_has_controls = 1;
1331 break;
1332 }
1333 }
1334 if (ich_has_controls)
1335 build_mixer_unit_ctl(state, desc, pin, ich,
1336 unitid, &iterm);
1337 }
1338 }
1339 return 0;
1340}
1341
1342
1343/*
1344 * Processing Unit / Extension Unit
1345 */
1346
1347/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001348static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001350 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int err, val;
1352
1353 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001354 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ucontrol->value.integer.value[0] = cval->min;
1356 return 0;
1357 }
1358 if (err < 0)
1359 return err;
1360 val = get_relative_value(cval, val);
1361 ucontrol->value.integer.value[0] = val;
1362 return 0;
1363}
1364
1365/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001366static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001368 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 int val, oval, err;
1370
1371 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1372 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001373 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375 return err;
1376 }
1377 val = ucontrol->value.integer.value[0];
1378 val = get_abs_value(cval, val);
1379 if (val != oval) {
1380 set_cur_ctl_value(cval, cval->control << 8, val);
1381 return 1;
1382 }
1383 return 0;
1384}
1385
1386/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001387static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1389 .name = "", /* will be filled later */
1390 .info = mixer_ctl_feature_info,
1391 .get = mixer_ctl_procunit_get,
1392 .put = mixer_ctl_procunit_put,
1393};
1394
1395
1396/*
1397 * predefined data for processing units
1398 */
1399struct procunit_value_info {
1400 int control;
1401 char *suffix;
1402 int val_type;
1403 int min_value;
1404};
1405
1406struct procunit_info {
1407 int type;
1408 char *name;
1409 struct procunit_value_info *values;
1410};
1411
1412static struct procunit_value_info updown_proc_info[] = {
1413 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1414 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1415 { 0 }
1416};
1417static struct procunit_value_info prologic_proc_info[] = {
1418 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1419 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1420 { 0 }
1421};
1422static struct procunit_value_info threed_enh_proc_info[] = {
1423 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1424 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1425 { 0 }
1426};
1427static struct procunit_value_info reverb_proc_info[] = {
1428 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1429 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1430 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1431 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1432 { 0 }
1433};
1434static struct procunit_value_info chorus_proc_info[] = {
1435 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1436 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1437 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1438 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1439 { 0 }
1440};
1441static struct procunit_value_info dcr_proc_info[] = {
1442 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1443 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1444 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1445 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1446 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1447 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1448 { 0 }
1449};
1450
1451static struct procunit_info procunits[] = {
1452 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1453 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1454 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1455 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1456 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1457 { USB_PROC_DCR, "DCR", dcr_proc_info },
1458 { 0 },
1459};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001460/*
1461 * predefined data for extension units
1462 */
1463static struct procunit_value_info clock_rate_xu_info[] = {
Daniel Macke213e9c2010-05-11 18:13:50 +02001464 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1465 { 0 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001466};
1467static struct procunit_value_info clock_source_xu_info[] = {
1468 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1469 { 0 }
1470};
1471static struct procunit_value_info spdif_format_xu_info[] = {
1472 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1473 { 0 }
1474};
1475static struct procunit_value_info soft_limit_xu_info[] = {
1476 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1477 { 0 }
1478};
1479static struct procunit_info extunits[] = {
1480 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1481 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1482 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1483 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1484 { 0 }
1485};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/*
1487 * build a processing/extension unit
1488 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001489static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Daniel Mack99fc8642010-03-11 21:13:24 +01001491 struct uac_processing_unit_descriptor *desc = raw_desc;
1492 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001493 struct usb_mixer_elem_info *cval;
1494 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int i, err, nameid, type, len;
1496 struct procunit_info *info;
1497 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001498 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 static struct procunit_value_info default_value_info[] = {
1500 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1501 { 0 }
1502 };
1503 static struct procunit_info default_info = {
1504 0, NULL, default_value_info
1505 };
1506
Daniel Mack23caaf12010-03-11 21:13:25 +01001507 if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
1508 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1510 return -EINVAL;
1511 }
1512
1513 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001514 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return err;
1516 }
1517
Daniel Mack99fc8642010-03-11 21:13:24 +01001518 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 for (info = list; info && info->type; info++)
1520 if (info->type == type)
1521 break;
1522 if (! info || ! info->type)
1523 info = &default_info;
1524
1525 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001526 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
Daniel Mack99fc8642010-03-11 21:13:24 +01001527
1528 if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001530 map = find_map(state, unitid, valinfo->control);
1531 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001533 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (! cval) {
1535 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1536 return -ENOMEM;
1537 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001538 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 cval->id = unitid;
1540 cval->control = valinfo->control;
1541 cval->val_type = valinfo->val_type;
1542 cval->channels = 1;
1543
1544 /* get min/max values */
1545 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
Daniel Mack23caaf12010-03-11 21:13:25 +01001546 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* FIXME: hard-coded */
1548 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01001549 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 cval->res = 1;
1551 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001552 } else {
1553 if (type == USB_XU_CLOCK_RATE) {
1554 /* E-Mu USB 0404/0202/TrackerPre
1555 * samplerate control quirk
1556 */
1557 cval->min = 0;
1558 cval->max = 5;
1559 cval->res = 1;
1560 cval->initialized = 1;
1561 } else
1562 get_min_max(cval, valinfo->min_value);
1563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1566 if (! kctl) {
1567 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1568 kfree(cval);
1569 return -ENOMEM;
1570 }
1571 kctl->private_free = usb_mixer_elem_free;
1572
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001573 if (check_mapped_name(map, kctl->id.name,
1574 sizeof(kctl->id.name)))
1575 /* nothing */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 else if (info->name)
1577 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1578 else {
Daniel Mack23caaf12010-03-11 21:13:25 +01001579 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 len = 0;
1581 if (nameid)
1582 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1583 if (! len)
1584 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1585 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001586 append_ctl_name(kctl, " ");
1587 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1590 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001591 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return err;
1593 }
1594 return 0;
1595}
1596
1597
Daniel Mack99fc8642010-03-11 21:13:24 +01001598static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Daniel Mack99fc8642010-03-11 21:13:24 +01001600 return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
Daniel Mack99fc8642010-03-11 21:13:24 +01001603static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Daniel Mack99fc8642010-03-11 21:13:24 +01001605 /* Note that we parse extension units with processing unit descriptors.
1606 * That's ok as the layout is the same */
1607 return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610
1611/*
1612 * Selector Unit
1613 */
1614
1615/* info callback for selector unit
1616 * use an enumerator type for routing
1617 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001618static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001620 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 char **itemlist = (char **)kcontrol->private_value;
1622
Takashi Iwai5e246b82008-08-08 17:12:47 +02001623 if (snd_BUG_ON(!itemlist))
1624 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1626 uinfo->count = 1;
1627 uinfo->value.enumerated.items = cval->max;
1628 if ((int)uinfo->value.enumerated.item >= cval->max)
1629 uinfo->value.enumerated.item = cval->max - 1;
1630 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1631 return 0;
1632}
1633
1634/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001635static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001637 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int val, err;
1639
1640 err = get_cur_ctl_value(cval, 0, &val);
1641 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001642 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 ucontrol->value.enumerated.item[0] = 0;
1644 return 0;
1645 }
1646 return err;
1647 }
1648 val = get_relative_value(cval, val);
1649 ucontrol->value.enumerated.item[0] = val;
1650 return 0;
1651}
1652
1653/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001654static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001656 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int val, oval, err;
1658
1659 err = get_cur_ctl_value(cval, 0, &oval);
1660 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001661 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 0;
1663 return err;
1664 }
1665 val = ucontrol->value.enumerated.item[0];
1666 val = get_abs_value(cval, val);
1667 if (val != oval) {
1668 set_cur_ctl_value(cval, 0, val);
1669 return 1;
1670 }
1671 return 0;
1672}
1673
1674/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001675static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1677 .name = "", /* will be filled later */
1678 .info = mixer_ctl_selector_info,
1679 .get = mixer_ctl_selector_get,
1680 .put = mixer_ctl_selector_put,
1681};
1682
1683
1684/* private free callback.
1685 * free both private_data and private_value
1686 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001687static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 int i, num_ins = 0;
1690
1691 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001692 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 num_ins = cval->max;
1694 kfree(cval);
1695 kctl->private_data = NULL;
1696 }
1697 if (kctl->private_value) {
1698 char **itemlist = (char **)kctl->private_value;
1699 for (i = 0; i < num_ins; i++)
1700 kfree(itemlist[i]);
1701 kfree(itemlist);
1702 kctl->private_value = 0;
1703 }
1704}
1705
1706/*
1707 * parse a selector unit
1708 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001709static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Daniel Mack99fc8642010-03-11 21:13:24 +01001711 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 unsigned int i, nameid, len;
1713 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001714 struct usb_mixer_elem_info *cval;
1715 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001716 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 char **namelist;
1718
Daniel Mack99fc8642010-03-11 21:13:24 +01001719 if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1721 return -EINVAL;
1722 }
1723
Daniel Mack99fc8642010-03-11 21:13:24 +01001724 for (i = 0; i < desc->bNrInPins; i++) {
1725 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return err;
1727 }
1728
Daniel Mack99fc8642010-03-11 21:13:24 +01001729 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
1731
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001732 map = find_map(state, unitid, 0);
1733 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return 0;
1735
Takashi Iwai561b2202005-09-09 14:22:34 +02001736 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (! cval) {
1738 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1739 return -ENOMEM;
1740 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001741 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 cval->id = unitid;
1743 cval->val_type = USB_MIXER_U8;
1744 cval->channels = 1;
1745 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01001746 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 cval->res = 1;
1748 cval->initialized = 1;
1749
Daniel Mack99fc8642010-03-11 21:13:24 +01001750 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (! namelist) {
1752 snd_printk(KERN_ERR "cannot malloc\n");
1753 kfree(cval);
1754 return -ENOMEM;
1755 }
1756#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01001757 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001758 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 len = 0;
1760 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1761 if (! namelist[i]) {
1762 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001763 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 kfree(namelist[i]);
1765 kfree(namelist);
1766 kfree(cval);
1767 return -ENOMEM;
1768 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001769 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1770 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01001771 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1773 if (! len)
1774 sprintf(namelist[i], "Input %d", i);
1775 }
1776
1777 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1778 if (! kctl) {
1779 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001780 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 kfree(cval);
1782 return -ENOMEM;
1783 }
1784 kctl->private_value = (unsigned long)namelist;
1785 kctl->private_free = usb_mixer_selector_elem_free;
1786
Daniel Mack99fc8642010-03-11 21:13:24 +01001787 nameid = uac_selector_unit_iSelector(desc);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001788 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (len)
1790 ;
1791 else if (nameid)
1792 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1793 else {
1794 len = get_term_name(state, &state->oterm,
1795 kctl->id.name, sizeof(kctl->id.name), 0);
1796 if (! len)
1797 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1798
1799 if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02001800 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02001802 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
1805 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
Daniel Mack99fc8642010-03-11 21:13:24 +01001806 cval->id, kctl->id.name, desc->bNrInPins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001807 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809
1810 return 0;
1811}
1812
1813
1814/*
1815 * parse an audio unit recursively
1816 */
1817
Takashi Iwai86e07d32005-11-17 15:08:02 +01001818static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 unsigned char *p1;
1821
1822 if (test_and_set_bit(unitid, state->unitbitmap))
1823 return 0; /* the unit already visited */
1824
1825 p1 = find_audio_control_unit(state, unitid);
1826 if (!p1) {
1827 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1828 return -EINVAL;
1829 }
1830
1831 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001832 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01001834 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001836 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001838 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001840 case UAC_PROCESSING_UNIT_V1:
Daniel Mack23caaf12010-03-11 21:13:25 +01001841 /* UAC2_EFFECT_UNIT has the same value */
1842 if (state->mixer->protocol == UAC_VERSION_1)
1843 return parse_audio_processing_unit(state, unitid, p1);
1844 else
1845 return 0; /* FIXME - effect units not implemented yet */
Daniel Mackde48c7b2010-02-22 23:49:13 +01001846 case UAC_EXTENSION_UNIT_V1:
Daniel Mack23caaf12010-03-11 21:13:25 +01001847 /* UAC2_PROCESSING_UNIT_V2 has the same value */
1848 if (state->mixer->protocol == UAC_VERSION_1)
1849 return parse_audio_extension_unit(state, unitid, p1);
1850 else /* UAC_VERSION_2 */
1851 return parse_audio_processing_unit(state, unitid, p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 default:
1853 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1854 return -EINVAL;
1855 }
1856}
1857
Clemens Ladisch84957a82005-04-29 16:23:13 +02001858static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1859{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001860 kfree(mixer->id_elems);
1861 if (mixer->urb) {
1862 kfree(mixer->urb->transfer_buffer);
1863 usb_free_urb(mixer->urb);
1864 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001865 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001866 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001867 kfree(mixer);
1868}
1869
Takashi Iwai86e07d32005-11-17 15:08:02 +01001870static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001871{
1872 struct usb_mixer_interface *mixer = device->device_data;
1873 snd_usb_mixer_free(mixer);
1874 return 0;
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877/*
1878 * create mixer controls
1879 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01001880 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001882static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001884 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 int err;
1886 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001887 struct usb_host_interface *hostif;
Daniel Mack23caaf12010-03-11 21:13:25 +01001888 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Clemens Ladisch84957a82005-04-29 16:23:13 +02001890 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001892 state.chip = mixer->chip;
1893 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 state.buffer = hostif->extra;
1895 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001898 for (map = usbmix_ctl_maps; map->id; map++) {
1899 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001901 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001902 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 }
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Daniel Mack23caaf12010-03-11 21:13:25 +01001907 p = NULL;
1908 while ((p = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, p, UAC_OUTPUT_TERMINAL)) != NULL) {
1909 if (mixer->protocol == UAC_VERSION_1) {
1910 struct uac_output_terminal_descriptor_v1 *desc = p;
1911
1912 if (desc->bLength < sizeof(*desc))
1913 continue; /* invalid descriptor? */
1914 set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
1915 state.oterm.id = desc->bTerminalID;
1916 state.oterm.type = le16_to_cpu(desc->wTerminalType);
1917 state.oterm.name = desc->iTerminal;
1918 err = parse_audio_unit(&state, desc->bSourceID);
1919 if (err < 0)
1920 return err;
1921 } else { /* UAC_VERSION_2 */
1922 struct uac2_output_terminal_descriptor *desc = p;
1923
1924 if (desc->bLength < sizeof(*desc))
1925 continue; /* invalid descriptor? */
1926 set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
1927 state.oterm.id = desc->bTerminalID;
1928 state.oterm.type = le16_to_cpu(desc->wTerminalType);
1929 state.oterm.name = desc->iTerminal;
1930 err = parse_audio_unit(&state, desc->bSourceID);
1931 if (err < 0)
1932 return err;
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Daniel Mack23caaf12010-03-11 21:13:25 +01001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001938
Daniel Mack7b1eda22010-03-11 21:13:22 +01001939void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001940{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001941 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001942
1943 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1944 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1945 info->elem_id);
1946}
1947
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001948static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1949 int unitid,
1950 struct usb_mixer_elem_info *cval)
1951{
1952 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1953 "S8", "U8", "S16", "U16"};
1954 snd_iprintf(buffer, " Unit: %i\n", unitid);
1955 if (cval->elem_id)
1956 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1957 cval->elem_id->name, cval->elem_id->index);
1958 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1959 "channels=%i, type=\"%s\"\n", cval->id,
1960 cval->control, cval->cmask, cval->channels,
1961 val_types[cval->val_type]);
1962 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1963 cval->min, cval->max, cval->dBmin, cval->dBmax);
1964}
1965
1966static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1967 struct snd_info_buffer *buffer)
1968{
1969 struct snd_usb_audio *chip = entry->private_data;
1970 struct usb_mixer_interface *mixer;
1971 struct usb_mixer_elem_info *cval;
1972 int unitid;
1973
1974 list_for_each_entry(mixer, &chip->mixer_list, list) {
1975 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01001976 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1977 chip->usb_id, mixer->ctrlif,
1978 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001979 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1980 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1981 for (cval = mixer->id_elems[unitid]; cval;
1982 cval = cval->next_id_elem)
1983 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1984 }
1985 }
1986}
1987
Daniel Macke213e9c2010-05-11 18:13:50 +02001988static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
1989 int attribute, int value, int index)
1990{
1991 struct usb_mixer_elem_info *info;
1992 __u8 unitid = (index >> 8) & 0xff;
1993 __u8 control = (value >> 8) & 0xff;
1994 __u8 channel = value & 0xff;
1995
1996 if (channel >= MAX_CHANNELS) {
1997 snd_printk(KERN_DEBUG "%s(): bogus channel number %d\n",
1998 __func__, channel);
1999 return;
2000 }
2001
2002 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) {
2003 if (info->control != control)
2004 continue;
2005
2006 switch (attribute) {
2007 case UAC2_CS_CUR:
2008 /* invalidate cache, so the value is read from the device */
2009 if (channel)
2010 info->cached &= ~(1 << channel);
2011 else /* master channel */
2012 info->cached = 0;
2013
2014 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2015 info->elem_id);
2016 break;
2017
2018 case UAC2_CS_RANGE:
2019 /* TODO */
2020 break;
2021
2022 case UAC2_CS_MEM:
2023 /* TODO */
2024 break;
2025
2026 default:
2027 snd_printk(KERN_DEBUG "unknown attribute %d in interrupt\n",
2028 attribute);
2029 break;
2030 } /* switch */
2031 }
2032}
2033
2034static void snd_usb_mixer_interrupt(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002035{
2036 struct usb_mixer_interface *mixer = urb->context;
Daniel Macke213e9c2010-05-11 18:13:50 +02002037 int len = urb->actual_length;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002038
Daniel Macke213e9c2010-05-11 18:13:50 +02002039 if (urb->status != 0)
2040 goto requeue;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002041
Daniel Macke213e9c2010-05-11 18:13:50 +02002042 if (mixer->protocol == UAC_VERSION_1) {
2043 struct uac1_status_word *status;
2044
2045 for (status = urb->transfer_buffer;
2046 len >= sizeof(*status);
2047 len -= sizeof(*status), status++) {
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002048 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
Daniel Macke213e9c2010-05-11 18:13:50 +02002049 status->bStatusType,
2050 status->bOriginator);
2051
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002052 /* ignore any notifications not from the control interface */
Daniel Macke213e9c2010-05-11 18:13:50 +02002053 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2054 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002055 continue;
Daniel Macke213e9c2010-05-11 18:13:50 +02002056
2057 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
2058 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
Clemens Ladischb259b102005-04-29 16:29:28 +02002059 else
Daniel Macke213e9c2010-05-11 18:13:50 +02002060 snd_usb_mixer_notify_id(mixer, status->bOriginator);
2061 }
2062 } else { /* UAC_VERSION_2 */
2063 struct uac2_interrupt_data_msg *msg;
2064
2065 for (msg = urb->transfer_buffer;
2066 len >= sizeof(*msg);
2067 len -= sizeof(*msg), msg++) {
2068 /* drop vendor specific and endpoint requests */
2069 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
2070 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
2071 continue;
2072
2073 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
2074 le16_to_cpu(msg->wValue),
2075 le16_to_cpu(msg->wIndex));
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002076 }
2077 }
Daniel Macke213e9c2010-05-11 18:13:50 +02002078
2079requeue:
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002080 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
2081 urb->dev = mixer->chip->dev;
2082 usb_submit_urb(urb, GFP_ATOMIC);
2083 }
2084}
2085
2086/* create the handler for the optional status interrupt endpoint */
2087static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2088{
2089 struct usb_host_interface *hostif;
2090 struct usb_endpoint_descriptor *ep;
2091 void *transfer_buffer;
2092 int buffer_length;
2093 unsigned int epnum;
2094
2095 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
2096 /* we need one interrupt input endpoint */
2097 if (get_iface_desc(hostif)->bNumEndpoints < 1)
2098 return 0;
2099 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002100 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002101 return 0;
2102
Julia Lawall42a6e662008-12-29 11:23:02 +01002103 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002104 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
2105 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
2106 if (!transfer_buffer)
2107 return -ENOMEM;
2108 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2109 if (!mixer->urb) {
2110 kfree(transfer_buffer);
2111 return -ENOMEM;
2112 }
2113 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
2114 usb_rcvintpipe(mixer->chip->dev, epnum),
2115 transfer_buffer, buffer_length,
Daniel Macke213e9c2010-05-11 18:13:50 +02002116 snd_usb_mixer_interrupt, mixer, ep->bInterval);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002117 usb_submit_urb(mixer->urb, GFP_KERNEL);
2118 return 0;
2119}
2120
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002121int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2122 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002123{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002124 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002125 .dev_free = snd_usb_mixer_dev_free
2126 };
2127 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002128 struct snd_info_entry *entry;
Daniel Mack7b8a0432010-02-22 23:49:12 +01002129 struct usb_host_interface *host_iface;
Daniel Mack23caaf12010-03-11 21:13:25 +01002130 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002131
2132 strcpy(chip->card->mixername, "USB Mixer");
2133
Takashi Iwai561b2202005-09-09 14:22:34 +02002134 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002135 if (!mixer)
2136 return -ENOMEM;
2137 mixer->chip = chip;
2138 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002139 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002140 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2141 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002142 if (!mixer->id_elems) {
2143 kfree(mixer);
2144 return -ENOMEM;
2145 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002146
Daniel Mack7b8a0432010-02-22 23:49:12 +01002147 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
Jaroslav Kyselaca4c2ad2010-04-16 10:32:54 +02002148 mixer->protocol = get_iface_desc(host_iface)->bInterfaceProtocol;
Daniel Mack7b8a0432010-02-22 23:49:12 +01002149
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002150 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002151 (err = snd_usb_mixer_status_create(mixer)) < 0)
2152 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002153
Daniel Mack7b1eda22010-03-11 21:13:22 +01002154 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002155
Clemens Ladisch84957a82005-04-29 16:23:13 +02002156 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002157 if (err < 0)
2158 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002159
2160 if (list_empty(&chip->mixer_list) &&
2161 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2162 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2163
Clemens Ladisch84957a82005-04-29 16:23:13 +02002164 list_add(&mixer->list, &chip->mixer_list);
2165 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002166
2167_error:
2168 snd_usb_mixer_free(mixer);
2169 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002170}
2171
2172void snd_usb_mixer_disconnect(struct list_head *p)
2173{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002174 struct usb_mixer_interface *mixer;
Daniel Mack7b1eda22010-03-11 21:13:22 +01002175
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002176 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002177 usb_kill_urb(mixer->urb);
2178 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002179}