blob: bc8bd00047ad587272a2c082d4e11d92e0f9b2a3 [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 unsigned long rc_hwdep_open;
83 u32 rc_code;
84 wait_queue_head_t rc_waitq;
85 struct urb *rc_urb;
86 struct usb_ctrlrequest *rc_setup_packet;
87 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020088
89 u8 audigy2nx_leds[3];
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
114struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200115 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100116 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
117 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned int id;
119 unsigned int control; /* CS or ICN (high byte) */
120 unsigned int cmask; /* channel mask bitmap: 0 = master */
121 int channels;
122 int val_type;
123 int min, max, res;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200124 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127
128enum {
129 USB_FEATURE_NONE = 0,
130 USB_FEATURE_MUTE = 1,
131 USB_FEATURE_VOLUME,
132 USB_FEATURE_BASS,
133 USB_FEATURE_MID,
134 USB_FEATURE_TREBLE,
135 USB_FEATURE_GEQ,
136 USB_FEATURE_AGC,
137 USB_FEATURE_DELAY,
138 USB_FEATURE_BASSBOOST,
139 USB_FEATURE_LOUDNESS
140};
141
142enum {
143 USB_MIXER_BOOLEAN,
144 USB_MIXER_INV_BOOLEAN,
145 USB_MIXER_S8,
146 USB_MIXER_U8,
147 USB_MIXER_S16,
148 USB_MIXER_U16,
149};
150
151enum {
152 USB_PROC_UPDOWN = 1,
153 USB_PROC_UPDOWN_SWITCH = 1,
154 USB_PROC_UPDOWN_MODE_SEL = 2,
155
156 USB_PROC_PROLOGIC = 2,
157 USB_PROC_PROLOGIC_SWITCH = 1,
158 USB_PROC_PROLOGIC_MODE_SEL = 2,
159
160 USB_PROC_3DENH = 3,
161 USB_PROC_3DENH_SWITCH = 1,
162 USB_PROC_3DENH_SPACE = 2,
163
164 USB_PROC_REVERB = 4,
165 USB_PROC_REVERB_SWITCH = 1,
166 USB_PROC_REVERB_LEVEL = 2,
167 USB_PROC_REVERB_TIME = 3,
168 USB_PROC_REVERB_DELAY = 4,
169
170 USB_PROC_CHORUS = 5,
171 USB_PROC_CHORUS_SWITCH = 1,
172 USB_PROC_CHORUS_LEVEL = 2,
173 USB_PROC_CHORUS_RATE = 3,
174 USB_PROC_CHORUS_DEPTH = 4,
175
176 USB_PROC_DCR = 6,
177 USB_PROC_DCR_SWITCH = 1,
178 USB_PROC_DCR_RATIO = 2,
179 USB_PROC_DCR_MAX_AMP = 3,
180 USB_PROC_DCR_THRESHOLD = 4,
181 USB_PROC_DCR_ATTACK = 5,
182 USB_PROC_DCR_RELEASE = 6,
183};
184
185#define MAX_CHANNELS 10 /* max logical channels */
186
187
188/*
189 * manual mapping of mixer names
190 * if the mixer topology is too complicated and the parsed names are
191 * ambiguous, add the entries in usbmixer_maps.c.
192 */
193#include "usbmixer_maps.c"
194
195/* get the mapped name if the unit matches */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100196static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 const struct usbmix_name_map *p;
199
200 if (! state->map)
201 return 0;
202
203 for (p = state->map; p->id; p++) {
204 if (p->id == unitid && p->name &&
205 (! control || ! p->control || control == p->control)) {
206 buflen--;
207 return strlcpy(buf, p->name, buflen);
208 }
209 }
210 return 0;
211}
212
213/* check whether the control should be ignored */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100214static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 const struct usbmix_name_map *p;
217
218 if (! state->map)
219 return 0;
220 for (p = state->map; p->id; p++) {
221 if (p->id == unitid && ! p->name &&
222 (! control || ! p->control || control == p->control)) {
223 // printk("ignored control %d:%d\n", unitid, control);
224 return 1;
225 }
226 }
227 return 0;
228}
229
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200230/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100231static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200232 int index, char *buf, int buflen)
233{
234 const struct usbmix_selector_map *p;
235
236 if (! state->selector_map)
237 return 0;
238 for (p = state->selector_map; p->id; p++) {
239 if (p->id == unitid && index < p->count)
240 return strlcpy(buf, p->names[index], buflen);
241 }
242 return 0;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * find an audio control unit with the given unit id
247 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100248static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 unsigned char *p;
251
252 p = NULL;
253 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
254 USB_DT_CS_INTERFACE)) != NULL) {
255 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
256 return p;
257 }
258 return NULL;
259}
260
261
262/*
263 * copy a string with the given id
264 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100265static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
268 buf[len] = 0;
269 return len;
270}
271
272/*
273 * convert from the byte/word on usb descriptor to the zero-based integer
274 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100275static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 switch (cval->val_type) {
278 case USB_MIXER_BOOLEAN:
279 return !!val;
280 case USB_MIXER_INV_BOOLEAN:
281 return !val;
282 case USB_MIXER_U8:
283 val &= 0xff;
284 break;
285 case USB_MIXER_S8:
286 val &= 0xff;
287 if (val >= 0x80)
288 val -= 0x100;
289 break;
290 case USB_MIXER_U16:
291 val &= 0xffff;
292 break;
293 case USB_MIXER_S16:
294 val &= 0xffff;
295 if (val >= 0x8000)
296 val -= 0x10000;
297 break;
298 }
299 return val;
300}
301
302/*
303 * convert from the zero-based int to the byte/word for usb descriptor
304 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100305static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 switch (cval->val_type) {
308 case USB_MIXER_BOOLEAN:
309 return !!val;
310 case USB_MIXER_INV_BOOLEAN:
311 return !val;
312 case USB_MIXER_S8:
313 case USB_MIXER_U8:
314 return val & 0xff;
315 case USB_MIXER_S16:
316 case USB_MIXER_U16:
317 return val & 0xffff;
318 }
319 return 0; /* not reached */
320}
321
Takashi Iwai86e07d32005-11-17 15:08:02 +0100322static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 if (! cval->res)
325 cval->res = 1;
326 if (val < cval->min)
327 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200328 else if (val >= cval->max)
329 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 else
331 return (val - cval->min) / cval->res;
332}
333
Takashi Iwai86e07d32005-11-17 15:08:02 +0100334static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 if (val < 0)
337 return cval->min;
338 if (! cval->res)
339 cval->res = 1;
340 val *= cval->res;
341 val += cval->min;
342 if (val > cval->max)
343 return cval->max;
344 return val;
345}
346
347
348/*
349 * retrieve a mixer value
350 */
351
Takashi Iwai86e07d32005-11-17 15:08:02 +0100352static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 unsigned char buf[2];
355 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
356 int timeout = 10;
357
358 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200359 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
360 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 request,
362 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200363 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200364 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
366 return 0;
367 }
368 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200369 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
370 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -EINVAL;
372}
373
Takashi Iwai86e07d32005-11-17 15:08:02 +0100374static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 return get_ctl_value(cval, GET_CUR, validx, value);
377}
378
379/* channel = 0: master, 1 = first channel */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100380static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
383}
384
385/*
386 * set a mixer value
387 */
388
Takashi Iwai86e07d32005-11-17 15:08:02 +0100389static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned char buf[2];
392 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
393 int timeout = 10;
394
395 value_set = convert_bytes_value(cval, value_set);
396 buf[0] = value_set & 0xff;
397 buf[1] = (value_set >> 8) & 0xff;
398 while (timeout -- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200399 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
400 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 request,
402 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200403 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 buf, val_len, 100) >= 0)
405 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200406 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
407 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -EINVAL;
409}
410
Takashi Iwai86e07d32005-11-17 15:08:02 +0100411static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 return set_ctl_value(cval, SET_CUR, validx, value);
414}
415
Takashi Iwai86e07d32005-11-17 15:08:02 +0100416static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
419}
420
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200421/*
422 * TLV callback for mixer volume controls
423 */
424static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
425 unsigned int size, unsigned int __user *_tlv)
426{
427 struct usb_mixer_elem_info *cval = kcontrol->private_data;
428 DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
429
430 if (size < sizeof(scale))
431 return -ENOMEM;
432 /* USB descriptions contain the dB scale in 1/256 dB unit
433 * while ALSA TLV contains in 1/100 dB unit
434 */
435 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
436 scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
437 if (copy_to_user(_tlv, scale, sizeof(scale)))
438 return -EFAULT;
439 return 0;
440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/*
443 * parser routines begin here...
444 */
445
Takashi Iwai86e07d32005-11-17 15:08:02 +0100446static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448
449/*
450 * check if the input/output channel routing is enabled on the given bitmap.
451 * used for mixer unit parser
452 */
453static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
454{
455 int idx = ich * num_outs + och;
456 return bmap[idx >> 3] & (0x80 >> (idx & 7));
457}
458
459
460/*
461 * add an alsa control element
462 * search and increment the index until an empty slot is found.
463 *
464 * if failed, give up and free the control instance.
465 */
466
Takashi Iwai86e07d32005-11-17 15:08:02 +0100467static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100469 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200471
472 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200474 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200478 cval->elem_id = &kctl->id;
479 cval->next_id_elem = state->mixer->id_elems[cval->id];
480 state->mixer->id_elems[cval->id] = cval;
481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484
485/*
486 * get a terminal name string
487 */
488
489static struct iterm_name_combo {
490 int type;
491 char *name;
492} iterm_names[] = {
493 { 0x0300, "Output" },
494 { 0x0301, "Speaker" },
495 { 0x0302, "Headphone" },
496 { 0x0303, "HMD Audio" },
497 { 0x0304, "Desktop Speaker" },
498 { 0x0305, "Room Speaker" },
499 { 0x0306, "Com Speaker" },
500 { 0x0307, "LFE" },
501 { 0x0600, "External In" },
502 { 0x0601, "Analog In" },
503 { 0x0602, "Digital In" },
504 { 0x0603, "Line" },
505 { 0x0604, "Legacy In" },
506 { 0x0605, "IEC958 In" },
507 { 0x0606, "1394 DA Stream" },
508 { 0x0607, "1394 DV Stream" },
509 { 0x0700, "Embedded" },
510 { 0x0701, "Noise Source" },
511 { 0x0702, "Equalization Noise" },
512 { 0x0703, "CD" },
513 { 0x0704, "DAT" },
514 { 0x0705, "DCC" },
515 { 0x0706, "MiniDisk" },
516 { 0x0707, "Analog Tape" },
517 { 0x0708, "Phonograph" },
518 { 0x0709, "VCR Audio" },
519 { 0x070a, "Video Disk Audio" },
520 { 0x070b, "DVD Audio" },
521 { 0x070c, "TV Tuner Audio" },
522 { 0x070d, "Satellite Rec Audio" },
523 { 0x070e, "Cable Tuner Audio" },
524 { 0x070f, "DSS Audio" },
525 { 0x0710, "Radio Receiver" },
526 { 0x0711, "Radio Transmitter" },
527 { 0x0712, "Multi-Track Recorder" },
528 { 0x0713, "Synthesizer" },
529 { 0 },
530};
531
Takashi Iwai86e07d32005-11-17 15:08:02 +0100532static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned char *name, int maxlen, int term_only)
534{
535 struct iterm_name_combo *names;
536
537 if (iterm->name)
538 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
539
540 /* virtual type - not a real terminal */
541 if (iterm->type >> 16) {
542 if (term_only)
543 return 0;
544 switch (iterm->type >> 16) {
545 case SELECTOR_UNIT:
546 strcpy(name, "Selector"); return 8;
547 case PROCESSING_UNIT:
548 strcpy(name, "Process Unit"); return 12;
549 case EXTENSION_UNIT:
550 strcpy(name, "Ext Unit"); return 8;
551 case MIXER_UNIT:
552 strcpy(name, "Mixer"); return 5;
553 default:
554 return sprintf(name, "Unit %d", iterm->id);
555 }
556 }
557
558 switch (iterm->type & 0xff00) {
559 case 0x0100:
560 strcpy(name, "PCM"); return 3;
561 case 0x0200:
562 strcpy(name, "Mic"); return 3;
563 case 0x0400:
564 strcpy(name, "Headset"); return 7;
565 case 0x0500:
566 strcpy(name, "Phone"); return 5;
567 }
568
569 for (names = iterm_names; names->type; names++)
570 if (names->type == iterm->type) {
571 strcpy(name, names->name);
572 return strlen(names->name);
573 }
574 return 0;
575}
576
577
578/*
579 * parse the source unit recursively until it reaches to a terminal
580 * or a branched unit.
581 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100582static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 unsigned char *p1;
585
586 memset(term, 0, sizeof(*term));
587 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
588 term->id = id;
589 switch (p1[2]) {
590 case INPUT_TERMINAL:
591 term->type = combine_word(p1 + 4);
592 term->channels = p1[7];
593 term->chconfig = combine_word(p1 + 8);
594 term->name = p1[11];
595 return 0;
596 case FEATURE_UNIT:
597 id = p1[4];
598 break; /* continue to parse */
599 case MIXER_UNIT:
600 term->type = p1[2] << 16; /* virtual type */
601 term->channels = p1[5 + p1[4]];
602 term->chconfig = combine_word(p1 + 6 + p1[4]);
603 term->name = p1[p1[0] - 1];
604 return 0;
605 case SELECTOR_UNIT:
606 /* call recursively to retrieve the channel info */
607 if (check_input_term(state, p1[5], term) < 0)
608 return -ENODEV;
609 term->type = p1[2] << 16; /* virtual type */
610 term->id = id;
611 term->name = p1[9 + p1[0] - 1];
612 return 0;
613 case PROCESSING_UNIT:
614 case EXTENSION_UNIT:
615 if (p1[6] == 1) {
616 id = p1[7];
617 break; /* continue to parse */
618 }
619 term->type = p1[2] << 16; /* virtual type */
620 term->channels = p1[7 + p1[6]];
621 term->chconfig = combine_word(p1 + 8 + p1[6]);
622 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
623 return 0;
624 default:
625 return -ENODEV;
626 }
627 }
628 return -ENODEV;
629}
630
631
632/*
633 * Feature Unit
634 */
635
636/* feature unit control information */
637struct usb_feature_control_info {
638 const char *name;
639 unsigned int type; /* control type (mute, volume, etc.) */
640};
641
642static struct usb_feature_control_info audio_feature_info[] = {
643 { "Mute", USB_MIXER_INV_BOOLEAN },
644 { "Volume", USB_MIXER_S16 },
645 { "Tone Control - Bass", USB_MIXER_S8 },
646 { "Tone Control - Mid", USB_MIXER_S8 },
647 { "Tone Control - Treble", USB_MIXER_S8 },
648 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
649 { "Auto Gain Control", USB_MIXER_BOOLEAN },
650 { "Delay Control", USB_MIXER_U16 },
651 { "Bass Boost", USB_MIXER_BOOLEAN },
652 { "Loudness", USB_MIXER_BOOLEAN },
653};
654
655
656/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100657static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Jesper Juhl4d572772005-05-30 17:30:32 +0200659 kfree(kctl->private_data);
660 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663
664/*
665 * interface to ALSA control for feature/mixer units
666 */
667
668/*
669 * retrieve the minimum and maximum values for the specified control
670 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100671static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 /* for failsafe */
674 cval->min = default_min;
675 cval->max = cval->min + 1;
676 cval->res = 1;
677
678 if (cval->val_type == USB_MIXER_BOOLEAN ||
679 cval->val_type == USB_MIXER_INV_BOOLEAN) {
680 cval->initialized = 1;
681 } else {
682 int minchn = 0;
683 if (cval->cmask) {
684 int i;
685 for (i = 0; i < MAX_CHANNELS; i++)
686 if (cval->cmask & (1 << i)) {
687 minchn = i + 1;
688 break;
689 }
690 }
691 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
692 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200693 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
694 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EINVAL;
696 }
697 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
698 cval->res = 1;
699 } else {
700 int last_valid_res = cval->res;
701
702 while (cval->res > 1) {
703 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
704 break;
705 cval->res /= 2;
706 }
707 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
708 cval->res = last_valid_res;
709 }
710 if (cval->res == 0)
711 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200712
713 /* Additional checks for the proper resolution
714 *
715 * Some devices report smaller resolutions than actually
716 * reacting. They don't return errors but simply clip
717 * to the lower aligned value.
718 */
719 if (cval->min + cval->res < cval->max) {
720 int last_valid_res = cval->res;
721 int saved, test, check;
722 get_cur_mix_value(cval, minchn, &saved);
723 for (;;) {
724 test = saved;
725 if (test < cval->max)
726 test += cval->res;
727 else
728 test -= cval->res;
729 if (test < cval->min || test > cval->max ||
730 set_cur_mix_value(cval, minchn, test) ||
731 get_cur_mix_value(cval, minchn, &check)) {
732 cval->res = last_valid_res;
733 break;
734 }
735 if (test == check)
736 break;
737 cval->res *= 2;
738 }
739 set_cur_mix_value(cval, minchn, saved);
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 cval->initialized = 1;
743 }
744 return 0;
745}
746
747
748/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100749static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100751 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (cval->val_type == USB_MIXER_BOOLEAN ||
754 cval->val_type == USB_MIXER_INV_BOOLEAN)
755 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
756 else
757 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
758 uinfo->count = cval->channels;
759 if (cval->val_type == USB_MIXER_BOOLEAN ||
760 cval->val_type == USB_MIXER_INV_BOOLEAN) {
761 uinfo->value.integer.min = 0;
762 uinfo->value.integer.max = 1;
763 } else {
764 if (! cval->initialized)
765 get_min_max(cval, 0);
766 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200767 uinfo->value.integer.max =
768 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 return 0;
771}
772
773/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100774static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100776 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int c, cnt, val, err;
778
779 if (cval->cmask) {
780 cnt = 0;
781 for (c = 0; c < MAX_CHANNELS; c++) {
782 if (cval->cmask & (1 << c)) {
783 err = get_cur_mix_value(cval, c + 1, &val);
784 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200785 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 ucontrol->value.integer.value[0] = cval->min;
787 return 0;
788 }
789 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
790 return err;
791 }
792 val = get_relative_value(cval, val);
793 ucontrol->value.integer.value[cnt] = val;
794 cnt++;
795 }
796 }
797 } else {
798 /* master channel */
799 err = get_cur_mix_value(cval, 0, &val);
800 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200801 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 ucontrol->value.integer.value[0] = cval->min;
803 return 0;
804 }
805 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
806 return err;
807 }
808 val = get_relative_value(cval, val);
809 ucontrol->value.integer.value[0] = val;
810 }
811 return 0;
812}
813
814/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100815static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100817 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int c, cnt, val, oval, err;
819 int changed = 0;
820
821 if (cval->cmask) {
822 cnt = 0;
823 for (c = 0; c < MAX_CHANNELS; c++) {
824 if (cval->cmask & (1 << c)) {
825 err = get_cur_mix_value(cval, c + 1, &oval);
826 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200827 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829 return err;
830 }
831 val = ucontrol->value.integer.value[cnt];
832 val = get_abs_value(cval, val);
833 if (oval != val) {
834 set_cur_mix_value(cval, c + 1, val);
835 changed = 1;
836 }
837 get_cur_mix_value(cval, c + 1, &val);
838 cnt++;
839 }
840 }
841 } else {
842 /* master channel */
843 err = get_cur_mix_value(cval, 0, &oval);
Clemens Ladisch84957a82005-04-29 16:23:13 +0200844 if (err < 0 && cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 0;
846 if (err < 0)
847 return err;
848 val = ucontrol->value.integer.value[0];
849 val = get_abs_value(cval, val);
850 if (val != oval) {
851 set_cur_mix_value(cval, 0, val);
852 changed = 1;
853 }
854 }
855 return changed;
856}
857
Takashi Iwai86e07d32005-11-17 15:08:02 +0100858static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
860 .name = "", /* will be filled later manually */
861 .info = mixer_ctl_feature_info,
862 .get = mixer_ctl_feature_get,
863 .put = mixer_ctl_feature_put,
864};
865
866
867/*
868 * build a feature control
869 */
870
Takashi Iwai86e07d32005-11-17 15:08:02 +0100871static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100873 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 unsigned int len = 0;
876 int mapped_name = 0;
877 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100878 struct snd_kcontrol *kctl;
879 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 control++; /* change from zero-based to 1-based value */
882
883 if (control == USB_FEATURE_GEQ) {
884 /* FIXME: not supported yet */
885 return;
886 }
887
888 if (check_ignored_ctl(state, unitid, control))
889 return;
890
Takashi Iwai561b2202005-09-09 14:22:34 +0200891 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (! cval) {
893 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
894 return;
895 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200896 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 cval->id = unitid;
898 cval->control = control;
899 cval->cmask = ctl_mask;
900 cval->val_type = audio_feature_info[control-1].type;
901 if (ctl_mask == 0)
902 cval->channels = 1; /* master channel */
903 else {
904 int i, c = 0;
905 for (i = 0; i < 16; i++)
906 if (ctl_mask & (1 << i))
907 c++;
908 cval->channels = c;
909 }
910
911 /* get min/max values */
912 get_min_max(cval, 0);
913
914 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
915 if (! kctl) {
916 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
917 kfree(cval);
918 return;
919 }
920 kctl->private_free = usb_mixer_elem_free;
921
922 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
923 mapped_name = len != 0;
924 if (! len && nameid)
925 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
926
927 switch (control) {
928 case USB_FEATURE_MUTE:
929 case USB_FEATURE_VOLUME:
930 /* determine the control name. the rule is:
931 * - if a name id is given in descriptor, use it.
932 * - if the connected input can be determined, then use the name
933 * of terminal type.
934 * - if the connected output can be determined, use it.
935 * - otherwise, anonymous name.
936 */
937 if (! len) {
938 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
939 if (! len)
940 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
941 if (! len)
942 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
943 "Feature %d", unitid);
944 }
945 /* determine the stream direction:
946 * if the connected output is USB stream, then it's likely a
947 * capture stream. otherwise it should be playback (hopefully :)
948 */
949 if (! mapped_name && ! (state->oterm.type >> 16)) {
950 if ((state->oterm.type & 0xff00) == 0x0100) {
951 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
952 } else {
953 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
954 }
955 }
956 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
957 sizeof(kctl->id.name));
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200958 if (control == USB_FEATURE_VOLUME) {
959 kctl->tlv.c = mixer_vol_tlv;
960 kctl->vd[0].access |=
961 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
962 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965
966 default:
967 if (! len)
968 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
969 sizeof(kctl->id.name));
970 break;
971 }
972
973 /* quirk for UDA1321/N101 */
974 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
975 /* is not very clear from datasheets */
976 /* I hope that the min value is -15360 for newer firmware --jk */
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200977 switch (state->chip->usb_id) {
978 case USB_ID(0x0471, 0x0101):
979 case USB_ID(0x0471, 0x0104):
980 case USB_ID(0x0471, 0x0105):
981 case USB_ID(0x0672, 0x1041):
982 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
983 cval->min == -15616) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200984 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200985 cval->max = -256;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
989 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
990 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +0200991 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994
995
996/*
997 * parse a feature unit
998 *
999 * most of controlls are defined here.
1000 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001001static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001004 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned int master_bits, first_ch_bits;
1006 int err, csize;
1007
1008 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1009 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1010 return -EINVAL;
1011 }
1012
1013 /* parse the source unit */
1014 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1015 return err;
1016
1017 /* determine the input source type and name */
1018 if (check_input_term(state, ftr[4], &iterm) < 0)
1019 return -EINVAL;
1020
1021 channels = (ftr[0] - 7) / csize - 1;
1022
1023 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1024 if (channels > 0)
1025 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1026 else
1027 first_ch_bits = 0;
1028 /* check all control types */
1029 for (i = 0; i < 10; i++) {
1030 unsigned int ch_bits = 0;
1031 for (j = 0; j < channels; j++) {
1032 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1033 if (mask & (1 << i))
1034 ch_bits |= (1 << j);
1035 }
1036 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1037 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1038 if (master_bits & (1 << i))
1039 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1040 }
1041
1042 return 0;
1043}
1044
1045
1046/*
1047 * Mixer Unit
1048 */
1049
1050/*
1051 * build a mixer unit control
1052 *
1053 * the callbacks are identical with feature unit.
1054 * input channel number (zero based) is given in control field instead.
1055 */
1056
Takashi Iwai86e07d32005-11-17 15:08:02 +01001057static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001059 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001061 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 unsigned int input_pins = desc[4];
1063 unsigned int num_outs = desc[5 + input_pins];
1064 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001065 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (check_ignored_ctl(state, unitid, 0))
1068 return;
1069
Takashi Iwai561b2202005-09-09 14:22:34 +02001070 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (! cval)
1072 return;
1073
Clemens Ladisch84957a82005-04-29 16:23:13 +02001074 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 cval->id = unitid;
1076 cval->control = in_ch + 1; /* based on 1 */
1077 cval->val_type = USB_MIXER_S16;
1078 for (i = 0; i < num_outs; i++) {
1079 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1080 cval->cmask |= (1 << i);
1081 cval->channels++;
1082 }
1083 }
1084
1085 /* get min/max values */
1086 get_min_max(cval, 0);
1087
1088 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1089 if (! kctl) {
1090 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1091 kfree(cval);
1092 return;
1093 }
1094 kctl->private_free = usb_mixer_elem_free;
1095
1096 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1097 if (! len)
1098 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1099 if (! len)
1100 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1101 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1102
1103 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1104 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001105 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108
1109/*
1110 * parse a mixer unit
1111 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001112static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001114 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int input_pins, num_ins, num_outs;
1116 int pin, ich, err;
1117
1118 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1119 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1120 return -EINVAL;
1121 }
1122 /* no bmControls field (e.g. Maya44) -> ignore */
1123 if (desc[0] <= 10 + input_pins) {
1124 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1125 return 0;
1126 }
1127
1128 num_ins = 0;
1129 ich = 0;
1130 for (pin = 0; pin < input_pins; pin++) {
1131 err = parse_audio_unit(state, desc[5 + pin]);
1132 if (err < 0)
1133 return err;
1134 err = check_input_term(state, desc[5 + pin], &iterm);
1135 if (err < 0)
1136 return err;
1137 num_ins += iterm.channels;
1138 for (; ich < num_ins; ++ich) {
1139 int och, ich_has_controls = 0;
1140
1141 for (och = 0; och < num_outs; ++och) {
1142 if (check_matrix_bitmap(desc + 9 + input_pins,
1143 ich, och, num_outs)) {
1144 ich_has_controls = 1;
1145 break;
1146 }
1147 }
1148 if (ich_has_controls)
1149 build_mixer_unit_ctl(state, desc, pin, ich,
1150 unitid, &iterm);
1151 }
1152 }
1153 return 0;
1154}
1155
1156
1157/*
1158 * Processing Unit / Extension Unit
1159 */
1160
1161/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001162static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001164 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int err, val;
1166
1167 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001168 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ucontrol->value.integer.value[0] = cval->min;
1170 return 0;
1171 }
1172 if (err < 0)
1173 return err;
1174 val = get_relative_value(cval, val);
1175 ucontrol->value.integer.value[0] = val;
1176 return 0;
1177}
1178
1179/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001180static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001182 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int val, oval, err;
1184
1185 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1186 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001187 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189 return err;
1190 }
1191 val = ucontrol->value.integer.value[0];
1192 val = get_abs_value(cval, val);
1193 if (val != oval) {
1194 set_cur_ctl_value(cval, cval->control << 8, val);
1195 return 1;
1196 }
1197 return 0;
1198}
1199
1200/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001201static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1203 .name = "", /* will be filled later */
1204 .info = mixer_ctl_feature_info,
1205 .get = mixer_ctl_procunit_get,
1206 .put = mixer_ctl_procunit_put,
1207};
1208
1209
1210/*
1211 * predefined data for processing units
1212 */
1213struct procunit_value_info {
1214 int control;
1215 char *suffix;
1216 int val_type;
1217 int min_value;
1218};
1219
1220struct procunit_info {
1221 int type;
1222 char *name;
1223 struct procunit_value_info *values;
1224};
1225
1226static struct procunit_value_info updown_proc_info[] = {
1227 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1228 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1229 { 0 }
1230};
1231static struct procunit_value_info prologic_proc_info[] = {
1232 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1233 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1234 { 0 }
1235};
1236static struct procunit_value_info threed_enh_proc_info[] = {
1237 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1238 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1239 { 0 }
1240};
1241static struct procunit_value_info reverb_proc_info[] = {
1242 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1243 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1244 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1245 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1246 { 0 }
1247};
1248static struct procunit_value_info chorus_proc_info[] = {
1249 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1250 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1251 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1252 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1253 { 0 }
1254};
1255static struct procunit_value_info dcr_proc_info[] = {
1256 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1257 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1258 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1259 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1260 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1261 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1262 { 0 }
1263};
1264
1265static struct procunit_info procunits[] = {
1266 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1267 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1268 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1269 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1270 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1271 { USB_PROC_DCR, "DCR", dcr_proc_info },
1272 { 0 },
1273};
1274
1275/*
1276 * build a processing/extension unit
1277 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001278static 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 -07001279{
1280 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001281 struct usb_mixer_elem_info *cval;
1282 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int i, err, nameid, type, len;
1284 struct procunit_info *info;
1285 struct procunit_value_info *valinfo;
1286 static struct procunit_value_info default_value_info[] = {
1287 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1288 { 0 }
1289 };
1290 static struct procunit_info default_info = {
1291 0, NULL, default_value_info
1292 };
1293
1294 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1295 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1296 return -EINVAL;
1297 }
1298
1299 for (i = 0; i < num_ins; i++) {
1300 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1301 return err;
1302 }
1303
1304 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 for (info = list; info && info->type; info++)
1306 if (info->type == type)
1307 break;
1308 if (! info || ! info->type)
1309 info = &default_info;
1310
1311 for (valinfo = info->values; valinfo->control; valinfo++) {
1312 /* FIXME: bitmap might be longer than 8bit */
1313 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1314 continue;
1315 if (check_ignored_ctl(state, unitid, valinfo->control))
1316 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001317 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (! cval) {
1319 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1320 return -ENOMEM;
1321 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001322 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 cval->id = unitid;
1324 cval->control = valinfo->control;
1325 cval->val_type = valinfo->val_type;
1326 cval->channels = 1;
1327
1328 /* get min/max values */
1329 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1330 /* FIXME: hard-coded */
1331 cval->min = 1;
1332 cval->max = dsc[15];
1333 cval->res = 1;
1334 cval->initialized = 1;
1335 } else
1336 get_min_max(cval, valinfo->min_value);
1337
1338 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1339 if (! kctl) {
1340 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1341 kfree(cval);
1342 return -ENOMEM;
1343 }
1344 kctl->private_free = usb_mixer_elem_free;
1345
1346 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1347 ;
1348 else if (info->name)
1349 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1350 else {
1351 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1352 len = 0;
1353 if (nameid)
1354 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1355 if (! len)
1356 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1357 }
1358 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1359 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1360
1361 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1362 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001363 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return err;
1365 }
1366 return 0;
1367}
1368
1369
Takashi Iwai86e07d32005-11-17 15:08:02 +01001370static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1373}
1374
Takashi Iwai86e07d32005-11-17 15:08:02 +01001375static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1378}
1379
1380
1381/*
1382 * Selector Unit
1383 */
1384
1385/* info callback for selector unit
1386 * use an enumerator type for routing
1387 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001388static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001390 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 char **itemlist = (char **)kcontrol->private_value;
1392
Takashi Iwai5e246b82008-08-08 17:12:47 +02001393 if (snd_BUG_ON(!itemlist))
1394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1396 uinfo->count = 1;
1397 uinfo->value.enumerated.items = cval->max;
1398 if ((int)uinfo->value.enumerated.item >= cval->max)
1399 uinfo->value.enumerated.item = cval->max - 1;
1400 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1401 return 0;
1402}
1403
1404/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001405static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001407 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int val, err;
1409
1410 err = get_cur_ctl_value(cval, 0, &val);
1411 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001412 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 ucontrol->value.enumerated.item[0] = 0;
1414 return 0;
1415 }
1416 return err;
1417 }
1418 val = get_relative_value(cval, val);
1419 ucontrol->value.enumerated.item[0] = val;
1420 return 0;
1421}
1422
1423/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001424static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001426 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int val, oval, err;
1428
1429 err = get_cur_ctl_value(cval, 0, &oval);
1430 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001431 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
1433 return err;
1434 }
1435 val = ucontrol->value.enumerated.item[0];
1436 val = get_abs_value(cval, val);
1437 if (val != oval) {
1438 set_cur_ctl_value(cval, 0, val);
1439 return 1;
1440 }
1441 return 0;
1442}
1443
1444/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001445static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1447 .name = "", /* will be filled later */
1448 .info = mixer_ctl_selector_info,
1449 .get = mixer_ctl_selector_get,
1450 .put = mixer_ctl_selector_put,
1451};
1452
1453
1454/* private free callback.
1455 * free both private_data and private_value
1456 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001457static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 int i, num_ins = 0;
1460
1461 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001462 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 num_ins = cval->max;
1464 kfree(cval);
1465 kctl->private_data = NULL;
1466 }
1467 if (kctl->private_value) {
1468 char **itemlist = (char **)kctl->private_value;
1469 for (i = 0; i < num_ins; i++)
1470 kfree(itemlist[i]);
1471 kfree(itemlist);
1472 kctl->private_value = 0;
1473 }
1474}
1475
1476/*
1477 * parse a selector unit
1478 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001479static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 unsigned int num_ins = desc[4];
1482 unsigned int i, nameid, len;
1483 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001484 struct usb_mixer_elem_info *cval;
1485 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 char **namelist;
1487
Russ Cox38977e92007-08-06 15:37:58 +02001488 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1490 return -EINVAL;
1491 }
1492
1493 for (i = 0; i < num_ins; i++) {
1494 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1495 return err;
1496 }
1497
1498 if (num_ins == 1) /* only one ? nonsense! */
1499 return 0;
1500
1501 if (check_ignored_ctl(state, unitid, 0))
1502 return 0;
1503
Takashi Iwai561b2202005-09-09 14:22:34 +02001504 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (! cval) {
1506 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1507 return -ENOMEM;
1508 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001509 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 cval->id = unitid;
1511 cval->val_type = USB_MIXER_U8;
1512 cval->channels = 1;
1513 cval->min = 1;
1514 cval->max = num_ins;
1515 cval->res = 1;
1516 cval->initialized = 1;
1517
1518 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1519 if (! namelist) {
1520 snd_printk(KERN_ERR "cannot malloc\n");
1521 kfree(cval);
1522 return -ENOMEM;
1523 }
1524#define MAX_ITEM_NAME_LEN 64
1525 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001526 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 len = 0;
1528 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1529 if (! namelist[i]) {
1530 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001531 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 kfree(namelist[i]);
1533 kfree(namelist);
1534 kfree(cval);
1535 return -ENOMEM;
1536 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001537 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1538 MAX_ITEM_NAME_LEN);
1539 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1541 if (! len)
1542 sprintf(namelist[i], "Input %d", i);
1543 }
1544
1545 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1546 if (! kctl) {
1547 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001548 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 kfree(cval);
1550 return -ENOMEM;
1551 }
1552 kctl->private_value = (unsigned long)namelist;
1553 kctl->private_free = usb_mixer_selector_elem_free;
1554
1555 nameid = desc[desc[0] - 1];
1556 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1557 if (len)
1558 ;
1559 else if (nameid)
1560 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1561 else {
1562 len = get_term_name(state, &state->oterm,
1563 kctl->id.name, sizeof(kctl->id.name), 0);
1564 if (! len)
1565 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1566
1567 if ((state->oterm.type & 0xff00) == 0x0100)
1568 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1569 else
1570 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1571 }
1572
1573 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1574 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001575 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return err;
1577
1578 return 0;
1579}
1580
1581
1582/*
1583 * parse an audio unit recursively
1584 */
1585
Takashi Iwai86e07d32005-11-17 15:08:02 +01001586static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 unsigned char *p1;
1589
1590 if (test_and_set_bit(unitid, state->unitbitmap))
1591 return 0; /* the unit already visited */
1592
1593 p1 = find_audio_control_unit(state, unitid);
1594 if (!p1) {
1595 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1596 return -EINVAL;
1597 }
1598
1599 switch (p1[2]) {
1600 case INPUT_TERMINAL:
1601 return 0; /* NOP */
1602 case MIXER_UNIT:
1603 return parse_audio_mixer_unit(state, unitid, p1);
1604 case SELECTOR_UNIT:
1605 return parse_audio_selector_unit(state, unitid, p1);
1606 case FEATURE_UNIT:
1607 return parse_audio_feature_unit(state, unitid, p1);
1608 case PROCESSING_UNIT:
1609 return parse_audio_processing_unit(state, unitid, p1);
1610 case EXTENSION_UNIT:
1611 return parse_audio_extension_unit(state, unitid, p1);
1612 default:
1613 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1614 return -EINVAL;
1615 }
1616}
1617
Clemens Ladisch84957a82005-04-29 16:23:13 +02001618static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1619{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001620 kfree(mixer->id_elems);
1621 if (mixer->urb) {
1622 kfree(mixer->urb->transfer_buffer);
1623 usb_free_urb(mixer->urb);
1624 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001625 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001626 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001627 kfree(mixer);
1628}
1629
Takashi Iwai86e07d32005-11-17 15:08:02 +01001630static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001631{
1632 struct usb_mixer_interface *mixer = device->device_data;
1633 snd_usb_mixer_free(mixer);
1634 return 0;
1635}
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637/*
1638 * create mixer controls
1639 *
1640 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1641 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001642static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001645 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int err;
1647 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001648 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Clemens Ladisch84957a82005-04-29 16:23:13 +02001650 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001652 state.chip = mixer->chip;
1653 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 state.buffer = hostif->extra;
1655 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001658 for (map = usbmix_ctl_maps; map->id; map++) {
1659 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001661 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001662 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 break;
1664 }
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 desc = NULL;
1668 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1669 if (desc[0] < 9)
1670 continue; /* invalid descriptor? */
1671 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1672 state.oterm.id = desc[3];
1673 state.oterm.type = combine_word(&desc[4]);
1674 state.oterm.name = desc[8];
1675 err = parse_audio_unit(&state, desc[7]);
1676 if (err < 0)
1677 return err;
1678 }
1679 return 0;
1680}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001681
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001682static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1683 int unitid)
1684{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001685 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001686
1687 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1688 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1689 info->elem_id);
1690}
1691
Clemens Ladischb259b102005-04-29 16:29:28 +02001692static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1693 int unitid)
1694{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001695 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001696 return;
1697 /* unit ids specific to Extigy/Audigy 2 NX: */
1698 switch (unitid) {
1699 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001700 mixer->rc_urb->dev = mixer->chip->dev;
1701 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001702 break;
1703 case 4: /* digital in jack */
1704 case 7: /* line in jacks */
1705 case 19: /* speaker out jacks */
1706 case 20: /* headphones out jack */
1707 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001708 /* live24ext: 4 = line-in jack */
1709 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001710 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1711 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001712 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1713 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001714 default:
1715 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1716 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001717 }
1718}
1719
David Howells7d12e782006-10-05 14:55:46 +01001720static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001721{
1722 struct usb_mixer_interface *mixer = urb->context;
1723
1724 if (urb->status == 0) {
1725 u8 *buf = urb->transfer_buffer;
1726 int i;
1727
1728 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1729 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1730 buf[0], buf[1]);
1731 /* ignore any notifications not from the control interface */
1732 if ((buf[0] & 0x0f) != 0)
1733 continue;
1734 if (!(buf[0] & 0x40))
1735 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001736 else
1737 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001738 }
1739 }
1740 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1741 urb->dev = mixer->chip->dev;
1742 usb_submit_urb(urb, GFP_ATOMIC);
1743 }
1744}
1745
1746/* create the handler for the optional status interrupt endpoint */
1747static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1748{
1749 struct usb_host_interface *hostif;
1750 struct usb_endpoint_descriptor *ep;
1751 void *transfer_buffer;
1752 int buffer_length;
1753 unsigned int epnum;
1754
1755 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1756 /* we need one interrupt input endpoint */
1757 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1758 return 0;
1759 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001760 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001761 return 0;
1762
Julia Lawall42a6e662008-12-29 11:23:02 +01001763 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001764 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1765 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1766 if (!transfer_buffer)
1767 return -ENOMEM;
1768 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1769 if (!mixer->urb) {
1770 kfree(transfer_buffer);
1771 return -ENOMEM;
1772 }
1773 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1774 usb_rcvintpipe(mixer->chip->dev, epnum),
1775 transfer_buffer, buffer_length,
1776 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1777 usb_submit_urb(mixer->urb, GFP_KERNEL);
1778 return 0;
1779}
1780
David Howells7d12e782006-10-05 14:55:46 +01001781static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001782{
1783 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001784 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001785 u32 code;
1786
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001787 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001788 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001789
1790 code = mixer->rc_buffer[rc->offset];
1791 if (rc->length == 2)
1792 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1793
Clemens Ladischb259b102005-04-29 16:29:28 +02001794 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001795 if (code == rc->mute_code)
1796 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001797 mixer->rc_code = code;
1798 wmb();
1799 wake_up(&mixer->rc_waitq);
1800}
1801
Takashi Iwai86e07d32005-11-17 15:08:02 +01001802static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
Clemens Ladischb259b102005-04-29 16:29:28 +02001803{
1804 struct usb_mixer_interface *mixer = hw->private_data;
1805
1806 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1807 return -EBUSY;
1808 return 0;
1809}
1810
Takashi Iwai86e07d32005-11-17 15:08:02 +01001811static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
Clemens Ladischb259b102005-04-29 16:29:28 +02001812{
1813 struct usb_mixer_interface *mixer = hw->private_data;
1814
1815 clear_bit(0, &mixer->rc_hwdep_open);
1816 smp_mb__after_clear_bit();
1817 return 0;
1818}
1819
Takashi Iwai86e07d32005-11-17 15:08:02 +01001820static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001821 long count, loff_t *offset)
1822{
1823 struct usb_mixer_interface *mixer = hw->private_data;
1824 int err;
1825 u32 rc_code;
1826
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001827 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001828 return -EINVAL;
1829 err = wait_event_interruptible(mixer->rc_waitq,
1830 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1831 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001832 if (count == 1)
1833 err = put_user(rc_code, buf);
1834 else
1835 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001836 }
1837 return err < 0 ? err : count;
1838}
1839
Takashi Iwai86e07d32005-11-17 15:08:02 +01001840static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001841 poll_table *wait)
1842{
1843 struct usb_mixer_interface *mixer = hw->private_data;
1844
1845 poll_wait(file, &mixer->rc_waitq, wait);
1846 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1847}
1848
1849static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1850{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001851 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001852 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02001853
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001854 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1855 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1856 break;
1857 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02001858 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001859 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02001860
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001861 len = mixer->rc_cfg->packet_length;
1862
Clemens Ladischb259b102005-04-29 16:29:28 +02001863 init_waitqueue_head(&mixer->rc_waitq);
1864 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1865 if (err < 0)
1866 return err;
1867 snprintf(hwdep->name, sizeof(hwdep->name),
1868 "%s remote control", mixer->chip->card->shortname);
1869 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1870 hwdep->private_data = mixer;
1871 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1872 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1873 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1874 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1875
1876 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1877 if (!mixer->rc_urb)
1878 return -ENOMEM;
1879 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1880 if (!mixer->rc_setup_packet) {
1881 usb_free_urb(mixer->rc_urb);
1882 mixer->rc_urb = NULL;
1883 return -ENOMEM;
1884 }
1885 mixer->rc_setup_packet->bRequestType =
1886 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1887 mixer->rc_setup_packet->bRequest = GET_MEM;
1888 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1889 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1890 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1891 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1892 usb_rcvctrlpipe(mixer->chip->dev, 0),
1893 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1894 snd_usb_soundblaster_remote_complete, mixer);
1895 return 0;
1896}
1897
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001898#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001899
Takashi Iwai86e07d32005-11-17 15:08:02 +01001900static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001901{
1902 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1903 int index = kcontrol->private_value;
1904
1905 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1906 return 0;
1907}
1908
Takashi Iwai86e07d32005-11-17 15:08:02 +01001909static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001910{
1911 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1912 int index = kcontrol->private_value;
1913 int value = ucontrol->value.integer.value[0];
1914 int err, changed;
1915
1916 if (value > 1)
1917 return -EINVAL;
1918 changed = value != mixer->audigy2nx_leds[index];
1919 err = snd_usb_ctl_msg(mixer->chip->dev,
1920 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1921 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1922 value, index + 2, NULL, 0, 100);
1923 if (err < 0)
1924 return err;
1925 mixer->audigy2nx_leds[index] = value;
1926 return changed;
1927}
1928
Takashi Iwai86e07d32005-11-17 15:08:02 +01001929static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001930 {
1931 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1932 .name = "CMSS LED Switch",
1933 .info = snd_audigy2nx_led_info,
1934 .get = snd_audigy2nx_led_get,
1935 .put = snd_audigy2nx_led_put,
1936 .private_value = 0,
1937 },
1938 {
1939 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1940 .name = "Power LED Switch",
1941 .info = snd_audigy2nx_led_info,
1942 .get = snd_audigy2nx_led_get,
1943 .put = snd_audigy2nx_led_put,
1944 .private_value = 1,
1945 },
1946 {
1947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1948 .name = "Dolby Digital LED Switch",
1949 .info = snd_audigy2nx_led_info,
1950 .get = snd_audigy2nx_led_get,
1951 .put = snd_audigy2nx_led_put,
1952 .private_value = 2,
1953 },
1954};
1955
1956static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1957{
1958 int i, err;
1959
1960 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01001961 if (i > 1 && /* Live24ext has 2 LEDs only */
1962 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1963 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001964 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001965 err = snd_ctl_add(mixer->chip->card,
1966 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1967 if (err < 0)
1968 return err;
1969 }
1970 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1971 return 0;
1972}
1973
Takashi Iwai86e07d32005-11-17 15:08:02 +01001974static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1975 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02001976{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001977 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02001978 int unitid;
1979 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001980 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02001981 {4, "dig in "},
1982 {7, "line in"},
1983 {19, "spk out"},
1984 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001985 {-1, NULL}
1986 }, jacks_live24ext[] = {
1987 {4, "line in"}, /* &1=Line, &2=Mic*/
1988 {3, "hph out"}, /* headphones */
1989 {0, "RC "}, /* last command, 6 bytes see rc_config above */
1990 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02001991 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001992 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02001993 struct usb_mixer_interface *mixer = entry->private_data;
1994 int i, err;
1995 u8 buf[3];
1996
1997 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001998 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
1999 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002000 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2001 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002002 jacks = jacks_live24ext;
2003 else
2004 return;
2005
2006 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002007 snd_iprintf(buffer, "%s: ", jacks[i].name);
2008 err = snd_usb_ctl_msg(mixer->chip->dev,
2009 usb_rcvctrlpipe(mixer->chip->dev, 0),
2010 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2011 USB_RECIP_INTERFACE, 0,
2012 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002013 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002014 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2015 else
2016 snd_iprintf(buffer, "?\n");
2017 }
2018}
2019
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002020int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2021 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002022{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002023 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002024 .dev_free = snd_usb_mixer_dev_free
2025 };
2026 struct usb_mixer_interface *mixer;
2027 int err;
2028
2029 strcpy(chip->card->mixername, "USB Mixer");
2030
Takashi Iwai561b2202005-09-09 14:22:34 +02002031 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002032 if (!mixer)
2033 return -ENOMEM;
2034 mixer->chip = chip;
2035 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002036 mixer->ignore_ctl_error = ignore_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002037 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2038 if (!mixer->id_elems) {
2039 kfree(mixer);
2040 return -ENOMEM;
2041 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002042
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002043 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002044 (err = snd_usb_mixer_status_create(mixer)) < 0)
2045 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002046
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002047 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2048 goto _error;
2049
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002050 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002051 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2052 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002053 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02002054
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002055 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2056 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002057 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002058 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002059 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002060 }
2061
Clemens Ladisch84957a82005-04-29 16:23:13 +02002062 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002063 if (err < 0)
2064 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002065 list_add(&mixer->list, &chip->mixer_list);
2066 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002067
2068_error:
2069 snd_usb_mixer_free(mixer);
2070 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002071}
2072
2073void snd_usb_mixer_disconnect(struct list_head *p)
2074{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002075 struct usb_mixer_interface *mixer;
2076
2077 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002078 usb_kill_urb(mixer->urb);
2079 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002080}