blob: ab8f0f0b65bea70c3f537a813b2abe3514783355 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/bitops.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/slab.h>
33#include <linux/string.h>
34#include <linux/usb.h>
Daniel Mack28e1b772010-02-22 23:49:09 +010035#include <linux/usb/audio.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/core.h>
38#include <sound/control.h>
Clemens Ladischb259b102005-04-29 16:29:28 +020039#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020040#include <sound/info.h>
Takashi Iwai7bc5ba72006-07-14 15:18:19 +020041#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "usbaudio.h"
Daniel Mack3e1aebe2010-03-04 19:46:12 +010044#include "usbmixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010045#include "helper.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 */
49
50/* ignore error from controls - for debugging */
51/* #define IGNORE_CTL_ERROR */
52
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020053/*
54 * Sound Blaster remote control configuration
55 *
56 * format of remote control data:
57 * Extigy: xx 00
58 * Audigy 2 NX: 06 80 xx 00 00 00
59 * Live! 24-bit: 06 80 xx yy 22 83
60 */
61static const struct rc_config {
62 u32 usb_id;
63 u8 offset;
64 u8 length;
65 u8 packet_length;
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020066 u8 min_packet_length; /* minimum accepted length of the URB result */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020067 u8 mute_mixer_id;
68 u32 mute_code;
69} rc_configs[] = {
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020070 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
71 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
72 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
Andrea Borgia31959542009-01-07 22:58:50 +010073 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020074};
75
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010076#define MAX_ID_ELEMS 256
77
Clemens Ladisch84957a82005-04-29 16:23:13 +020078struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010079 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020080 unsigned int ctrlif;
81 struct list_head list;
82 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020083 struct urb *urb;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +010084 /* array[MAX_ID_ELEMS], indexed by unit id */
85 struct usb_mixer_elem_info **id_elems;
Clemens Ladischb259b102005-04-29 16:29:28 +020086
87 /* Sound Blaster remote control stuff */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020088 const struct rc_config *rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +020089 u32 rc_code;
90 wait_queue_head_t rc_waitq;
91 struct urb *rc_urb;
92 struct usb_ctrlrequest *rc_setup_packet;
93 u8 rc_buffer[6];
Clemens Ladisch93446ed2005-05-03 08:02:40 +020094
95 u8 audigy2nx_leds[3];
Clemens Ladisch468b8fd2009-07-13 11:39:29 +020096 u8 xonar_u1_status;
Clemens Ladisch84957a82005-04-29 16:23:13 +020097};
98
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100struct usb_audio_term {
101 int id;
102 int type;
103 int channels;
104 unsigned int chconfig;
105 int name;
106};
107
108struct usbmix_name_map;
109
Takashi Iwai86e07d32005-11-17 15:08:02 +0100110struct mixer_build {
111 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200112 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned char *buffer;
114 unsigned int buflen;
Jaroslav Kysela291186e2010-02-16 11:55:18 +0100115 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100116 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200118 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Takashi Iwai641b4872009-01-15 17:05:24 +0100121#define MAX_CHANNELS 10 /* max logical channels */
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200124 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100125 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
126 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned int id;
128 unsigned int control; /* CS or ICN (high byte) */
129 unsigned int cmask; /* channel mask bitmap: 0 = master */
130 int channels;
131 int val_type;
132 int min, max, res;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100133 int dBmin, dBmax;
Takashi Iwai641b4872009-01-15 17:05:24 +0100134 int cached;
135 int cache_val[MAX_CHANNELS];
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200136 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139enum {
140 USB_MIXER_BOOLEAN,
141 USB_MIXER_INV_BOOLEAN,
142 USB_MIXER_S8,
143 USB_MIXER_U8,
144 USB_MIXER_S16,
145 USB_MIXER_U16,
146};
147
148enum {
149 USB_PROC_UPDOWN = 1,
150 USB_PROC_UPDOWN_SWITCH = 1,
151 USB_PROC_UPDOWN_MODE_SEL = 2,
152
153 USB_PROC_PROLOGIC = 2,
154 USB_PROC_PROLOGIC_SWITCH = 1,
155 USB_PROC_PROLOGIC_MODE_SEL = 2,
156
157 USB_PROC_3DENH = 3,
158 USB_PROC_3DENH_SWITCH = 1,
159 USB_PROC_3DENH_SPACE = 2,
160
161 USB_PROC_REVERB = 4,
162 USB_PROC_REVERB_SWITCH = 1,
163 USB_PROC_REVERB_LEVEL = 2,
164 USB_PROC_REVERB_TIME = 3,
165 USB_PROC_REVERB_DELAY = 4,
166
167 USB_PROC_CHORUS = 5,
168 USB_PROC_CHORUS_SWITCH = 1,
169 USB_PROC_CHORUS_LEVEL = 2,
170 USB_PROC_CHORUS_RATE = 3,
171 USB_PROC_CHORUS_DEPTH = 4,
172
173 USB_PROC_DCR = 6,
174 USB_PROC_DCR_SWITCH = 1,
175 USB_PROC_DCR_RATIO = 2,
176 USB_PROC_DCR_MAX_AMP = 3,
177 USB_PROC_DCR_THRESHOLD = 4,
178 USB_PROC_DCR_ATTACK = 5,
179 USB_PROC_DCR_RELEASE = 6,
180};
181
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -0800182/*E-mu 0202(0404) eXtension Unit(XU) control*/
183enum {
184 USB_XU_CLOCK_RATE = 0xe301,
185 USB_XU_CLOCK_SOURCE = 0xe302,
186 USB_XU_DIGITAL_IO_STATUS = 0xe303,
187 USB_XU_DEVICE_OPTIONS = 0xe304,
188 USB_XU_DIRECT_MONITORING = 0xe305,
189 USB_XU_METERING = 0xe306
190};
191enum {
192 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
193 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
194 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
195 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
196};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/*
199 * manual mapping of mixer names
200 * if the mixer topology is too complicated and the parsed names are
201 * ambiguous, add the entries in usbmixer_maps.c.
202 */
203#include "usbmixer_maps.c"
204
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100205static const struct usbmix_name_map *
206find_map(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100208 const struct usbmix_name_map *p = state->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100210 if (!p)
211 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 for (p = state->map; p->id; p++) {
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100214 if (p->id == unitid &&
215 (!control || !p->control || control == p->control))
216 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100218 return NULL;
219}
220
221/* get the mapped name if the unit matches */
222static int
223check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
224{
225 if (!p || !p->name)
226 return 0;
227
228 buflen--;
229 return strlcpy(buf, p->name, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/* check whether the control should be ignored */
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100233static inline int
234check_ignored_ctl(const struct usbmix_name_map *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100236 if (!p || p->name || p->dB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100238 return 1;
239}
240
241/* dB mapping */
242static inline void check_mapped_dB(const struct usbmix_name_map *p,
243 struct usb_mixer_elem_info *cval)
244{
245 if (p && p->dB) {
246 cval->dBmin = p->dB->min;
247 cval->dBmax = p->dB->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200251/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100252static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200253 int index, char *buf, int buflen)
254{
255 const struct usbmix_selector_map *p;
256
257 if (! state->selector_map)
258 return 0;
259 for (p = state->selector_map; p->id; p++) {
260 if (p->id == unitid && index < p->count)
261 return strlcpy(buf, p->names[index], buflen);
262 }
263 return 0;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
267 * find an audio control unit with the given unit id
268 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100269static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 unsigned char *p;
272
273 p = NULL;
274 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
275 USB_DT_CS_INTERFACE)) != NULL) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100276 if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC_EXTENSION_UNIT_V1 && p[3] == unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return p;
278 }
279 return NULL;
280}
281
282
283/*
284 * copy a string with the given id
285 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100286static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
289 buf[len] = 0;
290 return len;
291}
292
293/*
294 * convert from the byte/word on usb descriptor to the zero-based integer
295 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100296static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 switch (cval->val_type) {
299 case USB_MIXER_BOOLEAN:
300 return !!val;
301 case USB_MIXER_INV_BOOLEAN:
302 return !val;
303 case USB_MIXER_U8:
304 val &= 0xff;
305 break;
306 case USB_MIXER_S8:
307 val &= 0xff;
308 if (val >= 0x80)
309 val -= 0x100;
310 break;
311 case USB_MIXER_U16:
312 val &= 0xffff;
313 break;
314 case USB_MIXER_S16:
315 val &= 0xffff;
316 if (val >= 0x8000)
317 val -= 0x10000;
318 break;
319 }
320 return val;
321}
322
323/*
324 * convert from the zero-based int to the byte/word for usb descriptor
325 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100326static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 switch (cval->val_type) {
329 case USB_MIXER_BOOLEAN:
330 return !!val;
331 case USB_MIXER_INV_BOOLEAN:
332 return !val;
333 case USB_MIXER_S8:
334 case USB_MIXER_U8:
335 return val & 0xff;
336 case USB_MIXER_S16:
337 case USB_MIXER_U16:
338 return val & 0xffff;
339 }
340 return 0; /* not reached */
341}
342
Takashi Iwai86e07d32005-11-17 15:08:02 +0100343static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 if (! cval->res)
346 cval->res = 1;
347 if (val < cval->min)
348 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200349 else if (val >= cval->max)
350 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 else
352 return (val - cval->min) / cval->res;
353}
354
Takashi Iwai86e07d32005-11-17 15:08:02 +0100355static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 if (val < 0)
358 return cval->min;
359 if (! cval->res)
360 cval->res = 1;
361 val *= cval->res;
362 val += cval->min;
363 if (val > cval->max)
364 return cval->max;
365 return val;
366}
367
368
369/*
370 * retrieve a mixer value
371 */
372
Takashi Iwai86e07d32005-11-17 15:08:02 +0100373static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 unsigned char buf[2];
376 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
377 int timeout = 10;
378
379 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200380 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
381 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 request,
383 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200384 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200385 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
387 return 0;
388 }
389 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200390 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
391 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
393}
394
Takashi Iwai86e07d32005-11-17 15:08:02 +0100395static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100397 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100401static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
402 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100404 return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Takashi Iwai641b4872009-01-15 17:05:24 +0100407static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
408 int channel, int index, int *value)
409{
410 int err;
411
412 if (cval->cached & (1 << channel)) {
413 *value = cval->cache_val[index];
414 return 0;
415 }
416 err = get_cur_mix_raw(cval, channel, value);
417 if (err < 0) {
418 if (!cval->mixer->ignore_ctl_error)
419 snd_printd(KERN_ERR "cannot get current value for "
420 "control %d ch %d: err = %d\n",
421 cval->control, channel, err);
422 return err;
423 }
424 cval->cached |= 1 << channel;
425 cval->cache_val[index] = *value;
426 return 0;
427}
428
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * set a mixer value
432 */
433
Takashi Iwai86e07d32005-11-17 15:08:02 +0100434static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned char buf[2];
437 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
438 int timeout = 10;
439
440 value_set = convert_bytes_value(cval, value_set);
441 buf[0] = value_set & 0xff;
442 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530443 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200444 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
445 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 request,
447 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200448 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 buf, val_len, 100) >= 0)
450 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200451 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
452 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
454}
455
Takashi Iwai86e07d32005-11-17 15:08:02 +0100456static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Daniel Mackde48c7b2010-02-22 23:49:13 +0100458 return set_ctl_value(cval, UAC_SET_CUR, validx, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Takashi Iwai641b4872009-01-15 17:05:24 +0100461static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
462 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai641b4872009-01-15 17:05:24 +0100464 int err;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100465 err = set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
Takashi Iwai641b4872009-01-15 17:05:24 +0100466 value);
467 if (err < 0)
468 return err;
469 cval->cached |= 1 << channel;
470 cval->cache_val[index] = value;
471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200474/*
475 * TLV callback for mixer volume controls
476 */
477static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
478 unsigned int size, unsigned int __user *_tlv)
479{
480 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200481 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200482
483 if (size < sizeof(scale))
484 return -ENOMEM;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100485 scale[2] = cval->dBmin;
486 scale[3] = cval->dBmax;
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200487 if (copy_to_user(_tlv, scale, sizeof(scale)))
488 return -EFAULT;
489 return 0;
490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492/*
493 * parser routines begin here...
494 */
495
Takashi Iwai86e07d32005-11-17 15:08:02 +0100496static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498
499/*
500 * check if the input/output channel routing is enabled on the given bitmap.
501 * used for mixer unit parser
502 */
503static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
504{
505 int idx = ich * num_outs + och;
506 return bmap[idx >> 3] & (0x80 >> (idx & 7));
507}
508
509
510/*
511 * add an alsa control element
512 * search and increment the index until an empty slot is found.
513 *
514 * if failed, give up and free the control instance.
515 */
516
Takashi Iwai86e07d32005-11-17 15:08:02 +0100517static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100519 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200521
522 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200524 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200526 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200528 cval->elem_id = &kctl->id;
529 cval->next_id_elem = state->mixer->id_elems[cval->id];
530 state->mixer->id_elems[cval->id] = cval;
531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534
535/*
536 * get a terminal name string
537 */
538
539static struct iterm_name_combo {
540 int type;
541 char *name;
542} iterm_names[] = {
543 { 0x0300, "Output" },
544 { 0x0301, "Speaker" },
545 { 0x0302, "Headphone" },
546 { 0x0303, "HMD Audio" },
547 { 0x0304, "Desktop Speaker" },
548 { 0x0305, "Room Speaker" },
549 { 0x0306, "Com Speaker" },
550 { 0x0307, "LFE" },
551 { 0x0600, "External In" },
552 { 0x0601, "Analog In" },
553 { 0x0602, "Digital In" },
554 { 0x0603, "Line" },
555 { 0x0604, "Legacy In" },
556 { 0x0605, "IEC958 In" },
557 { 0x0606, "1394 DA Stream" },
558 { 0x0607, "1394 DV Stream" },
559 { 0x0700, "Embedded" },
560 { 0x0701, "Noise Source" },
561 { 0x0702, "Equalization Noise" },
562 { 0x0703, "CD" },
563 { 0x0704, "DAT" },
564 { 0x0705, "DCC" },
565 { 0x0706, "MiniDisk" },
566 { 0x0707, "Analog Tape" },
567 { 0x0708, "Phonograph" },
568 { 0x0709, "VCR Audio" },
569 { 0x070a, "Video Disk Audio" },
570 { 0x070b, "DVD Audio" },
571 { 0x070c, "TV Tuner Audio" },
572 { 0x070d, "Satellite Rec Audio" },
573 { 0x070e, "Cable Tuner Audio" },
574 { 0x070f, "DSS Audio" },
575 { 0x0710, "Radio Receiver" },
576 { 0x0711, "Radio Transmitter" },
577 { 0x0712, "Multi-Track Recorder" },
578 { 0x0713, "Synthesizer" },
579 { 0 },
580};
581
Takashi Iwai86e07d32005-11-17 15:08:02 +0100582static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned char *name, int maxlen, int term_only)
584{
585 struct iterm_name_combo *names;
586
587 if (iterm->name)
588 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
589
590 /* virtual type - not a real terminal */
591 if (iterm->type >> 16) {
592 if (term_only)
593 return 0;
594 switch (iterm->type >> 16) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100595 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 strcpy(name, "Selector"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100597 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 strcpy(name, "Process Unit"); return 12;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100599 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 strcpy(name, "Ext Unit"); return 8;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100601 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 strcpy(name, "Mixer"); return 5;
603 default:
604 return sprintf(name, "Unit %d", iterm->id);
605 }
606 }
607
608 switch (iterm->type & 0xff00) {
609 case 0x0100:
610 strcpy(name, "PCM"); return 3;
611 case 0x0200:
612 strcpy(name, "Mic"); return 3;
613 case 0x0400:
614 strcpy(name, "Headset"); return 7;
615 case 0x0500:
616 strcpy(name, "Phone"); return 5;
617 }
618
619 for (names = iterm_names; names->type; names++)
620 if (names->type == iterm->type) {
621 strcpy(name, names->name);
622 return strlen(names->name);
623 }
624 return 0;
625}
626
627
628/*
629 * parse the source unit recursively until it reaches to a terminal
630 * or a branched unit.
631 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100632static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 unsigned char *p1;
635
636 memset(term, 0, sizeof(*term));
637 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
638 term->id = id;
639 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100640 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 term->type = combine_word(p1 + 4);
642 term->channels = p1[7];
643 term->chconfig = combine_word(p1 + 8);
644 term->name = p1[11];
645 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100646 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 id = p1[4];
648 break; /* continue to parse */
Daniel Mackde48c7b2010-02-22 23:49:13 +0100649 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 term->type = p1[2] << 16; /* virtual type */
651 term->channels = p1[5 + p1[4]];
652 term->chconfig = combine_word(p1 + 6 + p1[4]);
653 term->name = p1[p1[0] - 1];
654 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100655 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* call recursively to retrieve the channel info */
657 if (check_input_term(state, p1[5], term) < 0)
658 return -ENODEV;
659 term->type = p1[2] << 16; /* virtual type */
660 term->id = id;
661 term->name = p1[9 + p1[0] - 1];
662 return 0;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100663 case UAC_PROCESSING_UNIT_V1:
664 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (p1[6] == 1) {
666 id = p1[7];
667 break; /* continue to parse */
668 }
669 term->type = p1[2] << 16; /* virtual type */
670 term->channels = p1[7 + p1[6]];
671 term->chconfig = combine_word(p1 + 8 + p1[6]);
672 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
673 return 0;
674 default:
675 return -ENODEV;
676 }
677 }
678 return -ENODEV;
679}
680
681
682/*
683 * Feature Unit
684 */
685
686/* feature unit control information */
687struct usb_feature_control_info {
688 const char *name;
689 unsigned int type; /* control type (mute, volume, etc.) */
690};
691
692static struct usb_feature_control_info audio_feature_info[] = {
693 { "Mute", USB_MIXER_INV_BOOLEAN },
694 { "Volume", USB_MIXER_S16 },
695 { "Tone Control - Bass", USB_MIXER_S8 },
696 { "Tone Control - Mid", USB_MIXER_S8 },
697 { "Tone Control - Treble", USB_MIXER_S8 },
698 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
699 { "Auto Gain Control", USB_MIXER_BOOLEAN },
700 { "Delay Control", USB_MIXER_U16 },
701 { "Bass Boost", USB_MIXER_BOOLEAN },
702 { "Loudness", USB_MIXER_BOOLEAN },
703};
704
705
706/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100707static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Jesper Juhl4d572772005-05-30 17:30:32 +0200709 kfree(kctl->private_data);
710 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713
714/*
715 * interface to ALSA control for feature/mixer units
716 */
717
718/*
719 * retrieve the minimum and maximum values for the specified control
720 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100721static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 /* for failsafe */
724 cval->min = default_min;
725 cval->max = cval->min + 1;
726 cval->res = 1;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100727 cval->dBmin = cval->dBmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (cval->val_type == USB_MIXER_BOOLEAN ||
730 cval->val_type == USB_MIXER_INV_BOOLEAN) {
731 cval->initialized = 1;
732 } else {
733 int minchn = 0;
734 if (cval->cmask) {
735 int i;
736 for (i = 0; i < MAX_CHANNELS; i++)
737 if (cval->cmask & (1 << i)) {
738 minchn = i + 1;
739 break;
740 }
741 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100742 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
743 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200744 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
745 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EINVAL;
747 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100748 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 cval->res = 1;
750 } else {
751 int last_valid_res = cval->res;
752
753 while (cval->res > 1) {
Daniel Mackde48c7b2010-02-22 23:49:13 +0100754 if (set_ctl_value(cval, UAC_SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 cval->res /= 2;
757 }
Daniel Mackde48c7b2010-02-22 23:49:13 +0100758 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 cval->res = last_valid_res;
760 }
761 if (cval->res == 0)
762 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200763
764 /* Additional checks for the proper resolution
765 *
766 * Some devices report smaller resolutions than actually
767 * reacting. They don't return errors but simply clip
768 * to the lower aligned value.
769 */
770 if (cval->min + cval->res < cval->max) {
771 int last_valid_res = cval->res;
772 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100773 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200774 for (;;) {
775 test = saved;
776 if (test < cval->max)
777 test += cval->res;
778 else
779 test -= cval->res;
780 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100781 set_cur_mix_value(cval, minchn, 0, test) ||
782 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200783 cval->res = last_valid_res;
784 break;
785 }
786 if (test == check)
787 break;
788 cval->res *= 2;
789 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100790 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 cval->initialized = 1;
794 }
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100795
796 /* USB descriptions contain the dB scale in 1/256 dB unit
797 * while ALSA TLV contains in 1/100 dB unit
798 */
799 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
800 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
801 if (cval->dBmin > cval->dBmax) {
802 /* something is wrong; assume it's either from/to 0dB */
803 if (cval->dBmin < 0)
804 cval->dBmax = 0;
805 else if (cval->dBmin > 0)
806 cval->dBmin = 0;
807 if (cval->dBmin > cval->dBmax) {
808 /* totally crap, return an error */
809 return -EINVAL;
810 }
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0;
814}
815
816
817/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100818static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100820 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (cval->val_type == USB_MIXER_BOOLEAN ||
823 cval->val_type == USB_MIXER_INV_BOOLEAN)
824 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
825 else
826 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
827 uinfo->count = cval->channels;
828 if (cval->val_type == USB_MIXER_BOOLEAN ||
829 cval->val_type == USB_MIXER_INV_BOOLEAN) {
830 uinfo->value.integer.min = 0;
831 uinfo->value.integer.max = 1;
832 } else {
833 if (! cval->initialized)
834 get_min_max(cval, 0);
835 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200836 uinfo->value.integer.max =
837 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 return 0;
840}
841
842/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100843static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100845 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int c, cnt, val, err;
847
Takashi Iwai641b4872009-01-15 17:05:24 +0100848 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (cval->cmask) {
850 cnt = 0;
851 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100852 if (!(cval->cmask & (1 << c)))
853 continue;
854 err = get_cur_mix_value(cval, c + 1, cnt, &val);
855 if (err < 0)
856 return cval->mixer->ignore_ctl_error ? 0 : err;
857 val = get_relative_value(cval, val);
858 ucontrol->value.integer.value[cnt] = val;
859 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 } else {
863 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100864 err = get_cur_mix_value(cval, 0, 0, &val);
865 if (err < 0)
866 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 val = get_relative_value(cval, val);
868 ucontrol->value.integer.value[0] = val;
869 }
870 return 0;
871}
872
873/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100874static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100876 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int c, cnt, val, oval, err;
878 int changed = 0;
879
880 if (cval->cmask) {
881 cnt = 0;
882 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100883 if (!(cval->cmask & (1 << c)))
884 continue;
885 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
886 if (err < 0)
887 return cval->mixer->ignore_ctl_error ? 0 : err;
888 val = ucontrol->value.integer.value[cnt];
889 val = get_abs_value(cval, val);
890 if (oval != val) {
891 set_cur_mix_value(cval, c + 1, cnt, val);
892 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100894 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 } else {
897 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100898 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100900 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 val = ucontrol->value.integer.value[0];
902 val = get_abs_value(cval, val);
903 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100904 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 changed = 1;
906 }
907 }
908 return changed;
909}
910
Takashi Iwai86e07d32005-11-17 15:08:02 +0100911static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
913 .name = "", /* will be filled later manually */
914 .info = mixer_ctl_feature_info,
915 .get = mixer_ctl_feature_get,
916 .put = mixer_ctl_feature_put,
917};
918
919
920/*
921 * build a feature control
922 */
923
Takashi Iwai08d1e632009-10-02 14:06:08 +0200924static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
925{
926 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
927}
928
Takashi Iwai86e07d32005-11-17 15:08:02 +0100929static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100931 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 unsigned int len = 0;
934 int mapped_name = 0;
935 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100936 struct snd_kcontrol *kctl;
937 struct usb_mixer_elem_info *cval;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100938 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 control++; /* change from zero-based to 1-based value */
941
Daniel Mack45d76052010-03-11 21:13:21 +0100942 if (control == UAC_GRAPHIC_EQUALIZER_CONTROL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* FIXME: not supported yet */
944 return;
945 }
946
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100947 map = find_map(state, unitid, control);
948 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return;
950
Takashi Iwai561b2202005-09-09 14:22:34 +0200951 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (! cval) {
953 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
954 return;
955 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200956 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 cval->id = unitid;
958 cval->control = control;
959 cval->cmask = ctl_mask;
960 cval->val_type = audio_feature_info[control-1].type;
961 if (ctl_mask == 0)
962 cval->channels = 1; /* master channel */
963 else {
964 int i, c = 0;
965 for (i = 0; i < 16; i++)
966 if (ctl_mask & (1 << i))
967 c++;
968 cval->channels = c;
969 }
970
971 /* get min/max values */
972 get_min_max(cval, 0);
973
974 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
975 if (! kctl) {
976 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
977 kfree(cval);
978 return;
979 }
980 kctl->private_free = usb_mixer_elem_free;
981
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100982 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 mapped_name = len != 0;
984 if (! len && nameid)
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +0100985 len = snd_usb_copy_string_desc(state, nameid,
986 kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 switch (control) {
Daniel Mack45d76052010-03-11 21:13:21 +0100989 case UAC_MUTE_CONTROL:
990 case UAC_VOLUME_CONTROL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* determine the control name. the rule is:
992 * - if a name id is given in descriptor, use it.
993 * - if the connected input can be determined, then use the name
994 * of terminal type.
995 * - if the connected output can be determined, use it.
996 * - otherwise, anonymous name.
997 */
998 if (! len) {
999 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1000 if (! len)
1001 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1002 if (! len)
1003 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1004 "Feature %d", unitid);
1005 }
1006 /* determine the stream direction:
1007 * if the connected output is USB stream, then it's likely a
1008 * capture stream. otherwise it should be playback (hopefully :)
1009 */
1010 if (! mapped_name && ! (state->oterm.type >> 16)) {
1011 if ((state->oterm.type & 0xff00) == 0x0100) {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001012 len = append_ctl_name(kctl, " Capture");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 } else {
Takashi Iwai08d1e632009-10-02 14:06:08 +02001014 len = append_ctl_name(kctl, " Playback");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 }
Daniel Mack45d76052010-03-11 21:13:21 +01001017 append_ctl_name(kctl, control == UAC_MUTE_CONTROL ?
Takashi Iwai08d1e632009-10-02 14:06:08 +02001018 " Switch" : " Volume");
Daniel Mack45d76052010-03-11 21:13:21 +01001019 if (control == UAC_VOLUME_CONTROL) {
Takashi Iwai7bc5ba72006-07-14 15:18:19 +02001020 kctl->tlv.c = mixer_vol_tlv;
1021 kctl->vd[0].access |=
1022 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1023 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001024 check_mapped_dB(map, cval);
Takashi Iwai7bc5ba72006-07-14 15:18:19 +02001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
1027
1028 default:
1029 if (! len)
1030 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1031 sizeof(kctl->id.name));
1032 break;
1033 }
1034
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001035 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001036 switch (state->chip->usb_id) {
1037 case USB_ID(0x0471, 0x0101):
1038 case USB_ID(0x0471, 0x0104):
1039 case USB_ID(0x0471, 0x0105):
1040 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001041 /* quirk for UDA1321/N101.
1042 * note that detection between firmware 2.1.1.7 (N101)
1043 * and later 2.1.1.21 is not very clear from datasheets.
1044 * I hope that the min value is -15360 for newer firmware --jk
1045 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001046 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1047 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001048 snd_printk(KERN_INFO
1049 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001050 cval->max = -256;
1051 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001052 break;
1053
1054 case USB_ID(0x046d, 0x09a4):
1055 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1056 snd_printk(KERN_INFO
1057 "set volume quirk for QuickCam E3500\n");
1058 cval->min = 6080;
1059 cval->max = 8768;
1060 cval->res = 192;
1061 }
1062 break;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1067 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001068 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071
1072
1073/*
1074 * parse a feature unit
1075 *
1076 * most of controlls are defined here.
1077 */
Daniel Mack28e1b772010-02-22 23:49:09 +01001078static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001081 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 unsigned int master_bits, first_ch_bits;
1083 int err, csize;
Daniel Mack28e1b772010-02-22 23:49:09 +01001084 struct uac_feature_unit_descriptor *ftr = _ftr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Daniel Mack28e1b772010-02-22 23:49:09 +01001086 if (ftr->bLength < 7 || ! (csize = ftr->bControlSize) || ftr->bLength < 7 + csize) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001087 snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EINVAL;
1089 }
1090
1091 /* parse the source unit */
Daniel Mack28e1b772010-02-22 23:49:09 +01001092 if ((err = parse_audio_unit(state, ftr->bSourceID)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return err;
1094
1095 /* determine the input source type and name */
Daniel Mack28e1b772010-02-22 23:49:09 +01001096 if (check_input_term(state, ftr->bSourceID, &iterm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EINVAL;
1098
Daniel Mack28e1b772010-02-22 23:49:09 +01001099 channels = (ftr->bLength - 7) / csize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Daniel Mack28e1b772010-02-22 23:49:09 +01001101 master_bits = snd_usb_combine_bytes(ftr->controls, csize);
Javier Kohen0c3cee52009-11-17 15:36:13 +01001102 /* master configuration quirks */
1103 switch (state->chip->usb_id) {
1104 case USB_ID(0x08bb, 0x2702):
1105 snd_printk(KERN_INFO
1106 "usbmixer: master volume quirk for PCM2702 chip\n");
1107 /* disable non-functional volume control */
Daniel Mack45d76052010-03-11 21:13:21 +01001108 master_bits &= ~UAC_FU_VOLUME;
Javier Kohen0c3cee52009-11-17 15:36:13 +01001109 break;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (channels > 0)
Daniel Mack28e1b772010-02-22 23:49:09 +01001112 first_ch_bits = snd_usb_combine_bytes(ftr->controls + csize, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 else
1114 first_ch_bits = 0;
1115 /* check all control types */
1116 for (i = 0; i < 10; i++) {
1117 unsigned int ch_bits = 0;
1118 for (j = 0; j < channels; j++) {
Daniel Mack28e1b772010-02-22 23:49:09 +01001119 unsigned int mask = snd_usb_combine_bytes(ftr->controls + csize * (j+1), csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (mask & (1 << i))
1121 ch_bits |= (1 << j);
1122 }
1123 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
Daniel Mack28e1b772010-02-22 23:49:09 +01001124 build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (master_bits & (1 << i))
Daniel Mack28e1b772010-02-22 23:49:09 +01001126 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
1129 return 0;
1130}
1131
1132
1133/*
1134 * Mixer Unit
1135 */
1136
1137/*
1138 * build a mixer unit control
1139 *
1140 * the callbacks are identical with feature unit.
1141 * input channel number (zero based) is given in control field instead.
1142 */
1143
Takashi Iwai86e07d32005-11-17 15:08:02 +01001144static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001146 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001148 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 unsigned int input_pins = desc[4];
1150 unsigned int num_outs = desc[5 + input_pins];
1151 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001152 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001153 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001155 map = find_map(state, unitid, 0);
1156 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return;
1158
Takashi Iwai561b2202005-09-09 14:22:34 +02001159 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (! cval)
1161 return;
1162
Clemens Ladisch84957a82005-04-29 16:23:13 +02001163 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 cval->id = unitid;
1165 cval->control = in_ch + 1; /* based on 1 */
1166 cval->val_type = USB_MIXER_S16;
1167 for (i = 0; i < num_outs; i++) {
1168 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1169 cval->cmask |= (1 << i);
1170 cval->channels++;
1171 }
1172 }
1173
1174 /* get min/max values */
1175 get_min_max(cval, 0);
1176
1177 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1178 if (! kctl) {
1179 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1180 kfree(cval);
1181 return;
1182 }
1183 kctl->private_free = usb_mixer_elem_free;
1184
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001185 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (! len)
1187 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1188 if (! len)
1189 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
Takashi Iwai08d1e632009-10-02 14:06:08 +02001190 append_ctl_name(kctl, " Volume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1193 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001194 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197
1198/*
1199 * parse a mixer unit
1200 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001201static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001203 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int input_pins, num_ins, num_outs;
1205 int pin, ich, err;
1206
1207 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1208 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1209 return -EINVAL;
1210 }
1211 /* no bmControls field (e.g. Maya44) -> ignore */
1212 if (desc[0] <= 10 + input_pins) {
1213 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1214 return 0;
1215 }
1216
1217 num_ins = 0;
1218 ich = 0;
1219 for (pin = 0; pin < input_pins; pin++) {
1220 err = parse_audio_unit(state, desc[5 + pin]);
1221 if (err < 0)
1222 return err;
1223 err = check_input_term(state, desc[5 + pin], &iterm);
1224 if (err < 0)
1225 return err;
1226 num_ins += iterm.channels;
1227 for (; ich < num_ins; ++ich) {
1228 int och, ich_has_controls = 0;
1229
1230 for (och = 0; och < num_outs; ++och) {
1231 if (check_matrix_bitmap(desc + 9 + input_pins,
1232 ich, och, num_outs)) {
1233 ich_has_controls = 1;
1234 break;
1235 }
1236 }
1237 if (ich_has_controls)
1238 build_mixer_unit_ctl(state, desc, pin, ich,
1239 unitid, &iterm);
1240 }
1241 }
1242 return 0;
1243}
1244
1245
1246/*
1247 * Processing Unit / Extension Unit
1248 */
1249
1250/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001251static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001253 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int err, val;
1255
1256 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001257 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 ucontrol->value.integer.value[0] = cval->min;
1259 return 0;
1260 }
1261 if (err < 0)
1262 return err;
1263 val = get_relative_value(cval, val);
1264 ucontrol->value.integer.value[0] = val;
1265 return 0;
1266}
1267
1268/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001269static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001271 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int val, oval, err;
1273
1274 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1275 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001276 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278 return err;
1279 }
1280 val = ucontrol->value.integer.value[0];
1281 val = get_abs_value(cval, val);
1282 if (val != oval) {
1283 set_cur_ctl_value(cval, cval->control << 8, val);
1284 return 1;
1285 }
1286 return 0;
1287}
1288
1289/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001290static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1292 .name = "", /* will be filled later */
1293 .info = mixer_ctl_feature_info,
1294 .get = mixer_ctl_procunit_get,
1295 .put = mixer_ctl_procunit_put,
1296};
1297
1298
1299/*
1300 * predefined data for processing units
1301 */
1302struct procunit_value_info {
1303 int control;
1304 char *suffix;
1305 int val_type;
1306 int min_value;
1307};
1308
1309struct procunit_info {
1310 int type;
1311 char *name;
1312 struct procunit_value_info *values;
1313};
1314
1315static struct procunit_value_info updown_proc_info[] = {
1316 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1317 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1318 { 0 }
1319};
1320static struct procunit_value_info prologic_proc_info[] = {
1321 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1322 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1323 { 0 }
1324};
1325static struct procunit_value_info threed_enh_proc_info[] = {
1326 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1327 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1328 { 0 }
1329};
1330static struct procunit_value_info reverb_proc_info[] = {
1331 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1332 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1333 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1334 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1335 { 0 }
1336};
1337static struct procunit_value_info chorus_proc_info[] = {
1338 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1339 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1340 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1341 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1342 { 0 }
1343};
1344static struct procunit_value_info dcr_proc_info[] = {
1345 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1346 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1347 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1348 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1349 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1350 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1351 { 0 }
1352};
1353
1354static struct procunit_info procunits[] = {
1355 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1356 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1357 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1358 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1359 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1360 { USB_PROC_DCR, "DCR", dcr_proc_info },
1361 { 0 },
1362};
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001363/*
1364 * predefined data for extension units
1365 */
1366static struct procunit_value_info clock_rate_xu_info[] = {
1367 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1368 { 0 }
1369};
1370static struct procunit_value_info clock_source_xu_info[] = {
1371 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1372 { 0 }
1373};
1374static struct procunit_value_info spdif_format_xu_info[] = {
1375 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1376 { 0 }
1377};
1378static struct procunit_value_info soft_limit_xu_info[] = {
1379 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1380 { 0 }
1381};
1382static struct procunit_info extunits[] = {
1383 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1384 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1385 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1386 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1387 { 0 }
1388};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
1390 * build a processing/extension unit
1391 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001392static 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 -07001393{
1394 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001395 struct usb_mixer_elem_info *cval;
1396 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int i, err, nameid, type, len;
1398 struct procunit_info *info;
1399 struct procunit_value_info *valinfo;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001400 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 static struct procunit_value_info default_value_info[] = {
1402 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1403 { 0 }
1404 };
1405 static struct procunit_info default_info = {
1406 0, NULL, default_value_info
1407 };
1408
1409 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1410 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1411 return -EINVAL;
1412 }
1413
1414 for (i = 0; i < num_ins; i++) {
1415 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1416 return err;
1417 }
1418
1419 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 for (info = list; info && info->type; info++)
1421 if (info->type == type)
1422 break;
1423 if (! info || ! info->type)
1424 info = &default_info;
1425
1426 for (valinfo = info->values; valinfo->control; valinfo++) {
1427 /* FIXME: bitmap might be longer than 8bit */
1428 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1429 continue;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001430 map = find_map(state, unitid, valinfo->control);
1431 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001433 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (! cval) {
1435 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1436 return -ENOMEM;
1437 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001438 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 cval->id = unitid;
1440 cval->control = valinfo->control;
1441 cval->val_type = valinfo->val_type;
1442 cval->channels = 1;
1443
1444 /* get min/max values */
1445 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1446 /* FIXME: hard-coded */
1447 cval->min = 1;
1448 cval->max = dsc[15];
1449 cval->res = 1;
1450 cval->initialized = 1;
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001451 } else {
1452 if (type == USB_XU_CLOCK_RATE) {
1453 /* E-Mu USB 0404/0202/TrackerPre
1454 * samplerate control quirk
1455 */
1456 cval->min = 0;
1457 cval->max = 5;
1458 cval->res = 1;
1459 cval->initialized = 1;
1460 } else
1461 get_min_max(cval, valinfo->min_value);
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1465 if (! kctl) {
1466 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1467 kfree(cval);
1468 return -ENOMEM;
1469 }
1470 kctl->private_free = usb_mixer_elem_free;
1471
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001472 if (check_mapped_name(map, kctl->id.name,
1473 sizeof(kctl->id.name)))
1474 /* nothing */ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 else if (info->name)
1476 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1477 else {
1478 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1479 len = 0;
1480 if (nameid)
1481 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1482 if (! len)
1483 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1484 }
Takashi Iwai08d1e632009-10-02 14:06:08 +02001485 append_ctl_name(kctl, " ");
1486 append_ctl_name(kctl, valinfo->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1489 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001490 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return err;
1492 }
1493 return 0;
1494}
1495
1496
Takashi Iwai86e07d32005-11-17 15:08:02 +01001497static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1500}
1501
Takashi Iwai86e07d32005-11-17 15:08:02 +01001502static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001504 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507
1508/*
1509 * Selector Unit
1510 */
1511
1512/* info callback for selector unit
1513 * use an enumerator type for routing
1514 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001515static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001517 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 char **itemlist = (char **)kcontrol->private_value;
1519
Takashi Iwai5e246b82008-08-08 17:12:47 +02001520 if (snd_BUG_ON(!itemlist))
1521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1523 uinfo->count = 1;
1524 uinfo->value.enumerated.items = cval->max;
1525 if ((int)uinfo->value.enumerated.item >= cval->max)
1526 uinfo->value.enumerated.item = cval->max - 1;
1527 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1528 return 0;
1529}
1530
1531/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001532static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001534 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int val, err;
1536
1537 err = get_cur_ctl_value(cval, 0, &val);
1538 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001539 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 ucontrol->value.enumerated.item[0] = 0;
1541 return 0;
1542 }
1543 return err;
1544 }
1545 val = get_relative_value(cval, val);
1546 ucontrol->value.enumerated.item[0] = val;
1547 return 0;
1548}
1549
1550/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001551static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001553 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int val, oval, err;
1555
1556 err = get_cur_ctl_value(cval, 0, &oval);
1557 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001558 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return 0;
1560 return err;
1561 }
1562 val = ucontrol->value.enumerated.item[0];
1563 val = get_abs_value(cval, val);
1564 if (val != oval) {
1565 set_cur_ctl_value(cval, 0, val);
1566 return 1;
1567 }
1568 return 0;
1569}
1570
1571/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001572static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1574 .name = "", /* will be filled later */
1575 .info = mixer_ctl_selector_info,
1576 .get = mixer_ctl_selector_get,
1577 .put = mixer_ctl_selector_put,
1578};
1579
1580
1581/* private free callback.
1582 * free both private_data and private_value
1583 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001584static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 int i, num_ins = 0;
1587
1588 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001589 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 num_ins = cval->max;
1591 kfree(cval);
1592 kctl->private_data = NULL;
1593 }
1594 if (kctl->private_value) {
1595 char **itemlist = (char **)kctl->private_value;
1596 for (i = 0; i < num_ins; i++)
1597 kfree(itemlist[i]);
1598 kfree(itemlist);
1599 kctl->private_value = 0;
1600 }
1601}
1602
1603/*
1604 * parse a selector unit
1605 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001606static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 unsigned int num_ins = desc[4];
1609 unsigned int i, nameid, len;
1610 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001611 struct usb_mixer_elem_info *cval;
1612 struct snd_kcontrol *kctl;
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001613 const struct usbmix_name_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 char **namelist;
1615
Russ Cox38977e92007-08-06 15:37:58 +02001616 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1618 return -EINVAL;
1619 }
1620
1621 for (i = 0; i < num_ins; i++) {
1622 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1623 return err;
1624 }
1625
1626 if (num_ins == 1) /* only one ? nonsense! */
1627 return 0;
1628
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001629 map = find_map(state, unitid, 0);
1630 if (check_ignored_ctl(map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632
Takashi Iwai561b2202005-09-09 14:22:34 +02001633 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (! cval) {
1635 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1636 return -ENOMEM;
1637 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001638 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 cval->id = unitid;
1640 cval->val_type = USB_MIXER_U8;
1641 cval->channels = 1;
1642 cval->min = 1;
1643 cval->max = num_ins;
1644 cval->res = 1;
1645 cval->initialized = 1;
1646
1647 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1648 if (! namelist) {
1649 snd_printk(KERN_ERR "cannot malloc\n");
1650 kfree(cval);
1651 return -ENOMEM;
1652 }
1653#define MAX_ITEM_NAME_LEN 64
1654 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001655 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 len = 0;
1657 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1658 if (! namelist[i]) {
1659 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001660 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 kfree(namelist[i]);
1662 kfree(namelist);
1663 kfree(cval);
1664 return -ENOMEM;
1665 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001666 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1667 MAX_ITEM_NAME_LEN);
1668 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1670 if (! len)
1671 sprintf(namelist[i], "Input %d", i);
1672 }
1673
1674 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1675 if (! kctl) {
1676 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001677 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 kfree(cval);
1679 return -ENOMEM;
1680 }
1681 kctl->private_value = (unsigned long)namelist;
1682 kctl->private_free = usb_mixer_selector_elem_free;
1683
1684 nameid = desc[desc[0] - 1];
Jaroslav Kyselac3a3e042010-02-11 17:50:44 +01001685 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (len)
1687 ;
1688 else if (nameid)
1689 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1690 else {
1691 len = get_term_name(state, &state->oterm,
1692 kctl->id.name, sizeof(kctl->id.name), 0);
1693 if (! len)
1694 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1695
1696 if ((state->oterm.type & 0xff00) == 0x0100)
Takashi Iwai08d1e632009-10-02 14:06:08 +02001697 append_ctl_name(kctl, " Capture Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 else
Takashi Iwai08d1e632009-10-02 14:06:08 +02001699 append_ctl_name(kctl, " Playback Source");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
1702 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1703 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001704 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return err;
1706
1707 return 0;
1708}
1709
1710
1711/*
1712 * parse an audio unit recursively
1713 */
1714
Takashi Iwai86e07d32005-11-17 15:08:02 +01001715static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 unsigned char *p1;
1718
1719 if (test_and_set_bit(unitid, state->unitbitmap))
1720 return 0; /* the unit already visited */
1721
1722 p1 = find_audio_control_unit(state, unitid);
1723 if (!p1) {
1724 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1725 return -EINVAL;
1726 }
1727
1728 switch (p1[2]) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001729 case UAC_INPUT_TERMINAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0; /* NOP */
Daniel Mackde48c7b2010-02-22 23:49:13 +01001731 case UAC_MIXER_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return parse_audio_mixer_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001733 case UAC_SELECTOR_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return parse_audio_selector_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001735 case UAC_FEATURE_UNIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return parse_audio_feature_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001737 case UAC_PROCESSING_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return parse_audio_processing_unit(state, unitid, p1);
Daniel Mackde48c7b2010-02-22 23:49:13 +01001739 case UAC_EXTENSION_UNIT_V1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return parse_audio_extension_unit(state, unitid, p1);
1741 default:
1742 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1743 return -EINVAL;
1744 }
1745}
1746
Clemens Ladisch84957a82005-04-29 16:23:13 +02001747static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1748{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001749 kfree(mixer->id_elems);
1750 if (mixer->urb) {
1751 kfree(mixer->urb->transfer_buffer);
1752 usb_free_urb(mixer->urb);
1753 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001754 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001755 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001756 kfree(mixer);
1757}
1758
Takashi Iwai86e07d32005-11-17 15:08:02 +01001759static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001760{
1761 struct usb_mixer_interface *mixer = device->device_data;
1762 snd_usb_mixer_free(mixer);
1763 return 0;
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
1767 * create mixer controls
1768 *
Daniel Mackde48c7b2010-02-22 23:49:13 +01001769 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001771static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Daniel Mack28e1b772010-02-22 23:49:09 +01001773 struct uac_output_terminal_descriptor_v1 *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001774 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int err;
1776 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001777 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Clemens Ladisch84957a82005-04-29 16:23:13 +02001779 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001781 state.chip = mixer->chip;
1782 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 state.buffer = hostif->extra;
1784 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001787 for (map = usbmix_ctl_maps; map->id; map++) {
1788 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001790 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001791 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793 }
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 desc = NULL;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001797 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, UAC_OUTPUT_TERMINAL)) != NULL) {
Daniel Mack28e1b772010-02-22 23:49:09 +01001798 if (desc->bLength < 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 continue; /* invalid descriptor? */
Daniel Mack28e1b772010-02-22 23:49:09 +01001800 set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
1801 state.oterm.id = desc->bTerminalID;
1802 state.oterm.type = le16_to_cpu(desc->wTerminalType);
1803 state.oterm.name = desc->iTerminal;
1804 err = parse_audio_unit(&state, desc->bSourceID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (err < 0)
1806 return err;
1807 }
1808 return 0;
1809}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001810
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001811static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1812 int unitid)
1813{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001814 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001815
1816 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1817 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1818 info->elem_id);
1819}
1820
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001821static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1822 int unitid,
1823 struct usb_mixer_elem_info *cval)
1824{
1825 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1826 "S8", "U8", "S16", "U16"};
1827 snd_iprintf(buffer, " Unit: %i\n", unitid);
1828 if (cval->elem_id)
1829 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1830 cval->elem_id->name, cval->elem_id->index);
1831 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1832 "channels=%i, type=\"%s\"\n", cval->id,
1833 cval->control, cval->cmask, cval->channels,
1834 val_types[cval->val_type]);
1835 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1836 cval->min, cval->max, cval->dBmin, cval->dBmax);
1837}
1838
1839static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1840 struct snd_info_buffer *buffer)
1841{
1842 struct snd_usb_audio *chip = entry->private_data;
1843 struct usb_mixer_interface *mixer;
1844 struct usb_mixer_elem_info *cval;
1845 int unitid;
1846
1847 list_for_each_entry(mixer, &chip->mixer_list, list) {
1848 snd_iprintf(buffer,
Jaroslav Kysela7affdc12010-02-16 11:52:27 +01001849 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1850 chip->usb_id, mixer->ctrlif,
1851 mixer->ignore_ctl_error);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01001852 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1853 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1854 for (cval = mixer->id_elems[unitid]; cval;
1855 cval = cval->next_id_elem)
1856 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1857 }
1858 }
1859}
1860
Clemens Ladischb259b102005-04-29 16:29:28 +02001861static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1862 int unitid)
1863{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001864 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001865 return;
1866 /* unit ids specific to Extigy/Audigy 2 NX: */
1867 switch (unitid) {
1868 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001869 mixer->rc_urb->dev = mixer->chip->dev;
1870 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001871 break;
1872 case 4: /* digital in jack */
1873 case 7: /* line in jacks */
1874 case 19: /* speaker out jacks */
1875 case 20: /* headphones out jack */
1876 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001877 /* live24ext: 4 = line-in jack */
1878 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001879 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1880 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001881 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1882 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001883 default:
1884 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1885 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001886 }
1887}
1888
David Howells7d12e782006-10-05 14:55:46 +01001889static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001890{
1891 struct usb_mixer_interface *mixer = urb->context;
1892
1893 if (urb->status == 0) {
1894 u8 *buf = urb->transfer_buffer;
1895 int i;
1896
1897 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1898 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1899 buf[0], buf[1]);
1900 /* ignore any notifications not from the control interface */
1901 if ((buf[0] & 0x0f) != 0)
1902 continue;
1903 if (!(buf[0] & 0x40))
1904 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001905 else
1906 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001907 }
1908 }
1909 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1910 urb->dev = mixer->chip->dev;
1911 usb_submit_urb(urb, GFP_ATOMIC);
1912 }
1913}
1914
1915/* create the handler for the optional status interrupt endpoint */
1916static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1917{
1918 struct usb_host_interface *hostif;
1919 struct usb_endpoint_descriptor *ep;
1920 void *transfer_buffer;
1921 int buffer_length;
1922 unsigned int epnum;
1923
1924 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1925 /* we need one interrupt input endpoint */
1926 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1927 return 0;
1928 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001929 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001930 return 0;
1931
Julia Lawall42a6e662008-12-29 11:23:02 +01001932 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001933 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1934 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1935 if (!transfer_buffer)
1936 return -ENOMEM;
1937 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1938 if (!mixer->urb) {
1939 kfree(transfer_buffer);
1940 return -ENOMEM;
1941 }
1942 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1943 usb_rcvintpipe(mixer->chip->dev, epnum),
1944 transfer_buffer, buffer_length,
1945 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1946 usb_submit_urb(mixer->urb, GFP_KERNEL);
1947 return 0;
1948}
1949
David Howells7d12e782006-10-05 14:55:46 +01001950static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001951{
1952 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001953 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001954 u32 code;
1955
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001956 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001957 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001958
1959 code = mixer->rc_buffer[rc->offset];
1960 if (rc->length == 2)
1961 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1962
Clemens Ladischb259b102005-04-29 16:29:28 +02001963 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001964 if (code == rc->mute_code)
1965 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001966 mixer->rc_code = code;
1967 wmb();
1968 wake_up(&mixer->rc_waitq);
1969}
1970
Takashi Iwai86e07d32005-11-17 15:08:02 +01001971static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001972 long count, loff_t *offset)
1973{
1974 struct usb_mixer_interface *mixer = hw->private_data;
1975 int err;
1976 u32 rc_code;
1977
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001978 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001979 return -EINVAL;
1980 err = wait_event_interruptible(mixer->rc_waitq,
1981 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1982 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001983 if (count == 1)
1984 err = put_user(rc_code, buf);
1985 else
1986 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001987 }
1988 return err < 0 ? err : count;
1989}
1990
Takashi Iwai86e07d32005-11-17 15:08:02 +01001991static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001992 poll_table *wait)
1993{
1994 struct usb_mixer_interface *mixer = hw->private_data;
1995
1996 poll_wait(file, &mixer->rc_waitq, wait);
1997 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1998}
1999
2000static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
2001{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002002 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002003 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02002004
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002005 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
2006 if (rc_configs[i].usb_id == mixer->chip->usb_id)
2007 break;
2008 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02002009 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002010 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02002011
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02002012 len = mixer->rc_cfg->packet_length;
2013
Clemens Ladischb259b102005-04-29 16:29:28 +02002014 init_waitqueue_head(&mixer->rc_waitq);
2015 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
2016 if (err < 0)
2017 return err;
2018 snprintf(hwdep->name, sizeof(hwdep->name),
2019 "%s remote control", mixer->chip->card->shortname);
2020 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
2021 hwdep->private_data = mixer;
2022 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02002023 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01002024 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02002025
2026 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
2027 if (!mixer->rc_urb)
2028 return -ENOMEM;
2029 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
2030 if (!mixer->rc_setup_packet) {
2031 usb_free_urb(mixer->rc_urb);
2032 mixer->rc_urb = NULL;
2033 return -ENOMEM;
2034 }
2035 mixer->rc_setup_packet->bRequestType =
2036 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
Daniel Mackde48c7b2010-02-22 23:49:13 +01002037 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
Clemens Ladischb259b102005-04-29 16:29:28 +02002038 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2039 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2040 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2041 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2042 usb_rcvctrlpipe(mixer->chip->dev, 0),
2043 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2044 snd_usb_soundblaster_remote_complete, mixer);
2045 return 0;
2046}
2047
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002048#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002049
Takashi Iwai86e07d32005-11-17 15:08:02 +01002050static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002051{
2052 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2053 int index = kcontrol->private_value;
2054
2055 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2056 return 0;
2057}
2058
Takashi Iwai86e07d32005-11-17 15:08:02 +01002059static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002060{
2061 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2062 int index = kcontrol->private_value;
2063 int value = ucontrol->value.integer.value[0];
2064 int err, changed;
2065
2066 if (value > 1)
2067 return -EINVAL;
2068 changed = value != mixer->audigy2nx_leds[index];
2069 err = snd_usb_ctl_msg(mixer->chip->dev,
2070 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2071 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2072 value, index + 2, NULL, 0, 100);
2073 if (err < 0)
2074 return err;
2075 mixer->audigy2nx_leds[index] = value;
2076 return changed;
2077}
2078
Takashi Iwai86e07d32005-11-17 15:08:02 +01002079static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002080 {
2081 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2082 .name = "CMSS LED Switch",
2083 .info = snd_audigy2nx_led_info,
2084 .get = snd_audigy2nx_led_get,
2085 .put = snd_audigy2nx_led_put,
2086 .private_value = 0,
2087 },
2088 {
2089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2090 .name = "Power LED Switch",
2091 .info = snd_audigy2nx_led_info,
2092 .get = snd_audigy2nx_led_get,
2093 .put = snd_audigy2nx_led_put,
2094 .private_value = 1,
2095 },
2096 {
2097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2098 .name = "Dolby Digital LED Switch",
2099 .info = snd_audigy2nx_led_info,
2100 .get = snd_audigy2nx_led_get,
2101 .put = snd_audigy2nx_led_put,
2102 .private_value = 2,
2103 },
2104};
2105
2106static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2107{
2108 int i, err;
2109
2110 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01002111 if (i > 1 && /* Live24ext has 2 LEDs only */
2112 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2113 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002114 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002115 err = snd_ctl_add(mixer->chip->card,
2116 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2117 if (err < 0)
2118 return err;
2119 }
2120 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2121 return 0;
2122}
2123
Takashi Iwai86e07d32005-11-17 15:08:02 +01002124static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2125 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02002126{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002127 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02002128 int unitid;
2129 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002130 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02002131 {4, "dig in "},
2132 {7, "line in"},
2133 {19, "spk out"},
2134 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002135 {-1, NULL}
2136 }, jacks_live24ext[] = {
2137 {4, "line in"}, /* &1=Line, &2=Mic*/
2138 {3, "hph out"}, /* headphones */
2139 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2140 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002141 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002142 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002143 struct usb_mixer_interface *mixer = entry->private_data;
2144 int i, err;
2145 u8 buf[3];
2146
2147 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002148 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2149 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002150 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2151 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002152 jacks = jacks_live24ext;
2153 else
2154 return;
2155
2156 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002157 snd_iprintf(buffer, "%s: ", jacks[i].name);
2158 err = snd_usb_ctl_msg(mixer->chip->dev,
2159 usb_rcvctrlpipe(mixer->chip->dev, 0),
Daniel Mackde48c7b2010-02-22 23:49:13 +01002160 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
Clemens Ladischaafad562005-05-10 14:47:38 +02002161 USB_RECIP_INTERFACE, 0,
2162 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002163 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002164 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2165 else
2166 snd_iprintf(buffer, "?\n");
2167 }
2168}
2169
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002170static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_value *ucontrol)
2172{
2173 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2174
2175 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2176 return 0;
2177}
2178
2179static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2181{
2182 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2183 u8 old_status, new_status;
2184 int err, changed;
2185
2186 old_status = mixer->xonar_u1_status;
2187 if (ucontrol->value.integer.value[0])
2188 new_status = old_status | 0x02;
2189 else
2190 new_status = old_status & ~0x02;
2191 changed = new_status != old_status;
2192 err = snd_usb_ctl_msg(mixer->chip->dev,
2193 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2195 50, 0, &new_status, 1, 100);
2196 if (err < 0)
2197 return err;
2198 mixer->xonar_u1_status = new_status;
2199 return changed;
2200}
2201
2202static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2204 .name = "Digital Playback Switch",
2205 .info = snd_ctl_boolean_mono_info,
2206 .get = snd_xonar_u1_switch_get,
2207 .put = snd_xonar_u1_switch_put,
2208};
2209
2210static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2211{
2212 int err;
2213
2214 err = snd_ctl_add(mixer->chip->card,
2215 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2216 if (err < 0)
2217 return err;
2218 mixer->xonar_u1_status = 0x05;
2219 return 0;
2220}
2221
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002222void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002223 unsigned char samplerate_id)
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002224{
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002225 struct usb_mixer_interface *mixer;
2226 struct usb_mixer_elem_info *cval;
2227 int unitid = 12; /* SamleRate ExtensionUnit ID */
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002228
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002229 list_for_each_entry(mixer, &chip->mixer_list, list) {
2230 cval = mixer->id_elems[unitid];
2231 if (cval) {
2232 set_cur_ctl_value(cval, cval->control << 8,
2233 samplerate_id);
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002234 snd_usb_mixer_notify_id(mixer, unitid);
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002235 }
2236 break;
2237 }
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08002238}
2239
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002240int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2241 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002242{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002243 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002244 .dev_free = snd_usb_mixer_dev_free
2245 };
2246 struct usb_mixer_interface *mixer;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002247 struct snd_info_entry *entry;
Daniel Mack7b8a0432010-02-22 23:49:12 +01002248 struct usb_host_interface *host_iface;
2249 int err, protocol;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002250
2251 strcpy(chip->card->mixername, "USB Mixer");
2252
Takashi Iwai561b2202005-09-09 14:22:34 +02002253 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002254 if (!mixer)
2255 return -ENOMEM;
2256 mixer->chip = chip;
2257 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002258 mixer->ignore_ctl_error = ignore_error;
Jaroslav Kysela291186e2010-02-16 11:55:18 +01002259 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2260 GFP_KERNEL);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002261 if (!mixer->id_elems) {
2262 kfree(mixer);
2263 return -ENOMEM;
2264 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002265
Daniel Mack7b8a0432010-02-22 23:49:12 +01002266 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2267 protocol = host_iface->desc.bInterfaceProtocol;
2268
2269 /* FIXME! */
2270 if (protocol != UAC_VERSION_1) {
2271 snd_printk(KERN_WARNING "mixer interface protocol 0x%02x not yet supported\n",
2272 protocol);
2273 return 0;
2274 }
2275
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002276 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002277 (err = snd_usb_mixer_status_create(mixer)) < 0)
2278 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002279
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002280 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2281 goto _error;
2282
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002283 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002284 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2285 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002286 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2287 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002288 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002289 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002290 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002291 }
2292
Clemens Ladisch468b8fd2009-07-13 11:39:29 +02002293 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2294 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2295 err = snd_xonar_u1_controls_create(mixer);
2296 if (err < 0)
2297 goto _error;
2298 }
2299
Clemens Ladisch84957a82005-04-29 16:23:13 +02002300 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002301 if (err < 0)
2302 goto _error;
Jaroslav Kyselaebfdeea2010-02-16 11:17:09 +01002303
2304 if (list_empty(&chip->mixer_list) &&
2305 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2306 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2307
Clemens Ladisch84957a82005-04-29 16:23:13 +02002308 list_add(&mixer->list, &chip->mixer_list);
2309 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002310
2311_error:
2312 snd_usb_mixer_free(mixer);
2313 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002314}
2315
2316void snd_usb_mixer_disconnect(struct list_head *p)
2317{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002318 struct usb_mixer_interface *mixer;
2319
2320 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002321 usb_kill_urb(mixer->urb);
2322 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002323}