Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6] / sound / pci / rme9652 / hdsp.c
1 /*
2  *   ALSA driver for RME Hammerfall DSP audio interface(s)
3  *
4  *      Copyright (c) 2002  Paul Davis
5  *                          Marcus Andersson
6  *                          Thomas Charbonnel
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */
23
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <linux/firmware.h>
31 #include <linux/moduleparam.h>
32
33 #include <sound/core.h>
34 #include <sound/control.h>
35 #include <sound/pcm.h>
36 #include <sound/info.h>
37 #include <sound/asoundef.h>
38 #include <sound/rawmidi.h>
39 #include <sound/hwdep.h>
40 #include <sound/initval.h>
41 #include <sound/hdsp.h>
42
43 #include <asm/byteorder.h>
44 #include <asm/current.h>
45 #include <asm/io.h>
46
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
50
51 module_param_array(index, int, NULL, 0444);
52 MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
53 module_param_array(id, charp, NULL, 0444);
54 MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
55 module_param_array(enable, bool, NULL, 0444);
56 MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
57 MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
58 MODULE_DESCRIPTION("RME Hammerfall DSP");
59 MODULE_LICENSE("GPL");
60 MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
61                 "{RME HDSP-9652},"
62                 "{RME HDSP-9632}}");
63
64 #define HDSP_MAX_CHANNELS        26
65 #define HDSP_MAX_DS_CHANNELS     14
66 #define HDSP_MAX_QS_CHANNELS     8
67 #define DIGIFACE_SS_CHANNELS     26
68 #define DIGIFACE_DS_CHANNELS     14
69 #define MULTIFACE_SS_CHANNELS    18
70 #define MULTIFACE_DS_CHANNELS    14
71 #define H9652_SS_CHANNELS        26
72 #define H9652_DS_CHANNELS        14
73 /* This does not include possible Analog Extension Boards
74    AEBs are detected at card initialization
75 */
76 #define H9632_SS_CHANNELS        12
77 #define H9632_DS_CHANNELS        8
78 #define H9632_QS_CHANNELS        4
79
80 /* Write registers. These are defined as byte-offsets from the iobase value.
81  */
82 #define HDSP_resetPointer               0
83 #define HDSP_outputBufferAddress        32
84 #define HDSP_inputBufferAddress         36
85 #define HDSP_controlRegister            64
86 #define HDSP_interruptConfirmation      96
87 #define HDSP_outputEnable               128
88 #define HDSP_control2Reg                256
89 #define HDSP_midiDataOut0               352
90 #define HDSP_midiDataOut1               356
91 #define HDSP_fifoData                   368
92 #define HDSP_inputEnable                384
93
94 /* Read registers. These are defined as byte-offsets from the iobase value
95  */
96
97 #define HDSP_statusRegister    0
98 #define HDSP_timecode        128
99 #define HDSP_status2Register 192
100 #define HDSP_midiDataOut0    352
101 #define HDSP_midiDataOut1    356
102 #define HDSP_midiDataIn0     360
103 #define HDSP_midiDataIn1     364
104 #define HDSP_midiStatusOut0  384
105 #define HDSP_midiStatusOut1  388
106 #define HDSP_midiStatusIn0   392
107 #define HDSP_midiStatusIn1   396
108 #define HDSP_fifoStatus      400
109
110 /* the meters are regular i/o-mapped registers, but offset
111    considerably from the rest. the peak registers are reset
112    when read; the least-significant 4 bits are full-scale counters; 
113    the actual peak value is in the most-significant 24 bits.
114 */
115
116 #define HDSP_playbackPeakLevel  4096  /* 26 * 32 bit values */
117 #define HDSP_inputPeakLevel     4224  /* 26 * 32 bit values */
118 #define HDSP_outputPeakLevel    4352  /* (26+2) * 32 bit values */
119 #define HDSP_playbackRmsLevel   4612  /* 26 * 64 bit values */
120 #define HDSP_inputRmsLevel      4868  /* 26 * 64 bit values */
121
122
123 /* This is for H9652 cards
124    Peak values are read downward from the base
125    Rms values are read upward
126    There are rms values for the outputs too
127    26*3 values are read in ss mode
128    14*3 in ds mode, with no gap between values
129 */
130 #define HDSP_9652_peakBase      7164    
131 #define HDSP_9652_rmsBase       4096
132
133 /* c.f. the hdsp_9632_meters_t struct */
134 #define HDSP_9632_metersBase    4096
135
136 #define HDSP_IO_EXTENT     7168
137
138 /* control2 register bits */
139
140 #define HDSP_TMS                0x01
141 #define HDSP_TCK                0x02
142 #define HDSP_TDI                0x04
143 #define HDSP_JTAG               0x08
144 #define HDSP_PWDN               0x10
145 #define HDSP_PROGRAM            0x020
146 #define HDSP_CONFIG_MODE_0      0x040
147 #define HDSP_CONFIG_MODE_1      0x080
148 #define HDSP_VERSION_BIT        0x100
149 #define HDSP_BIGENDIAN_MODE     0x200
150 #define HDSP_RD_MULTIPLE        0x400
151 #define HDSP_9652_ENABLE_MIXER  0x800
152 #define HDSP_TDO                0x10000000
153
154 #define HDSP_S_PROGRAM          (HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
155 #define HDSP_S_LOAD             (HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
156
157 /* Control Register bits */
158
159 #define HDSP_Start                (1<<0)  /* start engine */
160 #define HDSP_Latency0             (1<<1)  /* buffer size = 2^n where n is defined by Latency{2,1,0} */
161 #define HDSP_Latency1             (1<<2)  /* [ see above ] */
162 #define HDSP_Latency2             (1<<3)  /* [ see above ] */
163 #define HDSP_ClockModeMaster      (1<<4)  /* 1=Master, 0=Slave/Autosync */
164 #define HDSP_AudioInterruptEnable (1<<5)  /* what do you think ? */
165 #define HDSP_Frequency0           (1<<6)  /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
166 #define HDSP_Frequency1           (1<<7)  /* 0=32kHz/64kHz/128kHz */
167 #define HDSP_DoubleSpeed          (1<<8)  /* 0=normal speed, 1=double speed */
168 #define HDSP_SPDIFProfessional    (1<<9)  /* 0=consumer, 1=professional */
169 #define HDSP_SPDIFEmphasis        (1<<10) /* 0=none, 1=on */
170 #define HDSP_SPDIFNonAudio        (1<<11) /* 0=off, 1=on */
171 #define HDSP_SPDIFOpticalOut      (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
172 #define HDSP_SyncRef2             (1<<13) 
173 #define HDSP_SPDIFInputSelect0    (1<<14) 
174 #define HDSP_SPDIFInputSelect1    (1<<15) 
175 #define HDSP_SyncRef0             (1<<16) 
176 #define HDSP_SyncRef1             (1<<17)
177 #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */ 
178 #define HDSP_XLRBreakoutCable     (1<<20) /* For H9632 cards */
179 #define HDSP_Midi0InterruptEnable (1<<22)
180 #define HDSP_Midi1InterruptEnable (1<<23)
181 #define HDSP_LineOut              (1<<24)
182 #define HDSP_ADGain0              (1<<25) /* From here : H9632 specific */
183 #define HDSP_ADGain1              (1<<26)
184 #define HDSP_DAGain0              (1<<27)
185 #define HDSP_DAGain1              (1<<28)
186 #define HDSP_PhoneGain0           (1<<29)
187 #define HDSP_PhoneGain1           (1<<30)
188 #define HDSP_QuadSpeed            (1<<31)
189
190 #define HDSP_ADGainMask       (HDSP_ADGain0|HDSP_ADGain1)
191 #define HDSP_ADGainMinus10dBV  HDSP_ADGainMask
192 #define HDSP_ADGainPlus4dBu   (HDSP_ADGain0)
193 #define HDSP_ADGainLowGain     0
194
195 #define HDSP_DAGainMask         (HDSP_DAGain0|HDSP_DAGain1)
196 #define HDSP_DAGainHighGain      HDSP_DAGainMask
197 #define HDSP_DAGainPlus4dBu     (HDSP_DAGain0)
198 #define HDSP_DAGainMinus10dBV    0
199
200 #define HDSP_PhoneGainMask      (HDSP_PhoneGain0|HDSP_PhoneGain1)
201 #define HDSP_PhoneGain0dB        HDSP_PhoneGainMask
202 #define HDSP_PhoneGainMinus6dB  (HDSP_PhoneGain0)
203 #define HDSP_PhoneGainMinus12dB  0
204
205 #define HDSP_LatencyMask    (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
206 #define HDSP_FrequencyMask  (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
207
208 #define HDSP_SPDIFInputMask    (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
209 #define HDSP_SPDIFInputADAT1    0
210 #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
211 #define HDSP_SPDIFInputCdrom   (HDSP_SPDIFInputSelect1)
212 #define HDSP_SPDIFInputAES     (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
213
214 #define HDSP_SyncRefMask        (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
215 #define HDSP_SyncRef_ADAT1       0
216 #define HDSP_SyncRef_ADAT2      (HDSP_SyncRef0)
217 #define HDSP_SyncRef_ADAT3      (HDSP_SyncRef1)
218 #define HDSP_SyncRef_SPDIF      (HDSP_SyncRef0|HDSP_SyncRef1)
219 #define HDSP_SyncRef_WORD       (HDSP_SyncRef2)
220 #define HDSP_SyncRef_ADAT_SYNC  (HDSP_SyncRef0|HDSP_SyncRef2)
221
222 /* Sample Clock Sources */
223
224 #define HDSP_CLOCK_SOURCE_AUTOSYNC           0
225 #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ     1
226 #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ   2
227 #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ     3
228 #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ     4
229 #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ   5
230 #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ     6
231 #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ    7
232 #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ  8
233 #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ    9
234
235 /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
236
237 #define HDSP_SYNC_FROM_WORD      0
238 #define HDSP_SYNC_FROM_SPDIF     1
239 #define HDSP_SYNC_FROM_ADAT1     2
240 #define HDSP_SYNC_FROM_ADAT_SYNC 3
241 #define HDSP_SYNC_FROM_ADAT2     4
242 #define HDSP_SYNC_FROM_ADAT3     5
243
244 /* SyncCheck status */
245
246 #define HDSP_SYNC_CHECK_NO_LOCK 0
247 #define HDSP_SYNC_CHECK_LOCK    1
248 #define HDSP_SYNC_CHECK_SYNC    2
249
250 /* AutoSync references - used by "autosync_ref" control switch */
251
252 #define HDSP_AUTOSYNC_FROM_WORD      0
253 #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
254 #define HDSP_AUTOSYNC_FROM_SPDIF     2
255 #define HDSP_AUTOSYNC_FROM_NONE      3
256 #define HDSP_AUTOSYNC_FROM_ADAT1     4
257 #define HDSP_AUTOSYNC_FROM_ADAT2     5
258 #define HDSP_AUTOSYNC_FROM_ADAT3     6
259
260 /* Possible sources of S/PDIF input */
261
262 #define HDSP_SPDIFIN_OPTICAL  0 /* optical  (ADAT1) */
263 #define HDSP_SPDIFIN_COAXIAL  1 /* coaxial (RCA) */
264 #define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
265 #define HDSP_SPDIFIN_AES      3 /* xlr for H9632 (AES)*/
266
267 #define HDSP_Frequency32KHz    HDSP_Frequency0
268 #define HDSP_Frequency44_1KHz  HDSP_Frequency1
269 #define HDSP_Frequency48KHz    (HDSP_Frequency1|HDSP_Frequency0)
270 #define HDSP_Frequency64KHz    (HDSP_DoubleSpeed|HDSP_Frequency0)
271 #define HDSP_Frequency88_2KHz  (HDSP_DoubleSpeed|HDSP_Frequency1)
272 #define HDSP_Frequency96KHz    (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
273 /* For H9632 cards */
274 #define HDSP_Frequency128KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
275 #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
276 #define HDSP_Frequency192KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
277
278 #define hdsp_encode_latency(x)       (((x)<<1) & HDSP_LatencyMask)
279 #define hdsp_decode_latency(x)       (((x) & HDSP_LatencyMask)>>1)
280
281 #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
282 #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
283
284 /* Status Register bits */
285
286 #define HDSP_audioIRQPending    (1<<0)
287 #define HDSP_Lock2              (1<<1)     /* this is for Digiface and H9652 */
288 #define HDSP_spdifFrequency3    HDSP_Lock2 /* this is for H9632 only */
289 #define HDSP_Lock1              (1<<2)
290 #define HDSP_Lock0              (1<<3)
291 #define HDSP_SPDIFSync          (1<<4)
292 #define HDSP_TimecodeLock       (1<<5)
293 #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
294 #define HDSP_Sync2              (1<<16)
295 #define HDSP_Sync1              (1<<17)
296 #define HDSP_Sync0              (1<<18)
297 #define HDSP_DoubleSpeedStatus  (1<<19)
298 #define HDSP_ConfigError        (1<<20)
299 #define HDSP_DllError           (1<<21)
300 #define HDSP_spdifFrequency0    (1<<22)
301 #define HDSP_spdifFrequency1    (1<<23)
302 #define HDSP_spdifFrequency2    (1<<24)
303 #define HDSP_SPDIFErrorFlag     (1<<25)
304 #define HDSP_BufferID           (1<<26)
305 #define HDSP_TimecodeSync       (1<<27)
306 #define HDSP_AEBO               (1<<28) /* H9632 specific Analog Extension Boards */
307 #define HDSP_AEBI               (1<<29) /* 0 = present, 1 = absent */
308 #define HDSP_midi0IRQPending    (1<<30) 
309 #define HDSP_midi1IRQPending    (1<<31)
310
311 #define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
312
313 #define HDSP_spdifFrequency32KHz   (HDSP_spdifFrequency0)
314 #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
315 #define HDSP_spdifFrequency48KHz   (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
316
317 #define HDSP_spdifFrequency64KHz   (HDSP_spdifFrequency2)
318 #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
319 #define HDSP_spdifFrequency96KHz   (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
320
321 /* This is for H9632 cards */
322 #define HDSP_spdifFrequency128KHz   HDSP_spdifFrequencyMask
323 #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
324 #define HDSP_spdifFrequency192KHz   (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
325
326 /* Status2 Register bits */
327
328 #define HDSP_version0     (1<<0)
329 #define HDSP_version1     (1<<1)
330 #define HDSP_version2     (1<<2)
331 #define HDSP_wc_lock      (1<<3)
332 #define HDSP_wc_sync      (1<<4)
333 #define HDSP_inp_freq0    (1<<5)
334 #define HDSP_inp_freq1    (1<<6)
335 #define HDSP_inp_freq2    (1<<7)
336 #define HDSP_SelSyncRef0  (1<<8)
337 #define HDSP_SelSyncRef1  (1<<9)
338 #define HDSP_SelSyncRef2  (1<<10)
339
340 #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
341
342 #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
343 #define HDSP_systemFrequency32   (HDSP_inp_freq0)
344 #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
345 #define HDSP_systemFrequency48   (HDSP_inp_freq0|HDSP_inp_freq1)
346 #define HDSP_systemFrequency64   (HDSP_inp_freq2)
347 #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
348 #define HDSP_systemFrequency96   (HDSP_inp_freq1|HDSP_inp_freq2)
349 /* FIXME : more values for 9632 cards ? */
350
351 #define HDSP_SelSyncRefMask        (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
352 #define HDSP_SelSyncRef_ADAT1      0
353 #define HDSP_SelSyncRef_ADAT2      (HDSP_SelSyncRef0)
354 #define HDSP_SelSyncRef_ADAT3      (HDSP_SelSyncRef1)
355 #define HDSP_SelSyncRef_SPDIF      (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
356 #define HDSP_SelSyncRef_WORD       (HDSP_SelSyncRef2)
357 #define HDSP_SelSyncRef_ADAT_SYNC  (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
358
359 /* Card state flags */
360
361 #define HDSP_InitializationComplete  (1<<0)
362 #define HDSP_FirmwareLoaded          (1<<1)
363 #define HDSP_FirmwareCached          (1<<2)
364
365 /* FIFO wait times, defined in terms of 1/10ths of msecs */
366
367 #define HDSP_LONG_WAIT   5000
368 #define HDSP_SHORT_WAIT  30
369
370 #define UNITY_GAIN                       32768
371 #define MINUS_INFINITY_GAIN              0
372
373 /* the size of a substream (1 mono data stream) */
374
375 #define HDSP_CHANNEL_BUFFER_SAMPLES  (16*1024)
376 #define HDSP_CHANNEL_BUFFER_BYTES    (4*HDSP_CHANNEL_BUFFER_SAMPLES)
377
378 /* the size of the area we need to allocate for DMA transfers. the
379    size is the same regardless of the number of channels - the 
380    Multiface still uses the same memory area.
381
382    Note that we allocate 1 more channel than is apparently needed
383    because the h/w seems to write 1 byte beyond the end of the last
384    page. Sigh.
385 */
386
387 #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
388 #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
389
390 /* use hotplug firmeare loader? */
391 #if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
392 #ifndef HDSP_USE_HWDEP_LOADER
393 #define HDSP_FW_LOADER
394 #endif
395 #endif
396
397 struct hdsp_9632_meters {
398     u32 input_peak[16];
399     u32 playback_peak[16];
400     u32 output_peak[16];
401     u32 xxx_peak[16];
402     u32 padding[64];
403     u32 input_rms_low[16];
404     u32 playback_rms_low[16];
405     u32 output_rms_low[16];
406     u32 xxx_rms_low[16];
407     u32 input_rms_high[16];
408     u32 playback_rms_high[16];
409     u32 output_rms_high[16];
410     u32 xxx_rms_high[16];
411 };
412
413 struct hdsp_midi {
414     struct hdsp             *hdsp;
415     int                      id;
416     struct snd_rawmidi           *rmidi;
417     struct snd_rawmidi_substream *input;
418     struct snd_rawmidi_substream *output;
419     char                     istimer; /* timer in use */
420     struct timer_list        timer;
421     spinlock_t               lock;
422     int                      pending;
423 };
424
425 struct hdsp {
426         spinlock_t            lock;
427         struct snd_pcm_substream *capture_substream;
428         struct snd_pcm_substream *playback_substream;
429         struct hdsp_midi      midi[2];
430         struct tasklet_struct midi_tasklet;
431         int                   use_midi_tasklet;
432         int                   precise_ptr;
433         u32                   control_register;      /* cached value */
434         u32                   control2_register;     /* cached value */
435         u32                   creg_spdif;
436         u32                   creg_spdif_stream;
437         int                   clock_source_locked;
438         char                 *card_name;             /* digiface/multiface */
439         enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
440         unsigned short        firmware_rev;
441         unsigned short        state;                 /* stores state bits */
442         u32                   firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
443         size_t                period_bytes;          /* guess what this is */
444         unsigned char         max_channels;
445         unsigned char         qs_in_channels;        /* quad speed mode for H9632 */
446         unsigned char         ds_in_channels;
447         unsigned char         ss_in_channels;       /* different for multiface/digiface */
448         unsigned char         qs_out_channels;      
449         unsigned char         ds_out_channels;
450         unsigned char         ss_out_channels;
451
452         struct snd_dma_buffer capture_dma_buf;
453         struct snd_dma_buffer playback_dma_buf;
454         unsigned char        *capture_buffer;       /* suitably aligned address */
455         unsigned char        *playback_buffer;      /* suitably aligned address */
456
457         pid_t                 capture_pid;
458         pid_t                 playback_pid;
459         int                   running;
460         int                   system_sample_rate;
461         char                 *channel_map;
462         int                   dev;
463         int                   irq;
464         unsigned long         port;
465         void __iomem         *iobase;
466         struct snd_card *card;
467         struct snd_pcm *pcm;
468         struct snd_hwdep          *hwdep;
469         struct pci_dev       *pci;
470         struct snd_kcontrol *spdif_ctl;
471         unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
472 };
473
474 /* These tables map the ALSA channels 1..N to the channels that we
475    need to use in order to find the relevant channel buffer. RME
476    refer to this kind of mapping as between "the ADAT channel and
477    the DMA channel." We index it using the logical audio channel,
478    and the value is the DMA channel (i.e. channel buffer number)
479    where the data for that channel can be read/written from/to.
480 */
481
482 static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
483         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
484         18, 19, 20, 21, 22, 23, 24, 25
485 };
486
487 static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
488         /* Analog */
489         0, 1, 2, 3, 4, 5, 6, 7, 
490         /* ADAT 2 */
491         16, 17, 18, 19, 20, 21, 22, 23, 
492         /* SPDIF */
493         24, 25,
494         -1, -1, -1, -1, -1, -1, -1, -1
495 };
496
497 static char channel_map_ds[HDSP_MAX_CHANNELS] = {
498         /* ADAT channels are remapped */
499         1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
500         /* channels 12 and 13 are S/PDIF */
501         24, 25,
502         /* others don't exist */
503         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
504 };
505
506 static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
507         /* ADAT channels */
508         0, 1, 2, 3, 4, 5, 6, 7,
509         /* SPDIF */
510         8, 9,
511         /* Analog */
512         10, 11, 
513         /* AO4S-192 and AI4S-192 extension boards */
514         12, 13, 14, 15,
515         /* others don't exist */
516         -1, -1, -1, -1, -1, -1, -1, -1, 
517         -1, -1
518 };
519
520 static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
521         /* ADAT */
522         1, 3, 5, 7,
523         /* SPDIF */
524         8, 9,
525         /* Analog */
526         10, 11, 
527         /* AO4S-192 and AI4S-192 extension boards */
528         12, 13, 14, 15,
529         /* others don't exist */
530         -1, -1, -1, -1, -1, -1, -1, -1,
531         -1, -1, -1, -1, -1, -1
532 };
533
534 static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
535         /* ADAT is disabled in this mode */
536         /* SPDIF */
537         8, 9,
538         /* Analog */
539         10, 11,
540         /* AO4S-192 and AI4S-192 extension boards */
541         12, 13, 14, 15,
542         /* others don't exist */
543         -1, -1, -1, -1, -1, -1, -1, -1,
544         -1, -1, -1, -1, -1, -1, -1, -1,
545         -1, -1
546 };
547
548 static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
549 {
550         dmab->dev.type = SNDRV_DMA_TYPE_DEV;
551         dmab->dev.dev = snd_dma_pci_data(pci);
552         if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
553                 if (dmab->bytes >= size)
554                         return 0;
555         }
556         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
557                                 size, dmab) < 0)
558                 return -ENOMEM;
559         return 0;
560 }
561
562 static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
563 {
564         if (dmab->area) {
565                 dmab->dev.dev = NULL; /* make it anonymous */
566                 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
567         }
568 }
569
570
571 static struct pci_device_id snd_hdsp_ids[] __devinitdata = {
572         {
573                 .vendor = PCI_VENDOR_ID_XILINX,
574                 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP, 
575                 .subvendor = PCI_ANY_ID,
576                 .subdevice = PCI_ANY_ID,
577         }, /* RME Hammerfall-DSP */
578         { 0, },
579 };
580
581 MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
582
583 /* prototypes */
584 static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
585 static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
586 static int snd_hdsp_enable_io (struct hdsp *hdsp);
587 static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
588 static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
589 static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
590 static int hdsp_autosync_ref(struct hdsp *hdsp);
591 static int snd_hdsp_set_defaults(struct hdsp *hdsp);
592 static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
593
594 static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
595 {
596         switch (hdsp->firmware_rev) {
597         case 0xa:
598                 return (64 * out) + (32 + (in));
599         case 0x96:
600         case 0x97:
601                 return (32 * out) + (16 + (in));
602         default:
603                 return (52 * out) + (26 + (in));
604         }
605 }
606
607 static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
608 {
609         switch (hdsp->firmware_rev) {
610         case 0xa:
611                 return (64 * out) + in;
612         case 0x96:
613         case 0x97:
614                 return (32 * out) + in;
615         default:
616                 return (52 * out) + in;
617         }
618 }
619
620 static void hdsp_write(struct hdsp *hdsp, int reg, int val)
621 {
622         writel(val, hdsp->iobase + reg);
623 }
624
625 static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
626 {
627         return readl (hdsp->iobase + reg);
628 }
629
630 static int hdsp_check_for_iobox (struct hdsp *hdsp)
631 {
632
633         if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
634         if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
635                 snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
636                 hdsp->state &= ~HDSP_FirmwareLoaded;
637                 return -EIO;
638         }
639         return 0;
640
641 }
642
643 static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
644
645         int i;
646         unsigned long flags;
647
648         if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
649                 
650                 snd_printk ("Hammerfall-DSP: loading firmware\n");
651
652                 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
653                 hdsp_write (hdsp, HDSP_fifoData, 0);
654                 
655                 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
656                         snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
657                         return -EIO;
658                 }
659                 
660                 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
661                 
662                 for (i = 0; i < 24413; ++i) {
663                         hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
664                         if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
665                                 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
666                                 return -EIO;
667                         }
668                 }
669
670                 ssleep(3);
671                 
672                 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
673                         snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
674                         return -EIO;
675                 }
676
677 #ifdef SNDRV_BIG_ENDIAN
678                 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
679 #else
680                 hdsp->control2_register = 0;
681 #endif
682                 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
683                 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
684                 
685         }
686         if (hdsp->state & HDSP_InitializationComplete) {
687                 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
688                 spin_lock_irqsave(&hdsp->lock, flags);
689                 snd_hdsp_set_defaults(hdsp);
690                 spin_unlock_irqrestore(&hdsp->lock, flags); 
691         }
692         
693         hdsp->state |= HDSP_FirmwareLoaded;
694
695         return 0;
696 }
697
698 static int hdsp_get_iobox_version (struct hdsp *hdsp)
699 {
700         if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
701         
702                 hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
703                 hdsp_write (hdsp, HDSP_fifoData, 0);
704                 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
705                         return -EIO;
706
707                 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
708                 hdsp_write (hdsp, HDSP_fifoData, 0);
709
710                 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
711                         hdsp->io_type = Multiface;
712                         hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
713                         hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
714                         hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
715                 } else {
716                         hdsp->io_type = Digiface;
717                 } 
718         } else {
719                 /* firmware was already loaded, get iobox type */
720                 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
721                         hdsp->io_type = Multiface;
722                 else
723                         hdsp->io_type = Digiface;
724         }
725         return 0;
726 }
727
728
729 static int hdsp_check_for_firmware (struct hdsp *hdsp, int show_err)
730 {
731         if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
732         if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
733                 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
734                 hdsp->state &= ~HDSP_FirmwareLoaded;
735                 if (! show_err)
736                         return -EIO;
737                 /* try to load firmware */
738                 if (hdsp->state & HDSP_FirmwareCached) {
739                         if (snd_hdsp_load_firmware_from_cache(hdsp) != 0)
740                                 snd_printk(KERN_ERR "Hammerfall-DSP: Firmware loading from cache failed, please upload manually.\n");
741                 } else {
742                         snd_printk(KERN_ERR "Hammerfall-DSP: No firmware loaded nor cached, please upload firmware.\n");
743                 }
744                 return -EIO;
745         }
746         return 0;
747 }
748
749
750 static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
751 {    
752         int i;
753
754         /* the fifoStatus registers reports on how many words
755            are available in the command FIFO.
756         */
757         
758         for (i = 0; i < timeout; i++) {
759
760                 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
761                         return 0;
762
763                 /* not very friendly, but we only do this during a firmware
764                    load and changing the mixer, so we just put up with it.
765                 */
766
767                 udelay (100);
768         }
769
770         snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
771                     count, timeout);
772         return -1;
773 }
774
775 static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
776 {
777         if (addr >= HDSP_MATRIX_MIXER_SIZE)
778                 return 0;
779
780         return hdsp->mixer_matrix[addr];
781 }
782
783 static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
784 {
785         unsigned int ad;
786
787         if (addr >= HDSP_MATRIX_MIXER_SIZE)
788                 return -1;
789         
790         if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
791
792                 /* from martin bjornsen:
793                    
794                    "You can only write dwords to the
795                    mixer memory which contain two
796                    mixer values in the low and high
797                    word. So if you want to change
798                    value 0 you have to read value 1
799                    from the cache and write both to
800                    the first dword in the mixer
801                    memory."
802                 */
803
804                 if (hdsp->io_type == H9632 && addr >= 512)
805                         return 0;
806
807                 if (hdsp->io_type == H9652 && addr >= 1352)
808                         return 0;
809
810                 hdsp->mixer_matrix[addr] = data;
811
812                 
813                 /* `addr' addresses a 16-bit wide address, but
814                    the address space accessed via hdsp_write
815                    uses byte offsets. put another way, addr
816                    varies from 0 to 1351, but to access the
817                    corresponding memory location, we need
818                    to access 0 to 2703 ...
819                 */
820                 ad = addr/2;
821         
822                 hdsp_write (hdsp, 4096 + (ad*4), 
823                             (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) + 
824                             hdsp->mixer_matrix[addr&0x7fe]);
825                 
826                 return 0;
827
828         } else {
829
830                 ad = (addr << 16) + data;
831                 
832                 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
833                         return -1;
834
835                 hdsp_write (hdsp, HDSP_fifoData, ad);
836                 hdsp->mixer_matrix[addr] = data;
837
838         }
839
840         return 0;
841 }
842
843 static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
844 {
845         unsigned long flags;
846         int ret = 1;
847
848         spin_lock_irqsave(&hdsp->lock, flags);
849         if ((hdsp->playback_pid != hdsp->capture_pid) &&
850             (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
851                 ret = 0;
852         spin_unlock_irqrestore(&hdsp->lock, flags);
853         return ret;
854 }
855
856 static int hdsp_external_sample_rate (struct hdsp *hdsp)
857 {
858         unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
859         unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
860
861         switch (rate_bits) {
862         case HDSP_systemFrequency32:   return 32000;
863         case HDSP_systemFrequency44_1: return 44100;
864         case HDSP_systemFrequency48:   return 48000;
865         case HDSP_systemFrequency64:   return 64000;
866         case HDSP_systemFrequency88_2: return 88200;
867         case HDSP_systemFrequency96:   return 96000;
868         default:
869                 return 0;
870         }
871 }
872
873 static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
874 {
875         unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
876         unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
877
878         if (status & HDSP_SPDIFErrorFlag)
879                 return 0;
880         
881         switch (rate_bits) {
882         case HDSP_spdifFrequency32KHz: return 32000;
883         case HDSP_spdifFrequency44_1KHz: return 44100;
884         case HDSP_spdifFrequency48KHz: return 48000;
885         case HDSP_spdifFrequency64KHz: return 64000;
886         case HDSP_spdifFrequency88_2KHz: return 88200;
887         case HDSP_spdifFrequency96KHz: return 96000;
888         case HDSP_spdifFrequency128KHz: 
889                 if (hdsp->io_type == H9632) return 128000;
890                 break;
891         case HDSP_spdifFrequency176_4KHz: 
892                 if (hdsp->io_type == H9632) return 176400;
893                 break;
894         case HDSP_spdifFrequency192KHz: 
895                 if (hdsp->io_type == H9632) return 192000;
896                 break;
897         default:
898                 break;
899         }
900         snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
901         return 0;
902 }
903
904 static void hdsp_compute_period_size(struct hdsp *hdsp)
905 {
906         hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
907 }
908
909 static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
910 {
911         int position;
912
913         position = hdsp_read(hdsp, HDSP_statusRegister);
914
915         if (!hdsp->precise_ptr)
916                 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
917
918         position &= HDSP_BufferPositionMask;
919         position /= 4;
920         position &= (hdsp->period_bytes/2) - 1;
921         return position;
922 }
923
924 static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
925 {
926         hdsp_write (hdsp, HDSP_resetPointer, 0);
927 }
928
929 static void hdsp_start_audio(struct hdsp *s)
930 {
931         s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
932         hdsp_write(s, HDSP_controlRegister, s->control_register);
933 }
934
935 static void hdsp_stop_audio(struct hdsp *s)
936 {
937         s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
938         hdsp_write(s, HDSP_controlRegister, s->control_register);
939 }
940
941 static void hdsp_silence_playback(struct hdsp *hdsp)
942 {
943         memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
944 }
945
946 static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
947 {
948         int n;
949
950         spin_lock_irq(&s->lock);
951
952         frames >>= 7;
953         n = 0;
954         while (frames) {
955                 n++;
956                 frames >>= 1;
957         }
958
959         s->control_register &= ~HDSP_LatencyMask;
960         s->control_register |= hdsp_encode_latency(n);
961
962         hdsp_write(s, HDSP_controlRegister, s->control_register);
963
964         hdsp_compute_period_size(s);
965
966         spin_unlock_irq(&s->lock);
967
968         return 0;
969 }
970
971 static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
972 {
973         int reject_if_open = 0;
974         int current_rate;
975         int rate_bits;
976
977         /* ASSUMPTION: hdsp->lock is either held, or
978            there is no need for it (e.g. during module
979            initialization).
980         */
981         
982         if (!(hdsp->control_register & HDSP_ClockModeMaster)) { 
983                 if (called_internally) {
984                         /* request from ctl or card initialization */
985                         snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
986                         return -1;
987                 } else {                
988                         /* hw_param request while in AutoSync mode */
989                         int external_freq = hdsp_external_sample_rate(hdsp);
990                         int spdif_freq = hdsp_spdif_sample_rate(hdsp);
991                 
992                         if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
993                                 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
994                         else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
995                                 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");                     
996                         else if (rate != external_freq) {
997                                 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
998                                 return -1;
999                         }               
1000                 }       
1001         }
1002
1003         current_rate = hdsp->system_sample_rate;
1004
1005         /* Changing from a "single speed" to a "double speed" rate is
1006            not allowed if any substreams are open. This is because
1007            such a change causes a shift in the location of 
1008            the DMA buffers and a reduction in the number of available
1009            buffers. 
1010
1011            Note that a similar but essentially insoluble problem
1012            exists for externally-driven rate changes. All we can do
1013            is to flag rate changes in the read/write routines.  */
1014
1015         if (rate > 96000 && hdsp->io_type != H9632)
1016                 return -EINVAL;
1017         
1018         switch (rate) {
1019         case 32000:
1020                 if (current_rate > 48000)
1021                         reject_if_open = 1;
1022                 rate_bits = HDSP_Frequency32KHz;
1023                 break;
1024         case 44100:
1025                 if (current_rate > 48000)
1026                         reject_if_open = 1;
1027                 rate_bits = HDSP_Frequency44_1KHz;
1028                 break;
1029         case 48000:
1030                 if (current_rate > 48000)
1031                         reject_if_open = 1;
1032                 rate_bits = HDSP_Frequency48KHz;
1033                 break;
1034         case 64000:
1035                 if (current_rate <= 48000 || current_rate > 96000)
1036                         reject_if_open = 1;
1037                 rate_bits = HDSP_Frequency64KHz;
1038                 break;
1039         case 88200:
1040                 if (current_rate <= 48000 || current_rate > 96000)
1041                         reject_if_open = 1;
1042                 rate_bits = HDSP_Frequency88_2KHz;
1043                 break;
1044         case 96000:
1045                 if (current_rate <= 48000 || current_rate > 96000)
1046                         reject_if_open = 1;
1047                 rate_bits = HDSP_Frequency96KHz;
1048                 break;
1049         case 128000:
1050                 if (current_rate < 128000)
1051                         reject_if_open = 1;
1052                 rate_bits = HDSP_Frequency128KHz;
1053                 break;
1054         case 176400:
1055                 if (current_rate < 128000)
1056                         reject_if_open = 1;
1057                 rate_bits = HDSP_Frequency176_4KHz;
1058                 break;
1059         case 192000:
1060                 if (current_rate < 128000)
1061                         reject_if_open = 1;
1062                 rate_bits = HDSP_Frequency192KHz;
1063                 break;
1064         default:
1065                 return -EINVAL;
1066         }
1067
1068         if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1069                 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1070                             hdsp->capture_pid,
1071                             hdsp->playback_pid);
1072                 return -EBUSY;
1073         }
1074
1075         hdsp->control_register &= ~HDSP_FrequencyMask;
1076         hdsp->control_register |= rate_bits;
1077         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1078
1079         if (rate >= 128000) {
1080                 hdsp->channel_map = channel_map_H9632_qs;
1081         } else if (rate > 48000) {
1082                 if (hdsp->io_type == H9632)
1083                         hdsp->channel_map = channel_map_H9632_ds;
1084                 else
1085                         hdsp->channel_map = channel_map_ds;
1086         } else {
1087                 switch (hdsp->io_type) {
1088                 case Multiface:
1089                         hdsp->channel_map = channel_map_mf_ss;
1090                         break;
1091                 case Digiface:
1092                 case H9652:
1093                         hdsp->channel_map = channel_map_df_ss;
1094                         break;
1095                 case H9632:
1096                         hdsp->channel_map = channel_map_H9632_ss;
1097                         break;
1098                 default:
1099                         /* should never happen */
1100                         break;
1101                 }
1102         }
1103         
1104         hdsp->system_sample_rate = rate;
1105
1106         return 0;
1107 }
1108
1109 /*----------------------------------------------------------------------------
1110    MIDI
1111   ----------------------------------------------------------------------------*/
1112
1113 static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
1114 {
1115         /* the hardware already does the relevant bit-mask with 0xff */
1116         if (id)
1117                 return hdsp_read(hdsp, HDSP_midiDataIn1);
1118         else
1119                 return hdsp_read(hdsp, HDSP_midiDataIn0);
1120 }
1121
1122 static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
1123 {
1124         /* the hardware already does the relevant bit-mask with 0xff */
1125         if (id)
1126                 hdsp_write(hdsp, HDSP_midiDataOut1, val);
1127         else
1128                 hdsp_write(hdsp, HDSP_midiDataOut0, val);
1129 }
1130
1131 static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
1132 {
1133         if (id)
1134                 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
1135         else
1136                 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
1137 }
1138
1139 static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
1140 {
1141         int fifo_bytes_used;
1142
1143         if (id)
1144                 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
1145         else
1146                 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
1147
1148         if (fifo_bytes_used < 128)
1149                 return  128 - fifo_bytes_used;
1150         else
1151                 return 0;
1152 }
1153
1154 static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
1155 {
1156         while (snd_hdsp_midi_input_available (hdsp, id))
1157                 snd_hdsp_midi_read_byte (hdsp, id);
1158 }
1159
1160 static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
1161 {
1162         unsigned long flags;
1163         int n_pending;
1164         int to_write;
1165         int i;
1166         unsigned char buf[128];
1167
1168         /* Output is not interrupt driven */
1169                 
1170         spin_lock_irqsave (&hmidi->lock, flags);
1171         if (hmidi->output) {
1172                 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1173                         if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1174                                 if (n_pending > (int)sizeof (buf))
1175                                         n_pending = sizeof (buf);
1176                                 
1177                                 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
1178                                         for (i = 0; i < to_write; ++i) 
1179                                                 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1180                                 }
1181                         }
1182                 }
1183         }
1184         spin_unlock_irqrestore (&hmidi->lock, flags);
1185         return 0;
1186 }
1187
1188 static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
1189 {
1190         unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1191         unsigned long flags;
1192         int n_pending;
1193         int i;
1194
1195         spin_lock_irqsave (&hmidi->lock, flags);
1196         if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1197                 if (hmidi->input) {
1198                         if (n_pending > (int)sizeof (buf))
1199                                 n_pending = sizeof (buf);
1200                         for (i = 0; i < n_pending; ++i)
1201                                 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1202                         if (n_pending)
1203                                 snd_rawmidi_receive (hmidi->input, buf, n_pending);
1204                 } else {
1205                         /* flush the MIDI input FIFO */
1206                         while (--n_pending)
1207                                 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1208                 }
1209         }
1210         hmidi->pending = 0;
1211         if (hmidi->id)
1212                 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1213         else
1214                 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
1215         hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1216         spin_unlock_irqrestore (&hmidi->lock, flags);
1217         return snd_hdsp_midi_output_write (hmidi);
1218 }
1219
1220 static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1221 {
1222         struct hdsp *hdsp;
1223         struct hdsp_midi *hmidi;
1224         unsigned long flags;
1225         u32 ie;
1226
1227         hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1228         hdsp = hmidi->hdsp;
1229         ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1230         spin_lock_irqsave (&hdsp->lock, flags);
1231         if (up) {
1232                 if (!(hdsp->control_register & ie)) {
1233                         snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1234                         hdsp->control_register |= ie;
1235                 }
1236         } else {
1237                 hdsp->control_register &= ~ie;
1238                 tasklet_kill(&hdsp->midi_tasklet);
1239         }
1240
1241         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1242         spin_unlock_irqrestore (&hdsp->lock, flags);
1243 }
1244
1245 static void snd_hdsp_midi_output_timer(unsigned long data)
1246 {
1247         struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
1248         unsigned long flags;
1249         
1250         snd_hdsp_midi_output_write(hmidi);
1251         spin_lock_irqsave (&hmidi->lock, flags);
1252
1253         /* this does not bump hmidi->istimer, because the
1254            kernel automatically removed the timer when it
1255            expired, and we are now adding it back, thus
1256            leaving istimer wherever it was set before.  
1257         */
1258
1259         if (hmidi->istimer) {
1260                 hmidi->timer.expires = 1 + jiffies;
1261                 add_timer(&hmidi->timer);
1262         }
1263
1264         spin_unlock_irqrestore (&hmidi->lock, flags);
1265 }
1266
1267 static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1268 {
1269         struct hdsp_midi *hmidi;
1270         unsigned long flags;
1271
1272         hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1273         spin_lock_irqsave (&hmidi->lock, flags);
1274         if (up) {
1275                 if (!hmidi->istimer) {
1276                         init_timer(&hmidi->timer);
1277                         hmidi->timer.function = snd_hdsp_midi_output_timer;
1278                         hmidi->timer.data = (unsigned long) hmidi;
1279                         hmidi->timer.expires = 1 + jiffies;
1280                         add_timer(&hmidi->timer);
1281                         hmidi->istimer++;
1282                 }
1283         } else {
1284                 if (hmidi->istimer && --hmidi->istimer <= 0)
1285                         del_timer (&hmidi->timer);
1286         }
1287         spin_unlock_irqrestore (&hmidi->lock, flags);
1288         if (up)
1289                 snd_hdsp_midi_output_write(hmidi);
1290 }
1291
1292 static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
1293 {
1294         struct hdsp_midi *hmidi;
1295
1296         hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1297         spin_lock_irq (&hmidi->lock);
1298         snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1299         hmidi->input = substream;
1300         spin_unlock_irq (&hmidi->lock);
1301
1302         return 0;
1303 }
1304
1305 static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
1306 {
1307         struct hdsp_midi *hmidi;
1308
1309         hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1310         spin_lock_irq (&hmidi->lock);
1311         hmidi->output = substream;
1312         spin_unlock_irq (&hmidi->lock);
1313
1314         return 0;
1315 }
1316
1317 static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
1318 {
1319         struct hdsp_midi *hmidi;
1320
1321         snd_hdsp_midi_input_trigger (substream, 0);
1322
1323         hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1324         spin_lock_irq (&hmidi->lock);
1325         hmidi->input = NULL;
1326         spin_unlock_irq (&hmidi->lock);
1327
1328         return 0;
1329 }
1330
1331 static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
1332 {
1333         struct hdsp_midi *hmidi;
1334
1335         snd_hdsp_midi_output_trigger (substream, 0);
1336
1337         hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1338         spin_lock_irq (&hmidi->lock);
1339         hmidi->output = NULL;
1340         spin_unlock_irq (&hmidi->lock);
1341
1342         return 0;
1343 }
1344
1345 static struct snd_rawmidi_ops snd_hdsp_midi_output =
1346 {
1347         .open =         snd_hdsp_midi_output_open,
1348         .close =        snd_hdsp_midi_output_close,
1349         .trigger =      snd_hdsp_midi_output_trigger,
1350 };
1351
1352 static struct snd_rawmidi_ops snd_hdsp_midi_input =
1353 {
1354         .open =         snd_hdsp_midi_input_open,
1355         .close =        snd_hdsp_midi_input_close,
1356         .trigger =      snd_hdsp_midi_input_trigger,
1357 };
1358
1359 static int __devinit snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
1360 {
1361         char buf[32];
1362
1363         hdsp->midi[id].id = id;
1364         hdsp->midi[id].rmidi = NULL;
1365         hdsp->midi[id].input = NULL;
1366         hdsp->midi[id].output = NULL;
1367         hdsp->midi[id].hdsp = hdsp;
1368         hdsp->midi[id].istimer = 0;
1369         hdsp->midi[id].pending = 0;
1370         spin_lock_init (&hdsp->midi[id].lock);
1371
1372         sprintf (buf, "%s MIDI %d", card->shortname, id+1);
1373         if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
1374                 return -1;
1375
1376         sprintf (hdsp->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
1377         hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1378
1379         snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1380         snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1381
1382         hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1383                 SNDRV_RAWMIDI_INFO_INPUT |
1384                 SNDRV_RAWMIDI_INFO_DUPLEX;
1385
1386         return 0;
1387 }
1388
1389 /*-----------------------------------------------------------------------------
1390   Control Interface
1391   ----------------------------------------------------------------------------*/
1392
1393 static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
1394 {
1395         u32 val = 0;
1396         val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1397         val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1398         if (val & HDSP_SPDIFProfessional)
1399                 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1400         else
1401                 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1402         return val;
1403 }
1404
1405 static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
1406 {
1407         aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1408                          ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1409         if (val & HDSP_SPDIFProfessional)
1410                 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1411         else
1412                 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1413 }
1414
1415 static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1416 {
1417         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1418         uinfo->count = 1;
1419         return 0;
1420 }
1421
1422 static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1423 {
1424         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1425         
1426         snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1427         return 0;
1428 }
1429
1430 static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1431 {
1432         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1433         int change;
1434         u32 val;
1435         
1436         val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1437         spin_lock_irq(&hdsp->lock);
1438         change = val != hdsp->creg_spdif;
1439         hdsp->creg_spdif = val;
1440         spin_unlock_irq(&hdsp->lock);
1441         return change;
1442 }
1443
1444 static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1445 {
1446         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1447         uinfo->count = 1;
1448         return 0;
1449 }
1450
1451 static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1452 {
1453         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1454         
1455         snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1456         return 0;
1457 }
1458
1459 static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1460 {
1461         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1462         int change;
1463         u32 val;
1464         
1465         val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1466         spin_lock_irq(&hdsp->lock);
1467         change = val != hdsp->creg_spdif_stream;
1468         hdsp->creg_spdif_stream = val;
1469         hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1470         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1471         spin_unlock_irq(&hdsp->lock);
1472         return change;
1473 }
1474
1475 static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1476 {
1477         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1478         uinfo->count = 1;
1479         return 0;
1480 }
1481
1482 static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1483 {
1484         ucontrol->value.iec958.status[0] = kcontrol->private_value;
1485         return 0;
1486 }
1487
1488 #define HDSP_SPDIF_IN(xname, xindex) \
1489 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1490   .name = xname, \
1491   .index = xindex, \
1492   .info = snd_hdsp_info_spdif_in, \
1493   .get = snd_hdsp_get_spdif_in, \
1494   .put = snd_hdsp_put_spdif_in }
1495
1496 static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
1497 {
1498         return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1499 }
1500
1501 static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
1502 {
1503         hdsp->control_register &= ~HDSP_SPDIFInputMask;
1504         hdsp->control_register |= hdsp_encode_spdif_in(in);
1505         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1506         return 0;
1507 }
1508
1509 static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1510 {
1511         static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
1512         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1513
1514         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1515         uinfo->count = 1;
1516         uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1517         if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1518                 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1519         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1520         return 0;
1521 }
1522
1523 static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1524 {
1525         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1526         
1527         ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1528         return 0;
1529 }
1530
1531 static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1532 {
1533         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1534         int change;
1535         unsigned int val;
1536         
1537         if (!snd_hdsp_use_is_exclusive(hdsp))
1538                 return -EBUSY;
1539         val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1540         spin_lock_irq(&hdsp->lock);
1541         change = val != hdsp_spdif_in(hdsp);
1542         if (change)
1543                 hdsp_set_spdif_input(hdsp, val);
1544         spin_unlock_irq(&hdsp->lock);
1545         return change;
1546 }
1547
1548 #define HDSP_SPDIF_OUT(xname, xindex) \
1549 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1550   .info = snd_hdsp_info_spdif_bits, \
1551   .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
1552
1553 static int hdsp_spdif_out(struct hdsp *hdsp)
1554 {
1555         return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
1556 }
1557
1558 static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
1559 {
1560         if (out)
1561                 hdsp->control_register |= HDSP_SPDIFOpticalOut;
1562         else
1563                 hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
1564         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1565         return 0;
1566 }
1567
1568 static int snd_hdsp_info_spdif_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1569 {
1570         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1571         uinfo->count = 1;
1572         uinfo->value.integer.min = 0;
1573         uinfo->value.integer.max = 1;
1574         return 0;
1575 }
1576
1577 static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1578 {
1579         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1580         
1581         ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
1582         return 0;
1583 }
1584
1585 static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1586 {
1587         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1588         int change;
1589         unsigned int val;
1590         
1591         if (!snd_hdsp_use_is_exclusive(hdsp))
1592                 return -EBUSY;
1593         val = ucontrol->value.integer.value[0] & 1;
1594         spin_lock_irq(&hdsp->lock);
1595         change = (int)val != hdsp_spdif_out(hdsp);
1596         hdsp_set_spdif_output(hdsp, val);
1597         spin_unlock_irq(&hdsp->lock);
1598         return change;
1599 }
1600
1601 #define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
1602 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1603   .info = snd_hdsp_info_spdif_bits, \
1604   .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
1605
1606 static int hdsp_spdif_professional(struct hdsp *hdsp)
1607 {
1608         return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
1609 }
1610
1611 static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
1612 {
1613         if (val)
1614                 hdsp->control_register |= HDSP_SPDIFProfessional;
1615         else
1616                 hdsp->control_register &= ~HDSP_SPDIFProfessional;
1617         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1618         return 0;
1619 }
1620
1621 static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1622 {
1623         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1624         
1625         ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
1626         return 0;
1627 }
1628
1629 static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1630 {
1631         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1632         int change;
1633         unsigned int val;
1634         
1635         if (!snd_hdsp_use_is_exclusive(hdsp))
1636                 return -EBUSY;
1637         val = ucontrol->value.integer.value[0] & 1;
1638         spin_lock_irq(&hdsp->lock);
1639         change = (int)val != hdsp_spdif_professional(hdsp);
1640         hdsp_set_spdif_professional(hdsp, val);
1641         spin_unlock_irq(&hdsp->lock);
1642         return change;
1643 }
1644
1645 #define HDSP_SPDIF_EMPHASIS(xname, xindex) \
1646 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1647   .info = snd_hdsp_info_spdif_bits, \
1648   .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
1649
1650 static int hdsp_spdif_emphasis(struct hdsp *hdsp)
1651 {
1652         return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
1653 }
1654
1655 static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
1656 {
1657         if (val)
1658                 hdsp->control_register |= HDSP_SPDIFEmphasis;
1659         else
1660                 hdsp->control_register &= ~HDSP_SPDIFEmphasis;
1661         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1662         return 0;
1663 }
1664
1665 static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1666 {
1667         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1668         
1669         ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
1670         return 0;
1671 }
1672
1673 static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1674 {
1675         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1676         int change;
1677         unsigned int val;
1678         
1679         if (!snd_hdsp_use_is_exclusive(hdsp))
1680                 return -EBUSY;
1681         val = ucontrol->value.integer.value[0] & 1;
1682         spin_lock_irq(&hdsp->lock);
1683         change = (int)val != hdsp_spdif_emphasis(hdsp);
1684         hdsp_set_spdif_emphasis(hdsp, val);
1685         spin_unlock_irq(&hdsp->lock);
1686         return change;
1687 }
1688
1689 #define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
1690 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1691   .info = snd_hdsp_info_spdif_bits, \
1692   .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
1693
1694 static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
1695 {
1696         return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
1697 }
1698
1699 static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
1700 {
1701         if (val)
1702                 hdsp->control_register |= HDSP_SPDIFNonAudio;
1703         else
1704                 hdsp->control_register &= ~HDSP_SPDIFNonAudio;
1705         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1706         return 0;
1707 }
1708
1709 static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1710 {
1711         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1712         
1713         ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
1714         return 0;
1715 }
1716
1717 static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1718 {
1719         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1720         int change;
1721         unsigned int val;
1722         
1723         if (!snd_hdsp_use_is_exclusive(hdsp))
1724                 return -EBUSY;
1725         val = ucontrol->value.integer.value[0] & 1;
1726         spin_lock_irq(&hdsp->lock);
1727         change = (int)val != hdsp_spdif_nonaudio(hdsp);
1728         hdsp_set_spdif_nonaudio(hdsp, val);
1729         spin_unlock_irq(&hdsp->lock);
1730         return change;
1731 }
1732
1733 #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
1734 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1735   .name = xname, \
1736   .index = xindex, \
1737   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1738   .info = snd_hdsp_info_spdif_sample_rate, \
1739   .get = snd_hdsp_get_spdif_sample_rate \
1740 }
1741
1742 static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1743 {
1744         static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
1745         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1746
1747         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1748         uinfo->count = 1;
1749         uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1750         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1751                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1752         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1753         return 0;
1754 }
1755
1756 static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1757 {
1758         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1759         
1760         switch (hdsp_spdif_sample_rate(hdsp)) {
1761         case 32000:
1762                 ucontrol->value.enumerated.item[0] = 0;
1763                 break;
1764         case 44100:
1765                 ucontrol->value.enumerated.item[0] = 1;
1766                 break;
1767         case 48000:
1768                 ucontrol->value.enumerated.item[0] = 2;
1769                 break;
1770         case 64000:
1771                 ucontrol->value.enumerated.item[0] = 3;
1772                 break;
1773         case 88200:
1774                 ucontrol->value.enumerated.item[0] = 4;
1775                 break;
1776         case 96000:
1777                 ucontrol->value.enumerated.item[0] = 5;
1778                 break;
1779         case 128000:
1780                 ucontrol->value.enumerated.item[0] = 7;
1781                 break;
1782         case 176400:
1783                 ucontrol->value.enumerated.item[0] = 8;
1784                 break;
1785         case 192000:
1786                 ucontrol->value.enumerated.item[0] = 9;
1787                 break;
1788         default:
1789                 ucontrol->value.enumerated.item[0] = 6;         
1790         }
1791         return 0;
1792 }
1793
1794 #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
1795 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1796   .name = xname, \
1797   .index = xindex, \
1798   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1799   .info = snd_hdsp_info_system_sample_rate, \
1800   .get = snd_hdsp_get_system_sample_rate \
1801 }
1802
1803 static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1804 {
1805         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1806         uinfo->count = 1;
1807         return 0;
1808 }
1809
1810 static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1811 {
1812         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1813         
1814         ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1815         return 0;
1816 }
1817
1818 #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
1819 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1820   .name = xname, \
1821   .index = xindex, \
1822   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1823   .info = snd_hdsp_info_autosync_sample_rate, \
1824   .get = snd_hdsp_get_autosync_sample_rate \
1825 }
1826
1827 static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1828 {
1829         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1830         static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};    
1831         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1832         uinfo->count = 1;
1833         uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1834         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1835                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1836         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1837         return 0;
1838 }
1839
1840 static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1841 {
1842         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1843         
1844         switch (hdsp_external_sample_rate(hdsp)) {
1845         case 32000:
1846                 ucontrol->value.enumerated.item[0] = 0;
1847                 break;
1848         case 44100:
1849                 ucontrol->value.enumerated.item[0] = 1;
1850                 break;
1851         case 48000:
1852                 ucontrol->value.enumerated.item[0] = 2;
1853                 break;
1854         case 64000:
1855                 ucontrol->value.enumerated.item[0] = 3;
1856                 break;
1857         case 88200:
1858                 ucontrol->value.enumerated.item[0] = 4;
1859                 break;
1860         case 96000:
1861                 ucontrol->value.enumerated.item[0] = 5;
1862                 break;
1863         case 128000:
1864                 ucontrol->value.enumerated.item[0] = 7;
1865                 break;
1866         case 176400:
1867                 ucontrol->value.enumerated.item[0] = 8;
1868                 break;
1869         case 192000:
1870                 ucontrol->value.enumerated.item[0] = 9;
1871                 break;  
1872         default:
1873                 ucontrol->value.enumerated.item[0] = 6;         
1874         }
1875         return 0;
1876 }
1877
1878 #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
1879 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1880   .name = xname, \
1881   .index = xindex, \
1882   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1883   .info = snd_hdsp_info_system_clock_mode, \
1884   .get = snd_hdsp_get_system_clock_mode \
1885 }
1886
1887 static int hdsp_system_clock_mode(struct hdsp *hdsp)
1888 {
1889         if (hdsp->control_register & HDSP_ClockModeMaster)
1890                 return 0;
1891         else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
1892                         return 0;
1893         return 1;
1894 }
1895
1896 static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1897 {
1898         static char *texts[] = {"Master", "Slave" };
1899         
1900         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1901         uinfo->count = 1;
1902         uinfo->value.enumerated.items = 2;
1903         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1904                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1905         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1906         return 0;
1907 }
1908
1909 static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1910 {
1911         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1912         
1913         ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1914         return 0;
1915 }
1916
1917 #define HDSP_CLOCK_SOURCE(xname, xindex) \
1918 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1919   .name = xname, \
1920   .index = xindex, \
1921   .info = snd_hdsp_info_clock_source, \
1922   .get = snd_hdsp_get_clock_source, \
1923   .put = snd_hdsp_put_clock_source \
1924 }
1925
1926 static int hdsp_clock_source(struct hdsp *hdsp)
1927 {
1928         if (hdsp->control_register & HDSP_ClockModeMaster) {
1929                 switch (hdsp->system_sample_rate) {
1930                 case 32000:
1931                         return 1;
1932                 case 44100:
1933                         return 2;
1934                 case 48000:
1935                         return 3;
1936                 case 64000:
1937                         return 4;
1938                 case 88200:
1939                         return 5;
1940                 case 96000:
1941                         return 6;
1942                 case 128000:
1943                         return 7;
1944                 case 176400:
1945                         return 8;
1946                 case 192000:
1947                         return 9;
1948                 default:
1949                         return 3;       
1950                 }
1951         } else {
1952                 return 0;
1953         }
1954 }
1955
1956 static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
1957 {
1958         int rate;
1959         switch (mode) {
1960         case HDSP_CLOCK_SOURCE_AUTOSYNC:
1961                 if (hdsp_external_sample_rate(hdsp) != 0) {
1962                     if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
1963                         hdsp->control_register &= ~HDSP_ClockModeMaster;                
1964                         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1965                         return 0;
1966                     }
1967                 }
1968                 return -1;
1969         case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
1970                 rate = 32000;
1971                 break;
1972         case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
1973                 rate = 44100;
1974                 break;      
1975         case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
1976                 rate = 48000;
1977                 break;
1978         case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
1979                 rate = 64000;
1980                 break;
1981         case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
1982                 rate = 88200;
1983                 break;
1984         case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
1985                 rate = 96000;
1986                 break;
1987         case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
1988                 rate = 128000;
1989                 break;
1990         case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
1991                 rate = 176400;
1992                 break;
1993         case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
1994                 rate = 192000;
1995                 break;
1996         default:
1997                 rate = 48000;
1998         }
1999         hdsp->control_register |= HDSP_ClockModeMaster;
2000         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2001         hdsp_set_rate(hdsp, rate, 1);
2002         return 0;
2003 }
2004
2005 static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2006 {
2007         static char *texts[] = {"AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz", "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz", "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz", "Internal 192.0 KHz" };
2008         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2009         
2010         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2011         uinfo->count = 1;
2012         if (hdsp->io_type == H9632)
2013             uinfo->value.enumerated.items = 10;
2014         else
2015             uinfo->value.enumerated.items = 7;  
2016         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2017                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2018         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2019         return 0;
2020 }
2021
2022 static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2023 {
2024         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2025         
2026         ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2027         return 0;
2028 }
2029
2030 static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2031 {
2032         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2033         int change;
2034         int val;
2035         
2036         if (!snd_hdsp_use_is_exclusive(hdsp))
2037                 return -EBUSY;
2038         val = ucontrol->value.enumerated.item[0];
2039         if (val < 0) val = 0;
2040         if (hdsp->io_type == H9632) {
2041                 if (val > 9)
2042                         val = 9;
2043         } else {
2044                 if (val > 6)
2045                         val = 6;
2046         }
2047         spin_lock_irq(&hdsp->lock);
2048         if (val != hdsp_clock_source(hdsp))
2049                 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2050         else
2051                 change = 0;
2052         spin_unlock_irq(&hdsp->lock);
2053         return change;
2054 }
2055
2056 static int snd_hdsp_info_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2057 {
2058         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2059         uinfo->count = 1;
2060         uinfo->value.integer.min = 0;
2061         uinfo->value.integer.max = 1;
2062         return 0;
2063 }
2064
2065 static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2066 {
2067         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2068         
2069         ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2070         return 0;
2071 }
2072
2073 static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2074 {
2075         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2076         int change;
2077
2078         change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2079         if (change)
2080                 hdsp->clock_source_locked = ucontrol->value.integer.value[0];
2081         return change;
2082 }
2083
2084 #define HDSP_DA_GAIN(xname, xindex) \
2085 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2086   .name = xname, \
2087   .index = xindex, \
2088   .info = snd_hdsp_info_da_gain, \
2089   .get = snd_hdsp_get_da_gain, \
2090   .put = snd_hdsp_put_da_gain \
2091 }
2092
2093 static int hdsp_da_gain(struct hdsp *hdsp)
2094 {
2095         switch (hdsp->control_register & HDSP_DAGainMask) {
2096         case HDSP_DAGainHighGain:
2097                 return 0;
2098         case HDSP_DAGainPlus4dBu:
2099                 return 1;
2100         case HDSP_DAGainMinus10dBV:
2101                 return 2;
2102         default:
2103                 return 1;       
2104         }
2105 }
2106
2107 static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
2108 {
2109         hdsp->control_register &= ~HDSP_DAGainMask;
2110         switch (mode) {
2111         case 0:
2112                 hdsp->control_register |= HDSP_DAGainHighGain;
2113                 break;
2114         case 1:
2115                 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2116                 break;
2117         case 2:
2118                 hdsp->control_register |= HDSP_DAGainMinus10dBV;                
2119                 break;      
2120         default:
2121                 return -1;
2122
2123         }
2124         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2125         return 0;
2126 }
2127
2128 static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2129 {
2130         static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
2131         
2132         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2133         uinfo->count = 1;
2134         uinfo->value.enumerated.items = 3;
2135         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2136                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2137         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2138         return 0;
2139 }
2140
2141 static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2142 {
2143         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2144         
2145         ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2146         return 0;
2147 }
2148
2149 static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2150 {
2151         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2152         int change;
2153         int val;
2154         
2155         if (!snd_hdsp_use_is_exclusive(hdsp))
2156                 return -EBUSY;
2157         val = ucontrol->value.enumerated.item[0];
2158         if (val < 0) val = 0;
2159         if (val > 2) val = 2;
2160         spin_lock_irq(&hdsp->lock);
2161         if (val != hdsp_da_gain(hdsp))
2162                 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2163         else
2164                 change = 0;
2165         spin_unlock_irq(&hdsp->lock);
2166         return change;
2167 }
2168
2169 #define HDSP_AD_GAIN(xname, xindex) \
2170 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2171   .name = xname, \
2172   .index = xindex, \
2173   .info = snd_hdsp_info_ad_gain, \
2174   .get = snd_hdsp_get_ad_gain, \
2175   .put = snd_hdsp_put_ad_gain \
2176 }
2177
2178 static int hdsp_ad_gain(struct hdsp *hdsp)
2179 {
2180         switch (hdsp->control_register & HDSP_ADGainMask) {
2181         case HDSP_ADGainMinus10dBV:
2182                 return 0;
2183         case HDSP_ADGainPlus4dBu:
2184                 return 1;
2185         case HDSP_ADGainLowGain:
2186                 return 2;
2187         default:
2188                 return 1;       
2189         }
2190 }
2191
2192 static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
2193 {
2194         hdsp->control_register &= ~HDSP_ADGainMask;
2195         switch (mode) {
2196         case 0:
2197                 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2198                 break;
2199         case 1:
2200                 hdsp->control_register |= HDSP_ADGainPlus4dBu;          
2201                 break;
2202         case 2:
2203                 hdsp->control_register |= HDSP_ADGainLowGain;           
2204                 break;      
2205         default:
2206                 return -1;
2207
2208         }
2209         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2210         return 0;
2211 }
2212
2213 static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2214 {
2215         static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
2216         
2217         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2218         uinfo->count = 1;
2219         uinfo->value.enumerated.items = 3;
2220         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2221                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2222         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2223         return 0;
2224 }
2225
2226 static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2227 {
2228         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2229         
2230         ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2231         return 0;
2232 }
2233
2234 static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2235 {
2236         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2237         int change;
2238         int val;
2239         
2240         if (!snd_hdsp_use_is_exclusive(hdsp))
2241                 return -EBUSY;
2242         val = ucontrol->value.enumerated.item[0];
2243         if (val < 0) val = 0;
2244         if (val > 2) val = 2;
2245         spin_lock_irq(&hdsp->lock);
2246         if (val != hdsp_ad_gain(hdsp))
2247                 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2248         else
2249                 change = 0;
2250         spin_unlock_irq(&hdsp->lock);
2251         return change;
2252 }
2253
2254 #define HDSP_PHONE_GAIN(xname, xindex) \
2255 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2256   .name = xname, \
2257   .index = xindex, \
2258   .info = snd_hdsp_info_phone_gain, \
2259   .get = snd_hdsp_get_phone_gain, \
2260   .put = snd_hdsp_put_phone_gain \
2261 }
2262
2263 static int hdsp_phone_gain(struct hdsp *hdsp)
2264 {
2265         switch (hdsp->control_register & HDSP_PhoneGainMask) {
2266         case HDSP_PhoneGain0dB:
2267                 return 0;
2268         case HDSP_PhoneGainMinus6dB:
2269                 return 1;
2270         case HDSP_PhoneGainMinus12dB:
2271                 return 2;
2272         default:
2273                 return 0;       
2274         }
2275 }
2276
2277 static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
2278 {
2279         hdsp->control_register &= ~HDSP_PhoneGainMask;
2280         switch (mode) {
2281         case 0:
2282                 hdsp->control_register |= HDSP_PhoneGain0dB;
2283                 break;
2284         case 1:
2285                 hdsp->control_register |= HDSP_PhoneGainMinus6dB;               
2286                 break;
2287         case 2:
2288                 hdsp->control_register |= HDSP_PhoneGainMinus12dB;              
2289                 break;      
2290         default:
2291                 return -1;
2292
2293         }
2294         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2295         return 0;
2296 }
2297
2298 static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2299 {
2300         static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
2301         
2302         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2303         uinfo->count = 1;
2304         uinfo->value.enumerated.items = 3;
2305         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2306                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2307         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2308         return 0;
2309 }
2310
2311 static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2312 {
2313         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2314         
2315         ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2316         return 0;
2317 }
2318
2319 static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2320 {
2321         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2322         int change;
2323         int val;
2324         
2325         if (!snd_hdsp_use_is_exclusive(hdsp))
2326                 return -EBUSY;
2327         val = ucontrol->value.enumerated.item[0];
2328         if (val < 0) val = 0;
2329         if (val > 2) val = 2;
2330         spin_lock_irq(&hdsp->lock);
2331         if (val != hdsp_phone_gain(hdsp))
2332                 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2333         else
2334                 change = 0;
2335         spin_unlock_irq(&hdsp->lock);
2336         return change;
2337 }
2338
2339 #define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
2340 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2341   .name = xname, \
2342   .index = xindex, \
2343   .info = snd_hdsp_info_xlr_breakout_cable, \
2344   .get = snd_hdsp_get_xlr_breakout_cable, \
2345   .put = snd_hdsp_put_xlr_breakout_cable \
2346 }
2347
2348 static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
2349 {
2350         if (hdsp->control_register & HDSP_XLRBreakoutCable)
2351                 return 1;
2352         return 0;
2353 }
2354
2355 static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
2356 {
2357         if (mode)
2358                 hdsp->control_register |= HDSP_XLRBreakoutCable;
2359         else
2360                 hdsp->control_register &= ~HDSP_XLRBreakoutCable;
2361         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2362         return 0;
2363 }
2364
2365 static int snd_hdsp_info_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2366 {
2367         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2368         uinfo->count = 1;
2369         uinfo->value.integer.min = 0;
2370         uinfo->value.integer.max = 1;
2371         return 0;
2372 }
2373
2374 static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2375 {
2376         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2377         
2378         ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
2379         return 0;
2380 }
2381
2382 static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2383 {
2384         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2385         int change;
2386         int val;
2387         
2388         if (!snd_hdsp_use_is_exclusive(hdsp))
2389                 return -EBUSY;
2390         val = ucontrol->value.integer.value[0] & 1;
2391         spin_lock_irq(&hdsp->lock);
2392         change = (int)val != hdsp_xlr_breakout_cable(hdsp);
2393         hdsp_set_xlr_breakout_cable(hdsp, val);
2394         spin_unlock_irq(&hdsp->lock);
2395         return change;
2396 }
2397
2398 /* (De)activates old RME Analog Extension Board
2399    These are connected to the internal ADAT connector
2400    Switching this on desactivates external ADAT
2401 */
2402 #define HDSP_AEB(xname, xindex) \
2403 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2404   .name = xname, \
2405   .index = xindex, \
2406   .info = snd_hdsp_info_aeb, \
2407   .get = snd_hdsp_get_aeb, \
2408   .put = snd_hdsp_put_aeb \
2409 }
2410
2411 static int hdsp_aeb(struct hdsp *hdsp)
2412 {
2413         if (hdsp->control_register & HDSP_AnalogExtensionBoard)
2414                 return 1;
2415         return 0;
2416 }
2417
2418 static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
2419 {
2420         if (mode)
2421                 hdsp->control_register |= HDSP_AnalogExtensionBoard;
2422         else
2423                 hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
2424         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2425         return 0;
2426 }
2427
2428 static int snd_hdsp_info_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2429 {
2430         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2431         uinfo->count = 1;
2432         uinfo->value.integer.min = 0;
2433         uinfo->value.integer.max = 1;
2434         return 0;
2435 }
2436
2437 static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2438 {
2439         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2440         
2441         ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
2442         return 0;
2443 }
2444
2445 static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2446 {
2447         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2448         int change;
2449         int val;
2450         
2451         if (!snd_hdsp_use_is_exclusive(hdsp))
2452                 return -EBUSY;
2453         val = ucontrol->value.integer.value[0] & 1;
2454         spin_lock_irq(&hdsp->lock);
2455         change = (int)val != hdsp_aeb(hdsp);
2456         hdsp_set_aeb(hdsp, val);
2457         spin_unlock_irq(&hdsp->lock);
2458         return change;
2459 }
2460
2461 #define HDSP_PREF_SYNC_REF(xname, xindex) \
2462 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2463   .name = xname, \
2464   .index = xindex, \
2465   .info = snd_hdsp_info_pref_sync_ref, \
2466   .get = snd_hdsp_get_pref_sync_ref, \
2467   .put = snd_hdsp_put_pref_sync_ref \
2468 }
2469
2470 static int hdsp_pref_sync_ref(struct hdsp *hdsp)
2471 {
2472         /* Notice that this looks at the requested sync source,
2473            not the one actually in use.
2474         */
2475
2476         switch (hdsp->control_register & HDSP_SyncRefMask) {
2477         case HDSP_SyncRef_ADAT1:
2478                 return HDSP_SYNC_FROM_ADAT1;
2479         case HDSP_SyncRef_ADAT2:
2480                 return HDSP_SYNC_FROM_ADAT2;
2481         case HDSP_SyncRef_ADAT3:
2482                 return HDSP_SYNC_FROM_ADAT3;
2483         case HDSP_SyncRef_SPDIF:
2484                 return HDSP_SYNC_FROM_SPDIF;
2485         case HDSP_SyncRef_WORD:
2486                 return HDSP_SYNC_FROM_WORD;
2487         case HDSP_SyncRef_ADAT_SYNC:
2488                 return HDSP_SYNC_FROM_ADAT_SYNC;
2489         default:
2490                 return HDSP_SYNC_FROM_WORD;
2491         }
2492         return 0;
2493 }
2494
2495 static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
2496 {
2497         hdsp->control_register &= ~HDSP_SyncRefMask;
2498         switch (pref) {
2499         case HDSP_SYNC_FROM_ADAT1:
2500                 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2501                 break;
2502         case HDSP_SYNC_FROM_ADAT2:
2503                 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2504                 break;
2505         case HDSP_SYNC_FROM_ADAT3:
2506                 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2507                 break;
2508         case HDSP_SYNC_FROM_SPDIF:
2509                 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2510                 break;
2511         case HDSP_SYNC_FROM_WORD:
2512                 hdsp->control_register |= HDSP_SyncRef_WORD;
2513                 break;
2514         case HDSP_SYNC_FROM_ADAT_SYNC:
2515                 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2516                 break;
2517         default:
2518                 return -1;
2519         }
2520         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2521         return 0;
2522 }
2523
2524 static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2525 {
2526         static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
2527         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2528         
2529         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2530         uinfo->count = 1;
2531
2532         switch (hdsp->io_type) {
2533         case Digiface:
2534         case H9652:
2535                 uinfo->value.enumerated.items = 6;
2536                 break;
2537         case Multiface:
2538                 uinfo->value.enumerated.items = 4;
2539                 break;
2540         case H9632:
2541                 uinfo->value.enumerated.items = 3;
2542                 break;
2543         default:
2544                 uinfo->value.enumerated.items = 0;
2545                 break;
2546         }
2547                 
2548         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2549                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2550         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2551         return 0;
2552 }
2553
2554 static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2555 {
2556         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2557         
2558         ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2559         return 0;
2560 }
2561
2562 static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2563 {
2564         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2565         int change, max;
2566         unsigned int val;
2567         
2568         if (!snd_hdsp_use_is_exclusive(hdsp))
2569                 return -EBUSY;
2570
2571         switch (hdsp->io_type) {
2572         case Digiface:
2573         case H9652:
2574                 max = 6;
2575                 break;
2576         case Multiface:
2577                 max = 4;
2578                 break;
2579         case H9632:
2580                 max = 3;
2581                 break;
2582         default:
2583                 return -EIO;
2584         }
2585
2586         val = ucontrol->value.enumerated.item[0] % max;
2587         spin_lock_irq(&hdsp->lock);
2588         change = (int)val != hdsp_pref_sync_ref(hdsp);
2589         hdsp_set_pref_sync_ref(hdsp, val);
2590         spin_unlock_irq(&hdsp->lock);
2591         return change;
2592 }
2593
2594 #define HDSP_AUTOSYNC_REF(xname, xindex) \
2595 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2596   .name = xname, \
2597   .index = xindex, \
2598   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2599   .info = snd_hdsp_info_autosync_ref, \
2600   .get = snd_hdsp_get_autosync_ref, \
2601 }
2602
2603 static int hdsp_autosync_ref(struct hdsp *hdsp)
2604 {
2605         /* This looks at the autosync selected sync reference */
2606         unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2607
2608         switch (status2 & HDSP_SelSyncRefMask) {
2609         case HDSP_SelSyncRef_WORD:
2610                 return HDSP_AUTOSYNC_FROM_WORD;
2611         case HDSP_SelSyncRef_ADAT_SYNC:
2612                 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2613         case HDSP_SelSyncRef_SPDIF:
2614                 return HDSP_AUTOSYNC_FROM_SPDIF;
2615         case HDSP_SelSyncRefMask:
2616                 return HDSP_AUTOSYNC_FROM_NONE; 
2617         case HDSP_SelSyncRef_ADAT1:
2618                 return HDSP_AUTOSYNC_FROM_ADAT1;
2619         case HDSP_SelSyncRef_ADAT2:
2620                 return HDSP_AUTOSYNC_FROM_ADAT2;
2621         case HDSP_SelSyncRef_ADAT3:
2622                 return HDSP_AUTOSYNC_FROM_ADAT3;
2623         default:
2624                 return HDSP_AUTOSYNC_FROM_WORD;
2625         }
2626         return 0;
2627 }
2628
2629 static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2630 {
2631         static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
2632         
2633         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2634         uinfo->count = 1;
2635         uinfo->value.enumerated.items = 7;
2636         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2637                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2638         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2639         return 0;
2640 }
2641
2642 static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2643 {
2644         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2645         
2646         ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2647         return 0;
2648 }
2649
2650 #define HDSP_LINE_OUT(xname, xindex) \
2651 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2652   .name = xname, \
2653   .index = xindex, \
2654   .info = snd_hdsp_info_line_out, \
2655   .get = snd_hdsp_get_line_out, \
2656   .put = snd_hdsp_put_line_out \
2657 }
2658
2659 static int hdsp_line_out(struct hdsp *hdsp)
2660 {
2661         return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
2662 }
2663
2664 static int hdsp_set_line_output(struct hdsp *hdsp, int out)
2665 {
2666         if (out)
2667                 hdsp->control_register |= HDSP_LineOut;
2668         else
2669                 hdsp->control_register &= ~HDSP_LineOut;
2670         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2671         return 0;
2672 }
2673
2674 static int snd_hdsp_info_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2675 {
2676         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2677         uinfo->count = 1;
2678         uinfo->value.integer.min = 0;
2679         uinfo->value.integer.max = 1;
2680         return 0;
2681 }
2682
2683 static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2684 {
2685         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2686         
2687         spin_lock_irq(&hdsp->lock);
2688         ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
2689         spin_unlock_irq(&hdsp->lock);
2690         return 0;
2691 }
2692
2693 static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2694 {
2695         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2696         int change;
2697         unsigned int val;
2698         
2699         if (!snd_hdsp_use_is_exclusive(hdsp))
2700                 return -EBUSY;
2701         val = ucontrol->value.integer.value[0] & 1;
2702         spin_lock_irq(&hdsp->lock);
2703         change = (int)val != hdsp_line_out(hdsp);
2704         hdsp_set_line_output(hdsp, val);
2705         spin_unlock_irq(&hdsp->lock);
2706         return change;
2707 }
2708
2709 #define HDSP_PRECISE_POINTER(xname, xindex) \
2710 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
2711   .name = xname, \
2712   .index = xindex, \
2713   .info = snd_hdsp_info_precise_pointer, \
2714   .get = snd_hdsp_get_precise_pointer, \
2715   .put = snd_hdsp_put_precise_pointer \
2716 }
2717
2718 static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
2719 {
2720         if (precise)
2721                 hdsp->precise_ptr = 1;
2722         else
2723                 hdsp->precise_ptr = 0;
2724         return 0;
2725 }
2726
2727 static int snd_hdsp_info_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2728 {
2729         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2730         uinfo->count = 1;
2731         uinfo->value.integer.min = 0;
2732         uinfo->value.integer.max = 1;
2733         return 0;
2734 }
2735
2736 static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2737 {
2738         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2739         
2740         spin_lock_irq(&hdsp->lock);
2741         ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2742         spin_unlock_irq(&hdsp->lock);
2743         return 0;
2744 }
2745
2746 static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2747 {
2748         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2749         int change;
2750         unsigned int val;
2751         
2752         if (!snd_hdsp_use_is_exclusive(hdsp))
2753                 return -EBUSY;
2754         val = ucontrol->value.integer.value[0] & 1;
2755         spin_lock_irq(&hdsp->lock);
2756         change = (int)val != hdsp->precise_ptr;
2757         hdsp_set_precise_pointer(hdsp, val);
2758         spin_unlock_irq(&hdsp->lock);
2759         return change;
2760 }
2761
2762 #define HDSP_USE_MIDI_TASKLET(xname, xindex) \
2763 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
2764   .name = xname, \
2765   .index = xindex, \
2766   .info = snd_hdsp_info_use_midi_tasklet, \
2767   .get = snd_hdsp_get_use_midi_tasklet, \
2768   .put = snd_hdsp_put_use_midi_tasklet \
2769 }
2770
2771 static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
2772 {
2773         if (use_tasklet)
2774                 hdsp->use_midi_tasklet = 1;
2775         else
2776                 hdsp->use_midi_tasklet = 0;
2777         return 0;
2778 }
2779
2780 static int snd_hdsp_info_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2781 {
2782         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2783         uinfo->count = 1;
2784         uinfo->value.integer.min = 0;
2785         uinfo->value.integer.max = 1;
2786         return 0;
2787 }
2788
2789 static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2790 {
2791         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2792         
2793         spin_lock_irq(&hdsp->lock);
2794         ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2795         spin_unlock_irq(&hdsp->lock);
2796         return 0;
2797 }
2798
2799 static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2800 {
2801         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2802         int change;
2803         unsigned int val;
2804         
2805         if (!snd_hdsp_use_is_exclusive(hdsp))
2806                 return -EBUSY;
2807         val = ucontrol->value.integer.value[0] & 1;
2808         spin_lock_irq(&hdsp->lock);
2809         change = (int)val != hdsp->use_midi_tasklet;
2810         hdsp_set_use_midi_tasklet(hdsp, val);
2811         spin_unlock_irq(&hdsp->lock);
2812         return change;
2813 }
2814
2815 #define HDSP_MIXER(xname, xindex) \
2816 { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2817   .name = xname, \
2818   .index = xindex, \
2819   .device = 0, \
2820   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2821                  SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2822   .info = snd_hdsp_info_mixer, \
2823   .get = snd_hdsp_get_mixer, \
2824   .put = snd_hdsp_put_mixer \
2825 }
2826
2827 static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2828 {
2829         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2830         uinfo->count = 3;
2831         uinfo->value.integer.min = 0;
2832         uinfo->value.integer.max = 65536;
2833         uinfo->value.integer.step = 1;
2834         return 0;
2835 }
2836
2837 static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2838 {
2839         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2840         int source;
2841         int destination;
2842         int addr;
2843
2844         source = ucontrol->value.integer.value[0];
2845         destination = ucontrol->value.integer.value[1];
2846         
2847         if (source >= hdsp->max_channels)
2848                 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2849         else
2850                 addr = hdsp_input_to_output_key(hdsp,source, destination);
2851         
2852         spin_lock_irq(&hdsp->lock);
2853         ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2854         spin_unlock_irq(&hdsp->lock);
2855         return 0;
2856 }
2857
2858 static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2859 {
2860         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2861         int change;
2862         int source;
2863         int destination;
2864         int gain;
2865         int addr;
2866
2867         if (!snd_hdsp_use_is_exclusive(hdsp))
2868                 return -EBUSY;
2869
2870         source = ucontrol->value.integer.value[0];
2871         destination = ucontrol->value.integer.value[1];
2872
2873         if (source >= hdsp->max_channels)
2874                 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2875         else
2876                 addr = hdsp_input_to_output_key(hdsp,source, destination);
2877
2878         gain = ucontrol->value.integer.value[2];
2879
2880         spin_lock_irq(&hdsp->lock);
2881         change = gain != hdsp_read_gain(hdsp, addr);
2882         if (change)
2883                 hdsp_write_gain(hdsp, addr, gain);
2884         spin_unlock_irq(&hdsp->lock);
2885         return change;
2886 }
2887
2888 #define HDSP_WC_SYNC_CHECK(xname, xindex) \
2889 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2890   .name = xname, \
2891   .index = xindex, \
2892   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2893   .info = snd_hdsp_info_sync_check, \
2894   .get = snd_hdsp_get_wc_sync_check \
2895 }
2896
2897 static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2898 {
2899         static char *texts[] = {"No Lock", "Lock", "Sync" };    
2900         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2901         uinfo->count = 1;
2902         uinfo->value.enumerated.items = 3;
2903         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2904                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2905         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2906         return 0;
2907 }
2908
2909 static int hdsp_wc_sync_check(struct hdsp *hdsp)
2910 {
2911         int status2 = hdsp_read(hdsp, HDSP_status2Register);
2912         if (status2 & HDSP_wc_lock) {
2913                 if (status2 & HDSP_wc_sync)
2914                         return 2;
2915                 else
2916                          return 1;
2917         } else
2918                 return 0;
2919         return 0;
2920 }
2921
2922 static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2923 {
2924         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2925
2926         ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2927         return 0;
2928 }
2929
2930 #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
2931 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2932   .name = xname, \
2933   .index = xindex, \
2934   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2935   .info = snd_hdsp_info_sync_check, \
2936   .get = snd_hdsp_get_spdif_sync_check \
2937 }
2938
2939 static int hdsp_spdif_sync_check(struct hdsp *hdsp)
2940 {
2941         int status = hdsp_read(hdsp, HDSP_statusRegister);
2942         if (status & HDSP_SPDIFErrorFlag)
2943                 return 0;
2944         else {  
2945                 if (status & HDSP_SPDIFSync)
2946                         return 2;
2947                 else
2948                         return 1;
2949         }
2950         return 0;
2951 }
2952
2953 static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2954 {
2955         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2956
2957         ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
2958         return 0;
2959 }
2960
2961 #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
2962 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2963   .name = xname, \
2964   .index = xindex, \
2965   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2966   .info = snd_hdsp_info_sync_check, \
2967   .get = snd_hdsp_get_adatsync_sync_check \
2968 }
2969
2970 static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
2971 {
2972         int status = hdsp_read(hdsp, HDSP_statusRegister);
2973         if (status & HDSP_TimecodeLock) {
2974                 if (status & HDSP_TimecodeSync)
2975                         return 2;
2976                 else
2977                         return 1;
2978         } else
2979                 return 0;
2980 }       
2981
2982 static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2983 {
2984         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2985
2986         ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
2987         return 0;
2988 }
2989
2990 #define HDSP_ADAT_SYNC_CHECK \
2991 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2992   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2993   .info = snd_hdsp_info_sync_check, \
2994   .get = snd_hdsp_get_adat_sync_check \
2995 }
2996
2997 static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
2998 {       
2999         int status = hdsp_read(hdsp, HDSP_statusRegister);
3000         
3001         if (status & (HDSP_Lock0>>idx)) {
3002                 if (status & (HDSP_Sync0>>idx))
3003                         return 2;
3004                 else
3005                         return 1;               
3006         } else
3007                 return 0;
3008
3009
3010 static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3011 {
3012         int offset;
3013         struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3014
3015         offset = ucontrol->id.index - 1;
3016         snd_assert(offset >= 0);
3017
3018         switch (hdsp->io_type) {
3019         case Digiface:
3020         case H9652:
3021                 if (offset >= 3)
3022                         return -EINVAL;
3023                 break;
3024         case Multiface:
3025         case H9632:
3026                 if (offset >= 1) 
3027                         return -EINVAL;
3028                 break;
3029         default:
3030                 return -EIO;
3031         }
3032
3033         ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
3034         return 0;
3035 }
3036
3037 static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
3038 HDSP_DA_GAIN("DA Gain", 0),
3039 HDSP_AD_GAIN("AD Gain", 0),
3040 HDSP_PHONE_GAIN("Phones Gain", 0),
3041 HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0)
3042 };
3043
3044 static struct snd_kcontrol_new snd_hdsp_controls[] = {
3045 {
3046         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
3047         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
3048         .info =         snd_hdsp_control_spdif_info,
3049         .get =          snd_hdsp_control_spdif_get,
3050         .put =          snd_hdsp_control_spdif_put,
3051 },
3052 {
3053         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
3054         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
3055         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
3056         .info =         snd_hdsp_control_spdif_stream_info,
3057         .get =          snd_hdsp_control_spdif_stream_get,
3058         .put =          snd_hdsp_control_spdif_stream_put,
3059 },
3060 {
3061         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
3062         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
3063         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
3064         .info =         snd_hdsp_control_spdif_mask_info,
3065         .get =          snd_hdsp_control_spdif_mask_get,
3066         .private_value = IEC958_AES0_NONAUDIO |
3067                          IEC958_AES0_PROFESSIONAL |
3068                          IEC958_AES0_CON_EMPHASIS,                                                                                            
3069 },
3070 {
3071         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
3072         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
3073         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
3074         .info =         snd_hdsp_control_spdif_mask_info,
3075         .get =          snd_hdsp_control_spdif_mask_get,
3076         .private_value = IEC958_AES0_NONAUDIO |
3077                          IEC958_AES0_PROFESSIONAL |
3078                          IEC958_AES0_PRO_EMPHASIS,
3079 },
3080 HDSP_MIXER("Mixer", 0),
3081 HDSP_SPDIF_IN("IEC958 Input Connector", 0),
3082 HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
3083 HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
3084 HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
3085 HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
3086 /* 'Sample Clock Source' complies with the alsa control naming scheme */ 
3087 HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
3088 {
3089         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3090         .name = "Sample Clock Source Locking",
3091         .info = snd_hdsp_info_clock_source_lock,
3092         .get = snd_hdsp_get_clock_source_lock,
3093         .put = snd_hdsp_put_clock_source_lock,
3094 },
3095 HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3096 HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3097 HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3098 HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3099 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3100 /* 'External Rate' complies with the alsa control naming scheme */
3101 HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3102 HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3103 HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3104 HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
3105 HDSP_LINE_OUT("Line Out", 0),
3106 HDSP_PRECISE_POINTER("Precise Pointer", 0),
3107 HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3108 };
3109
3110 static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
3111 static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
3112
3113 static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
3114 {
3115         unsigned int idx;
3116         int err;
3117         struct snd_kcontrol *kctl;
3118
3119         for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
3120                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
3121                         return err;
3122                 if (idx == 1)   /* IEC958 (S/PDIF) Stream */
3123                         hdsp->spdif_ctl = kctl;
3124         }
3125
3126         /* ADAT SyncCheck status */
3127         snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3128         snd_hdsp_adat_sync_check.index = 1;
3129         if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
3130                 return err;
3131         if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3132                 for (idx = 1; idx < 3; ++idx) {
3133                         snd_hdsp_adat_sync_check.index = idx+1;
3134                         if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
3135                                 return err;
3136                 }
3137         }
3138         
3139         /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3140         if (hdsp->io_type == H9632) {
3141                 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
3142                         if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
3143                                 return err;
3144                 }
3145         }
3146
3147         /* AEB control for H96xx card */
3148         if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
3149                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
3150                                 return err;
3151         }
3152
3153         return 0;
3154 }
3155
3156 /*------------------------------------------------------------
3157    /proc interface 
3158  ------------------------------------------------------------*/
3159
3160 static void
3161 snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3162 {
3163         struct hdsp *hdsp = (struct hdsp *) entry->private_data;
3164         unsigned int status;
3165         unsigned int status2;
3166         char *pref_sync_ref;
3167         char *autosync_ref;
3168         char *system_clock_mode;
3169         char *clock_source;
3170         int x;
3171
3172         if (hdsp_check_for_iobox (hdsp))
3173                 snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
3174                 return;
3175
3176         if (hdsp_check_for_firmware(hdsp, 0)) {
3177                 if (hdsp->state & HDSP_FirmwareCached) {
3178                         if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3179                                 snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
3180                                 return;
3181                         }
3182                 } else {
3183                         snd_iprintf(buffer, "No firmware loaded nor cached, please upload firmware.\n");
3184                         return;
3185                 }
3186         }
3187         
3188         status = hdsp_read(hdsp, HDSP_statusRegister);
3189         status2 = hdsp_read(hdsp, HDSP_status2Register);
3190
3191         snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
3192         snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3193                     hdsp->capture_buffer, hdsp->playback_buffer);
3194         snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3195                     hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3196         snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3197         snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
3198         snd_iprintf(buffer, "Status register: 0x%x\n", status);
3199         snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3200         snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3201         snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3202         snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3203         snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3204         snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3205         snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3206
3207         snd_iprintf(buffer, "\n");
3208
3209         x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3210
3211         snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3212         snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3213         snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3214         snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3215
3216         snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3217
3218         snd_iprintf(buffer, "\n");
3219
3220
3221         switch (hdsp_clock_source(hdsp)) {
3222         case HDSP_CLOCK_SOURCE_AUTOSYNC:
3223                 clock_source = "AutoSync";
3224                 break;
3225         case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3226                 clock_source = "Internal 32 kHz";
3227                 break;
3228         case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3229                 clock_source = "Internal 44.1 kHz";
3230                 break;
3231         case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3232                 clock_source = "Internal 48 kHz";
3233                 break;
3234         case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3235                 clock_source = "Internal 64 kHz";
3236                 break;
3237         case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3238                 clock_source = "Internal 88.2 kHz";
3239                 break;
3240         case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3241                 clock_source = "Internal 96 kHz";
3242                 break;
3243         case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3244                 clock_source = "Internal 128 kHz";
3245                 break;
3246         case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3247                 clock_source = "Internal 176.4 kHz";
3248                 break;
3249                 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3250                 clock_source = "Internal 192 kHz";
3251                 break;  
3252         default:
3253                 clock_source = "Error";         
3254         }
3255         snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
3256                         
3257         if (hdsp_system_clock_mode(hdsp))
3258                 system_clock_mode = "Slave";
3259         else
3260                 system_clock_mode = "Master";
3261         
3262         switch (hdsp_pref_sync_ref (hdsp)) {
3263         case HDSP_SYNC_FROM_WORD:
3264                 pref_sync_ref = "Word Clock";
3265                 break;
3266         case HDSP_SYNC_FROM_ADAT_SYNC:
3267                 pref_sync_ref = "ADAT Sync";
3268                 break;
3269         case HDSP_SYNC_FROM_SPDIF:
3270                 pref_sync_ref = "SPDIF";
3271                 break;
3272         case HDSP_SYNC_FROM_ADAT1:
3273                 pref_sync_ref = "ADAT1";
3274                 break;
3275         case HDSP_SYNC_FROM_ADAT2:
3276                 pref_sync_ref = "ADAT2";
3277                 break;
3278         case HDSP_SYNC_FROM_ADAT3:
3279                 pref_sync_ref = "ADAT3";
3280                 break;
3281         default:
3282                 pref_sync_ref = "Word Clock";
3283                 break;
3284         }
3285         snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
3286         
3287         switch (hdsp_autosync_ref (hdsp)) {
3288         case HDSP_AUTOSYNC_FROM_WORD:
3289                 autosync_ref = "Word Clock";
3290                 break;
3291         case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3292                 autosync_ref = "ADAT Sync";
3293                 break;
3294         case HDSP_AUTOSYNC_FROM_SPDIF:
3295                 autosync_ref = "SPDIF";
3296                 break;
3297         case HDSP_AUTOSYNC_FROM_NONE:
3298                 autosync_ref = "None";
3299                 break;  
3300         case HDSP_AUTOSYNC_FROM_ADAT1:
3301                 autosync_ref = "ADAT1";
3302                 break;
3303         case HDSP_AUTOSYNC_FROM_ADAT2:
3304                 autosync_ref = "ADAT2";
3305                 break;
3306         case HDSP_AUTOSYNC_FROM_ADAT3:
3307                 autosync_ref = "ADAT3";
3308                 break;
3309         default:
3310                 autosync_ref = "---";
3311                 break;
3312         }
3313         snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
3314         
3315         snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
3316         
3317         snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3318
3319         snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3320         snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
3321                 
3322         snd_iprintf(buffer, "\n");
3323
3324         switch (hdsp_spdif_in(hdsp)) {
3325         case HDSP_SPDIFIN_OPTICAL:
3326                 snd_iprintf(buffer, "IEC958 input: Optical\n");
3327                 break;
3328         case HDSP_SPDIFIN_COAXIAL:
3329                 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3330                 break;
3331         case HDSP_SPDIFIN_INTERNAL:
3332                 snd_iprintf(buffer, "IEC958 input: Internal\n");
3333                 break;
3334         case HDSP_SPDIFIN_AES:
3335                 snd_iprintf(buffer, "IEC958 input: AES\n");
3336                 break;
3337         default:
3338                 snd_iprintf(buffer, "IEC958 input: ???\n");
3339                 break;
3340         }
3341         
3342         if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3343                 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3344         else
3345                 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
3346
3347         if (hdsp->control_register & HDSP_SPDIFProfessional)
3348                 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3349         else
3350                 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3351
3352         if (hdsp->control_register & HDSP_SPDIFEmphasis)
3353                 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3354         else
3355                 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3356
3357         if (hdsp->control_register & HDSP_SPDIFNonAudio)
3358                 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3359         else
3360                 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3361         if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
3362                 snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
3363         else
3364                 snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
3365
3366         snd_iprintf(buffer, "\n");
3367
3368         /* Sync Check */
3369         x = status & HDSP_Sync0;
3370         if (status & HDSP_Lock0)
3371                 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3372         else
3373                 snd_iprintf(buffer, "ADAT1: No Lock\n");
3374
3375         switch (hdsp->io_type) {
3376         case Digiface:
3377         case H9652:
3378                 x = status & HDSP_Sync1;
3379                 if (status & HDSP_Lock1)
3380                         snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3381                 else
3382                         snd_iprintf(buffer, "ADAT2: No Lock\n");
3383                 x = status & HDSP_Sync2;
3384                 if (status & HDSP_Lock2)
3385                         snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3386                 else
3387                         snd_iprintf(buffer, "ADAT3: No Lock\n");
3388                 break;
3389         default:
3390                 /* relax */
3391                 break;
3392         }
3393
3394         x = status & HDSP_SPDIFSync;
3395         if (status & HDSP_SPDIFErrorFlag)
3396                 snd_iprintf (buffer, "SPDIF: No Lock\n");
3397         else
3398                 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
3399         
3400         x = status2 & HDSP_wc_sync;
3401         if (status2 & HDSP_wc_lock)
3402                 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3403         else
3404                 snd_iprintf (buffer, "Word Clock: No Lock\n");
3405         
3406         x = status & HDSP_TimecodeSync;
3407         if (status & HDSP_TimecodeLock)
3408                 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3409         else
3410                 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
3411
3412         snd_iprintf(buffer, "\n");
3413         
3414         /* Informations about H9632 specific controls */
3415         if (hdsp->io_type == H9632) {
3416                 char *tmp;
3417         
3418                 switch (hdsp_ad_gain(hdsp)) {
3419                 case 0:
3420                         tmp = "-10 dBV";
3421                         break;
3422                 case 1:
3423                         tmp = "+4 dBu";
3424                         break;
3425                 default:
3426                         tmp = "Lo Gain";
3427                         break;
3428                 }
3429                 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3430
3431                 switch (hdsp_da_gain(hdsp)) {
3432                 case 0:
3433                         tmp = "Hi Gain";
3434                         break;
3435                 case 1:
3436                         tmp = "+4 dBu";
3437                         break;
3438                 default:
3439                         tmp = "-10 dBV";
3440                         break;
3441                 }
3442                 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
3443                 
3444                 switch (hdsp_phone_gain(hdsp)) {
3445                 case 0:
3446                         tmp = "0 dB";
3447                         break;
3448                 case 1:
3449                         tmp = "-6 dB";
3450                         break;
3451                 default:
3452                         tmp = "-12 dB";
3453                         break;
3454                 }
3455                 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3456
3457                 snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no"); 
3458                 
3459                 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
3460                         snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3461                 else
3462                         snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
3463                 snd_iprintf(buffer, "\n");
3464         }
3465
3466 }
3467
3468 static void __devinit snd_hdsp_proc_init(struct hdsp *hdsp)
3469 {
3470         struct snd_info_entry *entry;
3471
3472         if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
3473                 snd_info_set_text_ops(entry, hdsp, 1024, snd_hdsp_proc_read);
3474 }
3475
3476 static void snd_hdsp_free_buffers(struct hdsp *hdsp)
3477 {
3478         snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3479         snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3480 }
3481
3482 static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
3483 {
3484         unsigned long pb_bus, cb_bus;
3485
3486         if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3487             snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3488                 if (hdsp->capture_dma_buf.area)
3489                         snd_dma_free_pages(&hdsp->capture_dma_buf);
3490                 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
3491                 return -ENOMEM;
3492         }
3493
3494         /* Align to bus-space 64K boundary */
3495
3496         cb_bus = (hdsp->capture_dma_buf.addr + 0xFFFF) & ~0xFFFFl;
3497         pb_bus = (hdsp->playback_dma_buf.addr + 0xFFFF) & ~0xFFFFl;
3498
3499         /* Tell the card where it is */
3500
3501         hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3502         hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3503
3504         hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3505         hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3506
3507         return 0;
3508 }
3509
3510 static int snd_hdsp_set_defaults(struct hdsp *hdsp)
3511 {
3512         unsigned int i;
3513
3514         /* ASSUMPTION: hdsp->lock is either held, or
3515            there is no need to hold it (e.g. during module
3516            initalization).
3517          */
3518
3519         /* set defaults:
3520
3521            SPDIF Input via Coax 
3522            Master clock mode
3523            maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3524                             which implies 2 4096 sample, 32Kbyte periods).
3525            Enable line out.                         
3526          */
3527
3528         hdsp->control_register = HDSP_ClockModeMaster | 
3529                                  HDSP_SPDIFInputCoaxial | 
3530                                  hdsp_encode_latency(7) | 
3531                                  HDSP_LineOut;
3532         
3533
3534         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3535
3536 #ifdef SNDRV_BIG_ENDIAN
3537         hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3538 #else
3539         hdsp->control2_register = 0;
3540 #endif
3541         if (hdsp->io_type == H9652)
3542                 snd_hdsp_9652_enable_mixer (hdsp);
3543         else
3544                 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
3545
3546         hdsp_reset_hw_pointer(hdsp);
3547         hdsp_compute_period_size(hdsp);
3548
3549         /* silence everything */
3550         
3551         for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
3552                 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
3553
3554         for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3555                 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
3556                         return -EIO;
3557         }
3558         
3559         /* H9632 specific defaults */
3560         if (hdsp->io_type == H9632) {
3561                 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3562                 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3563         }
3564
3565         /* set a default rate so that the channel map is set up.
3566          */
3567
3568         hdsp_set_rate(hdsp, 48000, 1);
3569
3570         return 0;
3571 }
3572
3573 static void hdsp_midi_tasklet(unsigned long arg)
3574 {
3575         struct hdsp *hdsp = (struct hdsp *)arg;
3576         
3577         if (hdsp->midi[0].pending)
3578                 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3579         if (hdsp->midi[1].pending)
3580                 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3581
3582
3583 static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3584 {
3585         struct hdsp *hdsp = (struct hdsp *) dev_id;
3586         unsigned int status;
3587         int audio;
3588         int midi0;
3589         int midi1;
3590         unsigned int midi0status;
3591         unsigned int midi1status;
3592         int schedule = 0;
3593         
3594         status = hdsp_read(hdsp, HDSP_statusRegister);
3595
3596         audio = status & HDSP_audioIRQPending;
3597         midi0 = status & HDSP_midi0IRQPending;
3598         midi1 = status & HDSP_midi1IRQPending;
3599
3600         if (!audio && !midi0 && !midi1)
3601                 return IRQ_NONE;
3602
3603         hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3604
3605         midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3606         midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
3607         
3608         if (audio) {
3609                 if (hdsp->capture_substream)
3610                         snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
3611                 
3612                 if (hdsp->playback_substream)
3613                         snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
3614         }
3615         
3616         if (midi0 && midi0status) {
3617                 if (hdsp->use_midi_tasklet) {
3618                         /* we disable interrupts for this input until processing is done */
3619                         hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3620                         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3621                         hdsp->midi[0].pending = 1;
3622                         schedule = 1;
3623                 } else {
3624                         snd_hdsp_midi_input_read (&hdsp->midi[0]);
3625                 }
3626         }
3627         if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
3628                 if (hdsp->use_midi_tasklet) {
3629                         /* we disable interrupts for this input until processing is done */
3630                         hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3631                         hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3632                         hdsp->midi[1].pending = 1;
3633                         schedule = 1;
3634                 } else {
3635                         snd_hdsp_midi_input_read (&hdsp->midi[1]);
3636                 }
3637         }
3638         if (hdsp->use_midi_tasklet && schedule)
3639                 tasklet_hi_schedule(&hdsp->midi_tasklet);
3640         return IRQ_HANDLED;
3641 }
3642
3643 static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
3644 {
3645         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3646         return hdsp_hw_pointer(hdsp);
3647 }
3648
3649 static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
3650                                              int stream,
3651                                              int channel)
3652
3653 {
3654         int mapped_channel;
3655
3656         snd_assert(channel >= 0 && channel < hdsp->max_channels, return NULL);
3657         
3658         if ((mapped_channel = hdsp->channel_map[channel]) < 0)
3659                 return NULL;
3660         
3661         if (stream == SNDRV_PCM_STREAM_CAPTURE)
3662                 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3663         else
3664                 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3665 }
3666
3667 static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
3668                                   snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3669 {
3670         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3671         char *channel_buf;
3672
3673         snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
3674
3675         channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3676         snd_assert(channel_buf != NULL, return -EIO);
3677         if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3678                 return -EFAULT;
3679         return count;
3680 }
3681
3682 static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
3683                                  snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3684 {
3685         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3686         char *channel_buf;
3687
3688         snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
3689
3690         channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3691         snd_assert(channel_buf != NULL, return -EIO);
3692         if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3693                 return -EFAULT;
3694         return count;
3695 }
3696
3697 static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
3698                                   snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
3699 {
3700         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3701         char *channel_buf;
3702
3703         channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3704         snd_assert(channel_buf != NULL, return -EIO);
3705         memset(channel_buf + pos * 4, 0, count * 4);
3706         return count;
3707 }
3708
3709 static int snd_hdsp_reset(struct snd_pcm_substream *substream)
3710 {
3711         struct snd_pcm_runtime *runtime = substream->runtime;
3712         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3713         struct snd_pcm_substream *other;
3714         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3715                 other = hdsp->capture_substream;
3716         else
3717                 other = hdsp->playback_substream;
3718         if (hdsp->running)
3719                 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3720         else
3721                 runtime->status->hw_ptr = 0;
3722         if (other) {
3723                 struct list_head *pos;
3724                 struct snd_pcm_substream *s;
3725                 struct snd_pcm_runtime *oruntime = other->runtime;
3726                 snd_pcm_group_for_each(pos, substream) {
3727                         s = snd_pcm_group_substream_entry(pos);
3728                         if (s == other) {
3729                                 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3730                                 break;
3731                         }
3732                 }
3733         }
3734         return 0;
3735 }
3736
3737 static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3738                                  struct snd_pcm_hw_params *params)
3739 {
3740         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3741         int err;
3742         pid_t this_pid;
3743         pid_t other_pid;
3744
3745         if (hdsp_check_for_iobox (hdsp))
3746                 return -EIO;
3747
3748         if (hdsp_check_for_firmware(hdsp, 1))
3749                 return -EIO;
3750
3751         spin_lock_irq(&hdsp->lock);
3752
3753         if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3754                 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
3755                 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
3756                 this_pid = hdsp->playback_pid;
3757                 other_pid = hdsp->capture_pid;
3758         } else {
3759                 this_pid = hdsp->capture_pid;
3760                 other_pid = hdsp->playback_pid;
3761         }
3762
3763         if ((other_pid > 0) && (this_pid != other_pid)) {
3764
3765                 /* The other stream is open, and not by the same
3766                    task as this one. Make sure that the parameters
3767                    that matter are the same.
3768                  */
3769
3770                 if (params_rate(params) != hdsp->system_sample_rate) {
3771                         spin_unlock_irq(&hdsp->lock);
3772                         _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3773                         return -EBUSY;
3774                 }
3775
3776                 if (params_period_size(params) != hdsp->period_bytes / 4) {
3777                         spin_unlock_irq(&hdsp->lock);
3778                         _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3779                         return -EBUSY;
3780                 }
3781
3782                 /* We're fine. */
3783
3784                 spin_unlock_irq(&hdsp->lock);
3785                 return 0;
3786
3787         } else {
3788                 spin_unlock_irq(&hdsp->lock);
3789         }
3790
3791         /* how to make sure that the rate matches an externally-set one ?
3792          */
3793
3794         spin_lock_irq(&hdsp->lock);
3795         if (! hdsp->clock_source_locked) {
3796                 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
3797                         spin_unlock_irq(&hdsp->lock);
3798                         _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3799                         return err;
3800                 }
3801         }
3802         spin_unlock_irq(&hdsp->lock);
3803
3804         if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
3805                 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3806                 return err;
3807         }
3808
3809         return 0;
3810 }
3811
3812 static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
3813                                     struct snd_pcm_channel_info *info)
3814 {
3815         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3816         int mapped_channel;
3817
3818         snd_assert(info->channel < hdsp->max_channels, return -EINVAL);
3819
3820         if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
3821                 return -EINVAL;
3822
3823         info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
3824         info->first = 0;
3825         info->step = 32;
3826         return 0;
3827 }
3828
3829 static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
3830                              unsigned int cmd, void *arg)
3831 {
3832         switch (cmd) {
3833         case SNDRV_PCM_IOCTL1_RESET:
3834                 return snd_hdsp_reset(substream);
3835         case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
3836                 return snd_hdsp_channel_info(substream, arg);
3837         default:
3838                 break;
3839         }
3840
3841         return snd_pcm_lib_ioctl(substream, cmd, arg);
3842 }
3843
3844 static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
3845 {
3846         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3847         struct snd_pcm_substream *other;
3848         int running;
3849         
3850         if (hdsp_check_for_iobox (hdsp))
3851                 return -EIO;
3852
3853         if (hdsp_check_for_firmware(hdsp, 1))
3854                 return -EIO;
3855
3856         spin_lock(&hdsp->lock);
3857         running = hdsp->running;
3858         switch (cmd) {
3859         case SNDRV_PCM_TRIGGER_START:
3860                 running |= 1 << substream->stream;
3861                 break;
3862         case SNDRV_PCM_TRIGGER_STOP:
3863                 running &= ~(1 << substream->stream);
3864                 break;
3865         default:
3866                 snd_BUG();
3867                 spin_unlock(&hdsp->lock);
3868                 return -EINVAL;
3869         }
3870         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3871                 other = hdsp->capture_substream;
3872         else
3873                 other = hdsp->playback_substream;
3874
3875         if (other) {
3876                 struct list_head *pos;
3877                 struct snd_pcm_substream *s;
3878                 snd_pcm_group_for_each(pos, substream) {
3879                         s = snd_pcm_group_substream_entry(pos);
3880                         if (s == other) {
3881                                 snd_pcm_trigger_done(s, substream);
3882                                 if (cmd == SNDRV_PCM_TRIGGER_START)
3883                                         running |= 1 << s->stream;
3884                                 else
3885                                         running &= ~(1 << s->stream);
3886                                 goto _ok;
3887                         }
3888                 }
3889                 if (cmd == SNDRV_PCM_TRIGGER_START) {
3890                         if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
3891                             substream->stream == SNDRV_PCM_STREAM_CAPTURE)
3892                                 hdsp_silence_playback(hdsp);
3893                 } else {
3894                         if (running &&
3895                             substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3896                                 hdsp_silence_playback(hdsp);
3897                 }
3898         } else {
3899                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
3900                                 hdsp_silence_playback(hdsp);
3901         }
3902  _ok:
3903         snd_pcm_trigger_done(substream, substream);
3904         if (!hdsp->running && running)
3905                 hdsp_start_audio(hdsp);
3906         else if (hdsp->running && !running)
3907                 hdsp_stop_audio(hdsp);
3908         hdsp->running = running;
3909         spin_unlock(&hdsp->lock);
3910
3911         return 0;
3912 }
3913
3914 static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
3915 {
3916         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3917         int result = 0;
3918
3919         if (hdsp_check_for_iobox (hdsp))
3920                 return -EIO;
3921
3922         if (hdsp_check_for_firmware(hdsp, 1))
3923                 return -EIO;
3924
3925         spin_lock_irq(&hdsp->lock);
3926         if (!hdsp->running)
3927                 hdsp_reset_hw_pointer(hdsp);
3928         spin_unlock_irq(&hdsp->lock);
3929         return result;
3930 }
3931
3932 static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
3933 {
3934         .info =                 (SNDRV_PCM_INFO_MMAP |
3935                                  SNDRV_PCM_INFO_MMAP_VALID |
3936                                  SNDRV_PCM_INFO_NONINTERLEAVED |
3937                                  SNDRV_PCM_INFO_SYNC_START |
3938                                  SNDRV_PCM_INFO_DOUBLE),
3939 #ifdef SNDRV_BIG_ENDIAN
3940         .formats =              SNDRV_PCM_FMTBIT_S32_BE,
3941 #else
3942         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
3943 #endif
3944         .rates =                (SNDRV_PCM_RATE_32000 |
3945                                  SNDRV_PCM_RATE_44100 | 
3946                                  SNDRV_PCM_RATE_48000 | 
3947                                  SNDRV_PCM_RATE_64000 | 
3948                                  SNDRV_PCM_RATE_88200 | 
3949                                  SNDRV_PCM_RATE_96000),
3950         .rate_min =             32000,
3951         .rate_max =             96000,
3952         .channels_min =         14,
3953         .channels_max =         HDSP_MAX_CHANNELS,
3954         .buffer_bytes_max =     HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
3955         .period_bytes_min =     (64 * 4) * 10,
3956         .period_bytes_max =     (8192 * 4) * HDSP_MAX_CHANNELS,
3957         .periods_min =          2,
3958         .periods_max =          2,
3959         .fifo_size =            0
3960 };
3961
3962 static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
3963 {
3964         .info =                 (SNDRV_PCM_INFO_MMAP |
3965                                  SNDRV_PCM_INFO_MMAP_VALID |
3966                                  SNDRV_PCM_INFO_NONINTERLEAVED |
3967                                  SNDRV_PCM_INFO_SYNC_START),
3968 #ifdef SNDRV_BIG_ENDIAN
3969         .formats =              SNDRV_PCM_FMTBIT_S32_BE,
3970 #else
3971         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
3972 #endif
3973         .rates =                (SNDRV_PCM_RATE_32000 |
3974                                  SNDRV_PCM_RATE_44100 | 
3975                                  SNDRV_PCM_RATE_48000 | 
3976                                  SNDRV_PCM_RATE_64000 | 
3977                                  SNDRV_PCM_RATE_88200 | 
3978                                  SNDRV_PCM_RATE_96000),
3979         .rate_min =             32000,
3980         .rate_max =             96000,
3981         .channels_min =         14,
3982         .channels_max =         HDSP_MAX_CHANNELS,
3983         .buffer_bytes_max =     HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
3984         .period_bytes_min =     (64 * 4) * 10,
3985         .period_bytes_max =     (8192 * 4) * HDSP_MAX_CHANNELS,
3986         .periods_min =          2,
3987         .periods_max =          2,
3988         .fifo_size =            0
3989 };
3990
3991 static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
3992
3993 static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
3994         .count = ARRAY_SIZE(hdsp_period_sizes),
3995         .list = hdsp_period_sizes,
3996         .mask = 0
3997 };
3998
3999 static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4000
4001 static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
4002         .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4003         .list = hdsp_9632_sample_rates,
4004         .mask = 0
4005 };
4006
4007 static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4008                                         struct snd_pcm_hw_rule *rule)
4009 {
4010         struct hdsp *hdsp = rule->private;
4011         struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4012         if (hdsp->io_type == H9632) {
4013                 unsigned int list[3];
4014                 list[0] = hdsp->qs_in_channels;
4015                 list[1] = hdsp->ds_in_channels;
4016                 list[2] = hdsp->ss_in_channels;
4017                 return snd_interval_list(c, 3, list, 0);
4018         } else {
4019                 unsigned int list[2];
4020                 list[0] = hdsp->ds_in_channels;
4021                 list[1] = hdsp->ss_in_channels;
4022                 return snd_interval_list(c, 2, list, 0);
4023         }
4024 }
4025
4026 static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4027                                         struct snd_pcm_hw_rule *rule)
4028 {
4029         unsigned int list[3];
4030         struct hdsp *hdsp = rule->private;
4031         struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4032         if (hdsp->io_type == H9632) {
4033                 list[0] = hdsp->qs_out_channels;
4034                 list[1] = hdsp->ds_out_channels;
4035                 list[2] = hdsp->ss_out_channels;
4036                 return snd_interval_list(c, 3, list, 0);
4037         } else {
4038                 list[0] = hdsp->ds_out_channels;
4039                 list[1] = hdsp->ss_out_channels;
4040         }
4041         return snd_interval_list(c, 2, list, 0);
4042 }
4043
4044 static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4045                                              struct snd_pcm_hw_rule *rule)
4046 {
4047         struct hdsp *hdsp = rule->private;
4048         struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4049         struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4050         if (r->min > 96000 && hdsp->io_type == H9632) {
4051                 struct snd_interval t = {
4052                         .min = hdsp->qs_in_channels,
4053                         .max = hdsp->qs_in_channels,
4054                         .integer = 1,
4055                 };
4056                 return snd_interval_refine(c, &t);      
4057         } else if (r->min > 48000 && r->max <= 96000) {
4058                 struct snd_interval t = {
4059                         .min = hdsp->ds_in_channels,
4060                         .max = hdsp->ds_in_channels,
4061                         .integer = 1,
4062                 };
4063                 return snd_interval_refine(c, &t);
4064         } else if (r->max < 64000) {
4065                 struct snd_interval t = {
4066                         .min = hdsp->ss_in_channels,
4067                         .max = hdsp->ss_in_channels,
4068                         .integer = 1,
4069                 };
4070                 return snd_interval_refine(c, &t);
4071         }
4072         return 0;
4073 }
4074
4075 static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4076                                              struct snd_pcm_hw_rule *rule)
4077 {
4078         struct hdsp *hdsp = rule->private;
4079         struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4080         struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4081         if (r->min > 96000 && hdsp->io_type == H9632) {
4082                 struct snd_interval t = {
4083                         .min = hdsp->qs_out_channels,
4084                         .max = hdsp->qs_out_channels,
4085                         .integer = 1,
4086                 };
4087                 return snd_interval_refine(c, &t);      
4088         } else if (r->min > 48000 && r->max <= 96000) {
4089                 struct snd_interval t = {
4090                         .min = hdsp->ds_out_channels,
4091                         .max = hdsp->ds_out_channels,
4092                         .integer = 1,
4093                 };
4094                 return snd_interval_refine(c, &t);
4095         } else if (r->max < 64000) {
4096                 struct snd_interval t = {
4097                         .min = hdsp->ss_out_channels,
4098                         .max = hdsp->ss_out_channels,
4099                         .integer = 1,
4100                 };
4101                 return snd_interval_refine(c, &t);
4102         }
4103         return 0;
4104 }
4105
4106 static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4107                                              struct snd_pcm_hw_rule *rule)
4108 {
4109         struct hdsp *hdsp = rule->private;
4110         struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4111         struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4112         if (c->min >= hdsp->ss_out_channels) {
4113                 struct snd_interval t = {
4114                         .min = 32000,
4115                         .max = 48000,
4116                         .integer = 1,
4117                 };
4118                 return snd_interval_refine(r, &t);
4119         } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
4120                 struct snd_interval t = {
4121                         .min = 128000,
4122                         .max = 192000,
4123                         .integer = 1,
4124                 };
4125                 return snd_interval_refine(r, &t);
4126         } else if (c->max <= hdsp->ds_out_channels) {
4127                 struct snd_interval t = {
4128                         .min = 64000,
4129                         .max = 96000,
4130                         .integer = 1,
4131                 };
4132                 return snd_interval_refine(r, &t);
4133         }
4134         return 0;
4135 }
4136
4137 static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4138                                              struct snd_pcm_hw_rule *rule)
4139 {
4140         struct hdsp *hdsp = rule->private;
4141         struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4142         struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4143         if (c->min >= hdsp->ss_in_channels) {
4144                 struct snd_interval t = {
4145                         .min = 32000,
4146                         .max = 48000,
4147                         .integer = 1,
4148                 };
4149                 return snd_interval_refine(r, &t);
4150         } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
4151                 struct snd_interval t = {
4152                         .min = 128000,
4153                         .max = 192000,
4154                         .integer = 1,
4155                 };
4156                 return snd_interval_refine(r, &t);
4157         } else if (c->max <= hdsp->ds_in_channels) {
4158                 struct snd_interval t = {
4159                         .min = 64000,
4160                         .max = 96000,
4161                         .integer = 1,
4162                 };
4163                 return snd_interval_refine(r, &t);
4164         }
4165         return 0;
4166 }
4167
4168 static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
4169 {
4170         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4171         struct snd_pcm_runtime *runtime = substream->runtime;
4172
4173         if (hdsp_check_for_iobox (hdsp))
4174                 return -EIO;
4175
4176         if (hdsp_check_for_firmware(hdsp, 1))
4177                 return -EIO;
4178
4179         spin_lock_irq(&hdsp->lock);
4180
4181         snd_pcm_set_sync(substream);
4182
4183         runtime->hw = snd_hdsp_playback_subinfo;
4184         runtime->dma_area = hdsp->playback_buffer;
4185         runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4186
4187         hdsp->playback_pid = current->pid;
4188         hdsp->playback_substream = substream;
4189
4190         spin_unlock_irq(&hdsp->lock);
4191
4192         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4193         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4194         if (hdsp->clock_source_locked) {
4195                 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4196         } else if (hdsp->io_type == H9632) {
4197                 runtime->hw.rate_max = 192000;
4198                 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4199                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4200         }
4201         if (hdsp->io_type == H9632) {
4202                 runtime->hw.channels_min = hdsp->qs_out_channels;
4203                 runtime->hw.channels_max = hdsp->ss_out_channels;
4204         }       
4205         
4206         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4207                              snd_hdsp_hw_rule_out_channels, hdsp,
4208                              SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4209         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4210                              snd_hdsp_hw_rule_out_channels_rate, hdsp,
4211                              SNDRV_PCM_HW_PARAM_RATE, -1);
4212         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4213                              snd_hdsp_hw_rule_rate_out_channels, hdsp,
4214                              SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4215
4216         hdsp->creg_spdif_stream = hdsp->creg_spdif;
4217         hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4218         snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4219                        SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4220         return 0;
4221 }
4222
4223 static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
4224 {
4225         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4226
4227         spin_lock_irq(&hdsp->lock);
4228
4229         hdsp->playback_pid = -1;
4230         hdsp->playback_substream = NULL;
4231
4232         spin_unlock_irq(&hdsp->lock);
4233
4234         hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4235         snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4236                        SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4237         return 0;
4238 }
4239
4240
4241 static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
4242 {
4243         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4244         struct snd_pcm_runtime *runtime = substream->runtime;
4245
4246         if (hdsp_check_for_iobox (hdsp))
4247                 return -EIO;
4248
4249         if (hdsp_check_for_firmware(hdsp, 1))
4250                 return -EIO;
4251
4252         spin_lock_irq(&hdsp->lock);
4253
4254         snd_pcm_set_sync(substream);
4255
4256         runtime->hw = snd_hdsp_capture_subinfo;
4257         runtime->dma_area = hdsp->capture_buffer;
4258         runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4259
4260         hdsp->capture_pid = current->pid;
4261         hdsp->capture_substream = substream;
4262
4263         spin_unlock_irq(&hdsp->lock);
4264
4265         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4266         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4267         if (hdsp->io_type == H9632) {
4268                 runtime->hw.channels_min = hdsp->qs_in_channels;
4269                 runtime->hw.channels_max = hdsp->ss_in_channels;
4270                 runtime->hw.rate_max = 192000;
4271                 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4272                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4273         }
4274         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4275                              snd_hdsp_hw_rule_in_channels, hdsp,
4276                              SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4277         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4278                              snd_hdsp_hw_rule_in_channels_rate, hdsp,
4279                              SNDRV_PCM_HW_PARAM_RATE, -1);
4280         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4281                              snd_hdsp_hw_rule_rate_in_channels, hdsp,
4282                              SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4283         return 0;
4284 }
4285
4286 static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
4287 {
4288         struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4289
4290         spin_lock_irq(&hdsp->lock);
4291
4292         hdsp->capture_pid = -1;
4293         hdsp->capture_substream = NULL;
4294
4295         spin_unlock_irq(&hdsp->lock);
4296         return 0;
4297 }
4298
4299 static int snd_hdsp_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
4300 {
4301         /* we have nothing to initialize but the call is required */
4302         return 0;
4303 }
4304
4305
4306 /* helper functions for copying meter values */
4307 static inline int copy_u32_le(void __user *dest, void __iomem *src)
4308 {
4309         u32 val = readl(src);
4310         return copy_to_user(dest, &val, 4);
4311 }
4312
4313 static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4314 {
4315         u32 rms_low, rms_high;
4316         u64 rms;
4317         rms_low = readl(src_low);
4318         rms_high = readl(src_high);
4319         rms = ((u64)rms_high << 32) | rms_low;
4320         return copy_to_user(dest, &rms, 8);
4321 }
4322
4323 static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4324 {
4325         u32 rms_low, rms_high;
4326         u64 rms;
4327         rms_low = readl(src_low) & 0xffffff00;
4328         rms_high = readl(src_high) & 0xffffff00;
4329         rms = ((u64)rms_high << 32) | rms_low;
4330         return copy_to_user(dest, &rms, 8);
4331 }
4332
4333 static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
4334 {
4335         int doublespeed = 0;
4336         int i, j, channels, ofs;
4337
4338         if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4339                 doublespeed = 1;
4340         channels = doublespeed ? 14 : 26;
4341         for (i = 0, j = 0; i < 26; ++i) {
4342                 if (doublespeed && (i & 4))
4343                         continue;
4344                 ofs = HDSP_9652_peakBase - j * 4;
4345                 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4346                         return -EFAULT;
4347                 ofs -= channels * 4;
4348                 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4349                         return -EFAULT;
4350                 ofs -= channels * 4;
4351                 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4352                         return -EFAULT;
4353                 ofs = HDSP_9652_rmsBase + j * 8;
4354                 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4355                                 hdsp->iobase + ofs + 4))
4356                         return -EFAULT;
4357                 ofs += channels * 8;
4358                 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4359                                 hdsp->iobase + ofs + 4))
4360                         return -EFAULT;
4361                 ofs += channels * 8;
4362                 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4363                                 hdsp->iobase + ofs + 4))
4364                         return -EFAULT;
4365                 j++;
4366         }
4367         return 0;
4368 }
4369
4370 static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
4371 {
4372         int i, j;
4373         struct hdsp_9632_meters __iomem *m;
4374         int doublespeed = 0;
4375
4376         if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4377                 doublespeed = 1;
4378         m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
4379         for (i = 0, j = 0; i < 16; ++i, ++j) {
4380                 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4381                         return -EFAULT;
4382                 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4383                         return -EFAULT;
4384                 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4385                         return -EFAULT;
4386                 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4387                                 &m->input_rms_high[j]))
4388                         return -EFAULT;
4389                 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4390                                 &m->playback_rms_high[j]))
4391                         return -EFAULT;
4392                 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4393                                 &m->output_rms_high[j]))
4394                         return -EFAULT;
4395                 if (doublespeed && i == 3) i += 4;
4396         }
4397         return 0;
4398 }
4399
4400 static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
4401 {
4402         int i;
4403
4404         for (i = 0; i < 26; i++) {
4405                 if (copy_u32_le(&peak_rms->playback_peaks[i],
4406                                 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4407                         return -EFAULT;
4408                 if (copy_u32_le(&peak_rms->input_peaks[i],
4409                                 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4410                         return -EFAULT;
4411         }
4412         for (i = 0; i < 28; i++) {
4413                 if (copy_u32_le(&peak_rms->output_peaks[i],
4414                                 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4415                         return -EFAULT;
4416         }
4417         for (i = 0; i < 26; ++i) {
4418                 if (copy_u64_le(&peak_rms->playback_rms[i],
4419                                 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4420                                 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4421                         return -EFAULT;
4422                 if (copy_u64_le(&peak_rms->input_rms[i], 
4423                                 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4424                                 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4425                         return -EFAULT;
4426         }
4427         return 0;
4428 }
4429
4430 static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
4431 {
4432         struct hdsp *hdsp = (struct hdsp *)hw->private_data;    
4433         void __user *argp = (void __user *)arg;
4434
4435         switch (cmd) {
4436         case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
4437                 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
4438
4439                 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4440                         snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
4441                         return -EINVAL;
4442                 }
4443
4444                 switch (hdsp->io_type) {
4445                 case H9652:
4446                         return hdsp_9652_get_peak(hdsp, peak_rms);
4447                 case H9632:
4448                         return hdsp_9632_get_peak(hdsp, peak_rms);
4449                 default:
4450                         return hdsp_get_peak(hdsp, peak_rms);
4451                 }
4452         }
4453         case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
4454                 struct hdsp_config_info info;
4455                 unsigned long flags;
4456                 int i;
4457                 
4458                 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4459                         snd_printk(KERN_ERR "Hammerfall-DSP: Firmware needs to be uploaded to the card.\n");    
4460                         return -EINVAL;
4461                 }
4462                 spin_lock_irqsave(&hdsp->lock, flags);
4463                 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4464                 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4465                 if (hdsp->io_type != H9632)
4466                     info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
4467                 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
4468                 for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
4469                         info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
4470                 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
4471                 info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
4472                 info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
4473                 info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
4474                 info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
4475                 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4476                 info.system_sample_rate = hdsp->system_sample_rate;
4477                 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4478                 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4479                 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4480                 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
4481                 info.line_out = (unsigned char)hdsp_line_out(hdsp);
4482                 if (hdsp->io_type == H9632) {
4483                         info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4484                         info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4485                         info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
4486                         info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
4487                 
4488                 }
4489                 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
4490                         info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
4491                 spin_unlock_irqrestore(&hdsp->lock, flags);
4492                 if (copy_to_user(argp, &info, sizeof(info)))
4493                         return -EFAULT;
4494                 break;
4495         }
4496         case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
4497                 struct hdsp_9632_aeb h9632_aeb;
4498                 
4499                 if (hdsp->io_type != H9632) return -EINVAL;
4500                 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4501                 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4502                 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4503                         return -EFAULT;
4504                 break;
4505         }
4506         case SNDRV_HDSP_IOCTL_GET_VERSION: {
4507                 struct hdsp_version hdsp_version;
4508                 int err;
4509                 
4510                 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4511                 if (hdsp->io_type == Undefined) {
4512                         if ((err = hdsp_get_iobox_version(hdsp)) < 0)
4513                                 return err;
4514                 }
4515                 hdsp_version.io_type = hdsp->io_type;
4516                 hdsp_version.firmware_rev = hdsp->firmware_rev;
4517                 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
4518                         return -EFAULT;
4519                 break;
4520         }
4521         case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
4522                 struct hdsp_firmware __user *firmware;
4523                 u32 __user *firmware_data;
4524                 int err;
4525                 
4526                 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4527                 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4528                 if (hdsp->io_type == Undefined) return -EINVAL;
4529
4530                 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4531                         return -EBUSY;
4532
4533                 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
4534                 firmware = (struct hdsp_firmware __user *)argp;
4535
4536                 if (get_user(firmware_data, &firmware->firmware_data))
4537                         return -EFAULT;
4538                 
4539                 if (hdsp_check_for_iobox (hdsp))
4540                         return -EIO;
4541
4542                 if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
4543                         return -EFAULT;
4544                 
4545                 hdsp->state |= HDSP_FirmwareCached;
4546
4547                 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
4548                         return err;
4549                 
4550                 if (!(hdsp->state & HDSP_InitializationComplete)) {
4551                         if ((err = snd_hdsp_enable_io(hdsp)) < 0)
4552                                 return err;
4553                         
4554                         snd_hdsp_initialize_channels(hdsp);             
4555                         snd_hdsp_initialize_midi_flush(hdsp);
4556             
4557                         if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4558                                 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
4559                                 return err;
4560                         }
4561                 }
4562                 break;
4563         }
4564         case SNDRV_HDSP_IOCTL_GET_MIXER: {
4565                 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
4566                 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4567                         return -EFAULT;
4568                 break;
4569         }
4570         default:
4571                 return -EINVAL;
4572         }
4573         return 0;
4574 }
4575
4576 static struct snd_pcm_ops snd_hdsp_playback_ops = {
4577         .open =         snd_hdsp_playback_open,
4578         .close =        snd_hdsp_playback_release,
4579         .ioctl =        snd_hdsp_ioctl,
4580         .hw_params =    snd_hdsp_hw_params,
4581         .prepare =      snd_hdsp_prepare,
4582         .trigger =      snd_hdsp_trigger,
4583         .pointer =      snd_hdsp_hw_pointer,
4584         .copy =         snd_hdsp_playback_copy,
4585         .silence =      snd_hdsp_hw_silence,
4586 };
4587
4588 static struct snd_pcm_ops snd_hdsp_capture_ops = {
4589         .open =         snd_hdsp_capture_open,
4590         .close =        snd_hdsp_capture_release,
4591         .ioctl =        snd_hdsp_ioctl,
4592         .hw_params =    snd_hdsp_hw_params,
4593         .prepare =      snd_hdsp_prepare,
4594         .trigger =      snd_hdsp_trigger,
4595         .pointer =      snd_hdsp_hw_pointer,
4596         .copy =         snd_hdsp_capture_copy,
4597 };
4598
4599 static int __devinit snd_hdsp_create_hwdep(struct snd_card *card,
4600                                            struct hdsp *hdsp)
4601 {
4602         struct snd_hwdep *hw;
4603         int err;
4604         
4605         if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4606                 return err;
4607                 
4608         hdsp->hwdep = hw;
4609         hw->private_data = hdsp;
4610         strcpy(hw->name, "HDSP hwdep interface");
4611
4612         hw->ops.open = snd_hdsp_hwdep_dummy_op;
4613         hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
4614         hw->ops.release = snd_hdsp_hwdep_dummy_op;
4615                 
4616         return 0;
4617 }
4618
4619 static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
4620 {
4621         struct snd_pcm *pcm;
4622         int err;
4623
4624         if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4625                 return err;
4626
4627         hdsp->pcm = pcm;
4628         pcm->private_data = hdsp;
4629         strcpy(pcm->name, hdsp->card_name);
4630
4631         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4632         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4633
4634         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4635
4636         return 0;
4637 }
4638
4639 static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
4640 {
4641         hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4642         hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4643 }
4644
4645 static int snd_hdsp_enable_io (struct hdsp *hdsp)
4646 {
4647         int i;
4648         
4649         if (hdsp_fifo_wait (hdsp, 0, 100)) {
4650                 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
4651                 return -EIO;
4652         }
4653         
4654         for (i = 0; i < hdsp->max_channels; ++i) {
4655                 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4656                 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4657         }
4658         
4659         return 0;
4660 }
4661
4662 static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
4663 {
4664         int status, aebi_channels, aebo_channels;
4665         
4666         switch (hdsp->io_type) {
4667         case Digiface:
4668                 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4669                 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4670                 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4671                 break;
4672
4673         case H9652:
4674                 hdsp->card_name = "RME Hammerfall HDSP 9652";
4675                 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4676                 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4677                 break;
4678         
4679         case H9632:
4680                 status = hdsp_read(hdsp, HDSP_statusRegister);
4681                 /* HDSP_AEBx bits are low when AEB are connected */
4682                 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4683                 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4684                 hdsp->card_name = "RME Hammerfall HDSP 9632";
4685                 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4686                 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4687                 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4688                 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4689                 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4690                 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4691                 break;
4692
4693         case Multiface:
4694                 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4695                 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4696                 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4697                 break;
4698                 
4699         default:
4700                 /* should never get here */
4701                 break;
4702         }
4703 }
4704
4705 static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
4706 {
4707         snd_hdsp_flush_midi_input (hdsp, 0);
4708         snd_hdsp_flush_midi_input (hdsp, 1);
4709 }
4710
4711 static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
4712 {
4713         int err;
4714         
4715         if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
4716                 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
4717                 return err;
4718         }
4719         
4720
4721         if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
4722                 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
4723                 return err;
4724         }
4725
4726         if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
4727                 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
4728                         snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
4729                         return err;
4730                 }
4731         }
4732
4733         if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
4734                 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
4735                 return err;
4736         }
4737
4738         snd_hdsp_proc_init(hdsp);
4739
4740         hdsp->system_sample_rate = -1;
4741         hdsp->playback_pid = -1;
4742         hdsp->capture_pid = -1;
4743         hdsp->capture_substream = NULL;
4744         hdsp->playback_substream = NULL;
4745
4746         if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
4747                 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
4748                 return err;
4749         }
4750         
4751         if (!(hdsp->state & HDSP_InitializationComplete)) {
4752                 strcpy(card->shortname, "Hammerfall DSP");
4753                 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name, 
4754                         hdsp->port, hdsp->irq);
4755             
4756                 if ((err = snd_card_register(card)) < 0) {
4757                         snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
4758                         return err;
4759                 }
4760                 hdsp->state |= HDSP_InitializationComplete;
4761         }
4762         
4763         return 0;
4764 }
4765
4766 #ifdef HDSP_FW_LOADER
4767 /* load firmware via hotplug fw loader */
4768 static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp)
4769 {
4770         const char *fwfile;
4771         const struct firmware *fw;
4772         int err;
4773                 
4774         if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4775                 return 0;
4776         if (hdsp->io_type == Undefined) {
4777                 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
4778                         return err;
4779                 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4780                         return 0;
4781         }
4782         
4783         /* caution: max length of firmware filename is 30! */
4784         switch (hdsp->io_type) {
4785         case Multiface:
4786                 if (hdsp->firmware_rev == 0xa)
4787                         fwfile = "multiface_firmware.bin";
4788                 else
4789                         fwfile = "multiface_firmware_rev11.bin";
4790                 break;
4791         case Digiface:
4792                 if (hdsp->firmware_rev == 0xa)
4793                         fwfile = "digiface_firmware.bin";
4794                 else
4795                         fwfile = "digiface_firmware_rev11.bin";
4796                 break;
4797         default:
4798                 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
4799                 return -EINVAL;
4800         }
4801
4802         if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
4803                 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
4804                 return -ENOENT;
4805         }
4806         if (fw->size < sizeof(hdsp->firmware_cache)) {
4807                 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
4808                            (int)fw->size, (int)sizeof(hdsp->firmware_cache));
4809                 release_firmware(fw);
4810                 return -EINVAL;
4811         }
4812
4813         memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
4814
4815         release_firmware(fw);
4816                 
4817         hdsp->state |= HDSP_FirmwareCached;
4818
4819         if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
4820                 return err;
4821                 
4822         if (!(hdsp->state & HDSP_InitializationComplete)) {
4823                 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
4824                         return err;
4825
4826                 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
4827                         snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
4828                         return err;
4829                 }
4830                 snd_hdsp_initialize_channels(hdsp);
4831                 snd_hdsp_initialize_midi_flush(hdsp);
4832                 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4833                         snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
4834                         return err;
4835                 }
4836         }
4837         return 0;
4838 }
4839 #endif
4840
4841 static int __devinit snd_hdsp_create(struct snd_card *card,
4842                                      struct hdsp *hdsp)
4843 {
4844         struct pci_dev *pci = hdsp->pci;
4845         int err;
4846         int is_9652 = 0;
4847         int is_9632 = 0;
4848
4849         hdsp->irq = -1;
4850         hdsp->state = 0;
4851         hdsp->midi[0].rmidi = NULL;
4852         hdsp->midi[1].rmidi = NULL;
4853         hdsp->midi[0].input = NULL;
4854         hdsp->midi[1].input = NULL;
4855         hdsp->midi[0].output = NULL;
4856         hdsp->midi[1].output = NULL;
4857         hdsp->midi[0].pending = 0;
4858         hdsp->midi[1].pending = 0;
4859         spin_lock_init(&hdsp->midi[0].lock);
4860         spin_lock_init(&hdsp->midi[1].lock);
4861         hdsp->iobase = NULL;
4862         hdsp->control_register = 0;
4863         hdsp->control2_register = 0;
4864         hdsp->io_type = Undefined;
4865         hdsp->max_channels = 26;
4866
4867         hdsp->card = card;
4868         
4869         spin_lock_init(&hdsp->lock);
4870
4871         tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
4872         
4873         pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
4874         hdsp->firmware_rev &= 0xff;
4875         
4876         /* From Martin Bjoernsen :
4877             "It is important that the card's latency timer register in
4878             the PCI configuration space is set to a value much larger
4879             than 0 by the computer's BIOS or the driver.
4880             The windows driver always sets this 8 bit register [...]
4881             to its maximum 255 to avoid problems with some computers."
4882         */
4883         pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
4884         
4885         strcpy(card->driver, "H-DSP");
4886         strcpy(card->mixername, "Xilinx FPGA");
4887
4888         if (hdsp->firmware_rev < 0xa)
4889                 return -ENODEV;
4890         else if (hdsp->firmware_rev < 0x64)
4891                 hdsp->card_name = "RME Hammerfall DSP";
4892         else if (hdsp->firmware_rev < 0x96) {
4893                 hdsp->card_name = "RME HDSP 9652";
4894                 is_9652 = 1;
4895         } else {
4896                 hdsp->card_name = "RME HDSP 9632";
4897                 hdsp->max_channels = 16;
4898                 is_9632 = 1;    
4899         }
4900
4901         if ((err = pci_enable_device(pci)) < 0)
4902                 return err;
4903
4904         pci_set_master(hdsp->pci);
4905
4906         if ((err = pci_request_regions(pci, "hdsp")) < 0)
4907                 return err;
4908         hdsp->port = pci_resource_start(pci, 0);
4909         if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
4910                 snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
4911                 return -EBUSY;
4912         }
4913
4914         if (request_irq(pci->irq, snd_hdsp_interrupt, SA_INTERRUPT|SA_SHIRQ, "hdsp", (void *)hdsp)) {
4915                 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
4916                 return -EBUSY;
4917         }
4918
4919         hdsp->irq = pci->irq;
4920         hdsp->precise_ptr = 1;
4921         hdsp->use_midi_tasklet = 1;
4922
4923         if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
4924                 return err;
4925         
4926         if (!is_9652 && !is_9632) {
4927                 /* we wait 2 seconds to let freshly inserted cardbus cards do their hardware init */
4928                 ssleep(2);
4929
4930                 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
4931 #ifdef HDSP_FW_LOADER
4932                         if ((err = hdsp_request_fw_loader(hdsp)) < 0)
4933                                 /* we don't fail as this can happen
4934                                    if userspace is not ready for
4935                                    firmware upload
4936                                 */
4937                                 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
4938                         else
4939                                 /* init is complete, we return */
4940                                 return 0;
4941 #endif
4942                         /* no iobox connected, we defer initialization */
4943                         snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
4944                         if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
4945                                 return err;
4946                         return 0;
4947                 } else {
4948                         snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");     
4949                         if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
4950                                 hdsp->io_type = Multiface;
4951                         else 
4952                                 hdsp->io_type = Digiface;
4953                 }
4954         }
4955         
4956         if ((err = snd_hdsp_enable_io(hdsp)) != 0)
4957                 return err;
4958         
4959         if (is_9652)
4960                 hdsp->io_type = H9652;
4961         
4962         if (is_9632)
4963                 hdsp->io_type = H9632;
4964
4965         if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
4966                 return err;
4967         
4968         snd_hdsp_initialize_channels(hdsp);
4969         snd_hdsp_initialize_midi_flush(hdsp);
4970
4971         hdsp->state |= HDSP_FirmwareLoaded;     
4972
4973         if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
4974                 return err;
4975
4976         return 0;       
4977 }
4978
4979 static int snd_hdsp_free(struct hdsp *hdsp)
4980 {
4981         if (hdsp->port) {
4982                 /* stop the audio, and cancel all interrupts */
4983                 tasklet_kill(&hdsp->midi_tasklet);
4984                 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
4985                 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
4986         }
4987
4988         if (hdsp->irq >= 0)
4989                 free_irq(hdsp->irq, (void *)hdsp);
4990
4991         snd_hdsp_free_buffers(hdsp);
4992         
4993         if (hdsp->iobase)
4994                 iounmap(hdsp->iobase);
4995
4996         if (hdsp->port)
4997                 pci_release_regions(hdsp->pci);
4998                 
4999         pci_disable_device(hdsp->pci);
5000         return 0;
5001 }
5002
5003 static void snd_hdsp_card_free(struct snd_card *card)
5004 {
5005         struct hdsp *hdsp = (struct hdsp *) card->private_data;
5006
5007         if (hdsp)
5008                 snd_hdsp_free(hdsp);
5009 }
5010
5011 static int __devinit snd_hdsp_probe(struct pci_dev *pci,
5012                                     const struct pci_device_id *pci_id)
5013 {
5014         static int dev;
5015         struct hdsp *hdsp;
5016         struct snd_card *card;
5017         int err;
5018
5019         if (dev >= SNDRV_CARDS)
5020                 return -ENODEV;
5021         if (!enable[dev]) {
5022                 dev++;
5023                 return -ENOENT;
5024         }
5025
5026         if (!(card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct hdsp))))
5027                 return -ENOMEM;
5028
5029         hdsp = (struct hdsp *) card->private_data;
5030         card->private_free = snd_hdsp_card_free;
5031         hdsp->dev = dev;
5032         hdsp->pci = pci;
5033         snd_card_set_dev(card, &pci->dev);
5034
5035         if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5036                 snd_card_free(card);
5037                 return err;
5038         }
5039
5040         strcpy(card->shortname, "Hammerfall DSP");
5041         sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name, 
5042                 hdsp->port, hdsp->irq);
5043
5044         if ((err = snd_card_register(card)) < 0) {
5045                 snd_card_free(card);
5046                 return err;
5047         }
5048         pci_set_drvdata(pci, card);
5049         dev++;
5050         return 0;
5051 }
5052
5053 static void __devexit snd_hdsp_remove(struct pci_dev *pci)
5054 {
5055         snd_card_free(pci_get_drvdata(pci));
5056         pci_set_drvdata(pci, NULL);
5057 }
5058
5059 static struct pci_driver driver = {
5060         .name =     "RME Hammerfall DSP",
5061         .id_table = snd_hdsp_ids,
5062         .probe =    snd_hdsp_probe,
5063         .remove = __devexit_p(snd_hdsp_remove),
5064 };
5065
5066 static int __init alsa_card_hdsp_init(void)
5067 {
5068         return pci_register_driver(&driver);
5069 }
5070
5071 static void __exit alsa_card_hdsp_exit(void)
5072 {
5073         pci_unregister_driver(&driver);
5074 }
5075
5076 module_init(alsa_card_hdsp_init)
5077 module_exit(alsa_card_hdsp_exit)