blob: 3b3ef657f73eb86e323e69f423bcec58eb884f56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for RME Hammerfall DSP audio interface(s)
3 *
4 * Copyright (c) 2002 Paul Davis
5 * Marcus Andersson
6 * Thomas Charbonnel
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <sound/driver.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/interrupt.h>
28#include <linux/slab.h>
29#include <linux/pci.h>
30#include <linux/firmware.h>
31#include <linux/moduleparam.h>
32
33#include <sound/core.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/info.h>
37#include <sound/asoundef.h>
38#include <sound/rawmidi.h>
39#include <sound/hwdep.h>
40#include <sound/initval.h>
41#include <sound/hdsp.h>
42
43#include <asm/byteorder.h>
44#include <asm/current.h>
45#include <asm/io.h>
46
47static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
49static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
50
51module_param_array(index, int, NULL, 0444);
52MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
53module_param_array(id, charp, NULL, 0444);
54MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
55module_param_array(enable, bool, NULL, 0444);
56MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
57MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
58MODULE_DESCRIPTION("RME Hammerfall DSP");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
61 "{RME HDSP-9652},"
62 "{RME HDSP-9632}}");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020063#ifdef HDSP_FW_LOADER
64MODULE_FIRMWARE("multiface_firmware.bin");
65MODULE_FIRMWARE("multiface_firmware_rev11.bin");
66MODULE_FIRMWARE("digiface_firmware.bin");
67MODULE_FIRMWARE("digiface_firmware_rev11.bin");
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define HDSP_MAX_CHANNELS 26
71#define HDSP_MAX_DS_CHANNELS 14
72#define HDSP_MAX_QS_CHANNELS 8
73#define DIGIFACE_SS_CHANNELS 26
74#define DIGIFACE_DS_CHANNELS 14
75#define MULTIFACE_SS_CHANNELS 18
76#define MULTIFACE_DS_CHANNELS 14
77#define H9652_SS_CHANNELS 26
78#define H9652_DS_CHANNELS 14
79/* This does not include possible Analog Extension Boards
80 AEBs are detected at card initialization
81*/
82#define H9632_SS_CHANNELS 12
83#define H9632_DS_CHANNELS 8
84#define H9632_QS_CHANNELS 4
85
86/* Write registers. These are defined as byte-offsets from the iobase value.
87 */
88#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020089#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define HDSP_outputBufferAddress 32
91#define HDSP_inputBufferAddress 36
92#define HDSP_controlRegister 64
93#define HDSP_interruptConfirmation 96
94#define HDSP_outputEnable 128
95#define HDSP_control2Reg 256
96#define HDSP_midiDataOut0 352
97#define HDSP_midiDataOut1 356
98#define HDSP_fifoData 368
99#define HDSP_inputEnable 384
100
101/* Read registers. These are defined as byte-offsets from the iobase value
102 */
103
104#define HDSP_statusRegister 0
105#define HDSP_timecode 128
106#define HDSP_status2Register 192
107#define HDSP_midiDataOut0 352
108#define HDSP_midiDataOut1 356
109#define HDSP_midiDataIn0 360
110#define HDSP_midiDataIn1 364
111#define HDSP_midiStatusOut0 384
112#define HDSP_midiStatusOut1 388
113#define HDSP_midiStatusIn0 392
114#define HDSP_midiStatusIn1 396
115#define HDSP_fifoStatus 400
116
117/* the meters are regular i/o-mapped registers, but offset
118 considerably from the rest. the peak registers are reset
119 when read; the least-significant 4 bits are full-scale counters;
120 the actual peak value is in the most-significant 24 bits.
121*/
122
123#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
124#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
125#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
126#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
127#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
128
129
130/* This is for H9652 cards
131 Peak values are read downward from the base
132 Rms values are read upward
133 There are rms values for the outputs too
134 26*3 values are read in ss mode
135 14*3 in ds mode, with no gap between values
136*/
137#define HDSP_9652_peakBase 7164
138#define HDSP_9652_rmsBase 4096
139
140/* c.f. the hdsp_9632_meters_t struct */
141#define HDSP_9632_metersBase 4096
142
143#define HDSP_IO_EXTENT 7168
144
145/* control2 register bits */
146
147#define HDSP_TMS 0x01
148#define HDSP_TCK 0x02
149#define HDSP_TDI 0x04
150#define HDSP_JTAG 0x08
151#define HDSP_PWDN 0x10
152#define HDSP_PROGRAM 0x020
153#define HDSP_CONFIG_MODE_0 0x040
154#define HDSP_CONFIG_MODE_1 0x080
155#define HDSP_VERSION_BIT 0x100
156#define HDSP_BIGENDIAN_MODE 0x200
157#define HDSP_RD_MULTIPLE 0x400
158#define HDSP_9652_ENABLE_MIXER 0x800
159#define HDSP_TDO 0x10000000
160
161#define HDSP_S_PROGRAM (HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
162#define HDSP_S_LOAD (HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
163
164/* Control Register bits */
165
166#define HDSP_Start (1<<0) /* start engine */
167#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
168#define HDSP_Latency1 (1<<2) /* [ see above ] */
169#define HDSP_Latency2 (1<<3) /* [ see above ] */
170#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
171#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
172#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
173#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
174#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
175#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
176#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
177#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
178#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
179#define HDSP_SyncRef2 (1<<13)
180#define HDSP_SPDIFInputSelect0 (1<<14)
181#define HDSP_SPDIFInputSelect1 (1<<15)
182#define HDSP_SyncRef0 (1<<16)
183#define HDSP_SyncRef1 (1<<17)
184#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
185#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
186#define HDSP_Midi0InterruptEnable (1<<22)
187#define HDSP_Midi1InterruptEnable (1<<23)
188#define HDSP_LineOut (1<<24)
189#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
190#define HDSP_ADGain1 (1<<26)
191#define HDSP_DAGain0 (1<<27)
192#define HDSP_DAGain1 (1<<28)
193#define HDSP_PhoneGain0 (1<<29)
194#define HDSP_PhoneGain1 (1<<30)
195#define HDSP_QuadSpeed (1<<31)
196
197#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
198#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
199#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
200#define HDSP_ADGainLowGain 0
201
202#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
203#define HDSP_DAGainHighGain HDSP_DAGainMask
204#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
205#define HDSP_DAGainMinus10dBV 0
206
207#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
208#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
209#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
210#define HDSP_PhoneGainMinus12dB 0
211
212#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
213#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
214
215#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
216#define HDSP_SPDIFInputADAT1 0
217#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
218#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
219#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
220
221#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
222#define HDSP_SyncRef_ADAT1 0
223#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
224#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
225#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
226#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
227#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
228
229/* Sample Clock Sources */
230
231#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
232#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
233#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
234#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
235#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
236#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
237#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
238#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
239#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
240#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
241
242/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
243
244#define HDSP_SYNC_FROM_WORD 0
245#define HDSP_SYNC_FROM_SPDIF 1
246#define HDSP_SYNC_FROM_ADAT1 2
247#define HDSP_SYNC_FROM_ADAT_SYNC 3
248#define HDSP_SYNC_FROM_ADAT2 4
249#define HDSP_SYNC_FROM_ADAT3 5
250
251/* SyncCheck status */
252
253#define HDSP_SYNC_CHECK_NO_LOCK 0
254#define HDSP_SYNC_CHECK_LOCK 1
255#define HDSP_SYNC_CHECK_SYNC 2
256
257/* AutoSync references - used by "autosync_ref" control switch */
258
259#define HDSP_AUTOSYNC_FROM_WORD 0
260#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
261#define HDSP_AUTOSYNC_FROM_SPDIF 2
262#define HDSP_AUTOSYNC_FROM_NONE 3
263#define HDSP_AUTOSYNC_FROM_ADAT1 4
264#define HDSP_AUTOSYNC_FROM_ADAT2 5
265#define HDSP_AUTOSYNC_FROM_ADAT3 6
266
267/* Possible sources of S/PDIF input */
268
269#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
270#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
271#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
272#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
273
274#define HDSP_Frequency32KHz HDSP_Frequency0
275#define HDSP_Frequency44_1KHz HDSP_Frequency1
276#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
277#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
278#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
279#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
280/* For H9632 cards */
281#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
282#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
283#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
Julian Cablee4b60882007-03-19 11:44:40 +0100284/* RME says n = 104857600000000, but in the windows MADI driver, I see:
285 return 104857600000000 / rate; // 100 MHz
286 return 110100480000000 / rate; // 105 MHz
287*/
288#define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
291#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
292
293#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
294#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
295
296/* Status Register bits */
297
298#define HDSP_audioIRQPending (1<<0)
299#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
300#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
301#define HDSP_Lock1 (1<<2)
302#define HDSP_Lock0 (1<<3)
303#define HDSP_SPDIFSync (1<<4)
304#define HDSP_TimecodeLock (1<<5)
305#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
306#define HDSP_Sync2 (1<<16)
307#define HDSP_Sync1 (1<<17)
308#define HDSP_Sync0 (1<<18)
309#define HDSP_DoubleSpeedStatus (1<<19)
310#define HDSP_ConfigError (1<<20)
311#define HDSP_DllError (1<<21)
312#define HDSP_spdifFrequency0 (1<<22)
313#define HDSP_spdifFrequency1 (1<<23)
314#define HDSP_spdifFrequency2 (1<<24)
315#define HDSP_SPDIFErrorFlag (1<<25)
316#define HDSP_BufferID (1<<26)
317#define HDSP_TimecodeSync (1<<27)
318#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
319#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
320#define HDSP_midi0IRQPending (1<<30)
321#define HDSP_midi1IRQPending (1<<31)
322
323#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
324
325#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
326#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
327#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
328
329#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
330#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
331#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
332
333/* This is for H9632 cards */
334#define HDSP_spdifFrequency128KHz HDSP_spdifFrequencyMask
335#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
336#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
337
338/* Status2 Register bits */
339
340#define HDSP_version0 (1<<0)
341#define HDSP_version1 (1<<1)
342#define HDSP_version2 (1<<2)
343#define HDSP_wc_lock (1<<3)
344#define HDSP_wc_sync (1<<4)
345#define HDSP_inp_freq0 (1<<5)
346#define HDSP_inp_freq1 (1<<6)
347#define HDSP_inp_freq2 (1<<7)
348#define HDSP_SelSyncRef0 (1<<8)
349#define HDSP_SelSyncRef1 (1<<9)
350#define HDSP_SelSyncRef2 (1<<10)
351
352#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
353
354#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
355#define HDSP_systemFrequency32 (HDSP_inp_freq0)
356#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
357#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
358#define HDSP_systemFrequency64 (HDSP_inp_freq2)
359#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
360#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
361/* FIXME : more values for 9632 cards ? */
362
363#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
364#define HDSP_SelSyncRef_ADAT1 0
365#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
366#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
367#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
368#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
369#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
370
371/* Card state flags */
372
373#define HDSP_InitializationComplete (1<<0)
374#define HDSP_FirmwareLoaded (1<<1)
375#define HDSP_FirmwareCached (1<<2)
376
377/* FIFO wait times, defined in terms of 1/10ths of msecs */
378
379#define HDSP_LONG_WAIT 5000
380#define HDSP_SHORT_WAIT 30
381
382#define UNITY_GAIN 32768
383#define MINUS_INFINITY_GAIN 0
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/* the size of a substream (1 mono data stream) */
386
387#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
388#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
389
390/* the size of the area we need to allocate for DMA transfers. the
391 size is the same regardless of the number of channels - the
392 Multiface still uses the same memory area.
393
394 Note that we allocate 1 more channel than is apparently needed
395 because the h/w seems to write 1 byte beyond the end of the last
396 page. Sigh.
397*/
398
399#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
400#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
401
402/* use hotplug firmeare loader? */
403#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
Takashi Iwai140432f2006-05-22 14:31:57 +0200404#if !defined(HDSP_USE_HWDEP_LOADER) && !defined(CONFIG_SND_HDSP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#define HDSP_FW_LOADER
406#endif
407#endif
408
Takashi Iwai55e957d2005-11-17 14:52:13 +0100409struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 u32 input_peak[16];
411 u32 playback_peak[16];
412 u32 output_peak[16];
413 u32 xxx_peak[16];
414 u32 padding[64];
415 u32 input_rms_low[16];
416 u32 playback_rms_low[16];
417 u32 output_rms_low[16];
418 u32 xxx_rms_low[16];
419 u32 input_rms_high[16];
420 u32 playback_rms_high[16];
421 u32 output_rms_high[16];
422 u32 xxx_rms_high[16];
423};
424
Takashi Iwai55e957d2005-11-17 14:52:13 +0100425struct hdsp_midi {
426 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100428 struct snd_rawmidi *rmidi;
429 struct snd_rawmidi_substream *input;
430 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 char istimer; /* timer in use */
432 struct timer_list timer;
433 spinlock_t lock;
434 int pending;
435};
436
Takashi Iwai55e957d2005-11-17 14:52:13 +0100437struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100439 struct snd_pcm_substream *capture_substream;
440 struct snd_pcm_substream *playback_substream;
441 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct tasklet_struct midi_tasklet;
443 int use_midi_tasklet;
444 int precise_ptr;
445 u32 control_register; /* cached value */
446 u32 control2_register; /* cached value */
447 u32 creg_spdif;
448 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200449 int clock_source_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 char *card_name; /* digiface/multiface */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100451 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 unsigned short firmware_rev;
453 unsigned short state; /* stores state bits */
454 u32 firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
455 size_t period_bytes; /* guess what this is */
456 unsigned char max_channels;
457 unsigned char qs_in_channels; /* quad speed mode for H9632 */
458 unsigned char ds_in_channels;
459 unsigned char ss_in_channels; /* different for multiface/digiface */
460 unsigned char qs_out_channels;
461 unsigned char ds_out_channels;
462 unsigned char ss_out_channels;
463
464 struct snd_dma_buffer capture_dma_buf;
465 struct snd_dma_buffer playback_dma_buf;
466 unsigned char *capture_buffer; /* suitably aligned address */
467 unsigned char *playback_buffer; /* suitably aligned address */
468
469 pid_t capture_pid;
470 pid_t playback_pid;
471 int running;
472 int system_sample_rate;
473 char *channel_map;
474 int dev;
475 int irq;
476 unsigned long port;
477 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100478 struct snd_card *card;
479 struct snd_pcm *pcm;
480 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100482 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200484 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485};
486
487/* These tables map the ALSA channels 1..N to the channels that we
488 need to use in order to find the relevant channel buffer. RME
489 refer to this kind of mapping as between "the ADAT channel and
490 the DMA channel." We index it using the logical audio channel,
491 and the value is the DMA channel (i.e. channel buffer number)
492 where the data for that channel can be read/written from/to.
493*/
494
495static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
496 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
497 18, 19, 20, 21, 22, 23, 24, 25
498};
499
500static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
501 /* Analog */
502 0, 1, 2, 3, 4, 5, 6, 7,
503 /* ADAT 2 */
504 16, 17, 18, 19, 20, 21, 22, 23,
505 /* SPDIF */
506 24, 25,
507 -1, -1, -1, -1, -1, -1, -1, -1
508};
509
510static char channel_map_ds[HDSP_MAX_CHANNELS] = {
511 /* ADAT channels are remapped */
512 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
513 /* channels 12 and 13 are S/PDIF */
514 24, 25,
515 /* others don't exist */
516 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
517};
518
519static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
520 /* ADAT channels */
521 0, 1, 2, 3, 4, 5, 6, 7,
522 /* SPDIF */
523 8, 9,
524 /* Analog */
525 10, 11,
526 /* AO4S-192 and AI4S-192 extension boards */
527 12, 13, 14, 15,
528 /* others don't exist */
529 -1, -1, -1, -1, -1, -1, -1, -1,
530 -1, -1
531};
532
533static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
534 /* ADAT */
535 1, 3, 5, 7,
536 /* SPDIF */
537 8, 9,
538 /* Analog */
539 10, 11,
540 /* AO4S-192 and AI4S-192 extension boards */
541 12, 13, 14, 15,
542 /* others don't exist */
543 -1, -1, -1, -1, -1, -1, -1, -1,
544 -1, -1, -1, -1, -1, -1
545};
546
547static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
548 /* ADAT is disabled in this mode */
549 /* SPDIF */
550 8, 9,
551 /* Analog */
552 10, 11,
553 /* AO4S-192 and AI4S-192 extension boards */
554 12, 13, 14, 15,
555 /* others don't exist */
556 -1, -1, -1, -1, -1, -1, -1, -1,
557 -1, -1, -1, -1, -1, -1, -1, -1,
558 -1, -1
559};
560
561static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
562{
563 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
564 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200565 if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
566 if (dmab->bytes >= size)
567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Takashi Iwaib6a96912005-05-30 18:27:03 +0200569 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
570 size, dmab) < 0)
571 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
573}
574
575static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
576{
Takashi Iwaib6a96912005-05-30 18:27:03 +0200577 if (dmab->area) {
578 dmab->dev.dev = NULL; /* make it anonymous */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
Takashi Iwaib6a96912005-05-30 18:27:03 +0200580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583
Takashi Iwaif40b6892006-07-05 16:51:05 +0200584static struct pci_device_id snd_hdsp_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 {
586 .vendor = PCI_VENDOR_ID_XILINX,
587 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
588 .subvendor = PCI_ANY_ID,
589 .subdevice = PCI_ANY_ID,
590 }, /* RME Hammerfall-DSP */
591 { 0, },
592};
593
594MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
595
596/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100597static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
598static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
599static int snd_hdsp_enable_io (struct hdsp *hdsp);
600static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
601static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
602static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
603static int hdsp_autosync_ref(struct hdsp *hdsp);
604static int snd_hdsp_set_defaults(struct hdsp *hdsp);
605static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Takashi Iwai55e957d2005-11-17 14:52:13 +0100607static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 switch (hdsp->firmware_rev) {
610 case 0xa:
611 return (64 * out) + (32 + (in));
612 case 0x96:
613 case 0x97:
Remy Bruno6add0f42006-10-09 15:52:01 +0200614 case 0x98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return (32 * out) + (16 + (in));
616 default:
617 return (52 * out) + (26 + (in));
618 }
619}
620
Takashi Iwai55e957d2005-11-17 14:52:13 +0100621static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 switch (hdsp->firmware_rev) {
624 case 0xa:
625 return (64 * out) + in;
626 case 0x96:
627 case 0x97:
Remy Bruno6add0f42006-10-09 15:52:01 +0200628 case 0x98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return (32 * out) + in;
630 default:
631 return (52 * out) + in;
632 }
633}
634
Takashi Iwai55e957d2005-11-17 14:52:13 +0100635static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 writel(val, hdsp->iobase + reg);
638}
639
Takashi Iwai55e957d2005-11-17 14:52:13 +0100640static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 return readl (hdsp->iobase + reg);
643}
644
Takashi Iwai55e957d2005-11-17 14:52:13 +0100645static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647
648 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
649 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
650 snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
651 hdsp->state &= ~HDSP_FirmwareLoaded;
652 return -EIO;
653 }
654 return 0;
655
656}
657
Takashi Iwai55e957d2005-11-17 14:52:13 +0100658static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 int i;
661 unsigned long flags;
662
663 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
664
665 snd_printk ("Hammerfall-DSP: loading firmware\n");
666
667 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
668 hdsp_write (hdsp, HDSP_fifoData, 0);
669
670 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
671 snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
672 return -EIO;
673 }
674
675 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
676
677 for (i = 0; i < 24413; ++i) {
678 hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
679 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
680 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
681 return -EIO;
682 }
683 }
684
Takashi Iwaib0b98112005-10-20 18:29:58 +0200685 ssleep(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
688 snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
689 return -EIO;
690 }
691
692#ifdef SNDRV_BIG_ENDIAN
693 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
694#else
695 hdsp->control2_register = 0;
696#endif
697 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
698 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
699
700 }
701 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaib0b98112005-10-20 18:29:58 +0200702 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 spin_lock_irqsave(&hdsp->lock, flags);
704 snd_hdsp_set_defaults(hdsp);
705 spin_unlock_irqrestore(&hdsp->lock, flags);
706 }
707
708 hdsp->state |= HDSP_FirmwareLoaded;
709
710 return 0;
711}
712
Takashi Iwai55e957d2005-11-17 14:52:13 +0100713static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
716
717 hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
718 hdsp_write (hdsp, HDSP_fifoData, 0);
Takashi Iwaib0b98112005-10-20 18:29:58 +0200719 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
723 hdsp_write (hdsp, HDSP_fifoData, 0);
724
725 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
726 hdsp->io_type = Multiface;
727 hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
728 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
729 hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
730 } else {
731 hdsp->io_type = Digiface;
732 }
733 } else {
734 /* firmware was already loaded, get iobox type */
Takashi Iwaib0b98112005-10-20 18:29:58 +0200735 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200737 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 return 0;
741}
742
743
Takashi Iwai311e70a2006-09-06 12:13:37 +0200744#ifdef HDSP_FW_LOADER
745static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp);
746#endif
747
748static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200750 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200754 if (! load_on_demand)
Takashi Iwaib0b98112005-10-20 18:29:58 +0200755 return -EIO;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200756 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +0200757 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200758 if (! (hdsp->state & HDSP_FirmwareCached)) {
759#ifdef HDSP_FW_LOADER
760 if (! hdsp_request_fw_loader(hdsp))
761 return 0;
762#endif
763 snd_printk(KERN_ERR
764 "Hammerfall-DSP: No firmware loaded nor "
765 "cached, please upload firmware.\n");
766 return -EIO;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200767 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200768 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
769 snd_printk(KERN_ERR
770 "Hammerfall-DSP: Firmware loading from "
771 "cache failed, please upload manually.\n");
772 return -EIO;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 return 0;
776}
777
778
Takashi Iwai55e957d2005-11-17 14:52:13 +0100779static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 int i;
782
783 /* the fifoStatus registers reports on how many words
784 are available in the command FIFO.
785 */
786
787 for (i = 0; i < timeout; i++) {
788
789 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
790 return 0;
791
792 /* not very friendly, but we only do this during a firmware
793 load and changing the mixer, so we just put up with it.
794 */
795
796 udelay (100);
797 }
798
799 snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
800 count, timeout);
801 return -1;
802}
803
Takashi Iwai55e957d2005-11-17 14:52:13 +0100804static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Takashi Iwaib0b98112005-10-20 18:29:58 +0200806 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return hdsp->mixer_matrix[addr];
810}
811
Takashi Iwai55e957d2005-11-17 14:52:13 +0100812static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 unsigned int ad;
815
816 if (addr >= HDSP_MATRIX_MIXER_SIZE)
817 return -1;
818
819 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
820
821 /* from martin bjornsen:
822
823 "You can only write dwords to the
824 mixer memory which contain two
825 mixer values in the low and high
826 word. So if you want to change
827 value 0 you have to read value 1
828 from the cache and write both to
829 the first dword in the mixer
830 memory."
831 */
832
Takashi Iwaib0b98112005-10-20 18:29:58 +0200833 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Takashi Iwaib0b98112005-10-20 18:29:58 +0200836 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 hdsp->mixer_matrix[addr] = data;
840
841
842 /* `addr' addresses a 16-bit wide address, but
843 the address space accessed via hdsp_write
844 uses byte offsets. put another way, addr
845 varies from 0 to 1351, but to access the
846 corresponding memory location, we need
847 to access 0 to 2703 ...
848 */
849 ad = addr/2;
850
851 hdsp_write (hdsp, 4096 + (ad*4),
852 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
853 hdsp->mixer_matrix[addr&0x7fe]);
854
855 return 0;
856
857 } else {
858
859 ad = (addr << 16) + data;
860
Takashi Iwaib0b98112005-10-20 18:29:58 +0200861 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 hdsp_write (hdsp, HDSP_fifoData, ad);
865 hdsp->mixer_matrix[addr] = data;
866
867 }
868
869 return 0;
870}
871
Takashi Iwai55e957d2005-11-17 14:52:13 +0100872static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 unsigned long flags;
875 int ret = 1;
876
877 spin_lock_irqsave(&hdsp->lock, flags);
878 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b98112005-10-20 18:29:58 +0200879 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 spin_unlock_irqrestore(&hdsp->lock, flags);
882 return ret;
883}
884
Takashi Iwai55e957d2005-11-17 14:52:13 +0100885static int hdsp_external_sample_rate (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
888 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
889
890 switch (rate_bits) {
891 case HDSP_systemFrequency32: return 32000;
892 case HDSP_systemFrequency44_1: return 44100;
893 case HDSP_systemFrequency48: return 48000;
894 case HDSP_systemFrequency64: return 64000;
895 case HDSP_systemFrequency88_2: return 88200;
896 case HDSP_systemFrequency96: return 96000;
897 default:
898 return 0;
899 }
900}
901
Takashi Iwai55e957d2005-11-17 14:52:13 +0100902static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
905 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
906
Takashi Iwaib0b98112005-10-20 18:29:58 +0200907 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 switch (rate_bits) {
911 case HDSP_spdifFrequency32KHz: return 32000;
912 case HDSP_spdifFrequency44_1KHz: return 44100;
913 case HDSP_spdifFrequency48KHz: return 48000;
914 case HDSP_spdifFrequency64KHz: return 64000;
915 case HDSP_spdifFrequency88_2KHz: return 88200;
916 case HDSP_spdifFrequency96KHz: return 96000;
917 case HDSP_spdifFrequency128KHz:
918 if (hdsp->io_type == H9632) return 128000;
919 break;
920 case HDSP_spdifFrequency176_4KHz:
921 if (hdsp->io_type == H9632) return 176400;
922 break;
923 case HDSP_spdifFrequency192KHz:
924 if (hdsp->io_type == H9632) return 192000;
925 break;
926 default:
927 break;
928 }
929 snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
930 return 0;
931}
932
Takashi Iwai55e957d2005-11-17 14:52:13 +0100933static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
936}
937
Takashi Iwai55e957d2005-11-17 14:52:13 +0100938static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 int position;
941
942 position = hdsp_read(hdsp, HDSP_statusRegister);
943
Takashi Iwaib0b98112005-10-20 18:29:58 +0200944 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 position &= HDSP_BufferPositionMask;
948 position /= 4;
949 position &= (hdsp->period_bytes/2) - 1;
950 return position;
951}
952
Takashi Iwai55e957d2005-11-17 14:52:13 +0100953static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +0200956 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
957 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
958 * requires (?) to write again DDS value after a reset pointer
959 * (at least, it works like this) */
960 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Takashi Iwai55e957d2005-11-17 14:52:13 +0100963static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
966 hdsp_write(s, HDSP_controlRegister, s->control_register);
967}
968
Takashi Iwai55e957d2005-11-17 14:52:13 +0100969static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
972 hdsp_write(s, HDSP_controlRegister, s->control_register);
973}
974
Takashi Iwai55e957d2005-11-17 14:52:13 +0100975static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
978}
979
Takashi Iwai55e957d2005-11-17 14:52:13 +0100980static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 int n;
983
984 spin_lock_irq(&s->lock);
985
986 frames >>= 7;
987 n = 0;
988 while (frames) {
989 n++;
990 frames >>= 1;
991 }
992
993 s->control_register &= ~HDSP_LatencyMask;
994 s->control_register |= hdsp_encode_latency(n);
995
996 hdsp_write(s, HDSP_controlRegister, s->control_register);
997
998 hdsp_compute_period_size(s);
999
1000 spin_unlock_irq(&s->lock);
1001
1002 return 0;
1003}
1004
Remy Brunod7923b22006-10-17 12:41:56 +02001005static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1006{
1007 u64 n;
1008 u32 r;
1009
1010 if (rate >= 112000)
1011 rate /= 4;
1012 else if (rate >= 56000)
1013 rate /= 2;
1014
Julian Cablee4b60882007-03-19 11:44:40 +01001015 n = DDS_NUMERATOR;
Remy Brunod7923b22006-10-17 12:41:56 +02001016 div64_32(&n, rate, &r);
1017 /* n should be less than 2^32 for being written to FREQ register */
1018 snd_assert((n >> 32) == 0);
1019 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1020 value to write it after a reset */
1021 hdsp->dds_value = n;
1022 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1023}
1024
Takashi Iwai55e957d2005-11-17 14:52:13 +01001025static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 int reject_if_open = 0;
1028 int current_rate;
1029 int rate_bits;
1030
1031 /* ASSUMPTION: hdsp->lock is either held, or
1032 there is no need for it (e.g. during module
1033 initialization).
1034 */
1035
1036 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
1037 if (called_internally) {
1038 /* request from ctl or card initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001039 snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -1;
1041 } else {
1042 /* hw_param request while in AutoSync mode */
1043 int external_freq = hdsp_external_sample_rate(hdsp);
1044 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
1045
Takashi Iwaib0b98112005-10-20 18:29:58 +02001046 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1047 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1048 else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1049 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
1050 else if (rate != external_freq) {
1051 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -1;
1053 }
1054 }
1055 }
1056
1057 current_rate = hdsp->system_sample_rate;
1058
1059 /* Changing from a "single speed" to a "double speed" rate is
1060 not allowed if any substreams are open. This is because
1061 such a change causes a shift in the location of
1062 the DMA buffers and a reduction in the number of available
1063 buffers.
1064
1065 Note that a similar but essentially insoluble problem
1066 exists for externally-driven rate changes. All we can do
1067 is to flag rate changes in the read/write routines. */
1068
Takashi Iwaib0b98112005-10-20 18:29:58 +02001069 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 switch (rate) {
1073 case 32000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001074 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 rate_bits = HDSP_Frequency32KHz;
1077 break;
1078 case 44100:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001079 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 rate_bits = HDSP_Frequency44_1KHz;
1082 break;
1083 case 48000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001084 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 rate_bits = HDSP_Frequency48KHz;
1087 break;
1088 case 64000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001089 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 rate_bits = HDSP_Frequency64KHz;
1092 break;
1093 case 88200:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001094 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 rate_bits = HDSP_Frequency88_2KHz;
1097 break;
1098 case 96000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001099 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rate_bits = HDSP_Frequency96KHz;
1102 break;
1103 case 128000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001104 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 rate_bits = HDSP_Frequency128KHz;
1107 break;
1108 case 176400:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001109 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 rate_bits = HDSP_Frequency176_4KHz;
1112 break;
1113 case 192000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001114 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 rate_bits = HDSP_Frequency192KHz;
1117 break;
1118 default:
1119 return -EINVAL;
1120 }
1121
1122 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1123 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1124 hdsp->capture_pid,
1125 hdsp->playback_pid);
1126 return -EBUSY;
1127 }
1128
1129 hdsp->control_register &= ~HDSP_FrequencyMask;
1130 hdsp->control_register |= rate_bits;
1131 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1132
Remy Brunod7923b22006-10-17 12:41:56 +02001133 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1134 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1135 hdsp_set_dds_value(hdsp, rate);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (rate >= 128000) {
1138 hdsp->channel_map = channel_map_H9632_qs;
1139 } else if (rate > 48000) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001140 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001142 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 } else {
1145 switch (hdsp->io_type) {
1146 case Multiface:
1147 hdsp->channel_map = channel_map_mf_ss;
1148 break;
1149 case Digiface:
1150 case H9652:
1151 hdsp->channel_map = channel_map_df_ss;
1152 break;
1153 case H9632:
1154 hdsp->channel_map = channel_map_H9632_ss;
1155 break;
1156 default:
1157 /* should never happen */
1158 break;
1159 }
1160 }
1161
1162 hdsp->system_sample_rate = rate;
1163
1164 return 0;
1165}
1166
1167/*----------------------------------------------------------------------------
1168 MIDI
1169 ----------------------------------------------------------------------------*/
1170
Takashi Iwai55e957d2005-11-17 14:52:13 +01001171static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001174 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001176 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Takashi Iwai55e957d2005-11-17 14:52:13 +01001180static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001183 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001185 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Takashi Iwai55e957d2005-11-17 14:52:13 +01001189static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001191 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001193 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Takashi Iwai55e957d2005-11-17 14:52:13 +01001197static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 int fifo_bytes_used;
1200
Takashi Iwaib0b98112005-10-20 18:29:58 +02001201 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001203 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Takashi Iwaib0b98112005-10-20 18:29:58 +02001206 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return 128 - fifo_bytes_used;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001208 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Takashi Iwai55e957d2005-11-17 14:52:13 +01001212static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001214 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Takashi Iwai55e957d2005-11-17 14:52:13 +01001218static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 unsigned long flags;
1221 int n_pending;
1222 int to_write;
1223 int i;
1224 unsigned char buf[128];
1225
1226 /* Output is not interrupt driven */
1227
1228 spin_lock_irqsave (&hmidi->lock, flags);
1229 if (hmidi->output) {
1230 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1231 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1232 if (n_pending > (int)sizeof (buf))
1233 n_pending = sizeof (buf);
1234
1235 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
1236 for (i = 0; i < to_write; ++i)
1237 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1238 }
1239 }
1240 }
1241 }
1242 spin_unlock_irqrestore (&hmidi->lock, flags);
1243 return 0;
1244}
1245
Takashi Iwai55e957d2005-11-17 14:52:13 +01001246static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1249 unsigned long flags;
1250 int n_pending;
1251 int i;
1252
1253 spin_lock_irqsave (&hmidi->lock, flags);
1254 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1255 if (hmidi->input) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001256 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 n_pending = sizeof (buf);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001258 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001260 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
1263 /* flush the MIDI input FIFO */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001264 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 }
1268 hmidi->pending = 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001269 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001271 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1274 spin_unlock_irqrestore (&hmidi->lock, flags);
1275 return snd_hdsp_midi_output_write (hmidi);
1276}
1277
Takashi Iwai55e957d2005-11-17 14:52:13 +01001278static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001280 struct hdsp *hdsp;
1281 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 unsigned long flags;
1283 u32 ie;
1284
Takashi Iwai55e957d2005-11-17 14:52:13 +01001285 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 hdsp = hmidi->hdsp;
1287 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1288 spin_lock_irqsave (&hdsp->lock, flags);
1289 if (up) {
1290 if (!(hdsp->control_register & ie)) {
1291 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1292 hdsp->control_register |= ie;
1293 }
1294 } else {
1295 hdsp->control_register &= ~ie;
1296 tasklet_kill(&hdsp->midi_tasklet);
1297 }
1298
1299 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1300 spin_unlock_irqrestore (&hdsp->lock, flags);
1301}
1302
1303static void snd_hdsp_midi_output_timer(unsigned long data)
1304{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001305 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 unsigned long flags;
1307
1308 snd_hdsp_midi_output_write(hmidi);
1309 spin_lock_irqsave (&hmidi->lock, flags);
1310
1311 /* this does not bump hmidi->istimer, because the
1312 kernel automatically removed the timer when it
1313 expired, and we are now adding it back, thus
1314 leaving istimer wherever it was set before.
1315 */
1316
1317 if (hmidi->istimer) {
1318 hmidi->timer.expires = 1 + jiffies;
1319 add_timer(&hmidi->timer);
1320 }
1321
1322 spin_unlock_irqrestore (&hmidi->lock, flags);
1323}
1324
Takashi Iwai55e957d2005-11-17 14:52:13 +01001325static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001327 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 unsigned long flags;
1329
Takashi Iwai55e957d2005-11-17 14:52:13 +01001330 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 spin_lock_irqsave (&hmidi->lock, flags);
1332 if (up) {
1333 if (!hmidi->istimer) {
1334 init_timer(&hmidi->timer);
1335 hmidi->timer.function = snd_hdsp_midi_output_timer;
1336 hmidi->timer.data = (unsigned long) hmidi;
1337 hmidi->timer.expires = 1 + jiffies;
1338 add_timer(&hmidi->timer);
1339 hmidi->istimer++;
1340 }
1341 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001342 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 spin_unlock_irqrestore (&hmidi->lock, flags);
1346 if (up)
1347 snd_hdsp_midi_output_write(hmidi);
1348}
1349
Takashi Iwai55e957d2005-11-17 14:52:13 +01001350static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001352 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Takashi Iwai55e957d2005-11-17 14:52:13 +01001354 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 spin_lock_irq (&hmidi->lock);
1356 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1357 hmidi->input = substream;
1358 spin_unlock_irq (&hmidi->lock);
1359
1360 return 0;
1361}
1362
Takashi Iwai55e957d2005-11-17 14:52:13 +01001363static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001365 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Takashi Iwai55e957d2005-11-17 14:52:13 +01001367 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 spin_lock_irq (&hmidi->lock);
1369 hmidi->output = substream;
1370 spin_unlock_irq (&hmidi->lock);
1371
1372 return 0;
1373}
1374
Takashi Iwai55e957d2005-11-17 14:52:13 +01001375static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001377 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 snd_hdsp_midi_input_trigger (substream, 0);
1380
Takashi Iwai55e957d2005-11-17 14:52:13 +01001381 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_lock_irq (&hmidi->lock);
1383 hmidi->input = NULL;
1384 spin_unlock_irq (&hmidi->lock);
1385
1386 return 0;
1387}
1388
Takashi Iwai55e957d2005-11-17 14:52:13 +01001389static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001391 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 snd_hdsp_midi_output_trigger (substream, 0);
1394
Takashi Iwai55e957d2005-11-17 14:52:13 +01001395 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 spin_lock_irq (&hmidi->lock);
1397 hmidi->output = NULL;
1398 spin_unlock_irq (&hmidi->lock);
1399
1400 return 0;
1401}
1402
Takashi Iwai55e957d2005-11-17 14:52:13 +01001403static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 .open = snd_hdsp_midi_output_open,
1406 .close = snd_hdsp_midi_output_close,
1407 .trigger = snd_hdsp_midi_output_trigger,
1408};
1409
Takashi Iwai55e957d2005-11-17 14:52:13 +01001410static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 .open = snd_hdsp_midi_input_open,
1413 .close = snd_hdsp_midi_input_close,
1414 .trigger = snd_hdsp_midi_input_trigger,
1415};
1416
Takashi Iwaif40b6892006-07-05 16:51:05 +02001417static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 char buf[32];
1420
1421 hdsp->midi[id].id = id;
1422 hdsp->midi[id].rmidi = NULL;
1423 hdsp->midi[id].input = NULL;
1424 hdsp->midi[id].output = NULL;
1425 hdsp->midi[id].hdsp = hdsp;
1426 hdsp->midi[id].istimer = 0;
1427 hdsp->midi[id].pending = 0;
1428 spin_lock_init (&hdsp->midi[id].lock);
1429
1430 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001431 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 sprintf (hdsp->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
1435 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1436
1437 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1438 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1439
1440 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1441 SNDRV_RAWMIDI_INFO_INPUT |
1442 SNDRV_RAWMIDI_INFO_DUPLEX;
1443
1444 return 0;
1445}
1446
1447/*-----------------------------------------------------------------------------
1448 Control Interface
1449 ----------------------------------------------------------------------------*/
1450
Takashi Iwai55e957d2005-11-17 14:52:13 +01001451static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 u32 val = 0;
1454 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1455 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1456 if (val & HDSP_SPDIFProfessional)
1457 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1458 else
1459 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1460 return val;
1461}
1462
Takashi Iwai55e957d2005-11-17 14:52:13 +01001463static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1466 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1467 if (val & HDSP_SPDIFProfessional)
1468 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1469 else
1470 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1471}
1472
Takashi Iwai55e957d2005-11-17 14:52:13 +01001473static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1476 uinfo->count = 1;
1477 return 0;
1478}
1479
Takashi Iwai55e957d2005-11-17 14:52:13 +01001480static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001482 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1485 return 0;
1486}
1487
Takashi Iwai55e957d2005-11-17 14:52:13 +01001488static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001490 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int change;
1492 u32 val;
1493
1494 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1495 spin_lock_irq(&hdsp->lock);
1496 change = val != hdsp->creg_spdif;
1497 hdsp->creg_spdif = val;
1498 spin_unlock_irq(&hdsp->lock);
1499 return change;
1500}
1501
Takashi Iwai55e957d2005-11-17 14:52:13 +01001502static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1505 uinfo->count = 1;
1506 return 0;
1507}
1508
Takashi Iwai55e957d2005-11-17 14:52:13 +01001509static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001511 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1514 return 0;
1515}
1516
Takashi Iwai55e957d2005-11-17 14:52:13 +01001517static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001519 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int change;
1521 u32 val;
1522
1523 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1524 spin_lock_irq(&hdsp->lock);
1525 change = val != hdsp->creg_spdif_stream;
1526 hdsp->creg_spdif_stream = val;
1527 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1528 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1529 spin_unlock_irq(&hdsp->lock);
1530 return change;
1531}
1532
Takashi Iwai55e957d2005-11-17 14:52:13 +01001533static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1536 uinfo->count = 1;
1537 return 0;
1538}
1539
Takashi Iwai55e957d2005-11-17 14:52:13 +01001540static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1543 return 0;
1544}
1545
1546#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001547{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 .name = xname, \
1549 .index = xindex, \
1550 .info = snd_hdsp_info_spdif_in, \
1551 .get = snd_hdsp_get_spdif_in, \
1552 .put = snd_hdsp_put_spdif_in }
1553
Takashi Iwai55e957d2005-11-17 14:52:13 +01001554static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1557}
1558
Takashi Iwai55e957d2005-11-17 14:52:13 +01001559static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1562 hdsp->control_register |= hdsp_encode_spdif_in(in);
1563 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1564 return 0;
1565}
1566
Takashi Iwai55e957d2005-11-17 14:52:13 +01001567static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001570 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1573 uinfo->count = 1;
1574 uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1575 if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1576 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1577 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1578 return 0;
1579}
1580
Takashi Iwai55e957d2005-11-17 14:52:13 +01001581static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001583 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1586 return 0;
1587}
1588
Takashi Iwai55e957d2005-11-17 14:52:13 +01001589static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001591 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int change;
1593 unsigned int val;
1594
1595 if (!snd_hdsp_use_is_exclusive(hdsp))
1596 return -EBUSY;
1597 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1598 spin_lock_irq(&hdsp->lock);
1599 change = val != hdsp_spdif_in(hdsp);
1600 if (change)
1601 hdsp_set_spdif_input(hdsp, val);
1602 spin_unlock_irq(&hdsp->lock);
1603 return change;
1604}
1605
1606#define HDSP_SPDIF_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001607{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .info = snd_hdsp_info_spdif_bits, \
1609 .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
1610
Takashi Iwai55e957d2005-11-17 14:52:13 +01001611static int hdsp_spdif_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
1614}
1615
Takashi Iwai55e957d2005-11-17 14:52:13 +01001616static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001618 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 hdsp->control_register |= HDSP_SPDIFOpticalOut;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001620 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1623 return 0;
1624}
1625
Takashi Iwai55e957d2005-11-17 14:52:13 +01001626static int snd_hdsp_info_spdif_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1629 uinfo->count = 1;
1630 uinfo->value.integer.min = 0;
1631 uinfo->value.integer.max = 1;
1632 return 0;
1633}
1634
Takashi Iwai55e957d2005-11-17 14:52:13 +01001635static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001637 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
1640 return 0;
1641}
1642
Takashi Iwai55e957d2005-11-17 14:52:13 +01001643static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001645 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int change;
1647 unsigned int val;
1648
1649 if (!snd_hdsp_use_is_exclusive(hdsp))
1650 return -EBUSY;
1651 val = ucontrol->value.integer.value[0] & 1;
1652 spin_lock_irq(&hdsp->lock);
1653 change = (int)val != hdsp_spdif_out(hdsp);
1654 hdsp_set_spdif_output(hdsp, val);
1655 spin_unlock_irq(&hdsp->lock);
1656 return change;
1657}
1658
1659#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001660{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 .info = snd_hdsp_info_spdif_bits, \
1662 .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
1663
Takashi Iwai55e957d2005-11-17 14:52:13 +01001664static int hdsp_spdif_professional(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
1667}
1668
Takashi Iwai55e957d2005-11-17 14:52:13 +01001669static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001671 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 hdsp->control_register |= HDSP_SPDIFProfessional;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001673 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 hdsp->control_register &= ~HDSP_SPDIFProfessional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1676 return 0;
1677}
1678
Takashi Iwai55e957d2005-11-17 14:52:13 +01001679static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001681 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
1684 return 0;
1685}
1686
Takashi Iwai55e957d2005-11-17 14:52:13 +01001687static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001689 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int change;
1691 unsigned int val;
1692
1693 if (!snd_hdsp_use_is_exclusive(hdsp))
1694 return -EBUSY;
1695 val = ucontrol->value.integer.value[0] & 1;
1696 spin_lock_irq(&hdsp->lock);
1697 change = (int)val != hdsp_spdif_professional(hdsp);
1698 hdsp_set_spdif_professional(hdsp, val);
1699 spin_unlock_irq(&hdsp->lock);
1700 return change;
1701}
1702
1703#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001704{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 .info = snd_hdsp_info_spdif_bits, \
1706 .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
1707
Takashi Iwai55e957d2005-11-17 14:52:13 +01001708static int hdsp_spdif_emphasis(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
1711}
1712
Takashi Iwai55e957d2005-11-17 14:52:13 +01001713static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001715 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 hdsp->control_register |= HDSP_SPDIFEmphasis;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001717 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 hdsp->control_register &= ~HDSP_SPDIFEmphasis;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1720 return 0;
1721}
1722
Takashi Iwai55e957d2005-11-17 14:52:13 +01001723static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001725 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
1728 return 0;
1729}
1730
Takashi Iwai55e957d2005-11-17 14:52:13 +01001731static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001733 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int change;
1735 unsigned int val;
1736
1737 if (!snd_hdsp_use_is_exclusive(hdsp))
1738 return -EBUSY;
1739 val = ucontrol->value.integer.value[0] & 1;
1740 spin_lock_irq(&hdsp->lock);
1741 change = (int)val != hdsp_spdif_emphasis(hdsp);
1742 hdsp_set_spdif_emphasis(hdsp, val);
1743 spin_unlock_irq(&hdsp->lock);
1744 return change;
1745}
1746
1747#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001748{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 .info = snd_hdsp_info_spdif_bits, \
1750 .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
1751
Takashi Iwai55e957d2005-11-17 14:52:13 +01001752static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
1755}
1756
Takashi Iwai55e957d2005-11-17 14:52:13 +01001757static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001759 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 hdsp->control_register |= HDSP_SPDIFNonAudio;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001761 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 hdsp->control_register &= ~HDSP_SPDIFNonAudio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1764 return 0;
1765}
1766
Takashi Iwai55e957d2005-11-17 14:52:13 +01001767static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001769 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
1772 return 0;
1773}
1774
Takashi Iwai55e957d2005-11-17 14:52:13 +01001775static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001777 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int change;
1779 unsigned int val;
1780
1781 if (!snd_hdsp_use_is_exclusive(hdsp))
1782 return -EBUSY;
1783 val = ucontrol->value.integer.value[0] & 1;
1784 spin_lock_irq(&hdsp->lock);
1785 change = (int)val != hdsp_spdif_nonaudio(hdsp);
1786 hdsp_set_spdif_nonaudio(hdsp, val);
1787 spin_unlock_irq(&hdsp->lock);
1788 return change;
1789}
1790
1791#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001792{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 .name = xname, \
1794 .index = xindex, \
1795 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1796 .info = snd_hdsp_info_spdif_sample_rate, \
1797 .get = snd_hdsp_get_spdif_sample_rate \
1798}
1799
Takashi Iwai55e957d2005-11-17 14:52:13 +01001800static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001803 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1806 uinfo->count = 1;
1807 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1808 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1809 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1810 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1811 return 0;
1812}
1813
Takashi Iwai55e957d2005-11-17 14:52:13 +01001814static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001816 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 switch (hdsp_spdif_sample_rate(hdsp)) {
1819 case 32000:
1820 ucontrol->value.enumerated.item[0] = 0;
1821 break;
1822 case 44100:
1823 ucontrol->value.enumerated.item[0] = 1;
1824 break;
1825 case 48000:
1826 ucontrol->value.enumerated.item[0] = 2;
1827 break;
1828 case 64000:
1829 ucontrol->value.enumerated.item[0] = 3;
1830 break;
1831 case 88200:
1832 ucontrol->value.enumerated.item[0] = 4;
1833 break;
1834 case 96000:
1835 ucontrol->value.enumerated.item[0] = 5;
1836 break;
1837 case 128000:
1838 ucontrol->value.enumerated.item[0] = 7;
1839 break;
1840 case 176400:
1841 ucontrol->value.enumerated.item[0] = 8;
1842 break;
1843 case 192000:
1844 ucontrol->value.enumerated.item[0] = 9;
1845 break;
1846 default:
1847 ucontrol->value.enumerated.item[0] = 6;
1848 }
1849 return 0;
1850}
1851
1852#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001853{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 .name = xname, \
1855 .index = xindex, \
1856 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1857 .info = snd_hdsp_info_system_sample_rate, \
1858 .get = snd_hdsp_get_system_sample_rate \
1859}
1860
Takashi Iwai55e957d2005-11-17 14:52:13 +01001861static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1864 uinfo->count = 1;
1865 return 0;
1866}
1867
Takashi Iwai55e957d2005-11-17 14:52:13 +01001868static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001870 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1873 return 0;
1874}
1875
1876#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001877{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 .name = xname, \
1879 .index = xindex, \
1880 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1881 .info = snd_hdsp_info_autosync_sample_rate, \
1882 .get = snd_hdsp_get_autosync_sample_rate \
1883}
1884
Takashi Iwai55e957d2005-11-17 14:52:13 +01001885static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001887 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
1889 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1890 uinfo->count = 1;
1891 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1892 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1893 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1894 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1895 return 0;
1896}
1897
Takashi Iwai55e957d2005-11-17 14:52:13 +01001898static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001900 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 switch (hdsp_external_sample_rate(hdsp)) {
1903 case 32000:
1904 ucontrol->value.enumerated.item[0] = 0;
1905 break;
1906 case 44100:
1907 ucontrol->value.enumerated.item[0] = 1;
1908 break;
1909 case 48000:
1910 ucontrol->value.enumerated.item[0] = 2;
1911 break;
1912 case 64000:
1913 ucontrol->value.enumerated.item[0] = 3;
1914 break;
1915 case 88200:
1916 ucontrol->value.enumerated.item[0] = 4;
1917 break;
1918 case 96000:
1919 ucontrol->value.enumerated.item[0] = 5;
1920 break;
1921 case 128000:
1922 ucontrol->value.enumerated.item[0] = 7;
1923 break;
1924 case 176400:
1925 ucontrol->value.enumerated.item[0] = 8;
1926 break;
1927 case 192000:
1928 ucontrol->value.enumerated.item[0] = 9;
1929 break;
1930 default:
1931 ucontrol->value.enumerated.item[0] = 6;
1932 }
1933 return 0;
1934}
1935
1936#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001937{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 .name = xname, \
1939 .index = xindex, \
1940 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1941 .info = snd_hdsp_info_system_clock_mode, \
1942 .get = snd_hdsp_get_system_clock_mode \
1943}
1944
Takashi Iwai55e957d2005-11-17 14:52:13 +01001945static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001947 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001949 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 1;
1952}
1953
Takashi Iwai55e957d2005-11-17 14:52:13 +01001954static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 static char *texts[] = {"Master", "Slave" };
1957
1958 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1959 uinfo->count = 1;
1960 uinfo->value.enumerated.items = 2;
1961 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1962 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1963 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1964 return 0;
1965}
1966
Takashi Iwai55e957d2005-11-17 14:52:13 +01001967static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001969 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1972 return 0;
1973}
1974
1975#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001976{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 .name = xname, \
1978 .index = xindex, \
1979 .info = snd_hdsp_info_clock_source, \
1980 .get = snd_hdsp_get_clock_source, \
1981 .put = snd_hdsp_put_clock_source \
1982}
1983
Takashi Iwai55e957d2005-11-17 14:52:13 +01001984static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 if (hdsp->control_register & HDSP_ClockModeMaster) {
1987 switch (hdsp->system_sample_rate) {
1988 case 32000:
1989 return 1;
1990 case 44100:
1991 return 2;
1992 case 48000:
1993 return 3;
1994 case 64000:
1995 return 4;
1996 case 88200:
1997 return 5;
1998 case 96000:
1999 return 6;
2000 case 128000:
2001 return 7;
2002 case 176400:
2003 return 8;
2004 case 192000:
2005 return 9;
2006 default:
2007 return 3;
2008 }
2009 } else {
2010 return 0;
2011 }
2012}
2013
Takashi Iwai55e957d2005-11-17 14:52:13 +01002014static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 int rate;
2017 switch (mode) {
2018 case HDSP_CLOCK_SOURCE_AUTOSYNC:
2019 if (hdsp_external_sample_rate(hdsp) != 0) {
2020 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
2021 hdsp->control_register &= ~HDSP_ClockModeMaster;
2022 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2023 return 0;
2024 }
2025 }
2026 return -1;
2027 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2028 rate = 32000;
2029 break;
2030 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2031 rate = 44100;
2032 break;
2033 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2034 rate = 48000;
2035 break;
2036 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2037 rate = 64000;
2038 break;
2039 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2040 rate = 88200;
2041 break;
2042 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2043 rate = 96000;
2044 break;
2045 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2046 rate = 128000;
2047 break;
2048 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2049 rate = 176400;
2050 break;
2051 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2052 rate = 192000;
2053 break;
2054 default:
2055 rate = 48000;
2056 }
2057 hdsp->control_register |= HDSP_ClockModeMaster;
2058 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2059 hdsp_set_rate(hdsp, rate, 1);
2060 return 0;
2061}
2062
Takashi Iwai55e957d2005-11-17 14:52:13 +01002063static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 static char *texts[] = {"AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz", "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz", "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz", "Internal 192.0 KHz" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002066 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2069 uinfo->count = 1;
2070 if (hdsp->io_type == H9632)
2071 uinfo->value.enumerated.items = 10;
2072 else
2073 uinfo->value.enumerated.items = 7;
2074 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2075 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2076 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2077 return 0;
2078}
2079
Takashi Iwai55e957d2005-11-17 14:52:13 +01002080static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002082 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2085 return 0;
2086}
2087
Takashi Iwai55e957d2005-11-17 14:52:13 +01002088static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002090 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int change;
2092 int val;
2093
2094 if (!snd_hdsp_use_is_exclusive(hdsp))
2095 return -EBUSY;
2096 val = ucontrol->value.enumerated.item[0];
2097 if (val < 0) val = 0;
2098 if (hdsp->io_type == H9632) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002099 if (val > 9)
2100 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002102 if (val > 6)
2103 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002106 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002108 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 spin_unlock_irq(&hdsp->lock);
2111 return change;
2112}
2113
Takashi Iwai55e957d2005-11-17 14:52:13 +01002114static int snd_hdsp_info_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002115{
2116 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2117 uinfo->count = 1;
2118 uinfo->value.integer.min = 0;
2119 uinfo->value.integer.max = 1;
2120 return 0;
2121}
2122
Takashi Iwai55e957d2005-11-17 14:52:13 +01002123static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002124{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002125 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002126
2127 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2128 return 0;
2129}
2130
Takashi Iwai55e957d2005-11-17 14:52:13 +01002131static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002132{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002133 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002134 int change;
2135
2136 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2137 if (change)
2138 hdsp->clock_source_locked = ucontrol->value.integer.value[0];
2139 return change;
2140}
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002143{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 .name = xname, \
2145 .index = xindex, \
2146 .info = snd_hdsp_info_da_gain, \
2147 .get = snd_hdsp_get_da_gain, \
2148 .put = snd_hdsp_put_da_gain \
2149}
2150
Takashi Iwai55e957d2005-11-17 14:52:13 +01002151static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 switch (hdsp->control_register & HDSP_DAGainMask) {
2154 case HDSP_DAGainHighGain:
2155 return 0;
2156 case HDSP_DAGainPlus4dBu:
2157 return 1;
2158 case HDSP_DAGainMinus10dBV:
2159 return 2;
2160 default:
2161 return 1;
2162 }
2163}
2164
Takashi Iwai55e957d2005-11-17 14:52:13 +01002165static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 hdsp->control_register &= ~HDSP_DAGainMask;
2168 switch (mode) {
2169 case 0:
2170 hdsp->control_register |= HDSP_DAGainHighGain;
2171 break;
2172 case 1:
2173 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2174 break;
2175 case 2:
2176 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2177 break;
2178 default:
2179 return -1;
2180
2181 }
2182 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2183 return 0;
2184}
2185
Takashi Iwai55e957d2005-11-17 14:52:13 +01002186static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
2189
2190 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2191 uinfo->count = 1;
2192 uinfo->value.enumerated.items = 3;
2193 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2194 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2195 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2196 return 0;
2197}
2198
Takashi Iwai55e957d2005-11-17 14:52:13 +01002199static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002201 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2204 return 0;
2205}
2206
Takashi Iwai55e957d2005-11-17 14:52:13 +01002207static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002209 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 int change;
2211 int val;
2212
2213 if (!snd_hdsp_use_is_exclusive(hdsp))
2214 return -EBUSY;
2215 val = ucontrol->value.enumerated.item[0];
2216 if (val < 0) val = 0;
2217 if (val > 2) val = 2;
2218 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002219 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002221 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 spin_unlock_irq(&hdsp->lock);
2224 return change;
2225}
2226
2227#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002228{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 .name = xname, \
2230 .index = xindex, \
2231 .info = snd_hdsp_info_ad_gain, \
2232 .get = snd_hdsp_get_ad_gain, \
2233 .put = snd_hdsp_put_ad_gain \
2234}
2235
Takashi Iwai55e957d2005-11-17 14:52:13 +01002236static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 switch (hdsp->control_register & HDSP_ADGainMask) {
2239 case HDSP_ADGainMinus10dBV:
2240 return 0;
2241 case HDSP_ADGainPlus4dBu:
2242 return 1;
2243 case HDSP_ADGainLowGain:
2244 return 2;
2245 default:
2246 return 1;
2247 }
2248}
2249
Takashi Iwai55e957d2005-11-17 14:52:13 +01002250static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 hdsp->control_register &= ~HDSP_ADGainMask;
2253 switch (mode) {
2254 case 0:
2255 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2256 break;
2257 case 1:
2258 hdsp->control_register |= HDSP_ADGainPlus4dBu;
2259 break;
2260 case 2:
2261 hdsp->control_register |= HDSP_ADGainLowGain;
2262 break;
2263 default:
2264 return -1;
2265
2266 }
2267 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2268 return 0;
2269}
2270
Takashi Iwai55e957d2005-11-17 14:52:13 +01002271static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
2274
2275 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2276 uinfo->count = 1;
2277 uinfo->value.enumerated.items = 3;
2278 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2279 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2280 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2281 return 0;
2282}
2283
Takashi Iwai55e957d2005-11-17 14:52:13 +01002284static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002286 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2289 return 0;
2290}
2291
Takashi Iwai55e957d2005-11-17 14:52:13 +01002292static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002294 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 int change;
2296 int val;
2297
2298 if (!snd_hdsp_use_is_exclusive(hdsp))
2299 return -EBUSY;
2300 val = ucontrol->value.enumerated.item[0];
2301 if (val < 0) val = 0;
2302 if (val > 2) val = 2;
2303 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002304 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002306 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 spin_unlock_irq(&hdsp->lock);
2309 return change;
2310}
2311
2312#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002313{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 .name = xname, \
2315 .index = xindex, \
2316 .info = snd_hdsp_info_phone_gain, \
2317 .get = snd_hdsp_get_phone_gain, \
2318 .put = snd_hdsp_put_phone_gain \
2319}
2320
Takashi Iwai55e957d2005-11-17 14:52:13 +01002321static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2324 case HDSP_PhoneGain0dB:
2325 return 0;
2326 case HDSP_PhoneGainMinus6dB:
2327 return 1;
2328 case HDSP_PhoneGainMinus12dB:
2329 return 2;
2330 default:
2331 return 0;
2332 }
2333}
2334
Takashi Iwai55e957d2005-11-17 14:52:13 +01002335static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 hdsp->control_register &= ~HDSP_PhoneGainMask;
2338 switch (mode) {
2339 case 0:
2340 hdsp->control_register |= HDSP_PhoneGain0dB;
2341 break;
2342 case 1:
2343 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
2344 break;
2345 case 2:
2346 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2347 break;
2348 default:
2349 return -1;
2350
2351 }
2352 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2353 return 0;
2354}
2355
Takashi Iwai55e957d2005-11-17 14:52:13 +01002356static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
2359
2360 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2361 uinfo->count = 1;
2362 uinfo->value.enumerated.items = 3;
2363 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2364 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2365 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2366 return 0;
2367}
2368
Takashi Iwai55e957d2005-11-17 14:52:13 +01002369static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002371 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2374 return 0;
2375}
2376
Takashi Iwai55e957d2005-11-17 14:52:13 +01002377static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002379 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int change;
2381 int val;
2382
2383 if (!snd_hdsp_use_is_exclusive(hdsp))
2384 return -EBUSY;
2385 val = ucontrol->value.enumerated.item[0];
2386 if (val < 0) val = 0;
2387 if (val > 2) val = 2;
2388 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002389 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002391 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 spin_unlock_irq(&hdsp->lock);
2394 return change;
2395}
2396
2397#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002398{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 .name = xname, \
2400 .index = xindex, \
2401 .info = snd_hdsp_info_xlr_breakout_cable, \
2402 .get = snd_hdsp_get_xlr_breakout_cable, \
2403 .put = snd_hdsp_put_xlr_breakout_cable \
2404}
2405
Takashi Iwai55e957d2005-11-17 14:52:13 +01002406static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002408 if (hdsp->control_register & HDSP_XLRBreakoutCable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return 0;
2411}
2412
Takashi Iwai55e957d2005-11-17 14:52:13 +01002413static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002415 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 hdsp->control_register |= HDSP_XLRBreakoutCable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002417 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 hdsp->control_register &= ~HDSP_XLRBreakoutCable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2420 return 0;
2421}
2422
Takashi Iwai55e957d2005-11-17 14:52:13 +01002423static int snd_hdsp_info_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
2425 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2426 uinfo->count = 1;
2427 uinfo->value.integer.min = 0;
2428 uinfo->value.integer.max = 1;
2429 return 0;
2430}
2431
Takashi Iwai55e957d2005-11-17 14:52:13 +01002432static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002434 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
2437 return 0;
2438}
2439
Takashi Iwai55e957d2005-11-17 14:52:13 +01002440static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002442 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int change;
2444 int val;
2445
2446 if (!snd_hdsp_use_is_exclusive(hdsp))
2447 return -EBUSY;
2448 val = ucontrol->value.integer.value[0] & 1;
2449 spin_lock_irq(&hdsp->lock);
2450 change = (int)val != hdsp_xlr_breakout_cable(hdsp);
2451 hdsp_set_xlr_breakout_cable(hdsp, val);
2452 spin_unlock_irq(&hdsp->lock);
2453 return change;
2454}
2455
2456/* (De)activates old RME Analog Extension Board
2457 These are connected to the internal ADAT connector
2458 Switching this on desactivates external ADAT
2459*/
2460#define HDSP_AEB(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002461{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 .name = xname, \
2463 .index = xindex, \
2464 .info = snd_hdsp_info_aeb, \
2465 .get = snd_hdsp_get_aeb, \
2466 .put = snd_hdsp_put_aeb \
2467}
2468
Takashi Iwai55e957d2005-11-17 14:52:13 +01002469static int hdsp_aeb(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002471 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 0;
2474}
2475
Takashi Iwai55e957d2005-11-17 14:52:13 +01002476static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002478 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 hdsp->control_register |= HDSP_AnalogExtensionBoard;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002480 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2483 return 0;
2484}
2485
Takashi Iwai55e957d2005-11-17 14:52:13 +01002486static int snd_hdsp_info_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2489 uinfo->count = 1;
2490 uinfo->value.integer.min = 0;
2491 uinfo->value.integer.max = 1;
2492 return 0;
2493}
2494
Takashi Iwai55e957d2005-11-17 14:52:13 +01002495static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002497 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
2500 return 0;
2501}
2502
Takashi Iwai55e957d2005-11-17 14:52:13 +01002503static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002505 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 int change;
2507 int val;
2508
2509 if (!snd_hdsp_use_is_exclusive(hdsp))
2510 return -EBUSY;
2511 val = ucontrol->value.integer.value[0] & 1;
2512 spin_lock_irq(&hdsp->lock);
2513 change = (int)val != hdsp_aeb(hdsp);
2514 hdsp_set_aeb(hdsp, val);
2515 spin_unlock_irq(&hdsp->lock);
2516 return change;
2517}
2518
2519#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002520{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 .name = xname, \
2522 .index = xindex, \
2523 .info = snd_hdsp_info_pref_sync_ref, \
2524 .get = snd_hdsp_get_pref_sync_ref, \
2525 .put = snd_hdsp_put_pref_sync_ref \
2526}
2527
Takashi Iwai55e957d2005-11-17 14:52:13 +01002528static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 /* Notice that this looks at the requested sync source,
2531 not the one actually in use.
2532 */
2533
2534 switch (hdsp->control_register & HDSP_SyncRefMask) {
2535 case HDSP_SyncRef_ADAT1:
2536 return HDSP_SYNC_FROM_ADAT1;
2537 case HDSP_SyncRef_ADAT2:
2538 return HDSP_SYNC_FROM_ADAT2;
2539 case HDSP_SyncRef_ADAT3:
2540 return HDSP_SYNC_FROM_ADAT3;
2541 case HDSP_SyncRef_SPDIF:
2542 return HDSP_SYNC_FROM_SPDIF;
2543 case HDSP_SyncRef_WORD:
2544 return HDSP_SYNC_FROM_WORD;
2545 case HDSP_SyncRef_ADAT_SYNC:
2546 return HDSP_SYNC_FROM_ADAT_SYNC;
2547 default:
2548 return HDSP_SYNC_FROM_WORD;
2549 }
2550 return 0;
2551}
2552
Takashi Iwai55e957d2005-11-17 14:52:13 +01002553static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 hdsp->control_register &= ~HDSP_SyncRefMask;
2556 switch (pref) {
2557 case HDSP_SYNC_FROM_ADAT1:
2558 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2559 break;
2560 case HDSP_SYNC_FROM_ADAT2:
2561 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2562 break;
2563 case HDSP_SYNC_FROM_ADAT3:
2564 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2565 break;
2566 case HDSP_SYNC_FROM_SPDIF:
2567 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2568 break;
2569 case HDSP_SYNC_FROM_WORD:
2570 hdsp->control_register |= HDSP_SyncRef_WORD;
2571 break;
2572 case HDSP_SYNC_FROM_ADAT_SYNC:
2573 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2574 break;
2575 default:
2576 return -1;
2577 }
2578 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2579 return 0;
2580}
2581
Takashi Iwai55e957d2005-11-17 14:52:13 +01002582static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
2584 static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002585 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2588 uinfo->count = 1;
2589
2590 switch (hdsp->io_type) {
2591 case Digiface:
2592 case H9652:
2593 uinfo->value.enumerated.items = 6;
2594 break;
2595 case Multiface:
2596 uinfo->value.enumerated.items = 4;
2597 break;
2598 case H9632:
2599 uinfo->value.enumerated.items = 3;
2600 break;
2601 default:
2602 uinfo->value.enumerated.items = 0;
2603 break;
2604 }
2605
2606 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2607 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2608 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2609 return 0;
2610}
2611
Takashi Iwai55e957d2005-11-17 14:52:13 +01002612static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002614 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2617 return 0;
2618}
2619
Takashi Iwai55e957d2005-11-17 14:52:13 +01002620static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002622 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 int change, max;
2624 unsigned int val;
2625
2626 if (!snd_hdsp_use_is_exclusive(hdsp))
2627 return -EBUSY;
2628
2629 switch (hdsp->io_type) {
2630 case Digiface:
2631 case H9652:
2632 max = 6;
2633 break;
2634 case Multiface:
2635 max = 4;
2636 break;
2637 case H9632:
2638 max = 3;
2639 break;
2640 default:
2641 return -EIO;
2642 }
2643
2644 val = ucontrol->value.enumerated.item[0] % max;
2645 spin_lock_irq(&hdsp->lock);
2646 change = (int)val != hdsp_pref_sync_ref(hdsp);
2647 hdsp_set_pref_sync_ref(hdsp, val);
2648 spin_unlock_irq(&hdsp->lock);
2649 return change;
2650}
2651
2652#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002653{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 .name = xname, \
2655 .index = xindex, \
2656 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2657 .info = snd_hdsp_info_autosync_ref, \
2658 .get = snd_hdsp_get_autosync_ref, \
2659}
2660
Takashi Iwai55e957d2005-11-17 14:52:13 +01002661static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 /* This looks at the autosync selected sync reference */
2664 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2665
2666 switch (status2 & HDSP_SelSyncRefMask) {
2667 case HDSP_SelSyncRef_WORD:
2668 return HDSP_AUTOSYNC_FROM_WORD;
2669 case HDSP_SelSyncRef_ADAT_SYNC:
2670 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2671 case HDSP_SelSyncRef_SPDIF:
2672 return HDSP_AUTOSYNC_FROM_SPDIF;
2673 case HDSP_SelSyncRefMask:
2674 return HDSP_AUTOSYNC_FROM_NONE;
2675 case HDSP_SelSyncRef_ADAT1:
2676 return HDSP_AUTOSYNC_FROM_ADAT1;
2677 case HDSP_SelSyncRef_ADAT2:
2678 return HDSP_AUTOSYNC_FROM_ADAT2;
2679 case HDSP_SelSyncRef_ADAT3:
2680 return HDSP_AUTOSYNC_FROM_ADAT3;
2681 default:
2682 return HDSP_AUTOSYNC_FROM_WORD;
2683 }
2684 return 0;
2685}
2686
Takashi Iwai55e957d2005-11-17 14:52:13 +01002687static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
2690
2691 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2692 uinfo->count = 1;
2693 uinfo->value.enumerated.items = 7;
2694 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2695 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2696 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2697 return 0;
2698}
2699
Takashi Iwai55e957d2005-11-17 14:52:13 +01002700static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002702 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2705 return 0;
2706}
2707
2708#define HDSP_LINE_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002709{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 .name = xname, \
2711 .index = xindex, \
2712 .info = snd_hdsp_info_line_out, \
2713 .get = snd_hdsp_get_line_out, \
2714 .put = snd_hdsp_put_line_out \
2715}
2716
Takashi Iwai55e957d2005-11-17 14:52:13 +01002717static int hdsp_line_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
2720}
2721
Takashi Iwai55e957d2005-11-17 14:52:13 +01002722static int hdsp_set_line_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002724 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 hdsp->control_register |= HDSP_LineOut;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002726 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 hdsp->control_register &= ~HDSP_LineOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2729 return 0;
2730}
2731
Takashi Iwai55e957d2005-11-17 14:52:13 +01002732static int snd_hdsp_info_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
2734 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2735 uinfo->count = 1;
2736 uinfo->value.integer.min = 0;
2737 uinfo->value.integer.max = 1;
2738 return 0;
2739}
2740
Takashi Iwai55e957d2005-11-17 14:52:13 +01002741static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002743 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 spin_lock_irq(&hdsp->lock);
2746 ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
2747 spin_unlock_irq(&hdsp->lock);
2748 return 0;
2749}
2750
Takashi Iwai55e957d2005-11-17 14:52:13 +01002751static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002753 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 int change;
2755 unsigned int val;
2756
2757 if (!snd_hdsp_use_is_exclusive(hdsp))
2758 return -EBUSY;
2759 val = ucontrol->value.integer.value[0] & 1;
2760 spin_lock_irq(&hdsp->lock);
2761 change = (int)val != hdsp_line_out(hdsp);
2762 hdsp_set_line_output(hdsp, val);
2763 spin_unlock_irq(&hdsp->lock);
2764 return change;
2765}
2766
2767#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002768{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .name = xname, \
2770 .index = xindex, \
2771 .info = snd_hdsp_info_precise_pointer, \
2772 .get = snd_hdsp_get_precise_pointer, \
2773 .put = snd_hdsp_put_precise_pointer \
2774}
2775
Takashi Iwai55e957d2005-11-17 14:52:13 +01002776static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002778 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 hdsp->precise_ptr = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002780 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return 0;
2783}
2784
Takashi Iwai55e957d2005-11-17 14:52:13 +01002785static int snd_hdsp_info_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2788 uinfo->count = 1;
2789 uinfo->value.integer.min = 0;
2790 uinfo->value.integer.max = 1;
2791 return 0;
2792}
2793
Takashi Iwai55e957d2005-11-17 14:52:13 +01002794static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002796 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 spin_lock_irq(&hdsp->lock);
2799 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2800 spin_unlock_irq(&hdsp->lock);
2801 return 0;
2802}
2803
Takashi Iwai55e957d2005-11-17 14:52:13 +01002804static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002806 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 int change;
2808 unsigned int val;
2809
2810 if (!snd_hdsp_use_is_exclusive(hdsp))
2811 return -EBUSY;
2812 val = ucontrol->value.integer.value[0] & 1;
2813 spin_lock_irq(&hdsp->lock);
2814 change = (int)val != hdsp->precise_ptr;
2815 hdsp_set_precise_pointer(hdsp, val);
2816 spin_unlock_irq(&hdsp->lock);
2817 return change;
2818}
2819
2820#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002821{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .name = xname, \
2823 .index = xindex, \
2824 .info = snd_hdsp_info_use_midi_tasklet, \
2825 .get = snd_hdsp_get_use_midi_tasklet, \
2826 .put = snd_hdsp_put_use_midi_tasklet \
2827}
2828
Takashi Iwai55e957d2005-11-17 14:52:13 +01002829static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002831 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002833 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
2836}
2837
Takashi Iwai55e957d2005-11-17 14:52:13 +01002838static int snd_hdsp_info_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2841 uinfo->count = 1;
2842 uinfo->value.integer.min = 0;
2843 uinfo->value.integer.max = 1;
2844 return 0;
2845}
2846
Takashi Iwai55e957d2005-11-17 14:52:13 +01002847static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002849 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 spin_lock_irq(&hdsp->lock);
2852 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2853 spin_unlock_irq(&hdsp->lock);
2854 return 0;
2855}
2856
Takashi Iwai55e957d2005-11-17 14:52:13 +01002857static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002859 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int change;
2861 unsigned int val;
2862
2863 if (!snd_hdsp_use_is_exclusive(hdsp))
2864 return -EBUSY;
2865 val = ucontrol->value.integer.value[0] & 1;
2866 spin_lock_irq(&hdsp->lock);
2867 change = (int)val != hdsp->use_midi_tasklet;
2868 hdsp_set_use_midi_tasklet(hdsp, val);
2869 spin_unlock_irq(&hdsp->lock);
2870 return change;
2871}
2872
2873#define HDSP_MIXER(xname, xindex) \
2874{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2875 .name = xname, \
2876 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002877 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2879 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2880 .info = snd_hdsp_info_mixer, \
2881 .get = snd_hdsp_get_mixer, \
2882 .put = snd_hdsp_put_mixer \
2883}
2884
Takashi Iwai55e957d2005-11-17 14:52:13 +01002885static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2888 uinfo->count = 3;
2889 uinfo->value.integer.min = 0;
2890 uinfo->value.integer.max = 65536;
2891 uinfo->value.integer.step = 1;
2892 return 0;
2893}
2894
Takashi Iwai55e957d2005-11-17 14:52:13 +01002895static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002897 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 int source;
2899 int destination;
2900 int addr;
2901
2902 source = ucontrol->value.integer.value[0];
2903 destination = ucontrol->value.integer.value[1];
2904
Takashi Iwaib0b98112005-10-20 18:29:58 +02002905 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002907 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 spin_lock_irq(&hdsp->lock);
2911 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2912 spin_unlock_irq(&hdsp->lock);
2913 return 0;
2914}
2915
Takashi Iwai55e957d2005-11-17 14:52:13 +01002916static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002918 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 int change;
2920 int source;
2921 int destination;
2922 int gain;
2923 int addr;
2924
2925 if (!snd_hdsp_use_is_exclusive(hdsp))
2926 return -EBUSY;
2927
2928 source = ucontrol->value.integer.value[0];
2929 destination = ucontrol->value.integer.value[1];
2930
Takashi Iwaib0b98112005-10-20 18:29:58 +02002931 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002933 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 gain = ucontrol->value.integer.value[2];
2937
2938 spin_lock_irq(&hdsp->lock);
2939 change = gain != hdsp_read_gain(hdsp, addr);
2940 if (change)
2941 hdsp_write_gain(hdsp, addr, gain);
2942 spin_unlock_irq(&hdsp->lock);
2943 return change;
2944}
2945
2946#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002947{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 .name = xname, \
2949 .index = xindex, \
2950 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2951 .info = snd_hdsp_info_sync_check, \
2952 .get = snd_hdsp_get_wc_sync_check \
2953}
2954
Takashi Iwai55e957d2005-11-17 14:52:13 +01002955static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 static char *texts[] = {"No Lock", "Lock", "Sync" };
2958 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2959 uinfo->count = 1;
2960 uinfo->value.enumerated.items = 3;
2961 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2962 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2963 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2964 return 0;
2965}
2966
Takashi Iwai55e957d2005-11-17 14:52:13 +01002967static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2970 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002971 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002973 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002975 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return 0;
2978}
2979
Takashi Iwai55e957d2005-11-17 14:52:13 +01002980static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002982 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2985 return 0;
2986}
2987
2988#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002989{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .name = xname, \
2991 .index = xindex, \
2992 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2993 .info = snd_hdsp_info_sync_check, \
2994 .get = snd_hdsp_get_spdif_sync_check \
2995}
2996
Takashi Iwai55e957d2005-11-17 14:52:13 +01002997static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003000 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003002 else {
3003 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003005 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
3008 return 0;
3009}
3010
Takashi Iwai55e957d2005-11-17 14:52:13 +01003011static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003013 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
3016 return 0;
3017}
3018
3019#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003020{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 .name = xname, \
3022 .index = xindex, \
3023 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3024 .info = snd_hdsp_info_sync_check, \
3025 .get = snd_hdsp_get_adatsync_sync_check \
3026}
3027
Takashi Iwai55e957d2005-11-17 14:52:13 +01003028static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 int status = hdsp_read(hdsp, HDSP_statusRegister);
3031 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003032 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003034 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003036 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Takashi Iwai55e957d2005-11-17 14:52:13 +01003040static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003042 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
3045 return 0;
3046}
3047
3048#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003049{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3051 .info = snd_hdsp_info_sync_check, \
3052 .get = snd_hdsp_get_adat_sync_check \
3053}
3054
Takashi Iwai55e957d2005-11-17 14:52:13 +01003055static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 int status = hdsp_read(hdsp, HDSP_statusRegister);
3058
3059 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003060 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003062 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003064 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Takashi Iwai55e957d2005-11-17 14:52:13 +01003068static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003071 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 offset = ucontrol->id.index - 1;
3074 snd_assert(offset >= 0);
3075
3076 switch (hdsp->io_type) {
3077 case Digiface:
3078 case H9652:
3079 if (offset >= 3)
3080 return -EINVAL;
3081 break;
3082 case Multiface:
3083 case H9632:
3084 if (offset >= 1)
3085 return -EINVAL;
3086 break;
3087 default:
3088 return -EIO;
3089 }
3090
3091 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
3092 return 0;
3093}
3094
Julian Cablee4b60882007-03-19 11:44:40 +01003095#define HDSP_DDS_OFFSET(xname, xindex) \
3096{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3097 .name = xname, \
3098 .index = xindex, \
3099 .info = snd_hdsp_info_dds_offset, \
3100 .get = snd_hdsp_get_dds_offset, \
3101 .put = snd_hdsp_put_dds_offset \
3102}
3103
3104static int hdsp_dds_offset(struct hdsp *hdsp)
3105{
3106 u64 n;
3107 u32 r;
3108 unsigned int dds_value = hdsp->dds_value;
3109 int system_sample_rate = hdsp->system_sample_rate;
3110
3111 n = DDS_NUMERATOR;
3112 /*
3113 * dds_value = n / rate
3114 * rate = n / dds_value
3115 */
3116 div64_32(&n, dds_value, &r);
3117 if (system_sample_rate >= 112000)
3118 n *= 4;
3119 else if (system_sample_rate >= 56000)
3120 n *= 2;
3121 return ((int)n) - system_sample_rate;
3122}
3123
3124static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
3125{
3126 int rate = hdsp->system_sample_rate + offset_hz;
3127 hdsp_set_dds_value(hdsp, rate);
3128 return 0;
3129}
3130
3131static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3132{
3133 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3134 uinfo->count = 1;
3135 uinfo->value.integer.min = -5000;
3136 uinfo->value.integer.max = 5000;
3137 return 0;
3138}
3139
3140static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3141{
3142 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3143
3144 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
3145 return 0;
3146}
3147
3148static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3149{
3150 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3151 int change;
3152 int val;
3153
3154 if (!snd_hdsp_use_is_exclusive(hdsp))
3155 return -EBUSY;
3156 val = ucontrol->value.enumerated.item[0];
3157 spin_lock_irq(&hdsp->lock);
3158 if (val != hdsp_dds_offset(hdsp))
3159 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
3160 else
3161 change = 0;
3162 spin_unlock_irq(&hdsp->lock);
3163 return change;
3164}
3165
Takashi Iwai55e957d2005-11-17 14:52:13 +01003166static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167HDSP_DA_GAIN("DA Gain", 0),
3168HDSP_AD_GAIN("AD Gain", 0),
3169HDSP_PHONE_GAIN("Phones Gain", 0),
Julian Cablee4b60882007-03-19 11:44:40 +01003170HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
3171HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172};
3173
Takashi Iwai55e957d2005-11-17 14:52:13 +01003174static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Clemens Ladisch5549d542005-08-03 13:50:30 +02003176 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
3178 .info = snd_hdsp_control_spdif_info,
3179 .get = snd_hdsp_control_spdif_get,
3180 .put = snd_hdsp_control_spdif_put,
3181},
3182{
3183 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003184 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
3186 .info = snd_hdsp_control_spdif_stream_info,
3187 .get = snd_hdsp_control_spdif_stream_get,
3188 .put = snd_hdsp_control_spdif_stream_put,
3189},
3190{
3191 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003192 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
3194 .info = snd_hdsp_control_spdif_mask_info,
3195 .get = snd_hdsp_control_spdif_mask_get,
3196 .private_value = IEC958_AES0_NONAUDIO |
3197 IEC958_AES0_PROFESSIONAL |
3198 IEC958_AES0_CON_EMPHASIS,
3199},
3200{
3201 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003202 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
3204 .info = snd_hdsp_control_spdif_mask_info,
3205 .get = snd_hdsp_control_spdif_mask_get,
3206 .private_value = IEC958_AES0_NONAUDIO |
3207 IEC958_AES0_PROFESSIONAL |
3208 IEC958_AES0_PRO_EMPHASIS,
3209},
3210HDSP_MIXER("Mixer", 0),
3211HDSP_SPDIF_IN("IEC958 Input Connector", 0),
3212HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
3213HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
3214HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
3215HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
3216/* 'Sample Clock Source' complies with the alsa control naming scheme */
3217HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003218{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003219 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3220 .name = "Sample Clock Source Locking",
3221 .info = snd_hdsp_info_clock_source_lock,
3222 .get = snd_hdsp_get_clock_source_lock,
3223 .put = snd_hdsp_put_clock_source_lock,
3224},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3226HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3227HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3228HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3229HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3230/* 'External Rate' complies with the alsa control naming scheme */
3231HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3232HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3233HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3234HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
3235HDSP_LINE_OUT("Line Out", 0),
3236HDSP_PRECISE_POINTER("Precise Pointer", 0),
3237HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3238};
3239
Takashi Iwai55e957d2005-11-17 14:52:13 +01003240static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
3241static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Takashi Iwai55e957d2005-11-17 14:52:13 +01003243static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
3245 unsigned int idx;
3246 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003247 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003250 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3253 hdsp->spdif_ctl = kctl;
3254 }
3255
3256 /* ADAT SyncCheck status */
3257 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3258 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003259 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3262 for (idx = 1; idx < 3; ++idx) {
3263 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003264 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267 }
3268
3269 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3270 if (hdsp->io_type == H9632) {
3271 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003272 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 }
3275 }
3276
3277 /* AEB control for H96xx card */
3278 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003279 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
3282
3283 return 0;
3284}
3285
3286/*------------------------------------------------------------
3287 /proc interface
3288 ------------------------------------------------------------*/
3289
3290static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003291snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003293 struct hdsp *hdsp = (struct hdsp *) entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 unsigned int status;
3295 unsigned int status2;
3296 char *pref_sync_ref;
3297 char *autosync_ref;
3298 char *system_clock_mode;
3299 char *clock_source;
3300 int x;
3301
Remy Brunoecb594e2006-06-12 09:25:22 +02003302 if (hdsp_check_for_iobox (hdsp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
3304 return;
Remy Brunoecb594e2006-06-12 09:25:22 +02003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Takashi Iwaib0b98112005-10-20 18:29:58 +02003307 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (hdsp->state & HDSP_FirmwareCached) {
3309 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3310 snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
3311 return;
3312 }
3313 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003314 int err = -EINVAL;
3315#ifdef HDSP_FW_LOADER
3316 err = hdsp_request_fw_loader(hdsp);
3317#endif
3318 if (err < 0) {
3319 snd_iprintf(buffer,
3320 "No firmware loaded nor cached, "
3321 "please upload firmware.\n");
3322 return;
3323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325 }
3326
3327 status = hdsp_read(hdsp, HDSP_statusRegister);
3328 status2 = hdsp_read(hdsp, HDSP_status2Register);
3329
3330 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
3331 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3332 hdsp->capture_buffer, hdsp->playback_buffer);
3333 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3334 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3335 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3336 snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
3337 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3338 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3339 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3340 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3341 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3342 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3343 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3344 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3345
3346 snd_iprintf(buffer, "\n");
3347
3348 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3349
3350 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3351 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3352 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3353 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3354
3355 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3356
3357 snd_iprintf(buffer, "\n");
3358
3359
3360 switch (hdsp_clock_source(hdsp)) {
3361 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3362 clock_source = "AutoSync";
3363 break;
3364 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3365 clock_source = "Internal 32 kHz";
3366 break;
3367 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3368 clock_source = "Internal 44.1 kHz";
3369 break;
3370 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3371 clock_source = "Internal 48 kHz";
3372 break;
3373 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3374 clock_source = "Internal 64 kHz";
3375 break;
3376 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3377 clock_source = "Internal 88.2 kHz";
3378 break;
3379 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3380 clock_source = "Internal 96 kHz";
3381 break;
3382 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3383 clock_source = "Internal 128 kHz";
3384 break;
3385 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3386 clock_source = "Internal 176.4 kHz";
3387 break;
3388 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3389 clock_source = "Internal 192 kHz";
3390 break;
3391 default:
3392 clock_source = "Error";
3393 }
3394 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
3395
Takashi Iwaib0b98112005-10-20 18:29:58 +02003396 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 system_clock_mode = "Slave";
Takashi Iwaib0b98112005-10-20 18:29:58 +02003398 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 system_clock_mode = "Master";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401 switch (hdsp_pref_sync_ref (hdsp)) {
3402 case HDSP_SYNC_FROM_WORD:
3403 pref_sync_ref = "Word Clock";
3404 break;
3405 case HDSP_SYNC_FROM_ADAT_SYNC:
3406 pref_sync_ref = "ADAT Sync";
3407 break;
3408 case HDSP_SYNC_FROM_SPDIF:
3409 pref_sync_ref = "SPDIF";
3410 break;
3411 case HDSP_SYNC_FROM_ADAT1:
3412 pref_sync_ref = "ADAT1";
3413 break;
3414 case HDSP_SYNC_FROM_ADAT2:
3415 pref_sync_ref = "ADAT2";
3416 break;
3417 case HDSP_SYNC_FROM_ADAT3:
3418 pref_sync_ref = "ADAT3";
3419 break;
3420 default:
3421 pref_sync_ref = "Word Clock";
3422 break;
3423 }
3424 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
3425
3426 switch (hdsp_autosync_ref (hdsp)) {
3427 case HDSP_AUTOSYNC_FROM_WORD:
3428 autosync_ref = "Word Clock";
3429 break;
3430 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3431 autosync_ref = "ADAT Sync";
3432 break;
3433 case HDSP_AUTOSYNC_FROM_SPDIF:
3434 autosync_ref = "SPDIF";
3435 break;
3436 case HDSP_AUTOSYNC_FROM_NONE:
3437 autosync_ref = "None";
3438 break;
3439 case HDSP_AUTOSYNC_FROM_ADAT1:
3440 autosync_ref = "ADAT1";
3441 break;
3442 case HDSP_AUTOSYNC_FROM_ADAT2:
3443 autosync_ref = "ADAT2";
3444 break;
3445 case HDSP_AUTOSYNC_FROM_ADAT3:
3446 autosync_ref = "ADAT3";
3447 break;
3448 default:
3449 autosync_ref = "---";
3450 break;
3451 }
3452 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
3453
3454 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
3455
3456 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3457
3458 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003459 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 snd_iprintf(buffer, "\n");
3462
3463 switch (hdsp_spdif_in(hdsp)) {
3464 case HDSP_SPDIFIN_OPTICAL:
3465 snd_iprintf(buffer, "IEC958 input: Optical\n");
3466 break;
3467 case HDSP_SPDIFIN_COAXIAL:
3468 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3469 break;
3470 case HDSP_SPDIFIN_INTERNAL:
3471 snd_iprintf(buffer, "IEC958 input: Internal\n");
3472 break;
3473 case HDSP_SPDIFIN_AES:
3474 snd_iprintf(buffer, "IEC958 input: AES\n");
3475 break;
3476 default:
3477 snd_iprintf(buffer, "IEC958 input: ???\n");
3478 break;
3479 }
3480
Takashi Iwaib0b98112005-10-20 18:29:58 +02003481 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003483 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Takashi Iwaib0b98112005-10-20 18:29:58 +02003486 if (hdsp->control_register & HDSP_SPDIFProfessional)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 snd_iprintf(buffer, "IEC958 quality: Professional\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003488 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Takashi Iwaib0b98112005-10-20 18:29:58 +02003491 if (hdsp->control_register & HDSP_SPDIFEmphasis)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 snd_iprintf(buffer, "IEC958 emphasis: on\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003493 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 snd_iprintf(buffer, "IEC958 emphasis: off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Takashi Iwaib0b98112005-10-20 18:29:58 +02003496 if (hdsp->control_register & HDSP_SPDIFNonAudio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003498 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003500 if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003502 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 snd_iprintf(buffer, "\n");
3506
3507 /* Sync Check */
3508 x = status & HDSP_Sync0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003509 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003511 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 switch (hdsp->io_type) {
3515 case Digiface:
3516 case H9652:
3517 x = status & HDSP_Sync1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003518 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003520 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 x = status & HDSP_Sync2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003523 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003525 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 default:
3529 /* relax */
3530 break;
3531 }
3532
3533 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003534 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003536 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003540 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003542 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 snd_iprintf (buffer, "Word Clock: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003546 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003548 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 snd_iprintf(buffer, "\n");
3552
3553 /* Informations about H9632 specific controls */
3554 if (hdsp->io_type == H9632) {
3555 char *tmp;
3556
3557 switch (hdsp_ad_gain(hdsp)) {
3558 case 0:
3559 tmp = "-10 dBV";
3560 break;
3561 case 1:
3562 tmp = "+4 dBu";
3563 break;
3564 default:
3565 tmp = "Lo Gain";
3566 break;
3567 }
3568 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3569
3570 switch (hdsp_da_gain(hdsp)) {
3571 case 0:
3572 tmp = "Hi Gain";
3573 break;
3574 case 1:
3575 tmp = "+4 dBu";
3576 break;
3577 default:
3578 tmp = "-10 dBV";
3579 break;
3580 }
3581 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
3582
3583 switch (hdsp_phone_gain(hdsp)) {
3584 case 0:
3585 tmp = "0 dB";
3586 break;
3587 case 1:
3588 tmp = "-6 dB";
3589 break;
3590 default:
3591 tmp = "-12 dB";
3592 break;
3593 }
3594 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3595
3596 snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
3597
Takashi Iwaib0b98112005-10-20 18:29:58 +02003598 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003600 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 snd_iprintf(buffer, "\n");
3603 }
3604
3605}
3606
Takashi Iwai55e957d2005-11-17 14:52:13 +01003607static void __devinit snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003609 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
3611 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003612 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
Takashi Iwai55e957d2005-11-17 14:52:13 +01003615static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3618 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3619}
3620
Takashi Iwai55e957d2005-11-17 14:52:13 +01003621static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
3623 unsigned long pb_bus, cb_bus;
3624
3625 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3626 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3627 if (hdsp->capture_dma_buf.area)
3628 snd_dma_free_pages(&hdsp->capture_dma_buf);
3629 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
3630 return -ENOMEM;
3631 }
3632
3633 /* Align to bus-space 64K boundary */
3634
Clemens Ladisch7ab39922006-10-09 08:13:32 +02003635 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3636 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 /* Tell the card where it is */
3639
3640 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3641 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3642
3643 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3644 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3645
3646 return 0;
3647}
3648
Takashi Iwai55e957d2005-11-17 14:52:13 +01003649static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 unsigned int i;
3652
3653 /* ASSUMPTION: hdsp->lock is either held, or
3654 there is no need to hold it (e.g. during module
3655 initalization).
3656 */
3657
3658 /* set defaults:
3659
3660 SPDIF Input via Coax
3661 Master clock mode
3662 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3663 which implies 2 4096 sample, 32Kbyte periods).
3664 Enable line out.
3665 */
3666
3667 hdsp->control_register = HDSP_ClockModeMaster |
3668 HDSP_SPDIFInputCoaxial |
3669 hdsp_encode_latency(7) |
3670 HDSP_LineOut;
3671
3672
3673 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3674
3675#ifdef SNDRV_BIG_ENDIAN
3676 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3677#else
3678 hdsp->control2_register = 0;
3679#endif
Takashi Iwaib0b98112005-10-20 18:29:58 +02003680 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003682 else
3683 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685 hdsp_reset_hw_pointer(hdsp);
3686 hdsp_compute_period_size(hdsp);
3687
3688 /* silence everything */
3689
Takashi Iwaib0b98112005-10-20 18:29:58 +02003690 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693 for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003694 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
3697
3698 /* H9632 specific defaults */
3699 if (hdsp->io_type == H9632) {
3700 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3701 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3702 }
3703
3704 /* set a default rate so that the channel map is set up.
3705 */
3706
3707 hdsp_set_rate(hdsp, 48000, 1);
3708
3709 return 0;
3710}
3711
3712static void hdsp_midi_tasklet(unsigned long arg)
3713{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003714 struct hdsp *hdsp = (struct hdsp *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Takashi Iwaib0b98112005-10-20 18:29:58 +02003716 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003718 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
David Howells7d12e782006-10-05 14:55:46 +01003722static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003724 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 unsigned int status;
3726 int audio;
3727 int midi0;
3728 int midi1;
3729 unsigned int midi0status;
3730 unsigned int midi1status;
3731 int schedule = 0;
3732
3733 status = hdsp_read(hdsp, HDSP_statusRegister);
3734
3735 audio = status & HDSP_audioIRQPending;
3736 midi0 = status & HDSP_midi0IRQPending;
3737 midi1 = status & HDSP_midi1IRQPending;
3738
Takashi Iwaib0b98112005-10-20 18:29:58 +02003739 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
3742 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3743
3744 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3745 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
3746
3747 if (audio) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003748 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Takashi Iwaib0b98112005-10-20 18:29:58 +02003751 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754
3755 if (midi0 && midi0status) {
3756 if (hdsp->use_midi_tasklet) {
3757 /* we disable interrupts for this input until processing is done */
3758 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3759 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3760 hdsp->midi[0].pending = 1;
3761 schedule = 1;
3762 } else {
3763 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3764 }
3765 }
3766 if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
3767 if (hdsp->use_midi_tasklet) {
3768 /* we disable interrupts for this input until processing is done */
3769 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3770 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3771 hdsp->midi[1].pending = 1;
3772 schedule = 1;
3773 } else {
3774 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3775 }
3776 }
3777 if (hdsp->use_midi_tasklet && schedule)
3778 tasklet_hi_schedule(&hdsp->midi_tasklet);
3779 return IRQ_HANDLED;
3780}
3781
Takashi Iwai55e957d2005-11-17 14:52:13 +01003782static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003784 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return hdsp_hw_pointer(hdsp);
3786}
3787
Takashi Iwai55e957d2005-11-17 14:52:13 +01003788static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 int stream,
3790 int channel)
3791
3792{
3793 int mapped_channel;
3794
3795 snd_assert(channel >= 0 && channel < hdsp->max_channels, return NULL);
3796
Takashi Iwaib0b98112005-10-20 18:29:58 +02003797 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Takashi Iwaib0b98112005-10-20 18:29:58 +02003800 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003802 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Takashi Iwai55e957d2005-11-17 14:52:13 +01003806static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3808{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003809 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 char *channel_buf;
3811
3812 snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
3813
3814 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3815 snd_assert(channel_buf != NULL, return -EIO);
3816 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3817 return -EFAULT;
3818 return count;
3819}
3820
Takashi Iwai55e957d2005-11-17 14:52:13 +01003821static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3823{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003824 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 char *channel_buf;
3826
3827 snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
3828
3829 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3830 snd_assert(channel_buf != NULL, return -EIO);
3831 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3832 return -EFAULT;
3833 return count;
3834}
3835
Takashi Iwai55e957d2005-11-17 14:52:13 +01003836static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
3838{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003839 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 char *channel_buf;
3841
3842 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3843 snd_assert(channel_buf != NULL, return -EIO);
3844 memset(channel_buf + pos * 4, 0, count * 4);
3845 return count;
3846}
3847
Takashi Iwai55e957d2005-11-17 14:52:13 +01003848static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003850 struct snd_pcm_runtime *runtime = substream->runtime;
3851 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3852 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3854 other = hdsp->capture_substream;
3855 else
3856 other = hdsp->playback_substream;
3857 if (hdsp->running)
3858 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3859 else
3860 runtime->status->hw_ptr = 0;
3861 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01003862 struct snd_pcm_substream *s;
3863 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01003864 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 if (s == other) {
3866 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3867 break;
3868 }
3869 }
3870 }
3871 return 0;
3872}
3873
Takashi Iwai55e957d2005-11-17 14:52:13 +01003874static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3875 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003877 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 int err;
3879 pid_t this_pid;
3880 pid_t other_pid;
3881
Takashi Iwaib0b98112005-10-20 18:29:58 +02003882 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Takashi Iwaib0b98112005-10-20 18:29:58 +02003885 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
3888 spin_lock_irq(&hdsp->lock);
3889
3890 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3891 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
3892 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
3893 this_pid = hdsp->playback_pid;
3894 other_pid = hdsp->capture_pid;
3895 } else {
3896 this_pid = hdsp->capture_pid;
3897 other_pid = hdsp->playback_pid;
3898 }
3899
3900 if ((other_pid > 0) && (this_pid != other_pid)) {
3901
3902 /* The other stream is open, and not by the same
3903 task as this one. Make sure that the parameters
3904 that matter are the same.
3905 */
3906
3907 if (params_rate(params) != hdsp->system_sample_rate) {
3908 spin_unlock_irq(&hdsp->lock);
3909 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3910 return -EBUSY;
3911 }
3912
3913 if (params_period_size(params) != hdsp->period_bytes / 4) {
3914 spin_unlock_irq(&hdsp->lock);
3915 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3916 return -EBUSY;
3917 }
3918
3919 /* We're fine. */
3920
3921 spin_unlock_irq(&hdsp->lock);
3922 return 0;
3923
3924 } else {
3925 spin_unlock_irq(&hdsp->lock);
3926 }
3927
3928 /* how to make sure that the rate matches an externally-set one ?
3929 */
3930
3931 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003932 if (! hdsp->clock_source_locked) {
3933 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
3934 spin_unlock_irq(&hdsp->lock);
3935 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3936 return err;
3937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003939 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
3941 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
3942 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3943 return err;
3944 }
3945
3946 return 0;
3947}
3948
Takashi Iwai55e957d2005-11-17 14:52:13 +01003949static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
3950 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003952 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 int mapped_channel;
3954
3955 snd_assert(info->channel < hdsp->max_channels, return -EINVAL);
3956
Takashi Iwaib0b98112005-10-20 18:29:58 +02003957 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
3961 info->first = 0;
3962 info->step = 32;
3963 return 0;
3964}
3965
Takashi Iwai55e957d2005-11-17 14:52:13 +01003966static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 unsigned int cmd, void *arg)
3968{
3969 switch (cmd) {
3970 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b98112005-10-20 18:29:58 +02003973 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 default:
3975 break;
3976 }
3977
3978 return snd_pcm_lib_ioctl(substream, cmd, arg);
3979}
3980
Takashi Iwai55e957d2005-11-17 14:52:13 +01003981static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003983 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3984 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 int running;
3986
Takashi Iwaib0b98112005-10-20 18:29:58 +02003987 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Takashi Iwai311e70a2006-09-06 12:13:37 +02003990 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 spin_lock(&hdsp->lock);
3994 running = hdsp->running;
3995 switch (cmd) {
3996 case SNDRV_PCM_TRIGGER_START:
3997 running |= 1 << substream->stream;
3998 break;
3999 case SNDRV_PCM_TRIGGER_STOP:
4000 running &= ~(1 << substream->stream);
4001 break;
4002 default:
4003 snd_BUG();
4004 spin_unlock(&hdsp->lock);
4005 return -EINVAL;
4006 }
4007 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4008 other = hdsp->capture_substream;
4009 else
4010 other = hdsp->playback_substream;
4011
4012 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004013 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004014 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (s == other) {
4016 snd_pcm_trigger_done(s, substream);
4017 if (cmd == SNDRV_PCM_TRIGGER_START)
4018 running |= 1 << s->stream;
4019 else
4020 running &= ~(1 << s->stream);
4021 goto _ok;
4022 }
4023 }
4024 if (cmd == SNDRV_PCM_TRIGGER_START) {
4025 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4026 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4027 hdsp_silence_playback(hdsp);
4028 } else {
4029 if (running &&
4030 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4031 hdsp_silence_playback(hdsp);
4032 }
4033 } else {
4034 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4035 hdsp_silence_playback(hdsp);
4036 }
4037 _ok:
4038 snd_pcm_trigger_done(substream, substream);
4039 if (!hdsp->running && running)
4040 hdsp_start_audio(hdsp);
4041 else if (hdsp->running && !running)
4042 hdsp_stop_audio(hdsp);
4043 hdsp->running = running;
4044 spin_unlock(&hdsp->lock);
4045
4046 return 0;
4047}
4048
Takashi Iwai55e957d2005-11-17 14:52:13 +01004049static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004051 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 int result = 0;
4053
Takashi Iwaib0b98112005-10-20 18:29:58 +02004054 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Takashi Iwaib0b98112005-10-20 18:29:58 +02004057 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 spin_lock_irq(&hdsp->lock);
4061 if (!hdsp->running)
4062 hdsp_reset_hw_pointer(hdsp);
4063 spin_unlock_irq(&hdsp->lock);
4064 return result;
4065}
4066
Takashi Iwai55e957d2005-11-17 14:52:13 +01004067static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
4069 .info = (SNDRV_PCM_INFO_MMAP |
4070 SNDRV_PCM_INFO_MMAP_VALID |
4071 SNDRV_PCM_INFO_NONINTERLEAVED |
4072 SNDRV_PCM_INFO_SYNC_START |
4073 SNDRV_PCM_INFO_DOUBLE),
4074#ifdef SNDRV_BIG_ENDIAN
4075 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4076#else
4077 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4078#endif
4079 .rates = (SNDRV_PCM_RATE_32000 |
4080 SNDRV_PCM_RATE_44100 |
4081 SNDRV_PCM_RATE_48000 |
4082 SNDRV_PCM_RATE_64000 |
4083 SNDRV_PCM_RATE_88200 |
4084 SNDRV_PCM_RATE_96000),
4085 .rate_min = 32000,
4086 .rate_max = 96000,
4087 .channels_min = 14,
4088 .channels_max = HDSP_MAX_CHANNELS,
4089 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4090 .period_bytes_min = (64 * 4) * 10,
4091 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4092 .periods_min = 2,
4093 .periods_max = 2,
4094 .fifo_size = 0
4095};
4096
Takashi Iwai55e957d2005-11-17 14:52:13 +01004097static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
4099 .info = (SNDRV_PCM_INFO_MMAP |
4100 SNDRV_PCM_INFO_MMAP_VALID |
4101 SNDRV_PCM_INFO_NONINTERLEAVED |
4102 SNDRV_PCM_INFO_SYNC_START),
4103#ifdef SNDRV_BIG_ENDIAN
4104 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4105#else
4106 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4107#endif
4108 .rates = (SNDRV_PCM_RATE_32000 |
4109 SNDRV_PCM_RATE_44100 |
4110 SNDRV_PCM_RATE_48000 |
4111 SNDRV_PCM_RATE_64000 |
4112 SNDRV_PCM_RATE_88200 |
4113 SNDRV_PCM_RATE_96000),
4114 .rate_min = 32000,
4115 .rate_max = 96000,
4116 .channels_min = 14,
4117 .channels_max = HDSP_MAX_CHANNELS,
4118 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4119 .period_bytes_min = (64 * 4) * 10,
4120 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4121 .periods_min = 2,
4122 .periods_max = 2,
4123 .fifo_size = 0
4124};
4125
4126static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4127
Takashi Iwai55e957d2005-11-17 14:52:13 +01004128static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 .count = ARRAY_SIZE(hdsp_period_sizes),
4130 .list = hdsp_period_sizes,
4131 .mask = 0
4132};
4133
4134static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4135
Takashi Iwai55e957d2005-11-17 14:52:13 +01004136static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4138 .list = hdsp_9632_sample_rates,
4139 .mask = 0
4140};
4141
Takashi Iwai55e957d2005-11-17 14:52:13 +01004142static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4143 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004145 struct hdsp *hdsp = rule->private;
4146 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 if (hdsp->io_type == H9632) {
4148 unsigned int list[3];
4149 list[0] = hdsp->qs_in_channels;
4150 list[1] = hdsp->ds_in_channels;
4151 list[2] = hdsp->ss_in_channels;
4152 return snd_interval_list(c, 3, list, 0);
4153 } else {
4154 unsigned int list[2];
4155 list[0] = hdsp->ds_in_channels;
4156 list[1] = hdsp->ss_in_channels;
4157 return snd_interval_list(c, 2, list, 0);
4158 }
4159}
4160
Takashi Iwai55e957d2005-11-17 14:52:13 +01004161static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4162 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
4164 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004165 struct hdsp *hdsp = rule->private;
4166 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (hdsp->io_type == H9632) {
4168 list[0] = hdsp->qs_out_channels;
4169 list[1] = hdsp->ds_out_channels;
4170 list[2] = hdsp->ss_out_channels;
4171 return snd_interval_list(c, 3, list, 0);
4172 } else {
4173 list[0] = hdsp->ds_out_channels;
4174 list[1] = hdsp->ss_out_channels;
4175 }
4176 return snd_interval_list(c, 2, list, 0);
4177}
4178
Takashi Iwai55e957d2005-11-17 14:52:13 +01004179static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4180 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004182 struct hdsp *hdsp = rule->private;
4183 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4184 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004186 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 .min = hdsp->qs_in_channels,
4188 .max = hdsp->qs_in_channels,
4189 .integer = 1,
4190 };
4191 return snd_interval_refine(c, &t);
4192 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004193 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 .min = hdsp->ds_in_channels,
4195 .max = hdsp->ds_in_channels,
4196 .integer = 1,
4197 };
4198 return snd_interval_refine(c, &t);
4199 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004200 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 .min = hdsp->ss_in_channels,
4202 .max = hdsp->ss_in_channels,
4203 .integer = 1,
4204 };
4205 return snd_interval_refine(c, &t);
4206 }
4207 return 0;
4208}
4209
Takashi Iwai55e957d2005-11-17 14:52:13 +01004210static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4211 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004213 struct hdsp *hdsp = rule->private;
4214 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4215 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004217 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 .min = hdsp->qs_out_channels,
4219 .max = hdsp->qs_out_channels,
4220 .integer = 1,
4221 };
4222 return snd_interval_refine(c, &t);
4223 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004224 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 .min = hdsp->ds_out_channels,
4226 .max = hdsp->ds_out_channels,
4227 .integer = 1,
4228 };
4229 return snd_interval_refine(c, &t);
4230 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004231 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 .min = hdsp->ss_out_channels,
4233 .max = hdsp->ss_out_channels,
4234 .integer = 1,
4235 };
4236 return snd_interval_refine(c, &t);
4237 }
4238 return 0;
4239}
4240
Takashi Iwai55e957d2005-11-17 14:52:13 +01004241static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4242 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004244 struct hdsp *hdsp = rule->private;
4245 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4246 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004248 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 .min = 32000,
4250 .max = 48000,
4251 .integer = 1,
4252 };
4253 return snd_interval_refine(r, &t);
4254 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004255 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 .min = 128000,
4257 .max = 192000,
4258 .integer = 1,
4259 };
4260 return snd_interval_refine(r, &t);
4261 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004262 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 .min = 64000,
4264 .max = 96000,
4265 .integer = 1,
4266 };
4267 return snd_interval_refine(r, &t);
4268 }
4269 return 0;
4270}
4271
Takashi Iwai55e957d2005-11-17 14:52:13 +01004272static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4273 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004275 struct hdsp *hdsp = rule->private;
4276 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4277 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004279 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 .min = 32000,
4281 .max = 48000,
4282 .integer = 1,
4283 };
4284 return snd_interval_refine(r, &t);
4285 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004286 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 .min = 128000,
4288 .max = 192000,
4289 .integer = 1,
4290 };
4291 return snd_interval_refine(r, &t);
4292 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004293 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 .min = 64000,
4295 .max = 96000,
4296 .integer = 1,
4297 };
4298 return snd_interval_refine(r, &t);
4299 }
4300 return 0;
4301}
4302
Takashi Iwai55e957d2005-11-17 14:52:13 +01004303static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004305 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4306 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Takashi Iwaib0b98112005-10-20 18:29:58 +02004308 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Takashi Iwaib0b98112005-10-20 18:29:58 +02004311 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 spin_lock_irq(&hdsp->lock);
4315
4316 snd_pcm_set_sync(substream);
4317
4318 runtime->hw = snd_hdsp_playback_subinfo;
4319 runtime->dma_area = hdsp->playback_buffer;
4320 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4321
4322 hdsp->playback_pid = current->pid;
4323 hdsp->playback_substream = substream;
4324
4325 spin_unlock_irq(&hdsp->lock);
4326
4327 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4328 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004329 if (hdsp->clock_source_locked) {
4330 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4331 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 runtime->hw.rate_max = 192000;
4333 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4334 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4335 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004336 if (hdsp->io_type == H9632) {
4337 runtime->hw.channels_min = hdsp->qs_out_channels;
4338 runtime->hw.channels_max = hdsp->ss_out_channels;
4339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
4341 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4342 snd_hdsp_hw_rule_out_channels, hdsp,
4343 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4344 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4345 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4346 SNDRV_PCM_HW_PARAM_RATE, -1);
4347 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4348 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4349 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4350
4351 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4352 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4353 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4354 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4355 return 0;
4356}
4357
Takashi Iwai55e957d2005-11-17 14:52:13 +01004358static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004360 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 spin_lock_irq(&hdsp->lock);
4363
4364 hdsp->playback_pid = -1;
4365 hdsp->playback_substream = NULL;
4366
4367 spin_unlock_irq(&hdsp->lock);
4368
4369 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4370 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4371 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4372 return 0;
4373}
4374
4375
Takashi Iwai55e957d2005-11-17 14:52:13 +01004376static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004378 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
Takashi Iwaib0b98112005-10-20 18:29:58 +02004381 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Takashi Iwaib0b98112005-10-20 18:29:58 +02004384 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
4387 spin_lock_irq(&hdsp->lock);
4388
4389 snd_pcm_set_sync(substream);
4390
4391 runtime->hw = snd_hdsp_capture_subinfo;
4392 runtime->dma_area = hdsp->capture_buffer;
4393 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4394
4395 hdsp->capture_pid = current->pid;
4396 hdsp->capture_substream = substream;
4397
4398 spin_unlock_irq(&hdsp->lock);
4399
4400 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4401 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4402 if (hdsp->io_type == H9632) {
4403 runtime->hw.channels_min = hdsp->qs_in_channels;
4404 runtime->hw.channels_max = hdsp->ss_in_channels;
4405 runtime->hw.rate_max = 192000;
4406 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4407 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4408 }
4409 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4410 snd_hdsp_hw_rule_in_channels, hdsp,
4411 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4412 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4413 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4414 SNDRV_PCM_HW_PARAM_RATE, -1);
4415 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4416 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4417 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4418 return 0;
4419}
4420
Takashi Iwai55e957d2005-11-17 14:52:13 +01004421static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004423 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 spin_lock_irq(&hdsp->lock);
4426
4427 hdsp->capture_pid = -1;
4428 hdsp->capture_substream = NULL;
4429
4430 spin_unlock_irq(&hdsp->lock);
4431 return 0;
4432}
4433
Takashi Iwai55e957d2005-11-17 14:52:13 +01004434static int snd_hdsp_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
4436 /* we have nothing to initialize but the call is required */
4437 return 0;
4438}
4439
4440
4441/* helper functions for copying meter values */
4442static inline int copy_u32_le(void __user *dest, void __iomem *src)
4443{
4444 u32 val = readl(src);
4445 return copy_to_user(dest, &val, 4);
4446}
4447
4448static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4449{
4450 u32 rms_low, rms_high;
4451 u64 rms;
4452 rms_low = readl(src_low);
4453 rms_high = readl(src_high);
4454 rms = ((u64)rms_high << 32) | rms_low;
4455 return copy_to_user(dest, &rms, 8);
4456}
4457
4458static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4459{
4460 u32 rms_low, rms_high;
4461 u64 rms;
4462 rms_low = readl(src_low) & 0xffffff00;
4463 rms_high = readl(src_high) & 0xffffff00;
4464 rms = ((u64)rms_high << 32) | rms_low;
4465 return copy_to_user(dest, &rms, 8);
4466}
4467
Takashi Iwai55e957d2005-11-17 14:52:13 +01004468static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
4470 int doublespeed = 0;
4471 int i, j, channels, ofs;
4472
4473 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4474 doublespeed = 1;
4475 channels = doublespeed ? 14 : 26;
4476 for (i = 0, j = 0; i < 26; ++i) {
4477 if (doublespeed && (i & 4))
4478 continue;
4479 ofs = HDSP_9652_peakBase - j * 4;
4480 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4481 return -EFAULT;
4482 ofs -= channels * 4;
4483 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4484 return -EFAULT;
4485 ofs -= channels * 4;
4486 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4487 return -EFAULT;
4488 ofs = HDSP_9652_rmsBase + j * 8;
4489 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4490 hdsp->iobase + ofs + 4))
4491 return -EFAULT;
4492 ofs += channels * 8;
4493 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4494 hdsp->iobase + ofs + 4))
4495 return -EFAULT;
4496 ofs += channels * 8;
4497 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4498 hdsp->iobase + ofs + 4))
4499 return -EFAULT;
4500 j++;
4501 }
4502 return 0;
4503}
4504
Takashi Iwai55e957d2005-11-17 14:52:13 +01004505static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
4507 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004508 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 int doublespeed = 0;
4510
4511 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4512 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004513 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 for (i = 0, j = 0; i < 16; ++i, ++j) {
4515 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4516 return -EFAULT;
4517 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4518 return -EFAULT;
4519 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4520 return -EFAULT;
4521 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4522 &m->input_rms_high[j]))
4523 return -EFAULT;
4524 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4525 &m->playback_rms_high[j]))
4526 return -EFAULT;
4527 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4528 &m->output_rms_high[j]))
4529 return -EFAULT;
4530 if (doublespeed && i == 3) i += 4;
4531 }
4532 return 0;
4533}
4534
Takashi Iwai55e957d2005-11-17 14:52:13 +01004535static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
4537 int i;
4538
4539 for (i = 0; i < 26; i++) {
4540 if (copy_u32_le(&peak_rms->playback_peaks[i],
4541 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4542 return -EFAULT;
4543 if (copy_u32_le(&peak_rms->input_peaks[i],
4544 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4545 return -EFAULT;
4546 }
4547 for (i = 0; i < 28; i++) {
4548 if (copy_u32_le(&peak_rms->output_peaks[i],
4549 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4550 return -EFAULT;
4551 }
4552 for (i = 0; i < 26; ++i) {
4553 if (copy_u64_le(&peak_rms->playback_rms[i],
4554 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4555 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4556 return -EFAULT;
4557 if (copy_u64_le(&peak_rms->input_rms[i],
4558 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4559 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4560 return -EFAULT;
4561 }
4562 return 0;
4563}
4564
Takashi Iwai55e957d2005-11-17 14:52:13 +01004565static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004567 struct hdsp *hdsp = (struct hdsp *)hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 void __user *argp = (void __user *)arg;
4569
4570 switch (cmd) {
4571 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004572 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
4574 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4575 snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
4576 return -EINVAL;
4577 }
4578
4579 switch (hdsp->io_type) {
4580 case H9652:
4581 return hdsp_9652_get_peak(hdsp, peak_rms);
4582 case H9632:
4583 return hdsp_9632_get_peak(hdsp, peak_rms);
4584 default:
4585 return hdsp_get_peak(hdsp, peak_rms);
4586 }
4587 }
4588 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004589 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 unsigned long flags;
4591 int i;
4592
4593 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004594 snd_printk(KERN_ERR "Hammerfall-DSP: Firmware needs to be uploaded to the card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return -EINVAL;
4596 }
4597 spin_lock_irqsave(&hdsp->lock, flags);
4598 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4599 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004600 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004603 for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
4606 info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
4607 info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
4608 info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
4609 info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
4610 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4611 info.system_sample_rate = hdsp->system_sample_rate;
4612 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4613 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4614 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4615 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
4616 info.line_out = (unsigned char)hdsp_line_out(hdsp);
4617 if (hdsp->io_type == H9632) {
4618 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4619 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4620 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
4621 info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
4622
4623 }
Takashi Iwaib0b98112005-10-20 18:29:58 +02004624 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 spin_unlock_irqrestore(&hdsp->lock, flags);
4627 if (copy_to_user(argp, &info, sizeof(info)))
4628 return -EFAULT;
4629 break;
4630 }
4631 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004632 struct hdsp_9632_aeb h9632_aeb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 if (hdsp->io_type != H9632) return -EINVAL;
4635 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4636 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4637 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4638 return -EFAULT;
4639 break;
4640 }
4641 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004642 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 int err;
4644
4645 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4646 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004647 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
4650 hdsp_version.io_type = hdsp->io_type;
4651 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004652 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 break;
4655 }
4656 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004657 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 u32 __user *firmware_data;
4659 int err;
4660
4661 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4662 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4663 if (hdsp->io_type == Undefined) return -EINVAL;
4664
4665 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4666 return -EBUSY;
4667
Takashi Iwaib0b98112005-10-20 18:29:58 +02004668 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01004669 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Takashi Iwaib0b98112005-10-20 18:29:58 +02004671 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Takashi Iwaib0b98112005-10-20 18:29:58 +02004674 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Takashi Iwaib0b98112005-10-20 18:29:58 +02004677 if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 hdsp->state |= HDSP_FirmwareCached;
4681
Takashi Iwaib0b98112005-10-20 18:29:58 +02004682 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
4685 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004686 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
4689 snd_hdsp_initialize_channels(hdsp);
4690 snd_hdsp_initialize_midi_flush(hdsp);
4691
4692 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004693 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
4694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
4696 }
4697 break;
4698 }
4699 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004700 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4702 return -EFAULT;
4703 break;
4704 }
4705 default:
4706 return -EINVAL;
4707 }
4708 return 0;
4709}
4710
Takashi Iwai55e957d2005-11-17 14:52:13 +01004711static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 .open = snd_hdsp_playback_open,
4713 .close = snd_hdsp_playback_release,
4714 .ioctl = snd_hdsp_ioctl,
4715 .hw_params = snd_hdsp_hw_params,
4716 .prepare = snd_hdsp_prepare,
4717 .trigger = snd_hdsp_trigger,
4718 .pointer = snd_hdsp_hw_pointer,
4719 .copy = snd_hdsp_playback_copy,
4720 .silence = snd_hdsp_hw_silence,
4721};
4722
Takashi Iwai55e957d2005-11-17 14:52:13 +01004723static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 .open = snd_hdsp_capture_open,
4725 .close = snd_hdsp_capture_release,
4726 .ioctl = snd_hdsp_ioctl,
4727 .hw_params = snd_hdsp_hw_params,
4728 .prepare = snd_hdsp_prepare,
4729 .trigger = snd_hdsp_trigger,
4730 .pointer = snd_hdsp_hw_pointer,
4731 .copy = snd_hdsp_capture_copy,
4732};
4733
Takashi Iwai55e957d2005-11-17 14:52:13 +01004734static int __devinit snd_hdsp_create_hwdep(struct snd_card *card,
4735 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004737 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 int err;
4739
4740 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4741 return err;
4742
4743 hdsp->hwdep = hw;
4744 hw->private_data = hdsp;
4745 strcpy(hw->name, "HDSP hwdep interface");
4746
4747 hw->ops.open = snd_hdsp_hwdep_dummy_op;
4748 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
4749 hw->ops.release = snd_hdsp_hwdep_dummy_op;
4750
4751 return 0;
4752}
4753
Takashi Iwai55e957d2005-11-17 14:52:13 +01004754static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004756 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 int err;
4758
4759 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4760 return err;
4761
4762 hdsp->pcm = pcm;
4763 pcm->private_data = hdsp;
4764 strcpy(pcm->name, hdsp->card_name);
4765
4766 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4767 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4768
4769 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4770
4771 return 0;
4772}
4773
Takashi Iwai55e957d2005-11-17 14:52:13 +01004774static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
4776 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4777 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4778}
4779
Takashi Iwai55e957d2005-11-17 14:52:13 +01004780static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
4782 int i;
4783
4784 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004785 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 return -EIO;
4787 }
4788
4789 for (i = 0; i < hdsp->max_channels; ++i) {
4790 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4791 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4792 }
4793
4794 return 0;
4795}
4796
Takashi Iwai55e957d2005-11-17 14:52:13 +01004797static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
4799 int status, aebi_channels, aebo_channels;
4800
4801 switch (hdsp->io_type) {
4802 case Digiface:
4803 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4804 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4805 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4806 break;
4807
4808 case H9652:
4809 hdsp->card_name = "RME Hammerfall HDSP 9652";
4810 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4811 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4812 break;
4813
4814 case H9632:
4815 status = hdsp_read(hdsp, HDSP_statusRegister);
4816 /* HDSP_AEBx bits are low when AEB are connected */
4817 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4818 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4819 hdsp->card_name = "RME Hammerfall HDSP 9632";
4820 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4821 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4822 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4823 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4824 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4825 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4826 break;
4827
4828 case Multiface:
4829 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4830 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4831 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4832 break;
4833
4834 default:
4835 /* should never get here */
4836 break;
4837 }
4838}
4839
Takashi Iwai55e957d2005-11-17 14:52:13 +01004840static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
4842 snd_hdsp_flush_midi_input (hdsp, 0);
4843 snd_hdsp_flush_midi_input (hdsp, 1);
4844}
4845
Takashi Iwai55e957d2005-11-17 14:52:13 +01004846static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
4848 int err;
4849
4850 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004851 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 return err;
4853 }
4854
4855
4856 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004857 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 return err;
4859 }
4860
4861 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
4862 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004863 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return err;
4865 }
4866 }
4867
4868 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004869 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return err;
4871 }
4872
4873 snd_hdsp_proc_init(hdsp);
4874
4875 hdsp->system_sample_rate = -1;
4876 hdsp->playback_pid = -1;
4877 hdsp->capture_pid = -1;
4878 hdsp->capture_substream = NULL;
4879 hdsp->playback_substream = NULL;
4880
4881 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004882 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 return err;
4884 }
4885
4886 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02004887 strcpy(card->shortname, "Hammerfall DSP");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
4889 hdsp->port, hdsp->irq);
4890
4891 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004892 snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return err;
4894 }
4895 hdsp->state |= HDSP_InitializationComplete;
4896 }
4897
4898 return 0;
4899}
4900
4901#ifdef HDSP_FW_LOADER
4902/* load firmware via hotplug fw loader */
Takashi Iwai55e957d2005-11-17 14:52:13 +01004903static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
4905 const char *fwfile;
4906 const struct firmware *fw;
4907 int err;
4908
4909 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4910 return 0;
4911 if (hdsp->io_type == Undefined) {
4912 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
4913 return err;
4914 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4915 return 0;
4916 }
4917
4918 /* caution: max length of firmware filename is 30! */
4919 switch (hdsp->io_type) {
4920 case Multiface:
4921 if (hdsp->firmware_rev == 0xa)
4922 fwfile = "multiface_firmware.bin";
4923 else
4924 fwfile = "multiface_firmware_rev11.bin";
4925 break;
4926 case Digiface:
4927 if (hdsp->firmware_rev == 0xa)
4928 fwfile = "digiface_firmware.bin";
4929 else
4930 fwfile = "digiface_firmware_rev11.bin";
4931 break;
4932 default:
4933 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
4934 return -EINVAL;
4935 }
4936
4937 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
4938 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
4939 return -ENOENT;
4940 }
4941 if (fw->size < sizeof(hdsp->firmware_cache)) {
4942 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
4943 (int)fw->size, (int)sizeof(hdsp->firmware_cache));
4944 release_firmware(fw);
4945 return -EINVAL;
4946 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02004947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
Thomas Charbonnel7679a032005-04-25 11:35:29 +02004949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 release_firmware(fw);
4951
4952 hdsp->state |= HDSP_FirmwareCached;
4953
4954 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
4955 return err;
4956
4957 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004958 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004962 snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 return err;
4964 }
4965 snd_hdsp_initialize_channels(hdsp);
4966 snd_hdsp_initialize_midi_flush(hdsp);
4967 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004968 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return err;
4970 }
4971 }
4972 return 0;
4973}
4974#endif
4975
Takashi Iwai55e957d2005-11-17 14:52:13 +01004976static int __devinit snd_hdsp_create(struct snd_card *card,
4977 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
4979 struct pci_dev *pci = hdsp->pci;
4980 int err;
4981 int is_9652 = 0;
4982 int is_9632 = 0;
4983
4984 hdsp->irq = -1;
4985 hdsp->state = 0;
4986 hdsp->midi[0].rmidi = NULL;
4987 hdsp->midi[1].rmidi = NULL;
4988 hdsp->midi[0].input = NULL;
4989 hdsp->midi[1].input = NULL;
4990 hdsp->midi[0].output = NULL;
4991 hdsp->midi[1].output = NULL;
4992 hdsp->midi[0].pending = 0;
4993 hdsp->midi[1].pending = 0;
4994 spin_lock_init(&hdsp->midi[0].lock);
4995 spin_lock_init(&hdsp->midi[1].lock);
4996 hdsp->iobase = NULL;
4997 hdsp->control_register = 0;
4998 hdsp->control2_register = 0;
4999 hdsp->io_type = Undefined;
5000 hdsp->max_channels = 26;
5001
5002 hdsp->card = card;
5003
5004 spin_lock_init(&hdsp->lock);
5005
5006 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
5007
5008 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5009 hdsp->firmware_rev &= 0xff;
5010
5011 /* From Martin Bjoernsen :
5012 "It is important that the card's latency timer register in
5013 the PCI configuration space is set to a value much larger
5014 than 0 by the computer's BIOS or the driver.
5015 The windows driver always sets this 8 bit register [...]
5016 to its maximum 255 to avoid problems with some computers."
5017 */
5018 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
5019
5020 strcpy(card->driver, "H-DSP");
5021 strcpy(card->mixername, "Xilinx FPGA");
5022
Takashi Iwaib0b98112005-10-20 18:29:58 +02005023 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 return -ENODEV;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005025 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b98112005-10-20 18:29:58 +02005027 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 hdsp->card_name = "RME HDSP 9652";
5029 is_9652 = 1;
5030 } else {
5031 hdsp->card_name = "RME HDSP 9632";
5032 hdsp->max_channels = 16;
5033 is_9632 = 1;
5034 }
5035
Takashi Iwaib0b98112005-10-20 18:29:58 +02005036 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
5039 pci_set_master(hdsp->pci);
5040
5041 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5042 return err;
5043 hdsp->port = pci_resource_start(pci, 0);
5044 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005045 snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return -EBUSY;
5047 }
5048
Takashi Iwai437a5a42006-11-21 12:14:23 +01005049 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
5050 "hdsp", hdsp)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005051 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 return -EBUSY;
5053 }
5054
5055 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005056 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005058 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Takashi Iwaib0b98112005-10-20 18:29:58 +02005060 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 if (!is_9652 && !is_9632) {
5064 /* we wait 2 seconds to let freshly inserted cardbus cards do their hardware init */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005065 ssleep(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
5067 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
5068#ifdef HDSP_FW_LOADER
Takashi Iwaib0b98112005-10-20 18:29:58 +02005069 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 /* we don't fail as this can happen
5071 if userspace is not ready for
5072 firmware upload
5073 */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005074 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5075 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /* init is complete, we return */
5077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078#endif
5079 /* no iobox connected, we defer initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005080 snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5081 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return 0;
5084 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005085 snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
5086 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005088 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 }
5091 }
5092
Takashi Iwaib0b98112005-10-20 18:29:58 +02005093 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Takashi Iwaib0b98112005-10-20 18:29:58 +02005096 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 hdsp->io_type = H9652;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Takashi Iwaib0b98112005-10-20 18:29:58 +02005099 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Takashi Iwaib0b98112005-10-20 18:29:58 +02005102 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
5105 snd_hdsp_initialize_channels(hdsp);
5106 snd_hdsp_initialize_midi_flush(hdsp);
5107
5108 hdsp->state |= HDSP_FirmwareLoaded;
5109
Takashi Iwaib0b98112005-10-20 18:29:58 +02005110 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
5113 return 0;
5114}
5115
Takashi Iwai55e957d2005-11-17 14:52:13 +01005116static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
5118 if (hdsp->port) {
5119 /* stop the audio, and cancel all interrupts */
5120 tasklet_kill(&hdsp->midi_tasklet);
5121 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5122 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5123 }
5124
5125 if (hdsp->irq >= 0)
5126 free_irq(hdsp->irq, (void *)hdsp);
5127
5128 snd_hdsp_free_buffers(hdsp);
5129
5130 if (hdsp->iobase)
5131 iounmap(hdsp->iobase);
5132
5133 if (hdsp->port)
5134 pci_release_regions(hdsp->pci);
5135
5136 pci_disable_device(hdsp->pci);
5137 return 0;
5138}
5139
Takashi Iwai55e957d2005-11-17 14:52:13 +01005140static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005142 struct hdsp *hdsp = (struct hdsp *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 if (hdsp)
5145 snd_hdsp_free(hdsp);
5146}
5147
5148static int __devinit snd_hdsp_probe(struct pci_dev *pci,
5149 const struct pci_device_id *pci_id)
5150{
5151 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005152 struct hdsp *hdsp;
5153 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 int err;
5155
5156 if (dev >= SNDRV_CARDS)
5157 return -ENODEV;
5158 if (!enable[dev]) {
5159 dev++;
5160 return -ENOENT;
5161 }
5162
Takashi Iwai55e957d2005-11-17 14:52:13 +01005163 if (!(card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 return -ENOMEM;
5165
Takashi Iwai55e957d2005-11-17 14:52:13 +01005166 hdsp = (struct hdsp *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 card->private_free = snd_hdsp_card_free;
5168 hdsp->dev = dev;
5169 hdsp->pci = pci;
5170 snd_card_set_dev(card, &pci->dev);
5171
5172 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5173 snd_card_free(card);
5174 return err;
5175 }
5176
5177 strcpy(card->shortname, "Hammerfall DSP");
5178 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
5179 hdsp->port, hdsp->irq);
5180
5181 if ((err = snd_card_register(card)) < 0) {
5182 snd_card_free(card);
5183 return err;
5184 }
5185 pci_set_drvdata(pci, card);
5186 dev++;
5187 return 0;
5188}
5189
5190static void __devexit snd_hdsp_remove(struct pci_dev *pci)
5191{
5192 snd_card_free(pci_get_drvdata(pci));
5193 pci_set_drvdata(pci, NULL);
5194}
5195
5196static struct pci_driver driver = {
5197 .name = "RME Hammerfall DSP",
5198 .id_table = snd_hdsp_ids,
5199 .probe = snd_hdsp_probe,
5200 .remove = __devexit_p(snd_hdsp_remove),
5201};
5202
5203static int __init alsa_card_hdsp_init(void)
5204{
Takashi Iwai01d25d42005-04-11 16:58:24 +02005205 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206}
5207
5208static void __exit alsa_card_hdsp_exit(void)
5209{
5210 pci_unregister_driver(&driver);
5211}
5212
5213module_init(alsa_card_hdsp_init)
5214module_exit(alsa_card_hdsp_exit)