blob: 03f125dca5ffb274210efc6dabc6b28e2aa4e99a [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>
35#include <sound/core.h>
36#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020037#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020038#include <sound/info.h>
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +020039#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "usbaudio.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 */
45
46/* ignore error from controls - for debugging */
47/* #define IGNORE_CTL_ERROR */
48
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020049/*
50 * Sound Blaster remote control configuration
51 *
52 * format of remote control data:
53 * Extigy: xx 00
54 * Audigy 2 NX: 06 80 xx 00 00 00
55 * Live! 24-bit: 06 80 xx yy 22 83
56 */
57static const struct rc_config {
58 u32 usb_id;
59 u8 offset;
60 u8 length;
61 u8 packet_length;
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020062 u8 min_packet_length; /* minimum accepted length of the URB result */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020063 u8 mute_mixer_id;
64 u32 mute_code;
65} rc_configs[] = {
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020066 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
67 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
68 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
Andrea Borgia31959542009-01-07 22:58:50 +010069 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020070};
71
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010072#define MAX_ID_ELEMS 256
73
Clemens Ladisch84957a82005-04-29 16:23:13 +020074struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010075 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020076 unsigned int ctrlif;
77 struct list_head list;
78 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020079 struct urb *urb;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010080 /* array[MAX_ID_ELEMS], indexed by unit id */
81 struct usb_mixer_elem_info **id_elems;
Clemens Ladischb259b102005-04-29 16:29:28 +020082
83 /* Sound Blaster remote control stuff */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020084 const struct rc_config *rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +020085 u32 rc_code;
86 wait_queue_head_t rc_waitq;
87 struct urb *rc_urb;
88 struct usb_ctrlrequest *rc_setup_packet;
89 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020090
91 u8 audigy2nx_leds[3];
Clemens Ladisch468b8fd2009-07-13 11:39:29 +020092 u8 xonar_u1_status;
Clemens Ladisch84957a82005-04-29 16:23:13 +020093};
94
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096struct usb_audio_term {
97 int id;
98 int type;
99 int channels;
100 unsigned int chconfig;
101 int name;
102};
103
104struct usbmix_name_map;
105
Takashi Iwai86e07d32005-11-17 15:08:02 +0100106struct mixer_build {
107 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200108 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unsigned char *buffer;
110 unsigned int buflen;
Clemens Ladisch707e6072005-04-29 09:56:17 +0200111 DECLARE_BITMAP(unitbitmap, 256);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100112 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200114 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
Takashi Iwai641b4872009-01-15 17:05:24 +0100117#define MAX_CHANNELS 10 /* max logical channels */
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200120 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100121 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
122 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned int id;
124 unsigned int control; /* CS or ICN (high byte) */
125 unsigned int cmask; /* channel mask bitmap: 0 = master */
126 int channels;
127 int val_type;
128 int min, max, res;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100129 int dBmin, dBmax;
Takashi Iwai641b4872009-01-15 17:05:24 +0100130 int cached;
131 int cache_val[MAX_CHANNELS];
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200132 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135
136enum {
137 USB_FEATURE_NONE = 0,
138 USB_FEATURE_MUTE = 1,
139 USB_FEATURE_VOLUME,
140 USB_FEATURE_BASS,
141 USB_FEATURE_MID,
142 USB_FEATURE_TREBLE,
143 USB_FEATURE_GEQ,
144 USB_FEATURE_AGC,
145 USB_FEATURE_DELAY,
146 USB_FEATURE_BASSBOOST,
147 USB_FEATURE_LOUDNESS
148};
149
150enum {
151 USB_MIXER_BOOLEAN,
152 USB_MIXER_INV_BOOLEAN,
153 USB_MIXER_S8,
154 USB_MIXER_U8,
155 USB_MIXER_S16,
156 USB_MIXER_U16,
157};
158
159enum {
160 USB_PROC_UPDOWN = 1,
161 USB_PROC_UPDOWN_SWITCH = 1,
162 USB_PROC_UPDOWN_MODE_SEL = 2,
163
164 USB_PROC_PROLOGIC = 2,
165 USB_PROC_PROLOGIC_SWITCH = 1,
166 USB_PROC_PROLOGIC_MODE_SEL = 2,
167
168 USB_PROC_3DENH = 3,
169 USB_PROC_3DENH_SWITCH = 1,
170 USB_PROC_3DENH_SPACE = 2,
171
172 USB_PROC_REVERB = 4,
173 USB_PROC_REVERB_SWITCH = 1,
174 USB_PROC_REVERB_LEVEL = 2,
175 USB_PROC_REVERB_TIME = 3,
176 USB_PROC_REVERB_DELAY = 4,
177
178 USB_PROC_CHORUS = 5,
179 USB_PROC_CHORUS_SWITCH = 1,
180 USB_PROC_CHORUS_LEVEL = 2,
181 USB_PROC_CHORUS_RATE = 3,
182 USB_PROC_CHORUS_DEPTH = 4,
183
184 USB_PROC_DCR = 6,
185 USB_PROC_DCR_SWITCH = 1,
186 USB_PROC_DCR_RATIO = 2,
187 USB_PROC_DCR_MAX_AMP = 3,
188 USB_PROC_DCR_THRESHOLD = 4,
189 USB_PROC_DCR_ATTACK = 5,
190 USB_PROC_DCR_RELEASE = 6,
191};
192
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -0800193/*E-mu 0202(0404) eXtension Unit(XU) control*/
194enum {
195 USB_XU_CLOCK_RATE = 0xe301,
196 USB_XU_CLOCK_SOURCE = 0xe302,
197 USB_XU_DIGITAL_IO_STATUS = 0xe303,
198 USB_XU_DEVICE_OPTIONS = 0xe304,
199 USB_XU_DIRECT_MONITORING = 0xe305,
200 USB_XU_METERING = 0xe306
201};
202enum {
203 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
204 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
205 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
206 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
207};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * manual mapping of mixer names
211 * if the mixer topology is too complicated and the parsed names are
212 * ambiguous, add the entries in usbmixer_maps.c.
213 */
214#include "usbmixer_maps.c"
215
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100216static const struct usbmix_name_map *
217find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100219 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100221 if (!p)
222 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100225 if (p->id == unitid &&
226 (!control || !p->control || control == p->control))
227 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100229 return NULL;
230}
231
232/* get the mapped name if the unit matches */
233static int
234check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
235{
236 if (!p || !p->name)
237 return 0;
238
239 buflen--;
240 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100244static inline int
245check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100247 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100249 return 1;
250}
251
252/* dB mapping */
253static inline void check_mapped_dB(const struct usbmix_name_map *p,
254 struct usb_mixer_elem_info *cval)
255{
256 if (p && p->dB) {
257 cval->dBmin = p->dB->min;
258 cval->dBmax = p->dB->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200262/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100263static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200264 int index, char *buf, int buflen)
265{
266 const struct usbmix_selector_map *p;
267
268 if (! state->selector_map)
269 return 0;
270 for (p = state->selector_map; p->id; p++) {
271 if (p->id == unitid && index < p->count)
272 return strlcpy(buf, p->names[index], buflen);
273 }
274 return 0;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * find an audio control unit with the given unit id
279 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100280static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 unsigned char *p;
283
284 p = NULL;
285 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
286 USB_DT_CS_INTERFACE)) != NULL) {
287 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
288 return p;
289 }
290 return NULL;
291}
292
293
294/*
295 * copy a string with the given id
296 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100297static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
300 buf[len] = 0;
301 return len;
302}
303
304/*
305 * convert from the byte/word on usb descriptor to the zero-based integer
306 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100307static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 switch (cval->val_type) {
310 case USB_MIXER_BOOLEAN:
311 return !!val;
312 case USB_MIXER_INV_BOOLEAN:
313 return !val;
314 case USB_MIXER_U8:
315 val &= 0xff;
316 break;
317 case USB_MIXER_S8:
318 val &= 0xff;
319 if (val >= 0x80)
320 val -= 0x100;
321 break;
322 case USB_MIXER_U16:
323 val &= 0xffff;
324 break;
325 case USB_MIXER_S16:
326 val &= 0xffff;
327 if (val >= 0x8000)
328 val -= 0x10000;
329 break;
330 }
331 return val;
332}
333
334/*
335 * convert from the zero-based int to the byte/word for usb descriptor
336 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100337static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 switch (cval->val_type) {
340 case USB_MIXER_BOOLEAN:
341 return !!val;
342 case USB_MIXER_INV_BOOLEAN:
343 return !val;
344 case USB_MIXER_S8:
345 case USB_MIXER_U8:
346 return val & 0xff;
347 case USB_MIXER_S16:
348 case USB_MIXER_U16:
349 return val & 0xffff;
350 }
351 return 0; /* not reached */
352}
353
Takashi Iwai86e07d32005-11-17 15:08:02 +0100354static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 if (! cval->res)
357 cval->res = 1;
358 if (val < cval->min)
359 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200360 else if (val >= cval->max)
361 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 else
363 return (val - cval->min) / cval->res;
364}
365
Takashi Iwai86e07d32005-11-17 15:08:02 +0100366static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 if (val < 0)
369 return cval->min;
370 if (! cval->res)
371 cval->res = 1;
372 val *= cval->res;
373 val += cval->min;
374 if (val > cval->max)
375 return cval->max;
376 return val;
377}
378
379
380/*
381 * retrieve a mixer value
382 */
383
Takashi Iwai86e07d32005-11-17 15:08:02 +0100384static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 unsigned char buf[2];
387 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
388 int timeout = 10;
389
390 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200391 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
392 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 request,
394 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200395 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200396 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
398 return 0;
399 }
400 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200401 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
402 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return -EINVAL;
404}
405
Takashi Iwai86e07d32005-11-17 15:08:02 +0100406static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 return get_ctl_value(cval, GET_CUR, validx, value);
409}
410
411/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100412static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
413 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
416}
417
Takashi Iwai641b4872009-01-15 17:05:24 +0100418static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
419 int channel, int index, int *value)
420{
421 int err;
422
423 if (cval->cached & (1 << channel)) {
424 *value = cval->cache_val[index];
425 return 0;
426 }
427 err = get_cur_mix_raw(cval, channel, value);
428 if (err < 0) {
429 if (!cval->mixer->ignore_ctl_error)
430 snd_printd(KERN_ERR "cannot get current value for "
431 "control %d ch %d: err = %d\n",
432 cval->control, channel, err);
433 return err;
434 }
435 cval->cached |= 1 << channel;
436 cval->cache_val[index] = *value;
437 return 0;
438}
439
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/*
442 * set a mixer value
443 */
444
Takashi Iwai86e07d32005-11-17 15:08:02 +0100445static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 unsigned char buf[2];
448 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
449 int timeout = 10;
450
451 value_set = convert_bytes_value(cval, value_set);
452 buf[0] = value_set & 0xff;
453 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530454 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200455 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
456 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 request,
458 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200459 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 buf, val_len, 100) >= 0)
461 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200462 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
463 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EINVAL;
465}
466
Takashi Iwai86e07d32005-11-17 15:08:02 +0100467static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return set_ctl_value(cval, SET_CUR, validx, value);
470}
471
Takashi Iwai641b4872009-01-15 17:05:24 +0100472static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
473 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Takashi Iwai641b4872009-01-15 17:05:24 +0100475 int err;
476 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
477 value);
478 if (err < 0)
479 return err;
480 cval->cached |= 1 << channel;
481 cval->cache_val[index] = value;
482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200485/*
486 * TLV callback for mixer volume controls
487 */
488static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
489 unsigned int size, unsigned int __user *_tlv)
490{
491 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200492 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200493
494 if (size < sizeof(scale))
495 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100496 scale[2] = cval->dBmin;
497 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200498 if (copy_to_user(_tlv, scale, sizeof(scale)))
499 return -EFAULT;
500 return 0;
501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503/*
504 * parser routines begin here...
505 */
506
Takashi Iwai86e07d32005-11-17 15:08:02 +0100507static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509
510/*
511 * check if the input/output channel routing is enabled on the given bitmap.
512 * used for mixer unit parser
513 */
514static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
515{
516 int idx = ich * num_outs + och;
517 return bmap[idx >> 3] & (0x80 >> (idx & 7));
518}
519
520
521/*
522 * add an alsa control element
523 * search and increment the index until an empty slot is found.
524 *
525 * if failed, give up and free the control instance.
526 */
527
Takashi Iwai86e07d32005-11-17 15:08:02 +0100528static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100530 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200532
533 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200535 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200537 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200539 cval->elem_id = &kctl->id;
540 cval->next_id_elem = state->mixer->id_elems[cval->id];
541 state->mixer->id_elems[cval->id] = cval;
542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545
546/*
547 * get a terminal name string
548 */
549
550static struct iterm_name_combo {
551 int type;
552 char *name;
553} iterm_names[] = {
554 { 0x0300, "Output" },
555 { 0x0301, "Speaker" },
556 { 0x0302, "Headphone" },
557 { 0x0303, "HMD Audio" },
558 { 0x0304, "Desktop Speaker" },
559 { 0x0305, "Room Speaker" },
560 { 0x0306, "Com Speaker" },
561 { 0x0307, "LFE" },
562 { 0x0600, "External In" },
563 { 0x0601, "Analog In" },
564 { 0x0602, "Digital In" },
565 { 0x0603, "Line" },
566 { 0x0604, "Legacy In" },
567 { 0x0605, "IEC958 In" },
568 { 0x0606, "1394 DA Stream" },
569 { 0x0607, "1394 DV Stream" },
570 { 0x0700, "Embedded" },
571 { 0x0701, "Noise Source" },
572 { 0x0702, "Equalization Noise" },
573 { 0x0703, "CD" },
574 { 0x0704, "DAT" },
575 { 0x0705, "DCC" },
576 { 0x0706, "MiniDisk" },
577 { 0x0707, "Analog Tape" },
578 { 0x0708, "Phonograph" },
579 { 0x0709, "VCR Audio" },
580 { 0x070a, "Video Disk Audio" },
581 { 0x070b, "DVD Audio" },
582 { 0x070c, "TV Tuner Audio" },
583 { 0x070d, "Satellite Rec Audio" },
584 { 0x070e, "Cable Tuner Audio" },
585 { 0x070f, "DSS Audio" },
586 { 0x0710, "Radio Receiver" },
587 { 0x0711, "Radio Transmitter" },
588 { 0x0712, "Multi-Track Recorder" },
589 { 0x0713, "Synthesizer" },
590 { 0 },
591};
592
Takashi Iwai86e07d32005-11-17 15:08:02 +0100593static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned char *name, int maxlen, int term_only)
595{
596 struct iterm_name_combo *names;
597
598 if (iterm->name)
599 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
600
601 /* virtual type - not a real terminal */
602 if (iterm->type >> 16) {
603 if (term_only)
604 return 0;
605 switch (iterm->type >> 16) {
606 case SELECTOR_UNIT:
607 strcpy(name, "Selector"); return 8;
608 case PROCESSING_UNIT:
609 strcpy(name, "Process Unit"); return 12;
610 case EXTENSION_UNIT:
611 strcpy(name, "Ext Unit"); return 8;
612 case MIXER_UNIT:
613 strcpy(name, "Mixer"); return 5;
614 default:
615 return sprintf(name, "Unit %d", iterm->id);
616 }
617 }
618
619 switch (iterm->type & 0xff00) {
620 case 0x0100:
621 strcpy(name, "PCM"); return 3;
622 case 0x0200:
623 strcpy(name, "Mic"); return 3;
624 case 0x0400:
625 strcpy(name, "Headset"); return 7;
626 case 0x0500:
627 strcpy(name, "Phone"); return 5;
628 }
629
630 for (names = iterm_names; names->type; names++)
631 if (names->type == iterm->type) {
632 strcpy(name, names->name);
633 return strlen(names->name);
634 }
635 return 0;
636}
637
638
639/*
640 * parse the source unit recursively until it reaches to a terminal
641 * or a branched unit.
642 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100643static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 unsigned char *p1;
646
647 memset(term, 0, sizeof(*term));
648 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
649 term->id = id;
650 switch (p1[2]) {
651 case INPUT_TERMINAL:
652 term->type = combine_word(p1 + 4);
653 term->channels = p1[7];
654 term->chconfig = combine_word(p1 + 8);
655 term->name = p1[11];
656 return 0;
657 case FEATURE_UNIT:
658 id = p1[4];
659 break; /* continue to parse */
660 case MIXER_UNIT:
661 term->type = p1[2] << 16; /* virtual type */
662 term->channels = p1[5 + p1[4]];
663 term->chconfig = combine_word(p1 + 6 + p1[4]);
664 term->name = p1[p1[0] - 1];
665 return 0;
666 case SELECTOR_UNIT:
667 /* call recursively to retrieve the channel info */
668 if (check_input_term(state, p1[5], term) < 0)
669 return -ENODEV;
670 term->type = p1[2] << 16; /* virtual type */
671 term->id = id;
672 term->name = p1[9 + p1[0] - 1];
673 return 0;
674 case PROCESSING_UNIT:
675 case EXTENSION_UNIT:
676 if (p1[6] == 1) {
677 id = p1[7];
678 break; /* continue to parse */
679 }
680 term->type = p1[2] << 16; /* virtual type */
681 term->channels = p1[7 + p1[6]];
682 term->chconfig = combine_word(p1 + 8 + p1[6]);
683 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
684 return 0;
685 default:
686 return -ENODEV;
687 }
688 }
689 return -ENODEV;
690}
691
692
693/*
694 * Feature Unit
695 */
696
697/* feature unit control information */
698struct usb_feature_control_info {
699 const char *name;
700 unsigned int type; /* control type (mute, volume, etc.) */
701};
702
703static struct usb_feature_control_info audio_feature_info[] = {
704 { "Mute", USB_MIXER_INV_BOOLEAN },
705 { "Volume", USB_MIXER_S16 },
706 { "Tone Control - Bass", USB_MIXER_S8 },
707 { "Tone Control - Mid", USB_MIXER_S8 },
708 { "Tone Control - Treble", USB_MIXER_S8 },
709 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
710 { "Auto Gain Control", USB_MIXER_BOOLEAN },
711 { "Delay Control", USB_MIXER_U16 },
712 { "Bass Boost", USB_MIXER_BOOLEAN },
713 { "Loudness", USB_MIXER_BOOLEAN },
714};
715
716
717/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100718static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jesper Juhl4d572772005-05-30 17:30:32 +0200720 kfree(kctl->private_data);
721 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724
725/*
726 * interface to ALSA control for feature/mixer units
727 */
728
729/*
730 * retrieve the minimum and maximum values for the specified control
731 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100732static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 /* for failsafe */
735 cval->min = default_min;
736 cval->max = cval->min + 1;
737 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100738 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (cval->val_type == USB_MIXER_BOOLEAN ||
741 cval->val_type == USB_MIXER_INV_BOOLEAN) {
742 cval->initialized = 1;
743 } else {
744 int minchn = 0;
745 if (cval->cmask) {
746 int i;
747 for (i = 0; i < MAX_CHANNELS; i++)
748 if (cval->cmask & (1 << i)) {
749 minchn = i + 1;
750 break;
751 }
752 }
753 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
754 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200755 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
756 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EINVAL;
758 }
759 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
760 cval->res = 1;
761 } else {
762 int last_valid_res = cval->res;
763
764 while (cval->res > 1) {
765 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
766 break;
767 cval->res /= 2;
768 }
769 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
770 cval->res = last_valid_res;
771 }
772 if (cval->res == 0)
773 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200774
775 /* Additional checks for the proper resolution
776 *
777 * Some devices report smaller resolutions than actually
778 * reacting. They don't return errors but simply clip
779 * to the lower aligned value.
780 */
781 if (cval->min + cval->res < cval->max) {
782 int last_valid_res = cval->res;
783 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100784 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200785 for (;;) {
786 test = saved;
787 if (test < cval->max)
788 test += cval->res;
789 else
790 test -= cval->res;
791 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100792 set_cur_mix_value(cval, minchn, 0, test) ||
793 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200794 cval->res = last_valid_res;
795 break;
796 }
797 if (test == check)
798 break;
799 cval->res *= 2;
800 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100801 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 cval->initialized = 1;
805 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100806
807 /* USB descriptions contain the dB scale in 1/256 dB unit
808 * while ALSA TLV contains in 1/100 dB unit
809 */
810 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
811 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
812 if (cval->dBmin > cval->dBmax) {
813 /* something is wrong; assume it's either from/to 0dB */
814 if (cval->dBmin < 0)
815 cval->dBmax = 0;
816 else if (cval->dBmin > 0)
817 cval->dBmin = 0;
818 if (cval->dBmin > cval->dBmax) {
819 /* totally crap, return an error */
820 return -EINVAL;
821 }
822 }
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825}
826
827
828/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100829static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100831 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (cval->val_type == USB_MIXER_BOOLEAN ||
834 cval->val_type == USB_MIXER_INV_BOOLEAN)
835 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
836 else
837 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
838 uinfo->count = cval->channels;
839 if (cval->val_type == USB_MIXER_BOOLEAN ||
840 cval->val_type == USB_MIXER_INV_BOOLEAN) {
841 uinfo->value.integer.min = 0;
842 uinfo->value.integer.max = 1;
843 } else {
844 if (! cval->initialized)
845 get_min_max(cval, 0);
846 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200847 uinfo->value.integer.max =
848 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 return 0;
851}
852
853/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100854static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100856 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int c, cnt, val, err;
858
Takashi Iwai641b4872009-01-15 17:05:24 +0100859 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (cval->cmask) {
861 cnt = 0;
862 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100863 if (!(cval->cmask & (1 << c)))
864 continue;
865 err = get_cur_mix_value(cval, c + 1, cnt, &val);
866 if (err < 0)
867 return cval->mixer->ignore_ctl_error ? 0 : err;
868 val = get_relative_value(cval, val);
869 ucontrol->value.integer.value[cnt] = val;
870 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
874 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100875 err = get_cur_mix_value(cval, 0, 0, &val);
876 if (err < 0)
877 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 val = get_relative_value(cval, val);
879 ucontrol->value.integer.value[0] = val;
880 }
881 return 0;
882}
883
884/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100885static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100887 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int c, cnt, val, oval, err;
889 int changed = 0;
890
891 if (cval->cmask) {
892 cnt = 0;
893 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100894 if (!(cval->cmask & (1 << c)))
895 continue;
896 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
897 if (err < 0)
898 return cval->mixer->ignore_ctl_error ? 0 : err;
899 val = ucontrol->value.integer.value[cnt];
900 val = get_abs_value(cval, val);
901 if (oval != val) {
902 set_cur_mix_value(cval, c + 1, cnt, val);
903 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100905 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 } else {
908 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100909 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100911 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 val = ucontrol->value.integer.value[0];
913 val = get_abs_value(cval, val);
914 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100915 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 changed = 1;
917 }
918 }
919 return changed;
920}
921
Takashi Iwai86e07d32005-11-17 15:08:02 +0100922static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
924 .name = "", /* will be filled later manually */
925 .info = mixer_ctl_feature_info,
926 .get = mixer_ctl_feature_get,
927 .put = mixer_ctl_feature_put,
928};
929
930
931/*
932 * build a feature control
933 */
934
Takashi Iwai08d1e632009-10-02 14:06:08 +0200935static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
936{
937 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
938}
939
Takashi Iwai86e07d32005-11-17 15:08:02 +0100940static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100942 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 unsigned int len = 0;
945 int mapped_name = 0;
946 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100947 struct snd_kcontrol *kctl;
948 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100949 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 control++; /* change from zero-based to 1-based value */
952
953 if (control == USB_FEATURE_GEQ) {
954 /* FIXME: not supported yet */
955 return;
956 }
957
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100958 map = find_map(state, unitid, control);
959 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
961
Takashi Iwai561b2202005-09-09 14:22:34 +0200962 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (! cval) {
964 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
965 return;
966 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200967 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 cval->id = unitid;
969 cval->control = control;
970 cval->cmask = ctl_mask;
971 cval->val_type = audio_feature_info[control-1].type;
972 if (ctl_mask == 0)
973 cval->channels = 1; /* master channel */
974 else {
975 int i, c = 0;
976 for (i = 0; i < 16; i++)
977 if (ctl_mask & (1 << i))
978 c++;
979 cval->channels = c;
980 }
981
982 /* get min/max values */
983 get_min_max(cval, 0);
984
985 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
986 if (! kctl) {
987 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
988 kfree(cval);
989 return;
990 }
991 kctl->private_free = usb_mixer_elem_free;
992
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100993 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 mapped_name = len != 0;
995 if (! len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100996 len = snd_usb_copy_string_desc(state, nameid,
997 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 switch (control) {
1000 case USB_FEATURE_MUTE:
1001 case USB_FEATURE_VOLUME:
1002 /* determine the control name. the rule is:
1003 * - if a name id is given in descriptor, use it.
1004 * - if the connected input can be determined, then use the name
1005 * of terminal type.
1006 * - if the connected output can be determined, use it.
1007 * - otherwise, anonymous name.
1008 */
1009 if (! len) {
1010 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1011 if (! len)
1012 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1013 if (! len)
1014 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1015 "Feature %d", unitid);
1016 }
1017 /* determine the stream direction:
1018 * if the connected output is USB stream, then it's likely a
1019 * capture stream. otherwise it should be playback (hopefully :)
1020 */
1021 if (! mapped_name && ! (state->oterm.type >> 16)) {
1022 if ((state->oterm.type & 0xff00) == 0x0100) {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001023 len = append_ctl_name(kctl, " Capture");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 } else {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001025 len = append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001028 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1029 " Switch" : " Volume");
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +02001030 if (control == USB_FEATURE_VOLUME) {
1031 kctl->tlv.c = mixer_vol_tlv;
1032 kctl->vd[0].access |=
1033 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1034 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001035 check_mapped_dB(map, cval);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +02001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
1038
1039 default:
1040 if (! len)
1041 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1042 sizeof(kctl->id.name));
1043 break;
1044 }
1045
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001046 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001047 switch (state->chip->usb_id) {
1048 case USB_ID(0x0471, 0x0101):
1049 case USB_ID(0x0471, 0x0104):
1050 case USB_ID(0x0471, 0x0105):
1051 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001052 /* quirk for UDA1321/N101.
1053 * note that detection between firmware 2.1.1.7 (N101)
1054 * and later 2.1.1.21 is not very clear from datasheets.
1055 * I hope that the min value is -15360 for newer firmware --jk
1056 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001057 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1058 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001059 snd_printk(KERN_INFO
1060 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001061 cval->max = -256;
1062 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001063 break;
1064
1065 case USB_ID(0x046d, 0x09a4):
1066 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1067 snd_printk(KERN_INFO
1068 "set volume quirk for QuickCam E3500\n");
1069 cval->min = 6080;
1070 cval->max = 8768;
1071 cval->res = 192;
1072 }
1073 break;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1078 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001079 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082
1083
1084/*
1085 * parse a feature unit
1086 *
1087 * most of controlls are defined here.
1088 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001089static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001092 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 unsigned int master_bits, first_ch_bits;
1094 int err, csize;
1095
1096 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1097 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1098 return -EINVAL;
1099 }
1100
1101 /* parse the source unit */
1102 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1103 return err;
1104
1105 /* determine the input source type and name */
1106 if (check_input_term(state, ftr[4], &iterm) < 0)
1107 return -EINVAL;
1108
1109 channels = (ftr[0] - 7) / csize - 1;
1110
1111 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001112 /* master configuration quirks */
1113 switch (state->chip->usb_id) {
1114 case USB_ID(0x08bb, 0x2702):
1115 snd_printk(KERN_INFO
1116 "usbmixer: master volume quirk for PCM2702 chip\n");
1117 /* disable non-functional volume control */
1118 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1119 break;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (channels > 0)
1122 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1123 else
1124 first_ch_bits = 0;
1125 /* check all control types */
1126 for (i = 0; i < 10; i++) {
1127 unsigned int ch_bits = 0;
1128 for (j = 0; j < channels; j++) {
1129 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1130 if (mask & (1 << i))
1131 ch_bits |= (1 << j);
1132 }
1133 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1134 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1135 if (master_bits & (1 << i))
1136 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1137 }
1138
1139 return 0;
1140}
1141
1142
1143/*
1144 * Mixer Unit
1145 */
1146
1147/*
1148 * build a mixer unit control
1149 *
1150 * the callbacks are identical with feature unit.
1151 * input channel number (zero based) is given in control field instead.
1152 */
1153
Takashi Iwai86e07d32005-11-17 15:08:02 +01001154static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001156 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001158 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 unsigned int input_pins = desc[4];
1160 unsigned int num_outs = desc[5 + input_pins];
1161 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001162 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001163 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001165 map = find_map(state, unitid, 0);
1166 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return;
1168
Takashi Iwai561b2202005-09-09 14:22:34 +02001169 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (! cval)
1171 return;
1172
Clemens Ladisch84957a82005-04-29 16:23:13 +02001173 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 cval->id = unitid;
1175 cval->control = in_ch + 1; /* based on 1 */
1176 cval->val_type = USB_MIXER_S16;
1177 for (i = 0; i < num_outs; i++) {
1178 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1179 cval->cmask |= (1 << i);
1180 cval->channels++;
1181 }
1182 }
1183
1184 /* get min/max values */
1185 get_min_max(cval, 0);
1186
1187 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1188 if (! kctl) {
1189 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1190 kfree(cval);
1191 return;
1192 }
1193 kctl->private_free = usb_mixer_elem_free;
1194
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001195 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (! len)
1197 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1198 if (! len)
1199 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001200 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1203 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001204 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207
1208/*
1209 * parse a mixer unit
1210 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001211static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001213 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int input_pins, num_ins, num_outs;
1215 int pin, ich, err;
1216
1217 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1218 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1219 return -EINVAL;
1220 }
1221 /* no bmControls field (e.g. Maya44) -> ignore */
1222 if (desc[0] <= 10 + input_pins) {
1223 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1224 return 0;
1225 }
1226
1227 num_ins = 0;
1228 ich = 0;
1229 for (pin = 0; pin < input_pins; pin++) {
1230 err = parse_audio_unit(state, desc[5 + pin]);
1231 if (err < 0)
1232 return err;
1233 err = check_input_term(state, desc[5 + pin], &iterm);
1234 if (err < 0)
1235 return err;
1236 num_ins += iterm.channels;
1237 for (; ich < num_ins; ++ich) {
1238 int och, ich_has_controls = 0;
1239
1240 for (och = 0; och < num_outs; ++och) {
1241 if (check_matrix_bitmap(desc + 9 + input_pins,
1242 ich, och, num_outs)) {
1243 ich_has_controls = 1;
1244 break;
1245 }
1246 }
1247 if (ich_has_controls)
1248 build_mixer_unit_ctl(state, desc, pin, ich,
1249 unitid, &iterm);
1250 }
1251 }
1252 return 0;
1253}
1254
1255
1256/*
1257 * Processing Unit / Extension Unit
1258 */
1259
1260/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001261static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001263 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int err, val;
1265
1266 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001267 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ucontrol->value.integer.value[0] = cval->min;
1269 return 0;
1270 }
1271 if (err < 0)
1272 return err;
1273 val = get_relative_value(cval, val);
1274 ucontrol->value.integer.value[0] = val;
1275 return 0;
1276}
1277
1278/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001279static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001281 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int val, oval, err;
1283
1284 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1285 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001286 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return 0;
1288 return err;
1289 }
1290 val = ucontrol->value.integer.value[0];
1291 val = get_abs_value(cval, val);
1292 if (val != oval) {
1293 set_cur_ctl_value(cval, cval->control << 8, val);
1294 return 1;
1295 }
1296 return 0;
1297}
1298
1299/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001300static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1302 .name = "", /* will be filled later */
1303 .info = mixer_ctl_feature_info,
1304 .get = mixer_ctl_procunit_get,
1305 .put = mixer_ctl_procunit_put,
1306};
1307
1308
1309/*
1310 * predefined data for processing units
1311 */
1312struct procunit_value_info {
1313 int control;
1314 char *suffix;
1315 int val_type;
1316 int min_value;
1317};
1318
1319struct procunit_info {
1320 int type;
1321 char *name;
1322 struct procunit_value_info *values;
1323};
1324
1325static struct procunit_value_info updown_proc_info[] = {
1326 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1327 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1328 { 0 }
1329};
1330static struct procunit_value_info prologic_proc_info[] = {
1331 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1332 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1333 { 0 }
1334};
1335static struct procunit_value_info threed_enh_proc_info[] = {
1336 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1337 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1338 { 0 }
1339};
1340static struct procunit_value_info reverb_proc_info[] = {
1341 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1342 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1343 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1344 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1345 { 0 }
1346};
1347static struct procunit_value_info chorus_proc_info[] = {
1348 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1349 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1350 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1351 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1352 { 0 }
1353};
1354static struct procunit_value_info dcr_proc_info[] = {
1355 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1356 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1357 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1358 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1359 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1360 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1361 { 0 }
1362};
1363
1364static struct procunit_info procunits[] = {
1365 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1366 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1367 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1368 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1369 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1370 { USB_PROC_DCR, "DCR", dcr_proc_info },
1371 { 0 },
1372};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001373/*
1374 * predefined data for extension units
1375 */
1376static struct procunit_value_info clock_rate_xu_info[] = {
1377 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1378 { 0 }
1379};
1380static struct procunit_value_info clock_source_xu_info[] = {
1381 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1382 { 0 }
1383};
1384static struct procunit_value_info spdif_format_xu_info[] = {
1385 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1386 { 0 }
1387};
1388static struct procunit_value_info soft_limit_xu_info[] = {
1389 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1390 { 0 }
1391};
1392static struct procunit_info extunits[] = {
1393 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1394 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1395 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1396 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1397 { 0 }
1398};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/*
1400 * build a processing/extension unit
1401 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001402static 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 -07001403{
1404 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001405 struct usb_mixer_elem_info *cval;
1406 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int i, err, nameid, type, len;
1408 struct procunit_info *info;
1409 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001410 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 static struct procunit_value_info default_value_info[] = {
1412 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1413 { 0 }
1414 };
1415 static struct procunit_info default_info = {
1416 0, NULL, default_value_info
1417 };
1418
1419 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1420 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1421 return -EINVAL;
1422 }
1423
1424 for (i = 0; i < num_ins; i++) {
1425 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1426 return err;
1427 }
1428
1429 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 for (info = list; info && info->type; info++)
1431 if (info->type == type)
1432 break;
1433 if (! info || ! info->type)
1434 info = &default_info;
1435
1436 for (valinfo = info->values; valinfo->control; valinfo++) {
1437 /* FIXME: bitmap might be longer than 8bit */
1438 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1439 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001440 map = find_map(state, unitid, valinfo->control);
1441 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001443 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (! cval) {
1445 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1446 return -ENOMEM;
1447 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001448 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 cval->id = unitid;
1450 cval->control = valinfo->control;
1451 cval->val_type = valinfo->val_type;
1452 cval->channels = 1;
1453
1454 /* get min/max values */
1455 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1456 /* FIXME: hard-coded */
1457 cval->min = 1;
1458 cval->max = dsc[15];
1459 cval->res = 1;
1460 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001461 } else {
1462 if (type == USB_XU_CLOCK_RATE) {
1463 /* E-Mu USB 0404/0202/TrackerPre
1464 * samplerate control quirk
1465 */
1466 cval->min = 0;
1467 cval->max = 5;
1468 cval->res = 1;
1469 cval->initialized = 1;
1470 } else
1471 get_min_max(cval, valinfo->min_value);
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1475 if (! kctl) {
1476 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1477 kfree(cval);
1478 return -ENOMEM;
1479 }
1480 kctl->private_free = usb_mixer_elem_free;
1481
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001482 if (check_mapped_name(map, kctl->id.name,
1483 sizeof(kctl->id.name)))
1484 /* nothing */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 else if (info->name)
1486 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1487 else {
1488 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1489 len = 0;
1490 if (nameid)
1491 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1492 if (! len)
1493 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1494 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001495 append_ctl_name(kctl, " ");
1496 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1499 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001500 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return err;
1502 }
1503 return 0;
1504}
1505
1506
Takashi Iwai86e07d32005-11-17 15:08:02 +01001507static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1510}
1511
Takashi Iwai86e07d32005-11-17 15:08:02 +01001512static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001514 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517
1518/*
1519 * Selector Unit
1520 */
1521
1522/* info callback for selector unit
1523 * use an enumerator type for routing
1524 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001525static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001527 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 char **itemlist = (char **)kcontrol->private_value;
1529
Takashi Iwai5e246b82008-08-08 17:12:47 +02001530 if (snd_BUG_ON(!itemlist))
1531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1533 uinfo->count = 1;
1534 uinfo->value.enumerated.items = cval->max;
1535 if ((int)uinfo->value.enumerated.item >= cval->max)
1536 uinfo->value.enumerated.item = cval->max - 1;
1537 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1538 return 0;
1539}
1540
1541/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001542static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001544 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int val, err;
1546
1547 err = get_cur_ctl_value(cval, 0, &val);
1548 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001549 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 ucontrol->value.enumerated.item[0] = 0;
1551 return 0;
1552 }
1553 return err;
1554 }
1555 val = get_relative_value(cval, val);
1556 ucontrol->value.enumerated.item[0] = val;
1557 return 0;
1558}
1559
1560/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001561static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001563 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int val, oval, err;
1565
1566 err = get_cur_ctl_value(cval, 0, &oval);
1567 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001568 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570 return err;
1571 }
1572 val = ucontrol->value.enumerated.item[0];
1573 val = get_abs_value(cval, val);
1574 if (val != oval) {
1575 set_cur_ctl_value(cval, 0, val);
1576 return 1;
1577 }
1578 return 0;
1579}
1580
1581/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001582static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1584 .name = "", /* will be filled later */
1585 .info = mixer_ctl_selector_info,
1586 .get = mixer_ctl_selector_get,
1587 .put = mixer_ctl_selector_put,
1588};
1589
1590
1591/* private free callback.
1592 * free both private_data and private_value
1593 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001594static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 int i, num_ins = 0;
1597
1598 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001599 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 num_ins = cval->max;
1601 kfree(cval);
1602 kctl->private_data = NULL;
1603 }
1604 if (kctl->private_value) {
1605 char **itemlist = (char **)kctl->private_value;
1606 for (i = 0; i < num_ins; i++)
1607 kfree(itemlist[i]);
1608 kfree(itemlist);
1609 kctl->private_value = 0;
1610 }
1611}
1612
1613/*
1614 * parse a selector unit
1615 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001616static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 unsigned int num_ins = desc[4];
1619 unsigned int i, nameid, len;
1620 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001621 struct usb_mixer_elem_info *cval;
1622 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001623 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 char **namelist;
1625
Russ Cox38977e92007-08-06 15:37:58 +02001626 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1628 return -EINVAL;
1629 }
1630
1631 for (i = 0; i < num_ins; i++) {
1632 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1633 return err;
1634 }
1635
1636 if (num_ins == 1) /* only one ? nonsense! */
1637 return 0;
1638
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001639 map = find_map(state, unitid, 0);
1640 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return 0;
1642
Takashi Iwai561b2202005-09-09 14:22:34 +02001643 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (! cval) {
1645 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1646 return -ENOMEM;
1647 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001648 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 cval->id = unitid;
1650 cval->val_type = USB_MIXER_U8;
1651 cval->channels = 1;
1652 cval->min = 1;
1653 cval->max = num_ins;
1654 cval->res = 1;
1655 cval->initialized = 1;
1656
1657 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1658 if (! namelist) {
1659 snd_printk(KERN_ERR "cannot malloc\n");
1660 kfree(cval);
1661 return -ENOMEM;
1662 }
1663#define MAX_ITEM_NAME_LEN 64
1664 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001665 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 len = 0;
1667 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1668 if (! namelist[i]) {
1669 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001670 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 kfree(namelist[i]);
1672 kfree(namelist);
1673 kfree(cval);
1674 return -ENOMEM;
1675 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001676 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1677 MAX_ITEM_NAME_LEN);
1678 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1680 if (! len)
1681 sprintf(namelist[i], "Input %d", i);
1682 }
1683
1684 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1685 if (! kctl) {
1686 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001687 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 kfree(cval);
1689 return -ENOMEM;
1690 }
1691 kctl->private_value = (unsigned long)namelist;
1692 kctl->private_free = usb_mixer_selector_elem_free;
1693
1694 nameid = desc[desc[0] - 1];
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001695 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (len)
1697 ;
1698 else if (nameid)
1699 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1700 else {
1701 len = get_term_name(state, &state->oterm,
1702 kctl->id.name, sizeof(kctl->id.name), 0);
1703 if (! len)
1704 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1705
1706 if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02001707 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02001709 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1713 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001714 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return err;
1716
1717 return 0;
1718}
1719
1720
1721/*
1722 * parse an audio unit recursively
1723 */
1724
Takashi Iwai86e07d32005-11-17 15:08:02 +01001725static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 unsigned char *p1;
1728
1729 if (test_and_set_bit(unitid, state->unitbitmap))
1730 return 0; /* the unit already visited */
1731
1732 p1 = find_audio_control_unit(state, unitid);
1733 if (!p1) {
1734 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1735 return -EINVAL;
1736 }
1737
1738 switch (p1[2]) {
1739 case INPUT_TERMINAL:
1740 return 0; /* NOP */
1741 case MIXER_UNIT:
1742 return parse_audio_mixer_unit(state, unitid, p1);
1743 case SELECTOR_UNIT:
1744 return parse_audio_selector_unit(state, unitid, p1);
1745 case FEATURE_UNIT:
1746 return parse_audio_feature_unit(state, unitid, p1);
1747 case PROCESSING_UNIT:
1748 return parse_audio_processing_unit(state, unitid, p1);
1749 case EXTENSION_UNIT:
1750 return parse_audio_extension_unit(state, unitid, p1);
1751 default:
1752 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1753 return -EINVAL;
1754 }
1755}
1756
Clemens Ladisch84957a82005-04-29 16:23:13 +02001757static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1758{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001759 kfree(mixer->id_elems);
1760 if (mixer->urb) {
1761 kfree(mixer->urb->transfer_buffer);
1762 usb_free_urb(mixer->urb);
1763 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001764 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001765 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001766 kfree(mixer);
1767}
1768
Takashi Iwai86e07d32005-11-17 15:08:02 +01001769static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001770{
1771 struct usb_mixer_interface *mixer = device->device_data;
1772 snd_usb_mixer_free(mixer);
1773 return 0;
1774}
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776/*
1777 * create mixer controls
1778 *
1779 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1780 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001781static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001784 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int err;
1786 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001787 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Clemens Ladisch84957a82005-04-29 16:23:13 +02001789 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001791 state.chip = mixer->chip;
1792 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 state.buffer = hostif->extra;
1794 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001797 for (map = usbmix_ctl_maps; map->id; map++) {
1798 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001800 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001801 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 break;
1803 }
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 desc = NULL;
1807 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1808 if (desc[0] < 9)
1809 continue; /* invalid descriptor? */
1810 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1811 state.oterm.id = desc[3];
1812 state.oterm.type = combine_word(&desc[4]);
1813 state.oterm.name = desc[8];
1814 err = parse_audio_unit(&state, desc[7]);
1815 if (err < 0)
1816 return err;
1817 }
1818 return 0;
1819}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001820
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001821static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1822 int unitid)
1823{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001824 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001825
1826 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1827 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1828 info->elem_id);
1829}
1830
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001831static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1832 int unitid,
1833 struct usb_mixer_elem_info *cval)
1834{
1835 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1836 "S8", "U8", "S16", "U16"};
1837 snd_iprintf(buffer, " Unit: %i\n", unitid);
1838 if (cval->elem_id)
1839 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1840 cval->elem_id->name, cval->elem_id->index);
1841 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1842 "channels=%i, type=\"%s\"\n", cval->id,
1843 cval->control, cval->cmask, cval->channels,
1844 val_types[cval->val_type]);
1845 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1846 cval->min, cval->max, cval->dBmin, cval->dBmax);
1847}
1848
1849static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1850 struct snd_info_buffer *buffer)
1851{
1852 struct snd_usb_audio *chip = entry->private_data;
1853 struct usb_mixer_interface *mixer;
1854 struct usb_mixer_elem_info *cval;
1855 int unitid;
1856
1857 list_for_each_entry(mixer, &chip->mixer_list, list) {
1858 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01001859 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1860 chip->usb_id, mixer->ctrlif,
1861 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001862 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1863 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1864 for (cval = mixer->id_elems[unitid]; cval;
1865 cval = cval->next_id_elem)
1866 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1867 }
1868 }
1869}
1870
Clemens Ladischb259b102005-04-29 16:29:28 +02001871static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1872 int unitid)
1873{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001874 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001875 return;
1876 /* unit ids specific to Extigy/Audigy 2 NX: */
1877 switch (unitid) {
1878 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001879 mixer->rc_urb->dev = mixer->chip->dev;
1880 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001881 break;
1882 case 4: /* digital in jack */
1883 case 7: /* line in jacks */
1884 case 19: /* speaker out jacks */
1885 case 20: /* headphones out jack */
1886 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001887 /* live24ext: 4 = line-in jack */
1888 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001889 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1890 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001891 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1892 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001893 default:
1894 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1895 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001896 }
1897}
1898
David Howells7d12e782006-10-05 14:55:46 +01001899static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001900{
1901 struct usb_mixer_interface *mixer = urb->context;
1902
1903 if (urb->status == 0) {
1904 u8 *buf = urb->transfer_buffer;
1905 int i;
1906
1907 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1908 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1909 buf[0], buf[1]);
1910 /* ignore any notifications not from the control interface */
1911 if ((buf[0] & 0x0f) != 0)
1912 continue;
1913 if (!(buf[0] & 0x40))
1914 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001915 else
1916 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001917 }
1918 }
1919 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1920 urb->dev = mixer->chip->dev;
1921 usb_submit_urb(urb, GFP_ATOMIC);
1922 }
1923}
1924
1925/* create the handler for the optional status interrupt endpoint */
1926static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1927{
1928 struct usb_host_interface *hostif;
1929 struct usb_endpoint_descriptor *ep;
1930 void *transfer_buffer;
1931 int buffer_length;
1932 unsigned int epnum;
1933
1934 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1935 /* we need one interrupt input endpoint */
1936 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1937 return 0;
1938 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001939 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001940 return 0;
1941
Julia Lawall42a6e662008-12-29 11:23:02 +01001942 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001943 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1944 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1945 if (!transfer_buffer)
1946 return -ENOMEM;
1947 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1948 if (!mixer->urb) {
1949 kfree(transfer_buffer);
1950 return -ENOMEM;
1951 }
1952 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1953 usb_rcvintpipe(mixer->chip->dev, epnum),
1954 transfer_buffer, buffer_length,
1955 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1956 usb_submit_urb(mixer->urb, GFP_KERNEL);
1957 return 0;
1958}
1959
David Howells7d12e782006-10-05 14:55:46 +01001960static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001961{
1962 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001963 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001964 u32 code;
1965
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001966 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001967 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001968
1969 code = mixer->rc_buffer[rc->offset];
1970 if (rc->length == 2)
1971 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1972
Clemens Ladischb259b102005-04-29 16:29:28 +02001973 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001974 if (code == rc->mute_code)
1975 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001976 mixer->rc_code = code;
1977 wmb();
1978 wake_up(&mixer->rc_waitq);
1979}
1980
Takashi Iwai86e07d32005-11-17 15:08:02 +01001981static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001982 long count, loff_t *offset)
1983{
1984 struct usb_mixer_interface *mixer = hw->private_data;
1985 int err;
1986 u32 rc_code;
1987
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001988 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001989 return -EINVAL;
1990 err = wait_event_interruptible(mixer->rc_waitq,
1991 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1992 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001993 if (count == 1)
1994 err = put_user(rc_code, buf);
1995 else
1996 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001997 }
1998 return err < 0 ? err : count;
1999}
2000
Takashi Iwai86e07d32005-11-17 15:08:02 +01002001static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02002002 poll_table *wait)
2003{
2004 struct usb_mixer_interface *mixer = hw->private_data;
2005
2006 poll_wait(file, &mixer->rc_waitq, wait);
2007 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
2008}
2009
2010static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
2011{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002012 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002013 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02002014
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002015 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
2016 if (rc_configs[i].usb_id == mixer->chip->usb_id)
2017 break;
2018 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02002019 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002020 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02002021
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002022 len = mixer->rc_cfg->packet_length;
2023
Clemens Ladischb259b102005-04-29 16:29:28 +02002024 init_waitqueue_head(&mixer->rc_waitq);
2025 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
2026 if (err < 0)
2027 return err;
2028 snprintf(hwdep->name, sizeof(hwdep->name),
2029 "%s remote control", mixer->chip->card->shortname);
2030 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
2031 hwdep->private_data = mixer;
2032 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02002033 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01002034 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02002035
2036 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
2037 if (!mixer->rc_urb)
2038 return -ENOMEM;
2039 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
2040 if (!mixer->rc_setup_packet) {
2041 usb_free_urb(mixer->rc_urb);
2042 mixer->rc_urb = NULL;
2043 return -ENOMEM;
2044 }
2045 mixer->rc_setup_packet->bRequestType =
2046 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2047 mixer->rc_setup_packet->bRequest = GET_MEM;
2048 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2049 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2050 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2051 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2052 usb_rcvctrlpipe(mixer->chip->dev, 0),
2053 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2054 snd_usb_soundblaster_remote_complete, mixer);
2055 return 0;
2056}
2057
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002058#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002059
Takashi Iwai86e07d32005-11-17 15:08:02 +01002060static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002061{
2062 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2063 int index = kcontrol->private_value;
2064
2065 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2066 return 0;
2067}
2068
Takashi Iwai86e07d32005-11-17 15:08:02 +01002069static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002070{
2071 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2072 int index = kcontrol->private_value;
2073 int value = ucontrol->value.integer.value[0];
2074 int err, changed;
2075
2076 if (value > 1)
2077 return -EINVAL;
2078 changed = value != mixer->audigy2nx_leds[index];
2079 err = snd_usb_ctl_msg(mixer->chip->dev,
2080 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2081 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2082 value, index + 2, NULL, 0, 100);
2083 if (err < 0)
2084 return err;
2085 mixer->audigy2nx_leds[index] = value;
2086 return changed;
2087}
2088
Takashi Iwai86e07d32005-11-17 15:08:02 +01002089static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002090 {
2091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2092 .name = "CMSS LED Switch",
2093 .info = snd_audigy2nx_led_info,
2094 .get = snd_audigy2nx_led_get,
2095 .put = snd_audigy2nx_led_put,
2096 .private_value = 0,
2097 },
2098 {
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = "Power LED Switch",
2101 .info = snd_audigy2nx_led_info,
2102 .get = snd_audigy2nx_led_get,
2103 .put = snd_audigy2nx_led_put,
2104 .private_value = 1,
2105 },
2106 {
2107 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2108 .name = "Dolby Digital LED Switch",
2109 .info = snd_audigy2nx_led_info,
2110 .get = snd_audigy2nx_led_get,
2111 .put = snd_audigy2nx_led_put,
2112 .private_value = 2,
2113 },
2114};
2115
2116static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2117{
2118 int i, err;
2119
2120 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01002121 if (i > 1 && /* Live24ext has 2 LEDs only */
2122 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2123 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002124 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002125 err = snd_ctl_add(mixer->chip->card,
2126 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2127 if (err < 0)
2128 return err;
2129 }
2130 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2131 return 0;
2132}
2133
Takashi Iwai86e07d32005-11-17 15:08:02 +01002134static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2135 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02002136{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002137 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02002138 int unitid;
2139 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002140 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02002141 {4, "dig in "},
2142 {7, "line in"},
2143 {19, "spk out"},
2144 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002145 {-1, NULL}
2146 }, jacks_live24ext[] = {
2147 {4, "line in"}, /* &1=Line, &2=Mic*/
2148 {3, "hph out"}, /* headphones */
2149 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2150 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002151 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002152 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002153 struct usb_mixer_interface *mixer = entry->private_data;
2154 int i, err;
2155 u8 buf[3];
2156
2157 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002158 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2159 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002160 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2161 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002162 jacks = jacks_live24ext;
2163 else
2164 return;
2165
2166 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002167 snd_iprintf(buffer, "%s: ", jacks[i].name);
2168 err = snd_usb_ctl_msg(mixer->chip->dev,
2169 usb_rcvctrlpipe(mixer->chip->dev, 0),
2170 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2171 USB_RECIP_INTERFACE, 0,
2172 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002173 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002174 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2175 else
2176 snd_iprintf(buffer, "?\n");
2177 }
2178}
2179
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002180static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2182{
2183 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2184
2185 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2186 return 0;
2187}
2188
2189static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2191{
2192 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2193 u8 old_status, new_status;
2194 int err, changed;
2195
2196 old_status = mixer->xonar_u1_status;
2197 if (ucontrol->value.integer.value[0])
2198 new_status = old_status | 0x02;
2199 else
2200 new_status = old_status & ~0x02;
2201 changed = new_status != old_status;
2202 err = snd_usb_ctl_msg(mixer->chip->dev,
2203 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2204 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2205 50, 0, &new_status, 1, 100);
2206 if (err < 0)
2207 return err;
2208 mixer->xonar_u1_status = new_status;
2209 return changed;
2210}
2211
2212static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2214 .name = "Digital Playback Switch",
2215 .info = snd_ctl_boolean_mono_info,
2216 .get = snd_xonar_u1_switch_get,
2217 .put = snd_xonar_u1_switch_put,
2218};
2219
2220static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2221{
2222 int err;
2223
2224 err = snd_ctl_add(mixer->chip->card,
2225 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2226 if (err < 0)
2227 return err;
2228 mixer->xonar_u1_status = 0x05;
2229 return 0;
2230}
2231
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002232void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002233 unsigned char samplerate_id)
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002234{
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002235 struct usb_mixer_interface *mixer;
2236 struct usb_mixer_elem_info *cval;
2237 int unitid = 12; /* SamleRate ExtensionUnit ID */
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002238
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002239 list_for_each_entry(mixer, &chip->mixer_list, list) {
2240 cval = mixer->id_elems[unitid];
2241 if (cval) {
2242 set_cur_ctl_value(cval, cval->control << 8,
2243 samplerate_id);
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002244 snd_usb_mixer_notify_id(mixer, unitid);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002245 }
2246 break;
2247 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002248}
2249
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002250int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2251 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002252{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002253 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002254 .dev_free = snd_usb_mixer_dev_free
2255 };
2256 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002257 struct snd_info_entry *entry;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002258 int err;
2259
2260 strcpy(chip->card->mixername, "USB Mixer");
2261
Takashi Iwai561b2202005-09-09 14:22:34 +02002262 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002263 if (!mixer)
2264 return -ENOMEM;
2265 mixer->chip = chip;
2266 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002267 mixer->ignore_ctl_error = ignore_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002268 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2269 if (!mixer->id_elems) {
2270 kfree(mixer);
2271 return -ENOMEM;
2272 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002273
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002274 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002275 (err = snd_usb_mixer_status_create(mixer)) < 0)
2276 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002277
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002278 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2279 goto _error;
2280
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002281 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002282 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2283 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002284 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2285 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002286 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002287 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002288 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002289 }
2290
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002291 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2292 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2293 err = snd_xonar_u1_controls_create(mixer);
2294 if (err < 0)
2295 goto _error;
2296 }
2297
Clemens Ladisch84957a82005-04-29 16:23:13 +02002298 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002299 if (err < 0)
2300 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002301
2302 if (list_empty(&chip->mixer_list) &&
2303 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2304 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2305
Clemens Ladisch84957a82005-04-29 16:23:13 +02002306 list_add(&mixer->list, &chip->mixer_list);
2307 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002308
2309_error:
2310 snd_usb_mixer_free(mixer);
2311 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002312}
2313
2314void snd_usb_mixer_disconnect(struct list_head *p)
2315{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002316 struct usb_mixer_interface *mixer;
2317
2318 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002319 usb_kill_urb(mixer->urb);
2320 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002321}