blob: ab5a3ac2ac47c5ee9aa086d87481e5446777438d [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 Iwai7bc5ba72006-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;
Takashi Iwai641b4872009-01-15 17:05:24 +0100126 int cached;
127 int cache_val[MAX_CHANNELS];
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200128 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131
132enum {
133 USB_FEATURE_NONE = 0,
134 USB_FEATURE_MUTE = 1,
135 USB_FEATURE_VOLUME,
136 USB_FEATURE_BASS,
137 USB_FEATURE_MID,
138 USB_FEATURE_TREBLE,
139 USB_FEATURE_GEQ,
140 USB_FEATURE_AGC,
141 USB_FEATURE_DELAY,
142 USB_FEATURE_BASSBOOST,
143 USB_FEATURE_LOUDNESS
144};
145
146enum {
147 USB_MIXER_BOOLEAN,
148 USB_MIXER_INV_BOOLEAN,
149 USB_MIXER_S8,
150 USB_MIXER_U8,
151 USB_MIXER_S16,
152 USB_MIXER_U16,
153};
154
155enum {
156 USB_PROC_UPDOWN = 1,
157 USB_PROC_UPDOWN_SWITCH = 1,
158 USB_PROC_UPDOWN_MODE_SEL = 2,
159
160 USB_PROC_PROLOGIC = 2,
161 USB_PROC_PROLOGIC_SWITCH = 1,
162 USB_PROC_PROLOGIC_MODE_SEL = 2,
163
164 USB_PROC_3DENH = 3,
165 USB_PROC_3DENH_SWITCH = 1,
166 USB_PROC_3DENH_SPACE = 2,
167
168 USB_PROC_REVERB = 4,
169 USB_PROC_REVERB_SWITCH = 1,
170 USB_PROC_REVERB_LEVEL = 2,
171 USB_PROC_REVERB_TIME = 3,
172 USB_PROC_REVERB_DELAY = 4,
173
174 USB_PROC_CHORUS = 5,
175 USB_PROC_CHORUS_SWITCH = 1,
176 USB_PROC_CHORUS_LEVEL = 2,
177 USB_PROC_CHORUS_RATE = 3,
178 USB_PROC_CHORUS_DEPTH = 4,
179
180 USB_PROC_DCR = 6,
181 USB_PROC_DCR_SWITCH = 1,
182 USB_PROC_DCR_RATIO = 2,
183 USB_PROC_DCR_MAX_AMP = 3,
184 USB_PROC_DCR_THRESHOLD = 4,
185 USB_PROC_DCR_ATTACK = 5,
186 USB_PROC_DCR_RELEASE = 6,
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/*
191 * manual mapping of mixer names
192 * if the mixer topology is too complicated and the parsed names are
193 * ambiguous, add the entries in usbmixer_maps.c.
194 */
195#include "usbmixer_maps.c"
196
197/* get the mapped name if the unit matches */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100198static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 const struct usbmix_name_map *p;
201
202 if (! state->map)
203 return 0;
204
205 for (p = state->map; p->id; p++) {
206 if (p->id == unitid && p->name &&
207 (! control || ! p->control || control == p->control)) {
208 buflen--;
209 return strlcpy(buf, p->name, buflen);
210 }
211 }
212 return 0;
213}
214
215/* check whether the control should be ignored */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100216static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 const struct usbmix_name_map *p;
219
220 if (! state->map)
221 return 0;
222 for (p = state->map; p->id; p++) {
223 if (p->id == unitid && ! p->name &&
224 (! control || ! p->control || control == p->control)) {
Takashi Iwai54530bd2009-02-05 15:55:18 +0100225 /*
226 printk(KERN_DEBUG "ignored control %d:%d\n",
227 unitid, control);
228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 1;
230 }
231 }
232 return 0;
233}
234
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200235/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100236static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200237 int index, char *buf, int buflen)
238{
239 const struct usbmix_selector_map *p;
240
241 if (! state->selector_map)
242 return 0;
243 for (p = state->selector_map; p->id; p++) {
244 if (p->id == unitid && index < p->count)
245 return strlcpy(buf, p->names[index], buflen);
246 }
247 return 0;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * find an audio control unit with the given unit id
252 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100253static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned char *p;
256
257 p = NULL;
258 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
259 USB_DT_CS_INTERFACE)) != NULL) {
260 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
261 return p;
262 }
263 return NULL;
264}
265
266
267/*
268 * copy a string with the given id
269 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100270static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
273 buf[len] = 0;
274 return len;
275}
276
277/*
278 * convert from the byte/word on usb descriptor to the zero-based integer
279 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100280static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 switch (cval->val_type) {
283 case USB_MIXER_BOOLEAN:
284 return !!val;
285 case USB_MIXER_INV_BOOLEAN:
286 return !val;
287 case USB_MIXER_U8:
288 val &= 0xff;
289 break;
290 case USB_MIXER_S8:
291 val &= 0xff;
292 if (val >= 0x80)
293 val -= 0x100;
294 break;
295 case USB_MIXER_U16:
296 val &= 0xffff;
297 break;
298 case USB_MIXER_S16:
299 val &= 0xffff;
300 if (val >= 0x8000)
301 val -= 0x10000;
302 break;
303 }
304 return val;
305}
306
307/*
308 * convert from the zero-based int to the byte/word for usb descriptor
309 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100310static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 switch (cval->val_type) {
313 case USB_MIXER_BOOLEAN:
314 return !!val;
315 case USB_MIXER_INV_BOOLEAN:
316 return !val;
317 case USB_MIXER_S8:
318 case USB_MIXER_U8:
319 return val & 0xff;
320 case USB_MIXER_S16:
321 case USB_MIXER_U16:
322 return val & 0xffff;
323 }
324 return 0; /* not reached */
325}
326
Takashi Iwai86e07d32005-11-17 15:08:02 +0100327static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 if (! cval->res)
330 cval->res = 1;
331 if (val < cval->min)
332 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200333 else if (val >= cval->max)
334 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 else
336 return (val - cval->min) / cval->res;
337}
338
Takashi Iwai86e07d32005-11-17 15:08:02 +0100339static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 if (val < 0)
342 return cval->min;
343 if (! cval->res)
344 cval->res = 1;
345 val *= cval->res;
346 val += cval->min;
347 if (val > cval->max)
348 return cval->max;
349 return val;
350}
351
352
353/*
354 * retrieve a mixer value
355 */
356
Takashi Iwai86e07d32005-11-17 15:08:02 +0100357static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 unsigned char buf[2];
360 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
361 int timeout = 10;
362
363 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200364 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
365 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 request,
367 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200368 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200369 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
371 return 0;
372 }
373 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200374 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
375 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EINVAL;
377}
378
Takashi Iwai86e07d32005-11-17 15:08:02 +0100379static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 return get_ctl_value(cval, GET_CUR, validx, value);
382}
383
384/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100385static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
386 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
389}
390
Takashi Iwai641b4872009-01-15 17:05:24 +0100391static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
392 int channel, int index, int *value)
393{
394 int err;
395
396 if (cval->cached & (1 << channel)) {
397 *value = cval->cache_val[index];
398 return 0;
399 }
400 err = get_cur_mix_raw(cval, channel, value);
401 if (err < 0) {
402 if (!cval->mixer->ignore_ctl_error)
403 snd_printd(KERN_ERR "cannot get current value for "
404 "control %d ch %d: err = %d\n",
405 cval->control, channel, err);
406 return err;
407 }
408 cval->cached |= 1 << channel;
409 cval->cache_val[index] = *value;
410 return 0;
411}
412
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/*
415 * set a mixer value
416 */
417
Takashi Iwai86e07d32005-11-17 15:08:02 +0100418static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 unsigned char buf[2];
421 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
422 int timeout = 10;
423
424 value_set = convert_bytes_value(cval, value_set);
425 buf[0] = value_set & 0xff;
426 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530427 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200428 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
429 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 request,
431 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200432 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 buf, val_len, 100) >= 0)
434 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200435 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
436 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EINVAL;
438}
439
Takashi Iwai86e07d32005-11-17 15:08:02 +0100440static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 return set_ctl_value(cval, SET_CUR, validx, value);
443}
444
Takashi Iwai641b4872009-01-15 17:05:24 +0100445static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
446 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Takashi Iwai641b4872009-01-15 17:05:24 +0100448 int err;
449 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
450 value);
451 if (err < 0)
452 return err;
453 cval->cached |= 1 << channel;
454 cval->cache_val[index] = value;
455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200458/*
459 * TLV callback for mixer volume controls
460 */
461static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
462 unsigned int size, unsigned int __user *_tlv)
463{
464 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200465 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200466
467 if (size < sizeof(scale))
468 return -ENOMEM;
469 /* USB descriptions contain the dB scale in 1/256 dB unit
470 * while ALSA TLV contains in 1/100 dB unit
471 */
472 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200473 scale[3] = (convert_signed_value(cval, cval->max) * 100) / 256;
Takashi Iwaieedbdf02009-06-16 14:27:35 +0200474 if (scale[3] <= scale[2]) {
475 /* something is wrong; assume it's either from/to 0dB */
476 if (scale[2] < 0)
477 scale[3] = 0;
478 else if (scale[2] > 0)
479 scale[2] = 0;
480 else /* totally crap, return an error */
481 return -EINVAL;
482 }
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200483 if (copy_to_user(_tlv, scale, sizeof(scale)))
484 return -EFAULT;
485 return 0;
486}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/*
489 * parser routines begin here...
490 */
491
Takashi Iwai86e07d32005-11-17 15:08:02 +0100492static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494
495/*
496 * check if the input/output channel routing is enabled on the given bitmap.
497 * used for mixer unit parser
498 */
499static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
500{
501 int idx = ich * num_outs + och;
502 return bmap[idx >> 3] & (0x80 >> (idx & 7));
503}
504
505
506/*
507 * add an alsa control element
508 * search and increment the index until an empty slot is found.
509 *
510 * if failed, give up and free the control instance.
511 */
512
Takashi Iwai86e07d32005-11-17 15:08:02 +0100513static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100515 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200517
518 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200520 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200524 cval->elem_id = &kctl->id;
525 cval->next_id_elem = state->mixer->id_elems[cval->id];
526 state->mixer->id_elems[cval->id] = cval;
527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530
531/*
532 * get a terminal name string
533 */
534
535static struct iterm_name_combo {
536 int type;
537 char *name;
538} iterm_names[] = {
539 { 0x0300, "Output" },
540 { 0x0301, "Speaker" },
541 { 0x0302, "Headphone" },
542 { 0x0303, "HMD Audio" },
543 { 0x0304, "Desktop Speaker" },
544 { 0x0305, "Room Speaker" },
545 { 0x0306, "Com Speaker" },
546 { 0x0307, "LFE" },
547 { 0x0600, "External In" },
548 { 0x0601, "Analog In" },
549 { 0x0602, "Digital In" },
550 { 0x0603, "Line" },
551 { 0x0604, "Legacy In" },
552 { 0x0605, "IEC958 In" },
553 { 0x0606, "1394 DA Stream" },
554 { 0x0607, "1394 DV Stream" },
555 { 0x0700, "Embedded" },
556 { 0x0701, "Noise Source" },
557 { 0x0702, "Equalization Noise" },
558 { 0x0703, "CD" },
559 { 0x0704, "DAT" },
560 { 0x0705, "DCC" },
561 { 0x0706, "MiniDisk" },
562 { 0x0707, "Analog Tape" },
563 { 0x0708, "Phonograph" },
564 { 0x0709, "VCR Audio" },
565 { 0x070a, "Video Disk Audio" },
566 { 0x070b, "DVD Audio" },
567 { 0x070c, "TV Tuner Audio" },
568 { 0x070d, "Satellite Rec Audio" },
569 { 0x070e, "Cable Tuner Audio" },
570 { 0x070f, "DSS Audio" },
571 { 0x0710, "Radio Receiver" },
572 { 0x0711, "Radio Transmitter" },
573 { 0x0712, "Multi-Track Recorder" },
574 { 0x0713, "Synthesizer" },
575 { 0 },
576};
577
Takashi Iwai86e07d32005-11-17 15:08:02 +0100578static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned char *name, int maxlen, int term_only)
580{
581 struct iterm_name_combo *names;
582
583 if (iterm->name)
584 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
585
586 /* virtual type - not a real terminal */
587 if (iterm->type >> 16) {
588 if (term_only)
589 return 0;
590 switch (iterm->type >> 16) {
591 case SELECTOR_UNIT:
592 strcpy(name, "Selector"); return 8;
593 case PROCESSING_UNIT:
594 strcpy(name, "Process Unit"); return 12;
595 case EXTENSION_UNIT:
596 strcpy(name, "Ext Unit"); return 8;
597 case MIXER_UNIT:
598 strcpy(name, "Mixer"); return 5;
599 default:
600 return sprintf(name, "Unit %d", iterm->id);
601 }
602 }
603
604 switch (iterm->type & 0xff00) {
605 case 0x0100:
606 strcpy(name, "PCM"); return 3;
607 case 0x0200:
608 strcpy(name, "Mic"); return 3;
609 case 0x0400:
610 strcpy(name, "Headset"); return 7;
611 case 0x0500:
612 strcpy(name, "Phone"); return 5;
613 }
614
615 for (names = iterm_names; names->type; names++)
616 if (names->type == iterm->type) {
617 strcpy(name, names->name);
618 return strlen(names->name);
619 }
620 return 0;
621}
622
623
624/*
625 * parse the source unit recursively until it reaches to a terminal
626 * or a branched unit.
627 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100628static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 unsigned char *p1;
631
632 memset(term, 0, sizeof(*term));
633 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
634 term->id = id;
635 switch (p1[2]) {
636 case INPUT_TERMINAL:
637 term->type = combine_word(p1 + 4);
638 term->channels = p1[7];
639 term->chconfig = combine_word(p1 + 8);
640 term->name = p1[11];
641 return 0;
642 case FEATURE_UNIT:
643 id = p1[4];
644 break; /* continue to parse */
645 case MIXER_UNIT:
646 term->type = p1[2] << 16; /* virtual type */
647 term->channels = p1[5 + p1[4]];
648 term->chconfig = combine_word(p1 + 6 + p1[4]);
649 term->name = p1[p1[0] - 1];
650 return 0;
651 case SELECTOR_UNIT:
652 /* call recursively to retrieve the channel info */
653 if (check_input_term(state, p1[5], term) < 0)
654 return -ENODEV;
655 term->type = p1[2] << 16; /* virtual type */
656 term->id = id;
657 term->name = p1[9 + p1[0] - 1];
658 return 0;
659 case PROCESSING_UNIT:
660 case EXTENSION_UNIT:
661 if (p1[6] == 1) {
662 id = p1[7];
663 break; /* continue to parse */
664 }
665 term->type = p1[2] << 16; /* virtual type */
666 term->channels = p1[7 + p1[6]];
667 term->chconfig = combine_word(p1 + 8 + p1[6]);
668 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
669 return 0;
670 default:
671 return -ENODEV;
672 }
673 }
674 return -ENODEV;
675}
676
677
678/*
679 * Feature Unit
680 */
681
682/* feature unit control information */
683struct usb_feature_control_info {
684 const char *name;
685 unsigned int type; /* control type (mute, volume, etc.) */
686};
687
688static struct usb_feature_control_info audio_feature_info[] = {
689 { "Mute", USB_MIXER_INV_BOOLEAN },
690 { "Volume", USB_MIXER_S16 },
691 { "Tone Control - Bass", USB_MIXER_S8 },
692 { "Tone Control - Mid", USB_MIXER_S8 },
693 { "Tone Control - Treble", USB_MIXER_S8 },
694 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
695 { "Auto Gain Control", USB_MIXER_BOOLEAN },
696 { "Delay Control", USB_MIXER_U16 },
697 { "Bass Boost", USB_MIXER_BOOLEAN },
698 { "Loudness", USB_MIXER_BOOLEAN },
699};
700
701
702/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100703static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Jesper Juhl4d572772005-05-30 17:30:32 +0200705 kfree(kctl->private_data);
706 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709
710/*
711 * interface to ALSA control for feature/mixer units
712 */
713
714/*
715 * retrieve the minimum and maximum values for the specified control
716 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100717static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 /* for failsafe */
720 cval->min = default_min;
721 cval->max = cval->min + 1;
722 cval->res = 1;
723
724 if (cval->val_type == USB_MIXER_BOOLEAN ||
725 cval->val_type == USB_MIXER_INV_BOOLEAN) {
726 cval->initialized = 1;
727 } else {
728 int minchn = 0;
729 if (cval->cmask) {
730 int i;
731 for (i = 0; i < MAX_CHANNELS; i++)
732 if (cval->cmask & (1 << i)) {
733 minchn = i + 1;
734 break;
735 }
736 }
737 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
738 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200739 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
740 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EINVAL;
742 }
743 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
744 cval->res = 1;
745 } else {
746 int last_valid_res = cval->res;
747
748 while (cval->res > 1) {
749 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
750 break;
751 cval->res /= 2;
752 }
753 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
754 cval->res = last_valid_res;
755 }
756 if (cval->res == 0)
757 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200758
759 /* Additional checks for the proper resolution
760 *
761 * Some devices report smaller resolutions than actually
762 * reacting. They don't return errors but simply clip
763 * to the lower aligned value.
764 */
765 if (cval->min + cval->res < cval->max) {
766 int last_valid_res = cval->res;
767 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100768 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200769 for (;;) {
770 test = saved;
771 if (test < cval->max)
772 test += cval->res;
773 else
774 test -= cval->res;
775 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100776 set_cur_mix_value(cval, minchn, 0, test) ||
777 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200778 cval->res = last_valid_res;
779 break;
780 }
781 if (test == check)
782 break;
783 cval->res *= 2;
784 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100785 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200786 }
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 cval->initialized = 1;
789 }
790 return 0;
791}
792
793
794/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100795static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100797 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (cval->val_type == USB_MIXER_BOOLEAN ||
800 cval->val_type == USB_MIXER_INV_BOOLEAN)
801 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
802 else
803 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
804 uinfo->count = cval->channels;
805 if (cval->val_type == USB_MIXER_BOOLEAN ||
806 cval->val_type == USB_MIXER_INV_BOOLEAN) {
807 uinfo->value.integer.min = 0;
808 uinfo->value.integer.max = 1;
809 } else {
810 if (! cval->initialized)
811 get_min_max(cval, 0);
812 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200813 uinfo->value.integer.max =
814 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 return 0;
817}
818
819/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100820static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100822 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int c, cnt, val, err;
824
Takashi Iwai641b4872009-01-15 17:05:24 +0100825 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (cval->cmask) {
827 cnt = 0;
828 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100829 if (!(cval->cmask & (1 << c)))
830 continue;
831 err = get_cur_mix_value(cval, c + 1, cnt, &val);
832 if (err < 0)
833 return cval->mixer->ignore_ctl_error ? 0 : err;
834 val = get_relative_value(cval, val);
835 ucontrol->value.integer.value[cnt] = val;
836 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 } else {
840 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100841 err = get_cur_mix_value(cval, 0, 0, &val);
842 if (err < 0)
843 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 val = get_relative_value(cval, val);
845 ucontrol->value.integer.value[0] = val;
846 }
847 return 0;
848}
849
850/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100851static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100853 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int c, cnt, val, oval, err;
855 int changed = 0;
856
857 if (cval->cmask) {
858 cnt = 0;
859 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100860 if (!(cval->cmask & (1 << c)))
861 continue;
862 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
863 if (err < 0)
864 return cval->mixer->ignore_ctl_error ? 0 : err;
865 val = ucontrol->value.integer.value[cnt];
866 val = get_abs_value(cval, val);
867 if (oval != val) {
868 set_cur_mix_value(cval, c + 1, cnt, val);
869 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100871 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 } else {
874 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100875 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100877 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 val = ucontrol->value.integer.value[0];
879 val = get_abs_value(cval, val);
880 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100881 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 changed = 1;
883 }
884 }
885 return changed;
886}
887
Takashi Iwai86e07d32005-11-17 15:08:02 +0100888static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
890 .name = "", /* will be filled later manually */
891 .info = mixer_ctl_feature_info,
892 .get = mixer_ctl_feature_get,
893 .put = mixer_ctl_feature_put,
894};
895
896
897/*
898 * build a feature control
899 */
900
Takashi Iwai86e07d32005-11-17 15:08:02 +0100901static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100903 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 unsigned int len = 0;
906 int mapped_name = 0;
907 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100908 struct snd_kcontrol *kctl;
909 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 control++; /* change from zero-based to 1-based value */
912
913 if (control == USB_FEATURE_GEQ) {
914 /* FIXME: not supported yet */
915 return;
916 }
917
918 if (check_ignored_ctl(state, unitid, control))
919 return;
920
Takashi Iwai561b2202005-09-09 14:22:34 +0200921 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (! cval) {
923 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
924 return;
925 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200926 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 cval->id = unitid;
928 cval->control = control;
929 cval->cmask = ctl_mask;
930 cval->val_type = audio_feature_info[control-1].type;
931 if (ctl_mask == 0)
932 cval->channels = 1; /* master channel */
933 else {
934 int i, c = 0;
935 for (i = 0; i < 16; i++)
936 if (ctl_mask & (1 << i))
937 c++;
938 cval->channels = c;
939 }
940
941 /* get min/max values */
942 get_min_max(cval, 0);
943
944 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
945 if (! kctl) {
946 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
947 kfree(cval);
948 return;
949 }
950 kctl->private_free = usb_mixer_elem_free;
951
952 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
953 mapped_name = len != 0;
954 if (! len && nameid)
955 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
956
957 switch (control) {
958 case USB_FEATURE_MUTE:
959 case USB_FEATURE_VOLUME:
960 /* determine the control name. the rule is:
961 * - if a name id is given in descriptor, use it.
962 * - if the connected input can be determined, then use the name
963 * of terminal type.
964 * - if the connected output can be determined, use it.
965 * - otherwise, anonymous name.
966 */
967 if (! len) {
968 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
969 if (! len)
970 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
971 if (! len)
972 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
973 "Feature %d", unitid);
974 }
975 /* determine the stream direction:
976 * if the connected output is USB stream, then it's likely a
977 * capture stream. otherwise it should be playback (hopefully :)
978 */
979 if (! mapped_name && ! (state->oterm.type >> 16)) {
980 if ((state->oterm.type & 0xff00) == 0x0100) {
981 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
982 } else {
983 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
984 }
985 }
986 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
987 sizeof(kctl->id.name));
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200988 if (control == USB_FEATURE_VOLUME) {
989 kctl->tlv.c = mixer_vol_tlv;
990 kctl->vd[0].access |=
991 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
992 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995
996 default:
997 if (! len)
998 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
999 sizeof(kctl->id.name));
1000 break;
1001 }
1002
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001003 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001004 switch (state->chip->usb_id) {
1005 case USB_ID(0x0471, 0x0101):
1006 case USB_ID(0x0471, 0x0104):
1007 case USB_ID(0x0471, 0x0105):
1008 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001009 /* quirk for UDA1321/N101.
1010 * note that detection between firmware 2.1.1.7 (N101)
1011 * and later 2.1.1.21 is not very clear from datasheets.
1012 * I hope that the min value is -15360 for newer firmware --jk
1013 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001014 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1015 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001016 snd_printk(KERN_INFO
1017 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001018 cval->max = -256;
1019 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001020 break;
1021
1022 case USB_ID(0x046d, 0x09a4):
1023 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1024 snd_printk(KERN_INFO
1025 "set volume quirk for QuickCam E3500\n");
1026 cval->min = 6080;
1027 cval->max = 8768;
1028 cval->res = 192;
1029 }
1030 break;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1035 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001036 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039
1040
1041/*
1042 * parse a feature unit
1043 *
1044 * most of controlls are defined here.
1045 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001046static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001049 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned int master_bits, first_ch_bits;
1051 int err, csize;
1052
1053 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1054 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1055 return -EINVAL;
1056 }
1057
1058 /* parse the source unit */
1059 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1060 return err;
1061
1062 /* determine the input source type and name */
1063 if (check_input_term(state, ftr[4], &iterm) < 0)
1064 return -EINVAL;
1065
1066 channels = (ftr[0] - 7) / csize - 1;
1067
1068 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1069 if (channels > 0)
1070 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1071 else
1072 first_ch_bits = 0;
1073 /* check all control types */
1074 for (i = 0; i < 10; i++) {
1075 unsigned int ch_bits = 0;
1076 for (j = 0; j < channels; j++) {
1077 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1078 if (mask & (1 << i))
1079 ch_bits |= (1 << j);
1080 }
1081 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1082 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1083 if (master_bits & (1 << i))
1084 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1085 }
1086
1087 return 0;
1088}
1089
1090
1091/*
1092 * Mixer Unit
1093 */
1094
1095/*
1096 * build a mixer unit control
1097 *
1098 * the callbacks are identical with feature unit.
1099 * input channel number (zero based) is given in control field instead.
1100 */
1101
Takashi Iwai86e07d32005-11-17 15:08:02 +01001102static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001104 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001106 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unsigned int input_pins = desc[4];
1108 unsigned int num_outs = desc[5 + input_pins];
1109 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001110 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (check_ignored_ctl(state, unitid, 0))
1113 return;
1114
Takashi Iwai561b2202005-09-09 14:22:34 +02001115 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (! cval)
1117 return;
1118
Clemens Ladisch84957a82005-04-29 16:23:13 +02001119 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 cval->id = unitid;
1121 cval->control = in_ch + 1; /* based on 1 */
1122 cval->val_type = USB_MIXER_S16;
1123 for (i = 0; i < num_outs; i++) {
1124 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1125 cval->cmask |= (1 << i);
1126 cval->channels++;
1127 }
1128 }
1129
1130 /* get min/max values */
1131 get_min_max(cval, 0);
1132
1133 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1134 if (! kctl) {
1135 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1136 kfree(cval);
1137 return;
1138 }
1139 kctl->private_free = usb_mixer_elem_free;
1140
1141 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1142 if (! len)
1143 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1144 if (! len)
1145 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1146 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1147
1148 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1149 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001150 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153
1154/*
1155 * parse a mixer unit
1156 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001157static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001159 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int input_pins, num_ins, num_outs;
1161 int pin, ich, err;
1162
1163 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1164 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1165 return -EINVAL;
1166 }
1167 /* no bmControls field (e.g. Maya44) -> ignore */
1168 if (desc[0] <= 10 + input_pins) {
1169 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1170 return 0;
1171 }
1172
1173 num_ins = 0;
1174 ich = 0;
1175 for (pin = 0; pin < input_pins; pin++) {
1176 err = parse_audio_unit(state, desc[5 + pin]);
1177 if (err < 0)
1178 return err;
1179 err = check_input_term(state, desc[5 + pin], &iterm);
1180 if (err < 0)
1181 return err;
1182 num_ins += iterm.channels;
1183 for (; ich < num_ins; ++ich) {
1184 int och, ich_has_controls = 0;
1185
1186 for (och = 0; och < num_outs; ++och) {
1187 if (check_matrix_bitmap(desc + 9 + input_pins,
1188 ich, och, num_outs)) {
1189 ich_has_controls = 1;
1190 break;
1191 }
1192 }
1193 if (ich_has_controls)
1194 build_mixer_unit_ctl(state, desc, pin, ich,
1195 unitid, &iterm);
1196 }
1197 }
1198 return 0;
1199}
1200
1201
1202/*
1203 * Processing Unit / Extension Unit
1204 */
1205
1206/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001207static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001209 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int err, val;
1211
1212 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001213 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ucontrol->value.integer.value[0] = cval->min;
1215 return 0;
1216 }
1217 if (err < 0)
1218 return err;
1219 val = get_relative_value(cval, val);
1220 ucontrol->value.integer.value[0] = val;
1221 return 0;
1222}
1223
1224/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001225static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001227 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int val, oval, err;
1229
1230 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1231 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001232 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234 return err;
1235 }
1236 val = ucontrol->value.integer.value[0];
1237 val = get_abs_value(cval, val);
1238 if (val != oval) {
1239 set_cur_ctl_value(cval, cval->control << 8, val);
1240 return 1;
1241 }
1242 return 0;
1243}
1244
1245/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001246static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1248 .name = "", /* will be filled later */
1249 .info = mixer_ctl_feature_info,
1250 .get = mixer_ctl_procunit_get,
1251 .put = mixer_ctl_procunit_put,
1252};
1253
1254
1255/*
1256 * predefined data for processing units
1257 */
1258struct procunit_value_info {
1259 int control;
1260 char *suffix;
1261 int val_type;
1262 int min_value;
1263};
1264
1265struct procunit_info {
1266 int type;
1267 char *name;
1268 struct procunit_value_info *values;
1269};
1270
1271static struct procunit_value_info updown_proc_info[] = {
1272 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1273 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1274 { 0 }
1275};
1276static struct procunit_value_info prologic_proc_info[] = {
1277 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1278 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1279 { 0 }
1280};
1281static struct procunit_value_info threed_enh_proc_info[] = {
1282 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1283 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1284 { 0 }
1285};
1286static struct procunit_value_info reverb_proc_info[] = {
1287 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1288 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1289 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1290 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1291 { 0 }
1292};
1293static struct procunit_value_info chorus_proc_info[] = {
1294 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1295 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1296 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1297 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1298 { 0 }
1299};
1300static struct procunit_value_info dcr_proc_info[] = {
1301 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1302 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1303 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1304 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1305 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1306 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1307 { 0 }
1308};
1309
1310static struct procunit_info procunits[] = {
1311 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1312 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1313 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1314 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1315 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1316 { USB_PROC_DCR, "DCR", dcr_proc_info },
1317 { 0 },
1318};
1319
1320/*
1321 * build a processing/extension unit
1322 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001323static 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 -07001324{
1325 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001326 struct usb_mixer_elem_info *cval;
1327 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int i, err, nameid, type, len;
1329 struct procunit_info *info;
1330 struct procunit_value_info *valinfo;
1331 static struct procunit_value_info default_value_info[] = {
1332 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1333 { 0 }
1334 };
1335 static struct procunit_info default_info = {
1336 0, NULL, default_value_info
1337 };
1338
1339 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1340 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1341 return -EINVAL;
1342 }
1343
1344 for (i = 0; i < num_ins; i++) {
1345 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1346 return err;
1347 }
1348
1349 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 for (info = list; info && info->type; info++)
1351 if (info->type == type)
1352 break;
1353 if (! info || ! info->type)
1354 info = &default_info;
1355
1356 for (valinfo = info->values; valinfo->control; valinfo++) {
1357 /* FIXME: bitmap might be longer than 8bit */
1358 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1359 continue;
1360 if (check_ignored_ctl(state, unitid, valinfo->control))
1361 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001362 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (! cval) {
1364 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1365 return -ENOMEM;
1366 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001367 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 cval->id = unitid;
1369 cval->control = valinfo->control;
1370 cval->val_type = valinfo->val_type;
1371 cval->channels = 1;
1372
1373 /* get min/max values */
1374 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1375 /* FIXME: hard-coded */
1376 cval->min = 1;
1377 cval->max = dsc[15];
1378 cval->res = 1;
1379 cval->initialized = 1;
1380 } else
1381 get_min_max(cval, valinfo->min_value);
1382
1383 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1384 if (! kctl) {
1385 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1386 kfree(cval);
1387 return -ENOMEM;
1388 }
1389 kctl->private_free = usb_mixer_elem_free;
1390
1391 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1392 ;
1393 else if (info->name)
1394 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1395 else {
1396 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1397 len = 0;
1398 if (nameid)
1399 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1400 if (! len)
1401 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1402 }
1403 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1404 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1405
1406 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1407 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001408 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return err;
1410 }
1411 return 0;
1412}
1413
1414
Takashi Iwai86e07d32005-11-17 15:08:02 +01001415static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1418}
1419
Takashi Iwai86e07d32005-11-17 15:08:02 +01001420static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1423}
1424
1425
1426/*
1427 * Selector Unit
1428 */
1429
1430/* info callback for selector unit
1431 * use an enumerator type for routing
1432 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001433static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001435 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 char **itemlist = (char **)kcontrol->private_value;
1437
Takashi Iwai5e246b82008-08-08 17:12:47 +02001438 if (snd_BUG_ON(!itemlist))
1439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1441 uinfo->count = 1;
1442 uinfo->value.enumerated.items = cval->max;
1443 if ((int)uinfo->value.enumerated.item >= cval->max)
1444 uinfo->value.enumerated.item = cval->max - 1;
1445 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1446 return 0;
1447}
1448
1449/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001450static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001452 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int val, err;
1454
1455 err = get_cur_ctl_value(cval, 0, &val);
1456 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001457 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ucontrol->value.enumerated.item[0] = 0;
1459 return 0;
1460 }
1461 return err;
1462 }
1463 val = get_relative_value(cval, val);
1464 ucontrol->value.enumerated.item[0] = val;
1465 return 0;
1466}
1467
1468/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001469static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001471 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int val, oval, err;
1473
1474 err = get_cur_ctl_value(cval, 0, &oval);
1475 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001476 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478 return err;
1479 }
1480 val = ucontrol->value.enumerated.item[0];
1481 val = get_abs_value(cval, val);
1482 if (val != oval) {
1483 set_cur_ctl_value(cval, 0, val);
1484 return 1;
1485 }
1486 return 0;
1487}
1488
1489/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001490static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1492 .name = "", /* will be filled later */
1493 .info = mixer_ctl_selector_info,
1494 .get = mixer_ctl_selector_get,
1495 .put = mixer_ctl_selector_put,
1496};
1497
1498
1499/* private free callback.
1500 * free both private_data and private_value
1501 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001502static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 int i, num_ins = 0;
1505
1506 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001507 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 num_ins = cval->max;
1509 kfree(cval);
1510 kctl->private_data = NULL;
1511 }
1512 if (kctl->private_value) {
1513 char **itemlist = (char **)kctl->private_value;
1514 for (i = 0; i < num_ins; i++)
1515 kfree(itemlist[i]);
1516 kfree(itemlist);
1517 kctl->private_value = 0;
1518 }
1519}
1520
1521/*
1522 * parse a selector unit
1523 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001524static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 unsigned int num_ins = desc[4];
1527 unsigned int i, nameid, len;
1528 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001529 struct usb_mixer_elem_info *cval;
1530 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 char **namelist;
1532
Russ Cox38977e92007-08-06 15:37:58 +02001533 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1535 return -EINVAL;
1536 }
1537
1538 for (i = 0; i < num_ins; i++) {
1539 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1540 return err;
1541 }
1542
1543 if (num_ins == 1) /* only one ? nonsense! */
1544 return 0;
1545
1546 if (check_ignored_ctl(state, unitid, 0))
1547 return 0;
1548
Takashi Iwai561b2202005-09-09 14:22:34 +02001549 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (! cval) {
1551 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1552 return -ENOMEM;
1553 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001554 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 cval->id = unitid;
1556 cval->val_type = USB_MIXER_U8;
1557 cval->channels = 1;
1558 cval->min = 1;
1559 cval->max = num_ins;
1560 cval->res = 1;
1561 cval->initialized = 1;
1562
1563 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1564 if (! namelist) {
1565 snd_printk(KERN_ERR "cannot malloc\n");
1566 kfree(cval);
1567 return -ENOMEM;
1568 }
1569#define MAX_ITEM_NAME_LEN 64
1570 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001571 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 len = 0;
1573 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1574 if (! namelist[i]) {
1575 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001576 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 kfree(namelist[i]);
1578 kfree(namelist);
1579 kfree(cval);
1580 return -ENOMEM;
1581 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001582 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1583 MAX_ITEM_NAME_LEN);
1584 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1586 if (! len)
1587 sprintf(namelist[i], "Input %d", i);
1588 }
1589
1590 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1591 if (! kctl) {
1592 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001593 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 kfree(cval);
1595 return -ENOMEM;
1596 }
1597 kctl->private_value = (unsigned long)namelist;
1598 kctl->private_free = usb_mixer_selector_elem_free;
1599
1600 nameid = desc[desc[0] - 1];
1601 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1602 if (len)
1603 ;
1604 else if (nameid)
1605 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1606 else {
1607 len = get_term_name(state, &state->oterm,
1608 kctl->id.name, sizeof(kctl->id.name), 0);
1609 if (! len)
1610 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1611
1612 if ((state->oterm.type & 0xff00) == 0x0100)
1613 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1614 else
1615 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1616 }
1617
1618 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1619 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001620 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return err;
1622
1623 return 0;
1624}
1625
1626
1627/*
1628 * parse an audio unit recursively
1629 */
1630
Takashi Iwai86e07d32005-11-17 15:08:02 +01001631static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 unsigned char *p1;
1634
1635 if (test_and_set_bit(unitid, state->unitbitmap))
1636 return 0; /* the unit already visited */
1637
1638 p1 = find_audio_control_unit(state, unitid);
1639 if (!p1) {
1640 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1641 return -EINVAL;
1642 }
1643
1644 switch (p1[2]) {
1645 case INPUT_TERMINAL:
1646 return 0; /* NOP */
1647 case MIXER_UNIT:
1648 return parse_audio_mixer_unit(state, unitid, p1);
1649 case SELECTOR_UNIT:
1650 return parse_audio_selector_unit(state, unitid, p1);
1651 case FEATURE_UNIT:
1652 return parse_audio_feature_unit(state, unitid, p1);
1653 case PROCESSING_UNIT:
1654 return parse_audio_processing_unit(state, unitid, p1);
1655 case EXTENSION_UNIT:
1656 return parse_audio_extension_unit(state, unitid, p1);
1657 default:
1658 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1659 return -EINVAL;
1660 }
1661}
1662
Clemens Ladisch84957a82005-04-29 16:23:13 +02001663static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1664{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001665 kfree(mixer->id_elems);
1666 if (mixer->urb) {
1667 kfree(mixer->urb->transfer_buffer);
1668 usb_free_urb(mixer->urb);
1669 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001670 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001671 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001672 kfree(mixer);
1673}
1674
Takashi Iwai86e07d32005-11-17 15:08:02 +01001675static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001676{
1677 struct usb_mixer_interface *mixer = device->device_data;
1678 snd_usb_mixer_free(mixer);
1679 return 0;
1680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/*
1683 * create mixer controls
1684 *
1685 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1686 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001687static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001690 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int err;
1692 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001693 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Clemens Ladisch84957a82005-04-29 16:23:13 +02001695 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001697 state.chip = mixer->chip;
1698 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 state.buffer = hostif->extra;
1700 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001703 for (map = usbmix_ctl_maps; map->id; map++) {
1704 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001706 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001707 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 }
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 desc = NULL;
1713 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1714 if (desc[0] < 9)
1715 continue; /* invalid descriptor? */
1716 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1717 state.oterm.id = desc[3];
1718 state.oterm.type = combine_word(&desc[4]);
1719 state.oterm.name = desc[8];
1720 err = parse_audio_unit(&state, desc[7]);
1721 if (err < 0)
1722 return err;
1723 }
1724 return 0;
1725}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001726
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001727static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1728 int unitid)
1729{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001730 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001731
1732 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1733 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1734 info->elem_id);
1735}
1736
Clemens Ladischb259b102005-04-29 16:29:28 +02001737static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1738 int unitid)
1739{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001740 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001741 return;
1742 /* unit ids specific to Extigy/Audigy 2 NX: */
1743 switch (unitid) {
1744 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001745 mixer->rc_urb->dev = mixer->chip->dev;
1746 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001747 break;
1748 case 4: /* digital in jack */
1749 case 7: /* line in jacks */
1750 case 19: /* speaker out jacks */
1751 case 20: /* headphones out jack */
1752 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001753 /* live24ext: 4 = line-in jack */
1754 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001755 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1756 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001757 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1758 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001759 default:
1760 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1761 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001762 }
1763}
1764
David Howells7d12e782006-10-05 14:55:46 +01001765static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001766{
1767 struct usb_mixer_interface *mixer = urb->context;
1768
1769 if (urb->status == 0) {
1770 u8 *buf = urb->transfer_buffer;
1771 int i;
1772
1773 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1774 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1775 buf[0], buf[1]);
1776 /* ignore any notifications not from the control interface */
1777 if ((buf[0] & 0x0f) != 0)
1778 continue;
1779 if (!(buf[0] & 0x40))
1780 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001781 else
1782 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001783 }
1784 }
1785 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1786 urb->dev = mixer->chip->dev;
1787 usb_submit_urb(urb, GFP_ATOMIC);
1788 }
1789}
1790
1791/* create the handler for the optional status interrupt endpoint */
1792static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1793{
1794 struct usb_host_interface *hostif;
1795 struct usb_endpoint_descriptor *ep;
1796 void *transfer_buffer;
1797 int buffer_length;
1798 unsigned int epnum;
1799
1800 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1801 /* we need one interrupt input endpoint */
1802 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1803 return 0;
1804 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001805 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001806 return 0;
1807
Julia Lawall42a6e662008-12-29 11:23:02 +01001808 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001809 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1810 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1811 if (!transfer_buffer)
1812 return -ENOMEM;
1813 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1814 if (!mixer->urb) {
1815 kfree(transfer_buffer);
1816 return -ENOMEM;
1817 }
1818 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1819 usb_rcvintpipe(mixer->chip->dev, epnum),
1820 transfer_buffer, buffer_length,
1821 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1822 usb_submit_urb(mixer->urb, GFP_KERNEL);
1823 return 0;
1824}
1825
David Howells7d12e782006-10-05 14:55:46 +01001826static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001827{
1828 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001829 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001830 u32 code;
1831
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001832 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001833 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001834
1835 code = mixer->rc_buffer[rc->offset];
1836 if (rc->length == 2)
1837 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1838
Clemens Ladischb259b102005-04-29 16:29:28 +02001839 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001840 if (code == rc->mute_code)
1841 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001842 mixer->rc_code = code;
1843 wmb();
1844 wake_up(&mixer->rc_waitq);
1845}
1846
Takashi Iwai86e07d32005-11-17 15:08:02 +01001847static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001848 long count, loff_t *offset)
1849{
1850 struct usb_mixer_interface *mixer = hw->private_data;
1851 int err;
1852 u32 rc_code;
1853
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001854 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001855 return -EINVAL;
1856 err = wait_event_interruptible(mixer->rc_waitq,
1857 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1858 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001859 if (count == 1)
1860 err = put_user(rc_code, buf);
1861 else
1862 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001863 }
1864 return err < 0 ? err : count;
1865}
1866
Takashi Iwai86e07d32005-11-17 15:08:02 +01001867static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001868 poll_table *wait)
1869{
1870 struct usb_mixer_interface *mixer = hw->private_data;
1871
1872 poll_wait(file, &mixer->rc_waitq, wait);
1873 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1874}
1875
1876static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1877{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001878 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001879 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02001880
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001881 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1882 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1883 break;
1884 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02001885 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001886 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02001887
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001888 len = mixer->rc_cfg->packet_length;
1889
Clemens Ladischb259b102005-04-29 16:29:28 +02001890 init_waitqueue_head(&mixer->rc_waitq);
1891 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1892 if (err < 0)
1893 return err;
1894 snprintf(hwdep->name, sizeof(hwdep->name),
1895 "%s remote control", mixer->chip->card->shortname);
1896 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1897 hwdep->private_data = mixer;
1898 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02001899 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01001900 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02001901
1902 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1903 if (!mixer->rc_urb)
1904 return -ENOMEM;
1905 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1906 if (!mixer->rc_setup_packet) {
1907 usb_free_urb(mixer->rc_urb);
1908 mixer->rc_urb = NULL;
1909 return -ENOMEM;
1910 }
1911 mixer->rc_setup_packet->bRequestType =
1912 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1913 mixer->rc_setup_packet->bRequest = GET_MEM;
1914 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1915 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1916 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1917 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1918 usb_rcvctrlpipe(mixer->chip->dev, 0),
1919 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1920 snd_usb_soundblaster_remote_complete, mixer);
1921 return 0;
1922}
1923
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001924#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001925
Takashi Iwai86e07d32005-11-17 15:08:02 +01001926static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001927{
1928 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1929 int index = kcontrol->private_value;
1930
1931 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1932 return 0;
1933}
1934
Takashi Iwai86e07d32005-11-17 15:08:02 +01001935static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001936{
1937 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1938 int index = kcontrol->private_value;
1939 int value = ucontrol->value.integer.value[0];
1940 int err, changed;
1941
1942 if (value > 1)
1943 return -EINVAL;
1944 changed = value != mixer->audigy2nx_leds[index];
1945 err = snd_usb_ctl_msg(mixer->chip->dev,
1946 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1947 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1948 value, index + 2, NULL, 0, 100);
1949 if (err < 0)
1950 return err;
1951 mixer->audigy2nx_leds[index] = value;
1952 return changed;
1953}
1954
Takashi Iwai86e07d32005-11-17 15:08:02 +01001955static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001956 {
1957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958 .name = "CMSS LED Switch",
1959 .info = snd_audigy2nx_led_info,
1960 .get = snd_audigy2nx_led_get,
1961 .put = snd_audigy2nx_led_put,
1962 .private_value = 0,
1963 },
1964 {
1965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1966 .name = "Power LED Switch",
1967 .info = snd_audigy2nx_led_info,
1968 .get = snd_audigy2nx_led_get,
1969 .put = snd_audigy2nx_led_put,
1970 .private_value = 1,
1971 },
1972 {
1973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1974 .name = "Dolby Digital LED Switch",
1975 .info = snd_audigy2nx_led_info,
1976 .get = snd_audigy2nx_led_get,
1977 .put = snd_audigy2nx_led_put,
1978 .private_value = 2,
1979 },
1980};
1981
1982static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1983{
1984 int i, err;
1985
1986 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01001987 if (i > 1 && /* Live24ext has 2 LEDs only */
1988 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1989 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001990 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001991 err = snd_ctl_add(mixer->chip->card,
1992 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1993 if (err < 0)
1994 return err;
1995 }
1996 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1997 return 0;
1998}
1999
Takashi Iwai86e07d32005-11-17 15:08:02 +01002000static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2001 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02002002{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002003 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02002004 int unitid;
2005 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002006 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02002007 {4, "dig in "},
2008 {7, "line in"},
2009 {19, "spk out"},
2010 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002011 {-1, NULL}
2012 }, jacks_live24ext[] = {
2013 {4, "line in"}, /* &1=Line, &2=Mic*/
2014 {3, "hph out"}, /* headphones */
2015 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2016 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002017 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002018 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002019 struct usb_mixer_interface *mixer = entry->private_data;
2020 int i, err;
2021 u8 buf[3];
2022
2023 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002024 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2025 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002026 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2027 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002028 jacks = jacks_live24ext;
2029 else
2030 return;
2031
2032 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002033 snd_iprintf(buffer, "%s: ", jacks[i].name);
2034 err = snd_usb_ctl_msg(mixer->chip->dev,
2035 usb_rcvctrlpipe(mixer->chip->dev, 0),
2036 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2037 USB_RECIP_INTERFACE, 0,
2038 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002039 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002040 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2041 else
2042 snd_iprintf(buffer, "?\n");
2043 }
2044}
2045
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002046static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
2048{
2049 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2050
2051 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2052 return 0;
2053}
2054
2055static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2059 u8 old_status, new_status;
2060 int err, changed;
2061
2062 old_status = mixer->xonar_u1_status;
2063 if (ucontrol->value.integer.value[0])
2064 new_status = old_status | 0x02;
2065 else
2066 new_status = old_status & ~0x02;
2067 changed = new_status != old_status;
2068 err = snd_usb_ctl_msg(mixer->chip->dev,
2069 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2070 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2071 50, 0, &new_status, 1, 100);
2072 if (err < 0)
2073 return err;
2074 mixer->xonar_u1_status = new_status;
2075 return changed;
2076}
2077
2078static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2080 .name = "Digital Playback Switch",
2081 .info = snd_ctl_boolean_mono_info,
2082 .get = snd_xonar_u1_switch_get,
2083 .put = snd_xonar_u1_switch_put,
2084};
2085
2086static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2087{
2088 int err;
2089
2090 err = snd_ctl_add(mixer->chip->card,
2091 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2092 if (err < 0)
2093 return err;
2094 mixer->xonar_u1_status = 0x05;
2095 return 0;
2096}
2097
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002098int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2099 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002100{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002101 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002102 .dev_free = snd_usb_mixer_dev_free
2103 };
2104 struct usb_mixer_interface *mixer;
2105 int err;
2106
2107 strcpy(chip->card->mixername, "USB Mixer");
2108
Takashi Iwai561b2202005-09-09 14:22:34 +02002109 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002110 if (!mixer)
2111 return -ENOMEM;
2112 mixer->chip = chip;
2113 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002114 mixer->ignore_ctl_error = ignore_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002115 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2116 if (!mixer->id_elems) {
2117 kfree(mixer);
2118 return -ENOMEM;
2119 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002120
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002121 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002122 (err = snd_usb_mixer_status_create(mixer)) < 0)
2123 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002124
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002125 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2126 goto _error;
2127
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002128 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002129 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2130 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002131 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02002132
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002133 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2134 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002135 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002136 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002137 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002138 }
2139
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002140 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2141 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2142 err = snd_xonar_u1_controls_create(mixer);
2143 if (err < 0)
2144 goto _error;
2145 }
2146
Clemens Ladisch84957a82005-04-29 16:23:13 +02002147 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002148 if (err < 0)
2149 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002150 list_add(&mixer->list, &chip->mixer_list);
2151 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002152
2153_error:
2154 snd_usb_mixer_free(mixer);
2155 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002156}
2157
2158void snd_usb_mixer_disconnect(struct list_head *p)
2159{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002160 struct usb_mixer_interface *mixer;
2161
2162 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002163 usb_kill_urb(mixer->urb);
2164 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002165}