blob: 6540037a11e4d9c2e693acc801baf39f6ef1758c [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}}");
63
64#define HDSP_MAX_CHANNELS 26
65#define HDSP_MAX_DS_CHANNELS 14
66#define HDSP_MAX_QS_CHANNELS 8
67#define DIGIFACE_SS_CHANNELS 26
68#define DIGIFACE_DS_CHANNELS 14
69#define MULTIFACE_SS_CHANNELS 18
70#define MULTIFACE_DS_CHANNELS 14
71#define H9652_SS_CHANNELS 26
72#define H9652_DS_CHANNELS 14
73/* This does not include possible Analog Extension Boards
74 AEBs are detected at card initialization
75*/
76#define H9632_SS_CHANNELS 12
77#define H9632_DS_CHANNELS 8
78#define H9632_QS_CHANNELS 4
79
80/* Write registers. These are defined as byte-offsets from the iobase value.
81 */
82#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020083#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define HDSP_outputBufferAddress 32
85#define HDSP_inputBufferAddress 36
86#define HDSP_controlRegister 64
87#define HDSP_interruptConfirmation 96
88#define HDSP_outputEnable 128
89#define HDSP_control2Reg 256
90#define HDSP_midiDataOut0 352
91#define HDSP_midiDataOut1 356
92#define HDSP_fifoData 368
93#define HDSP_inputEnable 384
94
95/* Read registers. These are defined as byte-offsets from the iobase value
96 */
97
98#define HDSP_statusRegister 0
99#define HDSP_timecode 128
100#define HDSP_status2Register 192
101#define HDSP_midiDataOut0 352
102#define HDSP_midiDataOut1 356
103#define HDSP_midiDataIn0 360
104#define HDSP_midiDataIn1 364
105#define HDSP_midiStatusOut0 384
106#define HDSP_midiStatusOut1 388
107#define HDSP_midiStatusIn0 392
108#define HDSP_midiStatusIn1 396
109#define HDSP_fifoStatus 400
110
111/* the meters are regular i/o-mapped registers, but offset
112 considerably from the rest. the peak registers are reset
113 when read; the least-significant 4 bits are full-scale counters;
114 the actual peak value is in the most-significant 24 bits.
115*/
116
117#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
118#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
119#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
120#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
121#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
122
123
124/* This is for H9652 cards
125 Peak values are read downward from the base
126 Rms values are read upward
127 There are rms values for the outputs too
128 26*3 values are read in ss mode
129 14*3 in ds mode, with no gap between values
130*/
131#define HDSP_9652_peakBase 7164
132#define HDSP_9652_rmsBase 4096
133
134/* c.f. the hdsp_9632_meters_t struct */
135#define HDSP_9632_metersBase 4096
136
137#define HDSP_IO_EXTENT 7168
138
139/* control2 register bits */
140
141#define HDSP_TMS 0x01
142#define HDSP_TCK 0x02
143#define HDSP_TDI 0x04
144#define HDSP_JTAG 0x08
145#define HDSP_PWDN 0x10
146#define HDSP_PROGRAM 0x020
147#define HDSP_CONFIG_MODE_0 0x040
148#define HDSP_CONFIG_MODE_1 0x080
149#define HDSP_VERSION_BIT 0x100
150#define HDSP_BIGENDIAN_MODE 0x200
151#define HDSP_RD_MULTIPLE 0x400
152#define HDSP_9652_ENABLE_MIXER 0x800
153#define HDSP_TDO 0x10000000
154
155#define HDSP_S_PROGRAM (HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
156#define HDSP_S_LOAD (HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
157
158/* Control Register bits */
159
160#define HDSP_Start (1<<0) /* start engine */
161#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
162#define HDSP_Latency1 (1<<2) /* [ see above ] */
163#define HDSP_Latency2 (1<<3) /* [ see above ] */
164#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
165#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
166#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
167#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
168#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
169#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
170#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
171#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
172#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
173#define HDSP_SyncRef2 (1<<13)
174#define HDSP_SPDIFInputSelect0 (1<<14)
175#define HDSP_SPDIFInputSelect1 (1<<15)
176#define HDSP_SyncRef0 (1<<16)
177#define HDSP_SyncRef1 (1<<17)
178#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
179#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
180#define HDSP_Midi0InterruptEnable (1<<22)
181#define HDSP_Midi1InterruptEnable (1<<23)
182#define HDSP_LineOut (1<<24)
183#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
184#define HDSP_ADGain1 (1<<26)
185#define HDSP_DAGain0 (1<<27)
186#define HDSP_DAGain1 (1<<28)
187#define HDSP_PhoneGain0 (1<<29)
188#define HDSP_PhoneGain1 (1<<30)
189#define HDSP_QuadSpeed (1<<31)
190
191#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
192#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
193#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
194#define HDSP_ADGainLowGain 0
195
196#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
197#define HDSP_DAGainHighGain HDSP_DAGainMask
198#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
199#define HDSP_DAGainMinus10dBV 0
200
201#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
202#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
203#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
204#define HDSP_PhoneGainMinus12dB 0
205
206#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
207#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
208
209#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
210#define HDSP_SPDIFInputADAT1 0
211#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
212#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
213#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
214
215#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
216#define HDSP_SyncRef_ADAT1 0
217#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
218#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
219#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
220#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
221#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
222
223/* Sample Clock Sources */
224
225#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
226#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
227#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
228#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
229#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
230#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
231#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
232#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
233#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
234#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
235
236/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
237
238#define HDSP_SYNC_FROM_WORD 0
239#define HDSP_SYNC_FROM_SPDIF 1
240#define HDSP_SYNC_FROM_ADAT1 2
241#define HDSP_SYNC_FROM_ADAT_SYNC 3
242#define HDSP_SYNC_FROM_ADAT2 4
243#define HDSP_SYNC_FROM_ADAT3 5
244
245/* SyncCheck status */
246
247#define HDSP_SYNC_CHECK_NO_LOCK 0
248#define HDSP_SYNC_CHECK_LOCK 1
249#define HDSP_SYNC_CHECK_SYNC 2
250
251/* AutoSync references - used by "autosync_ref" control switch */
252
253#define HDSP_AUTOSYNC_FROM_WORD 0
254#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
255#define HDSP_AUTOSYNC_FROM_SPDIF 2
256#define HDSP_AUTOSYNC_FROM_NONE 3
257#define HDSP_AUTOSYNC_FROM_ADAT1 4
258#define HDSP_AUTOSYNC_FROM_ADAT2 5
259#define HDSP_AUTOSYNC_FROM_ADAT3 6
260
261/* Possible sources of S/PDIF input */
262
263#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
264#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
265#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
266#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
267
268#define HDSP_Frequency32KHz HDSP_Frequency0
269#define HDSP_Frequency44_1KHz HDSP_Frequency1
270#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
271#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
272#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
273#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
274/* For H9632 cards */
275#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
276#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
277#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
278
279#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
280#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
281
282#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
283#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
284
285/* Status Register bits */
286
287#define HDSP_audioIRQPending (1<<0)
288#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
289#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
290#define HDSP_Lock1 (1<<2)
291#define HDSP_Lock0 (1<<3)
292#define HDSP_SPDIFSync (1<<4)
293#define HDSP_TimecodeLock (1<<5)
294#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
295#define HDSP_Sync2 (1<<16)
296#define HDSP_Sync1 (1<<17)
297#define HDSP_Sync0 (1<<18)
298#define HDSP_DoubleSpeedStatus (1<<19)
299#define HDSP_ConfigError (1<<20)
300#define HDSP_DllError (1<<21)
301#define HDSP_spdifFrequency0 (1<<22)
302#define HDSP_spdifFrequency1 (1<<23)
303#define HDSP_spdifFrequency2 (1<<24)
304#define HDSP_SPDIFErrorFlag (1<<25)
305#define HDSP_BufferID (1<<26)
306#define HDSP_TimecodeSync (1<<27)
307#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
308#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
309#define HDSP_midi0IRQPending (1<<30)
310#define HDSP_midi1IRQPending (1<<31)
311
312#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
313
314#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
315#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
316#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
317
318#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
319#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
320#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
321
322/* This is for H9632 cards */
323#define HDSP_spdifFrequency128KHz HDSP_spdifFrequencyMask
324#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
325#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
326
327/* Status2 Register bits */
328
329#define HDSP_version0 (1<<0)
330#define HDSP_version1 (1<<1)
331#define HDSP_version2 (1<<2)
332#define HDSP_wc_lock (1<<3)
333#define HDSP_wc_sync (1<<4)
334#define HDSP_inp_freq0 (1<<5)
335#define HDSP_inp_freq1 (1<<6)
336#define HDSP_inp_freq2 (1<<7)
337#define HDSP_SelSyncRef0 (1<<8)
338#define HDSP_SelSyncRef1 (1<<9)
339#define HDSP_SelSyncRef2 (1<<10)
340
341#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
342
343#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
344#define HDSP_systemFrequency32 (HDSP_inp_freq0)
345#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
346#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
347#define HDSP_systemFrequency64 (HDSP_inp_freq2)
348#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
349#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
350/* FIXME : more values for 9632 cards ? */
351
352#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
353#define HDSP_SelSyncRef_ADAT1 0
354#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
355#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
356#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
357#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
358#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
359
360/* Card state flags */
361
362#define HDSP_InitializationComplete (1<<0)
363#define HDSP_FirmwareLoaded (1<<1)
364#define HDSP_FirmwareCached (1<<2)
365
366/* FIFO wait times, defined in terms of 1/10ths of msecs */
367
368#define HDSP_LONG_WAIT 5000
369#define HDSP_SHORT_WAIT 30
370
371#define UNITY_GAIN 32768
372#define MINUS_INFINITY_GAIN 0
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/* the size of a substream (1 mono data stream) */
375
376#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
377#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
378
379/* the size of the area we need to allocate for DMA transfers. the
380 size is the same regardless of the number of channels - the
381 Multiface still uses the same memory area.
382
383 Note that we allocate 1 more channel than is apparently needed
384 because the h/w seems to write 1 byte beyond the end of the last
385 page. Sigh.
386*/
387
388#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
389#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
390
391/* use hotplug firmeare loader? */
392#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
Takashi Iwai140432f2006-05-22 14:31:57 +0200393#if !defined(HDSP_USE_HWDEP_LOADER) && !defined(CONFIG_SND_HDSP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#define HDSP_FW_LOADER
395#endif
396#endif
397
Takashi Iwai55e957d2005-11-17 14:52:13 +0100398struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 u32 input_peak[16];
400 u32 playback_peak[16];
401 u32 output_peak[16];
402 u32 xxx_peak[16];
403 u32 padding[64];
404 u32 input_rms_low[16];
405 u32 playback_rms_low[16];
406 u32 output_rms_low[16];
407 u32 xxx_rms_low[16];
408 u32 input_rms_high[16];
409 u32 playback_rms_high[16];
410 u32 output_rms_high[16];
411 u32 xxx_rms_high[16];
412};
413
Takashi Iwai55e957d2005-11-17 14:52:13 +0100414struct hdsp_midi {
415 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100417 struct snd_rawmidi *rmidi;
418 struct snd_rawmidi_substream *input;
419 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 char istimer; /* timer in use */
421 struct timer_list timer;
422 spinlock_t lock;
423 int pending;
424};
425
Takashi Iwai55e957d2005-11-17 14:52:13 +0100426struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100428 struct snd_pcm_substream *capture_substream;
429 struct snd_pcm_substream *playback_substream;
430 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct tasklet_struct midi_tasklet;
432 int use_midi_tasklet;
433 int precise_ptr;
434 u32 control_register; /* cached value */
435 u32 control2_register; /* cached value */
436 u32 creg_spdif;
437 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200438 int clock_source_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 char *card_name; /* digiface/multiface */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100440 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned short firmware_rev;
442 unsigned short state; /* stores state bits */
443 u32 firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
444 size_t period_bytes; /* guess what this is */
445 unsigned char max_channels;
446 unsigned char qs_in_channels; /* quad speed mode for H9632 */
447 unsigned char ds_in_channels;
448 unsigned char ss_in_channels; /* different for multiface/digiface */
449 unsigned char qs_out_channels;
450 unsigned char ds_out_channels;
451 unsigned char ss_out_channels;
452
453 struct snd_dma_buffer capture_dma_buf;
454 struct snd_dma_buffer playback_dma_buf;
455 unsigned char *capture_buffer; /* suitably aligned address */
456 unsigned char *playback_buffer; /* suitably aligned address */
457
458 pid_t capture_pid;
459 pid_t playback_pid;
460 int running;
461 int system_sample_rate;
462 char *channel_map;
463 int dev;
464 int irq;
465 unsigned long port;
466 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100467 struct snd_card *card;
468 struct snd_pcm *pcm;
469 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100471 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200473 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
476/* These tables map the ALSA channels 1..N to the channels that we
477 need to use in order to find the relevant channel buffer. RME
478 refer to this kind of mapping as between "the ADAT channel and
479 the DMA channel." We index it using the logical audio channel,
480 and the value is the DMA channel (i.e. channel buffer number)
481 where the data for that channel can be read/written from/to.
482*/
483
484static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
485 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
486 18, 19, 20, 21, 22, 23, 24, 25
487};
488
489static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
490 /* Analog */
491 0, 1, 2, 3, 4, 5, 6, 7,
492 /* ADAT 2 */
493 16, 17, 18, 19, 20, 21, 22, 23,
494 /* SPDIF */
495 24, 25,
496 -1, -1, -1, -1, -1, -1, -1, -1
497};
498
499static char channel_map_ds[HDSP_MAX_CHANNELS] = {
500 /* ADAT channels are remapped */
501 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
502 /* channels 12 and 13 are S/PDIF */
503 24, 25,
504 /* others don't exist */
505 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
506};
507
508static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
509 /* ADAT channels */
510 0, 1, 2, 3, 4, 5, 6, 7,
511 /* SPDIF */
512 8, 9,
513 /* Analog */
514 10, 11,
515 /* AO4S-192 and AI4S-192 extension boards */
516 12, 13, 14, 15,
517 /* others don't exist */
518 -1, -1, -1, -1, -1, -1, -1, -1,
519 -1, -1
520};
521
522static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
523 /* ADAT */
524 1, 3, 5, 7,
525 /* SPDIF */
526 8, 9,
527 /* Analog */
528 10, 11,
529 /* AO4S-192 and AI4S-192 extension boards */
530 12, 13, 14, 15,
531 /* others don't exist */
532 -1, -1, -1, -1, -1, -1, -1, -1,
533 -1, -1, -1, -1, -1, -1
534};
535
536static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
537 /* ADAT is disabled in this mode */
538 /* SPDIF */
539 8, 9,
540 /* Analog */
541 10, 11,
542 /* AO4S-192 and AI4S-192 extension boards */
543 12, 13, 14, 15,
544 /* others don't exist */
545 -1, -1, -1, -1, -1, -1, -1, -1,
546 -1, -1, -1, -1, -1, -1, -1, -1,
547 -1, -1
548};
549
550static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
551{
552 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
553 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200554 if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
555 if (dmab->bytes >= size)
556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Takashi Iwaib6a96912005-05-30 18:27:03 +0200558 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
559 size, dmab) < 0)
560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
562}
563
564static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
565{
Takashi Iwaib6a96912005-05-30 18:27:03 +0200566 if (dmab->area) {
567 dmab->dev.dev = NULL; /* make it anonymous */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
Takashi Iwaib6a96912005-05-30 18:27:03 +0200569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572
Takashi Iwaif40b6892006-07-05 16:51:05 +0200573static struct pci_device_id snd_hdsp_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 {
575 .vendor = PCI_VENDOR_ID_XILINX,
576 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
577 .subvendor = PCI_ANY_ID,
578 .subdevice = PCI_ANY_ID,
579 }, /* RME Hammerfall-DSP */
580 { 0, },
581};
582
583MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
584
585/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100586static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
587static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
588static int snd_hdsp_enable_io (struct hdsp *hdsp);
589static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
590static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
591static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
592static int hdsp_autosync_ref(struct hdsp *hdsp);
593static int snd_hdsp_set_defaults(struct hdsp *hdsp);
594static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Takashi Iwai55e957d2005-11-17 14:52:13 +0100596static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 switch (hdsp->firmware_rev) {
599 case 0xa:
600 return (64 * out) + (32 + (in));
601 case 0x96:
602 case 0x97:
Remy Bruno6add0f42006-10-09 15:52:01 +0200603 case 0x98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return (32 * out) + (16 + (in));
605 default:
606 return (52 * out) + (26 + (in));
607 }
608}
609
Takashi Iwai55e957d2005-11-17 14:52:13 +0100610static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 switch (hdsp->firmware_rev) {
613 case 0xa:
614 return (64 * out) + in;
615 case 0x96:
616 case 0x97:
Remy Bruno6add0f42006-10-09 15:52:01 +0200617 case 0x98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return (32 * out) + in;
619 default:
620 return (52 * out) + in;
621 }
622}
623
Takashi Iwai55e957d2005-11-17 14:52:13 +0100624static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 writel(val, hdsp->iobase + reg);
627}
628
Takashi Iwai55e957d2005-11-17 14:52:13 +0100629static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 return readl (hdsp->iobase + reg);
632}
633
Takashi Iwai55e957d2005-11-17 14:52:13 +0100634static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636
637 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
638 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
639 snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
640 hdsp->state &= ~HDSP_FirmwareLoaded;
641 return -EIO;
642 }
643 return 0;
644
645}
646
Takashi Iwai55e957d2005-11-17 14:52:13 +0100647static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 int i;
650 unsigned long flags;
651
652 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
653
654 snd_printk ("Hammerfall-DSP: loading firmware\n");
655
656 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
657 hdsp_write (hdsp, HDSP_fifoData, 0);
658
659 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
660 snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
661 return -EIO;
662 }
663
664 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
665
666 for (i = 0; i < 24413; ++i) {
667 hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
668 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
669 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
670 return -EIO;
671 }
672 }
673
Takashi Iwaib0b98112005-10-20 18:29:58 +0200674 ssleep(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
677 snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
678 return -EIO;
679 }
680
681#ifdef SNDRV_BIG_ENDIAN
682 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
683#else
684 hdsp->control2_register = 0;
685#endif
686 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
687 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
688
689 }
690 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaib0b98112005-10-20 18:29:58 +0200691 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 spin_lock_irqsave(&hdsp->lock, flags);
693 snd_hdsp_set_defaults(hdsp);
694 spin_unlock_irqrestore(&hdsp->lock, flags);
695 }
696
697 hdsp->state |= HDSP_FirmwareLoaded;
698
699 return 0;
700}
701
Takashi Iwai55e957d2005-11-17 14:52:13 +0100702static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
705
706 hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
707 hdsp_write (hdsp, HDSP_fifoData, 0);
Takashi Iwaib0b98112005-10-20 18:29:58 +0200708 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
712 hdsp_write (hdsp, HDSP_fifoData, 0);
713
714 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
715 hdsp->io_type = Multiface;
716 hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
717 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
718 hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
719 } else {
720 hdsp->io_type = Digiface;
721 }
722 } else {
723 /* firmware was already loaded, get iobox type */
Takashi Iwaib0b98112005-10-20 18:29:58 +0200724 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200726 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 return 0;
730}
731
732
Takashi Iwai311e70a2006-09-06 12:13:37 +0200733#ifdef HDSP_FW_LOADER
734static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp);
735#endif
736
737static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200739 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200743 if (! load_on_demand)
Takashi Iwaib0b98112005-10-20 18:29:58 +0200744 return -EIO;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200745 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +0200746 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200747 if (! (hdsp->state & HDSP_FirmwareCached)) {
748#ifdef HDSP_FW_LOADER
749 if (! hdsp_request_fw_loader(hdsp))
750 return 0;
751#endif
752 snd_printk(KERN_ERR
753 "Hammerfall-DSP: No firmware loaded nor "
754 "cached, please upload firmware.\n");
755 return -EIO;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200756 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200757 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
758 snd_printk(KERN_ERR
759 "Hammerfall-DSP: Firmware loading from "
760 "cache failed, please upload manually.\n");
761 return -EIO;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 return 0;
765}
766
767
Takashi Iwai55e957d2005-11-17 14:52:13 +0100768static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 int i;
771
772 /* the fifoStatus registers reports on how many words
773 are available in the command FIFO.
774 */
775
776 for (i = 0; i < timeout; i++) {
777
778 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
779 return 0;
780
781 /* not very friendly, but we only do this during a firmware
782 load and changing the mixer, so we just put up with it.
783 */
784
785 udelay (100);
786 }
787
788 snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
789 count, timeout);
790 return -1;
791}
792
Takashi Iwai55e957d2005-11-17 14:52:13 +0100793static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Takashi Iwaib0b98112005-10-20 18:29:58 +0200795 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return hdsp->mixer_matrix[addr];
799}
800
Takashi Iwai55e957d2005-11-17 14:52:13 +0100801static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 unsigned int ad;
804
805 if (addr >= HDSP_MATRIX_MIXER_SIZE)
806 return -1;
807
808 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
809
810 /* from martin bjornsen:
811
812 "You can only write dwords to the
813 mixer memory which contain two
814 mixer values in the low and high
815 word. So if you want to change
816 value 0 you have to read value 1
817 from the cache and write both to
818 the first dword in the mixer
819 memory."
820 */
821
Takashi Iwaib0b98112005-10-20 18:29:58 +0200822 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Takashi Iwaib0b98112005-10-20 18:29:58 +0200825 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 hdsp->mixer_matrix[addr] = data;
829
830
831 /* `addr' addresses a 16-bit wide address, but
832 the address space accessed via hdsp_write
833 uses byte offsets. put another way, addr
834 varies from 0 to 1351, but to access the
835 corresponding memory location, we need
836 to access 0 to 2703 ...
837 */
838 ad = addr/2;
839
840 hdsp_write (hdsp, 4096 + (ad*4),
841 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
842 hdsp->mixer_matrix[addr&0x7fe]);
843
844 return 0;
845
846 } else {
847
848 ad = (addr << 16) + data;
849
Takashi Iwaib0b98112005-10-20 18:29:58 +0200850 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 hdsp_write (hdsp, HDSP_fifoData, ad);
854 hdsp->mixer_matrix[addr] = data;
855
856 }
857
858 return 0;
859}
860
Takashi Iwai55e957d2005-11-17 14:52:13 +0100861static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 unsigned long flags;
864 int ret = 1;
865
866 spin_lock_irqsave(&hdsp->lock, flags);
867 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b98112005-10-20 18:29:58 +0200868 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 spin_unlock_irqrestore(&hdsp->lock, flags);
871 return ret;
872}
873
Takashi Iwai55e957d2005-11-17 14:52:13 +0100874static int hdsp_external_sample_rate (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
877 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
878
879 switch (rate_bits) {
880 case HDSP_systemFrequency32: return 32000;
881 case HDSP_systemFrequency44_1: return 44100;
882 case HDSP_systemFrequency48: return 48000;
883 case HDSP_systemFrequency64: return 64000;
884 case HDSP_systemFrequency88_2: return 88200;
885 case HDSP_systemFrequency96: return 96000;
886 default:
887 return 0;
888 }
889}
890
Takashi Iwai55e957d2005-11-17 14:52:13 +0100891static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
894 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
895
Takashi Iwaib0b98112005-10-20 18:29:58 +0200896 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 switch (rate_bits) {
900 case HDSP_spdifFrequency32KHz: return 32000;
901 case HDSP_spdifFrequency44_1KHz: return 44100;
902 case HDSP_spdifFrequency48KHz: return 48000;
903 case HDSP_spdifFrequency64KHz: return 64000;
904 case HDSP_spdifFrequency88_2KHz: return 88200;
905 case HDSP_spdifFrequency96KHz: return 96000;
906 case HDSP_spdifFrequency128KHz:
907 if (hdsp->io_type == H9632) return 128000;
908 break;
909 case HDSP_spdifFrequency176_4KHz:
910 if (hdsp->io_type == H9632) return 176400;
911 break;
912 case HDSP_spdifFrequency192KHz:
913 if (hdsp->io_type == H9632) return 192000;
914 break;
915 default:
916 break;
917 }
918 snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
919 return 0;
920}
921
Takashi Iwai55e957d2005-11-17 14:52:13 +0100922static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
925}
926
Takashi Iwai55e957d2005-11-17 14:52:13 +0100927static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 int position;
930
931 position = hdsp_read(hdsp, HDSP_statusRegister);
932
Takashi Iwaib0b98112005-10-20 18:29:58 +0200933 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 position &= HDSP_BufferPositionMask;
937 position /= 4;
938 position &= (hdsp->period_bytes/2) - 1;
939 return position;
940}
941
Takashi Iwai55e957d2005-11-17 14:52:13 +0100942static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +0200945 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
946 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
947 * requires (?) to write again DDS value after a reset pointer
948 * (at least, it works like this) */
949 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Takashi Iwai55e957d2005-11-17 14:52:13 +0100952static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
955 hdsp_write(s, HDSP_controlRegister, s->control_register);
956}
957
Takashi Iwai55e957d2005-11-17 14:52:13 +0100958static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
961 hdsp_write(s, HDSP_controlRegister, s->control_register);
962}
963
Takashi Iwai55e957d2005-11-17 14:52:13 +0100964static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
967}
968
Takashi Iwai55e957d2005-11-17 14:52:13 +0100969static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 int n;
972
973 spin_lock_irq(&s->lock);
974
975 frames >>= 7;
976 n = 0;
977 while (frames) {
978 n++;
979 frames >>= 1;
980 }
981
982 s->control_register &= ~HDSP_LatencyMask;
983 s->control_register |= hdsp_encode_latency(n);
984
985 hdsp_write(s, HDSP_controlRegister, s->control_register);
986
987 hdsp_compute_period_size(s);
988
989 spin_unlock_irq(&s->lock);
990
991 return 0;
992}
993
Remy Brunod7923b22006-10-17 12:41:56 +0200994static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
995{
996 u64 n;
997 u32 r;
998
999 if (rate >= 112000)
1000 rate /= 4;
1001 else if (rate >= 56000)
1002 rate /= 2;
1003
1004 /* RME says n = 104857600000000, but in the windows MADI driver, I see:
1005// return 104857600000000 / rate; // 100 MHz
1006 return 110100480000000 / rate; // 105 MHz
1007 */
1008 n = 104857600000000ULL; /* = 2^20 * 10^8 */
1009 div64_32(&n, rate, &r);
1010 /* n should be less than 2^32 for being written to FREQ register */
1011 snd_assert((n >> 32) == 0);
1012 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1013 value to write it after a reset */
1014 hdsp->dds_value = n;
1015 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1016}
1017
Takashi Iwai55e957d2005-11-17 14:52:13 +01001018static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 int reject_if_open = 0;
1021 int current_rate;
1022 int rate_bits;
1023
1024 /* ASSUMPTION: hdsp->lock is either held, or
1025 there is no need for it (e.g. during module
1026 initialization).
1027 */
1028
1029 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
1030 if (called_internally) {
1031 /* request from ctl or card initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001032 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 -07001033 return -1;
1034 } else {
1035 /* hw_param request while in AutoSync mode */
1036 int external_freq = hdsp_external_sample_rate(hdsp);
1037 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
1038
Takashi Iwaib0b98112005-10-20 18:29:58 +02001039 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1040 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1041 else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1042 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
1043 else if (rate != external_freq) {
1044 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return -1;
1046 }
1047 }
1048 }
1049
1050 current_rate = hdsp->system_sample_rate;
1051
1052 /* Changing from a "single speed" to a "double speed" rate is
1053 not allowed if any substreams are open. This is because
1054 such a change causes a shift in the location of
1055 the DMA buffers and a reduction in the number of available
1056 buffers.
1057
1058 Note that a similar but essentially insoluble problem
1059 exists for externally-driven rate changes. All we can do
1060 is to flag rate changes in the read/write routines. */
1061
Takashi Iwaib0b98112005-10-20 18:29:58 +02001062 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 switch (rate) {
1066 case 32000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001067 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 rate_bits = HDSP_Frequency32KHz;
1070 break;
1071 case 44100:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001072 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 rate_bits = HDSP_Frequency44_1KHz;
1075 break;
1076 case 48000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001077 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 rate_bits = HDSP_Frequency48KHz;
1080 break;
1081 case 64000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001082 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 rate_bits = HDSP_Frequency64KHz;
1085 break;
1086 case 88200:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001087 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 rate_bits = HDSP_Frequency88_2KHz;
1090 break;
1091 case 96000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001092 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 rate_bits = HDSP_Frequency96KHz;
1095 break;
1096 case 128000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001097 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 rate_bits = HDSP_Frequency128KHz;
1100 break;
1101 case 176400:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001102 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 rate_bits = HDSP_Frequency176_4KHz;
1105 break;
1106 case 192000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001107 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 rate_bits = HDSP_Frequency192KHz;
1110 break;
1111 default:
1112 return -EINVAL;
1113 }
1114
1115 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1116 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1117 hdsp->capture_pid,
1118 hdsp->playback_pid);
1119 return -EBUSY;
1120 }
1121
1122 hdsp->control_register &= ~HDSP_FrequencyMask;
1123 hdsp->control_register |= rate_bits;
1124 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1125
Remy Brunod7923b22006-10-17 12:41:56 +02001126 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1127 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1128 hdsp_set_dds_value(hdsp, rate);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (rate >= 128000) {
1131 hdsp->channel_map = channel_map_H9632_qs;
1132 } else if (rate > 48000) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001133 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001135 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 } else {
1138 switch (hdsp->io_type) {
1139 case Multiface:
1140 hdsp->channel_map = channel_map_mf_ss;
1141 break;
1142 case Digiface:
1143 case H9652:
1144 hdsp->channel_map = channel_map_df_ss;
1145 break;
1146 case H9632:
1147 hdsp->channel_map = channel_map_H9632_ss;
1148 break;
1149 default:
1150 /* should never happen */
1151 break;
1152 }
1153 }
1154
1155 hdsp->system_sample_rate = rate;
1156
1157 return 0;
1158}
1159
1160/*----------------------------------------------------------------------------
1161 MIDI
1162 ----------------------------------------------------------------------------*/
1163
Takashi Iwai55e957d2005-11-17 14:52:13 +01001164static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001167 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001169 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Takashi Iwai55e957d2005-11-17 14:52:13 +01001173static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001176 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001178 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Takashi Iwai55e957d2005-11-17 14:52:13 +01001182static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001184 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001186 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Takashi Iwai55e957d2005-11-17 14:52:13 +01001190static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 int fifo_bytes_used;
1193
Takashi Iwaib0b98112005-10-20 18:29:58 +02001194 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001196 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Takashi Iwaib0b98112005-10-20 18:29:58 +02001199 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 128 - fifo_bytes_used;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001201 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Takashi Iwai55e957d2005-11-17 14:52:13 +01001205static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001207 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Takashi Iwai55e957d2005-11-17 14:52:13 +01001211static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 unsigned long flags;
1214 int n_pending;
1215 int to_write;
1216 int i;
1217 unsigned char buf[128];
1218
1219 /* Output is not interrupt driven */
1220
1221 spin_lock_irqsave (&hmidi->lock, flags);
1222 if (hmidi->output) {
1223 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1224 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1225 if (n_pending > (int)sizeof (buf))
1226 n_pending = sizeof (buf);
1227
1228 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
1229 for (i = 0; i < to_write; ++i)
1230 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1231 }
1232 }
1233 }
1234 }
1235 spin_unlock_irqrestore (&hmidi->lock, flags);
1236 return 0;
1237}
1238
Takashi Iwai55e957d2005-11-17 14:52:13 +01001239static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1242 unsigned long flags;
1243 int n_pending;
1244 int i;
1245
1246 spin_lock_irqsave (&hmidi->lock, flags);
1247 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1248 if (hmidi->input) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001249 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 n_pending = sizeof (buf);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001251 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001253 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 } else {
1256 /* flush the MIDI input FIFO */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001257 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 }
1261 hmidi->pending = 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001262 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001264 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1267 spin_unlock_irqrestore (&hmidi->lock, flags);
1268 return snd_hdsp_midi_output_write (hmidi);
1269}
1270
Takashi Iwai55e957d2005-11-17 14:52:13 +01001271static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001273 struct hdsp *hdsp;
1274 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 unsigned long flags;
1276 u32 ie;
1277
Takashi Iwai55e957d2005-11-17 14:52:13 +01001278 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 hdsp = hmidi->hdsp;
1280 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1281 spin_lock_irqsave (&hdsp->lock, flags);
1282 if (up) {
1283 if (!(hdsp->control_register & ie)) {
1284 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1285 hdsp->control_register |= ie;
1286 }
1287 } else {
1288 hdsp->control_register &= ~ie;
1289 tasklet_kill(&hdsp->midi_tasklet);
1290 }
1291
1292 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1293 spin_unlock_irqrestore (&hdsp->lock, flags);
1294}
1295
1296static void snd_hdsp_midi_output_timer(unsigned long data)
1297{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001298 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 unsigned long flags;
1300
1301 snd_hdsp_midi_output_write(hmidi);
1302 spin_lock_irqsave (&hmidi->lock, flags);
1303
1304 /* this does not bump hmidi->istimer, because the
1305 kernel automatically removed the timer when it
1306 expired, and we are now adding it back, thus
1307 leaving istimer wherever it was set before.
1308 */
1309
1310 if (hmidi->istimer) {
1311 hmidi->timer.expires = 1 + jiffies;
1312 add_timer(&hmidi->timer);
1313 }
1314
1315 spin_unlock_irqrestore (&hmidi->lock, flags);
1316}
1317
Takashi Iwai55e957d2005-11-17 14:52:13 +01001318static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001320 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 unsigned long flags;
1322
Takashi Iwai55e957d2005-11-17 14:52:13 +01001323 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 spin_lock_irqsave (&hmidi->lock, flags);
1325 if (up) {
1326 if (!hmidi->istimer) {
1327 init_timer(&hmidi->timer);
1328 hmidi->timer.function = snd_hdsp_midi_output_timer;
1329 hmidi->timer.data = (unsigned long) hmidi;
1330 hmidi->timer.expires = 1 + jiffies;
1331 add_timer(&hmidi->timer);
1332 hmidi->istimer++;
1333 }
1334 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001335 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 spin_unlock_irqrestore (&hmidi->lock, flags);
1339 if (up)
1340 snd_hdsp_midi_output_write(hmidi);
1341}
1342
Takashi Iwai55e957d2005-11-17 14:52:13 +01001343static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001345 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Takashi Iwai55e957d2005-11-17 14:52:13 +01001347 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_lock_irq (&hmidi->lock);
1349 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1350 hmidi->input = substream;
1351 spin_unlock_irq (&hmidi->lock);
1352
1353 return 0;
1354}
1355
Takashi Iwai55e957d2005-11-17 14:52:13 +01001356static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001358 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Takashi Iwai55e957d2005-11-17 14:52:13 +01001360 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_lock_irq (&hmidi->lock);
1362 hmidi->output = substream;
1363 spin_unlock_irq (&hmidi->lock);
1364
1365 return 0;
1366}
1367
Takashi Iwai55e957d2005-11-17 14:52:13 +01001368static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001370 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 snd_hdsp_midi_input_trigger (substream, 0);
1373
Takashi Iwai55e957d2005-11-17 14:52:13 +01001374 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 spin_lock_irq (&hmidi->lock);
1376 hmidi->input = NULL;
1377 spin_unlock_irq (&hmidi->lock);
1378
1379 return 0;
1380}
1381
Takashi Iwai55e957d2005-11-17 14:52:13 +01001382static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001384 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 snd_hdsp_midi_output_trigger (substream, 0);
1387
Takashi Iwai55e957d2005-11-17 14:52:13 +01001388 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_lock_irq (&hmidi->lock);
1390 hmidi->output = NULL;
1391 spin_unlock_irq (&hmidi->lock);
1392
1393 return 0;
1394}
1395
Takashi Iwai55e957d2005-11-17 14:52:13 +01001396static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 .open = snd_hdsp_midi_output_open,
1399 .close = snd_hdsp_midi_output_close,
1400 .trigger = snd_hdsp_midi_output_trigger,
1401};
1402
Takashi Iwai55e957d2005-11-17 14:52:13 +01001403static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 .open = snd_hdsp_midi_input_open,
1406 .close = snd_hdsp_midi_input_close,
1407 .trigger = snd_hdsp_midi_input_trigger,
1408};
1409
Takashi Iwaif40b6892006-07-05 16:51:05 +02001410static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 char buf[32];
1413
1414 hdsp->midi[id].id = id;
1415 hdsp->midi[id].rmidi = NULL;
1416 hdsp->midi[id].input = NULL;
1417 hdsp->midi[id].output = NULL;
1418 hdsp->midi[id].hdsp = hdsp;
1419 hdsp->midi[id].istimer = 0;
1420 hdsp->midi[id].pending = 0;
1421 spin_lock_init (&hdsp->midi[id].lock);
1422
1423 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001424 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 sprintf (hdsp->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
1428 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1429
1430 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1431 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1432
1433 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1434 SNDRV_RAWMIDI_INFO_INPUT |
1435 SNDRV_RAWMIDI_INFO_DUPLEX;
1436
1437 return 0;
1438}
1439
1440/*-----------------------------------------------------------------------------
1441 Control Interface
1442 ----------------------------------------------------------------------------*/
1443
Takashi Iwai55e957d2005-11-17 14:52:13 +01001444static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 u32 val = 0;
1447 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1448 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1449 if (val & HDSP_SPDIFProfessional)
1450 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1451 else
1452 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1453 return val;
1454}
1455
Takashi Iwai55e957d2005-11-17 14:52:13 +01001456static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1459 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1460 if (val & HDSP_SPDIFProfessional)
1461 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1462 else
1463 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1464}
1465
Takashi Iwai55e957d2005-11-17 14:52:13 +01001466static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1469 uinfo->count = 1;
1470 return 0;
1471}
1472
Takashi Iwai55e957d2005-11-17 14:52:13 +01001473static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001475 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1478 return 0;
1479}
1480
Takashi Iwai55e957d2005-11-17 14:52:13 +01001481static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001483 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int change;
1485 u32 val;
1486
1487 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1488 spin_lock_irq(&hdsp->lock);
1489 change = val != hdsp->creg_spdif;
1490 hdsp->creg_spdif = val;
1491 spin_unlock_irq(&hdsp->lock);
1492 return change;
1493}
1494
Takashi Iwai55e957d2005-11-17 14:52:13 +01001495static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1498 uinfo->count = 1;
1499 return 0;
1500}
1501
Takashi Iwai55e957d2005-11-17 14:52:13 +01001502static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001504 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1507 return 0;
1508}
1509
Takashi Iwai55e957d2005-11-17 14:52:13 +01001510static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001512 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 int change;
1514 u32 val;
1515
1516 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1517 spin_lock_irq(&hdsp->lock);
1518 change = val != hdsp->creg_spdif_stream;
1519 hdsp->creg_spdif_stream = val;
1520 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1521 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1522 spin_unlock_irq(&hdsp->lock);
1523 return change;
1524}
1525
Takashi Iwai55e957d2005-11-17 14:52:13 +01001526static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1529 uinfo->count = 1;
1530 return 0;
1531}
1532
Takashi Iwai55e957d2005-11-17 14:52:13 +01001533static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1536 return 0;
1537}
1538
1539#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001540{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 .name = xname, \
1542 .index = xindex, \
1543 .info = snd_hdsp_info_spdif_in, \
1544 .get = snd_hdsp_get_spdif_in, \
1545 .put = snd_hdsp_put_spdif_in }
1546
Takashi Iwai55e957d2005-11-17 14:52:13 +01001547static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1550}
1551
Takashi Iwai55e957d2005-11-17 14:52:13 +01001552static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1555 hdsp->control_register |= hdsp_encode_spdif_in(in);
1556 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1557 return 0;
1558}
1559
Takashi Iwai55e957d2005-11-17 14:52:13 +01001560static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001563 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1566 uinfo->count = 1;
1567 uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1568 if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1569 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1570 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1571 return 0;
1572}
1573
Takashi Iwai55e957d2005-11-17 14:52:13 +01001574static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001576 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1579 return 0;
1580}
1581
Takashi Iwai55e957d2005-11-17 14:52:13 +01001582static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001584 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 int change;
1586 unsigned int val;
1587
1588 if (!snd_hdsp_use_is_exclusive(hdsp))
1589 return -EBUSY;
1590 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1591 spin_lock_irq(&hdsp->lock);
1592 change = val != hdsp_spdif_in(hdsp);
1593 if (change)
1594 hdsp_set_spdif_input(hdsp, val);
1595 spin_unlock_irq(&hdsp->lock);
1596 return change;
1597}
1598
1599#define HDSP_SPDIF_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001600{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 .info = snd_hdsp_info_spdif_bits, \
1602 .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
1603
Takashi Iwai55e957d2005-11-17 14:52:13 +01001604static int hdsp_spdif_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
1607}
1608
Takashi Iwai55e957d2005-11-17 14:52:13 +01001609static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001611 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 hdsp->control_register |= HDSP_SPDIFOpticalOut;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001613 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1616 return 0;
1617}
1618
Takashi Iwai55e957d2005-11-17 14:52:13 +01001619static int snd_hdsp_info_spdif_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1622 uinfo->count = 1;
1623 uinfo->value.integer.min = 0;
1624 uinfo->value.integer.max = 1;
1625 return 0;
1626}
1627
Takashi Iwai55e957d2005-11-17 14:52:13 +01001628static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001630 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
1633 return 0;
1634}
1635
Takashi Iwai55e957d2005-11-17 14:52:13 +01001636static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001638 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int change;
1640 unsigned int val;
1641
1642 if (!snd_hdsp_use_is_exclusive(hdsp))
1643 return -EBUSY;
1644 val = ucontrol->value.integer.value[0] & 1;
1645 spin_lock_irq(&hdsp->lock);
1646 change = (int)val != hdsp_spdif_out(hdsp);
1647 hdsp_set_spdif_output(hdsp, val);
1648 spin_unlock_irq(&hdsp->lock);
1649 return change;
1650}
1651
1652#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001653{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .info = snd_hdsp_info_spdif_bits, \
1655 .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
1656
Takashi Iwai55e957d2005-11-17 14:52:13 +01001657static int hdsp_spdif_professional(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
1660}
1661
Takashi Iwai55e957d2005-11-17 14:52:13 +01001662static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001664 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 hdsp->control_register |= HDSP_SPDIFProfessional;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001666 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 hdsp->control_register &= ~HDSP_SPDIFProfessional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1669 return 0;
1670}
1671
Takashi Iwai55e957d2005-11-17 14:52:13 +01001672static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001674 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
1677 return 0;
1678}
1679
Takashi Iwai55e957d2005-11-17 14:52:13 +01001680static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001682 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int change;
1684 unsigned int val;
1685
1686 if (!snd_hdsp_use_is_exclusive(hdsp))
1687 return -EBUSY;
1688 val = ucontrol->value.integer.value[0] & 1;
1689 spin_lock_irq(&hdsp->lock);
1690 change = (int)val != hdsp_spdif_professional(hdsp);
1691 hdsp_set_spdif_professional(hdsp, val);
1692 spin_unlock_irq(&hdsp->lock);
1693 return change;
1694}
1695
1696#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001697{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 .info = snd_hdsp_info_spdif_bits, \
1699 .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
1700
Takashi Iwai55e957d2005-11-17 14:52:13 +01001701static int hdsp_spdif_emphasis(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
1704}
1705
Takashi Iwai55e957d2005-11-17 14:52:13 +01001706static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001708 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 hdsp->control_register |= HDSP_SPDIFEmphasis;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001710 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 hdsp->control_register &= ~HDSP_SPDIFEmphasis;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1713 return 0;
1714}
1715
Takashi Iwai55e957d2005-11-17 14:52:13 +01001716static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001718 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
1721 return 0;
1722}
1723
Takashi Iwai55e957d2005-11-17 14:52:13 +01001724static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001726 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int change;
1728 unsigned int val;
1729
1730 if (!snd_hdsp_use_is_exclusive(hdsp))
1731 return -EBUSY;
1732 val = ucontrol->value.integer.value[0] & 1;
1733 spin_lock_irq(&hdsp->lock);
1734 change = (int)val != hdsp_spdif_emphasis(hdsp);
1735 hdsp_set_spdif_emphasis(hdsp, val);
1736 spin_unlock_irq(&hdsp->lock);
1737 return change;
1738}
1739
1740#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001741{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 .info = snd_hdsp_info_spdif_bits, \
1743 .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
1744
Takashi Iwai55e957d2005-11-17 14:52:13 +01001745static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
1748}
1749
Takashi Iwai55e957d2005-11-17 14:52:13 +01001750static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001752 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 hdsp->control_register |= HDSP_SPDIFNonAudio;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001754 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 hdsp->control_register &= ~HDSP_SPDIFNonAudio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1757 return 0;
1758}
1759
Takashi Iwai55e957d2005-11-17 14:52:13 +01001760static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001762 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
1765 return 0;
1766}
1767
Takashi Iwai55e957d2005-11-17 14:52:13 +01001768static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001770 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 int change;
1772 unsigned int val;
1773
1774 if (!snd_hdsp_use_is_exclusive(hdsp))
1775 return -EBUSY;
1776 val = ucontrol->value.integer.value[0] & 1;
1777 spin_lock_irq(&hdsp->lock);
1778 change = (int)val != hdsp_spdif_nonaudio(hdsp);
1779 hdsp_set_spdif_nonaudio(hdsp, val);
1780 spin_unlock_irq(&hdsp->lock);
1781 return change;
1782}
1783
1784#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001785{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 .name = xname, \
1787 .index = xindex, \
1788 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1789 .info = snd_hdsp_info_spdif_sample_rate, \
1790 .get = snd_hdsp_get_spdif_sample_rate \
1791}
1792
Takashi Iwai55e957d2005-11-17 14:52:13 +01001793static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001796 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1799 uinfo->count = 1;
1800 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1801 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1802 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1803 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1804 return 0;
1805}
1806
Takashi Iwai55e957d2005-11-17 14:52:13 +01001807static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001809 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 switch (hdsp_spdif_sample_rate(hdsp)) {
1812 case 32000:
1813 ucontrol->value.enumerated.item[0] = 0;
1814 break;
1815 case 44100:
1816 ucontrol->value.enumerated.item[0] = 1;
1817 break;
1818 case 48000:
1819 ucontrol->value.enumerated.item[0] = 2;
1820 break;
1821 case 64000:
1822 ucontrol->value.enumerated.item[0] = 3;
1823 break;
1824 case 88200:
1825 ucontrol->value.enumerated.item[0] = 4;
1826 break;
1827 case 96000:
1828 ucontrol->value.enumerated.item[0] = 5;
1829 break;
1830 case 128000:
1831 ucontrol->value.enumerated.item[0] = 7;
1832 break;
1833 case 176400:
1834 ucontrol->value.enumerated.item[0] = 8;
1835 break;
1836 case 192000:
1837 ucontrol->value.enumerated.item[0] = 9;
1838 break;
1839 default:
1840 ucontrol->value.enumerated.item[0] = 6;
1841 }
1842 return 0;
1843}
1844
1845#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001846{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 .name = xname, \
1848 .index = xindex, \
1849 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1850 .info = snd_hdsp_info_system_sample_rate, \
1851 .get = snd_hdsp_get_system_sample_rate \
1852}
1853
Takashi Iwai55e957d2005-11-17 14:52:13 +01001854static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1857 uinfo->count = 1;
1858 return 0;
1859}
1860
Takashi Iwai55e957d2005-11-17 14:52:13 +01001861static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001863 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1866 return 0;
1867}
1868
1869#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001870{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .name = xname, \
1872 .index = xindex, \
1873 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1874 .info = snd_hdsp_info_autosync_sample_rate, \
1875 .get = snd_hdsp_get_autosync_sample_rate \
1876}
1877
Takashi Iwai55e957d2005-11-17 14:52:13 +01001878static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001880 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
1882 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1883 uinfo->count = 1;
1884 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1885 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1886 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1887 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1888 return 0;
1889}
1890
Takashi Iwai55e957d2005-11-17 14:52:13 +01001891static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001893 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 switch (hdsp_external_sample_rate(hdsp)) {
1896 case 32000:
1897 ucontrol->value.enumerated.item[0] = 0;
1898 break;
1899 case 44100:
1900 ucontrol->value.enumerated.item[0] = 1;
1901 break;
1902 case 48000:
1903 ucontrol->value.enumerated.item[0] = 2;
1904 break;
1905 case 64000:
1906 ucontrol->value.enumerated.item[0] = 3;
1907 break;
1908 case 88200:
1909 ucontrol->value.enumerated.item[0] = 4;
1910 break;
1911 case 96000:
1912 ucontrol->value.enumerated.item[0] = 5;
1913 break;
1914 case 128000:
1915 ucontrol->value.enumerated.item[0] = 7;
1916 break;
1917 case 176400:
1918 ucontrol->value.enumerated.item[0] = 8;
1919 break;
1920 case 192000:
1921 ucontrol->value.enumerated.item[0] = 9;
1922 break;
1923 default:
1924 ucontrol->value.enumerated.item[0] = 6;
1925 }
1926 return 0;
1927}
1928
1929#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001930{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 .name = xname, \
1932 .index = xindex, \
1933 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1934 .info = snd_hdsp_info_system_clock_mode, \
1935 .get = snd_hdsp_get_system_clock_mode \
1936}
1937
Takashi Iwai55e957d2005-11-17 14:52:13 +01001938static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001940 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001942 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 1;
1945}
1946
Takashi Iwai55e957d2005-11-17 14:52:13 +01001947static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 static char *texts[] = {"Master", "Slave" };
1950
1951 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1952 uinfo->count = 1;
1953 uinfo->value.enumerated.items = 2;
1954 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1955 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1956 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1957 return 0;
1958}
1959
Takashi Iwai55e957d2005-11-17 14:52:13 +01001960static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001962 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1965 return 0;
1966}
1967
1968#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001969{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 .name = xname, \
1971 .index = xindex, \
1972 .info = snd_hdsp_info_clock_source, \
1973 .get = snd_hdsp_get_clock_source, \
1974 .put = snd_hdsp_put_clock_source \
1975}
1976
Takashi Iwai55e957d2005-11-17 14:52:13 +01001977static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 if (hdsp->control_register & HDSP_ClockModeMaster) {
1980 switch (hdsp->system_sample_rate) {
1981 case 32000:
1982 return 1;
1983 case 44100:
1984 return 2;
1985 case 48000:
1986 return 3;
1987 case 64000:
1988 return 4;
1989 case 88200:
1990 return 5;
1991 case 96000:
1992 return 6;
1993 case 128000:
1994 return 7;
1995 case 176400:
1996 return 8;
1997 case 192000:
1998 return 9;
1999 default:
2000 return 3;
2001 }
2002 } else {
2003 return 0;
2004 }
2005}
2006
Takashi Iwai55e957d2005-11-17 14:52:13 +01002007static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 int rate;
2010 switch (mode) {
2011 case HDSP_CLOCK_SOURCE_AUTOSYNC:
2012 if (hdsp_external_sample_rate(hdsp) != 0) {
2013 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
2014 hdsp->control_register &= ~HDSP_ClockModeMaster;
2015 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2016 return 0;
2017 }
2018 }
2019 return -1;
2020 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2021 rate = 32000;
2022 break;
2023 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2024 rate = 44100;
2025 break;
2026 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2027 rate = 48000;
2028 break;
2029 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2030 rate = 64000;
2031 break;
2032 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2033 rate = 88200;
2034 break;
2035 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2036 rate = 96000;
2037 break;
2038 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2039 rate = 128000;
2040 break;
2041 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2042 rate = 176400;
2043 break;
2044 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2045 rate = 192000;
2046 break;
2047 default:
2048 rate = 48000;
2049 }
2050 hdsp->control_register |= HDSP_ClockModeMaster;
2051 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2052 hdsp_set_rate(hdsp, rate, 1);
2053 return 0;
2054}
2055
Takashi Iwai55e957d2005-11-17 14:52:13 +01002056static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 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 +01002059 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2062 uinfo->count = 1;
2063 if (hdsp->io_type == H9632)
2064 uinfo->value.enumerated.items = 10;
2065 else
2066 uinfo->value.enumerated.items = 7;
2067 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2068 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2069 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2070 return 0;
2071}
2072
Takashi Iwai55e957d2005-11-17 14:52:13 +01002073static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002075 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2078 return 0;
2079}
2080
Takashi Iwai55e957d2005-11-17 14:52:13 +01002081static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002083 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int change;
2085 int val;
2086
2087 if (!snd_hdsp_use_is_exclusive(hdsp))
2088 return -EBUSY;
2089 val = ucontrol->value.enumerated.item[0];
2090 if (val < 0) val = 0;
2091 if (hdsp->io_type == H9632) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002092 if (val > 9)
2093 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002095 if (val > 6)
2096 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002099 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002101 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 spin_unlock_irq(&hdsp->lock);
2104 return change;
2105}
2106
Takashi Iwai55e957d2005-11-17 14:52:13 +01002107static int snd_hdsp_info_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002108{
2109 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2110 uinfo->count = 1;
2111 uinfo->value.integer.min = 0;
2112 uinfo->value.integer.max = 1;
2113 return 0;
2114}
2115
Takashi Iwai55e957d2005-11-17 14:52:13 +01002116static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002117{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002118 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002119
2120 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2121 return 0;
2122}
2123
Takashi Iwai55e957d2005-11-17 14:52:13 +01002124static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002125{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002126 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002127 int change;
2128
2129 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2130 if (change)
2131 hdsp->clock_source_locked = ucontrol->value.integer.value[0];
2132 return change;
2133}
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002136{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 .name = xname, \
2138 .index = xindex, \
2139 .info = snd_hdsp_info_da_gain, \
2140 .get = snd_hdsp_get_da_gain, \
2141 .put = snd_hdsp_put_da_gain \
2142}
2143
Takashi Iwai55e957d2005-11-17 14:52:13 +01002144static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 switch (hdsp->control_register & HDSP_DAGainMask) {
2147 case HDSP_DAGainHighGain:
2148 return 0;
2149 case HDSP_DAGainPlus4dBu:
2150 return 1;
2151 case HDSP_DAGainMinus10dBV:
2152 return 2;
2153 default:
2154 return 1;
2155 }
2156}
2157
Takashi Iwai55e957d2005-11-17 14:52:13 +01002158static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 hdsp->control_register &= ~HDSP_DAGainMask;
2161 switch (mode) {
2162 case 0:
2163 hdsp->control_register |= HDSP_DAGainHighGain;
2164 break;
2165 case 1:
2166 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2167 break;
2168 case 2:
2169 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2170 break;
2171 default:
2172 return -1;
2173
2174 }
2175 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2176 return 0;
2177}
2178
Takashi Iwai55e957d2005-11-17 14:52:13 +01002179static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
2182
2183 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2184 uinfo->count = 1;
2185 uinfo->value.enumerated.items = 3;
2186 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2187 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2188 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2189 return 0;
2190}
2191
Takashi Iwai55e957d2005-11-17 14:52:13 +01002192static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002194 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2197 return 0;
2198}
2199
Takashi Iwai55e957d2005-11-17 14:52:13 +01002200static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002202 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int change;
2204 int val;
2205
2206 if (!snd_hdsp_use_is_exclusive(hdsp))
2207 return -EBUSY;
2208 val = ucontrol->value.enumerated.item[0];
2209 if (val < 0) val = 0;
2210 if (val > 2) val = 2;
2211 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002212 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002214 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 spin_unlock_irq(&hdsp->lock);
2217 return change;
2218}
2219
2220#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002221{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 .name = xname, \
2223 .index = xindex, \
2224 .info = snd_hdsp_info_ad_gain, \
2225 .get = snd_hdsp_get_ad_gain, \
2226 .put = snd_hdsp_put_ad_gain \
2227}
2228
Takashi Iwai55e957d2005-11-17 14:52:13 +01002229static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 switch (hdsp->control_register & HDSP_ADGainMask) {
2232 case HDSP_ADGainMinus10dBV:
2233 return 0;
2234 case HDSP_ADGainPlus4dBu:
2235 return 1;
2236 case HDSP_ADGainLowGain:
2237 return 2;
2238 default:
2239 return 1;
2240 }
2241}
2242
Takashi Iwai55e957d2005-11-17 14:52:13 +01002243static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 hdsp->control_register &= ~HDSP_ADGainMask;
2246 switch (mode) {
2247 case 0:
2248 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2249 break;
2250 case 1:
2251 hdsp->control_register |= HDSP_ADGainPlus4dBu;
2252 break;
2253 case 2:
2254 hdsp->control_register |= HDSP_ADGainLowGain;
2255 break;
2256 default:
2257 return -1;
2258
2259 }
2260 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2261 return 0;
2262}
2263
Takashi Iwai55e957d2005-11-17 14:52:13 +01002264static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
2267
2268 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2269 uinfo->count = 1;
2270 uinfo->value.enumerated.items = 3;
2271 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2272 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2273 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2274 return 0;
2275}
2276
Takashi Iwai55e957d2005-11-17 14:52:13 +01002277static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002279 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2282 return 0;
2283}
2284
Takashi Iwai55e957d2005-11-17 14:52:13 +01002285static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002287 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int change;
2289 int val;
2290
2291 if (!snd_hdsp_use_is_exclusive(hdsp))
2292 return -EBUSY;
2293 val = ucontrol->value.enumerated.item[0];
2294 if (val < 0) val = 0;
2295 if (val > 2) val = 2;
2296 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002297 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002299 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 spin_unlock_irq(&hdsp->lock);
2302 return change;
2303}
2304
2305#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002306{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 .name = xname, \
2308 .index = xindex, \
2309 .info = snd_hdsp_info_phone_gain, \
2310 .get = snd_hdsp_get_phone_gain, \
2311 .put = snd_hdsp_put_phone_gain \
2312}
2313
Takashi Iwai55e957d2005-11-17 14:52:13 +01002314static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2317 case HDSP_PhoneGain0dB:
2318 return 0;
2319 case HDSP_PhoneGainMinus6dB:
2320 return 1;
2321 case HDSP_PhoneGainMinus12dB:
2322 return 2;
2323 default:
2324 return 0;
2325 }
2326}
2327
Takashi Iwai55e957d2005-11-17 14:52:13 +01002328static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 hdsp->control_register &= ~HDSP_PhoneGainMask;
2331 switch (mode) {
2332 case 0:
2333 hdsp->control_register |= HDSP_PhoneGain0dB;
2334 break;
2335 case 1:
2336 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
2337 break;
2338 case 2:
2339 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2340 break;
2341 default:
2342 return -1;
2343
2344 }
2345 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2346 return 0;
2347}
2348
Takashi Iwai55e957d2005-11-17 14:52:13 +01002349static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
2352
2353 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2354 uinfo->count = 1;
2355 uinfo->value.enumerated.items = 3;
2356 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2357 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2358 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2359 return 0;
2360}
2361
Takashi Iwai55e957d2005-11-17 14:52:13 +01002362static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002364 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2367 return 0;
2368}
2369
Takashi Iwai55e957d2005-11-17 14:52:13 +01002370static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002372 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 int change;
2374 int val;
2375
2376 if (!snd_hdsp_use_is_exclusive(hdsp))
2377 return -EBUSY;
2378 val = ucontrol->value.enumerated.item[0];
2379 if (val < 0) val = 0;
2380 if (val > 2) val = 2;
2381 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002382 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002384 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 spin_unlock_irq(&hdsp->lock);
2387 return change;
2388}
2389
2390#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002391{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 .name = xname, \
2393 .index = xindex, \
2394 .info = snd_hdsp_info_xlr_breakout_cable, \
2395 .get = snd_hdsp_get_xlr_breakout_cable, \
2396 .put = snd_hdsp_put_xlr_breakout_cable \
2397}
2398
Takashi Iwai55e957d2005-11-17 14:52:13 +01002399static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002401 if (hdsp->control_register & HDSP_XLRBreakoutCable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return 0;
2404}
2405
Takashi Iwai55e957d2005-11-17 14:52:13 +01002406static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002408 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 hdsp->control_register |= HDSP_XLRBreakoutCable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002410 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 hdsp->control_register &= ~HDSP_XLRBreakoutCable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2413 return 0;
2414}
2415
Takashi Iwai55e957d2005-11-17 14:52:13 +01002416static int snd_hdsp_info_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2419 uinfo->count = 1;
2420 uinfo->value.integer.min = 0;
2421 uinfo->value.integer.max = 1;
2422 return 0;
2423}
2424
Takashi Iwai55e957d2005-11-17 14:52:13 +01002425static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002427 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
2430 return 0;
2431}
2432
Takashi Iwai55e957d2005-11-17 14:52:13 +01002433static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002435 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int change;
2437 int val;
2438
2439 if (!snd_hdsp_use_is_exclusive(hdsp))
2440 return -EBUSY;
2441 val = ucontrol->value.integer.value[0] & 1;
2442 spin_lock_irq(&hdsp->lock);
2443 change = (int)val != hdsp_xlr_breakout_cable(hdsp);
2444 hdsp_set_xlr_breakout_cable(hdsp, val);
2445 spin_unlock_irq(&hdsp->lock);
2446 return change;
2447}
2448
2449/* (De)activates old RME Analog Extension Board
2450 These are connected to the internal ADAT connector
2451 Switching this on desactivates external ADAT
2452*/
2453#define HDSP_AEB(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002454{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .name = xname, \
2456 .index = xindex, \
2457 .info = snd_hdsp_info_aeb, \
2458 .get = snd_hdsp_get_aeb, \
2459 .put = snd_hdsp_put_aeb \
2460}
2461
Takashi Iwai55e957d2005-11-17 14:52:13 +01002462static int hdsp_aeb(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002464 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467}
2468
Takashi Iwai55e957d2005-11-17 14:52:13 +01002469static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002471 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 hdsp->control_register |= HDSP_AnalogExtensionBoard;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002473 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2476 return 0;
2477}
2478
Takashi Iwai55e957d2005-11-17 14:52:13 +01002479static int snd_hdsp_info_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2482 uinfo->count = 1;
2483 uinfo->value.integer.min = 0;
2484 uinfo->value.integer.max = 1;
2485 return 0;
2486}
2487
Takashi Iwai55e957d2005-11-17 14:52:13 +01002488static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002490 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
2493 return 0;
2494}
2495
Takashi Iwai55e957d2005-11-17 14:52:13 +01002496static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002498 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 int change;
2500 int val;
2501
2502 if (!snd_hdsp_use_is_exclusive(hdsp))
2503 return -EBUSY;
2504 val = ucontrol->value.integer.value[0] & 1;
2505 spin_lock_irq(&hdsp->lock);
2506 change = (int)val != hdsp_aeb(hdsp);
2507 hdsp_set_aeb(hdsp, val);
2508 spin_unlock_irq(&hdsp->lock);
2509 return change;
2510}
2511
2512#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002513{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .name = xname, \
2515 .index = xindex, \
2516 .info = snd_hdsp_info_pref_sync_ref, \
2517 .get = snd_hdsp_get_pref_sync_ref, \
2518 .put = snd_hdsp_put_pref_sync_ref \
2519}
2520
Takashi Iwai55e957d2005-11-17 14:52:13 +01002521static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 /* Notice that this looks at the requested sync source,
2524 not the one actually in use.
2525 */
2526
2527 switch (hdsp->control_register & HDSP_SyncRefMask) {
2528 case HDSP_SyncRef_ADAT1:
2529 return HDSP_SYNC_FROM_ADAT1;
2530 case HDSP_SyncRef_ADAT2:
2531 return HDSP_SYNC_FROM_ADAT2;
2532 case HDSP_SyncRef_ADAT3:
2533 return HDSP_SYNC_FROM_ADAT3;
2534 case HDSP_SyncRef_SPDIF:
2535 return HDSP_SYNC_FROM_SPDIF;
2536 case HDSP_SyncRef_WORD:
2537 return HDSP_SYNC_FROM_WORD;
2538 case HDSP_SyncRef_ADAT_SYNC:
2539 return HDSP_SYNC_FROM_ADAT_SYNC;
2540 default:
2541 return HDSP_SYNC_FROM_WORD;
2542 }
2543 return 0;
2544}
2545
Takashi Iwai55e957d2005-11-17 14:52:13 +01002546static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 hdsp->control_register &= ~HDSP_SyncRefMask;
2549 switch (pref) {
2550 case HDSP_SYNC_FROM_ADAT1:
2551 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2552 break;
2553 case HDSP_SYNC_FROM_ADAT2:
2554 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2555 break;
2556 case HDSP_SYNC_FROM_ADAT3:
2557 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2558 break;
2559 case HDSP_SYNC_FROM_SPDIF:
2560 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2561 break;
2562 case HDSP_SYNC_FROM_WORD:
2563 hdsp->control_register |= HDSP_SyncRef_WORD;
2564 break;
2565 case HDSP_SYNC_FROM_ADAT_SYNC:
2566 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2567 break;
2568 default:
2569 return -1;
2570 }
2571 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2572 return 0;
2573}
2574
Takashi Iwai55e957d2005-11-17 14:52:13 +01002575static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002578 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2581 uinfo->count = 1;
2582
2583 switch (hdsp->io_type) {
2584 case Digiface:
2585 case H9652:
2586 uinfo->value.enumerated.items = 6;
2587 break;
2588 case Multiface:
2589 uinfo->value.enumerated.items = 4;
2590 break;
2591 case H9632:
2592 uinfo->value.enumerated.items = 3;
2593 break;
2594 default:
2595 uinfo->value.enumerated.items = 0;
2596 break;
2597 }
2598
2599 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2600 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2601 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2602 return 0;
2603}
2604
Takashi Iwai55e957d2005-11-17 14:52:13 +01002605static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002607 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2610 return 0;
2611}
2612
Takashi Iwai55e957d2005-11-17 14:52:13 +01002613static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002615 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 int change, max;
2617 unsigned int val;
2618
2619 if (!snd_hdsp_use_is_exclusive(hdsp))
2620 return -EBUSY;
2621
2622 switch (hdsp->io_type) {
2623 case Digiface:
2624 case H9652:
2625 max = 6;
2626 break;
2627 case Multiface:
2628 max = 4;
2629 break;
2630 case H9632:
2631 max = 3;
2632 break;
2633 default:
2634 return -EIO;
2635 }
2636
2637 val = ucontrol->value.enumerated.item[0] % max;
2638 spin_lock_irq(&hdsp->lock);
2639 change = (int)val != hdsp_pref_sync_ref(hdsp);
2640 hdsp_set_pref_sync_ref(hdsp, val);
2641 spin_unlock_irq(&hdsp->lock);
2642 return change;
2643}
2644
2645#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002646{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .name = xname, \
2648 .index = xindex, \
2649 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2650 .info = snd_hdsp_info_autosync_ref, \
2651 .get = snd_hdsp_get_autosync_ref, \
2652}
2653
Takashi Iwai55e957d2005-11-17 14:52:13 +01002654static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 /* This looks at the autosync selected sync reference */
2657 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2658
2659 switch (status2 & HDSP_SelSyncRefMask) {
2660 case HDSP_SelSyncRef_WORD:
2661 return HDSP_AUTOSYNC_FROM_WORD;
2662 case HDSP_SelSyncRef_ADAT_SYNC:
2663 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2664 case HDSP_SelSyncRef_SPDIF:
2665 return HDSP_AUTOSYNC_FROM_SPDIF;
2666 case HDSP_SelSyncRefMask:
2667 return HDSP_AUTOSYNC_FROM_NONE;
2668 case HDSP_SelSyncRef_ADAT1:
2669 return HDSP_AUTOSYNC_FROM_ADAT1;
2670 case HDSP_SelSyncRef_ADAT2:
2671 return HDSP_AUTOSYNC_FROM_ADAT2;
2672 case HDSP_SelSyncRef_ADAT3:
2673 return HDSP_AUTOSYNC_FROM_ADAT3;
2674 default:
2675 return HDSP_AUTOSYNC_FROM_WORD;
2676 }
2677 return 0;
2678}
2679
Takashi Iwai55e957d2005-11-17 14:52:13 +01002680static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
2683
2684 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2685 uinfo->count = 1;
2686 uinfo->value.enumerated.items = 7;
2687 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2688 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2689 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2690 return 0;
2691}
2692
Takashi Iwai55e957d2005-11-17 14:52:13 +01002693static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002695 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2698 return 0;
2699}
2700
2701#define HDSP_LINE_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002702{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 .name = xname, \
2704 .index = xindex, \
2705 .info = snd_hdsp_info_line_out, \
2706 .get = snd_hdsp_get_line_out, \
2707 .put = snd_hdsp_put_line_out \
2708}
2709
Takashi Iwai55e957d2005-11-17 14:52:13 +01002710static int hdsp_line_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
2713}
2714
Takashi Iwai55e957d2005-11-17 14:52:13 +01002715static int hdsp_set_line_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002717 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 hdsp->control_register |= HDSP_LineOut;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002719 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 hdsp->control_register &= ~HDSP_LineOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2722 return 0;
2723}
2724
Takashi Iwai55e957d2005-11-17 14:52:13 +01002725static int snd_hdsp_info_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2728 uinfo->count = 1;
2729 uinfo->value.integer.min = 0;
2730 uinfo->value.integer.max = 1;
2731 return 0;
2732}
2733
Takashi Iwai55e957d2005-11-17 14:52:13 +01002734static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002736 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 spin_lock_irq(&hdsp->lock);
2739 ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
2740 spin_unlock_irq(&hdsp->lock);
2741 return 0;
2742}
2743
Takashi Iwai55e957d2005-11-17 14:52:13 +01002744static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002746 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int change;
2748 unsigned int val;
2749
2750 if (!snd_hdsp_use_is_exclusive(hdsp))
2751 return -EBUSY;
2752 val = ucontrol->value.integer.value[0] & 1;
2753 spin_lock_irq(&hdsp->lock);
2754 change = (int)val != hdsp_line_out(hdsp);
2755 hdsp_set_line_output(hdsp, val);
2756 spin_unlock_irq(&hdsp->lock);
2757 return change;
2758}
2759
2760#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002761{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 .name = xname, \
2763 .index = xindex, \
2764 .info = snd_hdsp_info_precise_pointer, \
2765 .get = snd_hdsp_get_precise_pointer, \
2766 .put = snd_hdsp_put_precise_pointer \
2767}
2768
Takashi Iwai55e957d2005-11-17 14:52:13 +01002769static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002771 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 hdsp->precise_ptr = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002773 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
2776}
2777
Takashi Iwai55e957d2005-11-17 14:52:13 +01002778static int snd_hdsp_info_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2781 uinfo->count = 1;
2782 uinfo->value.integer.min = 0;
2783 uinfo->value.integer.max = 1;
2784 return 0;
2785}
2786
Takashi Iwai55e957d2005-11-17 14:52:13 +01002787static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002789 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 spin_lock_irq(&hdsp->lock);
2792 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2793 spin_unlock_irq(&hdsp->lock);
2794 return 0;
2795}
2796
Takashi Iwai55e957d2005-11-17 14:52:13 +01002797static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002799 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 int change;
2801 unsigned int val;
2802
2803 if (!snd_hdsp_use_is_exclusive(hdsp))
2804 return -EBUSY;
2805 val = ucontrol->value.integer.value[0] & 1;
2806 spin_lock_irq(&hdsp->lock);
2807 change = (int)val != hdsp->precise_ptr;
2808 hdsp_set_precise_pointer(hdsp, val);
2809 spin_unlock_irq(&hdsp->lock);
2810 return change;
2811}
2812
2813#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002814{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 .name = xname, \
2816 .index = xindex, \
2817 .info = snd_hdsp_info_use_midi_tasklet, \
2818 .get = snd_hdsp_get_use_midi_tasklet, \
2819 .put = snd_hdsp_put_use_midi_tasklet \
2820}
2821
Takashi Iwai55e957d2005-11-17 14:52:13 +01002822static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002824 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002826 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return 0;
2829}
2830
Takashi Iwai55e957d2005-11-17 14:52:13 +01002831static int snd_hdsp_info_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2834 uinfo->count = 1;
2835 uinfo->value.integer.min = 0;
2836 uinfo->value.integer.max = 1;
2837 return 0;
2838}
2839
Takashi Iwai55e957d2005-11-17 14:52:13 +01002840static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002842 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 spin_lock_irq(&hdsp->lock);
2845 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2846 spin_unlock_irq(&hdsp->lock);
2847 return 0;
2848}
2849
Takashi Iwai55e957d2005-11-17 14:52:13 +01002850static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002852 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 int change;
2854 unsigned int val;
2855
2856 if (!snd_hdsp_use_is_exclusive(hdsp))
2857 return -EBUSY;
2858 val = ucontrol->value.integer.value[0] & 1;
2859 spin_lock_irq(&hdsp->lock);
2860 change = (int)val != hdsp->use_midi_tasklet;
2861 hdsp_set_use_midi_tasklet(hdsp, val);
2862 spin_unlock_irq(&hdsp->lock);
2863 return change;
2864}
2865
2866#define HDSP_MIXER(xname, xindex) \
2867{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2868 .name = xname, \
2869 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002870 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2872 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2873 .info = snd_hdsp_info_mixer, \
2874 .get = snd_hdsp_get_mixer, \
2875 .put = snd_hdsp_put_mixer \
2876}
2877
Takashi Iwai55e957d2005-11-17 14:52:13 +01002878static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
2880 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2881 uinfo->count = 3;
2882 uinfo->value.integer.min = 0;
2883 uinfo->value.integer.max = 65536;
2884 uinfo->value.integer.step = 1;
2885 return 0;
2886}
2887
Takashi Iwai55e957d2005-11-17 14:52:13 +01002888static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002890 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 int source;
2892 int destination;
2893 int addr;
2894
2895 source = ucontrol->value.integer.value[0];
2896 destination = ucontrol->value.integer.value[1];
2897
Takashi Iwaib0b98112005-10-20 18:29:58 +02002898 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002900 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 spin_lock_irq(&hdsp->lock);
2904 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2905 spin_unlock_irq(&hdsp->lock);
2906 return 0;
2907}
2908
Takashi Iwai55e957d2005-11-17 14:52:13 +01002909static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002911 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 int change;
2913 int source;
2914 int destination;
2915 int gain;
2916 int addr;
2917
2918 if (!snd_hdsp_use_is_exclusive(hdsp))
2919 return -EBUSY;
2920
2921 source = ucontrol->value.integer.value[0];
2922 destination = ucontrol->value.integer.value[1];
2923
Takashi Iwaib0b98112005-10-20 18:29:58 +02002924 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002926 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 gain = ucontrol->value.integer.value[2];
2930
2931 spin_lock_irq(&hdsp->lock);
2932 change = gain != hdsp_read_gain(hdsp, addr);
2933 if (change)
2934 hdsp_write_gain(hdsp, addr, gain);
2935 spin_unlock_irq(&hdsp->lock);
2936 return change;
2937}
2938
2939#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002940{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 .name = xname, \
2942 .index = xindex, \
2943 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2944 .info = snd_hdsp_info_sync_check, \
2945 .get = snd_hdsp_get_wc_sync_check \
2946}
2947
Takashi Iwai55e957d2005-11-17 14:52:13 +01002948static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 static char *texts[] = {"No Lock", "Lock", "Sync" };
2951 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2952 uinfo->count = 1;
2953 uinfo->value.enumerated.items = 3;
2954 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2955 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2956 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2957 return 0;
2958}
2959
Takashi Iwai55e957d2005-11-17 14:52:13 +01002960static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2963 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002964 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002966 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002968 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return 0;
2971}
2972
Takashi Iwai55e957d2005-11-17 14:52:13 +01002973static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002975 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2978 return 0;
2979}
2980
2981#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002982{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 .name = xname, \
2984 .index = xindex, \
2985 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2986 .info = snd_hdsp_info_sync_check, \
2987 .get = snd_hdsp_get_spdif_sync_check \
2988}
2989
Takashi Iwai55e957d2005-11-17 14:52:13 +01002990static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
2992 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002993 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002995 else {
2996 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002998 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001 return 0;
3002}
3003
Takashi Iwai55e957d2005-11-17 14:52:13 +01003004static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003006 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
3009 return 0;
3010}
3011
3012#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003013{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 .name = xname, \
3015 .index = xindex, \
3016 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3017 .info = snd_hdsp_info_sync_check, \
3018 .get = snd_hdsp_get_adatsync_sync_check \
3019}
3020
Takashi Iwai55e957d2005-11-17 14:52:13 +01003021static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 int status = hdsp_read(hdsp, HDSP_statusRegister);
3024 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003025 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003027 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003029 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Takashi Iwai55e957d2005-11-17 14:52:13 +01003033static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003035 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
3038 return 0;
3039}
3040
3041#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003042{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3044 .info = snd_hdsp_info_sync_check, \
3045 .get = snd_hdsp_get_adat_sync_check \
3046}
3047
Takashi Iwai55e957d2005-11-17 14:52:13 +01003048static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 int status = hdsp_read(hdsp, HDSP_statusRegister);
3051
3052 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003053 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003055 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003057 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Takashi Iwai55e957d2005-11-17 14:52:13 +01003061static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003064 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 offset = ucontrol->id.index - 1;
3067 snd_assert(offset >= 0);
3068
3069 switch (hdsp->io_type) {
3070 case Digiface:
3071 case H9652:
3072 if (offset >= 3)
3073 return -EINVAL;
3074 break;
3075 case Multiface:
3076 case H9632:
3077 if (offset >= 1)
3078 return -EINVAL;
3079 break;
3080 default:
3081 return -EIO;
3082 }
3083
3084 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
3085 return 0;
3086}
3087
Takashi Iwai55e957d2005-11-17 14:52:13 +01003088static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089HDSP_DA_GAIN("DA Gain", 0),
3090HDSP_AD_GAIN("AD Gain", 0),
3091HDSP_PHONE_GAIN("Phones Gain", 0),
3092HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0)
3093};
3094
Takashi Iwai55e957d2005-11-17 14:52:13 +01003095static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Clemens Ladisch5549d542005-08-03 13:50:30 +02003097 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
3099 .info = snd_hdsp_control_spdif_info,
3100 .get = snd_hdsp_control_spdif_get,
3101 .put = snd_hdsp_control_spdif_put,
3102},
3103{
3104 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003105 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
3107 .info = snd_hdsp_control_spdif_stream_info,
3108 .get = snd_hdsp_control_spdif_stream_get,
3109 .put = snd_hdsp_control_spdif_stream_put,
3110},
3111{
3112 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003113 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
3115 .info = snd_hdsp_control_spdif_mask_info,
3116 .get = snd_hdsp_control_spdif_mask_get,
3117 .private_value = IEC958_AES0_NONAUDIO |
3118 IEC958_AES0_PROFESSIONAL |
3119 IEC958_AES0_CON_EMPHASIS,
3120},
3121{
3122 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003123 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
3125 .info = snd_hdsp_control_spdif_mask_info,
3126 .get = snd_hdsp_control_spdif_mask_get,
3127 .private_value = IEC958_AES0_NONAUDIO |
3128 IEC958_AES0_PROFESSIONAL |
3129 IEC958_AES0_PRO_EMPHASIS,
3130},
3131HDSP_MIXER("Mixer", 0),
3132HDSP_SPDIF_IN("IEC958 Input Connector", 0),
3133HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
3134HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
3135HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
3136HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
3137/* 'Sample Clock Source' complies with the alsa control naming scheme */
3138HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003139{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003140 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3141 .name = "Sample Clock Source Locking",
3142 .info = snd_hdsp_info_clock_source_lock,
3143 .get = snd_hdsp_get_clock_source_lock,
3144 .put = snd_hdsp_put_clock_source_lock,
3145},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3147HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3148HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3149HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3150HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3151/* 'External Rate' complies with the alsa control naming scheme */
3152HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3153HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3154HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3155HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
3156HDSP_LINE_OUT("Line Out", 0),
3157HDSP_PRECISE_POINTER("Precise Pointer", 0),
3158HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3159};
3160
Takashi Iwai55e957d2005-11-17 14:52:13 +01003161static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
3162static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Takashi Iwai55e957d2005-11-17 14:52:13 +01003164static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
3166 unsigned int idx;
3167 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003168 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003171 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3174 hdsp->spdif_ctl = kctl;
3175 }
3176
3177 /* ADAT SyncCheck status */
3178 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3179 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003180 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3183 for (idx = 1; idx < 3; ++idx) {
3184 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003185 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188 }
3189
3190 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3191 if (hdsp->io_type == H9632) {
3192 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003193 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196 }
3197
3198 /* AEB control for H96xx card */
3199 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003200 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
3203
3204 return 0;
3205}
3206
3207/*------------------------------------------------------------
3208 /proc interface
3209 ------------------------------------------------------------*/
3210
3211static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003212snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003214 struct hdsp *hdsp = (struct hdsp *) entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 unsigned int status;
3216 unsigned int status2;
3217 char *pref_sync_ref;
3218 char *autosync_ref;
3219 char *system_clock_mode;
3220 char *clock_source;
3221 int x;
3222
Remy Brunoecb594e2006-06-12 09:25:22 +02003223 if (hdsp_check_for_iobox (hdsp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
3225 return;
Remy Brunoecb594e2006-06-12 09:25:22 +02003226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Takashi Iwaib0b98112005-10-20 18:29:58 +02003228 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (hdsp->state & HDSP_FirmwareCached) {
3230 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3231 snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
3232 return;
3233 }
3234 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003235 int err = -EINVAL;
3236#ifdef HDSP_FW_LOADER
3237 err = hdsp_request_fw_loader(hdsp);
3238#endif
3239 if (err < 0) {
3240 snd_iprintf(buffer,
3241 "No firmware loaded nor cached, "
3242 "please upload firmware.\n");
3243 return;
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246 }
3247
3248 status = hdsp_read(hdsp, HDSP_statusRegister);
3249 status2 = hdsp_read(hdsp, HDSP_status2Register);
3250
3251 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
3252 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3253 hdsp->capture_buffer, hdsp->playback_buffer);
3254 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3255 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3256 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3257 snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
3258 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3259 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3260 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3261 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3262 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3263 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3264 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3265 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3266
3267 snd_iprintf(buffer, "\n");
3268
3269 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3270
3271 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3272 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3273 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3274 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3275
3276 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3277
3278 snd_iprintf(buffer, "\n");
3279
3280
3281 switch (hdsp_clock_source(hdsp)) {
3282 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3283 clock_source = "AutoSync";
3284 break;
3285 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3286 clock_source = "Internal 32 kHz";
3287 break;
3288 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3289 clock_source = "Internal 44.1 kHz";
3290 break;
3291 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3292 clock_source = "Internal 48 kHz";
3293 break;
3294 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3295 clock_source = "Internal 64 kHz";
3296 break;
3297 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3298 clock_source = "Internal 88.2 kHz";
3299 break;
3300 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3301 clock_source = "Internal 96 kHz";
3302 break;
3303 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3304 clock_source = "Internal 128 kHz";
3305 break;
3306 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3307 clock_source = "Internal 176.4 kHz";
3308 break;
3309 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3310 clock_source = "Internal 192 kHz";
3311 break;
3312 default:
3313 clock_source = "Error";
3314 }
3315 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
3316
Takashi Iwaib0b98112005-10-20 18:29:58 +02003317 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 system_clock_mode = "Slave";
Takashi Iwaib0b98112005-10-20 18:29:58 +02003319 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 system_clock_mode = "Master";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 switch (hdsp_pref_sync_ref (hdsp)) {
3323 case HDSP_SYNC_FROM_WORD:
3324 pref_sync_ref = "Word Clock";
3325 break;
3326 case HDSP_SYNC_FROM_ADAT_SYNC:
3327 pref_sync_ref = "ADAT Sync";
3328 break;
3329 case HDSP_SYNC_FROM_SPDIF:
3330 pref_sync_ref = "SPDIF";
3331 break;
3332 case HDSP_SYNC_FROM_ADAT1:
3333 pref_sync_ref = "ADAT1";
3334 break;
3335 case HDSP_SYNC_FROM_ADAT2:
3336 pref_sync_ref = "ADAT2";
3337 break;
3338 case HDSP_SYNC_FROM_ADAT3:
3339 pref_sync_ref = "ADAT3";
3340 break;
3341 default:
3342 pref_sync_ref = "Word Clock";
3343 break;
3344 }
3345 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
3346
3347 switch (hdsp_autosync_ref (hdsp)) {
3348 case HDSP_AUTOSYNC_FROM_WORD:
3349 autosync_ref = "Word Clock";
3350 break;
3351 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3352 autosync_ref = "ADAT Sync";
3353 break;
3354 case HDSP_AUTOSYNC_FROM_SPDIF:
3355 autosync_ref = "SPDIF";
3356 break;
3357 case HDSP_AUTOSYNC_FROM_NONE:
3358 autosync_ref = "None";
3359 break;
3360 case HDSP_AUTOSYNC_FROM_ADAT1:
3361 autosync_ref = "ADAT1";
3362 break;
3363 case HDSP_AUTOSYNC_FROM_ADAT2:
3364 autosync_ref = "ADAT2";
3365 break;
3366 case HDSP_AUTOSYNC_FROM_ADAT3:
3367 autosync_ref = "ADAT3";
3368 break;
3369 default:
3370 autosync_ref = "---";
3371 break;
3372 }
3373 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
3374
3375 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
3376
3377 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3378
3379 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003380 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 snd_iprintf(buffer, "\n");
3383
3384 switch (hdsp_spdif_in(hdsp)) {
3385 case HDSP_SPDIFIN_OPTICAL:
3386 snd_iprintf(buffer, "IEC958 input: Optical\n");
3387 break;
3388 case HDSP_SPDIFIN_COAXIAL:
3389 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3390 break;
3391 case HDSP_SPDIFIN_INTERNAL:
3392 snd_iprintf(buffer, "IEC958 input: Internal\n");
3393 break;
3394 case HDSP_SPDIFIN_AES:
3395 snd_iprintf(buffer, "IEC958 input: AES\n");
3396 break;
3397 default:
3398 snd_iprintf(buffer, "IEC958 input: ???\n");
3399 break;
3400 }
3401
Takashi Iwaib0b98112005-10-20 18:29:58 +02003402 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003404 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Takashi Iwaib0b98112005-10-20 18:29:58 +02003407 if (hdsp->control_register & HDSP_SPDIFProfessional)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 snd_iprintf(buffer, "IEC958 quality: Professional\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003409 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Takashi Iwaib0b98112005-10-20 18:29:58 +02003412 if (hdsp->control_register & HDSP_SPDIFEmphasis)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 snd_iprintf(buffer, "IEC958 emphasis: on\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003414 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 snd_iprintf(buffer, "IEC958 emphasis: off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Takashi Iwaib0b98112005-10-20 18:29:58 +02003417 if (hdsp->control_register & HDSP_SPDIFNonAudio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003419 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003421 if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003423 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 snd_iprintf(buffer, "\n");
3427
3428 /* Sync Check */
3429 x = status & HDSP_Sync0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003430 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003432 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 switch (hdsp->io_type) {
3436 case Digiface:
3437 case H9652:
3438 x = status & HDSP_Sync1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003439 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003441 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 x = status & HDSP_Sync2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003444 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003446 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003448 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 default:
3450 /* relax */
3451 break;
3452 }
3453
3454 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003455 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003457 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003461 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003463 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 snd_iprintf (buffer, "Word Clock: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003467 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003469 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 snd_iprintf(buffer, "\n");
3473
3474 /* Informations about H9632 specific controls */
3475 if (hdsp->io_type == H9632) {
3476 char *tmp;
3477
3478 switch (hdsp_ad_gain(hdsp)) {
3479 case 0:
3480 tmp = "-10 dBV";
3481 break;
3482 case 1:
3483 tmp = "+4 dBu";
3484 break;
3485 default:
3486 tmp = "Lo Gain";
3487 break;
3488 }
3489 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3490
3491 switch (hdsp_da_gain(hdsp)) {
3492 case 0:
3493 tmp = "Hi Gain";
3494 break;
3495 case 1:
3496 tmp = "+4 dBu";
3497 break;
3498 default:
3499 tmp = "-10 dBV";
3500 break;
3501 }
3502 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
3503
3504 switch (hdsp_phone_gain(hdsp)) {
3505 case 0:
3506 tmp = "0 dB";
3507 break;
3508 case 1:
3509 tmp = "-6 dB";
3510 break;
3511 default:
3512 tmp = "-12 dB";
3513 break;
3514 }
3515 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3516
3517 snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
3518
Takashi Iwaib0b98112005-10-20 18:29:58 +02003519 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003521 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 snd_iprintf(buffer, "\n");
3524 }
3525
3526}
3527
Takashi Iwai55e957d2005-11-17 14:52:13 +01003528static void __devinit snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003530 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003533 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Takashi Iwai55e957d2005-11-17 14:52:13 +01003536static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3539 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3540}
3541
Takashi Iwai55e957d2005-11-17 14:52:13 +01003542static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
3544 unsigned long pb_bus, cb_bus;
3545
3546 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3547 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3548 if (hdsp->capture_dma_buf.area)
3549 snd_dma_free_pages(&hdsp->capture_dma_buf);
3550 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
3551 return -ENOMEM;
3552 }
3553
3554 /* Align to bus-space 64K boundary */
3555
Clemens Ladisch7ab39922006-10-09 08:13:32 +02003556 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3557 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 /* Tell the card where it is */
3560
3561 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3562 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3563
3564 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3565 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3566
3567 return 0;
3568}
3569
Takashi Iwai55e957d2005-11-17 14:52:13 +01003570static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 unsigned int i;
3573
3574 /* ASSUMPTION: hdsp->lock is either held, or
3575 there is no need to hold it (e.g. during module
3576 initalization).
3577 */
3578
3579 /* set defaults:
3580
3581 SPDIF Input via Coax
3582 Master clock mode
3583 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3584 which implies 2 4096 sample, 32Kbyte periods).
3585 Enable line out.
3586 */
3587
3588 hdsp->control_register = HDSP_ClockModeMaster |
3589 HDSP_SPDIFInputCoaxial |
3590 hdsp_encode_latency(7) |
3591 HDSP_LineOut;
3592
3593
3594 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3595
3596#ifdef SNDRV_BIG_ENDIAN
3597 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3598#else
3599 hdsp->control2_register = 0;
3600#endif
Takashi Iwaib0b98112005-10-20 18:29:58 +02003601 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003603 else
3604 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 hdsp_reset_hw_pointer(hdsp);
3607 hdsp_compute_period_size(hdsp);
3608
3609 /* silence everything */
3610
Takashi Iwaib0b98112005-10-20 18:29:58 +02003611 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 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 +02003615 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
3618
3619 /* H9632 specific defaults */
3620 if (hdsp->io_type == H9632) {
3621 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3622 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3623 }
3624
3625 /* set a default rate so that the channel map is set up.
3626 */
3627
3628 hdsp_set_rate(hdsp, 48000, 1);
3629
3630 return 0;
3631}
3632
3633static void hdsp_midi_tasklet(unsigned long arg)
3634{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003635 struct hdsp *hdsp = (struct hdsp *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Takashi Iwaib0b98112005-10-20 18:29:58 +02003637 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003639 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641}
3642
David Howells7d12e782006-10-05 14:55:46 +01003643static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003645 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 unsigned int status;
3647 int audio;
3648 int midi0;
3649 int midi1;
3650 unsigned int midi0status;
3651 unsigned int midi1status;
3652 int schedule = 0;
3653
3654 status = hdsp_read(hdsp, HDSP_statusRegister);
3655
3656 audio = status & HDSP_audioIRQPending;
3657 midi0 = status & HDSP_midi0IRQPending;
3658 midi1 = status & HDSP_midi1IRQPending;
3659
Takashi Iwaib0b98112005-10-20 18:29:58 +02003660 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3664
3665 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3666 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
3667
3668 if (audio) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003669 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Takashi Iwaib0b98112005-10-20 18:29:58 +02003672 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
3675
3676 if (midi0 && midi0status) {
3677 if (hdsp->use_midi_tasklet) {
3678 /* we disable interrupts for this input until processing is done */
3679 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3680 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3681 hdsp->midi[0].pending = 1;
3682 schedule = 1;
3683 } else {
3684 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3685 }
3686 }
3687 if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
3688 if (hdsp->use_midi_tasklet) {
3689 /* we disable interrupts for this input until processing is done */
3690 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3691 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3692 hdsp->midi[1].pending = 1;
3693 schedule = 1;
3694 } else {
3695 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3696 }
3697 }
3698 if (hdsp->use_midi_tasklet && schedule)
3699 tasklet_hi_schedule(&hdsp->midi_tasklet);
3700 return IRQ_HANDLED;
3701}
3702
Takashi Iwai55e957d2005-11-17 14:52:13 +01003703static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003705 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return hdsp_hw_pointer(hdsp);
3707}
3708
Takashi Iwai55e957d2005-11-17 14:52:13 +01003709static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 int stream,
3711 int channel)
3712
3713{
3714 int mapped_channel;
3715
3716 snd_assert(channel >= 0 && channel < hdsp->max_channels, return NULL);
3717
Takashi Iwaib0b98112005-10-20 18:29:58 +02003718 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Takashi Iwaib0b98112005-10-20 18:29:58 +02003721 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003723 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Takashi Iwai55e957d2005-11-17 14:52:13 +01003727static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3729{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003730 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 char *channel_buf;
3732
3733 snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
3734
3735 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3736 snd_assert(channel_buf != NULL, return -EIO);
3737 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3738 return -EFAULT;
3739 return count;
3740}
3741
Takashi Iwai55e957d2005-11-17 14:52:13 +01003742static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3744{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003745 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 char *channel_buf;
3747
3748 snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
3749
3750 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3751 snd_assert(channel_buf != NULL, return -EIO);
3752 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3753 return -EFAULT;
3754 return count;
3755}
3756
Takashi Iwai55e957d2005-11-17 14:52:13 +01003757static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
3759{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003760 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 char *channel_buf;
3762
3763 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3764 snd_assert(channel_buf != NULL, return -EIO);
3765 memset(channel_buf + pos * 4, 0, count * 4);
3766 return count;
3767}
3768
Takashi Iwai55e957d2005-11-17 14:52:13 +01003769static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003771 struct snd_pcm_runtime *runtime = substream->runtime;
3772 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3773 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3775 other = hdsp->capture_substream;
3776 else
3777 other = hdsp->playback_substream;
3778 if (hdsp->running)
3779 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3780 else
3781 runtime->status->hw_ptr = 0;
3782 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01003783 struct snd_pcm_substream *s;
3784 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01003785 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (s == other) {
3787 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3788 break;
3789 }
3790 }
3791 }
3792 return 0;
3793}
3794
Takashi Iwai55e957d2005-11-17 14:52:13 +01003795static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3796 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003798 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int err;
3800 pid_t this_pid;
3801 pid_t other_pid;
3802
Takashi Iwaib0b98112005-10-20 18:29:58 +02003803 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Takashi Iwaib0b98112005-10-20 18:29:58 +02003806 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
3809 spin_lock_irq(&hdsp->lock);
3810
3811 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3812 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
3813 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
3814 this_pid = hdsp->playback_pid;
3815 other_pid = hdsp->capture_pid;
3816 } else {
3817 this_pid = hdsp->capture_pid;
3818 other_pid = hdsp->playback_pid;
3819 }
3820
3821 if ((other_pid > 0) && (this_pid != other_pid)) {
3822
3823 /* The other stream is open, and not by the same
3824 task as this one. Make sure that the parameters
3825 that matter are the same.
3826 */
3827
3828 if (params_rate(params) != hdsp->system_sample_rate) {
3829 spin_unlock_irq(&hdsp->lock);
3830 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3831 return -EBUSY;
3832 }
3833
3834 if (params_period_size(params) != hdsp->period_bytes / 4) {
3835 spin_unlock_irq(&hdsp->lock);
3836 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3837 return -EBUSY;
3838 }
3839
3840 /* We're fine. */
3841
3842 spin_unlock_irq(&hdsp->lock);
3843 return 0;
3844
3845 } else {
3846 spin_unlock_irq(&hdsp->lock);
3847 }
3848
3849 /* how to make sure that the rate matches an externally-set one ?
3850 */
3851
3852 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003853 if (! hdsp->clock_source_locked) {
3854 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
3855 spin_unlock_irq(&hdsp->lock);
3856 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3857 return err;
3858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003860 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
3863 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3864 return err;
3865 }
3866
3867 return 0;
3868}
3869
Takashi Iwai55e957d2005-11-17 14:52:13 +01003870static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
3871 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003873 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 int mapped_channel;
3875
3876 snd_assert(info->channel < hdsp->max_channels, return -EINVAL);
3877
Takashi Iwaib0b98112005-10-20 18:29:58 +02003878 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
3881 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
3882 info->first = 0;
3883 info->step = 32;
3884 return 0;
3885}
3886
Takashi Iwai55e957d2005-11-17 14:52:13 +01003887static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 unsigned int cmd, void *arg)
3889{
3890 switch (cmd) {
3891 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b98112005-10-20 18:29:58 +02003894 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 default:
3896 break;
3897 }
3898
3899 return snd_pcm_lib_ioctl(substream, cmd, arg);
3900}
3901
Takashi Iwai55e957d2005-11-17 14:52:13 +01003902static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003904 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3905 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 int running;
3907
Takashi Iwaib0b98112005-10-20 18:29:58 +02003908 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Takashi Iwai311e70a2006-09-06 12:13:37 +02003911 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 spin_lock(&hdsp->lock);
3915 running = hdsp->running;
3916 switch (cmd) {
3917 case SNDRV_PCM_TRIGGER_START:
3918 running |= 1 << substream->stream;
3919 break;
3920 case SNDRV_PCM_TRIGGER_STOP:
3921 running &= ~(1 << substream->stream);
3922 break;
3923 default:
3924 snd_BUG();
3925 spin_unlock(&hdsp->lock);
3926 return -EINVAL;
3927 }
3928 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3929 other = hdsp->capture_substream;
3930 else
3931 other = hdsp->playback_substream;
3932
3933 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01003934 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01003935 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (s == other) {
3937 snd_pcm_trigger_done(s, substream);
3938 if (cmd == SNDRV_PCM_TRIGGER_START)
3939 running |= 1 << s->stream;
3940 else
3941 running &= ~(1 << s->stream);
3942 goto _ok;
3943 }
3944 }
3945 if (cmd == SNDRV_PCM_TRIGGER_START) {
3946 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
3947 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
3948 hdsp_silence_playback(hdsp);
3949 } else {
3950 if (running &&
3951 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3952 hdsp_silence_playback(hdsp);
3953 }
3954 } else {
3955 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
3956 hdsp_silence_playback(hdsp);
3957 }
3958 _ok:
3959 snd_pcm_trigger_done(substream, substream);
3960 if (!hdsp->running && running)
3961 hdsp_start_audio(hdsp);
3962 else if (hdsp->running && !running)
3963 hdsp_stop_audio(hdsp);
3964 hdsp->running = running;
3965 spin_unlock(&hdsp->lock);
3966
3967 return 0;
3968}
3969
Takashi Iwai55e957d2005-11-17 14:52:13 +01003970static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003972 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 int result = 0;
3974
Takashi Iwaib0b98112005-10-20 18:29:58 +02003975 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Takashi Iwaib0b98112005-10-20 18:29:58 +02003978 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 spin_lock_irq(&hdsp->lock);
3982 if (!hdsp->running)
3983 hdsp_reset_hw_pointer(hdsp);
3984 spin_unlock_irq(&hdsp->lock);
3985 return result;
3986}
3987
Takashi Iwai55e957d2005-11-17 14:52:13 +01003988static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
3990 .info = (SNDRV_PCM_INFO_MMAP |
3991 SNDRV_PCM_INFO_MMAP_VALID |
3992 SNDRV_PCM_INFO_NONINTERLEAVED |
3993 SNDRV_PCM_INFO_SYNC_START |
3994 SNDRV_PCM_INFO_DOUBLE),
3995#ifdef SNDRV_BIG_ENDIAN
3996 .formats = SNDRV_PCM_FMTBIT_S32_BE,
3997#else
3998 .formats = SNDRV_PCM_FMTBIT_S32_LE,
3999#endif
4000 .rates = (SNDRV_PCM_RATE_32000 |
4001 SNDRV_PCM_RATE_44100 |
4002 SNDRV_PCM_RATE_48000 |
4003 SNDRV_PCM_RATE_64000 |
4004 SNDRV_PCM_RATE_88200 |
4005 SNDRV_PCM_RATE_96000),
4006 .rate_min = 32000,
4007 .rate_max = 96000,
4008 .channels_min = 14,
4009 .channels_max = HDSP_MAX_CHANNELS,
4010 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4011 .period_bytes_min = (64 * 4) * 10,
4012 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4013 .periods_min = 2,
4014 .periods_max = 2,
4015 .fifo_size = 0
4016};
4017
Takashi Iwai55e957d2005-11-17 14:52:13 +01004018static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020 .info = (SNDRV_PCM_INFO_MMAP |
4021 SNDRV_PCM_INFO_MMAP_VALID |
4022 SNDRV_PCM_INFO_NONINTERLEAVED |
4023 SNDRV_PCM_INFO_SYNC_START),
4024#ifdef SNDRV_BIG_ENDIAN
4025 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4026#else
4027 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4028#endif
4029 .rates = (SNDRV_PCM_RATE_32000 |
4030 SNDRV_PCM_RATE_44100 |
4031 SNDRV_PCM_RATE_48000 |
4032 SNDRV_PCM_RATE_64000 |
4033 SNDRV_PCM_RATE_88200 |
4034 SNDRV_PCM_RATE_96000),
4035 .rate_min = 32000,
4036 .rate_max = 96000,
4037 .channels_min = 14,
4038 .channels_max = HDSP_MAX_CHANNELS,
4039 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4040 .period_bytes_min = (64 * 4) * 10,
4041 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4042 .periods_min = 2,
4043 .periods_max = 2,
4044 .fifo_size = 0
4045};
4046
4047static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4048
Takashi Iwai55e957d2005-11-17 14:52:13 +01004049static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 .count = ARRAY_SIZE(hdsp_period_sizes),
4051 .list = hdsp_period_sizes,
4052 .mask = 0
4053};
4054
4055static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4056
Takashi Iwai55e957d2005-11-17 14:52:13 +01004057static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4059 .list = hdsp_9632_sample_rates,
4060 .mask = 0
4061};
4062
Takashi Iwai55e957d2005-11-17 14:52:13 +01004063static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4064 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004066 struct hdsp *hdsp = rule->private;
4067 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (hdsp->io_type == H9632) {
4069 unsigned int list[3];
4070 list[0] = hdsp->qs_in_channels;
4071 list[1] = hdsp->ds_in_channels;
4072 list[2] = hdsp->ss_in_channels;
4073 return snd_interval_list(c, 3, list, 0);
4074 } else {
4075 unsigned int list[2];
4076 list[0] = hdsp->ds_in_channels;
4077 list[1] = hdsp->ss_in_channels;
4078 return snd_interval_list(c, 2, list, 0);
4079 }
4080}
4081
Takashi Iwai55e957d2005-11-17 14:52:13 +01004082static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4083 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004086 struct hdsp *hdsp = rule->private;
4087 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 if (hdsp->io_type == H9632) {
4089 list[0] = hdsp->qs_out_channels;
4090 list[1] = hdsp->ds_out_channels;
4091 list[2] = hdsp->ss_out_channels;
4092 return snd_interval_list(c, 3, list, 0);
4093 } else {
4094 list[0] = hdsp->ds_out_channels;
4095 list[1] = hdsp->ss_out_channels;
4096 }
4097 return snd_interval_list(c, 2, list, 0);
4098}
4099
Takashi Iwai55e957d2005-11-17 14:52:13 +01004100static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4101 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004103 struct hdsp *hdsp = rule->private;
4104 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4105 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004107 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 .min = hdsp->qs_in_channels,
4109 .max = hdsp->qs_in_channels,
4110 .integer = 1,
4111 };
4112 return snd_interval_refine(c, &t);
4113 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004114 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 .min = hdsp->ds_in_channels,
4116 .max = hdsp->ds_in_channels,
4117 .integer = 1,
4118 };
4119 return snd_interval_refine(c, &t);
4120 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004121 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 .min = hdsp->ss_in_channels,
4123 .max = hdsp->ss_in_channels,
4124 .integer = 1,
4125 };
4126 return snd_interval_refine(c, &t);
4127 }
4128 return 0;
4129}
4130
Takashi Iwai55e957d2005-11-17 14:52:13 +01004131static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4132 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004134 struct hdsp *hdsp = rule->private;
4135 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4136 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004138 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 .min = hdsp->qs_out_channels,
4140 .max = hdsp->qs_out_channels,
4141 .integer = 1,
4142 };
4143 return snd_interval_refine(c, &t);
4144 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004145 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 .min = hdsp->ds_out_channels,
4147 .max = hdsp->ds_out_channels,
4148 .integer = 1,
4149 };
4150 return snd_interval_refine(c, &t);
4151 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004152 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 .min = hdsp->ss_out_channels,
4154 .max = hdsp->ss_out_channels,
4155 .integer = 1,
4156 };
4157 return snd_interval_refine(c, &t);
4158 }
4159 return 0;
4160}
4161
Takashi Iwai55e957d2005-11-17 14:52:13 +01004162static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4163 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
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);
4167 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004169 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 .min = 32000,
4171 .max = 48000,
4172 .integer = 1,
4173 };
4174 return snd_interval_refine(r, &t);
4175 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004176 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 .min = 128000,
4178 .max = 192000,
4179 .integer = 1,
4180 };
4181 return snd_interval_refine(r, &t);
4182 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004183 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 .min = 64000,
4185 .max = 96000,
4186 .integer = 1,
4187 };
4188 return snd_interval_refine(r, &t);
4189 }
4190 return 0;
4191}
4192
Takashi Iwai55e957d2005-11-17 14:52:13 +01004193static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4194 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004196 struct hdsp *hdsp = rule->private;
4197 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4198 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004200 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 .min = 32000,
4202 .max = 48000,
4203 .integer = 1,
4204 };
4205 return snd_interval_refine(r, &t);
4206 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004207 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 .min = 128000,
4209 .max = 192000,
4210 .integer = 1,
4211 };
4212 return snd_interval_refine(r, &t);
4213 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004214 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 .min = 64000,
4216 .max = 96000,
4217 .integer = 1,
4218 };
4219 return snd_interval_refine(r, &t);
4220 }
4221 return 0;
4222}
4223
Takashi Iwai55e957d2005-11-17 14:52:13 +01004224static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004226 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4227 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Takashi Iwaib0b98112005-10-20 18:29:58 +02004229 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Takashi Iwaib0b98112005-10-20 18:29:58 +02004232 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235 spin_lock_irq(&hdsp->lock);
4236
4237 snd_pcm_set_sync(substream);
4238
4239 runtime->hw = snd_hdsp_playback_subinfo;
4240 runtime->dma_area = hdsp->playback_buffer;
4241 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4242
4243 hdsp->playback_pid = current->pid;
4244 hdsp->playback_substream = substream;
4245
4246 spin_unlock_irq(&hdsp->lock);
4247
4248 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4249 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 +02004250 if (hdsp->clock_source_locked) {
4251 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4252 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 runtime->hw.rate_max = 192000;
4254 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4255 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4256 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004257 if (hdsp->io_type == H9632) {
4258 runtime->hw.channels_min = hdsp->qs_out_channels;
4259 runtime->hw.channels_max = hdsp->ss_out_channels;
4260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4263 snd_hdsp_hw_rule_out_channels, hdsp,
4264 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4265 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4266 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4267 SNDRV_PCM_HW_PARAM_RATE, -1);
4268 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4269 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4270 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4271
4272 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4273 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4274 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4275 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4276 return 0;
4277}
4278
Takashi Iwai55e957d2005-11-17 14:52:13 +01004279static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004281 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283 spin_lock_irq(&hdsp->lock);
4284
4285 hdsp->playback_pid = -1;
4286 hdsp->playback_substream = NULL;
4287
4288 spin_unlock_irq(&hdsp->lock);
4289
4290 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4291 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4292 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4293 return 0;
4294}
4295
4296
Takashi Iwai55e957d2005-11-17 14:52:13 +01004297static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004299 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4300 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Takashi Iwaib0b98112005-10-20 18:29:58 +02004302 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Takashi Iwaib0b98112005-10-20 18:29:58 +02004305 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 spin_lock_irq(&hdsp->lock);
4309
4310 snd_pcm_set_sync(substream);
4311
4312 runtime->hw = snd_hdsp_capture_subinfo;
4313 runtime->dma_area = hdsp->capture_buffer;
4314 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4315
4316 hdsp->capture_pid = current->pid;
4317 hdsp->capture_substream = substream;
4318
4319 spin_unlock_irq(&hdsp->lock);
4320
4321 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4322 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4323 if (hdsp->io_type == H9632) {
4324 runtime->hw.channels_min = hdsp->qs_in_channels;
4325 runtime->hw.channels_max = hdsp->ss_in_channels;
4326 runtime->hw.rate_max = 192000;
4327 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4328 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4329 }
4330 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4331 snd_hdsp_hw_rule_in_channels, hdsp,
4332 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4333 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4334 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4335 SNDRV_PCM_HW_PARAM_RATE, -1);
4336 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4337 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4338 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4339 return 0;
4340}
4341
Takashi Iwai55e957d2005-11-17 14:52:13 +01004342static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004344 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346 spin_lock_irq(&hdsp->lock);
4347
4348 hdsp->capture_pid = -1;
4349 hdsp->capture_substream = NULL;
4350
4351 spin_unlock_irq(&hdsp->lock);
4352 return 0;
4353}
4354
Takashi Iwai55e957d2005-11-17 14:52:13 +01004355static int snd_hdsp_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
4357 /* we have nothing to initialize but the call is required */
4358 return 0;
4359}
4360
4361
4362/* helper functions for copying meter values */
4363static inline int copy_u32_le(void __user *dest, void __iomem *src)
4364{
4365 u32 val = readl(src);
4366 return copy_to_user(dest, &val, 4);
4367}
4368
4369static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4370{
4371 u32 rms_low, rms_high;
4372 u64 rms;
4373 rms_low = readl(src_low);
4374 rms_high = readl(src_high);
4375 rms = ((u64)rms_high << 32) | rms_low;
4376 return copy_to_user(dest, &rms, 8);
4377}
4378
4379static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4380{
4381 u32 rms_low, rms_high;
4382 u64 rms;
4383 rms_low = readl(src_low) & 0xffffff00;
4384 rms_high = readl(src_high) & 0xffffff00;
4385 rms = ((u64)rms_high << 32) | rms_low;
4386 return copy_to_user(dest, &rms, 8);
4387}
4388
Takashi Iwai55e957d2005-11-17 14:52:13 +01004389static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 int doublespeed = 0;
4392 int i, j, channels, ofs;
4393
4394 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4395 doublespeed = 1;
4396 channels = doublespeed ? 14 : 26;
4397 for (i = 0, j = 0; i < 26; ++i) {
4398 if (doublespeed && (i & 4))
4399 continue;
4400 ofs = HDSP_9652_peakBase - j * 4;
4401 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4402 return -EFAULT;
4403 ofs -= channels * 4;
4404 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4405 return -EFAULT;
4406 ofs -= channels * 4;
4407 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4408 return -EFAULT;
4409 ofs = HDSP_9652_rmsBase + j * 8;
4410 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4411 hdsp->iobase + ofs + 4))
4412 return -EFAULT;
4413 ofs += channels * 8;
4414 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4415 hdsp->iobase + ofs + 4))
4416 return -EFAULT;
4417 ofs += channels * 8;
4418 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4419 hdsp->iobase + ofs + 4))
4420 return -EFAULT;
4421 j++;
4422 }
4423 return 0;
4424}
4425
Takashi Iwai55e957d2005-11-17 14:52:13 +01004426static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
4428 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004429 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 int doublespeed = 0;
4431
4432 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4433 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004434 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 for (i = 0, j = 0; i < 16; ++i, ++j) {
4436 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4437 return -EFAULT;
4438 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4439 return -EFAULT;
4440 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4441 return -EFAULT;
4442 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4443 &m->input_rms_high[j]))
4444 return -EFAULT;
4445 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4446 &m->playback_rms_high[j]))
4447 return -EFAULT;
4448 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4449 &m->output_rms_high[j]))
4450 return -EFAULT;
4451 if (doublespeed && i == 3) i += 4;
4452 }
4453 return 0;
4454}
4455
Takashi Iwai55e957d2005-11-17 14:52:13 +01004456static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
4458 int i;
4459
4460 for (i = 0; i < 26; i++) {
4461 if (copy_u32_le(&peak_rms->playback_peaks[i],
4462 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4463 return -EFAULT;
4464 if (copy_u32_le(&peak_rms->input_peaks[i],
4465 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4466 return -EFAULT;
4467 }
4468 for (i = 0; i < 28; i++) {
4469 if (copy_u32_le(&peak_rms->output_peaks[i],
4470 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4471 return -EFAULT;
4472 }
4473 for (i = 0; i < 26; ++i) {
4474 if (copy_u64_le(&peak_rms->playback_rms[i],
4475 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4476 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4477 return -EFAULT;
4478 if (copy_u64_le(&peak_rms->input_rms[i],
4479 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4480 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4481 return -EFAULT;
4482 }
4483 return 0;
4484}
4485
Takashi Iwai55e957d2005-11-17 14:52:13 +01004486static 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 -07004487{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004488 struct hdsp *hdsp = (struct hdsp *)hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 void __user *argp = (void __user *)arg;
4490
4491 switch (cmd) {
4492 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004493 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
4495 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4496 snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
4497 return -EINVAL;
4498 }
4499
4500 switch (hdsp->io_type) {
4501 case H9652:
4502 return hdsp_9652_get_peak(hdsp, peak_rms);
4503 case H9632:
4504 return hdsp_9632_get_peak(hdsp, peak_rms);
4505 default:
4506 return hdsp_get_peak(hdsp, peak_rms);
4507 }
4508 }
4509 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004510 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 unsigned long flags;
4512 int i;
4513
4514 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004515 snd_printk(KERN_ERR "Hammerfall-DSP: Firmware needs to be uploaded to the card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return -EINVAL;
4517 }
4518 spin_lock_irqsave(&hdsp->lock, flags);
4519 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4520 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004521 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004524 for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
4527 info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
4528 info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
4529 info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
4530 info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
4531 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4532 info.system_sample_rate = hdsp->system_sample_rate;
4533 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4534 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4535 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4536 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
4537 info.line_out = (unsigned char)hdsp_line_out(hdsp);
4538 if (hdsp->io_type == H9632) {
4539 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4540 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4541 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
4542 info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
4543
4544 }
Takashi Iwaib0b98112005-10-20 18:29:58 +02004545 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 spin_unlock_irqrestore(&hdsp->lock, flags);
4548 if (copy_to_user(argp, &info, sizeof(info)))
4549 return -EFAULT;
4550 break;
4551 }
4552 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004553 struct hdsp_9632_aeb h9632_aeb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
4555 if (hdsp->io_type != H9632) return -EINVAL;
4556 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4557 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4558 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4559 return -EFAULT;
4560 break;
4561 }
4562 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004563 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 int err;
4565
4566 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4567 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004568 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 }
4571 hdsp_version.io_type = hdsp->io_type;
4572 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004573 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 break;
4576 }
4577 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004578 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 u32 __user *firmware_data;
4580 int err;
4581
4582 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4583 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4584 if (hdsp->io_type == Undefined) return -EINVAL;
4585
4586 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4587 return -EBUSY;
4588
Takashi Iwaib0b98112005-10-20 18:29:58 +02004589 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01004590 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Takashi Iwaib0b98112005-10-20 18:29:58 +02004592 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Takashi Iwaib0b98112005-10-20 18:29:58 +02004595 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Takashi Iwaib0b98112005-10-20 18:29:58 +02004598 if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601 hdsp->state |= HDSP_FirmwareCached;
4602
Takashi Iwaib0b98112005-10-20 18:29:58 +02004603 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004607 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
4610 snd_hdsp_initialize_channels(hdsp);
4611 snd_hdsp_initialize_midi_flush(hdsp);
4612
4613 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004614 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
4615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
4617 }
4618 break;
4619 }
4620 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004621 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4623 return -EFAULT;
4624 break;
4625 }
4626 default:
4627 return -EINVAL;
4628 }
4629 return 0;
4630}
4631
Takashi Iwai55e957d2005-11-17 14:52:13 +01004632static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 .open = snd_hdsp_playback_open,
4634 .close = snd_hdsp_playback_release,
4635 .ioctl = snd_hdsp_ioctl,
4636 .hw_params = snd_hdsp_hw_params,
4637 .prepare = snd_hdsp_prepare,
4638 .trigger = snd_hdsp_trigger,
4639 .pointer = snd_hdsp_hw_pointer,
4640 .copy = snd_hdsp_playback_copy,
4641 .silence = snd_hdsp_hw_silence,
4642};
4643
Takashi Iwai55e957d2005-11-17 14:52:13 +01004644static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 .open = snd_hdsp_capture_open,
4646 .close = snd_hdsp_capture_release,
4647 .ioctl = snd_hdsp_ioctl,
4648 .hw_params = snd_hdsp_hw_params,
4649 .prepare = snd_hdsp_prepare,
4650 .trigger = snd_hdsp_trigger,
4651 .pointer = snd_hdsp_hw_pointer,
4652 .copy = snd_hdsp_capture_copy,
4653};
4654
Takashi Iwai55e957d2005-11-17 14:52:13 +01004655static int __devinit snd_hdsp_create_hwdep(struct snd_card *card,
4656 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004658 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 int err;
4660
4661 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4662 return err;
4663
4664 hdsp->hwdep = hw;
4665 hw->private_data = hdsp;
4666 strcpy(hw->name, "HDSP hwdep interface");
4667
4668 hw->ops.open = snd_hdsp_hwdep_dummy_op;
4669 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
4670 hw->ops.release = snd_hdsp_hwdep_dummy_op;
4671
4672 return 0;
4673}
4674
Takashi Iwai55e957d2005-11-17 14:52:13 +01004675static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004677 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 int err;
4679
4680 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4681 return err;
4682
4683 hdsp->pcm = pcm;
4684 pcm->private_data = hdsp;
4685 strcpy(pcm->name, hdsp->card_name);
4686
4687 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4688 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4689
4690 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4691
4692 return 0;
4693}
4694
Takashi Iwai55e957d2005-11-17 14:52:13 +01004695static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
4697 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4698 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4699}
4700
Takashi Iwai55e957d2005-11-17 14:52:13 +01004701static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
4703 int i;
4704
4705 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004706 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return -EIO;
4708 }
4709
4710 for (i = 0; i < hdsp->max_channels; ++i) {
4711 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4712 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4713 }
4714
4715 return 0;
4716}
4717
Takashi Iwai55e957d2005-11-17 14:52:13 +01004718static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
4720 int status, aebi_channels, aebo_channels;
4721
4722 switch (hdsp->io_type) {
4723 case Digiface:
4724 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4725 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4726 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4727 break;
4728
4729 case H9652:
4730 hdsp->card_name = "RME Hammerfall HDSP 9652";
4731 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4732 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4733 break;
4734
4735 case H9632:
4736 status = hdsp_read(hdsp, HDSP_statusRegister);
4737 /* HDSP_AEBx bits are low when AEB are connected */
4738 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4739 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4740 hdsp->card_name = "RME Hammerfall HDSP 9632";
4741 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4742 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4743 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4744 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4745 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4746 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4747 break;
4748
4749 case Multiface:
4750 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4751 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4752 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4753 break;
4754
4755 default:
4756 /* should never get here */
4757 break;
4758 }
4759}
4760
Takashi Iwai55e957d2005-11-17 14:52:13 +01004761static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
4763 snd_hdsp_flush_midi_input (hdsp, 0);
4764 snd_hdsp_flush_midi_input (hdsp, 1);
4765}
4766
Takashi Iwai55e957d2005-11-17 14:52:13 +01004767static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
4769 int err;
4770
4771 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004772 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 return err;
4774 }
4775
4776
4777 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004778 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 return err;
4780 }
4781
4782 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
4783 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004784 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return err;
4786 }
4787 }
4788
4789 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004790 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return err;
4792 }
4793
4794 snd_hdsp_proc_init(hdsp);
4795
4796 hdsp->system_sample_rate = -1;
4797 hdsp->playback_pid = -1;
4798 hdsp->capture_pid = -1;
4799 hdsp->capture_substream = NULL;
4800 hdsp->playback_substream = NULL;
4801
4802 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004803 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return err;
4805 }
4806
4807 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02004808 strcpy(card->shortname, "Hammerfall DSP");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
4810 hdsp->port, hdsp->irq);
4811
4812 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004813 snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 return err;
4815 }
4816 hdsp->state |= HDSP_InitializationComplete;
4817 }
4818
4819 return 0;
4820}
4821
4822#ifdef HDSP_FW_LOADER
4823/* load firmware via hotplug fw loader */
Takashi Iwai55e957d2005-11-17 14:52:13 +01004824static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
4826 const char *fwfile;
4827 const struct firmware *fw;
4828 int err;
4829
4830 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4831 return 0;
4832 if (hdsp->io_type == Undefined) {
4833 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
4834 return err;
4835 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4836 return 0;
4837 }
4838
4839 /* caution: max length of firmware filename is 30! */
4840 switch (hdsp->io_type) {
4841 case Multiface:
4842 if (hdsp->firmware_rev == 0xa)
4843 fwfile = "multiface_firmware.bin";
4844 else
4845 fwfile = "multiface_firmware_rev11.bin";
4846 break;
4847 case Digiface:
4848 if (hdsp->firmware_rev == 0xa)
4849 fwfile = "digiface_firmware.bin";
4850 else
4851 fwfile = "digiface_firmware_rev11.bin";
4852 break;
4853 default:
4854 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
4855 return -EINVAL;
4856 }
4857
4858 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
4859 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
4860 return -ENOENT;
4861 }
4862 if (fw->size < sizeof(hdsp->firmware_cache)) {
4863 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
4864 (int)fw->size, (int)sizeof(hdsp->firmware_cache));
4865 release_firmware(fw);
4866 return -EINVAL;
4867 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02004868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
Thomas Charbonnel7679a032005-04-25 11:35:29 +02004870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 release_firmware(fw);
4872
4873 hdsp->state |= HDSP_FirmwareCached;
4874
4875 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
4876 return err;
4877
4878 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004879 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004883 snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 return err;
4885 }
4886 snd_hdsp_initialize_channels(hdsp);
4887 snd_hdsp_initialize_midi_flush(hdsp);
4888 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004889 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return err;
4891 }
4892 }
4893 return 0;
4894}
4895#endif
4896
Takashi Iwai55e957d2005-11-17 14:52:13 +01004897static int __devinit snd_hdsp_create(struct snd_card *card,
4898 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899{
4900 struct pci_dev *pci = hdsp->pci;
4901 int err;
4902 int is_9652 = 0;
4903 int is_9632 = 0;
4904
4905 hdsp->irq = -1;
4906 hdsp->state = 0;
4907 hdsp->midi[0].rmidi = NULL;
4908 hdsp->midi[1].rmidi = NULL;
4909 hdsp->midi[0].input = NULL;
4910 hdsp->midi[1].input = NULL;
4911 hdsp->midi[0].output = NULL;
4912 hdsp->midi[1].output = NULL;
4913 hdsp->midi[0].pending = 0;
4914 hdsp->midi[1].pending = 0;
4915 spin_lock_init(&hdsp->midi[0].lock);
4916 spin_lock_init(&hdsp->midi[1].lock);
4917 hdsp->iobase = NULL;
4918 hdsp->control_register = 0;
4919 hdsp->control2_register = 0;
4920 hdsp->io_type = Undefined;
4921 hdsp->max_channels = 26;
4922
4923 hdsp->card = card;
4924
4925 spin_lock_init(&hdsp->lock);
4926
4927 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
4928
4929 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
4930 hdsp->firmware_rev &= 0xff;
4931
4932 /* From Martin Bjoernsen :
4933 "It is important that the card's latency timer register in
4934 the PCI configuration space is set to a value much larger
4935 than 0 by the computer's BIOS or the driver.
4936 The windows driver always sets this 8 bit register [...]
4937 to its maximum 255 to avoid problems with some computers."
4938 */
4939 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
4940
4941 strcpy(card->driver, "H-DSP");
4942 strcpy(card->mixername, "Xilinx FPGA");
4943
Takashi Iwaib0b98112005-10-20 18:29:58 +02004944 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return -ENODEV;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004946 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b98112005-10-20 18:29:58 +02004948 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 hdsp->card_name = "RME HDSP 9652";
4950 is_9652 = 1;
4951 } else {
4952 hdsp->card_name = "RME HDSP 9632";
4953 hdsp->max_channels = 16;
4954 is_9632 = 1;
4955 }
4956
Takashi Iwaib0b98112005-10-20 18:29:58 +02004957 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960 pci_set_master(hdsp->pci);
4961
4962 if ((err = pci_request_regions(pci, "hdsp")) < 0)
4963 return err;
4964 hdsp->port = pci_resource_start(pci, 0);
4965 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004966 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 -07004967 return -EBUSY;
4968 }
4969
Takashi Iwai437a5a42006-11-21 12:14:23 +01004970 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
4971 "hdsp", hdsp)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004972 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 return -EBUSY;
4974 }
4975
4976 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02004977 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02004979 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Takashi Iwaib0b98112005-10-20 18:29:58 +02004981 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 if (!is_9652 && !is_9632) {
4985 /* we wait 2 seconds to let freshly inserted cardbus cards do their hardware init */
Takashi Iwaib0b98112005-10-20 18:29:58 +02004986 ssleep(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
4989#ifdef HDSP_FW_LOADER
Takashi Iwaib0b98112005-10-20 18:29:58 +02004990 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 /* we don't fail as this can happen
4992 if userspace is not ready for
4993 firmware upload
4994 */
Takashi Iwaib0b98112005-10-20 18:29:58 +02004995 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
4996 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 /* init is complete, we return */
4998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999#endif
5000 /* no iobox connected, we defer initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005001 snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5002 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 return 0;
5005 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005006 snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
5007 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005009 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
5012 }
5013
Takashi Iwaib0b98112005-10-20 18:29:58 +02005014 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Takashi Iwaib0b98112005-10-20 18:29:58 +02005017 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 hdsp->io_type = H9652;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Takashi Iwaib0b98112005-10-20 18:29:58 +02005020 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Takashi Iwaib0b98112005-10-20 18:29:58 +02005023 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 snd_hdsp_initialize_channels(hdsp);
5027 snd_hdsp_initialize_midi_flush(hdsp);
5028
5029 hdsp->state |= HDSP_FirmwareLoaded;
5030
Takashi Iwaib0b98112005-10-20 18:29:58 +02005031 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
5034 return 0;
5035}
5036
Takashi Iwai55e957d2005-11-17 14:52:13 +01005037static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038{
5039 if (hdsp->port) {
5040 /* stop the audio, and cancel all interrupts */
5041 tasklet_kill(&hdsp->midi_tasklet);
5042 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5043 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5044 }
5045
5046 if (hdsp->irq >= 0)
5047 free_irq(hdsp->irq, (void *)hdsp);
5048
5049 snd_hdsp_free_buffers(hdsp);
5050
5051 if (hdsp->iobase)
5052 iounmap(hdsp->iobase);
5053
5054 if (hdsp->port)
5055 pci_release_regions(hdsp->pci);
5056
5057 pci_disable_device(hdsp->pci);
5058 return 0;
5059}
5060
Takashi Iwai55e957d2005-11-17 14:52:13 +01005061static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005063 struct hdsp *hdsp = (struct hdsp *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 if (hdsp)
5066 snd_hdsp_free(hdsp);
5067}
5068
5069static int __devinit snd_hdsp_probe(struct pci_dev *pci,
5070 const struct pci_device_id *pci_id)
5071{
5072 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005073 struct hdsp *hdsp;
5074 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 int err;
5076
5077 if (dev >= SNDRV_CARDS)
5078 return -ENODEV;
5079 if (!enable[dev]) {
5080 dev++;
5081 return -ENOENT;
5082 }
5083
Takashi Iwai55e957d2005-11-17 14:52:13 +01005084 if (!(card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return -ENOMEM;
5086
Takashi Iwai55e957d2005-11-17 14:52:13 +01005087 hdsp = (struct hdsp *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 card->private_free = snd_hdsp_card_free;
5089 hdsp->dev = dev;
5090 hdsp->pci = pci;
5091 snd_card_set_dev(card, &pci->dev);
5092
5093 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5094 snd_card_free(card);
5095 return err;
5096 }
5097
5098 strcpy(card->shortname, "Hammerfall DSP");
5099 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
5100 hdsp->port, hdsp->irq);
5101
5102 if ((err = snd_card_register(card)) < 0) {
5103 snd_card_free(card);
5104 return err;
5105 }
5106 pci_set_drvdata(pci, card);
5107 dev++;
5108 return 0;
5109}
5110
5111static void __devexit snd_hdsp_remove(struct pci_dev *pci)
5112{
5113 snd_card_free(pci_get_drvdata(pci));
5114 pci_set_drvdata(pci, NULL);
5115}
5116
5117static struct pci_driver driver = {
5118 .name = "RME Hammerfall DSP",
5119 .id_table = snd_hdsp_ids,
5120 .probe = snd_hdsp_probe,
5121 .remove = __devexit_p(snd_hdsp_remove),
5122};
5123
5124static int __init alsa_card_hdsp_init(void)
5125{
Takashi Iwai01d25d42005-04-11 16:58:24 +02005126 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127}
5128
5129static void __exit alsa_card_hdsp_exit(void)
5130{
5131 pci_unregister_driver(&driver);
5132}
5133
5134module_init(alsa_card_hdsp_init)
5135module_exit(alsa_card_hdsp_exit)