blob: c72ad0c8258146df0a3ce9a4992dbce38b3a9541 [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
Clemens Ladisch84957a82005-04-29 16:23:13 +020072struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010073 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020074 unsigned int ctrlif;
75 struct list_head list;
76 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020077 struct urb *urb;
Takashi Iwai86e07d32005-11-17 15:08:02 +010078 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
Clemens Ladischb259b102005-04-29 16:29:28 +020079
80 /* Sound Blaster remote control stuff */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020081 const struct rc_config *rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +020082 u32 rc_code;
83 wait_queue_head_t rc_waitq;
84 struct urb *rc_urb;
85 struct usb_ctrlrequest *rc_setup_packet;
86 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020087
88 u8 audigy2nx_leds[3];
Clemens Ladisch468b8fd2009-07-13 11:39:29 +020089 u8 xonar_u1_status;
Clemens Ladisch84957a82005-04-29 16:23:13 +020090};
91
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct usb_audio_term {
94 int id;
95 int type;
96 int channels;
97 unsigned int chconfig;
98 int name;
99};
100
101struct usbmix_name_map;
102
Takashi Iwai86e07d32005-11-17 15:08:02 +0100103struct mixer_build {
104 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200105 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned char *buffer;
107 unsigned int buflen;
Clemens Ladisch707e6072005-04-29 09:56:17 +0200108 DECLARE_BITMAP(unitbitmap, 256);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100109 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200111 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Takashi Iwai641b4872009-01-15 17:05:24 +0100114#define MAX_CHANNELS 10 /* max logical channels */
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200117 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100118 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
119 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int id;
121 unsigned int control; /* CS or ICN (high byte) */
122 unsigned int cmask; /* channel mask bitmap: 0 = master */
123 int channels;
124 int val_type;
125 int min, max, res;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100126 int dBmin, dBmax;
Takashi Iwai641b4872009-01-15 17:05:24 +0100127 int cached;
128 int cache_val[MAX_CHANNELS];
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200129 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132
133enum {
134 USB_FEATURE_NONE = 0,
135 USB_FEATURE_MUTE = 1,
136 USB_FEATURE_VOLUME,
137 USB_FEATURE_BASS,
138 USB_FEATURE_MID,
139 USB_FEATURE_TREBLE,
140 USB_FEATURE_GEQ,
141 USB_FEATURE_AGC,
142 USB_FEATURE_DELAY,
143 USB_FEATURE_BASSBOOST,
144 USB_FEATURE_LOUDNESS
145};
146
147enum {
148 USB_MIXER_BOOLEAN,
149 USB_MIXER_INV_BOOLEAN,
150 USB_MIXER_S8,
151 USB_MIXER_U8,
152 USB_MIXER_S16,
153 USB_MIXER_U16,
154};
155
156enum {
157 USB_PROC_UPDOWN = 1,
158 USB_PROC_UPDOWN_SWITCH = 1,
159 USB_PROC_UPDOWN_MODE_SEL = 2,
160
161 USB_PROC_PROLOGIC = 2,
162 USB_PROC_PROLOGIC_SWITCH = 1,
163 USB_PROC_PROLOGIC_MODE_SEL = 2,
164
165 USB_PROC_3DENH = 3,
166 USB_PROC_3DENH_SWITCH = 1,
167 USB_PROC_3DENH_SPACE = 2,
168
169 USB_PROC_REVERB = 4,
170 USB_PROC_REVERB_SWITCH = 1,
171 USB_PROC_REVERB_LEVEL = 2,
172 USB_PROC_REVERB_TIME = 3,
173 USB_PROC_REVERB_DELAY = 4,
174
175 USB_PROC_CHORUS = 5,
176 USB_PROC_CHORUS_SWITCH = 1,
177 USB_PROC_CHORUS_LEVEL = 2,
178 USB_PROC_CHORUS_RATE = 3,
179 USB_PROC_CHORUS_DEPTH = 4,
180
181 USB_PROC_DCR = 6,
182 USB_PROC_DCR_SWITCH = 1,
183 USB_PROC_DCR_RATIO = 2,
184 USB_PROC_DCR_MAX_AMP = 3,
185 USB_PROC_DCR_THRESHOLD = 4,
186 USB_PROC_DCR_ATTACK = 5,
187 USB_PROC_DCR_RELEASE = 6,
188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/*
192 * manual mapping of mixer names
193 * if the mixer topology is too complicated and the parsed names are
194 * ambiguous, add the entries in usbmixer_maps.c.
195 */
196#include "usbmixer_maps.c"
197
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100198static const struct usbmix_name_map *
199find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100201 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100203 if (!p)
204 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100207 if (p->id == unitid &&
208 (!control || !p->control || control == p->control))
209 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100211 return NULL;
212}
213
214/* get the mapped name if the unit matches */
215static int
216check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
217{
218 if (!p || !p->name)
219 return 0;
220
221 buflen--;
222 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100226static inline int
227check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100229 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100231 return 1;
232}
233
234/* dB mapping */
235static inline void check_mapped_dB(const struct usbmix_name_map *p,
236 struct usb_mixer_elem_info *cval)
237{
238 if (p && p->dB) {
239 cval->dBmin = p->dB->min;
240 cval->dBmax = p->dB->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200244/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100245static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200246 int index, char *buf, int buflen)
247{
248 const struct usbmix_selector_map *p;
249
250 if (! state->selector_map)
251 return 0;
252 for (p = state->selector_map; p->id; p++) {
253 if (p->id == unitid && index < p->count)
254 return strlcpy(buf, p->names[index], buflen);
255 }
256 return 0;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * find an audio control unit with the given unit id
261 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100262static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 unsigned char *p;
265
266 p = NULL;
267 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
268 USB_DT_CS_INTERFACE)) != NULL) {
269 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
270 return p;
271 }
272 return NULL;
273}
274
275
276/*
277 * copy a string with the given id
278 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100279static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
282 buf[len] = 0;
283 return len;
284}
285
286/*
287 * convert from the byte/word on usb descriptor to the zero-based integer
288 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100289static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 switch (cval->val_type) {
292 case USB_MIXER_BOOLEAN:
293 return !!val;
294 case USB_MIXER_INV_BOOLEAN:
295 return !val;
296 case USB_MIXER_U8:
297 val &= 0xff;
298 break;
299 case USB_MIXER_S8:
300 val &= 0xff;
301 if (val >= 0x80)
302 val -= 0x100;
303 break;
304 case USB_MIXER_U16:
305 val &= 0xffff;
306 break;
307 case USB_MIXER_S16:
308 val &= 0xffff;
309 if (val >= 0x8000)
310 val -= 0x10000;
311 break;
312 }
313 return val;
314}
315
316/*
317 * convert from the zero-based int to the byte/word for usb descriptor
318 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100319static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 switch (cval->val_type) {
322 case USB_MIXER_BOOLEAN:
323 return !!val;
324 case USB_MIXER_INV_BOOLEAN:
325 return !val;
326 case USB_MIXER_S8:
327 case USB_MIXER_U8:
328 return val & 0xff;
329 case USB_MIXER_S16:
330 case USB_MIXER_U16:
331 return val & 0xffff;
332 }
333 return 0; /* not reached */
334}
335
Takashi Iwai86e07d32005-11-17 15:08:02 +0100336static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 if (! cval->res)
339 cval->res = 1;
340 if (val < cval->min)
341 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200342 else if (val >= cval->max)
343 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 else
345 return (val - cval->min) / cval->res;
346}
347
Takashi Iwai86e07d32005-11-17 15:08:02 +0100348static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 if (val < 0)
351 return cval->min;
352 if (! cval->res)
353 cval->res = 1;
354 val *= cval->res;
355 val += cval->min;
356 if (val > cval->max)
357 return cval->max;
358 return val;
359}
360
361
362/*
363 * retrieve a mixer value
364 */
365
Takashi Iwai86e07d32005-11-17 15:08:02 +0100366static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 unsigned char buf[2];
369 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
370 int timeout = 10;
371
372 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200373 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
374 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 request,
376 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200377 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200378 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
380 return 0;
381 }
382 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200383 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
384 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -EINVAL;
386}
387
Takashi Iwai86e07d32005-11-17 15:08:02 +0100388static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 return get_ctl_value(cval, GET_CUR, validx, value);
391}
392
393/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100394static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
395 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
398}
399
Takashi Iwai641b4872009-01-15 17:05:24 +0100400static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
401 int channel, int index, int *value)
402{
403 int err;
404
405 if (cval->cached & (1 << channel)) {
406 *value = cval->cache_val[index];
407 return 0;
408 }
409 err = get_cur_mix_raw(cval, channel, value);
410 if (err < 0) {
411 if (!cval->mixer->ignore_ctl_error)
412 snd_printd(KERN_ERR "cannot get current value for "
413 "control %d ch %d: err = %d\n",
414 cval->control, channel, err);
415 return err;
416 }
417 cval->cached |= 1 << channel;
418 cval->cache_val[index] = *value;
419 return 0;
420}
421
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * set a mixer value
425 */
426
Takashi Iwai86e07d32005-11-17 15:08:02 +0100427static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 unsigned char buf[2];
430 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
431 int timeout = 10;
432
433 value_set = convert_bytes_value(cval, value_set);
434 buf[0] = value_set & 0xff;
435 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530436 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200437 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
438 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 request,
440 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200441 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 buf, val_len, 100) >= 0)
443 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200444 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
445 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -EINVAL;
447}
448
Takashi Iwai86e07d32005-11-17 15:08:02 +0100449static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 return set_ctl_value(cval, SET_CUR, validx, value);
452}
453
Takashi Iwai641b4872009-01-15 17:05:24 +0100454static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
455 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Takashi Iwai641b4872009-01-15 17:05:24 +0100457 int err;
458 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
459 value);
460 if (err < 0)
461 return err;
462 cval->cached |= 1 << channel;
463 cval->cache_val[index] = value;
464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200467/*
468 * TLV callback for mixer volume controls
469 */
470static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
471 unsigned int size, unsigned int __user *_tlv)
472{
473 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200474 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200475
476 if (size < sizeof(scale))
477 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100478 scale[2] = cval->dBmin;
479 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200480 if (copy_to_user(_tlv, scale, sizeof(scale)))
481 return -EFAULT;
482 return 0;
483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485/*
486 * parser routines begin here...
487 */
488
Takashi Iwai86e07d32005-11-17 15:08:02 +0100489static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491
492/*
493 * check if the input/output channel routing is enabled on the given bitmap.
494 * used for mixer unit parser
495 */
496static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
497{
498 int idx = ich * num_outs + och;
499 return bmap[idx >> 3] & (0x80 >> (idx & 7));
500}
501
502
503/*
504 * add an alsa control element
505 * search and increment the index until an empty slot is found.
506 *
507 * if failed, give up and free the control instance.
508 */
509
Takashi Iwai86e07d32005-11-17 15:08:02 +0100510static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100512 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200514
515 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200517 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200521 cval->elem_id = &kctl->id;
522 cval->next_id_elem = state->mixer->id_elems[cval->id];
523 state->mixer->id_elems[cval->id] = cval;
524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527
528/*
529 * get a terminal name string
530 */
531
532static struct iterm_name_combo {
533 int type;
534 char *name;
535} iterm_names[] = {
536 { 0x0300, "Output" },
537 { 0x0301, "Speaker" },
538 { 0x0302, "Headphone" },
539 { 0x0303, "HMD Audio" },
540 { 0x0304, "Desktop Speaker" },
541 { 0x0305, "Room Speaker" },
542 { 0x0306, "Com Speaker" },
543 { 0x0307, "LFE" },
544 { 0x0600, "External In" },
545 { 0x0601, "Analog In" },
546 { 0x0602, "Digital In" },
547 { 0x0603, "Line" },
548 { 0x0604, "Legacy In" },
549 { 0x0605, "IEC958 In" },
550 { 0x0606, "1394 DA Stream" },
551 { 0x0607, "1394 DV Stream" },
552 { 0x0700, "Embedded" },
553 { 0x0701, "Noise Source" },
554 { 0x0702, "Equalization Noise" },
555 { 0x0703, "CD" },
556 { 0x0704, "DAT" },
557 { 0x0705, "DCC" },
558 { 0x0706, "MiniDisk" },
559 { 0x0707, "Analog Tape" },
560 { 0x0708, "Phonograph" },
561 { 0x0709, "VCR Audio" },
562 { 0x070a, "Video Disk Audio" },
563 { 0x070b, "DVD Audio" },
564 { 0x070c, "TV Tuner Audio" },
565 { 0x070d, "Satellite Rec Audio" },
566 { 0x070e, "Cable Tuner Audio" },
567 { 0x070f, "DSS Audio" },
568 { 0x0710, "Radio Receiver" },
569 { 0x0711, "Radio Transmitter" },
570 { 0x0712, "Multi-Track Recorder" },
571 { 0x0713, "Synthesizer" },
572 { 0 },
573};
574
Takashi Iwai86e07d32005-11-17 15:08:02 +0100575static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 unsigned char *name, int maxlen, int term_only)
577{
578 struct iterm_name_combo *names;
579
580 if (iterm->name)
581 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
582
583 /* virtual type - not a real terminal */
584 if (iterm->type >> 16) {
585 if (term_only)
586 return 0;
587 switch (iterm->type >> 16) {
588 case SELECTOR_UNIT:
589 strcpy(name, "Selector"); return 8;
590 case PROCESSING_UNIT:
591 strcpy(name, "Process Unit"); return 12;
592 case EXTENSION_UNIT:
593 strcpy(name, "Ext Unit"); return 8;
594 case MIXER_UNIT:
595 strcpy(name, "Mixer"); return 5;
596 default:
597 return sprintf(name, "Unit %d", iterm->id);
598 }
599 }
600
601 switch (iterm->type & 0xff00) {
602 case 0x0100:
603 strcpy(name, "PCM"); return 3;
604 case 0x0200:
605 strcpy(name, "Mic"); return 3;
606 case 0x0400:
607 strcpy(name, "Headset"); return 7;
608 case 0x0500:
609 strcpy(name, "Phone"); return 5;
610 }
611
612 for (names = iterm_names; names->type; names++)
613 if (names->type == iterm->type) {
614 strcpy(name, names->name);
615 return strlen(names->name);
616 }
617 return 0;
618}
619
620
621/*
622 * parse the source unit recursively until it reaches to a terminal
623 * or a branched unit.
624 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100625static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned char *p1;
628
629 memset(term, 0, sizeof(*term));
630 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
631 term->id = id;
632 switch (p1[2]) {
633 case INPUT_TERMINAL:
634 term->type = combine_word(p1 + 4);
635 term->channels = p1[7];
636 term->chconfig = combine_word(p1 + 8);
637 term->name = p1[11];
638 return 0;
639 case FEATURE_UNIT:
640 id = p1[4];
641 break; /* continue to parse */
642 case MIXER_UNIT:
643 term->type = p1[2] << 16; /* virtual type */
644 term->channels = p1[5 + p1[4]];
645 term->chconfig = combine_word(p1 + 6 + p1[4]);
646 term->name = p1[p1[0] - 1];
647 return 0;
648 case SELECTOR_UNIT:
649 /* call recursively to retrieve the channel info */
650 if (check_input_term(state, p1[5], term) < 0)
651 return -ENODEV;
652 term->type = p1[2] << 16; /* virtual type */
653 term->id = id;
654 term->name = p1[9 + p1[0] - 1];
655 return 0;
656 case PROCESSING_UNIT:
657 case EXTENSION_UNIT:
658 if (p1[6] == 1) {
659 id = p1[7];
660 break; /* continue to parse */
661 }
662 term->type = p1[2] << 16; /* virtual type */
663 term->channels = p1[7 + p1[6]];
664 term->chconfig = combine_word(p1 + 8 + p1[6]);
665 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
666 return 0;
667 default:
668 return -ENODEV;
669 }
670 }
671 return -ENODEV;
672}
673
674
675/*
676 * Feature Unit
677 */
678
679/* feature unit control information */
680struct usb_feature_control_info {
681 const char *name;
682 unsigned int type; /* control type (mute, volume, etc.) */
683};
684
685static struct usb_feature_control_info audio_feature_info[] = {
686 { "Mute", USB_MIXER_INV_BOOLEAN },
687 { "Volume", USB_MIXER_S16 },
688 { "Tone Control - Bass", USB_MIXER_S8 },
689 { "Tone Control - Mid", USB_MIXER_S8 },
690 { "Tone Control - Treble", USB_MIXER_S8 },
691 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
692 { "Auto Gain Control", USB_MIXER_BOOLEAN },
693 { "Delay Control", USB_MIXER_U16 },
694 { "Bass Boost", USB_MIXER_BOOLEAN },
695 { "Loudness", USB_MIXER_BOOLEAN },
696};
697
698
699/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100700static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jesper Juhl4d572772005-05-30 17:30:32 +0200702 kfree(kctl->private_data);
703 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706
707/*
708 * interface to ALSA control for feature/mixer units
709 */
710
711/*
712 * retrieve the minimum and maximum values for the specified control
713 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100714static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 /* for failsafe */
717 cval->min = default_min;
718 cval->max = cval->min + 1;
719 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100720 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (cval->val_type == USB_MIXER_BOOLEAN ||
723 cval->val_type == USB_MIXER_INV_BOOLEAN) {
724 cval->initialized = 1;
725 } else {
726 int minchn = 0;
727 if (cval->cmask) {
728 int i;
729 for (i = 0; i < MAX_CHANNELS; i++)
730 if (cval->cmask & (1 << i)) {
731 minchn = i + 1;
732 break;
733 }
734 }
735 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
736 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200737 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
738 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EINVAL;
740 }
741 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
742 cval->res = 1;
743 } else {
744 int last_valid_res = cval->res;
745
746 while (cval->res > 1) {
747 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
748 break;
749 cval->res /= 2;
750 }
751 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
752 cval->res = last_valid_res;
753 }
754 if (cval->res == 0)
755 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200756
757 /* Additional checks for the proper resolution
758 *
759 * Some devices report smaller resolutions than actually
760 * reacting. They don't return errors but simply clip
761 * to the lower aligned value.
762 */
763 if (cval->min + cval->res < cval->max) {
764 int last_valid_res = cval->res;
765 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100766 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200767 for (;;) {
768 test = saved;
769 if (test < cval->max)
770 test += cval->res;
771 else
772 test -= cval->res;
773 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100774 set_cur_mix_value(cval, minchn, 0, test) ||
775 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200776 cval->res = last_valid_res;
777 break;
778 }
779 if (test == check)
780 break;
781 cval->res *= 2;
782 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100783 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200784 }
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 cval->initialized = 1;
787 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100788
789 /* USB descriptions contain the dB scale in 1/256 dB unit
790 * while ALSA TLV contains in 1/100 dB unit
791 */
792 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
793 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
794 if (cval->dBmin > cval->dBmax) {
795 /* something is wrong; assume it's either from/to 0dB */
796 if (cval->dBmin < 0)
797 cval->dBmax = 0;
798 else if (cval->dBmin > 0)
799 cval->dBmin = 0;
800 if (cval->dBmin > cval->dBmax) {
801 /* totally crap, return an error */
802 return -EINVAL;
803 }
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 0;
807}
808
809
810/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100811static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100813 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (cval->val_type == USB_MIXER_BOOLEAN ||
816 cval->val_type == USB_MIXER_INV_BOOLEAN)
817 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
818 else
819 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
820 uinfo->count = cval->channels;
821 if (cval->val_type == USB_MIXER_BOOLEAN ||
822 cval->val_type == USB_MIXER_INV_BOOLEAN) {
823 uinfo->value.integer.min = 0;
824 uinfo->value.integer.max = 1;
825 } else {
826 if (! cval->initialized)
827 get_min_max(cval, 0);
828 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200829 uinfo->value.integer.max =
830 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 return 0;
833}
834
835/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100836static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100838 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int c, cnt, val, err;
840
Takashi Iwai641b4872009-01-15 17:05:24 +0100841 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (cval->cmask) {
843 cnt = 0;
844 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100845 if (!(cval->cmask & (1 << c)))
846 continue;
847 err = get_cur_mix_value(cval, c + 1, cnt, &val);
848 if (err < 0)
849 return cval->mixer->ignore_ctl_error ? 0 : err;
850 val = get_relative_value(cval, val);
851 ucontrol->value.integer.value[cnt] = val;
852 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
856 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100857 err = get_cur_mix_value(cval, 0, 0, &val);
858 if (err < 0)
859 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 val = get_relative_value(cval, val);
861 ucontrol->value.integer.value[0] = val;
862 }
863 return 0;
864}
865
866/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100867static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100869 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 int c, cnt, val, oval, err;
871 int changed = 0;
872
873 if (cval->cmask) {
874 cnt = 0;
875 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100876 if (!(cval->cmask & (1 << c)))
877 continue;
878 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
879 if (err < 0)
880 return cval->mixer->ignore_ctl_error ? 0 : err;
881 val = ucontrol->value.integer.value[cnt];
882 val = get_abs_value(cval, val);
883 if (oval != val) {
884 set_cur_mix_value(cval, c + 1, cnt, val);
885 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100887 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 } else {
890 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100891 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100893 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 val = ucontrol->value.integer.value[0];
895 val = get_abs_value(cval, val);
896 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100897 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 changed = 1;
899 }
900 }
901 return changed;
902}
903
Takashi Iwai86e07d32005-11-17 15:08:02 +0100904static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
906 .name = "", /* will be filled later manually */
907 .info = mixer_ctl_feature_info,
908 .get = mixer_ctl_feature_get,
909 .put = mixer_ctl_feature_put,
910};
911
912
913/*
914 * build a feature control
915 */
916
Takashi Iwai08d1e632009-10-02 14:06:08 +0200917static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
918{
919 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
920}
921
Takashi Iwai86e07d32005-11-17 15:08:02 +0100922static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100924 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 unsigned int len = 0;
927 int mapped_name = 0;
928 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100929 struct snd_kcontrol *kctl;
930 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100931 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 control++; /* change from zero-based to 1-based value */
934
935 if (control == USB_FEATURE_GEQ) {
936 /* FIXME: not supported yet */
937 return;
938 }
939
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100940 map = find_map(state, unitid, control);
941 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return;
943
Takashi Iwai561b2202005-09-09 14:22:34 +0200944 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (! cval) {
946 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
947 return;
948 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200949 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 cval->id = unitid;
951 cval->control = control;
952 cval->cmask = ctl_mask;
953 cval->val_type = audio_feature_info[control-1].type;
954 if (ctl_mask == 0)
955 cval->channels = 1; /* master channel */
956 else {
957 int i, c = 0;
958 for (i = 0; i < 16; i++)
959 if (ctl_mask & (1 << i))
960 c++;
961 cval->channels = c;
962 }
963
964 /* get min/max values */
965 get_min_max(cval, 0);
966
967 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
968 if (! kctl) {
969 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
970 kfree(cval);
971 return;
972 }
973 kctl->private_free = usb_mixer_elem_free;
974
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100975 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 mapped_name = len != 0;
977 if (! len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100978 len = snd_usb_copy_string_desc(state, nameid,
979 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 switch (control) {
982 case USB_FEATURE_MUTE:
983 case USB_FEATURE_VOLUME:
984 /* determine the control name. the rule is:
985 * - if a name id is given in descriptor, use it.
986 * - if the connected input can be determined, then use the name
987 * of terminal type.
988 * - if the connected output can be determined, use it.
989 * - otherwise, anonymous name.
990 */
991 if (! len) {
992 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
993 if (! len)
994 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
995 if (! len)
996 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
997 "Feature %d", unitid);
998 }
999 /* determine the stream direction:
1000 * if the connected output is USB stream, then it's likely a
1001 * capture stream. otherwise it should be playback (hopefully :)
1002 */
1003 if (! mapped_name && ! (state->oterm.type >> 16)) {
1004 if ((state->oterm.type & 0xff00) == 0x0100) {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001005 len = append_ctl_name(kctl, " Capture");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 } else {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001007 len = append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001010 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1011 " Switch" : " Volume");
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +02001012 if (control == USB_FEATURE_VOLUME) {
1013 kctl->tlv.c = mixer_vol_tlv;
1014 kctl->vd[0].access |=
1015 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1016 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001017 check_mapped_dB(map, cval);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +02001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
1020
1021 default:
1022 if (! len)
1023 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1024 sizeof(kctl->id.name));
1025 break;
1026 }
1027
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001028 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001029 switch (state->chip->usb_id) {
1030 case USB_ID(0x0471, 0x0101):
1031 case USB_ID(0x0471, 0x0104):
1032 case USB_ID(0x0471, 0x0105):
1033 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001034 /* quirk for UDA1321/N101.
1035 * note that detection between firmware 2.1.1.7 (N101)
1036 * and later 2.1.1.21 is not very clear from datasheets.
1037 * I hope that the min value is -15360 for newer firmware --jk
1038 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001039 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1040 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001041 snd_printk(KERN_INFO
1042 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001043 cval->max = -256;
1044 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001045 break;
1046
1047 case USB_ID(0x046d, 0x09a4):
1048 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1049 snd_printk(KERN_INFO
1050 "set volume quirk for QuickCam E3500\n");
1051 cval->min = 6080;
1052 cval->max = 8768;
1053 cval->res = 192;
1054 }
1055 break;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
1059 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1060 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001061 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064
1065
1066/*
1067 * parse a feature unit
1068 *
1069 * most of controlls are defined here.
1070 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001071static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001074 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 unsigned int master_bits, first_ch_bits;
1076 int err, csize;
1077
1078 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1079 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1080 return -EINVAL;
1081 }
1082
1083 /* parse the source unit */
1084 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1085 return err;
1086
1087 /* determine the input source type and name */
1088 if (check_input_term(state, ftr[4], &iterm) < 0)
1089 return -EINVAL;
1090
1091 channels = (ftr[0] - 7) / csize - 1;
1092
1093 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001094 /* master configuration quirks */
1095 switch (state->chip->usb_id) {
1096 case USB_ID(0x08bb, 0x2702):
1097 snd_printk(KERN_INFO
1098 "usbmixer: master volume quirk for PCM2702 chip\n");
1099 /* disable non-functional volume control */
1100 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1101 break;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (channels > 0)
1104 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1105 else
1106 first_ch_bits = 0;
1107 /* check all control types */
1108 for (i = 0; i < 10; i++) {
1109 unsigned int ch_bits = 0;
1110 for (j = 0; j < channels; j++) {
1111 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1112 if (mask & (1 << i))
1113 ch_bits |= (1 << j);
1114 }
1115 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1116 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1117 if (master_bits & (1 << i))
1118 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1119 }
1120
1121 return 0;
1122}
1123
1124
1125/*
1126 * Mixer Unit
1127 */
1128
1129/*
1130 * build a mixer unit control
1131 *
1132 * the callbacks are identical with feature unit.
1133 * input channel number (zero based) is given in control field instead.
1134 */
1135
Takashi Iwai86e07d32005-11-17 15:08:02 +01001136static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001138 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001140 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 unsigned int input_pins = desc[4];
1142 unsigned int num_outs = desc[5 + input_pins];
1143 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001144 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001145 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001147 map = find_map(state, unitid, 0);
1148 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return;
1150
Takashi Iwai561b2202005-09-09 14:22:34 +02001151 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (! cval)
1153 return;
1154
Clemens Ladisch84957a82005-04-29 16:23:13 +02001155 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 cval->id = unitid;
1157 cval->control = in_ch + 1; /* based on 1 */
1158 cval->val_type = USB_MIXER_S16;
1159 for (i = 0; i < num_outs; i++) {
1160 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1161 cval->cmask |= (1 << i);
1162 cval->channels++;
1163 }
1164 }
1165
1166 /* get min/max values */
1167 get_min_max(cval, 0);
1168
1169 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1170 if (! kctl) {
1171 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1172 kfree(cval);
1173 return;
1174 }
1175 kctl->private_free = usb_mixer_elem_free;
1176
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001177 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (! len)
1179 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1180 if (! len)
1181 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001182 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1185 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001186 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189
1190/*
1191 * parse a mixer unit
1192 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001193static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001195 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int input_pins, num_ins, num_outs;
1197 int pin, ich, err;
1198
1199 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1200 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1201 return -EINVAL;
1202 }
1203 /* no bmControls field (e.g. Maya44) -> ignore */
1204 if (desc[0] <= 10 + input_pins) {
1205 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1206 return 0;
1207 }
1208
1209 num_ins = 0;
1210 ich = 0;
1211 for (pin = 0; pin < input_pins; pin++) {
1212 err = parse_audio_unit(state, desc[5 + pin]);
1213 if (err < 0)
1214 return err;
1215 err = check_input_term(state, desc[5 + pin], &iterm);
1216 if (err < 0)
1217 return err;
1218 num_ins += iterm.channels;
1219 for (; ich < num_ins; ++ich) {
1220 int och, ich_has_controls = 0;
1221
1222 for (och = 0; och < num_outs; ++och) {
1223 if (check_matrix_bitmap(desc + 9 + input_pins,
1224 ich, och, num_outs)) {
1225 ich_has_controls = 1;
1226 break;
1227 }
1228 }
1229 if (ich_has_controls)
1230 build_mixer_unit_ctl(state, desc, pin, ich,
1231 unitid, &iterm);
1232 }
1233 }
1234 return 0;
1235}
1236
1237
1238/*
1239 * Processing Unit / Extension Unit
1240 */
1241
1242/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001243static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001245 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int err, val;
1247
1248 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001249 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ucontrol->value.integer.value[0] = cval->min;
1251 return 0;
1252 }
1253 if (err < 0)
1254 return err;
1255 val = get_relative_value(cval, val);
1256 ucontrol->value.integer.value[0] = val;
1257 return 0;
1258}
1259
1260/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001261static int mixer_ctl_procunit_put(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 val, oval, err;
1265
1266 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1267 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001268 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270 return err;
1271 }
1272 val = ucontrol->value.integer.value[0];
1273 val = get_abs_value(cval, val);
1274 if (val != oval) {
1275 set_cur_ctl_value(cval, cval->control << 8, val);
1276 return 1;
1277 }
1278 return 0;
1279}
1280
1281/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001282static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1284 .name = "", /* will be filled later */
1285 .info = mixer_ctl_feature_info,
1286 .get = mixer_ctl_procunit_get,
1287 .put = mixer_ctl_procunit_put,
1288};
1289
1290
1291/*
1292 * predefined data for processing units
1293 */
1294struct procunit_value_info {
1295 int control;
1296 char *suffix;
1297 int val_type;
1298 int min_value;
1299};
1300
1301struct procunit_info {
1302 int type;
1303 char *name;
1304 struct procunit_value_info *values;
1305};
1306
1307static struct procunit_value_info updown_proc_info[] = {
1308 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1309 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1310 { 0 }
1311};
1312static struct procunit_value_info prologic_proc_info[] = {
1313 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1314 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1315 { 0 }
1316};
1317static struct procunit_value_info threed_enh_proc_info[] = {
1318 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1319 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1320 { 0 }
1321};
1322static struct procunit_value_info reverb_proc_info[] = {
1323 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1324 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1325 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1326 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1327 { 0 }
1328};
1329static struct procunit_value_info chorus_proc_info[] = {
1330 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1331 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1332 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1333 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1334 { 0 }
1335};
1336static struct procunit_value_info dcr_proc_info[] = {
1337 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1338 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1339 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1340 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1341 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1342 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1343 { 0 }
1344};
1345
1346static struct procunit_info procunits[] = {
1347 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1348 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1349 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1350 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1351 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1352 { USB_PROC_DCR, "DCR", dcr_proc_info },
1353 { 0 },
1354};
1355
1356/*
1357 * build a processing/extension unit
1358 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001359static 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 -07001360{
1361 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001362 struct usb_mixer_elem_info *cval;
1363 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 int i, err, nameid, type, len;
1365 struct procunit_info *info;
1366 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001367 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 static struct procunit_value_info default_value_info[] = {
1369 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1370 { 0 }
1371 };
1372 static struct procunit_info default_info = {
1373 0, NULL, default_value_info
1374 };
1375
1376 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1377 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1378 return -EINVAL;
1379 }
1380
1381 for (i = 0; i < num_ins; i++) {
1382 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1383 return err;
1384 }
1385
1386 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 for (info = list; info && info->type; info++)
1388 if (info->type == type)
1389 break;
1390 if (! info || ! info->type)
1391 info = &default_info;
1392
1393 for (valinfo = info->values; valinfo->control; valinfo++) {
1394 /* FIXME: bitmap might be longer than 8bit */
1395 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1396 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001397 map = find_map(state, unitid, valinfo->control);
1398 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001400 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (! cval) {
1402 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1403 return -ENOMEM;
1404 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001405 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 cval->id = unitid;
1407 cval->control = valinfo->control;
1408 cval->val_type = valinfo->val_type;
1409 cval->channels = 1;
1410
1411 /* get min/max values */
1412 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1413 /* FIXME: hard-coded */
1414 cval->min = 1;
1415 cval->max = dsc[15];
1416 cval->res = 1;
1417 cval->initialized = 1;
1418 } else
1419 get_min_max(cval, valinfo->min_value);
1420
1421 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1422 if (! kctl) {
1423 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1424 kfree(cval);
1425 return -ENOMEM;
1426 }
1427 kctl->private_free = usb_mixer_elem_free;
1428
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001429 if (check_mapped_name(map, kctl->id.name,
1430 sizeof(kctl->id.name)))
1431 /* nothing */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 else if (info->name)
1433 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1434 else {
1435 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1436 len = 0;
1437 if (nameid)
1438 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1439 if (! len)
1440 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1441 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001442 append_ctl_name(kctl, " ");
1443 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1446 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001447 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return err;
1449 }
1450 return 0;
1451}
1452
1453
Takashi Iwai86e07d32005-11-17 15:08:02 +01001454static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1457}
1458
Takashi Iwai86e07d32005-11-17 15:08:02 +01001459static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1462}
1463
1464
1465/*
1466 * Selector Unit
1467 */
1468
1469/* info callback for selector unit
1470 * use an enumerator type for routing
1471 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001472static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001474 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 char **itemlist = (char **)kcontrol->private_value;
1476
Takashi Iwai5e246b82008-08-08 17:12:47 +02001477 if (snd_BUG_ON(!itemlist))
1478 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1480 uinfo->count = 1;
1481 uinfo->value.enumerated.items = cval->max;
1482 if ((int)uinfo->value.enumerated.item >= cval->max)
1483 uinfo->value.enumerated.item = cval->max - 1;
1484 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1485 return 0;
1486}
1487
1488/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001489static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001491 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int val, err;
1493
1494 err = get_cur_ctl_value(cval, 0, &val);
1495 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001496 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 ucontrol->value.enumerated.item[0] = 0;
1498 return 0;
1499 }
1500 return err;
1501 }
1502 val = get_relative_value(cval, val);
1503 ucontrol->value.enumerated.item[0] = val;
1504 return 0;
1505}
1506
1507/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001508static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001510 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int val, oval, err;
1512
1513 err = get_cur_ctl_value(cval, 0, &oval);
1514 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001515 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return 0;
1517 return err;
1518 }
1519 val = ucontrol->value.enumerated.item[0];
1520 val = get_abs_value(cval, val);
1521 if (val != oval) {
1522 set_cur_ctl_value(cval, 0, val);
1523 return 1;
1524 }
1525 return 0;
1526}
1527
1528/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001529static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1531 .name = "", /* will be filled later */
1532 .info = mixer_ctl_selector_info,
1533 .get = mixer_ctl_selector_get,
1534 .put = mixer_ctl_selector_put,
1535};
1536
1537
1538/* private free callback.
1539 * free both private_data and private_value
1540 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001541static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 int i, num_ins = 0;
1544
1545 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001546 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 num_ins = cval->max;
1548 kfree(cval);
1549 kctl->private_data = NULL;
1550 }
1551 if (kctl->private_value) {
1552 char **itemlist = (char **)kctl->private_value;
1553 for (i = 0; i < num_ins; i++)
1554 kfree(itemlist[i]);
1555 kfree(itemlist);
1556 kctl->private_value = 0;
1557 }
1558}
1559
1560/*
1561 * parse a selector unit
1562 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001563static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 unsigned int num_ins = desc[4];
1566 unsigned int i, nameid, len;
1567 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001568 struct usb_mixer_elem_info *cval;
1569 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001570 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 char **namelist;
1572
Russ Cox38977e92007-08-06 15:37:58 +02001573 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1575 return -EINVAL;
1576 }
1577
1578 for (i = 0; i < num_ins; i++) {
1579 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1580 return err;
1581 }
1582
1583 if (num_ins == 1) /* only one ? nonsense! */
1584 return 0;
1585
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001586 map = find_map(state, unitid, 0);
1587 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return 0;
1589
Takashi Iwai561b2202005-09-09 14:22:34 +02001590 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (! cval) {
1592 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1593 return -ENOMEM;
1594 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001595 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 cval->id = unitid;
1597 cval->val_type = USB_MIXER_U8;
1598 cval->channels = 1;
1599 cval->min = 1;
1600 cval->max = num_ins;
1601 cval->res = 1;
1602 cval->initialized = 1;
1603
1604 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1605 if (! namelist) {
1606 snd_printk(KERN_ERR "cannot malloc\n");
1607 kfree(cval);
1608 return -ENOMEM;
1609 }
1610#define MAX_ITEM_NAME_LEN 64
1611 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001612 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 len = 0;
1614 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1615 if (! namelist[i]) {
1616 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001617 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 kfree(namelist[i]);
1619 kfree(namelist);
1620 kfree(cval);
1621 return -ENOMEM;
1622 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001623 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1624 MAX_ITEM_NAME_LEN);
1625 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1627 if (! len)
1628 sprintf(namelist[i], "Input %d", i);
1629 }
1630
1631 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1632 if (! kctl) {
1633 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001634 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 kfree(cval);
1636 return -ENOMEM;
1637 }
1638 kctl->private_value = (unsigned long)namelist;
1639 kctl->private_free = usb_mixer_selector_elem_free;
1640
1641 nameid = desc[desc[0] - 1];
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001642 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (len)
1644 ;
1645 else if (nameid)
1646 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1647 else {
1648 len = get_term_name(state, &state->oterm,
1649 kctl->id.name, sizeof(kctl->id.name), 0);
1650 if (! len)
1651 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1652
1653 if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02001654 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02001656 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658
1659 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1660 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001661 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return err;
1663
1664 return 0;
1665}
1666
1667
1668/*
1669 * parse an audio unit recursively
1670 */
1671
Takashi Iwai86e07d32005-11-17 15:08:02 +01001672static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 unsigned char *p1;
1675
1676 if (test_and_set_bit(unitid, state->unitbitmap))
1677 return 0; /* the unit already visited */
1678
1679 p1 = find_audio_control_unit(state, unitid);
1680 if (!p1) {
1681 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1682 return -EINVAL;
1683 }
1684
1685 switch (p1[2]) {
1686 case INPUT_TERMINAL:
1687 return 0; /* NOP */
1688 case MIXER_UNIT:
1689 return parse_audio_mixer_unit(state, unitid, p1);
1690 case SELECTOR_UNIT:
1691 return parse_audio_selector_unit(state, unitid, p1);
1692 case FEATURE_UNIT:
1693 return parse_audio_feature_unit(state, unitid, p1);
1694 case PROCESSING_UNIT:
1695 return parse_audio_processing_unit(state, unitid, p1);
1696 case EXTENSION_UNIT:
1697 return parse_audio_extension_unit(state, unitid, p1);
1698 default:
1699 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1700 return -EINVAL;
1701 }
1702}
1703
Clemens Ladisch84957a82005-04-29 16:23:13 +02001704static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1705{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001706 kfree(mixer->id_elems);
1707 if (mixer->urb) {
1708 kfree(mixer->urb->transfer_buffer);
1709 usb_free_urb(mixer->urb);
1710 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001711 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001712 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001713 kfree(mixer);
1714}
1715
Takashi Iwai86e07d32005-11-17 15:08:02 +01001716static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001717{
1718 struct usb_mixer_interface *mixer = device->device_data;
1719 snd_usb_mixer_free(mixer);
1720 return 0;
1721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723/*
1724 * create mixer controls
1725 *
1726 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1727 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001728static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001731 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int err;
1733 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001734 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Clemens Ladisch84957a82005-04-29 16:23:13 +02001736 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001738 state.chip = mixer->chip;
1739 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 state.buffer = hostif->extra;
1741 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001744 for (map = usbmix_ctl_maps; map->id; map++) {
1745 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001747 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001748 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 break;
1750 }
1751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 desc = NULL;
1754 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1755 if (desc[0] < 9)
1756 continue; /* invalid descriptor? */
1757 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1758 state.oterm.id = desc[3];
1759 state.oterm.type = combine_word(&desc[4]);
1760 state.oterm.name = desc[8];
1761 err = parse_audio_unit(&state, desc[7]);
1762 if (err < 0)
1763 return err;
1764 }
1765 return 0;
1766}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001767
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001768static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1769 int unitid)
1770{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001771 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001772
1773 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1774 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1775 info->elem_id);
1776}
1777
Clemens Ladischb259b102005-04-29 16:29:28 +02001778static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1779 int unitid)
1780{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001781 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001782 return;
1783 /* unit ids specific to Extigy/Audigy 2 NX: */
1784 switch (unitid) {
1785 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001786 mixer->rc_urb->dev = mixer->chip->dev;
1787 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001788 break;
1789 case 4: /* digital in jack */
1790 case 7: /* line in jacks */
1791 case 19: /* speaker out jacks */
1792 case 20: /* headphones out jack */
1793 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001794 /* live24ext: 4 = line-in jack */
1795 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001796 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1797 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001798 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1799 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001800 default:
1801 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1802 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001803 }
1804}
1805
David Howells7d12e782006-10-05 14:55:46 +01001806static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001807{
1808 struct usb_mixer_interface *mixer = urb->context;
1809
1810 if (urb->status == 0) {
1811 u8 *buf = urb->transfer_buffer;
1812 int i;
1813
1814 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1815 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1816 buf[0], buf[1]);
1817 /* ignore any notifications not from the control interface */
1818 if ((buf[0] & 0x0f) != 0)
1819 continue;
1820 if (!(buf[0] & 0x40))
1821 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001822 else
1823 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001824 }
1825 }
1826 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1827 urb->dev = mixer->chip->dev;
1828 usb_submit_urb(urb, GFP_ATOMIC);
1829 }
1830}
1831
1832/* create the handler for the optional status interrupt endpoint */
1833static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1834{
1835 struct usb_host_interface *hostif;
1836 struct usb_endpoint_descriptor *ep;
1837 void *transfer_buffer;
1838 int buffer_length;
1839 unsigned int epnum;
1840
1841 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1842 /* we need one interrupt input endpoint */
1843 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1844 return 0;
1845 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001846 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001847 return 0;
1848
Julia Lawall42a6e662008-12-29 11:23:02 +01001849 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001850 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1851 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1852 if (!transfer_buffer)
1853 return -ENOMEM;
1854 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1855 if (!mixer->urb) {
1856 kfree(transfer_buffer);
1857 return -ENOMEM;
1858 }
1859 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1860 usb_rcvintpipe(mixer->chip->dev, epnum),
1861 transfer_buffer, buffer_length,
1862 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1863 usb_submit_urb(mixer->urb, GFP_KERNEL);
1864 return 0;
1865}
1866
David Howells7d12e782006-10-05 14:55:46 +01001867static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001868{
1869 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001870 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001871 u32 code;
1872
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001873 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001874 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001875
1876 code = mixer->rc_buffer[rc->offset];
1877 if (rc->length == 2)
1878 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1879
Clemens Ladischb259b102005-04-29 16:29:28 +02001880 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001881 if (code == rc->mute_code)
1882 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001883 mixer->rc_code = code;
1884 wmb();
1885 wake_up(&mixer->rc_waitq);
1886}
1887
Takashi Iwai86e07d32005-11-17 15:08:02 +01001888static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001889 long count, loff_t *offset)
1890{
1891 struct usb_mixer_interface *mixer = hw->private_data;
1892 int err;
1893 u32 rc_code;
1894
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001895 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001896 return -EINVAL;
1897 err = wait_event_interruptible(mixer->rc_waitq,
1898 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1899 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001900 if (count == 1)
1901 err = put_user(rc_code, buf);
1902 else
1903 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001904 }
1905 return err < 0 ? err : count;
1906}
1907
Takashi Iwai86e07d32005-11-17 15:08:02 +01001908static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001909 poll_table *wait)
1910{
1911 struct usb_mixer_interface *mixer = hw->private_data;
1912
1913 poll_wait(file, &mixer->rc_waitq, wait);
1914 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1915}
1916
1917static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1918{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001919 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001920 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02001921
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001922 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1923 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1924 break;
1925 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02001926 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001927 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02001928
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001929 len = mixer->rc_cfg->packet_length;
1930
Clemens Ladischb259b102005-04-29 16:29:28 +02001931 init_waitqueue_head(&mixer->rc_waitq);
1932 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1933 if (err < 0)
1934 return err;
1935 snprintf(hwdep->name, sizeof(hwdep->name),
1936 "%s remote control", mixer->chip->card->shortname);
1937 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1938 hwdep->private_data = mixer;
1939 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02001940 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01001941 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02001942
1943 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1944 if (!mixer->rc_urb)
1945 return -ENOMEM;
1946 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1947 if (!mixer->rc_setup_packet) {
1948 usb_free_urb(mixer->rc_urb);
1949 mixer->rc_urb = NULL;
1950 return -ENOMEM;
1951 }
1952 mixer->rc_setup_packet->bRequestType =
1953 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1954 mixer->rc_setup_packet->bRequest = GET_MEM;
1955 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1956 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1957 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1958 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1959 usb_rcvctrlpipe(mixer->chip->dev, 0),
1960 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1961 snd_usb_soundblaster_remote_complete, mixer);
1962 return 0;
1963}
1964
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001965#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001966
Takashi Iwai86e07d32005-11-17 15:08:02 +01001967static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001968{
1969 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1970 int index = kcontrol->private_value;
1971
1972 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1973 return 0;
1974}
1975
Takashi Iwai86e07d32005-11-17 15:08:02 +01001976static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001977{
1978 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1979 int index = kcontrol->private_value;
1980 int value = ucontrol->value.integer.value[0];
1981 int err, changed;
1982
1983 if (value > 1)
1984 return -EINVAL;
1985 changed = value != mixer->audigy2nx_leds[index];
1986 err = snd_usb_ctl_msg(mixer->chip->dev,
1987 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1988 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1989 value, index + 2, NULL, 0, 100);
1990 if (err < 0)
1991 return err;
1992 mixer->audigy2nx_leds[index] = value;
1993 return changed;
1994}
1995
Takashi Iwai86e07d32005-11-17 15:08:02 +01001996static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001997 {
1998 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1999 .name = "CMSS LED Switch",
2000 .info = snd_audigy2nx_led_info,
2001 .get = snd_audigy2nx_led_get,
2002 .put = snd_audigy2nx_led_put,
2003 .private_value = 0,
2004 },
2005 {
2006 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2007 .name = "Power LED Switch",
2008 .info = snd_audigy2nx_led_info,
2009 .get = snd_audigy2nx_led_get,
2010 .put = snd_audigy2nx_led_put,
2011 .private_value = 1,
2012 },
2013 {
2014 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2015 .name = "Dolby Digital LED Switch",
2016 .info = snd_audigy2nx_led_info,
2017 .get = snd_audigy2nx_led_get,
2018 .put = snd_audigy2nx_led_put,
2019 .private_value = 2,
2020 },
2021};
2022
2023static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2024{
2025 int i, err;
2026
2027 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01002028 if (i > 1 && /* Live24ext has 2 LEDs only */
2029 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2030 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002031 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002032 err = snd_ctl_add(mixer->chip->card,
2033 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2034 if (err < 0)
2035 return err;
2036 }
2037 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2038 return 0;
2039}
2040
Takashi Iwai86e07d32005-11-17 15:08:02 +01002041static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2042 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02002043{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002044 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02002045 int unitid;
2046 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002047 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02002048 {4, "dig in "},
2049 {7, "line in"},
2050 {19, "spk out"},
2051 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002052 {-1, NULL}
2053 }, jacks_live24ext[] = {
2054 {4, "line in"}, /* &1=Line, &2=Mic*/
2055 {3, "hph out"}, /* headphones */
2056 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2057 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002058 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002059 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002060 struct usb_mixer_interface *mixer = entry->private_data;
2061 int i, err;
2062 u8 buf[3];
2063
2064 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002065 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2066 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002067 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2068 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002069 jacks = jacks_live24ext;
2070 else
2071 return;
2072
2073 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002074 snd_iprintf(buffer, "%s: ", jacks[i].name);
2075 err = snd_usb_ctl_msg(mixer->chip->dev,
2076 usb_rcvctrlpipe(mixer->chip->dev, 0),
2077 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2078 USB_RECIP_INTERFACE, 0,
2079 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002080 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002081 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2082 else
2083 snd_iprintf(buffer, "?\n");
2084 }
2085}
2086
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002087static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2088 struct snd_ctl_elem_value *ucontrol)
2089{
2090 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2091
2092 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2093 return 0;
2094}
2095
2096static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_value *ucontrol)
2098{
2099 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2100 u8 old_status, new_status;
2101 int err, changed;
2102
2103 old_status = mixer->xonar_u1_status;
2104 if (ucontrol->value.integer.value[0])
2105 new_status = old_status | 0x02;
2106 else
2107 new_status = old_status & ~0x02;
2108 changed = new_status != old_status;
2109 err = snd_usb_ctl_msg(mixer->chip->dev,
2110 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2111 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2112 50, 0, &new_status, 1, 100);
2113 if (err < 0)
2114 return err;
2115 mixer->xonar_u1_status = new_status;
2116 return changed;
2117}
2118
2119static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2120 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2121 .name = "Digital Playback Switch",
2122 .info = snd_ctl_boolean_mono_info,
2123 .get = snd_xonar_u1_switch_get,
2124 .put = snd_xonar_u1_switch_put,
2125};
2126
2127static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2128{
2129 int err;
2130
2131 err = snd_ctl_add(mixer->chip->card,
2132 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2133 if (err < 0)
2134 return err;
2135 mixer->xonar_u1_status = 0x05;
2136 return 0;
2137}
2138
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002139int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2140 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002141{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002142 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002143 .dev_free = snd_usb_mixer_dev_free
2144 };
2145 struct usb_mixer_interface *mixer;
2146 int err;
2147
2148 strcpy(chip->card->mixername, "USB Mixer");
2149
Takashi Iwai561b2202005-09-09 14:22:34 +02002150 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002151 if (!mixer)
2152 return -ENOMEM;
2153 mixer->chip = chip;
2154 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002155 mixer->ignore_ctl_error = ignore_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002156 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2157 if (!mixer->id_elems) {
2158 kfree(mixer);
2159 return -ENOMEM;
2160 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002161
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002162 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002163 (err = snd_usb_mixer_status_create(mixer)) < 0)
2164 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002165
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002166 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2167 goto _error;
2168
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002169 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002170 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2171 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002172 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02002173
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002174 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2175 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002176 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002177 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002178 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002179 }
2180
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002181 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2182 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2183 err = snd_xonar_u1_controls_create(mixer);
2184 if (err < 0)
2185 goto _error;
2186 }
2187
Clemens Ladisch84957a82005-04-29 16:23:13 +02002188 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002189 if (err < 0)
2190 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002191 list_add(&mixer->list, &chip->mixer_list);
2192 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002193
2194_error:
2195 snd_usb_mixer_free(mixer);
2196 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002197}
2198
2199void snd_usb_mixer_disconnect(struct list_head *p)
2200{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002201 struct usb_mixer_interface *mixer;
2202
2203 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002204 usb_kill_urb(mixer->urb);
2205 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002206}