sound: use DEFINE_PCI_DEVICE_TABLE
[safe/jmp/linux-2.6] / sound / pci / rme9652 / hdspm.c
index 9e86d0e..3d72c1e 100644 (file)
@@ -1,11 +1,12 @@
-/*   -*- linux-c -*-
- *
+/*
  *   ALSA driver for RME Hammerfall DSP MADI audio interface(s)
  *
  *      Copyright (c) 2003 Winfried Ritsch (IEM)
  *      code based on hdsp.c   Paul Davis
  *                             Marcus Andersson
  *                             Thomas Charbonnel
+ *      Modified 2006-06-01 for AES32 support by Remy Bruno
+ *                                               <remy.bruno@trinnov.com>
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  *
  */
-#include <sound/driver.h>
 #include <linux/init.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/moduleparam.h>
 #include <linux/slab.h>
 #include <linux/pci.h>
+#include <linux/math64.h>
 #include <asm/io.h>
 
 #include <sound/core.h>
@@ -65,7 +66,7 @@ module_param_array(enable, bool, NULL, 0444);
 MODULE_PARM_DESC(enable, "Enable/disable specific HDSPM soundcards.");
 
 module_param_array(precise_ptr, bool, NULL, 0444);
-MODULE_PARM_DESC(precise_ptr, "Enable precise pointer, or disable.");
+MODULE_PARM_DESC(precise_ptr, "Enable or disable precise pointer.");
 
 module_param_array(line_outs_monitor, bool, NULL, 0444);
 MODULE_PARM_DESC(line_outs_monitor,
@@ -76,8 +77,10 @@ MODULE_PARM_DESC(enable_monitor,
                 "Enable Analog Out on Channel 63/64 by default.");
 
 MODULE_AUTHOR
-      ("Winfried Ritsch <ritsch_AT_iem.at>, Paul Davis <paul@linuxaudiosystems.com>, "
-       "Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
+      ("Winfried Ritsch <ritsch_AT_iem.at>, "
+       "Paul Davis <paul@linuxaudiosystems.com>, "
+       "Marcus Andersson, Thomas Charbonnel <thomas@undata.org>, "
+       "Remy Bruno <remy.bruno@trinnov.com>");
 MODULE_DESCRIPTION("RME HDSPM");
 MODULE_LICENSE("GPL");
 MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
@@ -88,8 +91,10 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_controlRegister       64
 #define HDSPM_interruptConfirmation  96
 #define HDSPM_control2Reg           256  /* not in specs ???????? */
+#define HDSPM_freqReg                256  /* for AES32 */
 #define HDSPM_midiDataOut0          352  /* just believe in old code */
 #define HDSPM_midiDataOut1          356
+#define HDSPM_eeprom_wr                     384  /* for AES32 */
 
 /* DMA enable for 64 channels, only Bit 0 is relevant */
 #define HDSPM_outputEnableBase       512  /* 512-767  input  DMA */ 
@@ -107,7 +112,12 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 /* --- Read registers. ---
    These are defined as byte-offsets from the iobase value */
 #define HDSPM_statusRegister    0
-#define HDSPM_statusRegister2  96
+/*#define HDSPM_statusRegister2  96 */
+/* after RME Windows driver sources, status2 is 4-byte word # 48 = word at
+ * offset 192, for AES32 *and* MADI
+ * => need to check that offset 192 is working on MADI */
+#define HDSPM_statusRegister2  192
+#define HDSPM_timecodeRegister 128
 
 #define HDSPM_midiDataIn0     360
 #define HDSPM_midiDataIn1     364
@@ -140,37 +150,54 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_Frequency0  (1<<6)  /* 0=44.1kHz/88.2kHz 1=48kHz/96kHz */
 #define HDSPM_Frequency1  (1<<7)  /* 0=32kHz/64kHz */
 #define HDSPM_DoubleSpeed (1<<8)  /* 0=normal speed, 1=double speed */
-#define HDSPM_QuadSpeed   (1<<31) /* quad speed bit, not implemented now */
+#define HDSPM_QuadSpeed   (1<<31) /* quad speed bit */
 
+#define HDSPM_Professional (1<<9) /* Professional */ /* AES32 ONLY */
 #define HDSPM_TX_64ch     (1<<10) /* Output 64channel MODE=1,
-                                    56channelMODE=0 */
+                                    56channelMODE=0 */ /* MADI ONLY*/
+#define HDSPM_Emphasis    (1<<10) /* Emphasis */ /* AES32 ONLY */
 
 #define HDSPM_AutoInp     (1<<11) /* Auto Input (takeover) == Safe Mode, 
-                                     0=off, 1=on  */
+                                     0=off, 1=on  */ /* MADI ONLY */
+#define HDSPM_Dolby       (1<<11) /* Dolby = "NonAudio" ?? */ /* AES32 ONLY */
 
-#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax */
+#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax
+                                   * -- MADI ONLY
+                                   */
 #define HDSPM_InputSelect1 (1<<15) /* should be 0 */
 
 #define HDSPM_SyncRef0     (1<<16) /* 0=WOrd, 1=MADI */
-#define HDSPM_SyncRef1     (1<<17) /* should be 0 */
+#define HDSPM_SyncRef1     (1<<17) /* for AES32: SyncRefN codes the AES # */
+#define HDSPM_SyncRef2     (1<<13)
+#define HDSPM_SyncRef3     (1<<25)
 
+#define HDSPM_SMUX         (1<<18) /* Frame ??? */ /* MADI ONY */
 #define HDSPM_clr_tms      (1<<19) /* clear track marker, do not use 
                                       AES additional bits in
                                      lower 5 Audiodatabits ??? */
+#define HDSPM_taxi_reset   (1<<20) /* ??? */ /* MADI ONLY ? */
+#define HDSPM_WCK48        (1<<20) /* Frame ??? = HDSPM_SMUX */ /* AES32 ONLY */
 
 #define HDSPM_Midi0InterruptEnable (1<<22)
 #define HDSPM_Midi1InterruptEnable (1<<23)
 
 #define HDSPM_LineOut (1<<24) /* Analog Out on channel 63/64 on=1, mute=0 */
 
+#define HDSPM_DS_DoubleWire (1<<26) /* AES32 ONLY */
+#define HDSPM_QS_DoubleWire (1<<27) /* AES32 ONLY */
+#define HDSPM_QS_QuadWire   (1<<28) /* AES32 ONLY */
+
+#define HDSPM_wclk_sel (1<<30)
 
 /* --- bit helper defines */
 #define HDSPM_LatencyMask    (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
-#define HDSPM_FrequencyMask  (HDSPM_Frequency0|HDSPM_Frequency1)
+#define HDSPM_FrequencyMask  (HDSPM_Frequency0|HDSPM_Frequency1|\
+                             HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
 #define HDSPM_InputMask      (HDSPM_InputSelect0|HDSPM_InputSelect1)
 #define HDSPM_InputOptical   0
 #define HDSPM_InputCoaxial   (HDSPM_InputSelect0)
-#define HDSPM_SyncRefMask    (HDSPM_SyncRef0|HDSPM_SyncRef1)
+#define HDSPM_SyncRefMask    (HDSPM_SyncRef0|HDSPM_SyncRef1|\
+                             HDSPM_SyncRef2|HDSPM_SyncRef3)
 #define HDSPM_SyncRef_Word   0
 #define HDSPM_SyncRef_MADI   (HDSPM_SyncRef0)
 
@@ -182,7 +209,12 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_Frequency48KHz   (HDSPM_Frequency1|HDSPM_Frequency0)
 #define HDSPM_Frequency64KHz   (HDSPM_DoubleSpeed|HDSPM_Frequency0)
 #define HDSPM_Frequency88_2KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1)
-#define HDSPM_Frequency96KHz   (HDSPM_DoubleSpeed|HDSPM_Frequency1|HDSPM_Frequency0)
+#define HDSPM_Frequency96KHz   (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
+                               HDSPM_Frequency0)
+#define HDSPM_Frequency128KHz   (HDSPM_QuadSpeed|HDSPM_Frequency0)
+#define HDSPM_Frequency176_4KHz   (HDSPM_QuadSpeed|HDSPM_Frequency1)
+#define HDSPM_Frequency192KHz   (HDSPM_QuadSpeed|HDSPM_Frequency1|\
+                                HDSPM_Frequency0)
 
 /* --- for internal discrimination */
 #define HDSPM_CLOCK_SOURCE_AUTOSYNC          0 /* Sample Clock Sources */
@@ -229,10 +261,15 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_BIGENDIAN_MODE  (1<<9)
 #define HDSPM_RD_MULTIPLE     (1<<10)
 
-/* --- Status Register bits --- */
+/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
+     that do not conflict with specific bits for AES32 seem to be valid also
+     for the AES32
+ */
 #define HDSPM_audioIRQPending    (1<<0)        /* IRQ is high and pending */
-#define HDSPM_RX_64ch            (1<<1)        /* Input 64chan. MODE=1, 56chn. MODE=0 */
-#define HDSPM_AB_int             (1<<2)        /* InputChannel Opt=0, Coax=1 (like inp0) */
+#define HDSPM_RX_64ch            (1<<1)        /* Input 64chan. MODE=1, 56chn MODE=0 */
+#define HDSPM_AB_int             (1<<2)        /* InputChannel Opt=0, Coax=1
+                                        * (like inp0)
+                                        */
 #define HDSPM_madiLock           (1<<3)        /* MADI Locked =1, no=0 */
 
 #define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
@@ -247,12 +284,15 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_madiFreq2         (1<<24)        /* 4=64, 5=88.2 6=96 */
 #define HDSPM_madiFreq3         (1<<25)        /* 7=128, 8=176.4 9=192 */
 
-#define HDSPM_BufferID          (1<<26)        /* (Double)Buffer ID toggles with Interrupt */
+#define HDSPM_BufferID          (1<<26)        /* (Double)Buffer ID toggles with
+                                        * Interrupt
+                                        */
 #define HDSPM_midi0IRQPending   (1<<30)        /* MIDI IRQ is pending  */
 #define HDSPM_midi1IRQPending   (1<<31)        /* and aktiv */
 
 /* --- status bit helpers */
-#define HDSPM_madiFreqMask  (HDSPM_madiFreq0|HDSPM_madiFreq1|HDSPM_madiFreq2|HDSPM_madiFreq3)
+#define HDSPM_madiFreqMask  (HDSPM_madiFreq0|HDSPM_madiFreq1|\
+                            HDSPM_madiFreq2|HDSPM_madiFreq3)
 #define HDSPM_madiFreq32    (HDSPM_madiFreq0)
 #define HDSPM_madiFreq44_1  (HDSPM_madiFreq1)
 #define HDSPM_madiFreq48    (HDSPM_madiFreq0|HDSPM_madiFreq1)
@@ -263,7 +303,7 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_madiFreq176_4 (HDSPM_madiFreq3)
 #define HDSPM_madiFreq192   (HDSPM_madiFreq3|HDSPM_madiFreq0)
 
-/* Status2 Register bits */
+/* Status2 Register bits */ /* MADI ONLY */
 
 #define HDSPM_version0 (1<<0)  /* not realy defined but I guess */
 #define HDSPM_version1 (1<<1)  /* in former cards it was ??? */
@@ -292,27 +332,67 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_wcFreq96    (HDSPM_wc_freq1|HDSPM_wc_freq2)
 
 
-#define HDSPM_SelSyncRefMask       (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|HDSPM_SelSyncRef2)
+#define HDSPM_SelSyncRefMask       (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
+                                   HDSPM_SelSyncRef2)
 #define HDSPM_SelSyncRef_WORD      0
 #define HDSPM_SelSyncRef_MADI      (HDSPM_SelSyncRef0)
-#define HDSPM_SelSyncRef_NVALID    (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|HDSPM_SelSyncRef2)
+#define HDSPM_SelSyncRef_NVALID    (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
+                                   HDSPM_SelSyncRef2)
+
+/*
+   For AES32, bits for status, status2 and timecode are different
+*/
+/* status */
+#define HDSPM_AES32_wcLock     0x0200000
+#define HDSPM_AES32_wcFreq_bit  22
+/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function 
+  HDSPM_bit2freq */
+#define HDSPM_AES32_syncref_bit  16
+/* (status >> HDSPM_AES32_syncref_bit) & 0xF gives sync source */
+
+#define HDSPM_AES32_AUTOSYNC_FROM_WORD 0
+#define HDSPM_AES32_AUTOSYNC_FROM_AES1 1
+#define HDSPM_AES32_AUTOSYNC_FROM_AES2 2
+#define HDSPM_AES32_AUTOSYNC_FROM_AES3 3
+#define HDSPM_AES32_AUTOSYNC_FROM_AES4 4
+#define HDSPM_AES32_AUTOSYNC_FROM_AES5 5
+#define HDSPM_AES32_AUTOSYNC_FROM_AES6 6
+#define HDSPM_AES32_AUTOSYNC_FROM_AES7 7
+#define HDSPM_AES32_AUTOSYNC_FROM_AES8 8
+#define HDSPM_AES32_AUTOSYNC_FROM_NONE 9
+
+/*  status2 */
+/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
+#define HDSPM_LockAES   0x80
+#define HDSPM_LockAES1  0x80
+#define HDSPM_LockAES2  0x40
+#define HDSPM_LockAES3  0x20
+#define HDSPM_LockAES4  0x10
+#define HDSPM_LockAES5  0x8
+#define HDSPM_LockAES6  0x4
+#define HDSPM_LockAES7  0x2
+#define HDSPM_LockAES8  0x1
+/*
+   Timecode
+   After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
+   AES i+1
+ bits 3210
+      0001  32kHz
+      0010  44.1kHz
+      0011  48kHz
+      0100  64kHz
+      0101  88.2kHz
+      0110  96kHz
+      0111  128kHz
+      1000  176.4kHz
+      1001  192kHz
+  NB: Timecode register doesn't seem to work on AES32 card revision 230
+*/
 
 /* Mixer Values */
 #define UNITY_GAIN          32768      /* = 65536/2 */
 #define MINUS_INFINITY_GAIN 0
 
-/* PCI info */
-#ifndef PCI_VENDOR_ID_XILINX
-#define PCI_VENDOR_ID_XILINX           0x10ee
-#endif
-#ifndef PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP
-#define PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP 0x3fc5
-#endif
-#ifndef PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI
-#define PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI 0x3fc6
-#endif
-
-
 /* Number of channels for different Speed Modes */
 #define MADI_SS_CHANNELS       64
 #define MADI_DS_CHANNELS       32
@@ -330,28 +410,38 @@ MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
 #define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
 #define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
 
-typedef struct _hdspm hdspm_t;
-typedef struct _hdspm_midi hdspm_midi_t;
+/* revisions >= 230 indicate AES32 card */
+#define HDSPM_AESREVISION 230
+
+/* speed factor modes */
+#define HDSPM_SPEED_SINGLE 0
+#define HDSPM_SPEED_DOUBLE 1
+#define HDSPM_SPEED_QUAD   2
+/* names for speed modes */
+static char *hdspm_speed_names[] = { "single", "double", "quad" };
 
