[ALSA] Changed Jaroslav Kysela's e-mail from perex@suse.cz to perex@perex.cz
[safe/jmp/linux-2.6] / sound / pci / emu10k1 / emufx.c
index 03e8c16..9bf1cd5 100644 (file)
@@ -1,8 +1,11 @@
 /*
- *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
+ *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  *                   Creative Labs, Inc.
  *  Routines for effect processor FX8010
  *
+ *  Copyright (c) by James Courtier-Dutton <James@superbug.co.uk>
+ *     Added EMU 1010 support.
+ *
  *  BUGS:
  *    --
  *
 
 #include <sound/driver.h>
 #include <linux/pci.h>
+#include <linux/capability.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
+#include <linux/vmalloc.h>
 #include <linux/init.h>
+#include <linux/mutex.h>
+
 #include <sound/core.h>
+#include <sound/tlv.h>
 #include <sound/emu10k1.h>
 
 #if 0          /* for testing purposes - digital out -> capture */
@@ -262,6 +270,7 @@ static const u32 treble_table[41][5] = {
        { 0x37c4448b, 0xa45ef51d, 0x262f3267, 0x081e36dc, 0xfd8f5d14 }
 };
 
+/* dB gain = (float) 20 * log10( float(db_table_value) / 0x8000000 ) */
 static const u32 db_table[101] = {
        0x00000000, 0x01571f82, 0x01674b41, 0x01783a1b, 0x0189f540,
        0x019c8651, 0x01aff763, 0x01c45306, 0x01d9a446, 0x01eff6b8,
@@ -286,6 +295,9 @@ static const u32 db_table[101] = {
        0x7fffffff,
 };
 
+/* EMU10k1/EMU10k2 DSP control db gain */
+static const DECLARE_TLV_DB_SCALE(snd_emu10k1_db_scale1, -4000, 40, 1);
+
 static const u32 onoff_table[2] = {
        0x00000000, 0x00000001
 };
@@ -309,9 +321,10 @@ static inline void snd_leave_user(mm_segment_t fs)
  *   controls
  */
 
-static int snd_emu10k1_gpr_ctl_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_emu10k1_gpr_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
 {
-       snd_emu10k1_fx8010_ctl_t *ctl = (snd_emu10k1_fx8010_ctl_t *)kcontrol->private_value;
+       struct snd_emu10k1_fx8010_ctl *ctl =
+               (struct snd_emu10k1_fx8010_ctl *) kcontrol->private_value;
 
        if (ctl->min == 0 && ctl->max == 1)
                uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
@@ -323,10 +336,11 @@ static int snd_emu10k1_gpr_ctl_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_
        return 0;
 }
 
-static int snd_emu10k1_gpr_ctl_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_emu10k1_gpr_ctl_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
 {
-       emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
-       snd_emu10k1_fx8010_ctl_t *ctl = (snd_emu10k1_fx8010_ctl_t *)kcontrol->private_value;
+       struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_emu10k1_fx8010_ctl *ctl =
+               (struct snd_emu10k1_fx8010_ctl *) kcontrol->private_value;
        unsigned long flags;
        unsigned int i;
        
@@ -337,10 +351,11 @@ static int snd_emu10k1_gpr_ctl_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value
        return 0;
 }
 
-static int snd_emu10k1_gpr_ctl_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_emu10k1_gpr_ctl_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
 {
-       emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
-       snd_emu10k1_fx8010_ctl_t *ctl = (snd_emu10k1_fx8010_ctl_t *)kcontrol->private_value;
+       struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_emu10k1_fx8010_ctl *ctl =
+               (struct snd_emu10k1_fx8010_ctl *) kcontrol->private_value;
        unsigned long flags;
        unsigned int nval, val;
        unsigned int i, j;
@@ -393,9 +408,9 @@ static int snd_emu10k1_gpr_ctl_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value
  *   Interrupt handler
  */
 
-static void snd_emu10k1_fx8010_interrupt(emu10k1_t *emu)
+static void snd_emu10k1_fx8010_interrupt(struct snd_emu10k1 *emu)
 {
-       snd_emu10k1_fx8010_irq_t *irq, *nirq;
+       struct snd_emu10k1_fx8010_irq *irq, *nirq;
 
        irq = emu->fx8010.irq_handlers;
        while (irq) {
@@ -409,13 +424,13 @@ static void snd_emu10k1_fx8010_interrupt(emu10k1_t *emu)
        }
 }
 
-int snd_emu10k1_fx8010_register_irq_handler(emu10k1_t *emu,
-                                                  snd_fx8010_irq_handler_t *handler,
-                                                  unsigned char gpr_running,
-                                                  void *private_data,
-                                                  snd_emu10k1_fx8010_irq_t **r_irq)
+int snd_emu10k1_fx8010_register_irq_handler(struct snd_emu10k1 *emu,
+                                           snd_fx8010_irq_handler_t *handler,
+                                           unsigned char gpr_running,
+                                           void *private_data,
+                                           struct snd_emu10k1_fx8010_irq **r_irq)
 {
-       snd_emu10k1_fx8010_irq_t *irq;
+       struct snd_emu10k1_fx8010_irq *irq;
        unsigned long flags;
        
        irq = kmalloc(sizeof(*irq), GFP_ATOMIC);
@@ -440,10 +455,10 @@ int snd_emu10k1_fx8010_register_irq_handler(emu10k1_t *emu,
        return 0;
 }
 
-int snd_emu10k1_fx8010_unregister_irq_handler(emu10k1_t *emu,
-                                             snd_emu10k1_fx8010_irq_t *irq)
+int snd_emu10k1_fx8010_unregister_irq_handler(struct snd_emu10k1 *emu,
+                                             struct snd_emu10k1_fx8010_irq *irq)
 {
-       snd_emu10k1_fx8010_irq_t *tmp;
+       struct snd_emu10k1_fx8010_irq *tmp;
        unsigned long flags;
        
        spin_lock_irqsave(&emu->fx8010.irq_lock, flags);
@@ -468,7 +483,8 @@ int snd_emu10k1_fx8010_unregister_irq_handler(emu10k1_t *emu,
  * EMU10K1 effect manager
  *************************************************************************/
 
-static void snd_emu10k1_write_op(emu10k1_fx8010_code_t *icode, unsigned int *ptr,
+static void snd_emu10k1_write_op(struct snd_emu10k1_fx8010_code *icode,
+                                unsigned int *ptr,
                                 u32 op, u32 r, u32 a, u32 x, u32 y)
 {
        u_int32_t *code;
@@ -483,7 +499,8 @@ static void snd_emu10k1_write_op(emu10k1_fx8010_code_t *icode, unsigned int *ptr
 #define OP(icode, ptr, op, r, a, x, y) \
        snd_emu10k1_write_op(icode, ptr, op, r, a, x, y)
 
-static void snd_emu10k1_audigy_write_op(emu10k1_fx8010_code_t *icode, unsigned int *ptr,
+static void snd_emu10k1_audigy_write_op(struct snd_emu10k1_fx8010_code *icode,
+                                       unsigned int *ptr,
                                        u32 op, u32 r, u32 a, u32 x, u32 y)
 {
        u_int32_t *code;
@@ -498,19 +515,20 @@ static void snd_emu10k1_audigy_write_op(emu10k1_fx8010_code_t *icode, unsigned i
 #define A_OP(icode, ptr, op, r, a, x, y) \
        snd_emu10k1_audigy_write_op(icode, ptr, op, r, a, x, y)
 
-static void snd_emu10k1_efx_write(emu10k1_t *emu, unsigned int pc, unsigned int data)
+static void snd_emu10k1_efx_write(struct snd_emu10k1 *emu, unsigned int pc, unsigned int data)
 {
        pc += emu->audigy ? A_MICROCODEBASE : MICROCODEBASE;
        snd_emu10k1_ptr_write(emu, pc, 0, data);
 }
 
-unsigned int snd_emu10k1_efx_read(emu10k1_t *emu, unsigned int pc)
+unsigned int snd_emu10k1_efx_read(struct snd_emu10k1 *emu, unsigned int pc)
 {
        pc += emu->audigy ? A_MICROCODEBASE : MICROCODEBASE;
        return snd_emu10k1_ptr_read(emu, pc, 0);
 }
 
-static int snd_emu10k1_gpr_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_gpr_poke(struct snd_emu10k1 *emu,
+                               struct snd_emu10k1_fx8010_code *icode)
 {
        int gpr;
        u32 val;
@@ -525,7 +543,8 @@ static int snd_emu10k1_gpr_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        return 0;
 }
 
-static int snd_emu10k1_gpr_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_gpr_peek(struct snd_emu10k1 *emu,
+                               struct snd_emu10k1_fx8010_code *icode)
 {
        int gpr;
        u32 val;
@@ -539,7 +558,8 @@ static int snd_emu10k1_gpr_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        return 0;
 }
 
-static int snd_emu10k1_tram_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_tram_poke(struct snd_emu10k1 *emu,
+                                struct snd_emu10k1_fx8010_code *icode)
 {
        int tram;
        u32 addr, val;
@@ -561,7 +581,8 @@ static int snd_emu10k1_tram_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        return 0;
 }
 
-static int snd_emu10k1_tram_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_tram_peek(struct snd_emu10k1 *emu,
+                                struct snd_emu10k1_fx8010_code *icode)
 {
        int tram;
        u32 val, addr;
@@ -583,7 +604,8 @@ static int snd_emu10k1_tram_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        return 0;
 }
 
-static int snd_emu10k1_code_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_code_poke(struct snd_emu10k1 *emu,
+                                struct snd_emu10k1_fx8010_code *icode)
 {
        u32 pc, lo, hi;
 
@@ -599,7 +621,8 @@ static int snd_emu10k1_code_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        return 0;
 }
 
-static int snd_emu10k1_code_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_code_peek(struct snd_emu10k1 *emu,
+                                struct snd_emu10k1_fx8010_code *icode)
 {
        u32 pc;
 
@@ -614,14 +637,13 @@ static int snd_emu10k1_code_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        return 0;
 }
 
-static snd_emu10k1_fx8010_ctl_t *snd_emu10k1_look_for_ctl(emu10k1_t *emu, snd_ctl_elem_id_t *id)
+static struct snd_emu10k1_fx8010_ctl *
+snd_emu10k1_look_for_ctl(struct snd_emu10k1 *emu, struct snd_ctl_elem_id *id)
 {
-       snd_emu10k1_fx8010_ctl_t *ctl;
-       snd_kcontrol_t *kcontrol;
-       struct list_head *list;
-       
-       list_for_each(list, &emu->fx8010.gpr_ctl) {
-               ctl = emu10k1_gpr_ctl(list);
+       struct snd_emu10k1_fx8010_ctl *ctl;
+       struct snd_kcontrol *kcontrol;
+
+       list_for_each_entry(ctl, &emu->fx8010.gpr_ctl, list) {
                kcontrol = ctl->kcontrol;
                if (kcontrol->id.iface == id->iface &&
                    !strcmp(kcontrol->id.name, id->name) &&
@@ -631,13 +653,67 @@ static snd_emu10k1_fx8010_ctl_t *snd_emu10k1_look_for_ctl(emu10k1_t *emu, snd_ct
        return NULL;
 }
 
-static int snd_emu10k1_verify_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+#define MAX_TLV_SIZE   256
+
+static unsigned int *copy_tlv(const unsigned int __user *_tlv)
+{
+       unsigned int data[2];
+       unsigned int *tlv;
+
+       if (!_tlv)
+               return NULL;
+       if (copy_from_user(data, _tlv, sizeof(data)))
+               return NULL;
+       if (data[1] >= MAX_TLV_SIZE)
+               return NULL;
+       tlv = kmalloc(data[1] * 4 + sizeof(data), GFP_KERNEL);
+       if (!tlv)
+               return NULL;
+       memcpy(tlv, data, sizeof(data));
+       if (copy_from_user(tlv + 2, _tlv + 2, data[1])) {
+               kfree(tlv);
+               return NULL;
+       }
+       return tlv;
+}
+
+static int copy_gctl(struct snd_emu10k1 *emu,
+                    struct snd_emu10k1_fx8010_control_gpr *gctl,
+                    struct snd_emu10k1_fx8010_control_gpr __user *_gctl,
+                    int idx)
+{
+       struct snd_emu10k1_fx8010_control_old_gpr __user *octl;
+
+       if (emu->support_tlv)
+               return copy_from_user(gctl, &_gctl[idx], sizeof(*gctl));
+       octl = (struct snd_emu10k1_fx8010_control_old_gpr __user *)_gctl;
+       if (copy_from_user(gctl, &octl[idx], sizeof(*octl)))
+               return -EFAULT;
+       gctl->tlv = NULL;
+       return 0;
+}
+
+static int copy_gctl_to_user(struct snd_emu10k1 *emu,
+                    struct snd_emu10k1_fx8010_control_gpr __user *_gctl,
+                    struct snd_emu10k1_fx8010_control_gpr *gctl,
+                    int idx)
+{
+       struct snd_emu10k1_fx8010_control_old_gpr __user *octl;
+
+       if (emu->support_tlv)
+               return copy_to_user(&_gctl[idx], gctl, sizeof(*gctl));
+       
+       octl = (struct snd_emu10k1_fx8010_control_old_gpr __user *)_gctl;
+       return copy_to_user(&octl[idx], gctl, sizeof(*octl));
+}
+
+static int snd_emu10k1_verify_controls(struct snd_emu10k1 *emu,
+                                      struct snd_emu10k1_fx8010_code *icode)
 {
        unsigned int i;
-       snd_ctl_elem_id_t __user *_id;
-       snd_ctl_elem_id_t id;
-       emu10k1_fx8010_control_gpr_t __user *_gctl;
-       emu10k1_fx8010_control_gpr_t *gctl;
+       struct snd_ctl_elem_id __user *_id;
+       struct snd_ctl_elem_id id;
+       struct snd_emu10k1_fx8010_control_gpr *gctl;
        int err;
        
        for (i = 0, _id = icode->gpr_del_controls;
@@ -651,9 +727,8 @@ static int snd_emu10k1_verify_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *ic
        if (! gctl)
                return -ENOMEM;
        err = 0;
-       for (i = 0, _gctl = icode->gpr_add_controls;
-            i < icode->gpr_add_control_count; i++, _gctl++) {
-               if (copy_from_user(gctl, _gctl, sizeof(*gctl))) {
+       for (i = 0; i < icode->gpr_add_control_count; i++) {
+               if (copy_gctl(emu, gctl, icode->gpr_add_controls, i)) {
                        err = -EFAULT;
                        goto __error;
                }
@@ -672,10 +747,9 @@ static int snd_emu10k1_verify_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *ic
                        goto __error;
                }
        }
-       for (i = 0, _gctl = icode->gpr_list_controls;
-            i < icode->gpr_list_control_count; i++, _gctl++) {
+       for (i = 0; i < icode->gpr_list_control_count; i++) {
                /* FIXME: we need to check the WRITE access */
-               if (copy_from_user(gctl, _gctl, sizeof(*gctl))) {
+               if (copy_gctl(emu, gctl, icode->gpr_list_controls, i)) {
                        err = -EFAULT;
                        goto __error;
                }
@@ -685,28 +759,30 @@ static int snd_emu10k1_verify_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *ic
        return err;
 }
 
-static void snd_emu10k1_ctl_private_free(snd_kcontrol_t *kctl)
+static void snd_emu10k1_ctl_private_free(struct snd_kcontrol *kctl)
 {
-       snd_emu10k1_fx8010_ctl_t *ctl;
+       struct snd_emu10k1_fx8010_ctl *ctl;
        
-       ctl = (snd_emu10k1_fx8010_ctl_t *)kctl->private_value;
+       ctl = (struct snd_emu10k1_fx8010_ctl *) kctl->private_value;
        kctl->private_value = 0;
        list_del(&ctl->list);
        kfree(ctl);
+       if (kctl->tlv.p)
+               kfree(kctl->tlv.p);
 }
 
-static int snd_emu10k1_add_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_add_controls(struct snd_emu10k1 *emu,
+                                   struct snd_emu10k1_fx8010_code *icode)
 {
        unsigned int i, j;
-       emu10k1_fx8010_control_gpr_t __user *_gctl;
-       emu10k1_fx8010_control_gpr_t *gctl;
-       snd_emu10k1_fx8010_ctl_t *ctl, *nctl;
-       snd_kcontrol_new_t knew;
-       snd_kcontrol_t *kctl;
-       snd_ctl_elem_value_t *val;
+       struct snd_emu10k1_fx8010_control_gpr *gctl;
+       struct snd_emu10k1_fx8010_ctl *ctl, *nctl;
+       struct snd_kcontrol_new knew;
+       struct snd_kcontrol *kctl;
+       struct snd_ctl_elem_value *val;
        int err = 0;
 
-       val = (snd_ctl_elem_value_t *)kmalloc(sizeof(*val), GFP_KERNEL);
+       val = kmalloc(sizeof(*val), GFP_KERNEL);
        gctl = kmalloc(sizeof(*gctl), GFP_KERNEL);
        nctl = kmalloc(sizeof(*nctl), GFP_KERNEL);
        if (!val || !gctl || !nctl) {
@@ -714,9 +790,8 @@ static int snd_emu10k1_add_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode
                goto __error;
        }
 
-       for (i = 0, _gctl = icode->gpr_add_controls;
-            i < icode->gpr_add_control_count; i++, _gctl++) {
-               if (copy_from_user(gctl, _gctl, sizeof(*gctl))) {
+       for (i = 0; i < icode->gpr_add_control_count; i++) {
+               if (copy_gctl(emu, gctl, icode->gpr_add_controls, i)) {
                        err = -EFAULT;
                        goto __error;
                }
@@ -737,6 +812,10 @@ static int snd_emu10k1_add_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode
                knew.device = gctl->id.device;
                knew.subdevice = gctl->id.subdevice;
                knew.info = snd_emu10k1_gpr_ctl_info;
+               knew.tlv.p = copy_tlv(gctl->tlv);
+               if (knew.tlv.p)
+                       knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                               SNDRV_CTL_ELEM_ACCESS_TLV_READ;
                knew.get = snd_emu10k1_gpr_ctl_get;
                knew.put = snd_emu10k1_gpr_ctl_put;
                memset(nctl, 0, sizeof(*nctl));
@@ -751,15 +830,17 @@ static int snd_emu10k1_add_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode
                nctl->max = gctl->max;
                nctl->translation = gctl->translation;
                if (ctl == NULL) {
-                       ctl = (snd_emu10k1_fx8010_ctl_t *)kmalloc(sizeof(*ctl), GFP_KERNEL);
+                       ctl = kmalloc(sizeof(*ctl), GFP_KERNEL);
                        if (ctl == NULL) {
                                err = -ENOMEM;
+                               kfree(knew.tlv.p);
                                goto __error;
                        }
                        knew.private_value = (unsigned long)ctl;
                        *ctl = *nctl;
                        if ((err = snd_ctl_add(emu->card, kctl = snd_ctl_new1(&knew, emu))) < 0) {
                                kfree(ctl);
+                               kfree(knew.tlv.p);
                                goto __error;
                        }
                        kctl->private_free = snd_emu10k1_ctl_private_free;
@@ -782,13 +863,14 @@ static int snd_emu10k1_add_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode
        return err;
 }
 
-static int snd_emu10k1_del_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_del_controls(struct snd_emu10k1 *emu,
+                                   struct snd_emu10k1_fx8010_code *icode)
 {
        unsigned int i;
-       snd_ctl_elem_id_t id;
-       snd_ctl_elem_id_t __user *_id;
-       snd_emu10k1_fx8010_ctl_t *ctl;
-       snd_card_t *card = emu->card;
+       struct snd_ctl_elem_id id;
+       struct snd_ctl_elem_id __user *_id;
+       struct snd_emu10k1_fx8010_ctl *ctl;
+       struct snd_card *card = emu->card;
        
        for (i = 0, _id = icode->gpr_del_controls;
             i < icode->gpr_del_control_count; i++, _id++) {
@@ -803,25 +885,23 @@ static int snd_emu10k1_del_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode
        return 0;
 }
 
-static int snd_emu10k1_list_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_list_controls(struct snd_emu10k1 *emu,
+                                    struct snd_emu10k1_fx8010_code *icode)
 {
        unsigned int i = 0, j;
        unsigned int total = 0;
-       emu10k1_fx8010_control_gpr_t *gctl;
-       emu10k1_fx8010_control_gpr_t __user *_gctl;
-       snd_emu10k1_fx8010_ctl_t *ctl;
-       snd_ctl_elem_id_t *id;
-       struct list_head *list;
+       struct snd_emu10k1_fx8010_control_gpr *gctl;
+       struct snd_emu10k1_fx8010_ctl *ctl;
+       struct snd_ctl_elem_id *id;
 
        gctl = kmalloc(sizeof(*gctl), GFP_KERNEL);
        if (! gctl)
                return -ENOMEM;
 
-       _gctl = icode->gpr_list_controls;       
-       list_for_each(list, &emu->fx8010.gpr_ctl) {
-               ctl = emu10k1_gpr_ctl(list);
+       list_for_each_entry(ctl, &emu->fx8010.gpr_ctl, list) {
                total++;
-               if (_gctl && i < icode->gpr_list_control_count) {
+               if (icode->gpr_list_controls &&
+                   i < icode->gpr_list_control_count) {
                        memset(gctl, 0, sizeof(*gctl));
                        id = &ctl->kcontrol->id;
                        gctl->id.iface = id->iface;
@@ -838,11 +918,11 @@ static int snd_emu10k1_list_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icod
                        gctl->min = ctl->min;
                        gctl->max = ctl->max;
                        gctl->translation = ctl->translation;
-                       if (copy_to_user(_gctl, gctl, sizeof(*gctl))) {
+                       if (copy_gctl_to_user(emu, icode->gpr_list_controls,
+                                             gctl, i)) {
                                kfree(gctl);
                                return -EFAULT;
                        }
-                       _gctl++;
                        i++;
                }
        }
@@ -851,11 +931,12 @@ static int snd_emu10k1_list_controls(emu10k1_t *emu, emu10k1_fx8010_code_t *icod
        return 0;
 }
 
-static int snd_emu10k1_icode_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_icode_poke(struct snd_emu10k1 *emu,
+                                 struct snd_emu10k1_fx8010_code *icode)
 {
        int err = 0;
 
-       down(&emu->fx8010.lock);
+       mutex_lock(&emu->fx8010.lock);
        if ((err = snd_emu10k1_verify_controls(emu, icode)) < 0)
                goto __error;
        strlcpy(emu->fx8010.name, icode->name, sizeof(emu->fx8010.name));
@@ -878,15 +959,16 @@ static int snd_emu10k1_icode_poke(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
        else
                snd_emu10k1_ptr_write(emu, DBG, 0, emu->fx8010.dbg);
       __error:
-       up(&emu->fx8010.lock);
+       mutex_unlock(&emu->fx8010.lock);
        return err;
 }
 
-static int snd_emu10k1_icode_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
+static int snd_emu10k1_icode_peek(struct snd_emu10k1 *emu,
+                                 struct snd_emu10k1_fx8010_code *icode)
 {
        int err;
 
-       down(&emu->fx8010.lock);
+       mutex_lock(&emu->fx8010.lock);
        strlcpy(icode->name, emu->fx8010.name, sizeof(icode->name));
        /* ok, do the main job */
        err = snd_emu10k1_gpr_peek(emu, icode);
@@ -896,22 +978,23 @@ static int snd_emu10k1_icode_peek(emu10k1_t *emu, emu10k1_fx8010_code_t *icode)
                err = snd_emu10k1_code_peek(emu, icode);
        if (err >= 0)
                err = snd_emu10k1_list_controls(emu, icode);
-       up(&emu->fx8010.lock);
+       mutex_unlock(&emu->fx8010.lock);
        return err;
 }
 
-static int snd_emu10k1_ipcm_poke(emu10k1_t *emu, emu10k1_fx8010_pcm_t *ipcm)
+static int snd_emu10k1_ipcm_poke(struct snd_emu10k1 *emu,
+                                struct snd_emu10k1_fx8010_pcm_rec *ipcm)
 {
        unsigned int i;
        int err = 0;
-       snd_emu10k1_fx8010_pcm_t *pcm;
+       struct snd_emu10k1_fx8010_pcm *pcm;
 
        if (ipcm->substream >= EMU10K1_FX8010_PCM_COUNT)
                return -EINVAL;
        if (ipcm->channels > 32)
                return -EINVAL;
        pcm = &emu->fx8010.pcm[ipcm->substream];
-       down(&emu->fx8010.lock);
+       mutex_lock(&emu->fx8010.lock);
        spin_lock_irq(&emu->reg_lock);
        if (pcm->opened) {
                err = -EBUSY;
@@ -941,20 +1024,21 @@ static int snd_emu10k1_ipcm_poke(emu10k1_t *emu, emu10k1_fx8010_pcm_t *ipcm)
        }
       __error:
        spin_unlock_irq(&emu->reg_lock);
-       up(&emu->fx8010.lock);
+       mutex_unlock(&emu->fx8010.lock);
        return err;
 }
 
-static int snd_emu10k1_ipcm_peek(emu10k1_t *emu, emu10k1_fx8010_pcm_t *ipcm)
+static int snd_emu10k1_ipcm_peek(struct snd_emu10k1 *emu,
+                                struct snd_emu10k1_fx8010_pcm_rec *ipcm)
 {
        unsigned int i;
        int err = 0;
-       snd_emu10k1_fx8010_pcm_t *pcm;
+       struct snd_emu10k1_fx8010_pcm *pcm;
 
        if (ipcm->substream >= EMU10K1_FX8010_PCM_COUNT)
                return -EINVAL;
        pcm = &emu->fx8010.pcm[ipcm->substream];
-       down(&emu->fx8010.lock);
+       mutex_lock(&emu->fx8010.lock);
        spin_lock_irq(&emu->reg_lock);
        ipcm->channels = pcm->channels;
        ipcm->tram_start = pcm->tram_start;
@@ -970,7 +1054,7 @@ static int snd_emu10k1_ipcm_peek(emu10k1_t *emu, emu10k1_fx8010_pcm_t *ipcm)
        ipcm->res1 = ipcm->res2 = 0;
        ipcm->pad = 0;
        spin_unlock_irq(&emu->reg_lock);
-       up(&emu->fx8010.lock);
+       mutex_unlock(&emu->fx8010.lock);
        return err;
 }
 
@@ -979,7 +1063,9 @@ static int snd_emu10k1_ipcm_peek(emu10k1_t *emu, emu10k1_fx8010_pcm_t *ipcm)
 #define SND_EMU10K1_PLAYBACK_CHANNELS  8
 #define SND_EMU10K1_CAPTURE_CHANNELS   4
 
-static void __devinit snd_emu10k1_init_mono_control(emu10k1_fx8010_control_gpr_t *ctl, const char *name, int gpr, int defval)
+static void __devinit
+snd_emu10k1_init_mono_control(struct snd_emu10k1_fx8010_control_gpr *ctl,
+                             const char *name, int gpr, int defval)
 {
        ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
        strcpy(ctl->id.name, name);
@@ -987,10 +1073,13 @@ static void __devinit snd_emu10k1_init_mono_control(emu10k1_fx8010_control_gpr_t
        ctl->gpr[0] = gpr + 0; ctl->value[0] = defval;
        ctl->min = 0;
        ctl->max = 100;
+       ctl->tlv = snd_emu10k1_db_scale1;
        ctl->translation = EMU10K1_GPR_TRANSLATION_TABLE100;    
 }
 
-static void __devinit snd_emu10k1_init_stereo_control(emu10k1_fx8010_control_gpr_t *ctl, const char *name, int gpr, int defval)
+static void __devinit
+snd_emu10k1_init_stereo_control(struct snd_emu10k1_fx8010_control_gpr *ctl,
+                               const char *name, int gpr, int defval)
 {
        ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
        strcpy(ctl->id.name, name);
@@ -999,10 +1088,13 @@ static void __devinit snd_emu10k1_init_stereo_control(emu10k1_fx8010_control_gpr
        ctl->gpr[1] = gpr + 1; ctl->value[1] = defval;
        ctl->min = 0;
        ctl->max = 100;
+       ctl->tlv = snd_emu10k1_db_scale1;
        ctl->translation = EMU10K1_GPR_TRANSLATION_TABLE100;
 }
 
-static void __devinit snd_emu10k1_init_mono_onoff_control(emu10k1_fx8010_control_gpr_t *ctl, const char *name, int gpr, int defval)
+static void __devinit
+snd_emu10k1_init_mono_onoff_control(struct snd_emu10k1_fx8010_control_gpr *ctl,
+                                   const char *name, int gpr, int defval)
 {
        ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
        strcpy(ctl->id.name, name);
@@ -1013,7 +1105,9 @@ static void __devinit snd_emu10k1_init_mono_onoff_control(emu10k1_fx8010_control
        ctl->translation = EMU10K1_GPR_TRANSLATION_ONOFF;
 }
 
-static void __devinit snd_emu10k1_init_stereo_onoff_control(emu10k1_fx8010_control_gpr_t *ctl, const char *name, int gpr, int defval)
+static void __devinit
+snd_emu10k1_init_stereo_onoff_control(struct snd_emu10k1_fx8010_control_gpr *ctl,
+                                     const char *name, int gpr, int defval)
 {
        ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
        strcpy(ctl->id.name, name);
@@ -1025,30 +1119,51 @@ static void __devinit snd_emu10k1_init_stereo_onoff_control(emu10k1_fx8010_contr
        ctl->translation = EMU10K1_GPR_TRANSLATION_ONOFF;
 }
 
+/*
+ * Used for emu1010 - conversion from 32-bit capture inputs from HANA
+ * to 2 x 16-bit registers in audigy - their values are read via DMA.
+ * Conversion is performed by Audigy DSP instructions of FX8010.
+ */
+static int snd_emu10k1_audigy_dsp_convert_32_to_2x16(
+                               struct snd_emu10k1_fx8010_code *icode,
+                               u32 *ptr, int tmp, int bit_shifter16,
+                               int reg_in, int reg_out)
+{
+       A_OP(icode, ptr, iACC3, A_GPR(tmp + 1), reg_in, A_C_00000000, A_C_00000000);
+       A_OP(icode, ptr, iANDXOR, A_GPR(tmp), A_GPR(tmp + 1), A_GPR(bit_shifter16 - 1), A_C_00000000);
+       A_OP(icode, ptr, iTSTNEG, A_GPR(tmp + 2), A_GPR(tmp), A_C_80000000, A_GPR(bit_shifter16 - 2));
+       A_OP(icode, ptr, iANDXOR, A_GPR(tmp + 2), A_GPR(tmp + 2), A_C_80000000, A_C_00000000);
+       A_OP(icode, ptr, iANDXOR, A_GPR(tmp), A_GPR(tmp), A_GPR(bit_shifter16 - 3), A_C_00000000);
+       A_OP(icode, ptr, iMACINT0, A_GPR(tmp), A_C_00000000, A_GPR(tmp), A_C_00010000);
+       A_OP(icode, ptr, iANDXOR, reg_out, A_GPR(tmp), A_C_ffffffff, A_GPR(tmp + 2));
+       A_OP(icode, ptr, iACC3, reg_out + 1, A_GPR(tmp + 1), A_C_00000000, A_C_00000000);
+       return 1;
+}
 
 /*
  * initial DSP configuration for Audigy
  */
 
-static int __devinit _snd_emu10k1_audigy_init_efx(emu10k1_t *emu)
+static int __devinit _snd_emu10k1_audigy_init_efx(struct snd_emu10k1 *emu)
 {
        int err, i, z, gpr, nctl;
+       int bit_shifter16;
        const int playback = 10;
        const int capture = playback + (SND_EMU10K1_PLAYBACK_CHANNELS * 2); /* we reserve 10 voices */
        const int stereo_mix = capture + 2;
        const int tmp = 0x88;
        u32 ptr;
-       emu10k1_fx8010_code_t *icode = NULL;
-       emu10k1_fx8010_control_gpr_t *controls = NULL, *ctl;
+       struct snd_emu10k1_fx8010_code *icode = NULL;
+       struct snd_emu10k1_fx8010_control_gpr *controls = NULL, *ctl;
        u32 *gpr_map;
        mm_segment_t seg;
 
-       spin_lock_init(&emu->fx8010.irq_lock);
-       INIT_LIST_HEAD(&emu->fx8010.gpr_ctl);
-
        if ((icode = kzalloc(sizeof(*icode), GFP_KERNEL)) == NULL ||
-           (icode->gpr_map = (u_int32_t __user *)kcalloc(512 + 256 + 256 + 2 * 1024, sizeof(u_int32_t), GFP_KERNEL)) == NULL ||
-           (controls = kcalloc(SND_EMU10K1_GPR_CONTROLS, sizeof(*controls), GFP_KERNEL)) == NULL) {
+           (icode->gpr_map = (u_int32_t __user *)
+            kcalloc(512 + 256 + 256 + 2 * 1024, sizeof(u_int32_t),
+                    GFP_KERNEL)) == NULL ||
+           (controls = kcalloc(SND_EMU10K1_GPR_CONTROLS,
+                               sizeof(*controls), GFP_KERNEL)) == NULL) {
                err = -ENOMEM;
                goto __err;
        }
@@ -1070,16 +1185,25 @@ static int __devinit _snd_emu10k1_audigy_init_efx(emu10k1_t *emu)
        ptr = 0;
        nctl = 0;
        gpr = stereo_mix + 10;
+       gpr_map[gpr++] = 0x00007fff;
+       gpr_map[gpr++] = 0x00008000;
+       gpr_map[gpr++] = 0x0000ffff;
+       bit_shifter16 = gpr;
 
        /* stop FX processor */
        snd_emu10k1_ptr_write(emu, A_DBG, 0, (emu->fx8010.dbg = 0) | A_DBG_SINGLE_STEP);
 
-       /* PCM front Playback Volume (independent from stereo mix) */
+#if 1
+       /* PCM front Playback Volume (independent from stereo mix)
+        * playback = 0 + ( gpr * FXBUS_PCM_LEFT_FRONT >> 31)
+        * where gpr contains attenuation from corresponding mixer control
+        * (snd_emu10k1_init_stereo_control)
+        */
        A_OP(icode, &ptr, iMAC0, A_GPR(playback), A_C_00000000, A_GPR(gpr), A_FXBUS(FXBUS_PCM_LEFT_FRONT));
        A_OP(icode, &ptr, iMAC0, A_GPR(playback+1), A_C_00000000, A_GPR(gpr+1), A_FXBUS(FXBUS_PCM_RIGHT_FRONT));
        snd_emu10k1_init_stereo_control(&controls[nctl++], "PCM Front Playback Volume", gpr, 100);
        gpr += 2;
-       
+
        /* PCM Surround Playback (independent from stereo mix) */
        A_OP(icode, &ptr, iMAC0, A_GPR(playback+2), A_C_00000000, A_GPR(gpr), A_FXBUS(FXBUS_PCM_LEFT_REAR));
        A_OP(icode, &ptr, iMAC0, A_GPR(playback+3), A_C_00000000, A_GPR(gpr+1), A_FXBUS(FXBUS_PCM_RIGHT_REAR));
@@ -1130,13 +1254,28 @@ static int __devinit _snd_emu10k1_audigy_init_efx(emu10k1_t *emu)
        A_OP(icode, &ptr, iMAC0, A_GPR(capture+1), A_GPR(capture+1), A_GPR(gpr+1), A_FXBUS(FXBUS_MIDI_RIGHT));
        snd_emu10k1_init_stereo_control(&controls[nctl++], "Synth Capture Volume", gpr, 0);
        gpr += 2;
-
+      
        /*
         * inputs
         */
 #define A_ADD_VOLUME_IN(var,vol,input) \
 A_OP(icode, &ptr, iMAC0, A_GPR(var), A_GPR(var), A_GPR(vol), A_EXTIN(input))
 
+       /* emu1212 DSP 0 and DSP 1 Capture */
+       if (emu->card_capabilities->emu1010) {
+               if (emu->card_capabilities->ca0108_chip) {
+                       /* Note:JCD:No longer bit shift lower 16bits to upper 16bits of 32bit value. */
+                       A_OP(icode, &ptr, iMACINT0, A_GPR(tmp), A_C_00000000, A3_EMU32IN(0x0), A_C_00000001);
+                       A_OP(icode, &ptr, iMAC0, A_GPR(capture+0), A_GPR(capture+0), A_GPR(gpr), A_GPR(tmp));
+                       A_OP(icode, &ptr, iMACINT0, A_GPR(tmp), A_C_00000000, A3_EMU32IN(0x1), A_C_00000001);
+                       A_OP(icode, &ptr, iMAC0, A_GPR(capture+1), A_GPR(capture+1), A_GPR(gpr), A_GPR(tmp));
+               } else {
+                       A_OP(icode, &ptr, iMAC0, A_GPR(capture+0), A_GPR(capture+0), A_GPR(gpr), A_P16VIN(0x0));
+                       A_OP(icode, &ptr, iMAC0, A_GPR(capture+1), A_GPR(capture+1), A_GPR(gpr+1), A_P16VIN(0x1));
+               }
+               snd_emu10k1_init_stereo_control(&controls[nctl++], "EMU Capture Volume", gpr, 0);
+               gpr += 2;
+       }
        /* AC'97 Playback Volume - used only for mic (renamed later) */
        A_ADD_VOLUME_IN(stereo_mix, gpr, A_EXTIN_AC97_L);
        A_ADD_VOLUME_IN(stereo_mix+1, gpr+1, A_EXTIN_AC97_R);
@@ -1377,6 +1516,17 @@ A_OP(icode, &ptr, iMAC0, A_GPR(var), A_GPR(var), A_GPR(vol), A_EXTIN(input))
 
        /* digital outputs */
        /* A_PUT_STEREO_OUTPUT(A_EXTOUT_FRONT_L, A_EXTOUT_FRONT_R, playback + SND_EMU10K1_PLAYBACK_CHANNELS); */
+       if (emu->card_capabilities->emu1010) {
+               /* EMU1010 Outputs from PCM Front, Rear, Center, LFE, Side */
+               snd_printk("EMU outputs on\n");
+               for (z = 0; z < 8; z++) {
+                       if (emu->card_capabilities->ca0108_chip) {
+                               A_OP(icode, &ptr, iACC3, A3_EMU32OUT(z), A_GPR(playback + SND_EMU10K1_PLAYBACK_CHANNELS + z), A_C_00000000, A_C_00000000);
+                       } else {
+                               A_OP(icode, &ptr, iACC3, A_EMU32OUTL(z), A_GPR(playback + SND_EMU10K1_PLAYBACK_CHANNELS + z), A_C_00000000, A_C_00000000);
+                       }
+               }
+       }
 
        /* IEC958 Optical Raw Playback Switch */ 
        gpr_map[gpr++] = 0;
@@ -1414,11 +1564,135 @@ A_OP(icode, &ptr, iMAC0, A_GPR(var), A_GPR(var), A_GPR(vol), A_EXTIN(input))
        A_PUT_OUTPUT(A_EXTOUT_ADC_CAP_R, capture+1);
 #endif
 
-       /* EFX capture - capture the 16 EXTINs */
-       for (z = 0; z < 16; z++) {
-               A_OP(icode, &ptr, iACC3, A_FXBUS2(z), A_C_00000000, A_C_00000000, A_EXTIN(z));
+       if (emu->card_capabilities->emu1010) {
+               if (emu->card_capabilities->ca0108_chip) {
+                       snd_printk("EMU2 inputs on\n");
+                       for (z = 0; z < 0x10; z++) {
+                               snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, 
+                                                                       bit_shifter16,
+                                                                       A3_EMU32IN(z),
+                                                                       A_FXBUS2(z*2) );
+                       }
+               } else {
+                       snd_printk("EMU inputs on\n");
+                       /* Capture 16 (originally 8) channels of S32_LE sound */
+
+                       /* printk("emufx.c: gpr=0x%x, tmp=0x%x\n",gpr, tmp); */
+                       /* For the EMU1010: How to get 32bit values from the DSP. High 16bits into L, low 16bits into R. */
+                       /* A_P16VIN(0) is delayed by one sample,
+                        * so all other A_P16VIN channels will need to also be delayed
+                        */
+                       /* Left ADC in. 1 of 2 */
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_P16VIN(0x0), A_FXBUS2(0) );
+                       /* Right ADC in 1 of 2 */
+                       gpr_map[gpr++] = 0x00000000;
+                       /* Delaying by one sample: instead of copying the input
+                        * value A_P16VIN to output A_FXBUS2 as in the first channel,
+                        * we use an auxiliary register, delaying the value by one
+                        * sample
+                        */
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(2) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x1), A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(4) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x2), A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(6) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x3), A_C_00000000, A_C_00000000);
+                       /* For 96kHz mode */
+                       /* Left ADC in. 2 of 2 */
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(0x8) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x4), A_C_00000000, A_C_00000000);
+                       /* Right ADC in 2 of 2 */
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(0xa) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x5), A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(0xc) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x6), A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16( icode, &ptr, tmp, bit_shifter16, A_GPR(gpr - 1), A_FXBUS2(0xe) );
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x7), A_C_00000000, A_C_00000000);
+                       /* Pavel Hofman - we still have voices, A_FXBUS2s, and
+                        * A_P16VINs available -
+                        * let's add 8 more capture channels - total of 16
+                        */
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x10));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x8),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x12));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0x9),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x14));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0xa),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x16));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0xb),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x18));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0xc),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x1a));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0xd),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x1c));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0xe),
+                            A_C_00000000, A_C_00000000);
+                       gpr_map[gpr++] = 0x00000000;
+                       snd_emu10k1_audigy_dsp_convert_32_to_2x16(icode, &ptr, tmp,
+                                                                 bit_shifter16,
+                                                                 A_GPR(gpr - 1),
+                                                                 A_FXBUS2(0x1e));
+                       A_OP(icode, &ptr, iACC3, A_GPR(gpr - 1), A_P16VIN(0xf),
+                            A_C_00000000, A_C_00000000);
+               }
+
+#if 0
+               for (z = 4; z < 8; z++) {
+                       A_OP(icode, &ptr, iACC3, A_FXBUS2(z), A_C_00000000, A_C_00000000, A_C_00000000);
+               }
+               for (z = 0xc; z < 0x10; z++) {
+                       A_OP(icode, &ptr, iACC3, A_FXBUS2(z), A_C_00000000, A_C_00000000, A_C_00000000);
+               }
+#endif
+       } else {
+               /* EFX capture - capture the 16 EXTINs */
+               /* Capture 16 channels of S16_LE sound */
+               for (z = 0; z < 16; z++) {
+                       A_OP(icode, &ptr, iACC3, A_FXBUS2(z), A_C_00000000, A_C_00000000, A_EXTIN(z));
+               }
        }
        
+#endif /* JCD test */
        /*
         * ok, set up done..
         */
@@ -1434,8 +1708,10 @@ A_OP(icode, &ptr, iMAC0, A_GPR(var), A_GPR(var), A_GPR(vol), A_EXTIN(input))
 
        seg = snd_enter_user();
        icode->gpr_add_control_count = nctl;
-       icode->gpr_add_controls = (emu10k1_fx8010_control_gpr_t __user *)controls;
+       icode->gpr_add_controls = (struct snd_emu10k1_fx8010_control_gpr __user *)controls;
+       emu->support_tlv = 1; /* support TLV */
        err = snd_emu10k1_icode_poke(emu, icode);
+       emu->support_tlv = 0; /* clear again */
        snd_leave_user(seg);
 
  __err:
@@ -1454,14 +1730,14 @@ A_OP(icode, &ptr, iMAC0, A_GPR(var), A_GPR(var), A_GPR(vol), A_EXTIN(input))
 
 /* when volume = max, then copy only to avoid volume modification */
 /* with iMAC0 (negative values) */
-static void __devinit _volume(emu10k1_fx8010_code_t *icode, u32 *ptr, u32 dst, u32 src, u32 vol)
+static void __devinit _volume(struct snd_emu10k1_fx8010_code *icode, u32 *ptr, u32 dst, u32 src, u32 vol)
 {
        OP(icode, ptr, iMAC0, dst, C_00000000, src, vol);
        OP(icode, ptr, iANDXOR, C_00000000, vol, C_ffffffff, C_7fffffff);
        OP(icode, ptr, iSKIP, GPR_COND, GPR_COND, CC_REG_NONZERO, C_00000001);
        OP(icode, ptr, iACC3, dst, src, C_00000000, C_00000000);
 }
-static void __devinit _volume_add(emu10k1_fx8010_code_t *icode, u32 *ptr, u32 dst, u32 src, u32 vol)
+static void __devinit _volume_add(struct snd_emu10k1_fx8010_code *icode, u32 *ptr, u32 dst, u32 src, u32 vol)
 {
        OP(icode, ptr, iANDXOR, C_00000000, vol, C_ffffffff, C_7fffffff);
        OP(icode, ptr, iSKIP, GPR_COND, GPR_COND, CC_REG_NONZERO, C_00000002);
@@ -1469,7 +1745,7 @@ static void __devinit _volume_add(emu10k1_fx8010_code_t *icode, u32 *ptr, u32 ds
        OP(icode, ptr, iSKIP, C_00000000, C_7fffffff, C_7fffffff, C_00000001);
        OP(icode, ptr, iMAC0, dst, dst, src, vol);
 }
-static void __devinit _volume_out(emu10k1_fx8010_code_t *icode, u32 *ptr, u32 dst, u32 src, u32 vol)
+static void __devinit _volume_out(struct snd_emu10k1_fx8010_code *icode, u32 *ptr, u32 dst, u32 src, u32 vol)
 {
        OP(icode, ptr, iANDXOR, C_00000000, vol, C_ffffffff, C_7fffffff);
        OP(icode, ptr, iSKIP, GPR_COND, GPR_COND, CC_REG_NONZERO, C_00000002);
@@ -1500,23 +1776,24 @@ static void __devinit _volume_out(emu10k1_fx8010_code_t *icode, u32 *ptr, u32 ds
                _SWITCH_NEG(icode, ptr, GPR(dst), GPR(src))
 
 
-static int __devinit _snd_emu10k1_init_efx(emu10k1_t *emu)
+static int __devinit _snd_emu10k1_init_efx(struct snd_emu10k1 *emu)
 {
        int err, i, z, gpr, tmp, playback, capture;
        u32 ptr;
-       emu10k1_fx8010_code_t *icode;
-       emu10k1_fx8010_pcm_t *ipcm = NULL;
-       emu10k1_fx8010_control_gpr_t *controls = NULL, *ctl;
+       struct snd_emu10k1_fx8010_code *icode;
+       struct snd_emu10k1_fx8010_pcm_rec *ipcm = NULL;
+       struct snd_emu10k1_fx8010_control_gpr *controls = NULL, *ctl;
        u32 *gpr_map;
        mm_segment_t seg;
 
-       spin_lock_init(&emu->fx8010.irq_lock);
-       INIT_LIST_HEAD(&emu->fx8010.gpr_ctl);
-
        if ((icode = kzalloc(sizeof(*icode), GFP_KERNEL)) == NULL)
                return -ENOMEM;
-       if ((icode->gpr_map = (u_int32_t __user *)kcalloc(256 + 160 + 160 + 2 * 512, sizeof(u_int32_t), GFP_KERNEL)) == NULL ||
-            (controls = kcalloc(SND_EMU10K1_GPR_CONTROLS, sizeof(emu10k1_fx8010_control_gpr_t), GFP_KERNEL)) == NULL ||
+       if ((icode->gpr_map = (u_int32_t __user *)
+            kcalloc(256 + 160 + 160 + 2 * 512, sizeof(u_int32_t),
+                    GFP_KERNEL)) == NULL ||
+            (controls = kcalloc(SND_EMU10K1_GPR_CONTROLS,
+                               sizeof(struct snd_emu10k1_fx8010_control_gpr),
+                               GFP_KERNEL)) == NULL ||
            (ipcm = kzalloc(sizeof(*ipcm), GFP_KERNEL)) == NULL) {
                err = -ENOMEM;
                goto __err;
@@ -2050,8 +2327,10 @@ static int __devinit _snd_emu10k1_init_efx(emu10k1_t *emu)
                goto __err;
        seg = snd_enter_user();
        icode->gpr_add_control_count = i;
-       icode->gpr_add_controls = (emu10k1_fx8010_control_gpr_t __user *)controls;
+       icode->gpr_add_controls = (struct snd_emu10k1_fx8010_control_gpr __user *)controls;
+       emu->support_tlv = 1; /* support TLV */
        err = snd_emu10k1_icode_poke(emu, icode);
+       emu->support_tlv = 0; /* clear again */
        snd_leave_user(seg);
        if (err >= 0)
                err = snd_emu10k1_ipcm_poke(emu, ipcm);
@@ -2065,15 +2344,17 @@ static int __devinit _snd_emu10k1_init_efx(emu10k1_t *emu)
        return err;
 }
 
-int __devinit snd_emu10k1_init_efx(emu10k1_t *emu)
+int __devinit snd_emu10k1_init_efx(struct snd_emu10k1 *emu)
 {
+       spin_lock_init(&emu->fx8010.irq_lock);
+       INIT_LIST_HEAD(&emu->fx8010.gpr_ctl);
        if (emu->audigy)
                return _snd_emu10k1_audigy_init_efx(emu);
        else
                return _snd_emu10k1_init_efx(emu);
 }
 
-void snd_emu10k1_free_efx(emu10k1_t *emu)
+void snd_emu10k1_free_efx(struct snd_emu10k1 *emu)
 {
        /* stop processor */
        if (emu->audigy)
@@ -2082,8 +2363,8 @@ void snd_emu10k1_free_efx(emu10k1_t *emu)
                snd_emu10k1_ptr_write(emu, DBG, 0, emu->fx8010.dbg = EMU10K1_DBG_SINGLE_STEP);
 }
 
-#if 0 // FIXME: who use them?
-int snd_emu10k1_fx8010_tone_control_activate(emu10k1_t *emu, int output)
+#if 0 /* FIXME: who use them? */
+int snd_emu10k1_fx8010_tone_control_activate(struct snd_emu10k1 *emu, int output)
 {
        if (output < 0 || output >= 6)
                return -EINVAL;
@@ -2091,7 +2372,7 @@ int snd_emu10k1_fx8010_tone_control_activate(emu10k1_t *emu, int output)
        return 0;
 }
 
-int snd_emu10k1_fx8010_tone_control_deactivate(emu10k1_t *emu, int output)
+int snd_emu10k1_fx8010_tone_control_deactivate(struct snd_emu10k1 *emu, int output)
 {
        if (output < 0 || output >= 6)
                return -EINVAL;
@@ -2100,7 +2381,7 @@ int snd_emu10k1_fx8010_tone_control_deactivate(emu10k1_t *emu, int output)
 }
 #endif
 
-int snd_emu10k1_fx8010_tram_setup(emu10k1_t *emu, u32 size)
+int snd_emu10k1_fx8010_tram_setup(struct snd_emu10k1 *emu, u32 size)
 {
        u8 size_reg = 0;
 
@@ -2136,13 +2417,13 @@ int snd_emu10k1_fx8010_tram_setup(emu10k1_t *emu, u32 size)
                snd_emu10k1_ptr_write(emu, TCBS, 0, size_reg);
                spin_lock_irq(&emu->emu_lock);
                outl(inl(emu->port + HCFG) & ~HCFG_LOCKTANKCACHE_MASK, emu->port + HCFG);
-               spin_unlock_irq(&emu->emu_lock);        
+               spin_unlock_irq(&emu->emu_lock);
        }
 
        return 0;
 }
 
-static int snd_emu10k1_fx8010_open(snd_hwdep_t * hw, struct file *file)
+static int snd_emu10k1_fx8010_open(struct snd_hwdep * hw, struct file *file)
 {
        return 0;
 }
@@ -2155,13 +2436,13 @@ static void copy_string(char *dst, char *src, char *null, int idx)
                strcpy(dst, src);
 }
 
-static int snd_emu10k1_fx8010_info(emu10k1_t *emu, emu10k1_fx8010_info_t *info)
+static void snd_emu10k1_fx8010_info(struct snd_emu10k1 *emu,
+                                  struct snd_emu10k1_fx8010_info *info)
 {
        char **fxbus, **extin, **extout;
        unsigned short fxbus_mask, extin_mask, extout_mask;
        int res;
 
-       memset(info, 0, sizeof(info));
        info->internal_tram_size = emu->fx8010.itram_size;
        info->external_tram_size = emu->fx8010.etram_pages.bytes / 2;
        fxbus = fxbuses;
@@ -2178,28 +2459,27 @@ static int snd_emu10k1_fx8010_info(emu10k1_t *emu, emu10k1_fx8010_info_t *info)
        for (res = 16; res < 32; res++, extout++)
                copy_string(info->extout_names[res], extout_mask & (1 << res) ? *extout : NULL, "Unused", res);
        info->gpr_controls = emu->fx8010.gpr_count;
-       return 0;
 }
 
-static int snd_emu10k1_fx8010_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg)
+static int snd_emu10k1_fx8010_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
 {
-       emu10k1_t *emu = hw->private_data;
-       emu10k1_fx8010_info_t *info;
-       emu10k1_fx8010_code_t *icode;
-       emu10k1_fx8010_pcm_t *ipcm;
+       struct snd_emu10k1 *emu = hw->private_data;
+       struct snd_emu10k1_fx8010_info *info;
+       struct snd_emu10k1_fx8010_code *icode;
+       struct snd_emu10k1_fx8010_pcm_rec *ipcm;
        unsigned int addr;
        void __user *argp = (void __user *)arg;
        int res;
        
        switch (cmd) {
+       case SNDRV_EMU10K1_IOCTL_PVERSION:
+               emu->support_tlv = 1;
+               return put_user(SNDRV_EMU10K1_VERSION, (int __user *)argp);
        case SNDRV_EMU10K1_IOCTL_INFO:
-               info = (emu10k1_fx8010_info_t *)kmalloc(sizeof(*info), GFP_KERNEL);
+               info = kmalloc(sizeof(*info), GFP_KERNEL);
                if (!info)
                        return -ENOMEM;
-               if ((res = snd_emu10k1_fx8010_info(emu, info)) < 0) {
-                       kfree(info);
-                       return res;
-               }
+               snd_emu10k1_fx8010_info(emu, info);
                if (copy_to_user(argp, info, sizeof(*info))) {
                        kfree(info);
                        return -EFAULT;
@@ -2209,7 +2489,7 @@ static int snd_emu10k1_fx8010_ioctl(snd_hwdep_t * hw, struct file *file, unsigne
        case SNDRV_EMU10K1_IOCTL_CODE_POKE:
                if (!capable(CAP_SYS_ADMIN))
                        return -EPERM;
-               icode = (emu10k1_fx8010_code_t *)kmalloc(sizeof(*icode), GFP_KERNEL);
+               icode = kmalloc(sizeof(*icode), GFP_KERNEL);
                if (icode == NULL)
                        return -ENOMEM;
                if (copy_from_user(icode, argp, sizeof(*icode))) {
@@ -2220,7 +2500,7 @@ static int snd_emu10k1_fx8010_ioctl(snd_hwdep_t * hw, struct file *file, unsigne
                kfree(icode);
                return res;
        case SNDRV_EMU10K1_IOCTL_CODE_PEEK:
-               icode = (emu10k1_fx8010_code_t *)kmalloc(sizeof(*icode), GFP_KERNEL);
+               icode = kmalloc(sizeof(*icode), GFP_KERNEL);
                if (icode == NULL)
                        return -ENOMEM;
                if (copy_from_user(icode, argp, sizeof(*icode))) {
@@ -2235,7 +2515,7 @@ static int snd_emu10k1_fx8010_ioctl(snd_hwdep_t * hw, struct file *file, unsigne
                kfree(icode);
                return res;
        case SNDRV_EMU10K1_IOCTL_PCM_POKE:
-               ipcm = (emu10k1_fx8010_pcm_t *)kmalloc(sizeof(*ipcm), GFP_KERNEL);
+               ipcm = kmalloc(sizeof(*ipcm), GFP_KERNEL);
                if (ipcm == NULL)
                        return -ENOMEM;
                if (copy_from_user(ipcm, argp, sizeof(*ipcm))) {
@@ -2265,9 +2545,9 @@ static int snd_emu10k1_fx8010_ioctl(snd_hwdep_t * hw, struct file *file, unsigne
                        return -EPERM;
                if (get_user(addr, (unsigned int __user *)argp))
                        return -EFAULT;
-               down(&emu->fx8010.lock);
+               mutex_lock(&emu->fx8010.lock);
                res = snd_emu10k1_fx8010_tram_setup(emu, addr);
-               up(&emu->fx8010.lock);
+               mutex_unlock(&emu->fx8010.lock);
                return res;
        case SNDRV_EMU10K1_IOCTL_STOP:
                if (!capable(CAP_SYS_ADMIN))
@@ -2327,14 +2607,14 @@ static int snd_emu10k1_fx8010_ioctl(snd_hwdep_t * hw, struct file *file, unsigne
        return -ENOTTY;
 }
 
-static int snd_emu10k1_fx8010_release(snd_hwdep_t * hw, struct file *file)
+static int snd_emu10k1_fx8010_release(struct snd_hwdep * hw, struct file *file)
 {
        return 0;
 }
 
-int __devinit snd_emu10k1_fx8010_new(emu10k1_t *emu, int device, snd_hwdep_t ** rhwdep)
+int __devinit snd_emu10k1_fx8010_new(struct snd_emu10k1 *emu, int device, struct snd_hwdep ** rhwdep)
 {
-       snd_hwdep_t *hw;
+       struct snd_hwdep *hw;
        int err;
        
        if (rhwdep)
@@ -2351,3 +2631,114 @@ int __devinit snd_emu10k1_fx8010_new(emu10k1_t *emu, int device, snd_hwdep_t **
                *rhwdep = hw;
        return 0;
 }
+
+#ifdef CONFIG_PM
+int __devinit snd_emu10k1_efx_alloc_pm_buffer(struct snd_emu10k1 *emu)
+{
+       int len;
+
+       len = emu->audigy ? 0x200 : 0x100;
+       emu->saved_gpr = kmalloc(len * 4, GFP_KERNEL);
+       if (! emu->saved_gpr)
+               return -ENOMEM;
+       len = emu->audigy ? 0x100 : 0xa0;
+       emu->tram_val_saved = kmalloc(len * 4, GFP_KERNEL);
+       emu->tram_addr_saved = kmalloc(len * 4, GFP_KERNEL);
+       if (! emu->tram_val_saved || ! emu->tram_addr_saved)
+               return -ENOMEM;
+       len = emu->audigy ? 2 * 1024 : 2 * 512;
+       emu->saved_icode = vmalloc(len * 4);
+       if (! emu->saved_icode)
+               return -ENOMEM;
+       return 0;
+}
+
+void snd_emu10k1_efx_free_pm_buffer(struct snd_emu10k1 *emu)
+{
+       kfree(emu->saved_gpr);
+       kfree(emu->tram_val_saved);
+       kfree(emu->tram_addr_saved);
+       vfree(emu->saved_icode);
+}
+
+/*
+ * save/restore GPR, TRAM and codes
+ */
+void snd_emu10k1_efx_suspend(struct snd_emu10k1 *emu)
+{
+       int i, len;
+
+       len = emu->audigy ? 0x200 : 0x100;
+       for (i = 0; i < len; i++)
+               emu->saved_gpr[i] = snd_emu10k1_ptr_read(emu, emu->gpr_base + i, 0);
+
+       len = emu->audigy ? 0x100 : 0xa0;
+       for (i = 0; i < len; i++) {
+               emu->tram_val_saved[i] = snd_emu10k1_ptr_read(emu, TANKMEMDATAREGBASE + i, 0);
+               emu->tram_addr_saved[i] = snd_emu10k1_ptr_read(emu, TANKMEMADDRREGBASE + i, 0);
+               if (emu->audigy) {
+                       emu->tram_addr_saved[i] >>= 12;
+                       emu->tram_addr_saved[i] |=
+                               snd_emu10k1_ptr_read(emu, A_TANKMEMCTLREGBASE + i, 0) << 20;
+               }
+       }
+
+       len = emu->audigy ? 2 * 1024 : 2 * 512;
+       for (i = 0; i < len; i++)
+               emu->saved_icode[i] = snd_emu10k1_efx_read(emu, i);
+}
+
+void snd_emu10k1_efx_resume(struct snd_emu10k1 *emu)
+{
+       int i, len;
+
+       /* set up TRAM */
+       if (emu->fx8010.etram_pages.bytes > 0) {
+               unsigned size, size_reg = 0;
+               size = emu->fx8010.etram_pages.bytes / 2;
+               size = (size - 1) >> 13;
+               while (size) {
+                       size >>= 1;
+                       size_reg++;
+               }
+               outl(HCFG_LOCKTANKCACHE_MASK | inl(emu->port + HCFG), emu->port + HCFG);
+               snd_emu10k1_ptr_write(emu, TCB, 0, emu->fx8010.etram_pages.addr);
+               snd_emu10k1_ptr_write(emu, TCBS, 0, size_reg);
+               outl(inl(emu->port + HCFG) & ~HCFG_LOCKTANKCACHE_MASK, emu->port + HCFG);
+       }
+
+       if (emu->audigy)
+               snd_emu10k1_ptr_write(emu, A_DBG, 0, emu->fx8010.dbg | A_DBG_SINGLE_STEP);
+       else
+               snd_emu10k1_ptr_write(emu, DBG, 0, emu->fx8010.dbg | EMU10K1_DBG_SINGLE_STEP);
+
+       len = emu->audigy ? 0x200 : 0x100;
+       for (i = 0; i < len; i++)
+               snd_emu10k1_ptr_write(emu, emu->gpr_base + i, 0, emu->saved_gpr[i]);
+
+       len = emu->audigy ? 0x100 : 0xa0;
+       for (i = 0; i < len; i++) {
+               snd_emu10k1_ptr_write(emu, TANKMEMDATAREGBASE + i, 0,
+                                     emu->tram_val_saved[i]);
+               if (! emu->audigy)
+                       snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + i, 0,
+                                             emu->tram_addr_saved[i]);
+               else {
+                       snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + i, 0,
+                                             emu->tram_addr_saved[i] << 12);
+                       snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + i, 0,
+                                             emu->tram_addr_saved[i] >> 20);
+               }
+       }
+
+       len = emu->audigy ? 2 * 1024 : 2 * 512;
+       for (i = 0; i < len; i++)
+               snd_emu10k1_efx_write(emu, i, emu->saved_icode[i]);
+
+       /* start FX processor when the DSP code is updated */
+       if (emu->audigy)
+               snd_emu10k1_ptr_write(emu, A_DBG, 0, emu->fx8010.dbg);
+       else
+               snd_emu10k1_ptr_write(emu, DBG, 0, emu->fx8010.dbg);
+}
+#endif