blob: 994b0385235c5d0a08a297bd359613fef6105081 [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>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/core.h>
38#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020039#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020040#include <sound/info.h>
Takashi Iwai7bc5ba72006-07-14 15:18:19 +020041#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010044#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010045#include "helper.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010046#include "mixer_quirks.h"
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020047
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010048#define MAX_ID_ELEMS 256
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct usb_audio_term {
51 int id;
52 int type;
53 int channels;
54 unsigned int chconfig;
55 int name;
56};
57
58struct usbmix_name_map;
59
Takashi Iwai86e07d32005-11-17 15:08:02 +010060struct mixer_build {
61 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020062 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned char *buffer;
64 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +010065 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +010066 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020068 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071enum {
72 USB_MIXER_BOOLEAN,
73 USB_MIXER_INV_BOOLEAN,
74 USB_MIXER_S8,
75 USB_MIXER_U8,
76 USB_MIXER_S16,
77 USB_MIXER_U16,
78};
79
80enum {
81 USB_PROC_UPDOWN = 1,
82 USB_PROC_UPDOWN_SWITCH = 1,
83 USB_PROC_UPDOWN_MODE_SEL = 2,
84
85 USB_PROC_PROLOGIC = 2,
86 USB_PROC_PROLOGIC_SWITCH = 1,
87 USB_PROC_PROLOGIC_MODE_SEL = 2,
88
89 USB_PROC_3DENH = 3,
90 USB_PROC_3DENH_SWITCH = 1,
91 USB_PROC_3DENH_SPACE = 2,
92
93 USB_PROC_REVERB = 4,
94 USB_PROC_REVERB_SWITCH = 1,
95 USB_PROC_REVERB_LEVEL = 2,
96 USB_PROC_REVERB_TIME = 3,
97 USB_PROC_REVERB_DELAY = 4,
98
99 USB_PROC_CHORUS = 5,
100 USB_PROC_CHORUS_SWITCH = 1,
101 USB_PROC_CHORUS_LEVEL = 2,
102 USB_PROC_CHORUS_RATE = 3,
103 USB_PROC_CHORUS_DEPTH = 4,
104
105 USB_PROC_DCR = 6,
106 USB_PROC_DCR_SWITCH = 1,
107 USB_PROC_DCR_RATIO = 2,
108 USB_PROC_DCR_MAX_AMP = 3,
109 USB_PROC_DCR_THRESHOLD = 4,
110 USB_PROC_DCR_ATTACK = 5,
111 USB_PROC_DCR_RELEASE = 6,
112};
113
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -0800114/*E-mu 0202(0404) eXtension Unit(XU) control*/
115enum {
116 USB_XU_CLOCK_RATE = 0xe301,
117 USB_XU_CLOCK_SOURCE = 0xe302,
118 USB_XU_DIGITAL_IO_STATUS = 0xe303,
119 USB_XU_DEVICE_OPTIONS = 0xe304,
120 USB_XU_DIRECT_MONITORING = 0xe305,
121 USB_XU_METERING = 0xe306
122};
123enum {
124 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
125 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
126 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
127 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * manual mapping of mixer names
132 * if the mixer topology is too complicated and the parsed names are
133 * ambiguous, add the entries in usbmixer_maps.c.
134 */
Daniel Mackf0b5e632010-03-11 21:13:23 +0100135#include "mixer_maps.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100137static const struct usbmix_name_map *
138find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100140 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100142 if (!p)
143 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100146 if (p->id == unitid &&
147 (!control || !p->control || control == p->control))
148 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100150 return NULL;
151}
152
153/* get the mapped name if the unit matches */
154static int
155check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
156{
157 if (!p || !p->name)
158 return 0;
159
160 buflen--;
161 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100165static inline int
166check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100168 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100170 return 1;
171}
172
173/* dB mapping */
174static inline void check_mapped_dB(const struct usbmix_name_map *p,
175 struct usb_mixer_elem_info *cval)
176{
177 if (p && p->dB) {
178 cval->dBmin = p->dB->min;
179 cval->dBmax = p->dB->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200183/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100184static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200185 int index, char *buf, int buflen)
186{
187 const struct usbmix_selector_map *p;
188
189 if (! state->selector_map)
190 return 0;
191 for (p = state->selector_map; p->id; p++) {
192 if (p->id == unitid && index < p->count)
193 return strlcpy(buf, p->names[index], buflen);
194 }
195 return 0;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * find an audio control unit with the given unit id
200 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100201static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 unsigned char *p;
204
205 p = NULL;
206 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
207 USB_DT_CS_INTERFACE)) != NULL) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100208 if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC_EXTENSION_UNIT_V1 && p[3] == unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return p;
210 }
211 return NULL;
212}
213
214
215/*
216 * copy a string with the given id
217 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100218static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
221 buf[len] = 0;
222 return len;
223}
224
225/*
226 * convert from the byte/word on usb descriptor to the zero-based integer
227 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100228static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 switch (cval->val_type) {
231 case USB_MIXER_BOOLEAN:
232 return !!val;
233 case USB_MIXER_INV_BOOLEAN:
234 return !val;
235 case USB_MIXER_U8:
236 val &= 0xff;
237 break;
238 case USB_MIXER_S8:
239 val &= 0xff;
240 if (val >= 0x80)
241 val -= 0x100;
242 break;
243 case USB_MIXER_U16:
244 val &= 0xffff;
245 break;
246 case USB_MIXER_S16:
247 val &= 0xffff;
248 if (val >= 0x8000)
249 val -= 0x10000;
250 break;
251 }
252 return val;
253}
254
255/*
256 * convert from the zero-based int to the byte/word for usb descriptor
257 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100258static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 switch (cval->val_type) {
261 case USB_MIXER_BOOLEAN:
262 return !!val;
263 case USB_MIXER_INV_BOOLEAN:
264 return !val;
265 case USB_MIXER_S8:
266 case USB_MIXER_U8:
267 return val & 0xff;
268 case USB_MIXER_S16:
269 case USB_MIXER_U16:
270 return val & 0xffff;
271 }
272 return 0; /* not reached */
273}
274
Takashi Iwai86e07d32005-11-17 15:08:02 +0100275static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 if (! cval->res)
278 cval->res = 1;
279 if (val < cval->min)
280 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200281 else if (val >= cval->max)
282 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 else
284 return (val - cval->min) / cval->res;
285}
286
Takashi Iwai86e07d32005-11-17 15:08:02 +0100287static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 if (val < 0)
290 return cval->min;
291 if (! cval->res)
292 cval->res = 1;
293 val *= cval->res;
294 val += cval->min;
295 if (val > cval->max)
296 return cval->max;
297 return val;
298}
299
300
301/*
302 * retrieve a mixer value
303 */
304
Takashi Iwai86e07d32005-11-17 15:08:02 +0100305static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 unsigned char buf[2];
308 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
309 int timeout = 10;
310
311 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200312 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
313 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 request,
315 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200316 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200317 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
319 return 0;
320 }
321 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200322 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
323 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -EINVAL;
325}
326
Takashi Iwai86e07d32005-11-17 15:08:02 +0100327static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100329 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100333static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
334 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100336 return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Takashi Iwai641b4872009-01-15 17:05:24 +0100339static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
340 int channel, int index, int *value)
341{
342 int err;
343
344 if (cval->cached & (1 << channel)) {
345 *value = cval->cache_val[index];
346 return 0;
347 }
348 err = get_cur_mix_raw(cval, channel, value);
349 if (err < 0) {
350 if (!cval->mixer->ignore_ctl_error)
351 snd_printd(KERN_ERR "cannot get current value for "
352 "control %d ch %d: err = %d\n",
353 cval->control, channel, err);
354 return err;
355 }
356 cval->cached |= 1 << channel;
357 cval->cache_val[index] = *value;
358 return 0;
359}
360
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/*
363 * set a mixer value
364 */
365
Daniel Mack7b1eda22010-03-11 21:13:22 +0100366int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
367 int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 unsigned char buf[2];
370 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
371 int timeout = 10;
372
373 value_set = convert_bytes_value(cval, value_set);
374 buf[0] = value_set & 0xff;
375 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530376 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200377 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
378 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 request,
380 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200381 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 buf, val_len, 100) >= 0)
383 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200384 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
385 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -EINVAL;
387}
388
Takashi Iwai86e07d32005-11-17 15:08:02 +0100389static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Daniel Mack7b1eda22010-03-11 21:13:22 +0100391 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Takashi Iwai641b4872009-01-15 17:05:24 +0100394static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
395 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Takashi Iwai641b4872009-01-15 17:05:24 +0100397 int err;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100398 err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100399 value);
400 if (err < 0)
401 return err;
402 cval->cached |= 1 << channel;
403 cval->cache_val[index] = value;
404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200407/*
408 * TLV callback for mixer volume controls
409 */
410static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
411 unsigned int size, unsigned int __user *_tlv)
412{
413 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200414 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200415
416 if (size < sizeof(scale))
417 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100418 scale[2] = cval->dBmin;
419 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200420 if (copy_to_user(_tlv, scale, sizeof(scale)))
421 return -EFAULT;
422 return 0;
423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425/*
426 * parser routines begin here...
427 */
428
Takashi Iwai86e07d32005-11-17 15:08:02 +0100429static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431
432/*
433 * check if the input/output channel routing is enabled on the given bitmap.
434 * used for mixer unit parser
435 */
436static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
437{
438 int idx = ich * num_outs + och;
439 return bmap[idx >> 3] & (0x80 >> (idx & 7));
440}
441
442
443/*
444 * add an alsa control element
445 * search and increment the index until an empty slot is found.
446 *
447 * if failed, give up and free the control instance.
448 */
449
Takashi Iwai86e07d32005-11-17 15:08:02 +0100450static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100452 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200454
455 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200457 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200461 cval->elem_id = &kctl->id;
462 cval->next_id_elem = state->mixer->id_elems[cval->id];
463 state->mixer->id_elems[cval->id] = cval;
464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467
468/*
469 * get a terminal name string
470 */
471
472static struct iterm_name_combo {
473 int type;
474 char *name;
475} iterm_names[] = {
476 { 0x0300, "Output" },
477 { 0x0301, "Speaker" },
478 { 0x0302, "Headphone" },
479 { 0x0303, "HMD Audio" },
480 { 0x0304, "Desktop Speaker" },
481 { 0x0305, "Room Speaker" },
482 { 0x0306, "Com Speaker" },
483 { 0x0307, "LFE" },
484 { 0x0600, "External In" },
485 { 0x0601, "Analog In" },
486 { 0x0602, "Digital In" },
487 { 0x0603, "Line" },
488 { 0x0604, "Legacy In" },
489 { 0x0605, "IEC958 In" },
490 { 0x0606, "1394 DA Stream" },
491 { 0x0607, "1394 DV Stream" },
492 { 0x0700, "Embedded" },
493 { 0x0701, "Noise Source" },
494 { 0x0702, "Equalization Noise" },
495 { 0x0703, "CD" },
496 { 0x0704, "DAT" },
497 { 0x0705, "DCC" },
498 { 0x0706, "MiniDisk" },
499 { 0x0707, "Analog Tape" },
500 { 0x0708, "Phonograph" },
501 { 0x0709, "VCR Audio" },
502 { 0x070a, "Video Disk Audio" },
503 { 0x070b, "DVD Audio" },
504 { 0x070c, "TV Tuner Audio" },
505 { 0x070d, "Satellite Rec Audio" },
506 { 0x070e, "Cable Tuner Audio" },
507 { 0x070f, "DSS Audio" },
508 { 0x0710, "Radio Receiver" },
509 { 0x0711, "Radio Transmitter" },
510 { 0x0712, "Multi-Track Recorder" },
511 { 0x0713, "Synthesizer" },
512 { 0 },
513};
514
Takashi Iwai86e07d32005-11-17 15:08:02 +0100515static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned char *name, int maxlen, int term_only)
517{
518 struct iterm_name_combo *names;
519
520 if (iterm->name)
521 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
522
523 /* virtual type - not a real terminal */
524 if (iterm->type >> 16) {
525 if (term_only)
526 return 0;
527 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100528 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 strcpy(name, "Selector"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100530 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 strcpy(name, "Process Unit"); return 12;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100532 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 strcpy(name, "Ext Unit"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100534 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 strcpy(name, "Mixer"); return 5;
536 default:
537 return sprintf(name, "Unit %d", iterm->id);
538 }
539 }
540
541 switch (iterm->type & 0xff00) {
542 case 0x0100:
543 strcpy(name, "PCM"); return 3;
544 case 0x0200:
545 strcpy(name, "Mic"); return 3;
546 case 0x0400:
547 strcpy(name, "Headset"); return 7;
548 case 0x0500:
549 strcpy(name, "Phone"); return 5;
550 }
551
552 for (names = iterm_names; names->type; names++)
553 if (names->type == iterm->type) {
554 strcpy(name, names->name);
555 return strlen(names->name);
556 }
557 return 0;
558}
559
560
561/*
562 * parse the source unit recursively until it reaches to a terminal
563 * or a branched unit.
564 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100565static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 unsigned char *p1;
568
569 memset(term, 0, sizeof(*term));
570 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
571 term->id = id;
572 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100573 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 term->type = combine_word(p1 + 4);
575 term->channels = p1[7];
576 term->chconfig = combine_word(p1 + 8);
577 term->name = p1[11];
578 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100579 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 id = p1[4];
581 break; /* continue to parse */
Daniel Mackde48c7b2010-02-22 23:49:13 +0100582 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 term->type = p1[2] << 16; /* virtual type */
584 term->channels = p1[5 + p1[4]];
585 term->chconfig = combine_word(p1 + 6 + p1[4]);
586 term->name = p1[p1[0] - 1];
587 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100588 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* call recursively to retrieve the channel info */
590 if (check_input_term(state, p1[5], term) < 0)
591 return -ENODEV;
592 term->type = p1[2] << 16; /* virtual type */
593 term->id = id;
594 term->name = p1[9 + p1[0] - 1];
595 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100596 case UAC_PROCESSING_UNIT_V1:
597 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (p1[6] == 1) {
599 id = p1[7];
600 break; /* continue to parse */
601 }
602 term->type = p1[2] << 16; /* virtual type */
603 term->channels = p1[7 + p1[6]];
604 term->chconfig = combine_word(p1 + 8 + p1[6]);
605 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
606 return 0;
607 default:
608 return -ENODEV;
609 }
610 }
611 return -ENODEV;
612}
613
614
615/*
616 * Feature Unit
617 */
618
619/* feature unit control information */
620struct usb_feature_control_info {
621 const char *name;
622 unsigned int type; /* control type (mute, volume, etc.) */
623};
624
625static struct usb_feature_control_info audio_feature_info[] = {
626 { "Mute", USB_MIXER_INV_BOOLEAN },
627 { "Volume", USB_MIXER_S16 },
628 { "Tone Control - Bass", USB_MIXER_S8 },
629 { "Tone Control - Mid", USB_MIXER_S8 },
630 { "Tone Control - Treble", USB_MIXER_S8 },
631 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
632 { "Auto Gain Control", USB_MIXER_BOOLEAN },
633 { "Delay Control", USB_MIXER_U16 },
634 { "Bass Boost", USB_MIXER_BOOLEAN },
635 { "Loudness", USB_MIXER_BOOLEAN },
636};
637
638
639/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100640static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Jesper Juhl4d572772005-05-30 17:30:32 +0200642 kfree(kctl->private_data);
643 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646
647/*
648 * interface to ALSA control for feature/mixer units
649 */
650
651/*
652 * retrieve the minimum and maximum values for the specified control
653 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100654static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 /* for failsafe */
657 cval->min = default_min;
658 cval->max = cval->min + 1;
659 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100660 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (cval->val_type == USB_MIXER_BOOLEAN ||
663 cval->val_type == USB_MIXER_INV_BOOLEAN) {
664 cval->initialized = 1;
665 } else {
666 int minchn = 0;
667 if (cval->cmask) {
668 int i;
669 for (i = 0; i < MAX_CHANNELS; i++)
670 if (cval->cmask & (1 << i)) {
671 minchn = i + 1;
672 break;
673 }
674 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100675 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
676 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200677 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
678 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return -EINVAL;
680 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100681 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 cval->res = 1;
683 } else {
684 int last_valid_res = cval->res;
685
686 while (cval->res > 1) {
Daniel Mack7b1eda22010-03-11 21:13:22 +0100687 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
688 (cval->control << 8) | minchn, cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 break;
690 cval->res /= 2;
691 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100692 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 cval->res = last_valid_res;
694 }
695 if (cval->res == 0)
696 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200697
698 /* Additional checks for the proper resolution
699 *
700 * Some devices report smaller resolutions than actually
701 * reacting. They don't return errors but simply clip
702 * to the lower aligned value.
703 */
704 if (cval->min + cval->res < cval->max) {
705 int last_valid_res = cval->res;
706 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100707 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200708 for (;;) {
709 test = saved;
710 if (test < cval->max)
711 test += cval->res;
712 else
713 test -= cval->res;
714 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100715 set_cur_mix_value(cval, minchn, 0, test) ||
716 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200717 cval->res = last_valid_res;
718 break;
719 }
720 if (test == check)
721 break;
722 cval->res *= 2;
723 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100724 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 cval->initialized = 1;
728 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100729
730 /* USB descriptions contain the dB scale in 1/256 dB unit
731 * while ALSA TLV contains in 1/100 dB unit
732 */
733 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
734 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
735 if (cval->dBmin > cval->dBmax) {
736 /* something is wrong; assume it's either from/to 0dB */
737 if (cval->dBmin < 0)
738 cval->dBmax = 0;
739 else if (cval->dBmin > 0)
740 cval->dBmin = 0;
741 if (cval->dBmin > cval->dBmax) {
742 /* totally crap, return an error */
743 return -EINVAL;
744 }
745 }
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
748}
749
750
751/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100752static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100754 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (cval->val_type == USB_MIXER_BOOLEAN ||
757 cval->val_type == USB_MIXER_INV_BOOLEAN)
758 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
759 else
760 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
761 uinfo->count = cval->channels;
762 if (cval->val_type == USB_MIXER_BOOLEAN ||
763 cval->val_type == USB_MIXER_INV_BOOLEAN) {
764 uinfo->value.integer.min = 0;
765 uinfo->value.integer.max = 1;
766 } else {
767 if (! cval->initialized)
768 get_min_max(cval, 0);
769 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200770 uinfo->value.integer.max =
771 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 return 0;
774}
775
776/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100777static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100779 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int c, cnt, val, err;
781
Takashi Iwai641b4872009-01-15 17:05:24 +0100782 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (cval->cmask) {
784 cnt = 0;
785 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100786 if (!(cval->cmask & (1 << c)))
787 continue;
788 err = get_cur_mix_value(cval, c + 1, cnt, &val);
789 if (err < 0)
790 return cval->mixer->ignore_ctl_error ? 0 : err;
791 val = get_relative_value(cval, val);
792 ucontrol->value.integer.value[cnt] = val;
793 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 } else {
797 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100798 err = get_cur_mix_value(cval, 0, 0, &val);
799 if (err < 0)
800 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 val = get_relative_value(cval, val);
802 ucontrol->value.integer.value[0] = val;
803 }
804 return 0;
805}
806
807/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100808static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100810 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int c, cnt, val, oval, err;
812 int changed = 0;
813
814 if (cval->cmask) {
815 cnt = 0;
816 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100817 if (!(cval->cmask & (1 << c)))
818 continue;
819 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
820 if (err < 0)
821 return cval->mixer->ignore_ctl_error ? 0 : err;
822 val = ucontrol->value.integer.value[cnt];
823 val = get_abs_value(cval, val);
824 if (oval != val) {
825 set_cur_mix_value(cval, c + 1, cnt, val);
826 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100828 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 } else {
831 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100832 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100834 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 val = ucontrol->value.integer.value[0];
836 val = get_abs_value(cval, val);
837 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100838 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 changed = 1;
840 }
841 }
842 return changed;
843}
844
Takashi Iwai86e07d32005-11-17 15:08:02 +0100845static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
847 .name = "", /* will be filled later manually */
848 .info = mixer_ctl_feature_info,
849 .get = mixer_ctl_feature_get,
850 .put = mixer_ctl_feature_put,
851};
852
853
854/*
855 * build a feature control
856 */
857
Takashi Iwai08d1e632009-10-02 14:06:08 +0200858static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
859{
860 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
861}
862
Daniel Mack99fc8642010-03-11 21:13:24 +0100863static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100865 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Daniel Mack99fc8642010-03-11 21:13:24 +0100867 struct uac_feature_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned int len = 0;
869 int mapped_name = 0;
Daniel Mack99fc8642010-03-11 21:13:24 +0100870 int nameid = uac_feature_unit_iFeature(desc);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100871 struct snd_kcontrol *kctl;
872 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100873 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 control++; /* change from zero-based to 1-based value */
876
Daniel Mack45d76052010-03-11 21:13:21 +0100877 if (control == UAC_GRAPHIC_EQUALIZER_CONTROL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* FIXME: not supported yet */
879 return;
880 }
881
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100882 map = find_map(state, unitid, control);
883 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return;
885
Takashi Iwai561b2202005-09-09 14:22:34 +0200886 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (! cval) {
888 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
889 return;
890 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200891 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 cval->id = unitid;
893 cval->control = control;
894 cval->cmask = ctl_mask;
895 cval->val_type = audio_feature_info[control-1].type;
896 if (ctl_mask == 0)
897 cval->channels = 1; /* master channel */
898 else {
899 int i, c = 0;
900 for (i = 0; i < 16; i++)
901 if (ctl_mask & (1 << i))
902 c++;
903 cval->channels = c;
904 }
905
906 /* get min/max values */
907 get_min_max(cval, 0);
908
909 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
910 if (! kctl) {
911 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
912 kfree(cval);
913 return;
914 }
915 kctl->private_free = usb_mixer_elem_free;
916
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100917 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 mapped_name = len != 0;
919 if (! len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100920 len = snd_usb_copy_string_desc(state, nameid,
921 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 switch (control) {
Daniel Mack45d76052010-03-11 21:13:21 +0100924 case UAC_MUTE_CONTROL:
925 case UAC_VOLUME_CONTROL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* determine the control name. the rule is:
927 * - if a name id is given in descriptor, use it.
928 * - if the connected input can be determined, then use the name
929 * of terminal type.
930 * - if the connected output can be determined, use it.
931 * - otherwise, anonymous name.
932 */
933 if (! len) {
934 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
935 if (! len)
936 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
937 if (! len)
938 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
939 "Feature %d", unitid);
940 }
941 /* determine the stream direction:
942 * if the connected output is USB stream, then it's likely a
943 * capture stream. otherwise it should be playback (hopefully :)
944 */
945 if (! mapped_name && ! (state->oterm.type >> 16)) {
946 if ((state->oterm.type & 0xff00) == 0x0100) {
Takashi Iwai08d1e632009-10-02 14:06:08 +0200947 len = append_ctl_name(kctl, " Capture");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 } else {
Takashi Iwai08d1e632009-10-02 14:06:08 +0200949 len = append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 }
Daniel Mack45d76052010-03-11 21:13:21 +0100952 append_ctl_name(kctl, control == UAC_MUTE_CONTROL ?
Takashi Iwai08d1e632009-10-02 14:06:08 +0200953 " Switch" : " Volume");
Daniel Mack45d76052010-03-11 21:13:21 +0100954 if (control == UAC_VOLUME_CONTROL) {
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200955 kctl->tlv.c = mixer_vol_tlv;
956 kctl->vd[0].access |=
957 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
958 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100959 check_mapped_dB(map, cval);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962
963 default:
964 if (! len)
965 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
966 sizeof(kctl->id.name));
967 break;
968 }
969
Alexey Fisher2cf313e2009-07-22 14:57:54 +0200970 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200971 switch (state->chip->usb_id) {
972 case USB_ID(0x0471, 0x0101):
973 case USB_ID(0x0471, 0x0104):
974 case USB_ID(0x0471, 0x0105):
975 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +0200976 /* quirk for UDA1321/N101.
977 * note that detection between firmware 2.1.1.7 (N101)
978 * and later 2.1.1.21 is not very clear from datasheets.
979 * I hope that the min value is -15360 for newer firmware --jk
980 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200981 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
982 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +0200983 snd_printk(KERN_INFO
984 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200985 cval->max = -256;
986 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +0200987 break;
988
989 case USB_ID(0x046d, 0x09a4):
990 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
991 snd_printk(KERN_INFO
992 "set volume quirk for QuickCam E3500\n");
993 cval->min = 6080;
994 cval->max = 8768;
995 cval->res = 192;
996 }
997 break;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
1001 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1002 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001003 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006
1007
1008/*
1009 * parse a feature unit
1010 *
1011 * most of controlls are defined here.
1012 */
Daniel Mack28e1b772010-02-22 23:49:09 +01001013static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001016 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned int master_bits, first_ch_bits;
1018 int err, csize;
Daniel Mack28e1b772010-02-22 23:49:09 +01001019 struct uac_feature_unit_descriptor *ftr = _ftr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Daniel Mack28e1b772010-02-22 23:49:09 +01001021 if (ftr->bLength < 7 || ! (csize = ftr->bControlSize) || ftr->bLength < 7 + csize) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001022 snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EINVAL;
1024 }
1025
1026 /* parse the source unit */
Daniel Mack28e1b772010-02-22 23:49:09 +01001027 if ((err = parse_audio_unit(state, ftr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return err;
1029
1030 /* determine the input source type and name */
Daniel Mack28e1b772010-02-22 23:49:09 +01001031 if (check_input_term(state, ftr->bSourceID, &iterm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -EINVAL;
1033
Daniel Mack28e1b772010-02-22 23:49:09 +01001034 channels = (ftr->bLength - 7) / csize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Daniel Mack99fc8642010-03-11 21:13:24 +01001036 master_bits = snd_usb_combine_bytes(ftr->bmaControls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001037 /* master configuration quirks */
1038 switch (state->chip->usb_id) {
1039 case USB_ID(0x08bb, 0x2702):
1040 snd_printk(KERN_INFO
1041 "usbmixer: master volume quirk for PCM2702 chip\n");
1042 /* disable non-functional volume control */
Daniel Mack45d76052010-03-11 21:13:21 +01001043 master_bits &= ~UAC_FU_VOLUME;
Javier Kohen0c3cee52009-11-17 15:36:13 +01001044 break;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (channels > 0)
Daniel Mack99fc8642010-03-11 21:13:24 +01001047 first_ch_bits = snd_usb_combine_bytes(ftr->bmaControls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 else
1049 first_ch_bits = 0;
1050 /* check all control types */
1051 for (i = 0; i < 10; i++) {
1052 unsigned int ch_bits = 0;
1053 for (j = 0; j < channels; j++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001054 unsigned int mask = snd_usb_combine_bytes(ftr->bmaControls + csize * (j+1), csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (mask & (1 << i))
1056 ch_bits |= (1 << j);
1057 }
1058 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
Daniel Mack28e1b772010-02-22 23:49:09 +01001059 build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (master_bits & (1 << i))
Daniel Mack28e1b772010-02-22 23:49:09 +01001061 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
1064 return 0;
1065}
1066
1067
1068/*
1069 * Mixer Unit
1070 */
1071
1072/*
1073 * build a mixer unit control
1074 *
1075 * the callbacks are identical with feature unit.
1076 * input channel number (zero based) is given in control field instead.
1077 */
1078
Daniel Mack99fc8642010-03-11 21:13:24 +01001079static void build_mixer_unit_ctl(struct mixer_build *state,
1080 struct uac_mixer_unit_descriptor *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001082 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001084 struct usb_mixer_elem_info *cval;
Daniel Mack99fc8642010-03-11 21:13:24 +01001085 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001087 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001088 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001090 map = find_map(state, unitid, 0);
1091 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return;
1093
Takashi Iwai561b2202005-09-09 14:22:34 +02001094 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (! cval)
1096 return;
1097
Clemens Ladisch84957a82005-04-29 16:23:13 +02001098 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 cval->id = unitid;
1100 cval->control = in_ch + 1; /* based on 1 */
1101 cval->val_type = USB_MIXER_S16;
1102 for (i = 0; i < num_outs; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001103 if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc), in_ch, i, num_outs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 cval->cmask |= (1 << i);
1105 cval->channels++;
1106 }
1107 }
1108
1109 /* get min/max values */
1110 get_min_max(cval, 0);
1111
1112 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1113 if (! kctl) {
1114 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1115 kfree(cval);
1116 return;
1117 }
1118 kctl->private_free = usb_mixer_elem_free;
1119
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001120 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (! len)
1122 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1123 if (! len)
1124 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001125 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1128 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001129 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132
1133/*
1134 * parse a mixer unit
1135 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001136static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Daniel Mack99fc8642010-03-11 21:13:24 +01001138 struct uac_mixer_unit_descriptor *desc = raw_desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001139 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int input_pins, num_ins, num_outs;
1141 int pin, ich, err;
1142
Daniel Mack99fc8642010-03-11 21:13:24 +01001143 if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1145 return -EINVAL;
1146 }
1147 /* no bmControls field (e.g. Maya44) -> ignore */
Daniel Mack99fc8642010-03-11 21:13:24 +01001148 if (desc->bLength <= 10 + input_pins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1150 return 0;
1151 }
1152
1153 num_ins = 0;
1154 ich = 0;
1155 for (pin = 0; pin < input_pins; pin++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001156 err = parse_audio_unit(state, desc->baSourceID[pin]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (err < 0)
1158 return err;
Daniel Mack99fc8642010-03-11 21:13:24 +01001159 err = check_input_term(state, desc->baSourceID[pin], &iterm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (err < 0)
1161 return err;
1162 num_ins += iterm.channels;
1163 for (; ich < num_ins; ++ich) {
1164 int och, ich_has_controls = 0;
1165
1166 for (och = 0; och < num_outs; ++och) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001167 if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ich, och, num_outs)) {
1169 ich_has_controls = 1;
1170 break;
1171 }
1172 }
1173 if (ich_has_controls)
1174 build_mixer_unit_ctl(state, desc, pin, ich,
1175 unitid, &iterm);
1176 }
1177 }
1178 return 0;
1179}
1180
1181
1182/*
1183 * Processing Unit / Extension Unit
1184 */
1185
1186/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001187static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001189 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int err, val;
1191
1192 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001193 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ucontrol->value.integer.value[0] = cval->min;
1195 return 0;
1196 }
1197 if (err < 0)
1198 return err;
1199 val = get_relative_value(cval, val);
1200 ucontrol->value.integer.value[0] = val;
1201 return 0;
1202}
1203
1204/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001205static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001207 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int val, oval, err;
1209
1210 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1211 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001212 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 0;
1214 return err;
1215 }
1216 val = ucontrol->value.integer.value[0];
1217 val = get_abs_value(cval, val);
1218 if (val != oval) {
1219 set_cur_ctl_value(cval, cval->control << 8, val);
1220 return 1;
1221 }
1222 return 0;
1223}
1224
1225/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001226static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1228 .name = "", /* will be filled later */
1229 .info = mixer_ctl_feature_info,
1230 .get = mixer_ctl_procunit_get,
1231 .put = mixer_ctl_procunit_put,
1232};
1233
1234
1235/*
1236 * predefined data for processing units
1237 */
1238struct procunit_value_info {
1239 int control;
1240 char *suffix;
1241 int val_type;
1242 int min_value;
1243};
1244
1245struct procunit_info {
1246 int type;
1247 char *name;
1248 struct procunit_value_info *values;
1249};
1250
1251static struct procunit_value_info updown_proc_info[] = {
1252 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1253 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1254 { 0 }
1255};
1256static struct procunit_value_info prologic_proc_info[] = {
1257 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1258 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1259 { 0 }
1260};
1261static struct procunit_value_info threed_enh_proc_info[] = {
1262 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1263 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1264 { 0 }
1265};
1266static struct procunit_value_info reverb_proc_info[] = {
1267 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1268 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1269 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1270 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1271 { 0 }
1272};
1273static struct procunit_value_info chorus_proc_info[] = {
1274 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1275 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1276 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1277 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1278 { 0 }
1279};
1280static struct procunit_value_info dcr_proc_info[] = {
1281 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1282 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1283 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1284 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1285 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1286 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1287 { 0 }
1288};
1289
1290static struct procunit_info procunits[] = {
1291 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1292 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1293 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1294 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1295 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1296 { USB_PROC_DCR, "DCR", dcr_proc_info },
1297 { 0 },
1298};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001299/*
1300 * predefined data for extension units
1301 */
1302static struct procunit_value_info clock_rate_xu_info[] = {
1303 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1304 { 0 }
1305};
1306static struct procunit_value_info clock_source_xu_info[] = {
1307 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1308 { 0 }
1309};
1310static struct procunit_value_info spdif_format_xu_info[] = {
1311 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1312 { 0 }
1313};
1314static struct procunit_value_info soft_limit_xu_info[] = {
1315 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1316 { 0 }
1317};
1318static struct procunit_info extunits[] = {
1319 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1320 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1321 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1322 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1323 { 0 }
1324};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/*
1326 * build a processing/extension unit
1327 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001328static 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 -07001329{
Daniel Mack99fc8642010-03-11 21:13:24 +01001330 struct uac_processing_unit_descriptor *desc = raw_desc;
1331 int num_ins = desc->bNrInPins;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001332 struct usb_mixer_elem_info *cval;
1333 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 int i, err, nameid, type, len;
1335 struct procunit_info *info;
1336 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001337 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 static struct procunit_value_info default_value_info[] = {
1339 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1340 { 0 }
1341 };
1342 static struct procunit_info default_info = {
1343 0, NULL, default_value_info
1344 };
1345
Daniel Mack99fc8642010-03-11 21:13:24 +01001346 if (desc->bLength < 13 || desc->bLength < 13 + num_ins || desc->bLength < num_ins + uac_processing_unit_bControlSize(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1348 return -EINVAL;
1349 }
1350
1351 for (i = 0; i < num_ins; i++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001352 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return err;
1354 }
1355
Daniel Mack99fc8642010-03-11 21:13:24 +01001356 type = le16_to_cpu(desc->wProcessType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 for (info = list; info && info->type; info++)
1358 if (info->type == type)
1359 break;
1360 if (! info || ! info->type)
1361 info = &default_info;
1362
1363 for (valinfo = info->values; valinfo->control; valinfo++) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001364 __u8 *controls = uac_processing_unit_bmControls(desc);
1365
1366 if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001368 map = find_map(state, unitid, valinfo->control);
1369 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001371 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (! cval) {
1373 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1374 return -ENOMEM;
1375 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001376 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 cval->id = unitid;
1378 cval->control = valinfo->control;
1379 cval->val_type = valinfo->val_type;
1380 cval->channels = 1;
1381
1382 /* get min/max values */
1383 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
Daniel Mack99fc8642010-03-11 21:13:24 +01001384 __u8 *control_spec = uac_processing_unit_specific(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* FIXME: hard-coded */
1386 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01001387 cval->max = control_spec[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 cval->res = 1;
1389 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001390 } else {
1391 if (type == USB_XU_CLOCK_RATE) {
1392 /* E-Mu USB 0404/0202/TrackerPre
1393 * samplerate control quirk
1394 */
1395 cval->min = 0;
1396 cval->max = 5;
1397 cval->res = 1;
1398 cval->initialized = 1;
1399 } else
1400 get_min_max(cval, valinfo->min_value);
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1404 if (! kctl) {
1405 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1406 kfree(cval);
1407 return -ENOMEM;
1408 }
1409 kctl->private_free = usb_mixer_elem_free;
1410
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001411 if (check_mapped_name(map, kctl->id.name,
1412 sizeof(kctl->id.name)))
1413 /* nothing */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 else if (info->name)
1415 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1416 else {
Daniel Mack99fc8642010-03-11 21:13:24 +01001417 nameid = uac_processing_unit_iProcessing(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 len = 0;
1419 if (nameid)
1420 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1421 if (! len)
1422 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1423 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001424 append_ctl_name(kctl, " ");
1425 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1428 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001429 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return err;
1431 }
1432 return 0;
1433}
1434
1435
Daniel Mack99fc8642010-03-11 21:13:24 +01001436static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Daniel Mack99fc8642010-03-11 21:13:24 +01001438 return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Daniel Mack99fc8642010-03-11 21:13:24 +01001441static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Daniel Mack99fc8642010-03-11 21:13:24 +01001443 /* Note that we parse extension units with processing unit descriptors.
1444 * That's ok as the layout is the same */
1445 return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448
1449/*
1450 * Selector Unit
1451 */
1452
1453/* info callback for selector unit
1454 * use an enumerator type for routing
1455 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001456static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001458 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 char **itemlist = (char **)kcontrol->private_value;
1460
Takashi Iwai5e246b82008-08-08 17:12:47 +02001461 if (snd_BUG_ON(!itemlist))
1462 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1464 uinfo->count = 1;
1465 uinfo->value.enumerated.items = cval->max;
1466 if ((int)uinfo->value.enumerated.item >= cval->max)
1467 uinfo->value.enumerated.item = cval->max - 1;
1468 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1469 return 0;
1470}
1471
1472/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001473static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001475 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int val, err;
1477
1478 err = get_cur_ctl_value(cval, 0, &val);
1479 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001480 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ucontrol->value.enumerated.item[0] = 0;
1482 return 0;
1483 }
1484 return err;
1485 }
1486 val = get_relative_value(cval, val);
1487 ucontrol->value.enumerated.item[0] = val;
1488 return 0;
1489}
1490
1491/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001492static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001494 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int val, oval, err;
1496
1497 err = get_cur_ctl_value(cval, 0, &oval);
1498 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001499 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501 return err;
1502 }
1503 val = ucontrol->value.enumerated.item[0];
1504 val = get_abs_value(cval, val);
1505 if (val != oval) {
1506 set_cur_ctl_value(cval, 0, val);
1507 return 1;
1508 }
1509 return 0;
1510}
1511
1512/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001513static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1515 .name = "", /* will be filled later */
1516 .info = mixer_ctl_selector_info,
1517 .get = mixer_ctl_selector_get,
1518 .put = mixer_ctl_selector_put,
1519};
1520
1521
1522/* private free callback.
1523 * free both private_data and private_value
1524 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001525static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 int i, num_ins = 0;
1528
1529 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001530 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 num_ins = cval->max;
1532 kfree(cval);
1533 kctl->private_data = NULL;
1534 }
1535 if (kctl->private_value) {
1536 char **itemlist = (char **)kctl->private_value;
1537 for (i = 0; i < num_ins; i++)
1538 kfree(itemlist[i]);
1539 kfree(itemlist);
1540 kctl->private_value = 0;
1541 }
1542}
1543
1544/*
1545 * parse a selector unit
1546 */
Daniel Mack99fc8642010-03-11 21:13:24 +01001547static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Daniel Mack99fc8642010-03-11 21:13:24 +01001549 struct uac_selector_unit_descriptor *desc = raw_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 unsigned int i, nameid, len;
1551 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001552 struct usb_mixer_elem_info *cval;
1553 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001554 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 char **namelist;
1556
Daniel Mack99fc8642010-03-11 21:13:24 +01001557 if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1559 return -EINVAL;
1560 }
1561
Daniel Mack99fc8642010-03-11 21:13:24 +01001562 for (i = 0; i < desc->bNrInPins; i++) {
1563 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return err;
1565 }
1566
Daniel Mack99fc8642010-03-11 21:13:24 +01001567 if (desc->bNrInPins == 1) /* only one ? nonsense! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return 0;
1569
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001570 map = find_map(state, unitid, 0);
1571 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return 0;
1573
Takashi Iwai561b2202005-09-09 14:22:34 +02001574 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (! cval) {
1576 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1577 return -ENOMEM;
1578 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001579 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cval->id = unitid;
1581 cval->val_type = USB_MIXER_U8;
1582 cval->channels = 1;
1583 cval->min = 1;
Daniel Mack99fc8642010-03-11 21:13:24 +01001584 cval->max = desc->bNrInPins;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 cval->res = 1;
1586 cval->initialized = 1;
1587
Daniel Mack99fc8642010-03-11 21:13:24 +01001588 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (! namelist) {
1590 snd_printk(KERN_ERR "cannot malloc\n");
1591 kfree(cval);
1592 return -ENOMEM;
1593 }
1594#define MAX_ITEM_NAME_LEN 64
Daniel Mack99fc8642010-03-11 21:13:24 +01001595 for (i = 0; i < desc->bNrInPins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001596 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 len = 0;
1598 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1599 if (! namelist[i]) {
1600 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001601 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 kfree(namelist[i]);
1603 kfree(namelist);
1604 kfree(cval);
1605 return -ENOMEM;
1606 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001607 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1608 MAX_ITEM_NAME_LEN);
Daniel Mack99fc8642010-03-11 21:13:24 +01001609 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1611 if (! len)
1612 sprintf(namelist[i], "Input %d", i);
1613 }
1614
1615 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1616 if (! kctl) {
1617 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001618 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 kfree(cval);
1620 return -ENOMEM;
1621 }
1622 kctl->private_value = (unsigned long)namelist;
1623 kctl->private_free = usb_mixer_selector_elem_free;
1624
Daniel Mack99fc8642010-03-11 21:13:24 +01001625 nameid = uac_selector_unit_iSelector(desc);
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001626 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (len)
1628 ;
1629 else if (nameid)
1630 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1631 else {
1632 len = get_term_name(state, &state->oterm,
1633 kctl->id.name, sizeof(kctl->id.name), 0);
1634 if (! len)
1635 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1636
1637 if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02001638 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02001640 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642
1643 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
Daniel Mack99fc8642010-03-11 21:13:24 +01001644 cval->id, kctl->id.name, desc->bNrInPins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001645 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return err;
1647
1648 return 0;
1649}
1650
1651
1652/*
1653 * parse an audio unit recursively
1654 */
1655
Takashi Iwai86e07d32005-11-17 15:08:02 +01001656static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 unsigned char *p1;
1659
1660 if (test_and_set_bit(unitid, state->unitbitmap))
1661 return 0; /* the unit already visited */
1662
1663 p1 = find_audio_control_unit(state, unitid);
1664 if (!p1) {
1665 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1666 return -EINVAL;
1667 }
1668
1669 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001670 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01001672 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001674 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001676 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001678 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return parse_audio_processing_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001680 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return parse_audio_extension_unit(state, unitid, p1);
1682 default:
1683 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1684 return -EINVAL;
1685 }
1686}
1687
Clemens Ladisch84957a82005-04-29 16:23:13 +02001688static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1689{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001690 kfree(mixer->id_elems);
1691 if (mixer->urb) {
1692 kfree(mixer->urb->transfer_buffer);
1693 usb_free_urb(mixer->urb);
1694 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001695 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001696 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001697 kfree(mixer);
1698}
1699
Takashi Iwai86e07d32005-11-17 15:08:02 +01001700static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001701{
1702 struct usb_mixer_interface *mixer = device->device_data;
1703 snd_usb_mixer_free(mixer);
1704 return 0;
1705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
1708 * create mixer controls
1709 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01001710 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001712static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Daniel Mack28e1b772010-02-22 23:49:09 +01001714 struct uac_output_terminal_descriptor_v1 *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001715 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int err;
1717 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001718 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Clemens Ladisch84957a82005-04-29 16:23:13 +02001720 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001722 state.chip = mixer->chip;
1723 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 state.buffer = hostif->extra;
1725 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001728 for (map = usbmix_ctl_maps; map->id; map++) {
1729 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001731 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001732 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 break;
1734 }
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 desc = NULL;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001738 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack28e1b772010-02-22 23:49:09 +01001739 if (desc->bLength < 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 continue; /* invalid descriptor? */
Daniel Mack28e1b772010-02-22 23:49:09 +01001741 set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
1742 state.oterm.id = desc->bTerminalID;
1743 state.oterm.type = le16_to_cpu(desc->wTerminalType);
1744 state.oterm.name = desc->iTerminal;
1745 err = parse_audio_unit(&state, desc->bSourceID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (err < 0)
1747 return err;
1748 }
1749 return 0;
1750}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001751
Daniel Mack7b1eda22010-03-11 21:13:22 +01001752void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001753{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001754 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001755
1756 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1757 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1758 info->elem_id);
1759}
1760
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001761static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1762 int unitid,
1763 struct usb_mixer_elem_info *cval)
1764{
1765 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1766 "S8", "U8", "S16", "U16"};
1767 snd_iprintf(buffer, " Unit: %i\n", unitid);
1768 if (cval->elem_id)
1769 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1770 cval->elem_id->name, cval->elem_id->index);
1771 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1772 "channels=%i, type=\"%s\"\n", cval->id,
1773 cval->control, cval->cmask, cval->channels,
1774 val_types[cval->val_type]);
1775 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1776 cval->min, cval->max, cval->dBmin, cval->dBmax);
1777}
1778
1779static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1780 struct snd_info_buffer *buffer)
1781{
1782 struct snd_usb_audio *chip = entry->private_data;
1783 struct usb_mixer_interface *mixer;
1784 struct usb_mixer_elem_info *cval;
1785 int unitid;
1786
1787 list_for_each_entry(mixer, &chip->mixer_list, list) {
1788 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01001789 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1790 chip->usb_id, mixer->ctrlif,
1791 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001792 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1793 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1794 for (cval = mixer->id_elems[unitid]; cval;
1795 cval = cval->next_id_elem)
1796 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1797 }
1798 }
1799}
1800
David Howells7d12e782006-10-05 14:55:46 +01001801static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001802{
1803 struct usb_mixer_interface *mixer = urb->context;
1804
1805 if (urb->status == 0) {
1806 u8 *buf = urb->transfer_buffer;
1807 int i;
1808
1809 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1810 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1811 buf[0], buf[1]);
1812 /* ignore any notifications not from the control interface */
1813 if ((buf[0] & 0x0f) != 0)
1814 continue;
1815 if (!(buf[0] & 0x40))
1816 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001817 else
Daniel Mack7b1eda22010-03-11 21:13:22 +01001818 snd_usb_mixer_rc_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001819 }
1820 }
1821 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1822 urb->dev = mixer->chip->dev;
1823 usb_submit_urb(urb, GFP_ATOMIC);
1824 }
1825}
1826
1827/* create the handler for the optional status interrupt endpoint */
1828static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1829{
1830 struct usb_host_interface *hostif;
1831 struct usb_endpoint_descriptor *ep;
1832 void *transfer_buffer;
1833 int buffer_length;
1834 unsigned int epnum;
1835
1836 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1837 /* we need one interrupt input endpoint */
1838 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1839 return 0;
1840 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001841 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001842 return 0;
1843
Julia Lawall42a6e662008-12-29 11:23:02 +01001844 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001845 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1846 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1847 if (!transfer_buffer)
1848 return -ENOMEM;
1849 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1850 if (!mixer->urb) {
1851 kfree(transfer_buffer);
1852 return -ENOMEM;
1853 }
1854 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1855 usb_rcvintpipe(mixer->chip->dev, epnum),
1856 transfer_buffer, buffer_length,
1857 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1858 usb_submit_urb(mixer->urb, GFP_KERNEL);
1859 return 0;
1860}
1861
Takashi Iwai7a9b8062008-08-13 15:40:53 +02001862int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
1863 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001864{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001865 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001866 .dev_free = snd_usb_mixer_dev_free
1867 };
1868 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001869 struct snd_info_entry *entry;
Daniel Mack7b8a0432010-02-22 23:49:12 +01001870 struct usb_host_interface *host_iface;
1871 int err, protocol;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001872
1873 strcpy(chip->card->mixername, "USB Mixer");
1874
Takashi Iwai561b2202005-09-09 14:22:34 +02001875 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001876 if (!mixer)
1877 return -ENOMEM;
1878 mixer->chip = chip;
1879 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02001880 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01001881 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
1882 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001883 if (!mixer->id_elems) {
1884 kfree(mixer);
1885 return -ENOMEM;
1886 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001887
Daniel Mack7b8a0432010-02-22 23:49:12 +01001888 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
1889 protocol = host_iface->desc.bInterfaceProtocol;
1890
1891 /* FIXME! */
1892 if (protocol != UAC_VERSION_1) {
1893 snd_printk(KERN_WARNING "mixer interface protocol 0x%02x not yet supported\n",
1894 protocol);
1895 return 0;
1896 }
1897
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001898 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001899 (err = snd_usb_mixer_status_create(mixer)) < 0)
1900 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001901
Daniel Mack7b1eda22010-03-11 21:13:22 +01001902 snd_usb_mixer_apply_create_quirk(mixer);
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02001903
Clemens Ladisch84957a82005-04-29 16:23:13 +02001904 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001905 if (err < 0)
1906 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001907
1908 if (list_empty(&chip->mixer_list) &&
1909 !snd_card_proc_new(chip->card, "usbmixer", &entry))
1910 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
1911
Clemens Ladisch84957a82005-04-29 16:23:13 +02001912 list_add(&mixer->list, &chip->mixer_list);
1913 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001914
1915_error:
1916 snd_usb_mixer_free(mixer);
1917 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001918}
1919
1920void snd_usb_mixer_disconnect(struct list_head *p)
1921{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001922 struct usb_mixer_interface *mixer;
Daniel Mack7b1eda22010-03-11 21:13:22 +01001923
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001924 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001925 usb_kill_urb(mixer->urb);
1926 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001927}