blob: 678dac2d4dba56b6dee4c8574033590e756f6512 [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
29#include <sound/driver.h>
30#include <linux/bitops.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/slab.h>
34#include <linux/string.h>
35#include <linux/usb.h>
36#include <sound/core.h>
37#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020038#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020039#include <sound/info.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
Clemens Ladisch84957a82005-04-29 16:23:13 +020049struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010050 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020051 unsigned int ctrlif;
52 struct list_head list;
53 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020054 struct urb *urb;
Takashi Iwai86e07d32005-11-17 15:08:02 +010055 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
Clemens Ladischb259b102005-04-29 16:29:28 +020056
57 /* Sound Blaster remote control stuff */
58 enum {
59 RC_NONE,
60 RC_EXTIGY,
61 RC_AUDIGY2NX,
62 } rc_type;
63 unsigned long rc_hwdep_open;
64 u32 rc_code;
65 wait_queue_head_t rc_waitq;
66 struct urb *rc_urb;
67 struct usb_ctrlrequest *rc_setup_packet;
68 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020069
70 u8 audigy2nx_leds[3];
Clemens Ladisch84957a82005-04-29 16:23:13 +020071};
72
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct usb_audio_term {
75 int id;
76 int type;
77 int channels;
78 unsigned int chconfig;
79 int name;
80};
81
82struct usbmix_name_map;
83
Takashi Iwai86e07d32005-11-17 15:08:02 +010084struct mixer_build {
85 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020086 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned char *buffer;
88 unsigned int buflen;
Clemens Ladisch707e6072005-04-29 09:56:17 +020089 DECLARE_BITMAP(unitbitmap, 256);
Takashi Iwai86e07d32005-11-17 15:08:02 +010090 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +020092 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +020096 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +010097 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
98 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned int id;
100 unsigned int control; /* CS or ICN (high byte) */
101 unsigned int cmask; /* channel mask bitmap: 0 = master */
102 int channels;
103 int val_type;
104 int min, max, res;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200105 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108
109enum {
110 USB_FEATURE_NONE = 0,
111 USB_FEATURE_MUTE = 1,
112 USB_FEATURE_VOLUME,
113 USB_FEATURE_BASS,
114 USB_FEATURE_MID,
115 USB_FEATURE_TREBLE,
116 USB_FEATURE_GEQ,
117 USB_FEATURE_AGC,
118 USB_FEATURE_DELAY,
119 USB_FEATURE_BASSBOOST,
120 USB_FEATURE_LOUDNESS
121};
122
123enum {
124 USB_MIXER_BOOLEAN,
125 USB_MIXER_INV_BOOLEAN,
126 USB_MIXER_S8,
127 USB_MIXER_U8,
128 USB_MIXER_S16,
129 USB_MIXER_U16,
130};
131
132enum {
133 USB_PROC_UPDOWN = 1,
134 USB_PROC_UPDOWN_SWITCH = 1,
135 USB_PROC_UPDOWN_MODE_SEL = 2,
136
137 USB_PROC_PROLOGIC = 2,
138 USB_PROC_PROLOGIC_SWITCH = 1,
139 USB_PROC_PROLOGIC_MODE_SEL = 2,
140
141 USB_PROC_3DENH = 3,
142 USB_PROC_3DENH_SWITCH = 1,
143 USB_PROC_3DENH_SPACE = 2,
144
145 USB_PROC_REVERB = 4,
146 USB_PROC_REVERB_SWITCH = 1,
147 USB_PROC_REVERB_LEVEL = 2,
148 USB_PROC_REVERB_TIME = 3,
149 USB_PROC_REVERB_DELAY = 4,
150
151 USB_PROC_CHORUS = 5,
152 USB_PROC_CHORUS_SWITCH = 1,
153 USB_PROC_CHORUS_LEVEL = 2,
154 USB_PROC_CHORUS_RATE = 3,
155 USB_PROC_CHORUS_DEPTH = 4,
156
157 USB_PROC_DCR = 6,
158 USB_PROC_DCR_SWITCH = 1,
159 USB_PROC_DCR_RATIO = 2,
160 USB_PROC_DCR_MAX_AMP = 3,
161 USB_PROC_DCR_THRESHOLD = 4,
162 USB_PROC_DCR_ATTACK = 5,
163 USB_PROC_DCR_RELEASE = 6,
164};
165
166#define MAX_CHANNELS 10 /* max logical channels */
167
168
169/*
170 * manual mapping of mixer names
171 * if the mixer topology is too complicated and the parsed names are
172 * ambiguous, add the entries in usbmixer_maps.c.
173 */
174#include "usbmixer_maps.c"
175
176/* get the mapped name if the unit matches */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100177static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 const struct usbmix_name_map *p;
180
181 if (! state->map)
182 return 0;
183
184 for (p = state->map; p->id; p++) {
185 if (p->id == unitid && p->name &&
186 (! control || ! p->control || control == p->control)) {
187 buflen--;
188 return strlcpy(buf, p->name, buflen);
189 }
190 }
191 return 0;
192}
193
194/* check whether the control should be ignored */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100195static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 const struct usbmix_name_map *p;
198
199 if (! state->map)
200 return 0;
201 for (p = state->map; p->id; p++) {
202 if (p->id == unitid && ! p->name &&
203 (! control || ! p->control || control == p->control)) {
204 // printk("ignored control %d:%d\n", unitid, control);
205 return 1;
206 }
207 }
208 return 0;
209}
210
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200211/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100212static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200213 int index, char *buf, int buflen)
214{
215 const struct usbmix_selector_map *p;
216
217 if (! state->selector_map)
218 return 0;
219 for (p = state->selector_map; p->id; p++) {
220 if (p->id == unitid && index < p->count)
221 return strlcpy(buf, p->names[index], buflen);
222 }
223 return 0;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
227 * find an audio control unit with the given unit id
228 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100229static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 unsigned char *p;
232
233 p = NULL;
234 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
235 USB_DT_CS_INTERFACE)) != NULL) {
236 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
237 return p;
238 }
239 return NULL;
240}
241
242
243/*
244 * copy a string with the given id
245 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100246static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
249 buf[len] = 0;
250 return len;
251}
252
253/*
254 * convert from the byte/word on usb descriptor to the zero-based integer
255 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100256static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 switch (cval->val_type) {
259 case USB_MIXER_BOOLEAN:
260 return !!val;
261 case USB_MIXER_INV_BOOLEAN:
262 return !val;
263 case USB_MIXER_U8:
264 val &= 0xff;
265 break;
266 case USB_MIXER_S8:
267 val &= 0xff;
268 if (val >= 0x80)
269 val -= 0x100;
270 break;
271 case USB_MIXER_U16:
272 val &= 0xffff;
273 break;
274 case USB_MIXER_S16:
275 val &= 0xffff;
276 if (val >= 0x8000)
277 val -= 0x10000;
278 break;
279 }
280 return val;
281}
282
283/*
284 * convert from the zero-based int to the byte/word for usb descriptor
285 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100286static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 switch (cval->val_type) {
289 case USB_MIXER_BOOLEAN:
290 return !!val;
291 case USB_MIXER_INV_BOOLEAN:
292 return !val;
293 case USB_MIXER_S8:
294 case USB_MIXER_U8:
295 return val & 0xff;
296 case USB_MIXER_S16:
297 case USB_MIXER_U16:
298 return val & 0xffff;
299 }
300 return 0; /* not reached */
301}
302
Takashi Iwai86e07d32005-11-17 15:08:02 +0100303static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 if (! cval->res)
306 cval->res = 1;
307 if (val < cval->min)
308 return 0;
309 else if (val > cval->max)
310 return (cval->max - cval->min) / cval->res;
311 else
312 return (val - cval->min) / cval->res;
313}
314
Takashi Iwai86e07d32005-11-17 15:08:02 +0100315static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 if (val < 0)
318 return cval->min;
319 if (! cval->res)
320 cval->res = 1;
321 val *= cval->res;
322 val += cval->min;
323 if (val > cval->max)
324 return cval->max;
325 return val;
326}
327
328
329/*
330 * retrieve a mixer value
331 */
332
Takashi Iwai86e07d32005-11-17 15:08:02 +0100333static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 unsigned char buf[2];
336 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
337 int timeout = 10;
338
339 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200340 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
341 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 request,
343 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200344 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 buf, val_len, 100) >= 0) {
346 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
347 return 0;
348 }
349 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200350 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
351 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -EINVAL;
353}
354
Takashi Iwai86e07d32005-11-17 15:08:02 +0100355static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 return get_ctl_value(cval, GET_CUR, validx, value);
358}
359
360/* channel = 0: master, 1 = first channel */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100361static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
364}
365
366/*
367 * set a mixer value
368 */
369
Takashi Iwai86e07d32005-11-17 15:08:02 +0100370static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 unsigned char buf[2];
373 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
374 int timeout = 10;
375
376 value_set = convert_bytes_value(cval, value_set);
377 buf[0] = value_set & 0xff;
378 buf[1] = (value_set >> 8) & 0xff;
379 while (timeout -- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200380 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
381 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 request,
383 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200384 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 buf, val_len, 100) >= 0)
386 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200387 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
388 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -EINVAL;
390}
391
Takashi Iwai86e07d32005-11-17 15:08:02 +0100392static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 return set_ctl_value(cval, SET_CUR, validx, value);
395}
396
Takashi Iwai86e07d32005-11-17 15:08:02 +0100397static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
400}
401
402
403/*
404 * parser routines begin here...
405 */
406
Takashi Iwai86e07d32005-11-17 15:08:02 +0100407static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409
410/*
411 * check if the input/output channel routing is enabled on the given bitmap.
412 * used for mixer unit parser
413 */
414static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
415{
416 int idx = ich * num_outs + och;
417 return bmap[idx >> 3] & (0x80 >> (idx & 7));
418}
419
420
421/*
422 * add an alsa control element
423 * search and increment the index until an empty slot is found.
424 *
425 * if failed, give up and free the control instance.
426 */
427
Takashi Iwai86e07d32005-11-17 15:08:02 +0100428static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100430 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200432
433 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200435 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
437 snd_ctl_free_one(kctl);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200440 cval->elem_id = &kctl->id;
441 cval->next_id_elem = state->mixer->id_elems[cval->id];
442 state->mixer->id_elems[cval->id] = cval;
443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446
447/*
448 * get a terminal name string
449 */
450
451static struct iterm_name_combo {
452 int type;
453 char *name;
454} iterm_names[] = {
455 { 0x0300, "Output" },
456 { 0x0301, "Speaker" },
457 { 0x0302, "Headphone" },
458 { 0x0303, "HMD Audio" },
459 { 0x0304, "Desktop Speaker" },
460 { 0x0305, "Room Speaker" },
461 { 0x0306, "Com Speaker" },
462 { 0x0307, "LFE" },
463 { 0x0600, "External In" },
464 { 0x0601, "Analog In" },
465 { 0x0602, "Digital In" },
466 { 0x0603, "Line" },
467 { 0x0604, "Legacy In" },
468 { 0x0605, "IEC958 In" },
469 { 0x0606, "1394 DA Stream" },
470 { 0x0607, "1394 DV Stream" },
471 { 0x0700, "Embedded" },
472 { 0x0701, "Noise Source" },
473 { 0x0702, "Equalization Noise" },
474 { 0x0703, "CD" },
475 { 0x0704, "DAT" },
476 { 0x0705, "DCC" },
477 { 0x0706, "MiniDisk" },
478 { 0x0707, "Analog Tape" },
479 { 0x0708, "Phonograph" },
480 { 0x0709, "VCR Audio" },
481 { 0x070a, "Video Disk Audio" },
482 { 0x070b, "DVD Audio" },
483 { 0x070c, "TV Tuner Audio" },
484 { 0x070d, "Satellite Rec Audio" },
485 { 0x070e, "Cable Tuner Audio" },
486 { 0x070f, "DSS Audio" },
487 { 0x0710, "Radio Receiver" },
488 { 0x0711, "Radio Transmitter" },
489 { 0x0712, "Multi-Track Recorder" },
490 { 0x0713, "Synthesizer" },
491 { 0 },
492};
493
Takashi Iwai86e07d32005-11-17 15:08:02 +0100494static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unsigned char *name, int maxlen, int term_only)
496{
497 struct iterm_name_combo *names;
498
499 if (iterm->name)
500 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
501
502 /* virtual type - not a real terminal */
503 if (iterm->type >> 16) {
504 if (term_only)
505 return 0;
506 switch (iterm->type >> 16) {
507 case SELECTOR_UNIT:
508 strcpy(name, "Selector"); return 8;
509 case PROCESSING_UNIT:
510 strcpy(name, "Process Unit"); return 12;
511 case EXTENSION_UNIT:
512 strcpy(name, "Ext Unit"); return 8;
513 case MIXER_UNIT:
514 strcpy(name, "Mixer"); return 5;
515 default:
516 return sprintf(name, "Unit %d", iterm->id);
517 }
518 }
519
520 switch (iterm->type & 0xff00) {
521 case 0x0100:
522 strcpy(name, "PCM"); return 3;
523 case 0x0200:
524 strcpy(name, "Mic"); return 3;
525 case 0x0400:
526 strcpy(name, "Headset"); return 7;
527 case 0x0500:
528 strcpy(name, "Phone"); return 5;
529 }
530
531 for (names = iterm_names; names->type; names++)
532 if (names->type == iterm->type) {
533 strcpy(name, names->name);
534 return strlen(names->name);
535 }
536 return 0;
537}
538
539
540/*
541 * parse the source unit recursively until it reaches to a terminal
542 * or a branched unit.
543 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100544static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 unsigned char *p1;
547
548 memset(term, 0, sizeof(*term));
549 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
550 term->id = id;
551 switch (p1[2]) {
552 case INPUT_TERMINAL:
553 term->type = combine_word(p1 + 4);
554 term->channels = p1[7];
555 term->chconfig = combine_word(p1 + 8);
556 term->name = p1[11];
557 return 0;
558 case FEATURE_UNIT:
559 id = p1[4];
560 break; /* continue to parse */
561 case MIXER_UNIT:
562 term->type = p1[2] << 16; /* virtual type */
563 term->channels = p1[5 + p1[4]];
564 term->chconfig = combine_word(p1 + 6 + p1[4]);
565 term->name = p1[p1[0] - 1];
566 return 0;
567 case SELECTOR_UNIT:
568 /* call recursively to retrieve the channel info */
569 if (check_input_term(state, p1[5], term) < 0)
570 return -ENODEV;
571 term->type = p1[2] << 16; /* virtual type */
572 term->id = id;
573 term->name = p1[9 + p1[0] - 1];
574 return 0;
575 case PROCESSING_UNIT:
576 case EXTENSION_UNIT:
577 if (p1[6] == 1) {
578 id = p1[7];
579 break; /* continue to parse */
580 }
581 term->type = p1[2] << 16; /* virtual type */
582 term->channels = p1[7 + p1[6]];
583 term->chconfig = combine_word(p1 + 8 + p1[6]);
584 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
585 return 0;
586 default:
587 return -ENODEV;
588 }
589 }
590 return -ENODEV;
591}
592
593
594/*
595 * Feature Unit
596 */
597
598/* feature unit control information */
599struct usb_feature_control_info {
600 const char *name;
601 unsigned int type; /* control type (mute, volume, etc.) */
602};
603
604static struct usb_feature_control_info audio_feature_info[] = {
605 { "Mute", USB_MIXER_INV_BOOLEAN },
606 { "Volume", USB_MIXER_S16 },
607 { "Tone Control - Bass", USB_MIXER_S8 },
608 { "Tone Control - Mid", USB_MIXER_S8 },
609 { "Tone Control - Treble", USB_MIXER_S8 },
610 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
611 { "Auto Gain Control", USB_MIXER_BOOLEAN },
612 { "Delay Control", USB_MIXER_U16 },
613 { "Bass Boost", USB_MIXER_BOOLEAN },
614 { "Loudness", USB_MIXER_BOOLEAN },
615};
616
617
618/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100619static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jesper Juhl4d572772005-05-30 17:30:32 +0200621 kfree(kctl->private_data);
622 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625
626/*
627 * interface to ALSA control for feature/mixer units
628 */
629
630/*
631 * retrieve the minimum and maximum values for the specified control
632 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100633static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 /* for failsafe */
636 cval->min = default_min;
637 cval->max = cval->min + 1;
638 cval->res = 1;
639
640 if (cval->val_type == USB_MIXER_BOOLEAN ||
641 cval->val_type == USB_MIXER_INV_BOOLEAN) {
642 cval->initialized = 1;
643 } else {
644 int minchn = 0;
645 if (cval->cmask) {
646 int i;
647 for (i = 0; i < MAX_CHANNELS; i++)
648 if (cval->cmask & (1 << i)) {
649 minchn = i + 1;
650 break;
651 }
652 }
653 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
654 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200655 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
656 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -EINVAL;
658 }
659 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
660 cval->res = 1;
661 } else {
662 int last_valid_res = cval->res;
663
664 while (cval->res > 1) {
665 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
666 break;
667 cval->res /= 2;
668 }
669 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
670 cval->res = last_valid_res;
671 }
672 if (cval->res == 0)
673 cval->res = 1;
674 cval->initialized = 1;
675 }
676 return 0;
677}
678
679
680/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100681static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100683 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (cval->val_type == USB_MIXER_BOOLEAN ||
686 cval->val_type == USB_MIXER_INV_BOOLEAN)
687 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
688 else
689 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
690 uinfo->count = cval->channels;
691 if (cval->val_type == USB_MIXER_BOOLEAN ||
692 cval->val_type == USB_MIXER_INV_BOOLEAN) {
693 uinfo->value.integer.min = 0;
694 uinfo->value.integer.max = 1;
695 } else {
696 if (! cval->initialized)
697 get_min_max(cval, 0);
698 uinfo->value.integer.min = 0;
699 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
700 }
701 return 0;
702}
703
704/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100705static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100707 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int c, cnt, val, err;
709
710 if (cval->cmask) {
711 cnt = 0;
712 for (c = 0; c < MAX_CHANNELS; c++) {
713 if (cval->cmask & (1 << c)) {
714 err = get_cur_mix_value(cval, c + 1, &val);
715 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200716 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ucontrol->value.integer.value[0] = cval->min;
718 return 0;
719 }
720 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
721 return err;
722 }
723 val = get_relative_value(cval, val);
724 ucontrol->value.integer.value[cnt] = val;
725 cnt++;
726 }
727 }
728 } else {
729 /* master channel */
730 err = get_cur_mix_value(cval, 0, &val);
731 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200732 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ucontrol->value.integer.value[0] = cval->min;
734 return 0;
735 }
736 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
737 return err;
738 }
739 val = get_relative_value(cval, val);
740 ucontrol->value.integer.value[0] = val;
741 }
742 return 0;
743}
744
745/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100746static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100748 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int c, cnt, val, oval, err;
750 int changed = 0;
751
752 if (cval->cmask) {
753 cnt = 0;
754 for (c = 0; c < MAX_CHANNELS; c++) {
755 if (cval->cmask & (1 << c)) {
756 err = get_cur_mix_value(cval, c + 1, &oval);
757 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200758 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
760 return err;
761 }
762 val = ucontrol->value.integer.value[cnt];
763 val = get_abs_value(cval, val);
764 if (oval != val) {
765 set_cur_mix_value(cval, c + 1, val);
766 changed = 1;
767 }
768 get_cur_mix_value(cval, c + 1, &val);
769 cnt++;
770 }
771 }
772 } else {
773 /* master channel */
774 err = get_cur_mix_value(cval, 0, &oval);
Clemens Ladisch84957a82005-04-29 16:23:13 +0200775 if (err < 0 && cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777 if (err < 0)
778 return err;
779 val = ucontrol->value.integer.value[0];
780 val = get_abs_value(cval, val);
781 if (val != oval) {
782 set_cur_mix_value(cval, 0, val);
783 changed = 1;
784 }
785 }
786 return changed;
787}
788
Takashi Iwai86e07d32005-11-17 15:08:02 +0100789static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
791 .name = "", /* will be filled later manually */
792 .info = mixer_ctl_feature_info,
793 .get = mixer_ctl_feature_get,
794 .put = mixer_ctl_feature_put,
795};
796
797
798/*
799 * build a feature control
800 */
801
Takashi Iwai86e07d32005-11-17 15:08:02 +0100802static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100804 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 unsigned int len = 0;
807 int mapped_name = 0;
808 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100809 struct snd_kcontrol *kctl;
810 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 control++; /* change from zero-based to 1-based value */
813
814 if (control == USB_FEATURE_GEQ) {
815 /* FIXME: not supported yet */
816 return;
817 }
818
819 if (check_ignored_ctl(state, unitid, control))
820 return;
821
Takashi Iwai561b2202005-09-09 14:22:34 +0200822 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (! cval) {
824 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
825 return;
826 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200827 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 cval->id = unitid;
829 cval->control = control;
830 cval->cmask = ctl_mask;
831 cval->val_type = audio_feature_info[control-1].type;
832 if (ctl_mask == 0)
833 cval->channels = 1; /* master channel */
834 else {
835 int i, c = 0;
836 for (i = 0; i < 16; i++)
837 if (ctl_mask & (1 << i))
838 c++;
839 cval->channels = c;
840 }
841
842 /* get min/max values */
843 get_min_max(cval, 0);
844
845 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
846 if (! kctl) {
847 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
848 kfree(cval);
849 return;
850 }
851 kctl->private_free = usb_mixer_elem_free;
852
853 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
854 mapped_name = len != 0;
855 if (! len && nameid)
856 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
857
858 switch (control) {
859 case USB_FEATURE_MUTE:
860 case USB_FEATURE_VOLUME:
861 /* determine the control name. the rule is:
862 * - if a name id is given in descriptor, use it.
863 * - if the connected input can be determined, then use the name
864 * of terminal type.
865 * - if the connected output can be determined, use it.
866 * - otherwise, anonymous name.
867 */
868 if (! len) {
869 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
870 if (! len)
871 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
872 if (! len)
873 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
874 "Feature %d", unitid);
875 }
876 /* determine the stream direction:
877 * if the connected output is USB stream, then it's likely a
878 * capture stream. otherwise it should be playback (hopefully :)
879 */
880 if (! mapped_name && ! (state->oterm.type >> 16)) {
881 if ((state->oterm.type & 0xff00) == 0x0100) {
882 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
883 } else {
884 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
885 }
886 }
887 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
888 sizeof(kctl->id.name));
889 break;
890
891 default:
892 if (! len)
893 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
894 sizeof(kctl->id.name));
895 break;
896 }
897
898 /* quirk for UDA1321/N101 */
899 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
900 /* is not very clear from datasheets */
901 /* I hope that the min value is -15360 for newer firmware --jk */
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200902 switch (state->chip->usb_id) {
903 case USB_ID(0x0471, 0x0101):
904 case USB_ID(0x0471, 0x0104):
905 case USB_ID(0x0471, 0x0105):
906 case USB_ID(0x0672, 0x1041):
907 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
908 cval->min == -15616) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200909 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200910 cval->max = -256;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913
914 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
915 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +0200916 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919
920
921/*
922 * parse a feature unit
923 *
924 * most of controlls are defined here.
925 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100926static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100929 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int master_bits, first_ch_bits;
931 int err, csize;
932
933 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
934 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
935 return -EINVAL;
936 }
937
938 /* parse the source unit */
939 if ((err = parse_audio_unit(state, ftr[4])) < 0)
940 return err;
941
942 /* determine the input source type and name */
943 if (check_input_term(state, ftr[4], &iterm) < 0)
944 return -EINVAL;
945
946 channels = (ftr[0] - 7) / csize - 1;
947
948 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
949 if (channels > 0)
950 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
951 else
952 first_ch_bits = 0;
953 /* check all control types */
954 for (i = 0; i < 10; i++) {
955 unsigned int ch_bits = 0;
956 for (j = 0; j < channels; j++) {
957 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
958 if (mask & (1 << i))
959 ch_bits |= (1 << j);
960 }
961 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
962 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
963 if (master_bits & (1 << i))
964 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
965 }
966
967 return 0;
968}
969
970
971/*
972 * Mixer Unit
973 */
974
975/*
976 * build a mixer unit control
977 *
978 * the callbacks are identical with feature unit.
979 * input channel number (zero based) is given in control field instead.
980 */
981
Takashi Iwai86e07d32005-11-17 15:08:02 +0100982static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100984 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100986 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned int input_pins = desc[4];
988 unsigned int num_outs = desc[5 + input_pins];
989 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100990 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (check_ignored_ctl(state, unitid, 0))
993 return;
994
Takashi Iwai561b2202005-09-09 14:22:34 +0200995 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (! cval)
997 return;
998
Clemens Ladisch84957a82005-04-29 16:23:13 +0200999 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 cval->id = unitid;
1001 cval->control = in_ch + 1; /* based on 1 */
1002 cval->val_type = USB_MIXER_S16;
1003 for (i = 0; i < num_outs; i++) {
1004 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1005 cval->cmask |= (1 << i);
1006 cval->channels++;
1007 }
1008 }
1009
1010 /* get min/max values */
1011 get_min_max(cval, 0);
1012
1013 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1014 if (! kctl) {
1015 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1016 kfree(cval);
1017 return;
1018 }
1019 kctl->private_free = usb_mixer_elem_free;
1020
1021 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1022 if (! len)
1023 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1024 if (! len)
1025 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1026 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1027
1028 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1029 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001030 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033
1034/*
1035 * parse a mixer unit
1036 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001037static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001039 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int input_pins, num_ins, num_outs;
1041 int pin, ich, err;
1042
1043 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1044 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1045 return -EINVAL;
1046 }
1047 /* no bmControls field (e.g. Maya44) -> ignore */
1048 if (desc[0] <= 10 + input_pins) {
1049 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1050 return 0;
1051 }
1052
1053 num_ins = 0;
1054 ich = 0;
1055 for (pin = 0; pin < input_pins; pin++) {
1056 err = parse_audio_unit(state, desc[5 + pin]);
1057 if (err < 0)
1058 return err;
1059 err = check_input_term(state, desc[5 + pin], &iterm);
1060 if (err < 0)
1061 return err;
1062 num_ins += iterm.channels;
1063 for (; ich < num_ins; ++ich) {
1064 int och, ich_has_controls = 0;
1065
1066 for (och = 0; och < num_outs; ++och) {
1067 if (check_matrix_bitmap(desc + 9 + input_pins,
1068 ich, och, num_outs)) {
1069 ich_has_controls = 1;
1070 break;
1071 }
1072 }
1073 if (ich_has_controls)
1074 build_mixer_unit_ctl(state, desc, pin, ich,
1075 unitid, &iterm);
1076 }
1077 }
1078 return 0;
1079}
1080
1081
1082/*
1083 * Processing Unit / Extension Unit
1084 */
1085
1086/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001087static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001089 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int err, val;
1091
1092 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001093 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ucontrol->value.integer.value[0] = cval->min;
1095 return 0;
1096 }
1097 if (err < 0)
1098 return err;
1099 val = get_relative_value(cval, val);
1100 ucontrol->value.integer.value[0] = val;
1101 return 0;
1102}
1103
1104/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001105static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001107 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int val, oval, err;
1109
1110 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1111 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001112 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return 0;
1114 return err;
1115 }
1116 val = ucontrol->value.integer.value[0];
1117 val = get_abs_value(cval, val);
1118 if (val != oval) {
1119 set_cur_ctl_value(cval, cval->control << 8, val);
1120 return 1;
1121 }
1122 return 0;
1123}
1124
1125/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001126static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1128 .name = "", /* will be filled later */
1129 .info = mixer_ctl_feature_info,
1130 .get = mixer_ctl_procunit_get,
1131 .put = mixer_ctl_procunit_put,
1132};
1133
1134
1135/*
1136 * predefined data for processing units
1137 */
1138struct procunit_value_info {
1139 int control;
1140 char *suffix;
1141 int val_type;
1142 int min_value;
1143};
1144
1145struct procunit_info {
1146 int type;
1147 char *name;
1148 struct procunit_value_info *values;
1149};
1150
1151static struct procunit_value_info updown_proc_info[] = {
1152 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1153 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1154 { 0 }
1155};
1156static struct procunit_value_info prologic_proc_info[] = {
1157 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1158 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1159 { 0 }
1160};
1161static struct procunit_value_info threed_enh_proc_info[] = {
1162 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1163 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1164 { 0 }
1165};
1166static struct procunit_value_info reverb_proc_info[] = {
1167 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1168 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1169 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1170 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1171 { 0 }
1172};
1173static struct procunit_value_info chorus_proc_info[] = {
1174 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1175 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1176 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1177 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1178 { 0 }
1179};
1180static struct procunit_value_info dcr_proc_info[] = {
1181 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1182 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1183 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1184 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1185 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1186 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1187 { 0 }
1188};
1189
1190static struct procunit_info procunits[] = {
1191 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1192 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1193 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1194 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1195 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1196 { USB_PROC_DCR, "DCR", dcr_proc_info },
1197 { 0 },
1198};
1199
1200/*
1201 * build a processing/extension unit
1202 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001203static 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 -07001204{
1205 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001206 struct usb_mixer_elem_info *cval;
1207 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int i, err, nameid, type, len;
1209 struct procunit_info *info;
1210 struct procunit_value_info *valinfo;
1211 static struct procunit_value_info default_value_info[] = {
1212 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1213 { 0 }
1214 };
1215 static struct procunit_info default_info = {
1216 0, NULL, default_value_info
1217 };
1218
1219 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1220 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1221 return -EINVAL;
1222 }
1223
1224 for (i = 0; i < num_ins; i++) {
1225 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1226 return err;
1227 }
1228
1229 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 for (info = list; info && info->type; info++)
1231 if (info->type == type)
1232 break;
1233 if (! info || ! info->type)
1234 info = &default_info;
1235
1236 for (valinfo = info->values; valinfo->control; valinfo++) {
1237 /* FIXME: bitmap might be longer than 8bit */
1238 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1239 continue;
1240 if (check_ignored_ctl(state, unitid, valinfo->control))
1241 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001242 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (! cval) {
1244 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1245 return -ENOMEM;
1246 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001247 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 cval->id = unitid;
1249 cval->control = valinfo->control;
1250 cval->val_type = valinfo->val_type;
1251 cval->channels = 1;
1252
1253 /* get min/max values */
1254 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1255 /* FIXME: hard-coded */
1256 cval->min = 1;
1257 cval->max = dsc[15];
1258 cval->res = 1;
1259 cval->initialized = 1;
1260 } else
1261 get_min_max(cval, valinfo->min_value);
1262
1263 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1264 if (! kctl) {
1265 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1266 kfree(cval);
1267 return -ENOMEM;
1268 }
1269 kctl->private_free = usb_mixer_elem_free;
1270
1271 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1272 ;
1273 else if (info->name)
1274 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1275 else {
1276 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1277 len = 0;
1278 if (nameid)
1279 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1280 if (! len)
1281 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1282 }
1283 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1284 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1285
1286 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1287 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001288 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return err;
1290 }
1291 return 0;
1292}
1293
1294
Takashi Iwai86e07d32005-11-17 15:08:02 +01001295static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1298}
1299
Takashi Iwai86e07d32005-11-17 15:08:02 +01001300static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1303}
1304
1305
1306/*
1307 * Selector Unit
1308 */
1309
1310/* info callback for selector unit
1311 * use an enumerator type for routing
1312 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001313static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001315 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 char **itemlist = (char **)kcontrol->private_value;
1317
1318 snd_assert(itemlist, return -EINVAL);
1319 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1320 uinfo->count = 1;
1321 uinfo->value.enumerated.items = cval->max;
1322 if ((int)uinfo->value.enumerated.item >= cval->max)
1323 uinfo->value.enumerated.item = cval->max - 1;
1324 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1325 return 0;
1326}
1327
1328/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001329static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001331 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int val, err;
1333
1334 err = get_cur_ctl_value(cval, 0, &val);
1335 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001336 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 ucontrol->value.enumerated.item[0] = 0;
1338 return 0;
1339 }
1340 return err;
1341 }
1342 val = get_relative_value(cval, val);
1343 ucontrol->value.enumerated.item[0] = val;
1344 return 0;
1345}
1346
1347/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001348static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001350 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int val, oval, err;
1352
1353 err = get_cur_ctl_value(cval, 0, &oval);
1354 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001355 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return 0;
1357 return err;
1358 }
1359 val = ucontrol->value.enumerated.item[0];
1360 val = get_abs_value(cval, val);
1361 if (val != oval) {
1362 set_cur_ctl_value(cval, 0, val);
1363 return 1;
1364 }
1365 return 0;
1366}
1367
1368/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001369static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1371 .name = "", /* will be filled later */
1372 .info = mixer_ctl_selector_info,
1373 .get = mixer_ctl_selector_get,
1374 .put = mixer_ctl_selector_put,
1375};
1376
1377
1378/* private free callback.
1379 * free both private_data and private_value
1380 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001381static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 int i, num_ins = 0;
1384
1385 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001386 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 num_ins = cval->max;
1388 kfree(cval);
1389 kctl->private_data = NULL;
1390 }
1391 if (kctl->private_value) {
1392 char **itemlist = (char **)kctl->private_value;
1393 for (i = 0; i < num_ins; i++)
1394 kfree(itemlist[i]);
1395 kfree(itemlist);
1396 kctl->private_value = 0;
1397 }
1398}
1399
1400/*
1401 * parse a selector unit
1402 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001403static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 unsigned int num_ins = desc[4];
1406 unsigned int i, nameid, len;
1407 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001408 struct usb_mixer_elem_info *cval;
1409 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 char **namelist;
1411
1412 if (! num_ins || desc[0] < 6 + num_ins) {
1413 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1414 return -EINVAL;
1415 }
1416
1417 for (i = 0; i < num_ins; i++) {
1418 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1419 return err;
1420 }
1421
1422 if (num_ins == 1) /* only one ? nonsense! */
1423 return 0;
1424
1425 if (check_ignored_ctl(state, unitid, 0))
1426 return 0;
1427
Takashi Iwai561b2202005-09-09 14:22:34 +02001428 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (! cval) {
1430 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1431 return -ENOMEM;
1432 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001433 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 cval->id = unitid;
1435 cval->val_type = USB_MIXER_U8;
1436 cval->channels = 1;
1437 cval->min = 1;
1438 cval->max = num_ins;
1439 cval->res = 1;
1440 cval->initialized = 1;
1441
1442 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1443 if (! namelist) {
1444 snd_printk(KERN_ERR "cannot malloc\n");
1445 kfree(cval);
1446 return -ENOMEM;
1447 }
1448#define MAX_ITEM_NAME_LEN 64
1449 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001450 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 len = 0;
1452 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1453 if (! namelist[i]) {
1454 snd_printk(KERN_ERR "cannot malloc\n");
1455 while (--i > 0)
1456 kfree(namelist[i]);
1457 kfree(namelist);
1458 kfree(cval);
1459 return -ENOMEM;
1460 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001461 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1462 MAX_ITEM_NAME_LEN);
1463 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1465 if (! len)
1466 sprintf(namelist[i], "Input %d", i);
1467 }
1468
1469 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1470 if (! kctl) {
1471 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1472 kfree(cval);
1473 return -ENOMEM;
1474 }
1475 kctl->private_value = (unsigned long)namelist;
1476 kctl->private_free = usb_mixer_selector_elem_free;
1477
1478 nameid = desc[desc[0] - 1];
1479 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1480 if (len)
1481 ;
1482 else if (nameid)
1483 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1484 else {
1485 len = get_term_name(state, &state->oterm,
1486 kctl->id.name, sizeof(kctl->id.name), 0);
1487 if (! len)
1488 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1489
1490 if ((state->oterm.type & 0xff00) == 0x0100)
1491 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1492 else
1493 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1494 }
1495
1496 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1497 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001498 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return err;
1500
1501 return 0;
1502}
1503
1504
1505/*
1506 * parse an audio unit recursively
1507 */
1508
Takashi Iwai86e07d32005-11-17 15:08:02 +01001509static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 unsigned char *p1;
1512
1513 if (test_and_set_bit(unitid, state->unitbitmap))
1514 return 0; /* the unit already visited */
1515
1516 p1 = find_audio_control_unit(state, unitid);
1517 if (!p1) {
1518 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1519 return -EINVAL;
1520 }
1521
1522 switch (p1[2]) {
1523 case INPUT_TERMINAL:
1524 return 0; /* NOP */
1525 case MIXER_UNIT:
1526 return parse_audio_mixer_unit(state, unitid, p1);
1527 case SELECTOR_UNIT:
1528 return parse_audio_selector_unit(state, unitid, p1);
1529 case FEATURE_UNIT:
1530 return parse_audio_feature_unit(state, unitid, p1);
1531 case PROCESSING_UNIT:
1532 return parse_audio_processing_unit(state, unitid, p1);
1533 case EXTENSION_UNIT:
1534 return parse_audio_extension_unit(state, unitid, p1);
1535 default:
1536 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1537 return -EINVAL;
1538 }
1539}
1540
Clemens Ladisch84957a82005-04-29 16:23:13 +02001541static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1542{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001543 kfree(mixer->id_elems);
1544 if (mixer->urb) {
1545 kfree(mixer->urb->transfer_buffer);
1546 usb_free_urb(mixer->urb);
1547 }
Clemens Ladischb259b102005-04-29 16:29:28 +02001548 if (mixer->rc_urb)
1549 usb_free_urb(mixer->rc_urb);
1550 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001551 kfree(mixer);
1552}
1553
Takashi Iwai86e07d32005-11-17 15:08:02 +01001554static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001555{
1556 struct usb_mixer_interface *mixer = device->device_data;
1557 snd_usb_mixer_free(mixer);
1558 return 0;
1559}
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561/*
1562 * create mixer controls
1563 *
1564 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1565 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001566static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001569 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int err;
1571 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001572 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Clemens Ladisch84957a82005-04-29 16:23:13 +02001574 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001576 state.chip = mixer->chip;
1577 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 state.buffer = hostif->extra;
1579 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001582 for (map = usbmix_ctl_maps; map->id; map++) {
1583 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001585 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001586 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
1588 }
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 desc = NULL;
1592 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1593 if (desc[0] < 9)
1594 continue; /* invalid descriptor? */
1595 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1596 state.oterm.id = desc[3];
1597 state.oterm.type = combine_word(&desc[4]);
1598 state.oterm.name = desc[8];
1599 err = parse_audio_unit(&state, desc[7]);
1600 if (err < 0)
1601 return err;
1602 }
1603 return 0;
1604}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001605
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001606static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1607 int unitid)
1608{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001609 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001610
1611 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1612 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1613 info->elem_id);
1614}
1615
Clemens Ladischb259b102005-04-29 16:29:28 +02001616static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1617 int unitid)
1618{
Clemens Ladischaafad562005-05-10 14:47:38 +02001619 if (mixer->rc_type == RC_NONE)
1620 return;
1621 /* unit ids specific to Extigy/Audigy 2 NX: */
1622 switch (unitid) {
1623 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001624 mixer->rc_urb->dev = mixer->chip->dev;
1625 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001626 break;
1627 case 4: /* digital in jack */
1628 case 7: /* line in jacks */
1629 case 19: /* speaker out jacks */
1630 case 20: /* headphones out jack */
1631 break;
1632 default:
1633 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1634 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001635 }
1636}
1637
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001638static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
1639{
1640 struct usb_mixer_interface *mixer = urb->context;
1641
1642 if (urb->status == 0) {
1643 u8 *buf = urb->transfer_buffer;
1644 int i;
1645
1646 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1647 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1648 buf[0], buf[1]);
1649 /* ignore any notifications not from the control interface */
1650 if ((buf[0] & 0x0f) != 0)
1651 continue;
1652 if (!(buf[0] & 0x40))
1653 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001654 else
1655 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001656 }
1657 }
1658 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1659 urb->dev = mixer->chip->dev;
1660 usb_submit_urb(urb, GFP_ATOMIC);
1661 }
1662}
1663
1664/* create the handler for the optional status interrupt endpoint */
1665static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1666{
1667 struct usb_host_interface *hostif;
1668 struct usb_endpoint_descriptor *ep;
1669 void *transfer_buffer;
1670 int buffer_length;
1671 unsigned int epnum;
1672
1673 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1674 /* we need one interrupt input endpoint */
1675 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1676 return 0;
1677 ep = get_endpoint(hostif, 0);
1678 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1679 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1680 return 0;
1681
1682 epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1683 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1684 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1685 if (!transfer_buffer)
1686 return -ENOMEM;
1687 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1688 if (!mixer->urb) {
1689 kfree(transfer_buffer);
1690 return -ENOMEM;
1691 }
1692 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1693 usb_rcvintpipe(mixer->chip->dev, epnum),
1694 transfer_buffer, buffer_length,
1695 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1696 usb_submit_urb(mixer->urb, GFP_KERNEL);
1697 return 0;
1698}
1699
Clemens Ladischb259b102005-04-29 16:29:28 +02001700static void snd_usb_soundblaster_remote_complete(struct urb *urb,
1701 struct pt_regs *regs)
1702{
1703 struct usb_mixer_interface *mixer = urb->context;
1704 /*
1705 * format of remote control data:
1706 * Extigy: xx 00
1707 * Audigy 2 NX: 06 80 xx 00 00 00
1708 */
1709 int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2;
1710 u32 code;
1711
1712 if (urb->status < 0 || urb->actual_length <= offset)
1713 return;
1714 code = mixer->rc_buffer[offset];
1715 /* the Mute button actually changes the mixer control */
1716 if (code == 13)
1717 snd_usb_mixer_notify_id(mixer, 18);
1718 mixer->rc_code = code;
1719 wmb();
1720 wake_up(&mixer->rc_waitq);
1721}
1722
Takashi Iwai86e07d32005-11-17 15:08:02 +01001723static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
Clemens Ladischb259b102005-04-29 16:29:28 +02001724{
1725 struct usb_mixer_interface *mixer = hw->private_data;
1726
1727 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1728 return -EBUSY;
1729 return 0;
1730}
1731
Takashi Iwai86e07d32005-11-17 15:08:02 +01001732static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
Clemens Ladischb259b102005-04-29 16:29:28 +02001733{
1734 struct usb_mixer_interface *mixer = hw->private_data;
1735
1736 clear_bit(0, &mixer->rc_hwdep_open);
1737 smp_mb__after_clear_bit();
1738 return 0;
1739}
1740
Takashi Iwai86e07d32005-11-17 15:08:02 +01001741static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001742 long count, loff_t *offset)
1743{
1744 struct usb_mixer_interface *mixer = hw->private_data;
1745 int err;
1746 u32 rc_code;
1747
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001748 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001749 return -EINVAL;
1750 err = wait_event_interruptible(mixer->rc_waitq,
1751 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1752 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001753 if (count == 1)
1754 err = put_user(rc_code, buf);
1755 else
1756 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001757 }
1758 return err < 0 ? err : count;
1759}
1760
Takashi Iwai86e07d32005-11-17 15:08:02 +01001761static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001762 poll_table *wait)
1763{
1764 struct usb_mixer_interface *mixer = hw->private_data;
1765
1766 poll_wait(file, &mixer->rc_waitq, wait);
1767 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1768}
1769
1770static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1771{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001772 struct snd_hwdep *hwdep;
Clemens Ladischb259b102005-04-29 16:29:28 +02001773 int err, len;
1774
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001775 switch (mixer->chip->usb_id) {
1776 case USB_ID(0x041e, 0x3000):
Clemens Ladischb259b102005-04-29 16:29:28 +02001777 mixer->rc_type = RC_EXTIGY;
1778 len = 2;
1779 break;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001780 case USB_ID(0x041e, 0x3020):
Clemens Ladischb259b102005-04-29 16:29:28 +02001781 mixer->rc_type = RC_AUDIGY2NX;
1782 len = 6;
1783 break;
1784 default:
1785 return 0;
1786 }
1787
1788 init_waitqueue_head(&mixer->rc_waitq);
1789 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1790 if (err < 0)
1791 return err;
1792 snprintf(hwdep->name, sizeof(hwdep->name),
1793 "%s remote control", mixer->chip->card->shortname);
1794 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1795 hwdep->private_data = mixer;
1796 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1797 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1798 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1799 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1800
1801 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1802 if (!mixer->rc_urb)
1803 return -ENOMEM;
1804 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1805 if (!mixer->rc_setup_packet) {
1806 usb_free_urb(mixer->rc_urb);
1807 mixer->rc_urb = NULL;
1808 return -ENOMEM;
1809 }
1810 mixer->rc_setup_packet->bRequestType =
1811 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1812 mixer->rc_setup_packet->bRequest = GET_MEM;
1813 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1814 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1815 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1816 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1817 usb_rcvctrlpipe(mixer->chip->dev, 0),
1818 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1819 snd_usb_soundblaster_remote_complete, mixer);
1820 return 0;
1821}
1822
Takashi Iwai86e07d32005-11-17 15:08:02 +01001823static int snd_audigy2nx_led_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001824{
1825 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1826 uinfo->count = 1;
1827 uinfo->value.integer.min = 0;
1828 uinfo->value.integer.max = 1;
1829 return 0;
1830}
1831
Takashi Iwai86e07d32005-11-17 15:08:02 +01001832static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001833{
1834 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1835 int index = kcontrol->private_value;
1836
1837 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1838 return 0;
1839}
1840
Takashi Iwai86e07d32005-11-17 15:08:02 +01001841static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001842{
1843 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1844 int index = kcontrol->private_value;
1845 int value = ucontrol->value.integer.value[0];
1846 int err, changed;
1847
1848 if (value > 1)
1849 return -EINVAL;
1850 changed = value != mixer->audigy2nx_leds[index];
1851 err = snd_usb_ctl_msg(mixer->chip->dev,
1852 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1853 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1854 value, index + 2, NULL, 0, 100);
1855 if (err < 0)
1856 return err;
1857 mixer->audigy2nx_leds[index] = value;
1858 return changed;
1859}
1860
Takashi Iwai86e07d32005-11-17 15:08:02 +01001861static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001862 {
1863 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1864 .name = "CMSS LED Switch",
1865 .info = snd_audigy2nx_led_info,
1866 .get = snd_audigy2nx_led_get,
1867 .put = snd_audigy2nx_led_put,
1868 .private_value = 0,
1869 },
1870 {
1871 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1872 .name = "Power LED Switch",
1873 .info = snd_audigy2nx_led_info,
1874 .get = snd_audigy2nx_led_get,
1875 .put = snd_audigy2nx_led_put,
1876 .private_value = 1,
1877 },
1878 {
1879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1880 .name = "Dolby Digital LED Switch",
1881 .info = snd_audigy2nx_led_info,
1882 .get = snd_audigy2nx_led_get,
1883 .put = snd_audigy2nx_led_put,
1884 .private_value = 2,
1885 },
1886};
1887
1888static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1889{
1890 int i, err;
1891
1892 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1893 err = snd_ctl_add(mixer->chip->card,
1894 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1895 if (err < 0)
1896 return err;
1897 }
1898 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1899 return 0;
1900}
1901
Takashi Iwai86e07d32005-11-17 15:08:02 +01001902static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1903 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02001904{
1905 static const struct {
1906 int unitid;
1907 const char *name;
1908 } jacks[] = {
1909 {4, "dig in "},
1910 {7, "line in"},
1911 {19, "spk out"},
1912 {20, "hph out"},
1913 };
1914 struct usb_mixer_interface *mixer = entry->private_data;
1915 int i, err;
1916 u8 buf[3];
1917
1918 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1919 for (i = 0; i < ARRAY_SIZE(jacks); ++i) {
1920 snd_iprintf(buffer, "%s: ", jacks[i].name);
1921 err = snd_usb_ctl_msg(mixer->chip->dev,
1922 usb_rcvctrlpipe(mixer->chip->dev, 0),
1923 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
1924 USB_RECIP_INTERFACE, 0,
1925 jacks[i].unitid << 8, buf, 3, 100);
1926 if (err == 3 && buf[0] == 3)
1927 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
1928 else
1929 snd_iprintf(buffer, "?\n");
1930 }
1931}
1932
Takashi Iwai86e07d32005-11-17 15:08:02 +01001933int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001934{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001935 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001936 .dev_free = snd_usb_mixer_dev_free
1937 };
1938 struct usb_mixer_interface *mixer;
1939 int err;
1940
1941 strcpy(chip->card->mixername, "USB Mixer");
1942
Takashi Iwai561b2202005-09-09 14:22:34 +02001943 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001944 if (!mixer)
1945 return -ENOMEM;
1946 mixer->chip = chip;
1947 mixer->ctrlif = ctrlif;
1948#ifdef IGNORE_CTL_ERROR
1949 mixer->ignore_ctl_error = 1;
1950#endif
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001951 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
1952 if (!mixer->id_elems) {
1953 kfree(mixer);
1954 return -ENOMEM;
1955 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001956
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001957 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001958 (err = snd_usb_mixer_status_create(mixer)) < 0)
1959 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001960
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001961 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1962 goto _error;
1963
1964 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001965 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02001966
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001967 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
1968 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02001969 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
1970 snd_info_set_text_ops(entry, mixer, 1024,
1971 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02001972 }
1973
Clemens Ladisch84957a82005-04-29 16:23:13 +02001974 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001975 if (err < 0)
1976 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001977 list_add(&mixer->list, &chip->mixer_list);
1978 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001979
1980_error:
1981 snd_usb_mixer_free(mixer);
1982 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001983}
1984
1985void snd_usb_mixer_disconnect(struct list_head *p)
1986{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001987 struct usb_mixer_interface *mixer;
1988
1989 mixer = list_entry(p, struct usb_mixer_interface, list);
1990 if (mixer->urb)
1991 usb_kill_urb(mixer->urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001992 if (mixer->rc_urb)
1993 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001994}