-struct _hdspm_midi {
-       hdspm_t *hdspm;
+struct hdspm_midi {
+       struct hdspm *hdspm;
        int id;
-       snd_rawmidi_t *rmidi;
-       snd_rawmidi_substream_t *input;
-       snd_rawmidi_substream_t *output;
+       struct snd_rawmidi *rmidi;
+       struct snd_rawmidi_substream *input;
+       struct snd_rawmidi_substream *output;
        char istimer;           /* timer in use */
        struct timer_list timer;
        spinlock_t lock;
        int pending;
 };
 
-struct _hdspm {
+struct hdspm {
         spinlock_t lock;
-        snd_pcm_substream_t *capture_substream;         /* only one playback */
-        snd_pcm_substream_t *playback_substream; /* and/or capture stream */
+       /* only one playback and/or capture stream */
+        struct snd_pcm_substream *capture_substream;
+        struct snd_pcm_substream *playback_substream;
 
        char *card_name;             /* for procinfo */
-       unsigned short firmware_rev; /* dont know if relevant */
+       unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
+
+       unsigned char is_aes32;    /* indicates if card is AES32 */
 
        int precise_ptr;        /* use precise pointers, to be tested */
        int monitor_outs;       /* set up monitoring outs init flag */
@@ -359,7 +449,7 @@ struct _hdspm {
        u32 control_register;   /* cached value */
        u32 control2_register;  /* cached value */
 
-       hdspm_midi_t midi[2];
+       struct hdspm_midi midi[2];
        struct tasklet_struct midi_tasklet;
 
        size_t period_bytes;
@@ -387,15 +477,18 @@ struct _hdspm {
 
        int irq_count;          /* for debug */
 
-       snd_card_t *card;       /* one card */
-       snd_pcm_t *pcm;         /* has one pcm */
-       snd_hwdep_t *hwdep;     /* and a hwdep for additional ioctl */
+       struct snd_card *card;  /* one card */
+       struct snd_pcm *pcm;            /* has one pcm */
+       struct snd_hwdep *hwdep;        /* and a hwdep for additional ioctl */
        struct pci_dev *pci;    /* and an pci info */
 
        /* Mixer vars */
-       snd_kcontrol_t *playback_mixer_ctls[HDSPM_MAX_CHANNELS];        /* fast alsa mixer */
-       snd_kcontrol_t *input_mixer_ctls[HDSPM_MAX_CHANNELS];   /* but input to much, so not used */
-       hdspm_mixer_t *mixer;   /* full mixer accessable over mixer ioctl or hwdep-device */
+       /* fast alsa mixer */
+       struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
+       /* but input to much, so not used */
+       struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
+       /* full mixer accessable over mixer ioctl or hwdep-device */
+       struct hdspm_mixer *mixer;
 
 };
 
@@ -418,30 +511,8 @@ static char channel_map_madi_ss[HDSPM_MAX_CHANNELS] = {
    56, 57, 58, 59, 60, 61, 62, 63
 };
 
-static char channel_map_madi_ds[HDSPM_MAX_CHANNELS] = {
-  0, 2, 4, 6, 8, 10, 12, 14,
-  16, 18, 20, 22, 24, 26, 28, 30,
-  32, 34, 36, 38, 40, 42, 44, 46,
-  48, 50, 52, 54, 56, 58, 60, 62,
-  -1, -1, -1, -1, -1, -1, -1, -1,
-  -1, -1, -1, -1, -1, -1, -1, -1,
-  -1, -1, -1, -1, -1, -1, -1, -1,
-  -1, -1, -1, -1, -1, -1, -1, -1
-};
-
-static char channel_map_madi_qs[HDSPM_MAX_CHANNELS] = {
-  0,   4,  8, 12, 16, 20, 24,  28,  
-  32, 36, 40, 44, 48, 52, 56,  60
-  -1, -1, -1, -1, -1, -1, -1, -1,  
-  -1, -1, -1, -1, -1, -1, -1, -1,  
-  -1, -1, -1, -1, -1, -1, -1, -1, 
-  -1, -1, -1, -1, -1, -1, -1, -1, 
-  -1, -1, -1, -1, -1, -1, -1, -1, 
-  -1, -1, -1, -1, -1, -1, -1, -1
-};
-
 
-static struct pci_device_id snd_hdspm_ids[] = {
+static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
        {
         .vendor = PCI_VENDOR_ID_XILINX,
         .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
@@ -456,28 +527,39 @@ static struct pci_device_id snd_hdspm_ids[] = {
 MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
 
 /* prototypes */
-static int __devinit snd_hdspm_create_alsa_devices(snd_card_t * card,
-                                                  hdspm_t * hdspm);
-static int __devinit snd_hdspm_create_pcm(snd_card_t * card,
-                                         hdspm_t * hdspm);
-
-static inline void snd_hdspm_initialize_midi_flush(hdspm_t * hdspm);
-static int hdspm_update_simple_mixer_controls(hdspm_t * hdspm);
-static int hdspm_autosync_ref(hdspm_t * hdspm);
-static int snd_hdspm_set_defaults(hdspm_t * hdspm);
-static void hdspm_set_sgbuf(hdspm_t * hdspm, struct snd_sg_buf *sgbuf,
+static int __devinit snd_hdspm_create_alsa_devices(struct snd_card *card,
+                                                  struct hdspm * hdspm);
+static int __devinit snd_hdspm_create_pcm(struct snd_card *card,
+                                         struct hdspm * hdspm);
+
+static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm);
+static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm);
+static int hdspm_autosync_ref(struct hdspm * hdspm);
+static int snd_hdspm_set_defaults(struct hdspm * hdspm);
+static void hdspm_set_sgbuf(struct hdspm * hdspm,
+                           struct snd_pcm_substream *substream,
                             unsigned int reg, int channels);
 
+static inline int HDSPM_bit2freq(int n)
+{
+       static const int bit2freq_tab[] = {
+               0, 32000, 44100, 48000, 64000, 88200,
+               96000, 128000, 176400, 192000 };
+       if (n < 1 || n > 9)
+               return 0;
+       return bit2freq_tab[n];
+}
+
 /* Write/read to/from HDSPM with Adresses in Bytes
    not words but only 32Bit writes are allowed */
 
-static inline void hdspm_write(hdspm_t * hdspm, unsigned int reg,
+static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
                               unsigned int val)
 {
        writel(val, hdspm->iobase + reg);
 }
 
-static inline unsigned int hdspm_read(hdspm_t * hdspm, unsigned int reg)
+static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
 {
        return readl(hdspm->iobase + reg);
 }
@@ -486,24 +568,24 @@ static inline unsigned int hdspm_read(hdspm_t * hdspm, unsigned int reg)
    mixer is write only on hardware so we have to cache him for read 
    each fader is a u32, but uses only the first 16 bit */
 
-static inline int hdspm_read_in_gain(hdspm_t * hdspm, unsigned int chan,
+static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
                                     unsigned int in)
 {
-       if (chan > HDSPM_MIXER_CHANNELS || in > HDSPM_MIXER_CHANNELS)
+       if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
                return 0;
 
        return hdspm->mixer->ch[chan].in[in];
 }
 
-static inline int hdspm_read_pb_gain(hdspm_t * hdspm, unsigned int chan,
+static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
                                     unsigned int pb)
 {
-       if (chan > HDSPM_MIXER_CHANNELS || pb > HDSPM_MIXER_CHANNELS)
+       if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
                return 0;
        return hdspm->mixer->ch[chan].pb[pb];
 }
 
-static inline int hdspm_write_in_gain(hdspm_t * hdspm, unsigned int chan,
+static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
                                      unsigned int in, unsigned short data)
 {
        if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
@@ -516,7 +598,7 @@ static inline int hdspm_write_in_gain(hdspm_t * hdspm, unsigned int chan,
        return 0;
 }
 
-static inline int hdspm_write_pb_gain(hdspm_t * hdspm, unsigned int chan,
+static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
                                      unsigned int pb, unsigned short data)
 {
        if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
@@ -531,18 +613,18 @@ static inline int hdspm_write_pb_gain(hdspm_t * hdspm, unsigned int chan,
 
 
 /* enable DMA for specific channels, now available for DSP-MADI */
-static inline void snd_hdspm_enable_in(hdspm_t * hdspm, int i, int v)
+static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
 {
        hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
 }
 
-static inline void snd_hdspm_enable_out(hdspm_t * hdspm, int i, int v)
+static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
 {
        hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
 }
 
 /* check if same process is writing and reading */
-static inline int snd_hdspm_use_is_exclusive(hdspm_t * hdspm)
+static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
 {
        unsigned long flags;
        int ret = 1;
@@ -557,109 +639,132 @@ static inline int snd_hdspm_use_is_exclusive(hdspm_t * hdspm)
 }
 
 /* check for external sample rate */
-static inline int hdspm_external_sample_rate(hdspm_t * hdspm)
-{
-       unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
-       unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
-       unsigned int rate_bits;
-       int rate = 0;
+static int hdspm_external_sample_rate(struct hdspm *hdspm)
+{
+       if (hdspm->is_aes32) {
+               unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
+               unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
+               unsigned int timecode =
+                       hdspm_read(hdspm, HDSPM_timecodeRegister);
+
+               int syncref = hdspm_autosync_ref(hdspm);
+
+               if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
+                               status & HDSPM_AES32_wcLock)
+                       return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit)
+                                             & 0xF);
+               if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
+                       syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
+                       status2 & (HDSPM_LockAES >>
+                                 (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
+                       return HDSPM_bit2freq((timecode >>
+                         (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
+               return 0;
+       } else {
+               unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
+               unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
+               unsigned int rate_bits;
+               int rate = 0;
 
-       /* if wordclock has synced freq and wordclock is valid */
-       if ((status2 & HDSPM_wcLock) != 0 &&
-           (status & HDSPM_SelSyncRef0) == 0) {
+               /* if wordclock has synced freq and wordclock is valid */
+               if ((status2 & HDSPM_wcLock) != 0 &&
+                               (status & HDSPM_SelSyncRef0) == 0) {
 
-               rate_bits = status2 & HDSPM_wcFreqMask;
+                       rate_bits = status2 & HDSPM_wcFreqMask;
 
-               switch (rate_bits) {
-               case HDSPM_wcFreq32:
-                       rate = 32000;
-                       break;
-               case HDSPM_wcFreq44_1:
-                       rate = 44100;
-                       break;
-               case HDSPM_wcFreq48:
-                       rate = 48000;
-                       break;
-               case HDSPM_wcFreq64:
-                       rate = 64000;
-                       break;
-               case HDSPM_wcFreq88_2:
-                       rate = 88200;
-                       break;
-               case HDSPM_wcFreq96:
-                       rate = 96000;
-                       break;
-                       /* Quadspeed Bit missing ???? */
-               default:
-                       rate = 0;
-                       break;
+                       switch (rate_bits) {
+                       case HDSPM_wcFreq32:
+                               rate = 32000;
+                               break;
+                       case HDSPM_wcFreq44_1:
+                               rate = 44100;
+                               break;
+                       case HDSPM_wcFreq48:
+                               rate = 48000;
+                               break;
+                       case HDSPM_wcFreq64:
+                               rate = 64000;
+                               break;
+                       case HDSPM_wcFreq88_2:
+                               rate = 88200;
+                               break;
+                       case HDSPM_wcFreq96:
+                               rate = 96000;
+                               break;
+                               /* Quadspeed Bit missing ???? */
+                       default:
+                               rate = 0;
+                               break;
+                       }
                }
-       }
 
-       /* if rate detected and Syncref is Word than have it, word has priority to MADI */
-       if (rate != 0
-           && (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
-               return rate;
+               /* if rate detected and Syncref is Word than have it,
+                * word has priority to MADI
+                */
+               if (rate != 0 &&
+                   (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
+                       return rate;
 
-       /* maby a madi input (which is taken if sel sync is madi) */
-       if (status & HDSPM_madiLock) {
-               rate_bits = status & HDSPM_madiFreqMask;
+               /* maby a madi input (which is taken if sel sync is madi) */
+               if (status & HDSPM_madiLock) {
+                       rate_bits = status & HDSPM_madiFreqMask;
 
-               switch (rate_bits) {
-               case HDSPM_madiFreq32:
-                       rate = 32000;
-                       break;
-               case HDSPM_madiFreq44_1:
-                       rate = 44100;
-                       break;
-               case HDSPM_madiFreq48:
-                       rate = 48000;
-                       break;
-               case HDSPM_madiFreq64:
-                       rate = 64000;
-                       break;
-               case HDSPM_madiFreq88_2:
-                       rate = 88200;
-                       break;
-               case HDSPM_madiFreq96:
-                       rate = 96000;
-                       break;
-               case HDSPM_madiFreq128:
-                       rate = 128000;
-                       break;
-               case HDSPM_madiFreq176_4:
-                       rate = 176400;
-                       break;
-               case HDSPM_madiFreq192:
-                       rate = 192000;
-                       break;
-               default:
-                       rate = 0;
-                       break;
+                       switch (rate_bits) {
+                       case HDSPM_madiFreq32:
+                               rate = 32000;
+                               break;
+                       case HDSPM_madiFreq44_1:
+                               rate = 44100;
+                               break;
+                       case HDSPM_madiFreq48:
+                               rate = 48000;
+                               break;
+                       case HDSPM_madiFreq64:
+                               rate = 64000;
+                               break;
+                       case HDSPM_madiFreq88_2:
+                               rate = 88200;
+                               break;
+                       case HDSPM_madiFreq96:
+                               rate = 96000;
+                               break;
+                       case HDSPM_madiFreq128:
+                               rate = 128000;
+                               break;
+                       case HDSPM_madiFreq176_4:
+                               rate = 176400;
+                               break;
+                       case HDSPM_madiFreq192:
+                               rate = 192000;
+                               break;
+                       default:
+                               rate = 0;
+                               break;
+                       }
                }
+               return rate;
        }
-       return rate;
 }
 
 /* Latency function */
-static inline void hdspm_compute_period_size(hdspm_t * hdspm)
+static inline void hdspm_compute_period_size(struct hdspm * hdspm)
 {
        hdspm->period_bytes =
            1 << ((hdspm_decode_latency(hdspm->control_register) + 8));
 }
 
-static snd_pcm_uframes_t hdspm_hw_pointer(hdspm_t * hdspm)
+static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm * hdspm)
 {
        int position;
 
        position = hdspm_read(hdspm, HDSPM_statusRegister);
 
-       if (!hdspm->precise_ptr) {
-               return (position & HDSPM_BufferID) ? (hdspm->period_bytes /
-                                                     4) : 0;
-       }
+       if (!hdspm->precise_ptr)
+               return (position & HDSPM_BufferID) ?
+                       (hdspm->period_bytes / 4) : 0;
 
-       /* hwpointer comes in bytes and is 64Bytes accurate (by docu since PCI Burst)
+       /* hwpointer comes in bytes and is 64Bytes accurate (by docu since
+          PCI Burst)
           i have experimented that it is at most 64 Byte to much for playing 
           so substraction of 64 byte should be ok for ALSA, but use it only
           for application where you know what you do since if you come to
@@ -672,26 +777,27 @@ static snd_pcm_uframes_t hdspm_hw_pointer(hdspm_t * hdspm)
 }
 
 
-static inline void hdspm_start_audio(hdspm_t * s)
+static inline void hdspm_start_audio(struct hdspm * s)
 {
        s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
        hdspm_write(s, HDSPM_controlRegister, s->control_register);
 }
 
-static inline void hdspm_stop_audio(hdspm_t * s)
+static inline void hdspm_stop_audio(struct hdspm * s)
 {
        s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
        hdspm_write(s, HDSPM_controlRegister, s->control_register);
 }
 
 /* should I silence all or only opened ones ? doit all for first even is 4MB*/
-static inline void hdspm_silence_playback(hdspm_t * hdspm)
+static void hdspm_silence_playback(struct hdspm *hdspm)
 {
        int i;
        int n = hdspm->period_bytes;
        void *buf = hdspm->playback_buffer;
 
-       snd_assert(buf != NULL, return);
+       if (buf == NULL)
+               return;
 
        for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
                memset(buf, 0, n);
@@ -699,7 +805,7 @@ static inline void hdspm_silence_playback(hdspm_t * hdspm)
        }
 }
 
-static int hdspm_set_interrupt_interval(hdspm_t * s, unsigned int frames)
+static int hdspm_set_interrupt_interval(struct hdspm * s, unsigned int frames)
 {
        int n;
 
@@ -723,14 +829,34 @@ static int hdspm_set_interrupt_interval(hdspm_t * s, unsigned int frames)
        return 0;
 }
 
+static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
+{
+       u64 n;
+       
+       if (rate >= 112000)
+               rate /= 4;
+       else if (rate >= 56000)
+               rate /= 2;
+
+       /* RME says n = 104857600000000, but in the windows MADI driver, I see:
+//     return 104857600000000 / rate; // 100 MHz
+       return 110100480000000 / rate; // 105 MHz
+        */        
+       /* n = 104857600000000ULL; */ /*  =  2^20 * 10^8 */
+       n = 110100480000000ULL;    /* Value checked for AES32 and MADI */
+       n = div_u64(n, rate);
+       /* n should be less than 2^32 for being written to FREQ register */
+       snd_BUG_ON(n >> 32);
+       hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
+}
 
 /* dummy set rate lets see what happens */
-static int hdspm_set_rate(hdspm_t * hdspm, int rate, int called_internally)
+static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
 {
-       int reject_if_open = 0;
        int current_rate;
        int rate_bits;
        int not_set = 0;
+       int current_speed, target_speed;
 
        /* ASSUMPTION: hdspm->lock is either set, or there is no need for
           it (e.g. during module initialization).
@@ -745,8 +871,9 @@ static int hdspm_set_rate(hdspm_t * hdspm, int rate, int called_internally)
                     just make a warning an remember setting 
                     for future master mode switching */
     
-                       snd_printk
-                           (KERN_WARNING "HDSPM: Warning: device is not running as a clock master.\n");
+                       snd_printk(KERN_WARNING "HDSPM: "
+                                  "Warning: device is not running "
+                                  "as a clock master.\n");
                        not_set = 1;
                } else {
 
@@ -754,16 +881,18 @@ static int hdspm_set_rate(hdspm_t * hdspm, int rate, int called_internally)
                        int external_freq =
                            hdspm_external_sample_rate(hdspm);
 
-                       if ((hdspm_autosync_ref(hdspm) ==
-                            HDSPM_AUTOSYNC_FROM_NONE)) {
+                       if (hdspm_autosync_ref(hdspm) ==
+                           HDSPM_AUTOSYNC_FROM_NONE) {
 
-                               snd_printk(KERN_WARNING "HDSPM: Detected no Externel Sync \n");
+                               snd_printk(KERN_WARNING "HDSPM: "
+                                          "Detected no Externel Sync \n");
                                not_set = 1;
 
                        } else if (rate != external_freq) {
 
-                               snd_printk
-                                   (KERN_WARNING "HDSPM: Warning: No AutoSync source for requested rate\n");
+                               snd_printk(KERN_WARNING "HDSPM: "
+                                          "Warning: No AutoSync source for "
+                                          "requested rate\n");
                                not_set = 1;
                        }
                }
@@ -781,51 +910,60 @@ static int hdspm_set_rate(hdspm_t * hdspm, int rate, int called_internally)
           changes in the read/write routines.  
         */
 
+       if (current_rate <= 48000)
+               current_speed = HDSPM_SPEED_SINGLE;
+       else if (current_rate <= 96000)
+               current_speed = HDSPM_SPEED_DOUBLE;
+       else
+               current_speed = HDSPM_SPEED_QUAD;
+
+       if (rate <= 48000)
+               target_speed = HDSPM_SPEED_SINGLE;
+       else if (rate <= 96000)
+               target_speed = HDSPM_SPEED_DOUBLE;
+       else
+               target_speed = HDSPM_SPEED_QUAD;
+
        switch (rate) {
        case 32000:
-               if (current_rate > 48000) {
-                       reject_if_open = 1;
-               }
                rate_bits = HDSPM_Frequency32KHz;
                break;
        case 44100:
-               if (current_rate > 48000) {
-                       reject_if_open = 1;
-               }
                rate_bits = HDSPM_Frequency44_1KHz;
                break;
        case 48000:
-               if (current_rate > 48000) {
-                       reject_if_open = 1;
-               }
                rate_bits = HDSPM_Frequency48KHz;
                break;
        case 64000:
-               if (current_rate <= 48000) {
-                       reject_if_open = 1;
-               }
                rate_bits = HDSPM_Frequency64KHz;
                break;
        case 88200:
-               if (current_rate <= 48000) {
-                       reject_if_open = 1;
-               }
                rate_bits = HDSPM_Frequency88_2KHz;
                break;
        case 96000:
-               if (current_rate <= 48000) {
-                       reject_if_open = 1;
-               }
                rate_bits = HDSPM_Frequency96KHz;
                break;
+       case 128000:
+               rate_bits = HDSPM_Frequency128KHz;
+               break;
+       case 176400:
+               rate_bits = HDSPM_Frequency176_4KHz;
+               break;
+       case 192000:
+               rate_bits = HDSPM_Frequency192KHz;
+               break;
        default:
                return -EINVAL;
        }
 
-       if (reject_if_open
+       if (current_speed != target_speed
            && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
                snd_printk
-                   (KERN_ERR "HDSPM: cannot change between single- and double-speed mode (capture PID = %d, playback PID = %d)\n",
+                   (KERN_ERR "HDSPM: "
+                    "cannot change from %s speed to %s speed mode "
+                    "(capture PID = %d, playback PID = %d)\n",
+                    hdspm_speed_names[current_speed],
+                    hdspm_speed_names[target_speed],
                     hdspm->capture_pid, hdspm->playback_pid);
                return -EBUSY;
        }
@@ -834,12 +972,16 @@ static int hdspm_set_rate(hdspm_t * hdspm, int rate, int called_internally)
        hdspm->control_register |= rate_bits;
        hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
 
-       if (rate > 64000)
-               hdspm->channel_map = channel_map_madi_qs;
-       else if (rate > 48000)
-               hdspm->channel_map = channel_map_madi_ds;
-       else 
-               hdspm->channel_map = channel_map_madi_ss;
+       /* For AES32, need to set DDS value in FREQ register
+          For MADI, also apparently */
+       hdspm_set_dds_value(hdspm, rate);
+       
+       if (hdspm->is_aes32 && rate != current_rate)
+               hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
+       
+       /* For AES32 and for MADI (at least rev 204), channel_map needs to
+        * always be channel_map_madi_ss, whatever the sample rate */
+       hdspm->channel_map = channel_map_madi_ss;
 
        hdspm->system_sample_rate = rate;
 
@@ -850,11 +992,17 @@ static int hdspm_set_rate(hdspm_t * hdspm, int rate, int called_internally)
 }
 
 /* mainly for init to 0 on load */
-static void all_in_all_mixer(hdspm_t * hdspm, int sgain)
+static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
 {
        int i, j;
-       unsigned int gain =
-           (sgain > UNITY_GAIN) ? UNITY_GAIN : (sgain < 0) ? 0 : sgain;
+       unsigned int gain;
+
+       if (sgain > UNITY_GAIN)
+               gain = UNITY_GAIN;
+       else if (sgain < 0)
+               gain = 0;
+       else
+               gain = sgain;
 
        for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
                for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
@@ -867,7 +1015,8 @@ static void all_in_all_mixer(hdspm_t * hdspm, int sgain)
    MIDI
   ----------------------------------------------------------------------------*/
 
-static inline unsigned char snd_hdspm_midi_read_byte (hdspm_t *hdspm, int id)
+static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
+                                                     int id)
 {
        /* the hardware already does the relevant bit-mask with 0xff */
        if (id)
@@ -876,16 +1025,17 @@ static inline unsigned char snd_hdspm_midi_read_byte (hdspm_t *hdspm, int id)
                return hdspm_read(hdspm, HDSPM_midiDataIn0);
 }
 
-static inline void snd_hdspm_midi_write_byte (hdspm_t *hdspm, int id, int val)
+static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
+                                             int val)
 {
        /* the hardware already does the relevant bit-mask with 0xff */
        if (id)
-               return hdspm_write(hdspm, HDSPM_midiDataOut1, val);
+               hdspm_write(hdspm, HDSPM_midiDataOut1, val);
        else
-               return hdspm_write(hdspm, HDSPM_midiDataOut0, val);
+               hdspm_write(hdspm, HDSPM_midiDataOut0, val);
 }
 
-static inline int snd_hdspm_midi_input_available (hdspm_t *hdspm, int id)
+static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
 {
        if (id)
                return (hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xff);
@@ -893,14 +1043,15 @@ static inline int snd_hdspm_midi_input_available (hdspm_t *hdspm, int id)
                return (hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xff);
 }
 
-static inline int snd_hdspm_midi_output_possible (hdspm_t *hdspm, int id)
+static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
 {
        int fifo_bytes_used;
 
        if (id)
-               fifo_bytes_used = hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xff;
+               fifo_bytes_used = hdspm_read(hdspm, HDSPM_midiStatusOut1);
        else
-               fifo_bytes_used = hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xff;
+               fifo_bytes_used = hdspm_read(hdspm, HDSPM_midiStatusOut0);
+       fifo_bytes_used &= 0xff;
 
        if (fifo_bytes_used < 128)
                return  128 - fifo_bytes_used;
@@ -908,13 +1059,13 @@ static inline int snd_hdspm_midi_output_possible (hdspm_t *hdspm, int id)
                return 0;
 }
 
-static inline void snd_hdspm_flush_midi_input (hdspm_t *hdspm, int id)
+static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
 {
        while (snd_hdspm_midi_input_available (hdspm, id))
                snd_hdspm_midi_read_byte (hdspm, id);
 }
 
-static int snd_hdspm_midi_output_write (hdspm_midi_t *hmidi)
+static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
 {
        unsigned long flags;
        int n_pending;
@@ -925,16 +1076,21 @@ static int snd_hdspm_midi_output_write (hdspm_midi_t *hmidi)
        /* Output is not interrupt driven */
                
        spin_lock_irqsave (&hmidi->lock, flags);
-       if (hmidi->output) {
-               if (!snd_rawmidi_transmit_empty (hmidi->output)) {
-                       if ((n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm, hmidi->id)) > 0) {
-                               if (n_pending > (int)sizeof (buf))
-                                       n_pending = sizeof (buf);
-                               
-                               if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
-                                       for (i = 0; i < to_write; ++i) 
-                                               snd_hdspm_midi_write_byte (hmidi->hdspm, hmidi->id, buf[i]);
-                               }
+       if (hmidi->output &&
+           !snd_rawmidi_transmit_empty (hmidi->output)) {
+               n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
+                                                           hmidi->id);
+               if (n_pending > 0) {
+                       if (n_pending > (int)sizeof (buf))
+                               n_pending = sizeof (buf);
+               
+                       to_write = snd_rawmidi_transmit (hmidi->output, buf,
+                                                        n_pending);
+                       if (to_write > 0) {
+                               for (i = 0; i < to_write; ++i) 
+                                       snd_hdspm_midi_write_byte (hmidi->hdspm,
+                                                                  hmidi->id,
+                                                                  buf[i]);
                        }
                }
        }
@@ -942,53 +1098,57 @@ static int snd_hdspm_midi_output_write (hdspm_midi_t *hmidi)
        return 0;
 }
 
-static int snd_hdspm_midi_input_read (hdspm_midi_t *hmidi)
+static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
 {
-       unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
+       unsigned char buf[128]; /* this buffer is designed to match the MIDI
+                                * input FIFO size
+                                */
        unsigned long flags;
        int n_pending;
        int i;
 
        spin_lock_irqsave (&hmidi->lock, flags);
-       if ((n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id)) > 0) {
+       n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
+       if (n_pending > 0) {
                if (hmidi->input) {
-                       if (n_pending > (int)sizeof (buf)) {
+                       if (n_pending > (int)sizeof (buf))
                                n_pending = sizeof (buf);
-                       }
-                       for (i = 0; i < n_pending; ++i) {
-                               buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm, hmidi->id);
-                       }
-                       if (n_pending) {
-                               snd_rawmidi_receive (hmidi->input, buf, n_pending);
-                       }
+                       for (i = 0; i < n_pending; ++i)
+                               buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
+                                                                  hmidi->id);
+                       if (n_pending)
+                               snd_rawmidi_receive (hmidi->input, buf,
+                                                    n_pending);
                } else {
                        /* flush the MIDI input FIFO */
-                       while (n_pending--) {
-                               snd_hdspm_midi_read_byte (hmidi->hdspm, hmidi->id);
-                       }
+                       while (n_pending--)
+                               snd_hdspm_midi_read_byte (hmidi->hdspm,
+                                                         hmidi->id);
                }
        }
        hmidi->pending = 0;
-       if (hmidi->id) {
+       if (hmidi->id)
                hmidi->hdspm->control_register |= HDSPM_Midi1InterruptEnable;
-       } else {
+       else
                hmidi->hdspm->control_register |= HDSPM_Midi0InterruptEnable;
-       }
-       hdspm_write(hmidi->hdspm, HDSPM_controlRegister, hmidi->hdspm->control_register);
+       hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
+                   hmidi->hdspm->control_register);
        spin_unlock_irqrestore (&hmidi->lock, flags);
        return snd_hdspm_midi_output_write (hmidi);
 }
 
-static void snd_hdspm_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
+static void
+snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
 {
-       hdspm_t *hdspm;
-       hdspm_midi_t *hmidi;
+       struct hdspm *hdspm;
+       struct hdspm_midi *hmidi;
        unsigned long flags;
        u32 ie;
 
-       hmidi = (hdspm_midi_t *) substream->rmidi->private_data;
+       hmidi = substream->rmidi->private_data;
        hdspm = hmidi->hdspm;
-       ie = hmidi->id ? HDSPM_Midi1InterruptEnable : HDSPM_Midi0InterruptEnable;
+       ie = hmidi->id ?
+               HDSPM_Midi1InterruptEnable : HDSPM_Midi0InterruptEnable;
        spin_lock_irqsave (&hdspm->lock, flags);
        if (up) {
                if (!(hdspm->control_register & ie)) {
@@ -1005,7 +1165,7 @@ static void snd_hdspm_midi_input_trigger(snd_rawmidi_substream_t * substream, in
 
 static void snd_hdspm_midi_output_timer(unsigned long data)
 {
-       hdspm_midi_t *hmidi = (hdspm_midi_t *) data;
+       struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
        unsigned long flags;
        
        snd_hdspm_midi_output_write(hmidi);
@@ -1025,12 +1185,13 @@ static void snd_hdspm_midi_output_timer(unsigned long data)
        spin_unlock_irqrestore (&hmidi->lock, flags);
 }
 
-static void snd_hdspm_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
+static void
+snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
 {
-       hdspm_midi_t *hmidi;
+       struct hdspm_midi *hmidi;
        unsigned long flags;
 
-       hmidi = (hdspm_midi_t *) substream->rmidi->private_data;
+       hmidi = substream->rmidi->private_data;
        spin_lock_irqsave (&hmidi->lock, flags);
        if (up) {
                if (!hmidi->istimer) {
@@ -1042,20 +1203,19 @@ static void snd_hdspm_midi_output_trigger(snd_rawmidi_substream_t * substream, i
                        hmidi->istimer++;
                }
        } else {
-               if (hmidi->istimer && --hmidi->istimer <= 0) {
+               if (hmidi->istimer && --hmidi->istimer <= 0)
                        del_timer (&hmidi->timer);
-               }
        }
        spin_unlock_irqrestore (&hmidi->lock, flags);
        if (up)
                snd_hdspm_midi_output_write(hmidi);
 }
 
-static int snd_hdspm_midi_input_open(snd_rawmidi_substream_t * substream)
+static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
 {
-       hdspm_midi_t *hmidi;
+       struct hdspm_midi *hmidi;
 
-       hmidi = (hdspm_midi_t *) substream->rmidi->private_data;
+       hmidi = substream->rmidi->private_data;
        spin_lock_irq (&hmidi->lock);
        snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
        hmidi->input = substream;
@@ -1064,11 +1224,11 @@ static int snd_hdspm_midi_input_open(snd_rawmidi_substream_t * substream)
        return 0;
 }
 
-static int snd_hdspm_midi_output_open(snd_rawmidi_substream_t * substream)
+static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
 {
-       hdspm_midi_t *hmidi;
+       struct hdspm_midi *hmidi;
 
-       hmidi = (hdspm_midi_t *) substream->rmidi->private_data;
+       hmidi = substream->rmidi->private_data;
        spin_lock_irq (&hmidi->lock);
        hmidi->output = substream;
        spin_unlock_irq (&hmidi->lock);
@@ -1076,13 +1236,13 @@ static int snd_hdspm_midi_output_open(snd_rawmidi_substream_t * substream)
        return 0;
 }
 
-static int snd_hdspm_midi_input_close(snd_rawmidi_substream_t * substream)
+static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
 {
-       hdspm_midi_t *hmidi;
+       struct hdspm_midi *hmidi;
 
        snd_hdspm_midi_input_trigger (substream, 0);
 
-       hmidi = (hdspm_midi_t *) substream->rmidi->private_data;
+       hmidi = substream->rmidi->private_data;
        spin_lock_irq (&hmidi->lock);
        hmidi->input = NULL;
        spin_unlock_irq (&hmidi->lock);
@@ -1090,13 +1250,13 @@ static int snd_hdspm_midi_input_close(snd_rawmidi_substream_t * substream)
        return 0;
 }
 
-static int snd_hdspm_midi_output_close(snd_rawmidi_substream_t * substream)
+static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
 {
-       hdspm_midi_t *hmidi;
+       struct hdspm_midi *hmidi;
 
        snd_hdspm_midi_output_trigger (substream, 0);
 
-       hmidi = (hdspm_midi_t *) substream->rmidi->private_data;
+       hmidi = substream->rmidi->private_data;
        spin_lock_irq (&hmidi->lock);
        hmidi->output = NULL;
        spin_unlock_irq (&hmidi->lock);
@@ -1104,43 +1264,42 @@ static int snd_hdspm_midi_output_close(snd_rawmidi_substream_t * substream)
        return 0;
 }
 
-snd_rawmidi_ops_t snd_hdspm_midi_output =
+static struct snd_rawmidi_ops snd_hdspm_midi_output =
 {
        .open =         snd_hdspm_midi_output_open,
        .close =        snd_hdspm_midi_output_close,
        .trigger =      snd_hdspm_midi_output_trigger,
 };
 
-snd_rawmidi_ops_t snd_hdspm_midi_input =
+static struct snd_rawmidi_ops snd_hdspm_midi_input =
 {
        .open =         snd_hdspm_midi_input_open,
        .close =        snd_hdspm_midi_input_close,
        .trigger =      snd_hdspm_midi_input_trigger,
 };
 
-static int __devinit snd_hdspm_create_midi (snd_card_t *card, hdspm_t *hdspm, int id)
+static int __devinit snd_hdspm_create_midi (struct snd_card *card,
+                                           struct hdspm *hdspm, int id)
 {
        int err;
        char buf[32];
 
        hdspm->midi[id].id = id;
-       hdspm->midi[id].rmidi = NULL;
-       hdspm->midi[id].input = NULL;
-       hdspm->midi[id].output = NULL;
        hdspm->midi[id].hdspm = hdspm;
-       hdspm->midi[id].istimer = 0;
-       hdspm->midi[id].pending = 0;
        spin_lock_init (&hdspm->midi[id].lock);
 
        sprintf (buf, "%s MIDI %d", card->shortname, id+1);
-       if ((err = snd_rawmidi_new (card, buf, id, 1, 1, &hdspm->midi[id].rmidi)) < 0)
+       err = snd_rawmidi_new (card, buf, id, 1, 1, &hdspm->midi[id].rmidi);
+       if (err < 0)
                return err;
 
-       sprintf (hdspm->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
+       sprintf(hdspm->midi[id].rmidi->name, "HDSPM MIDI %d", id+1);
        hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
 
-       snd_rawmidi_set_ops (hdspm->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdspm_midi_output);
-       snd_rawmidi_set_ops (hdspm->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdspm_midi_input);
+       snd_rawmidi_set_ops(hdspm->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
+                           &snd_hdspm_midi_output);
+       snd_rawmidi_set_ops(hdspm->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
+                           &snd_hdspm_midi_input);
 
        hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
                SNDRV_RAWMIDI_INFO_INPUT |
@@ -1152,7 +1311,7 @@ static int __devinit snd_hdspm_create_midi (snd_card_t *card, hdspm_t *hdspm, in
 
 static void hdspm_midi_tasklet(unsigned long arg)
 {
-       hdspm_t *hdspm = (hdspm_t *)arg;
+       struct hdspm *hdspm = (struct hdspm *)arg;
        
        if (hdspm->midi[0].pending)
                snd_hdspm_midi_input_read (&hdspm->midi[0]);
@@ -1168,7 +1327,7 @@ static void hdspm_midi_tasklet(unsigned long arg)
 /* get the system sample rate which is set */
 
 #define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
@@ -1176,26 +1335,26 @@ static void hdspm_midi_tasklet(unsigned long arg)
   .get = snd_hdspm_get_system_sample_rate \
 }
 
-static int snd_hdspm_info_system_sample_rate(snd_kcontrol_t * kcontrol,
-                                            snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
+                                            struct snd_ctl_elem_info *uinfo)
 {
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 1;
        return 0;
 }
 
-static int snd_hdspm_get_system_sample_rate(snd_kcontrol_t * kcontrol,
-                                           snd_ctl_elem_value_t *
+static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
+                                           struct snd_ctl_elem_value *
                                            ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] = hdspm->system_sample_rate;
        return 0;
 }
 
 #define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_PCM, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
@@ -1203,8 +1362,8 @@ static int snd_hdspm_get_system_sample_rate(snd_kcontrol_t * kcontrol,
   .get = snd_hdspm_get_autosync_sample_rate \
 }
 
-static int snd_hdspm_info_autosync_sample_rate(snd_kcontrol_t * kcontrol,
-                                              snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
+                                              struct snd_ctl_elem_info *uinfo)
 {
        static char *texts[] = { "32000", "44100", "48000",
                "64000", "88200", "96000",
@@ -1222,11 +1381,11 @@ static int snd_hdspm_info_autosync_sample_rate(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_get_autosync_sample_rate(snd_kcontrol_t * kcontrol,
-                                             snd_ctl_elem_value_t *
+static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
+                                             struct snd_ctl_elem_value *
                                              ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        switch (hdspm_external_sample_rate(hdspm)) {
        case 32000:
@@ -1264,7 +1423,7 @@ static int snd_hdspm_get_autosync_sample_rate(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
@@ -1274,7 +1433,7 @@ static int snd_hdspm_get_autosync_sample_rate(snd_kcontrol_t * kcontrol,
 
 
 
-static int hdspm_system_clock_mode(hdspm_t * hdspm)
+static int hdspm_system_clock_mode(struct hdspm * hdspm)
 {
         /* Always reflect the hardware info, rme is never wrong !!!! */
 
@@ -1283,8 +1442,8 @@ static int hdspm_system_clock_mode(hdspm_t * hdspm)
        return 1;
 }
 
-static int snd_hdspm_info_system_clock_mode(snd_kcontrol_t * kcontrol,
-                                           snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
+                                           struct snd_ctl_elem_info *uinfo)
 {
        static char *texts[] = { "Master", "Slave" };
 
@@ -1299,10 +1458,10 @@ static int snd_hdspm_info_system_clock_mode(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_get_system_clock_mode(snd_kcontrol_t * kcontrol,
-                                          snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
+                                          struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] =
            hdspm_system_clock_mode(hdspm);
@@ -1310,7 +1469,7 @@ static int snd_hdspm_get_system_clock_mode(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_CLOCK_SOURCE(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_PCM, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .info = snd_hdspm_info_clock_source, \
@@ -1318,7 +1477,7 @@ static int snd_hdspm_get_system_clock_mode(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_clock_source \
 }
 
-static int hdspm_clock_source(hdspm_t * hdspm)
+static int hdspm_clock_source(struct hdspm * hdspm)
 {
        if (hdspm->control_register & HDSPM_ClockModeMaster) {
                switch (hdspm->system_sample_rate) {
@@ -1348,7 +1507,7 @@ static int hdspm_clock_source(hdspm_t * hdspm)
        }
 }
 
-static int hdspm_set_clock_source(hdspm_t * hdspm, int mode)
+static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
 {
        int rate;
        switch (mode) {
@@ -1398,8 +1557,8 @@ static int hdspm_set_clock_source(hdspm_t * hdspm, int mode)
        return 0;
 }
 
-static int snd_hdspm_info_clock_source(snd_kcontrol_t * kcontrol,
-                                      snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_info *uinfo)
 {
        static char *texts[] = { "AutoSync",
                "Internal 32.0 kHz", "Internal 44.1 kHz",
@@ -1424,19 +1583,19 @@ static int snd_hdspm_info_clock_source(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_get_clock_source(snd_kcontrol_t * kcontrol,
-                                     snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
        return 0;
 }
 
-static int snd_hdspm_put_clock_source(snd_kcontrol_t * kcontrol,
-                                     snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        int val;
 
@@ -1445,8 +1604,8 @@ static int snd_hdspm_put_clock_source(snd_kcontrol_t * kcontrol,
        val = ucontrol->value.enumerated.item[0];
        if (val < 0)
                val = 0;
-       if (val > 6)
-               val = 6;
+       if (val > 9)
+               val = 9;
        spin_lock_irq(&hdspm->lock);
        if (val != hdspm_clock_source(hdspm))
                change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
@@ -1457,7 +1616,7 @@ static int snd_hdspm_put_clock_source(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_PREF_SYNC_REF(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .info = snd_hdspm_info_pref_sync_ref, \
@@ -1465,74 +1624,145 @@ static int snd_hdspm_put_clock_source(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_pref_sync_ref \
 }
 
-static int hdspm_pref_sync_ref(hdspm_t * hdspm)
+static int hdspm_pref_sync_ref(struct hdspm * hdspm)
 {
        /* Notice that this looks at the requested sync source,
           not the one actually in use.
         */
-       switch (hdspm->control_register & HDSPM_SyncRefMask) {
-       case HDSPM_SyncRef_Word:
-               return HDSPM_SYNC_FROM_WORD;
-       case HDSPM_SyncRef_MADI:
-               return HDSPM_SYNC_FROM_MADI;
+       if (hdspm->is_aes32) {
+               switch (hdspm->control_register & HDSPM_SyncRefMask) {
+               /* number gives AES index, except for 0 which
+                  corresponds to WordClock */
+               case 0: return 0;
+               case HDSPM_SyncRef0: return 1;
+               case HDSPM_SyncRef1: return 2;
+               case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3;
+               case HDSPM_SyncRef2: return 4;
+               case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5;
+               case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6;
+               case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0: return 7;
+               case HDSPM_SyncRef3: return 8;
+               }
+       } else {
+               switch (hdspm->control_register & HDSPM_SyncRefMask) {
+               case HDSPM_SyncRef_Word:
+                       return HDSPM_SYNC_FROM_WORD;
+               case HDSPM_SyncRef_MADI:
+                       return HDSPM_SYNC_FROM_MADI;
+               }
        }
 
        return HDSPM_SYNC_FROM_WORD;
 }
 
-static int hdspm_set_pref_sync_ref(hdspm_t * hdspm, int pref)
+static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
 {
        hdspm->control_register &= ~HDSPM_SyncRefMask;
 
-       switch (pref) {
-       case HDSPM_SYNC_FROM_MADI:
-               hdspm->control_register |= HDSPM_SyncRef_MADI;
-               break;
-       case HDSPM_SYNC_FROM_WORD:
-               hdspm->control_register |= HDSPM_SyncRef_Word;
-               break;
-       default:
-               return -1;
+       if (hdspm->is_aes32) {
+               switch (pref) {
+               case 0:
+                      hdspm->control_register |= 0;
+                      break;
+               case 1:
+                      hdspm->control_register |= HDSPM_SyncRef0;
+                      break;
+               case 2:
+                      hdspm->control_register |= HDSPM_SyncRef1;
+                      break;
+               case 3:
+                      hdspm->control_register |= HDSPM_SyncRef1+HDSPM_SyncRef0;
+                      break;
+               case 4:
+                      hdspm->control_register |= HDSPM_SyncRef2;
+                      break;
+               case 5:
+                      hdspm->control_register |= HDSPM_SyncRef2+HDSPM_SyncRef0;
+                      break;
+               case 6:
+                      hdspm->control_register |= HDSPM_SyncRef2+HDSPM_SyncRef1;
+                      break;
+               case 7:
+                      hdspm->control_register |=
+                              HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
+                      break;
+               case 8:
+                      hdspm->control_register |= HDSPM_SyncRef3;
+                      break;
+               default:
+                      return -1;
+               }
+       } else {
+               switch (pref) {
+               case HDSPM_SYNC_FROM_MADI:
+                       hdspm->control_register |= HDSPM_SyncRef_MADI;
+                       break;
+               case HDSPM_SYNC_FROM_WORD:
+                       hdspm->control_register |= HDSPM_SyncRef_Word;
+                       break;
+               default:
+                       return -1;
+               }
        }
        hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
        return 0;
 }
 
-static int snd_hdspm_info_pref_sync_ref(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_info *uinfo)
 {
-       static char *texts[] = { "Word", "MADI" };
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
-       uinfo->count = 1;
+       if (hdspm->is_aes32) {
+               static char *texts[] = { "Word", "AES1", "AES2", "AES3",
+                       "AES4", "AES5", "AES6", "AES7", "AES8" };
 
-       uinfo->value.enumerated.items = 2;
+               uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+               uinfo->count = 1;
 
-       if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
-               uinfo->value.enumerated.item =
-                   uinfo->value.enumerated.items - 1;
-       strcpy(uinfo->value.enumerated.name,
-              texts[uinfo->value.enumerated.item]);
+               uinfo->value.enumerated.items = 9;
+
+               if (uinfo->value.enumerated.item >=
+                   uinfo->value.enumerated.items)
+                       uinfo->value.enumerated.item =
+                               uinfo->value.enumerated.items - 1;
+               strcpy(uinfo->value.enumerated.name,
+                               texts[uinfo->value.enumerated.item]);
+       } else {
+               static char *texts[] = { "Word", "MADI" };
+
+               uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+               uinfo->count = 1;
+
+               uinfo->value.enumerated.items = 2;
+
+               if (uinfo->value.enumerated.item >=
+                   uinfo->value.enumerated.items)
+                       uinfo->value.enumerated.item =
+                               uinfo->value.enumerated.items - 1;
+               strcpy(uinfo->value.enumerated.name,
+                               texts[uinfo->value.enumerated.item]);
+       }
        return 0;
 }
 
-static int snd_hdspm_get_pref_sync_ref(snd_kcontrol_t * kcontrol,
-                                      snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] = hdspm_pref_sync_ref(hdspm);
        return 0;
 }
 
-static int snd_hdspm_put_pref_sync_ref(snd_kcontrol_t * kcontrol,
-                                      snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change, max;
        unsigned int val;
 
-       max = 2;
+       max = hdspm->is_aes32 ? 9 : 2;
 
        if (!snd_hdspm_use_is_exclusive(hdspm))
                return -EBUSY;
@@ -1547,7 +1777,7 @@ static int snd_hdspm_put_pref_sync_ref(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_AUTOSYNC_REF(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
@@ -1555,56 +1785,81 @@ static int snd_hdspm_put_pref_sync_ref(snd_kcontrol_t * kcontrol,
   .get = snd_hdspm_get_autosync_ref, \
 }
 
-static int hdspm_autosync_ref(hdspm_t * hdspm)
+static int hdspm_autosync_ref(struct hdspm * hdspm)
 {
-       /* This looks at the autosync selected sync reference */
-       unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
-
-       switch (status2 & HDSPM_SelSyncRefMask) {
-
-       case HDSPM_SelSyncRef_WORD:
-               return HDSPM_AUTOSYNC_FROM_WORD;
-
-       case HDSPM_SelSyncRef_MADI:
-               return HDSPM_AUTOSYNC_FROM_MADI;
-
-       case HDSPM_SelSyncRef_NVALID:
-               return HDSPM_AUTOSYNC_FROM_NONE;
+       if (hdspm->is_aes32) {
+               unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
+               unsigned int syncref = (status >> HDSPM_AES32_syncref_bit) &
+                       0xF;
+               if (syncref == 0)
+                       return HDSPM_AES32_AUTOSYNC_FROM_WORD;
+               if (syncref <= 8)
+                       return syncref;
+               return HDSPM_AES32_AUTOSYNC_FROM_NONE;
+       } else {
+               /* This looks at the autosync selected sync reference */
+               unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
+
+               switch (status2 & HDSPM_SelSyncRefMask) {
+               case HDSPM_SelSyncRef_WORD:
+                       return HDSPM_AUTOSYNC_FROM_WORD;
+               case HDSPM_SelSyncRef_MADI:
+                       return HDSPM_AUTOSYNC_FROM_MADI;
+               case HDSPM_SelSyncRef_NVALID:
+                       return HDSPM_AUTOSYNC_FROM_NONE;
+               default:
+                       return 0;
+               }
 
-       default:
                return 0;
        }
-
-       return 0;
 }
 
-static int snd_hdspm_info_autosync_ref(snd_kcontrol_t * kcontrol,
-                                      snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_info *uinfo)
 {
-       static char *texts[] = { "WordClock", "MADI", "None" };
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
-       uinfo->count = 1;
-       uinfo->value.enumerated.items = 3;
-       if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
-               uinfo->value.enumerated.item =
-                   uinfo->value.enumerated.items - 1;
-       strcpy(uinfo->value.enumerated.name,
-              texts[uinfo->value.enumerated.item]);
+       if (hdspm->is_aes32) {
+               static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
+                       "AES4", "AES5", "AES6", "AES7", "AES8", "None"};
+
+               uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+               uinfo->count = 1;
+               uinfo->value.enumerated.items = 10;
+               if (uinfo->value.enumerated.item >=
+                   uinfo->value.enumerated.items)
+                       uinfo->value.enumerated.item =
+                               uinfo->value.enumerated.items - 1;
+               strcpy(uinfo->value.enumerated.name,
+                               texts[uinfo->value.enumerated.item]);
+       } else {
+               static char *texts[] = { "WordClock", "MADI", "None" };
+
+               uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+               uinfo->count = 1;
+               uinfo->value.enumerated.items = 3;
+               if (uinfo->value.enumerated.item >=
+                   uinfo->value.enumerated.items)
+                       uinfo->value.enumerated.item =
+                               uinfo->value.enumerated.items - 1;
+               strcpy(uinfo->value.enumerated.name,
+                               texts[uinfo->value.enumerated.item]);
+       }
        return 0;
 }
 
-static int snd_hdspm_get_autosync_ref(snd_kcontrol_t * kcontrol,
-                                     snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
-       ucontrol->value.enumerated.item[0] = hdspm_pref_sync_ref(hdspm);
+       ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
        return 0;
 }
 
 #define HDSPM_LINE_OUT(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .info = snd_hdspm_info_line_out, \
@@ -1612,13 +1867,13 @@ static int snd_hdspm_get_autosync_ref(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_line_out \
 }
 
-static int hdspm_line_out(hdspm_t * hdspm)
+static int hdspm_line_out(struct hdspm * hdspm)
 {
        return (hdspm->control_register & HDSPM_LineOut) ? 1 : 0;
 }
 
 
-static int hdspm_set_line_output(hdspm_t * hdspm, int out)
+static int hdspm_set_line_output(struct hdspm * hdspm, int out)
 {
        if (out)
                hdspm->control_register |= HDSPM_LineOut;
@@ -1629,20 +1884,12 @@ static int hdspm_set_line_output(hdspm_t * hdspm, int out)
        return 0;
 }
 
-static int snd_hdspm_info_line_out(snd_kcontrol_t * kcontrol,
-                                  snd_ctl_elem_info_t * uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+#define snd_hdspm_info_line_out                snd_ctl_boolean_mono_info
 
-static int snd_hdspm_get_line_out(snd_kcontrol_t * kcontrol,
-                                 snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_line_out(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        spin_lock_irq(&hdspm->lock);
        ucontrol->value.integer.value[0] = hdspm_line_out(hdspm);
@@ -1650,10 +1897,10 @@ static int snd_hdspm_get_line_out(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_put_line_out(snd_kcontrol_t * kcontrol,
-                                 snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_line_out(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        unsigned int val;
 
@@ -1668,7 +1915,7 @@ static int snd_hdspm_put_line_out(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_TX_64(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .info = snd_hdspm_info_tx_64, \
@@ -1676,12 +1923,12 @@ static int snd_hdspm_put_line_out(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_tx_64 \
 }
 
-static int hdspm_tx_64(hdspm_t * hdspm)
+static int hdspm_tx_64(struct hdspm * hdspm)
 {
        return (hdspm->control_register & HDSPM_TX_64ch) ? 1 : 0;
 }
 
-static int hdspm_set_tx_64(hdspm_t * hdspm, int out)
+static int hdspm_set_tx_64(struct hdspm * hdspm, int out)
 {
        if (out)
                hdspm->control_register |= HDSPM_TX_64ch;
@@ -1692,20 +1939,12 @@ static int hdspm_set_tx_64(hdspm_t * hdspm, int out)
        return 0;
 }
 
-static int snd_hdspm_info_tx_64(snd_kcontrol_t * kcontrol,
-                               snd_ctl_elem_info_t * uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+#define snd_hdspm_info_tx_64           snd_ctl_boolean_mono_info
 
-static int snd_hdspm_get_tx_64(snd_kcontrol_t * kcontrol,
-                              snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_tx_64(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        spin_lock_irq(&hdspm->lock);
        ucontrol->value.integer.value[0] = hdspm_tx_64(hdspm);
@@ -1713,10 +1952,10 @@ static int snd_hdspm_get_tx_64(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_put_tx_64(snd_kcontrol_t * kcontrol,
-                              snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_tx_64(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        unsigned int val;
 
@@ -1731,7 +1970,7 @@ static int snd_hdspm_put_tx_64(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_C_TMS(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .info = snd_hdspm_info_c_tms, \
@@ -1739,12 +1978,12 @@ static int snd_hdspm_put_tx_64(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_c_tms \
 }
 
-static int hdspm_c_tms(hdspm_t * hdspm)
+static int hdspm_c_tms(struct hdspm * hdspm)
 {
        return (hdspm->control_register & HDSPM_clr_tms) ? 1 : 0;
 }
 
-static int hdspm_set_c_tms(hdspm_t * hdspm, int out)
+static int hdspm_set_c_tms(struct hdspm * hdspm, int out)
 {
        if (out)
                hdspm->control_register |= HDSPM_clr_tms;
@@ -1755,20 +1994,12 @@ static int hdspm_set_c_tms(hdspm_t * hdspm, int out)
        return 0;
 }
 
-static int snd_hdspm_info_c_tms(snd_kcontrol_t * kcontrol,
-                               snd_ctl_elem_info_t * uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+#define snd_hdspm_info_c_tms           snd_ctl_boolean_mono_info
 
-static int snd_hdspm_get_c_tms(snd_kcontrol_t * kcontrol,
-                              snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_c_tms(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        spin_lock_irq(&hdspm->lock);
        ucontrol->value.integer.value[0] = hdspm_c_tms(hdspm);
@@ -1776,10 +2007,10 @@ static int snd_hdspm_get_c_tms(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_put_c_tms(snd_kcontrol_t * kcontrol,
-                              snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_c_tms(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        unsigned int val;
 
@@ -1794,7 +2025,7 @@ static int snd_hdspm_put_c_tms(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_SAFE_MODE(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .info = snd_hdspm_info_safe_mode, \
@@ -1802,12 +2033,12 @@ static int snd_hdspm_put_c_tms(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_safe_mode \
 }
 
-static int hdspm_safe_mode(hdspm_t * hdspm)
+static int hdspm_safe_mode(struct hdspm * hdspm)
 {
        return (hdspm->control_register & HDSPM_AutoInp) ? 1 : 0;
 }
 
-static int hdspm_set_safe_mode(hdspm_t * hdspm, int out)
+static int hdspm_set_safe_mode(struct hdspm * hdspm, int out)
 {
        if (out)
                hdspm->control_register |= HDSPM_AutoInp;
@@ -1818,20 +2049,12 @@ static int hdspm_set_safe_mode(hdspm_t * hdspm, int out)
        return 0;
 }
 
-static int snd_hdspm_info_safe_mode(snd_kcontrol_t * kcontrol,
-                                   snd_ctl_elem_info_t * uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+#define snd_hdspm_info_safe_mode       snd_ctl_boolean_mono_info
 
-static int snd_hdspm_get_safe_mode(snd_kcontrol_t * kcontrol,
-                                  snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_safe_mode(struct snd_kcontrol *kcontrol,
+                                  struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        spin_lock_irq(&hdspm->lock);
        ucontrol->value.integer.value[0] = hdspm_safe_mode(hdspm);
@@ -1839,10 +2062,10 @@ static int snd_hdspm_get_safe_mode(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_put_safe_mode(snd_kcontrol_t * kcontrol,
-                                  snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_safe_mode(struct snd_kcontrol *kcontrol,
+                                  struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        unsigned int val;
 
@@ -1856,64 +2079,48 @@ static int snd_hdspm_put_safe_mode(snd_kcontrol_t * kcontrol,
        return change;
 }
 
-#define HDSPM_INPUT_SELECT(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+#define HDSPM_EMPHASIS(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
-  .info = snd_hdspm_info_input_select, \
-  .get = snd_hdspm_get_input_select, \
-  .put = snd_hdspm_put_input_select \
+  .info = snd_hdspm_info_emphasis, \
+  .get = snd_hdspm_get_emphasis, \
+  .put = snd_hdspm_put_emphasis \
 }
 
-static int hdspm_input_select(hdspm_t * hdspm)
+static int hdspm_emphasis(struct hdspm * hdspm)
 {
-       return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
+       return (hdspm->control_register & HDSPM_Emphasis) ? 1 : 0;
 }
 
-static int hdspm_set_input_select(hdspm_t * hdspm, int out)
+static int hdspm_set_emphasis(struct hdspm * hdspm, int emp)
 {
-       if (out)
-               hdspm->control_register |= HDSPM_InputSelect0;
+       if (emp)
+               hdspm->control_register |= HDSPM_Emphasis;
        else
-               hdspm->control_register &= ~HDSPM_InputSelect0;
+               hdspm->control_register &= ~HDSPM_Emphasis;
        hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
 
        return 0;
 }
 
-static int snd_hdspm_info_input_select(snd_kcontrol_t * kcontrol,
-                                      snd_ctl_elem_info_t * uinfo)
-{
-       static char *texts[] = { "optical", "coaxial" };
-
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
-       uinfo->count = 1;
-       uinfo->value.enumerated.items = 2;
-
-       if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
-               uinfo->value.enumerated.item =
-                   uinfo->value.enumerated.items - 1;
-       strcpy(uinfo->value.enumerated.name,
-              texts[uinfo->value.enumerated.item]);
-
-       return 0;
-}
+#define snd_hdspm_info_emphasis                snd_ctl_boolean_mono_info
 
-static int snd_hdspm_get_input_select(snd_kcontrol_t * kcontrol,
-                                     snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_emphasis(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        spin_lock_irq(&hdspm->lock);
-       ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
+       ucontrol->value.enumerated.item[0] = hdspm_emphasis(hdspm);
        spin_unlock_irq(&hdspm->lock);
        return 0;
 }
 
-static int snd_hdspm_put_input_select(snd_kcontrol_t * kcontrol,
-                                     snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_emphasis(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        unsigned int val;
 
@@ -1921,51 +2128,390 @@ static int snd_hdspm_put_input_select(snd_kcontrol_t * kcontrol,
                return -EBUSY;
        val = ucontrol->value.integer.value[0] & 1;
        spin_lock_irq(&hdspm->lock);
-       change = (int) val != hdspm_input_select(hdspm);
-       hdspm_set_input_select(hdspm, val);
+       change = (int) val != hdspm_emphasis(hdspm);
+       hdspm_set_emphasis(hdspm, val);
        spin_unlock_irq(&hdspm->lock);
        return change;
 }
 
-/*           Simple Mixer
-  deprecated since to much faders ???
-  MIXER interface says output (source, destination, value)
-   where source > MAX_channels are playback channels 
-   on MADICARD 
-  - playback mixer matrix: [channelout+64] [output] [value]
-  - input(thru) mixer matrix: [channelin] [output] [value]
-  (better do 2 kontrols for seperation ?)
-*/
-
-#define HDSPM_MIXER(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+#define HDSPM_DOLBY(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
-  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
-                SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
-  .info = snd_hdspm_info_mixer, \
-  .get = snd_hdspm_get_mixer, \
-  .put = snd_hdspm_put_mixer \
+  .info = snd_hdspm_info_dolby, \
+  .get = snd_hdspm_get_dolby, \
+  .put = snd_hdspm_put_dolby \
 }
 
-static int snd_hdspm_info_mixer(snd_kcontrol_t * kcontrol,
-                               snd_ctl_elem_info_t * uinfo)
+static int hdspm_dolby(struct hdspm * hdspm)
 {
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
-       uinfo->count = 3;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 65535;
-       uinfo->value.integer.step = 1;
-       return 0;
+       return (hdspm->control_register & HDSPM_Dolby) ? 1 : 0;
 }
 
-static int snd_hdspm_get_mixer(snd_kcontrol_t * kcontrol,
-                              snd_ctl_elem_value_t * ucontrol)
+static int hdspm_set_dolby(struct hdspm * hdspm, int dol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
-       int source;
-       int destination;
-
+       if (dol)
+               hdspm->control_register |= HDSPM_Dolby;
+       else
+               hdspm->control_register &= ~HDSPM_Dolby;
+       hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
+
+       return 0;
+}
+
+#define snd_hdspm_info_dolby           snd_ctl_boolean_mono_info
+
+static int snd_hdspm_get_dolby(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+
+       spin_lock_irq(&hdspm->lock);
+       ucontrol->value.enumerated.item[0] = hdspm_dolby(hdspm);
+       spin_unlock_irq(&hdspm->lock);
+       return 0;
+}
+
+static int snd_hdspm_put_dolby(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+       int change;
+       unsigned int val;
+
+       if (!snd_hdspm_use_is_exclusive(hdspm))
+               return -EBUSY;
+       val = ucontrol->value.integer.value[0] & 1;
+       spin_lock_irq(&hdspm->lock);
+       change = (int) val != hdspm_dolby(hdspm);
+       hdspm_set_dolby(hdspm, val);
+       spin_unlock_irq(&hdspm->lock);
+       return change;
+}
+
+#define HDSPM_PROFESSIONAL(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .name = xname, \
+  .index = xindex, \
+  .info = snd_hdspm_info_professional, \
+  .get = snd_hdspm_get_professional, \
+  .put = snd_hdspm_put_professional \
+}
+
+static int hdspm_professional(struct hdspm * hdspm)
+{
+       return (hdspm->control_register & HDSPM_Professional) ? 1 : 0;
+}
+
+static int hdspm_set_professional(struct hdspm * hdspm, int dol)
+{
+       if (dol)
+               hdspm->control_register |= HDSPM_Professional;
+       else
+               hdspm->control_register &= ~HDSPM_Professional;
+       hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
+
+       return 0;
+}
+
+#define snd_hdspm_info_professional    snd_ctl_boolean_mono_info
+
+static int snd_hdspm_get_professional(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+
+       spin_lock_irq(&hdspm->lock);
+       ucontrol->value.enumerated.item[0] = hdspm_professional(hdspm);
+       spin_unlock_irq(&hdspm->lock);
+       return 0;
+}
+
+static int snd_hdspm_put_professional(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+       int change;
+       unsigned int val;
+
+       if (!snd_hdspm_use_is_exclusive(hdspm))
+               return -EBUSY;
+       val = ucontrol->value.integer.value[0] & 1;
+       spin_lock_irq(&hdspm->lock);
+       change = (int) val != hdspm_professional(hdspm);
+       hdspm_set_professional(hdspm, val);
+       spin_unlock_irq(&hdspm->lock);
+       return change;
+}
+
+#define HDSPM_INPUT_SELECT(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .name = xname, \
+  .index = xindex, \
+  .info = snd_hdspm_info_input_select, \
+  .get = snd_hdspm_get_input_select, \
+  .put = snd_hdspm_put_input_select \
+}
+
+static int hdspm_input_select(struct hdspm * hdspm)
+{
+       return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
+}
+
+static int hdspm_set_input_select(struct hdspm * hdspm, int out)
+{
+       if (out)
+               hdspm->control_register |= HDSPM_InputSelect0;
+       else
+               hdspm->control_register &= ~HDSPM_InputSelect0;
+       hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
+
+       return 0;
+}
+
+static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_info *uinfo)
+{
+       static char *texts[] = { "optical", "coaxial" };
+
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+       uinfo->count = 1;
+       uinfo->value.enumerated.items = 2;
+
+       if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
+               uinfo->value.enumerated.item =
+                   uinfo->value.enumerated.items - 1;
+       strcpy(uinfo->value.enumerated.name,
+              texts[uinfo->value.enumerated.item]);
+
+       return 0;
+}
+
+static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+
+       spin_lock_irq(&hdspm->lock);
+       ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
+       spin_unlock_irq(&hdspm->lock);
+       return 0;
+}
+
+static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+       int change;
+       unsigned int val;
+
+       if (!snd_hdspm_use_is_exclusive(hdspm))
+               return -EBUSY;
+       val = ucontrol->value.integer.value[0] & 1;
+       spin_lock_irq(&hdspm->lock);
+       change = (int) val != hdspm_input_select(hdspm);
+       hdspm_set_input_select(hdspm, val);
+       spin_unlock_irq(&hdspm->lock);
+       return change;
+}
+
+#define HDSPM_DS_WIRE(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .name = xname, \
+  .index = xindex, \
+  .info = snd_hdspm_info_ds_wire, \
+  .get = snd_hdspm_get_ds_wire, \
+  .put = snd_hdspm_put_ds_wire \
+}
+
+static int hdspm_ds_wire(struct hdspm * hdspm)
+{
+       return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
+}
+
+static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
+{
+       if (ds)
+               hdspm->control_register |= HDSPM_DS_DoubleWire;
+       else
+               hdspm->control_register &= ~HDSPM_DS_DoubleWire;
+       hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
+
+       return 0;
+}
+
+static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_info *uinfo)
+{
+       static char *texts[] = { "Single", "Double" };
+
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+       uinfo->count = 1;
+       uinfo->value.enumerated.items = 2;
+
+       if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
+               uinfo->value.enumerated.item =
+                   uinfo->value.enumerated.items - 1;
+       strcpy(uinfo->value.enumerated.name,
+              texts[uinfo->value.enumerated.item]);
+
+       return 0;
+}
+
+static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+
+       spin_lock_irq(&hdspm->lock);
+       ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
+       spin_unlock_irq(&hdspm->lock);
+       return 0;
+}
+
+static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+       int change;
+       unsigned int val;
+
+       if (!snd_hdspm_use_is_exclusive(hdspm))
+               return -EBUSY;
+       val = ucontrol->value.integer.value[0] & 1;
+       spin_lock_irq(&hdspm->lock);
+       change = (int) val != hdspm_ds_wire(hdspm);
+       hdspm_set_ds_wire(hdspm, val);
+       spin_unlock_irq(&hdspm->lock);
+       return change;
+}
+
+#define HDSPM_QS_WIRE(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .name = xname, \
+  .index = xindex, \
+  .info = snd_hdspm_info_qs_wire, \
+  .get = snd_hdspm_get_qs_wire, \
+  .put = snd_hdspm_put_qs_wire \
+}
+
+static int hdspm_qs_wire(struct hdspm * hdspm)
+{
+       if (hdspm->control_register & HDSPM_QS_DoubleWire)
+               return 1;
+       if (hdspm->control_register & HDSPM_QS_QuadWire)
+               return 2;
+       return 0;
+}
+
+static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
+{
+       hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
+       switch (mode) {
+       case 0:
+               break;
+       case 1:
+               hdspm->control_register |= HDSPM_QS_DoubleWire;
+               break;
+       case 2:
+               hdspm->control_register |= HDSPM_QS_QuadWire;
+               break;
+       }
+       hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
+
+       return 0;
+}
+
+static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_info *uinfo)
+{
+       static char *texts[] = { "Single", "Double", "Quad" };
+
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+       uinfo->count = 1;
+       uinfo->value.enumerated.items = 3;
+
+       if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
+               uinfo->value.enumerated.item =
+                   uinfo->value.enumerated.items - 1;
+       strcpy(uinfo->value.enumerated.name,
+              texts[uinfo->value.enumerated.item]);
+
+       return 0;
+}
+
+static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+
+       spin_lock_irq(&hdspm->lock);
+       ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
+       spin_unlock_irq(&hdspm->lock);
+       return 0;
+}
+
+static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+       int change;
+       int val;
+
+       if (!snd_hdspm_use_is_exclusive(hdspm))
+               return -EBUSY;
+       val = ucontrol->value.integer.value[0];
+       if (val < 0)
+               val = 0;
+       if (val > 2)
+               val = 2;
+       spin_lock_irq(&hdspm->lock);
+       change = val != hdspm_qs_wire(hdspm);
+       hdspm_set_qs_wire(hdspm, val);
+       spin_unlock_irq(&hdspm->lock);
+       return change;
+}
+
+/*           Simple Mixer
+  deprecated since to much faders ???
+  MIXER interface says output (source, destination, value)
+   where source > MAX_channels are playback channels 
+   on MADICARD 
+  - playback mixer matrix: [channelout+64] [output] [value]
+  - input(thru) mixer matrix: [channelin] [output] [value]
+  (better do 2 kontrols for seperation ?)
+*/
+
+#define HDSPM_MIXER(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+  .name = xname, \
+  .index = xindex, \
+  .device = 0, \
+  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
+                SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
+  .info = snd_hdspm_info_mixer, \
+  .get = snd_hdspm_get_mixer, \
+  .put = snd_hdspm_put_mixer \
+}
+
+static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_info *uinfo)
+{
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+       uinfo->count = 3;
+       uinfo->value.integer.min = 0;
+       uinfo->value.integer.max = 65535;
+       uinfo->value.integer.step = 1;
+       return 0;
+}
+
+static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
+{
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+       int source;
+       int destination;
+
        source = ucontrol->value.integer.value[0];
        if (source < 0)
                source = 0;
@@ -1992,10 +2538,10 @@ static int snd_hdspm_get_mixer(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_put_mixer(snd_kcontrol_t * kcontrol,
-                              snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        int source;
        int destination;
@@ -2021,8 +2567,8 @@ static int snd_hdspm_put_mixer(snd_kcontrol_t * kcontrol,
                                                    source -
                                                    HDSPM_MAX_CHANNELS);
        else
-               change =
-                   gain != hdspm_read_in_gain(hdspm, destination, source);
+               change = gain != hdspm_read_in_gain(hdspm, destination,
+                                                   source);
 
        if (change) {
                if (source >= HDSPM_MAX_CHANNELS)
@@ -2052,8 +2598,8 @@ static int snd_hdspm_put_mixer(snd_kcontrol_t * kcontrol,
   .put = snd_hdspm_put_playback_mixer \
 }
 
-static int snd_hdspm_info_playback_mixer(snd_kcontrol_t * kcontrol,
-                                        snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
+                                        struct snd_ctl_elem_info *uinfo)
 {
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 1;
@@ -2063,19 +2609,20 @@ static int snd_hdspm_info_playback_mixer(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int snd_hdspm_get_playback_mixer(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int channel;
        int mapped_channel;
 
        channel = ucontrol->id.index - 1;
 
-       snd_assert(channel >= 0
-                  || channel < HDSPM_MAX_CHANNELS, return -EINVAL);
+       if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
+               return -EINVAL;
 
-       if ((mapped_channel = hdspm->channel_map[channel]) < 0)
+       mapped_channel = hdspm->channel_map[channel];
+       if (mapped_channel < 0)
                return -EINVAL;
 
        spin_lock_irq(&hdspm->lock);
@@ -2083,17 +2630,19 @@ static int snd_hdspm_get_playback_mixer(snd_kcontrol_t * kcontrol,
            hdspm_read_pb_gain(hdspm, mapped_channel, mapped_channel);
        spin_unlock_irq(&hdspm->lock);
 
-       /*    snd_printdd("get pb mixer index %d, channel %d, mapped_channel %d, value %d\n",
-          ucontrol->id.index,        channel, mapped_channel,  ucontrol->value.integer.value[0]); 
-        */
-
+       /*
+       snd_printdd("get pb mixer index %d, channel %d, mapped_channel %d, "
+                   "value %d\n",
+                   ucontrol->id.index, channel, mapped_channel,
+                   ucontrol->value.integer.value[0]); 
+       */
        return 0;
 }
 
-static int snd_hdspm_put_playback_mixer(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
        int change;
        int channel;
        int mapped_channel;
@@ -2104,10 +2653,11 @@ static int snd_hdspm_put_playback_mixer(snd_kcontrol_t * kcontrol,
 
        channel = ucontrol->id.index - 1;
 
-       snd_assert(channel >= 0
-                  || channel < HDSPM_MAX_CHANNELS, return -EINVAL);
+       if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
+               return -EINVAL;
 
-       if ((mapped_channel = hdspm->channel_map[channel]) < 0)
+       mapped_channel = hdspm->channel_map[channel];
+       if (mapped_channel < 0)
                return -EINVAL;
 
        gain = ucontrol->value.integer.value[0];
@@ -2124,7 +2674,7 @@ static int snd_hdspm_put_playback_mixer(snd_kcontrol_t * kcontrol,
 }
 
 #define HDSPM_WC_SYNC_CHECK(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
@@ -2132,8 +2682,8 @@ static int snd_hdspm_put_playback_mixer(snd_kcontrol_t * kcontrol,
   .get = snd_hdspm_get_wc_sync_check \
 }
 
-static int snd_hdspm_info_sync_check(snd_kcontrol_t * kcontrol,
-                                    snd_ctl_elem_info_t * uinfo)
+static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
+                                    struct snd_ctl_elem_info *uinfo)
 {
        static char *texts[] = { "No Lock", "Lock", "Sync" };
        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
@@ -2147,22 +2697,32 @@ static int snd_hdspm_info_sync_check(snd_kcontrol_t * kcontrol,
        return 0;
 }
 
-static int hdspm_wc_sync_check(hdspm_t * hdspm)
+static int hdspm_wc_sync_check(struct hdspm * hdspm)
 {
-       int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
-       if (status2 & HDSPM_wcLock) {
-               if (status2 & HDSPM_wcSync)
+       if (hdspm->is_aes32) {
+               int status = hdspm_read(hdspm, HDSPM_statusRegister);
+               if (status & HDSPM_AES32_wcLock) {
+                       /* I don't know how to differenciate sync from lock.
+                          Doing as if sync for now */
                        return 2;
-               else
-                       return 1;
+               }
+               return 0;
+       } else {
+               int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
+               if (status2 & HDSPM_wcLock) {
+                       if (status2 & HDSPM_wcSync)
+                               return 2;
+                       else
+                               return 1;
+               }
+               return 0;
        }
-       return 0;
 }
 
-static int snd_hdspm_get_wc_sync_check(snd_kcontrol_t * kcontrol,
-                                      snd_ctl_elem_value_t * ucontrol)
+static int snd_hdspm_get_wc_sync_check(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_value *ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] = hdspm_wc_sync_check(hdspm);
        return 0;
@@ -2170,7 +2730,7 @@ static int snd_hdspm_get_wc_sync_check(snd_kcontrol_t * kcontrol,
 
 
 #define HDSPM_MADI_SYNC_CHECK(xname, xindex) \
-{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
   .name = xname, \
   .index = xindex, \
   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
@@ -2178,7 +2738,7 @@ static int snd_hdspm_get_wc_sync_check(snd_kcontrol_t * kcontrol,
   .get = snd_hdspm_get_madisync_sync_check \
 }
 
-static int hdspm_madisync_sync_check(hdspm_t * hdspm)
+static int hdspm_madisync_sync_check(struct hdspm * hdspm)
 {
        int status = hdspm_read(hdspm, HDSPM_statusRegister);
        if (status & HDSPM_madiLock) {
@@ -2190,11 +2750,11 @@ static int hdspm_madisync_sync_check(hdspm_t * hdspm)
        return 0;
 }
 
-static int snd_hdspm_get_madisync_sync_check(snd_kcontrol_t * kcontrol,
-                                            snd_ctl_elem_value_t *
+static int snd_hdspm_get_madisync_sync_check(struct snd_kcontrol *kcontrol,
+                                            struct snd_ctl_elem_value *
                                             ucontrol)
 {
-       hdspm_t *hdspm = snd_kcontrol_chip(kcontrol);
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] =
            hdspm_madisync_sync_check(hdspm);
@@ -2202,9 +2762,43 @@ static int snd_hdspm_get_madisync_sync_check(snd_kcontrol_t * kcontrol,
 }
 
 
+#define HDSPM_AES_SYNC_CHECK(xname, xindex) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .name = xname, \
+  .index = xindex, \
+  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
+  .info = snd_hdspm_info_sync_check, \
+  .get = snd_hdspm_get_aes_sync_check \
+}
+
+static int hdspm_aes_sync_check(struct hdspm * hdspm, int idx)
+{
+       int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
+       if (status2 & (HDSPM_LockAES >> idx)) {
+               /* I don't know how to differenciate sync from lock.
+                  Doing as if sync for now */
+               return 2;
+       }
+       return 0;
+}
+
+static int snd_hdspm_get_aes_sync_check(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
+{
+       int offset;
+       struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
+
+       offset = ucontrol->id.index - 1;
+       if (offset < 0 || offset >= 8)
+               return -EINVAL;
+
+       ucontrol->value.enumerated.item[0] =
+               hdspm_aes_sync_check(hdspm, offset);
+       return 0;
+}
 
 
-static snd_kcontrol_new_t snd_hdspm_controls[] = {
+static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
 
        HDSPM_MIXER("Mixer", 0),
 /* 'Sample Clock Source' complies with the alsa control naming scheme */
@@ -2225,10 +2819,33 @@ static snd_kcontrol_new_t snd_hdspm_controls[] = {
        HDSPM_INPUT_SELECT("Input Select", 0),
 };
 
-static snd_kcontrol_new_t snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
+static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
+
+       HDSPM_MIXER("Mixer", 0),
+/* 'Sample Clock Source' complies with the alsa control naming scheme */
+       HDSPM_CLOCK_SOURCE("Sample Clock Source", 0),
+
+       HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
+       HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
+       HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
+       HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
+/* 'External Rate' complies with the alsa control naming scheme */
+       HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
+       HDSPM_WC_SYNC_CHECK("Word Clock Lock Status", 0),
+/*     HDSPM_AES_SYNC_CHECK("AES Lock Status", 0),*/ /* created in snd_hdspm_create_controls() */
+       HDSPM_LINE_OUT("Line Out", 0),
+       HDSPM_EMPHASIS("Emphasis", 0),
+       HDSPM_DOLBY("Non Audio", 0),
+       HDSPM_PROFESSIONAL("Professional", 0),
+       HDSPM_C_TMS("Clear Track Marker", 0),
+       HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
+       HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
+};
+
+static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
 
 
-static int hdspm_update_simple_mixer_controls(hdspm_t * hdspm)
+static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
 {
        int i;
 
@@ -2252,46 +2869,64 @@ static int hdspm_update_simple_mixer_controls(hdspm_t * hdspm)
 }
 
 
-static int snd_hdspm_create_controls(snd_card_t * card, hdspm_t * hdspm)
+static int snd_hdspm_create_controls(struct snd_card *card, struct hdspm * hdspm)
 {
        unsigned int idx, limit;
        int err;
-       snd_kcontrol_t *kctl;
+       struct snd_kcontrol *kctl;
 
        /* add control list first */
-
-       for (idx = 0; idx < ARRAY_SIZE(snd_hdspm_controls); idx++) {
-               if ((err =
-                    snd_ctl_add(card, kctl =
-                                snd_ctl_new1(&snd_hdspm_controls[idx],
-                                             hdspm))) < 0) {
-                       return err;
+       if (hdspm->is_aes32) {
+               struct snd_kcontrol_new aes_sync_ctl =
+                       HDSPM_AES_SYNC_CHECK("AES Lock Status", 0);
+
+               for (idx = 0; idx < ARRAY_SIZE(snd_hdspm_controls_aes32);
+                    idx++) {
+                       err = snd_ctl_add(card,
+                                         snd_ctl_new1(&snd_hdspm_controls_aes32[idx],
+                                                      hdspm));
+                       if (err < 0)
+                               return err;
+               }
+               for (idx = 1; idx <= 8; idx++) {
+                       aes_sync_ctl.index = idx;
+                       err = snd_ctl_add(card,
+                                         snd_ctl_new1(&aes_sync_ctl, hdspm));
+                       if (err < 0)
+                               return err;
+               }
+       } else {
+               for (idx = 0; idx < ARRAY_SIZE(snd_hdspm_controls_madi);
+                    idx++) {
+                       err = snd_ctl_add(card,
+                                         snd_ctl_new1(&snd_hdspm_controls_madi[idx],
+                                                      hdspm));
+                       if (err < 0)
+                               return err;
                }
        }
 
        /* Channel playback mixer as default control 
-          Note: the whole matrix would be 128*HDSPM_MIXER_CHANNELS Faders, thats to big for any alsamixer 
-          they are accesible via special IOCTL on hwdep
-          and the mixer 2dimensional mixer control */
+          Note: the whole matrix would be 128*HDSPM_MIXER_CHANNELS Faders,
+          thats too * big for any alsamixer they are accesible via special
+          IOCTL on hwdep and the mixer 2dimensional mixer control
+       */
 
        snd_hdspm_playback_mixer.name = "Chn";
        limit = HDSPM_MAX_CHANNELS;
 
-       /* The index values are one greater than the channel ID so that alsamixer
-          will display them correctly. We want to use the index for fast lookup
-          of the relevant channel, but if we use it at all, most ALSA software
-          does the wrong thing with it ...
+       /* The index values are one greater than the channel ID so that
+        * alsamixer will display them correctly. We want to use the index
+        * for fast lookup of the relevant channel, but if we use it at all,
+        * most ALSA software does the wrong thing with it ...
         */
 
        for (idx = 0; idx < limit; ++idx) {
                snd_hdspm_playback_mixer.index = idx + 1;
-               if ((err = snd_ctl_add(card,
-                                      kctl =
-                                      snd_ctl_new1
-                                      (&snd_hdspm_playback_mixer,
-                                       hdspm)))) {
+               kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
+               err = snd_ctl_add(card, kctl);
+               if (err < 0)
                        return err;
-               }
                hdspm->playback_mixer_ctls[idx] = kctl;
        }
 
@@ -2303,9 +2938,10 @@ static int snd_hdspm_create_controls(snd_card_t * card, hdspm_t * hdspm)
  ------------------------------------------------------------*/
 
 static void
-snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
+snd_hdspm_proc_read_madi(struct snd_info_entry * entry,
+                        struct snd_info_buffer *buffer)
 {
-       hdspm_t *hdspm = (hdspm_t *) entry->private_data;
+       struct hdspm *hdspm = entry->private_data;
        unsigned int status;
        unsigned int status2;
        char *pref_sync_ref;
@@ -2338,14 +2974,14 @@ snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
                    (status & HDSPM_midi1IRQPending) ? 1 : 0,
                    hdspm->irq_count);
        snd_iprintf(buffer,
-                   "HW pointer: id = %d, rawptr = %d (%d->%d) estimated= %ld (bytes)\n",
+                   "HW pointer: id = %d, rawptr = %d (%d->%d) "
+                   "estimated= %ld (bytes)\n",
                    ((status & HDSPM_BufferID) ? 1 : 0),
                    (status & HDSPM_BufferPositionMask),
-                   (status & HDSPM_BufferPositionMask) % (2 *
-                                                          (int)hdspm->
-                                                          period_bytes),
-                   ((status & HDSPM_BufferPositionMask) -
-                    64) % (2 * (int)hdspm->period_bytes),
+                   (status & HDSPM_BufferPositionMask) %
+                   (2 * (int)hdspm->period_bytes),
+                   ((status & HDSPM_BufferPositionMask) - 64) %
+                   (2 * (int)hdspm->period_bytes),
                    (long) hdspm_hw_pointer(hdspm) * 4);
 
        snd_iprintf(buffer,
@@ -2355,24 +2991,22 @@ snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
                    hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
                    hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
        snd_iprintf(buffer,
-                   "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, status2=0x%x\n",
+                   "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
+                   "status2=0x%x\n",
                    hdspm->control_register, hdspm->control2_register,
                    status, status2);
 
        snd_iprintf(buffer, "--- Settings ---\n");
 
-       x = 1 << (6 +
-                 hdspm_decode_latency(hdspm->
-                                      control_register &
-                                      HDSPM_LatencyMask));
+       x = 1 << (6 + hdspm_decode_latency(hdspm->control_register &
+                                          HDSPM_LatencyMask));
 
        snd_iprintf(buffer,
                    "Size (Latency): %d samples (2 periods of %lu bytes)\n",
                    x, (unsigned long) hdspm->period_bytes);
 
        snd_iprintf(buffer, "Line out: %s,   Precise Pointer: %s\n",
-                   (hdspm->
-                    control_register & HDSPM_LineOut) ? "on " : "off",
+                   (hdspm->control_register & HDSPM_LineOut) ? "on " : "off",
                    (hdspm->precise_ptr) ? "on" : "off");
 
        switch (hdspm->control_register & HDSPM_InputMask) {
@@ -2383,7 +3017,7 @@ snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
                insel = "Coaxial";
                break;
        default:
-               insel = "Unkown";
+               insel = "Unknown";
        }
 
        switch (hdspm->control_register & HDSPM_SyncRefMask) {
@@ -2394,13 +3028,14 @@ snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
                syncref = "MADI";
                break;
        default:
-               syncref = "Unkown";
+               syncref = "Unknown";
        }
        snd_iprintf(buffer, "Inputsel = %s, SyncRef = %s\n", insel,
                    syncref);
 
        snd_iprintf(buffer,
-                   "ClearTrackMarker = %s, Transmit in %s Channel Mode, Auto Input %s\n",
+                   "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
+                   "Auto Input %s\n",
                    (hdspm->
                     control_register & HDSPM_clr_tms) ? "on" : "off",
                    (hdspm->
@@ -2434,11 +3069,10 @@ snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
                clock_source = "Error";
        }
        snd_iprintf(buffer, "Sample Clock Source: %s\n", clock_source);
-       if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
+       if (!(hdspm->control_register & HDSPM_ClockModeMaster))
                system_clock_mode = "Slave";
-       } else {
+       else
                system_clock_mode = "Master";
-       }
        snd_iprintf(buffer, "System Clock Mode: %s\n", system_clock_mode);
 
        switch (hdspm_pref_sync_ref(hdspm)) {
@@ -2498,36 +3132,248 @@ snd_hdspm_proc_read(snd_info_entry_t * entry, snd_info_buffer_t * buffer)
        snd_iprintf(buffer, "\n");
 }
 
-static void __devinit snd_hdspm_proc_init(hdspm_t * hdspm)
+static void
+snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
+                         struct snd_info_buffer *buffer)
+{
+       struct hdspm *hdspm = entry->private_data;
+       unsigned int status;
+       unsigned int status2;
+       unsigned int timecode;
+       int pref_syncref;
+       char *autosync_ref;
+       char *system_clock_mode;
+       char *clock_source;
+       int x;
+
+       status = hdspm_read(hdspm, HDSPM_statusRegister);
+       status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
+       timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
+
+       snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
+                   hdspm->card_name, hdspm->card->number + 1,
+                   hdspm->firmware_rev);
+
+       snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
+                   hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
+
+       snd_iprintf(buffer, "--- System ---\n");
+
+       snd_iprintf(buffer,
+                   "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
+                   status & HDSPM_audioIRQPending,
+                   (status & HDSPM_midi0IRQPending) ? 1 : 0,
+                   (status & HDSPM_midi1IRQPending) ? 1 : 0,
+                   hdspm->irq_count);
+       snd_iprintf(buffer,
+                   "HW pointer: id = %d, rawptr = %d (%d->%d) "
+                   "estimated= %ld (bytes)\n",
+                   ((status & HDSPM_BufferID) ? 1 : 0),
+                   (status & HDSPM_BufferPositionMask),
+                   (status & HDSPM_BufferPositionMask) %
+                   (2 * (int)hdspm->period_bytes),
+                   ((status & HDSPM_BufferPositionMask) - 64) %
+                   (2 * (int)hdspm->period_bytes),
+                   (long) hdspm_hw_pointer(hdspm) * 4);
+
+       snd_iprintf(buffer,
+                   "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
+                   hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
+                   hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
+                   hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
+                   hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
+       snd_iprintf(buffer,
+                   "Register: ctrl1=0x%x, status1=0x%x, status2=0x%x, "
+                   "timecode=0x%x\n",
+                   hdspm->control_register,
+                   status, status2, timecode);
+
+       snd_iprintf(buffer, "--- Settings ---\n");
+
+       x = 1 << (6 + hdspm_decode_latency(hdspm->control_register &
+                                          HDSPM_LatencyMask));
+
+       snd_iprintf(buffer,
+                   "Size (Latency): %d samples (2 periods of %lu bytes)\n",
+                   x, (unsigned long) hdspm->period_bytes);
+
+       snd_iprintf(buffer, "Line out: %s,   Precise Pointer: %s\n",
+                   (hdspm->
+                    control_register & HDSPM_LineOut) ? "on " : "off",
+                   (hdspm->precise_ptr) ? "on" : "off");
+
+       snd_iprintf(buffer,
+                   "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
+                   (hdspm->
+                    control_register & HDSPM_clr_tms) ? "on" : "off",
+                   (hdspm->
+                    control_register & HDSPM_Emphasis) ? "on" : "off",
+                   (hdspm->
+                    control_register & HDSPM_Dolby) ? "on" : "off");
+
+       switch (hdspm_clock_source(hdspm)) {
+       case HDSPM_CLOCK_SOURCE_AUTOSYNC:
+               clock_source = "AutoSync";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_32KHZ:
+               clock_source = "Internal 32 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_44_1KHZ:
+               clock_source = "Internal 44.1 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_48KHZ:
+               clock_source = "Internal 48 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_64KHZ:
+               clock_source = "Internal 64 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_88_2KHZ:
+               clock_source = "Internal 88.2 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_96KHZ:
+               clock_source = "Internal 96 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_128KHZ:
+               clock_source = "Internal 128 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_176_4KHZ:
+               clock_source = "Internal 176.4 kHz";
+               break;
+       case HDSPM_CLOCK_SOURCE_INTERNAL_192KHZ:
+               clock_source = "Internal 192 kHz";
+               break;
+       default:
+               clock_source = "Error";
+       }
+       snd_iprintf(buffer, "Sample Clock Source: %s\n", clock_source);
+       if (!(hdspm->control_register & HDSPM_ClockModeMaster))
+               system_clock_mode = "Slave";
+       else
+               system_clock_mode = "Master";
+       snd_iprintf(buffer, "System Clock Mode: %s\n", system_clock_mode);
+
+       pref_syncref = hdspm_pref_sync_ref(hdspm);
+       if (pref_syncref == 0)
+               snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
+       else
+               snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
+                               pref_syncref);
+
+       snd_iprintf(buffer, "System Clock Frequency: %d\n",
+                   hdspm->system_sample_rate);
+
+       snd_iprintf(buffer, "Double speed: %s\n",
+                       hdspm->control_register & HDSPM_DS_DoubleWire?
+                       "Double wire" : "Single wire");
+       snd_iprintf(buffer, "Quad speed: %s\n",
+                       hdspm->control_register & HDSPM_QS_DoubleWire?
+                       "Double wire" :
+                       hdspm->control_register & HDSPM_QS_QuadWire?
+                       "Quad wire" : "Single wire");
+
+       snd_iprintf(buffer, "--- Status:\n");
+
+       snd_iprintf(buffer, "Word: %s  Frequency: %d\n",
+                   (status & HDSPM_AES32_wcLock)? "Sync   " : "No Lock",
+                   HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
+
+       for (x = 0; x < 8; x++) {
+               snd_iprintf(buffer, "AES%d: %s  Frequency: %d\n",
+                           x+1,
+                           (status2 & (HDSPM_LockAES >> x)) ?
+                           "Sync   ": "No Lock",
+                           HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
+       }
+
+       switch (hdspm_autosync_ref(hdspm)) {
+       case HDSPM_AES32_AUTOSYNC_FROM_NONE: autosync_ref="None"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_WORD: autosync_ref="Word Clock"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES1: autosync_ref="AES1"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES2: autosync_ref="AES2"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES3: autosync_ref="AES3"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES4: autosync_ref="AES4"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES5: autosync_ref="AES5"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES6: autosync_ref="AES6"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES7: autosync_ref="AES7"; break;
+       case HDSPM_AES32_AUTOSYNC_FROM_AES8: autosync_ref="AES8"; break;
+       default: autosync_ref = "---"; break;
+       }
+       snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
+
+       snd_iprintf(buffer, "\n");
+}
+
+#ifdef CONFIG_SND_DEBUG
+static void
+snd_hdspm_proc_read_debug(struct snd_info_entry * entry,
+                         struct snd_info_buffer *buffer)
+{
+       struct hdspm *hdspm = entry->private_data;
+
+       int j,i;
+
+       for (i = 0; i < 256 /* 1024*64 */; i += j) {
+               snd_iprintf(buffer, "0x%08X: ", i);
+               for (j = 0; j < 16; j += 4)
+                       snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
+               snd_iprintf(buffer, "\n");
+       }
+}
+#endif
+
+
+
+static void __devinit snd_hdspm_proc_init(struct hdspm * hdspm)
 {
-       snd_info_entry_t *entry;
+       struct snd_info_entry *entry;
 
        if (!snd_card_proc_new(hdspm->card, "hdspm", &entry))
-               snd_info_set_text_ops(entry, hdspm, 1024,
-                                     snd_hdspm_proc_read);
+               snd_info_set_text_ops(entry, hdspm,
+                                     hdspm->is_aes32 ?
+                                     snd_hdspm_proc_read_aes32 :
+                                     snd_hdspm_proc_read_madi);
+#ifdef CONFIG_SND_DEBUG
+       /* debug file to read all hdspm registers */
+       if (!snd_card_proc_new(hdspm->card, "debug", &entry))
+               snd_info_set_text_ops(entry, hdspm,
+                               snd_hdspm_proc_read_debug);
+#endif
 }
 
 /*------------------------------------------------------------
    hdspm intitialize 
  ------------------------------------------------------------*/
 
-static int snd_hdspm_set_defaults(hdspm_t * hdspm)
+static int snd_hdspm_set_defaults(struct hdspm * hdspm)
 {
        unsigned int i;
 
        /* ASSUMPTION: hdspm->lock is either held, or there is no need to
-          hold it (e.g. during module initalization).
+          hold it (e.g. during module initialization).
         */
 
        /* set defaults:       */
 
-       hdspm->control_register = HDSPM_ClockModeMaster |       /* Master Cloack Mode on */
-           hdspm_encode_latency(7) |   /* latency maximum = 8192 samples */
-           HDSPM_InputCoaxial |        /* Input Coax not Optical */
-           HDSPM_SyncRef_MADI |        /* Madi is syncclock */
-           HDSPM_LineOut |     /* Analog output in */
-           HDSPM_TX_64ch |     /* transmit in 64ch mode */
-           HDSPM_AutoInp;      /* AutoInput chossing (takeover) */
+       if (hdspm->is_aes32)
+               hdspm->control_register =
+                       HDSPM_ClockModeMaster | /* Master Cloack Mode on */
+                       hdspm_encode_latency(7) | /* latency maximum =
+                                                  * 8192 samples
+                                                  */
+                       HDSPM_SyncRef0 |        /* AES1 is syncclock */
+                       HDSPM_LineOut | /* Analog output in */
+                       HDSPM_Professional;  /* Professional mode */
+       else
+               hdspm->control_register =
+                       HDSPM_ClockModeMaster | /* Master Cloack Mode on */
+                       hdspm_encode_latency(7) | /* latency maximum =
+                                                  * 8192 samples
+                                                  */
+                       HDSPM_InputCoaxial |    /* Input Coax not Optical */
+                       HDSPM_SyncRef_MADI |    /* Madi is syncclock */
+                       HDSPM_LineOut | /* Analog output in */
+                       HDSPM_TX_64ch | /* transmit in 64ch mode */
+                       HDSPM_AutoInp;  /* AutoInput chossing (takeover) */
 
        /* ! HDSPM_Frequency0|HDSPM_Frequency1 = 44.1khz */
        /* !  HDSPM_DoubleSpeed HDSPM_QuadSpeed = normal speed */
@@ -2535,13 +3381,16 @@ static int snd_hdspm_set_defaults(hdspm_t * hdspm)
 
        hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
 
+        if (!hdspm->is_aes32) {
+               /* No control2 register for AES32 */
 #ifdef SNDRV_BIG_ENDIAN
-       hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
+               hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
 #else
-       hdspm->control2_register = 0;
+               hdspm->control2_register = 0;
 #endif
 
-       hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
+               hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
+       }
        hdspm_compute_period_size(hdspm);
 
        /* silence everything */
@@ -2550,7 +3399,8 @@ static int snd_hdspm_set_defaults(hdspm_t * hdspm)
 
        if (line_outs_monitor[hdspm->dev]) {
 
-               snd_printk(KERN_INFO "HDSPM: sending all playback streams to line outs.\n");
+               snd_printk(KERN_INFO "HDSPM: "
+                          "sending all playback streams to line outs.\n");
 
                for (i = 0; i < HDSPM_MIXER_CHANNELS; i++) {
                        if (hdspm_write_pb_gain(hdspm, i, i, UNITY_GAIN))
@@ -2567,13 +3417,12 @@ static int snd_hdspm_set_defaults(hdspm_t * hdspm)
 
 
 /*------------------------------------------------------------
-   interupt 
+   interrupt 
  ------------------------------------------------------------*/
 
-static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id,
-                                      struct pt_regs *regs)
+static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
 {
-       hdspm_t *hdspm = (hdspm_t *) dev_id;
+       struct hdspm *hdspm = (struct hdspm *) dev_id;
        unsigned int status;
        int audio;
        int midi0;
@@ -2600,20 +3449,16 @@ static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id,
        if (audio) {
 
                if (hdspm->capture_substream)
-                       snd_pcm_period_elapsed(hdspm->pcm->
-                                              streams
-                                              [SNDRV_PCM_STREAM_CAPTURE].
-                                              substream);
+                       snd_pcm_period_elapsed(hdspm->capture_substream);
 
                if (hdspm->playback_substream)
-                       snd_pcm_period_elapsed(hdspm->pcm->
-                                              streams
-                                              [SNDRV_PCM_STREAM_PLAYBACK].
-                                              substream);
+                       snd_pcm_period_elapsed(hdspm->playback_substream);
        }
 
        if (midi0 && midi0status) {
-               /* we disable interrupts for this input until processing is done */
+               /* we disable interrupts for this input until processing
+                * is done
+                */
                hdspm->control_register &= ~HDSPM_Midi0InterruptEnable;
                hdspm_write(hdspm, HDSPM_controlRegister,
                            hdspm->control_register);
@@ -2621,7 +3466,9 @@ static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id,
                schedule = 1;
        }
        if (midi1 && midi1status) {
-               /* we disable interrupts for this input until processing is done */
+               /* we disable interrupts for this input until processing
+                * is done
+                */
                hdspm->control_register &= ~HDSPM_Midi1InterruptEnable;
                hdspm_write(hdspm, HDSPM_controlRegister,
                            hdspm->control_register);
@@ -2629,7 +3476,7 @@ static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id,
                schedule = 1;
        }
        if (schedule)
-               tasklet_hi_schedule(&hdspm->midi_tasklet);
+               tasklet_schedule(&hdspm->midi_tasklet);
        return IRQ_HANDLED;
 }
 
@@ -2638,91 +3485,94 @@ static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id,
   ------------------------------------------------------------*/
 
 
-static snd_pcm_uframes_t snd_hdspm_hw_pointer(snd_pcm_substream_t *
+static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream *
                                              substream)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
        return hdspm_hw_pointer(hdspm);
 }
 
-static char *hdspm_channel_buffer_location(hdspm_t * hdspm,
+static char *hdspm_channel_buffer_location(struct hdspm * hdspm,
                                           int stream, int channel)
 {
        int mapped_channel;
 
-       snd_assert(channel >= 0
-                  || channel < HDSPM_MAX_CHANNELS, return NULL);
+       if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
+               return NULL;
 
-       if ((mapped_channel = hdspm->channel_map[channel]) < 0)
+       mapped_channel = hdspm->channel_map[channel];
+       if (mapped_channel < 0)
                return NULL;
 
-       if (stream == SNDRV_PCM_STREAM_CAPTURE) {
+       if (stream == SNDRV_PCM_STREAM_CAPTURE)
                return hdspm->capture_buffer +
                    mapped_channel * HDSPM_CHANNEL_BUFFER_BYTES;
-       } else {
+       else
                return hdspm->playback_buffer +
                    mapped_channel * HDSPM_CHANNEL_BUFFER_BYTES;
-       }
 }
 
 
 /* dont know why need it ??? */
-static int snd_hdspm_playback_copy(snd_pcm_substream_t * substream,
+static int snd_hdspm_playback_copy(struct snd_pcm_substream *substream,
                                   int channel, snd_pcm_uframes_t pos,
                                   void __user *src, snd_pcm_uframes_t count)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
        char *channel_buf;
 
-       snd_assert(pos + count <= HDSPM_CHANNEL_BUFFER_BYTES / 4,
-                  return -EINVAL);
+       if (snd_BUG_ON(pos + count > HDSPM_CHANNEL_BUFFER_BYTES / 4))
+               return -EINVAL;
 
-       channel_buf = hdspm_channel_buffer_location(hdspm,
-                                                   substream->pstr->
-                                                   stream, channel);
+       channel_buf =
+               hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
+                                             channel);
 
-       snd_assert(channel_buf != NULL, return -EIO);
+       if (snd_BUG_ON(!channel_buf))
+               return -EIO;
 
        return copy_from_user(channel_buf + pos * 4, src, count * 4);
 }
 
-static int snd_hdspm_capture_copy(snd_pcm_substream_t * substream,
+static int snd_hdspm_capture_copy(struct snd_pcm_substream *substream,
                                  int channel, snd_pcm_uframes_t pos,
                                  void __user *dst, snd_pcm_uframes_t count)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
        char *channel_buf;
 
-       snd_assert(pos + count <= HDSPM_CHANNEL_BUFFER_BYTES / 4,
-                  return -EINVAL);
+       if (snd_BUG_ON(pos + count > HDSPM_CHANNEL_BUFFER_BYTES / 4))
+               return -EINVAL;
 
-       channel_buf = hdspm_channel_buffer_location(hdspm,
-                                                   substream->pstr->
-                                                   stream, channel);
-       snd_assert(channel_buf != NULL, return -EIO);
+       channel_buf =
+               hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
+                                             channel);
+       if (snd_BUG_ON(!channel_buf))
+               return -EIO;
        return copy_to_user(dst, channel_buf + pos * 4, count * 4);
 }
 
-static int snd_hdspm_hw_silence(snd_pcm_substream_t * substream,
+static int snd_hdspm_hw_silence(struct snd_pcm_substream *substream,
                                int channel, snd_pcm_uframes_t pos,
                                snd_pcm_uframes_t count)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
        char *channel_buf;
 
        channel_buf =
-           hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
-                                         channel);
-       snd_assert(channel_buf != NULL, return -EIO);
+               hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
+                                             channel);
+       if (snd_BUG_ON(!channel_buf))
+               return -EIO;
        memset(channel_buf + pos * 4, 0, count * 4);
        return 0;
 }
 
-static int snd_hdspm_reset(snd_pcm_substream_t * substream)
+static int snd_hdspm_reset(struct snd_pcm_substream *substream)
 {
-       snd_pcm_runtime_t *runtime = substream->runtime;
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
-       snd_pcm_substream_t *other;
+       struct snd_pcm_runtime *runtime = substream->runtime;
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
+       struct snd_pcm_substream *other;
 
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
                other = hdspm->capture_substream;
@@ -2734,11 +3584,9 @@ static int snd_hdspm_reset(snd_pcm_substream_t * substream)
        else
                runtime->status->hw_ptr = 0;
        if (other) {
-               struct list_head *pos;
-               snd_pcm_substream_t *s;
-               snd_pcm_runtime_t *oruntime = other->runtime;
-               snd_pcm_group_for_each(pos, substream) {
-                       s = snd_pcm_group_substream_entry(pos);
+               struct snd_pcm_substream *s;
+               struct snd_pcm_runtime *oruntime = other->runtime;
+               snd_pcm_group_for_each_entry(s, substream) {
                        if (s == other) {
                                oruntime->status->hw_ptr =
                                    runtime->status->hw_ptr;
@@ -2749,16 +3597,14 @@ static int snd_hdspm_reset(snd_pcm_substream_t * substream)
        return 0;
 }
 
-static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
-                              snd_pcm_hw_params_t * params)
+static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
+                              struct snd_pcm_hw_params *params)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
        int err;
        int i;
        pid_t this_pid;
        pid_t other_pid;
-       struct snd_sg_buf *sgbuf;
-
 
        spin_lock_irq(&hdspm->lock);
 
@@ -2770,7 +3616,7 @@ static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
                other_pid = hdspm->playback_pid;
        }
 
-       if ((other_pid > 0) && (this_pid != other_pid)) {
+       if (other_pid > 0 && this_pid != other_pid) {
 
                /* The other stream is open, and not by the same
                   task as this one. Make sure that the parameters
@@ -2787,7 +3633,7 @@ static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
                if (params_period_size(params) != hdspm->period_bytes / 4) {
                        spin_unlock_irq(&hdspm->lock);
                        _snd_pcm_hw_param_setempty(params,
-                                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
+                                          SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
                        return -EBUSY;
                }
 
@@ -2798,7 +3644,8 @@ static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
        /* how to make sure that the rate matches an externally-set one ?   */
 
        spin_lock_irq(&hdspm->lock);
-       if ((err = hdspm_set_rate(hdspm, params_rate(params), 0)) < 0) {
+       err = hdspm_set_rate(hdspm, params_rate(params), 0);
+       if (err < 0) {
                spin_unlock_irq(&hdspm->lock);
                _snd_pcm_hw_param_setempty(params,
                                           SNDRV_PCM_HW_PARAM_RATE);
@@ -2806,30 +3653,28 @@ static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
        }
        spin_unlock_irq(&hdspm->lock);
 
-       if ((err =
-            hdspm_set_interrupt_interval(hdspm,
-                                         params_period_size(params))) <
-           0) {
+       err = hdspm_set_interrupt_interval(hdspm,
+                                          params_period_size(params));
+       if (err < 0) {
                _snd_pcm_hw_param_setempty(params,
                                           SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
                return err;
        }
 
-       /* Memory allocation, takashi's method, dont know if we should spinlock  */
+       /* Memory allocation, takashi's method, dont know if we should
+        * spinlock
+        */
        /* malloc all buffer even if not enabled to get sure */
-       /* malloc only needed bytes */
+       /* Update for MADI rev 204: we need to allocate for all channels,
+        * otherwise it doesn't work at 96kHz */
        err =
-           snd_pcm_lib_malloc_pages(substream,
-                                    HDSPM_CHANNEL_BUFFER_BYTES *
-                                    params_channels(params));
+           snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
        if (err < 0)
                return err;
 
-       sgbuf = snd_pcm_substream_sgbuf(substream);
-
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 
-               hdspm_set_sgbuf(hdspm, sgbuf, HDSPM_pageAddressBufferOut,
+               hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
                                params_channels(params));
 
                for (i = 0; i < params_channels(params); ++i)
@@ -2837,8 +3682,10 @@ static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
 
                hdspm->playback_buffer =
                    (unsigned char *) substream->runtime->dma_area;
+               snd_printdd("Allocated sample buffer for playback at %p\n",
+                               hdspm->playback_buffer);
        } else {
-               hdspm_set_sgbuf(hdspm, sgbuf, HDSPM_pageAddressBufferIn,
+               hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
                                params_channels(params));
 
                for (i = 0; i < params_channels(params); ++i)
@@ -2846,14 +3693,29 @@ static int snd_hdspm_hw_params(snd_pcm_substream_t * substream,
 
                hdspm->capture_buffer =
                    (unsigned char *) substream->runtime->dma_area;
+               snd_printdd("Allocated sample buffer for capture at %p\n",
+                               hdspm->capture_buffer);
        }
+       /*
+          snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
+          substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
+          "playback" : "capture",
+          snd_pcm_sgbuf_get_addr(substream, 0));
+        */
+       /*
+       snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
+                       substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
+                         "playback" : "capture",
+                       params_rate(params), params_channels(params),
+                       params_buffer_size(params));
+       */
        return 0;
 }
 
-static int snd_hdspm_hw_free(snd_pcm_substream_t * substream)
+static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
 {
        int i;
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
 
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 
@@ -2876,15 +3738,17 @@ static int snd_hdspm_hw_free(snd_pcm_substream_t * substream)
        return 0;
 }
 
-static int snd_hdspm_channel_info(snd_pcm_substream_t * substream,
-                                 snd_pcm_channel_info_t * info)
+static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
+                                 struct snd_pcm_channel_info * info)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
        int mapped_channel;
 
-       snd_assert(info->channel < HDSPM_MAX_CHANNELS, return -EINVAL);
+       if (snd_BUG_ON(info->channel >= HDSPM_MAX_CHANNELS))
+               return -EINVAL;
 
-       if ((mapped_channel = hdspm->channel_map[info->channel]) < 0)
+       mapped_channel = hdspm->channel_map[info->channel];
+       if (mapped_channel < 0)
                return -EINVAL;
 
        info->offset = mapped_channel * HDSPM_CHANNEL_BUFFER_BYTES;
@@ -2893,20 +3757,18 @@ static int snd_hdspm_channel_info(snd_pcm_substream_t * substream,
        return 0;
 }
 
-static int snd_hdspm_ioctl(snd_pcm_substream_t * substream,
+static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
                           unsigned int cmd, void *arg)
 {
        switch (cmd) {
        case SNDRV_PCM_IOCTL1_RESET:
-               {
-                       return snd_hdspm_reset(substream);
-               }
+               return snd_hdspm_reset(substream);
 
        case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
-               {
-                       snd_pcm_channel_info_t *info = arg;
-                       return snd_hdspm_channel_info(substream, info);
-               }
+       {
+               struct snd_pcm_channel_info *info = arg;
+               return snd_hdspm_channel_info(substream, info);
+       }
        default:
                break;
        }
@@ -2914,10 +3776,10 @@ static int snd_hdspm_ioctl(snd_pcm_substream_t * substream,
        return snd_pcm_lib_ioctl(substream, cmd, arg);
 }
 
-static int snd_hdspm_trigger(snd_pcm_substream_t * substream, int cmd)
+static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
-       snd_pcm_substream_t *other;
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
+       struct snd_pcm_substream *other;
        int running;
 
        spin_lock(&hdspm->lock);
@@ -2940,10 +3802,8 @@ static int snd_hdspm_trigger(snd_pcm_substream_t * substream, int cmd)
                other = hdspm->playback_substream;
 
        if (other) {
-               struct list_head *pos;
-               snd_pcm_substream_t *s;
-               snd_pcm_group_for_each(pos, substream) {
-                       s = snd_pcm_group_substream_entry(pos);
+               struct snd_pcm_substream *s;
+               snd_pcm_group_for_each_entry(s, substream) {
                        if (s == other) {
                                snd_pcm_trigger_done(s, substream);
                                if (cmd == SNDRV_PCM_TRIGGER_START)
@@ -2979,7 +3839,7 @@ static int snd_hdspm_trigger(snd_pcm_substream_t * substream, int cmd)
        return 0;
 }
 
-static int snd_hdspm_prepare(snd_pcm_substream_t * substream)
+static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
 {
        return 0;
 }
@@ -2987,7 +3847,7 @@ static int snd_hdspm_prepare(snd_pcm_substream_t * substream)
 static unsigned int period_sizes[] =
     { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
 
-static snd_pcm_hardware_t snd_hdspm_playback_subinfo = {
+static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
        .info = (SNDRV_PCM_INFO_MMAP |
                 SNDRV_PCM_INFO_MMAP_VALID |
                 SNDRV_PCM_INFO_NONINTERLEAVED |
@@ -2997,9 +3857,10 @@ static snd_pcm_hardware_t snd_hdspm_playback_subinfo = {
                  SNDRV_PCM_RATE_44100 |
                  SNDRV_PCM_RATE_48000 |
                  SNDRV_PCM_RATE_64000 |
-                 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000),
+                 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
+                 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
        .rate_min = 32000,
-       .rate_max = 96000,
+       .rate_max = 192000,
        .channels_min = 1,
        .channels_max = HDSPM_MAX_CHANNELS,
        .buffer_bytes_max =
@@ -3011,7 +3872,7 @@ static snd_pcm_hardware_t snd_hdspm_playback_subinfo = {
        .fifo_size = 0
 };
 
-static snd_pcm_hardware_t snd_hdspm_capture_subinfo = {
+static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
        .info = (SNDRV_PCM_INFO_MMAP |
                 SNDRV_PCM_INFO_MMAP_VALID |
                 SNDRV_PCM_INFO_NONINTERLEAVED |
@@ -3021,9 +3882,10 @@ static snd_pcm_hardware_t snd_hdspm_capture_subinfo = {
                  SNDRV_PCM_RATE_44100 |
                  SNDRV_PCM_RATE_48000 |
                  SNDRV_PCM_RATE_64000 |
-                 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000),
+                 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
+                 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
        .rate_min = 32000,
-       .rate_max = 96000,
+       .rate_max = 192000,
        .channels_min = 1,
        .channels_max = HDSPM_MAX_CHANNELS,
        .buffer_bytes_max =
@@ -3035,32 +3897,32 @@ static snd_pcm_hardware_t snd_hdspm_capture_subinfo = {
        .fifo_size = 0
 };
 
-static snd_pcm_hw_constraint_list_t hw_constraints_period_sizes = {
+static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
        .count = ARRAY_SIZE(period_sizes),
        .list = period_sizes,
        .mask = 0
 };
 
 
-static int snd_hdspm_hw_rule_channels_rate(snd_pcm_hw_params_t * params,
-                                          snd_pcm_hw_rule_t * rule)
+static int snd_hdspm_hw_rule_channels_rate(struct snd_pcm_hw_params *params,
+                                          struct snd_pcm_hw_rule * rule)
 {
-       hdspm_t *hdspm = rule->private;
-       snd_interval_t *c =
+       struct hdspm *hdspm = rule->private;
+       struct snd_interval *c =
            hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
-       snd_interval_t *r =
+       struct snd_interval *r =
            hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 
-       if (r->min > 48000) {
-               snd_interval_t t = {
-                       .min = 1,
+       if (r->min > 48000 && r->max <= 96000) {
+               struct snd_interval t = {
+                       .min = hdspm->ds_channels,
                        .max = hdspm->ds_channels,
                        .integer = 1,
                };
                return snd_interval_refine(c, &t);
        } else if (r->max < 64000) {
-               snd_interval_t t = {
-                       .min = 1,
+               struct snd_interval t = {
+                       .min = hdspm->ss_channels,
                        .max = hdspm->ss_channels,
                        .integer = 1,
                };
@@ -3069,24 +3931,24 @@ static int snd_hdspm_hw_rule_channels_rate(snd_pcm_hw_params_t * params,
        return 0;
 }
 
-static int snd_hdspm_hw_rule_rate_channels(snd_pcm_hw_params_t * params,
-                                          snd_pcm_hw_rule_t * rule)
+static int snd_hdspm_hw_rule_rate_channels(struct snd_pcm_hw_params *params,
+                                          struct snd_pcm_hw_rule * rule)
 {
-       hdspm_t *hdspm = rule->private;
-       snd_interval_t *c =
+       struct hdspm *hdspm = rule->private;
+       struct snd_interval *c =
            hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
-       snd_interval_t *r =
+       struct snd_interval *r =
            hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 
-       if (c->min <= hdspm->ss_channels) {
-               snd_interval_t t = {
+       if (c->min >= hdspm->ss_channels) {
+               struct snd_interval t = {
                        .min = 32000,
                        .max = 48000,
                        .integer = 1,
                };
                return snd_interval_refine(r, &t);
-       } else if (c->max > hdspm->ss_channels) {
-               snd_interval_t t = {
+       } else if (c->max <= hdspm->ds_channels) {
+               struct snd_interval t = {
                        .min = 64000,
                        .max = 96000,
                        .integer = 1,
@@ -3097,12 +3959,41 @@ static int snd_hdspm_hw_rule_rate_channels(snd_pcm_hw_params_t * params,
        return 0;
 }
 
-static int snd_hdspm_playback_open(snd_pcm_substream_t * substream)
+static int snd_hdspm_hw_rule_channels(struct snd_pcm_hw_params *params,
+                                     struct snd_pcm_hw_rule *rule)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       unsigned int list[3];
+       struct hdspm *hdspm = rule->private;
+       struct snd_interval *c = hw_param_interval(params,
+                       SNDRV_PCM_HW_PARAM_CHANNELS);
+       if (hdspm->is_aes32) {
+               list[0] = hdspm->qs_channels;
+               list[1] = hdspm->ds_channels;
+               list[2] = hdspm->ss_channels;
+               return snd_interval_list(c, 3, list, 0);
+       } else {
+               list[0] = hdspm->ds_channels;
+               list[1] = hdspm->ss_channels;
+               return snd_interval_list(c, 2, list, 0);
+       }
+}
+
+
+static unsigned int hdspm_aes32_sample_rates[] = {
+       32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
+};
+
+static struct snd_pcm_hw_constraint_list
+hdspm_hw_constraints_aes32_sample_rates = {
+       .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
+       .list = hdspm_aes32_sample_rates,
+       .mask = 0
+};
 
-       snd_printdd("Open device substream %d\n", substream->stream);
+static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
+{
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
 
        spin_lock_irq(&hdspm->lock);
 
@@ -3124,20 +4015,27 @@ static int snd_hdspm_playback_open(snd_pcm_substream_t * substream)
                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
                                   &hw_constraints_period_sizes);
 
-       snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
-                           snd_hdspm_hw_rule_channels_rate, hdspm,
-                           SNDRV_PCM_HW_PARAM_RATE, -1);
-
-       snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
-                           snd_hdspm_hw_rule_rate_channels, hdspm,
-                           SNDRV_PCM_HW_PARAM_CHANNELS, -1);
-
+       if (hdspm->is_aes32) {
+               snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+                               &hdspm_hw_constraints_aes32_sample_rates);
+       } else {
+               snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
+                                    snd_hdspm_hw_rule_channels, hdspm,
+                                    SNDRV_PCM_HW_PARAM_CHANNELS, -1);
+               snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
+                                   snd_hdspm_hw_rule_channels_rate, hdspm,
+                                   SNDRV_PCM_HW_PARAM_RATE, -1);
+
+               snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+                                   snd_hdspm_hw_rule_rate_channels, hdspm,
+                                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
+       }
        return 0;
 }
 
-static int snd_hdspm_playback_release(snd_pcm_substream_t * substream)
+static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
 
        spin_lock_irq(&hdspm->lock);
 
@@ -3150,10 +4048,10 @@ static int snd_hdspm_playback_release(snd_pcm_substream_t * substream)
 }
 
 
-static int snd_hdspm_capture_open(snd_pcm_substream_t * substream)
+static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
 
        spin_lock_irq(&hdspm->lock);
        snd_pcm_set_sync(substream);
@@ -3171,20 +4069,27 @@ static int snd_hdspm_capture_open(snd_pcm_substream_t * substream)
        snd_pcm_hw_constraint_list(runtime, 0,
                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
                                   &hw_constraints_period_sizes);
-
-       snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
-                           snd_hdspm_hw_rule_channels_rate, hdspm,
-                           SNDRV_PCM_HW_PARAM_RATE, -1);
-
-       snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
-                           snd_hdspm_hw_rule_rate_channels, hdspm,
-                           SNDRV_PCM_HW_PARAM_CHANNELS, -1);
+       if (hdspm->is_aes32) {
+               snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+                               &hdspm_hw_constraints_aes32_sample_rates);
+       } else {
+               snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
+                                    snd_hdspm_hw_rule_channels, hdspm,
+                                    SNDRV_PCM_HW_PARAM_CHANNELS, -1);
+               snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
+                                   snd_hdspm_hw_rule_channels_rate, hdspm,
+                                   SNDRV_PCM_HW_PARAM_RATE, -1);
+
+               snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+                                   snd_hdspm_hw_rule_rate_channels, hdspm,
+                                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
+       }
        return 0;
 }
 
-static int snd_hdspm_capture_release(snd_pcm_substream_t * substream)
+static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
 {
-       hdspm_t *hdspm = snd_pcm_substream_chip(substream);
+       struct hdspm *hdspm = snd_pcm_substream_chip(substream);
 
        spin_lock_irq(&hdspm->lock);
 
@@ -3195,32 +4100,26 @@ static int snd_hdspm_capture_release(snd_pcm_substream_t * substream)
        return 0;
 }
 
-static int snd_hdspm_hwdep_dummy_op(snd_hwdep_t * hw, struct file *file)
-{
-       /* we have nothing to initialize but the call is required */
-       return 0;
-}
-
-
-static int snd_hdspm_hwdep_ioctl(snd_hwdep_t * hw, struct file *file,
+static int snd_hdspm_hwdep_ioctl(struct snd_hwdep * hw, struct file *file,
                                 unsigned int cmd, unsigned long arg)
 {
-       hdspm_t *hdspm = (hdspm_t *) hw->private_data;
-       struct sndrv_hdspm_mixer_ioctl mixer;
-       hdspm_config_info_t info;
-       hdspm_version_t hdspm_version;
-       struct sndrv_hdspm_peak_rms_ioctl rms;
+       struct hdspm *hdspm = hw->private_data;
+       struct hdspm_mixer_ioctl mixer;
+       struct hdspm_config_info info;
+       struct hdspm_version hdspm_version;
+       struct hdspm_peak_rms_ioctl rms;
 
        switch (cmd) {
 
-               
        case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
                if (copy_from_user(&rms, (void __user *)arg, sizeof(rms)))
                        return -EFAULT;
-               /* maybe there is a chance to memorymap in future so dont touch just copy */
+               /* maybe there is a chance to memorymap in future
+                * so dont touch just copy
+                */
                if(copy_to_user_fromio((void __user *)rms.peak,
                                       hdspm->iobase+HDSPM_MADI_peakrmsbase,
-                                      sizeof(hdspm_peak_rms_t)) != 0 )
+                                      sizeof(struct hdspm_peak_rms)) != 0 )
                        return -EFAULT;
 
                break;
@@ -3229,21 +4128,16 @@ static int snd_hdspm_hwdep_ioctl(snd_hwdep_t * hw, struct file *file,
        case SNDRV_HDSPM_IOCTL_GET_CONFIG_INFO:
 
                spin_lock_irq(&hdspm->lock);
-               info.pref_sync_ref =
-                   (unsigned char) hdspm_pref_sync_ref(hdspm);
-               info.wordclock_sync_check =
-                   (unsigned char) hdspm_wc_sync_check(hdspm);
+               info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
+               info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
 
                info.system_sample_rate = hdspm->system_sample_rate;
                info.autosync_sample_rate =
                    hdspm_external_sample_rate(hdspm);
-               info.system_clock_mode =
-                   (unsigned char) hdspm_system_clock_mode(hdspm);
-               info.clock_source =
-                   (unsigned char) hdspm_clock_source(hdspm);
-               info.autosync_ref =
-                   (unsigned char) hdspm_autosync_ref(hdspm);
-               info.line_out = (unsigned char) hdspm_line_out(hdspm);
+               info.system_clock_mode = hdspm_system_clock_mode(hdspm);
+               info.clock_source = hdspm_clock_source(hdspm);
+               info.autosync_ref = hdspm_autosync_ref(hdspm);
+               info.line_out = hdspm_line_out(hdspm);
                info.passthru = 0;
                spin_unlock_irq(&hdspm->lock);
                if (copy_to_user((void __user *) arg, &info, sizeof(info)))
@@ -3260,8 +4154,8 @@ static int snd_hdspm_hwdep_ioctl(snd_hwdep_t * hw, struct file *file,
        case SNDRV_HDSPM_IOCTL_GET_MIXER:
                if (copy_from_user(&mixer, (void __user *)arg, sizeof(mixer)))
                        return -EFAULT;
-               if (copy_to_user
-                   ((void __user *)mixer.mixer, hdspm->mixer, sizeof(hdspm_mixer_t)))
+               if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
+                                sizeof(struct hdspm_mixer)))
                        return -EFAULT;
                break;
 
@@ -3271,7 +4165,7 @@ static int snd_hdspm_hwdep_ioctl(snd_hwdep_t * hw, struct file *file,
        return 0;
 }
 
-static snd_pcm_ops_t snd_hdspm_playback_ops = {
+static struct snd_pcm_ops snd_hdspm_playback_ops = {
        .open = snd_hdspm_playback_open,
        .close = snd_hdspm_playback_release,
        .ioctl = snd_hdspm_ioctl,
@@ -3285,7 +4179,7 @@ static snd_pcm_ops_t snd_hdspm_playback_ops = {
        .page = snd_pcm_sgbuf_ops_page,
 };
 
-static snd_pcm_ops_t snd_hdspm_capture_ops = {
+static struct snd_pcm_ops snd_hdspm_capture_ops = {
        .open = snd_hdspm_capture_open,
        .close = snd_hdspm_capture_release,
        .ioctl = snd_hdspm_ioctl,
@@ -3298,22 +4192,21 @@ static snd_pcm_ops_t snd_hdspm_capture_ops = {
        .page = snd_pcm_sgbuf_ops_page,
 };
 
-static int __devinit snd_hdspm_create_hwdep(snd_card_t * card,
-                                           hdspm_t * hdspm)
+static int __devinit snd_hdspm_create_hwdep(struct snd_card *card,
+                                           struct hdspm * hdspm)
 {
-       snd_hwdep_t *hw;
+       struct snd_hwdep *hw;
        int err;
 
-       if ((err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw)) < 0)
+       err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
+       if (err < 0)
                return err;
 
        hdspm->hwdep = hw;
        hw->private_data = hdspm;
        strcpy(hw->name, "HDSPM hwdep interface");
 
-       hw->ops.open = snd_hdspm_hwdep_dummy_op;
        hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
-       hw->ops.release = snd_hdspm_hwdep_dummy_op;
 
        return 0;
 }
@@ -3322,58 +4215,51 @@ static int __devinit snd_hdspm_create_hwdep(snd_card_t * card,
 /*------------------------------------------------------------
    memory interface 
  ------------------------------------------------------------*/
-static int __devinit snd_hdspm_preallocate_memory(hdspm_t * hdspm)
+static int __devinit snd_hdspm_preallocate_memory(struct hdspm * hdspm)
 {
        int err;
-       snd_pcm_t *pcm;
+       struct snd_pcm *pcm;
        size_t wanted;
 
        pcm = hdspm->pcm;
 
-       wanted = HDSPM_DMA_AREA_BYTES + 4096;   /* dont know why, but it works */
+       wanted = HDSPM_DMA_AREA_BYTES;
 
-       if ((err =
+       err =
             snd_pcm_lib_preallocate_pages_for_all(pcm,
                                                   SNDRV_DMA_TYPE_DEV_SG,
                                                   snd_dma_pci_data(hdspm->pci),
                                                   wanted,
-                                                  wanted)) < 0) {
-               snd_printdd("Could not preallocate %d  Bytes\n", wanted);
+                                                  wanted);
+       if (err < 0) {
+               snd_printdd("Could not preallocate %zd Bytes\n", wanted);
 
                return err;
        } else
-               snd_printdd(" Preallocated %d  Bytes\n", wanted);
-
-       return 0;
-}
-
-static int snd_hdspm_memory_free(hdspm_t * hdspm)
-{
-       snd_printdd("memory_free_for_all %p\n", hdspm->pcm);
+               snd_printdd(" Preallocated %zd Bytes\n", wanted);
 
-       snd_pcm_lib_preallocate_free_for_all(hdspm->pcm);
        return 0;
 }
 
-
-static void hdspm_set_sgbuf(hdspm_t * hdspm, struct snd_sg_buf *sgbuf,
+static void hdspm_set_sgbuf(struct hdspm * hdspm,
+                           struct snd_pcm_substream *substream,
                             unsigned int reg, int channels)
 {
        int i;
        for (i = 0; i < (channels * 16); i++)
                hdspm_write(hdspm, reg + 4 * i,
-                           snd_pcm_sgbuf_get_addr(sgbuf,
-                                                  (size_t) 4096 * i));
+                           snd_pcm_sgbuf_get_addr(substream, 4096 * i));
 }
 
 /* ------------- ALSA Devices ---------------------------- */
-static int __devinit snd_hdspm_create_pcm(snd_card_t * card,
-                                         hdspm_t * hdspm)
+static int __devinit snd_hdspm_create_pcm(struct snd_card *card,
+                                         struct hdspm * hdspm)
 {
-       snd_pcm_t *pcm;
+       struct snd_pcm *pcm;
        int err;
 
-       if ((err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm)) < 0)
+       err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
+       if (err < 0)
                return err;
 
        hdspm->pcm = pcm;
@@ -3387,37 +4273,43 @@ static int __devinit snd_hdspm_create_pcm(snd_card_t * card,
 
        pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
 
-       if ((err = snd_hdspm_preallocate_memory(hdspm)) < 0)
+       err = snd_hdspm_preallocate_memory(hdspm);
+       if (err < 0)
                return err;
 
        return 0;
 }
 
-static inline void snd_hdspm_initialize_midi_flush(hdspm_t * hdspm)
+static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
 {
        snd_hdspm_flush_midi_input(hdspm, 0);
        snd_hdspm_flush_midi_input(hdspm, 1);
 }
 
-static int __devinit snd_hdspm_create_alsa_devices(snd_card_t * card,
-                                                  hdspm_t * hdspm)
+static int __devinit snd_hdspm_create_alsa_devices(struct snd_card *card,
+                                                  struct hdspm * hdspm)
 {
        int err;
 
        snd_printdd("Create card...\n");
-       if ((err = snd_hdspm_create_pcm(card, hdspm)) < 0)
+       err = snd_hdspm_create_pcm(card, hdspm);
+       if (err < 0)
                return err;
 
-       if ((err = snd_hdspm_create_midi(card, hdspm, 0)) < 0)
+       err = snd_hdspm_create_midi(card, hdspm, 0);
+       if (err < 0)
                return err;
 
-       if ((err = snd_hdspm_create_midi(card, hdspm, 1)) < 0)
+       err = snd_hdspm_create_midi(card, hdspm, 1);
+       if (err < 0)
                return err;
 
-       if ((err = snd_hdspm_create_controls(card, hdspm)) < 0)
+       err = snd_hdspm_create_controls(card, hdspm);
+       if (err < 0)
                return err;
 
-       if ((err = snd_hdspm_create_hwdep(card, hdspm)) < 0)
+       err = snd_hdspm_create_hwdep(card, hdspm);
+       if (err < 0)
                return err;
 
        snd_printdd("proc init...\n");
@@ -3432,7 +4324,8 @@ static int __devinit snd_hdspm_create_alsa_devices(snd_card_t * card,
        hdspm->playback_substream = NULL;
 
        snd_printdd("Set defaults...\n");
-       if ((err = snd_hdspm_set_defaults(hdspm)) < 0)
+       err = snd_hdspm_set_defaults(hdspm);
+       if (err < 0)
                return err;
 
        snd_printdd("Update mixer controls...\n");
@@ -3440,7 +4333,8 @@ static int __devinit snd_hdspm_create_alsa_devices(snd_card_t * card,
 
        snd_printdd("Initializeing complete ???\n");
 
-       if ((err = snd_card_register(card)) < 0) {
+       err = snd_card_register(card);
+       if (err < 0) {
                snd_printk(KERN_ERR "HDSPM: error registering card\n");
                return err;
        }
@@ -3450,36 +4344,18 @@ static int __devinit snd_hdspm_create_alsa_devices(snd_card_t * card,
        return 0;
 }
 
-static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
+static int __devinit snd_hdspm_create(struct snd_card *card,
+                                     struct hdspm *hdspm,
                                      int precise_ptr, int enable_monitor)
 {
        struct pci_dev *pci = hdspm->pci;
        int err;
-       int i;
-
        unsigned long io_extent;
 
        hdspm->irq = -1;
-       hdspm->irq_count = 0;
-
-       hdspm->midi[0].rmidi = NULL;
-       hdspm->midi[1].rmidi = NULL;
-       hdspm->midi[0].input = NULL;
-       hdspm->midi[1].input = NULL;
-       hdspm->midi[0].output = NULL;
-       hdspm->midi[1].output = NULL;
+
        spin_lock_init(&hdspm->midi[0].lock);
        spin_lock_init(&hdspm->midi[1].lock);
-       hdspm->iobase = NULL;
-       hdspm->control_register = 0;
-       hdspm->control2_register = 0;
-
-       hdspm->playback_buffer = NULL;
-       hdspm->capture_buffer = NULL;
-
-       for (i = 0; i < HDSPM_MAX_CHANNELS; ++i)
-               hdspm->playback_mixer_ctls[i] = NULL;
-       hdspm->mixer = NULL;
 
        hdspm->card = card;
 
@@ -3491,16 +4367,25 @@ static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
        pci_read_config_word(hdspm->pci,
                             PCI_CLASS_REVISION, &hdspm->firmware_rev);
 
-       strcpy(card->driver, "HDSPM");
+       hdspm->is_aes32 = (hdspm->firmware_rev >= HDSPM_AESREVISION);
+
        strcpy(card->mixername, "Xilinx FPGA");
-       hdspm->card_name = "RME HDSPM MADI";
+       if (hdspm->is_aes32) {
+               strcpy(card->driver, "HDSPAES32");
+               hdspm->card_name = "RME HDSPM AES32";
+       } else {
+               strcpy(card->driver, "HDSPM");
+               hdspm->card_name = "RME HDSPM MADI";
+       }
 
-       if ((err = pci_enable_device(pci)) < 0)
+       err = pci_enable_device(pci);
+       if (err < 0)
                return err;
 
        pci_set_master(hdspm->pci);
 
-       if ((err = pci_request_regions(pci, "hdspm")) < 0)
+       err = pci_request_regions(pci, "hdspm");
+       if (err < 0)
                return err;
 
        hdspm->port = pci_resource_start(pci, 0);
@@ -3510,8 +4395,10 @@ static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
                   hdspm->port, hdspm->port + io_extent - 1);
 
 
-       if ((hdspm->iobase = ioremap_nocache(hdspm->port, io_extent)) == NULL) {
-               snd_printk(KERN_ERR "HDSPM: unable to remap region 0x%lx-0x%lx\n",
+       hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
+       if (!hdspm->iobase) {
+               snd_printk(KERN_ERR "HDSPM: "
+                          "unable to remap region 0x%lx-0x%lx\n",
                           hdspm->port, hdspm->port + io_extent - 1);
                return -EBUSY;
        }
@@ -3520,8 +4407,7 @@ static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
                   hdspm->port + io_extent - 1);
 
        if (request_irq(pci->irq, snd_hdspm_interrupt,
-                       SA_INTERRUPT | SA_SHIRQ, "hdspm",
-                       (void *) hdspm)) {
+                       IRQF_SHARED, "hdspm", hdspm)) {
                snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
                return -EBUSY;
        }
@@ -3533,13 +4419,13 @@ static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
 
        hdspm->monitor_outs = enable_monitor;
 
-       snd_printdd("kmalloc Mixer memory of %d Bytes\n",
-                  sizeof(hdspm_mixer_t));
-       if ((hdspm->mixer =
-            (hdspm_mixer_t *) kmalloc(sizeof(hdspm_mixer_t), GFP_KERNEL))
-           == NULL) {
-               snd_printk(KERN_ERR "HDSPM: unable to kmalloc Mixer memory of %d Bytes\n",
-                          (int)sizeof(hdspm_mixer_t));
+       snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
+                  sizeof(struct hdspm_mixer));
+       hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
+       if (!hdspm->mixer) {
+               snd_printk(KERN_ERR "HDSPM: "
+                          "unable to kmalloc Mixer memory of %d Bytes\n",
+                          (int)sizeof(struct hdspm_mixer));
                return err;
        }
 
@@ -3548,7 +4434,8 @@ static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
        hdspm->qs_channels = MADI_QS_CHANNELS;
 
        snd_printdd("create alsa devices.\n");
-       if ((err = snd_hdspm_create_alsa_devices(card, hdspm)) < 0)
+       err = snd_hdspm_create_alsa_devices(card, hdspm);
+       if (err < 0)
                return err;
 
        snd_hdspm_initialize_midi_flush(hdspm);
@@ -3556,16 +4443,15 @@ static int __devinit snd_hdspm_create(snd_card_t * card, hdspm_t * hdspm,
        return 0;
 }
 
-static int snd_hdspm_free(hdspm_t * hdspm)
+static int snd_hdspm_free(struct hdspm * hdspm)
 {
 
        if (hdspm->port) {
 
                /* stop th audio, and cancel all interrupts */
                hdspm->control_register &=
-                   ~(HDSPM_Start | HDSPM_AudioInterruptEnable
-                     | HDSPM_Midi0InterruptEnable |
-                     HDSPM_Midi1InterruptEnable);
+                   ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
+                     HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable);
                hdspm_write(hdspm, HDSPM_controlRegister,
                            hdspm->control_register);
        }
@@ -3573,15 +4459,11 @@ static int snd_hdspm_free(hdspm_t * hdspm)
        if (hdspm->irq >= 0)
                free_irq(hdspm->irq, (void *) hdspm);
 
-
-       if (hdspm->mixer)
-               kfree(hdspm->mixer);
+       kfree(hdspm->mixer);
 
        if (hdspm->iobase)
                iounmap(hdspm->iobase);
 
-       snd_hdspm_memory_free(hdspm);
-
        if (hdspm->port)
                pci_release_regions(hdspm->pci);
 
@@ -3589,9 +4471,9 @@ static int snd_hdspm_free(hdspm_t * hdspm)
        return 0;
 }
 
-static void snd_hdspm_card_free(snd_card_t * card)
+static void snd_hdspm_card_free(struct snd_card *card)
 {
-       hdspm_t *hdspm = (hdspm_t *) card->private_data;
+       struct hdspm *hdspm = card->private_data;
 
        if (hdspm)
                snd_hdspm_free(hdspm);
@@ -3601,8 +4483,8 @@ static int __devinit snd_hdspm_probe(struct pci_dev *pci,
                                     const struct pci_device_id *pci_id)
 {
        static int dev;
-       hdspm_t *hdspm;
-       snd_card_t *card;
+       struct hdspm *hdspm;
+       struct snd_card *card;
        int err;
 
        if (dev >= SNDRV_CARDS)
@@ -3612,18 +4494,21 @@ static int __devinit snd_hdspm_probe(struct pci_dev *pci,
                return -ENOENT;
        }
 
-       if (!(card = snd_card_new(index[dev], id[dev],
-                                 THIS_MODULE, sizeof(hdspm_t))))
-               return -ENOMEM;
+       err = snd_card_create(index[dev], id[dev],
+                             THIS_MODULE, sizeof(struct hdspm), &card);
+       if (err < 0)
+               return err;
 
-       hdspm = (hdspm_t *) card->private_data;
+       hdspm = card->private_data;
        card->private_free = snd_hdspm_card_free;
        hdspm->dev = dev;
        hdspm->pci = pci;
 
-       if ((err =
-            snd_hdspm_create(card, hdspm, precise_ptr[dev],
-                             enable_monitor[dev])) < 0) {
+       snd_card_set_dev(card, &pci->dev);
+
+       err = snd_hdspm_create(card, hdspm, precise_ptr[dev],
+                              enable_monitor[dev]);
+       if (err < 0) {
                snd_card_free(card);
                return err;
        }
@@ -3632,7 +4517,8 @@ static int __devinit snd_hdspm_probe(struct pci_dev *pci,
        sprintf(card->longname, "%s at 0x%lx, irq %d", hdspm->card_name,
                hdspm->port, hdspm->irq);
 
-       if ((err = snd_card_register(card)) < 0) {
+       err = snd_card_register(card);
+       if (err < 0) {
                snd_card_free(card);
                return err;
        }