blob: 43d53a362494eeca7e042f7deb2cbebbb9c39b03 [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 Mack3e1aebe2010-03-04 19:46:12 +010044#include "usbmixer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 */
48
49/* ignore error from controls - for debugging */
50/* #define IGNORE_CTL_ERROR */
51
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020052/*
53 * Sound Blaster remote control configuration
54 *
55 * format of remote control data:
56 * Extigy: xx 00
57 * Audigy 2 NX: 06 80 xx 00 00 00
58 * Live! 24-bit: 06 80 xx yy 22 83
59 */
60static const struct rc_config {
61 u32 usb_id;
62 u8 offset;
63 u8 length;
64 u8 packet_length;
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020065 u8 min_packet_length; /* minimum accepted length of the URB result */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020066 u8 mute_mixer_id;
67 u32 mute_code;
68} rc_configs[] = {
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020069 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
70 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
71 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
Andrea Borgia31959542009-01-07 22:58:50 +010072 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020073};
74
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010075#define MAX_ID_ELEMS 256
76
Clemens Ladisch84957a82005-04-29 16:23:13 +020077struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010078 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020079 unsigned int ctrlif;
80 struct list_head list;
81 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020082 struct urb *urb;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010083 /* array[MAX_ID_ELEMS], indexed by unit id */
84 struct usb_mixer_elem_info **id_elems;
Clemens Ladischb259b102005-04-29 16:29:28 +020085
86 /* Sound Blaster remote control stuff */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020087 const struct rc_config *rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +020088 u32 rc_code;
89 wait_queue_head_t rc_waitq;
90 struct urb *rc_urb;
91 struct usb_ctrlrequest *rc_setup_packet;
92 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020093
94 u8 audigy2nx_leds[3];
Clemens Ladisch468b8fd2009-07-13 11:39:29 +020095 u8 xonar_u1_status;
Clemens Ladisch84957a82005-04-29 16:23:13 +020096};
97
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct usb_audio_term {
100 int id;
101 int type;
102 int channels;
103 unsigned int chconfig;
104 int name;
105};
106
107struct usbmix_name_map;
108
Takashi Iwai86e07d32005-11-17 15:08:02 +0100109struct mixer_build {
110 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200111 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned char *buffer;
113 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +0100114 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100115 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200117 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Takashi Iwai641b4872009-01-15 17:05:24 +0100120#define MAX_CHANNELS 10 /* max logical channels */
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200123 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100124 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
125 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned int id;
127 unsigned int control; /* CS or ICN (high byte) */
128 unsigned int cmask; /* channel mask bitmap: 0 = master */
129 int channels;
130 int val_type;
131 int min, max, res;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100132 int dBmin, dBmax;
Takashi Iwai641b4872009-01-15 17:05:24 +0100133 int cached;
134 int cache_val[MAX_CHANNELS];
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200135 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138
139enum {
140 USB_FEATURE_NONE = 0,
141 USB_FEATURE_MUTE = 1,
142 USB_FEATURE_VOLUME,
143 USB_FEATURE_BASS,
144 USB_FEATURE_MID,
145 USB_FEATURE_TREBLE,
146 USB_FEATURE_GEQ,
147 USB_FEATURE_AGC,
148 USB_FEATURE_DELAY,
149 USB_FEATURE_BASSBOOST,
150 USB_FEATURE_LOUDNESS
151};
152
153enum {
154 USB_MIXER_BOOLEAN,
155 USB_MIXER_INV_BOOLEAN,
156 USB_MIXER_S8,
157 USB_MIXER_U8,
158 USB_MIXER_S16,
159 USB_MIXER_U16,
160};
161
162enum {
163 USB_PROC_UPDOWN = 1,
164 USB_PROC_UPDOWN_SWITCH = 1,
165 USB_PROC_UPDOWN_MODE_SEL = 2,
166
167 USB_PROC_PROLOGIC = 2,
168 USB_PROC_PROLOGIC_SWITCH = 1,
169 USB_PROC_PROLOGIC_MODE_SEL = 2,
170
171 USB_PROC_3DENH = 3,
172 USB_PROC_3DENH_SWITCH = 1,
173 USB_PROC_3DENH_SPACE = 2,
174
175 USB_PROC_REVERB = 4,
176 USB_PROC_REVERB_SWITCH = 1,
177 USB_PROC_REVERB_LEVEL = 2,
178 USB_PROC_REVERB_TIME = 3,
179 USB_PROC_REVERB_DELAY = 4,
180
181 USB_PROC_CHORUS = 5,
182 USB_PROC_CHORUS_SWITCH = 1,
183 USB_PROC_CHORUS_LEVEL = 2,
184 USB_PROC_CHORUS_RATE = 3,
185 USB_PROC_CHORUS_DEPTH = 4,
186
187 USB_PROC_DCR = 6,
188 USB_PROC_DCR_SWITCH = 1,
189 USB_PROC_DCR_RATIO = 2,
190 USB_PROC_DCR_MAX_AMP = 3,
191 USB_PROC_DCR_THRESHOLD = 4,
192 USB_PROC_DCR_ATTACK = 5,
193 USB_PROC_DCR_RELEASE = 6,
194};
195
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -0800196/*E-mu 0202(0404) eXtension Unit(XU) control*/
197enum {
198 USB_XU_CLOCK_RATE = 0xe301,
199 USB_XU_CLOCK_SOURCE = 0xe302,
200 USB_XU_DIGITAL_IO_STATUS = 0xe303,
201 USB_XU_DEVICE_OPTIONS = 0xe304,
202 USB_XU_DIRECT_MONITORING = 0xe305,
203 USB_XU_METERING = 0xe306
204};
205enum {
206 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
207 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
208 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
209 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
210};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/*
213 * manual mapping of mixer names
214 * if the mixer topology is too complicated and the parsed names are
215 * ambiguous, add the entries in usbmixer_maps.c.
216 */
217#include "usbmixer_maps.c"
218
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100219static const struct usbmix_name_map *
220find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100222 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100224 if (!p)
225 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100228 if (p->id == unitid &&
229 (!control || !p->control || control == p->control))
230 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100232 return NULL;
233}
234
235/* get the mapped name if the unit matches */
236static int
237check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
238{
239 if (!p || !p->name)
240 return 0;
241
242 buflen--;
243 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100247static inline int
248check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100250 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100252 return 1;
253}
254
255/* dB mapping */
256static inline void check_mapped_dB(const struct usbmix_name_map *p,
257 struct usb_mixer_elem_info *cval)
258{
259 if (p && p->dB) {
260 cval->dBmin = p->dB->min;
261 cval->dBmax = p->dB->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200265/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100266static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200267 int index, char *buf, int buflen)
268{
269 const struct usbmix_selector_map *p;
270
271 if (! state->selector_map)
272 return 0;
273 for (p = state->selector_map; p->id; p++) {
274 if (p->id == unitid && index < p->count)
275 return strlcpy(buf, p->names[index], buflen);
276 }
277 return 0;
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * find an audio control unit with the given unit id
282 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100283static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 unsigned char *p;
286
287 p = NULL;
288 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
289 USB_DT_CS_INTERFACE)) != NULL) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100290 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 -0700291 return p;
292 }
293 return NULL;
294}
295
296
297/*
298 * copy a string with the given id
299 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100300static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
303 buf[len] = 0;
304 return len;
305}
306
307/*
308 * convert from the byte/word on usb descriptor to the zero-based integer
309 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100310static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 switch (cval->val_type) {
313 case USB_MIXER_BOOLEAN:
314 return !!val;
315 case USB_MIXER_INV_BOOLEAN:
316 return !val;
317 case USB_MIXER_U8:
318 val &= 0xff;
319 break;
320 case USB_MIXER_S8:
321 val &= 0xff;
322 if (val >= 0x80)
323 val -= 0x100;
324 break;
325 case USB_MIXER_U16:
326 val &= 0xffff;
327 break;
328 case USB_MIXER_S16:
329 val &= 0xffff;
330 if (val >= 0x8000)
331 val -= 0x10000;
332 break;
333 }
334 return val;
335}
336
337/*
338 * convert from the zero-based int to the byte/word for usb descriptor
339 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100340static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 switch (cval->val_type) {
343 case USB_MIXER_BOOLEAN:
344 return !!val;
345 case USB_MIXER_INV_BOOLEAN:
346 return !val;
347 case USB_MIXER_S8:
348 case USB_MIXER_U8:
349 return val & 0xff;
350 case USB_MIXER_S16:
351 case USB_MIXER_U16:
352 return val & 0xffff;
353 }
354 return 0; /* not reached */
355}
356
Takashi Iwai86e07d32005-11-17 15:08:02 +0100357static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 if (! cval->res)
360 cval->res = 1;
361 if (val < cval->min)
362 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200363 else if (val >= cval->max)
364 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 else
366 return (val - cval->min) / cval->res;
367}
368
Takashi Iwai86e07d32005-11-17 15:08:02 +0100369static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 if (val < 0)
372 return cval->min;
373 if (! cval->res)
374 cval->res = 1;
375 val *= cval->res;
376 val += cval->min;
377 if (val > cval->max)
378 return cval->max;
379 return val;
380}
381
382
383/*
384 * retrieve a mixer value
385 */
386
Takashi Iwai86e07d32005-11-17 15:08:02 +0100387static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 unsigned char buf[2];
390 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
391 int timeout = 10;
392
393 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200394 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
395 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 request,
397 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200398 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200399 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
401 return 0;
402 }
403 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200404 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
405 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -EINVAL;
407}
408
Takashi Iwai86e07d32005-11-17 15:08:02 +0100409static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100411 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100415static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
416 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100418 return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Takashi Iwai641b4872009-01-15 17:05:24 +0100421static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
422 int channel, int index, int *value)
423{
424 int err;
425
426 if (cval->cached & (1 << channel)) {
427 *value = cval->cache_val[index];
428 return 0;
429 }
430 err = get_cur_mix_raw(cval, channel, value);
431 if (err < 0) {
432 if (!cval->mixer->ignore_ctl_error)
433 snd_printd(KERN_ERR "cannot get current value for "
434 "control %d ch %d: err = %d\n",
435 cval->control, channel, err);
436 return err;
437 }
438 cval->cached |= 1 << channel;
439 cval->cache_val[index] = *value;
440 return 0;
441}
442
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * set a mixer value
446 */
447
Takashi Iwai86e07d32005-11-17 15:08:02 +0100448static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 unsigned char buf[2];
451 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
452 int timeout = 10;
453
454 value_set = convert_bytes_value(cval, value_set);
455 buf[0] = value_set & 0xff;
456 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530457 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200458 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
459 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 request,
461 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200462 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 buf, val_len, 100) >= 0)
464 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200465 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
466 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
468}
469
Takashi Iwai86e07d32005-11-17 15:08:02 +0100470static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100472 return set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Takashi Iwai641b4872009-01-15 17:05:24 +0100475static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
476 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Takashi Iwai641b4872009-01-15 17:05:24 +0100478 int err;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100479 err = set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100480 value);
481 if (err < 0)
482 return err;
483 cval->cached |= 1 << channel;
484 cval->cache_val[index] = value;
485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200488/*
489 * TLV callback for mixer volume controls
490 */
491static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
492 unsigned int size, unsigned int __user *_tlv)
493{
494 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200495 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200496
497 if (size < sizeof(scale))
498 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100499 scale[2] = cval->dBmin;
500 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200501 if (copy_to_user(_tlv, scale, sizeof(scale)))
502 return -EFAULT;
503 return 0;
504}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506/*
507 * parser routines begin here...
508 */
509
Takashi Iwai86e07d32005-11-17 15:08:02 +0100510static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512
513/*
514 * check if the input/output channel routing is enabled on the given bitmap.
515 * used for mixer unit parser
516 */
517static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
518{
519 int idx = ich * num_outs + och;
520 return bmap[idx >> 3] & (0x80 >> (idx & 7));
521}
522
523
524/*
525 * add an alsa control element
526 * search and increment the index until an empty slot is found.
527 *
528 * if failed, give up and free the control instance.
529 */
530
Takashi Iwai86e07d32005-11-17 15:08:02 +0100531static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100533 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200535
536 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200538 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200542 cval->elem_id = &kctl->id;
543 cval->next_id_elem = state->mixer->id_elems[cval->id];
544 state->mixer->id_elems[cval->id] = cval;
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548
549/*
550 * get a terminal name string
551 */
552
553static struct iterm_name_combo {
554 int type;
555 char *name;
556} iterm_names[] = {
557 { 0x0300, "Output" },
558 { 0x0301, "Speaker" },
559 { 0x0302, "Headphone" },
560 { 0x0303, "HMD Audio" },
561 { 0x0304, "Desktop Speaker" },
562 { 0x0305, "Room Speaker" },
563 { 0x0306, "Com Speaker" },
564 { 0x0307, "LFE" },
565 { 0x0600, "External In" },
566 { 0x0601, "Analog In" },
567 { 0x0602, "Digital In" },
568 { 0x0603, "Line" },
569 { 0x0604, "Legacy In" },
570 { 0x0605, "IEC958 In" },
571 { 0x0606, "1394 DA Stream" },
572 { 0x0607, "1394 DV Stream" },
573 { 0x0700, "Embedded" },
574 { 0x0701, "Noise Source" },
575 { 0x0702, "Equalization Noise" },
576 { 0x0703, "CD" },
577 { 0x0704, "DAT" },
578 { 0x0705, "DCC" },
579 { 0x0706, "MiniDisk" },
580 { 0x0707, "Analog Tape" },
581 { 0x0708, "Phonograph" },
582 { 0x0709, "VCR Audio" },
583 { 0x070a, "Video Disk Audio" },
584 { 0x070b, "DVD Audio" },
585 { 0x070c, "TV Tuner Audio" },
586 { 0x070d, "Satellite Rec Audio" },
587 { 0x070e, "Cable Tuner Audio" },
588 { 0x070f, "DSS Audio" },
589 { 0x0710, "Radio Receiver" },
590 { 0x0711, "Radio Transmitter" },
591 { 0x0712, "Multi-Track Recorder" },
592 { 0x0713, "Synthesizer" },
593 { 0 },
594};
595
Takashi Iwai86e07d32005-11-17 15:08:02 +0100596static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned char *name, int maxlen, int term_only)
598{
599 struct iterm_name_combo *names;
600
601 if (iterm->name)
602 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
603
604 /* virtual type - not a real terminal */
605 if (iterm->type >> 16) {
606 if (term_only)
607 return 0;
608 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100609 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 strcpy(name, "Selector"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100611 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 strcpy(name, "Process Unit"); return 12;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100613 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 strcpy(name, "Ext Unit"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100615 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 strcpy(name, "Mixer"); return 5;
617 default:
618 return sprintf(name, "Unit %d", iterm->id);
619 }
620 }
621
622 switch (iterm->type & 0xff00) {
623 case 0x0100:
624 strcpy(name, "PCM"); return 3;
625 case 0x0200:
626 strcpy(name, "Mic"); return 3;
627 case 0x0400:
628 strcpy(name, "Headset"); return 7;
629 case 0x0500:
630 strcpy(name, "Phone"); return 5;
631 }
632
633 for (names = iterm_names; names->type; names++)
634 if (names->type == iterm->type) {
635 strcpy(name, names->name);
636 return strlen(names->name);
637 }
638 return 0;
639}
640
641
642/*
643 * parse the source unit recursively until it reaches to a terminal
644 * or a branched unit.
645 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100646static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 unsigned char *p1;
649
650 memset(term, 0, sizeof(*term));
651 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
652 term->id = id;
653 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100654 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 term->type = combine_word(p1 + 4);
656 term->channels = p1[7];
657 term->chconfig = combine_word(p1 + 8);
658 term->name = p1[11];
659 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100660 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 id = p1[4];
662 break; /* continue to parse */
Daniel Mackde48c7b2010-02-22 23:49:13 +0100663 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 term->type = p1[2] << 16; /* virtual type */
665 term->channels = p1[5 + p1[4]];
666 term->chconfig = combine_word(p1 + 6 + p1[4]);
667 term->name = p1[p1[0] - 1];
668 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100669 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* call recursively to retrieve the channel info */
671 if (check_input_term(state, p1[5], term) < 0)
672 return -ENODEV;
673 term->type = p1[2] << 16; /* virtual type */
674 term->id = id;
675 term->name = p1[9 + p1[0] - 1];
676 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100677 case UAC_PROCESSING_UNIT_V1:
678 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (p1[6] == 1) {
680 id = p1[7];
681 break; /* continue to parse */
682 }
683 term->type = p1[2] << 16; /* virtual type */
684 term->channels = p1[7 + p1[6]];
685 term->chconfig = combine_word(p1 + 8 + p1[6]);
686 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
687 return 0;
688 default:
689 return -ENODEV;
690 }
691 }
692 return -ENODEV;
693}
694
695
696/*
697 * Feature Unit
698 */
699
700/* feature unit control information */
701struct usb_feature_control_info {
702 const char *name;
703 unsigned int type; /* control type (mute, volume, etc.) */
704};
705
706static struct usb_feature_control_info audio_feature_info[] = {
707 { "Mute", USB_MIXER_INV_BOOLEAN },
708 { "Volume", USB_MIXER_S16 },
709 { "Tone Control - Bass", USB_MIXER_S8 },
710 { "Tone Control - Mid", USB_MIXER_S8 },
711 { "Tone Control - Treble", USB_MIXER_S8 },
712 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
713 { "Auto Gain Control", USB_MIXER_BOOLEAN },
714 { "Delay Control", USB_MIXER_U16 },
715 { "Bass Boost", USB_MIXER_BOOLEAN },
716 { "Loudness", USB_MIXER_BOOLEAN },
717};
718
719
720/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100721static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jesper Juhl4d572772005-05-30 17:30:32 +0200723 kfree(kctl->private_data);
724 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727
728/*
729 * interface to ALSA control for feature/mixer units
730 */
731
732/*
733 * retrieve the minimum and maximum values for the specified control
734 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100735static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 /* for failsafe */
738 cval->min = default_min;
739 cval->max = cval->min + 1;
740 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100741 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (cval->val_type == USB_MIXER_BOOLEAN ||
744 cval->val_type == USB_MIXER_INV_BOOLEAN) {
745 cval->initialized = 1;
746 } else {
747 int minchn = 0;
748 if (cval->cmask) {
749 int i;
750 for (i = 0; i < MAX_CHANNELS; i++)
751 if (cval->cmask & (1 << i)) {
752 minchn = i + 1;
753 break;
754 }
755 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100756 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
757 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200758 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
759 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EINVAL;
761 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100762 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 cval->res = 1;
764 } else {
765 int last_valid_res = cval->res;
766
767 while (cval->res > 1) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100768 if (set_ctl_value(cval, UAC_SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 cval->res /= 2;
771 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100772 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 cval->res = last_valid_res;
774 }
775 if (cval->res == 0)
776 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200777
778 /* Additional checks for the proper resolution
779 *
780 * Some devices report smaller resolutions than actually
781 * reacting. They don't return errors but simply clip
782 * to the lower aligned value.
783 */
784 if (cval->min + cval->res < cval->max) {
785 int last_valid_res = cval->res;
786 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100787 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200788 for (;;) {
789 test = saved;
790 if (test < cval->max)
791 test += cval->res;
792 else
793 test -= cval->res;
794 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100795 set_cur_mix_value(cval, minchn, 0, test) ||
796 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200797 cval->res = last_valid_res;
798 break;
799 }
800 if (test == check)
801 break;
802 cval->res *= 2;
803 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100804 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200805 }
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 cval->initialized = 1;
808 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100809
810 /* USB descriptions contain the dB scale in 1/256 dB unit
811 * while ALSA TLV contains in 1/100 dB unit
812 */
813 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
814 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
815 if (cval->dBmin > cval->dBmax) {
816 /* something is wrong; assume it's either from/to 0dB */
817 if (cval->dBmin < 0)
818 cval->dBmax = 0;
819 else if (cval->dBmin > 0)
820 cval->dBmin = 0;
821 if (cval->dBmin > cval->dBmax) {
822 /* totally crap, return an error */
823 return -EINVAL;
824 }
825 }
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828}
829
830
831/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100832static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100834 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (cval->val_type == USB_MIXER_BOOLEAN ||
837 cval->val_type == USB_MIXER_INV_BOOLEAN)
838 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
839 else
840 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
841 uinfo->count = cval->channels;
842 if (cval->val_type == USB_MIXER_BOOLEAN ||
843 cval->val_type == USB_MIXER_INV_BOOLEAN) {
844 uinfo->value.integer.min = 0;
845 uinfo->value.integer.max = 1;
846 } else {
847 if (! cval->initialized)
848 get_min_max(cval, 0);
849 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200850 uinfo->value.integer.max =
851 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 return 0;
854}
855
856/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100857static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100859 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int c, cnt, val, err;
861
Takashi Iwai641b4872009-01-15 17:05:24 +0100862 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (cval->cmask) {
864 cnt = 0;
865 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100866 if (!(cval->cmask & (1 << c)))
867 continue;
868 err = get_cur_mix_value(cval, c + 1, cnt, &val);
869 if (err < 0)
870 return cval->mixer->ignore_ctl_error ? 0 : err;
871 val = get_relative_value(cval, val);
872 ucontrol->value.integer.value[cnt] = val;
873 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 } else {
877 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100878 err = get_cur_mix_value(cval, 0, 0, &val);
879 if (err < 0)
880 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 val = get_relative_value(cval, val);
882 ucontrol->value.integer.value[0] = val;
883 }
884 return 0;
885}
886
887/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100888static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100890 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int c, cnt, val, oval, err;
892 int changed = 0;
893
894 if (cval->cmask) {
895 cnt = 0;
896 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100897 if (!(cval->cmask & (1 << c)))
898 continue;
899 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
900 if (err < 0)
901 return cval->mixer->ignore_ctl_error ? 0 : err;
902 val = ucontrol->value.integer.value[cnt];
903 val = get_abs_value(cval, val);
904 if (oval != val) {
905 set_cur_mix_value(cval, c + 1, cnt, val);
906 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100908 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 } else {
911 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100912 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100914 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 val = ucontrol->value.integer.value[0];
916 val = get_abs_value(cval, val);
917 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100918 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 changed = 1;
920 }
921 }
922 return changed;
923}
924
Takashi Iwai86e07d32005-11-17 15:08:02 +0100925static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
927 .name = "", /* will be filled later manually */
928 .info = mixer_ctl_feature_info,
929 .get = mixer_ctl_feature_get,
930 .put = mixer_ctl_feature_put,
931};
932
933
934/*
935 * build a feature control
936 */
937
Takashi Iwai08d1e632009-10-02 14:06:08 +0200938static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
939{
940 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
941}
942
Takashi Iwai86e07d32005-11-17 15:08:02 +0100943static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100945 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 unsigned int len = 0;
948 int mapped_name = 0;
949 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100950 struct snd_kcontrol *kctl;
951 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100952 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 control++; /* change from zero-based to 1-based value */
955
956 if (control == USB_FEATURE_GEQ) {
957 /* FIXME: not supported yet */
958 return;
959 }
960
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100961 map = find_map(state, unitid, control);
962 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return;
964
Takashi Iwai561b2202005-09-09 14:22:34 +0200965 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (! cval) {
967 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
968 return;
969 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200970 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 cval->id = unitid;
972 cval->control = control;
973 cval->cmask = ctl_mask;
974 cval->val_type = audio_feature_info[control-1].type;
975 if (ctl_mask == 0)
976 cval->channels = 1; /* master channel */
977 else {
978 int i, c = 0;
979 for (i = 0; i < 16; i++)
980 if (ctl_mask & (1 << i))
981 c++;
982 cval->channels = c;
983 }
984
985 /* get min/max values */
986 get_min_max(cval, 0);
987
988 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
989 if (! kctl) {
990 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
991 kfree(cval);
992 return;
993 }
994 kctl->private_free = usb_mixer_elem_free;
995
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100996 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 mapped_name = len != 0;
998 if (! len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100999 len = snd_usb_copy_string_desc(state, nameid,
1000 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 switch (control) {
1003 case USB_FEATURE_MUTE:
1004 case USB_FEATURE_VOLUME:
1005 /* determine the control name. the rule is:
1006 * - if a name id is given in descriptor, use it.
1007 * - if the connected input can be determined, then use the name
1008 * of terminal type.
1009 * - if the connected output can be determined, use it.
1010 * - otherwise, anonymous name.
1011 */
1012 if (! len) {
1013 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1014 if (! len)
1015 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1016 if (! len)
1017 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1018 "Feature %d", unitid);
1019 }
1020 /* determine the stream direction:
1021 * if the connected output is USB stream, then it's likely a
1022 * capture stream. otherwise it should be playback (hopefully :)
1023 */
1024 if (! mapped_name && ! (state->oterm.type >> 16)) {
1025 if ((state->oterm.type & 0xff00) == 0x0100) {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001026 len = append_ctl_name(kctl, " Capture");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 } else {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001028 len = append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001031 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1032 " Switch" : " Volume");
Takashi Iwai7bc5ba72006-07-14 15:18:19 +02001033 if (control == USB_FEATURE_VOLUME) {
1034 kctl->tlv.c = mixer_vol_tlv;
1035 kctl->vd[0].access |=
1036 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1037 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001038 check_mapped_dB(map, cval);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +02001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
1041
1042 default:
1043 if (! len)
1044 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1045 sizeof(kctl->id.name));
1046 break;
1047 }
1048
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001049 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001050 switch (state->chip->usb_id) {
1051 case USB_ID(0x0471, 0x0101):
1052 case USB_ID(0x0471, 0x0104):
1053 case USB_ID(0x0471, 0x0105):
1054 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001055 /* quirk for UDA1321/N101.
1056 * note that detection between firmware 2.1.1.7 (N101)
1057 * and later 2.1.1.21 is not very clear from datasheets.
1058 * I hope that the min value is -15360 for newer firmware --jk
1059 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001060 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1061 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001062 snd_printk(KERN_INFO
1063 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001064 cval->max = -256;
1065 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001066 break;
1067
1068 case USB_ID(0x046d, 0x09a4):
1069 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1070 snd_printk(KERN_INFO
1071 "set volume quirk for QuickCam E3500\n");
1072 cval->min = 6080;
1073 cval->max = 8768;
1074 cval->res = 192;
1075 }
1076 break;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
1080 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1081 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001082 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085
1086
1087/*
1088 * parse a feature unit
1089 *
1090 * most of controlls are defined here.
1091 */
Daniel Mack28e1b772010-02-22 23:49:09 +01001092static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001095 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 unsigned int master_bits, first_ch_bits;
1097 int err, csize;
Daniel Mack28e1b772010-02-22 23:49:09 +01001098 struct uac_feature_unit_descriptor *ftr = _ftr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Daniel Mack28e1b772010-02-22 23:49:09 +01001100 if (ftr->bLength < 7 || ! (csize = ftr->bControlSize) || ftr->bLength < 7 + csize) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001101 snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -EINVAL;
1103 }
1104
1105 /* parse the source unit */
Daniel Mack28e1b772010-02-22 23:49:09 +01001106 if ((err = parse_audio_unit(state, ftr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return err;
1108
1109 /* determine the input source type and name */
Daniel Mack28e1b772010-02-22 23:49:09 +01001110 if (check_input_term(state, ftr->bSourceID, &iterm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EINVAL;
1112
Daniel Mack28e1b772010-02-22 23:49:09 +01001113 channels = (ftr->bLength - 7) / csize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Daniel Mack28e1b772010-02-22 23:49:09 +01001115 master_bits = snd_usb_combine_bytes(ftr->controls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001116 /* master configuration quirks */
1117 switch (state->chip->usb_id) {
1118 case USB_ID(0x08bb, 0x2702):
1119 snd_printk(KERN_INFO
1120 "usbmixer: master volume quirk for PCM2702 chip\n");
1121 /* disable non-functional volume control */
1122 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1123 break;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (channels > 0)
Daniel Mack28e1b772010-02-22 23:49:09 +01001126 first_ch_bits = snd_usb_combine_bytes(ftr->controls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 else
1128 first_ch_bits = 0;
1129 /* check all control types */
1130 for (i = 0; i < 10; i++) {
1131 unsigned int ch_bits = 0;
1132 for (j = 0; j < channels; j++) {
Daniel Mack28e1b772010-02-22 23:49:09 +01001133 unsigned int mask = snd_usb_combine_bytes(ftr->controls + csize * (j+1), csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (mask & (1 << i))
1135 ch_bits |= (1 << j);
1136 }
1137 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
Daniel Mack28e1b772010-02-22 23:49:09 +01001138 build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (master_bits & (1 << i))
Daniel Mack28e1b772010-02-22 23:49:09 +01001140 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
1143 return 0;
1144}
1145
1146
1147/*
1148 * Mixer Unit
1149 */
1150
1151/*
1152 * build a mixer unit control
1153 *
1154 * the callbacks are identical with feature unit.
1155 * input channel number (zero based) is given in control field instead.
1156 */
1157
Takashi Iwai86e07d32005-11-17 15:08:02 +01001158static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001160 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001162 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 unsigned int input_pins = desc[4];
1164 unsigned int num_outs = desc[5 + input_pins];
1165 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001166 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001167 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001169 map = find_map(state, unitid, 0);
1170 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return;
1172
Takashi Iwai561b2202005-09-09 14:22:34 +02001173 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (! cval)
1175 return;
1176
Clemens Ladisch84957a82005-04-29 16:23:13 +02001177 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 cval->id = unitid;
1179 cval->control = in_ch + 1; /* based on 1 */
1180 cval->val_type = USB_MIXER_S16;
1181 for (i = 0; i < num_outs; i++) {
1182 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1183 cval->cmask |= (1 << i);
1184 cval->channels++;
1185 }
1186 }
1187
1188 /* get min/max values */
1189 get_min_max(cval, 0);
1190
1191 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1192 if (! kctl) {
1193 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1194 kfree(cval);
1195 return;
1196 }
1197 kctl->private_free = usb_mixer_elem_free;
1198
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001199 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (! len)
1201 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1202 if (! len)
1203 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001204 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1207 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001208 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211
1212/*
1213 * parse a mixer unit
1214 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001215static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001217 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int input_pins, num_ins, num_outs;
1219 int pin, ich, err;
1220
1221 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1222 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1223 return -EINVAL;
1224 }
1225 /* no bmControls field (e.g. Maya44) -> ignore */
1226 if (desc[0] <= 10 + input_pins) {
1227 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1228 return 0;
1229 }
1230
1231 num_ins = 0;
1232 ich = 0;
1233 for (pin = 0; pin < input_pins; pin++) {
1234 err = parse_audio_unit(state, desc[5 + pin]);
1235 if (err < 0)
1236 return err;
1237 err = check_input_term(state, desc[5 + pin], &iterm);
1238 if (err < 0)
1239 return err;
1240 num_ins += iterm.channels;
1241 for (; ich < num_ins; ++ich) {
1242 int och, ich_has_controls = 0;
1243
1244 for (och = 0; och < num_outs; ++och) {
1245 if (check_matrix_bitmap(desc + 9 + input_pins,
1246 ich, och, num_outs)) {
1247 ich_has_controls = 1;
1248 break;
1249 }
1250 }
1251 if (ich_has_controls)
1252 build_mixer_unit_ctl(state, desc, pin, ich,
1253 unitid, &iterm);
1254 }
1255 }
1256 return 0;
1257}
1258
1259
1260/*
1261 * Processing Unit / Extension Unit
1262 */
1263
1264/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001265static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001267 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int err, val;
1269
1270 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001271 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 ucontrol->value.integer.value[0] = cval->min;
1273 return 0;
1274 }
1275 if (err < 0)
1276 return err;
1277 val = get_relative_value(cval, val);
1278 ucontrol->value.integer.value[0] = val;
1279 return 0;
1280}
1281
1282/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001283static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001285 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int val, oval, err;
1287
1288 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1289 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001290 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292 return err;
1293 }
1294 val = ucontrol->value.integer.value[0];
1295 val = get_abs_value(cval, val);
1296 if (val != oval) {
1297 set_cur_ctl_value(cval, cval->control << 8, val);
1298 return 1;
1299 }
1300 return 0;
1301}
1302
1303/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001304static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1306 .name = "", /* will be filled later */
1307 .info = mixer_ctl_feature_info,
1308 .get = mixer_ctl_procunit_get,
1309 .put = mixer_ctl_procunit_put,
1310};
1311
1312
1313/*
1314 * predefined data for processing units
1315 */
1316struct procunit_value_info {
1317 int control;
1318 char *suffix;
1319 int val_type;
1320 int min_value;
1321};
1322
1323struct procunit_info {
1324 int type;
1325 char *name;
1326 struct procunit_value_info *values;
1327};
1328
1329static struct procunit_value_info updown_proc_info[] = {
1330 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1331 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1332 { 0 }
1333};
1334static struct procunit_value_info prologic_proc_info[] = {
1335 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1336 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1337 { 0 }
1338};
1339static struct procunit_value_info threed_enh_proc_info[] = {
1340 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1341 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1342 { 0 }
1343};
1344static struct procunit_value_info reverb_proc_info[] = {
1345 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1346 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1347 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1348 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1349 { 0 }
1350};
1351static struct procunit_value_info chorus_proc_info[] = {
1352 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1353 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1354 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1355 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1356 { 0 }
1357};
1358static struct procunit_value_info dcr_proc_info[] = {
1359 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1360 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1361 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1362 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1363 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1364 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1365 { 0 }
1366};
1367
1368static struct procunit_info procunits[] = {
1369 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1370 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1371 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1372 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1373 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1374 { USB_PROC_DCR, "DCR", dcr_proc_info },
1375 { 0 },
1376};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001377/*
1378 * predefined data for extension units
1379 */
1380static struct procunit_value_info clock_rate_xu_info[] = {
1381 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1382 { 0 }
1383};
1384static struct procunit_value_info clock_source_xu_info[] = {
1385 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1386 { 0 }
1387};
1388static struct procunit_value_info spdif_format_xu_info[] = {
1389 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1390 { 0 }
1391};
1392static struct procunit_value_info soft_limit_xu_info[] = {
1393 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1394 { 0 }
1395};
1396static struct procunit_info extunits[] = {
1397 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1398 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1399 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1400 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1401 { 0 }
1402};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
1404 * build a processing/extension unit
1405 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001406static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001409 struct usb_mixer_elem_info *cval;
1410 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int i, err, nameid, type, len;
1412 struct procunit_info *info;
1413 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001414 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 static struct procunit_value_info default_value_info[] = {
1416 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1417 { 0 }
1418 };
1419 static struct procunit_info default_info = {
1420 0, NULL, default_value_info
1421 };
1422
1423 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1424 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1425 return -EINVAL;
1426 }
1427
1428 for (i = 0; i < num_ins; i++) {
1429 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1430 return err;
1431 }
1432
1433 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 for (info = list; info && info->type; info++)
1435 if (info->type == type)
1436 break;
1437 if (! info || ! info->type)
1438 info = &default_info;
1439
1440 for (valinfo = info->values; valinfo->control; valinfo++) {
1441 /* FIXME: bitmap might be longer than 8bit */
1442 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1443 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001444 map = find_map(state, unitid, valinfo->control);
1445 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001447 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (! cval) {
1449 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1450 return -ENOMEM;
1451 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001452 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 cval->id = unitid;
1454 cval->control = valinfo->control;
1455 cval->val_type = valinfo->val_type;
1456 cval->channels = 1;
1457
1458 /* get min/max values */
1459 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1460 /* FIXME: hard-coded */
1461 cval->min = 1;
1462 cval->max = dsc[15];
1463 cval->res = 1;
1464 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001465 } else {
1466 if (type == USB_XU_CLOCK_RATE) {
1467 /* E-Mu USB 0404/0202/TrackerPre
1468 * samplerate control quirk
1469 */
1470 cval->min = 0;
1471 cval->max = 5;
1472 cval->res = 1;
1473 cval->initialized = 1;
1474 } else
1475 get_min_max(cval, valinfo->min_value);
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1479 if (! kctl) {
1480 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1481 kfree(cval);
1482 return -ENOMEM;
1483 }
1484 kctl->private_free = usb_mixer_elem_free;
1485
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001486 if (check_mapped_name(map, kctl->id.name,
1487 sizeof(kctl->id.name)))
1488 /* nothing */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 else if (info->name)
1490 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1491 else {
1492 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1493 len = 0;
1494 if (nameid)
1495 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1496 if (! len)
1497 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1498 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001499 append_ctl_name(kctl, " ");
1500 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1503 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001504 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return err;
1506 }
1507 return 0;
1508}
1509
1510
Takashi Iwai86e07d32005-11-17 15:08:02 +01001511static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1514}
1515
Takashi Iwai86e07d32005-11-17 15:08:02 +01001516static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001518 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521
1522/*
1523 * Selector Unit
1524 */
1525
1526/* info callback for selector unit
1527 * use an enumerator type for routing
1528 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001529static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001531 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 char **itemlist = (char **)kcontrol->private_value;
1533
Takashi Iwai5e246b82008-08-08 17:12:47 +02001534 if (snd_BUG_ON(!itemlist))
1535 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1537 uinfo->count = 1;
1538 uinfo->value.enumerated.items = cval->max;
1539 if ((int)uinfo->value.enumerated.item >= cval->max)
1540 uinfo->value.enumerated.item = cval->max - 1;
1541 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1542 return 0;
1543}
1544
1545/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001546static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001548 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int val, err;
1550
1551 err = get_cur_ctl_value(cval, 0, &val);
1552 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001553 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 ucontrol->value.enumerated.item[0] = 0;
1555 return 0;
1556 }
1557 return err;
1558 }
1559 val = get_relative_value(cval, val);
1560 ucontrol->value.enumerated.item[0] = val;
1561 return 0;
1562}
1563
1564/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001565static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001567 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int val, oval, err;
1569
1570 err = get_cur_ctl_value(cval, 0, &oval);
1571 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001572 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return 0;
1574 return err;
1575 }
1576 val = ucontrol->value.enumerated.item[0];
1577 val = get_abs_value(cval, val);
1578 if (val != oval) {
1579 set_cur_ctl_value(cval, 0, val);
1580 return 1;
1581 }
1582 return 0;
1583}
1584
1585/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001586static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1588 .name = "", /* will be filled later */
1589 .info = mixer_ctl_selector_info,
1590 .get = mixer_ctl_selector_get,
1591 .put = mixer_ctl_selector_put,
1592};
1593
1594
1595/* private free callback.
1596 * free both private_data and private_value
1597 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001598static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 int i, num_ins = 0;
1601
1602 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001603 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 num_ins = cval->max;
1605 kfree(cval);
1606 kctl->private_data = NULL;
1607 }
1608 if (kctl->private_value) {
1609 char **itemlist = (char **)kctl->private_value;
1610 for (i = 0; i < num_ins; i++)
1611 kfree(itemlist[i]);
1612 kfree(itemlist);
1613 kctl->private_value = 0;
1614 }
1615}
1616
1617/*
1618 * parse a selector unit
1619 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001620static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 unsigned int num_ins = desc[4];
1623 unsigned int i, nameid, len;
1624 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001625 struct usb_mixer_elem_info *cval;
1626 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001627 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 char **namelist;
1629
Russ Cox38977e92007-08-06 15:37:58 +02001630 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1632 return -EINVAL;
1633 }
1634
1635 for (i = 0; i < num_ins; i++) {
1636 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1637 return err;
1638 }
1639
1640 if (num_ins == 1) /* only one ? nonsense! */
1641 return 0;
1642
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001643 map = find_map(state, unitid, 0);
1644 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return 0;
1646
Takashi Iwai561b2202005-09-09 14:22:34 +02001647 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (! cval) {
1649 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1650 return -ENOMEM;
1651 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001652 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 cval->id = unitid;
1654 cval->val_type = USB_MIXER_U8;
1655 cval->channels = 1;
1656 cval->min = 1;
1657 cval->max = num_ins;
1658 cval->res = 1;
1659 cval->initialized = 1;
1660
1661 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1662 if (! namelist) {
1663 snd_printk(KERN_ERR "cannot malloc\n");
1664 kfree(cval);
1665 return -ENOMEM;
1666 }
1667#define MAX_ITEM_NAME_LEN 64
1668 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001669 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 len = 0;
1671 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1672 if (! namelist[i]) {
1673 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001674 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 kfree(namelist[i]);
1676 kfree(namelist);
1677 kfree(cval);
1678 return -ENOMEM;
1679 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001680 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1681 MAX_ITEM_NAME_LEN);
1682 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1684 if (! len)
1685 sprintf(namelist[i], "Input %d", i);
1686 }
1687
1688 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1689 if (! kctl) {
1690 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001691 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 kfree(cval);
1693 return -ENOMEM;
1694 }
1695 kctl->private_value = (unsigned long)namelist;
1696 kctl->private_free = usb_mixer_selector_elem_free;
1697
1698 nameid = desc[desc[0] - 1];
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001699 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (len)
1701 ;
1702 else if (nameid)
1703 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1704 else {
1705 len = get_term_name(state, &state->oterm,
1706 kctl->id.name, sizeof(kctl->id.name), 0);
1707 if (! len)
1708 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1709
1710 if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02001711 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02001713 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
1716 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1717 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001718 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return err;
1720
1721 return 0;
1722}
1723
1724
1725/*
1726 * parse an audio unit recursively
1727 */
1728
Takashi Iwai86e07d32005-11-17 15:08:02 +01001729static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 unsigned char *p1;
1732
1733 if (test_and_set_bit(unitid, state->unitbitmap))
1734 return 0; /* the unit already visited */
1735
1736 p1 = find_audio_control_unit(state, unitid);
1737 if (!p1) {
1738 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1739 return -EINVAL;
1740 }
1741
1742 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001743 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01001745 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001747 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001749 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001751 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return parse_audio_processing_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001753 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return parse_audio_extension_unit(state, unitid, p1);
1755 default:
1756 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1757 return -EINVAL;
1758 }
1759}
1760
Clemens Ladisch84957a82005-04-29 16:23:13 +02001761static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1762{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001763 kfree(mixer->id_elems);
1764 if (mixer->urb) {
1765 kfree(mixer->urb->transfer_buffer);
1766 usb_free_urb(mixer->urb);
1767 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001768 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001769 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001770 kfree(mixer);
1771}
1772
Takashi Iwai86e07d32005-11-17 15:08:02 +01001773static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001774{
1775 struct usb_mixer_interface *mixer = device->device_data;
1776 snd_usb_mixer_free(mixer);
1777 return 0;
1778}
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780/*
1781 * create mixer controls
1782 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01001783 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001785static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Daniel Mack28e1b772010-02-22 23:49:09 +01001787 struct uac_output_terminal_descriptor_v1 *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001788 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 int err;
1790 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001791 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Clemens Ladisch84957a82005-04-29 16:23:13 +02001793 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001795 state.chip = mixer->chip;
1796 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 state.buffer = hostif->extra;
1798 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001801 for (map = usbmix_ctl_maps; map->id; map++) {
1802 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001804 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001805 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 }
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 desc = NULL;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001811 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack28e1b772010-02-22 23:49:09 +01001812 if (desc->bLength < 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 continue; /* invalid descriptor? */
Daniel Mack28e1b772010-02-22 23:49:09 +01001814 set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
1815 state.oterm.id = desc->bTerminalID;
1816 state.oterm.type = le16_to_cpu(desc->wTerminalType);
1817 state.oterm.name = desc->iTerminal;
1818 err = parse_audio_unit(&state, desc->bSourceID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (err < 0)
1820 return err;
1821 }
1822 return 0;
1823}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001824
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001825static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1826 int unitid)
1827{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001828 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001829
1830 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1831 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1832 info->elem_id);
1833}
1834
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001835static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1836 int unitid,
1837 struct usb_mixer_elem_info *cval)
1838{
1839 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1840 "S8", "U8", "S16", "U16"};
1841 snd_iprintf(buffer, " Unit: %i\n", unitid);
1842 if (cval->elem_id)
1843 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1844 cval->elem_id->name, cval->elem_id->index);
1845 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1846 "channels=%i, type=\"%s\"\n", cval->id,
1847 cval->control, cval->cmask, cval->channels,
1848 val_types[cval->val_type]);
1849 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1850 cval->min, cval->max, cval->dBmin, cval->dBmax);
1851}
1852
1853static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1854 struct snd_info_buffer *buffer)
1855{
1856 struct snd_usb_audio *chip = entry->private_data;
1857 struct usb_mixer_interface *mixer;
1858 struct usb_mixer_elem_info *cval;
1859 int unitid;
1860
1861 list_for_each_entry(mixer, &chip->mixer_list, list) {
1862 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01001863 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1864 chip->usb_id, mixer->ctrlif,
1865 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001866 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1867 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1868 for (cval = mixer->id_elems[unitid]; cval;
1869 cval = cval->next_id_elem)
1870 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1871 }
1872 }
1873}
1874
Clemens Ladischb259b102005-04-29 16:29:28 +02001875static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1876 int unitid)
1877{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001878 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001879 return;
1880 /* unit ids specific to Extigy/Audigy 2 NX: */
1881 switch (unitid) {
1882 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001883 mixer->rc_urb->dev = mixer->chip->dev;
1884 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001885 break;
1886 case 4: /* digital in jack */
1887 case 7: /* line in jacks */
1888 case 19: /* speaker out jacks */
1889 case 20: /* headphones out jack */
1890 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001891 /* live24ext: 4 = line-in jack */
1892 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001893 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1894 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001895 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1896 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001897 default:
1898 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1899 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001900 }
1901}
1902
David Howells7d12e782006-10-05 14:55:46 +01001903static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001904{
1905 struct usb_mixer_interface *mixer = urb->context;
1906
1907 if (urb->status == 0) {
1908 u8 *buf = urb->transfer_buffer;
1909 int i;
1910
1911 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1912 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1913 buf[0], buf[1]);
1914 /* ignore any notifications not from the control interface */
1915 if ((buf[0] & 0x0f) != 0)
1916 continue;
1917 if (!(buf[0] & 0x40))
1918 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001919 else
1920 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001921 }
1922 }
1923 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1924 urb->dev = mixer->chip->dev;
1925 usb_submit_urb(urb, GFP_ATOMIC);
1926 }
1927}
1928
1929/* create the handler for the optional status interrupt endpoint */
1930static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1931{
1932 struct usb_host_interface *hostif;
1933 struct usb_endpoint_descriptor *ep;
1934 void *transfer_buffer;
1935 int buffer_length;
1936 unsigned int epnum;
1937
1938 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1939 /* we need one interrupt input endpoint */
1940 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1941 return 0;
1942 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001943 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001944 return 0;
1945
Julia Lawall42a6e662008-12-29 11:23:02 +01001946 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001947 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1948 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1949 if (!transfer_buffer)
1950 return -ENOMEM;
1951 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1952 if (!mixer->urb) {
1953 kfree(transfer_buffer);
1954 return -ENOMEM;
1955 }
1956 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1957 usb_rcvintpipe(mixer->chip->dev, epnum),
1958 transfer_buffer, buffer_length,
1959 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1960 usb_submit_urb(mixer->urb, GFP_KERNEL);
1961 return 0;
1962}
1963
David Howells7d12e782006-10-05 14:55:46 +01001964static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001965{
1966 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001967 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001968 u32 code;
1969
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001970 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001971 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001972
1973 code = mixer->rc_buffer[rc->offset];
1974 if (rc->length == 2)
1975 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1976
Clemens Ladischb259b102005-04-29 16:29:28 +02001977 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001978 if (code == rc->mute_code)
1979 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001980 mixer->rc_code = code;
1981 wmb();
1982 wake_up(&mixer->rc_waitq);
1983}
1984
Takashi Iwai86e07d32005-11-17 15:08:02 +01001985static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001986 long count, loff_t *offset)
1987{
1988 struct usb_mixer_interface *mixer = hw->private_data;
1989 int err;
1990 u32 rc_code;
1991
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001992 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001993 return -EINVAL;
1994 err = wait_event_interruptible(mixer->rc_waitq,
1995 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1996 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001997 if (count == 1)
1998 err = put_user(rc_code, buf);
1999 else
2000 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02002001 }
2002 return err < 0 ? err : count;
2003}
2004
Takashi Iwai86e07d32005-11-17 15:08:02 +01002005static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02002006 poll_table *wait)
2007{
2008 struct usb_mixer_interface *mixer = hw->private_data;
2009
2010 poll_wait(file, &mixer->rc_waitq, wait);
2011 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
2012}
2013
2014static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
2015{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002016 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002017 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02002018
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002019 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
2020 if (rc_configs[i].usb_id == mixer->chip->usb_id)
2021 break;
2022 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02002023 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002024 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02002025
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002026 len = mixer->rc_cfg->packet_length;
2027
Clemens Ladischb259b102005-04-29 16:29:28 +02002028 init_waitqueue_head(&mixer->rc_waitq);
2029 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
2030 if (err < 0)
2031 return err;
2032 snprintf(hwdep->name, sizeof(hwdep->name),
2033 "%s remote control", mixer->chip->card->shortname);
2034 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
2035 hwdep->private_data = mixer;
2036 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02002037 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01002038 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02002039
2040 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
2041 if (!mixer->rc_urb)
2042 return -ENOMEM;
2043 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
2044 if (!mixer->rc_setup_packet) {
2045 usb_free_urb(mixer->rc_urb);
2046 mixer->rc_urb = NULL;
2047 return -ENOMEM;
2048 }
2049 mixer->rc_setup_packet->bRequestType =
2050 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
Daniel Mackde48c7b2010-02-22 23:49:13 +01002051 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
Clemens Ladischb259b102005-04-29 16:29:28 +02002052 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2053 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2054 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2055 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2056 usb_rcvctrlpipe(mixer->chip->dev, 0),
2057 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2058 snd_usb_soundblaster_remote_complete, mixer);
2059 return 0;
2060}
2061
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002062#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002063
Takashi Iwai86e07d32005-11-17 15:08:02 +01002064static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002065{
2066 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2067 int index = kcontrol->private_value;
2068
2069 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2070 return 0;
2071}
2072
Takashi Iwai86e07d32005-11-17 15:08:02 +01002073static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002074{
2075 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2076 int index = kcontrol->private_value;
2077 int value = ucontrol->value.integer.value[0];
2078 int err, changed;
2079
2080 if (value > 1)
2081 return -EINVAL;
2082 changed = value != mixer->audigy2nx_leds[index];
2083 err = snd_usb_ctl_msg(mixer->chip->dev,
2084 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2085 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2086 value, index + 2, NULL, 0, 100);
2087 if (err < 0)
2088 return err;
2089 mixer->audigy2nx_leds[index] = value;
2090 return changed;
2091}
2092
Takashi Iwai86e07d32005-11-17 15:08:02 +01002093static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002094 {
2095 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2096 .name = "CMSS LED Switch",
2097 .info = snd_audigy2nx_led_info,
2098 .get = snd_audigy2nx_led_get,
2099 .put = snd_audigy2nx_led_put,
2100 .private_value = 0,
2101 },
2102 {
2103 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2104 .name = "Power LED Switch",
2105 .info = snd_audigy2nx_led_info,
2106 .get = snd_audigy2nx_led_get,
2107 .put = snd_audigy2nx_led_put,
2108 .private_value = 1,
2109 },
2110 {
2111 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2112 .name = "Dolby Digital LED Switch",
2113 .info = snd_audigy2nx_led_info,
2114 .get = snd_audigy2nx_led_get,
2115 .put = snd_audigy2nx_led_put,
2116 .private_value = 2,
2117 },
2118};
2119
2120static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2121{
2122 int i, err;
2123
2124 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01002125 if (i > 1 && /* Live24ext has 2 LEDs only */
2126 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2127 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002128 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002129 err = snd_ctl_add(mixer->chip->card,
2130 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2131 if (err < 0)
2132 return err;
2133 }
2134 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2135 return 0;
2136}
2137
Takashi Iwai86e07d32005-11-17 15:08:02 +01002138static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2139 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02002140{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002141 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02002142 int unitid;
2143 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002144 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02002145 {4, "dig in "},
2146 {7, "line in"},
2147 {19, "spk out"},
2148 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002149 {-1, NULL}
2150 }, jacks_live24ext[] = {
2151 {4, "line in"}, /* &1=Line, &2=Mic*/
2152 {3, "hph out"}, /* headphones */
2153 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2154 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002155 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002156 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002157 struct usb_mixer_interface *mixer = entry->private_data;
2158 int i, err;
2159 u8 buf[3];
2160
2161 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002162 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2163 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002164 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2165 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002166 jacks = jacks_live24ext;
2167 else
2168 return;
2169
2170 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002171 snd_iprintf(buffer, "%s: ", jacks[i].name);
2172 err = snd_usb_ctl_msg(mixer->chip->dev,
2173 usb_rcvctrlpipe(mixer->chip->dev, 0),
Daniel Mackde48c7b2010-02-22 23:49:13 +01002174 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
Clemens Ladischaafad562005-05-10 14:47:38 +02002175 USB_RECIP_INTERFACE, 0,
2176 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002177 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002178 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2179 else
2180 snd_iprintf(buffer, "?\n");
2181 }
2182}
2183
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002184static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
2187 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2188
2189 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2190 return 0;
2191}
2192
2193static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_value *ucontrol)
2195{
2196 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2197 u8 old_status, new_status;
2198 int err, changed;
2199
2200 old_status = mixer->xonar_u1_status;
2201 if (ucontrol->value.integer.value[0])
2202 new_status = old_status | 0x02;
2203 else
2204 new_status = old_status & ~0x02;
2205 changed = new_status != old_status;
2206 err = snd_usb_ctl_msg(mixer->chip->dev,
2207 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2208 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2209 50, 0, &new_status, 1, 100);
2210 if (err < 0)
2211 return err;
2212 mixer->xonar_u1_status = new_status;
2213 return changed;
2214}
2215
2216static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2217 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2218 .name = "Digital Playback Switch",
2219 .info = snd_ctl_boolean_mono_info,
2220 .get = snd_xonar_u1_switch_get,
2221 .put = snd_xonar_u1_switch_put,
2222};
2223
2224static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2225{
2226 int err;
2227
2228 err = snd_ctl_add(mixer->chip->card,
2229 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2230 if (err < 0)
2231 return err;
2232 mixer->xonar_u1_status = 0x05;
2233 return 0;
2234}
2235
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002236void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002237 unsigned char samplerate_id)
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002238{
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002239 struct usb_mixer_interface *mixer;
2240 struct usb_mixer_elem_info *cval;
2241 int unitid = 12; /* SamleRate ExtensionUnit ID */
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002242
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002243 list_for_each_entry(mixer, &chip->mixer_list, list) {
2244 cval = mixer->id_elems[unitid];
2245 if (cval) {
2246 set_cur_ctl_value(cval, cval->control << 8,
2247 samplerate_id);
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002248 snd_usb_mixer_notify_id(mixer, unitid);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002249 }
2250 break;
2251 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002252}
2253
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002254int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2255 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002256{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002257 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002258 .dev_free = snd_usb_mixer_dev_free
2259 };
2260 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002261 struct snd_info_entry *entry;
Daniel Mack7b8a0432010-02-22 23:49:12 +01002262 struct usb_host_interface *host_iface;
2263 int err, protocol;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002264
2265 strcpy(chip->card->mixername, "USB Mixer");
2266
Takashi Iwai561b2202005-09-09 14:22:34 +02002267 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002268 if (!mixer)
2269 return -ENOMEM;
2270 mixer->chip = chip;
2271 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002272 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002273 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2274 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002275 if (!mixer->id_elems) {
2276 kfree(mixer);
2277 return -ENOMEM;
2278 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002279
Daniel Mack7b8a0432010-02-22 23:49:12 +01002280 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2281 protocol = host_iface->desc.bInterfaceProtocol;
2282
2283 /* FIXME! */
2284 if (protocol != UAC_VERSION_1) {
2285 snd_printk(KERN_WARNING "mixer interface protocol 0x%02x not yet supported\n",
2286 protocol);
2287 return 0;
2288 }
2289
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002290 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002291 (err = snd_usb_mixer_status_create(mixer)) < 0)
2292 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002293
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002294 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2295 goto _error;
2296
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002297 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002298 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2299 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002300 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2301 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002302 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002303 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002304 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002305 }
2306
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002307 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2308 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2309 err = snd_xonar_u1_controls_create(mixer);
2310 if (err < 0)
2311 goto _error;
2312 }
2313
Clemens Ladisch84957a82005-04-29 16:23:13 +02002314 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002315 if (err < 0)
2316 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002317
2318 if (list_empty(&chip->mixer_list) &&
2319 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2320 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2321
Clemens Ladisch84957a82005-04-29 16:23:13 +02002322 list_add(&mixer->list, &chip->mixer_list);
2323 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002324
2325_error:
2326 snd_usb_mixer_free(mixer);
2327 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002328}
2329
2330void snd_usb_mixer_disconnect(struct list_head *p)
2331{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002332 struct usb_mixer_interface *mixer;
2333
2334 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002335 usb_kill_urb(mixer->urb);
2336 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002337}