blob: 89c63d073cc6fac8f0c00ca1646377994157c64b [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;
62 u8 mute_mixer_id;
63 u32 mute_code;
64} rc_configs[] = {
65 { USB_ID(0x041e, 0x3000), 0, 1, 2, 18, 0x0013 }, /* Extigy */
66 { USB_ID(0x041e, 0x3020), 2, 1, 6, 18, 0x0013 }, /* Audigy 2 NX */
67 { USB_ID(0x041e, 0x3040), 2, 2, 6, 2, 0x6e91 }, /* Live! 24-bit */
68};
69
Clemens Ladisch84957a82005-04-29 16:23:13 +020070struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010071 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020072 unsigned int ctrlif;
73 struct list_head list;
74 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020075 struct urb *urb;
Takashi Iwai86e07d32005-11-17 15:08:02 +010076 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
Clemens Ladischb259b102005-04-29 16:29:28 +020077
78 /* Sound Blaster remote control stuff */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020079 const struct rc_config *rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +020080 unsigned long rc_hwdep_open;
81 u32 rc_code;
82 wait_queue_head_t rc_waitq;
83 struct urb *rc_urb;
84 struct usb_ctrlrequest *rc_setup_packet;
85 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020086
87 u8 audigy2nx_leds[3];
Clemens Ladisch84957a82005-04-29 16:23:13 +020088};
89
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct usb_audio_term {
92 int id;
93 int type;
94 int channels;
95 unsigned int chconfig;
96 int name;
97};
98
99struct usbmix_name_map;
100
Takashi Iwai86e07d32005-11-17 15:08:02 +0100101struct mixer_build {
102 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200103 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned char *buffer;
105 unsigned int buflen;
Clemens Ladisch707e6072005-04-29 09:56:17 +0200106 DECLARE_BITMAP(unitbitmap, 256);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100107 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200109 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200113 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100114 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
115 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned int id;
117 unsigned int control; /* CS or ICN (high byte) */
118 unsigned int cmask; /* channel mask bitmap: 0 = master */
119 int channels;
120 int val_type;
121 int min, max, res;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200122 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125
126enum {
127 USB_FEATURE_NONE = 0,
128 USB_FEATURE_MUTE = 1,
129 USB_FEATURE_VOLUME,
130 USB_FEATURE_BASS,
131 USB_FEATURE_MID,
132 USB_FEATURE_TREBLE,
133 USB_FEATURE_GEQ,
134 USB_FEATURE_AGC,
135 USB_FEATURE_DELAY,
136 USB_FEATURE_BASSBOOST,
137 USB_FEATURE_LOUDNESS
138};
139
140enum {
141 USB_MIXER_BOOLEAN,
142 USB_MIXER_INV_BOOLEAN,
143 USB_MIXER_S8,
144 USB_MIXER_U8,
145 USB_MIXER_S16,
146 USB_MIXER_U16,
147};
148
149enum {
150 USB_PROC_UPDOWN = 1,
151 USB_PROC_UPDOWN_SWITCH = 1,
152 USB_PROC_UPDOWN_MODE_SEL = 2,
153
154 USB_PROC_PROLOGIC = 2,
155 USB_PROC_PROLOGIC_SWITCH = 1,
156 USB_PROC_PROLOGIC_MODE_SEL = 2,
157
158 USB_PROC_3DENH = 3,
159 USB_PROC_3DENH_SWITCH = 1,
160 USB_PROC_3DENH_SPACE = 2,
161
162 USB_PROC_REVERB = 4,
163 USB_PROC_REVERB_SWITCH = 1,
164 USB_PROC_REVERB_LEVEL = 2,
165 USB_PROC_REVERB_TIME = 3,
166 USB_PROC_REVERB_DELAY = 4,
167
168 USB_PROC_CHORUS = 5,
169 USB_PROC_CHORUS_SWITCH = 1,
170 USB_PROC_CHORUS_LEVEL = 2,
171 USB_PROC_CHORUS_RATE = 3,
172 USB_PROC_CHORUS_DEPTH = 4,
173
174 USB_PROC_DCR = 6,
175 USB_PROC_DCR_SWITCH = 1,
176 USB_PROC_DCR_RATIO = 2,
177 USB_PROC_DCR_MAX_AMP = 3,
178 USB_PROC_DCR_THRESHOLD = 4,
179 USB_PROC_DCR_ATTACK = 5,
180 USB_PROC_DCR_RELEASE = 6,
181};
182
183#define MAX_CHANNELS 10 /* max logical channels */
184
185
186/*
187 * manual mapping of mixer names
188 * if the mixer topology is too complicated and the parsed names are
189 * ambiguous, add the entries in usbmixer_maps.c.
190 */
191#include "usbmixer_maps.c"
192
193/* get the mapped name if the unit matches */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100194static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 const struct usbmix_name_map *p;
197
198 if (! state->map)
199 return 0;
200
201 for (p = state->map; p->id; p++) {
202 if (p->id == unitid && p->name &&
203 (! control || ! p->control || control == p->control)) {
204 buflen--;
205 return strlcpy(buf, p->name, buflen);
206 }
207 }
208 return 0;
209}
210
211/* check whether the control should be ignored */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100212static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 const struct usbmix_name_map *p;
215
216 if (! state->map)
217 return 0;
218 for (p = state->map; p->id; p++) {
219 if (p->id == unitid && ! p->name &&
220 (! control || ! p->control || control == p->control)) {
221 // printk("ignored control %d:%d\n", unitid, control);
222 return 1;
223 }
224 }
225 return 0;
226}
227
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200228/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100229static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200230 int index, char *buf, int buflen)
231{
232 const struct usbmix_selector_map *p;
233
234 if (! state->selector_map)
235 return 0;
236 for (p = state->selector_map; p->id; p++) {
237 if (p->id == unitid && index < p->count)
238 return strlcpy(buf, p->names[index], buflen);
239 }
240 return 0;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
244 * find an audio control unit with the given unit id
245 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100246static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned char *p;
249
250 p = NULL;
251 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
252 USB_DT_CS_INTERFACE)) != NULL) {
253 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
254 return p;
255 }
256 return NULL;
257}
258
259
260/*
261 * copy a string with the given id
262 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100263static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
266 buf[len] = 0;
267 return len;
268}
269
270/*
271 * convert from the byte/word on usb descriptor to the zero-based integer
272 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100273static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 switch (cval->val_type) {
276 case USB_MIXER_BOOLEAN:
277 return !!val;
278 case USB_MIXER_INV_BOOLEAN:
279 return !val;
280 case USB_MIXER_U8:
281 val &= 0xff;
282 break;
283 case USB_MIXER_S8:
284 val &= 0xff;
285 if (val >= 0x80)
286 val -= 0x100;
287 break;
288 case USB_MIXER_U16:
289 val &= 0xffff;
290 break;
291 case USB_MIXER_S16:
292 val &= 0xffff;
293 if (val >= 0x8000)
294 val -= 0x10000;
295 break;
296 }
297 return val;
298}
299
300/*
301 * convert from the zero-based int to the byte/word for usb descriptor
302 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100303static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 switch (cval->val_type) {
306 case USB_MIXER_BOOLEAN:
307 return !!val;
308 case USB_MIXER_INV_BOOLEAN:
309 return !val;
310 case USB_MIXER_S8:
311 case USB_MIXER_U8:
312 return val & 0xff;
313 case USB_MIXER_S16:
314 case USB_MIXER_U16:
315 return val & 0xffff;
316 }
317 return 0; /* not reached */
318}
319
Takashi Iwai86e07d32005-11-17 15:08:02 +0100320static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 if (! cval->res)
323 cval->res = 1;
324 if (val < cval->min)
325 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200326 else if (val >= cval->max)
327 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 else
329 return (val - cval->min) / cval->res;
330}
331
Takashi Iwai86e07d32005-11-17 15:08:02 +0100332static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 if (val < 0)
335 return cval->min;
336 if (! cval->res)
337 cval->res = 1;
338 val *= cval->res;
339 val += cval->min;
340 if (val > cval->max)
341 return cval->max;
342 return val;
343}
344
345
346/*
347 * retrieve a mixer value
348 */
349
Takashi Iwai86e07d32005-11-17 15:08:02 +0100350static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned char buf[2];
353 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
354 int timeout = 10;
355
356 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200357 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
358 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 request,
360 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200361 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200362 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
364 return 0;
365 }
366 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200367 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
368 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EINVAL;
370}
371
Takashi Iwai86e07d32005-11-17 15:08:02 +0100372static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 return get_ctl_value(cval, GET_CUR, validx, value);
375}
376
377/* channel = 0: master, 1 = first channel */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100378static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
381}
382
383/*
384 * set a mixer value
385 */
386
Takashi Iwai86e07d32005-11-17 15:08:02 +0100387static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 unsigned char buf[2];
390 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
391 int timeout = 10;
392
393 value_set = convert_bytes_value(cval, value_set);
394 buf[0] = value_set & 0xff;
395 buf[1] = (value_set >> 8) & 0xff;
396 while (timeout -- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200397 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
398 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 request,
400 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200401 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 buf, val_len, 100) >= 0)
403 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200404 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
405 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -EINVAL;
407}
408
Takashi Iwai86e07d32005-11-17 15:08:02 +0100409static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 return set_ctl_value(cval, SET_CUR, validx, value);
412}
413
Takashi Iwai86e07d32005-11-17 15:08:02 +0100414static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
417}
418
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200419/*
420 * TLV callback for mixer volume controls
421 */
422static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
423 unsigned int size, unsigned int __user *_tlv)
424{
425 struct usb_mixer_elem_info *cval = kcontrol->private_data;
426 DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
427
428 if (size < sizeof(scale))
429 return -ENOMEM;
430 /* USB descriptions contain the dB scale in 1/256 dB unit
431 * while ALSA TLV contains in 1/100 dB unit
432 */
433 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
434 scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
435 if (copy_to_user(_tlv, scale, sizeof(scale)))
436 return -EFAULT;
437 return 0;
438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/*
441 * parser routines begin here...
442 */
443
Takashi Iwai86e07d32005-11-17 15:08:02 +0100444static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446
447/*
448 * check if the input/output channel routing is enabled on the given bitmap.
449 * used for mixer unit parser
450 */
451static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
452{
453 int idx = ich * num_outs + och;
454 return bmap[idx >> 3] & (0x80 >> (idx & 7));
455}
456
457
458/*
459 * add an alsa control element
460 * search and increment the index until an empty slot is found.
461 *
462 * if failed, give up and free the control instance.
463 */
464
Takashi Iwai86e07d32005-11-17 15:08:02 +0100465static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100467 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200469
470 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200472 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200476 cval->elem_id = &kctl->id;
477 cval->next_id_elem = state->mixer->id_elems[cval->id];
478 state->mixer->id_elems[cval->id] = cval;
479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482
483/*
484 * get a terminal name string
485 */
486
487static struct iterm_name_combo {
488 int type;
489 char *name;
490} iterm_names[] = {
491 { 0x0300, "Output" },
492 { 0x0301, "Speaker" },
493 { 0x0302, "Headphone" },
494 { 0x0303, "HMD Audio" },
495 { 0x0304, "Desktop Speaker" },
496 { 0x0305, "Room Speaker" },
497 { 0x0306, "Com Speaker" },
498 { 0x0307, "LFE" },
499 { 0x0600, "External In" },
500 { 0x0601, "Analog In" },
501 { 0x0602, "Digital In" },
502 { 0x0603, "Line" },
503 { 0x0604, "Legacy In" },
504 { 0x0605, "IEC958 In" },
505 { 0x0606, "1394 DA Stream" },
506 { 0x0607, "1394 DV Stream" },
507 { 0x0700, "Embedded" },
508 { 0x0701, "Noise Source" },
509 { 0x0702, "Equalization Noise" },
510 { 0x0703, "CD" },
511 { 0x0704, "DAT" },
512 { 0x0705, "DCC" },
513 { 0x0706, "MiniDisk" },
514 { 0x0707, "Analog Tape" },
515 { 0x0708, "Phonograph" },
516 { 0x0709, "VCR Audio" },
517 { 0x070a, "Video Disk Audio" },
518 { 0x070b, "DVD Audio" },
519 { 0x070c, "TV Tuner Audio" },
520 { 0x070d, "Satellite Rec Audio" },
521 { 0x070e, "Cable Tuner Audio" },
522 { 0x070f, "DSS Audio" },
523 { 0x0710, "Radio Receiver" },
524 { 0x0711, "Radio Transmitter" },
525 { 0x0712, "Multi-Track Recorder" },
526 { 0x0713, "Synthesizer" },
527 { 0 },
528};
529
Takashi Iwai86e07d32005-11-17 15:08:02 +0100530static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 unsigned char *name, int maxlen, int term_only)
532{
533 struct iterm_name_combo *names;
534
535 if (iterm->name)
536 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
537
538 /* virtual type - not a real terminal */
539 if (iterm->type >> 16) {
540 if (term_only)
541 return 0;
542 switch (iterm->type >> 16) {
543 case SELECTOR_UNIT:
544 strcpy(name, "Selector"); return 8;
545 case PROCESSING_UNIT:
546 strcpy(name, "Process Unit"); return 12;
547 case EXTENSION_UNIT:
548 strcpy(name, "Ext Unit"); return 8;
549 case MIXER_UNIT:
550 strcpy(name, "Mixer"); return 5;
551 default:
552 return sprintf(name, "Unit %d", iterm->id);
553 }
554 }
555
556 switch (iterm->type & 0xff00) {
557 case 0x0100:
558 strcpy(name, "PCM"); return 3;
559 case 0x0200:
560 strcpy(name, "Mic"); return 3;
561 case 0x0400:
562 strcpy(name, "Headset"); return 7;
563 case 0x0500:
564 strcpy(name, "Phone"); return 5;
565 }
566
567 for (names = iterm_names; names->type; names++)
568 if (names->type == iterm->type) {
569 strcpy(name, names->name);
570 return strlen(names->name);
571 }
572 return 0;
573}
574
575
576/*
577 * parse the source unit recursively until it reaches to a terminal
578 * or a branched unit.
579 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100580static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 unsigned char *p1;
583
584 memset(term, 0, sizeof(*term));
585 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
586 term->id = id;
587 switch (p1[2]) {
588 case INPUT_TERMINAL:
589 term->type = combine_word(p1 + 4);
590 term->channels = p1[7];
591 term->chconfig = combine_word(p1 + 8);
592 term->name = p1[11];
593 return 0;
594 case FEATURE_UNIT:
595 id = p1[4];
596 break; /* continue to parse */
597 case MIXER_UNIT:
598 term->type = p1[2] << 16; /* virtual type */
599 term->channels = p1[5 + p1[4]];
600 term->chconfig = combine_word(p1 + 6 + p1[4]);
601 term->name = p1[p1[0] - 1];
602 return 0;
603 case SELECTOR_UNIT:
604 /* call recursively to retrieve the channel info */
605 if (check_input_term(state, p1[5], term) < 0)
606 return -ENODEV;
607 term->type = p1[2] << 16; /* virtual type */
608 term->id = id;
609 term->name = p1[9 + p1[0] - 1];
610 return 0;
611 case PROCESSING_UNIT:
612 case EXTENSION_UNIT:
613 if (p1[6] == 1) {
614 id = p1[7];
615 break; /* continue to parse */
616 }
617 term->type = p1[2] << 16; /* virtual type */
618 term->channels = p1[7 + p1[6]];
619 term->chconfig = combine_word(p1 + 8 + p1[6]);
620 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
621 return 0;
622 default:
623 return -ENODEV;
624 }
625 }
626 return -ENODEV;
627}
628
629
630/*
631 * Feature Unit
632 */
633
634/* feature unit control information */
635struct usb_feature_control_info {
636 const char *name;
637 unsigned int type; /* control type (mute, volume, etc.) */
638};
639
640static struct usb_feature_control_info audio_feature_info[] = {
641 { "Mute", USB_MIXER_INV_BOOLEAN },
642 { "Volume", USB_MIXER_S16 },
643 { "Tone Control - Bass", USB_MIXER_S8 },
644 { "Tone Control - Mid", USB_MIXER_S8 },
645 { "Tone Control - Treble", USB_MIXER_S8 },
646 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
647 { "Auto Gain Control", USB_MIXER_BOOLEAN },
648 { "Delay Control", USB_MIXER_U16 },
649 { "Bass Boost", USB_MIXER_BOOLEAN },
650 { "Loudness", USB_MIXER_BOOLEAN },
651};
652
653
654/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100655static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Jesper Juhl4d572772005-05-30 17:30:32 +0200657 kfree(kctl->private_data);
658 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661
662/*
663 * interface to ALSA control for feature/mixer units
664 */
665
666/*
667 * retrieve the minimum and maximum values for the specified control
668 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100669static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 /* for failsafe */
672 cval->min = default_min;
673 cval->max = cval->min + 1;
674 cval->res = 1;
675
676 if (cval->val_type == USB_MIXER_BOOLEAN ||
677 cval->val_type == USB_MIXER_INV_BOOLEAN) {
678 cval->initialized = 1;
679 } else {
680 int minchn = 0;
681 if (cval->cmask) {
682 int i;
683 for (i = 0; i < MAX_CHANNELS; i++)
684 if (cval->cmask & (1 << i)) {
685 minchn = i + 1;
686 break;
687 }
688 }
689 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
690 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200691 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
692 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EINVAL;
694 }
695 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
696 cval->res = 1;
697 } else {
698 int last_valid_res = cval->res;
699
700 while (cval->res > 1) {
701 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
702 break;
703 cval->res /= 2;
704 }
705 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
706 cval->res = last_valid_res;
707 }
708 if (cval->res == 0)
709 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200710
711 /* Additional checks for the proper resolution
712 *
713 * Some devices report smaller resolutions than actually
714 * reacting. They don't return errors but simply clip
715 * to the lower aligned value.
716 */
717 if (cval->min + cval->res < cval->max) {
718 int last_valid_res = cval->res;
719 int saved, test, check;
720 get_cur_mix_value(cval, minchn, &saved);
721 for (;;) {
722 test = saved;
723 if (test < cval->max)
724 test += cval->res;
725 else
726 test -= cval->res;
727 if (test < cval->min || test > cval->max ||
728 set_cur_mix_value(cval, minchn, test) ||
729 get_cur_mix_value(cval, minchn, &check)) {
730 cval->res = last_valid_res;
731 break;
732 }
733 if (test == check)
734 break;
735 cval->res *= 2;
736 }
737 set_cur_mix_value(cval, minchn, saved);
738 }
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 cval->initialized = 1;
741 }
742 return 0;
743}
744
745
746/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100747static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100749 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (cval->val_type == USB_MIXER_BOOLEAN ||
752 cval->val_type == USB_MIXER_INV_BOOLEAN)
753 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
754 else
755 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
756 uinfo->count = cval->channels;
757 if (cval->val_type == USB_MIXER_BOOLEAN ||
758 cval->val_type == USB_MIXER_INV_BOOLEAN) {
759 uinfo->value.integer.min = 0;
760 uinfo->value.integer.max = 1;
761 } else {
762 if (! cval->initialized)
763 get_min_max(cval, 0);
764 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200765 uinfo->value.integer.max =
766 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 return 0;
769}
770
771/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100772static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100774 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int c, cnt, val, err;
776
777 if (cval->cmask) {
778 cnt = 0;
779 for (c = 0; c < MAX_CHANNELS; c++) {
780 if (cval->cmask & (1 << c)) {
781 err = get_cur_mix_value(cval, c + 1, &val);
782 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200783 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ucontrol->value.integer.value[0] = cval->min;
785 return 0;
786 }
787 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
788 return err;
789 }
790 val = get_relative_value(cval, val);
791 ucontrol->value.integer.value[cnt] = val;
792 cnt++;
793 }
794 }
795 } else {
796 /* master channel */
797 err = get_cur_mix_value(cval, 0, &val);
798 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200799 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ucontrol->value.integer.value[0] = cval->min;
801 return 0;
802 }
803 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
804 return err;
805 }
806 val = get_relative_value(cval, val);
807 ucontrol->value.integer.value[0] = val;
808 }
809 return 0;
810}
811
812/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100813static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100815 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int c, cnt, val, oval, err;
817 int changed = 0;
818
819 if (cval->cmask) {
820 cnt = 0;
821 for (c = 0; c < MAX_CHANNELS; c++) {
822 if (cval->cmask & (1 << c)) {
823 err = get_cur_mix_value(cval, c + 1, &oval);
824 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200825 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827 return err;
828 }
829 val = ucontrol->value.integer.value[cnt];
830 val = get_abs_value(cval, val);
831 if (oval != val) {
832 set_cur_mix_value(cval, c + 1, val);
833 changed = 1;
834 }
835 get_cur_mix_value(cval, c + 1, &val);
836 cnt++;
837 }
838 }
839 } else {
840 /* master channel */
841 err = get_cur_mix_value(cval, 0, &oval);
Clemens Ladisch84957a82005-04-29 16:23:13 +0200842 if (err < 0 && cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844 if (err < 0)
845 return err;
846 val = ucontrol->value.integer.value[0];
847 val = get_abs_value(cval, val);
848 if (val != oval) {
849 set_cur_mix_value(cval, 0, val);
850 changed = 1;
851 }
852 }
853 return changed;
854}
855
Takashi Iwai86e07d32005-11-17 15:08:02 +0100856static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
858 .name = "", /* will be filled later manually */
859 .info = mixer_ctl_feature_info,
860 .get = mixer_ctl_feature_get,
861 .put = mixer_ctl_feature_put,
862};
863
864
865/*
866 * build a feature control
867 */
868
Takashi Iwai86e07d32005-11-17 15:08:02 +0100869static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100871 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 unsigned int len = 0;
874 int mapped_name = 0;
875 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100876 struct snd_kcontrol *kctl;
877 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 control++; /* change from zero-based to 1-based value */
880
881 if (control == USB_FEATURE_GEQ) {
882 /* FIXME: not supported yet */
883 return;
884 }
885
886 if (check_ignored_ctl(state, unitid, control))
887 return;
888
Takashi Iwai561b2202005-09-09 14:22:34 +0200889 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (! cval) {
891 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
892 return;
893 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200894 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 cval->id = unitid;
896 cval->control = control;
897 cval->cmask = ctl_mask;
898 cval->val_type = audio_feature_info[control-1].type;
899 if (ctl_mask == 0)
900 cval->channels = 1; /* master channel */
901 else {
902 int i, c = 0;
903 for (i = 0; i < 16; i++)
904 if (ctl_mask & (1 << i))
905 c++;
906 cval->channels = c;
907 }
908
909 /* get min/max values */
910 get_min_max(cval, 0);
911
912 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
913 if (! kctl) {
914 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
915 kfree(cval);
916 return;
917 }
918 kctl->private_free = usb_mixer_elem_free;
919
920 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
921 mapped_name = len != 0;
922 if (! len && nameid)
923 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
924
925 switch (control) {
926 case USB_FEATURE_MUTE:
927 case USB_FEATURE_VOLUME:
928 /* determine the control name. the rule is:
929 * - if a name id is given in descriptor, use it.
930 * - if the connected input can be determined, then use the name
931 * of terminal type.
932 * - if the connected output can be determined, use it.
933 * - otherwise, anonymous name.
934 */
935 if (! len) {
936 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
937 if (! len)
938 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
939 if (! len)
940 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
941 "Feature %d", unitid);
942 }
943 /* determine the stream direction:
944 * if the connected output is USB stream, then it's likely a
945 * capture stream. otherwise it should be playback (hopefully :)
946 */
947 if (! mapped_name && ! (state->oterm.type >> 16)) {
948 if ((state->oterm.type & 0xff00) == 0x0100) {
949 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
950 } else {
951 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
952 }
953 }
954 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
955 sizeof(kctl->id.name));
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200956 if (control == USB_FEATURE_VOLUME) {
957 kctl->tlv.c = mixer_vol_tlv;
958 kctl->vd[0].access |=
959 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
960 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963
964 default:
965 if (! len)
966 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
967 sizeof(kctl->id.name));
968 break;
969 }
970
971 /* quirk for UDA1321/N101 */
972 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
973 /* is not very clear from datasheets */
974 /* I hope that the min value is -15360 for newer firmware --jk */
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200975 switch (state->chip->usb_id) {
976 case USB_ID(0x0471, 0x0101):
977 case USB_ID(0x0471, 0x0104):
978 case USB_ID(0x0471, 0x0105):
979 case USB_ID(0x0672, 0x1041):
980 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
981 cval->min == -15616) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200982 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200983 cval->max = -256;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
988 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +0200989 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992
993
994/*
995 * parse a feature unit
996 *
997 * most of controlls are defined here.
998 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100999static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001002 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned int master_bits, first_ch_bits;
1004 int err, csize;
1005
1006 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1007 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1008 return -EINVAL;
1009 }
1010
1011 /* parse the source unit */
1012 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1013 return err;
1014
1015 /* determine the input source type and name */
1016 if (check_input_term(state, ftr[4], &iterm) < 0)
1017 return -EINVAL;
1018
1019 channels = (ftr[0] - 7) / csize - 1;
1020
1021 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1022 if (channels > 0)
1023 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1024 else
1025 first_ch_bits = 0;
1026 /* check all control types */
1027 for (i = 0; i < 10; i++) {
1028 unsigned int ch_bits = 0;
1029 for (j = 0; j < channels; j++) {
1030 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1031 if (mask & (1 << i))
1032 ch_bits |= (1 << j);
1033 }
1034 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1035 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1036 if (master_bits & (1 << i))
1037 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1038 }
1039
1040 return 0;
1041}
1042
1043
1044/*
1045 * Mixer Unit
1046 */
1047
1048/*
1049 * build a mixer unit control
1050 *
1051 * the callbacks are identical with feature unit.
1052 * input channel number (zero based) is given in control field instead.
1053 */
1054
Takashi Iwai86e07d32005-11-17 15:08:02 +01001055static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001057 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001059 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 unsigned int input_pins = desc[4];
1061 unsigned int num_outs = desc[5 + input_pins];
1062 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001063 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (check_ignored_ctl(state, unitid, 0))
1066 return;
1067
Takashi Iwai561b2202005-09-09 14:22:34 +02001068 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (! cval)
1070 return;
1071
Clemens Ladisch84957a82005-04-29 16:23:13 +02001072 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 cval->id = unitid;
1074 cval->control = in_ch + 1; /* based on 1 */
1075 cval->val_type = USB_MIXER_S16;
1076 for (i = 0; i < num_outs; i++) {
1077 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1078 cval->cmask |= (1 << i);
1079 cval->channels++;
1080 }
1081 }
1082
1083 /* get min/max values */
1084 get_min_max(cval, 0);
1085
1086 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1087 if (! kctl) {
1088 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1089 kfree(cval);
1090 return;
1091 }
1092 kctl->private_free = usb_mixer_elem_free;
1093
1094 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1095 if (! len)
1096 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1097 if (! len)
1098 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1099 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1100
1101 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1102 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001103 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106
1107/*
1108 * parse a mixer unit
1109 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001110static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001112 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int input_pins, num_ins, num_outs;
1114 int pin, ich, err;
1115
1116 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1117 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1118 return -EINVAL;
1119 }
1120 /* no bmControls field (e.g. Maya44) -> ignore */
1121 if (desc[0] <= 10 + input_pins) {
1122 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1123 return 0;
1124 }
1125
1126 num_ins = 0;
1127 ich = 0;
1128 for (pin = 0; pin < input_pins; pin++) {
1129 err = parse_audio_unit(state, desc[5 + pin]);
1130 if (err < 0)
1131 return err;
1132 err = check_input_term(state, desc[5 + pin], &iterm);
1133 if (err < 0)
1134 return err;
1135 num_ins += iterm.channels;
1136 for (; ich < num_ins; ++ich) {
1137 int och, ich_has_controls = 0;
1138
1139 for (och = 0; och < num_outs; ++och) {
1140 if (check_matrix_bitmap(desc + 9 + input_pins,
1141 ich, och, num_outs)) {
1142 ich_has_controls = 1;
1143 break;
1144 }
1145 }
1146 if (ich_has_controls)
1147 build_mixer_unit_ctl(state, desc, pin, ich,
1148 unitid, &iterm);
1149 }
1150 }
1151 return 0;
1152}
1153
1154
1155/*
1156 * Processing Unit / Extension Unit
1157 */
1158
1159/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001160static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001162 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int err, val;
1164
1165 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001166 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ucontrol->value.integer.value[0] = cval->min;
1168 return 0;
1169 }
1170 if (err < 0)
1171 return err;
1172 val = get_relative_value(cval, val);
1173 ucontrol->value.integer.value[0] = val;
1174 return 0;
1175}
1176
1177/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001178static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001180 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int val, oval, err;
1182
1183 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1184 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001185 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187 return err;
1188 }
1189 val = ucontrol->value.integer.value[0];
1190 val = get_abs_value(cval, val);
1191 if (val != oval) {
1192 set_cur_ctl_value(cval, cval->control << 8, val);
1193 return 1;
1194 }
1195 return 0;
1196}
1197
1198/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001199static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1201 .name = "", /* will be filled later */
1202 .info = mixer_ctl_feature_info,
1203 .get = mixer_ctl_procunit_get,
1204 .put = mixer_ctl_procunit_put,
1205};
1206
1207
1208/*
1209 * predefined data for processing units
1210 */
1211struct procunit_value_info {
1212 int control;
1213 char *suffix;
1214 int val_type;
1215 int min_value;
1216};
1217
1218struct procunit_info {
1219 int type;
1220 char *name;
1221 struct procunit_value_info *values;
1222};
1223
1224static struct procunit_value_info updown_proc_info[] = {
1225 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1226 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1227 { 0 }
1228};
1229static struct procunit_value_info prologic_proc_info[] = {
1230 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1231 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1232 { 0 }
1233};
1234static struct procunit_value_info threed_enh_proc_info[] = {
1235 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1236 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1237 { 0 }
1238};
1239static struct procunit_value_info reverb_proc_info[] = {
1240 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1241 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1242 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1243 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1244 { 0 }
1245};
1246static struct procunit_value_info chorus_proc_info[] = {
1247 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1248 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1249 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1250 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1251 { 0 }
1252};
1253static struct procunit_value_info dcr_proc_info[] = {
1254 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1255 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1256 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1257 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1258 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1259 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1260 { 0 }
1261};
1262
1263static struct procunit_info procunits[] = {
1264 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1265 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1266 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1267 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1268 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1269 { USB_PROC_DCR, "DCR", dcr_proc_info },
1270 { 0 },
1271};
1272
1273/*
1274 * build a processing/extension unit
1275 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001276static 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 -07001277{
1278 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001279 struct usb_mixer_elem_info *cval;
1280 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int i, err, nameid, type, len;
1282 struct procunit_info *info;
1283 struct procunit_value_info *valinfo;
1284 static struct procunit_value_info default_value_info[] = {
1285 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1286 { 0 }
1287 };
1288 static struct procunit_info default_info = {
1289 0, NULL, default_value_info
1290 };
1291
1292 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1293 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1294 return -EINVAL;
1295 }
1296
1297 for (i = 0; i < num_ins; i++) {
1298 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1299 return err;
1300 }
1301
1302 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 for (info = list; info && info->type; info++)
1304 if (info->type == type)
1305 break;
1306 if (! info || ! info->type)
1307 info = &default_info;
1308
1309 for (valinfo = info->values; valinfo->control; valinfo++) {
1310 /* FIXME: bitmap might be longer than 8bit */
1311 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1312 continue;
1313 if (check_ignored_ctl(state, unitid, valinfo->control))
1314 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001315 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (! cval) {
1317 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1318 return -ENOMEM;
1319 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001320 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 cval->id = unitid;
1322 cval->control = valinfo->control;
1323 cval->val_type = valinfo->val_type;
1324 cval->channels = 1;
1325
1326 /* get min/max values */
1327 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1328 /* FIXME: hard-coded */
1329 cval->min = 1;
1330 cval->max = dsc[15];
1331 cval->res = 1;
1332 cval->initialized = 1;
1333 } else
1334 get_min_max(cval, valinfo->min_value);
1335
1336 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1337 if (! kctl) {
1338 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1339 kfree(cval);
1340 return -ENOMEM;
1341 }
1342 kctl->private_free = usb_mixer_elem_free;
1343
1344 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1345 ;
1346 else if (info->name)
1347 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1348 else {
1349 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1350 len = 0;
1351 if (nameid)
1352 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1353 if (! len)
1354 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1355 }
1356 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1357 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1358
1359 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1360 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001361 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return err;
1363 }
1364 return 0;
1365}
1366
1367
Takashi Iwai86e07d32005-11-17 15:08:02 +01001368static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1371}
1372
Takashi Iwai86e07d32005-11-17 15:08:02 +01001373static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1376}
1377
1378
1379/*
1380 * Selector Unit
1381 */
1382
1383/* info callback for selector unit
1384 * use an enumerator type for routing
1385 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001386static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001388 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 char **itemlist = (char **)kcontrol->private_value;
1390
1391 snd_assert(itemlist, return -EINVAL);
1392 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1393 uinfo->count = 1;
1394 uinfo->value.enumerated.items = cval->max;
1395 if ((int)uinfo->value.enumerated.item >= cval->max)
1396 uinfo->value.enumerated.item = cval->max - 1;
1397 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1398 return 0;
1399}
1400
1401/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001402static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001404 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int val, err;
1406
1407 err = get_cur_ctl_value(cval, 0, &val);
1408 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001409 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 ucontrol->value.enumerated.item[0] = 0;
1411 return 0;
1412 }
1413 return err;
1414 }
1415 val = get_relative_value(cval, val);
1416 ucontrol->value.enumerated.item[0] = val;
1417 return 0;
1418}
1419
1420/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001421static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001423 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int val, oval, err;
1425
1426 err = get_cur_ctl_value(cval, 0, &oval);
1427 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001428 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430 return err;
1431 }
1432 val = ucontrol->value.enumerated.item[0];
1433 val = get_abs_value(cval, val);
1434 if (val != oval) {
1435 set_cur_ctl_value(cval, 0, val);
1436 return 1;
1437 }
1438 return 0;
1439}
1440
1441/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001442static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1444 .name = "", /* will be filled later */
1445 .info = mixer_ctl_selector_info,
1446 .get = mixer_ctl_selector_get,
1447 .put = mixer_ctl_selector_put,
1448};
1449
1450
1451/* private free callback.
1452 * free both private_data and private_value
1453 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001454static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 int i, num_ins = 0;
1457
1458 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001459 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 num_ins = cval->max;
1461 kfree(cval);
1462 kctl->private_data = NULL;
1463 }
1464 if (kctl->private_value) {
1465 char **itemlist = (char **)kctl->private_value;
1466 for (i = 0; i < num_ins; i++)
1467 kfree(itemlist[i]);
1468 kfree(itemlist);
1469 kctl->private_value = 0;
1470 }
1471}
1472
1473/*
1474 * parse a selector unit
1475 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001476static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 unsigned int num_ins = desc[4];
1479 unsigned int i, nameid, len;
1480 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001481 struct usb_mixer_elem_info *cval;
1482 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 char **namelist;
1484
Russ Cox38977e92007-08-06 15:37:58 +02001485 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1487 return -EINVAL;
1488 }
1489
1490 for (i = 0; i < num_ins; i++) {
1491 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1492 return err;
1493 }
1494
1495 if (num_ins == 1) /* only one ? nonsense! */
1496 return 0;
1497
1498 if (check_ignored_ctl(state, unitid, 0))
1499 return 0;
1500
Takashi Iwai561b2202005-09-09 14:22:34 +02001501 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (! cval) {
1503 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1504 return -ENOMEM;
1505 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001506 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 cval->id = unitid;
1508 cval->val_type = USB_MIXER_U8;
1509 cval->channels = 1;
1510 cval->min = 1;
1511 cval->max = num_ins;
1512 cval->res = 1;
1513 cval->initialized = 1;
1514
1515 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1516 if (! namelist) {
1517 snd_printk(KERN_ERR "cannot malloc\n");
1518 kfree(cval);
1519 return -ENOMEM;
1520 }
1521#define MAX_ITEM_NAME_LEN 64
1522 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001523 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 len = 0;
1525 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1526 if (! namelist[i]) {
1527 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001528 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 kfree(namelist[i]);
1530 kfree(namelist);
1531 kfree(cval);
1532 return -ENOMEM;
1533 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001534 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1535 MAX_ITEM_NAME_LEN);
1536 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1538 if (! len)
1539 sprintf(namelist[i], "Input %d", i);
1540 }
1541
1542 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1543 if (! kctl) {
1544 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001545 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 kfree(cval);
1547 return -ENOMEM;
1548 }
1549 kctl->private_value = (unsigned long)namelist;
1550 kctl->private_free = usb_mixer_selector_elem_free;
1551
1552 nameid = desc[desc[0] - 1];
1553 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1554 if (len)
1555 ;
1556 else if (nameid)
1557 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1558 else {
1559 len = get_term_name(state, &state->oterm,
1560 kctl->id.name, sizeof(kctl->id.name), 0);
1561 if (! len)
1562 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1563
1564 if ((state->oterm.type & 0xff00) == 0x0100)
1565 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1566 else
1567 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1568 }
1569
1570 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1571 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001572 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return err;
1574
1575 return 0;
1576}
1577
1578
1579/*
1580 * parse an audio unit recursively
1581 */
1582
Takashi Iwai86e07d32005-11-17 15:08:02 +01001583static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 unsigned char *p1;
1586
1587 if (test_and_set_bit(unitid, state->unitbitmap))
1588 return 0; /* the unit already visited */
1589
1590 p1 = find_audio_control_unit(state, unitid);
1591 if (!p1) {
1592 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1593 return -EINVAL;
1594 }
1595
1596 switch (p1[2]) {
1597 case INPUT_TERMINAL:
1598 return 0; /* NOP */
1599 case MIXER_UNIT:
1600 return parse_audio_mixer_unit(state, unitid, p1);
1601 case SELECTOR_UNIT:
1602 return parse_audio_selector_unit(state, unitid, p1);
1603 case FEATURE_UNIT:
1604 return parse_audio_feature_unit(state, unitid, p1);
1605 case PROCESSING_UNIT:
1606 return parse_audio_processing_unit(state, unitid, p1);
1607 case EXTENSION_UNIT:
1608 return parse_audio_extension_unit(state, unitid, p1);
1609 default:
1610 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1611 return -EINVAL;
1612 }
1613}
1614
Clemens Ladisch84957a82005-04-29 16:23:13 +02001615static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1616{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001617 kfree(mixer->id_elems);
1618 if (mixer->urb) {
1619 kfree(mixer->urb->transfer_buffer);
1620 usb_free_urb(mixer->urb);
1621 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001622 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001623 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001624 kfree(mixer);
1625}
1626
Takashi Iwai86e07d32005-11-17 15:08:02 +01001627static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001628{
1629 struct usb_mixer_interface *mixer = device->device_data;
1630 snd_usb_mixer_free(mixer);
1631 return 0;
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/*
1635 * create mixer controls
1636 *
1637 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1638 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001639static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001642 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int err;
1644 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001645 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Clemens Ladisch84957a82005-04-29 16:23:13 +02001647 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001649 state.chip = mixer->chip;
1650 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 state.buffer = hostif->extra;
1652 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001655 for (map = usbmix_ctl_maps; map->id; map++) {
1656 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001658 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001659 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 }
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 desc = NULL;
1665 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1666 if (desc[0] < 9)
1667 continue; /* invalid descriptor? */
1668 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1669 state.oterm.id = desc[3];
1670 state.oterm.type = combine_word(&desc[4]);
1671 state.oterm.name = desc[8];
1672 err = parse_audio_unit(&state, desc[7]);
1673 if (err < 0)
1674 return err;
1675 }
1676 return 0;
1677}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001678
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001679static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1680 int unitid)
1681{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001682 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001683
1684 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1685 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1686 info->elem_id);
1687}
1688
Clemens Ladischb259b102005-04-29 16:29:28 +02001689static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1690 int unitid)
1691{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001692 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001693 return;
1694 /* unit ids specific to Extigy/Audigy 2 NX: */
1695 switch (unitid) {
1696 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001697 mixer->rc_urb->dev = mixer->chip->dev;
1698 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001699 break;
1700 case 4: /* digital in jack */
1701 case 7: /* line in jacks */
1702 case 19: /* speaker out jacks */
1703 case 20: /* headphones out jack */
1704 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001705 /* live24ext: 4 = line-in jack */
1706 case 3: /* hp-out jack (may actuate Mute) */
1707 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040))
1708 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1709 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001710 default:
1711 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1712 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001713 }
1714}
1715
David Howells7d12e782006-10-05 14:55:46 +01001716static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001717{
1718 struct usb_mixer_interface *mixer = urb->context;
1719
1720 if (urb->status == 0) {
1721 u8 *buf = urb->transfer_buffer;
1722 int i;
1723
1724 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1725 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1726 buf[0], buf[1]);
1727 /* ignore any notifications not from the control interface */
1728 if ((buf[0] & 0x0f) != 0)
1729 continue;
1730 if (!(buf[0] & 0x40))
1731 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001732 else
1733 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001734 }
1735 }
1736 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1737 urb->dev = mixer->chip->dev;
1738 usb_submit_urb(urb, GFP_ATOMIC);
1739 }
1740}
1741
1742/* create the handler for the optional status interrupt endpoint */
1743static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1744{
1745 struct usb_host_interface *hostif;
1746 struct usb_endpoint_descriptor *ep;
1747 void *transfer_buffer;
1748 int buffer_length;
1749 unsigned int epnum;
1750
1751 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1752 /* we need one interrupt input endpoint */
1753 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1754 return 0;
1755 ep = get_endpoint(hostif, 0);
1756 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1757 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1758 return 0;
1759
1760 epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1761 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1762 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1763 if (!transfer_buffer)
1764 return -ENOMEM;
1765 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1766 if (!mixer->urb) {
1767 kfree(transfer_buffer);
1768 return -ENOMEM;
1769 }
1770 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1771 usb_rcvintpipe(mixer->chip->dev, epnum),
1772 transfer_buffer, buffer_length,
1773 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1774 usb_submit_urb(mixer->urb, GFP_KERNEL);
1775 return 0;
1776}
1777
David Howells7d12e782006-10-05 14:55:46 +01001778static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001779{
1780 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001781 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001782 u32 code;
1783
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001784 if (urb->status < 0 || urb->actual_length < rc->packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001785 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001786
1787 code = mixer->rc_buffer[rc->offset];
1788 if (rc->length == 2)
1789 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1790
Clemens Ladischb259b102005-04-29 16:29:28 +02001791 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001792 if (code == rc->mute_code)
1793 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001794 mixer->rc_code = code;
1795 wmb();
1796 wake_up(&mixer->rc_waitq);
1797}
1798
Takashi Iwai86e07d32005-11-17 15:08:02 +01001799static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
Clemens Ladischb259b102005-04-29 16:29:28 +02001800{
1801 struct usb_mixer_interface *mixer = hw->private_data;
1802
1803 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1804 return -EBUSY;
1805 return 0;
1806}
1807
Takashi Iwai86e07d32005-11-17 15:08:02 +01001808static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
Clemens Ladischb259b102005-04-29 16:29:28 +02001809{
1810 struct usb_mixer_interface *mixer = hw->private_data;
1811
1812 clear_bit(0, &mixer->rc_hwdep_open);
1813 smp_mb__after_clear_bit();
1814 return 0;
1815}
1816
Takashi Iwai86e07d32005-11-17 15:08:02 +01001817static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001818 long count, loff_t *offset)
1819{
1820 struct usb_mixer_interface *mixer = hw->private_data;
1821 int err;
1822 u32 rc_code;
1823
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001824 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001825 return -EINVAL;
1826 err = wait_event_interruptible(mixer->rc_waitq,
1827 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1828 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001829 if (count == 1)
1830 err = put_user(rc_code, buf);
1831 else
1832 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001833 }
1834 return err < 0 ? err : count;
1835}
1836
Takashi Iwai86e07d32005-11-17 15:08:02 +01001837static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001838 poll_table *wait)
1839{
1840 struct usb_mixer_interface *mixer = hw->private_data;
1841
1842 poll_wait(file, &mixer->rc_waitq, wait);
1843 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1844}
1845
1846static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1847{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001848 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001849 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02001850
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001851 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1852 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1853 break;
1854 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02001855 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001856 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02001857
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001858 len = mixer->rc_cfg->packet_length;
1859
Clemens Ladischb259b102005-04-29 16:29:28 +02001860 init_waitqueue_head(&mixer->rc_waitq);
1861 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1862 if (err < 0)
1863 return err;
1864 snprintf(hwdep->name, sizeof(hwdep->name),
1865 "%s remote control", mixer->chip->card->shortname);
1866 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1867 hwdep->private_data = mixer;
1868 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1869 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1870 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1871 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1872
1873 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1874 if (!mixer->rc_urb)
1875 return -ENOMEM;
1876 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1877 if (!mixer->rc_setup_packet) {
1878 usb_free_urb(mixer->rc_urb);
1879 mixer->rc_urb = NULL;
1880 return -ENOMEM;
1881 }
1882 mixer->rc_setup_packet->bRequestType =
1883 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1884 mixer->rc_setup_packet->bRequest = GET_MEM;
1885 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1886 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1887 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1888 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1889 usb_rcvctrlpipe(mixer->chip->dev, 0),
1890 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1891 snd_usb_soundblaster_remote_complete, mixer);
1892 return 0;
1893}
1894
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001895#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001896
Takashi Iwai86e07d32005-11-17 15:08:02 +01001897static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001898{
1899 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1900 int index = kcontrol->private_value;
1901
1902 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1903 return 0;
1904}
1905
Takashi Iwai86e07d32005-11-17 15:08:02 +01001906static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001907{
1908 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1909 int index = kcontrol->private_value;
1910 int value = ucontrol->value.integer.value[0];
1911 int err, changed;
1912
1913 if (value > 1)
1914 return -EINVAL;
1915 changed = value != mixer->audigy2nx_leds[index];
1916 err = snd_usb_ctl_msg(mixer->chip->dev,
1917 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1918 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1919 value, index + 2, NULL, 0, 100);
1920 if (err < 0)
1921 return err;
1922 mixer->audigy2nx_leds[index] = value;
1923 return changed;
1924}
1925
Takashi Iwai86e07d32005-11-17 15:08:02 +01001926static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001927 {
1928 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1929 .name = "CMSS LED Switch",
1930 .info = snd_audigy2nx_led_info,
1931 .get = snd_audigy2nx_led_get,
1932 .put = snd_audigy2nx_led_put,
1933 .private_value = 0,
1934 },
1935 {
1936 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1937 .name = "Power LED Switch",
1938 .info = snd_audigy2nx_led_info,
1939 .get = snd_audigy2nx_led_get,
1940 .put = snd_audigy2nx_led_put,
1941 .private_value = 1,
1942 },
1943 {
1944 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1945 .name = "Dolby Digital LED Switch",
1946 .info = snd_audigy2nx_led_info,
1947 .get = snd_audigy2nx_led_get,
1948 .put = snd_audigy2nx_led_put,
1949 .private_value = 2,
1950 },
1951};
1952
1953static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1954{
1955 int i, err;
1956
1957 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001958 if (i > 1 && /* Live24ext has 2 LEDs only */
1959 mixer->chip->usb_id == USB_ID(0x041e, 0x3040))
1960 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001961 err = snd_ctl_add(mixer->chip->card,
1962 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1963 if (err < 0)
1964 return err;
1965 }
1966 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1967 return 0;
1968}
1969
Takashi Iwai86e07d32005-11-17 15:08:02 +01001970static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1971 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02001972{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001973 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02001974 int unitid;
1975 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001976 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02001977 {4, "dig in "},
1978 {7, "line in"},
1979 {19, "spk out"},
1980 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001981 {-1, NULL}
1982 }, jacks_live24ext[] = {
1983 {4, "line in"}, /* &1=Line, &2=Mic*/
1984 {3, "hph out"}, /* headphones */
1985 {0, "RC "}, /* last command, 6 bytes see rc_config above */
1986 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02001987 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001988 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02001989 struct usb_mixer_interface *mixer = entry->private_data;
1990 int i, err;
1991 u8 buf[3];
1992
1993 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001994 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
1995 jacks = jacks_audigy2nx;
1996 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040))
1997 jacks = jacks_live24ext;
1998 else
1999 return;
2000
2001 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002002 snd_iprintf(buffer, "%s: ", jacks[i].name);
2003 err = snd_usb_ctl_msg(mixer->chip->dev,
2004 usb_rcvctrlpipe(mixer->chip->dev, 0),
2005 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2006 USB_RECIP_INTERFACE, 0,
2007 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002008 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002009 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2010 else
2011 snd_iprintf(buffer, "?\n");
2012 }
2013}
2014
Takashi Iwai86e07d32005-11-17 15:08:02 +01002015int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002016{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002017 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002018 .dev_free = snd_usb_mixer_dev_free
2019 };
2020 struct usb_mixer_interface *mixer;
2021 int err;
2022
2023 strcpy(chip->card->mixername, "USB Mixer");
2024
Takashi Iwai561b2202005-09-09 14:22:34 +02002025 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002026 if (!mixer)
2027 return -ENOMEM;
2028 mixer->chip = chip;
2029 mixer->ctrlif = ctrlif;
2030#ifdef IGNORE_CTL_ERROR
2031 mixer->ignore_ctl_error = 1;
2032#endif
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002033 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2034 if (!mixer->id_elems) {
2035 kfree(mixer);
2036 return -ENOMEM;
2037 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002038
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002039 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002040 (err = snd_usb_mixer_status_create(mixer)) < 0)
2041 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002042
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002043 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2044 goto _error;
2045
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002046 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2047 mixer->chip->usb_id == USB_ID(0x041e, 0x3040)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002048 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02002049
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002050 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2051 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002052 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002053 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002054 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002055 }
2056
Clemens Ladisch84957a82005-04-29 16:23:13 +02002057 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002058 if (err < 0)
2059 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002060 list_add(&mixer->list, &chip->mixer_list);
2061 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002062
2063_error:
2064 snd_usb_mixer_free(mixer);
2065 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002066}
2067
2068void snd_usb_mixer_disconnect(struct list_head *p)
2069{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002070 struct usb_mixer_interface *mixer;
2071
2072 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002073 usb_kill_urb(mixer->urb);
2074 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002075}