~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/usb/mixer.c

Version: ~ [ linux-5.10-rc1 ] ~ [ linux-5.9.1 ] ~ [ linux-5.8.16 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.72 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.152 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.202 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.240 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.240 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *   (Tentative) USB Audio Driver for ALSA
  3  *
  4  *   Mixer control part
  5  *
  6  *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7  *
  8  *   Many codes borrowed from audio.c by
  9  *          Alan Cox (alan@lxorguk.ukuu.org.uk)
 10  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
 11  *
 12  *
 13  *   This program is free software; you can redistribute it and/or modify
 14  *   it under the terms of the GNU General Public License as published by
 15  *   the Free Software Foundation; either version 2 of the License, or
 16  *   (at your option) any later version.
 17  *
 18  *   This program is distributed in the hope that it will be useful,
 19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  *   GNU General Public License for more details.
 22  *
 23  *   You should have received a copy of the GNU General Public License
 24  *   along with this program; if not, write to the Free Software
 25  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 26  *
 27  */
 28 
 29 /*
 30  * TODOs, for both the mixer and the streaming interfaces:
 31  *
 32  *  - support for UAC2 effect units
 33  *  - support for graphical equalizers
 34  *  - RANGE and MEM set commands (UAC2)
 35  *  - RANGE and MEM interrupt dispatchers (UAC2)
 36  *  - audio channel clustering (UAC2)
 37  *  - audio sample rate converter units (UAC2)
 38  *  - proper handling of clock multipliers (UAC2)
 39  *  - dispatch clock change notifications (UAC2)
 40  *      - stop PCM streams which use a clock that became invalid
 41  *      - stop PCM streams which use a clock selector that has changed
 42  *      - parse available sample rates again when clock sources changed
 43  */
 44 
 45 #include <linux/bitops.h>
 46 #include <linux/init.h>
 47 #include <linux/list.h>
 48 #include <linux/log2.h>
 49 #include <linux/slab.h>
 50 #include <linux/string.h>
 51 #include <linux/usb.h>
 52 #include <linux/usb/audio.h>
 53 #include <linux/usb/audio-v2.h>
 54 
 55 #include <sound/core.h>
 56 #include <sound/control.h>
 57 #include <sound/hwdep.h>
 58 #include <sound/info.h>
 59 #include <sound/tlv.h>
 60 
 61 #include "usbaudio.h"
 62 #include "mixer.h"
 63 #include "helper.h"
 64 #include "mixer_quirks.h"
 65 #include "power.h"
 66 
 67 #define MAX_ID_ELEMS    256
 68 
 69 struct usb_audio_term {
 70         int id;
 71         int type;
 72         int channels;
 73         unsigned int chconfig;
 74         int name;
 75 };
 76 
 77 struct usbmix_name_map;
 78 
 79 struct mixer_build {
 80         struct snd_usb_audio *chip;
 81         struct usb_mixer_interface *mixer;
 82         unsigned char *buffer;
 83         unsigned int buflen;
 84         DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
 85         struct usb_audio_term oterm;
 86         const struct usbmix_name_map *map;
 87         const struct usbmix_selector_map *selector_map;
 88 };
 89 
 90 /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
 91 enum {
 92         USB_XU_CLOCK_RATE               = 0xe301,
 93         USB_XU_CLOCK_SOURCE             = 0xe302,
 94         USB_XU_DIGITAL_IO_STATUS        = 0xe303,
 95         USB_XU_DEVICE_OPTIONS           = 0xe304,
 96         USB_XU_DIRECT_MONITORING        = 0xe305,
 97         USB_XU_METERING                 = 0xe306
 98 };
 99 enum {
100         USB_XU_CLOCK_SOURCE_SELECTOR = 0x02,    /* clock source*/
101         USB_XU_CLOCK_RATE_SELECTOR = 0x03,      /* clock rate */
102         USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01,  /* the spdif format */
103         USB_XU_SOFT_LIMIT_SELECTOR = 0x03       /* soft limiter */
104 };
105 
106 /*
107  * manual mapping of mixer names
108  * if the mixer topology is too complicated and the parsed names are
109  * ambiguous, add the entries in usbmixer_maps.c.
110  */
111 #include "mixer_maps.c"
112 
113 static const struct usbmix_name_map *
114 find_map(struct mixer_build *state, int unitid, int control)
115 {
116         const struct usbmix_name_map *p = state->map;
117 
118         if (!p)
119                 return NULL;
120 
121         for (p = state->map; p->id; p++) {
122                 if (p->id == unitid &&
123                     (!control || !p->control || control == p->control))
124                         return p;
125         }
126         return NULL;
127 }
128 
129 /* get the mapped name if the unit matches */
130 static int
131 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
132 {
133         if (!p || !p->name)
134                 return 0;
135 
136         buflen--;
137         return strlcpy(buf, p->name, buflen);
138 }
139 
140 /* ignore the error value if ignore_ctl_error flag is set */
141 #define filter_error(cval, err) \
142         ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
143 
144 /* check whether the control should be ignored */
145 static inline int
146 check_ignored_ctl(const struct usbmix_name_map *p)
147 {
148         if (!p || p->name || p->dB)
149                 return 0;
150         return 1;
151 }
152 
153 /* dB mapping */
154 static inline void check_mapped_dB(const struct usbmix_name_map *p,
155                                    struct usb_mixer_elem_info *cval)
156 {
157         if (p && p->dB) {
158                 cval->dBmin = p->dB->min;
159                 cval->dBmax = p->dB->max;
160                 cval->initialized = 1;
161         }
162 }
163 
164 /* get the mapped selector source name */
165 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
166                                       int index, char *buf, int buflen)
167 {
168         const struct usbmix_selector_map *p;
169 
170         if (!state->selector_map)
171                 return 0;
172         for (p = state->selector_map; p->id; p++) {
173                 if (p->id == unitid && index < p->count)
174                         return strlcpy(buf, p->names[index], buflen);
175         }
176         return 0;
177 }
178 
179 /*
180  * find an audio control unit with the given unit id
181  */
182 static void *find_audio_control_unit(struct mixer_build *state,
183                                      unsigned char unit)
184 {
185         /* we just parse the header */
186         struct uac_feature_unit_descriptor *hdr = NULL;
187 
188         while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
189                                         USB_DT_CS_INTERFACE)) != NULL) {
190                 if (hdr->bLength >= 4 &&
191                     hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
192                     hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER &&
193                     hdr->bUnitID == unit)
194                         return hdr;
195         }
196 
197         return NULL;
198 }
199 
200 /*
201  * copy a string with the given id
202  */
203 static int snd_usb_copy_string_desc(struct mixer_build *state,
204                                     int index, char *buf, int maxlen)
205 {
206         int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
207 
208         if (len < 0)
209                 return 0;
210 
211         buf[len] = 0;
212         return len;
213 }
214 
215 /*
216  * convert from the byte/word on usb descriptor to the zero-based integer
217  */
218 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
219 {
220         switch (cval->val_type) {
221         case USB_MIXER_BOOLEAN:
222                 return !!val;
223         case USB_MIXER_INV_BOOLEAN:
224                 return !val;
225         case USB_MIXER_U8:
226                 val &= 0xff;
227                 break;
228         case USB_MIXER_S8:
229                 val &= 0xff;
230                 if (val >= 0x80)
231                         val -= 0x100;
232                 break;
233         case USB_MIXER_U16:
234                 val &= 0xffff;
235                 break;
236         case USB_MIXER_S16:
237                 val &= 0xffff;
238                 if (val >= 0x8000)
239                         val -= 0x10000;
240                 break;
241         }
242         return val;
243 }
244 
245 /*
246  * convert from the zero-based int to the byte/word for usb descriptor
247  */
248 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
249 {
250         switch (cval->val_type) {
251         case USB_MIXER_BOOLEAN:
252                 return !!val;
253         case USB_MIXER_INV_BOOLEAN:
254                 return !val;
255         case USB_MIXER_S8:
256         case USB_MIXER_U8:
257                 return val & 0xff;
258         case USB_MIXER_S16:
259         case USB_MIXER_U16:
260                 return val & 0xffff;
261         }
262         return 0; /* not reached */
263 }
264 
265 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
266 {
267         if (!cval->res)
268                 cval->res = 1;
269         if (val < cval->min)
270                 return 0;
271         else if (val >= cval->max)
272                 return (cval->max - cval->min + cval->res - 1) / cval->res;
273         else
274                 return (val - cval->min) / cval->res;
275 }
276 
277 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
278 {
279         if (val < 0)
280                 return cval->min;
281         if (!cval->res)
282                 cval->res = 1;
283         val *= cval->res;
284         val += cval->min;
285         if (val > cval->max)
286                 return cval->max;
287         return val;
288 }
289 
290 static int uac2_ctl_value_size(int val_type)
291 {
292         switch (val_type) {
293         case USB_MIXER_S32:
294         case USB_MIXER_U32:
295                 return 4;
296         case USB_MIXER_S16:
297         case USB_MIXER_U16:
298                 return 2;
299         default:
300                 return 1;
301         }
302         return 0; /* unreachable */
303 }
304 
305 
306 /*
307  * retrieve a mixer value
308  */
309 
310 static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
311                             int validx, int *value_ret)
312 {
313         struct snd_usb_audio *chip = cval->head.mixer->chip;
314         unsigned char buf[2];
315         int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
316         int timeout = 10;
317         int idx = 0, err;
318 
319         err = snd_usb_lock_shutdown(chip);
320         if (err < 0)
321                 return -EIO;
322 
323         while (timeout-- > 0) {
324                 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
325                 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
326                                       USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
327                                       validx, idx, buf, val_len);
328                 if (err >= val_len) {
329                         *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
330                         err = 0;
331                         goto out;
332                 } else if (err == -ETIMEDOUT) {
333                         goto out;
334                 }
335         }
336         usb_audio_dbg(chip,
337                 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
338                 request, validx, idx, cval->val_type);
339         err = -EINVAL;
340 
341  out:
342         snd_usb_unlock_shutdown(chip);
343         return err;
344 }
345 
346 static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
347                             int validx, int *value_ret)
348 {
349         struct snd_usb_audio *chip = cval->head.mixer->chip;
350         /* enough space for one range */
351         unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
352         unsigned char *val;
353         int idx = 0, ret, val_size, size;
354         __u8 bRequest;
355 
356         val_size = uac2_ctl_value_size(cval->val_type);
357 
358         if (request == UAC_GET_CUR) {
359                 bRequest = UAC2_CS_CUR;
360                 size = val_size;
361         } else {
362                 bRequest = UAC2_CS_RANGE;
363                 size = sizeof(__u16) + 3 * val_size;
364         }
365 
366         memset(buf, 0, sizeof(buf));
367 
368         ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
369         if (ret)
370                 goto error;
371 
372         idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
373         ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
374                               USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
375                               validx, idx, buf, size);
376         snd_usb_unlock_shutdown(chip);
377 
378         if (ret < 0) {
379 error:
380                 usb_audio_err(chip,
381                         "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
382                         request, validx, idx, cval->val_type);
383                 return ret;
384         }
385 
386         /* FIXME: how should we handle multiple triplets here? */
387 
388         switch (request) {
389         case UAC_GET_CUR:
390                 val = buf;
391                 break;
392         case UAC_GET_MIN:
393                 val = buf + sizeof(__u16);
394                 break;
395         case UAC_GET_MAX:
396                 val = buf + sizeof(__u16) + val_size;
397                 break;
398         case UAC_GET_RES:
399                 val = buf + sizeof(__u16) + val_size * 2;
400                 break;
401         default:
402                 return -EINVAL;
403         }
404 
405         *value_ret = convert_signed_value(cval,
406                                           snd_usb_combine_bytes(val, val_size));
407 
408         return 0;
409 }
410 
411 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
412                          int validx, int *value_ret)
413 {
414         validx += cval->idx_off;
415 
416         return (cval->head.mixer->protocol == UAC_VERSION_1) ?
417                 get_ctl_value_v1(cval, request, validx, value_ret) :
418                 get_ctl_value_v2(cval, request, validx, value_ret);
419 }
420 
421 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
422                              int validx, int *value)
423 {
424         return get_ctl_value(cval, UAC_GET_CUR, validx, value);
425 }
426 
427 /* channel = 0: master, 1 = first channel */
428 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
429                                   int channel, int *value)
430 {
431         return get_ctl_value(cval, UAC_GET_CUR,
432                              (cval->control << 8) | channel,
433                              value);
434 }
435 
436 int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
437                              int channel, int index, int *value)
438 {
439         int err;
440 
441         if (cval->cached & (1 << channel)) {
442                 *value = cval->cache_val[index];
443                 return 0;
444         }
445         err = get_cur_mix_raw(cval, channel, value);
446         if (err < 0) {
447                 if (!cval->head.mixer->ignore_ctl_error)
448                         usb_audio_dbg(cval->head.mixer->chip,
449                                 "cannot get current value for control %d ch %d: err = %d\n",
450                                       cval->control, channel, err);
451                 return err;
452         }
453         cval->cached |= 1 << channel;
454         cval->cache_val[index] = *value;
455         return 0;
456 }
457 
458 /*
459  * set a mixer value
460  */
461 
462 int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
463                                 int request, int validx, int value_set)
464 {
465         struct snd_usb_audio *chip = cval->head.mixer->chip;
466         unsigned char buf[4];
467         int idx = 0, val_len, err, timeout = 10;
468 
469         validx += cval->idx_off;
470 
471         if (cval->head.mixer->protocol == UAC_VERSION_1) {
472                 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
473         } else { /* UAC_VERSION_2 */
474                 val_len = uac2_ctl_value_size(cval->val_type);
475 
476                 /* FIXME */
477                 if (request != UAC_SET_CUR) {
478                         usb_audio_dbg(chip, "RANGE setting not yet supported\n");
479                         return -EINVAL;
480                 }
481 
482                 request = UAC2_CS_CUR;
483         }
484 
485         value_set = convert_bytes_value(cval, value_set);
486         buf[0] = value_set & 0xff;
487         buf[1] = (value_set >> 8) & 0xff;
488         buf[2] = (value_set >> 16) & 0xff;
489         buf[3] = (value_set >> 24) & 0xff;
490 
491         err = snd_usb_lock_shutdown(chip);
492         if (err < 0)
493                 return -EIO;
494 
495         while (timeout-- > 0) {
496                 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
497                 err = snd_usb_ctl_msg(chip->dev,
498                                       usb_sndctrlpipe(chip->dev, 0), request,
499                                       USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500                                       validx, idx, buf, val_len);
501                 if (err >= 0) {
502                         err = 0;
503                         goto out;
504                 } else if (err == -ETIMEDOUT) {
505                         goto out;
506                 }
507         }
508         usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
509                       request, validx, idx, cval->val_type, buf[0], buf[1]);
510         err = -EINVAL;
511 
512  out:
513         snd_usb_unlock_shutdown(chip);
514         return err;
515 }
516 
517 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518                              int validx, int value)
519 {
520         return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
521 }
522 
523 int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
524                              int index, int value)
525 {
526         int err;
527         unsigned int read_only = (channel == 0) ?
528                 cval->master_readonly :
529                 cval->ch_readonly & (1 << (channel - 1));
530 
531         if (read_only) {
532                 usb_audio_dbg(cval->head.mixer->chip,
533                               "%s(): channel %d of control %d is read_only\n",
534                             __func__, channel, cval->control);
535                 return 0;
536         }
537 
538         err = snd_usb_mixer_set_ctl_value(cval,
539                                           UAC_SET_CUR, (cval->control << 8) | channel,
540                                           value);
541         if (err < 0)
542                 return err;
543         cval->cached |= 1 << channel;
544         cval->cache_val[index] = value;
545         return 0;
546 }
547 
548 /*
549  * TLV callback for mixer volume controls
550  */
551 int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
552                          unsigned int size, unsigned int __user *_tlv)
553 {
554         struct usb_mixer_elem_info *cval = kcontrol->private_data;
555         DECLARE_TLV_DB_MINMAX(scale, 0, 0);
556 
557         if (size < sizeof(scale))
558                 return -ENOMEM;
559         if (cval->min_mute)
560                 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
561         scale[2] = cval->dBmin;
562         scale[3] = cval->dBmax;
563         if (copy_to_user(_tlv, scale, sizeof(scale)))
564                 return -EFAULT;
565         return 0;
566 }
567 
568 /*
569  * parser routines begin here...
570  */
571 
572 static int parse_audio_unit(struct mixer_build *state, int unitid);
573 
574 
575 /*
576  * check if the input/output channel routing is enabled on the given bitmap.
577  * used for mixer unit parser
578  */
579 static int check_matrix_bitmap(unsigned char *bmap,
580                                int ich, int och, int num_outs)
581 {
582         int idx = ich * num_outs + och;
583         return bmap[idx >> 3] & (0x80 >> (idx & 7));
584 }
585 
586 /*
587  * add an alsa control element
588  * search and increment the index until an empty slot is found.
589  *
590  * if failed, give up and free the control instance.
591  */
592 
593 int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
594                               struct snd_kcontrol *kctl)
595 {
596         struct usb_mixer_interface *mixer = list->mixer;
597         int err;
598 
599         while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
600                 kctl->id.index++;
601         if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
602                 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
603                               err);
604                 return err;
605         }
606         list->kctl = kctl;
607         list->next_id_elem = mixer->id_elems[list->id];
608         mixer->id_elems[list->id] = list;
609         return 0;
610 }
611 
612 /*
613  * get a terminal name string
614  */
615 
616 static struct iterm_name_combo {
617         int type;
618         char *name;
619 } iterm_names[] = {
620         { 0x0300, "Output" },
621         { 0x0301, "Speaker" },
622         { 0x0302, "Headphone" },
623         { 0x0303, "HMD Audio" },
624         { 0x0304, "Desktop Speaker" },
625         { 0x0305, "Room Speaker" },
626         { 0x0306, "Com Speaker" },
627         { 0x0307, "LFE" },
628         { 0x0600, "External In" },
629         { 0x0601, "Analog In" },
630         { 0x0602, "Digital In" },
631         { 0x0603, "Line" },
632         { 0x0604, "Legacy In" },
633         { 0x0605, "IEC958 In" },
634         { 0x0606, "1394 DA Stream" },
635         { 0x0607, "1394 DV Stream" },
636         { 0x0700, "Embedded" },
637         { 0x0701, "Noise Source" },
638         { 0x0702, "Equalization Noise" },
639         { 0x0703, "CD" },
640         { 0x0704, "DAT" },
641         { 0x0705, "DCC" },
642         { 0x0706, "MiniDisk" },
643         { 0x0707, "Analog Tape" },
644         { 0x0708, "Phonograph" },
645         { 0x0709, "VCR Audio" },
646         { 0x070a, "Video Disk Audio" },
647         { 0x070b, "DVD Audio" },
648         { 0x070c, "TV Tuner Audio" },
649         { 0x070d, "Satellite Rec Audio" },
650         { 0x070e, "Cable Tuner Audio" },
651         { 0x070f, "DSS Audio" },
652         { 0x0710, "Radio Receiver" },
653         { 0x0711, "Radio Transmitter" },
654         { 0x0712, "Multi-Track Recorder" },
655         { 0x0713, "Synthesizer" },
656         { 0 },
657 };
658 
659 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
660                          unsigned char *name, int maxlen, int term_only)
661 {
662         struct iterm_name_combo *names;
663         int len;
664 
665         if (iterm->name) {
666                 len = snd_usb_copy_string_desc(state, iterm->name,
667                                                 name, maxlen);
668                 if (len)
669                         return len;
670         }
671 
672         /* virtual type - not a real terminal */
673         if (iterm->type >> 16) {
674                 if (term_only)
675                         return 0;
676                 switch (iterm->type >> 16) {
677                 case UAC_SELECTOR_UNIT:
678                         strcpy(name, "Selector");
679                         return 8;
680                 case UAC1_PROCESSING_UNIT:
681                         strcpy(name, "Process Unit");
682                         return 12;
683                 case UAC1_EXTENSION_UNIT:
684                         strcpy(name, "Ext Unit");
685                         return 8;
686                 case UAC_MIXER_UNIT:
687                         strcpy(name, "Mixer");
688                         return 5;
689                 default:
690                         return sprintf(name, "Unit %d", iterm->id);
691                 }
692         }
693 
694         switch (iterm->type & 0xff00) {
695         case 0x0100:
696                 strcpy(name, "PCM");
697                 return 3;
698         case 0x0200:
699                 strcpy(name, "Mic");
700                 return 3;
701         case 0x0400:
702                 strcpy(name, "Headset");
703                 return 7;
704         case 0x0500:
705                 strcpy(name, "Phone");
706                 return 5;
707         }
708 
709         for (names = iterm_names; names->type; names++) {
710                 if (names->type == iterm->type) {
711                         strcpy(name, names->name);
712                         return strlen(names->name);
713                 }
714         }
715 
716         return 0;
717 }
718 
719 /*
720  * parse the source unit recursively until it reaches to a terminal
721  * or a branched unit.
722  */
723 static int check_input_term(struct mixer_build *state, int id,
724                             struct usb_audio_term *term)
725 {
726         int err;
727         void *p1;
728 
729         memset(term, 0, sizeof(*term));
730         while ((p1 = find_audio_control_unit(state, id)) != NULL) {
731                 unsigned char *hdr = p1;
732                 term->id = id;
733                 switch (hdr[2]) {
734                 case UAC_INPUT_TERMINAL:
735                         if (state->mixer->protocol == UAC_VERSION_1) {
736                                 struct uac_input_terminal_descriptor *d = p1;
737                                 term->type = le16_to_cpu(d->wTerminalType);
738                                 term->channels = d->bNrChannels;
739                                 term->chconfig = le16_to_cpu(d->wChannelConfig);
740                                 term->name = d->iTerminal;
741                         } else { /* UAC_VERSION_2 */
742                                 struct uac2_input_terminal_descriptor *d = p1;
743 
744                                 /* call recursively to verify that the
745                                  * referenced clock entity is valid */
746                                 err = check_input_term(state, d->bCSourceID, term);
747                                 if (err < 0)
748                                         return err;
749 
750                                 /* save input term properties after recursion,
751                                  * to ensure they are not overriden by the
752                                  * recursion calls */
753                                 term->id = id;
754                                 term->type = le16_to_cpu(d->wTerminalType);
755                                 term->channels = d->bNrChannels;
756                                 term->chconfig = le32_to_cpu(d->bmChannelConfig);
757                                 term->name = d->iTerminal;
758                         }
759                         return 0;
760                 case UAC_FEATURE_UNIT: {
761                         /* the header is the same for v1 and v2 */
762                         struct uac_feature_unit_descriptor *d = p1;
763                         id = d->bSourceID;
764                         break; /* continue to parse */
765                 }
766                 case UAC_MIXER_UNIT: {
767                         struct uac_mixer_unit_descriptor *d = p1;
768                         term->type = d->bDescriptorSubtype << 16; /* virtual type */
769                         term->channels = uac_mixer_unit_bNrChannels(d);
770                         term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
771                         term->name = uac_mixer_unit_iMixer(d);
772                         return 0;
773                 }
774                 case UAC_SELECTOR_UNIT:
775                 case UAC2_CLOCK_SELECTOR: {
776                         struct uac_selector_unit_descriptor *d = p1;
777                         /* call recursively to retrieve the channel info */
778                         err = check_input_term(state, d->baSourceID[0], term);
779                         if (err < 0)
780                                 return err;
781                         term->type = d->bDescriptorSubtype << 16; /* virtual type */
782                         term->id = id;
783                         term->name = uac_selector_unit_iSelector(d);
784                         return 0;
785                 }
786                 case UAC1_PROCESSING_UNIT:
787                 case UAC1_EXTENSION_UNIT:
788                 /* UAC2_PROCESSING_UNIT_V2 */
789                 /* UAC2_EFFECT_UNIT */
790                 case UAC2_EXTENSION_UNIT_V2: {
791                         struct uac_processing_unit_descriptor *d = p1;
792 
793                         if (state->mixer->protocol == UAC_VERSION_2 &&
794                                 hdr[2] == UAC2_EFFECT_UNIT) {
795                                 /* UAC2/UAC1 unit IDs overlap here in an
796                                  * uncompatible way. Ignore this unit for now.
797                                  */
798                                 return 0;
799                         }
800 
801                         if (d->bNrInPins) {
802                                 id = d->baSourceID[0];
803                                 break; /* continue to parse */
804                         }
805                         term->type = d->bDescriptorSubtype << 16; /* virtual type */
806                         term->channels = uac_processing_unit_bNrChannels(d);
807                         term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);
808                         term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);
809                         return 0;
810                 }
811                 case UAC2_CLOCK_SOURCE: {
812                         struct uac_clock_source_descriptor *d = p1;
813                         term->type = d->bDescriptorSubtype << 16; /* virtual type */
814                         term->id = id;
815                         term->name = d->iClockSource;
816                         return 0;
817                 }
818                 default:
819                         return -ENODEV;
820                 }
821         }
822         return -ENODEV;
823 }
824 
825 /*
826  * Feature Unit
827  */
828 
829 /* feature unit control information */
830 struct usb_feature_control_info {
831         const char *name;
832         int type;       /* data type for uac1 */
833         int type_uac2;  /* data type for uac2 if different from uac1, else -1 */
834 };
835 
836 static struct usb_feature_control_info audio_feature_info[] = {
837         { "Mute",                       USB_MIXER_INV_BOOLEAN, -1 },
838         { "Volume",                     USB_MIXER_S16, -1 },
839         { "Tone Control - Bass",        USB_MIXER_S8, -1 },
840         { "Tone Control - Mid",         USB_MIXER_S8, -1 },
841         { "Tone Control - Treble",      USB_MIXER_S8, -1 },
842         { "Graphic Equalizer",          USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */
843         { "Auto Gain Control",          USB_MIXER_BOOLEAN, -1 },
844         { "Delay Control",              USB_MIXER_U16, USB_MIXER_U32 },
845         { "Bass Boost",                 USB_MIXER_BOOLEAN, -1 },
846         { "Loudness",                   USB_MIXER_BOOLEAN, -1 },
847         /* UAC2 specific */
848         { "Input Gain Control",         USB_MIXER_S16, -1 },
849         { "Input Gain Pad Control",     USB_MIXER_S16, -1 },
850         { "Phase Inverter Control",     USB_MIXER_BOOLEAN, -1 },
851 };
852 
853 /* private_free callback */
854 void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
855 {
856         kfree(kctl->private_data);
857         kctl->private_data = NULL;
858 }
859 
860 /*
861  * interface to ALSA control for feature/mixer units
862  */
863 
864 /* volume control quirks */
865 static void volume_control_quirks(struct usb_mixer_elem_info *cval,
866                                   struct snd_kcontrol *kctl)
867 {
868         struct snd_usb_audio *chip = cval->head.mixer->chip;
869         switch (chip->usb_id) {
870         case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
871         case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
872                 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
873                         cval->min = 0x0000;
874                         cval->max = 0xffff;
875                         cval->res = 0x00e6;
876                         break;
877                 }
878                 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
879                     strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
880                         cval->min = 0x00;
881                         cval->max = 0xff;
882                         break;
883                 }
884                 if (strstr(kctl->id.name, "Effect Return") != NULL) {
885                         cval->min = 0xb706;
886                         cval->max = 0xff7b;
887                         cval->res = 0x0073;
888                         break;
889                 }
890                 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
891                         (strstr(kctl->id.name, "Effect Send") != NULL)) {
892                         cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
893                         cval->max = 0xfcfe;
894                         cval->res = 0x0073;
895                 }
896                 break;
897 
898         case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
899         case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
900                 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
901                         usb_audio_info(chip,
902                                        "set quirk for FTU Effect Duration\n");
903                         cval->min = 0x0000;
904                         cval->max = 0x7f00;
905                         cval->res = 0x0100;
906                         break;
907                 }
908                 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
909                     strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
910                         usb_audio_info(chip,
911                                        "set quirks for FTU Effect Feedback/Volume\n");
912                         cval->min = 0x00;
913                         cval->max = 0x7f;
914                         break;
915                 }
916                 break;
917 
918         case USB_ID(0x0d8c, 0x0103):
919                 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
920                         usb_audio_info(chip,
921                                  "set volume quirk for CM102-A+/102S+\n");
922                         cval->min = -256;
923                 }
924                 break;
925 
926         case USB_ID(0x0471, 0x0101):
927         case USB_ID(0x0471, 0x0104):
928         case USB_ID(0x0471, 0x0105):
929         case USB_ID(0x0672, 0x1041):
930         /* quirk for UDA1321/N101.
931          * note that detection between firmware 2.1.1.7 (N101)
932          * and later 2.1.1.21 is not very clear from datasheets.
933          * I hope that the min value is -15360 for newer firmware --jk
934          */
935                 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
936                     cval->min == -15616) {
937                         usb_audio_info(chip,
938                                  "set volume quirk for UDA1321/N101 chip\n");
939                         cval->max = -256;
940                 }
941                 break;
942 
943         case USB_ID(0x046d, 0x09a4):
944                 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
945                         usb_audio_info(chip,
946                                 "set volume quirk for QuickCam E3500\n");
947                         cval->min = 6080;
948                         cval->max = 8768;
949                         cval->res = 192;
950                 }
951                 break;
952 
953         case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
954         case USB_ID(0x046d, 0x0808):
955         case USB_ID(0x046d, 0x0809):
956         case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
957         case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
958         case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
959         case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
960         case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
961         case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
962         case USB_ID(0x046d, 0x0991):
963         case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
964         /* Most audio usb devices lie about volume resolution.
965          * Most Logitech webcams have res = 384.
966          * Probably there is some logitech magic behind this number --fishor
967          */
968                 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
969                         usb_audio_info(chip,
970                                 "set resolution quirk: cval->res = 384\n");
971                         cval->res = 384;
972                 }
973                 break;
974         }
975 }
976 
977 /*
978  * retrieve the minimum and maximum values for the specified control
979  */
980 static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
981                                    int default_min, struct snd_kcontrol *kctl)
982 {
983         /* for failsafe */
984         cval->min = default_min;
985         cval->max = cval->min + 1;
986         cval->res = 1;
987         cval->dBmin = cval->dBmax = 0;
988 
989         if (cval->val_type == USB_MIXER_BOOLEAN ||
990             cval->val_type == USB_MIXER_INV_BOOLEAN) {
991                 cval->initialized = 1;
992         } else {
993                 int minchn = 0;
994                 if (cval->cmask) {
995                         int i;
996                         for (i = 0; i < MAX_CHANNELS; i++)
997                                 if (cval->cmask & (1 << i)) {
998                                         minchn = i + 1;
999                                         break;
1000                                 }
1001                 }
1002                 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1003                     get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
1004                         usb_audio_err(cval->head.mixer->chip,
1005                                       "%d:%d: cannot get min/max values for control %d (id %d)\n",
1006                                    cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1007                                                                cval->control, cval->head.id);
1008                         return -EINVAL;
1009                 }
1010                 if (get_ctl_value(cval, UAC_GET_RES,
1011                                   (cval->control << 8) | minchn,
1012                                   &cval->res) < 0) {
1013                         cval->res = 1;
1014                 } else {
1015                         int last_valid_res = cval->res;
1016 
1017                         while (cval->res > 1) {
1018                                 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
1019                                                                 (cval->control << 8) | minchn,
1020                                                                 cval->res / 2) < 0)
1021                                         break;
1022                                 cval->res /= 2;
1023                         }
1024                         if (get_ctl_value(cval, UAC_GET_RES,
1025                                           (cval->control << 8) | minchn, &cval->res) < 0)
1026                                 cval->res = last_valid_res;
1027                 }
1028                 if (cval->res == 0)
1029                         cval->res = 1;
1030 
1031                 /* Additional checks for the proper resolution
1032                  *
1033                  * Some devices report smaller resolutions than actually
1034                  * reacting.  They don't return errors but simply clip
1035                  * to the lower aligned value.
1036                  */
1037                 if (cval->min + cval->res < cval->max) {
1038                         int last_valid_res = cval->res;
1039                         int saved, test, check;
1040                         get_cur_mix_raw(cval, minchn, &saved);
1041                         for (;;) {
1042                                 test = saved;
1043                                 if (test < cval->max)
1044                                         test += cval->res;
1045                                 else
1046                                         test -= cval->res;
1047                                 if (test < cval->min || test > cval->max ||
1048                                     snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
1049                                     get_cur_mix_raw(cval, minchn, &check)) {
1050                                         cval->res = last_valid_res;
1051                                         break;
1052                                 }
1053                                 if (test == check)
1054                                         break;
1055                                 cval->res *= 2;
1056                         }
1057                         snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
1058                 }
1059 
1060                 cval->initialized = 1;
1061         }
1062 
1063         if (kctl)
1064                 volume_control_quirks(cval, kctl);
1065 
1066         /* USB descriptions contain the dB scale in 1/256 dB unit
1067          * while ALSA TLV contains in 1/100 dB unit
1068          */
1069         cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1070         cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1071         if (cval->dBmin > cval->dBmax) {
1072                 /* something is wrong; assume it's either from/to 0dB */
1073                 if (cval->dBmin < 0)
1074                         cval->dBmax = 0;
1075                 else if (cval->dBmin > 0)
1076                         cval->dBmin = 0;
1077                 if (cval->dBmin > cval->dBmax) {
1078                         /* totally crap, return an error */
1079                         return -EINVAL;
1080                 }
1081         }
1082 
1083         return 0;
1084 }
1085 
1086 #define get_min_max(cval, def)  get_min_max_with_quirks(cval, def, NULL)
1087 
1088 /* get a feature/mixer unit info */
1089 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1090                                   struct snd_ctl_elem_info *uinfo)
1091 {
1092         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1093 
1094         if (cval->val_type == USB_MIXER_BOOLEAN ||
1095             cval->val_type == USB_MIXER_INV_BOOLEAN)
1096                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1097         else
1098                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1099         uinfo->count = cval->channels;
1100         if (cval->val_type == USB_MIXER_BOOLEAN ||
1101             cval->val_type == USB_MIXER_INV_BOOLEAN) {
1102                 uinfo->value.integer.min = 0;
1103                 uinfo->value.integer.max = 1;
1104         } else {
1105                 if (!cval->initialized) {
1106                         get_min_max_with_quirks(cval, 0, kcontrol);
1107                         if (cval->initialized && cval->dBmin >= cval->dBmax) {
1108                                 kcontrol->vd[0].access &= 
1109                                         ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1110                                           SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
1111                                 snd_ctl_notify(cval->head.mixer->chip->card,
1112                                                SNDRV_CTL_EVENT_MASK_INFO,
1113                                                &kcontrol->id);
1114                         }
1115                 }
1116                 uinfo->value.integer.min = 0;
1117                 uinfo->value.integer.max =
1118                         (cval->max - cval->min + cval->res - 1) / cval->res;
1119         }
1120         return 0;
1121 }
1122 
1123 /* get the current value from feature/mixer unit */
1124 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1125                                  struct snd_ctl_elem_value *ucontrol)
1126 {
1127         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1128         int c, cnt, val, err;
1129 
1130         ucontrol->value.integer.value[0] = cval->min;
1131         if (cval->cmask) {
1132                 cnt = 0;
1133                 for (c = 0; c < MAX_CHANNELS; c++) {
1134                         if (!(cval->cmask & (1 << c)))
1135                                 continue;
1136                         err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
1137                         if (err < 0)
1138                                 return filter_error(cval, err);
1139                         val = get_relative_value(cval, val);
1140                         ucontrol->value.integer.value[cnt] = val;
1141                         cnt++;
1142                 }
1143                 return 0;
1144         } else {
1145                 /* master channel */
1146                 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1147                 if (err < 0)
1148                         return filter_error(cval, err);
1149                 val = get_relative_value(cval, val);
1150                 ucontrol->value.integer.value[0] = val;
1151         }
1152         return 0;
1153 }
1154 
1155 /* put the current value to feature/mixer unit */
1156 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1157                                  struct snd_ctl_elem_value *ucontrol)
1158 {
1159         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1160         int c, cnt, val, oval, err;
1161         int changed = 0;
1162 
1163         if (cval->cmask) {
1164                 cnt = 0;
1165                 for (c = 0; c < MAX_CHANNELS; c++) {
1166                         if (!(cval->cmask & (1 << c)))
1167                                 continue;
1168                         err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
1169                         if (err < 0)
1170                                 return filter_error(cval, err);
1171                         val = ucontrol->value.integer.value[cnt];
1172                         val = get_abs_value(cval, val);
1173                         if (oval != val) {
1174                                 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
1175                                 changed = 1;
1176                         }
1177                         cnt++;
1178                 }
1179         } else {
1180                 /* master channel */
1181                 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
1182                 if (err < 0)
1183                         return filter_error(cval, err);
1184                 val = ucontrol->value.integer.value[0];
1185                 val = get_abs_value(cval, val);
1186                 if (val != oval) {
1187                         snd_usb_set_cur_mix_value(cval, 0, 0, val);
1188                         changed = 1;
1189                 }
1190         }
1191         return changed;
1192 }
1193 
1194 static struct snd_kcontrol_new usb_feature_unit_ctl = {
1195         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1196         .name = "", /* will be filled later manually */
1197         .info = mixer_ctl_feature_info,
1198         .get = mixer_ctl_feature_get,
1199         .put = mixer_ctl_feature_put,
1200 };
1201 
1202 /* the read-only variant */
1203 static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1204         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1205         .name = "", /* will be filled later manually */
1206         .info = mixer_ctl_feature_info,
1207         .get = mixer_ctl_feature_get,
1208         .put = NULL,
1209 };
1210 
1211 /*
1212  * This symbol is exported in order to allow the mixer quirks to
1213  * hook up to the standard feature unit control mechanism
1214  */
1215 struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
1216 
1217 /*
1218  * build a feature control
1219  */
1220 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1221 {
1222         return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1223 }
1224 
1225 /*
1226  * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1227  * rename it to "Headphone". We determine if something is a headphone
1228  * similar to how udev determines form factor.
1229  */
1230 static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1231                                         struct snd_card *card)
1232 {
1233         const char *names_to_check[] = {
1234                 "Headset", "headset", "Headphone", "headphone", NULL};
1235         const char **s;
1236         bool found = false;
1237 
1238         if (strcmp("Speaker", kctl->id.name))
1239                 return;
1240 
1241         for (s = names_to_check; *s; s++)
1242                 if (strstr(card->shortname, *s)) {
1243                         found = true;
1244                         break;
1245                 }
1246 
1247         if (!found)
1248                 return;
1249 
1250         strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1251 }
1252 
1253 static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1254                               unsigned int ctl_mask, int control,
1255                               struct usb_audio_term *iterm, int unitid,
1256                               int readonly_mask)
1257 {
1258         struct uac_feature_unit_descriptor *desc = raw_desc;
1259         struct usb_feature_control_info *ctl_info;
1260         unsigned int len = 0;
1261         int mapped_name = 0;
1262         int nameid = uac_feature_unit_iFeature(desc);
1263         struct snd_kcontrol *kctl;
1264         struct usb_mixer_elem_info *cval;
1265         const struct usbmix_name_map *map;
1266         unsigned int range;
1267 
1268         control++; /* change from zero-based to 1-based value */
1269 
1270         if (control == UAC_FU_GRAPHIC_EQUALIZER) {
1271                 /* FIXME: not supported yet */
1272                 return;
1273         }
1274 
1275         map = find_map(state, unitid, control);
1276         if (check_ignored_ctl(map))
1277                 return;
1278 
1279         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1280         if (!cval)
1281                 return;
1282         snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
1283         cval->control = control;
1284         cval->cmask = ctl_mask;
1285         ctl_info = &audio_feature_info[control-1];
1286         if (state->mixer->protocol == UAC_VERSION_1)
1287                 cval->val_type = ctl_info->type;
1288         else /* UAC_VERSION_2 */
1289                 cval->val_type = ctl_info->type_uac2 >= 0 ?
1290                         ctl_info->type_uac2 : ctl_info->type;
1291 
1292         if (ctl_mask == 0) {
1293                 cval->channels = 1;     /* master channel */
1294                 cval->master_readonly = readonly_mask;
1295         } else {
1296                 int i, c = 0;
1297                 for (i = 0; i < 16; i++)
1298                         if (ctl_mask & (1 << i))
1299                                 c++;
1300                 cval->channels = c;
1301                 cval->ch_readonly = readonly_mask;
1302         }
1303 
1304         /*
1305          * If all channels in the mask are marked read-only, make the control
1306          * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
1307          * issue write commands to read-only channels.
1308          */
1309         if (cval->channels == readonly_mask)
1310                 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1311         else
1312                 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1313 
1314         if (!kctl) {
1315                 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1316                 kfree(cval);
1317                 return;
1318         }
1319         kctl->private_free = snd_usb_mixer_elem_free;
1320 
1321         len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1322         mapped_name = len != 0;
1323         if (!len && nameid)
1324                 len = snd_usb_copy_string_desc(state, nameid,
1325                                 kctl->id.name, sizeof(kctl->id.name));
1326 
1327         switch (control) {
1328         case UAC_FU_MUTE:
1329         case UAC_FU_VOLUME:
1330                 /*
1331                  * determine the control name.  the rule is:
1332                  * - if a name id is given in descriptor, use it.
1333                  * - if the connected input can be determined, then use the name
1334                  *   of terminal type.
1335                  * - if the connected output can be determined, use it.
1336                  * - otherwise, anonymous name.
1337                  */
1338                 if (!len) {
1339                         len = get_term_name(state, iterm, kctl->id.name,
1340                                             sizeof(kctl->id.name), 1);
1341                         if (!len)
1342                                 len = get_term_name(state, &state->oterm,
1343                                                     kctl->id.name,
1344                                                     sizeof(kctl->id.name), 1);
1345                         if (!len)
1346                                 snprintf(kctl->id.name, sizeof(kctl->id.name),
1347                                          "Feature %d", unitid);
1348                 }
1349 
1350                 if (!mapped_name)
1351                         check_no_speaker_on_headset(kctl, state->mixer->chip->card);
1352 
1353                 /*
1354                  * determine the stream direction:
1355                  * if the connected output is USB stream, then it's likely a
1356                  * capture stream.  otherwise it should be playback (hopefully :)
1357                  */
1358                 if (!mapped_name && !(state->oterm.type >> 16)) {
1359                         if ((state->oterm.type & 0xff00) == 0x0100)
1360                                 append_ctl_name(kctl, " Capture");
1361                         else
1362                                 append_ctl_name(kctl, " Playback");
1363                 }
1364                 append_ctl_name(kctl, control == UAC_FU_MUTE ?
1365                                 " Switch" : " Volume");
1366                 break;
1367         default:
1368                 if (!len)
1369                         strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1370                                 sizeof(kctl->id.name));
1371                 break;
1372         }
1373 
1374         /* get min/max values */
1375         get_min_max_with_quirks(cval, 0, kctl);
1376 
1377         if (control == UAC_FU_VOLUME) {
1378                 check_mapped_dB(map, cval);
1379                 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1380                         kctl->tlv.c = snd_usb_mixer_vol_tlv;
1381                         kctl->vd[0].access |=
1382                                 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1383                                 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1384                 }
1385         }
1386 
1387         snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
1388 
1389         range = (cval->max - cval->min) / cval->res;
1390         /*
1391          * Are there devices with volume range more than 255? I use a bit more
1392          * to be sure. 384 is a resolution magic number found on Logitech
1393          * devices. It will definitively catch all buggy Logitech devices.
1394          */
1395         if (range > 384) {
1396                 usb_audio_warn(state->chip,
1397                                "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
1398                                range);
1399                 usb_audio_warn(state->chip,
1400                                "[%d] FU [%s] ch = %d, val = %d/%d/%d",
1401                                cval->head.id, kctl->id.name, cval->channels,
1402                                cval->min, cval->max, cval->res);
1403         }
1404 
1405         usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1406                       cval->head.id, kctl->id.name, cval->channels,
1407                       cval->min, cval->max, cval->res);
1408         snd_usb_mixer_add_control(&cval->head, kctl);
1409 }
1410 
1411 static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1412                                    void *_ftr)
1413 {
1414         struct uac_clock_source_descriptor *hdr = _ftr;
1415         struct usb_mixer_elem_info *cval;
1416         struct snd_kcontrol *kctl;
1417         char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1418         int ret;
1419 
1420         if (state->mixer->protocol != UAC_VERSION_2)
1421                 return -EINVAL;
1422 
1423         if (hdr->bLength != sizeof(*hdr)) {
1424                 usb_audio_dbg(state->chip,
1425                               "Bogus clock source descriptor length of %d, ignoring.\n",
1426                               hdr->bLength);
1427                 return 0;
1428         }
1429 
1430         /*
1431          * The only property of this unit we are interested in is the
1432          * clock source validity. If that isn't readable, just bail out.
1433          */
1434         if (!uac2_control_is_readable(hdr->bmControls,
1435                                       ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1436                 return 0;
1437 
1438         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1439         if (!cval)
1440                 return -ENOMEM;
1441 
1442         snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1443 
1444         cval->min = 0;
1445         cval->max = 1;
1446         cval->channels = 1;
1447         cval->val_type = USB_MIXER_BOOLEAN;
1448         cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1449 
1450         if (uac2_control_is_writeable(hdr->bmControls,
1451                                       ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
1452                 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1453         else {
1454                 cval->master_readonly = 1;
1455                 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1456         }
1457 
1458         if (!kctl) {
1459                 kfree(cval);
1460                 return -ENOMEM;
1461         }
1462 
1463         kctl->private_free = snd_usb_mixer_elem_free;
1464         ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
1465                                        name, sizeof(name));
1466         if (ret > 0)
1467                 snprintf(kctl->id.name, sizeof(kctl->id.name),
1468                          "%s Validity", name);
1469         else
1470                 snprintf(kctl->id.name, sizeof(kctl->id.name),
1471                          "Clock Source %d Validity", hdr->bClockID);
1472 
1473         return snd_usb_mixer_add_control(&cval->head, kctl);
1474 }
1475 
1476 /*
1477  * parse a feature unit
1478  *
1479  * most of controls are defined here.
1480  */
1481 static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1482                                     void *_ftr)
1483 {
1484         int channels, i, j;
1485         struct usb_audio_term iterm;
1486         unsigned int master_bits, first_ch_bits;
1487         int err, csize;
1488         struct uac_feature_unit_descriptor *hdr = _ftr;
1489         __u8 *bmaControls;
1490 
1491         if (state->mixer->protocol == UAC_VERSION_1) {
1492                 if (hdr->bLength < 7) {
1493                         usb_audio_err(state->chip,
1494                                       "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1495                                       unitid);
1496                         return -EINVAL;
1497                 }
1498                 csize = hdr->bControlSize;
1499                 if (!csize) {
1500                         usb_audio_dbg(state->chip,
1501                                       "unit %u: invalid bControlSize == 0\n",
1502                                       unitid);
1503                         return -EINVAL;
1504                 }
1505                 channels = (hdr->bLength - 7) / csize - 1;
1506                 bmaControls = hdr->bmaControls;
1507                 if (hdr->bLength < 7 + csize) {
1508                         usb_audio_err(state->chip,
1509                                       "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1510                                       unitid);
1511                         return -EINVAL;
1512                 }
1513         } else {
1514                 struct uac2_feature_unit_descriptor *ftr = _ftr;
1515                 if (hdr->bLength < 6) {
1516                         usb_audio_err(state->chip,
1517                                       "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1518                                       unitid);
1519                         return -EINVAL;
1520                 }
1521                 csize = 4;
1522                 channels = (hdr->bLength - 6) / 4 - 1;
1523                 bmaControls = ftr->bmaControls;
1524                 if (hdr->bLength < 6 + csize) {
1525                         usb_audio_err(state->chip,
1526                                       "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1527                                       unitid);
1528                         return -EINVAL;
1529                 }
1530         }
1531 
1532         /* parse the source unit */
1533         if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
1534                 return err;
1535 
1536         /* determine the input source type and name */
1537         err = check_input_term(state, hdr->bSourceID, &iterm);
1538         if (err < 0)
1539                 return err;
1540 
1541         master_bits = snd_usb_combine_bytes(bmaControls, csize);
1542         /* master configuration quirks */
1543         switch (state->chip->usb_id) {
1544         case USB_ID(0x08bb, 0x2702):
1545                 usb_audio_info(state->chip,
1546                                "usbmixer: master volume quirk for PCM2702 chip\n");
1547                 /* disable non-functional volume control */
1548                 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
1549                 break;
1550         case USB_ID(0x1130, 0xf211):
1551                 usb_audio_info(state->chip,
1552                                "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
1553                 /* disable non-functional volume control */
1554                 channels = 0;
1555                 break;
1556 
1557         }
1558         if (channels > 0)
1559                 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
1560         else
1561                 first_ch_bits = 0;
1562 
1563         if (state->mixer->protocol == UAC_VERSION_1) {
1564                 /* check all control types */
1565                 for (i = 0; i < 10; i++) {
1566                         unsigned int ch_bits = 0;
1567                         for (j = 0; j < channels; j++) {
1568                                 unsigned int mask;
1569 
1570                                 mask = snd_usb_combine_bytes(bmaControls +
1571                                                              csize * (j+1), csize);
1572                                 if (mask & (1 << i))
1573                                         ch_bits |= (1 << j);
1574                         }
1575                         /* audio class v1 controls are never read-only */
1576 
1577                         /*
1578                          * The first channel must be set
1579                          * (for ease of programming).
1580                          */
1581                         if (ch_bits & 1)
1582                                 build_feature_ctl(state, _ftr, ch_bits, i,
1583                                                   &iterm, unitid, 0);
1584                         if (master_bits & (1 << i))
1585                                 build_feature_ctl(state, _ftr, 0, i, &iterm,
1586                                                   unitid, 0);
1587                 }
1588         } else { /* UAC_VERSION_2 */
1589                 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
1590                         unsigned int ch_bits = 0;
1591                         unsigned int ch_read_only = 0;
1592 
1593                         for (j = 0; j < channels; j++) {
1594                                 unsigned int mask;
1595 
1596                                 mask = snd_usb_combine_bytes(bmaControls +
1597                                                              csize * (j+1), csize);
1598                                 if (uac2_control_is_readable(mask, i)) {
1599                                         ch_bits |= (1 << j);
1600                                         if (!uac2_control_is_writeable(mask, i))
1601                                                 ch_read_only |= (1 << j);
1602                                 }
1603                         }
1604 
1605                         /*
1606                          * NOTE: build_feature_ctl() will mark the control
1607                          * read-only if all channels are marked read-only in
1608                          * the descriptors. Otherwise, the control will be
1609                          * reported as writeable, but the driver will not
1610                          * actually issue a write command for read-only
1611                          * channels.
1612                          */
1613 
1614                         /*
1615                          * The first channel must be set
1616                          * (for ease of programming).
1617                          */
1618                         if (ch_bits & 1)
1619                                 build_feature_ctl(state, _ftr, ch_bits, i,
1620                                                   &iterm, unitid, ch_read_only);
1621                         if (uac2_control_is_readable(master_bits, i))
1622                                 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
1623                                                   !uac2_control_is_writeable(master_bits, i));
1624                 }
1625         }
1626 
1627         return 0;
1628 }
1629 
1630 /*
1631  * Mixer Unit
1632  */
1633 
1634 /*
1635  * build a mixer unit control
1636  *
1637  * the callbacks are identical with feature unit.
1638  * input channel number (zero based) is given in control field instead.
1639  */
1640 static void build_mixer_unit_ctl(struct mixer_build *state,
1641                                  struct uac_mixer_unit_descriptor *desc,
1642                                  int in_pin, int in_ch, int unitid,
1643                                  struct usb_audio_term *iterm)
1644 {
1645         struct usb_mixer_elem_info *cval;
1646         unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
1647         unsigned int i, len;
1648         struct snd_kcontrol *kctl;
1649         const struct usbmix_name_map *map;
1650 
1651         map = find_map(state, unitid, 0);
1652         if (check_ignored_ctl(map))
1653                 return;
1654 
1655         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1656         if (!cval)
1657                 return;
1658 
1659         snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
1660         cval->control = in_ch + 1; /* based on 1 */
1661         cval->val_type = USB_MIXER_S16;
1662         for (i = 0; i < num_outs; i++) {
1663                 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
1664 
1665                 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
1666                         cval->cmask |= (1 << i);
1667                         cval->channels++;
1668                 }
1669         }
1670 
1671         /* get min/max values */
1672         get_min_max(cval, 0);
1673 
1674         kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1675         if (!kctl) {
1676                 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1677                 kfree(cval);
1678                 return;
1679         }
1680         kctl->private_free = snd_usb_mixer_elem_free;
1681 
1682         len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1683         if (!len)
1684                 len = get_term_name(state, iterm, kctl->id.name,
1685                                     sizeof(kctl->id.name), 0);
1686         if (!len)
1687                 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1688         append_ctl_name(kctl, " Volume");
1689 
1690         usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
1691                     cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
1692         snd_usb_mixer_add_control(&cval->head, kctl);
1693 }
1694 
1695 /*
1696  * parse a mixer unit
1697  */
1698 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
1699                                   void *raw_desc)
1700 {
1701         struct uac_mixer_unit_descriptor *desc = raw_desc;
1702         struct usb_audio_term iterm;
1703         int input_pins, num_ins, num_outs;
1704         int pin, ich, err;
1705 
1706         if (desc->bLength < 11 || !(input_pins = desc->bNrInPins) ||
1707             !(num_outs = uac_mixer_unit_bNrChannels(desc))) {
1708                 usb_audio_err(state->chip,
1709                               "invalid MIXER UNIT descriptor %d\n",
1710                               unitid);
1711                 return -EINVAL;
1712         }
1713 
1714         num_ins = 0;
1715         ich = 0;
1716         for (pin = 0; pin < input_pins; pin++) {
1717                 err = parse_audio_unit(state, desc->baSourceID[pin]);
1718                 if (err < 0)
1719                         continue;
1720                 /* no bmControls field (e.g. Maya44) -> ignore */
1721                 if (desc->bLength <= 10 + input_pins)
1722                         continue;
1723                 err = check_input_term(state, desc->baSourceID[pin], &iterm);
1724                 if (err < 0)
1725                         return err;
1726                 num_ins += iterm.channels;
1727                 for (; ich < num_ins; ich++) {
1728                         int och, ich_has_controls = 0;
1729 
1730                         for (och = 0; och < num_outs; och++) {
1731                                 __u8 *c = uac_mixer_unit_bmControls(desc,
1732                                                 state->mixer->protocol);
1733 
1734                                 if (check_matrix_bitmap(c, ich, och, num_outs)) {
1735                                         ich_has_controls = 1;
1736                                         break;
1737                                 }
1738                         }
1739                         if (ich_has_controls)
1740                                 build_mixer_unit_ctl(state, desc, pin, ich,
1741                                                      unitid, &iterm);
1742                 }
1743         }
1744         return 0;
1745 }
1746 
1747 /*
1748  * Processing Unit / Extension Unit
1749  */
1750 
1751 /* get callback for processing/extension unit */
1752 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
1753                                   struct snd_ctl_elem_value *ucontrol)
1754 {
1755         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1756         int err, val;
1757 
1758         err = get_cur_ctl_value(cval, cval->control << 8, &val);
1759         if (err < 0) {
1760                 ucontrol->value.integer.value[0] = cval->min;
1761                 return filter_error(cval, err);
1762         }
1763         val = get_relative_value(cval, val);
1764         ucontrol->value.integer.value[0] = val;
1765         return 0;
1766 }
1767 
1768 /* put callback for processing/extension unit */
1769 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
1770                                   struct snd_ctl_elem_value *ucontrol)
1771 {
1772         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1773         int val, oval, err;
1774 
1775         err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1776         if (err < 0)
1777                 return filter_error(cval, err);
1778         val = ucontrol->value.integer.value[0];
1779         val = get_abs_value(cval, val);
1780         if (val != oval) {
1781                 set_cur_ctl_value(cval, cval->control << 8, val);
1782                 return 1;
1783         }
1784         return 0;
1785 }
1786 
1787 /* alsa control interface for processing/extension unit */
1788 static const struct snd_kcontrol_new mixer_procunit_ctl = {
1789         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1790         .name = "", /* will be filled later */
1791         .info = mixer_ctl_feature_info,
1792         .get = mixer_ctl_procunit_get,
1793         .put = mixer_ctl_procunit_put,
1794 };
1795 
1796 /*
1797  * predefined data for processing units
1798  */
1799 struct procunit_value_info {
1800         int control;
1801         char *suffix;
1802         int val_type;
1803         int min_value;
1804 };
1805 
1806 struct procunit_info {
1807         int type;
1808         char *name;
1809         struct procunit_value_info *values;
1810 };
1811 
1812 static struct procunit_value_info updown_proc_info[] = {
1813         { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
1814         { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
1815         { 0 }
1816 };
1817 static struct procunit_value_info prologic_proc_info[] = {
1818         { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
1819         { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
1820         { 0 }
1821 };
1822 static struct procunit_value_info threed_enh_proc_info[] = {
1823         { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
1824         { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
1825         { 0 }
1826 };
1827 static struct procunit_value_info reverb_proc_info[] = {
1828         { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
1829         { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1830         { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
1831         { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
1832         { 0 }
1833 };
1834 static struct procunit_value_info chorus_proc_info[] = {
1835         { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
1836         { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1837         { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1838         { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1839         { 0 }
1840 };
1841 static struct procunit_value_info dcr_proc_info[] = {
1842         { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
1843         { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
1844         { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
1845         { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1846         { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
1847         { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
1848         { 0 }
1849 };
1850 
1851 static struct procunit_info procunits[] = {
1852         { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
1853         { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1854         { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
1855         { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
1856         { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
1857         { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
1858         { 0 },
1859 };
1860 /*
1861  * predefined data for extension units
1862  */
1863 static struct procunit_value_info clock_rate_xu_info[] = {
1864         { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1865         { 0 }
1866 };
1867 static struct procunit_value_info clock_source_xu_info[] = {
1868         { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1869         { 0 }
1870 };
1871 static struct procunit_value_info spdif_format_xu_info[] = {
1872         { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1873         { 0 }
1874 };
1875 static struct procunit_value_info soft_limit_xu_info[] = {
1876         { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1877         { 0 }
1878 };
1879 static struct procunit_info extunits[] = {
1880         { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1881         { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1882         { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1883         { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1884         { 0 }
1885 };
1886 
1887 /*
1888  * build a processing/extension unit
1889  */
1890 static int build_audio_procunit(struct mixer_build *state, int unitid,
1891                                 void *raw_desc, struct procunit_info *list,
1892                                 char *name)
1893 {
1894         struct uac_processing_unit_descriptor *desc = raw_desc;
1895         int num_ins = desc->bNrInPins;
1896         struct usb_mixer_elem_info *cval;
1897         struct snd_kcontrol *kctl;
1898         int i, err, nameid, type, len;
1899         struct procunit_info *info;
1900         struct procunit_value_info *valinfo;
1901         const struct usbmix_name_map *map;
1902         static struct procunit_value_info default_value_info[] = {
1903                 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1904                 { 0 }
1905         };
1906         static struct procunit_info default_info = {
1907                 0, NULL, default_value_info
1908         };
1909 
1910         if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
1911             desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
1912                 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
1913                 return -EINVAL;
1914         }
1915 
1916         for (i = 0; i < num_ins; i++) {
1917                 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
1918                         return err;
1919         }
1920 
1921         type = le16_to_cpu(desc->wProcessType);
1922         for (info = list; info && info->type; info++)
1923                 if (info->type == type)
1924                         break;
1925         if (!info || !info->type)
1926                 info = &default_info;
1927 
1928         for (valinfo = info->values; valinfo->control; valinfo++) {
1929                 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
1930 
1931                 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
1932                         continue;
1933                 map = find_map(state, unitid, valinfo->control);
1934                 if (check_ignored_ctl(map))
1935                         continue;
1936                 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1937                 if (!cval)
1938                         return -ENOMEM;
1939                 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
1940                 cval->control = valinfo->control;
1941                 cval->val_type = valinfo->val_type;
1942                 cval->channels = 1;
1943 
1944                 /* get min/max values */
1945                 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
1946                         __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
1947                         /* FIXME: hard-coded */
1948                         cval->min = 1;
1949                         cval->max = control_spec[0];
1950                         cval->res = 1;
1951                         cval->initialized = 1;
1952                 } else {
1953                         if (type == USB_XU_CLOCK_RATE) {
1954                                 /*
1955                                  * E-Mu USB 0404/0202/TrackerPre/0204
1956                                  * samplerate control quirk
1957                                  */
1958                                 cval->min = 0;
1959                                 cval->max = 5;
1960                                 cval->res = 1;
1961                                 cval->initialized = 1;
1962                         } else
1963                                 get_min_max(cval, valinfo->min_value);
1964                 }
1965 
1966                 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1967                 if (!kctl) {
1968                         kfree(cval);
1969                         return -ENOMEM;
1970                 }
1971                 kctl->private_free = snd_usb_mixer_elem_free;
1972 
1973                 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
1974                         /* nothing */ ;
1975                 } else if (info->name) {
1976                         strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1977                 } else {
1978                         nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
1979                         len = 0;
1980                         if (nameid)
1981                                 len = snd_usb_copy_string_desc(state, nameid,
1982                                                                kctl->id.name,
1983                                                                sizeof(kctl->id.name));
1984                         if (!len)
1985                                 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1986                 }
1987                 append_ctl_name(kctl, " ");
1988                 append_ctl_name(kctl, valinfo->suffix);
1989 
1990                 usb_audio_dbg(state->chip,
1991                               "[%d] PU [%s] ch = %d, val = %d/%d\n",
1992                               cval->head.id, kctl->id.name, cval->channels,
1993                               cval->min, cval->max);
1994 
1995                 err = snd_usb_mixer_add_control(&cval->head, kctl);
1996                 if (err < 0)
1997                         return err;
1998         }
1999         return 0;
2000 }
2001 
2002 static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2003                                        void *raw_desc)
2004 {
2005         return build_audio_procunit(state, unitid, raw_desc,
2006                                     procunits, "Processing Unit");
2007 }
2008 
2009 static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2010                                       void *raw_desc)
2011 {
2012         /*
2013          * Note that we parse extension units with processing unit descriptors.
2014          * That's ok as the layout is the same.
2015          */
2016         return build_audio_procunit(state, unitid, raw_desc,
2017                                     extunits, "Extension Unit");
2018 }
2019 
2020 /*
2021  * Selector Unit
2022  */
2023 
2024 /*
2025  * info callback for selector unit
2026  * use an enumerator type for routing
2027  */
2028 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2029                                    struct snd_ctl_elem_info *uinfo)
2030 {
2031         struct usb_mixer_elem_info *cval = kcontrol->private_data;
2032         const char **itemlist = (const char **)kcontrol->private_value;
2033 
2034         if (snd_BUG_ON(!itemlist))
2035                 return -EINVAL;
2036         return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
2037 }
2038 
2039 /* get callback for selector unit */
2040 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2041                                   struct snd_ctl_elem_value *ucontrol)
2042 {
2043         struct usb_mixer_elem_info *cval = kcontrol->private_data;
2044         int val, err;
2045 
2046         err = get_cur_ctl_value(cval, cval->control << 8, &val);
2047         if (err < 0) {
2048                 ucontrol->value.enumerated.item[0] = 0;
2049                 return filter_error(cval, err);
2050         }
2051         val = get_relative_value(cval, val);
2052         ucontrol->value.enumerated.item[0] = val;
2053         return 0;
2054 }
2055 
2056 /* put callback for selector unit */
2057 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2058                                   struct snd_ctl_elem_value *ucontrol)
2059 {
2060         struct usb_mixer_elem_info *cval = kcontrol->private_data;
2061         int val, oval, err;
2062 
2063         err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2064         if (err < 0)
2065                 return filter_error(cval, err);
2066         val = ucontrol->value.enumerated.item[0];
2067         val = get_abs_value(cval, val);
2068         if (val != oval) {
2069                 set_cur_ctl_value(cval, cval->control << 8, val);
2070                 return 1;
2071         }
2072         return 0;
2073 }
2074 
2075 /* alsa control interface for selector unit */
2076 static const struct snd_kcontrol_new mixer_selectunit_ctl = {
2077         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2078         .name = "", /* will be filled later */
2079         .info = mixer_ctl_selector_info,
2080         .get = mixer_ctl_selector_get,
2081         .put = mixer_ctl_selector_put,
2082 };
2083 
2084 /*
2085  * private free callback.
2086  * free both private_data and private_value
2087  */
2088 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
2089 {
2090         int i, num_ins = 0;
2091 
2092         if (kctl->private_data) {
2093                 struct usb_mixer_elem_info *cval = kctl->private_data;
2094                 num_ins = cval->max;
2095                 kfree(cval);
2096                 kctl->private_data = NULL;
2097         }
2098         if (kctl->private_value) {
2099                 char **itemlist = (char **)kctl->private_value;
2100                 for (i = 0; i < num_ins; i++)
2101                         kfree(itemlist[i]);
2102                 kfree(itemlist);
2103                 kctl->private_value = 0;
2104         }
2105 }
2106 
2107 /*
2108  * parse a selector unit
2109  */
2110 static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2111                                      void *raw_desc)
2112 {
2113         struct uac_selector_unit_descriptor *desc = raw_desc;
2114         unsigned int i, nameid, len;
2115         int err;
2116         struct usb_mixer_elem_info *cval;
2117         struct snd_kcontrol *kctl;
2118         const struct usbmix_name_map *map;
2119         char **namelist;
2120 
2121         if (desc->bLength < 5 || !desc->bNrInPins ||
2122             desc->bLength < 5 + desc->bNrInPins) {
2123                 usb_audio_err(state->chip,
2124                         "invalid SELECTOR UNIT descriptor %d\n", unitid);
2125                 return -EINVAL;
2126         }
2127 
2128         for (i = 0; i < desc->bNrInPins; i++) {
2129                 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
2130                         return err;
2131         }
2132 
2133         if (desc->bNrInPins == 1) /* only one ? nonsense! */
2134                 return 0;
2135 
2136         map = find_map(state, unitid, 0);
2137         if (check_ignored_ctl(map))
2138                 return 0;
2139 
2140         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2141         if (!cval)
2142                 return -ENOMEM;
2143         snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2144         cval->val_type = USB_MIXER_U8;
2145         cval->channels = 1;
2146         cval->min = 1;
2147         cval->max = desc->bNrInPins;
2148         cval->res = 1;
2149         cval->initialized = 1;
2150 
2151         if (state->mixer->protocol == UAC_VERSION_1)
2152                 cval->control = 0;
2153         else /* UAC_VERSION_2 */
2154                 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
2155                         UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
2156 
2157         namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
2158         if (!namelist) {
2159                 kfree(cval);
2160                 return -ENOMEM;
2161         }
2162 #define MAX_ITEM_NAME_LEN       64
2163         for (i = 0; i < desc->bNrInPins; i++) {
2164                 struct usb_audio_term iterm;
2165                 len = 0;
2166                 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
2167                 if (!namelist[i]) {
2168                         while (i--)
2169                                 kfree(namelist[i]);
2170                         kfree(namelist);
2171                         kfree(cval);
2172                         return -ENOMEM;
2173                 }
2174                 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2175                                                  MAX_ITEM_NAME_LEN);
2176                 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
2177                         len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
2178                 if (! len)
2179                         sprintf(namelist[i], "Input %u", i);
2180         }
2181 
2182         kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2183         if (! kctl) {
2184                 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2185                 kfree(namelist);
2186                 kfree(cval);
2187                 return -ENOMEM;
2188         }
2189         kctl->private_value = (unsigned long)namelist;
2190         kctl->private_free = usb_mixer_selector_elem_free;
2191 
2192         /* check the static mapping table at first */
2193         len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2194         if (!len) {
2195                 /* no mapping ? */
2196                 /* if iSelector is given, use it */
2197                 nameid = uac_selector_unit_iSelector(desc);
2198                 if (nameid)
2199                         len = snd_usb_copy_string_desc(state, nameid,
2200                                                        kctl->id.name,
2201                                                        sizeof(kctl->id.name));
2202                 /* ... or pick up the terminal name at next */
2203                 if (!len)
2204                         len = get_term_name(state, &state->oterm,
2205                                     kctl->id.name, sizeof(kctl->id.name), 0);
2206                 /* ... or use the fixed string "USB" as the last resort */
2207                 if (!len)
2208                         strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2209 
2210                 /* and add the proper suffix */
2211                 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
2212                         append_ctl_name(kctl, " Clock Source");
2213                 else if ((state->oterm.type & 0xff00) == 0x0100)
2214                         append_ctl_name(kctl, " Capture Source");
2215                 else
2216                         append_ctl_name(kctl, " Playback Source");
2217         }
2218 
2219         usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
2220                     cval->head.id, kctl->id.name, desc->bNrInPins);
2221         return snd_usb_mixer_add_control(&cval->head, kctl);
2222 }
2223 
2224 /*
2225  * parse an audio unit recursively
2226  */
2227 
2228 static int parse_audio_unit(struct mixer_build *state, int unitid)
2229 {
2230         unsigned char *p1;
2231 
2232         if (test_and_set_bit(unitid, state->unitbitmap))
2233                 return 0; /* the unit already visited */
2234 
2235         p1 = find_audio_control_unit(state, unitid);
2236         if (!p1) {
2237                 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
2238                 return -EINVAL;
2239         }
2240 
2241         switch (p1[2]) {
2242         case UAC_INPUT_TERMINAL:
2243                 return 0; /* NOP */
2244         case UAC_MIXER_UNIT:
2245                 return parse_audio_mixer_unit(state, unitid, p1);
2246         case UAC2_CLOCK_SOURCE:
2247                 return parse_clock_source_unit(state, unitid, p1);
2248         case UAC_SELECTOR_UNIT:
2249         case UAC2_CLOCK_SELECTOR:
2250                 return parse_audio_selector_unit(state, unitid, p1);
2251         case UAC_FEATURE_UNIT:
2252                 return parse_audio_feature_unit(state, unitid, p1);
2253         case UAC1_PROCESSING_UNIT:
2254         /*   UAC2_EFFECT_UNIT has the same value */
2255                 if (state->mixer->protocol == UAC_VERSION_1)
2256                         return parse_audio_processing_unit(state, unitid, p1);
2257                 else
2258                         return 0; /* FIXME - effect units not implemented yet */
2259         case UAC1_EXTENSION_UNIT:
2260         /*   UAC2_PROCESSING_UNIT_V2 has the same value */
2261                 if (state->mixer->protocol == UAC_VERSION_1)
2262                         return parse_audio_extension_unit(state, unitid, p1);
2263                 else /* UAC_VERSION_2 */
2264                         return parse_audio_processing_unit(state, unitid, p1);
2265         case UAC2_EXTENSION_UNIT_V2:
2266                 return parse_audio_extension_unit(state, unitid, p1);
2267         default:
2268                 usb_audio_err(state->chip,
2269                         "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2270                 return -EINVAL;
2271         }
2272 }
2273 
2274 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2275 {
2276         /* kill pending URBs */
2277         snd_usb_mixer_disconnect(mixer);
2278 
2279         kfree(mixer->id_elems);
2280         if (mixer->urb) {
2281                 kfree(mixer->urb->transfer_buffer);
2282                 usb_free_urb(mixer->urb);
2283         }
2284         usb_free_urb(mixer->rc_urb);
2285         kfree(mixer->rc_setup_packet);
2286         kfree(mixer);
2287 }
2288 
2289 static int snd_usb_mixer_dev_free(struct snd_device *device)
2290 {
2291         struct usb_mixer_interface *mixer = device->device_data;
2292         snd_usb_mixer_free(mixer);
2293         return 0;
2294 }
2295 
2296 /*
2297  * create mixer controls
2298  *
2299  * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
2300  */
2301 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
2302 {
2303         struct mixer_build state;
2304         int err;
2305         const struct usbmix_ctl_map *map;
2306         void *p;
2307 
2308         memset(&state, 0, sizeof(state));
2309         state.chip = mixer->chip;
2310         state.mixer = mixer;
2311         state.buffer = mixer->hostif->extra;
2312         state.buflen = mixer->hostif->extralen;
2313 
2314         /* check the mapping table */
2315         for (map = usbmix_ctl_maps; map->id; map++) {
2316                 if (map->id == state.chip->usb_id) {
2317                         state.map = map->map;
2318                         state.selector_map = map->selector_map;
2319                         mixer->ignore_ctl_error = map->ignore_ctl_error;
2320                         break;
2321                 }
2322         }
2323 
2324         p = NULL;
2325         while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
2326                                             mixer->hostif->extralen,
2327                                             p, UAC_OUTPUT_TERMINAL)) != NULL) {
2328                 if (mixer->protocol == UAC_VERSION_1) {
2329                         struct uac1_output_terminal_descriptor *desc = p;
2330 
2331                         if (desc->bLength < sizeof(*desc))
2332                                 continue; /* invalid descriptor? */
2333                         /* mark terminal ID as visited */
2334                         set_bit(desc->bTerminalID, state.unitbitmap);
2335                         state.oterm.id = desc->bTerminalID;
2336                         state.oterm.type = le16_to_cpu(desc->wTerminalType);
2337                         state.oterm.name = desc->iTerminal;
2338                         err = parse_audio_unit(&state, desc->bSourceID);
2339                         if (err < 0 && err != -EINVAL)
2340                                 return err;
2341                 } else { /* UAC_VERSION_2 */
2342                         struct uac2_output_terminal_descriptor *desc = p;
2343 
2344                         if (desc->bLength < sizeof(*desc))
2345                                 continue; /* invalid descriptor? */
2346                         /* mark terminal ID as visited */
2347                         set_bit(desc->bTerminalID, state.unitbitmap);
2348                         state.oterm.id = desc->bTerminalID;
2349                         state.oterm.type = le16_to_cpu(desc->wTerminalType);
2350                         state.oterm.name = desc->iTerminal;
2351                         err = parse_audio_unit(&state, desc->bSourceID);
2352                         if (err < 0 && err != -EINVAL)
2353                                 return err;
2354 
2355                         /*
2356                          * For UAC2, use the same approach to also add the
2357                          * clock selectors
2358                          */
2359                         err = parse_audio_unit(&state, desc->bCSourceID);
2360                         if (err < 0 && err != -EINVAL)
2361                                 return err;
2362                 }
2363         }
2364 
2365         return 0;
2366 }
2367 
2368 void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
2369 {
2370         struct usb_mixer_elem_list *list;
2371 
2372         for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
2373                 struct usb_mixer_elem_info *info =
2374                         (struct usb_mixer_elem_info *)list;
2375                 /* invalidate cache, so the value is read from the device */
2376                 info->cached = 0;
2377                 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2378                                &list->kctl->id);
2379         }
2380 }
2381 
2382 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
2383                                     struct usb_mixer_elem_list *list)
2384 {
2385         struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
2386         static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
2387                                     "S8", "U8", "S16", "U16"};
2388         snd_iprintf(buffer, "    Info: id=%i, control=%i, cmask=0x%x, "
2389                             "channels=%i, type=\"%s\"\n", cval->head.id,
2390                             cval->control, cval->cmask, cval->channels,
2391                             val_types[cval->val_type]);
2392         snd_iprintf(buffer, "    Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
2393                             cval->min, cval->max, cval->dBmin, cval->dBmax);
2394 }
2395 
2396 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
2397                                     struct snd_info_buffer *buffer)
2398 {
2399         struct snd_usb_audio *chip = entry->private_data;
2400         struct usb_mixer_interface *mixer;
2401         struct usb_mixer_elem_list *list;
2402         int unitid;
2403 
2404         list_for_each_entry(mixer, &chip->mixer_list, list) {
2405                 snd_iprintf(buffer,
2406                         "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
2407                                 chip->usb_id, snd_usb_ctrl_intf(chip),
2408                                 mixer->ignore_ctl_error);
2409                 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
2410                 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
2411                         for (list = mixer->id_elems[unitid]; list;
2412                              list = list->next_id_elem) {
2413                                 snd_iprintf(buffer, "  Unit: %i\n", list->id);
2414                                 if (list->kctl)
2415                                         snd_iprintf(buffer,
2416                                                     "    Control: name=\"%s\", index=%i\n",
2417                                                     list->kctl->id.name,
2418                                                     list->kctl->id.index);
2419                                 if (list->dump)
2420                                         list->dump(buffer, list);
2421                         }
2422                 }
2423         }
2424 }
2425 
2426 static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
2427                                        int attribute, int value, int index)
2428 {
2429         struct usb_mixer_elem_list *list;
2430         __u8 unitid = (index >> 8) & 0xff;
2431         __u8 control = (value >> 8) & 0xff;
2432         __u8 channel = value & 0xff;
2433         unsigned int count = 0;
2434 
2435         if (channel >= MAX_CHANNELS) {
2436                 usb_audio_dbg(mixer->chip,
2437                         "%s(): bogus channel number %d\n",
2438                         __func__, channel);
2439                 return;
2440         }
2441 
2442         for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
2443                 count++;
2444 
2445         if (count == 0)
2446                 return;
2447 
2448         for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
2449                 struct usb_mixer_elem_info *info;
2450 
2451                 if (!list->kctl)
2452                         continue;
2453 
2454                 info = (struct usb_mixer_elem_info *)list;
2455                 if (count > 1 && info->control != control)
2456                         continue;
2457 
2458                 switch (attribute) {
2459                 case UAC2_CS_CUR:
2460                         /* invalidate cache, so the value is read from the device */
2461                         if (channel)
2462                                 info->cached &= ~(1 << channel);
2463                         else /* master channel */
2464                                 info->cached = 0;
2465 
2466                         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2467                                        &info->head.kctl->id);
2468                         break;
2469 
2470                 case UAC2_CS_RANGE:
2471                         /* TODO */
2472                         break;
2473 
2474                 case UAC2_CS_MEM:
2475                         /* TODO */
2476                         break;
2477 
2478                 default:
2479                         usb_audio_dbg(mixer->chip,
2480                                 "unknown attribute %d in interrupt\n",
2481                                 attribute);
2482                         break;
2483                 } /* switch */
2484         }
2485 }
2486 
2487 static void snd_usb_mixer_interrupt(struct urb *urb)
2488 {
2489         struct usb_mixer_interface *mixer = urb->context;
2490         int len = urb->actual_length;
2491         int ustatus = urb->status;
2492 
2493         if (ustatus != 0)
2494                 goto requeue;
2495 
2496         if (mixer->protocol == UAC_VERSION_1) {
2497                 struct uac1_status_word *status;
2498 
2499                 for (status = urb->transfer_buffer;
2500                      len >= sizeof(*status);
2501                      len -= sizeof(*status), status++) {
2502                         dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
2503                                                 status->bStatusType,
2504                                                 status->bOriginator);
2505 
2506                         /* ignore any notifications not from the control interface */
2507                         if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
2508                                 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
2509                                 continue;
2510 
2511                         if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
2512                                 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
2513                         else
2514                                 snd_usb_mixer_notify_id(mixer, status->bOriginator);
2515                 }
2516         } else { /* UAC_VERSION_2 */
2517                 struct uac2_interrupt_data_msg *msg;
2518 
2519                 for (msg = urb->transfer_buffer;
2520                      len >= sizeof(*msg);
2521                      len -= sizeof(*msg), msg++) {
2522                         /* drop vendor specific and endpoint requests */
2523                         if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
2524                             (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
2525                                 continue;
2526 
2527                         snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
2528                                                    le16_to_cpu(msg->wValue),
2529                                                    le16_to_cpu(msg->wIndex));
2530                 }
2531         }
2532 
2533 requeue:
2534         if (ustatus != -ENOENT &&
2535             ustatus != -ECONNRESET &&
2536             ustatus != -ESHUTDOWN) {
2537                 urb->dev = mixer->chip->dev;
2538                 usb_submit_urb(urb, GFP_ATOMIC);
2539         }
2540 }
2541 
2542 /* create the handler for the optional status interrupt endpoint */
2543 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
2544 {
2545         struct usb_endpoint_descriptor *ep;
2546         void *transfer_buffer;
2547         int buffer_length;
2548         unsigned int epnum;
2549 
2550         /* we need one interrupt input endpoint */
2551         if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
2552                 return 0;
2553         ep = get_endpoint(mixer->hostif, 0);
2554         if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
2555                 return 0;
2556 
2557         epnum = usb_endpoint_num(ep);
2558         buffer_length = le16_to_cpu(ep->wMaxPacketSize);
2559         transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
2560         if (!transfer_buffer)
2561                 return -ENOMEM;
2562         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2563         if (!mixer->urb) {
2564                 kfree(transfer_buffer);
2565                 return -ENOMEM;
2566         }
2567         usb_fill_int_urb(mixer->urb, mixer->chip->dev,
2568                          usb_rcvintpipe(mixer->chip->dev, epnum),
2569                          transfer_buffer, buffer_length,
2570                          snd_usb_mixer_interrupt, mixer, ep->bInterval);
2571         usb_submit_urb(mixer->urb, GFP_KERNEL);
2572         return 0;
2573 }
2574 
2575 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2576                          int ignore_error)
2577 {
2578         static struct snd_device_ops dev_ops = {
2579                 .dev_free = snd_usb_mixer_dev_free
2580         };
2581         struct usb_mixer_interface *mixer;
2582         struct snd_info_entry *entry;
2583         int err;
2584 
2585         strcpy(chip->card->mixername, "USB Mixer");
2586 
2587         mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2588         if (!mixer)
2589                 return -ENOMEM;
2590         mixer->chip = chip;
2591         mixer->ignore_ctl_error = ignore_error;
2592         mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2593                                   GFP_KERNEL);
2594         if (!mixer->id_elems) {
2595                 kfree(mixer);
2596                 return -ENOMEM;
2597         }
2598 
2599         mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2600         switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
2601         case UAC_VERSION_1:
2602         default:
2603                 mixer->protocol = UAC_VERSION_1;
2604                 break;
2605         case UAC_VERSION_2:
2606                 mixer->protocol = UAC_VERSION_2;
2607                 break;
2608         }
2609 
2610         if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2611             (err = snd_usb_mixer_status_create(mixer)) < 0)
2612                 goto _error;
2613 
2614         snd_usb_mixer_apply_create_quirk(mixer);
2615 
2616         err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
2617         if (err < 0)
2618                 goto _error;
2619 
2620         if (list_empty(&chip->mixer_list) &&
2621             !snd_card_proc_new(chip->card, "usbmixer", &entry))
2622                 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2623 
2624         list_add(&mixer->list, &chip->mixer_list);
2625         return 0;
2626 
2627 _error:
2628         snd_usb_mixer_free(mixer);
2629         return err;
2630 }
2631 
2632 void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
2633 {
2634         if (mixer->disconnected)
2635                 return;
2636         if (mixer->urb)
2637                 usb_kill_urb(mixer->urb);
2638         if (mixer->rc_urb)
2639                 usb_kill_urb(mixer->rc_urb);
2640         mixer->disconnected = true;
2641 }
2642 
2643 #ifdef CONFIG_PM
2644 /* stop any bus activity of a mixer */
2645 static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
2646 {
2647         usb_kill_urb(mixer->urb);
2648         usb_kill_urb(mixer->rc_urb);
2649 }
2650 
2651 static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
2652 {
2653         int err;
2654 
2655         if (mixer->urb) {
2656                 err = usb_submit_urb(mixer->urb, GFP_NOIO);
2657                 if (err < 0)
2658                         return err;
2659         }
2660 
2661         return 0;
2662 }
2663 
2664 int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
2665 {
2666         snd_usb_mixer_inactivate(mixer);
2667         return 0;
2668 }
2669 
2670 static int restore_mixer_value(struct usb_mixer_elem_list *list)
2671 {
2672         struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
2673         int c, err, idx;
2674 
2675         if (cval->cmask) {
2676                 idx = 0;
2677                 for (c = 0; c < MAX_CHANNELS; c++) {
2678                         if (!(cval->cmask & (1 << c)))
2679                                 continue;
2680                         if (cval->cached & (1 << (c + 1))) {
2681                                 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
2682                                                         cval->cache_val[idx]);
2683                                 if (err < 0)
2684                                         return err;
2685                         }
2686                         idx++;
2687                 }
2688         } else {
2689                 /* master */
2690                 if (cval->cached) {
2691                         err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
2692                         if (err < 0)
2693                                 return err;
2694                 }
2695         }
2696 
2697         return 0;
2698 }
2699 
2700 int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
2701 {
2702         struct usb_mixer_elem_list *list;
2703         int id, err;
2704 
2705         if (reset_resume) {
2706                 /* restore cached mixer values */
2707                 for (id = 0; id < MAX_ID_ELEMS; id++) {
2708                         for (list = mixer->id_elems[id]; list;
2709                              list = list->next_id_elem) {
2710                                 if (list->resume) {
2711                                         err = list->resume(list);
2712                                         if (err < 0)
2713                                                 return err;
2714                                 }
2715                         }
2716                 }
2717         }
2718 
2719         return snd_usb_mixer_activate(mixer);
2720 }
2721 #endif
2722 
2723 void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
2724                                  struct usb_mixer_interface *mixer,
2725                                  int unitid)
2726 {
2727         list->mixer = mixer;
2728         list->id = unitid;
2729         list->dump = snd_usb_mixer_dump_cval;
2730 #ifdef CONFIG_PM
2731         list->resume = restore_mixer_value;
2732 #endif
2733 }
2734 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp