linux/sound/usb/mixer_quirks.c
<<
>>
Prefs
   1/*
   2 *   USB Audio Driver for ALSA
   3 *
   4 *   Quirks and vendor-specific extensions for mixer interfaces
   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 *   Audio Advantage Micro II support added by:
  13 *          Przemek Rudy (prudy1@o2.pl)
  14 *
  15 *   This program is free software; you can redistribute it and/or modify
  16 *   it under the terms of the GNU General Public License as published by
  17 *   the Free Software Foundation; either version 2 of the License, or
  18 *   (at your option) any later version.
  19 *
  20 *   This program is distributed in the hope that it will be useful,
  21 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  22 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23 *   GNU General Public License for more details.
  24 *
  25 *   You should have received a copy of the GNU General Public License
  26 *   along with this program; if not, write to the Free Software
  27 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  28 */
  29
  30#include <linux/hid.h>
  31#include <linux/init.h>
  32#include <linux/math64.h>
  33#include <linux/slab.h>
  34#include <linux/usb.h>
  35#include <linux/usb/audio.h>
  36
  37#include <sound/asoundef.h>
  38#include <sound/core.h>
  39#include <sound/control.h>
  40#include <sound/hwdep.h>
  41#include <sound/info.h>
  42#include <sound/tlv.h>
  43
  44#include "usbaudio.h"
  45#include "mixer.h"
  46#include "mixer_quirks.h"
  47#include "mixer_scarlett.h"
  48#include "mixer_us16x08.h"
  49#include "helper.h"
  50
  51struct std_mono_table {
  52        unsigned int unitid, control, cmask;
  53        int val_type;
  54        const char *name;
  55        snd_kcontrol_tlv_rw_t *tlv_callback;
  56};
  57
  58/* This function allows for the creation of standard UAC controls.
  59 * See the quirks for M-Audio FTUs or Ebox-44.
  60 * If you don't want to set a TLV callback pass NULL.
  61 *
  62 * Since there doesn't seem to be a devices that needs a multichannel
  63 * version, we keep it mono for simplicity.
  64 */
  65static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
  66                                unsigned int unitid,
  67                                unsigned int control,
  68                                unsigned int cmask,
  69                                int val_type,
  70                                unsigned int idx_off,
  71                                const char *name,
  72                                snd_kcontrol_tlv_rw_t *tlv_callback)
  73{
  74        struct usb_mixer_elem_info *cval;
  75        struct snd_kcontrol *kctl;
  76
  77        cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  78        if (!cval)
  79                return -ENOMEM;
  80
  81        snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
  82        cval->val_type = val_type;
  83        cval->channels = 1;
  84        cval->control = control;
  85        cval->cmask = cmask;
  86        cval->idx_off = idx_off;
  87
  88        /* get_min_max() is called only for integer volumes later,
  89         * so provide a short-cut for booleans */
  90        cval->min = 0;
  91        cval->max = 1;
  92        cval->res = 0;
  93        cval->dBmin = 0;
  94        cval->dBmax = 0;
  95
  96        /* Create control */
  97        kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
  98        if (!kctl) {
  99                kfree(cval);
 100                return -ENOMEM;
 101        }
 102
 103        /* Set name */
 104        snprintf(kctl->id.name, sizeof(kctl->id.name), name);
 105        kctl->private_free = snd_usb_mixer_elem_free;
 106
 107        /* set TLV */
 108        if (tlv_callback) {
 109                kctl->tlv.c = tlv_callback;
 110                kctl->vd[0].access |=
 111                        SNDRV_CTL_ELEM_ACCESS_TLV_READ |
 112                        SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
 113        }
 114        /* Add control to mixer */
 115        return snd_usb_mixer_add_control(&cval->head, kctl);
 116}
 117
 118static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
 119                                unsigned int unitid,
 120                                unsigned int control,
 121                                unsigned int cmask,
 122                                int val_type,
 123                                const char *name,
 124                                snd_kcontrol_tlv_rw_t *tlv_callback)
 125{
 126        return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
 127                val_type, 0 /* Offset */, name, tlv_callback);
 128}
 129
 130/*
 131 * Create a set of standard UAC controls from a table
 132 */
 133static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
 134                                struct std_mono_table *t)
 135{
 136        int err;
 137
 138        while (t->name != NULL) {
 139                err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
 140                                t->cmask, t->val_type, t->name, t->tlv_callback);
 141                if (err < 0)
 142                        return err;
 143                t++;
 144        }
 145
 146        return 0;
 147}
 148
 149static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
 150                                      int id,
 151                                      usb_mixer_elem_resume_func_t resume,
 152                                      const struct snd_kcontrol_new *knew,
 153                                      struct usb_mixer_elem_list **listp)
 154{
 155        struct usb_mixer_elem_list *list;
 156        struct snd_kcontrol *kctl;
 157
 158        list = kzalloc(sizeof(*list), GFP_KERNEL);
 159        if (!list)
 160                return -ENOMEM;
 161        if (listp)
 162                *listp = list;
 163        list->mixer = mixer;
 164        list->id = id;
 165        list->resume = resume;
 166        kctl = snd_ctl_new1(knew, list);
 167        if (!kctl) {
 168                kfree(list);
 169                return -ENOMEM;
 170        }
 171        kctl->private_free = snd_usb_mixer_elem_free;
 172        return snd_usb_mixer_add_control(list, kctl);
 173}
 174
 175/*
 176 * Sound Blaster remote control configuration
 177 *
 178 * format of remote control data:
 179 * Extigy:       xx 00
 180 * Audigy 2 NX:  06 80 xx 00 00 00
 181 * Live! 24-bit: 06 80 xx yy 22 83
 182 */
 183static const struct rc_config {
 184        u32 usb_id;
 185        u8  offset;
 186        u8  length;
 187        u8  packet_length;
 188        u8  min_packet_length; /* minimum accepted length of the URB result */
 189        u8  mute_mixer_id;
 190        u32 mute_code;
 191} rc_configs[] = {
 192        { USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
 193        { USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
 194        { USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
 195        { USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
 196        { USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
 197        { USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
 198        { USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
 199};
 200
 201static void snd_usb_soundblaster_remote_complete(struct urb *urb)
 202{
 203        struct usb_mixer_interface *mixer = urb->context;
 204        const struct rc_config *rc = mixer->rc_cfg;
 205        u32 code;
 206
 207        if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
 208                return;
 209
 210        code = mixer->rc_buffer[rc->offset];
 211        if (rc->length == 2)
 212                code |= mixer->rc_buffer[rc->offset + 1] << 8;
 213
 214        /* the Mute button actually changes the mixer control */
 215        if (code == rc->mute_code)
 216                snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
 217        mixer->rc_code = code;
 218        wmb();
 219        wake_up(&mixer->rc_waitq);
 220}
 221
 222static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
 223                                     long count, loff_t *offset)
 224{
 225        struct usb_mixer_interface *mixer = hw->private_data;
 226        int err;
 227        u32 rc_code;
 228
 229        if (count != 1 && count != 4)
 230                return -EINVAL;
 231        err = wait_event_interruptible(mixer->rc_waitq,
 232                                       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
 233        if (err == 0) {
 234                if (count == 1)
 235                        err = put_user(rc_code, buf);
 236                else
 237                        err = put_user(rc_code, (u32 __user *)buf);
 238        }
 239        return err < 0 ? err : count;
 240}
 241
 242static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
 243                                            poll_table *wait)
 244{
 245        struct usb_mixer_interface *mixer = hw->private_data;
 246
 247        poll_wait(file, &mixer->rc_waitq, wait);
 248        return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
 249}
 250
 251static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
 252{
 253        struct snd_hwdep *hwdep;
 254        int err, len, i;
 255
 256        for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
 257                if (rc_configs[i].usb_id == mixer->chip->usb_id)
 258                        break;
 259        if (i >= ARRAY_SIZE(rc_configs))
 260                return 0;
 261        mixer->rc_cfg = &rc_configs[i];
 262
 263        len = mixer->rc_cfg->packet_length;
 264
 265        init_waitqueue_head(&mixer->rc_waitq);
 266        err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
 267        if (err < 0)
 268                return err;
 269        snprintf(hwdep->name, sizeof(hwdep->name),
 270                 "%s remote control", mixer->chip->card->shortname);
 271        hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
 272        hwdep->private_data = mixer;
 273        hwdep->ops.read = snd_usb_sbrc_hwdep_read;
 274        hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
 275        hwdep->exclusive = 1;
 276
 277        mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
 278        if (!mixer->rc_urb)
 279                return -ENOMEM;
 280        mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
 281        if (!mixer->rc_setup_packet) {
 282                usb_free_urb(mixer->rc_urb);
 283                mixer->rc_urb = NULL;
 284                return -ENOMEM;
 285        }
 286        mixer->rc_setup_packet->bRequestType =
 287                USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
 288        mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
 289        mixer->rc_setup_packet->wValue = cpu_to_le16(0);
 290        mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
 291        mixer->rc_setup_packet->wLength = cpu_to_le16(len);
 292        usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
 293                             usb_rcvctrlpipe(mixer->chip->dev, 0),
 294                             (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
 295                             snd_usb_soundblaster_remote_complete, mixer);
 296        return 0;
 297}
 298
 299#define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
 300
 301static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
 302{
 303        ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
 304        return 0;
 305}
 306
 307static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
 308                                    int value, int index)
 309{
 310        struct snd_usb_audio *chip = mixer->chip;
 311        int err;
 312
 313        err = snd_usb_lock_shutdown(chip);
 314        if (err < 0)
 315                return err;
 316
 317        if (chip->usb_id == USB_ID(0x041e, 0x3042))
 318                err = snd_usb_ctl_msg(chip->dev,
 319                              usb_sndctrlpipe(chip->dev, 0), 0x24,
 320                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 321                              !value, 0, NULL, 0);
 322        /* USB X-Fi S51 Pro */
 323        if (chip->usb_id == USB_ID(0x041e, 0x30df))
 324                err = snd_usb_ctl_msg(chip->dev,
 325                              usb_sndctrlpipe(chip->dev, 0), 0x24,
 326                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 327                              !value, 0, NULL, 0);
 328        else
 329                err = snd_usb_ctl_msg(chip->dev,
 330                              usb_sndctrlpipe(chip->dev, 0), 0x24,
 331                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 332                              value, index + 2, NULL, 0);
 333        snd_usb_unlock_shutdown(chip);
 334        return err;
 335}
 336
 337static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
 338                                 struct snd_ctl_elem_value *ucontrol)
 339{
 340        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
 341        struct usb_mixer_interface *mixer = list->mixer;
 342        int index = kcontrol->private_value & 0xff;
 343        unsigned int value = ucontrol->value.integer.value[0];
 344        int old_value = kcontrol->private_value >> 8;
 345        int err;
 346
 347        if (value > 1)
 348                return -EINVAL;
 349        if (value == old_value)
 350                return 0;
 351        kcontrol->private_value = (value << 8) | index;
 352        err = snd_audigy2nx_led_update(mixer, value, index);
 353        return err < 0 ? err : 1;
 354}
 355
 356static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
 357{
 358        int priv_value = list->kctl->private_value;
 359
 360        return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
 361                                        priv_value & 0xff);
 362}
 363
 364/* name and private_value are set dynamically */
 365static const struct snd_kcontrol_new snd_audigy2nx_control = {
 366        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 367        .info = snd_audigy2nx_led_info,
 368        .get = snd_audigy2nx_led_get,
 369        .put = snd_audigy2nx_led_put,
 370};
 371
 372static const char * const snd_audigy2nx_led_names[] = {
 373        "CMSS LED Switch",
 374        "Power LED Switch",
 375        "Dolby Digital LED Switch",
 376};
 377
 378static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
 379{
 380        int i, err;
 381
 382        for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
 383                struct snd_kcontrol_new knew;
 384
 385                /* USB X-Fi S51 doesn't have a CMSS LED */
 386                if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
 387                        continue;
 388                /* USB X-Fi S51 Pro doesn't have one either */
 389                if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
 390                        continue;
 391                if (i > 1 && /* Live24ext has 2 LEDs only */
 392                        (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
 393                         mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
 394                         mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
 395                         mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
 396                        break; 
 397
 398                knew = snd_audigy2nx_control;
 399                knew.name = snd_audigy2nx_led_names[i];
 400                knew.private_value = (1 << 8) | i; /* LED on as default */
 401                err = add_single_ctl_with_resume(mixer, 0,
 402                                                 snd_audigy2nx_led_resume,
 403                                                 &knew, NULL);
 404                if (err < 0)
 405                        return err;
 406        }
 407        return 0;
 408}
 409
 410static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
 411                                    struct snd_info_buffer *buffer)
 412{
 413        static const struct sb_jack {
 414                int unitid;
 415                const char *name;
 416        }  jacks_audigy2nx[] = {
 417                {4,  "dig in "},
 418                {7,  "line in"},
 419                {19, "spk out"},
 420                {20, "hph out"},
 421                {-1, NULL}
 422        }, jacks_live24ext[] = {
 423                {4,  "line in"}, /* &1=Line, &2=Mic*/
 424                {3,  "hph out"}, /* headphones */
 425                {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
 426                {-1, NULL}
 427        };
 428        const struct sb_jack *jacks;
 429        struct usb_mixer_interface *mixer = entry->private_data;
 430        int i, err;
 431        u8 buf[3];
 432
 433        snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
 434        if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
 435                jacks = jacks_audigy2nx;
 436        else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
 437                 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
 438                jacks = jacks_live24ext;
 439        else
 440                return;
 441
 442        for (i = 0; jacks[i].name; ++i) {
 443                snd_iprintf(buffer, "%s: ", jacks[i].name);
 444                err = snd_usb_lock_shutdown(mixer->chip);
 445                if (err < 0)
 446                        return;
 447                err = snd_usb_ctl_msg(mixer->chip->dev,
 448                                      usb_rcvctrlpipe(mixer->chip->dev, 0),
 449                                      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
 450                                      USB_RECIP_INTERFACE, 0,
 451                                      jacks[i].unitid << 8, buf, 3);
 452                snd_usb_unlock_shutdown(mixer->chip);
 453                if (err == 3 && (buf[0] == 3 || buf[0] == 6))
 454                        snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
 455                else
 456                        snd_iprintf(buffer, "?\n");
 457        }
 458}
 459
 460/* EMU0204 */
 461static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
 462                                      struct snd_ctl_elem_info *uinfo)
 463{
 464        static const char * const texts[2] = {"1/2", "3/4"};
 465
 466        return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
 467}
 468
 469static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
 470                                     struct snd_ctl_elem_value *ucontrol)
 471{
 472        ucontrol->value.enumerated.item[0] = kcontrol->private_value;
 473        return 0;
 474}
 475
 476static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
 477                                        int value)
 478{
 479        struct snd_usb_audio *chip = mixer->chip;
 480        int err;
 481        unsigned char buf[2];
 482
 483        err = snd_usb_lock_shutdown(chip);
 484        if (err < 0)
 485                return err;
 486
 487        buf[0] = 0x01;
 488        buf[1] = value ? 0x02 : 0x01;
 489        err = snd_usb_ctl_msg(chip->dev,
 490                      usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
 491                      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
 492                      0x0400, 0x0e00, buf, 2);
 493        snd_usb_unlock_shutdown(chip);
 494        return err;
 495}
 496
 497static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
 498                                     struct snd_ctl_elem_value *ucontrol)
 499{
 500        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
 501        struct usb_mixer_interface *mixer = list->mixer;
 502        unsigned int value = ucontrol->value.enumerated.item[0];
 503        int err;
 504
 505        if (value > 1)
 506                return -EINVAL;
 507
 508        if (value == kcontrol->private_value)
 509                return 0;
 510
 511        kcontrol->private_value = value;
 512        err = snd_emu0204_ch_switch_update(mixer, value);
 513        return err < 0 ? err : 1;
 514}
 515
 516static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
 517{
 518        return snd_emu0204_ch_switch_update(list->mixer,
 519                                            list->kctl->private_value);
 520}
 521
 522static struct snd_kcontrol_new snd_emu0204_control = {
 523        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 524        .name = "Front Jack Channels",
 525        .info = snd_emu0204_ch_switch_info,
 526        .get = snd_emu0204_ch_switch_get,
 527        .put = snd_emu0204_ch_switch_put,
 528        .private_value = 0,
 529};
 530
 531static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
 532{
 533        return add_single_ctl_with_resume(mixer, 0,
 534                                          snd_emu0204_ch_switch_resume,
 535                                          &snd_emu0204_control, NULL);
 536}
 537
 538/* ASUS Xonar U1 / U3 controls */
 539
 540static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
 541                                   struct snd_ctl_elem_value *ucontrol)
 542{
 543        ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
 544        return 0;
 545}
 546
 547static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
 548                                      unsigned char status)
 549{
 550        struct snd_usb_audio *chip = mixer->chip;
 551        int err;
 552
 553        err = snd_usb_lock_shutdown(chip);
 554        if (err < 0)
 555                return err;
 556        err = snd_usb_ctl_msg(chip->dev,
 557                              usb_sndctrlpipe(chip->dev, 0), 0x08,
 558                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
 559                              50, 0, &status, 1);
 560        snd_usb_unlock_shutdown(chip);
 561        return err;
 562}
 563
 564static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
 565                                   struct snd_ctl_elem_value *ucontrol)
 566{
 567        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
 568        u8 old_status, new_status;
 569        int err;
 570
 571        old_status = kcontrol->private_value;
 572        if (ucontrol->value.integer.value[0])
 573                new_status = old_status | 0x02;
 574        else
 575                new_status = old_status & ~0x02;
 576        if (new_status == old_status)
 577                return 0;
 578
 579        kcontrol->private_value = new_status;
 580        err = snd_xonar_u1_switch_update(list->mixer, new_status);
 581        return err < 0 ? err : 1;
 582}
 583
 584static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
 585{
 586        return snd_xonar_u1_switch_update(list->mixer,
 587                                          list->kctl->private_value);
 588}
 589
 590static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
 591        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 592        .name = "Digital Playback Switch",
 593        .info = snd_ctl_boolean_mono_info,
 594        .get = snd_xonar_u1_switch_get,
 595        .put = snd_xonar_u1_switch_put,
 596        .private_value = 0x05,
 597};
 598
 599static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
 600{
 601        return add_single_ctl_with_resume(mixer, 0,
 602                                          snd_xonar_u1_switch_resume,
 603                                          &snd_xonar_u1_output_switch, NULL);
 604}
 605
 606/* Digidesign Mbox 1 clock source switch (internal/spdif) */
 607
 608static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
 609                                struct snd_ctl_elem_value *ucontrol)
 610{
 611        ucontrol->value.enumerated.item[0] = kctl->private_value;
 612        return 0;
 613}
 614
 615static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
 616{
 617        struct snd_usb_audio *chip = mixer->chip;
 618        int err;
 619        unsigned char buff[3];
 620
 621        err = snd_usb_lock_shutdown(chip);
 622        if (err < 0)
 623                return err;
 624
 625        /* Prepare for magic command to toggle clock source */
 626        err = snd_usb_ctl_msg(chip->dev,
 627                                usb_rcvctrlpipe(chip->dev, 0), 0x81,
 628                                USB_DIR_IN |
 629                                USB_TYPE_CLASS |
 630                                USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
 631        if (err < 0)
 632                goto err;
 633        err = snd_usb_ctl_msg(chip->dev,
 634                                usb_rcvctrlpipe(chip->dev, 0), 0x81,
 635                                USB_DIR_IN |
 636                                USB_TYPE_CLASS |
 637                                USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
 638        if (err < 0)
 639                goto err;
 640
 641        /* 2 possibilities:     Internal    -> send sample rate
 642         *                      S/PDIF sync -> send zeroes
 643         * NB: Sample rate locked to 48kHz on purpose to
 644         *     prevent user from resetting the sample rate
 645         *     while S/PDIF sync is enabled and confusing
 646         *     this configuration.
 647         */
 648        if (val == 0) {
 649                buff[0] = 0x80;
 650                buff[1] = 0xbb;
 651                buff[2] = 0x00;
 652        } else {
 653                buff[0] = buff[1] = buff[2] = 0x00;
 654        }
 655
 656        /* Send the magic command to toggle the clock source */
 657        err = snd_usb_ctl_msg(chip->dev,
 658                                usb_sndctrlpipe(chip->dev, 0), 0x1,
 659                                USB_TYPE_CLASS |
 660                                USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
 661        if (err < 0)
 662                goto err;
 663        err = snd_usb_ctl_msg(chip->dev,
 664                                usb_rcvctrlpipe(chip->dev, 0), 0x81,
 665                                USB_DIR_IN |
 666                                USB_TYPE_CLASS |
 667                                USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
 668        if (err < 0)
 669                goto err;
 670        err = snd_usb_ctl_msg(chip->dev,
 671                                usb_rcvctrlpipe(chip->dev, 0), 0x81,
 672                                USB_DIR_IN |
 673                                USB_TYPE_CLASS |
 674                                USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
 675        if (err < 0)
 676                goto err;
 677
 678err:
 679        snd_usb_unlock_shutdown(chip);
 680        return err;
 681}
 682
 683static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
 684                                struct snd_ctl_elem_value *ucontrol)
 685{
 686        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
 687        struct usb_mixer_interface *mixer = list->mixer;
 688        int err;
 689        bool cur_val, new_val;
 690
 691        cur_val = kctl->private_value;
 692        new_val = ucontrol->value.enumerated.item[0];
 693        if (cur_val == new_val)
 694                return 0;
 695
 696        kctl->private_value = new_val;
 697        err = snd_mbox1_switch_update(mixer, new_val);
 698        return err < 0 ? err : 1;
 699}
 700
 701static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
 702                                 struct snd_ctl_elem_info *uinfo)
 703{
 704        static const char *const texts[2] = {
 705                "Internal",
 706                "S/PDIF"
 707        };
 708
 709        return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
 710}
 711
 712static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
 713{
 714        return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
 715}
 716
 717static struct snd_kcontrol_new snd_mbox1_switch = {
 718        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 719        .name = "Clock Source",
 720        .index = 0,
 721        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 722        .info = snd_mbox1_switch_info,
 723        .get = snd_mbox1_switch_get,
 724        .put = snd_mbox1_switch_put,
 725        .private_value = 0
 726};
 727
 728static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
 729{
 730        return add_single_ctl_with_resume(mixer, 0,
 731                                          snd_mbox1_switch_resume,
 732                                          &snd_mbox1_switch, NULL);
 733}
 734
 735/* Native Instruments device quirks */
 736
 737#define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
 738
 739static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
 740                                   struct snd_kcontrol *kctl)
 741{
 742        struct usb_device *dev = mixer->chip->dev;
 743        unsigned int pval = kctl->private_value;
 744        u8 value;
 745        int err;
 746
 747        err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
 748                              (pval >> 16) & 0xff,
 749                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 750                              0, pval & 0xffff, &value, 1);
 751        if (err < 0) {
 752                dev_err(&dev->dev,
 753                        "unable to issue vendor read request (ret = %d)", err);
 754                return err;
 755        }
 756
 757        kctl->private_value |= (value << 24);
 758        return 0;
 759}
 760
 761static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
 762                                             struct snd_ctl_elem_value *ucontrol)
 763{
 764        ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
 765        return 0;
 766}
 767
 768static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
 769{
 770        struct snd_usb_audio *chip = list->mixer->chip;
 771        unsigned int pval = list->kctl->private_value;
 772        int err;
 773
 774        err = snd_usb_lock_shutdown(chip);
 775        if (err < 0)
 776                return err;
 777        err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
 778                              (pval >> 16) & 0xff,
 779                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 780                              pval >> 24, pval & 0xffff, NULL, 0, 1000);
 781        snd_usb_unlock_shutdown(chip);
 782        return err;
 783}
 784
 785static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
 786                                             struct snd_ctl_elem_value *ucontrol)
 787{
 788        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
 789        u8 oldval = (kcontrol->private_value >> 24) & 0xff;
 790        u8 newval = ucontrol->value.integer.value[0];
 791        int err;
 792
 793        if (oldval == newval)
 794                return 0;
 795
 796        kcontrol->private_value &= ~(0xff << 24);
 797        kcontrol->private_value |= (unsigned int)newval << 24;
 798        err = snd_ni_update_cur_val(list);
 799        return err < 0 ? err : 1;
 800}
 801
 802static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
 803        {
 804                .name = "Direct Thru Channel A",
 805                .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
 806        },
 807        {
 808                .name = "Direct Thru Channel B",
 809                .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
 810        },
 811        {
 812                .name = "Phono Input Channel A",
 813                .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
 814        },
 815        {
 816                .name = "Phono Input Channel B",
 817                .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
 818        },
 819};
 820
 821static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
 822        {
 823                .name = "Direct Thru Channel A",
 824                .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
 825        },
 826        {
 827                .name = "Direct Thru Channel B",
 828                .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
 829        },
 830        {
 831                .name = "Direct Thru Channel C",
 832                .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
 833        },
 834        {
 835                .name = "Direct Thru Channel D",
 836                .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
 837        },
 838        {
 839                .name = "Phono Input Channel A",
 840                .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
 841        },
 842        {
 843                .name = "Phono Input Channel B",
 844                .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
 845        },
 846        {
 847                .name = "Phono Input Channel C",
 848                .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
 849        },
 850        {
 851                .name = "Phono Input Channel D",
 852                .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
 853        },
 854};
 855
 856static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
 857                                              const struct snd_kcontrol_new *kc,
 858                                              unsigned int count)
 859{
 860        int i, err = 0;
 861        struct snd_kcontrol_new template = {
 862                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 863                .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 864                .get = snd_nativeinstruments_control_get,
 865                .put = snd_nativeinstruments_control_put,
 866                .info = snd_ctl_boolean_mono_info,
 867        };
 868
 869        for (i = 0; i < count; i++) {
 870                struct usb_mixer_elem_list *list;
 871
 872                template.name = kc[i].name;
 873                template.private_value = kc[i].private_value;
 874
 875                err = add_single_ctl_with_resume(mixer, 0,
 876                                                 snd_ni_update_cur_val,
 877                                                 &template, &list);
 878                if (err < 0)
 879                        break;
 880                snd_ni_control_init_val(mixer, list->kctl);
 881        }
 882
 883        return err;
 884}
 885
 886/* M-Audio FastTrack Ultra quirks */
 887/* FTU Effect switch (also used by C400/C600) */
 888static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
 889                                        struct snd_ctl_elem_info *uinfo)
 890{
 891        static const char *const texts[8] = {
 892                "Room 1", "Room 2", "Room 3", "Hall 1",
 893                "Hall 2", "Plate", "Delay", "Echo"
 894        };
 895
 896        return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
 897}
 898
 899static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
 900                                   struct snd_kcontrol *kctl)
 901{
 902        struct usb_device *dev = mixer->chip->dev;
 903        unsigned int pval = kctl->private_value;
 904        int err;
 905        unsigned char value[2];
 906
 907        value[0] = 0x00;
 908        value[1] = 0x00;
 909
 910        err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
 911                              USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
 912                              pval & 0xff00,
 913                              snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
 914                              value, 2);
 915        if (err < 0)
 916                return err;
 917
 918        kctl->private_value |= value[0] << 24;
 919        return 0;
 920}
 921
 922static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
 923                                        struct snd_ctl_elem_value *ucontrol)
 924{
 925        ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
 926        return 0;
 927}
 928
 929static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
 930{
 931        struct snd_usb_audio *chip = list->mixer->chip;
 932        unsigned int pval = list->kctl->private_value;
 933        unsigned char value[2];
 934        int err;
 935
 936        value[0] = pval >> 24;
 937        value[1] = 0;
 938
 939        err = snd_usb_lock_shutdown(chip);
 940        if (err < 0)
 941                return err;
 942        err = snd_usb_ctl_msg(chip->dev,
 943                              usb_sndctrlpipe(chip->dev, 0),
 944                              UAC_SET_CUR,
 945                              USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
 946                              pval & 0xff00,
 947                              snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
 948                              value, 2);
 949        snd_usb_unlock_shutdown(chip);
 950        return err;
 951}
 952
 953static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
 954                                        struct snd_ctl_elem_value *ucontrol)
 955{
 956        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
 957        unsigned int pval = list->kctl->private_value;
 958        int cur_val, err, new_val;
 959
 960        cur_val = pval >> 24;
 961        new_val = ucontrol->value.enumerated.item[0];
 962        if (cur_val == new_val)
 963                return 0;
 964
 965        kctl->private_value &= ~(0xff << 24);
 966        kctl->private_value |= new_val << 24;
 967        err = snd_ftu_eff_switch_update(list);
 968        return err < 0 ? err : 1;
 969}
 970
 971static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
 972        int validx, int bUnitID)
 973{
 974        static struct snd_kcontrol_new template = {
 975                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 976                .name = "Effect Program Switch",
 977                .index = 0,
 978                .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 979                .info = snd_ftu_eff_switch_info,
 980                .get = snd_ftu_eff_switch_get,
 981                .put = snd_ftu_eff_switch_put
 982        };
 983        struct usb_mixer_elem_list *list;
 984        int err;
 985
 986        err = add_single_ctl_with_resume(mixer, bUnitID,
 987                                         snd_ftu_eff_switch_update,
 988                                         &template, &list);
 989        if (err < 0)
 990                return err;
 991        list->kctl->private_value = (validx << 8) | bUnitID;
 992        snd_ftu_eff_switch_init(mixer, list->kctl);
 993        return 0;
 994}
 995
 996/* Create volume controls for FTU devices*/
 997static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
 998{
 999        char name[64];
1000        unsigned int control, cmask;
1001        int in, out, err;
1002
1003        const unsigned int id = 5;
1004        const int val_type = USB_MIXER_S16;
1005
1006        for (out = 0; out < 8; out++) {
1007                control = out + 1;
1008                for (in = 0; in < 8; in++) {
1009                        cmask = 1 << in;
1010                        snprintf(name, sizeof(name),
1011                                "AIn%d - Out%d Capture Volume",
1012                                in  + 1, out + 1);
1013                        err = snd_create_std_mono_ctl(mixer, id, control,
1014                                                        cmask, val_type, name,
1015                                                        &snd_usb_mixer_vol_tlv);
1016                        if (err < 0)
1017                                return err;
1018                }
1019                for (in = 8; in < 16; in++) {
1020                        cmask = 1 << in;
1021                        snprintf(name, sizeof(name),
1022                                "DIn%d - Out%d Playback Volume",
1023                                in - 7, out + 1);
1024                        err = snd_create_std_mono_ctl(mixer, id, control,
1025                                                        cmask, val_type, name,
1026                                                        &snd_usb_mixer_vol_tlv);
1027                        if (err < 0)
1028                                return err;
1029                }
1030        }
1031
1032        return 0;
1033}
1034
1035/* This control needs a volume quirk, see mixer.c */
1036static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1037{
1038        static const char name[] = "Effect Volume";
1039        const unsigned int id = 6;
1040        const int val_type = USB_MIXER_U8;
1041        const unsigned int control = 2;
1042        const unsigned int cmask = 0;
1043
1044        return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1045                                        name, snd_usb_mixer_vol_tlv);
1046}
1047
1048/* This control needs a volume quirk, see mixer.c */
1049static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1050{
1051        static const char name[] = "Effect Duration";
1052        const unsigned int id = 6;
1053        const int val_type = USB_MIXER_S16;
1054        const unsigned int control = 3;
1055        const unsigned int cmask = 0;
1056
1057        return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1058                                        name, snd_usb_mixer_vol_tlv);
1059}
1060
1061/* This control needs a volume quirk, see mixer.c */
1062static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1063{
1064        static const char name[] = "Effect Feedback Volume";
1065        const unsigned int id = 6;
1066        const int val_type = USB_MIXER_U8;
1067        const unsigned int control = 4;
1068        const unsigned int cmask = 0;
1069
1070        return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1071                                        name, NULL);
1072}
1073
1074static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1075{
1076        unsigned int cmask;
1077        int err, ch;
1078        char name[48];
1079
1080        const unsigned int id = 7;
1081        const int val_type = USB_MIXER_S16;
1082        const unsigned int control = 7;
1083
1084        for (ch = 0; ch < 4; ++ch) {
1085                cmask = 1 << ch;
1086                snprintf(name, sizeof(name),
1087                        "Effect Return %d Volume", ch + 1);
1088                err = snd_create_std_mono_ctl(mixer, id, control,
1089                                                cmask, val_type, name,
1090                                                snd_usb_mixer_vol_tlv);
1091                if (err < 0)
1092                        return err;
1093        }
1094
1095        return 0;
1096}
1097
1098static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1099{
1100        unsigned int  cmask;
1101        int err, ch;
1102        char name[48];
1103
1104        const unsigned int id = 5;
1105        const int val_type = USB_MIXER_S16;
1106        const unsigned int control = 9;
1107
1108        for (ch = 0; ch < 8; ++ch) {
1109                cmask = 1 << ch;
1110                snprintf(name, sizeof(name),
1111                        "Effect Send AIn%d Volume", ch + 1);
1112                err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1113                                                val_type, name,
1114                                                snd_usb_mixer_vol_tlv);
1115                if (err < 0)
1116                        return err;
1117        }
1118        for (ch = 8; ch < 16; ++ch) {
1119                cmask = 1 << ch;
1120                snprintf(name, sizeof(name),
1121                        "Effect Send DIn%d Volume", ch - 7);
1122                err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1123                                                val_type, name,
1124                                                snd_usb_mixer_vol_tlv);
1125                if (err < 0)
1126                        return err;
1127        }
1128        return 0;
1129}
1130
1131static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1132{
1133        int err;
1134
1135        err = snd_ftu_create_volume_ctls(mixer);
1136        if (err < 0)
1137                return err;
1138
1139        err = snd_ftu_create_effect_switch(mixer, 1, 6);
1140        if (err < 0)
1141                return err;
1142
1143        err = snd_ftu_create_effect_volume_ctl(mixer);
1144        if (err < 0)
1145                return err;
1146
1147        err = snd_ftu_create_effect_duration_ctl(mixer);
1148        if (err < 0)
1149                return err;
1150
1151        err = snd_ftu_create_effect_feedback_ctl(mixer);
1152        if (err < 0)
1153                return err;
1154
1155        err = snd_ftu_create_effect_return_ctls(mixer);
1156        if (err < 0)
1157                return err;
1158
1159        err = snd_ftu_create_effect_send_ctls(mixer);
1160        if (err < 0)
1161                return err;
1162
1163        return 0;
1164}
1165
1166void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1167                               unsigned char samplerate_id)
1168{
1169        struct usb_mixer_interface *mixer;
1170        struct usb_mixer_elem_info *cval;
1171        int unitid = 12; /* SamleRate ExtensionUnit ID */
1172
1173        list_for_each_entry(mixer, &chip->mixer_list, list) {
1174                cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
1175                if (cval) {
1176                        snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1177                                                    cval->control << 8,
1178                                                    samplerate_id);
1179                        snd_usb_mixer_notify_id(mixer, unitid);
1180                }
1181                break;
1182        }
1183}
1184
1185/* M-Audio Fast Track C400/C600 */
1186/* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1187static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1188{
1189        char name[64];
1190        unsigned int cmask, offset;
1191        int out, chan, err;
1192        int num_outs = 0;
1193        int num_ins = 0;
1194
1195        const unsigned int id = 0x40;
1196        const int val_type = USB_MIXER_S16;
1197        const int control = 1;
1198
1199        switch (mixer->chip->usb_id) {
1200        case USB_ID(0x0763, 0x2030):
1201                num_outs = 6;
1202                num_ins = 4;
1203                break;
1204        case USB_ID(0x0763, 0x2031):
1205                num_outs = 8;
1206                num_ins = 6;
1207                break;
1208        }
1209
1210        for (chan = 0; chan < num_outs + num_ins; chan++) {
1211                for (out = 0; out < num_outs; out++) {
1212                        if (chan < num_outs) {
1213                                snprintf(name, sizeof(name),
1214                                        "PCM%d-Out%d Playback Volume",
1215                                        chan + 1, out + 1);
1216                        } else {
1217                                snprintf(name, sizeof(name),
1218                                        "In%d-Out%d Playback Volume",
1219                                        chan - num_outs + 1, out + 1);
1220                        }
1221
1222                        cmask = (out == 0) ? 0 : 1 << (out - 1);
1223                        offset = chan * num_outs;
1224                        err = snd_create_std_mono_ctl_offset(mixer, id, control,
1225                                                cmask, val_type, offset, name,
1226                                                &snd_usb_mixer_vol_tlv);
1227                        if (err < 0)
1228                                return err;
1229                }
1230        }
1231
1232        return 0;
1233}
1234
1235/* This control needs a volume quirk, see mixer.c */
1236static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1237{
1238        static const char name[] = "Effect Volume";
1239        const unsigned int id = 0x43;
1240        const int val_type = USB_MIXER_U8;
1241        const unsigned int control = 3;
1242        const unsigned int cmask = 0;
1243
1244        return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1245                                        name, snd_usb_mixer_vol_tlv);
1246}
1247
1248/* This control needs a volume quirk, see mixer.c */
1249static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1250{
1251        static const char name[] = "Effect Duration";
1252        const unsigned int id = 0x43;
1253        const int val_type = USB_MIXER_S16;
1254        const unsigned int control = 4;
1255        const unsigned int cmask = 0;
1256
1257        return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1258                                        name, snd_usb_mixer_vol_tlv);
1259}
1260
1261/* This control needs a volume quirk, see mixer.c */
1262static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1263{
1264        static const char name[] = "Effect Feedback Volume";
1265        const unsigned int id = 0x43;
1266        const int val_type = USB_MIXER_U8;
1267        const unsigned int control = 5;
1268        const unsigned int cmask = 0;
1269
1270        return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1271                                        name, NULL);
1272}
1273
1274static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1275{
1276        char name[64];
1277        unsigned int cmask;
1278        int chan, err;
1279        int num_outs = 0;
1280        int num_ins = 0;
1281
1282        const unsigned int id = 0x42;
1283        const int val_type = USB_MIXER_S16;
1284        const int control = 1;
1285
1286        switch (mixer->chip->usb_id) {
1287        case USB_ID(0x0763, 0x2030):
1288                num_outs = 6;
1289                num_ins = 4;
1290                break;
1291        case USB_ID(0x0763, 0x2031):
1292                num_outs = 8;
1293                num_ins = 6;
1294                break;
1295        }
1296
1297        for (chan = 0; chan < num_outs + num_ins; chan++) {
1298                if (chan < num_outs) {
1299                        snprintf(name, sizeof(name),
1300                                "Effect Send DOut%d",
1301                                chan + 1);
1302                } else {
1303                        snprintf(name, sizeof(name),
1304                                "Effect Send AIn%d",
1305                                chan - num_outs + 1);
1306                }
1307
1308                cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1309                err = snd_create_std_mono_ctl(mixer, id, control,
1310                                                cmask, val_type, name,
1311                                                &snd_usb_mixer_vol_tlv);
1312                if (err < 0)
1313                        return err;
1314        }
1315
1316        return 0;
1317}
1318
1319static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1320{
1321        char name[64];
1322        unsigned int cmask;
1323        int chan, err;
1324        int num_outs = 0;
1325        int offset = 0;
1326
1327        const unsigned int id = 0x40;
1328        const int val_type = USB_MIXER_S16;
1329        const int control = 1;
1330
1331        switch (mixer->chip->usb_id) {
1332        case USB_ID(0x0763, 0x2030):
1333                num_outs = 6;
1334                offset = 0x3c;
1335                /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1336                break;
1337        case USB_ID(0x0763, 0x2031):
1338                num_outs = 8;
1339                offset = 0x70;
1340                /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1341                break;
1342        }
1343
1344        for (chan = 0; chan < num_outs; chan++) {
1345                snprintf(name, sizeof(name),
1346                        "Effect Return %d",
1347                        chan + 1);
1348
1349                cmask = (chan == 0) ? 0 :
1350                        1 << (chan + (chan % 2) * num_outs - 1);
1351                err = snd_create_std_mono_ctl_offset(mixer, id, control,
1352                                                cmask, val_type, offset, name,
1353                                                &snd_usb_mixer_vol_tlv);
1354                if (err < 0)
1355                        return err;
1356        }
1357
1358        return 0;
1359}
1360
1361static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1362{
1363        int err;
1364
1365        err = snd_c400_create_vol_ctls(mixer);
1366        if (err < 0)
1367                return err;
1368
1369        err = snd_c400_create_effect_vol_ctls(mixer);
1370        if (err < 0)
1371                return err;
1372
1373        err = snd_c400_create_effect_ret_vol_ctls(mixer);
1374        if (err < 0)
1375                return err;
1376
1377        err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1378        if (err < 0)
1379                return err;
1380
1381        err = snd_c400_create_effect_volume_ctl(mixer);
1382        if (err < 0)
1383                return err;
1384
1385        err = snd_c400_create_effect_duration_ctl(mixer);
1386        if (err < 0)
1387                return err;
1388
1389        err = snd_c400_create_effect_feedback_ctl(mixer);
1390        if (err < 0)
1391                return err;
1392
1393        return 0;
1394}
1395
1396/*
1397 * The mixer units for Ebox-44 are corrupt, and even where they
1398 * are valid they presents mono controls as L and R channels of
1399 * stereo. So we provide a good mixer here.
1400 */
1401static struct std_mono_table ebox44_table[] = {
1402        {
1403                .unitid = 4,
1404                .control = 1,
1405                .cmask = 0x0,
1406                .val_type = USB_MIXER_INV_BOOLEAN,
1407                .name = "Headphone Playback Switch"
1408        },
1409        {
1410                .unitid = 4,
1411                .control = 2,
1412                .cmask = 0x1,
1413                .val_type = USB_MIXER_S16,
1414                .name = "Headphone A Mix Playback Volume"
1415        },
1416        {
1417                .unitid = 4,
1418                .control = 2,
1419                .cmask = 0x2,
1420                .val_type = USB_MIXER_S16,
1421                .name = "Headphone B Mix Playback Volume"
1422        },
1423
1424        {
1425                .unitid = 7,
1426                .control = 1,
1427                .cmask = 0x0,
1428                .val_type = USB_MIXER_INV_BOOLEAN,
1429                .name = "Output Playback Switch"
1430        },
1431        {
1432                .unitid = 7,
1433                .control = 2,
1434                .cmask = 0x1,
1435                .val_type = USB_MIXER_S16,
1436                .name = "Output A Playback Volume"
1437        },
1438        {
1439                .unitid = 7,
1440                .control = 2,
1441                .cmask = 0x2,
1442                .val_type = USB_MIXER_S16,
1443                .name = "Output B Playback Volume"
1444        },
1445
1446        {
1447                .unitid = 10,
1448                .control = 1,
1449                .cmask = 0x0,
1450                .val_type = USB_MIXER_INV_BOOLEAN,
1451                .name = "Input Capture Switch"
1452        },
1453        {
1454                .unitid = 10,
1455                .control = 2,
1456                .cmask = 0x1,
1457                .val_type = USB_MIXER_S16,
1458                .name = "Input A Capture Volume"
1459        },
1460        {
1461                .unitid = 10,
1462                .control = 2,
1463                .cmask = 0x2,
1464                .val_type = USB_MIXER_S16,
1465                .name = "Input B Capture Volume"
1466        },
1467
1468        {}
1469};
1470
1471/* Audio Advantage Micro II findings:
1472 *
1473 * Mapping spdif AES bits to vendor register.bit:
1474 * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1475 * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1476 * AES2: [0 0 0 0 0 0 0 0]
1477 * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1478 *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1479 *
1480 * power on values:
1481 * r2: 0x10
1482 * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1483 *           just after it to 0xa0, presumably it disables/mutes some analog
1484 *           parts when there is no audio.)
1485 * r9: 0x28
1486 *
1487 * Optical transmitter on/off:
1488 * vendor register.bit: 9.1
1489 * 0 - on (0x28 register value)
1490 * 1 - off (0x2a register value)
1491 *
1492 */
1493static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1494        struct snd_ctl_elem_info *uinfo)
1495{
1496        uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1497        uinfo->count = 1;
1498        return 0;
1499}
1500
1501static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1502        struct snd_ctl_elem_value *ucontrol)
1503{
1504        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1505        struct snd_usb_audio *chip = list->mixer->chip;
1506        int err;
1507        struct usb_interface *iface;
1508        struct usb_host_interface *alts;
1509        unsigned int ep;
1510        unsigned char data[3];
1511        int rate;
1512
1513        err = snd_usb_lock_shutdown(chip);
1514        if (err < 0)
1515                return err;
1516
1517        ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1518        ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1519        ucontrol->value.iec958.status[2] = 0x00;
1520
1521        /* use known values for that card: interface#1 altsetting#1 */
1522        iface = usb_ifnum_to_if(chip->dev, 1);
1523        if (!iface || iface->num_altsetting < 2)
1524                return -EINVAL;
1525        alts = &iface->altsetting[1];
1526        if (get_iface_desc(alts)->bNumEndpoints < 1)
1527                return -EINVAL;
1528        ep = get_endpoint(alts, 0)->bEndpointAddress;
1529
1530        err = snd_usb_ctl_msg(chip->dev,
1531                        usb_rcvctrlpipe(chip->dev, 0),
1532                        UAC_GET_CUR,
1533                        USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1534                        UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1535                        ep,
1536                        data,
1537                        sizeof(data));
1538        if (err < 0)
1539                goto end;
1540
1541        rate = data[0] | (data[1] << 8) | (data[2] << 16);
1542        ucontrol->value.iec958.status[3] = (rate == 48000) ?
1543                        IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1544
1545        err = 0;
1546 end:
1547        snd_usb_unlock_shutdown(chip);
1548        return err;
1549}
1550
1551static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1552{
1553        struct snd_usb_audio *chip = list->mixer->chip;
1554        unsigned int pval = list->kctl->private_value;
1555        u8 reg;
1556        int err;
1557
1558        err = snd_usb_lock_shutdown(chip);
1559        if (err < 0)
1560                return err;
1561
1562        reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1563        err = snd_usb_ctl_msg(chip->dev,
1564                        usb_sndctrlpipe(chip->dev, 0),
1565                        UAC_SET_CUR,
1566                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1567                        reg,
1568                        2,
1569                        NULL,
1570                        0);
1571        if (err < 0)
1572                goto end;
1573
1574        reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1575        reg |= (pval >> 12) & 0x0f;
1576        err = snd_usb_ctl_msg(chip->dev,
1577                        usb_sndctrlpipe(chip->dev, 0),
1578                        UAC_SET_CUR,
1579                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1580                        reg,
1581                        3,
1582                        NULL,
1583                        0);
1584        if (err < 0)
1585                goto end;
1586
1587 end:
1588        snd_usb_unlock_shutdown(chip);
1589        return err;
1590}
1591
1592static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1593        struct snd_ctl_elem_value *ucontrol)
1594{
1595        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1596        unsigned int pval, pval_old;
1597        int err;
1598
1599        pval = pval_old = kcontrol->private_value;
1600        pval &= 0xfffff0f0;
1601        pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1602        pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1603
1604        pval &= 0xffff0fff;
1605        pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1606
1607        /* The frequency bits in AES3 cannot be set via register access. */
1608
1609        /* Silently ignore any bits from the request that cannot be set. */
1610
1611        if (pval == pval_old)
1612                return 0;
1613
1614        kcontrol->private_value = pval;
1615        err = snd_microii_spdif_default_update(list);
1616        return err < 0 ? err : 1;
1617}
1618
1619static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1620        struct snd_ctl_elem_value *ucontrol)
1621{
1622        ucontrol->value.iec958.status[0] = 0x0f;
1623        ucontrol->value.iec958.status[1] = 0xff;
1624        ucontrol->value.iec958.status[2] = 0x00;
1625        ucontrol->value.iec958.status[3] = 0x00;
1626
1627        return 0;
1628}
1629
1630static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1631        struct snd_ctl_elem_value *ucontrol)
1632{
1633        ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1634
1635        return 0;
1636}
1637
1638static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1639{
1640        struct snd_usb_audio *chip = list->mixer->chip;
1641        u8 reg = list->kctl->private_value;
1642        int err;
1643
1644        err = snd_usb_lock_shutdown(chip);
1645        if (err < 0)
1646                return err;
1647
1648        err = snd_usb_ctl_msg(chip->dev,
1649                        usb_sndctrlpipe(chip->dev, 0),
1650                        UAC_SET_CUR,
1651                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1652                        reg,
1653                        9,
1654                        NULL,
1655                        0);
1656
1657        snd_usb_unlock_shutdown(chip);
1658        return err;
1659}
1660
1661static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1662        struct snd_ctl_elem_value *ucontrol)
1663{
1664        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1665        u8 reg;
1666        int err;
1667
1668        reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1669        if (reg != list->kctl->private_value)
1670                return 0;
1671
1672        kcontrol->private_value = reg;
1673        err = snd_microii_spdif_switch_update(list);
1674        return err < 0 ? err : 1;
1675}
1676
1677static struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1678        {
1679                .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1680                .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1681                .info =     snd_microii_spdif_info,
1682                .get =      snd_microii_spdif_default_get,
1683                .put =      snd_microii_spdif_default_put,
1684                .private_value = 0x00000100UL,/* reset value */
1685        },
1686        {
1687                .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1688                .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1689                .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1690                .info =     snd_microii_spdif_info,
1691                .get =      snd_microii_spdif_mask_get,
1692        },
1693        {
1694                .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1695                .name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1696                .info =     snd_ctl_boolean_mono_info,
1697                .get =      snd_microii_spdif_switch_get,
1698                .put =      snd_microii_spdif_switch_put,
1699                .private_value = 0x00000028UL,/* reset value */
1700        }
1701};
1702
1703static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1704{
1705        int err, i;
1706        static usb_mixer_elem_resume_func_t resume_funcs[] = {
1707                snd_microii_spdif_default_update,
1708                NULL,
1709                snd_microii_spdif_switch_update
1710        };
1711
1712        for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1713                err = add_single_ctl_with_resume(mixer, 0,
1714                                                 resume_funcs[i],
1715                                                 &snd_microii_mixer_spdif[i],
1716                                                 NULL);
1717                if (err < 0)
1718                        return err;
1719        }
1720
1721        return 0;
1722}
1723
1724/* Creative Sound Blaster E1 */
1725
1726static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
1727                                          struct snd_ctl_elem_value *ucontrol)
1728{
1729        ucontrol->value.integer.value[0] = kcontrol->private_value;
1730        return 0;
1731}
1732
1733static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
1734                                             unsigned char state)
1735{
1736        struct snd_usb_audio *chip = mixer->chip;
1737        int err;
1738        unsigned char buff[2];
1739
1740        buff[0] = 0x02;
1741        buff[1] = state ? 0x02 : 0x00;
1742
1743        err = snd_usb_lock_shutdown(chip);
1744        if (err < 0)
1745                return err;
1746        err = snd_usb_ctl_msg(chip->dev,
1747                        usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
1748                        USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1749                        0x0202, 3, buff, 2);
1750        snd_usb_unlock_shutdown(chip);
1751        return err;
1752}
1753
1754static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
1755                                          struct snd_ctl_elem_value *ucontrol)
1756{
1757        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1758        unsigned char value = !!ucontrol->value.integer.value[0];
1759        int err;
1760
1761        if (kcontrol->private_value == value)
1762                return 0;
1763        kcontrol->private_value = value;
1764        err = snd_soundblaster_e1_switch_update(list->mixer, value);
1765        return err < 0 ? err : 1;
1766}
1767
1768static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
1769{
1770        return snd_soundblaster_e1_switch_update(list->mixer,
1771                                                 list->kctl->private_value);
1772}
1773
1774static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
1775                                           struct snd_ctl_elem_info *uinfo)
1776{
1777        static const char *const texts[2] = {
1778                "Mic", "Aux"
1779        };
1780
1781        return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1782}
1783
1784static struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
1785        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1786        .name = "Input Source",
1787        .info = snd_soundblaster_e1_switch_info,
1788        .get = snd_soundblaster_e1_switch_get,
1789        .put = snd_soundblaster_e1_switch_put,
1790        .private_value = 0,
1791};
1792
1793static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
1794{
1795        return add_single_ctl_with_resume(mixer, 0,
1796                                          snd_soundblaster_e1_switch_resume,
1797                                          &snd_soundblaster_e1_input_switch,
1798                                          NULL);
1799}
1800
1801static void dell_dock_init_vol(struct snd_usb_audio *chip, int ch, int id)
1802{
1803        u16 buf = 0;
1804
1805        snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
1806                        USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1807                        ch, snd_usb_ctrl_intf(chip) | (id << 8),
1808                        &buf, 2);
1809}
1810
1811static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
1812{
1813        /* fix to 0dB playback volumes */
1814        dell_dock_init_vol(mixer->chip, 1, 16);
1815        dell_dock_init_vol(mixer->chip, 2, 16);
1816        dell_dock_init_vol(mixer->chip, 1, 19);
1817        dell_dock_init_vol(mixer->chip, 2, 19);
1818        return 0;
1819}
1820
1821/* RME Class Compliant device quirks */
1822
1823#define SND_RME_GET_STATUS1                     23
1824#define SND_RME_GET_CURRENT_FREQ                17
1825#define SND_RME_CLK_SYSTEM_SHIFT                16
1826#define SND_RME_CLK_SYSTEM_MASK                 0x1f
1827#define SND_RME_CLK_AES_SHIFT                   8
1828#define SND_RME_CLK_SPDIF_SHIFT                 12
1829#define SND_RME_CLK_AES_SPDIF_MASK              0xf
1830#define SND_RME_CLK_SYNC_SHIFT                  6
1831#define SND_RME_CLK_SYNC_MASK                   0x3
1832#define SND_RME_CLK_FREQMUL_SHIFT               18
1833#define SND_RME_CLK_FREQMUL_MASK                0x7
1834#define SND_RME_CLK_SYSTEM(x) \
1835        ((x >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
1836#define SND_RME_CLK_AES(x) \
1837        ((x >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
1838#define SND_RME_CLK_SPDIF(x) \
1839        ((x >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
1840#define SND_RME_CLK_SYNC(x) \
1841        ((x >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
1842#define SND_RME_CLK_FREQMUL(x) \
1843        ((x >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
1844#define SND_RME_CLK_AES_LOCK                    0x1
1845#define SND_RME_CLK_AES_SYNC                    0x4
1846#define SND_RME_CLK_SPDIF_LOCK                  0x2
1847#define SND_RME_CLK_SPDIF_SYNC                  0x8
1848#define SND_RME_SPDIF_IF_SHIFT                  4
1849#define SND_RME_SPDIF_FORMAT_SHIFT              5
1850#define SND_RME_BINARY_MASK                     0x1
1851#define SND_RME_SPDIF_IF(x) \
1852        ((x >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
1853#define SND_RME_SPDIF_FORMAT(x) \
1854        ((x >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
1855
1856static const u32 snd_rme_rate_table[] = {
1857        32000, 44100, 48000, 50000,
1858        64000, 88200, 96000, 100000,
1859        128000, 176400, 192000, 200000,
1860        256000, 352800, 384000, 400000,
1861        512000, 705600, 768000, 800000
1862};
1863/* maximum number of items for AES and S/PDIF rates for above table */
1864#define SND_RME_RATE_IDX_AES_SPDIF_NUM          12
1865
1866enum snd_rme_domain {
1867        SND_RME_DOMAIN_SYSTEM,
1868        SND_RME_DOMAIN_AES,
1869        SND_RME_DOMAIN_SPDIF
1870};
1871
1872enum snd_rme_clock_status {
1873        SND_RME_CLOCK_NOLOCK,
1874        SND_RME_CLOCK_LOCK,
1875        SND_RME_CLOCK_SYNC
1876};
1877
1878static int snd_rme_read_value(struct snd_usb_audio *chip,
1879                              unsigned int item,
1880                              u32 *value)
1881{
1882        struct usb_device *dev = chip->dev;
1883        int err;
1884
1885        err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1886                              item,
1887                              USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1888                              0, 0,
1889                              value, sizeof(*value));
1890        if (err < 0)
1891                dev_err(&dev->dev,
1892                        "unable to issue vendor read request %d (ret = %d)",
1893                        item, err);
1894        return err;
1895}
1896
1897static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
1898                               u32 *status1)
1899{
1900        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1901        struct snd_usb_audio *chip = list->mixer->chip;
1902        int err;
1903
1904        err = snd_usb_lock_shutdown(chip);
1905        if (err < 0)
1906                return err;
1907        err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
1908        snd_usb_unlock_shutdown(chip);
1909        return err;
1910}
1911
1912static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
1913                            struct snd_ctl_elem_value *ucontrol)
1914{
1915        u32 status1;
1916        u32 rate = 0;
1917        int idx;
1918        int err;
1919
1920        err = snd_rme_get_status1(kcontrol, &status1);
1921        if (err < 0)
1922                return err;
1923        switch (kcontrol->private_value) {
1924        case SND_RME_DOMAIN_SYSTEM:
1925                idx = SND_RME_CLK_SYSTEM(status1);
1926                if (idx < ARRAY_SIZE(snd_rme_rate_table))
1927                        rate = snd_rme_rate_table[idx];
1928                break;
1929        case SND_RME_DOMAIN_AES:
1930                idx = SND_RME_CLK_AES(status1);
1931                if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
1932                        rate = snd_rme_rate_table[idx];
1933                break;
1934        case SND_RME_DOMAIN_SPDIF:
1935                idx = SND_RME_CLK_SPDIF(status1);
1936                if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
1937                        rate = snd_rme_rate_table[idx];
1938                break;
1939        default:
1940                return -EINVAL;
1941        }
1942        ucontrol->value.integer.value[0] = rate;
1943        return 0;
1944}
1945
1946static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
1947                                  struct snd_ctl_elem_value *ucontrol)
1948{
1949        u32 status1;
1950        int idx = SND_RME_CLOCK_NOLOCK;
1951        int err;
1952
1953        err = snd_rme_get_status1(kcontrol, &status1);
1954        if (err < 0)
1955                return err;
1956        switch (kcontrol->private_value) {
1957        case SND_RME_DOMAIN_AES:  /* AES */
1958                if (status1 & SND_RME_CLK_AES_SYNC)
1959                        idx = SND_RME_CLOCK_SYNC;
1960                else if (status1 & SND_RME_CLK_AES_LOCK)
1961                        idx = SND_RME_CLOCK_LOCK;
1962                break;
1963        case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
1964                if (status1 & SND_RME_CLK_SPDIF_SYNC)
1965                        idx = SND_RME_CLOCK_SYNC;
1966                else if (status1 & SND_RME_CLK_SPDIF_LOCK)
1967                        idx = SND_RME_CLOCK_LOCK;
1968                break;
1969        default:
1970                return -EINVAL;
1971        }
1972        ucontrol->value.enumerated.item[0] = idx;
1973        return 0;
1974}
1975
1976static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
1977                                struct snd_ctl_elem_value *ucontrol)
1978{
1979        u32 status1;
1980        int err;
1981
1982        err = snd_rme_get_status1(kcontrol, &status1);
1983        if (err < 0)
1984                return err;
1985        ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
1986        return 0;
1987}
1988
1989static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
1990                                    struct snd_ctl_elem_value *ucontrol)
1991{
1992        u32 status1;
1993        int err;
1994
1995        err = snd_rme_get_status1(kcontrol, &status1);
1996        if (err < 0)
1997                return err;
1998        ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
1999        return 0;
2000}
2001
2002static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
2003                                   struct snd_ctl_elem_value *ucontrol)
2004{
2005        u32 status1;
2006        int err;
2007
2008        err = snd_rme_get_status1(kcontrol, &status1);
2009        if (err < 0)
2010                return err;
2011        ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2012        return 0;
2013}
2014
2015static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2016                                    struct snd_ctl_elem_value *ucontrol)
2017{
2018        struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2019        struct snd_usb_audio *chip = list->mixer->chip;
2020        u32 status1;
2021        const u64 num = 104857600000000ULL;
2022        u32 den;
2023        unsigned int freq;
2024        int err;
2025
2026        err = snd_usb_lock_shutdown(chip);
2027        if (err < 0)
2028                return err;
2029        err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2030        if (err < 0)
2031                goto end;
2032        err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2033        if (err < 0)
2034                goto end;
2035        freq = (den == 0) ? 0 : div64_u64(num, den);
2036        freq <<= SND_RME_CLK_FREQMUL(status1);
2037        ucontrol->value.integer.value[0] = freq;
2038
2039end:
2040        snd_usb_unlock_shutdown(chip);
2041        return err;
2042}
2043
2044static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2045                             struct snd_ctl_elem_info *uinfo)
2046{
2047        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2048        uinfo->count = 1;
2049        switch (kcontrol->private_value) {
2050        case SND_RME_DOMAIN_SYSTEM:
2051                uinfo->value.integer.min = 32000;
2052                uinfo->value.integer.max = 800000;
2053                break;
2054        case SND_RME_DOMAIN_AES:
2055        case SND_RME_DOMAIN_SPDIF:
2056        default:
2057                uinfo->value.integer.min = 0;
2058                uinfo->value.integer.max = 200000;
2059        }
2060        uinfo->value.integer.step = 0;
2061        return 0;
2062}
2063
2064static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2065                                   struct snd_ctl_elem_info *uinfo)
2066{
2067        static const char *const sync_states[] = {
2068                "No Lock", "Lock", "Sync"
2069        };
2070
2071        return snd_ctl_enum_info(uinfo, 1,
2072                                 ARRAY_SIZE(sync_states), sync_states);
2073}
2074
2075static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2076                                 struct snd_ctl_elem_info *uinfo)
2077{
2078        static const char *const spdif_if[] = {
2079                "Coaxial", "Optical"
2080        };
2081
2082        return snd_ctl_enum_info(uinfo, 1,
2083                                 ARRAY_SIZE(spdif_if), spdif_if);
2084}
2085
2086static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2087                                     struct snd_ctl_elem_info *uinfo)
2088{
2089        static const char *const optical_type[] = {
2090                "Consumer", "Professional"
2091        };
2092
2093        return snd_ctl_enum_info(uinfo, 1,
2094                                 ARRAY_SIZE(optical_type), optical_type);
2095}
2096
2097static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2098                                    struct snd_ctl_elem_info *uinfo)
2099{
2100        static const char *const sync_sources[] = {
2101                "Internal", "AES", "SPDIF", "Internal"
2102        };
2103
2104        return snd_ctl_enum_info(uinfo, 1,
2105                                 ARRAY_SIZE(sync_sources), sync_sources);
2106}
2107
2108static struct snd_kcontrol_new snd_rme_controls[] = {
2109        {
2110                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2111                .name = "AES Rate",
2112                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2113                .info = snd_rme_rate_info,
2114                .get = snd_rme_rate_get,
2115                .private_value = SND_RME_DOMAIN_AES
2116        },
2117        {
2118                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2119                .name = "AES Sync",
2120                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2121                .info = snd_rme_sync_state_info,
2122                .get = snd_rme_sync_state_get,
2123                .private_value = SND_RME_DOMAIN_AES
2124        },
2125        {
2126                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2127                .name = "SPDIF Rate",
2128                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2129                .info = snd_rme_rate_info,
2130                .get = snd_rme_rate_get,
2131                .private_value = SND_RME_DOMAIN_SPDIF
2132        },
2133        {
2134                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2135                .name = "SPDIF Sync",
2136                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2137                .info = snd_rme_sync_state_info,
2138                .get = snd_rme_sync_state_get,
2139                .private_value = SND_RME_DOMAIN_SPDIF
2140        },
2141        {
2142                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2143                .name = "SPDIF Interface",
2144                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2145                .info = snd_rme_spdif_if_info,
2146                .get = snd_rme_spdif_if_get,
2147        },
2148        {
2149                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2150                .name = "SPDIF Format",
2151                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2152                .info = snd_rme_spdif_format_info,
2153                .get = snd_rme_spdif_format_get,
2154        },
2155        {
2156                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2157                .name = "Sync Source",
2158                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2159                .info = snd_rme_sync_source_info,
2160                .get = snd_rme_sync_source_get
2161        },
2162        {
2163                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2164                .name = "System Rate",
2165                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2166                .info = snd_rme_rate_info,
2167                .get = snd_rme_rate_get,
2168                .private_value = SND_RME_DOMAIN_SYSTEM
2169        },
2170        {
2171                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2172                .name = "Current Frequency",
2173                .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2174                .info = snd_rme_rate_info,
2175                .get = snd_rme_current_freq_get
2176        }
2177};
2178
2179static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2180{
2181        int err, i;
2182
2183        for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2184                err = add_single_ctl_with_resume(mixer, 0,
2185                                                 NULL,
2186                                                 &snd_rme_controls[i],
2187                                                 NULL);
2188                if (err < 0)
2189                        return err;
2190        }
2191
2192        return 0;
2193}
2194
2195int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
2196{
2197        int err = 0;
2198
2199        err = snd_usb_soundblaster_remote_init(mixer);
2200        if (err < 0)
2201                return err;
2202
2203        switch (mixer->chip->usb_id) {
2204        /* Tascam US-16x08 */
2205        case USB_ID(0x0644, 0x8047):
2206                err = snd_us16x08_controls_create(mixer);
2207                break;
2208        case USB_ID(0x041e, 0x3020):
2209        case USB_ID(0x041e, 0x3040):
2210        case USB_ID(0x041e, 0x3042):
2211        case USB_ID(0x041e, 0x30df):
2212        case USB_ID(0x041e, 0x3048):
2213                err = snd_audigy2nx_controls_create(mixer);
2214                if (err < 0)
2215                        break;
2216                snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
2217                                     mixer, snd_audigy2nx_proc_read);
2218                break;
2219
2220        /* EMU0204 */
2221        case USB_ID(0x041e, 0x3f19):
2222                err = snd_emu0204_controls_create(mixer);
2223                break;
2224
2225        case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
2226        case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
2227                err = snd_c400_create_mixer(mixer);
2228                break;
2229
2230        case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
2231        case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
2232                err = snd_ftu_create_mixer(mixer);
2233                break;
2234
2235        case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
2236        case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
2237        case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
2238                err = snd_xonar_u1_controls_create(mixer);
2239                break;
2240
2241        case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
2242                err = snd_microii_controls_create(mixer);
2243                break;
2244
2245        case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
2246                err = snd_mbox1_create_sync_switch(mixer);
2247                break;
2248
2249        case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
2250                err = snd_nativeinstruments_create_mixer(mixer,
2251                                snd_nativeinstruments_ta6_mixers,
2252                                ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
2253                break;
2254
2255        case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
2256                err = snd_nativeinstruments_create_mixer(mixer,
2257                                snd_nativeinstruments_ta10_mixers,
2258                                ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
2259                break;
2260
2261        case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
2262                /* detection is disabled in mixer_maps.c */
2263                err = snd_create_std_mono_table(mixer, ebox44_table);
2264                break;
2265
2266        case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
2267        case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
2268        case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
2269        case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
2270        case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
2271                err = snd_scarlett_controls_create(mixer);
2272                break;
2273
2274        case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
2275                err = snd_soundblaster_e1_switch_create(mixer);
2276                break;
2277        case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
2278                err = dell_dock_mixer_init(mixer);
2279                break;
2280
2281        case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
2282        case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
2283        case USB_ID(0x2a39, 0x3fd4): /* RME */
2284                err = snd_rme_controls_create(mixer);
2285                break;
2286        }
2287
2288        return err;
2289}
2290
2291#ifdef CONFIG_PM
2292void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
2293{
2294        switch (mixer->chip->usb_id) {
2295        case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
2296                dell_dock_mixer_init(mixer);
2297                break;
2298        }
2299}
2300#endif
2301
2302void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
2303                                    int unitid)
2304{
2305        if (!mixer->rc_cfg)
2306                return;
2307        /* unit ids specific to Extigy/Audigy 2 NX: */
2308        switch (unitid) {
2309        case 0: /* remote control */
2310                mixer->rc_urb->dev = mixer->chip->dev;
2311                usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
2312                break;
2313        case 4: /* digital in jack */
2314        case 7: /* line in jacks */
2315        case 19: /* speaker out jacks */
2316        case 20: /* headphones out jack */
2317                break;
2318        /* live24ext: 4 = line-in jack */
2319        case 3: /* hp-out jack (may actuate Mute) */
2320                if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2321                    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2322                        snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
2323                break;
2324        default:
2325                usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
2326                break;
2327        }
2328}
2329
2330static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
2331                                         struct usb_mixer_elem_info *cval,
2332                                         struct snd_kcontrol *kctl)
2333{
2334        /* Approximation using 10 ranges based on output measurement on hw v1.2.
2335         * This seems close to the cubic mapping e.g. alsamixer uses. */
2336        static const DECLARE_TLV_DB_RANGE(scale,
2337                 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
2338                 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
2339                 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
2340                 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
2341                15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
2342                17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
2343                20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
2344                27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
2345                32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
2346                41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
2347        );
2348
2349        if (cval->min == 0 && cval->max == 50) {
2350                usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
2351                kctl->tlv.p = scale;
2352                kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
2353                kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2354
2355        } else if (cval->min == 0 && cval->max <= 1000) {
2356                /* Some other clearly broken DragonFly variant.
2357                 * At least a 0..53 variant (hw v1.0) exists.
2358                 */
2359                usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
2360                kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2361        }
2362}
2363
2364void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
2365                                  struct usb_mixer_elem_info *cval, int unitid,
2366                                  struct snd_kcontrol *kctl)
2367{
2368        switch (mixer->chip->usb_id) {
2369        case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
2370                if (unitid == 7 && cval->control == UAC_FU_VOLUME)
2371                        snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
2372                break;
2373        /* lowest playback value is muted on C-Media devices */
2374        case USB_ID(0x0d8c, 0x000c):
2375        case USB_ID(0x0d8c, 0x0014):
2376                if (strstr(kctl->id.name, "Playback"))
2377                        cval->min_mute = 1;
2378                break;
2379        }
2380}
2381
2382