1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA driver for RME Hammerfall DSP audio interface(s)
5 * Copyright (c) 2002 Paul Davis
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/pci.h>
14 #include <linux/firmware.h>
15 #include <linux/module.h>
16 #include <linux/math64.h>
17 #include <linux/vmalloc.h>
19 #include <linux/nospec.h>
21 #include <sound/core.h>
22 #include <sound/control.h>
23 #include <sound/pcm.h>
24 #include <sound/info.h>
25 #include <sound/asoundef.h>
26 #include <sound/rawmidi.h>
27 #include <sound/hwdep.h>
28 #include <sound/initval.h>
29 #include <sound/hdsp.h>
31 #include <asm/byteorder.h>
32 #include <asm/current.h>
34 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
35 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
36 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
38 module_param_array(index, int, NULL, 0444);
39 MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
40 module_param_array(id, charp, NULL, 0444);
41 MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
42 module_param_array(enable, bool, NULL, 0444);
43 MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
44 MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
45 MODULE_DESCRIPTION("RME Hammerfall DSP");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
50 MODULE_FIRMWARE("rpm_firmware.bin");
51 MODULE_FIRMWARE("multiface_firmware.bin");
52 MODULE_FIRMWARE("multiface_firmware_rev11.bin");
53 MODULE_FIRMWARE("digiface_firmware.bin");
54 MODULE_FIRMWARE("digiface_firmware_rev11.bin");
56 #define HDSP_MAX_CHANNELS 26
57 #define HDSP_MAX_DS_CHANNELS 14
58 #define HDSP_MAX_QS_CHANNELS 8
59 #define DIGIFACE_SS_CHANNELS 26
60 #define DIGIFACE_DS_CHANNELS 14
61 #define MULTIFACE_SS_CHANNELS 18
62 #define MULTIFACE_DS_CHANNELS 14
63 #define H9652_SS_CHANNELS 26
64 #define H9652_DS_CHANNELS 14
65 /* This does not include possible Analog Extension Boards
66 AEBs are detected at card initialization
68 #define H9632_SS_CHANNELS 12
69 #define H9632_DS_CHANNELS 8
70 #define H9632_QS_CHANNELS 4
71 #define RPM_CHANNELS 6
73 /* Write registers. These are defined as byte-offsets from the iobase value.
75 #define HDSP_resetPointer 0
76 #define HDSP_freqReg 0
77 #define HDSP_outputBufferAddress 32
78 #define HDSP_inputBufferAddress 36
79 #define HDSP_controlRegister 64
80 #define HDSP_interruptConfirmation 96
81 #define HDSP_outputEnable 128
82 #define HDSP_control2Reg 256
83 #define HDSP_midiDataOut0 352
84 #define HDSP_midiDataOut1 356
85 #define HDSP_fifoData 368
86 #define HDSP_inputEnable 384
88 /* Read registers. These are defined as byte-offsets from the iobase value
91 #define HDSP_statusRegister 0
92 #define HDSP_timecode 128
93 #define HDSP_status2Register 192
94 #define HDSP_midiDataIn0 360
95 #define HDSP_midiDataIn1 364
96 #define HDSP_midiStatusOut0 384
97 #define HDSP_midiStatusOut1 388
98 #define HDSP_midiStatusIn0 392
99 #define HDSP_midiStatusIn1 396
100 #define HDSP_fifoStatus 400
102 /* the meters are regular i/o-mapped registers, but offset
103 considerably from the rest. the peak registers are reset
104 when read; the least-significant 4 bits are full-scale counters;
105 the actual peak value is in the most-significant 24 bits.
108 #define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
109 #define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
110 #define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
111 #define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
112 #define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
115 /* This is for H9652 cards
116 Peak values are read downward from the base
117 Rms values are read upward
118 There are rms values for the outputs too
119 26*3 values are read in ss mode
120 14*3 in ds mode, with no gap between values
122 #define HDSP_9652_peakBase 7164
123 #define HDSP_9652_rmsBase 4096
125 /* c.f. the hdsp_9632_meters_t struct */
126 #define HDSP_9632_metersBase 4096
128 #define HDSP_IO_EXTENT 7168
130 /* control2 register bits */
132 #define HDSP_TMS 0x01
133 #define HDSP_TCK 0x02
134 #define HDSP_TDI 0x04
135 #define HDSP_JTAG 0x08
136 #define HDSP_PWDN 0x10
137 #define HDSP_PROGRAM 0x020
138 #define HDSP_CONFIG_MODE_0 0x040
139 #define HDSP_CONFIG_MODE_1 0x080
140 #define HDSP_VERSION_BIT (0x100 | HDSP_S_LOAD)
141 #define HDSP_BIGENDIAN_MODE 0x200
142 #define HDSP_RD_MULTIPLE 0x400
143 #define HDSP_9652_ENABLE_MIXER 0x800
144 #define HDSP_S200 0x800
145 #define HDSP_S300 (0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
146 #define HDSP_CYCLIC_MODE 0x1000
147 #define HDSP_TDO 0x10000000
149 #define HDSP_S_PROGRAM (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
150 #define HDSP_S_LOAD (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
152 /* Control Register bits */
154 #define HDSP_Start (1<<0) /* start engine */
155 #define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
156 #define HDSP_Latency1 (1<<2) /* [ see above ] */
157 #define HDSP_Latency2 (1<<3) /* [ see above ] */
158 #define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
159 #define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
160 #define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
161 #define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
162 #define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
163 #define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
164 #define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
165 #define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
166 #define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
167 #define HDSP_SyncRef2 (1<<13)
168 #define HDSP_SPDIFInputSelect0 (1<<14)
169 #define HDSP_SPDIFInputSelect1 (1<<15)
170 #define HDSP_SyncRef0 (1<<16)
171 #define HDSP_SyncRef1 (1<<17)
172 #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
173 #define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
174 #define HDSP_Midi0InterruptEnable (1<<22)
175 #define HDSP_Midi1InterruptEnable (1<<23)
176 #define HDSP_LineOut (1<<24)
177 #define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
178 #define HDSP_ADGain1 (1<<26)
179 #define HDSP_DAGain0 (1<<27)
180 #define HDSP_DAGain1 (1<<28)
181 #define HDSP_PhoneGain0 (1<<29)
182 #define HDSP_PhoneGain1 (1<<30)
183 #define HDSP_QuadSpeed (1<<31)
185 /* RPM uses some of the registers for special purposes */
186 #define HDSP_RPM_Inp12 0x04A00
187 #define HDSP_RPM_Inp12_Phon_6dB 0x00800 /* Dolby */
188 #define HDSP_RPM_Inp12_Phon_0dB 0x00000 /* .. */
189 #define HDSP_RPM_Inp12_Phon_n6dB 0x04000 /* inp_0 */
190 #define HDSP_RPM_Inp12_Line_0dB 0x04200 /* Dolby+PRO */
191 #define HDSP_RPM_Inp12_Line_n6dB 0x00200 /* PRO */
193 #define HDSP_RPM_Inp34 0x32000
194 #define HDSP_RPM_Inp34_Phon_6dB 0x20000 /* SyncRef1 */
195 #define HDSP_RPM_Inp34_Phon_0dB 0x00000 /* .. */
196 #define HDSP_RPM_Inp34_Phon_n6dB 0x02000 /* SyncRef2 */
197 #define HDSP_RPM_Inp34_Line_0dB 0x30000 /* SyncRef1+SyncRef0 */
198 #define HDSP_RPM_Inp34_Line_n6dB 0x10000 /* SyncRef0 */
200 #define HDSP_RPM_Bypass 0x01000
202 #define HDSP_RPM_Disconnect 0x00001
204 #define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
205 #define HDSP_ADGainMinus10dBV HDSP_ADGainMask
206 #define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
207 #define HDSP_ADGainLowGain 0
209 #define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
210 #define HDSP_DAGainHighGain HDSP_DAGainMask
211 #define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
212 #define HDSP_DAGainMinus10dBV 0
214 #define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
215 #define HDSP_PhoneGain0dB HDSP_PhoneGainMask
216 #define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
217 #define HDSP_PhoneGainMinus12dB 0
219 #define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
220 #define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
222 #define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
223 #define HDSP_SPDIFInputADAT1 0
224 #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
225 #define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
226 #define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
228 #define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
229 #define HDSP_SyncRef_ADAT1 0
230 #define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
231 #define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
232 #define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
233 #define HDSP_SyncRef_WORD (HDSP_SyncRef2)
234 #define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
236 /* Sample Clock Sources */
238 #define HDSP_CLOCK_SOURCE_AUTOSYNC 0
239 #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
240 #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
241 #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
242 #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
243 #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
244 #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
245 #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
246 #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
247 #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
249 /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
251 #define HDSP_SYNC_FROM_WORD 0
252 #define HDSP_SYNC_FROM_SPDIF 1
253 #define HDSP_SYNC_FROM_ADAT1 2
254 #define HDSP_SYNC_FROM_ADAT_SYNC 3
255 #define HDSP_SYNC_FROM_ADAT2 4
256 #define HDSP_SYNC_FROM_ADAT3 5
258 /* SyncCheck status */
260 #define HDSP_SYNC_CHECK_NO_LOCK 0
261 #define HDSP_SYNC_CHECK_LOCK 1
262 #define HDSP_SYNC_CHECK_SYNC 2
264 /* AutoSync references - used by "autosync_ref" control switch */
266 #define HDSP_AUTOSYNC_FROM_WORD 0
267 #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
268 #define HDSP_AUTOSYNC_FROM_SPDIF 2
269 #define HDSP_AUTOSYNC_FROM_NONE 3
270 #define HDSP_AUTOSYNC_FROM_ADAT1 4
271 #define HDSP_AUTOSYNC_FROM_ADAT2 5
272 #define HDSP_AUTOSYNC_FROM_ADAT3 6
274 /* Possible sources of S/PDIF input */
276 #define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
277 #define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
278 #define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
279 #define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
281 #define HDSP_Frequency32KHz HDSP_Frequency0
282 #define HDSP_Frequency44_1KHz HDSP_Frequency1
283 #define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
284 #define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
285 #define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
286 #define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
287 /* For H9632 cards */
288 #define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
289 #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
290 #define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
291 /* RME says n = 104857600000000, but in the windows MADI driver, I see:
292 return 104857600000000 / rate; // 100 MHz
293 return 110100480000000 / rate; // 105 MHz
295 #define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
297 #define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
298 #define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
300 #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
301 #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
303 /* Status Register bits */
305 #define HDSP_audioIRQPending (1<<0)
306 #define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
307 #define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
308 #define HDSP_Lock1 (1<<2)
309 #define HDSP_Lock0 (1<<3)
310 #define HDSP_SPDIFSync (1<<4)
311 #define HDSP_TimecodeLock (1<<5)
312 #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
313 #define HDSP_Sync2 (1<<16)
314 #define HDSP_Sync1 (1<<17)
315 #define HDSP_Sync0 (1<<18)
316 #define HDSP_DoubleSpeedStatus (1<<19)
317 #define HDSP_ConfigError (1<<20)
318 #define HDSP_DllError (1<<21)
319 #define HDSP_spdifFrequency0 (1<<22)
320 #define HDSP_spdifFrequency1 (1<<23)
321 #define HDSP_spdifFrequency2 (1<<24)
322 #define HDSP_SPDIFErrorFlag (1<<25)
323 #define HDSP_BufferID (1<<26)
324 #define HDSP_TimecodeSync (1<<27)
325 #define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
326 #define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
327 #define HDSP_midi0IRQPending (1<<30)
328 #define HDSP_midi1IRQPending (1<<31)
330 #define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
331 #define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
332 HDSP_spdifFrequency1|\
333 HDSP_spdifFrequency2|\
334 HDSP_spdifFrequency3)
336 #define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
337 #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
338 #define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
340 #define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
341 #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
342 #define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
344 /* This is for H9632 cards */
345 #define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
346 HDSP_spdifFrequency1|\
347 HDSP_spdifFrequency2)
348 #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
349 #define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
351 /* Status2 Register bits */
353 #define HDSP_version0 (1<<0)
354 #define HDSP_version1 (1<<1)
355 #define HDSP_version2 (1<<2)
356 #define HDSP_wc_lock (1<<3)
357 #define HDSP_wc_sync (1<<4)
358 #define HDSP_inp_freq0 (1<<5)
359 #define HDSP_inp_freq1 (1<<6)
360 #define HDSP_inp_freq2 (1<<7)
361 #define HDSP_SelSyncRef0 (1<<8)
362 #define HDSP_SelSyncRef1 (1<<9)
363 #define HDSP_SelSyncRef2 (1<<10)
365 #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
367 #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
368 #define HDSP_systemFrequency32 (HDSP_inp_freq0)
369 #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
370 #define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
371 #define HDSP_systemFrequency64 (HDSP_inp_freq2)
372 #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
373 #define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
374 /* FIXME : more values for 9632 cards ? */
376 #define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
377 #define HDSP_SelSyncRef_ADAT1 0
378 #define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
379 #define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
380 #define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
381 #define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
382 #define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
384 /* Card state flags */
386 #define HDSP_InitializationComplete (1<<0)
387 #define HDSP_FirmwareLoaded (1<<1)
388 #define HDSP_FirmwareCached (1<<2)
390 /* FIFO wait times, defined in terms of 1/10ths of msecs */
392 #define HDSP_LONG_WAIT 5000
393 #define HDSP_SHORT_WAIT 30
395 #define UNITY_GAIN 32768
396 #define MINUS_INFINITY_GAIN 0
398 /* the size of a substream (1 mono data stream) */
400 #define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
401 #define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
403 /* the size of the area we need to allocate for DMA transfers. the
404 size is the same regardless of the number of channels - the
405 Multiface still uses the same memory area.
407 Note that we allocate 1 more channel than is apparently needed
408 because the h/w seems to write 1 byte beyond the end of the last
412 #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
413 #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
415 #define HDSP_FIRMWARE_SIZE (24413 * 4)
417 struct hdsp_9632_meters {
419 u32 playback_peak[16];
423 u32 input_rms_low[16];
424 u32 playback_rms_low[16];
425 u32 output_rms_low[16];
427 u32 input_rms_high[16];
428 u32 playback_rms_high[16];
429 u32 output_rms_high[16];
430 u32 xxx_rms_high[16];
436 struct snd_rawmidi *rmidi;
437 struct snd_rawmidi_substream *input;
438 struct snd_rawmidi_substream *output;
439 char istimer; /* timer in use */
440 struct timer_list timer;
447 struct snd_pcm_substream *capture_substream;
448 struct snd_pcm_substream *playback_substream;
449 struct hdsp_midi midi[2];
450 struct work_struct midi_work;
453 u32 control_register; /* cached value */
454 u32 control2_register; /* cached value */
456 u32 creg_spdif_stream;
457 int clock_source_locked;
458 char *card_name; /* digiface/multiface/rpm */
459 enum HDSP_IO_Type io_type; /* ditto, but for code use */
460 unsigned short firmware_rev;
461 unsigned short state; /* stores state bits */
462 const struct firmware *firmware;
464 size_t period_bytes; /* guess what this is */
465 unsigned char max_channels;
466 unsigned char qs_in_channels; /* quad speed mode for H9632 */
467 unsigned char ds_in_channels;
468 unsigned char ss_in_channels; /* different for multiface/digiface */
469 unsigned char qs_out_channels;
470 unsigned char ds_out_channels;
471 unsigned char ss_out_channels;
473 struct snd_dma_buffer capture_dma_buf;
474 struct snd_dma_buffer playback_dma_buf;
475 unsigned char *capture_buffer; /* suitably aligned address */
476 unsigned char *playback_buffer; /* suitably aligned address */
481 int system_sample_rate;
482 const char *channel_map;
486 void __iomem *iobase;
487 struct snd_card *card;
489 struct snd_hwdep *hwdep;
491 struct snd_kcontrol *spdif_ctl;
492 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
493 unsigned int dds_value; /* last value written to freq register */
496 /* These tables map the ALSA channels 1..N to the channels that we
497 need to use in order to find the relevant channel buffer. RME
498 refer to this kind of mapping as between "the ADAT channel and
499 the DMA channel." We index it using the logical audio channel,
500 and the value is the DMA channel (i.e. channel buffer number)
501 where the data for that channel can be read/written from/to.
504 static const char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
505 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
506 18, 19, 20, 21, 22, 23, 24, 25
509 static const char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
511 0, 1, 2, 3, 4, 5, 6, 7,
513 16, 17, 18, 19, 20, 21, 22, 23,
516 -1, -1, -1, -1, -1, -1, -1, -1
519 static const char channel_map_ds[HDSP_MAX_CHANNELS] = {
520 /* ADAT channels are remapped */
521 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
522 /* channels 12 and 13 are S/PDIF */
524 /* others don't exist */
525 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
528 static const char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
530 0, 1, 2, 3, 4, 5, 6, 7,
535 /* AO4S-192 and AI4S-192 extension boards */
537 /* others don't exist */
538 -1, -1, -1, -1, -1, -1, -1, -1,
542 static const char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
549 /* AO4S-192 and AI4S-192 extension boards */
551 /* others don't exist */
552 -1, -1, -1, -1, -1, -1, -1, -1,
553 -1, -1, -1, -1, -1, -1
556 static const char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
557 /* ADAT is disabled in this mode */
562 /* AO4S-192 and AI4S-192 extension boards */
564 /* others don't exist */
565 -1, -1, -1, -1, -1, -1, -1, -1,
566 -1, -1, -1, -1, -1, -1, -1, -1,
570 static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
572 return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev, size, dmab);
575 static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
578 snd_dma_free_pages(dmab);
582 static const struct pci_device_id snd_hdsp_ids[] = {
584 .vendor = PCI_VENDOR_ID_XILINX,
585 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
586 .subvendor = PCI_ANY_ID,
587 .subdevice = PCI_ANY_ID,
588 }, /* RME Hammerfall-DSP */
592 MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
595 static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
596 static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
597 static int snd_hdsp_enable_io (struct hdsp *hdsp);
598 static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
599 static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
600 static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
601 static int hdsp_autosync_ref(struct hdsp *hdsp);
602 static int snd_hdsp_set_defaults(struct hdsp *hdsp);
603 static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
605 static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
607 switch (hdsp->io_type) {
612 if (hdsp->firmware_rev == 0xa)
613 return (64 * out) + (32 + (in));
615 return (52 * out) + (26 + (in));
617 return (32 * out) + (16 + (in));
619 return (52 * out) + (26 + (in));
623 static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
625 switch (hdsp->io_type) {
630 if (hdsp->firmware_rev == 0xa)
631 return (64 * out) + in;
633 return (52 * out) + in;
635 return (32 * out) + in;
637 return (52 * out) + in;
641 static void hdsp_write(struct hdsp *hdsp, int reg, int val)
643 writel(val, hdsp->iobase + reg);
646 static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
648 return readl (hdsp->iobase + reg);
651 static int hdsp_check_for_iobox (struct hdsp *hdsp)
655 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
656 for (i = 0; i < 500; i++) {
657 if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
660 dev_dbg(hdsp->card->dev,
661 "IO box found after %d ms\n",
668 dev_err(hdsp->card->dev, "no IO box connected!\n");
669 hdsp->state &= ~HDSP_FirmwareLoaded;
673 static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
678 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
681 for (i = 0; i != loops; ++i) {
682 if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
685 dev_dbg(hdsp->card->dev, "iobox found after %ums!\n",
691 dev_info(hdsp->card->dev, "no IO box connected!\n");
692 hdsp->state &= ~HDSP_FirmwareLoaded;
696 static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
702 if (hdsp->fw_uploaded)
703 cache = hdsp->fw_uploaded;
707 cache = (u32 *)hdsp->firmware->data;
712 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
714 dev_info(hdsp->card->dev, "loading firmware\n");
716 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
717 hdsp_write (hdsp, HDSP_fifoData, 0);
719 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
720 dev_info(hdsp->card->dev,
721 "timeout waiting for download preparation\n");
722 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
726 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
728 for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
729 hdsp_write(hdsp, HDSP_fifoData, cache[i]);
730 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
731 dev_info(hdsp->card->dev,
732 "timeout during firmware loading\n");
733 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
738 hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
739 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
742 #ifdef SNDRV_BIG_ENDIAN
743 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
745 hdsp->control2_register = 0;
747 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
748 dev_info(hdsp->card->dev, "finished firmware loading\n");
751 if (hdsp->state & HDSP_InitializationComplete) {
752 dev_info(hdsp->card->dev,
753 "firmware loaded from cache, restoring defaults\n");
754 spin_lock_irqsave(&hdsp->lock, flags);
755 snd_hdsp_set_defaults(hdsp);
756 spin_unlock_irqrestore(&hdsp->lock, flags);
759 hdsp->state |= HDSP_FirmwareLoaded;
764 static int hdsp_get_iobox_version (struct hdsp *hdsp)
766 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
768 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
769 hdsp_write(hdsp, HDSP_fifoData, 0);
771 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
772 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
773 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
776 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
777 hdsp_write (hdsp, HDSP_fifoData, 0);
778 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0)
781 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
782 hdsp_write(hdsp, HDSP_fifoData, 0);
783 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
784 hdsp->io_type = Digiface;
785 dev_info(hdsp->card->dev, "Digiface found\n");
789 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
790 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
791 hdsp_write(hdsp, HDSP_fifoData, 0);
792 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0)
795 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
796 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
797 hdsp_write(hdsp, HDSP_fifoData, 0);
798 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0)
802 dev_info(hdsp->card->dev, "RPM found\n");
805 /* firmware was already loaded, get iobox type */
806 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
808 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
809 hdsp->io_type = Multiface;
811 hdsp->io_type = Digiface;
816 hdsp->io_type = Multiface;
817 dev_info(hdsp->card->dev, "Multiface found\n");
822 static int hdsp_request_fw_loader(struct hdsp *hdsp);
824 static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
826 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
828 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
829 hdsp->state &= ~HDSP_FirmwareLoaded;
830 if (! load_on_demand)
832 dev_err(hdsp->card->dev, "firmware not present.\n");
833 /* try to load firmware */
834 if (! (hdsp->state & HDSP_FirmwareCached)) {
835 if (! hdsp_request_fw_loader(hdsp))
837 dev_err(hdsp->card->dev,
838 "No firmware loaded nor cached, please upload firmware.\n");
841 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
842 dev_err(hdsp->card->dev,
843 "Firmware loading from cache failed, please upload manually.\n");
851 static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
855 /* the fifoStatus registers reports on how many words
856 are available in the command FIFO.
859 for (i = 0; i < timeout; i++) {
861 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
864 /* not very friendly, but we only do this during a firmware
865 load and changing the mixer, so we just put up with it.
871 dev_warn(hdsp->card->dev,
872 "wait for FIFO status <= %d failed after %d iterations\n",
877 static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
879 if (addr >= HDSP_MATRIX_MIXER_SIZE)
882 return hdsp->mixer_matrix[addr];
885 static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
889 if (addr >= HDSP_MATRIX_MIXER_SIZE)
892 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
894 /* from martin bjornsen:
896 "You can only write dwords to the
897 mixer memory which contain two
898 mixer values in the low and high
899 word. So if you want to change
900 value 0 you have to read value 1
901 from the cache and write both to
902 the first dword in the mixer
906 if (hdsp->io_type == H9632 && addr >= 512)
909 if (hdsp->io_type == H9652 && addr >= 1352)
912 hdsp->mixer_matrix[addr] = data;
915 /* `addr' addresses a 16-bit wide address, but
916 the address space accessed via hdsp_write
917 uses byte offsets. put another way, addr
918 varies from 0 to 1351, but to access the
919 corresponding memory location, we need
920 to access 0 to 2703 ...
924 hdsp_write (hdsp, 4096 + (ad*4),
925 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
926 hdsp->mixer_matrix[addr&0x7fe]);
932 ad = (addr << 16) + data;
934 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
937 hdsp_write (hdsp, HDSP_fifoData, ad);
938 hdsp->mixer_matrix[addr] = data;
945 static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
950 spin_lock_irqsave(&hdsp->lock, flags);
951 if ((hdsp->playback_pid != hdsp->capture_pid) &&
952 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
954 spin_unlock_irqrestore(&hdsp->lock, flags);
958 static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
960 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
961 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
963 /* For the 9632, the mask is different */
964 if (hdsp->io_type == H9632)
965 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
967 if (status & HDSP_SPDIFErrorFlag)
971 case HDSP_spdifFrequency32KHz: return 32000;
972 case HDSP_spdifFrequency44_1KHz: return 44100;
973 case HDSP_spdifFrequency48KHz: return 48000;
974 case HDSP_spdifFrequency64KHz: return 64000;
975 case HDSP_spdifFrequency88_2KHz: return 88200;
976 case HDSP_spdifFrequency96KHz: return 96000;
977 case HDSP_spdifFrequency128KHz:
978 if (hdsp->io_type == H9632) return 128000;
980 case HDSP_spdifFrequency176_4KHz:
981 if (hdsp->io_type == H9632) return 176400;
983 case HDSP_spdifFrequency192KHz:
984 if (hdsp->io_type == H9632) return 192000;
989 dev_warn(hdsp->card->dev,
990 "unknown spdif frequency status; bits = 0x%x, status = 0x%x\n",
995 static int hdsp_external_sample_rate(struct hdsp *hdsp)
997 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
998 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
1000 /* For the 9632 card, there seems to be no bit for indicating external
1001 * sample rate greater than 96kHz. The card reports the corresponding
1002 * single speed. So the best means seems to get spdif rate when
1003 * autosync reference is spdif */
1004 if (hdsp->io_type == H9632 &&
1005 hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
1006 return hdsp_spdif_sample_rate(hdsp);
1008 switch (rate_bits) {
1009 case HDSP_systemFrequency32: return 32000;
1010 case HDSP_systemFrequency44_1: return 44100;
1011 case HDSP_systemFrequency48: return 48000;
1012 case HDSP_systemFrequency64: return 64000;
1013 case HDSP_systemFrequency88_2: return 88200;
1014 case HDSP_systemFrequency96: return 96000;
1020 static void hdsp_compute_period_size(struct hdsp *hdsp)
1022 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
1025 static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
1029 position = hdsp_read(hdsp, HDSP_statusRegister);
1031 if (!hdsp->precise_ptr)
1032 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
1034 position &= HDSP_BufferPositionMask;
1036 position &= (hdsp->period_bytes/2) - 1;
1040 static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
1042 hdsp_write (hdsp, HDSP_resetPointer, 0);
1043 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1044 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
1045 * requires (?) to write again DDS value after a reset pointer
1046 * (at least, it works like this) */
1047 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
1050 static void hdsp_start_audio(struct hdsp *s)
1052 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
1053 hdsp_write(s, HDSP_controlRegister, s->control_register);
1056 static void hdsp_stop_audio(struct hdsp *s)
1058 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
1059 hdsp_write(s, HDSP_controlRegister, s->control_register);
1062 static void hdsp_silence_playback(struct hdsp *hdsp)
1064 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
1067 static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
1071 spin_lock_irq(&s->lock);
1080 s->control_register &= ~HDSP_LatencyMask;
1081 s->control_register |= hdsp_encode_latency(n);
1083 hdsp_write(s, HDSP_controlRegister, s->control_register);
1085 hdsp_compute_period_size(s);
1087 spin_unlock_irq(&s->lock);
1092 static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1098 else if (rate >= 56000)
1102 n = div_u64(n, rate);
1103 /* n should be less than 2^32 for being written to FREQ register */
1104 snd_BUG_ON(n >> 32);
1105 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1106 value to write it after a reset */
1107 hdsp->dds_value = n;
1108 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1111 static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
1113 int reject_if_open = 0;
1117 /* ASSUMPTION: hdsp->lock is either held, or
1118 there is no need for it (e.g. during module
1122 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
1123 if (called_internally) {
1124 /* request from ctl or card initialization */
1125 dev_err(hdsp->card->dev,
1126 "device is not running as a clock master: cannot set sample rate.\n");
1129 /* hw_param request while in AutoSync mode */
1130 int external_freq = hdsp_external_sample_rate(hdsp);
1131 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
1133 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1134 dev_info(hdsp->card->dev,
1135 "Detected ADAT in double speed mode\n");
1136 else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1137 dev_info(hdsp->card->dev,
1138 "Detected ADAT in quad speed mode\n");
1139 else if (rate != external_freq) {
1140 dev_info(hdsp->card->dev,
1141 "No AutoSync source for requested rate\n");
1147 current_rate = hdsp->system_sample_rate;
1149 /* Changing from a "single speed" to a "double speed" rate is
1150 not allowed if any substreams are open. This is because
1151 such a change causes a shift in the location of
1152 the DMA buffers and a reduction in the number of available
1155 Note that a similar but essentially insoluble problem
1156 exists for externally-driven rate changes. All we can do
1157 is to flag rate changes in the read/write routines. */
1159 if (rate > 96000 && hdsp->io_type != H9632)
1164 if (current_rate > 48000)
1166 rate_bits = HDSP_Frequency32KHz;
1169 if (current_rate > 48000)
1171 rate_bits = HDSP_Frequency44_1KHz;
1174 if (current_rate > 48000)
1176 rate_bits = HDSP_Frequency48KHz;
1179 if (current_rate <= 48000 || current_rate > 96000)
1181 rate_bits = HDSP_Frequency64KHz;
1184 if (current_rate <= 48000 || current_rate > 96000)
1186 rate_bits = HDSP_Frequency88_2KHz;
1189 if (current_rate <= 48000 || current_rate > 96000)
1191 rate_bits = HDSP_Frequency96KHz;
1194 if (current_rate < 128000)
1196 rate_bits = HDSP_Frequency128KHz;
1199 if (current_rate < 128000)
1201 rate_bits = HDSP_Frequency176_4KHz;
1204 if (current_rate < 128000)
1206 rate_bits = HDSP_Frequency192KHz;
1212 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1213 dev_warn(hdsp->card->dev,
1214 "cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1216 hdsp->playback_pid);
1220 hdsp->control_register &= ~HDSP_FrequencyMask;
1221 hdsp->control_register |= rate_bits;
1222 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1224 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1225 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1226 hdsp_set_dds_value(hdsp, rate);
1228 if (rate >= 128000) {
1229 hdsp->channel_map = channel_map_H9632_qs;
1230 } else if (rate > 48000) {
1231 if (hdsp->io_type == H9632)
1232 hdsp->channel_map = channel_map_H9632_ds;
1234 hdsp->channel_map = channel_map_ds;
1236 switch (hdsp->io_type) {
1239 hdsp->channel_map = channel_map_mf_ss;
1243 hdsp->channel_map = channel_map_df_ss;
1246 hdsp->channel_map = channel_map_H9632_ss;
1249 /* should never happen */
1254 hdsp->system_sample_rate = rate;
1259 /*----------------------------------------------------------------------------
1261 ----------------------------------------------------------------------------*/
1263 static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
1265 /* the hardware already does the relevant bit-mask with 0xff */
1267 return hdsp_read(hdsp, HDSP_midiDataIn1);
1269 return hdsp_read(hdsp, HDSP_midiDataIn0);
1272 static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
1274 /* the hardware already does the relevant bit-mask with 0xff */
1276 hdsp_write(hdsp, HDSP_midiDataOut1, val);
1278 hdsp_write(hdsp, HDSP_midiDataOut0, val);
1281 static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
1284 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
1286 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
1289 static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
1291 int fifo_bytes_used;
1294 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
1296 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
1298 if (fifo_bytes_used < 128)
1299 return 128 - fifo_bytes_used;
1304 static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
1306 while (snd_hdsp_midi_input_available (hdsp, id))
1307 snd_hdsp_midi_read_byte (hdsp, id);
1310 static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
1312 unsigned long flags;
1316 unsigned char buf[128];
1318 /* Output is not interrupt driven */
1320 spin_lock_irqsave (&hmidi->lock, flags);
1321 if (hmidi->output) {
1322 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1323 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1324 if (n_pending > (int)sizeof (buf))
1325 n_pending = sizeof (buf);
1327 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
1328 for (i = 0; i < to_write; ++i)
1329 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1334 spin_unlock_irqrestore (&hmidi->lock, flags);
1338 static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
1340 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1341 unsigned long flags;
1345 spin_lock_irqsave (&hmidi->lock, flags);
1346 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1348 if (n_pending > (int)sizeof (buf))
1349 n_pending = sizeof (buf);
1350 for (i = 0; i < n_pending; ++i)
1351 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1353 snd_rawmidi_receive (hmidi->input, buf, n_pending);
1355 /* flush the MIDI input FIFO */
1357 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1362 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1364 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
1365 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1366 spin_unlock_irqrestore (&hmidi->lock, flags);
1367 return snd_hdsp_midi_output_write (hmidi);
1370 static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1373 struct hdsp_midi *hmidi;
1374 unsigned long flags;
1377 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1379 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1380 spin_lock_irqsave (&hdsp->lock, flags);
1382 if (!(hdsp->control_register & ie)) {
1383 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1384 hdsp->control_register |= ie;
1387 hdsp->control_register &= ~ie;
1390 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1391 spin_unlock_irqrestore (&hdsp->lock, flags);
1394 static void snd_hdsp_midi_output_timer(struct timer_list *t)
1396 struct hdsp_midi *hmidi = from_timer(hmidi, t, timer);
1397 unsigned long flags;
1399 snd_hdsp_midi_output_write(hmidi);
1400 spin_lock_irqsave (&hmidi->lock, flags);
1402 /* this does not bump hmidi->istimer, because the
1403 kernel automatically removed the timer when it
1404 expired, and we are now adding it back, thus
1405 leaving istimer wherever it was set before.
1409 mod_timer(&hmidi->timer, 1 + jiffies);
1411 spin_unlock_irqrestore (&hmidi->lock, flags);
1414 static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1416 struct hdsp_midi *hmidi;
1417 unsigned long flags;
1419 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1420 spin_lock_irqsave (&hmidi->lock, flags);
1422 if (!hmidi->istimer) {
1423 timer_setup(&hmidi->timer, snd_hdsp_midi_output_timer,
1425 mod_timer(&hmidi->timer, 1 + jiffies);
1429 if (hmidi->istimer && --hmidi->istimer <= 0)
1430 del_timer (&hmidi->timer);
1432 spin_unlock_irqrestore (&hmidi->lock, flags);
1434 snd_hdsp_midi_output_write(hmidi);
1437 static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
1439 struct hdsp_midi *hmidi;
1441 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1442 spin_lock_irq (&hmidi->lock);
1443 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1444 hmidi->input = substream;
1445 spin_unlock_irq (&hmidi->lock);
1450 static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
1452 struct hdsp_midi *hmidi;
1454 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1455 spin_lock_irq (&hmidi->lock);
1456 hmidi->output = substream;
1457 spin_unlock_irq (&hmidi->lock);
1462 static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
1464 struct hdsp_midi *hmidi;
1466 snd_hdsp_midi_input_trigger (substream, 0);
1468 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1469 spin_lock_irq (&hmidi->lock);
1470 hmidi->input = NULL;
1471 spin_unlock_irq (&hmidi->lock);
1476 static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
1478 struct hdsp_midi *hmidi;
1480 snd_hdsp_midi_output_trigger (substream, 0);
1482 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
1483 spin_lock_irq (&hmidi->lock);
1484 hmidi->output = NULL;
1485 spin_unlock_irq (&hmidi->lock);
1490 static const struct snd_rawmidi_ops snd_hdsp_midi_output =
1492 .open = snd_hdsp_midi_output_open,
1493 .close = snd_hdsp_midi_output_close,
1494 .trigger = snd_hdsp_midi_output_trigger,
1497 static const struct snd_rawmidi_ops snd_hdsp_midi_input =
1499 .open = snd_hdsp_midi_input_open,
1500 .close = snd_hdsp_midi_input_close,
1501 .trigger = snd_hdsp_midi_input_trigger,
1504 static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
1508 hdsp->midi[id].id = id;
1509 hdsp->midi[id].rmidi = NULL;
1510 hdsp->midi[id].input = NULL;
1511 hdsp->midi[id].output = NULL;
1512 hdsp->midi[id].hdsp = hdsp;
1513 hdsp->midi[id].istimer = 0;
1514 hdsp->midi[id].pending = 0;
1515 spin_lock_init (&hdsp->midi[id].lock);
1517 snprintf(buf, sizeof(buf), "%s MIDI %d", card->shortname, id + 1);
1518 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
1521 sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
1522 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1524 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1525 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1527 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1528 SNDRV_RAWMIDI_INFO_INPUT |
1529 SNDRV_RAWMIDI_INFO_DUPLEX;
1534 /*-----------------------------------------------------------------------------
1536 ----------------------------------------------------------------------------*/
1538 static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
1541 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1542 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1543 if (val & HDSP_SPDIFProfessional)
1544 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1546 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1550 static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
1552 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1553 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1554 if (val & HDSP_SPDIFProfessional)
1555 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1557 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1560 static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1562 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1567 static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1569 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1571 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1575 static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1577 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1581 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1582 spin_lock_irq(&hdsp->lock);
1583 change = val != hdsp->creg_spdif;
1584 hdsp->creg_spdif = val;
1585 spin_unlock_irq(&hdsp->lock);
1589 static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1591 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1596 static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1598 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1600 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1604 static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1606 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1610 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1611 spin_lock_irq(&hdsp->lock);
1612 change = val != hdsp->creg_spdif_stream;
1613 hdsp->creg_spdif_stream = val;
1614 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1615 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1616 spin_unlock_irq(&hdsp->lock);
1620 static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1622 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1627 static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1629 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1633 #define HDSP_SPDIF_IN(xname, xindex) \
1634 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1637 .info = snd_hdsp_info_spdif_in, \
1638 .get = snd_hdsp_get_spdif_in, \
1639 .put = snd_hdsp_put_spdif_in }
1641 static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
1643 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1646 static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
1648 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1649 hdsp->control_register |= hdsp_encode_spdif_in(in);
1650 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1654 static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1656 static const char * const texts[4] = {
1657 "Optical", "Coaxial", "Internal", "AES"
1659 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1661 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
1665 static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1667 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1669 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1673 static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1675 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1679 if (!snd_hdsp_use_is_exclusive(hdsp))
1681 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1682 spin_lock_irq(&hdsp->lock);
1683 change = val != hdsp_spdif_in(hdsp);
1685 hdsp_set_spdif_input(hdsp, val);
1686 spin_unlock_irq(&hdsp->lock);
1690 #define HDSP_TOGGLE_SETTING(xname, xindex) \
1691 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1693 .private_value = xindex, \
1694 .info = snd_hdsp_info_toggle_setting, \
1695 .get = snd_hdsp_get_toggle_setting, \
1696 .put = snd_hdsp_put_toggle_setting \
1699 static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
1701 return (hdsp->control_register & regmask) ? 1 : 0;
1704 static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
1707 hdsp->control_register |= regmask;
1709 hdsp->control_register &= ~regmask;
1710 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1715 #define snd_hdsp_info_toggle_setting snd_ctl_boolean_mono_info
1717 static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
1718 struct snd_ctl_elem_value *ucontrol)
1720 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1721 u32 regmask = kcontrol->private_value;
1723 spin_lock_irq(&hdsp->lock);
1724 ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
1725 spin_unlock_irq(&hdsp->lock);
1729 static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
1732 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1733 u32 regmask = kcontrol->private_value;
1737 if (!snd_hdsp_use_is_exclusive(hdsp))
1739 val = ucontrol->value.integer.value[0] & 1;
1740 spin_lock_irq(&hdsp->lock);
1741 change = (int) val != hdsp_toggle_setting(hdsp, regmask);
1743 hdsp_set_toggle_setting(hdsp, regmask, val);
1744 spin_unlock_irq(&hdsp->lock);
1748 #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
1749 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1752 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1753 .info = snd_hdsp_info_spdif_sample_rate, \
1754 .get = snd_hdsp_get_spdif_sample_rate \
1757 static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1759 static const char * const texts[] = {
1760 "32000", "44100", "48000", "64000", "88200", "96000",
1761 "None", "128000", "176400", "192000"
1763 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1765 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
1769 static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1771 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1773 switch (hdsp_spdif_sample_rate(hdsp)) {
1775 ucontrol->value.enumerated.item[0] = 0;
1778 ucontrol->value.enumerated.item[0] = 1;
1781 ucontrol->value.enumerated.item[0] = 2;
1784 ucontrol->value.enumerated.item[0] = 3;
1787 ucontrol->value.enumerated.item[0] = 4;
1790 ucontrol->value.enumerated.item[0] = 5;
1793 ucontrol->value.enumerated.item[0] = 7;
1796 ucontrol->value.enumerated.item[0] = 8;
1799 ucontrol->value.enumerated.item[0] = 9;
1802 ucontrol->value.enumerated.item[0] = 6;
1807 #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
1808 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1811 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1812 .info = snd_hdsp_info_system_sample_rate, \
1813 .get = snd_hdsp_get_system_sample_rate \
1816 static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1818 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1823 static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1825 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1827 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1831 #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
1832 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1835 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1836 .info = snd_hdsp_info_autosync_sample_rate, \
1837 .get = snd_hdsp_get_autosync_sample_rate \
1840 static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1842 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1843 static const char * const texts[] = {
1844 "32000", "44100", "48000", "64000", "88200", "96000",
1845 "None", "128000", "176400", "192000"
1848 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
1852 static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1854 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1856 switch (hdsp_external_sample_rate(hdsp)) {
1858 ucontrol->value.enumerated.item[0] = 0;
1861 ucontrol->value.enumerated.item[0] = 1;
1864 ucontrol->value.enumerated.item[0] = 2;
1867 ucontrol->value.enumerated.item[0] = 3;
1870 ucontrol->value.enumerated.item[0] = 4;
1873 ucontrol->value.enumerated.item[0] = 5;
1876 ucontrol->value.enumerated.item[0] = 7;
1879 ucontrol->value.enumerated.item[0] = 8;
1882 ucontrol->value.enumerated.item[0] = 9;
1885 ucontrol->value.enumerated.item[0] = 6;
1890 #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
1891 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1894 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1895 .info = snd_hdsp_info_system_clock_mode, \
1896 .get = snd_hdsp_get_system_clock_mode \
1899 static int hdsp_system_clock_mode(struct hdsp *hdsp)
1901 if (hdsp->control_register & HDSP_ClockModeMaster)
1903 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
1908 static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1910 static const char * const texts[] = {"Master", "Slave" };
1912 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1915 static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1917 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1919 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1923 #define HDSP_CLOCK_SOURCE(xname, xindex) \
1924 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1927 .info = snd_hdsp_info_clock_source, \
1928 .get = snd_hdsp_get_clock_source, \
1929 .put = snd_hdsp_put_clock_source \
1932 static int hdsp_clock_source(struct hdsp *hdsp)
1934 if (hdsp->control_register & HDSP_ClockModeMaster) {
1935 switch (hdsp->system_sample_rate) {
1962 static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
1966 case HDSP_CLOCK_SOURCE_AUTOSYNC:
1967 if (hdsp_external_sample_rate(hdsp) != 0) {
1968 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
1969 hdsp->control_register &= ~HDSP_ClockModeMaster;
1970 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1975 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
1978 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
1981 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
1984 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
1987 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
1990 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
1993 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
1996 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
1999 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2005 hdsp->control_register |= HDSP_ClockModeMaster;
2006 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2007 hdsp_set_rate(hdsp, rate, 1);
2011 static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2013 static const char * const texts[] = {
2014 "AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz",
2015 "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz",
2016 "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz",
2017 "Internal 192.0 KHz"
2019 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2021 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
2025 static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2027 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2029 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2033 static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2035 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2039 if (!snd_hdsp_use_is_exclusive(hdsp))
2041 val = ucontrol->value.enumerated.item[0];
2042 if (val < 0) val = 0;
2043 if (hdsp->io_type == H9632) {
2050 spin_lock_irq(&hdsp->lock);
2051 if (val != hdsp_clock_source(hdsp))
2052 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2055 spin_unlock_irq(&hdsp->lock);
2059 #define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
2061 static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2063 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2065 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2069 static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2071 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2074 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2076 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
2080 #define HDSP_DA_GAIN(xname, xindex) \
2081 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2084 .info = snd_hdsp_info_da_gain, \
2085 .get = snd_hdsp_get_da_gain, \
2086 .put = snd_hdsp_put_da_gain \
2089 static int hdsp_da_gain(struct hdsp *hdsp)
2091 switch (hdsp->control_register & HDSP_DAGainMask) {
2092 case HDSP_DAGainHighGain:
2094 case HDSP_DAGainPlus4dBu:
2096 case HDSP_DAGainMinus10dBV:
2103 static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
2105 hdsp->control_register &= ~HDSP_DAGainMask;
2108 hdsp->control_register |= HDSP_DAGainHighGain;
2111 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2114 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2120 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2124 static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2126 static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
2128 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2131 static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2133 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2135 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2139 static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2141 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2145 if (!snd_hdsp_use_is_exclusive(hdsp))
2147 val = ucontrol->value.enumerated.item[0];
2148 if (val < 0) val = 0;
2149 if (val > 2) val = 2;
2150 spin_lock_irq(&hdsp->lock);
2151 if (val != hdsp_da_gain(hdsp))
2152 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2155 spin_unlock_irq(&hdsp->lock);
2159 #define HDSP_AD_GAIN(xname, xindex) \
2160 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2163 .info = snd_hdsp_info_ad_gain, \
2164 .get = snd_hdsp_get_ad_gain, \
2165 .put = snd_hdsp_put_ad_gain \
2168 static int hdsp_ad_gain(struct hdsp *hdsp)
2170 switch (hdsp->control_register & HDSP_ADGainMask) {
2171 case HDSP_ADGainMinus10dBV:
2173 case HDSP_ADGainPlus4dBu:
2175 case HDSP_ADGainLowGain:
2182 static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
2184 hdsp->control_register &= ~HDSP_ADGainMask;
2187 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2190 hdsp->control_register |= HDSP_ADGainPlus4dBu;
2193 hdsp->control_register |= HDSP_ADGainLowGain;
2199 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2203 static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2205 static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
2207 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2210 static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2212 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2214 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2218 static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2220 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2224 if (!snd_hdsp_use_is_exclusive(hdsp))
2226 val = ucontrol->value.enumerated.item[0];
2227 if (val < 0) val = 0;
2228 if (val > 2) val = 2;
2229 spin_lock_irq(&hdsp->lock);
2230 if (val != hdsp_ad_gain(hdsp))
2231 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2234 spin_unlock_irq(&hdsp->lock);
2238 #define HDSP_PHONE_GAIN(xname, xindex) \
2239 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2242 .info = snd_hdsp_info_phone_gain, \
2243 .get = snd_hdsp_get_phone_gain, \
2244 .put = snd_hdsp_put_phone_gain \
2247 static int hdsp_phone_gain(struct hdsp *hdsp)
2249 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2250 case HDSP_PhoneGain0dB:
2252 case HDSP_PhoneGainMinus6dB:
2254 case HDSP_PhoneGainMinus12dB:
2261 static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
2263 hdsp->control_register &= ~HDSP_PhoneGainMask;
2266 hdsp->control_register |= HDSP_PhoneGain0dB;
2269 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
2272 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2278 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2282 static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2284 static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
2286 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2289 static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2291 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2293 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2297 static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2299 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2303 if (!snd_hdsp_use_is_exclusive(hdsp))
2305 val = ucontrol->value.enumerated.item[0];
2306 if (val < 0) val = 0;
2307 if (val > 2) val = 2;
2308 spin_lock_irq(&hdsp->lock);
2309 if (val != hdsp_phone_gain(hdsp))
2310 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2313 spin_unlock_irq(&hdsp->lock);
2317 #define HDSP_PREF_SYNC_REF(xname, xindex) \
2318 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2321 .info = snd_hdsp_info_pref_sync_ref, \
2322 .get = snd_hdsp_get_pref_sync_ref, \
2323 .put = snd_hdsp_put_pref_sync_ref \
2326 static int hdsp_pref_sync_ref(struct hdsp *hdsp)
2328 /* Notice that this looks at the requested sync source,
2329 not the one actually in use.
2332 switch (hdsp->control_register & HDSP_SyncRefMask) {
2333 case HDSP_SyncRef_ADAT1:
2334 return HDSP_SYNC_FROM_ADAT1;
2335 case HDSP_SyncRef_ADAT2:
2336 return HDSP_SYNC_FROM_ADAT2;
2337 case HDSP_SyncRef_ADAT3:
2338 return HDSP_SYNC_FROM_ADAT3;
2339 case HDSP_SyncRef_SPDIF:
2340 return HDSP_SYNC_FROM_SPDIF;
2341 case HDSP_SyncRef_WORD:
2342 return HDSP_SYNC_FROM_WORD;
2343 case HDSP_SyncRef_ADAT_SYNC:
2344 return HDSP_SYNC_FROM_ADAT_SYNC;
2346 return HDSP_SYNC_FROM_WORD;
2351 static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
2353 hdsp->control_register &= ~HDSP_SyncRefMask;
2355 case HDSP_SYNC_FROM_ADAT1:
2356 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2358 case HDSP_SYNC_FROM_ADAT2:
2359 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2361 case HDSP_SYNC_FROM_ADAT3:
2362 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2364 case HDSP_SYNC_FROM_SPDIF:
2365 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2367 case HDSP_SYNC_FROM_WORD:
2368 hdsp->control_register |= HDSP_SyncRef_WORD;
2370 case HDSP_SYNC_FROM_ADAT_SYNC:
2371 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2376 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2380 static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2382 static const char * const texts[] = {
2383 "Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
2385 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2388 switch (hdsp->io_type) {
2403 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
2406 static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2408 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2410 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2414 static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2416 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2420 if (!snd_hdsp_use_is_exclusive(hdsp))
2423 switch (hdsp->io_type) {
2438 val = ucontrol->value.enumerated.item[0] % max;
2439 spin_lock_irq(&hdsp->lock);
2440 change = (int)val != hdsp_pref_sync_ref(hdsp);
2441 hdsp_set_pref_sync_ref(hdsp, val);
2442 spin_unlock_irq(&hdsp->lock);
2446 #define HDSP_AUTOSYNC_REF(xname, xindex) \
2447 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2450 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2451 .info = snd_hdsp_info_autosync_ref, \
2452 .get = snd_hdsp_get_autosync_ref, \
2455 static int hdsp_autosync_ref(struct hdsp *hdsp)
2457 /* This looks at the autosync selected sync reference */
2458 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2460 switch (status2 & HDSP_SelSyncRefMask) {
2461 case HDSP_SelSyncRef_WORD:
2462 return HDSP_AUTOSYNC_FROM_WORD;
2463 case HDSP_SelSyncRef_ADAT_SYNC:
2464 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2465 case HDSP_SelSyncRef_SPDIF:
2466 return HDSP_AUTOSYNC_FROM_SPDIF;
2467 case HDSP_SelSyncRefMask:
2468 return HDSP_AUTOSYNC_FROM_NONE;
2469 case HDSP_SelSyncRef_ADAT1:
2470 return HDSP_AUTOSYNC_FROM_ADAT1;
2471 case HDSP_SelSyncRef_ADAT2:
2472 return HDSP_AUTOSYNC_FROM_ADAT2;
2473 case HDSP_SelSyncRef_ADAT3:
2474 return HDSP_AUTOSYNC_FROM_ADAT3;
2476 return HDSP_AUTOSYNC_FROM_WORD;
2481 static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2483 static const char * const texts[] = {
2484 "Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
2487 return snd_ctl_enum_info(uinfo, 1, 7, texts);
2490 static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2492 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2494 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2498 #define HDSP_PRECISE_POINTER(xname, xindex) \
2499 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
2502 .info = snd_hdsp_info_precise_pointer, \
2503 .get = snd_hdsp_get_precise_pointer, \
2504 .put = snd_hdsp_put_precise_pointer \
2507 static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
2510 hdsp->precise_ptr = 1;
2512 hdsp->precise_ptr = 0;
2516 #define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
2518 static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2520 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2522 spin_lock_irq(&hdsp->lock);
2523 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2524 spin_unlock_irq(&hdsp->lock);
2528 static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2530 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2534 if (!snd_hdsp_use_is_exclusive(hdsp))
2536 val = ucontrol->value.integer.value[0] & 1;
2537 spin_lock_irq(&hdsp->lock);
2538 change = (int)val != hdsp->precise_ptr;
2539 hdsp_set_precise_pointer(hdsp, val);
2540 spin_unlock_irq(&hdsp->lock);
2544 #define HDSP_USE_MIDI_WORK(xname, xindex) \
2545 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
2548 .info = snd_hdsp_info_use_midi_work, \
2549 .get = snd_hdsp_get_use_midi_work, \
2550 .put = snd_hdsp_put_use_midi_work \
2553 static int hdsp_set_use_midi_work(struct hdsp *hdsp, int use_work)
2556 hdsp->use_midi_work = 1;
2558 hdsp->use_midi_work = 0;
2562 #define snd_hdsp_info_use_midi_work snd_ctl_boolean_mono_info
2564 static int snd_hdsp_get_use_midi_work(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2566 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2568 spin_lock_irq(&hdsp->lock);
2569 ucontrol->value.integer.value[0] = hdsp->use_midi_work;
2570 spin_unlock_irq(&hdsp->lock);
2574 static int snd_hdsp_put_use_midi_work(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2576 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2580 if (!snd_hdsp_use_is_exclusive(hdsp))
2582 val = ucontrol->value.integer.value[0] & 1;
2583 spin_lock_irq(&hdsp->lock);
2584 change = (int)val != hdsp->use_midi_work;
2585 hdsp_set_use_midi_work(hdsp, val);
2586 spin_unlock_irq(&hdsp->lock);
2590 #define HDSP_MIXER(xname, xindex) \
2591 { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2595 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2596 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2597 .info = snd_hdsp_info_mixer, \
2598 .get = snd_hdsp_get_mixer, \
2599 .put = snd_hdsp_put_mixer \
2602 static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2604 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2606 uinfo->value.integer.min = 0;
2607 uinfo->value.integer.max = 65536;
2608 uinfo->value.integer.step = 1;
2612 static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2614 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2619 source = ucontrol->value.integer.value[0];
2620 destination = ucontrol->value.integer.value[1];
2622 if (source >= hdsp->max_channels)
2623 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2625 addr = hdsp_input_to_output_key(hdsp,source, destination);
2627 spin_lock_irq(&hdsp->lock);
2628 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2629 spin_unlock_irq(&hdsp->lock);
2633 static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2635 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2642 if (!snd_hdsp_use_is_exclusive(hdsp))
2645 source = ucontrol->value.integer.value[0];
2646 destination = ucontrol->value.integer.value[1];
2648 if (source >= hdsp->max_channels)
2649 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2651 addr = hdsp_input_to_output_key(hdsp,source, destination);
2653 gain = ucontrol->value.integer.value[2];
2655 spin_lock_irq(&hdsp->lock);
2656 change = gain != hdsp_read_gain(hdsp, addr);
2658 hdsp_write_gain(hdsp, addr, gain);
2659 spin_unlock_irq(&hdsp->lock);
2663 #define HDSP_WC_SYNC_CHECK(xname, xindex) \
2664 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2667 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2668 .info = snd_hdsp_info_sync_check, \
2669 .get = snd_hdsp_get_wc_sync_check \
2672 static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2674 static const char * const texts[] = {"No Lock", "Lock", "Sync" };
2676 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2679 static int hdsp_wc_sync_check(struct hdsp *hdsp)
2681 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2682 if (status2 & HDSP_wc_lock) {
2683 if (status2 & HDSP_wc_sync)
2692 static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2694 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2696 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2700 #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
2701 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2704 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2705 .info = snd_hdsp_info_sync_check, \
2706 .get = snd_hdsp_get_spdif_sync_check \
2709 static int hdsp_spdif_sync_check(struct hdsp *hdsp)
2711 int status = hdsp_read(hdsp, HDSP_statusRegister);
2712 if (status & HDSP_SPDIFErrorFlag)
2715 if (status & HDSP_SPDIFSync)
2723 static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2725 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2727 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
2731 #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
2732 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2735 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2736 .info = snd_hdsp_info_sync_check, \
2737 .get = snd_hdsp_get_adatsync_sync_check \
2740 static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
2742 int status = hdsp_read(hdsp, HDSP_statusRegister);
2743 if (status & HDSP_TimecodeLock) {
2744 if (status & HDSP_TimecodeSync)
2752 static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2754 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2756 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
2760 #define HDSP_ADAT_SYNC_CHECK \
2761 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2762 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2763 .info = snd_hdsp_info_sync_check, \
2764 .get = snd_hdsp_get_adat_sync_check \
2767 static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
2769 int status = hdsp_read(hdsp, HDSP_statusRegister);
2771 if (status & (HDSP_Lock0>>idx)) {
2772 if (status & (HDSP_Sync0>>idx))
2780 static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2783 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2785 offset = ucontrol->id.index - 1;
2786 if (snd_BUG_ON(offset < 0))
2789 switch (hdsp->io_type) {
2804 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
2808 #define HDSP_DDS_OFFSET(xname, xindex) \
2809 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2812 .info = snd_hdsp_info_dds_offset, \
2813 .get = snd_hdsp_get_dds_offset, \
2814 .put = snd_hdsp_put_dds_offset \
2817 static int hdsp_dds_offset(struct hdsp *hdsp)
2820 unsigned int dds_value = hdsp->dds_value;
2821 int system_sample_rate = hdsp->system_sample_rate;
2828 * dds_value = n / rate
2829 * rate = n / dds_value
2831 n = div_u64(n, dds_value);
2832 if (system_sample_rate >= 112000)
2834 else if (system_sample_rate >= 56000)
2836 return ((int)n) - system_sample_rate;
2839 static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
2841 int rate = hdsp->system_sample_rate + offset_hz;
2842 hdsp_set_dds_value(hdsp, rate);
2846 static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2848 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2850 uinfo->value.integer.min = -5000;
2851 uinfo->value.integer.max = 5000;
2855 static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2857 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2859 ucontrol->value.integer.value[0] = hdsp_dds_offset(hdsp);
2863 static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2865 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2869 if (!snd_hdsp_use_is_exclusive(hdsp))
2871 val = ucontrol->value.integer.value[0];
2872 spin_lock_irq(&hdsp->lock);
2873 if (val != hdsp_dds_offset(hdsp))
2874 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
2877 spin_unlock_irq(&hdsp->lock);
2881 static const struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
2882 HDSP_DA_GAIN("DA Gain", 0),
2883 HDSP_AD_GAIN("AD Gain", 0),
2884 HDSP_PHONE_GAIN("Phones Gain", 0),
2885 HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
2886 HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
2889 static const struct snd_kcontrol_new snd_hdsp_controls[] = {
2891 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2892 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2893 .info = snd_hdsp_control_spdif_info,
2894 .get = snd_hdsp_control_spdif_get,
2895 .put = snd_hdsp_control_spdif_put,
2898 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2899 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2900 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2901 .info = snd_hdsp_control_spdif_stream_info,
2902 .get = snd_hdsp_control_spdif_stream_get,
2903 .put = snd_hdsp_control_spdif_stream_put,
2906 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2907 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2908 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2909 .info = snd_hdsp_control_spdif_mask_info,
2910 .get = snd_hdsp_control_spdif_mask_get,
2911 .private_value = IEC958_AES0_NONAUDIO |
2912 IEC958_AES0_PROFESSIONAL |
2913 IEC958_AES0_CON_EMPHASIS,
2916 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2917 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2918 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2919 .info = snd_hdsp_control_spdif_mask_info,
2920 .get = snd_hdsp_control_spdif_mask_get,
2921 .private_value = IEC958_AES0_NONAUDIO |
2922 IEC958_AES0_PROFESSIONAL |
2923 IEC958_AES0_PRO_EMPHASIS,
2925 HDSP_MIXER("Mixer", 0),
2926 HDSP_SPDIF_IN("IEC958 Input Connector", 0),
2927 HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
2928 HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
2929 HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
2930 HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
2931 /* 'Sample Clock Source' complies with the alsa control naming scheme */
2932 HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
2934 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2935 .name = "Sample Clock Source Locking",
2936 .info = snd_hdsp_info_clock_source_lock,
2937 .get = snd_hdsp_get_clock_source_lock,
2938 .put = snd_hdsp_put_clock_source_lock,
2940 HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
2941 HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
2942 HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
2943 HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
2944 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
2945 /* 'External Rate' complies with the alsa control naming scheme */
2946 HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
2947 HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
2948 HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
2949 HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
2950 HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
2951 HDSP_PRECISE_POINTER("Precise Pointer", 0),
2952 HDSP_USE_MIDI_WORK("Use Midi Tasklet", 0),
2956 static int hdsp_rpm_input12(struct hdsp *hdsp)
2958 switch (hdsp->control_register & HDSP_RPM_Inp12) {
2959 case HDSP_RPM_Inp12_Phon_6dB:
2961 case HDSP_RPM_Inp12_Phon_n6dB:
2963 case HDSP_RPM_Inp12_Line_0dB:
2965 case HDSP_RPM_Inp12_Line_n6dB:
2972 static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2974 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2976 ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
2981 static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
2983 hdsp->control_register &= ~HDSP_RPM_Inp12;
2986 hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
2991 hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
2994 hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
2997 hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
3003 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3008 static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3010 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3014 if (!snd_hdsp_use_is_exclusive(hdsp))
3016 val = ucontrol->value.enumerated.item[0];
3021 spin_lock_irq(&hdsp->lock);
3022 if (val != hdsp_rpm_input12(hdsp))
3023 change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
3026 spin_unlock_irq(&hdsp->lock);
3031 static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3033 static const char * const texts[] = {
3034 "Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
3037 return snd_ctl_enum_info(uinfo, 1, 5, texts);
3041 static int hdsp_rpm_input34(struct hdsp *hdsp)
3043 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3044 case HDSP_RPM_Inp34_Phon_6dB:
3046 case HDSP_RPM_Inp34_Phon_n6dB:
3048 case HDSP_RPM_Inp34_Line_0dB:
3050 case HDSP_RPM_Inp34_Line_n6dB:
3057 static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3059 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3061 ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
3066 static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
3068 hdsp->control_register &= ~HDSP_RPM_Inp34;
3071 hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
3076 hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
3079 hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
3082 hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
3088 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3093 static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3095 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3099 if (!snd_hdsp_use_is_exclusive(hdsp))
3101 val = ucontrol->value.enumerated.item[0];
3106 spin_lock_irq(&hdsp->lock);
3107 if (val != hdsp_rpm_input34(hdsp))
3108 change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
3111 spin_unlock_irq(&hdsp->lock);
3116 /* RPM Bypass switch */
3117 static int hdsp_rpm_bypass(struct hdsp *hdsp)
3119 return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
3123 static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3125 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3127 ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
3132 static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
3135 hdsp->control_register |= HDSP_RPM_Bypass;
3137 hdsp->control_register &= ~HDSP_RPM_Bypass;
3138 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3143 static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3145 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3149 if (!snd_hdsp_use_is_exclusive(hdsp))
3151 val = ucontrol->value.integer.value[0] & 1;
3152 spin_lock_irq(&hdsp->lock);
3153 change = (int)val != hdsp_rpm_bypass(hdsp);
3154 hdsp_set_rpm_bypass(hdsp, val);
3155 spin_unlock_irq(&hdsp->lock);
3160 static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3162 static const char * const texts[] = {"On", "Off"};
3164 return snd_ctl_enum_info(uinfo, 1, 2, texts);
3168 /* RPM Disconnect switch */
3169 static int hdsp_rpm_disconnect(struct hdsp *hdsp)
3171 return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
3175 static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3177 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3179 ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
3184 static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
3187 hdsp->control_register |= HDSP_RPM_Disconnect;
3189 hdsp->control_register &= ~HDSP_RPM_Disconnect;
3190 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3195 static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3197 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3201 if (!snd_hdsp_use_is_exclusive(hdsp))
3203 val = ucontrol->value.integer.value[0] & 1;
3204 spin_lock_irq(&hdsp->lock);
3205 change = (int)val != hdsp_rpm_disconnect(hdsp);
3206 hdsp_set_rpm_disconnect(hdsp, val);
3207 spin_unlock_irq(&hdsp->lock);
3211 static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3213 static const char * const texts[] = {"On", "Off"};
3215 return snd_ctl_enum_info(uinfo, 1, 2, texts);
3218 static const struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
3220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3221 .name = "RPM Bypass",
3222 .get = snd_hdsp_get_rpm_bypass,
3223 .put = snd_hdsp_put_rpm_bypass,
3224 .info = snd_hdsp_info_rpm_bypass
3227 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3228 .name = "RPM Disconnect",
3229 .get = snd_hdsp_get_rpm_disconnect,
3230 .put = snd_hdsp_put_rpm_disconnect,
3231 .info = snd_hdsp_info_rpm_disconnect
3234 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3235 .name = "Input 1/2",
3236 .get = snd_hdsp_get_rpm_input12,
3237 .put = snd_hdsp_put_rpm_input12,
3238 .info = snd_hdsp_info_rpm_input
3241 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3242 .name = "Input 3/4",
3243 .get = snd_hdsp_get_rpm_input34,
3244 .put = snd_hdsp_put_rpm_input34,
3245 .info = snd_hdsp_info_rpm_input
3247 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3248 HDSP_MIXER("Mixer", 0)
3251 static const struct snd_kcontrol_new snd_hdsp_96xx_aeb =
3252 HDSP_TOGGLE_SETTING("Analog Extension Board",
3253 HDSP_AnalogExtensionBoard);
3254 static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
3256 static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
3260 struct snd_kcontrol *kctl;
3262 if (hdsp->io_type == RPM) {
3263 /* RPM Bypass, Disconnect and Input switches */
3264 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3265 err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
3272 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
3273 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
3275 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3276 hdsp->spdif_ctl = kctl;
3279 /* ADAT SyncCheck status */
3280 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3281 snd_hdsp_adat_sync_check.index = 1;
3282 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
3284 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3285 for (idx = 1; idx < 3; ++idx) {
3286 snd_hdsp_adat_sync_check.index = idx+1;
3287 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
3292 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3293 if (hdsp->io_type == H9632) {
3294 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
3295 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
3300 /* AEB control for H96xx card */
3301 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
3302 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
3309 /*------------------------------------------------------------
3311 ------------------------------------------------------------*/
3314 snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3316 struct hdsp *hdsp = entry->private_data;
3317 unsigned int status;
3318 unsigned int status2;
3319 char *pref_sync_ref;
3321 char *system_clock_mode;
3325 status = hdsp_read(hdsp, HDSP_statusRegister);
3326 status2 = hdsp_read(hdsp, HDSP_status2Register);
3328 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3329 hdsp->card->number + 1);
3330 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3331 hdsp->capture_buffer, hdsp->playback_buffer);
3332 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3333 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3334 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3335 snd_iprintf(buffer, "Control2 register: 0x%x\n",
3336 hdsp->control2_register);
3337 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3338 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3340 if (hdsp_check_for_iobox(hdsp)) {
3341 snd_iprintf(buffer, "No I/O box connected.\n"
3342 "Please connect one and upload firmware.\n");
3346 if (hdsp_check_for_firmware(hdsp, 0)) {
3347 if (hdsp->state & HDSP_FirmwareCached) {
3348 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3349 snd_iprintf(buffer, "Firmware loading from "
3351 "please upload manually.\n");
3357 err = hdsp_request_fw_loader(hdsp);
3360 "No firmware loaded nor cached, "
3361 "please upload firmware.\n");
3367 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3368 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3369 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3370 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3371 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3372 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_work ? "on" : "off");
3374 snd_iprintf(buffer, "\n");
3376 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3378 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3379 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3380 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3381 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3383 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3385 snd_iprintf(buffer, "\n");
3387 switch (hdsp_clock_source(hdsp)) {
3388 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3389 clock_source = "AutoSync";
3391 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3392 clock_source = "Internal 32 kHz";
3394 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3395 clock_source = "Internal 44.1 kHz";
3397 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3398 clock_source = "Internal 48 kHz";
3400 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3401 clock_source = "Internal 64 kHz";
3403 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3404 clock_source = "Internal 88.2 kHz";
3406 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3407 clock_source = "Internal 96 kHz";
3409 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3410 clock_source = "Internal 128 kHz";
3412 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3413 clock_source = "Internal 176.4 kHz";
3415 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3416 clock_source = "Internal 192 kHz";
3419 clock_source = "Error";
3421 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
3423 if (hdsp_system_clock_mode(hdsp))
3424 system_clock_mode = "Slave";
3426 system_clock_mode = "Master";
3428 switch (hdsp_pref_sync_ref (hdsp)) {
3429 case HDSP_SYNC_FROM_WORD:
3430 pref_sync_ref = "Word Clock";
3432 case HDSP_SYNC_FROM_ADAT_SYNC:
3433 pref_sync_ref = "ADAT Sync";
3435 case HDSP_SYNC_FROM_SPDIF:
3436 pref_sync_ref = "SPDIF";
3438 case HDSP_SYNC_FROM_ADAT1:
3439 pref_sync_ref = "ADAT1";
3441 case HDSP_SYNC_FROM_ADAT2:
3442 pref_sync_ref = "ADAT2";
3444 case HDSP_SYNC_FROM_ADAT3:
3445 pref_sync_ref = "ADAT3";
3448 pref_sync_ref = "Word Clock";
3451 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
3453 switch (hdsp_autosync_ref (hdsp)) {
3454 case HDSP_AUTOSYNC_FROM_WORD:
3455 autosync_ref = "Word Clock";
3457 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3458 autosync_ref = "ADAT Sync";
3460 case HDSP_AUTOSYNC_FROM_SPDIF:
3461 autosync_ref = "SPDIF";
3463 case HDSP_AUTOSYNC_FROM_NONE:
3464 autosync_ref = "None";
3466 case HDSP_AUTOSYNC_FROM_ADAT1:
3467 autosync_ref = "ADAT1";
3469 case HDSP_AUTOSYNC_FROM_ADAT2:
3470 autosync_ref = "ADAT2";
3472 case HDSP_AUTOSYNC_FROM_ADAT3:
3473 autosync_ref = "ADAT3";
3476 autosync_ref = "---";
3479 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
3481 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
3483 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3485 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3486 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
3488 snd_iprintf(buffer, "\n");
3490 if (hdsp->io_type != RPM) {
3491 switch (hdsp_spdif_in(hdsp)) {
3492 case HDSP_SPDIFIN_OPTICAL:
3493 snd_iprintf(buffer, "IEC958 input: Optical\n");
3495 case HDSP_SPDIFIN_COAXIAL:
3496 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3498 case HDSP_SPDIFIN_INTERNAL:
3499 snd_iprintf(buffer, "IEC958 input: Internal\n");
3501 case HDSP_SPDIFIN_AES:
3502 snd_iprintf(buffer, "IEC958 input: AES\n");
3505 snd_iprintf(buffer, "IEC958 input: ???\n");
3510 if (RPM == hdsp->io_type) {
3511 if (hdsp->control_register & HDSP_RPM_Bypass)
3512 snd_iprintf(buffer, "RPM Bypass: disabled\n");
3514 snd_iprintf(buffer, "RPM Bypass: enabled\n");
3515 if (hdsp->control_register & HDSP_RPM_Disconnect)
3516 snd_iprintf(buffer, "RPM disconnected\n");
3518 snd_iprintf(buffer, "RPM connected\n");
3520 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3521 case HDSP_RPM_Inp12_Phon_6dB:
3522 snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
3524 case HDSP_RPM_Inp12_Phon_0dB:
3525 snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
3527 case HDSP_RPM_Inp12_Phon_n6dB:
3528 snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
3530 case HDSP_RPM_Inp12_Line_0dB:
3531 snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
3533 case HDSP_RPM_Inp12_Line_n6dB:
3534 snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
3537 snd_iprintf(buffer, "Input 1/2: ???\n");
3540 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3541 case HDSP_RPM_Inp34_Phon_6dB:
3542 snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
3544 case HDSP_RPM_Inp34_Phon_0dB:
3545 snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
3547 case HDSP_RPM_Inp34_Phon_n6dB:
3548 snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
3550 case HDSP_RPM_Inp34_Line_0dB:
3551 snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
3553 case HDSP_RPM_Inp34_Line_n6dB:
3554 snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
3557 snd_iprintf(buffer, "Input 3/4: ???\n");
3561 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3562 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3564 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
3566 if (hdsp->control_register & HDSP_SPDIFProfessional)
3567 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3569 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3571 if (hdsp->control_register & HDSP_SPDIFEmphasis)
3572 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3574 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3576 if (hdsp->control_register & HDSP_SPDIFNonAudio)
3577 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3579 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3580 x = hdsp_spdif_sample_rate(hdsp);
3582 snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3584 snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
3586 snd_iprintf(buffer, "\n");
3589 x = status & HDSP_Sync0;
3590 if (status & HDSP_Lock0)
3591 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3593 snd_iprintf(buffer, "ADAT1: No Lock\n");
3595 switch (hdsp->io_type) {
3598 x = status & HDSP_Sync1;
3599 if (status & HDSP_Lock1)
3600 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3602 snd_iprintf(buffer, "ADAT2: No Lock\n");
3603 x = status & HDSP_Sync2;
3604 if (status & HDSP_Lock2)
3605 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3607 snd_iprintf(buffer, "ADAT3: No Lock\n");
3614 x = status & HDSP_SPDIFSync;
3615 if (status & HDSP_SPDIFErrorFlag)
3616 snd_iprintf (buffer, "SPDIF: No Lock\n");
3618 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
3620 x = status2 & HDSP_wc_sync;
3621 if (status2 & HDSP_wc_lock)
3622 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3624 snd_iprintf (buffer, "Word Clock: No Lock\n");
3626 x = status & HDSP_TimecodeSync;
3627 if (status & HDSP_TimecodeLock)
3628 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3630 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
3632 snd_iprintf(buffer, "\n");
3634 /* Informations about H9632 specific controls */
3635 if (hdsp->io_type == H9632) {
3638 switch (hdsp_ad_gain(hdsp)) {
3649 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3651 switch (hdsp_da_gain(hdsp)) {
3662 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
3664 switch (hdsp_phone_gain(hdsp)) {
3675 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3677 snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
3678 hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
3681 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
3682 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3684 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
3685 snd_iprintf(buffer, "\n");
3690 static void snd_hdsp_proc_init(struct hdsp *hdsp)
3692 snd_card_ro_proc_new(hdsp->card, "hdsp", hdsp, snd_hdsp_proc_read);
3695 static void snd_hdsp_free_buffers(struct hdsp *hdsp)
3697 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3698 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3701 static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
3703 unsigned long pb_bus, cb_bus;
3705 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3706 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3707 if (hdsp->capture_dma_buf.area)
3708 snd_dma_free_pages(&hdsp->capture_dma_buf);
3709 dev_err(hdsp->card->dev,
3710 "%s: no buffers available\n", hdsp->card_name);
3714 /* Align to bus-space 64K boundary */
3716 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3717 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
3719 /* Tell the card where it is */
3721 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3722 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3724 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3725 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3730 static int snd_hdsp_set_defaults(struct hdsp *hdsp)
3734 /* ASSUMPTION: hdsp->lock is either held, or
3735 there is no need to hold it (e.g. during module
3741 SPDIF Input via Coax
3743 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3744 which implies 2 4096 sample, 32Kbyte periods).
3748 hdsp->control_register = HDSP_ClockModeMaster |
3749 HDSP_SPDIFInputCoaxial |
3750 hdsp_encode_latency(7) |
3754 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3756 #ifdef SNDRV_BIG_ENDIAN
3757 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3759 hdsp->control2_register = 0;
3761 if (hdsp->io_type == H9652)
3762 snd_hdsp_9652_enable_mixer (hdsp);
3764 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
3766 hdsp_reset_hw_pointer(hdsp);
3767 hdsp_compute_period_size(hdsp);
3769 /* silence everything */
3771 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
3772 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
3774 for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3775 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
3779 /* H9632 specific defaults */
3780 if (hdsp->io_type == H9632) {
3781 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3782 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3785 /* set a default rate so that the channel map is set up.
3788 hdsp_set_rate(hdsp, 48000, 1);
3793 static void hdsp_midi_work(struct work_struct *work)
3795 struct hdsp *hdsp = container_of(work, struct hdsp, midi_work);
3797 if (hdsp->midi[0].pending)
3798 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3799 if (hdsp->midi[1].pending)
3800 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3803 static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
3805 struct hdsp *hdsp = (struct hdsp *) dev_id;
3806 unsigned int status;
3810 unsigned int midi0status;
3811 unsigned int midi1status;
3814 status = hdsp_read(hdsp, HDSP_statusRegister);
3816 audio = status & HDSP_audioIRQPending;
3817 midi0 = status & HDSP_midi0IRQPending;
3818 midi1 = status & HDSP_midi1IRQPending;
3820 if (!audio && !midi0 && !midi1)
3823 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3825 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3826 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
3828 if (!(hdsp->state & HDSP_InitializationComplete))
3832 if (hdsp->capture_substream)
3833 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
3835 if (hdsp->playback_substream)
3836 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
3839 if (midi0 && midi0status) {
3840 if (hdsp->use_midi_work) {
3841 /* we disable interrupts for this input until processing is done */
3842 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3843 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3844 hdsp->midi[0].pending = 1;
3847 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3850 if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
3851 if (hdsp->use_midi_work) {
3852 /* we disable interrupts for this input until processing is done */
3853 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3854 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3855 hdsp->midi[1].pending = 1;
3858 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3861 if (hdsp->use_midi_work && schedule)
3862 queue_work(system_highpri_wq, &hdsp->midi_work);
3866 static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
3868 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3869 return hdsp_hw_pointer(hdsp);
3872 static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
3879 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3882 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
3885 if (stream == SNDRV_PCM_STREAM_CAPTURE)
3886 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3888 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3891 static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream,
3892 int channel, unsigned long pos,
3893 void __user *src, unsigned long count)
3895 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3898 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3901 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3902 if (snd_BUG_ON(!channel_buf))
3904 if (copy_from_user(channel_buf + pos, src, count))
3909 static int snd_hdsp_playback_copy_kernel(struct snd_pcm_substream *substream,
3910 int channel, unsigned long pos,
3911 void *src, unsigned long count)
3913 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3916 channel_buf = hdsp_channel_buffer_location(hdsp, substream->pstr->stream, channel);
3917 if (snd_BUG_ON(!channel_buf))
3919 memcpy(channel_buf + pos, src, count);
3923 static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream,
3924 int channel, unsigned long pos,
3925 void __user *dst, unsigned long count)
3927 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3930 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3933 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3934 if (snd_BUG_ON(!channel_buf))
3936 if (copy_to_user(dst, channel_buf + pos, count))
3941 static int snd_hdsp_capture_copy_kernel(struct snd_pcm_substream *substream,
3942 int channel, unsigned long pos,
3943 void *dst, unsigned long count)
3945 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3948 channel_buf = hdsp_channel_buffer_location(hdsp, substream->pstr->stream, channel);
3949 if (snd_BUG_ON(!channel_buf))
3951 memcpy(dst, channel_buf + pos, count);
3955 static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream,
3956 int channel, unsigned long pos,
3957 unsigned long count)
3959 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3962 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3963 if (snd_BUG_ON(!channel_buf))
3965 memset(channel_buf + pos, 0, count);
3969 static int snd_hdsp_reset(struct snd_pcm_substream *substream)
3971 struct snd_pcm_runtime *runtime = substream->runtime;
3972 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3973 struct snd_pcm_substream *other;
3974 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3975 other = hdsp->capture_substream;
3977 other = hdsp->playback_substream;
3979 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3981 runtime->status->hw_ptr = 0;
3983 struct snd_pcm_substream *s;
3984 struct snd_pcm_runtime *oruntime = other->runtime;
3985 snd_pcm_group_for_each_entry(s, substream) {
3987 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3995 static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3996 struct snd_pcm_hw_params *params)
3998 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4003 if (hdsp_check_for_iobox (hdsp))
4006 if (hdsp_check_for_firmware(hdsp, 1))
4009 spin_lock_irq(&hdsp->lock);
4011 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4012 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
4013 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
4014 this_pid = hdsp->playback_pid;
4015 other_pid = hdsp->capture_pid;
4017 this_pid = hdsp->capture_pid;
4018 other_pid = hdsp->playback_pid;
4021 if ((other_pid > 0) && (this_pid != other_pid)) {
4023 /* The other stream is open, and not by the same
4024 task as this one. Make sure that the parameters
4025 that matter are the same.
4028 if (params_rate(params) != hdsp->system_sample_rate) {
4029 spin_unlock_irq(&hdsp->lock);
4030 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4034 if (params_period_size(params) != hdsp->period_bytes / 4) {
4035 spin_unlock_irq(&hdsp->lock);
4036 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4042 spin_unlock_irq(&hdsp->lock);
4046 spin_unlock_irq(&hdsp->lock);
4049 /* how to make sure that the rate matches an externally-set one ?
4052 spin_lock_irq(&hdsp->lock);
4053 if (! hdsp->clock_source_locked) {
4054 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
4055 spin_unlock_irq(&hdsp->lock);
4056 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4060 spin_unlock_irq(&hdsp->lock);
4062 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
4063 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4070 static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
4071 struct snd_pcm_channel_info *info)
4073 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4074 unsigned int channel = info->channel;
4076 if (snd_BUG_ON(channel >= hdsp->max_channels))
4078 channel = array_index_nospec(channel, hdsp->max_channels);
4080 if (hdsp->channel_map[channel] < 0)
4083 info->offset = hdsp->channel_map[channel] * HDSP_CHANNEL_BUFFER_BYTES;
4089 static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
4090 unsigned int cmd, void *arg)
4093 case SNDRV_PCM_IOCTL1_RESET:
4094 return snd_hdsp_reset(substream);
4095 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
4096 return snd_hdsp_channel_info(substream, arg);
4101 return snd_pcm_lib_ioctl(substream, cmd, arg);
4104 static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
4106 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4107 struct snd_pcm_substream *other;
4110 if (hdsp_check_for_iobox (hdsp))
4113 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
4116 spin_lock(&hdsp->lock);
4117 running = hdsp->running;
4119 case SNDRV_PCM_TRIGGER_START:
4120 running |= 1 << substream->stream;
4122 case SNDRV_PCM_TRIGGER_STOP:
4123 running &= ~(1 << substream->stream);
4127 spin_unlock(&hdsp->lock);
4130 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4131 other = hdsp->capture_substream;
4133 other = hdsp->playback_substream;
4136 struct snd_pcm_substream *s;
4137 snd_pcm_group_for_each_entry(s, substream) {
4139 snd_pcm_trigger_done(s, substream);
4140 if (cmd == SNDRV_PCM_TRIGGER_START)
4141 running |= 1 << s->stream;
4143 running &= ~(1 << s->stream);
4147 if (cmd == SNDRV_PCM_TRIGGER_START) {
4148 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4149 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4150 hdsp_silence_playback(hdsp);
4153 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4154 hdsp_silence_playback(hdsp);
4157 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4158 hdsp_silence_playback(hdsp);
4161 snd_pcm_trigger_done(substream, substream);
4162 if (!hdsp->running && running)
4163 hdsp_start_audio(hdsp);
4164 else if (hdsp->running && !running)
4165 hdsp_stop_audio(hdsp);
4166 hdsp->running = running;
4167 spin_unlock(&hdsp->lock);
4172 static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
4174 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4177 if (hdsp_check_for_iobox (hdsp))
4180 if (hdsp_check_for_firmware(hdsp, 1))
4183 spin_lock_irq(&hdsp->lock);
4185 hdsp_reset_hw_pointer(hdsp);
4186 spin_unlock_irq(&hdsp->lock);
4190 static const struct snd_pcm_hardware snd_hdsp_playback_subinfo =
4192 .info = (SNDRV_PCM_INFO_MMAP |
4193 SNDRV_PCM_INFO_MMAP_VALID |
4194 SNDRV_PCM_INFO_NONINTERLEAVED |
4195 SNDRV_PCM_INFO_SYNC_START |
4196 SNDRV_PCM_INFO_DOUBLE),
4197 #ifdef SNDRV_BIG_ENDIAN
4198 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4200 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4202 .rates = (SNDRV_PCM_RATE_32000 |
4203 SNDRV_PCM_RATE_44100 |
4204 SNDRV_PCM_RATE_48000 |
4205 SNDRV_PCM_RATE_64000 |
4206 SNDRV_PCM_RATE_88200 |
4207 SNDRV_PCM_RATE_96000),
4211 .channels_max = HDSP_MAX_CHANNELS,
4212 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4213 .period_bytes_min = (64 * 4) * 10,
4214 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4220 static const struct snd_pcm_hardware snd_hdsp_capture_subinfo =
4222 .info = (SNDRV_PCM_INFO_MMAP |
4223 SNDRV_PCM_INFO_MMAP_VALID |
4224 SNDRV_PCM_INFO_NONINTERLEAVED |
4225 SNDRV_PCM_INFO_SYNC_START),
4226 #ifdef SNDRV_BIG_ENDIAN
4227 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4229 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4231 .rates = (SNDRV_PCM_RATE_32000 |
4232 SNDRV_PCM_RATE_44100 |
4233 SNDRV_PCM_RATE_48000 |
4234 SNDRV_PCM_RATE_64000 |
4235 SNDRV_PCM_RATE_88200 |
4236 SNDRV_PCM_RATE_96000),
4240 .channels_max = HDSP_MAX_CHANNELS,
4241 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4242 .period_bytes_min = (64 * 4) * 10,
4243 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4249 static const unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4251 static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
4252 .count = ARRAY_SIZE(hdsp_period_sizes),
4253 .list = hdsp_period_sizes,
4257 static const unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4259 static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
4260 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4261 .list = hdsp_9632_sample_rates,
4265 static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4266 struct snd_pcm_hw_rule *rule)
4268 struct hdsp *hdsp = rule->private;
4269 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4270 if (hdsp->io_type == H9632) {
4271 unsigned int list[3];
4272 list[0] = hdsp->qs_in_channels;
4273 list[1] = hdsp->ds_in_channels;
4274 list[2] = hdsp->ss_in_channels;
4275 return snd_interval_list(c, 3, list, 0);
4277 unsigned int list[2];
4278 list[0] = hdsp->ds_in_channels;
4279 list[1] = hdsp->ss_in_channels;
4280 return snd_interval_list(c, 2, list, 0);
4284 static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4285 struct snd_pcm_hw_rule *rule)
4287 unsigned int list[3];
4288 struct hdsp *hdsp = rule->private;
4289 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4290 if (hdsp->io_type == H9632) {
4291 list[0] = hdsp->qs_out_channels;
4292 list[1] = hdsp->ds_out_channels;
4293 list[2] = hdsp->ss_out_channels;
4294 return snd_interval_list(c, 3, list, 0);
4296 list[0] = hdsp->ds_out_channels;
4297 list[1] = hdsp->ss_out_channels;
4299 return snd_interval_list(c, 2, list, 0);
4302 static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4303 struct snd_pcm_hw_rule *rule)
4305 struct hdsp *hdsp = rule->private;
4306 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4307 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4308 if (r->min > 96000 && hdsp->io_type == H9632) {
4309 struct snd_interval t = {
4310 .min = hdsp->qs_in_channels,
4311 .max = hdsp->qs_in_channels,
4314 return snd_interval_refine(c, &t);
4315 } else if (r->min > 48000 && r->max <= 96000) {
4316 struct snd_interval t = {
4317 .min = hdsp->ds_in_channels,
4318 .max = hdsp->ds_in_channels,
4321 return snd_interval_refine(c, &t);
4322 } else if (r->max < 64000) {
4323 struct snd_interval t = {
4324 .min = hdsp->ss_in_channels,
4325 .max = hdsp->ss_in_channels,
4328 return snd_interval_refine(c, &t);
4333 static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4334 struct snd_pcm_hw_rule *rule)
4336 struct hdsp *hdsp = rule->private;
4337 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4338 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4339 if (r->min > 96000 && hdsp->io_type == H9632) {
4340 struct snd_interval t = {
4341 .min = hdsp->qs_out_channels,
4342 .max = hdsp->qs_out_channels,
4345 return snd_interval_refine(c, &t);
4346 } else if (r->min > 48000 && r->max <= 96000) {
4347 struct snd_interval t = {
4348 .min = hdsp->ds_out_channels,
4349 .max = hdsp->ds_out_channels,
4352 return snd_interval_refine(c, &t);
4353 } else if (r->max < 64000) {
4354 struct snd_interval t = {
4355 .min = hdsp->ss_out_channels,
4356 .max = hdsp->ss_out_channels,
4359 return snd_interval_refine(c, &t);
4364 static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4365 struct snd_pcm_hw_rule *rule)
4367 struct hdsp *hdsp = rule->private;
4368 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4369 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4370 if (c->min >= hdsp->ss_out_channels) {
4371 struct snd_interval t = {
4376 return snd_interval_refine(r, &t);
4377 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
4378 struct snd_interval t = {
4383 return snd_interval_refine(r, &t);
4384 } else if (c->max <= hdsp->ds_out_channels) {
4385 struct snd_interval t = {
4390 return snd_interval_refine(r, &t);
4395 static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4396 struct snd_pcm_hw_rule *rule)
4398 struct hdsp *hdsp = rule->private;
4399 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4400 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
4401 if (c->min >= hdsp->ss_in_channels) {
4402 struct snd_interval t = {
4407 return snd_interval_refine(r, &t);
4408 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
4409 struct snd_interval t = {
4414 return snd_interval_refine(r, &t);
4415 } else if (c->max <= hdsp->ds_in_channels) {
4416 struct snd_interval t = {
4421 return snd_interval_refine(r, &t);
4426 static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
4428 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4429 struct snd_pcm_runtime *runtime = substream->runtime;
4431 if (hdsp_check_for_iobox (hdsp))
4434 if (hdsp_check_for_firmware(hdsp, 1))
4437 spin_lock_irq(&hdsp->lock);
4439 snd_pcm_set_sync(substream);
4441 runtime->hw = snd_hdsp_playback_subinfo;
4442 runtime->dma_area = hdsp->playback_buffer;
4443 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4445 hdsp->playback_pid = current->pid;
4446 hdsp->playback_substream = substream;
4448 spin_unlock_irq(&hdsp->lock);
4450 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4451 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4452 if (hdsp->clock_source_locked) {
4453 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4454 } else if (hdsp->io_type == H9632) {
4455 runtime->hw.rate_max = 192000;
4456 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4457 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4459 if (hdsp->io_type == H9632) {
4460 runtime->hw.channels_min = hdsp->qs_out_channels;
4461 runtime->hw.channels_max = hdsp->ss_out_channels;
4464 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4465 snd_hdsp_hw_rule_out_channels, hdsp,
4466 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4467 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4468 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4469 SNDRV_PCM_HW_PARAM_RATE, -1);
4470 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4471 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4472 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4474 if (RPM != hdsp->io_type) {
4475 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4476 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4477 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4478 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4483 static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
4485 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4487 spin_lock_irq(&hdsp->lock);
4489 hdsp->playback_pid = -1;
4490 hdsp->playback_substream = NULL;
4492 spin_unlock_irq(&hdsp->lock);
4494 if (RPM != hdsp->io_type) {
4495 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4496 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4497 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4503 static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
4505 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4506 struct snd_pcm_runtime *runtime = substream->runtime;
4508 if (hdsp_check_for_iobox (hdsp))
4511 if (hdsp_check_for_firmware(hdsp, 1))
4514 spin_lock_irq(&hdsp->lock);
4516 snd_pcm_set_sync(substream);
4518 runtime->hw = snd_hdsp_capture_subinfo;
4519 runtime->dma_area = hdsp->capture_buffer;
4520 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4522 hdsp->capture_pid = current->pid;
4523 hdsp->capture_substream = substream;
4525 spin_unlock_irq(&hdsp->lock);
4527 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4528 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4529 if (hdsp->io_type == H9632) {
4530 runtime->hw.channels_min = hdsp->qs_in_channels;
4531 runtime->hw.channels_max = hdsp->ss_in_channels;
4532 runtime->hw.rate_max = 192000;
4533 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4534 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4536 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4537 snd_hdsp_hw_rule_in_channels, hdsp,
4538 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4539 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4540 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4541 SNDRV_PCM_HW_PARAM_RATE, -1);
4542 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4543 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4544 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4548 static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
4550 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4552 spin_lock_irq(&hdsp->lock);
4554 hdsp->capture_pid = -1;
4555 hdsp->capture_substream = NULL;
4557 spin_unlock_irq(&hdsp->lock);
4561 /* helper functions for copying meter values */
4562 static inline int copy_u32_le(void __user *dest, void __iomem *src)
4564 u32 val = readl(src);
4565 return copy_to_user(dest, &val, 4);
4568 static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4570 u32 rms_low, rms_high;
4572 rms_low = readl(src_low);
4573 rms_high = readl(src_high);
4574 rms = ((u64)rms_high << 32) | rms_low;
4575 return copy_to_user(dest, &rms, 8);
4578 static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4580 u32 rms_low, rms_high;
4582 rms_low = readl(src_low) & 0xffffff00;
4583 rms_high = readl(src_high) & 0xffffff00;
4584 rms = ((u64)rms_high << 32) | rms_low;
4585 return copy_to_user(dest, &rms, 8);
4588 static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
4590 int doublespeed = 0;
4591 int i, j, channels, ofs;
4593 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4595 channels = doublespeed ? 14 : 26;
4596 for (i = 0, j = 0; i < 26; ++i) {
4597 if (doublespeed && (i & 4))
4599 ofs = HDSP_9652_peakBase - j * 4;
4600 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4602 ofs -= channels * 4;
4603 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4605 ofs -= channels * 4;
4606 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4608 ofs = HDSP_9652_rmsBase + j * 8;
4609 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4610 hdsp->iobase + ofs + 4))
4612 ofs += channels * 8;
4613 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4614 hdsp->iobase + ofs + 4))
4616 ofs += channels * 8;
4617 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4618 hdsp->iobase + ofs + 4))
4625 static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
4628 struct hdsp_9632_meters __iomem *m;
4629 int doublespeed = 0;
4631 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4633 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
4634 for (i = 0, j = 0; i < 16; ++i, ++j) {
4635 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4637 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4639 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4641 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4642 &m->input_rms_high[j]))
4644 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4645 &m->playback_rms_high[j]))
4647 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4648 &m->output_rms_high[j]))
4650 if (doublespeed && i == 3) i += 4;
4655 static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
4659 for (i = 0; i < 26; i++) {
4660 if (copy_u32_le(&peak_rms->playback_peaks[i],
4661 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4663 if (copy_u32_le(&peak_rms->input_peaks[i],
4664 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4667 for (i = 0; i < 28; i++) {
4668 if (copy_u32_le(&peak_rms->output_peaks[i],
4669 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4672 for (i = 0; i < 26; ++i) {
4673 if (copy_u64_le(&peak_rms->playback_rms[i],
4674 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4675 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4677 if (copy_u64_le(&peak_rms->input_rms[i],
4678 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4679 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4685 static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
4687 struct hdsp *hdsp = hw->private_data;
4688 void __user *argp = (void __user *)arg;
4692 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
4693 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
4695 err = hdsp_check_for_iobox(hdsp);
4699 err = hdsp_check_for_firmware(hdsp, 1);
4703 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4704 dev_err(hdsp->card->dev,
4705 "firmware needs to be uploaded to the card.\n");
4709 switch (hdsp->io_type) {
4711 return hdsp_9652_get_peak(hdsp, peak_rms);
4713 return hdsp_9632_get_peak(hdsp, peak_rms);
4715 return hdsp_get_peak(hdsp, peak_rms);
4718 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
4719 struct hdsp_config_info info;
4720 unsigned long flags;
4723 err = hdsp_check_for_iobox(hdsp);
4727 err = hdsp_check_for_firmware(hdsp, 1);
4731 memset(&info, 0, sizeof(info));
4732 spin_lock_irqsave(&hdsp->lock, flags);
4733 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4734 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4735 if (hdsp->io_type != H9632)
4736 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
4737 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
4738 for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
4739 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
4740 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
4741 info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
4742 HDSP_SPDIFOpticalOut);
4743 info.spdif_professional = (unsigned char)
4744 hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
4745 info.spdif_emphasis = (unsigned char)
4746 hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
4747 info.spdif_nonaudio = (unsigned char)
4748 hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
4749 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4750 info.system_sample_rate = hdsp->system_sample_rate;
4751 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4752 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4753 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4754 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
4755 info.line_out = (unsigned char)
4756 hdsp_toggle_setting(hdsp, HDSP_LineOut);
4757 if (hdsp->io_type == H9632) {
4758 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4759 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4760 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
4761 info.xlr_breakout_cable =
4762 (unsigned char)hdsp_toggle_setting(hdsp,
4763 HDSP_XLRBreakoutCable);
4765 } else if (hdsp->io_type == RPM) {
4766 info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
4767 info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
4769 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
4770 info.analog_extension_board =
4771 (unsigned char)hdsp_toggle_setting(hdsp,
4772 HDSP_AnalogExtensionBoard);
4773 spin_unlock_irqrestore(&hdsp->lock, flags);
4774 if (copy_to_user(argp, &info, sizeof(info)))
4778 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
4779 struct hdsp_9632_aeb h9632_aeb;
4781 if (hdsp->io_type != H9632) return -EINVAL;
4782 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4783 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4784 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4788 case SNDRV_HDSP_IOCTL_GET_VERSION: {
4789 struct hdsp_version hdsp_version;
4792 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4793 if (hdsp->io_type == Undefined) {
4794 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
4797 memset(&hdsp_version, 0, sizeof(hdsp_version));
4798 hdsp_version.io_type = hdsp->io_type;
4799 hdsp_version.firmware_rev = hdsp->firmware_rev;
4800 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
4804 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
4805 struct hdsp_firmware firmware;
4806 u32 __user *firmware_data;
4809 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4810 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4811 if (hdsp->io_type == Undefined) return -EINVAL;
4813 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4816 dev_info(hdsp->card->dev,
4817 "initializing firmware upload\n");
4818 if (copy_from_user(&firmware, argp, sizeof(firmware)))
4820 firmware_data = (u32 __user *)firmware.firmware_data;
4822 if (hdsp_check_for_iobox (hdsp))
4825 if (!hdsp->fw_uploaded) {
4826 hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
4827 if (!hdsp->fw_uploaded)
4831 if (copy_from_user(hdsp->fw_uploaded, firmware_data,
4832 HDSP_FIRMWARE_SIZE)) {
4833 vfree(hdsp->fw_uploaded);
4834 hdsp->fw_uploaded = NULL;
4838 hdsp->state |= HDSP_FirmwareCached;
4840 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
4843 if (!(hdsp->state & HDSP_InitializationComplete)) {
4844 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
4847 snd_hdsp_initialize_channels(hdsp);
4848 snd_hdsp_initialize_midi_flush(hdsp);
4850 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4851 dev_err(hdsp->card->dev,
4852 "error creating alsa devices\n");
4858 case SNDRV_HDSP_IOCTL_GET_MIXER: {
4859 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
4860 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4870 static const struct snd_pcm_ops snd_hdsp_playback_ops = {
4871 .open = snd_hdsp_playback_open,
4872 .close = snd_hdsp_playback_release,
4873 .ioctl = snd_hdsp_ioctl,
4874 .hw_params = snd_hdsp_hw_params,
4875 .prepare = snd_hdsp_prepare,
4876 .trigger = snd_hdsp_trigger,
4877 .pointer = snd_hdsp_hw_pointer,
4878 .copy_user = snd_hdsp_playback_copy,
4879 .copy_kernel = snd_hdsp_playback_copy_kernel,
4880 .fill_silence = snd_hdsp_hw_silence,
4883 static const struct snd_pcm_ops snd_hdsp_capture_ops = {
4884 .open = snd_hdsp_capture_open,
4885 .close = snd_hdsp_capture_release,
4886 .ioctl = snd_hdsp_ioctl,
4887 .hw_params = snd_hdsp_hw_params,
4888 .prepare = snd_hdsp_prepare,
4889 .trigger = snd_hdsp_trigger,
4890 .pointer = snd_hdsp_hw_pointer,
4891 .copy_user = snd_hdsp_capture_copy,
4892 .copy_kernel = snd_hdsp_capture_copy_kernel,
4895 static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
4897 struct snd_hwdep *hw;
4900 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4904 hw->private_data = hdsp;
4905 strcpy(hw->name, "HDSP hwdep interface");
4907 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
4908 hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
4913 static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
4915 struct snd_pcm *pcm;
4918 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4922 pcm->private_data = hdsp;
4923 strcpy(pcm->name, hdsp->card_name);
4925 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4926 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4928 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4933 static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
4935 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4936 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4939 static int snd_hdsp_enable_io (struct hdsp *hdsp)
4943 if (hdsp_fifo_wait (hdsp, 0, 100)) {
4944 dev_err(hdsp->card->dev,
4945 "enable_io fifo_wait failed\n");
4949 for (i = 0; i < hdsp->max_channels; ++i) {
4950 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4951 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4957 static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
4959 int status, aebi_channels, aebo_channels;
4961 switch (hdsp->io_type) {
4963 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4964 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4965 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4969 hdsp->card_name = "RME Hammerfall HDSP 9652";
4970 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4971 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4975 status = hdsp_read(hdsp, HDSP_statusRegister);
4976 /* HDSP_AEBx bits are low when AEB are connected */
4977 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4978 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4979 hdsp->card_name = "RME Hammerfall HDSP 9632";
4980 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4981 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4982 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4983 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4984 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4985 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4989 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4990 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4991 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4995 hdsp->card_name = "RME Hammerfall DSP + RPM";
4996 hdsp->ss_in_channels = RPM_CHANNELS-1;
4997 hdsp->ss_out_channels = RPM_CHANNELS;
4998 hdsp->ds_in_channels = RPM_CHANNELS-1;
4999 hdsp->ds_out_channels = RPM_CHANNELS;
5003 /* should never get here */
5008 static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
5010 snd_hdsp_flush_midi_input (hdsp, 0);
5011 snd_hdsp_flush_midi_input (hdsp, 1);
5014 static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
5018 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
5020 "Error creating pcm interface\n");
5025 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
5027 "Error creating first midi interface\n");
5031 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
5032 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
5034 "Error creating second midi interface\n");
5039 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
5041 "Error creating ctl interface\n");
5045 snd_hdsp_proc_init(hdsp);
5047 hdsp->system_sample_rate = -1;
5048 hdsp->playback_pid = -1;
5049 hdsp->capture_pid = -1;
5050 hdsp->capture_substream = NULL;
5051 hdsp->playback_substream = NULL;
5053 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
5055 "Error setting default values\n");
5059 if (!(hdsp->state & HDSP_InitializationComplete)) {
5060 strcpy(card->shortname, "Hammerfall DSP");
5061 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
5062 hdsp->port, hdsp->irq);
5064 if ((err = snd_card_register(card)) < 0) {
5066 "error registering card\n");
5069 hdsp->state |= HDSP_InitializationComplete;
5075 /* load firmware via hotplug fw loader */
5076 static int hdsp_request_fw_loader(struct hdsp *hdsp)
5079 const struct firmware *fw;
5082 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5084 if (hdsp->io_type == Undefined) {
5085 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
5087 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5091 /* caution: max length of firmware filename is 30! */
5092 switch (hdsp->io_type) {
5094 fwfile = "rpm_firmware.bin";
5097 if (hdsp->firmware_rev == 0xa)
5098 fwfile = "multiface_firmware.bin";
5100 fwfile = "multiface_firmware_rev11.bin";
5103 if (hdsp->firmware_rev == 0xa)
5104 fwfile = "digiface_firmware.bin";
5106 fwfile = "digiface_firmware_rev11.bin";
5109 dev_err(hdsp->card->dev,
5110 "invalid io_type %d\n", hdsp->io_type);
5114 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5115 dev_err(hdsp->card->dev,
5116 "cannot load firmware %s\n", fwfile);
5119 if (fw->size < HDSP_FIRMWARE_SIZE) {
5120 dev_err(hdsp->card->dev,
5121 "too short firmware size %d (expected %d)\n",
5122 (int)fw->size, HDSP_FIRMWARE_SIZE);
5123 release_firmware(fw);
5127 hdsp->firmware = fw;
5129 hdsp->state |= HDSP_FirmwareCached;
5131 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
5134 if (!(hdsp->state & HDSP_InitializationComplete)) {
5135 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
5138 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
5139 dev_err(hdsp->card->dev,
5140 "error creating hwdep device\n");
5143 snd_hdsp_initialize_channels(hdsp);
5144 snd_hdsp_initialize_midi_flush(hdsp);
5145 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
5146 dev_err(hdsp->card->dev,
5147 "error creating alsa devices\n");
5154 static int snd_hdsp_create(struct snd_card *card,
5157 struct pci_dev *pci = hdsp->pci;
5164 hdsp->midi[0].rmidi = NULL;
5165 hdsp->midi[1].rmidi = NULL;
5166 hdsp->midi[0].input = NULL;
5167 hdsp->midi[1].input = NULL;
5168 hdsp->midi[0].output = NULL;
5169 hdsp->midi[1].output = NULL;
5170 hdsp->midi[0].pending = 0;
5171 hdsp->midi[1].pending = 0;
5172 spin_lock_init(&hdsp->midi[0].lock);
5173 spin_lock_init(&hdsp->midi[1].lock);
5174 hdsp->iobase = NULL;
5175 hdsp->control_register = 0;
5176 hdsp->control2_register = 0;
5177 hdsp->io_type = Undefined;
5178 hdsp->max_channels = 26;
5182 spin_lock_init(&hdsp->lock);
5184 INIT_WORK(&hdsp->midi_work, hdsp_midi_work);
5186 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5187 hdsp->firmware_rev &= 0xff;
5189 /* From Martin Bjoernsen :
5190 "It is important that the card's latency timer register in
5191 the PCI configuration space is set to a value much larger
5192 than 0 by the computer's BIOS or the driver.
5193 The windows driver always sets this 8 bit register [...]
5194 to its maximum 255 to avoid problems with some computers."
5196 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
5198 strcpy(card->driver, "H-DSP");
5199 strcpy(card->mixername, "Xilinx FPGA");
5201 if (hdsp->firmware_rev < 0xa)
5203 else if (hdsp->firmware_rev < 0x64)
5204 hdsp->card_name = "RME Hammerfall DSP";
5205 else if (hdsp->firmware_rev < 0x96) {
5206 hdsp->card_name = "RME HDSP 9652";
5209 hdsp->card_name = "RME HDSP 9632";
5210 hdsp->max_channels = 16;
5214 if ((err = pci_enable_device(pci)) < 0)
5217 pci_set_master(hdsp->pci);
5219 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5221 hdsp->port = pci_resource_start(pci, 0);
5222 if ((hdsp->iobase = ioremap(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
5223 dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
5224 hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
5228 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
5229 KBUILD_MODNAME, hdsp)) {
5230 dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
5234 hdsp->irq = pci->irq;
5235 card->sync_irq = hdsp->irq;
5236 hdsp->precise_ptr = 0;
5237 hdsp->use_midi_work = 1;
5238 hdsp->dds_value = 0;
5240 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
5243 if (!is_9652 && !is_9632) {
5244 /* we wait a maximum of 10 seconds to let freshly
5245 * inserted cardbus cards do their hardware init */
5246 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
5251 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
5252 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
5253 /* we don't fail as this can happen
5254 if userspace is not ready for
5257 dev_err(hdsp->card->dev,
5258 "couldn't get firmware from userspace. try using hdsploader\n");
5260 /* init is complete, we return */
5262 /* we defer initialization */
5263 dev_info(hdsp->card->dev,
5264 "card initialization pending : waiting for firmware\n");
5265 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
5269 dev_info(hdsp->card->dev,
5270 "Firmware already present, initializing card.\n");
5271 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
5272 hdsp->io_type = RPM;
5273 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
5274 hdsp->io_type = Multiface;
5276 hdsp->io_type = Digiface;
5280 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
5284 hdsp->io_type = H9652;
5287 hdsp->io_type = H9632;
5289 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
5292 snd_hdsp_initialize_channels(hdsp);
5293 snd_hdsp_initialize_midi_flush(hdsp);
5295 hdsp->state |= HDSP_FirmwareLoaded;
5297 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
5303 static int snd_hdsp_free(struct hdsp *hdsp)
5306 /* stop the audio, and cancel all interrupts */
5307 cancel_work_sync(&hdsp->midi_work);
5308 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5309 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5313 free_irq(hdsp->irq, (void *)hdsp);
5315 snd_hdsp_free_buffers(hdsp);
5317 release_firmware(hdsp->firmware);
5318 vfree(hdsp->fw_uploaded);
5319 iounmap(hdsp->iobase);
5322 pci_release_regions(hdsp->pci);
5324 pci_disable_device(hdsp->pci);
5328 static void snd_hdsp_card_free(struct snd_card *card)
5330 struct hdsp *hdsp = card->private_data;
5333 snd_hdsp_free(hdsp);
5336 static int snd_hdsp_probe(struct pci_dev *pci,
5337 const struct pci_device_id *pci_id)
5341 struct snd_card *card;
5344 if (dev >= SNDRV_CARDS)
5351 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
5352 sizeof(struct hdsp), &card);
5356 hdsp = card->private_data;
5357 card->private_free = snd_hdsp_card_free;
5360 err = snd_hdsp_create(card, hdsp);
5364 strcpy(card->shortname, "Hammerfall DSP");
5365 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
5366 hdsp->port, hdsp->irq);
5367 err = snd_card_register(card);
5370 snd_card_free(card);
5373 pci_set_drvdata(pci, card);
5378 static void snd_hdsp_remove(struct pci_dev *pci)
5380 snd_card_free(pci_get_drvdata(pci));
5383 static struct pci_driver hdsp_driver = {
5384 .name = KBUILD_MODNAME,
5385 .id_table = snd_hdsp_ids,
5386 .probe = snd_hdsp_probe,
5387 .remove = snd_hdsp_remove,
5390 module_pci_driver(hdsp_driver);