[ALSA] usb-audio - add Extigy/Audigy 2 NX remote control support
[linux-2.6.git] / sound / usb / usbmixer.c
index 7bbccebd76c6408d7606885cec1da78b0fe39ed0..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"
 
@@ -55,6 +56,21 @@ struct usb_mixer_interface {
        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];
 };
 
 
@@ -83,13 +99,15 @@ struct usb_mixer_build {
 
 struct usb_mixer_elem_info {
        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;
 };
 
 
@@ -414,6 +432,7 @@ static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_ou
 
 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(state->chip->card, &kctl->id))
@@ -421,8 +440,12 @@ static int add_control_to_empty(mixer_build_t *state, snd_kcontrol_t *kctl)
        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;
 }
 
 
@@ -1522,6 +1545,14 @@ 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);
 }
 
@@ -1580,6 +1611,209 @@ static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
        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 = {
@@ -1598,12 +1832,25 @@ int snd_usb_create_mixer(snd_usb_audio_t *chip, int 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) {
+       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);
@@ -1615,4 +1862,11 @@ int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
 
 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);
 }