[ALSA] Remove unused tlv_rw field from struct snd_kcontrol
[deliverable/linux.git] / sound / usb / usbmixer.c
CommitLineData
1da177e4
LT
1/*
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>
b259b10c 38#include <sound/hwdep.h>
aafad562 39#include <sound/info.h>
1da177e4
LT
40
41#include "usbaudio.h"
42
1da177e4
LT
43/*
44 */
45
46/* ignore error from controls - for debugging */
47/* #define IGNORE_CTL_ERROR */
48
4d1a70da
RC
49/*
50 * Sound Blaster remote control configuration
51 *
52 * format of remote control data:
53 * Extigy: xx 00
54 * Audigy 2 NX: 06 80 xx 00 00 00
55 * Live! 24-bit: 06 80 xx yy 22 83
56 */
57static const struct rc_config {
58 u32 usb_id;
59 u8 offset;
60 u8 length;
61 u8 packet_length;
62 u8 mute_mixer_id;
63 u32 mute_code;
64} rc_configs[] = {
65 { USB_ID(0x041e, 0x3000), 0, 1, 2, 18, 0x0013 }, /* Extigy */
66 { USB_ID(0x041e, 0x3020), 2, 1, 6, 18, 0x0013 }, /* Audigy 2 NX */
67 { USB_ID(0x041e, 0x3040), 2, 2, 6, 2, 0x6e91 }, /* Live! 24-bit */
68};
69
84957a8a 70struct usb_mixer_interface {
86e07d34 71 struct snd_usb_audio *chip;
84957a8a
CL
72 unsigned int ctrlif;
73 struct list_head list;
74 unsigned int ignore_ctl_error;
6639b6c2 75 struct urb *urb;
86e07d34 76 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
b259b10c
CL
77
78 /* Sound Blaster remote control stuff */
4d1a70da 79 const struct rc_config *rc_cfg;
b259b10c
CL
80 unsigned long rc_hwdep_open;
81 u32 rc_code;
82 wait_queue_head_t rc_waitq;
83 struct urb *rc_urb;
84 struct usb_ctrlrequest *rc_setup_packet;
85 u8 rc_buffer[6];
93446edc
CL
86
87 u8 audigy2nx_leds[3];
84957a8a
CL
88};
89
90
1da177e4
LT
91struct usb_audio_term {
92 int id;
93 int type;
94 int channels;
95 unsigned int chconfig;
96 int name;
97};
98
99struct usbmix_name_map;
100
86e07d34
TI
101struct mixer_build {
102 struct snd_usb_audio *chip;
84957a8a 103 struct usb_mixer_interface *mixer;
1da177e4
LT
104 unsigned char *buffer;
105 unsigned int buflen;
707e6073 106 DECLARE_BITMAP(unitbitmap, 256);
86e07d34 107 struct usb_audio_term oterm;
1da177e4 108 const struct usbmix_name_map *map;
8e062ec7 109 const struct usbmix_selector_map *selector_map;
1da177e4
LT
110};
111
112struct usb_mixer_elem_info {
84957a8a 113 struct usb_mixer_interface *mixer;
86e07d34
TI
114 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
115 struct snd_ctl_elem_id *elem_id;
1da177e4
LT
116 unsigned int id;
117 unsigned int control; /* CS or ICN (high byte) */
118 unsigned int cmask; /* channel mask bitmap: 0 = master */
119 int channels;
120 int val_type;
121 int min, max, res;
6639b6c2 122 u8 initialized;
1da177e4
LT
123};
124
125
126enum {
127 USB_FEATURE_NONE = 0,
128 USB_FEATURE_MUTE = 1,
129 USB_FEATURE_VOLUME,
130 USB_FEATURE_BASS,
131 USB_FEATURE_MID,
132 USB_FEATURE_TREBLE,
133 USB_FEATURE_GEQ,
134 USB_FEATURE_AGC,
135 USB_FEATURE_DELAY,
136 USB_FEATURE_BASSBOOST,
137 USB_FEATURE_LOUDNESS
138};
139
140enum {
141 USB_MIXER_BOOLEAN,
142 USB_MIXER_INV_BOOLEAN,
143 USB_MIXER_S8,
144 USB_MIXER_U8,
145 USB_MIXER_S16,
146 USB_MIXER_U16,
147};
148
149enum {
150 USB_PROC_UPDOWN = 1,
151 USB_PROC_UPDOWN_SWITCH = 1,
152 USB_PROC_UPDOWN_MODE_SEL = 2,
153
154 USB_PROC_PROLOGIC = 2,
155 USB_PROC_PROLOGIC_SWITCH = 1,
156 USB_PROC_PROLOGIC_MODE_SEL = 2,
157
158 USB_PROC_3DENH = 3,
159 USB_PROC_3DENH_SWITCH = 1,
160 USB_PROC_3DENH_SPACE = 2,
161
162 USB_PROC_REVERB = 4,
163 USB_PROC_REVERB_SWITCH = 1,
164 USB_PROC_REVERB_LEVEL = 2,
165 USB_PROC_REVERB_TIME = 3,
166 USB_PROC_REVERB_DELAY = 4,
167
168 USB_PROC_CHORUS = 5,
169 USB_PROC_CHORUS_SWITCH = 1,
170 USB_PROC_CHORUS_LEVEL = 2,
171 USB_PROC_CHORUS_RATE = 3,
172 USB_PROC_CHORUS_DEPTH = 4,
173
174 USB_PROC_DCR = 6,
175 USB_PROC_DCR_SWITCH = 1,
176 USB_PROC_DCR_RATIO = 2,
177 USB_PROC_DCR_MAX_AMP = 3,
178 USB_PROC_DCR_THRESHOLD = 4,
179 USB_PROC_DCR_ATTACK = 5,
180 USB_PROC_DCR_RELEASE = 6,
181};
182
183#define MAX_CHANNELS 10 /* max logical channels */
184
185
186/*
187 * manual mapping of mixer names
188 * if the mixer topology is too complicated and the parsed names are
189 * ambiguous, add the entries in usbmixer_maps.c.
190 */
191#include "usbmixer_maps.c"
192
193/* get the mapped name if the unit matches */
86e07d34 194static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
1da177e4
LT
195{
196 const struct usbmix_name_map *p;
197
198 if (! state->map)
199 return 0;
200
201 for (p = state->map; p->id; p++) {
202 if (p->id == unitid && p->name &&
203 (! control || ! p->control || control == p->control)) {
204 buflen--;
205 return strlcpy(buf, p->name, buflen);
206 }
207 }
208 return 0;
209}
210
211/* check whether the control should be ignored */
86e07d34 212static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
1da177e4
LT
213{
214 const struct usbmix_name_map *p;
215
216 if (! state->map)
217 return 0;
218 for (p = state->map; p->id; p++) {
219 if (p->id == unitid && ! p->name &&
220 (! control || ! p->control || control == p->control)) {
221 // printk("ignored control %d:%d\n", unitid, control);
222 return 1;
223 }
224 }
225 return 0;
226}
227
8e062ec7 228/* get the mapped selector source name */
86e07d34 229static int check_mapped_selector_name(struct mixer_build *state, int unitid,
8e062ec7
CL
230 int index, char *buf, int buflen)
231{
232 const struct usbmix_selector_map *p;
233
234 if (! state->selector_map)
235 return 0;
236 for (p = state->selector_map; p->id; p++) {
237 if (p->id == unitid && index < p->count)
238 return strlcpy(buf, p->names[index], buflen);
239 }
240 return 0;
241}
242
1da177e4
LT
243/*
244 * find an audio control unit with the given unit id
245 */
86e07d34 246static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
1da177e4
LT
247{
248 unsigned char *p;
249
250 p = NULL;
251 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
252 USB_DT_CS_INTERFACE)) != NULL) {
253 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
254 return p;
255 }
256 return NULL;
257}
258
259
260/*
261 * copy a string with the given id
262 */
86e07d34 263static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
1da177e4
LT
264{
265 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
266 buf[len] = 0;
267 return len;
268}
269
270/*
271 * convert from the byte/word on usb descriptor to the zero-based integer
272 */
86e07d34 273static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
274{
275 switch (cval->val_type) {
276 case USB_MIXER_BOOLEAN:
277 return !!val;
278 case USB_MIXER_INV_BOOLEAN:
279 return !val;
280 case USB_MIXER_U8:
281 val &= 0xff;
282 break;
283 case USB_MIXER_S8:
284 val &= 0xff;
285 if (val >= 0x80)
286 val -= 0x100;
287 break;
288 case USB_MIXER_U16:
289 val &= 0xffff;
290 break;
291 case USB_MIXER_S16:
292 val &= 0xffff;
293 if (val >= 0x8000)
294 val -= 0x10000;
295 break;
296 }
297 return val;
298}
299
300/*
301 * convert from the zero-based int to the byte/word for usb descriptor
302 */
86e07d34 303static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
304{
305 switch (cval->val_type) {
306 case USB_MIXER_BOOLEAN:
307 return !!val;
308 case USB_MIXER_INV_BOOLEAN:
309 return !val;
310 case USB_MIXER_S8:
311 case USB_MIXER_U8:
312 return val & 0xff;
313 case USB_MIXER_S16:
314 case USB_MIXER_U16:
315 return val & 0xffff;
316 }
317 return 0; /* not reached */
318}
319
86e07d34 320static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
321{
322 if (! cval->res)
323 cval->res = 1;
324 if (val < cval->min)
325 return 0;
14790f1c
TI
326 else if (val >= cval->max)
327 return (cval->max - cval->min + cval->res - 1) / cval->res;
1da177e4
LT
328 else
329 return (val - cval->min) / cval->res;
330}
331
86e07d34 332static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
333{
334 if (val < 0)
335 return cval->min;
336 if (! cval->res)
337 cval->res = 1;
338 val *= cval->res;
339 val += cval->min;
340 if (val > cval->max)
341 return cval->max;
342 return val;
343}
344
345
346/*
347 * retrieve a mixer value
348 */
349
86e07d34 350static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
1da177e4
LT
351{
352 unsigned char buf[2];
353 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
354 int timeout = 10;
355
356 while (timeout-- > 0) {
84957a8a
CL
357 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
358 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
1da177e4
LT
359 request,
360 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
84957a8a 361 validx, cval->mixer->ctrlif | (cval->id << 8),
1da177e4
LT
362 buf, val_len, 100) >= 0) {
363 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
364 return 0;
365 }
366 }
84957a8a
CL
367 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
368 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
1da177e4
LT
369 return -EINVAL;
370}
371
86e07d34 372static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
1da177e4
LT
373{
374 return get_ctl_value(cval, GET_CUR, validx, value);
375}
376
377/* channel = 0: master, 1 = first channel */
86e07d34 378static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
1da177e4
LT
379{
380 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
381}
382
383/*
384 * set a mixer value
385 */
386
86e07d34 387static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
1da177e4
LT
388{
389 unsigned char buf[2];
390 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
391 int timeout = 10;
392
393 value_set = convert_bytes_value(cval, value_set);
394 buf[0] = value_set & 0xff;
395 buf[1] = (value_set >> 8) & 0xff;
396 while (timeout -- > 0)
84957a8a
CL
397 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
398 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
1da177e4
LT
399 request,
400 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
84957a8a 401 validx, cval->mixer->ctrlif | (cval->id << 8),
1da177e4
LT
402 buf, val_len, 100) >= 0)
403 return 0;
84957a8a
CL
404 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
405 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
1da177e4
LT
406 return -EINVAL;
407}
408
86e07d34 409static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
1da177e4
LT
410{
411 return set_ctl_value(cval, SET_CUR, validx, value);
412}
413
86e07d34 414static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
1da177e4
LT
415{
416 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
417}
418
419
420/*
421 * parser routines begin here...
422 */
423
86e07d34 424static int parse_audio_unit(struct mixer_build *state, int unitid);
1da177e4
LT
425
426
427/*
428 * check if the input/output channel routing is enabled on the given bitmap.
429 * used for mixer unit parser
430 */
431static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
432{
433 int idx = ich * num_outs + och;
434 return bmap[idx >> 3] & (0x80 >> (idx & 7));
435}
436
437
438/*
439 * add an alsa control element
440 * search and increment the index until an empty slot is found.
441 *
442 * if failed, give up and free the control instance.
443 */
444
86e07d34 445static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
1da177e4 446{
86e07d34 447 struct usb_mixer_elem_info *cval = kctl->private_data;
1da177e4 448 int err;
84957a8a
CL
449
450 while (snd_ctl_find_id(state->chip->card, &kctl->id))
1da177e4 451 kctl->id.index++;
84957a8a 452 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
1da177e4 453 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
6639b6c2 454 return err;
1da177e4 455 }
6639b6c2
CL
456 cval->elem_id = &kctl->id;
457 cval->next_id_elem = state->mixer->id_elems[cval->id];
458 state->mixer->id_elems[cval->id] = cval;
459 return 0;
1da177e4
LT
460}
461
462
463/*
464 * get a terminal name string
465 */
466
467static struct iterm_name_combo {
468 int type;
469 char *name;
470} iterm_names[] = {
471 { 0x0300, "Output" },
472 { 0x0301, "Speaker" },
473 { 0x0302, "Headphone" },
474 { 0x0303, "HMD Audio" },
475 { 0x0304, "Desktop Speaker" },
476 { 0x0305, "Room Speaker" },
477 { 0x0306, "Com Speaker" },
478 { 0x0307, "LFE" },
479 { 0x0600, "External In" },
480 { 0x0601, "Analog In" },
481 { 0x0602, "Digital In" },
482 { 0x0603, "Line" },
483 { 0x0604, "Legacy In" },
484 { 0x0605, "IEC958 In" },
485 { 0x0606, "1394 DA Stream" },
486 { 0x0607, "1394 DV Stream" },
487 { 0x0700, "Embedded" },
488 { 0x0701, "Noise Source" },
489 { 0x0702, "Equalization Noise" },
490 { 0x0703, "CD" },
491 { 0x0704, "DAT" },
492 { 0x0705, "DCC" },
493 { 0x0706, "MiniDisk" },
494 { 0x0707, "Analog Tape" },
495 { 0x0708, "Phonograph" },
496 { 0x0709, "VCR Audio" },
497 { 0x070a, "Video Disk Audio" },
498 { 0x070b, "DVD Audio" },
499 { 0x070c, "TV Tuner Audio" },
500 { 0x070d, "Satellite Rec Audio" },
501 { 0x070e, "Cable Tuner Audio" },
502 { 0x070f, "DSS Audio" },
503 { 0x0710, "Radio Receiver" },
504 { 0x0711, "Radio Transmitter" },
505 { 0x0712, "Multi-Track Recorder" },
506 { 0x0713, "Synthesizer" },
507 { 0 },
508};
509
86e07d34 510static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
1da177e4
LT
511 unsigned char *name, int maxlen, int term_only)
512{
513 struct iterm_name_combo *names;
514
515 if (iterm->name)
516 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
517
518 /* virtual type - not a real terminal */
519 if (iterm->type >> 16) {
520 if (term_only)
521 return 0;
522 switch (iterm->type >> 16) {
523 case SELECTOR_UNIT:
524 strcpy(name, "Selector"); return 8;
525 case PROCESSING_UNIT:
526 strcpy(name, "Process Unit"); return 12;
527 case EXTENSION_UNIT:
528 strcpy(name, "Ext Unit"); return 8;
529 case MIXER_UNIT:
530 strcpy(name, "Mixer"); return 5;
531 default:
532 return sprintf(name, "Unit %d", iterm->id);
533 }
534 }
535
536 switch (iterm->type & 0xff00) {
537 case 0x0100:
538 strcpy(name, "PCM"); return 3;
539 case 0x0200:
540 strcpy(name, "Mic"); return 3;
541 case 0x0400:
542 strcpy(name, "Headset"); return 7;
543 case 0x0500:
544 strcpy(name, "Phone"); return 5;
545 }
546
547 for (names = iterm_names; names->type; names++)
548 if (names->type == iterm->type) {
549 strcpy(name, names->name);
550 return strlen(names->name);
551 }
552 return 0;
553}
554
555
556/*
557 * parse the source unit recursively until it reaches to a terminal
558 * or a branched unit.
559 */
86e07d34 560static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
1da177e4
LT
561{
562 unsigned char *p1;
563
564 memset(term, 0, sizeof(*term));
565 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
566 term->id = id;
567 switch (p1[2]) {
568 case INPUT_TERMINAL:
569 term->type = combine_word(p1 + 4);
570 term->channels = p1[7];
571 term->chconfig = combine_word(p1 + 8);
572 term->name = p1[11];
573 return 0;
574 case FEATURE_UNIT:
575 id = p1[4];
576 break; /* continue to parse */
577 case MIXER_UNIT:
578 term->type = p1[2] << 16; /* virtual type */
579 term->channels = p1[5 + p1[4]];
580 term->chconfig = combine_word(p1 + 6 + p1[4]);
581 term->name = p1[p1[0] - 1];
582 return 0;
583 case SELECTOR_UNIT:
584 /* call recursively to retrieve the channel info */
585 if (check_input_term(state, p1[5], term) < 0)
586 return -ENODEV;
587 term->type = p1[2] << 16; /* virtual type */
588 term->id = id;
589 term->name = p1[9 + p1[0] - 1];
590 return 0;
591 case PROCESSING_UNIT:
592 case EXTENSION_UNIT:
593 if (p1[6] == 1) {
594 id = p1[7];
595 break; /* continue to parse */
596 }
597 term->type = p1[2] << 16; /* virtual type */
598 term->channels = p1[7 + p1[6]];
599 term->chconfig = combine_word(p1 + 8 + p1[6]);
600 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
601 return 0;
602 default:
603 return -ENODEV;
604 }
605 }
606 return -ENODEV;
607}
608
609
610/*
611 * Feature Unit
612 */
613
614/* feature unit control information */
615struct usb_feature_control_info {
616 const char *name;
617 unsigned int type; /* control type (mute, volume, etc.) */
618};
619
620static struct usb_feature_control_info audio_feature_info[] = {
621 { "Mute", USB_MIXER_INV_BOOLEAN },
622 { "Volume", USB_MIXER_S16 },
623 { "Tone Control - Bass", USB_MIXER_S8 },
624 { "Tone Control - Mid", USB_MIXER_S8 },
625 { "Tone Control - Treble", USB_MIXER_S8 },
626 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
627 { "Auto Gain Control", USB_MIXER_BOOLEAN },
628 { "Delay Control", USB_MIXER_U16 },
629 { "Bass Boost", USB_MIXER_BOOLEAN },
630 { "Loudness", USB_MIXER_BOOLEAN },
631};
632
633
634/* private_free callback */
86e07d34 635static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
1da177e4 636{
4d572776
JJ
637 kfree(kctl->private_data);
638 kctl->private_data = NULL;
1da177e4
LT
639}
640
641
642/*
643 * interface to ALSA control for feature/mixer units
644 */
645
646/*
647 * retrieve the minimum and maximum values for the specified control
648 */
86e07d34 649static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
1da177e4
LT
650{
651 /* for failsafe */
652 cval->min = default_min;
653 cval->max = cval->min + 1;
654 cval->res = 1;
655
656 if (cval->val_type == USB_MIXER_BOOLEAN ||
657 cval->val_type == USB_MIXER_INV_BOOLEAN) {
658 cval->initialized = 1;
659 } else {
660 int minchn = 0;
661 if (cval->cmask) {
662 int i;
663 for (i = 0; i < MAX_CHANNELS; i++)
664 if (cval->cmask & (1 << i)) {
665 minchn = i + 1;
666 break;
667 }
668 }
669 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
670 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
84957a8a
CL
671 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
672 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
1da177e4
LT
673 return -EINVAL;
674 }
675 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
676 cval->res = 1;
677 } else {
678 int last_valid_res = cval->res;
679
680 while (cval->res > 1) {
681 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
682 break;
683 cval->res /= 2;
684 }
685 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
686 cval->res = last_valid_res;
687 }
688 if (cval->res == 0)
689 cval->res = 1;
14790f1c
TI
690
691 /* Additional checks for the proper resolution
692 *
693 * Some devices report smaller resolutions than actually
694 * reacting. They don't return errors but simply clip
695 * to the lower aligned value.
696 */
697 if (cval->min + cval->res < cval->max) {
698 int last_valid_res = cval->res;
699 int saved, test, check;
700 get_cur_mix_value(cval, minchn, &saved);
701 for (;;) {
702 test = saved;
703 if (test < cval->max)
704 test += cval->res;
705 else
706 test -= cval->res;
707 if (test < cval->min || test > cval->max ||
708 set_cur_mix_value(cval, minchn, test) ||
709 get_cur_mix_value(cval, minchn, &check)) {
710 cval->res = last_valid_res;
711 break;
712 }
713 if (test == check)
714 break;
715 cval->res *= 2;
716 }
717 set_cur_mix_value(cval, minchn, saved);
718 }
719
1da177e4
LT
720 cval->initialized = 1;
721 }
722 return 0;
723}
724
725
726/* get a feature/mixer unit info */
86e07d34 727static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4 728{
86e07d34 729 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
730
731 if (cval->val_type == USB_MIXER_BOOLEAN ||
732 cval->val_type == USB_MIXER_INV_BOOLEAN)
733 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
734 else
735 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
736 uinfo->count = cval->channels;
737 if (cval->val_type == USB_MIXER_BOOLEAN ||
738 cval->val_type == USB_MIXER_INV_BOOLEAN) {
739 uinfo->value.integer.min = 0;
740 uinfo->value.integer.max = 1;
741 } else {
742 if (! cval->initialized)
743 get_min_max(cval, 0);
744 uinfo->value.integer.min = 0;
14790f1c
TI
745 uinfo->value.integer.max =
746 (cval->max - cval->min + cval->res - 1) / cval->res;
1da177e4
LT
747 }
748 return 0;
749}
750
751/* get the current value from feature/mixer unit */
86e07d34 752static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 753{
86e07d34 754 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
755 int c, cnt, val, err;
756
757 if (cval->cmask) {
758 cnt = 0;
759 for (c = 0; c < MAX_CHANNELS; c++) {
760 if (cval->cmask & (1 << c)) {
761 err = get_cur_mix_value(cval, c + 1, &val);
762 if (err < 0) {
84957a8a 763 if (cval->mixer->ignore_ctl_error) {
1da177e4
LT
764 ucontrol->value.integer.value[0] = cval->min;
765 return 0;
766 }
767 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
768 return err;
769 }
770 val = get_relative_value(cval, val);
771 ucontrol->value.integer.value[cnt] = val;
772 cnt++;
773 }
774 }
775 } else {
776 /* master channel */
777 err = get_cur_mix_value(cval, 0, &val);
778 if (err < 0) {
84957a8a 779 if (cval->mixer->ignore_ctl_error) {
1da177e4
LT
780 ucontrol->value.integer.value[0] = cval->min;
781 return 0;
782 }
783 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
784 return err;
785 }
786 val = get_relative_value(cval, val);
787 ucontrol->value.integer.value[0] = val;
788 }
789 return 0;
790}
791
792/* put the current value to feature/mixer unit */
86e07d34 793static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 794{
86e07d34 795 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
796 int c, cnt, val, oval, err;
797 int changed = 0;
798
799 if (cval->cmask) {
800 cnt = 0;
801 for (c = 0; c < MAX_CHANNELS; c++) {
802 if (cval->cmask & (1 << c)) {
803 err = get_cur_mix_value(cval, c + 1, &oval);
804 if (err < 0) {
84957a8a 805 if (cval->mixer->ignore_ctl_error)
1da177e4
LT
806 return 0;
807 return err;
808 }
809 val = ucontrol->value.integer.value[cnt];
810 val = get_abs_value(cval, val);
811 if (oval != val) {
812 set_cur_mix_value(cval, c + 1, val);
813 changed = 1;
814 }
815 get_cur_mix_value(cval, c + 1, &val);
816 cnt++;
817 }
818 }
819 } else {
820 /* master channel */
821 err = get_cur_mix_value(cval, 0, &oval);
84957a8a 822 if (err < 0 && cval->mixer->ignore_ctl_error)
1da177e4
LT
823 return 0;
824 if (err < 0)
825 return err;
826 val = ucontrol->value.integer.value[0];
827 val = get_abs_value(cval, val);
828 if (val != oval) {
829 set_cur_mix_value(cval, 0, val);
830 changed = 1;
831 }
832 }
833 return changed;
834}
835
86e07d34 836static struct snd_kcontrol_new usb_feature_unit_ctl = {
1da177e4
LT
837 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
838 .name = "", /* will be filled later manually */
839 .info = mixer_ctl_feature_info,
840 .get = mixer_ctl_feature_get,
841 .put = mixer_ctl_feature_put,
842};
843
844
845/*
846 * build a feature control
847 */
848
86e07d34 849static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
1da177e4 850 unsigned int ctl_mask, int control,
86e07d34 851 struct usb_audio_term *iterm, int unitid)
1da177e4
LT
852{
853 unsigned int len = 0;
854 int mapped_name = 0;
855 int nameid = desc[desc[0] - 1];
86e07d34
TI
856 struct snd_kcontrol *kctl;
857 struct usb_mixer_elem_info *cval;
1da177e4
LT
858
859 control++; /* change from zero-based to 1-based value */
860
861 if (control == USB_FEATURE_GEQ) {
862 /* FIXME: not supported yet */
863 return;
864 }
865
866 if (check_ignored_ctl(state, unitid, control))
867 return;
868
561b220a 869 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1da177e4
LT
870 if (! cval) {
871 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
872 return;
873 }
84957a8a 874 cval->mixer = state->mixer;
1da177e4
LT
875 cval->id = unitid;
876 cval->control = control;
877 cval->cmask = ctl_mask;
878 cval->val_type = audio_feature_info[control-1].type;
879 if (ctl_mask == 0)
880 cval->channels = 1; /* master channel */
881 else {
882 int i, c = 0;
883 for (i = 0; i < 16; i++)
884 if (ctl_mask & (1 << i))
885 c++;
886 cval->channels = c;
887 }
888
889 /* get min/max values */
890 get_min_max(cval, 0);
891
892 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
893 if (! kctl) {
894 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
895 kfree(cval);
896 return;
897 }
898 kctl->private_free = usb_mixer_elem_free;
899
900 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
901 mapped_name = len != 0;
902 if (! len && nameid)
903 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
904
905 switch (control) {
906 case USB_FEATURE_MUTE:
907 case USB_FEATURE_VOLUME:
908 /* determine the control name. the rule is:
909 * - if a name id is given in descriptor, use it.
910 * - if the connected input can be determined, then use the name
911 * of terminal type.
912 * - if the connected output can be determined, use it.
913 * - otherwise, anonymous name.
914 */
915 if (! len) {
916 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
917 if (! len)
918 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
919 if (! len)
920 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
921 "Feature %d", unitid);
922 }
923 /* determine the stream direction:
924 * if the connected output is USB stream, then it's likely a
925 * capture stream. otherwise it should be playback (hopefully :)
926 */
927 if (! mapped_name && ! (state->oterm.type >> 16)) {
928 if ((state->oterm.type & 0xff00) == 0x0100) {
929 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
930 } else {
931 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
932 }
933 }
934 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
935 sizeof(kctl->id.name));
936 break;
937
938 default:
939 if (! len)
940 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
941 sizeof(kctl->id.name));
942 break;
943 }
944
945 /* quirk for UDA1321/N101 */
946 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
947 /* is not very clear from datasheets */
948 /* I hope that the min value is -15360 for newer firmware --jk */
27d10f56
CL
949 switch (state->chip->usb_id) {
950 case USB_ID(0x0471, 0x0101):
951 case USB_ID(0x0471, 0x0104):
952 case USB_ID(0x0471, 0x0105):
953 case USB_ID(0x0672, 0x1041):
954 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
955 cval->min == -15616) {
d3d579f8 956 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
27d10f56
CL
957 cval->max = -256;
958 }
1da177e4
LT
959 }
960
961 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
962 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
84957a8a 963 add_control_to_empty(state, kctl);
1da177e4
LT
964}
965
966
967
968/*
969 * parse a feature unit
970 *
971 * most of controlls are defined here.
972 */
86e07d34 973static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1da177e4
LT
974{
975 int channels, i, j;
86e07d34 976 struct usb_audio_term iterm;
1da177e4
LT
977 unsigned int master_bits, first_ch_bits;
978 int err, csize;
979
980 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
981 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
982 return -EINVAL;
983 }
984
985 /* parse the source unit */
986 if ((err = parse_audio_unit(state, ftr[4])) < 0)
987 return err;
988
989 /* determine the input source type and name */
990 if (check_input_term(state, ftr[4], &iterm) < 0)
991 return -EINVAL;
992
993 channels = (ftr[0] - 7) / csize - 1;
994
995 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
996 if (channels > 0)
997 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
998 else
999 first_ch_bits = 0;
1000 /* check all control types */
1001 for (i = 0; i < 10; i++) {
1002 unsigned int ch_bits = 0;
1003 for (j = 0; j < channels; j++) {
1004 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1005 if (mask & (1 << i))
1006 ch_bits |= (1 << j);
1007 }
1008 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1009 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1010 if (master_bits & (1 << i))
1011 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1012 }
1013
1014 return 0;
1015}
1016
1017
1018/*
1019 * Mixer Unit
1020 */
1021
1022/*
1023 * build a mixer unit control
1024 *
1025 * the callbacks are identical with feature unit.
1026 * input channel number (zero based) is given in control field instead.
1027 */
1028
86e07d34 1029static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1da177e4 1030 int in_pin, int in_ch, int unitid,
86e07d34 1031 struct usb_audio_term *iterm)
1da177e4 1032{
86e07d34 1033 struct usb_mixer_elem_info *cval;
1da177e4
LT
1034 unsigned int input_pins = desc[4];
1035 unsigned int num_outs = desc[5 + input_pins];
1036 unsigned int i, len;
86e07d34 1037 struct snd_kcontrol *kctl;
1da177e4
LT
1038
1039 if (check_ignored_ctl(state, unitid, 0))
1040 return;
1041
561b220a 1042 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1da177e4
LT
1043 if (! cval)
1044 return;
1045
84957a8a 1046 cval->mixer = state->mixer;
1da177e4
LT
1047 cval->id = unitid;
1048 cval->control = in_ch + 1; /* based on 1 */
1049 cval->val_type = USB_MIXER_S16;
1050 for (i = 0; i < num_outs; i++) {
1051 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1052 cval->cmask |= (1 << i);
1053 cval->channels++;
1054 }
1055 }
1056
1057 /* get min/max values */
1058 get_min_max(cval, 0);
1059
1060 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1061 if (! kctl) {
1062 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1063 kfree(cval);
1064 return;
1065 }
1066 kctl->private_free = usb_mixer_elem_free;
1067
1068 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1069 if (! len)
1070 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1071 if (! len)
1072 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1073 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1074
1075 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1076 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
84957a8a 1077 add_control_to_empty(state, kctl);
1da177e4
LT
1078}
1079
1080
1081/*
1082 * parse a mixer unit
1083 */
86e07d34 1084static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1da177e4 1085{
86e07d34 1086 struct usb_audio_term iterm;
1da177e4
LT
1087 int input_pins, num_ins, num_outs;
1088 int pin, ich, err;
1089
1090 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1091 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1092 return -EINVAL;
1093 }
1094 /* no bmControls field (e.g. Maya44) -> ignore */
1095 if (desc[0] <= 10 + input_pins) {
1096 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1097 return 0;
1098 }
1099
1100 num_ins = 0;
1101 ich = 0;
1102 for (pin = 0; pin < input_pins; pin++) {
1103 err = parse_audio_unit(state, desc[5 + pin]);
1104 if (err < 0)
1105 return err;
1106 err = check_input_term(state, desc[5 + pin], &iterm);
1107 if (err < 0)
1108 return err;
1109 num_ins += iterm.channels;
1110 for (; ich < num_ins; ++ich) {
1111 int och, ich_has_controls = 0;
1112
1113 for (och = 0; och < num_outs; ++och) {
1114 if (check_matrix_bitmap(desc + 9 + input_pins,
1115 ich, och, num_outs)) {
1116 ich_has_controls = 1;
1117 break;
1118 }
1119 }
1120 if (ich_has_controls)
1121 build_mixer_unit_ctl(state, desc, pin, ich,
1122 unitid, &iterm);
1123 }
1124 }
1125 return 0;
1126}
1127
1128
1129/*
1130 * Processing Unit / Extension Unit
1131 */
1132
1133/* get callback for processing/extension unit */
86e07d34 1134static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1135{
86e07d34 1136 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1137 int err, val;
1138
1139 err = get_cur_ctl_value(cval, cval->control << 8, &val);
84957a8a 1140 if (err < 0 && cval->mixer->ignore_ctl_error) {
1da177e4
LT
1141 ucontrol->value.integer.value[0] = cval->min;
1142 return 0;
1143 }
1144 if (err < 0)
1145 return err;
1146 val = get_relative_value(cval, val);
1147 ucontrol->value.integer.value[0] = val;
1148 return 0;
1149}
1150
1151/* put callback for processing/extension unit */
86e07d34 1152static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1153{
86e07d34 1154 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1155 int val, oval, err;
1156
1157 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1158 if (err < 0) {
84957a8a 1159 if (cval->mixer->ignore_ctl_error)
1da177e4
LT
1160 return 0;
1161 return err;
1162 }
1163 val = ucontrol->value.integer.value[0];
1164 val = get_abs_value(cval, val);
1165 if (val != oval) {
1166 set_cur_ctl_value(cval, cval->control << 8, val);
1167 return 1;
1168 }
1169 return 0;
1170}
1171
1172/* alsa control interface for processing/extension unit */
86e07d34 1173static struct snd_kcontrol_new mixer_procunit_ctl = {
1da177e4
LT
1174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1175 .name = "", /* will be filled later */
1176 .info = mixer_ctl_feature_info,
1177 .get = mixer_ctl_procunit_get,
1178 .put = mixer_ctl_procunit_put,
1179};
1180
1181
1182/*
1183 * predefined data for processing units
1184 */
1185struct procunit_value_info {
1186 int control;
1187 char *suffix;
1188 int val_type;
1189 int min_value;
1190};
1191
1192struct procunit_info {
1193 int type;
1194 char *name;
1195 struct procunit_value_info *values;
1196};
1197
1198static struct procunit_value_info updown_proc_info[] = {
1199 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1200 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1201 { 0 }
1202};
1203static struct procunit_value_info prologic_proc_info[] = {
1204 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1205 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1206 { 0 }
1207};
1208static struct procunit_value_info threed_enh_proc_info[] = {
1209 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1210 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1211 { 0 }
1212};
1213static struct procunit_value_info reverb_proc_info[] = {
1214 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1215 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1216 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1217 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1218 { 0 }
1219};
1220static struct procunit_value_info chorus_proc_info[] = {
1221 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1222 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1223 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1224 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1225 { 0 }
1226};
1227static struct procunit_value_info dcr_proc_info[] = {
1228 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1229 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1230 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1231 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1232 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1233 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1234 { 0 }
1235};
1236
1237static struct procunit_info procunits[] = {
1238 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1239 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1240 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1241 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1242 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1243 { USB_PROC_DCR, "DCR", dcr_proc_info },
1244 { 0 },
1245};
1246
1247/*
1248 * build a processing/extension unit
1249 */
86e07d34 1250static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1da177e4
LT
1251{
1252 int num_ins = dsc[6];
86e07d34
TI
1253 struct usb_mixer_elem_info *cval;
1254 struct snd_kcontrol *kctl;
1da177e4
LT
1255 int i, err, nameid, type, len;
1256 struct procunit_info *info;
1257 struct procunit_value_info *valinfo;
1258 static struct procunit_value_info default_value_info[] = {
1259 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1260 { 0 }
1261 };
1262 static struct procunit_info default_info = {
1263 0, NULL, default_value_info
1264 };
1265
1266 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1267 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1268 return -EINVAL;
1269 }
1270
1271 for (i = 0; i < num_ins; i++) {
1272 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1273 return err;
1274 }
1275
1276 type = combine_word(&dsc[4]);
1da177e4
LT
1277 for (info = list; info && info->type; info++)
1278 if (info->type == type)
1279 break;
1280 if (! info || ! info->type)
1281 info = &default_info;
1282
1283 for (valinfo = info->values; valinfo->control; valinfo++) {
1284 /* FIXME: bitmap might be longer than 8bit */
1285 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1286 continue;
1287 if (check_ignored_ctl(state, unitid, valinfo->control))
1288 continue;
561b220a 1289 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1da177e4
LT
1290 if (! cval) {
1291 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1292 return -ENOMEM;
1293 }
84957a8a 1294 cval->mixer = state->mixer;
1da177e4
LT
1295 cval->id = unitid;
1296 cval->control = valinfo->control;
1297 cval->val_type = valinfo->val_type;
1298 cval->channels = 1;
1299
1300 /* get min/max values */
1301 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1302 /* FIXME: hard-coded */
1303 cval->min = 1;
1304 cval->max = dsc[15];
1305 cval->res = 1;
1306 cval->initialized = 1;
1307 } else
1308 get_min_max(cval, valinfo->min_value);
1309
1310 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1311 if (! kctl) {
1312 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1313 kfree(cval);
1314 return -ENOMEM;
1315 }
1316 kctl->private_free = usb_mixer_elem_free;
1317
1318 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1319 ;
1320 else if (info->name)
1321 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1322 else {
1323 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1324 len = 0;
1325 if (nameid)
1326 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1327 if (! len)
1328 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1329 }
1330 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1331 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1332
1333 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1334 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
84957a8a 1335 if ((err = add_control_to_empty(state, kctl)) < 0)
1da177e4
LT
1336 return err;
1337 }
1338 return 0;
1339}
1340
1341
86e07d34 1342static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1da177e4
LT
1343{
1344 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1345}
1346
86e07d34 1347static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1da177e4
LT
1348{
1349 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1350}
1351
1352
1353/*
1354 * Selector Unit
1355 */
1356
1357/* info callback for selector unit
1358 * use an enumerator type for routing
1359 */
86e07d34 1360static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4 1361{
86e07d34 1362 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1363 char **itemlist = (char **)kcontrol->private_value;
1364
1365 snd_assert(itemlist, return -EINVAL);
1366 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1367 uinfo->count = 1;
1368 uinfo->value.enumerated.items = cval->max;
1369 if ((int)uinfo->value.enumerated.item >= cval->max)
1370 uinfo->value.enumerated.item = cval->max - 1;
1371 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1372 return 0;
1373}
1374
1375/* get callback for selector unit */
86e07d34 1376static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1377{
86e07d34 1378 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1379 int val, err;
1380
1381 err = get_cur_ctl_value(cval, 0, &val);
1382 if (err < 0) {
84957a8a 1383 if (cval->mixer->ignore_ctl_error) {
1da177e4
LT
1384 ucontrol->value.enumerated.item[0] = 0;
1385 return 0;
1386 }
1387 return err;
1388 }
1389 val = get_relative_value(cval, val);
1390 ucontrol->value.enumerated.item[0] = val;
1391 return 0;
1392}
1393
1394/* put callback for selector unit */
86e07d34 1395static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1396{
86e07d34 1397 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1398 int val, oval, err;
1399
1400 err = get_cur_ctl_value(cval, 0, &oval);
1401 if (err < 0) {
84957a8a 1402 if (cval->mixer->ignore_ctl_error)
1da177e4
LT
1403 return 0;
1404 return err;
1405 }
1406 val = ucontrol->value.enumerated.item[0];
1407 val = get_abs_value(cval, val);
1408 if (val != oval) {
1409 set_cur_ctl_value(cval, 0, val);
1410 return 1;
1411 }
1412 return 0;
1413}
1414
1415/* alsa control interface for selector unit */
86e07d34 1416static struct snd_kcontrol_new mixer_selectunit_ctl = {
1da177e4
LT
1417 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1418 .name = "", /* will be filled later */
1419 .info = mixer_ctl_selector_info,
1420 .get = mixer_ctl_selector_get,
1421 .put = mixer_ctl_selector_put,
1422};
1423
1424
1425/* private free callback.
1426 * free both private_data and private_value
1427 */
86e07d34 1428static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1da177e4
LT
1429{
1430 int i, num_ins = 0;
1431
1432 if (kctl->private_data) {
86e07d34 1433 struct usb_mixer_elem_info *cval = kctl->private_data;
1da177e4
LT
1434 num_ins = cval->max;
1435 kfree(cval);
1436 kctl->private_data = NULL;
1437 }
1438 if (kctl->private_value) {
1439 char **itemlist = (char **)kctl->private_value;
1440 for (i = 0; i < num_ins; i++)
1441 kfree(itemlist[i]);
1442 kfree(itemlist);
1443 kctl->private_value = 0;
1444 }
1445}
1446
1447/*
1448 * parse a selector unit
1449 */
86e07d34 1450static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1da177e4
LT
1451{
1452 unsigned int num_ins = desc[4];
1453 unsigned int i, nameid, len;
1454 int err;
86e07d34
TI
1455 struct usb_mixer_elem_info *cval;
1456 struct snd_kcontrol *kctl;
1da177e4
LT
1457 char **namelist;
1458
1459 if (! num_ins || desc[0] < 6 + num_ins) {
1460 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1461 return -EINVAL;
1462 }
1463
1464 for (i = 0; i < num_ins; i++) {
1465 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1466 return err;
1467 }
1468
1469 if (num_ins == 1) /* only one ? nonsense! */
1470 return 0;
1471
1472 if (check_ignored_ctl(state, unitid, 0))
1473 return 0;
1474
561b220a 1475 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1da177e4
LT
1476 if (! cval) {
1477 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1478 return -ENOMEM;
1479 }
84957a8a 1480 cval->mixer = state->mixer;
1da177e4
LT
1481 cval->id = unitid;
1482 cval->val_type = USB_MIXER_U8;
1483 cval->channels = 1;
1484 cval->min = 1;
1485 cval->max = num_ins;
1486 cval->res = 1;
1487 cval->initialized = 1;
1488
1489 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1490 if (! namelist) {
1491 snd_printk(KERN_ERR "cannot malloc\n");
1492 kfree(cval);
1493 return -ENOMEM;
1494 }
1495#define MAX_ITEM_NAME_LEN 64
1496 for (i = 0; i < num_ins; i++) {
86e07d34 1497 struct usb_audio_term iterm;
1da177e4
LT
1498 len = 0;
1499 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1500 if (! namelist[i]) {
1501 snd_printk(KERN_ERR "cannot malloc\n");
1502 while (--i > 0)
1503 kfree(namelist[i]);
1504 kfree(namelist);
1505 kfree(cval);
1506 return -ENOMEM;
1507 }
8e062ec7
CL
1508 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1509 MAX_ITEM_NAME_LEN);
1510 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1da177e4
LT
1511 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1512 if (! len)
1513 sprintf(namelist[i], "Input %d", i);
1514 }
1515
1516 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1517 if (! kctl) {
1518 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
878b4789 1519 kfree(namelist);
1da177e4
LT
1520 kfree(cval);
1521 return -ENOMEM;
1522 }
1523 kctl->private_value = (unsigned long)namelist;
1524 kctl->private_free = usb_mixer_selector_elem_free;
1525
1526 nameid = desc[desc[0] - 1];
1527 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1528 if (len)
1529 ;
1530 else if (nameid)
1531 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1532 else {
1533 len = get_term_name(state, &state->oterm,
1534 kctl->id.name, sizeof(kctl->id.name), 0);
1535 if (! len)
1536 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1537
1538 if ((state->oterm.type & 0xff00) == 0x0100)
1539 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1540 else
1541 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1542 }
1543
1544 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1545 cval->id, kctl->id.name, num_ins);
84957a8a 1546 if ((err = add_control_to_empty(state, kctl)) < 0)
1da177e4
LT
1547 return err;
1548
1549 return 0;
1550}
1551
1552
1553/*
1554 * parse an audio unit recursively
1555 */
1556
86e07d34 1557static int parse_audio_unit(struct mixer_build *state, int unitid)
1da177e4
LT
1558{
1559 unsigned char *p1;
1560
1561 if (test_and_set_bit(unitid, state->unitbitmap))
1562 return 0; /* the unit already visited */
1563
1564 p1 = find_audio_control_unit(state, unitid);
1565 if (!p1) {
1566 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1567 return -EINVAL;
1568 }
1569
1570 switch (p1[2]) {
1571 case INPUT_TERMINAL:
1572 return 0; /* NOP */
1573 case MIXER_UNIT:
1574 return parse_audio_mixer_unit(state, unitid, p1);
1575 case SELECTOR_UNIT:
1576 return parse_audio_selector_unit(state, unitid, p1);
1577 case FEATURE_UNIT:
1578 return parse_audio_feature_unit(state, unitid, p1);
1579 case PROCESSING_UNIT:
1580 return parse_audio_processing_unit(state, unitid, p1);
1581 case EXTENSION_UNIT:
1582 return parse_audio_extension_unit(state, unitid, p1);
1583 default:
1584 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1585 return -EINVAL;
1586 }
1587}
1588
84957a8a
CL
1589static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1590{
6639b6c2
CL
1591 kfree(mixer->id_elems);
1592 if (mixer->urb) {
1593 kfree(mixer->urb->transfer_buffer);
1594 usb_free_urb(mixer->urb);
1595 }
b259b10c
CL
1596 if (mixer->rc_urb)
1597 usb_free_urb(mixer->rc_urb);
1598 kfree(mixer->rc_setup_packet);
84957a8a
CL
1599 kfree(mixer);
1600}
1601
86e07d34 1602static int snd_usb_mixer_dev_free(struct snd_device *device)
84957a8a
CL
1603{
1604 struct usb_mixer_interface *mixer = device->device_data;
1605 snd_usb_mixer_free(mixer);
1606 return 0;
1607}
1608
1da177e4
LT
1609/*
1610 * create mixer controls
1611 *
1612 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1613 */
84957a8a 1614static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1da177e4
LT
1615{
1616 unsigned char *desc;
86e07d34 1617 struct mixer_build state;
1da177e4
LT
1618 int err;
1619 const struct usbmix_ctl_map *map;
84957a8a 1620 struct usb_host_interface *hostif;
1da177e4 1621
84957a8a 1622 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1da177e4 1623 memset(&state, 0, sizeof(state));
84957a8a
CL
1624 state.chip = mixer->chip;
1625 state.mixer = mixer;
1da177e4
LT
1626 state.buffer = hostif->extra;
1627 state.buflen = hostif->extralen;
1da177e4
LT
1628
1629 /* check the mapping table */
27d10f56
CL
1630 for (map = usbmix_ctl_maps; map->id; map++) {
1631 if (map->id == state.chip->usb_id) {
1da177e4 1632 state.map = map->map;
8e062ec7 1633 state.selector_map = map->selector_map;
84957a8a 1634 mixer->ignore_ctl_error = map->ignore_ctl_error;
1da177e4
LT
1635 break;
1636 }
1637 }
1da177e4
LT
1638
1639 desc = NULL;
1640 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1641 if (desc[0] < 9)
1642 continue; /* invalid descriptor? */
1643 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1644 state.oterm.id = desc[3];
1645 state.oterm.type = combine_word(&desc[4]);
1646 state.oterm.name = desc[8];
1647 err = parse_audio_unit(&state, desc[7]);
1648 if (err < 0)
1649 return err;
1650 }
1651 return 0;
1652}
84957a8a 1653
6639b6c2
CL
1654static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1655 int unitid)
1656{
86e07d34 1657 struct usb_mixer_elem_info *info;
6639b6c2
CL
1658
1659 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1660 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1661 info->elem_id);
1662}
1663
b259b10c
CL
1664static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1665 int unitid)
1666{
4d1a70da 1667 if (!mixer->rc_cfg)
aafad562
CL
1668 return;
1669 /* unit ids specific to Extigy/Audigy 2 NX: */
1670 switch (unitid) {
1671 case 0: /* remote control */
b259b10c
CL
1672 mixer->rc_urb->dev = mixer->chip->dev;
1673 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
aafad562
CL
1674 break;
1675 case 4: /* digital in jack */
1676 case 7: /* line in jacks */
1677 case 19: /* speaker out jacks */
1678 case 20: /* headphones out jack */
1679 break;
1680 default:
1681 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1682 break;
b259b10c
CL
1683 }
1684}
1685
6639b6c2
CL
1686static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
1687{
1688 struct usb_mixer_interface *mixer = urb->context;
1689
1690 if (urb->status == 0) {
1691 u8 *buf = urb->transfer_buffer;
1692 int i;
1693
1694 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1695 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1696 buf[0], buf[1]);
1697 /* ignore any notifications not from the control interface */
1698 if ((buf[0] & 0x0f) != 0)
1699 continue;
1700 if (!(buf[0] & 0x40))
1701 snd_usb_mixer_notify_id(mixer, buf[1]);
b259b10c
CL
1702 else
1703 snd_usb_mixer_memory_change(mixer, buf[1]);
6639b6c2
CL
1704 }
1705 }
1706 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1707 urb->dev = mixer->chip->dev;
1708 usb_submit_urb(urb, GFP_ATOMIC);
1709 }
1710}
1711
1712/* create the handler for the optional status interrupt endpoint */
1713static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1714{
1715 struct usb_host_interface *hostif;
1716 struct usb_endpoint_descriptor *ep;
1717 void *transfer_buffer;
1718 int buffer_length;
1719 unsigned int epnum;
1720
1721 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1722 /* we need one interrupt input endpoint */
1723 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1724 return 0;
1725 ep = get_endpoint(hostif, 0);
1726 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1727 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1728 return 0;
1729
1730 epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1731 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1732 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1733 if (!transfer_buffer)
1734 return -ENOMEM;
1735 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1736 if (!mixer->urb) {
1737 kfree(transfer_buffer);
1738 return -ENOMEM;
1739 }
1740 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1741 usb_rcvintpipe(mixer->chip->dev, epnum),
1742 transfer_buffer, buffer_length,
1743 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1744 usb_submit_urb(mixer->urb, GFP_KERNEL);
1745 return 0;
1746}
1747
b259b10c
CL
1748static void snd_usb_soundblaster_remote_complete(struct urb *urb,
1749 struct pt_regs *regs)
1750{
1751 struct usb_mixer_interface *mixer = urb->context;
4d1a70da 1752 const struct rc_config *rc = mixer->rc_cfg;
b259b10c
CL
1753 u32 code;
1754
4d1a70da 1755 if (urb->status < 0 || urb->actual_length < rc->packet_length)
b259b10c 1756 return;
4d1a70da
RC
1757
1758 code = mixer->rc_buffer[rc->offset];
1759 if (rc->length == 2)
1760 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1761
b259b10c 1762 /* the Mute button actually changes the mixer control */
4d1a70da
RC
1763 if (code == rc->mute_code)
1764 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
b259b10c
CL
1765 mixer->rc_code = code;
1766 wmb();
1767 wake_up(&mixer->rc_waitq);
1768}
1769
86e07d34 1770static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
b259b10c
CL
1771{
1772 struct usb_mixer_interface *mixer = hw->private_data;
1773
1774 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1775 return -EBUSY;
1776 return 0;
1777}
1778
86e07d34 1779static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
b259b10c
CL
1780{
1781 struct usb_mixer_interface *mixer = hw->private_data;
1782
1783 clear_bit(0, &mixer->rc_hwdep_open);
1784 smp_mb__after_clear_bit();
1785 return 0;
1786}
1787
86e07d34 1788static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
b259b10c
CL
1789 long count, loff_t *offset)
1790{
1791 struct usb_mixer_interface *mixer = hw->private_data;
1792 int err;
1793 u32 rc_code;
1794
434b7f56 1795 if (count != 1 && count != 4)
b259b10c
CL
1796 return -EINVAL;
1797 err = wait_event_interruptible(mixer->rc_waitq,
1798 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1799 if (err == 0) {
434b7f56
CL
1800 if (count == 1)
1801 err = put_user(rc_code, buf);
1802 else
1803 err = put_user(rc_code, (u32 __user *)buf);
b259b10c
CL
1804 }
1805 return err < 0 ? err : count;
1806}
1807
86e07d34 1808static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
b259b10c
CL
1809 poll_table *wait)
1810{
1811 struct usb_mixer_interface *mixer = hw->private_data;
1812
1813 poll_wait(file, &mixer->rc_waitq, wait);
1814 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1815}
1816
1817static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1818{
86e07d34 1819 struct snd_hwdep *hwdep;
4d1a70da 1820 int err, len, i;
b259b10c 1821
4d1a70da
RC
1822 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1823 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1824 break;
1825 if (i >= ARRAY_SIZE(rc_configs))
b259b10c 1826 return 0;
4d1a70da 1827 mixer->rc_cfg = &rc_configs[i];
b259b10c 1828
4d1a70da
RC
1829 len = mixer->rc_cfg->packet_length;
1830
b259b10c
CL
1831 init_waitqueue_head(&mixer->rc_waitq);
1832 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1833 if (err < 0)
1834 return err;
1835 snprintf(hwdep->name, sizeof(hwdep->name),
1836 "%s remote control", mixer->chip->card->shortname);
1837 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1838 hwdep->private_data = mixer;
1839 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1840 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1841 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1842 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1843
1844 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1845 if (!mixer->rc_urb)
1846 return -ENOMEM;
1847 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1848 if (!mixer->rc_setup_packet) {
1849 usb_free_urb(mixer->rc_urb);
1850 mixer->rc_urb = NULL;
1851 return -ENOMEM;
1852 }
1853 mixer->rc_setup_packet->bRequestType =
1854 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1855 mixer->rc_setup_packet->bRequest = GET_MEM;
1856 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1857 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1858 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1859 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1860 usb_rcvctrlpipe(mixer->chip->dev, 0),
1861 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1862 snd_usb_soundblaster_remote_complete, mixer);
1863 return 0;
1864}
1865
86e07d34 1866static int snd_audigy2nx_led_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
93446edc
CL
1867{
1868 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1869 uinfo->count = 1;
1870 uinfo->value.integer.min = 0;
1871 uinfo->value.integer.max = 1;
1872 return 0;
1873}
1874
86e07d34 1875static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
93446edc
CL
1876{
1877 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1878 int index = kcontrol->private_value;
1879
1880 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1881 return 0;
1882}
1883
86e07d34 1884static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
93446edc
CL
1885{
1886 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1887 int index = kcontrol->private_value;
1888 int value = ucontrol->value.integer.value[0];
1889 int err, changed;
1890
1891 if (value > 1)
1892 return -EINVAL;
1893 changed = value != mixer->audigy2nx_leds[index];
1894 err = snd_usb_ctl_msg(mixer->chip->dev,
1895 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1896 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1897 value, index + 2, NULL, 0, 100);
1898 if (err < 0)
1899 return err;
1900 mixer->audigy2nx_leds[index] = value;
1901 return changed;
1902}
1903
86e07d34 1904static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
93446edc
CL
1905 {
1906 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1907 .name = "CMSS LED Switch",
1908 .info = snd_audigy2nx_led_info,
1909 .get = snd_audigy2nx_led_get,
1910 .put = snd_audigy2nx_led_put,
1911 .private_value = 0,
1912 },
1913 {
1914 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1915 .name = "Power LED Switch",
1916 .info = snd_audigy2nx_led_info,
1917 .get = snd_audigy2nx_led_get,
1918 .put = snd_audigy2nx_led_put,
1919 .private_value = 1,
1920 },
1921 {
1922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1923 .name = "Dolby Digital LED Switch",
1924 .info = snd_audigy2nx_led_info,
1925 .get = snd_audigy2nx_led_get,
1926 .put = snd_audigy2nx_led_put,
1927 .private_value = 2,
1928 },
1929};
1930
1931static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1932{
1933 int i, err;
1934
1935 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1936 err = snd_ctl_add(mixer->chip->card,
1937 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1938 if (err < 0)
1939 return err;
1940 }
1941 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1942 return 0;
1943}
1944
86e07d34
TI
1945static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1946 struct snd_info_buffer *buffer)
aafad562
CL
1947{
1948 static const struct {
1949 int unitid;
1950 const char *name;
1951 } jacks[] = {
1952 {4, "dig in "},
1953 {7, "line in"},
1954 {19, "spk out"},
1955 {20, "hph out"},
1956 };
1957 struct usb_mixer_interface *mixer = entry->private_data;
1958 int i, err;
1959 u8 buf[3];
1960
1961 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1962 for (i = 0; i < ARRAY_SIZE(jacks); ++i) {
1963 snd_iprintf(buffer, "%s: ", jacks[i].name);
1964 err = snd_usb_ctl_msg(mixer->chip->dev,
1965 usb_rcvctrlpipe(mixer->chip->dev, 0),
1966 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
1967 USB_RECIP_INTERFACE, 0,
1968 jacks[i].unitid << 8, buf, 3, 100);
1969 if (err == 3 && buf[0] == 3)
1970 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
1971 else
1972 snd_iprintf(buffer, "?\n");
1973 }
1974}
1975
86e07d34 1976int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
84957a8a 1977{
86e07d34 1978 static struct snd_device_ops dev_ops = {
84957a8a
CL
1979 .dev_free = snd_usb_mixer_dev_free
1980 };
1981 struct usb_mixer_interface *mixer;
1982 int err;
1983
1984 strcpy(chip->card->mixername, "USB Mixer");
1985
561b220a 1986 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
84957a8a
CL
1987 if (!mixer)
1988 return -ENOMEM;
1989 mixer->chip = chip;
1990 mixer->ctrlif = ctrlif;
1991#ifdef IGNORE_CTL_ERROR
1992 mixer->ignore_ctl_error = 1;
1993#endif
6639b6c2
CL
1994 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
1995 if (!mixer->id_elems) {
1996 kfree(mixer);
1997 return -ENOMEM;
1998 }
84957a8a 1999
6639b6c2 2000 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
93446edc
CL
2001 (err = snd_usb_mixer_status_create(mixer)) < 0)
2002 goto _error;
84957a8a 2003
93446edc
CL
2004 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2005 goto _error;
2006
2007 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020)) {
86e07d34 2008 struct snd_info_entry *entry;
aafad562 2009
93446edc
CL
2010 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2011 goto _error;
aafad562 2012 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
bf850204 2013 snd_info_set_text_ops(entry, mixer,
aafad562 2014 snd_audigy2nx_proc_read);
b259b10c
CL
2015 }
2016
84957a8a 2017 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
93446edc
CL
2018 if (err < 0)
2019 goto _error;
84957a8a
CL
2020 list_add(&mixer->list, &chip->mixer_list);
2021 return 0;
93446edc
CL
2022
2023_error:
2024 snd_usb_mixer_free(mixer);
2025 return err;
84957a8a
CL
2026}
2027
2028void snd_usb_mixer_disconnect(struct list_head *p)
2029{
6639b6c2
CL
2030 struct usb_mixer_interface *mixer;
2031
2032 mixer = list_entry(p, struct usb_mixer_interface, list);
2033 if (mixer->urb)
2034 usb_kill_urb(mixer->urb);
b259b10c
CL
2035 if (mixer->rc_urb)
2036 usb_kill_urb(mixer->rc_urb);
84957a8a 2037}
This page took 0.432192 seconds and 5 git commands to generate.