[ALSA] usb-audio - add Extigy/Audigy 2 NX remote control support
[linux-2.6.git] / sound / usb / usbmixer.c
index e601f4ba5f2c95334841f366e58464b9033ce2a3..7ea42d43d7ff2f90a80fc8bcee101ab0c95a221d 100644 (file)
@@ -35,6 +35,7 @@
 #include <linux/usb.h>
 #include <sound/core.h>
 #include <sound/control.h>
+#include <sound/hwdep.h>
 
 #include "usbaudio.h"
 
@@ -50,6 +51,29 @@ typedef struct usb_audio_term usb_audio_term_t;
 typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
 
 
+struct usb_mixer_interface {
+       snd_usb_audio_t *chip;
+       unsigned int ctrlif;
+       struct list_head list;
+       unsigned int ignore_ctl_error;
+       struct urb *urb;
+       usb_mixer_elem_info_t **id_elems; /* array[256], indexed by unit id */
+
+       /* Sound Blaster remote control stuff */
+       enum {
+               RC_NONE,
+               RC_EXTIGY,
+               RC_AUDIGY2NX,
+       } rc_type;
+       unsigned long rc_hwdep_open;
+       u32 rc_code;
+       wait_queue_head_t rc_waitq;
+       struct urb *rc_urb;
+       struct usb_ctrlrequest *rc_setup_packet;
+       u8 rc_buffer[6];
+};
+
+
 struct usb_audio_term {
        int id;
        int type;
@@ -62,9 +86,9 @@ struct usbmix_name_map;
 
 struct usb_mixer_build {
        snd_usb_audio_t *chip;
+       struct usb_mixer_interface *mixer;
        unsigned char *buffer;
        unsigned int buflen;
-       unsigned int ctrlif;
        unsigned short vendor;
        unsigned short product;
        DECLARE_BITMAP(unitbitmap, 256);
@@ -74,15 +98,16 @@ struct usb_mixer_build {
 };
 
 struct usb_mixer_elem_info {
-       snd_usb_audio_t *chip;
-       unsigned int ctrlif;
+       struct usb_mixer_interface *mixer;
+       usb_mixer_elem_info_t *next_id_elem; /* list of controls with same id */
+       snd_ctl_elem_id_t *elem_id;
        unsigned int id;
        unsigned int control;   /* CS or ICN (high byte) */
        unsigned int cmask; /* channel mask bitmap: 0 = master */
        int channels;
        int val_type;
        int min, max, res;
-       unsigned int initialized: 1;
+       u8 initialized;
 };
 
 
@@ -317,16 +342,18 @@ static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, i
        int timeout = 10;
 
        while (timeout-- > 0) {
-               if (snd_usb_ctl_msg(cval->chip->dev, usb_rcvctrlpipe(cval->chip->dev, 0),
+               if (snd_usb_ctl_msg(cval->mixer->chip->dev,
+                                   usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
                                    request,
                                    USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
-                                   validx, cval->ctrlif | (cval->id << 8),
+                                   validx, cval->mixer->ctrlif | (cval->id << 8),
                                    buf, val_len, 100) >= 0) {
                        *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
                        return 0;
                }
        }
-       snd_printdd(KERN_ERR "cannot get ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type);
+       snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
+                   request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
        return -EINVAL;
 }
 
@@ -355,13 +382,15 @@ static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, i
        buf[0] = value_set & 0xff;
        buf[1] = (value_set >> 8) & 0xff;
        while (timeout -- > 0)
-               if (snd_usb_ctl_msg(cval->chip->dev, usb_sndctrlpipe(cval->chip->dev, 0),
+               if (snd_usb_ctl_msg(cval->mixer->chip->dev,
+                                   usb_sndctrlpipe(cval->mixer->chip->dev, 0),
                                    request,
                                    USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
-                                   validx, cval->ctrlif | (cval->id << 8),
+                                   validx, cval->mixer->ctrlif | (cval->id << 8),
                                    buf, val_len, 100) >= 0)
                        return 0;
-       snd_printdd(KERN_ERR "cannot set ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d, data = 0x%x/0x%x\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
+       snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
+                   request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
        return -EINVAL;
 }
 
@@ -401,16 +430,22 @@ static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_ou
  * if failed, give up and free the control instance.
  */
 
-static int add_control_to_empty(snd_card_t *card, snd_kcontrol_t *kctl)
+static int add_control_to_empty(mixer_build_t *state, snd_kcontrol_t *kctl)
 {
+       usb_mixer_elem_info_t *cval = kctl->private_data;
        int err;
-       while (snd_ctl_find_id(card, &kctl->id))
+
+       while (snd_ctl_find_id(state->chip->card, &kctl->id))
                kctl->id.index++;
-       if ((err = snd_ctl_add(card, kctl)) < 0) {
+       if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
                snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
                snd_ctl_free_one(kctl);
+               return err;
        }
-       return err;
+       cval->elem_id = &kctl->id;
+       cval->next_id_elem = state->mixer->id_elems[cval->id];
+       state->mixer->id_elems[cval->id] = cval;
+       return 0;
 }
 
 
@@ -624,7 +659,8 @@ static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
                }
                if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
                    get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
-                       snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", cval->id, cval->ctrlif, cval->control, cval->id);
+                       snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
+                                  cval->id, cval->mixer->ctrlif, cval->control, cval->id);
                        return -EINVAL;
                }
                if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
@@ -684,7 +720,7 @@ static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
                        if (cval->cmask & (1 << c)) {
                                err = get_cur_mix_value(cval, c + 1, &val);
                                if (err < 0) {
-                                       if (cval->chip->ignore_ctl_error) {
+                                       if (cval->mixer->ignore_ctl_error) {
                                                ucontrol->value.integer.value[0] = cval->min;
                                                return 0;
                                        }
@@ -700,7 +736,7 @@ static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
                /* master channel */
                err = get_cur_mix_value(cval, 0, &val);
                if (err < 0) {
-                       if (cval->chip->ignore_ctl_error) {
+                       if (cval->mixer->ignore_ctl_error) {
                                ucontrol->value.integer.value[0] = cval->min;
                                return 0;
                        }
@@ -726,7 +762,7 @@ static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
                        if (cval->cmask & (1 << c)) {
                                err = get_cur_mix_value(cval, c + 1, &oval);
                                if (err < 0) {
-                                       if (cval->chip->ignore_ctl_error)
+                                       if (cval->mixer->ignore_ctl_error)
                                                return 0;
                                        return err;
                                }
@@ -743,7 +779,7 @@ static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
        } else {
                /* master channel */
                err = get_cur_mix_value(cval, 0, &oval);
-               if (err < 0 && cval->chip->ignore_ctl_error)
+               if (err < 0 && cval->mixer->ignore_ctl_error)
                        return 0;
                if (err < 0)
                        return err;
@@ -795,8 +831,7 @@ static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
                snd_printk(KERN_ERR "cannot malloc kcontrol\n");
                return;
        }
-       cval->chip = state->chip;
-       cval->ctrlif = state->ctrlif;
+       cval->mixer = state->mixer;
        cval->id = unitid;
        cval->control = control;
        cval->cmask = ctl_mask;
@@ -880,7 +915,7 @@ static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
 
        snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
                    cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
-       add_control_to_empty(state->chip->card, kctl);
+       add_control_to_empty(state, kctl);
 }
 
 
@@ -963,8 +998,7 @@ static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
        if (! cval)
                return;
 
-       cval->chip = state->chip;
-       cval->ctrlif = state->ctrlif;
+       cval->mixer = state->mixer;
        cval->id = unitid;
        cval->control = in_ch + 1; /* based on 1 */
        cval->val_type = USB_MIXER_S16;
@@ -995,7 +1029,7 @@ static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
 
        snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
                    cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
-       add_control_to_empty(state->chip->card, kctl);
+       add_control_to_empty(state, kctl);
 }
 
 
@@ -1058,7 +1092,7 @@ static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
        int err, val;
 
        err = get_cur_ctl_value(cval, cval->control << 8, &val);
-       if (err < 0 && cval->chip->ignore_ctl_error) {
+       if (err < 0 && cval->mixer->ignore_ctl_error) {
                ucontrol->value.integer.value[0] = cval->min;
                return 0;
        }
@@ -1077,7 +1111,7 @@ static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
 
        err = get_cur_ctl_value(cval, cval->control << 8, &oval);
        if (err < 0) {
-               if (cval->chip->ignore_ctl_error)
+               if (cval->mixer->ignore_ctl_error)
                        return 0;
                return err;
        }
@@ -1215,8 +1249,7 @@ static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char
                        snd_printk(KERN_ERR "cannot malloc kcontrol\n");
                        return -ENOMEM;
                }
-               cval->chip = state->chip;
-               cval->ctrlif = state->ctrlif;
+               cval->mixer = state->mixer;
                cval->id = unitid;
                cval->control = valinfo->control;
                cval->val_type = valinfo->val_type;
@@ -1257,7 +1290,7 @@ static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char
 
                snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
                            cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
-               if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
+               if ((err = add_control_to_empty(state, kctl)) < 0)
                        return err;
        }
        return 0;
@@ -1305,7 +1338,7 @@ static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
 
        err = get_cur_ctl_value(cval, 0, &val);
        if (err < 0) {
-               if (cval->chip->ignore_ctl_error) {
+               if (cval->mixer->ignore_ctl_error) {
                        ucontrol->value.enumerated.item[0] = 0;
                        return 0;
                }
@@ -1324,7 +1357,7 @@ static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t
 
        err = get_cur_ctl_value(cval, 0, &oval);
        if (err < 0) {
-               if (cval->chip->ignore_ctl_error)
+               if (cval->mixer->ignore_ctl_error)
                        return 0;
                return err;
        }
@@ -1402,8 +1435,7 @@ static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned
                snd_printk(KERN_ERR "cannot malloc kcontrol\n");
                return -ENOMEM;
        }
-       cval->chip = state->chip;
-       cval->ctrlif = state->ctrlif;
+       cval->mixer = state->mixer;
        cval->id = unitid;
        cval->val_type = USB_MIXER_U8;
        cval->channels = 1;
@@ -1468,7 +1500,7 @@ static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned
 
        snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
                    cval->id, kctl->id.name, num_ins);
-       if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
+       if ((err = add_control_to_empty(state, kctl)) < 0)
                return err;
 
        return 0;
@@ -1511,27 +1543,46 @@ static int parse_audio_unit(mixer_build_t *state, int unitid)
        }
 }
 
+static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
+{
+       kfree(mixer->id_elems);
+       if (mixer->urb) {
+               kfree(mixer->urb->transfer_buffer);
+               usb_free_urb(mixer->urb);
+       }
+       if (mixer->rc_urb)
+               usb_free_urb(mixer->rc_urb);
+       kfree(mixer->rc_setup_packet);
+       kfree(mixer);
+}
+
+static int snd_usb_mixer_dev_free(snd_device_t *device)
+{
+       struct usb_mixer_interface *mixer = device->device_data;
+       snd_usb_mixer_free(mixer);
+       return 0;
+}
+
 /*
  * create mixer controls
  *
  * walk through all OUTPUT_TERMINAL descriptors to search for mixers
  */
-int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
+static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
 {
        unsigned char *desc;
        mixer_build_t state;
        int err;
        const struct usbmix_ctl_map *map;
-       struct usb_device_descriptor *dev = &chip->dev->descriptor;
-       struct usb_host_interface *hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
-
-       strcpy(chip->card->mixername, "USB Mixer");
+       struct usb_device_descriptor *dev = &mixer->chip->dev->descriptor;
+       struct usb_host_interface *hostif;
 
+       hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
        memset(&state, 0, sizeof(state));
-       state.chip = chip;
+       state.chip = mixer->chip;
+       state.mixer = mixer;
        state.buffer = hostif->extra;
        state.buflen = hostif->extralen;
-       state.ctrlif = ctrlif;
        state.vendor = le16_to_cpu(dev->idVendor);
        state.product = le16_to_cpu(dev->idProduct);
 
@@ -1540,13 +1591,10 @@ int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
                if (map->vendor == state.vendor && map->product == state.product) {
                        state.map = map->map;
                        state.selector_map = map->selector_map;
-                       chip->ignore_ctl_error = map->ignore_ctl_error;
+                       mixer->ignore_ctl_error = map->ignore_ctl_error;
                        break;
                }
        }
-#ifdef IGNORE_CTL_ERROR
-       chip->ignore_ctl_error = 1;
-#endif
 
        desc = NULL;
        while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
@@ -1562,3 +1610,263 @@ int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
        }
        return 0;
 }
+
+static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
+                                   int unitid)
+{
+       usb_mixer_elem_info_t *info;
+
+       for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
+               snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
+                              info->elem_id);
+}
+
+static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
+                                       int unitid)
+{
+       /* SB remote control */
+       if (mixer->rc_type != RC_NONE && unitid == 0) {
+               /* read control code from device memory */
+               mixer->rc_urb->dev = mixer->chip->dev;
+               usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
+       }
+}
+
+static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
+{
+       struct usb_mixer_interface *mixer = urb->context;
+
+       if (urb->status == 0) {
+               u8 *buf = urb->transfer_buffer;
+               int i;
+
+               for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
+                       snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
+                                  buf[0], buf[1]);
+                       /* ignore any notifications not from the control interface */
+                       if ((buf[0] & 0x0f) != 0)
+                               continue;
+                       if (!(buf[0] & 0x40))
+                               snd_usb_mixer_notify_id(mixer, buf[1]);
+                       else
+                               snd_usb_mixer_memory_change(mixer, buf[1]);
+               }
+       }
+       if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
+               urb->dev = mixer->chip->dev;
+               usb_submit_urb(urb, GFP_ATOMIC);
+       }
+}
+
+/* create the handler for the optional status interrupt endpoint */
+static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
+{
+       struct usb_host_interface *hostif;
+       struct usb_endpoint_descriptor *ep;
+       void *transfer_buffer;
+       int buffer_length;
+       unsigned int epnum;
+
+       hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
+       /* we need one interrupt input endpoint */
+       if (get_iface_desc(hostif)->bNumEndpoints < 1)
+               return 0;
+       ep = get_endpoint(hostif, 0);
+       if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
+           (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
+               return 0;
+
+       epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
+       buffer_length = le16_to_cpu(ep->wMaxPacketSize);
+       transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
+       if (!transfer_buffer)
+               return -ENOMEM;
+       mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
+       if (!mixer->urb) {
+               kfree(transfer_buffer);
+               return -ENOMEM;
+       }
+       usb_fill_int_urb(mixer->urb, mixer->chip->dev,
+                        usb_rcvintpipe(mixer->chip->dev, epnum),
+                        transfer_buffer, buffer_length,
+                        snd_usb_mixer_status_complete, mixer, ep->bInterval);
+       usb_submit_urb(mixer->urb, GFP_KERNEL);
+       return 0;
+}
+
+static void snd_usb_soundblaster_remote_complete(struct urb *urb,
+                                                struct pt_regs *regs)
+{
+       struct usb_mixer_interface *mixer = urb->context;
+       /*
+        * format of remote control data:
+        * Extigy:      xx 00
+        * Audigy 2 NX: 06 80 xx 00 00 00
+        */
+       int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2;
+       u32 code;
+
+       if (urb->status < 0 || urb->actual_length <= offset)
+               return;
+       code = mixer->rc_buffer[offset];
+       /* the Mute button actually changes the mixer control */
+       if (code == 13)
+               snd_usb_mixer_notify_id(mixer, 18);
+       mixer->rc_code = code;
+       wmb();
+       wake_up(&mixer->rc_waitq);
+}
+
+static int snd_usb_sbrc_hwdep_open(snd_hwdep_t *hw, struct file *file)
+{
+       struct usb_mixer_interface *mixer = hw->private_data;
+
+       if (test_and_set_bit(0, &mixer->rc_hwdep_open))
+               return -EBUSY;
+       return 0;
+}
+
+static int snd_usb_sbrc_hwdep_release(snd_hwdep_t *hw, struct file *file)
+{
+       struct usb_mixer_interface *mixer = hw->private_data;
+
+       clear_bit(0, &mixer->rc_hwdep_open);
+       smp_mb__after_clear_bit();
+       return 0;
+}
+
+static long snd_usb_sbrc_hwdep_read(snd_hwdep_t *hw, char __user *buf,
+                                    long count, loff_t *offset)
+{
+       struct usb_mixer_interface *mixer = hw->private_data;
+       int err;
+       u32 rc_code;
+
+       if (count != 1)
+               return -EINVAL;
+       err = wait_event_interruptible(mixer->rc_waitq,
+                                      (rc_code = xchg(&mixer->rc_code, 0)) != 0);
+       if (err == 0) {
+               err = put_user(rc_code, buf);
+       }
+       return err < 0 ? err : count;
+}
+
+static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t *hw, struct file *file,
+                                           poll_table *wait)
+{
+       struct usb_mixer_interface *mixer = hw->private_data;
+
+       poll_wait(file, &mixer->rc_waitq, wait);
+       return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
+}
+
+static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
+{
+       snd_hwdep_t *hwdep;
+       int err, len;
+
+       switch (le16_to_cpu(mixer->chip->dev->descriptor.idProduct)) {
+       case 0x3000:
+               mixer->rc_type = RC_EXTIGY;
+               len = 2;
+               break;
+       case 0x3020:
+               mixer->rc_type = RC_AUDIGY2NX;
+               len = 6;
+               break;
+       default:
+               return 0;
+       }
+
+       init_waitqueue_head(&mixer->rc_waitq);
+       err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
+       if (err < 0)
+               return err;
+       snprintf(hwdep->name, sizeof(hwdep->name),
+                "%s remote control", mixer->chip->card->shortname);
+       hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
+       hwdep->private_data = mixer;
+       hwdep->ops.read = snd_usb_sbrc_hwdep_read;
+       hwdep->ops.open = snd_usb_sbrc_hwdep_open;
+       hwdep->ops.release = snd_usb_sbrc_hwdep_release;
+       hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
+
+       mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
+       if (!mixer->rc_urb)
+               return -ENOMEM;
+       mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
+       if (!mixer->rc_setup_packet) {
+               usb_free_urb(mixer->rc_urb);
+               mixer->rc_urb = NULL;
+               return -ENOMEM;
+       }
+       mixer->rc_setup_packet->bRequestType =
+               USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
+       mixer->rc_setup_packet->bRequest = GET_MEM;
+       mixer->rc_setup_packet->wValue = cpu_to_le16(0);
+       mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
+       mixer->rc_setup_packet->wLength = cpu_to_le16(len);
+       usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
+                            usb_rcvctrlpipe(mixer->chip->dev, 0),
+                            (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
+                            snd_usb_soundblaster_remote_complete, mixer);
+       return 0;
+}
+
+int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
+{
+       static snd_device_ops_t dev_ops = {
+               .dev_free = snd_usb_mixer_dev_free
+       };
+       struct usb_mixer_interface *mixer;
+       int err;
+
+       strcpy(chip->card->mixername, "USB Mixer");
+
+       mixer = kcalloc(1, sizeof(*mixer), GFP_KERNEL);
+       if (!mixer)
+               return -ENOMEM;
+       mixer->chip = chip;
+       mixer->ctrlif = ctrlif;
+#ifdef IGNORE_CTL_ERROR
+       mixer->ignore_ctl_error = 1;
+#endif
+       mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
+       if (!mixer->id_elems) {
+               kfree(mixer);
+               return -ENOMEM;
+       }
+
+       if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
+           (err = snd_usb_mixer_status_create(mixer)) < 0) {
+               snd_usb_mixer_free(mixer);
+               return err;
+       }
+
+       if (le16_to_cpu(chip->dev->descriptor.idVendor) == 0x041e) {
+               if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0) {
+                       snd_usb_mixer_free(mixer);
+                       return err;
+               }
+       }
+
+       err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
+       if (err < 0) {
+               snd_usb_mixer_free(mixer);
+               return err;
+       }
+       list_add(&mixer->list, &chip->mixer_list);
+       return 0;
+}
+
+void snd_usb_mixer_disconnect(struct list_head *p)
+{
+       struct usb_mixer_interface *mixer;
+       
+       mixer = list_entry(p, struct usb_mixer_interface, list);
+       if (mixer->urb)
+               usb_kill_urb(mixer->urb);
+       if (mixer->rc_urb)
+               usb_kill_urb(mixer->rc_urb);
+}